lpfc_sli.c 490 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static IOCB_t *
  65. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  66. {
  67. return &iocbq->iocb;
  68. }
  69. /**
  70. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  71. * @q: The Work Queue to operate on.
  72. * @wqe: The work Queue Entry to put on the Work queue.
  73. *
  74. * This routine will copy the contents of @wqe to the next available entry on
  75. * the @q. This function will then ring the Work Queue Doorbell to signal the
  76. * HBA to start processing the Work Queue Entry. This function returns 0 if
  77. * successful. If no entries are available on @q then this function will return
  78. * -ENOMEM.
  79. * The caller is expected to hold the hbalock when calling this routine.
  80. **/
  81. static uint32_t
  82. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  83. {
  84. union lpfc_wqe *temp_wqe;
  85. struct lpfc_register doorbell;
  86. uint32_t host_index;
  87. /* sanity check on queue memory */
  88. if (unlikely(!q))
  89. return -ENOMEM;
  90. temp_wqe = q->qe[q->host_index].wqe;
  91. /* If the host has not yet processed the next entry then we are done */
  92. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  93. return -ENOMEM;
  94. /* set consumption flag every once in a while */
  95. if (!((q->host_index + 1) % q->entry_repost))
  96. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  97. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  98. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  99. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  100. /* Update the host index before invoking device */
  101. host_index = q->host_index;
  102. q->host_index = ((q->host_index + 1) % q->entry_count);
  103. /* Ring Doorbell */
  104. doorbell.word0 = 0;
  105. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  106. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  107. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  108. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  109. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  110. return 0;
  111. }
  112. /**
  113. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  114. * @q: The Work Queue to operate on.
  115. * @index: The index to advance the hba index to.
  116. *
  117. * This routine will update the HBA index of a queue to reflect consumption of
  118. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  119. * an entry the host calls this function to update the queue's internal
  120. * pointers. This routine returns the number of entries that were consumed by
  121. * the HBA.
  122. **/
  123. static uint32_t
  124. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  125. {
  126. uint32_t released = 0;
  127. /* sanity check on queue memory */
  128. if (unlikely(!q))
  129. return 0;
  130. if (q->hba_index == index)
  131. return 0;
  132. do {
  133. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  134. released++;
  135. } while (q->hba_index != index);
  136. return released;
  137. }
  138. /**
  139. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  140. * @q: The Mailbox Queue to operate on.
  141. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  142. *
  143. * This routine will copy the contents of @mqe to the next available entry on
  144. * the @q. This function will then ring the Work Queue Doorbell to signal the
  145. * HBA to start processing the Work Queue Entry. This function returns 0 if
  146. * successful. If no entries are available on @q then this function will return
  147. * -ENOMEM.
  148. * The caller is expected to hold the hbalock when calling this routine.
  149. **/
  150. static uint32_t
  151. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  152. {
  153. struct lpfc_mqe *temp_mqe;
  154. struct lpfc_register doorbell;
  155. uint32_t host_index;
  156. /* sanity check on queue memory */
  157. if (unlikely(!q))
  158. return -ENOMEM;
  159. temp_mqe = q->qe[q->host_index].mqe;
  160. /* If the host has not yet processed the next entry then we are done */
  161. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  162. return -ENOMEM;
  163. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  164. /* Save off the mailbox pointer for completion */
  165. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  166. /* Update the host index before invoking device */
  167. host_index = q->host_index;
  168. q->host_index = ((q->host_index + 1) % q->entry_count);
  169. /* Ring Doorbell */
  170. doorbell.word0 = 0;
  171. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  172. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  173. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  174. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  175. return 0;
  176. }
  177. /**
  178. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  179. * @q: The Mailbox Queue to operate on.
  180. *
  181. * This routine will update the HBA index of a queue to reflect consumption of
  182. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  183. * an entry the host calls this function to update the queue's internal
  184. * pointers. This routine returns the number of entries that were consumed by
  185. * the HBA.
  186. **/
  187. static uint32_t
  188. lpfc_sli4_mq_release(struct lpfc_queue *q)
  189. {
  190. /* sanity check on queue memory */
  191. if (unlikely(!q))
  192. return 0;
  193. /* Clear the mailbox pointer for completion */
  194. q->phba->mbox = NULL;
  195. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  196. return 1;
  197. }
  198. /**
  199. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  200. * @q: The Event Queue to get the first valid EQE from
  201. *
  202. * This routine will get the first valid Event Queue Entry from @q, update
  203. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  204. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  205. * processed, but not popped back to the HBA then this routine will return NULL.
  206. **/
  207. static struct lpfc_eqe *
  208. lpfc_sli4_eq_get(struct lpfc_queue *q)
  209. {
  210. struct lpfc_eqe *eqe;
  211. /* sanity check on queue memory */
  212. if (unlikely(!q))
  213. return NULL;
  214. eqe = q->qe[q->hba_index].eqe;
  215. /* If the next EQE is not valid then we are done */
  216. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  217. return NULL;
  218. /* If the host has not yet processed the next entry then we are done */
  219. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  220. return NULL;
  221. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  222. return eqe;
  223. }
  224. /**
  225. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  226. * @q: The Event Queue that the host has completed processing for.
  227. * @arm: Indicates whether the host wants to arms this CQ.
  228. *
  229. * This routine will mark all Event Queue Entries on @q, from the last
  230. * known completed entry to the last entry that was processed, as completed
  231. * by clearing the valid bit for each completion queue entry. Then it will
  232. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  233. * The internal host index in the @q will be updated by this routine to indicate
  234. * that the host has finished processing the entries. The @arm parameter
  235. * indicates that the queue should be rearmed when ringing the doorbell.
  236. *
  237. * This function will return the number of EQEs that were popped.
  238. **/
  239. uint32_t
  240. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  241. {
  242. uint32_t released = 0;
  243. struct lpfc_eqe *temp_eqe;
  244. struct lpfc_register doorbell;
  245. /* sanity check on queue memory */
  246. if (unlikely(!q))
  247. return 0;
  248. /* while there are valid entries */
  249. while (q->hba_index != q->host_index) {
  250. temp_eqe = q->qe[q->host_index].eqe;
  251. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  252. released++;
  253. q->host_index = ((q->host_index + 1) % q->entry_count);
  254. }
  255. if (unlikely(released == 0 && !arm))
  256. return 0;
  257. /* ring doorbell for number popped */
  258. doorbell.word0 = 0;
  259. if (arm) {
  260. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  261. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  262. }
  263. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  264. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  265. bf_set(lpfc_eqcq_doorbell_eqid_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. lpfc_sli4_node_prep(phba);
  5067. /* VPIs. */
  5068. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5069. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5070. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5071. phba->vpi_bmask = kzalloc(longs *
  5072. sizeof(unsigned long),
  5073. GFP_KERNEL);
  5074. if (unlikely(!phba->vpi_bmask)) {
  5075. rc = -ENOMEM;
  5076. goto free_rpi_ids;
  5077. }
  5078. phba->vpi_ids = kzalloc(count *
  5079. sizeof(uint16_t),
  5080. GFP_KERNEL);
  5081. if (unlikely(!phba->vpi_ids)) {
  5082. rc = -ENOMEM;
  5083. goto free_vpi_bmask;
  5084. }
  5085. for (i = 0; i < count; i++)
  5086. phba->vpi_ids[i] = base + i;
  5087. /* XRIs. */
  5088. count = phba->sli4_hba.max_cfg_param.max_xri;
  5089. base = phba->sli4_hba.max_cfg_param.xri_base;
  5090. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5091. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5092. sizeof(unsigned long),
  5093. GFP_KERNEL);
  5094. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5095. rc = -ENOMEM;
  5096. goto free_vpi_ids;
  5097. }
  5098. phba->sli4_hba.xri_ids = kzalloc(count *
  5099. sizeof(uint16_t),
  5100. GFP_KERNEL);
  5101. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5102. rc = -ENOMEM;
  5103. goto free_xri_bmask;
  5104. }
  5105. for (i = 0; i < count; i++)
  5106. phba->sli4_hba.xri_ids[i] = base + i;
  5107. /* VFIs. */
  5108. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5109. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5110. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5111. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5112. sizeof(unsigned long),
  5113. GFP_KERNEL);
  5114. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5115. rc = -ENOMEM;
  5116. goto free_xri_ids;
  5117. }
  5118. phba->sli4_hba.vfi_ids = kzalloc(count *
  5119. sizeof(uint16_t),
  5120. GFP_KERNEL);
  5121. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5122. rc = -ENOMEM;
  5123. goto free_vfi_bmask;
  5124. }
  5125. for (i = 0; i < count; i++)
  5126. phba->sli4_hba.vfi_ids[i] = base + i;
  5127. /*
  5128. * Mark all resources ready. An HBA reset doesn't need
  5129. * to reset the initialization.
  5130. */
  5131. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5132. LPFC_IDX_RSRC_RDY);
  5133. return 0;
  5134. }
  5135. free_vfi_bmask:
  5136. kfree(phba->sli4_hba.vfi_bmask);
  5137. free_xri_ids:
  5138. kfree(phba->sli4_hba.xri_ids);
  5139. free_xri_bmask:
  5140. kfree(phba->sli4_hba.xri_bmask);
  5141. free_vpi_ids:
  5142. kfree(phba->vpi_ids);
  5143. free_vpi_bmask:
  5144. kfree(phba->vpi_bmask);
  5145. free_rpi_ids:
  5146. kfree(phba->sli4_hba.rpi_ids);
  5147. free_rpi_bmask:
  5148. kfree(phba->sli4_hba.rpi_bmask);
  5149. err_exit:
  5150. return rc;
  5151. }
  5152. /**
  5153. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5154. * @phba: Pointer to HBA context object.
  5155. *
  5156. * This function allocates the number of elements for the specified
  5157. * resource type.
  5158. **/
  5159. int
  5160. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5161. {
  5162. if (phba->sli4_hba.extents_in_use) {
  5163. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5164. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5165. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5166. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5167. } else {
  5168. kfree(phba->vpi_bmask);
  5169. kfree(phba->vpi_ids);
  5170. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5171. kfree(phba->sli4_hba.xri_bmask);
  5172. kfree(phba->sli4_hba.xri_ids);
  5173. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5174. kfree(phba->sli4_hba.vfi_bmask);
  5175. kfree(phba->sli4_hba.vfi_ids);
  5176. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5177. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5178. }
  5179. return 0;
  5180. }
  5181. /**
  5182. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5183. * @phba: Pointer to HBA context object.
  5184. * @type: The resource extent type.
  5185. * @extnt_count: buffer to hold port extent count response
  5186. * @extnt_size: buffer to hold port extent size response.
  5187. *
  5188. * This function calls the port to read the host allocated extents
  5189. * for a particular type.
  5190. **/
  5191. int
  5192. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5193. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5194. {
  5195. bool emb;
  5196. int rc = 0;
  5197. uint16_t curr_blks = 0;
  5198. uint32_t req_len, emb_len;
  5199. uint32_t alloc_len, mbox_tmo;
  5200. struct list_head *blk_list_head;
  5201. struct lpfc_rsrc_blks *rsrc_blk;
  5202. LPFC_MBOXQ_t *mbox;
  5203. void *virtaddr = NULL;
  5204. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5205. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5206. union lpfc_sli4_cfg_shdr *shdr;
  5207. switch (type) {
  5208. case LPFC_RSC_TYPE_FCOE_VPI:
  5209. blk_list_head = &phba->lpfc_vpi_blk_list;
  5210. break;
  5211. case LPFC_RSC_TYPE_FCOE_XRI:
  5212. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5213. break;
  5214. case LPFC_RSC_TYPE_FCOE_VFI:
  5215. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5216. break;
  5217. case LPFC_RSC_TYPE_FCOE_RPI:
  5218. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5219. break;
  5220. default:
  5221. return -EIO;
  5222. }
  5223. /* Count the number of extents currently allocatd for this type. */
  5224. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5225. if (curr_blks == 0) {
  5226. /*
  5227. * The GET_ALLOCATED mailbox does not return the size,
  5228. * just the count. The size should be just the size
  5229. * stored in the current allocated block and all sizes
  5230. * for an extent type are the same so set the return
  5231. * value now.
  5232. */
  5233. *extnt_size = rsrc_blk->rsrc_size;
  5234. }
  5235. curr_blks++;
  5236. }
  5237. /* Calculate the total requested length of the dma memory. */
  5238. req_len = curr_blks * sizeof(uint16_t);
  5239. /*
  5240. * Calculate the size of an embedded mailbox. The uint32_t
  5241. * accounts for extents-specific word.
  5242. */
  5243. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5244. sizeof(uint32_t);
  5245. /*
  5246. * Presume the allocation and response will fit into an embedded
  5247. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5248. */
  5249. emb = LPFC_SLI4_MBX_EMBED;
  5250. req_len = emb_len;
  5251. if (req_len > emb_len) {
  5252. req_len = curr_blks * sizeof(uint16_t) +
  5253. sizeof(union lpfc_sli4_cfg_shdr) +
  5254. sizeof(uint32_t);
  5255. emb = LPFC_SLI4_MBX_NEMBED;
  5256. }
  5257. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5258. if (!mbox)
  5259. return -ENOMEM;
  5260. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5261. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5262. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5263. req_len, emb);
  5264. if (alloc_len < req_len) {
  5265. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5266. "2983 Allocated DMA memory size (x%x) is "
  5267. "less than the requested DMA memory "
  5268. "size (x%x)\n", alloc_len, req_len);
  5269. rc = -ENOMEM;
  5270. goto err_exit;
  5271. }
  5272. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5273. if (unlikely(rc)) {
  5274. rc = -EIO;
  5275. goto err_exit;
  5276. }
  5277. if (!phba->sli4_hba.intr_enable)
  5278. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5279. else {
  5280. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5281. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5282. }
  5283. if (unlikely(rc)) {
  5284. rc = -EIO;
  5285. goto err_exit;
  5286. }
  5287. /*
  5288. * Figure out where the response is located. Then get local pointers
  5289. * to the response data. The port does not guarantee to respond to
  5290. * all extents counts request so update the local variable with the
  5291. * allocated count from the port.
  5292. */
  5293. if (emb == LPFC_SLI4_MBX_EMBED) {
  5294. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5295. shdr = &rsrc_ext->header.cfg_shdr;
  5296. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5297. } else {
  5298. virtaddr = mbox->sge_array->addr[0];
  5299. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5300. shdr = &n_rsrc->cfg_shdr;
  5301. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5302. }
  5303. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5304. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5305. "2984 Failed to read allocated resources "
  5306. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5307. type,
  5308. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5309. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5310. rc = -EIO;
  5311. goto err_exit;
  5312. }
  5313. err_exit:
  5314. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5315. return rc;
  5316. }
  5317. /**
  5318. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5319. * @phba: Pointer to HBA context object.
  5320. *
  5321. * This function is the main SLI4 device intialization PCI function. This
  5322. * function is called by the HBA intialization code, HBA reset code and
  5323. * HBA error attention handler code. Caller is not required to hold any
  5324. * locks.
  5325. **/
  5326. int
  5327. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5328. {
  5329. int rc;
  5330. LPFC_MBOXQ_t *mboxq;
  5331. struct lpfc_mqe *mqe;
  5332. uint8_t *vpd;
  5333. uint32_t vpd_size;
  5334. uint32_t ftr_rsp = 0;
  5335. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5336. struct lpfc_vport *vport = phba->pport;
  5337. struct lpfc_dmabuf *mp;
  5338. /* Perform a PCI function reset to start from clean */
  5339. rc = lpfc_pci_function_reset(phba);
  5340. if (unlikely(rc))
  5341. return -ENODEV;
  5342. /* Check the HBA Host Status Register for readyness */
  5343. rc = lpfc_sli4_post_status_check(phba);
  5344. if (unlikely(rc))
  5345. return -ENODEV;
  5346. else {
  5347. spin_lock_irq(&phba->hbalock);
  5348. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5349. spin_unlock_irq(&phba->hbalock);
  5350. }
  5351. /*
  5352. * Allocate a single mailbox container for initializing the
  5353. * port.
  5354. */
  5355. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5356. if (!mboxq)
  5357. return -ENOMEM;
  5358. /* Issue READ_REV to collect vpd and FW information. */
  5359. vpd_size = SLI4_PAGE_SIZE;
  5360. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5361. if (!vpd) {
  5362. rc = -ENOMEM;
  5363. goto out_free_mbox;
  5364. }
  5365. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5366. if (unlikely(rc)) {
  5367. kfree(vpd);
  5368. goto out_free_mbox;
  5369. }
  5370. mqe = &mboxq->u.mqe;
  5371. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5372. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5373. phba->hba_flag |= HBA_FCOE_MODE;
  5374. else
  5375. phba->hba_flag &= ~HBA_FCOE_MODE;
  5376. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5377. LPFC_DCBX_CEE_MODE)
  5378. phba->hba_flag |= HBA_FIP_SUPPORT;
  5379. else
  5380. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5381. if (phba->sli_rev != LPFC_SLI_REV4) {
  5382. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5383. "0376 READ_REV Error. SLI Level %d "
  5384. "FCoE enabled %d\n",
  5385. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5386. rc = -EIO;
  5387. kfree(vpd);
  5388. goto out_free_mbox;
  5389. }
  5390. /*
  5391. * Continue initialization with default values even if driver failed
  5392. * to read FCoE param config regions, only read parameters if the
  5393. * board is FCoE
  5394. */
  5395. if (phba->hba_flag & HBA_FCOE_MODE &&
  5396. lpfc_sli4_read_fcoe_params(phba))
  5397. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5398. "2570 Failed to read FCoE parameters\n");
  5399. /*
  5400. * Retrieve sli4 device physical port name, failure of doing it
  5401. * is considered as non-fatal.
  5402. */
  5403. rc = lpfc_sli4_retrieve_pport_name(phba);
  5404. if (!rc)
  5405. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5406. "3080 Successful retrieving SLI4 device "
  5407. "physical port name: %s.\n", phba->Port);
  5408. /*
  5409. * Evaluate the read rev and vpd data. Populate the driver
  5410. * state with the results. If this routine fails, the failure
  5411. * is not fatal as the driver will use generic values.
  5412. */
  5413. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5414. if (unlikely(!rc)) {
  5415. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5416. "0377 Error %d parsing vpd. "
  5417. "Using defaults.\n", rc);
  5418. rc = 0;
  5419. }
  5420. kfree(vpd);
  5421. /* Save information as VPD data */
  5422. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5423. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5424. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5425. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5426. &mqe->un.read_rev);
  5427. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5428. &mqe->un.read_rev);
  5429. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5430. &mqe->un.read_rev);
  5431. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5432. &mqe->un.read_rev);
  5433. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5434. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5435. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5436. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5437. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5438. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5439. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5440. "(%d):0380 READ_REV Status x%x "
  5441. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5442. mboxq->vport ? mboxq->vport->vpi : 0,
  5443. bf_get(lpfc_mqe_status, mqe),
  5444. phba->vpd.rev.opFwName,
  5445. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5446. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5447. /*
  5448. * Discover the port's supported feature set and match it against the
  5449. * hosts requests.
  5450. */
  5451. lpfc_request_features(phba, mboxq);
  5452. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5453. if (unlikely(rc)) {
  5454. rc = -EIO;
  5455. goto out_free_mbox;
  5456. }
  5457. /*
  5458. * The port must support FCP initiator mode as this is the
  5459. * only mode running in the host.
  5460. */
  5461. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5462. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5463. "0378 No support for fcpi mode.\n");
  5464. ftr_rsp++;
  5465. }
  5466. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5467. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5468. else
  5469. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5470. /*
  5471. * If the port cannot support the host's requested features
  5472. * then turn off the global config parameters to disable the
  5473. * feature in the driver. This is not a fatal error.
  5474. */
  5475. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5476. if (phba->cfg_enable_bg) {
  5477. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5478. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5479. else
  5480. ftr_rsp++;
  5481. }
  5482. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5483. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5484. ftr_rsp++;
  5485. if (ftr_rsp) {
  5486. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5487. "0379 Feature Mismatch Data: x%08x %08x "
  5488. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5489. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5490. phba->cfg_enable_npiv, phba->max_vpi);
  5491. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5492. phba->cfg_enable_bg = 0;
  5493. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5494. phba->cfg_enable_npiv = 0;
  5495. }
  5496. /* These SLI3 features are assumed in SLI4 */
  5497. spin_lock_irq(&phba->hbalock);
  5498. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5499. spin_unlock_irq(&phba->hbalock);
  5500. /*
  5501. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5502. * calls depends on these resources to complete port setup.
  5503. */
  5504. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5505. if (rc) {
  5506. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5507. "2920 Failed to alloc Resource IDs "
  5508. "rc = x%x\n", rc);
  5509. goto out_free_mbox;
  5510. }
  5511. /* update physical xri mappings in the scsi buffers */
  5512. lpfc_scsi_buf_update(phba);
  5513. /* Read the port's service parameters. */
  5514. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5515. if (rc) {
  5516. phba->link_state = LPFC_HBA_ERROR;
  5517. rc = -ENOMEM;
  5518. goto out_free_mbox;
  5519. }
  5520. mboxq->vport = vport;
  5521. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5522. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5523. if (rc == MBX_SUCCESS) {
  5524. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5525. rc = 0;
  5526. }
  5527. /*
  5528. * This memory was allocated by the lpfc_read_sparam routine. Release
  5529. * it to the mbuf pool.
  5530. */
  5531. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5532. kfree(mp);
  5533. mboxq->context1 = NULL;
  5534. if (unlikely(rc)) {
  5535. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5536. "0382 READ_SPARAM command failed "
  5537. "status %d, mbxStatus x%x\n",
  5538. rc, bf_get(lpfc_mqe_status, mqe));
  5539. phba->link_state = LPFC_HBA_ERROR;
  5540. rc = -EIO;
  5541. goto out_free_mbox;
  5542. }
  5543. lpfc_update_vport_wwn(vport);
  5544. /* Update the fc_host data structures with new wwn. */
  5545. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5546. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5547. /* Register SGL pool to the device using non-embedded mailbox command */
  5548. if (!phba->sli4_hba.extents_in_use) {
  5549. rc = lpfc_sli4_post_els_sgl_list(phba);
  5550. if (unlikely(rc)) {
  5551. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5552. "0582 Error %d during els sgl post "
  5553. "operation\n", rc);
  5554. rc = -ENODEV;
  5555. goto out_free_mbox;
  5556. }
  5557. } else {
  5558. rc = lpfc_sli4_post_els_sgl_list_ext(phba);
  5559. if (unlikely(rc)) {
  5560. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5561. "2560 Error %d during els sgl post "
  5562. "operation\n", rc);
  5563. rc = -ENODEV;
  5564. goto out_free_mbox;
  5565. }
  5566. }
  5567. /* Register SCSI SGL pool to the device */
  5568. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5569. if (unlikely(rc)) {
  5570. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5571. "0383 Error %d during scsi sgl post "
  5572. "operation\n", rc);
  5573. /* Some Scsi buffers were moved to the abort scsi list */
  5574. /* A pci function reset will repost them */
  5575. rc = -ENODEV;
  5576. goto out_free_mbox;
  5577. }
  5578. /* Post the rpi header region to the device. */
  5579. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5580. if (unlikely(rc)) {
  5581. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5582. "0393 Error %d during rpi post operation\n",
  5583. rc);
  5584. rc = -ENODEV;
  5585. goto out_free_mbox;
  5586. }
  5587. /* Create all the SLI4 queues */
  5588. rc = lpfc_sli4_queue_create(phba);
  5589. if (rc) {
  5590. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5591. "3089 Failed to allocate queues\n");
  5592. rc = -ENODEV;
  5593. goto out_stop_timers;
  5594. }
  5595. /* Set up all the queues to the device */
  5596. rc = lpfc_sli4_queue_setup(phba);
  5597. if (unlikely(rc)) {
  5598. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5599. "0381 Error %d during queue setup.\n ", rc);
  5600. goto out_destroy_queue;
  5601. }
  5602. /* Arm the CQs and then EQs on device */
  5603. lpfc_sli4_arm_cqeq_intr(phba);
  5604. /* Indicate device interrupt mode */
  5605. phba->sli4_hba.intr_enable = 1;
  5606. /* Allow asynchronous mailbox command to go through */
  5607. spin_lock_irq(&phba->hbalock);
  5608. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5609. spin_unlock_irq(&phba->hbalock);
  5610. /* Post receive buffers to the device */
  5611. lpfc_sli4_rb_setup(phba);
  5612. /* Reset HBA FCF states after HBA reset */
  5613. phba->fcf.fcf_flag = 0;
  5614. phba->fcf.current_rec.flag = 0;
  5615. /* Start the ELS watchdog timer */
  5616. mod_timer(&vport->els_tmofunc,
  5617. jiffies + HZ * (phba->fc_ratov * 2));
  5618. /* Start heart beat timer */
  5619. mod_timer(&phba->hb_tmofunc,
  5620. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5621. phba->hb_outstanding = 0;
  5622. phba->last_completion_time = jiffies;
  5623. /* Start error attention (ERATT) polling timer */
  5624. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5625. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5626. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5627. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5628. if (!rc) {
  5629. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5630. "2829 This device supports "
  5631. "Advanced Error Reporting (AER)\n");
  5632. spin_lock_irq(&phba->hbalock);
  5633. phba->hba_flag |= HBA_AER_ENABLED;
  5634. spin_unlock_irq(&phba->hbalock);
  5635. } else {
  5636. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5637. "2830 This device does not support "
  5638. "Advanced Error Reporting (AER)\n");
  5639. phba->cfg_aer_support = 0;
  5640. }
  5641. rc = 0;
  5642. }
  5643. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5644. /*
  5645. * The FC Port needs to register FCFI (index 0)
  5646. */
  5647. lpfc_reg_fcfi(phba, mboxq);
  5648. mboxq->vport = phba->pport;
  5649. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5650. if (rc != MBX_SUCCESS)
  5651. goto out_unset_queue;
  5652. rc = 0;
  5653. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5654. &mboxq->u.mqe.un.reg_fcfi);
  5655. /* Check if the port is configured to be disabled */
  5656. lpfc_sli_read_link_ste(phba);
  5657. }
  5658. /*
  5659. * The port is ready, set the host's link state to LINK_DOWN
  5660. * in preparation for link interrupts.
  5661. */
  5662. spin_lock_irq(&phba->hbalock);
  5663. phba->link_state = LPFC_LINK_DOWN;
  5664. spin_unlock_irq(&phba->hbalock);
  5665. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5666. (phba->hba_flag & LINK_DISABLED)) {
  5667. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5668. "3103 Adapter Link is disabled.\n");
  5669. lpfc_down_link(phba, mboxq);
  5670. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5671. if (rc != MBX_SUCCESS) {
  5672. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5673. "3104 Adapter failed to issue "
  5674. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5675. goto out_unset_queue;
  5676. }
  5677. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5678. /* don't perform init_link on SLI4 FC port loopback test */
  5679. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5680. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5681. if (rc)
  5682. goto out_unset_queue;
  5683. }
  5684. }
  5685. mempool_free(mboxq, phba->mbox_mem_pool);
  5686. return rc;
  5687. out_unset_queue:
  5688. /* Unset all the queues set up in this routine when error out */
  5689. lpfc_sli4_queue_unset(phba);
  5690. out_destroy_queue:
  5691. lpfc_sli4_queue_destroy(phba);
  5692. out_stop_timers:
  5693. lpfc_stop_hba_timers(phba);
  5694. out_free_mbox:
  5695. mempool_free(mboxq, phba->mbox_mem_pool);
  5696. return rc;
  5697. }
  5698. /**
  5699. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5700. * @ptr: context object - pointer to hba structure.
  5701. *
  5702. * This is the callback function for mailbox timer. The mailbox
  5703. * timer is armed when a new mailbox command is issued and the timer
  5704. * is deleted when the mailbox complete. The function is called by
  5705. * the kernel timer code when a mailbox does not complete within
  5706. * expected time. This function wakes up the worker thread to
  5707. * process the mailbox timeout and returns. All the processing is
  5708. * done by the worker thread function lpfc_mbox_timeout_handler.
  5709. **/
  5710. void
  5711. lpfc_mbox_timeout(unsigned long ptr)
  5712. {
  5713. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5714. unsigned long iflag;
  5715. uint32_t tmo_posted;
  5716. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5717. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5718. if (!tmo_posted)
  5719. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5720. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5721. if (!tmo_posted)
  5722. lpfc_worker_wake_up(phba);
  5723. return;
  5724. }
  5725. /**
  5726. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5727. * @phba: Pointer to HBA context object.
  5728. *
  5729. * This function is called from worker thread when a mailbox command times out.
  5730. * The caller is not required to hold any locks. This function will reset the
  5731. * HBA and recover all the pending commands.
  5732. **/
  5733. void
  5734. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5735. {
  5736. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5737. MAILBOX_t *mb = &pmbox->u.mb;
  5738. struct lpfc_sli *psli = &phba->sli;
  5739. struct lpfc_sli_ring *pring;
  5740. /* Check the pmbox pointer first. There is a race condition
  5741. * between the mbox timeout handler getting executed in the
  5742. * worklist and the mailbox actually completing. When this
  5743. * race condition occurs, the mbox_active will be NULL.
  5744. */
  5745. spin_lock_irq(&phba->hbalock);
  5746. if (pmbox == NULL) {
  5747. lpfc_printf_log(phba, KERN_WARNING,
  5748. LOG_MBOX | LOG_SLI,
  5749. "0353 Active Mailbox cleared - mailbox timeout "
  5750. "exiting\n");
  5751. spin_unlock_irq(&phba->hbalock);
  5752. return;
  5753. }
  5754. /* Mbox cmd <mbxCommand> timeout */
  5755. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5756. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5757. mb->mbxCommand,
  5758. phba->pport->port_state,
  5759. phba->sli.sli_flag,
  5760. phba->sli.mbox_active);
  5761. spin_unlock_irq(&phba->hbalock);
  5762. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5763. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5764. * it to fail all outstanding SCSI IO.
  5765. */
  5766. spin_lock_irq(&phba->pport->work_port_lock);
  5767. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5768. spin_unlock_irq(&phba->pport->work_port_lock);
  5769. spin_lock_irq(&phba->hbalock);
  5770. phba->link_state = LPFC_LINK_UNKNOWN;
  5771. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5772. spin_unlock_irq(&phba->hbalock);
  5773. pring = &psli->ring[psli->fcp_ring];
  5774. lpfc_sli_abort_iocb_ring(phba, pring);
  5775. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5776. "0345 Resetting board due to mailbox timeout\n");
  5777. /* Reset the HBA device */
  5778. lpfc_reset_hba(phba);
  5779. }
  5780. /**
  5781. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5782. * @phba: Pointer to HBA context object.
  5783. * @pmbox: Pointer to mailbox object.
  5784. * @flag: Flag indicating how the mailbox need to be processed.
  5785. *
  5786. * This function is called by discovery code and HBA management code
  5787. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5788. * function gets the hbalock to protect the data structures.
  5789. * The mailbox command can be submitted in polling mode, in which case
  5790. * this function will wait in a polling loop for the completion of the
  5791. * mailbox.
  5792. * If the mailbox is submitted in no_wait mode (not polling) the
  5793. * function will submit the command and returns immediately without waiting
  5794. * for the mailbox completion. The no_wait is supported only when HBA
  5795. * is in SLI2/SLI3 mode - interrupts are enabled.
  5796. * The SLI interface allows only one mailbox pending at a time. If the
  5797. * mailbox is issued in polling mode and there is already a mailbox
  5798. * pending, then the function will return an error. If the mailbox is issued
  5799. * in NO_WAIT mode and there is a mailbox pending already, the function
  5800. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5801. * The sli layer owns the mailbox object until the completion of mailbox
  5802. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5803. * return codes the caller owns the mailbox command after the return of
  5804. * the function.
  5805. **/
  5806. static int
  5807. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5808. uint32_t flag)
  5809. {
  5810. MAILBOX_t *mb;
  5811. struct lpfc_sli *psli = &phba->sli;
  5812. uint32_t status, evtctr;
  5813. uint32_t ha_copy, hc_copy;
  5814. int i;
  5815. unsigned long timeout;
  5816. unsigned long drvr_flag = 0;
  5817. uint32_t word0, ldata;
  5818. void __iomem *to_slim;
  5819. int processing_queue = 0;
  5820. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5821. if (!pmbox) {
  5822. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5823. /* processing mbox queue from intr_handler */
  5824. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5825. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5826. return MBX_SUCCESS;
  5827. }
  5828. processing_queue = 1;
  5829. pmbox = lpfc_mbox_get(phba);
  5830. if (!pmbox) {
  5831. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5832. return MBX_SUCCESS;
  5833. }
  5834. }
  5835. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5836. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5837. if(!pmbox->vport) {
  5838. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5839. lpfc_printf_log(phba, KERN_ERR,
  5840. LOG_MBOX | LOG_VPORT,
  5841. "1806 Mbox x%x failed. No vport\n",
  5842. pmbox->u.mb.mbxCommand);
  5843. dump_stack();
  5844. goto out_not_finished;
  5845. }
  5846. }
  5847. /* If the PCI channel is in offline state, do not post mbox. */
  5848. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5849. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5850. goto out_not_finished;
  5851. }
  5852. /* If HBA has a deferred error attention, fail the iocb. */
  5853. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5854. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5855. goto out_not_finished;
  5856. }
  5857. psli = &phba->sli;
  5858. mb = &pmbox->u.mb;
  5859. status = MBX_SUCCESS;
  5860. if (phba->link_state == LPFC_HBA_ERROR) {
  5861. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5862. /* Mbox command <mbxCommand> cannot issue */
  5863. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5864. "(%d):0311 Mailbox command x%x cannot "
  5865. "issue Data: x%x x%x\n",
  5866. pmbox->vport ? pmbox->vport->vpi : 0,
  5867. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5868. goto out_not_finished;
  5869. }
  5870. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  5871. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  5872. !(hc_copy & HC_MBINT_ENA)) {
  5873. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5874. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5875. "(%d):2528 Mailbox command x%x cannot "
  5876. "issue Data: x%x x%x\n",
  5877. pmbox->vport ? pmbox->vport->vpi : 0,
  5878. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5879. goto out_not_finished;
  5880. }
  5881. }
  5882. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  5883. /* Polling for a mbox command when another one is already active
  5884. * is not allowed in SLI. Also, the driver must have established
  5885. * SLI2 mode to queue and process multiple mbox commands.
  5886. */
  5887. if (flag & MBX_POLL) {
  5888. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5889. /* Mbox command <mbxCommand> cannot issue */
  5890. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5891. "(%d):2529 Mailbox command x%x "
  5892. "cannot issue Data: x%x x%x\n",
  5893. pmbox->vport ? pmbox->vport->vpi : 0,
  5894. pmbox->u.mb.mbxCommand,
  5895. psli->sli_flag, flag);
  5896. goto out_not_finished;
  5897. }
  5898. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  5899. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5900. /* Mbox command <mbxCommand> cannot issue */
  5901. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5902. "(%d):2530 Mailbox command x%x "
  5903. "cannot issue Data: x%x x%x\n",
  5904. pmbox->vport ? pmbox->vport->vpi : 0,
  5905. pmbox->u.mb.mbxCommand,
  5906. psli->sli_flag, flag);
  5907. goto out_not_finished;
  5908. }
  5909. /* Another mailbox command is still being processed, queue this
  5910. * command to be processed later.
  5911. */
  5912. lpfc_mbox_put(phba, pmbox);
  5913. /* Mbox cmd issue - BUSY */
  5914. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5915. "(%d):0308 Mbox cmd issue - BUSY Data: "
  5916. "x%x x%x x%x x%x\n",
  5917. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  5918. mb->mbxCommand, phba->pport->port_state,
  5919. psli->sli_flag, flag);
  5920. psli->slistat.mbox_busy++;
  5921. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5922. if (pmbox->vport) {
  5923. lpfc_debugfs_disc_trc(pmbox->vport,
  5924. LPFC_DISC_TRC_MBOX_VPORT,
  5925. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  5926. (uint32_t)mb->mbxCommand,
  5927. mb->un.varWords[0], mb->un.varWords[1]);
  5928. }
  5929. else {
  5930. lpfc_debugfs_disc_trc(phba->pport,
  5931. LPFC_DISC_TRC_MBOX,
  5932. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  5933. (uint32_t)mb->mbxCommand,
  5934. mb->un.varWords[0], mb->un.varWords[1]);
  5935. }
  5936. return MBX_BUSY;
  5937. }
  5938. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  5939. /* If we are not polling, we MUST be in SLI2 mode */
  5940. if (flag != MBX_POLL) {
  5941. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  5942. (mb->mbxCommand != MBX_KILL_BOARD)) {
  5943. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5944. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5945. /* Mbox command <mbxCommand> cannot issue */
  5946. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5947. "(%d):2531 Mailbox command x%x "
  5948. "cannot issue Data: x%x x%x\n",
  5949. pmbox->vport ? pmbox->vport->vpi : 0,
  5950. pmbox->u.mb.mbxCommand,
  5951. psli->sli_flag, flag);
  5952. goto out_not_finished;
  5953. }
  5954. /* timeout active mbox command */
  5955. mod_timer(&psli->mbox_tmo, (jiffies +
  5956. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  5957. }
  5958. /* Mailbox cmd <cmd> issue */
  5959. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5960. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  5961. "x%x\n",
  5962. pmbox->vport ? pmbox->vport->vpi : 0,
  5963. mb->mbxCommand, phba->pport->port_state,
  5964. psli->sli_flag, flag);
  5965. if (mb->mbxCommand != MBX_HEARTBEAT) {
  5966. if (pmbox->vport) {
  5967. lpfc_debugfs_disc_trc(pmbox->vport,
  5968. LPFC_DISC_TRC_MBOX_VPORT,
  5969. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  5970. (uint32_t)mb->mbxCommand,
  5971. mb->un.varWords[0], mb->un.varWords[1]);
  5972. }
  5973. else {
  5974. lpfc_debugfs_disc_trc(phba->pport,
  5975. LPFC_DISC_TRC_MBOX,
  5976. "MBOX Send: cmd:x%x mb:x%x x%x",
  5977. (uint32_t)mb->mbxCommand,
  5978. mb->un.varWords[0], mb->un.varWords[1]);
  5979. }
  5980. }
  5981. psli->slistat.mbox_cmd++;
  5982. evtctr = psli->slistat.mbox_event;
  5983. /* next set own bit for the adapter and copy over command word */
  5984. mb->mbxOwner = OWN_CHIP;
  5985. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  5986. /* Populate mbox extension offset word. */
  5987. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  5988. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  5989. = (uint8_t *)phba->mbox_ext
  5990. - (uint8_t *)phba->mbox;
  5991. }
  5992. /* Copy the mailbox extension data */
  5993. if (pmbox->in_ext_byte_len && pmbox->context2) {
  5994. lpfc_sli_pcimem_bcopy(pmbox->context2,
  5995. (uint8_t *)phba->mbox_ext,
  5996. pmbox->in_ext_byte_len);
  5997. }
  5998. /* Copy command data to host SLIM area */
  5999. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6000. } else {
  6001. /* Populate mbox extension offset word. */
  6002. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6003. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6004. = MAILBOX_HBA_EXT_OFFSET;
  6005. /* Copy the mailbox extension data */
  6006. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6007. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6008. MAILBOX_HBA_EXT_OFFSET,
  6009. pmbox->context2, pmbox->in_ext_byte_len);
  6010. }
  6011. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6012. /* copy command data into host mbox for cmpl */
  6013. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6014. }
  6015. /* First copy mbox command data to HBA SLIM, skip past first
  6016. word */
  6017. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6018. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6019. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6020. /* Next copy over first word, with mbxOwner set */
  6021. ldata = *((uint32_t *)mb);
  6022. to_slim = phba->MBslimaddr;
  6023. writel(ldata, to_slim);
  6024. readl(to_slim); /* flush */
  6025. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6026. /* switch over to host mailbox */
  6027. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6028. }
  6029. }
  6030. wmb();
  6031. switch (flag) {
  6032. case MBX_NOWAIT:
  6033. /* Set up reference to mailbox command */
  6034. psli->mbox_active = pmbox;
  6035. /* Interrupt board to do it */
  6036. writel(CA_MBATT, phba->CAregaddr);
  6037. readl(phba->CAregaddr); /* flush */
  6038. /* Don't wait for it to finish, just return */
  6039. break;
  6040. case MBX_POLL:
  6041. /* Set up null reference to mailbox command */
  6042. psli->mbox_active = NULL;
  6043. /* Interrupt board to do it */
  6044. writel(CA_MBATT, phba->CAregaddr);
  6045. readl(phba->CAregaddr); /* flush */
  6046. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6047. /* First read mbox status word */
  6048. word0 = *((uint32_t *)phba->mbox);
  6049. word0 = le32_to_cpu(word0);
  6050. } else {
  6051. /* First read mbox status word */
  6052. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6053. spin_unlock_irqrestore(&phba->hbalock,
  6054. drvr_flag);
  6055. goto out_not_finished;
  6056. }
  6057. }
  6058. /* Read the HBA Host Attention Register */
  6059. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6060. spin_unlock_irqrestore(&phba->hbalock,
  6061. drvr_flag);
  6062. goto out_not_finished;
  6063. }
  6064. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6065. 1000) + jiffies;
  6066. i = 0;
  6067. /* Wait for command to complete */
  6068. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6069. (!(ha_copy & HA_MBATT) &&
  6070. (phba->link_state > LPFC_WARM_START))) {
  6071. if (time_after(jiffies, timeout)) {
  6072. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6073. spin_unlock_irqrestore(&phba->hbalock,
  6074. drvr_flag);
  6075. goto out_not_finished;
  6076. }
  6077. /* Check if we took a mbox interrupt while we were
  6078. polling */
  6079. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6080. && (evtctr != psli->slistat.mbox_event))
  6081. break;
  6082. if (i++ > 10) {
  6083. spin_unlock_irqrestore(&phba->hbalock,
  6084. drvr_flag);
  6085. msleep(1);
  6086. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6087. }
  6088. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6089. /* First copy command data */
  6090. word0 = *((uint32_t *)phba->mbox);
  6091. word0 = le32_to_cpu(word0);
  6092. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6093. MAILBOX_t *slimmb;
  6094. uint32_t slimword0;
  6095. /* Check real SLIM for any errors */
  6096. slimword0 = readl(phba->MBslimaddr);
  6097. slimmb = (MAILBOX_t *) & slimword0;
  6098. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6099. && slimmb->mbxStatus) {
  6100. psli->sli_flag &=
  6101. ~LPFC_SLI_ACTIVE;
  6102. word0 = slimword0;
  6103. }
  6104. }
  6105. } else {
  6106. /* First copy command data */
  6107. word0 = readl(phba->MBslimaddr);
  6108. }
  6109. /* Read the HBA Host Attention Register */
  6110. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6111. spin_unlock_irqrestore(&phba->hbalock,
  6112. drvr_flag);
  6113. goto out_not_finished;
  6114. }
  6115. }
  6116. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6117. /* copy results back to user */
  6118. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6119. /* Copy the mailbox extension data */
  6120. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6121. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6122. pmbox->context2,
  6123. pmbox->out_ext_byte_len);
  6124. }
  6125. } else {
  6126. /* First copy command data */
  6127. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6128. MAILBOX_CMD_SIZE);
  6129. /* Copy the mailbox extension data */
  6130. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6131. lpfc_memcpy_from_slim(pmbox->context2,
  6132. phba->MBslimaddr +
  6133. MAILBOX_HBA_EXT_OFFSET,
  6134. pmbox->out_ext_byte_len);
  6135. }
  6136. }
  6137. writel(HA_MBATT, phba->HAregaddr);
  6138. readl(phba->HAregaddr); /* flush */
  6139. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6140. status = mb->mbxStatus;
  6141. }
  6142. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6143. return status;
  6144. out_not_finished:
  6145. if (processing_queue) {
  6146. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6147. lpfc_mbox_cmpl_put(phba, pmbox);
  6148. }
  6149. return MBX_NOT_FINISHED;
  6150. }
  6151. /**
  6152. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6153. * @phba: Pointer to HBA context object.
  6154. *
  6155. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6156. * the driver internal pending mailbox queue. It will then try to wait out the
  6157. * possible outstanding mailbox command before return.
  6158. *
  6159. * Returns:
  6160. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6161. * the outstanding mailbox command timed out.
  6162. **/
  6163. static int
  6164. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6165. {
  6166. struct lpfc_sli *psli = &phba->sli;
  6167. int rc = 0;
  6168. unsigned long timeout = 0;
  6169. /* Mark the asynchronous mailbox command posting as blocked */
  6170. spin_lock_irq(&phba->hbalock);
  6171. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6172. /* Determine how long we might wait for the active mailbox
  6173. * command to be gracefully completed by firmware.
  6174. */
  6175. if (phba->sli.mbox_active)
  6176. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6177. phba->sli.mbox_active) *
  6178. 1000) + jiffies;
  6179. spin_unlock_irq(&phba->hbalock);
  6180. /* Wait for the outstnading mailbox command to complete */
  6181. while (phba->sli.mbox_active) {
  6182. /* Check active mailbox complete status every 2ms */
  6183. msleep(2);
  6184. if (time_after(jiffies, timeout)) {
  6185. /* Timeout, marked the outstanding cmd not complete */
  6186. rc = 1;
  6187. break;
  6188. }
  6189. }
  6190. /* Can not cleanly block async mailbox command, fails it */
  6191. if (rc) {
  6192. spin_lock_irq(&phba->hbalock);
  6193. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6194. spin_unlock_irq(&phba->hbalock);
  6195. }
  6196. return rc;
  6197. }
  6198. /**
  6199. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6200. * @phba: Pointer to HBA context object.
  6201. *
  6202. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6203. * commands from the driver internal pending mailbox queue. It makes sure
  6204. * that there is no outstanding mailbox command before resuming posting
  6205. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6206. * mailbox command, it will try to wait it out before resuming asynchronous
  6207. * mailbox command posting.
  6208. **/
  6209. static void
  6210. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6211. {
  6212. struct lpfc_sli *psli = &phba->sli;
  6213. spin_lock_irq(&phba->hbalock);
  6214. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6215. /* Asynchronous mailbox posting is not blocked, do nothing */
  6216. spin_unlock_irq(&phba->hbalock);
  6217. return;
  6218. }
  6219. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6220. * successful or timeout, after timing-out the outstanding mailbox
  6221. * command shall always be removed, so just unblock posting async
  6222. * mailbox command and resume
  6223. */
  6224. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6225. spin_unlock_irq(&phba->hbalock);
  6226. /* wake up worker thread to post asynchronlous mailbox command */
  6227. lpfc_worker_wake_up(phba);
  6228. }
  6229. /**
  6230. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6231. * @phba: Pointer to HBA context object.
  6232. * @mboxq: Pointer to mailbox object.
  6233. *
  6234. * The function posts a mailbox to the port. The mailbox is expected
  6235. * to be comletely filled in and ready for the port to operate on it.
  6236. * This routine executes a synchronous completion operation on the
  6237. * mailbox by polling for its completion.
  6238. *
  6239. * The caller must not be holding any locks when calling this routine.
  6240. *
  6241. * Returns:
  6242. * MBX_SUCCESS - mailbox posted successfully
  6243. * Any of the MBX error values.
  6244. **/
  6245. static int
  6246. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6247. {
  6248. int rc = MBX_SUCCESS;
  6249. unsigned long iflag;
  6250. uint32_t db_ready;
  6251. uint32_t mcqe_status;
  6252. uint32_t mbx_cmnd;
  6253. unsigned long timeout;
  6254. struct lpfc_sli *psli = &phba->sli;
  6255. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6256. struct lpfc_bmbx_create *mbox_rgn;
  6257. struct dma_address *dma_address;
  6258. struct lpfc_register bmbx_reg;
  6259. /*
  6260. * Only one mailbox can be active to the bootstrap mailbox region
  6261. * at a time and there is no queueing provided.
  6262. */
  6263. spin_lock_irqsave(&phba->hbalock, iflag);
  6264. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6265. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6266. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6267. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6268. "cannot issue Data: x%x x%x\n",
  6269. mboxq->vport ? mboxq->vport->vpi : 0,
  6270. mboxq->u.mb.mbxCommand,
  6271. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6272. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6273. psli->sli_flag, MBX_POLL);
  6274. return MBXERR_ERROR;
  6275. }
  6276. /* The server grabs the token and owns it until release */
  6277. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6278. phba->sli.mbox_active = mboxq;
  6279. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6280. /*
  6281. * Initialize the bootstrap memory region to avoid stale data areas
  6282. * in the mailbox post. Then copy the caller's mailbox contents to
  6283. * the bmbx mailbox region.
  6284. */
  6285. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6286. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6287. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6288. sizeof(struct lpfc_mqe));
  6289. /* Post the high mailbox dma address to the port and wait for ready. */
  6290. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6291. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6292. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6293. * 1000) + jiffies;
  6294. do {
  6295. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6296. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6297. if (!db_ready)
  6298. msleep(2);
  6299. if (time_after(jiffies, timeout)) {
  6300. rc = MBXERR_ERROR;
  6301. goto exit;
  6302. }
  6303. } while (!db_ready);
  6304. /* Post the low mailbox dma address to the port. */
  6305. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6306. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6307. * 1000) + jiffies;
  6308. do {
  6309. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6310. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6311. if (!db_ready)
  6312. msleep(2);
  6313. if (time_after(jiffies, timeout)) {
  6314. rc = MBXERR_ERROR;
  6315. goto exit;
  6316. }
  6317. } while (!db_ready);
  6318. /*
  6319. * Read the CQ to ensure the mailbox has completed.
  6320. * If so, update the mailbox status so that the upper layers
  6321. * can complete the request normally.
  6322. */
  6323. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6324. sizeof(struct lpfc_mqe));
  6325. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6326. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6327. sizeof(struct lpfc_mcqe));
  6328. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6329. /*
  6330. * When the CQE status indicates a failure and the mailbox status
  6331. * indicates success then copy the CQE status into the mailbox status
  6332. * (and prefix it with x4000).
  6333. */
  6334. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6335. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6336. bf_set(lpfc_mqe_status, mb,
  6337. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6338. rc = MBXERR_ERROR;
  6339. } else
  6340. lpfc_sli4_swap_str(phba, mboxq);
  6341. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6342. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6343. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6344. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6345. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6346. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6347. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6348. bf_get(lpfc_mqe_status, mb),
  6349. mb->un.mb_words[0], mb->un.mb_words[1],
  6350. mb->un.mb_words[2], mb->un.mb_words[3],
  6351. mb->un.mb_words[4], mb->un.mb_words[5],
  6352. mb->un.mb_words[6], mb->un.mb_words[7],
  6353. mb->un.mb_words[8], mb->un.mb_words[9],
  6354. mb->un.mb_words[10], mb->un.mb_words[11],
  6355. mb->un.mb_words[12], mboxq->mcqe.word0,
  6356. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6357. mboxq->mcqe.trailer);
  6358. exit:
  6359. /* We are holding the token, no needed for lock when release */
  6360. spin_lock_irqsave(&phba->hbalock, iflag);
  6361. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6362. phba->sli.mbox_active = NULL;
  6363. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6364. return rc;
  6365. }
  6366. /**
  6367. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6368. * @phba: Pointer to HBA context object.
  6369. * @pmbox: Pointer to mailbox object.
  6370. * @flag: Flag indicating how the mailbox need to be processed.
  6371. *
  6372. * This function is called by discovery code and HBA management code to submit
  6373. * a mailbox command to firmware with SLI-4 interface spec.
  6374. *
  6375. * Return codes the caller owns the mailbox command after the return of the
  6376. * function.
  6377. **/
  6378. static int
  6379. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6380. uint32_t flag)
  6381. {
  6382. struct lpfc_sli *psli = &phba->sli;
  6383. unsigned long iflags;
  6384. int rc;
  6385. /* dump from issue mailbox command if setup */
  6386. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6387. rc = lpfc_mbox_dev_check(phba);
  6388. if (unlikely(rc)) {
  6389. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6390. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6391. "cannot issue Data: x%x x%x\n",
  6392. mboxq->vport ? mboxq->vport->vpi : 0,
  6393. mboxq->u.mb.mbxCommand,
  6394. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6395. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6396. psli->sli_flag, flag);
  6397. goto out_not_finished;
  6398. }
  6399. /* Detect polling mode and jump to a handler */
  6400. if (!phba->sli4_hba.intr_enable) {
  6401. if (flag == MBX_POLL)
  6402. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6403. else
  6404. rc = -EIO;
  6405. if (rc != MBX_SUCCESS)
  6406. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6407. "(%d):2541 Mailbox command x%x "
  6408. "(x%x/x%x) cannot issue Data: "
  6409. "x%x x%x\n",
  6410. mboxq->vport ? mboxq->vport->vpi : 0,
  6411. mboxq->u.mb.mbxCommand,
  6412. lpfc_sli_config_mbox_subsys_get(phba,
  6413. mboxq),
  6414. lpfc_sli_config_mbox_opcode_get(phba,
  6415. mboxq),
  6416. psli->sli_flag, flag);
  6417. return rc;
  6418. } else if (flag == MBX_POLL) {
  6419. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6420. "(%d):2542 Try to issue mailbox command "
  6421. "x%x (x%x/x%x) synchronously ahead of async"
  6422. "mailbox command queue: x%x x%x\n",
  6423. mboxq->vport ? mboxq->vport->vpi : 0,
  6424. mboxq->u.mb.mbxCommand,
  6425. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6426. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6427. psli->sli_flag, flag);
  6428. /* Try to block the asynchronous mailbox posting */
  6429. rc = lpfc_sli4_async_mbox_block(phba);
  6430. if (!rc) {
  6431. /* Successfully blocked, now issue sync mbox cmd */
  6432. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6433. if (rc != MBX_SUCCESS)
  6434. lpfc_printf_log(phba, KERN_ERR,
  6435. LOG_MBOX | LOG_SLI,
  6436. "(%d):2597 Mailbox command "
  6437. "x%x (x%x/x%x) cannot issue "
  6438. "Data: x%x x%x\n",
  6439. mboxq->vport ?
  6440. mboxq->vport->vpi : 0,
  6441. mboxq->u.mb.mbxCommand,
  6442. lpfc_sli_config_mbox_subsys_get(phba,
  6443. mboxq),
  6444. lpfc_sli_config_mbox_opcode_get(phba,
  6445. mboxq),
  6446. psli->sli_flag, flag);
  6447. /* Unblock the async mailbox posting afterward */
  6448. lpfc_sli4_async_mbox_unblock(phba);
  6449. }
  6450. return rc;
  6451. }
  6452. /* Now, interrupt mode asynchrous mailbox command */
  6453. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6454. if (rc) {
  6455. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6456. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6457. "cannot issue Data: x%x x%x\n",
  6458. mboxq->vport ? mboxq->vport->vpi : 0,
  6459. mboxq->u.mb.mbxCommand,
  6460. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6461. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6462. psli->sli_flag, flag);
  6463. goto out_not_finished;
  6464. }
  6465. /* Put the mailbox command to the driver internal FIFO */
  6466. psli->slistat.mbox_busy++;
  6467. spin_lock_irqsave(&phba->hbalock, iflags);
  6468. lpfc_mbox_put(phba, mboxq);
  6469. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6470. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6471. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6472. "x%x (x%x/x%x) x%x x%x x%x\n",
  6473. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6474. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6475. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6476. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6477. phba->pport->port_state,
  6478. psli->sli_flag, MBX_NOWAIT);
  6479. /* Wake up worker thread to transport mailbox command from head */
  6480. lpfc_worker_wake_up(phba);
  6481. return MBX_BUSY;
  6482. out_not_finished:
  6483. return MBX_NOT_FINISHED;
  6484. }
  6485. /**
  6486. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6487. * @phba: Pointer to HBA context object.
  6488. *
  6489. * This function is called by worker thread to send a mailbox command to
  6490. * SLI4 HBA firmware.
  6491. *
  6492. **/
  6493. int
  6494. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6495. {
  6496. struct lpfc_sli *psli = &phba->sli;
  6497. LPFC_MBOXQ_t *mboxq;
  6498. int rc = MBX_SUCCESS;
  6499. unsigned long iflags;
  6500. struct lpfc_mqe *mqe;
  6501. uint32_t mbx_cmnd;
  6502. /* Check interrupt mode before post async mailbox command */
  6503. if (unlikely(!phba->sli4_hba.intr_enable))
  6504. return MBX_NOT_FINISHED;
  6505. /* Check for mailbox command service token */
  6506. spin_lock_irqsave(&phba->hbalock, iflags);
  6507. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6508. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6509. return MBX_NOT_FINISHED;
  6510. }
  6511. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6512. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6513. return MBX_NOT_FINISHED;
  6514. }
  6515. if (unlikely(phba->sli.mbox_active)) {
  6516. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6517. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6518. "0384 There is pending active mailbox cmd\n");
  6519. return MBX_NOT_FINISHED;
  6520. }
  6521. /* Take the mailbox command service token */
  6522. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6523. /* Get the next mailbox command from head of queue */
  6524. mboxq = lpfc_mbox_get(phba);
  6525. /* If no more mailbox command waiting for post, we're done */
  6526. if (!mboxq) {
  6527. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6528. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6529. return MBX_SUCCESS;
  6530. }
  6531. phba->sli.mbox_active = mboxq;
  6532. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6533. /* Check device readiness for posting mailbox command */
  6534. rc = lpfc_mbox_dev_check(phba);
  6535. if (unlikely(rc))
  6536. /* Driver clean routine will clean up pending mailbox */
  6537. goto out_not_finished;
  6538. /* Prepare the mbox command to be posted */
  6539. mqe = &mboxq->u.mqe;
  6540. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6541. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6542. mod_timer(&psli->mbox_tmo, (jiffies +
  6543. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6544. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6545. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6546. "x%x x%x\n",
  6547. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6548. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6549. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6550. phba->pport->port_state, psli->sli_flag);
  6551. if (mbx_cmnd != MBX_HEARTBEAT) {
  6552. if (mboxq->vport) {
  6553. lpfc_debugfs_disc_trc(mboxq->vport,
  6554. LPFC_DISC_TRC_MBOX_VPORT,
  6555. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6556. mbx_cmnd, mqe->un.mb_words[0],
  6557. mqe->un.mb_words[1]);
  6558. } else {
  6559. lpfc_debugfs_disc_trc(phba->pport,
  6560. LPFC_DISC_TRC_MBOX,
  6561. "MBOX Send: cmd:x%x mb:x%x x%x",
  6562. mbx_cmnd, mqe->un.mb_words[0],
  6563. mqe->un.mb_words[1]);
  6564. }
  6565. }
  6566. psli->slistat.mbox_cmd++;
  6567. /* Post the mailbox command to the port */
  6568. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6569. if (rc != MBX_SUCCESS) {
  6570. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6571. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6572. "cannot issue Data: x%x x%x\n",
  6573. mboxq->vport ? mboxq->vport->vpi : 0,
  6574. mboxq->u.mb.mbxCommand,
  6575. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6576. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6577. psli->sli_flag, MBX_NOWAIT);
  6578. goto out_not_finished;
  6579. }
  6580. return rc;
  6581. out_not_finished:
  6582. spin_lock_irqsave(&phba->hbalock, iflags);
  6583. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6584. __lpfc_mbox_cmpl_put(phba, mboxq);
  6585. /* Release the token */
  6586. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6587. phba->sli.mbox_active = NULL;
  6588. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6589. return MBX_NOT_FINISHED;
  6590. }
  6591. /**
  6592. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6593. * @phba: Pointer to HBA context object.
  6594. * @pmbox: Pointer to mailbox object.
  6595. * @flag: Flag indicating how the mailbox need to be processed.
  6596. *
  6597. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6598. * the API jump table function pointer from the lpfc_hba struct.
  6599. *
  6600. * Return codes the caller owns the mailbox command after the return of the
  6601. * function.
  6602. **/
  6603. int
  6604. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6605. {
  6606. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6607. }
  6608. /**
  6609. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6610. * @phba: The hba struct for which this call is being executed.
  6611. * @dev_grp: The HBA PCI-Device group number.
  6612. *
  6613. * This routine sets up the mbox interface API function jump table in @phba
  6614. * struct.
  6615. * Returns: 0 - success, -ENODEV - failure.
  6616. **/
  6617. int
  6618. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6619. {
  6620. switch (dev_grp) {
  6621. case LPFC_PCI_DEV_LP:
  6622. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6623. phba->lpfc_sli_handle_slow_ring_event =
  6624. lpfc_sli_handle_slow_ring_event_s3;
  6625. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6626. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6627. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6628. break;
  6629. case LPFC_PCI_DEV_OC:
  6630. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6631. phba->lpfc_sli_handle_slow_ring_event =
  6632. lpfc_sli_handle_slow_ring_event_s4;
  6633. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6634. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6635. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6636. break;
  6637. default:
  6638. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6639. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6640. dev_grp);
  6641. return -ENODEV;
  6642. break;
  6643. }
  6644. return 0;
  6645. }
  6646. /**
  6647. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6648. * @phba: Pointer to HBA context object.
  6649. * @pring: Pointer to driver SLI ring object.
  6650. * @piocb: Pointer to address of newly added command iocb.
  6651. *
  6652. * This function is called with hbalock held to add a command
  6653. * iocb to the txq when SLI layer cannot submit the command iocb
  6654. * to the ring.
  6655. **/
  6656. void
  6657. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6658. struct lpfc_iocbq *piocb)
  6659. {
  6660. /* Insert the caller's iocb in the txq tail for later processing. */
  6661. list_add_tail(&piocb->list, &pring->txq);
  6662. pring->txq_cnt++;
  6663. }
  6664. /**
  6665. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6666. * @phba: Pointer to HBA context object.
  6667. * @pring: Pointer to driver SLI ring object.
  6668. * @piocb: Pointer to address of newly added command iocb.
  6669. *
  6670. * This function is called with hbalock held before a new
  6671. * iocb is submitted to the firmware. This function checks
  6672. * txq to flush the iocbs in txq to Firmware before
  6673. * submitting new iocbs to the Firmware.
  6674. * If there are iocbs in the txq which need to be submitted
  6675. * to firmware, lpfc_sli_next_iocb returns the first element
  6676. * of the txq after dequeuing it from txq.
  6677. * If there is no iocb in the txq then the function will return
  6678. * *piocb and *piocb is set to NULL. Caller needs to check
  6679. * *piocb to find if there are more commands in the txq.
  6680. **/
  6681. static struct lpfc_iocbq *
  6682. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6683. struct lpfc_iocbq **piocb)
  6684. {
  6685. struct lpfc_iocbq * nextiocb;
  6686. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6687. if (!nextiocb) {
  6688. nextiocb = *piocb;
  6689. *piocb = NULL;
  6690. }
  6691. return nextiocb;
  6692. }
  6693. /**
  6694. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6695. * @phba: Pointer to HBA context object.
  6696. * @ring_number: SLI ring number to issue iocb on.
  6697. * @piocb: Pointer to command iocb.
  6698. * @flag: Flag indicating if this command can be put into txq.
  6699. *
  6700. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6701. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6702. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6703. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6704. * this function allows only iocbs for posting buffers. This function finds
  6705. * next available slot in the command ring and posts the command to the
  6706. * available slot and writes the port attention register to request HBA start
  6707. * processing new iocb. If there is no slot available in the ring and
  6708. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6709. * the function returns IOCB_BUSY.
  6710. *
  6711. * This function is called with hbalock held. The function will return success
  6712. * after it successfully submit the iocb to firmware or after adding to the
  6713. * txq.
  6714. **/
  6715. static int
  6716. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6717. struct lpfc_iocbq *piocb, uint32_t flag)
  6718. {
  6719. struct lpfc_iocbq *nextiocb;
  6720. IOCB_t *iocb;
  6721. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6722. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6723. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6724. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6725. lpfc_printf_log(phba, KERN_ERR,
  6726. LOG_SLI | LOG_VPORT,
  6727. "1807 IOCB x%x failed. No vport\n",
  6728. piocb->iocb.ulpCommand);
  6729. dump_stack();
  6730. return IOCB_ERROR;
  6731. }
  6732. /* If the PCI channel is in offline state, do not post iocbs. */
  6733. if (unlikely(pci_channel_offline(phba->pcidev)))
  6734. return IOCB_ERROR;
  6735. /* If HBA has a deferred error attention, fail the iocb. */
  6736. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6737. return IOCB_ERROR;
  6738. /*
  6739. * We should never get an IOCB if we are in a < LINK_DOWN state
  6740. */
  6741. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6742. return IOCB_ERROR;
  6743. /*
  6744. * Check to see if we are blocking IOCB processing because of a
  6745. * outstanding event.
  6746. */
  6747. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6748. goto iocb_busy;
  6749. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6750. /*
  6751. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6752. * can be issued if the link is not up.
  6753. */
  6754. switch (piocb->iocb.ulpCommand) {
  6755. case CMD_GEN_REQUEST64_CR:
  6756. case CMD_GEN_REQUEST64_CX:
  6757. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6758. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6759. FC_RCTL_DD_UNSOL_CMD) ||
  6760. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6761. MENLO_TRANSPORT_TYPE))
  6762. goto iocb_busy;
  6763. break;
  6764. case CMD_QUE_RING_BUF_CN:
  6765. case CMD_QUE_RING_BUF64_CN:
  6766. /*
  6767. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6768. * completion, iocb_cmpl MUST be 0.
  6769. */
  6770. if (piocb->iocb_cmpl)
  6771. piocb->iocb_cmpl = NULL;
  6772. /*FALLTHROUGH*/
  6773. case CMD_CREATE_XRI_CR:
  6774. case CMD_CLOSE_XRI_CN:
  6775. case CMD_CLOSE_XRI_CX:
  6776. break;
  6777. default:
  6778. goto iocb_busy;
  6779. }
  6780. /*
  6781. * For FCP commands, we must be in a state where we can process link
  6782. * attention events.
  6783. */
  6784. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6785. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6786. goto iocb_busy;
  6787. }
  6788. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6789. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6790. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6791. if (iocb)
  6792. lpfc_sli_update_ring(phba, pring);
  6793. else
  6794. lpfc_sli_update_full_ring(phba, pring);
  6795. if (!piocb)
  6796. return IOCB_SUCCESS;
  6797. goto out_busy;
  6798. iocb_busy:
  6799. pring->stats.iocb_cmd_delay++;
  6800. out_busy:
  6801. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6802. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6803. return IOCB_SUCCESS;
  6804. }
  6805. return IOCB_BUSY;
  6806. }
  6807. /**
  6808. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6809. * @phba: Pointer to HBA context object.
  6810. * @piocb: Pointer to command iocb.
  6811. * @sglq: Pointer to the scatter gather queue object.
  6812. *
  6813. * This routine converts the bpl or bde that is in the IOCB
  6814. * to a sgl list for the sli4 hardware. The physical address
  6815. * of the bpl/bde is converted back to a virtual address.
  6816. * If the IOCB contains a BPL then the list of BDE's is
  6817. * converted to sli4_sge's. If the IOCB contains a single
  6818. * BDE then it is converted to a single sli_sge.
  6819. * The IOCB is still in cpu endianess so the contents of
  6820. * the bpl can be used without byte swapping.
  6821. *
  6822. * Returns valid XRI = Success, NO_XRI = Failure.
  6823. **/
  6824. static uint16_t
  6825. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6826. struct lpfc_sglq *sglq)
  6827. {
  6828. uint16_t xritag = NO_XRI;
  6829. struct ulp_bde64 *bpl = NULL;
  6830. struct ulp_bde64 bde;
  6831. struct sli4_sge *sgl = NULL;
  6832. struct lpfc_dmabuf *dmabuf;
  6833. IOCB_t *icmd;
  6834. int numBdes = 0;
  6835. int i = 0;
  6836. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6837. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6838. if (!piocbq || !sglq)
  6839. return xritag;
  6840. sgl = (struct sli4_sge *)sglq->sgl;
  6841. icmd = &piocbq->iocb;
  6842. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6843. return sglq->sli4_xritag;
  6844. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6845. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6846. sizeof(struct ulp_bde64);
  6847. /* The addrHigh and addrLow fields within the IOCB
  6848. * have not been byteswapped yet so there is no
  6849. * need to swap them back.
  6850. */
  6851. if (piocbq->context3)
  6852. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  6853. else
  6854. return xritag;
  6855. bpl = (struct ulp_bde64 *)dmabuf->virt;
  6856. if (!bpl)
  6857. return xritag;
  6858. for (i = 0; i < numBdes; i++) {
  6859. /* Should already be byte swapped. */
  6860. sgl->addr_hi = bpl->addrHigh;
  6861. sgl->addr_lo = bpl->addrLow;
  6862. sgl->word2 = le32_to_cpu(sgl->word2);
  6863. if ((i+1) == numBdes)
  6864. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6865. else
  6866. bf_set(lpfc_sli4_sge_last, sgl, 0);
  6867. /* swap the size field back to the cpu so we
  6868. * can assign it to the sgl.
  6869. */
  6870. bde.tus.w = le32_to_cpu(bpl->tus.w);
  6871. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  6872. /* The offsets in the sgl need to be accumulated
  6873. * separately for the request and reply lists.
  6874. * The request is always first, the reply follows.
  6875. */
  6876. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  6877. /* add up the reply sg entries */
  6878. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  6879. inbound++;
  6880. /* first inbound? reset the offset */
  6881. if (inbound == 1)
  6882. offset = 0;
  6883. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  6884. bf_set(lpfc_sli4_sge_type, sgl,
  6885. LPFC_SGE_TYPE_DATA);
  6886. offset += bde.tus.f.bdeSize;
  6887. }
  6888. sgl->word2 = cpu_to_le32(sgl->word2);
  6889. bpl++;
  6890. sgl++;
  6891. }
  6892. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  6893. /* The addrHigh and addrLow fields of the BDE have not
  6894. * been byteswapped yet so they need to be swapped
  6895. * before putting them in the sgl.
  6896. */
  6897. sgl->addr_hi =
  6898. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  6899. sgl->addr_lo =
  6900. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  6901. sgl->word2 = le32_to_cpu(sgl->word2);
  6902. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6903. sgl->word2 = cpu_to_le32(sgl->word2);
  6904. sgl->sge_len =
  6905. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  6906. }
  6907. return sglq->sli4_xritag;
  6908. }
  6909. /**
  6910. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  6911. * @phba: Pointer to HBA context object.
  6912. *
  6913. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  6914. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  6915. * held.
  6916. *
  6917. * Return: index into SLI4 fast-path FCP queue index.
  6918. **/
  6919. static uint32_t
  6920. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  6921. {
  6922. ++phba->fcp_qidx;
  6923. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  6924. phba->fcp_qidx = 0;
  6925. return phba->fcp_qidx;
  6926. }
  6927. /**
  6928. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  6929. * @phba: Pointer to HBA context object.
  6930. * @piocb: Pointer to command iocb.
  6931. * @wqe: Pointer to the work queue entry.
  6932. *
  6933. * This routine converts the iocb command to its Work Queue Entry
  6934. * equivalent. The wqe pointer should not have any fields set when
  6935. * this routine is called because it will memcpy over them.
  6936. * This routine does not set the CQ_ID or the WQEC bits in the
  6937. * wqe.
  6938. *
  6939. * Returns: 0 = Success, IOCB_ERROR = Failure.
  6940. **/
  6941. static int
  6942. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  6943. union lpfc_wqe *wqe)
  6944. {
  6945. uint32_t xmit_len = 0, total_len = 0;
  6946. uint8_t ct = 0;
  6947. uint32_t fip;
  6948. uint32_t abort_tag;
  6949. uint8_t command_type = ELS_COMMAND_NON_FIP;
  6950. uint8_t cmnd;
  6951. uint16_t xritag;
  6952. uint16_t abrt_iotag;
  6953. struct lpfc_iocbq *abrtiocbq;
  6954. struct ulp_bde64 *bpl = NULL;
  6955. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  6956. int numBdes, i;
  6957. struct ulp_bde64 bde;
  6958. struct lpfc_nodelist *ndlp;
  6959. uint32_t *pcmd;
  6960. uint32_t if_type;
  6961. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  6962. /* The fcp commands will set command type */
  6963. if (iocbq->iocb_flag & LPFC_IO_FCP)
  6964. command_type = FCP_COMMAND;
  6965. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  6966. command_type = ELS_COMMAND_FIP;
  6967. else
  6968. command_type = ELS_COMMAND_NON_FIP;
  6969. /* Some of the fields are in the right position already */
  6970. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  6971. abort_tag = (uint32_t) iocbq->iotag;
  6972. xritag = iocbq->sli4_xritag;
  6973. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  6974. /* words0-2 bpl convert bde */
  6975. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6976. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  6977. sizeof(struct ulp_bde64);
  6978. bpl = (struct ulp_bde64 *)
  6979. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  6980. if (!bpl)
  6981. return IOCB_ERROR;
  6982. /* Should already be byte swapped. */
  6983. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  6984. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  6985. /* swap the size field back to the cpu so we
  6986. * can assign it to the sgl.
  6987. */
  6988. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  6989. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  6990. total_len = 0;
  6991. for (i = 0; i < numBdes; i++) {
  6992. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  6993. total_len += bde.tus.f.bdeSize;
  6994. }
  6995. } else
  6996. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  6997. iocbq->iocb.ulpIoTag = iocbq->iotag;
  6998. cmnd = iocbq->iocb.ulpCommand;
  6999. switch (iocbq->iocb.ulpCommand) {
  7000. case CMD_ELS_REQUEST64_CR:
  7001. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7002. if (!iocbq->iocb.ulpLe) {
  7003. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7004. "2007 Only Limited Edition cmd Format"
  7005. " supported 0x%x\n",
  7006. iocbq->iocb.ulpCommand);
  7007. return IOCB_ERROR;
  7008. }
  7009. wqe->els_req.payload_len = xmit_len;
  7010. /* Els_reguest64 has a TMO */
  7011. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7012. iocbq->iocb.ulpTimeout);
  7013. /* Need a VF for word 4 set the vf bit*/
  7014. bf_set(els_req64_vf, &wqe->els_req, 0);
  7015. /* And a VFID for word 12 */
  7016. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7017. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7018. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7019. iocbq->iocb.ulpContext);
  7020. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7021. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7022. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7023. if (command_type == ELS_COMMAND_FIP)
  7024. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7025. >> LPFC_FIP_ELS_ID_SHIFT);
  7026. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7027. iocbq->context2)->virt);
  7028. if_type = bf_get(lpfc_sli_intf_if_type,
  7029. &phba->sli4_hba.sli_intf);
  7030. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7031. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7032. *pcmd == ELS_CMD_SCR ||
  7033. *pcmd == ELS_CMD_FDISC ||
  7034. *pcmd == ELS_CMD_PLOGI)) {
  7035. bf_set(els_req64_sp, &wqe->els_req, 1);
  7036. bf_set(els_req64_sid, &wqe->els_req,
  7037. iocbq->vport->fc_myDID);
  7038. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7039. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7040. phba->vpi_ids[phba->pport->vpi]);
  7041. } else if (pcmd && iocbq->context1) {
  7042. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7043. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7044. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7045. }
  7046. }
  7047. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7048. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7049. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7050. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7051. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7052. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7053. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7054. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7055. break;
  7056. case CMD_XMIT_SEQUENCE64_CX:
  7057. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7058. iocbq->iocb.un.ulpWord[3]);
  7059. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7060. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7061. /* The entire sequence is transmitted for this IOCB */
  7062. xmit_len = total_len;
  7063. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7064. if (phba->link_flag & LS_LOOPBACK_MODE)
  7065. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7066. case CMD_XMIT_SEQUENCE64_CR:
  7067. /* word3 iocb=io_tag32 wqe=reserved */
  7068. wqe->xmit_sequence.rsvd3 = 0;
  7069. /* word4 relative_offset memcpy */
  7070. /* word5 r_ctl/df_ctl memcpy */
  7071. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7072. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7073. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7074. LPFC_WQE_IOD_WRITE);
  7075. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7076. LPFC_WQE_LENLOC_WORD12);
  7077. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7078. wqe->xmit_sequence.xmit_len = xmit_len;
  7079. command_type = OTHER_COMMAND;
  7080. break;
  7081. case CMD_XMIT_BCAST64_CN:
  7082. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7083. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7084. /* word4 iocb=rsvd wqe=rsvd */
  7085. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7086. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7087. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7088. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7089. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7090. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7091. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7092. LPFC_WQE_LENLOC_WORD3);
  7093. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7094. break;
  7095. case CMD_FCP_IWRITE64_CR:
  7096. command_type = FCP_COMMAND_DATA_OUT;
  7097. /* word3 iocb=iotag wqe=payload_offset_len */
  7098. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7099. wqe->fcp_iwrite.payload_offset_len =
  7100. xmit_len + sizeof(struct fcp_rsp);
  7101. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7102. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7103. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7104. iocbq->iocb.ulpFCP2Rcvy);
  7105. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7106. /* Always open the exchange */
  7107. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7108. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7109. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7110. LPFC_WQE_LENLOC_WORD4);
  7111. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7112. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7113. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7114. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7115. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7116. }
  7117. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7118. break;
  7119. case CMD_FCP_IREAD64_CR:
  7120. /* word3 iocb=iotag wqe=payload_offset_len */
  7121. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7122. wqe->fcp_iread.payload_offset_len =
  7123. xmit_len + sizeof(struct fcp_rsp);
  7124. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7125. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7126. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7127. iocbq->iocb.ulpFCP2Rcvy);
  7128. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7129. /* Always open the exchange */
  7130. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7131. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7132. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7133. LPFC_WQE_LENLOC_WORD4);
  7134. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7135. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7136. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7137. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7138. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7139. }
  7140. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7141. break;
  7142. case CMD_FCP_ICMND64_CR:
  7143. /* word3 iocb=IO_TAG wqe=reserved */
  7144. wqe->fcp_icmd.rsrvd3 = 0;
  7145. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7146. /* Always open the exchange */
  7147. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7148. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7149. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7150. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7151. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7152. LPFC_WQE_LENLOC_NONE);
  7153. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7154. break;
  7155. case CMD_GEN_REQUEST64_CR:
  7156. /* For this command calculate the xmit length of the
  7157. * request bde.
  7158. */
  7159. xmit_len = 0;
  7160. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7161. sizeof(struct ulp_bde64);
  7162. for (i = 0; i < numBdes; i++) {
  7163. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7164. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7165. break;
  7166. xmit_len += bde.tus.f.bdeSize;
  7167. }
  7168. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7169. wqe->gen_req.request_payload_len = xmit_len;
  7170. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7171. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7172. /* word6 context tag copied in memcpy */
  7173. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7174. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7175. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7176. "2015 Invalid CT %x command 0x%x\n",
  7177. ct, iocbq->iocb.ulpCommand);
  7178. return IOCB_ERROR;
  7179. }
  7180. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7181. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7182. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7183. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7184. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7185. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7186. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7187. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7188. command_type = OTHER_COMMAND;
  7189. break;
  7190. case CMD_XMIT_ELS_RSP64_CX:
  7191. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7192. /* words0-2 BDE memcpy */
  7193. /* word3 iocb=iotag32 wqe=response_payload_len */
  7194. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7195. /* word4 iocb=did wge=rsvd. */
  7196. wqe->xmit_els_rsp.rsvd4 = 0;
  7197. /* word5 iocb=rsvd wge=did */
  7198. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7199. iocbq->iocb.un.elsreq64.remoteID);
  7200. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7201. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7202. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7203. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7204. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7205. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7206. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7207. phba->vpi_ids[iocbq->vport->vpi]);
  7208. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7209. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7210. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7211. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7212. LPFC_WQE_LENLOC_WORD3);
  7213. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7214. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7215. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7216. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7217. iocbq->context2)->virt);
  7218. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7219. bf_set(els_req64_sp, &wqe->els_req, 1);
  7220. bf_set(els_req64_sid, &wqe->els_req,
  7221. iocbq->vport->fc_myDID);
  7222. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7223. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7224. phba->vpi_ids[phba->pport->vpi]);
  7225. }
  7226. command_type = OTHER_COMMAND;
  7227. break;
  7228. case CMD_CLOSE_XRI_CN:
  7229. case CMD_ABORT_XRI_CN:
  7230. case CMD_ABORT_XRI_CX:
  7231. /* words 0-2 memcpy should be 0 rserved */
  7232. /* port will send abts */
  7233. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7234. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7235. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7236. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7237. } else
  7238. fip = 0;
  7239. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7240. /*
  7241. * The link is down, or the command was ELS_FIP
  7242. * so the fw does not need to send abts
  7243. * on the wire.
  7244. */
  7245. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7246. else
  7247. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7248. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7249. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7250. wqe->abort_cmd.rsrvd5 = 0;
  7251. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7252. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7253. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7254. /*
  7255. * The abort handler will send us CMD_ABORT_XRI_CN or
  7256. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7257. */
  7258. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7259. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7260. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7261. LPFC_WQE_LENLOC_NONE);
  7262. cmnd = CMD_ABORT_XRI_CX;
  7263. command_type = OTHER_COMMAND;
  7264. xritag = 0;
  7265. break;
  7266. case CMD_XMIT_BLS_RSP64_CX:
  7267. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7268. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7269. * we re-construct this WQE here based on information in
  7270. * iocbq from scratch.
  7271. */
  7272. memset(wqe, 0, sizeof(union lpfc_wqe));
  7273. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7274. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7275. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7276. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7277. LPFC_ABTS_UNSOL_INT) {
  7278. /* ABTS sent by initiator to CT exchange, the
  7279. * RX_ID field will be filled with the newly
  7280. * allocated responder XRI.
  7281. */
  7282. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7283. iocbq->sli4_xritag);
  7284. } else {
  7285. /* ABTS sent by responder to CT exchange, the
  7286. * RX_ID field will be filled with the responder
  7287. * RX_ID from ABTS.
  7288. */
  7289. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7290. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7291. }
  7292. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7293. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7294. /* Use CT=VPI */
  7295. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7296. ndlp->nlp_DID);
  7297. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7298. iocbq->iocb.ulpContext);
  7299. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7300. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7301. phba->vpi_ids[phba->pport->vpi]);
  7302. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7303. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7304. LPFC_WQE_LENLOC_NONE);
  7305. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7306. command_type = OTHER_COMMAND;
  7307. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7308. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7309. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7310. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7311. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7312. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7313. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7314. }
  7315. break;
  7316. case CMD_XRI_ABORTED_CX:
  7317. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7318. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7319. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7320. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7321. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7322. default:
  7323. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7324. "2014 Invalid command 0x%x\n",
  7325. iocbq->iocb.ulpCommand);
  7326. return IOCB_ERROR;
  7327. break;
  7328. }
  7329. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7330. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7331. wqe->generic.wqe_com.abort_tag = abort_tag;
  7332. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7333. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7334. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7335. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7336. return 0;
  7337. }
  7338. /**
  7339. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7340. * @phba: Pointer to HBA context object.
  7341. * @ring_number: SLI ring number to issue iocb on.
  7342. * @piocb: Pointer to command iocb.
  7343. * @flag: Flag indicating if this command can be put into txq.
  7344. *
  7345. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7346. * an iocb command to an HBA with SLI-4 interface spec.
  7347. *
  7348. * This function is called with hbalock held. The function will return success
  7349. * after it successfully submit the iocb to firmware or after adding to the
  7350. * txq.
  7351. **/
  7352. static int
  7353. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7354. struct lpfc_iocbq *piocb, uint32_t flag)
  7355. {
  7356. struct lpfc_sglq *sglq;
  7357. union lpfc_wqe wqe;
  7358. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7359. if (piocb->sli4_xritag == NO_XRI) {
  7360. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7361. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7362. sglq = NULL;
  7363. else {
  7364. if (pring->txq_cnt) {
  7365. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7366. __lpfc_sli_ringtx_put(phba,
  7367. pring, piocb);
  7368. return IOCB_SUCCESS;
  7369. } else {
  7370. return IOCB_BUSY;
  7371. }
  7372. } else {
  7373. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7374. if (!sglq) {
  7375. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7376. __lpfc_sli_ringtx_put(phba,
  7377. pring,
  7378. piocb);
  7379. return IOCB_SUCCESS;
  7380. } else
  7381. return IOCB_BUSY;
  7382. }
  7383. }
  7384. }
  7385. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7386. /* These IO's already have an XRI and a mapped sgl. */
  7387. sglq = NULL;
  7388. } else {
  7389. /*
  7390. * This is a continuation of a commandi,(CX) so this
  7391. * sglq is on the active list
  7392. */
  7393. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7394. if (!sglq)
  7395. return IOCB_ERROR;
  7396. }
  7397. if (sglq) {
  7398. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7399. piocb->sli4_xritag = sglq->sli4_xritag;
  7400. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7401. return IOCB_ERROR;
  7402. }
  7403. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7404. return IOCB_ERROR;
  7405. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7406. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7407. /*
  7408. * For FCP command IOCB, get a new WQ index to distribute
  7409. * WQE across the WQsr. On the other hand, for abort IOCB,
  7410. * it carries the same WQ index to the original command
  7411. * IOCB.
  7412. */
  7413. if (piocb->iocb_flag & LPFC_IO_FCP)
  7414. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7415. if (unlikely(!phba->sli4_hba.fcp_wq))
  7416. return IOCB_ERROR;
  7417. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7418. &wqe))
  7419. return IOCB_ERROR;
  7420. } else {
  7421. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7422. return IOCB_ERROR;
  7423. }
  7424. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7425. return 0;
  7426. }
  7427. /**
  7428. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7429. *
  7430. * This routine wraps the actual lockless version for issusing IOCB function
  7431. * pointer from the lpfc_hba struct.
  7432. *
  7433. * Return codes:
  7434. * IOCB_ERROR - Error
  7435. * IOCB_SUCCESS - Success
  7436. * IOCB_BUSY - Busy
  7437. **/
  7438. int
  7439. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7440. struct lpfc_iocbq *piocb, uint32_t flag)
  7441. {
  7442. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7443. }
  7444. /**
  7445. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7446. * @phba: The hba struct for which this call is being executed.
  7447. * @dev_grp: The HBA PCI-Device group number.
  7448. *
  7449. * This routine sets up the SLI interface API function jump table in @phba
  7450. * struct.
  7451. * Returns: 0 - success, -ENODEV - failure.
  7452. **/
  7453. int
  7454. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7455. {
  7456. switch (dev_grp) {
  7457. case LPFC_PCI_DEV_LP:
  7458. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7459. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7460. break;
  7461. case LPFC_PCI_DEV_OC:
  7462. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7463. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7464. break;
  7465. default:
  7466. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7467. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7468. dev_grp);
  7469. return -ENODEV;
  7470. break;
  7471. }
  7472. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7473. return 0;
  7474. }
  7475. /**
  7476. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7477. * @phba: Pointer to HBA context object.
  7478. * @pring: Pointer to driver SLI ring object.
  7479. * @piocb: Pointer to command iocb.
  7480. * @flag: Flag indicating if this command can be put into txq.
  7481. *
  7482. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7483. * function. This function gets the hbalock and calls
  7484. * __lpfc_sli_issue_iocb function and will return the error returned
  7485. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7486. * functions which do not hold hbalock.
  7487. **/
  7488. int
  7489. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7490. struct lpfc_iocbq *piocb, uint32_t flag)
  7491. {
  7492. unsigned long iflags;
  7493. int rc;
  7494. spin_lock_irqsave(&phba->hbalock, iflags);
  7495. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7496. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7497. return rc;
  7498. }
  7499. /**
  7500. * lpfc_extra_ring_setup - Extra ring setup function
  7501. * @phba: Pointer to HBA context object.
  7502. *
  7503. * This function is called while driver attaches with the
  7504. * HBA to setup the extra ring. The extra ring is used
  7505. * only when driver needs to support target mode functionality
  7506. * or IP over FC functionalities.
  7507. *
  7508. * This function is called with no lock held.
  7509. **/
  7510. static int
  7511. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7512. {
  7513. struct lpfc_sli *psli;
  7514. struct lpfc_sli_ring *pring;
  7515. psli = &phba->sli;
  7516. /* Adjust cmd/rsp ring iocb entries more evenly */
  7517. /* Take some away from the FCP ring */
  7518. pring = &psli->ring[psli->fcp_ring];
  7519. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7520. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7521. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7522. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7523. /* and give them to the extra ring */
  7524. pring = &psli->ring[psli->extra_ring];
  7525. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7526. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7527. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7528. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7529. /* Setup default profile for this ring */
  7530. pring->iotag_max = 4096;
  7531. pring->num_mask = 1;
  7532. pring->prt[0].profile = 0; /* Mask 0 */
  7533. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7534. pring->prt[0].type = phba->cfg_multi_ring_type;
  7535. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7536. return 0;
  7537. }
  7538. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7539. * @vport: pointer to virtual port object.
  7540. * @ndlp: nodelist pointer for the impacted rport.
  7541. *
  7542. * The driver calls this routine in response to a XRI ABORT CQE
  7543. * event from the port. In this event, the driver is required to
  7544. * recover its login to the rport even though its login may be valid
  7545. * from the driver's perspective. The failed ABTS notice from the
  7546. * port indicates the rport is not responding.
  7547. */
  7548. static void
  7549. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7550. struct lpfc_nodelist *ndlp)
  7551. {
  7552. struct Scsi_Host *shost;
  7553. struct lpfc_hba *phba;
  7554. unsigned long flags = 0;
  7555. shost = lpfc_shost_from_vport(vport);
  7556. phba = vport->phba;
  7557. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7558. lpfc_printf_log(phba, KERN_INFO,
  7559. LOG_SLI, "3093 No rport recovery needed. "
  7560. "rport in state 0x%x\n",
  7561. ndlp->nlp_state);
  7562. return;
  7563. }
  7564. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7565. "3094 Start rport recovery on shost id 0x%x "
  7566. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7567. "flags 0x%x\n",
  7568. shost->host_no, ndlp->nlp_DID,
  7569. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7570. ndlp->nlp_flag);
  7571. /*
  7572. * The rport is not responding. Don't attempt ADISC recovery.
  7573. * Remove the FCP-2 flag to force a PLOGI.
  7574. */
  7575. spin_lock_irqsave(shost->host_lock, flags);
  7576. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7577. spin_unlock_irqrestore(shost->host_lock, flags);
  7578. lpfc_disc_state_machine(vport, ndlp, NULL,
  7579. NLP_EVT_DEVICE_RECOVERY);
  7580. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7581. spin_lock_irqsave(shost->host_lock, flags);
  7582. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7583. spin_unlock_irqrestore(shost->host_lock, flags);
  7584. lpfc_disc_start(vport);
  7585. }
  7586. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7587. * @phba: Pointer to HBA context object.
  7588. * @iocbq: Pointer to iocb object.
  7589. *
  7590. * The async_event handler calls this routine when it receives
  7591. * an ASYNC_STATUS_CN event from the port. The port generates
  7592. * this event when an Abort Sequence request to an rport fails
  7593. * twice in succession. The abort could be originated by the
  7594. * driver or by the port. The ABTS could have been for an ELS
  7595. * or FCP IO. The port only generates this event when an ABTS
  7596. * fails to complete after one retry.
  7597. */
  7598. static void
  7599. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7600. struct lpfc_iocbq *iocbq)
  7601. {
  7602. struct lpfc_nodelist *ndlp = NULL;
  7603. uint16_t rpi = 0, vpi = 0;
  7604. struct lpfc_vport *vport = NULL;
  7605. /* The rpi in the ulpContext is vport-sensitive. */
  7606. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7607. rpi = iocbq->iocb.ulpContext;
  7608. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7609. "3092 Port generated ABTS async event "
  7610. "on vpi %d rpi %d status 0x%x\n",
  7611. vpi, rpi, iocbq->iocb.ulpStatus);
  7612. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7613. if (!vport)
  7614. goto err_exit;
  7615. ndlp = lpfc_findnode_rpi(vport, rpi);
  7616. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7617. goto err_exit;
  7618. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7619. lpfc_sli_abts_recover_port(vport, ndlp);
  7620. return;
  7621. err_exit:
  7622. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7623. "3095 Event Context not found, no "
  7624. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7625. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7626. vpi, rpi);
  7627. }
  7628. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7629. * @phba: pointer to HBA context object.
  7630. * @ndlp: nodelist pointer for the impacted rport.
  7631. * @axri: pointer to the wcqe containing the failed exchange.
  7632. *
  7633. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7634. * port. The port generates this event when an abort exchange request to an
  7635. * rport fails twice in succession with no reply. The abort could be originated
  7636. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7637. */
  7638. void
  7639. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7640. struct lpfc_nodelist *ndlp,
  7641. struct sli4_wcqe_xri_aborted *axri)
  7642. {
  7643. struct lpfc_vport *vport;
  7644. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7645. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7646. "3115 Node Context not found, driver "
  7647. "ignoring abts err event\n");
  7648. return;
  7649. }
  7650. vport = ndlp->vport;
  7651. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7652. "3116 Port generated FCP XRI ABORT event on "
  7653. "vpi %d rpi %d xri x%x status 0x%x\n",
  7654. ndlp->vport->vpi, ndlp->nlp_rpi,
  7655. bf_get(lpfc_wcqe_xa_xri, axri),
  7656. bf_get(lpfc_wcqe_xa_status, axri));
  7657. if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
  7658. lpfc_sli_abts_recover_port(vport, ndlp);
  7659. }
  7660. /**
  7661. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7662. * @phba: Pointer to HBA context object.
  7663. * @pring: Pointer to driver SLI ring object.
  7664. * @iocbq: Pointer to iocb object.
  7665. *
  7666. * This function is called by the slow ring event handler
  7667. * function when there is an ASYNC event iocb in the ring.
  7668. * This function is called with no lock held.
  7669. * Currently this function handles only temperature related
  7670. * ASYNC events. The function decodes the temperature sensor
  7671. * event message and posts events for the management applications.
  7672. **/
  7673. static void
  7674. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7675. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7676. {
  7677. IOCB_t *icmd;
  7678. uint16_t evt_code;
  7679. struct temp_event temp_event_data;
  7680. struct Scsi_Host *shost;
  7681. uint32_t *iocb_w;
  7682. icmd = &iocbq->iocb;
  7683. evt_code = icmd->un.asyncstat.evt_code;
  7684. switch (evt_code) {
  7685. case ASYNC_TEMP_WARN:
  7686. case ASYNC_TEMP_SAFE:
  7687. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7688. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7689. if (evt_code == ASYNC_TEMP_WARN) {
  7690. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7691. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7692. "0347 Adapter is very hot, please take "
  7693. "corrective action. temperature : %d Celsius\n",
  7694. (uint32_t) icmd->ulpContext);
  7695. } else {
  7696. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7697. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7698. "0340 Adapter temperature is OK now. "
  7699. "temperature : %d Celsius\n",
  7700. (uint32_t) icmd->ulpContext);
  7701. }
  7702. /* Send temperature change event to applications */
  7703. shost = lpfc_shost_from_vport(phba->pport);
  7704. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7705. sizeof(temp_event_data), (char *) &temp_event_data,
  7706. LPFC_NL_VENDOR_ID);
  7707. break;
  7708. case ASYNC_STATUS_CN:
  7709. lpfc_sli_abts_err_handler(phba, iocbq);
  7710. break;
  7711. default:
  7712. iocb_w = (uint32_t *) icmd;
  7713. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7714. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7715. " evt_code 0x%x\n"
  7716. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7717. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7718. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7719. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7720. pring->ringno, icmd->un.asyncstat.evt_code,
  7721. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7722. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7723. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7724. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7725. break;
  7726. }
  7727. }
  7728. /**
  7729. * lpfc_sli_setup - SLI ring setup function
  7730. * @phba: Pointer to HBA context object.
  7731. *
  7732. * lpfc_sli_setup sets up rings of the SLI interface with
  7733. * number of iocbs per ring and iotags. This function is
  7734. * called while driver attach to the HBA and before the
  7735. * interrupts are enabled. So there is no need for locking.
  7736. *
  7737. * This function always returns 0.
  7738. **/
  7739. int
  7740. lpfc_sli_setup(struct lpfc_hba *phba)
  7741. {
  7742. int i, totiocbsize = 0;
  7743. struct lpfc_sli *psli = &phba->sli;
  7744. struct lpfc_sli_ring *pring;
  7745. psli->num_rings = MAX_CONFIGURED_RINGS;
  7746. psli->sli_flag = 0;
  7747. psli->fcp_ring = LPFC_FCP_RING;
  7748. psli->next_ring = LPFC_FCP_NEXT_RING;
  7749. psli->extra_ring = LPFC_EXTRA_RING;
  7750. psli->iocbq_lookup = NULL;
  7751. psli->iocbq_lookup_len = 0;
  7752. psli->last_iotag = 0;
  7753. for (i = 0; i < psli->num_rings; i++) {
  7754. pring = &psli->ring[i];
  7755. switch (i) {
  7756. case LPFC_FCP_RING: /* ring 0 - FCP */
  7757. /* numCiocb and numRiocb are used in config_port */
  7758. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7759. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7760. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7761. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7762. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7763. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7764. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7765. SLI3_IOCB_CMD_SIZE :
  7766. SLI2_IOCB_CMD_SIZE;
  7767. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7768. SLI3_IOCB_RSP_SIZE :
  7769. SLI2_IOCB_RSP_SIZE;
  7770. pring->iotag_ctr = 0;
  7771. pring->iotag_max =
  7772. (phba->cfg_hba_queue_depth * 2);
  7773. pring->fast_iotag = pring->iotag_max;
  7774. pring->num_mask = 0;
  7775. break;
  7776. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7777. /* numCiocb and numRiocb are used in config_port */
  7778. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7779. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7780. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7781. SLI3_IOCB_CMD_SIZE :
  7782. SLI2_IOCB_CMD_SIZE;
  7783. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7784. SLI3_IOCB_RSP_SIZE :
  7785. SLI2_IOCB_RSP_SIZE;
  7786. pring->iotag_max = phba->cfg_hba_queue_depth;
  7787. pring->num_mask = 0;
  7788. break;
  7789. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7790. /* numCiocb and numRiocb are used in config_port */
  7791. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7792. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7793. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7794. SLI3_IOCB_CMD_SIZE :
  7795. SLI2_IOCB_CMD_SIZE;
  7796. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7797. SLI3_IOCB_RSP_SIZE :
  7798. SLI2_IOCB_RSP_SIZE;
  7799. pring->fast_iotag = 0;
  7800. pring->iotag_ctr = 0;
  7801. pring->iotag_max = 4096;
  7802. pring->lpfc_sli_rcv_async_status =
  7803. lpfc_sli_async_event_handler;
  7804. pring->num_mask = LPFC_MAX_RING_MASK;
  7805. pring->prt[0].profile = 0; /* Mask 0 */
  7806. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7807. pring->prt[0].type = FC_TYPE_ELS;
  7808. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7809. lpfc_els_unsol_event;
  7810. pring->prt[1].profile = 0; /* Mask 1 */
  7811. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7812. pring->prt[1].type = FC_TYPE_ELS;
  7813. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7814. lpfc_els_unsol_event;
  7815. pring->prt[2].profile = 0; /* Mask 2 */
  7816. /* NameServer Inquiry */
  7817. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7818. /* NameServer */
  7819. pring->prt[2].type = FC_TYPE_CT;
  7820. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7821. lpfc_ct_unsol_event;
  7822. pring->prt[3].profile = 0; /* Mask 3 */
  7823. /* NameServer response */
  7824. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7825. /* NameServer */
  7826. pring->prt[3].type = FC_TYPE_CT;
  7827. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7828. lpfc_ct_unsol_event;
  7829. /* abort unsolicited sequence */
  7830. pring->prt[4].profile = 0; /* Mask 4 */
  7831. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7832. pring->prt[4].type = FC_TYPE_BLS;
  7833. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7834. lpfc_sli4_ct_abort_unsol_event;
  7835. break;
  7836. }
  7837. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7838. (pring->numRiocb * pring->sizeRiocb);
  7839. }
  7840. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7841. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7842. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7843. "SLI2 SLIM Data: x%x x%lx\n",
  7844. phba->brd_no, totiocbsize,
  7845. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7846. }
  7847. if (phba->cfg_multi_ring_support == 2)
  7848. lpfc_extra_ring_setup(phba);
  7849. return 0;
  7850. }
  7851. /**
  7852. * lpfc_sli_queue_setup - Queue initialization function
  7853. * @phba: Pointer to HBA context object.
  7854. *
  7855. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  7856. * ring. This function also initializes ring indices of each ring.
  7857. * This function is called during the initialization of the SLI
  7858. * interface of an HBA.
  7859. * This function is called with no lock held and always returns
  7860. * 1.
  7861. **/
  7862. int
  7863. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  7864. {
  7865. struct lpfc_sli *psli;
  7866. struct lpfc_sli_ring *pring;
  7867. int i;
  7868. psli = &phba->sli;
  7869. spin_lock_irq(&phba->hbalock);
  7870. INIT_LIST_HEAD(&psli->mboxq);
  7871. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  7872. /* Initialize list headers for txq and txcmplq as double linked lists */
  7873. for (i = 0; i < psli->num_rings; i++) {
  7874. pring = &psli->ring[i];
  7875. pring->ringno = i;
  7876. pring->next_cmdidx = 0;
  7877. pring->local_getidx = 0;
  7878. pring->cmdidx = 0;
  7879. INIT_LIST_HEAD(&pring->txq);
  7880. INIT_LIST_HEAD(&pring->txcmplq);
  7881. INIT_LIST_HEAD(&pring->iocb_continueq);
  7882. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  7883. INIT_LIST_HEAD(&pring->postbufq);
  7884. }
  7885. spin_unlock_irq(&phba->hbalock);
  7886. return 1;
  7887. }
  7888. /**
  7889. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  7890. * @phba: Pointer to HBA context object.
  7891. *
  7892. * This routine flushes the mailbox command subsystem. It will unconditionally
  7893. * flush all the mailbox commands in the three possible stages in the mailbox
  7894. * command sub-system: pending mailbox command queue; the outstanding mailbox
  7895. * command; and completed mailbox command queue. It is caller's responsibility
  7896. * to make sure that the driver is in the proper state to flush the mailbox
  7897. * command sub-system. Namely, the posting of mailbox commands into the
  7898. * pending mailbox command queue from the various clients must be stopped;
  7899. * either the HBA is in a state that it will never works on the outstanding
  7900. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  7901. * mailbox command has been completed.
  7902. **/
  7903. static void
  7904. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  7905. {
  7906. LIST_HEAD(completions);
  7907. struct lpfc_sli *psli = &phba->sli;
  7908. LPFC_MBOXQ_t *pmb;
  7909. unsigned long iflag;
  7910. /* Flush all the mailbox commands in the mbox system */
  7911. spin_lock_irqsave(&phba->hbalock, iflag);
  7912. /* The pending mailbox command queue */
  7913. list_splice_init(&phba->sli.mboxq, &completions);
  7914. /* The outstanding active mailbox command */
  7915. if (psli->mbox_active) {
  7916. list_add_tail(&psli->mbox_active->list, &completions);
  7917. psli->mbox_active = NULL;
  7918. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7919. }
  7920. /* The completed mailbox command queue */
  7921. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  7922. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7923. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  7924. while (!list_empty(&completions)) {
  7925. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  7926. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7927. if (pmb->mbox_cmpl)
  7928. pmb->mbox_cmpl(phba, pmb);
  7929. }
  7930. }
  7931. /**
  7932. * lpfc_sli_host_down - Vport cleanup function
  7933. * @vport: Pointer to virtual port object.
  7934. *
  7935. * lpfc_sli_host_down is called to clean up the resources
  7936. * associated with a vport before destroying virtual
  7937. * port data structures.
  7938. * This function does following operations:
  7939. * - Free discovery resources associated with this virtual
  7940. * port.
  7941. * - Free iocbs associated with this virtual port in
  7942. * the txq.
  7943. * - Send abort for all iocb commands associated with this
  7944. * vport in txcmplq.
  7945. *
  7946. * This function is called with no lock held and always returns 1.
  7947. **/
  7948. int
  7949. lpfc_sli_host_down(struct lpfc_vport *vport)
  7950. {
  7951. LIST_HEAD(completions);
  7952. struct lpfc_hba *phba = vport->phba;
  7953. struct lpfc_sli *psli = &phba->sli;
  7954. struct lpfc_sli_ring *pring;
  7955. struct lpfc_iocbq *iocb, *next_iocb;
  7956. int i;
  7957. unsigned long flags = 0;
  7958. uint16_t prev_pring_flag;
  7959. lpfc_cleanup_discovery_resources(vport);
  7960. spin_lock_irqsave(&phba->hbalock, flags);
  7961. for (i = 0; i < psli->num_rings; i++) {
  7962. pring = &psli->ring[i];
  7963. prev_pring_flag = pring->flag;
  7964. /* Only slow rings */
  7965. if (pring->ringno == LPFC_ELS_RING) {
  7966. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7967. /* Set the lpfc data pending flag */
  7968. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7969. }
  7970. /*
  7971. * Error everything on the txq since these iocbs have not been
  7972. * given to the FW yet.
  7973. */
  7974. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  7975. if (iocb->vport != vport)
  7976. continue;
  7977. list_move_tail(&iocb->list, &completions);
  7978. pring->txq_cnt--;
  7979. }
  7980. /* Next issue ABTS for everything on the txcmplq */
  7981. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  7982. list) {
  7983. if (iocb->vport != vport)
  7984. continue;
  7985. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  7986. }
  7987. pring->flag = prev_pring_flag;
  7988. }
  7989. spin_unlock_irqrestore(&phba->hbalock, flags);
  7990. /* Cancel all the IOCBs from the completions list */
  7991. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  7992. IOERR_SLI_DOWN);
  7993. return 1;
  7994. }
  7995. /**
  7996. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  7997. * @phba: Pointer to HBA context object.
  7998. *
  7999. * This function cleans up all iocb, buffers, mailbox commands
  8000. * while shutting down the HBA. This function is called with no
  8001. * lock held and always returns 1.
  8002. * This function does the following to cleanup driver resources:
  8003. * - Free discovery resources for each virtual port
  8004. * - Cleanup any pending fabric iocbs
  8005. * - Iterate through the iocb txq and free each entry
  8006. * in the list.
  8007. * - Free up any buffer posted to the HBA
  8008. * - Free mailbox commands in the mailbox queue.
  8009. **/
  8010. int
  8011. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8012. {
  8013. LIST_HEAD(completions);
  8014. struct lpfc_sli *psli = &phba->sli;
  8015. struct lpfc_sli_ring *pring;
  8016. struct lpfc_dmabuf *buf_ptr;
  8017. unsigned long flags = 0;
  8018. int i;
  8019. /* Shutdown the mailbox command sub-system */
  8020. lpfc_sli_mbox_sys_shutdown(phba);
  8021. lpfc_hba_down_prep(phba);
  8022. lpfc_fabric_abort_hba(phba);
  8023. spin_lock_irqsave(&phba->hbalock, flags);
  8024. for (i = 0; i < psli->num_rings; i++) {
  8025. pring = &psli->ring[i];
  8026. /* Only slow rings */
  8027. if (pring->ringno == LPFC_ELS_RING) {
  8028. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8029. /* Set the lpfc data pending flag */
  8030. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8031. }
  8032. /*
  8033. * Error everything on the txq since these iocbs have not been
  8034. * given to the FW yet.
  8035. */
  8036. list_splice_init(&pring->txq, &completions);
  8037. pring->txq_cnt = 0;
  8038. }
  8039. spin_unlock_irqrestore(&phba->hbalock, flags);
  8040. /* Cancel all the IOCBs from the completions list */
  8041. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8042. IOERR_SLI_DOWN);
  8043. spin_lock_irqsave(&phba->hbalock, flags);
  8044. list_splice_init(&phba->elsbuf, &completions);
  8045. phba->elsbuf_cnt = 0;
  8046. phba->elsbuf_prev_cnt = 0;
  8047. spin_unlock_irqrestore(&phba->hbalock, flags);
  8048. while (!list_empty(&completions)) {
  8049. list_remove_head(&completions, buf_ptr,
  8050. struct lpfc_dmabuf, list);
  8051. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8052. kfree(buf_ptr);
  8053. }
  8054. /* Return any active mbox cmds */
  8055. del_timer_sync(&psli->mbox_tmo);
  8056. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8057. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8058. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8059. return 1;
  8060. }
  8061. /**
  8062. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8063. * @srcp: Source memory pointer.
  8064. * @destp: Destination memory pointer.
  8065. * @cnt: Number of words required to be copied.
  8066. *
  8067. * This function is used for copying data between driver memory
  8068. * and the SLI memory. This function also changes the endianness
  8069. * of each word if native endianness is different from SLI
  8070. * endianness. This function can be called with or without
  8071. * lock.
  8072. **/
  8073. void
  8074. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8075. {
  8076. uint32_t *src = srcp;
  8077. uint32_t *dest = destp;
  8078. uint32_t ldata;
  8079. int i;
  8080. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8081. ldata = *src;
  8082. ldata = le32_to_cpu(ldata);
  8083. *dest = ldata;
  8084. src++;
  8085. dest++;
  8086. }
  8087. }
  8088. /**
  8089. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8090. * @srcp: Source memory pointer.
  8091. * @destp: Destination memory pointer.
  8092. * @cnt: Number of words required to be copied.
  8093. *
  8094. * This function is used for copying data between a data structure
  8095. * with big endian representation to local endianness.
  8096. * This function can be called with or without lock.
  8097. **/
  8098. void
  8099. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8100. {
  8101. uint32_t *src = srcp;
  8102. uint32_t *dest = destp;
  8103. uint32_t ldata;
  8104. int i;
  8105. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8106. ldata = *src;
  8107. ldata = be32_to_cpu(ldata);
  8108. *dest = ldata;
  8109. src++;
  8110. dest++;
  8111. }
  8112. }
  8113. /**
  8114. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8115. * @phba: Pointer to HBA context object.
  8116. * @pring: Pointer to driver SLI ring object.
  8117. * @mp: Pointer to driver buffer object.
  8118. *
  8119. * This function is called with no lock held.
  8120. * It always return zero after adding the buffer to the postbufq
  8121. * buffer list.
  8122. **/
  8123. int
  8124. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8125. struct lpfc_dmabuf *mp)
  8126. {
  8127. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8128. later */
  8129. spin_lock_irq(&phba->hbalock);
  8130. list_add_tail(&mp->list, &pring->postbufq);
  8131. pring->postbufq_cnt++;
  8132. spin_unlock_irq(&phba->hbalock);
  8133. return 0;
  8134. }
  8135. /**
  8136. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8137. * @phba: Pointer to HBA context object.
  8138. *
  8139. * When HBQ is enabled, buffers are searched based on tags. This function
  8140. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8141. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8142. * does not conflict with tags of buffer posted for unsolicited events.
  8143. * The function returns the allocated tag. The function is called with
  8144. * no locks held.
  8145. **/
  8146. uint32_t
  8147. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8148. {
  8149. spin_lock_irq(&phba->hbalock);
  8150. phba->buffer_tag_count++;
  8151. /*
  8152. * Always set the QUE_BUFTAG_BIT to distiguish between
  8153. * a tag assigned by HBQ.
  8154. */
  8155. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8156. spin_unlock_irq(&phba->hbalock);
  8157. return phba->buffer_tag_count;
  8158. }
  8159. /**
  8160. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8161. * @phba: Pointer to HBA context object.
  8162. * @pring: Pointer to driver SLI ring object.
  8163. * @tag: Buffer tag.
  8164. *
  8165. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8166. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8167. * iocb is posted to the response ring with the tag of the buffer.
  8168. * This function searches the pring->postbufq list using the tag
  8169. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8170. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8171. * buffer is returned to the caller else NULL is returned.
  8172. * This function is called with no lock held.
  8173. **/
  8174. struct lpfc_dmabuf *
  8175. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8176. uint32_t tag)
  8177. {
  8178. struct lpfc_dmabuf *mp, *next_mp;
  8179. struct list_head *slp = &pring->postbufq;
  8180. /* Search postbufq, from the beginning, looking for a match on tag */
  8181. spin_lock_irq(&phba->hbalock);
  8182. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8183. if (mp->buffer_tag == tag) {
  8184. list_del_init(&mp->list);
  8185. pring->postbufq_cnt--;
  8186. spin_unlock_irq(&phba->hbalock);
  8187. return mp;
  8188. }
  8189. }
  8190. spin_unlock_irq(&phba->hbalock);
  8191. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8192. "0402 Cannot find virtual addr for buffer tag on "
  8193. "ring %d Data x%lx x%p x%p x%x\n",
  8194. pring->ringno, (unsigned long) tag,
  8195. slp->next, slp->prev, pring->postbufq_cnt);
  8196. return NULL;
  8197. }
  8198. /**
  8199. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8200. * @phba: Pointer to HBA context object.
  8201. * @pring: Pointer to driver SLI ring object.
  8202. * @phys: DMA address of the buffer.
  8203. *
  8204. * This function searches the buffer list using the dma_address
  8205. * of unsolicited event to find the driver's lpfc_dmabuf object
  8206. * corresponding to the dma_address. The function returns the
  8207. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8208. * This function is called by the ct and els unsolicited event
  8209. * handlers to get the buffer associated with the unsolicited
  8210. * event.
  8211. *
  8212. * This function is called with no lock held.
  8213. **/
  8214. struct lpfc_dmabuf *
  8215. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8216. dma_addr_t phys)
  8217. {
  8218. struct lpfc_dmabuf *mp, *next_mp;
  8219. struct list_head *slp = &pring->postbufq;
  8220. /* Search postbufq, from the beginning, looking for a match on phys */
  8221. spin_lock_irq(&phba->hbalock);
  8222. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8223. if (mp->phys == phys) {
  8224. list_del_init(&mp->list);
  8225. pring->postbufq_cnt--;
  8226. spin_unlock_irq(&phba->hbalock);
  8227. return mp;
  8228. }
  8229. }
  8230. spin_unlock_irq(&phba->hbalock);
  8231. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8232. "0410 Cannot find virtual addr for mapped buf on "
  8233. "ring %d Data x%llx x%p x%p x%x\n",
  8234. pring->ringno, (unsigned long long)phys,
  8235. slp->next, slp->prev, pring->postbufq_cnt);
  8236. return NULL;
  8237. }
  8238. /**
  8239. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8240. * @phba: Pointer to HBA context object.
  8241. * @cmdiocb: Pointer to driver command iocb object.
  8242. * @rspiocb: Pointer to driver response iocb object.
  8243. *
  8244. * This function is the completion handler for the abort iocbs for
  8245. * ELS commands. This function is called from the ELS ring event
  8246. * handler with no lock held. This function frees memory resources
  8247. * associated with the abort iocb.
  8248. **/
  8249. static void
  8250. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8251. struct lpfc_iocbq *rspiocb)
  8252. {
  8253. IOCB_t *irsp = &rspiocb->iocb;
  8254. uint16_t abort_iotag, abort_context;
  8255. struct lpfc_iocbq *abort_iocb = NULL;
  8256. if (irsp->ulpStatus) {
  8257. /*
  8258. * Assume that the port already completed and returned, or
  8259. * will return the iocb. Just Log the message.
  8260. */
  8261. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8262. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8263. spin_lock_irq(&phba->hbalock);
  8264. if (phba->sli_rev < LPFC_SLI_REV4) {
  8265. if (abort_iotag != 0 &&
  8266. abort_iotag <= phba->sli.last_iotag)
  8267. abort_iocb =
  8268. phba->sli.iocbq_lookup[abort_iotag];
  8269. } else
  8270. /* For sli4 the abort_tag is the XRI,
  8271. * so the abort routine puts the iotag of the iocb
  8272. * being aborted in the context field of the abort
  8273. * IOCB.
  8274. */
  8275. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8276. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8277. "0327 Cannot abort els iocb %p "
  8278. "with tag %x context %x, abort status %x, "
  8279. "abort code %x\n",
  8280. abort_iocb, abort_iotag, abort_context,
  8281. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8282. spin_unlock_irq(&phba->hbalock);
  8283. }
  8284. lpfc_sli_release_iocbq(phba, cmdiocb);
  8285. return;
  8286. }
  8287. /**
  8288. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8289. * @phba: Pointer to HBA context object.
  8290. * @cmdiocb: Pointer to driver command iocb object.
  8291. * @rspiocb: Pointer to driver response iocb object.
  8292. *
  8293. * The function is called from SLI ring event handler with no
  8294. * lock held. This function is the completion handler for ELS commands
  8295. * which are aborted. The function frees memory resources used for
  8296. * the aborted ELS commands.
  8297. **/
  8298. static void
  8299. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8300. struct lpfc_iocbq *rspiocb)
  8301. {
  8302. IOCB_t *irsp = &rspiocb->iocb;
  8303. /* ELS cmd tag <ulpIoTag> completes */
  8304. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8305. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8306. "x%x x%x x%x\n",
  8307. irsp->ulpIoTag, irsp->ulpStatus,
  8308. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8309. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8310. lpfc_ct_free_iocb(phba, cmdiocb);
  8311. else
  8312. lpfc_els_free_iocb(phba, cmdiocb);
  8313. return;
  8314. }
  8315. /**
  8316. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8317. * @phba: Pointer to HBA context object.
  8318. * @pring: Pointer to driver SLI ring object.
  8319. * @cmdiocb: Pointer to driver command iocb object.
  8320. *
  8321. * This function issues an abort iocb for the provided command iocb down to
  8322. * the port. Other than the case the outstanding command iocb is an abort
  8323. * request, this function issues abort out unconditionally. This function is
  8324. * called with hbalock held. The function returns 0 when it fails due to
  8325. * memory allocation failure or when the command iocb is an abort request.
  8326. **/
  8327. static int
  8328. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8329. struct lpfc_iocbq *cmdiocb)
  8330. {
  8331. struct lpfc_vport *vport = cmdiocb->vport;
  8332. struct lpfc_iocbq *abtsiocbp;
  8333. IOCB_t *icmd = NULL;
  8334. IOCB_t *iabt = NULL;
  8335. int retval;
  8336. /*
  8337. * There are certain command types we don't want to abort. And we
  8338. * don't want to abort commands that are already in the process of
  8339. * being aborted.
  8340. */
  8341. icmd = &cmdiocb->iocb;
  8342. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8343. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8344. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8345. return 0;
  8346. /* issue ABTS for this IOCB based on iotag */
  8347. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8348. if (abtsiocbp == NULL)
  8349. return 0;
  8350. /* This signals the response to set the correct status
  8351. * before calling the completion handler
  8352. */
  8353. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8354. iabt = &abtsiocbp->iocb;
  8355. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8356. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8357. if (phba->sli_rev == LPFC_SLI_REV4) {
  8358. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8359. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8360. }
  8361. else
  8362. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8363. iabt->ulpLe = 1;
  8364. iabt->ulpClass = icmd->ulpClass;
  8365. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8366. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8367. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8368. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8369. if (phba->link_state >= LPFC_LINK_UP)
  8370. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8371. else
  8372. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8373. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8374. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8375. "0339 Abort xri x%x, original iotag x%x, "
  8376. "abort cmd iotag x%x\n",
  8377. iabt->un.acxri.abortIoTag,
  8378. iabt->un.acxri.abortContextTag,
  8379. abtsiocbp->iotag);
  8380. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8381. if (retval)
  8382. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8383. /*
  8384. * Caller to this routine should check for IOCB_ERROR
  8385. * and handle it properly. This routine no longer removes
  8386. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8387. */
  8388. return retval;
  8389. }
  8390. /**
  8391. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8392. * @phba: Pointer to HBA context object.
  8393. * @pring: Pointer to driver SLI ring object.
  8394. * @cmdiocb: Pointer to driver command iocb object.
  8395. *
  8396. * This function issues an abort iocb for the provided command iocb. In case
  8397. * of unloading, the abort iocb will not be issued to commands on the ELS
  8398. * ring. Instead, the callback function shall be changed to those commands
  8399. * so that nothing happens when them finishes. This function is called with
  8400. * hbalock held. The function returns 0 when the command iocb is an abort
  8401. * request.
  8402. **/
  8403. int
  8404. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8405. struct lpfc_iocbq *cmdiocb)
  8406. {
  8407. struct lpfc_vport *vport = cmdiocb->vport;
  8408. int retval = IOCB_ERROR;
  8409. IOCB_t *icmd = NULL;
  8410. /*
  8411. * There are certain command types we don't want to abort. And we
  8412. * don't want to abort commands that are already in the process of
  8413. * being aborted.
  8414. */
  8415. icmd = &cmdiocb->iocb;
  8416. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8417. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8418. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8419. return 0;
  8420. /*
  8421. * If we're unloading, don't abort iocb on the ELS ring, but change
  8422. * the callback so that nothing happens when it finishes.
  8423. */
  8424. if ((vport->load_flag & FC_UNLOADING) &&
  8425. (pring->ringno == LPFC_ELS_RING)) {
  8426. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8427. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8428. else
  8429. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8430. goto abort_iotag_exit;
  8431. }
  8432. /* Now, we try to issue the abort to the cmdiocb out */
  8433. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8434. abort_iotag_exit:
  8435. /*
  8436. * Caller to this routine should check for IOCB_ERROR
  8437. * and handle it properly. This routine no longer removes
  8438. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8439. */
  8440. return retval;
  8441. }
  8442. /**
  8443. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8444. * @phba: Pointer to HBA context object.
  8445. * @pring: Pointer to driver SLI ring object.
  8446. *
  8447. * This function aborts all iocbs in the given ring and frees all the iocb
  8448. * objects in txq. This function issues abort iocbs unconditionally for all
  8449. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8450. * to complete before the return of this function. The caller is not required
  8451. * to hold any locks.
  8452. **/
  8453. static void
  8454. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8455. {
  8456. LIST_HEAD(completions);
  8457. struct lpfc_iocbq *iocb, *next_iocb;
  8458. if (pring->ringno == LPFC_ELS_RING)
  8459. lpfc_fabric_abort_hba(phba);
  8460. spin_lock_irq(&phba->hbalock);
  8461. /* Take off all the iocbs on txq for cancelling */
  8462. list_splice_init(&pring->txq, &completions);
  8463. pring->txq_cnt = 0;
  8464. /* Next issue ABTS for everything on the txcmplq */
  8465. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8466. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8467. spin_unlock_irq(&phba->hbalock);
  8468. /* Cancel all the IOCBs from the completions list */
  8469. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8470. IOERR_SLI_ABORTED);
  8471. }
  8472. /**
  8473. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8474. * @phba: pointer to lpfc HBA data structure.
  8475. *
  8476. * This routine will abort all pending and outstanding iocbs to an HBA.
  8477. **/
  8478. void
  8479. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8480. {
  8481. struct lpfc_sli *psli = &phba->sli;
  8482. struct lpfc_sli_ring *pring;
  8483. int i;
  8484. for (i = 0; i < psli->num_rings; i++) {
  8485. pring = &psli->ring[i];
  8486. lpfc_sli_iocb_ring_abort(phba, pring);
  8487. }
  8488. }
  8489. /**
  8490. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8491. * @iocbq: Pointer to driver iocb object.
  8492. * @vport: Pointer to driver virtual port object.
  8493. * @tgt_id: SCSI ID of the target.
  8494. * @lun_id: LUN ID of the scsi device.
  8495. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8496. *
  8497. * This function acts as an iocb filter for functions which abort or count
  8498. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8499. * 0 if the filtering criteria is met for the given iocb and will return
  8500. * 1 if the filtering criteria is not met.
  8501. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8502. * given iocb is for the SCSI device specified by vport, tgt_id and
  8503. * lun_id parameter.
  8504. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8505. * given iocb is for the SCSI target specified by vport and tgt_id
  8506. * parameters.
  8507. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8508. * given iocb is for the SCSI host associated with the given vport.
  8509. * This function is called with no locks held.
  8510. **/
  8511. static int
  8512. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8513. uint16_t tgt_id, uint64_t lun_id,
  8514. lpfc_ctx_cmd ctx_cmd)
  8515. {
  8516. struct lpfc_scsi_buf *lpfc_cmd;
  8517. int rc = 1;
  8518. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8519. return rc;
  8520. if (iocbq->vport != vport)
  8521. return rc;
  8522. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8523. if (lpfc_cmd->pCmd == NULL)
  8524. return rc;
  8525. switch (ctx_cmd) {
  8526. case LPFC_CTX_LUN:
  8527. if ((lpfc_cmd->rdata->pnode) &&
  8528. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8529. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8530. rc = 0;
  8531. break;
  8532. case LPFC_CTX_TGT:
  8533. if ((lpfc_cmd->rdata->pnode) &&
  8534. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8535. rc = 0;
  8536. break;
  8537. case LPFC_CTX_HOST:
  8538. rc = 0;
  8539. break;
  8540. default:
  8541. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8542. __func__, ctx_cmd);
  8543. break;
  8544. }
  8545. return rc;
  8546. }
  8547. /**
  8548. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8549. * @vport: Pointer to virtual port.
  8550. * @tgt_id: SCSI ID of the target.
  8551. * @lun_id: LUN ID of the scsi device.
  8552. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8553. *
  8554. * This function returns number of FCP commands pending for the vport.
  8555. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8556. * commands pending on the vport associated with SCSI device specified
  8557. * by tgt_id and lun_id parameters.
  8558. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8559. * commands pending on the vport associated with SCSI target specified
  8560. * by tgt_id parameter.
  8561. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8562. * commands pending on the vport.
  8563. * This function returns the number of iocbs which satisfy the filter.
  8564. * This function is called without any lock held.
  8565. **/
  8566. int
  8567. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8568. lpfc_ctx_cmd ctx_cmd)
  8569. {
  8570. struct lpfc_hba *phba = vport->phba;
  8571. struct lpfc_iocbq *iocbq;
  8572. int sum, i;
  8573. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8574. iocbq = phba->sli.iocbq_lookup[i];
  8575. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8576. ctx_cmd) == 0)
  8577. sum++;
  8578. }
  8579. return sum;
  8580. }
  8581. /**
  8582. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8583. * @phba: Pointer to HBA context object
  8584. * @cmdiocb: Pointer to command iocb object.
  8585. * @rspiocb: Pointer to response iocb object.
  8586. *
  8587. * This function is called when an aborted FCP iocb completes. This
  8588. * function is called by the ring event handler with no lock held.
  8589. * This function frees the iocb.
  8590. **/
  8591. void
  8592. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8593. struct lpfc_iocbq *rspiocb)
  8594. {
  8595. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8596. "3096 ABORT_XRI_CN completing on xri x%x "
  8597. "original iotag x%x, abort cmd iotag x%x "
  8598. "status 0x%x, reason 0x%x\n",
  8599. cmdiocb->iocb.un.acxri.abortContextTag,
  8600. cmdiocb->iocb.un.acxri.abortIoTag,
  8601. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8602. rspiocb->iocb.un.ulpWord[4]);
  8603. lpfc_sli_release_iocbq(phba, cmdiocb);
  8604. return;
  8605. }
  8606. /**
  8607. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8608. * @vport: Pointer to virtual port.
  8609. * @pring: Pointer to driver SLI ring object.
  8610. * @tgt_id: SCSI ID of the target.
  8611. * @lun_id: LUN ID of the scsi device.
  8612. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8613. *
  8614. * This function sends an abort command for every SCSI command
  8615. * associated with the given virtual port pending on the ring
  8616. * filtered by lpfc_sli_validate_fcp_iocb function.
  8617. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8618. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8619. * parameters
  8620. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8621. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8622. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8623. * FCP iocbs associated with virtual port.
  8624. * This function returns number of iocbs it failed to abort.
  8625. * This function is called with no locks held.
  8626. **/
  8627. int
  8628. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8629. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8630. {
  8631. struct lpfc_hba *phba = vport->phba;
  8632. struct lpfc_iocbq *iocbq;
  8633. struct lpfc_iocbq *abtsiocb;
  8634. IOCB_t *cmd = NULL;
  8635. int errcnt = 0, ret_val = 0;
  8636. int i;
  8637. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8638. iocbq = phba->sli.iocbq_lookup[i];
  8639. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8640. abort_cmd) != 0)
  8641. continue;
  8642. /* issue ABTS for this IOCB based on iotag */
  8643. abtsiocb = lpfc_sli_get_iocbq(phba);
  8644. if (abtsiocb == NULL) {
  8645. errcnt++;
  8646. continue;
  8647. }
  8648. cmd = &iocbq->iocb;
  8649. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8650. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8651. if (phba->sli_rev == LPFC_SLI_REV4)
  8652. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8653. else
  8654. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8655. abtsiocb->iocb.ulpLe = 1;
  8656. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8657. abtsiocb->vport = phba->pport;
  8658. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8659. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8660. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8661. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8662. if (lpfc_is_link_up(phba))
  8663. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8664. else
  8665. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8666. /* Setup callback routine and issue the command. */
  8667. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8668. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8669. abtsiocb, 0);
  8670. if (ret_val == IOCB_ERROR) {
  8671. lpfc_sli_release_iocbq(phba, abtsiocb);
  8672. errcnt++;
  8673. continue;
  8674. }
  8675. }
  8676. return errcnt;
  8677. }
  8678. /**
  8679. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8680. * @phba: Pointer to HBA context object.
  8681. * @cmdiocbq: Pointer to command iocb.
  8682. * @rspiocbq: Pointer to response iocb.
  8683. *
  8684. * This function is the completion handler for iocbs issued using
  8685. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8686. * ring event handler function without any lock held. This function
  8687. * can be called from both worker thread context and interrupt
  8688. * context. This function also can be called from other thread which
  8689. * cleans up the SLI layer objects.
  8690. * This function copy the contents of the response iocb to the
  8691. * response iocb memory object provided by the caller of
  8692. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8693. * sleeps for the iocb completion.
  8694. **/
  8695. static void
  8696. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8697. struct lpfc_iocbq *cmdiocbq,
  8698. struct lpfc_iocbq *rspiocbq)
  8699. {
  8700. wait_queue_head_t *pdone_q;
  8701. unsigned long iflags;
  8702. struct lpfc_scsi_buf *lpfc_cmd;
  8703. spin_lock_irqsave(&phba->hbalock, iflags);
  8704. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8705. if (cmdiocbq->context2 && rspiocbq)
  8706. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8707. &rspiocbq->iocb, sizeof(IOCB_t));
  8708. /* Set the exchange busy flag for task management commands */
  8709. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8710. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8711. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8712. cur_iocbq);
  8713. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8714. }
  8715. pdone_q = cmdiocbq->context_un.wait_queue;
  8716. if (pdone_q)
  8717. wake_up(pdone_q);
  8718. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8719. return;
  8720. }
  8721. /**
  8722. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8723. * @phba: Pointer to HBA context object..
  8724. * @piocbq: Pointer to command iocb.
  8725. * @flag: Flag to test.
  8726. *
  8727. * This routine grabs the hbalock and then test the iocb_flag to
  8728. * see if the passed in flag is set.
  8729. * Returns:
  8730. * 1 if flag is set.
  8731. * 0 if flag is not set.
  8732. **/
  8733. static int
  8734. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8735. struct lpfc_iocbq *piocbq, uint32_t flag)
  8736. {
  8737. unsigned long iflags;
  8738. int ret;
  8739. spin_lock_irqsave(&phba->hbalock, iflags);
  8740. ret = piocbq->iocb_flag & flag;
  8741. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8742. return ret;
  8743. }
  8744. /**
  8745. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8746. * @phba: Pointer to HBA context object..
  8747. * @pring: Pointer to sli ring.
  8748. * @piocb: Pointer to command iocb.
  8749. * @prspiocbq: Pointer to response iocb.
  8750. * @timeout: Timeout in number of seconds.
  8751. *
  8752. * This function issues the iocb to firmware and waits for the
  8753. * iocb to complete. If the iocb command is not
  8754. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8755. * Caller should not free the iocb resources if this function
  8756. * returns IOCB_TIMEDOUT.
  8757. * The function waits for the iocb completion using an
  8758. * non-interruptible wait.
  8759. * This function will sleep while waiting for iocb completion.
  8760. * So, this function should not be called from any context which
  8761. * does not allow sleeping. Due to the same reason, this function
  8762. * cannot be called with interrupt disabled.
  8763. * This function assumes that the iocb completions occur while
  8764. * this function sleep. So, this function cannot be called from
  8765. * the thread which process iocb completion for this ring.
  8766. * This function clears the iocb_flag of the iocb object before
  8767. * issuing the iocb and the iocb completion handler sets this
  8768. * flag and wakes this thread when the iocb completes.
  8769. * The contents of the response iocb will be copied to prspiocbq
  8770. * by the completion handler when the command completes.
  8771. * This function returns IOCB_SUCCESS when success.
  8772. * This function is called with no lock held.
  8773. **/
  8774. int
  8775. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8776. uint32_t ring_number,
  8777. struct lpfc_iocbq *piocb,
  8778. struct lpfc_iocbq *prspiocbq,
  8779. uint32_t timeout)
  8780. {
  8781. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8782. long timeleft, timeout_req = 0;
  8783. int retval = IOCB_SUCCESS;
  8784. uint32_t creg_val;
  8785. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8786. /*
  8787. * If the caller has provided a response iocbq buffer, then context2
  8788. * is NULL or its an error.
  8789. */
  8790. if (prspiocbq) {
  8791. if (piocb->context2)
  8792. return IOCB_ERROR;
  8793. piocb->context2 = prspiocbq;
  8794. }
  8795. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8796. piocb->context_un.wait_queue = &done_q;
  8797. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8798. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8799. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8800. return IOCB_ERROR;
  8801. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8802. writel(creg_val, phba->HCregaddr);
  8803. readl(phba->HCregaddr); /* flush */
  8804. }
  8805. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8806. SLI_IOCB_RET_IOCB);
  8807. if (retval == IOCB_SUCCESS) {
  8808. timeout_req = timeout * HZ;
  8809. timeleft = wait_event_timeout(done_q,
  8810. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8811. timeout_req);
  8812. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8813. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8814. "0331 IOCB wake signaled\n");
  8815. } else if (timeleft == 0) {
  8816. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8817. "0338 IOCB wait timeout error - no "
  8818. "wake response Data x%x\n", timeout);
  8819. retval = IOCB_TIMEDOUT;
  8820. } else {
  8821. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8822. "0330 IOCB wake NOT set, "
  8823. "Data x%x x%lx\n",
  8824. timeout, (timeleft / jiffies));
  8825. retval = IOCB_TIMEDOUT;
  8826. }
  8827. } else if (retval == IOCB_BUSY) {
  8828. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8829. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8830. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8831. return retval;
  8832. } else {
  8833. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8834. "0332 IOCB wait issue failed, Data x%x\n",
  8835. retval);
  8836. retval = IOCB_ERROR;
  8837. }
  8838. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8839. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8840. return IOCB_ERROR;
  8841. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8842. writel(creg_val, phba->HCregaddr);
  8843. readl(phba->HCregaddr); /* flush */
  8844. }
  8845. if (prspiocbq)
  8846. piocb->context2 = NULL;
  8847. piocb->context_un.wait_queue = NULL;
  8848. piocb->iocb_cmpl = NULL;
  8849. return retval;
  8850. }
  8851. /**
  8852. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8853. * @phba: Pointer to HBA context object.
  8854. * @pmboxq: Pointer to driver mailbox object.
  8855. * @timeout: Timeout in number of seconds.
  8856. *
  8857. * This function issues the mailbox to firmware and waits for the
  8858. * mailbox command to complete. If the mailbox command is not
  8859. * completed within timeout seconds, it returns MBX_TIMEOUT.
  8860. * The function waits for the mailbox completion using an
  8861. * interruptible wait. If the thread is woken up due to a
  8862. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  8863. * should not free the mailbox resources, if this function returns
  8864. * MBX_TIMEOUT.
  8865. * This function will sleep while waiting for mailbox completion.
  8866. * So, this function should not be called from any context which
  8867. * does not allow sleeping. Due to the same reason, this function
  8868. * cannot be called with interrupt disabled.
  8869. * This function assumes that the mailbox completion occurs while
  8870. * this function sleep. So, this function cannot be called from
  8871. * the worker thread which processes mailbox completion.
  8872. * This function is called in the context of HBA management
  8873. * applications.
  8874. * This function returns MBX_SUCCESS when successful.
  8875. * This function is called with no lock held.
  8876. **/
  8877. int
  8878. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  8879. uint32_t timeout)
  8880. {
  8881. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8882. int retval;
  8883. unsigned long flag;
  8884. /* The caller must leave context1 empty. */
  8885. if (pmboxq->context1)
  8886. return MBX_NOT_FINISHED;
  8887. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  8888. /* setup wake call as IOCB callback */
  8889. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  8890. /* setup context field to pass wait_queue pointer to wake function */
  8891. pmboxq->context1 = &done_q;
  8892. /* now issue the command */
  8893. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  8894. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  8895. wait_event_interruptible_timeout(done_q,
  8896. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  8897. timeout * HZ);
  8898. spin_lock_irqsave(&phba->hbalock, flag);
  8899. pmboxq->context1 = NULL;
  8900. /*
  8901. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  8902. * else do not free the resources.
  8903. */
  8904. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  8905. retval = MBX_SUCCESS;
  8906. lpfc_sli4_swap_str(phba, pmboxq);
  8907. } else {
  8908. retval = MBX_TIMEOUT;
  8909. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8910. }
  8911. spin_unlock_irqrestore(&phba->hbalock, flag);
  8912. }
  8913. return retval;
  8914. }
  8915. /**
  8916. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  8917. * @phba: Pointer to HBA context.
  8918. *
  8919. * This function is called to shutdown the driver's mailbox sub-system.
  8920. * It first marks the mailbox sub-system is in a block state to prevent
  8921. * the asynchronous mailbox command from issued off the pending mailbox
  8922. * command queue. If the mailbox command sub-system shutdown is due to
  8923. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  8924. * the mailbox sub-system flush routine to forcefully bring down the
  8925. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  8926. * as with offline or HBA function reset), this routine will wait for the
  8927. * outstanding mailbox command to complete before invoking the mailbox
  8928. * sub-system flush routine to gracefully bring down mailbox sub-system.
  8929. **/
  8930. void
  8931. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  8932. {
  8933. struct lpfc_sli *psli = &phba->sli;
  8934. unsigned long timeout;
  8935. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  8936. spin_lock_irq(&phba->hbalock);
  8937. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  8938. spin_unlock_irq(&phba->hbalock);
  8939. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  8940. spin_lock_irq(&phba->hbalock);
  8941. /* Determine how long we might wait for the active mailbox
  8942. * command to be gracefully completed by firmware.
  8943. */
  8944. if (phba->sli.mbox_active)
  8945. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  8946. phba->sli.mbox_active) *
  8947. 1000) + jiffies;
  8948. spin_unlock_irq(&phba->hbalock);
  8949. while (phba->sli.mbox_active) {
  8950. /* Check active mailbox complete status every 2ms */
  8951. msleep(2);
  8952. if (time_after(jiffies, timeout))
  8953. /* Timeout, let the mailbox flush routine to
  8954. * forcefully release active mailbox command
  8955. */
  8956. break;
  8957. }
  8958. }
  8959. lpfc_sli_mbox_sys_flush(phba);
  8960. }
  8961. /**
  8962. * lpfc_sli_eratt_read - read sli-3 error attention events
  8963. * @phba: Pointer to HBA context.
  8964. *
  8965. * This function is called to read the SLI3 device error attention registers
  8966. * for possible error attention events. The caller must hold the hostlock
  8967. * with spin_lock_irq().
  8968. *
  8969. * This function returns 1 when there is Error Attention in the Host Attention
  8970. * Register and returns 0 otherwise.
  8971. **/
  8972. static int
  8973. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  8974. {
  8975. uint32_t ha_copy;
  8976. /* Read chip Host Attention (HA) register */
  8977. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  8978. goto unplug_err;
  8979. if (ha_copy & HA_ERATT) {
  8980. /* Read host status register to retrieve error event */
  8981. if (lpfc_sli_read_hs(phba))
  8982. goto unplug_err;
  8983. /* Check if there is a deferred error condition is active */
  8984. if ((HS_FFER1 & phba->work_hs) &&
  8985. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  8986. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  8987. phba->hba_flag |= DEFER_ERATT;
  8988. /* Clear all interrupt enable conditions */
  8989. writel(0, phba->HCregaddr);
  8990. readl(phba->HCregaddr);
  8991. }
  8992. /* Set the driver HA work bitmap */
  8993. phba->work_ha |= HA_ERATT;
  8994. /* Indicate polling handles this ERATT */
  8995. phba->hba_flag |= HBA_ERATT_HANDLED;
  8996. return 1;
  8997. }
  8998. return 0;
  8999. unplug_err:
  9000. /* Set the driver HS work bitmap */
  9001. phba->work_hs |= UNPLUG_ERR;
  9002. /* Set the driver HA work bitmap */
  9003. phba->work_ha |= HA_ERATT;
  9004. /* Indicate polling handles this ERATT */
  9005. phba->hba_flag |= HBA_ERATT_HANDLED;
  9006. return 1;
  9007. }
  9008. /**
  9009. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9010. * @phba: Pointer to HBA context.
  9011. *
  9012. * This function is called to read the SLI4 device error attention registers
  9013. * for possible error attention events. The caller must hold the hostlock
  9014. * with spin_lock_irq().
  9015. *
  9016. * This function returns 1 when there is Error Attention in the Host Attention
  9017. * Register and returns 0 otherwise.
  9018. **/
  9019. static int
  9020. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9021. {
  9022. uint32_t uerr_sta_hi, uerr_sta_lo;
  9023. uint32_t if_type, portsmphr;
  9024. struct lpfc_register portstat_reg;
  9025. /*
  9026. * For now, use the SLI4 device internal unrecoverable error
  9027. * registers for error attention. This can be changed later.
  9028. */
  9029. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9030. switch (if_type) {
  9031. case LPFC_SLI_INTF_IF_TYPE_0:
  9032. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9033. &uerr_sta_lo) ||
  9034. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9035. &uerr_sta_hi)) {
  9036. phba->work_hs |= UNPLUG_ERR;
  9037. phba->work_ha |= HA_ERATT;
  9038. phba->hba_flag |= HBA_ERATT_HANDLED;
  9039. return 1;
  9040. }
  9041. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9042. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9043. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9044. "1423 HBA Unrecoverable error: "
  9045. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9046. "ue_mask_lo_reg=0x%x, "
  9047. "ue_mask_hi_reg=0x%x\n",
  9048. uerr_sta_lo, uerr_sta_hi,
  9049. phba->sli4_hba.ue_mask_lo,
  9050. phba->sli4_hba.ue_mask_hi);
  9051. phba->work_status[0] = uerr_sta_lo;
  9052. phba->work_status[1] = uerr_sta_hi;
  9053. phba->work_ha |= HA_ERATT;
  9054. phba->hba_flag |= HBA_ERATT_HANDLED;
  9055. return 1;
  9056. }
  9057. break;
  9058. case LPFC_SLI_INTF_IF_TYPE_2:
  9059. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9060. &portstat_reg.word0) ||
  9061. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9062. &portsmphr)){
  9063. phba->work_hs |= UNPLUG_ERR;
  9064. phba->work_ha |= HA_ERATT;
  9065. phba->hba_flag |= HBA_ERATT_HANDLED;
  9066. return 1;
  9067. }
  9068. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9069. phba->work_status[0] =
  9070. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9071. phba->work_status[1] =
  9072. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9073. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9074. "2885 Port Status Event: "
  9075. "port status reg 0x%x, "
  9076. "port smphr reg 0x%x, "
  9077. "error 1=0x%x, error 2=0x%x\n",
  9078. portstat_reg.word0,
  9079. portsmphr,
  9080. phba->work_status[0],
  9081. phba->work_status[1]);
  9082. phba->work_ha |= HA_ERATT;
  9083. phba->hba_flag |= HBA_ERATT_HANDLED;
  9084. return 1;
  9085. }
  9086. break;
  9087. case LPFC_SLI_INTF_IF_TYPE_1:
  9088. default:
  9089. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9090. "2886 HBA Error Attention on unsupported "
  9091. "if type %d.", if_type);
  9092. return 1;
  9093. }
  9094. return 0;
  9095. }
  9096. /**
  9097. * lpfc_sli_check_eratt - check error attention events
  9098. * @phba: Pointer to HBA context.
  9099. *
  9100. * This function is called from timer soft interrupt context to check HBA's
  9101. * error attention register bit for error attention events.
  9102. *
  9103. * This function returns 1 when there is Error Attention in the Host Attention
  9104. * Register and returns 0 otherwise.
  9105. **/
  9106. int
  9107. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9108. {
  9109. uint32_t ha_copy;
  9110. /* If somebody is waiting to handle an eratt, don't process it
  9111. * here. The brdkill function will do this.
  9112. */
  9113. if (phba->link_flag & LS_IGNORE_ERATT)
  9114. return 0;
  9115. /* Check if interrupt handler handles this ERATT */
  9116. spin_lock_irq(&phba->hbalock);
  9117. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9118. /* Interrupt handler has handled ERATT */
  9119. spin_unlock_irq(&phba->hbalock);
  9120. return 0;
  9121. }
  9122. /*
  9123. * If there is deferred error attention, do not check for error
  9124. * attention
  9125. */
  9126. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9127. spin_unlock_irq(&phba->hbalock);
  9128. return 0;
  9129. }
  9130. /* If PCI channel is offline, don't process it */
  9131. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9132. spin_unlock_irq(&phba->hbalock);
  9133. return 0;
  9134. }
  9135. switch (phba->sli_rev) {
  9136. case LPFC_SLI_REV2:
  9137. case LPFC_SLI_REV3:
  9138. /* Read chip Host Attention (HA) register */
  9139. ha_copy = lpfc_sli_eratt_read(phba);
  9140. break;
  9141. case LPFC_SLI_REV4:
  9142. /* Read device Uncoverable Error (UERR) registers */
  9143. ha_copy = lpfc_sli4_eratt_read(phba);
  9144. break;
  9145. default:
  9146. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9147. "0299 Invalid SLI revision (%d)\n",
  9148. phba->sli_rev);
  9149. ha_copy = 0;
  9150. break;
  9151. }
  9152. spin_unlock_irq(&phba->hbalock);
  9153. return ha_copy;
  9154. }
  9155. /**
  9156. * lpfc_intr_state_check - Check device state for interrupt handling
  9157. * @phba: Pointer to HBA context.
  9158. *
  9159. * This inline routine checks whether a device or its PCI slot is in a state
  9160. * that the interrupt should be handled.
  9161. *
  9162. * This function returns 0 if the device or the PCI slot is in a state that
  9163. * interrupt should be handled, otherwise -EIO.
  9164. */
  9165. static inline int
  9166. lpfc_intr_state_check(struct lpfc_hba *phba)
  9167. {
  9168. /* If the pci channel is offline, ignore all the interrupts */
  9169. if (unlikely(pci_channel_offline(phba->pcidev)))
  9170. return -EIO;
  9171. /* Update device level interrupt statistics */
  9172. phba->sli.slistat.sli_intr++;
  9173. /* Ignore all interrupts during initialization. */
  9174. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9175. return -EIO;
  9176. return 0;
  9177. }
  9178. /**
  9179. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9180. * @irq: Interrupt number.
  9181. * @dev_id: The device context pointer.
  9182. *
  9183. * This function is directly called from the PCI layer as an interrupt
  9184. * service routine when device with SLI-3 interface spec is enabled with
  9185. * MSI-X multi-message interrupt mode and there are slow-path events in
  9186. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9187. * interrupt mode, this function is called as part of the device-level
  9188. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9189. * is undergoing initialization, the interrupt handler will not process
  9190. * the interrupt. The link attention and ELS ring attention events are
  9191. * handled by the worker thread. The interrupt handler signals the worker
  9192. * thread and returns for these events. This function is called without
  9193. * any lock held. It gets the hbalock to access and update SLI data
  9194. * structures.
  9195. *
  9196. * This function returns IRQ_HANDLED when interrupt is handled else it
  9197. * returns IRQ_NONE.
  9198. **/
  9199. irqreturn_t
  9200. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9201. {
  9202. struct lpfc_hba *phba;
  9203. uint32_t ha_copy, hc_copy;
  9204. uint32_t work_ha_copy;
  9205. unsigned long status;
  9206. unsigned long iflag;
  9207. uint32_t control;
  9208. MAILBOX_t *mbox, *pmbox;
  9209. struct lpfc_vport *vport;
  9210. struct lpfc_nodelist *ndlp;
  9211. struct lpfc_dmabuf *mp;
  9212. LPFC_MBOXQ_t *pmb;
  9213. int rc;
  9214. /*
  9215. * Get the driver's phba structure from the dev_id and
  9216. * assume the HBA is not interrupting.
  9217. */
  9218. phba = (struct lpfc_hba *)dev_id;
  9219. if (unlikely(!phba))
  9220. return IRQ_NONE;
  9221. /*
  9222. * Stuff needs to be attented to when this function is invoked as an
  9223. * individual interrupt handler in MSI-X multi-message interrupt mode
  9224. */
  9225. if (phba->intr_type == MSIX) {
  9226. /* Check device state for handling interrupt */
  9227. if (lpfc_intr_state_check(phba))
  9228. return IRQ_NONE;
  9229. /* Need to read HA REG for slow-path events */
  9230. spin_lock_irqsave(&phba->hbalock, iflag);
  9231. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9232. goto unplug_error;
  9233. /* If somebody is waiting to handle an eratt don't process it
  9234. * here. The brdkill function will do this.
  9235. */
  9236. if (phba->link_flag & LS_IGNORE_ERATT)
  9237. ha_copy &= ~HA_ERATT;
  9238. /* Check the need for handling ERATT in interrupt handler */
  9239. if (ha_copy & HA_ERATT) {
  9240. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9241. /* ERATT polling has handled ERATT */
  9242. ha_copy &= ~HA_ERATT;
  9243. else
  9244. /* Indicate interrupt handler handles ERATT */
  9245. phba->hba_flag |= HBA_ERATT_HANDLED;
  9246. }
  9247. /*
  9248. * If there is deferred error attention, do not check for any
  9249. * interrupt.
  9250. */
  9251. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9252. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9253. return IRQ_NONE;
  9254. }
  9255. /* Clear up only attention source related to slow-path */
  9256. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9257. goto unplug_error;
  9258. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9259. HC_LAINT_ENA | HC_ERINT_ENA),
  9260. phba->HCregaddr);
  9261. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9262. phba->HAregaddr);
  9263. writel(hc_copy, phba->HCregaddr);
  9264. readl(phba->HAregaddr); /* flush */
  9265. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9266. } else
  9267. ha_copy = phba->ha_copy;
  9268. work_ha_copy = ha_copy & phba->work_ha_mask;
  9269. if (work_ha_copy) {
  9270. if (work_ha_copy & HA_LATT) {
  9271. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9272. /*
  9273. * Turn off Link Attention interrupts
  9274. * until CLEAR_LA done
  9275. */
  9276. spin_lock_irqsave(&phba->hbalock, iflag);
  9277. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9278. if (lpfc_readl(phba->HCregaddr, &control))
  9279. goto unplug_error;
  9280. control &= ~HC_LAINT_ENA;
  9281. writel(control, phba->HCregaddr);
  9282. readl(phba->HCregaddr); /* flush */
  9283. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9284. }
  9285. else
  9286. work_ha_copy &= ~HA_LATT;
  9287. }
  9288. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9289. /*
  9290. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9291. * the only slow ring.
  9292. */
  9293. status = (work_ha_copy &
  9294. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9295. status >>= (4*LPFC_ELS_RING);
  9296. if (status & HA_RXMASK) {
  9297. spin_lock_irqsave(&phba->hbalock, iflag);
  9298. if (lpfc_readl(phba->HCregaddr, &control))
  9299. goto unplug_error;
  9300. lpfc_debugfs_slow_ring_trc(phba,
  9301. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9302. control, status,
  9303. (uint32_t)phba->sli.slistat.sli_intr);
  9304. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9305. lpfc_debugfs_slow_ring_trc(phba,
  9306. "ISR Disable ring:"
  9307. "pwork:x%x hawork:x%x wait:x%x",
  9308. phba->work_ha, work_ha_copy,
  9309. (uint32_t)((unsigned long)
  9310. &phba->work_waitq));
  9311. control &=
  9312. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9313. writel(control, phba->HCregaddr);
  9314. readl(phba->HCregaddr); /* flush */
  9315. }
  9316. else {
  9317. lpfc_debugfs_slow_ring_trc(phba,
  9318. "ISR slow ring: pwork:"
  9319. "x%x hawork:x%x wait:x%x",
  9320. phba->work_ha, work_ha_copy,
  9321. (uint32_t)((unsigned long)
  9322. &phba->work_waitq));
  9323. }
  9324. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9325. }
  9326. }
  9327. spin_lock_irqsave(&phba->hbalock, iflag);
  9328. if (work_ha_copy & HA_ERATT) {
  9329. if (lpfc_sli_read_hs(phba))
  9330. goto unplug_error;
  9331. /*
  9332. * Check if there is a deferred error condition
  9333. * is active
  9334. */
  9335. if ((HS_FFER1 & phba->work_hs) &&
  9336. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9337. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9338. phba->work_hs)) {
  9339. phba->hba_flag |= DEFER_ERATT;
  9340. /* Clear all interrupt enable conditions */
  9341. writel(0, phba->HCregaddr);
  9342. readl(phba->HCregaddr);
  9343. }
  9344. }
  9345. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9346. pmb = phba->sli.mbox_active;
  9347. pmbox = &pmb->u.mb;
  9348. mbox = phba->mbox;
  9349. vport = pmb->vport;
  9350. /* First check out the status word */
  9351. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9352. if (pmbox->mbxOwner != OWN_HOST) {
  9353. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9354. /*
  9355. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9356. * mbxStatus <status>
  9357. */
  9358. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9359. LOG_SLI,
  9360. "(%d):0304 Stray Mailbox "
  9361. "Interrupt mbxCommand x%x "
  9362. "mbxStatus x%x\n",
  9363. (vport ? vport->vpi : 0),
  9364. pmbox->mbxCommand,
  9365. pmbox->mbxStatus);
  9366. /* clear mailbox attention bit */
  9367. work_ha_copy &= ~HA_MBATT;
  9368. } else {
  9369. phba->sli.mbox_active = NULL;
  9370. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9371. phba->last_completion_time = jiffies;
  9372. del_timer(&phba->sli.mbox_tmo);
  9373. if (pmb->mbox_cmpl) {
  9374. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9375. MAILBOX_CMD_SIZE);
  9376. if (pmb->out_ext_byte_len &&
  9377. pmb->context2)
  9378. lpfc_sli_pcimem_bcopy(
  9379. phba->mbox_ext,
  9380. pmb->context2,
  9381. pmb->out_ext_byte_len);
  9382. }
  9383. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9384. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9385. lpfc_debugfs_disc_trc(vport,
  9386. LPFC_DISC_TRC_MBOX_VPORT,
  9387. "MBOX dflt rpi: : "
  9388. "status:x%x rpi:x%x",
  9389. (uint32_t)pmbox->mbxStatus,
  9390. pmbox->un.varWords[0], 0);
  9391. if (!pmbox->mbxStatus) {
  9392. mp = (struct lpfc_dmabuf *)
  9393. (pmb->context1);
  9394. ndlp = (struct lpfc_nodelist *)
  9395. pmb->context2;
  9396. /* Reg_LOGIN of dflt RPI was
  9397. * successful. new lets get
  9398. * rid of the RPI using the
  9399. * same mbox buffer.
  9400. */
  9401. lpfc_unreg_login(phba,
  9402. vport->vpi,
  9403. pmbox->un.varWords[0],
  9404. pmb);
  9405. pmb->mbox_cmpl =
  9406. lpfc_mbx_cmpl_dflt_rpi;
  9407. pmb->context1 = mp;
  9408. pmb->context2 = ndlp;
  9409. pmb->vport = vport;
  9410. rc = lpfc_sli_issue_mbox(phba,
  9411. pmb,
  9412. MBX_NOWAIT);
  9413. if (rc != MBX_BUSY)
  9414. lpfc_printf_log(phba,
  9415. KERN_ERR,
  9416. LOG_MBOX | LOG_SLI,
  9417. "0350 rc should have"
  9418. "been MBX_BUSY\n");
  9419. if (rc != MBX_NOT_FINISHED)
  9420. goto send_current_mbox;
  9421. }
  9422. }
  9423. spin_lock_irqsave(
  9424. &phba->pport->work_port_lock,
  9425. iflag);
  9426. phba->pport->work_port_events &=
  9427. ~WORKER_MBOX_TMO;
  9428. spin_unlock_irqrestore(
  9429. &phba->pport->work_port_lock,
  9430. iflag);
  9431. lpfc_mbox_cmpl_put(phba, pmb);
  9432. }
  9433. } else
  9434. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9435. if ((work_ha_copy & HA_MBATT) &&
  9436. (phba->sli.mbox_active == NULL)) {
  9437. send_current_mbox:
  9438. /* Process next mailbox command if there is one */
  9439. do {
  9440. rc = lpfc_sli_issue_mbox(phba, NULL,
  9441. MBX_NOWAIT);
  9442. } while (rc == MBX_NOT_FINISHED);
  9443. if (rc != MBX_SUCCESS)
  9444. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9445. LOG_SLI, "0349 rc should be "
  9446. "MBX_SUCCESS\n");
  9447. }
  9448. spin_lock_irqsave(&phba->hbalock, iflag);
  9449. phba->work_ha |= work_ha_copy;
  9450. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9451. lpfc_worker_wake_up(phba);
  9452. }
  9453. return IRQ_HANDLED;
  9454. unplug_error:
  9455. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9456. return IRQ_HANDLED;
  9457. } /* lpfc_sli_sp_intr_handler */
  9458. /**
  9459. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9460. * @irq: Interrupt number.
  9461. * @dev_id: The device context pointer.
  9462. *
  9463. * This function is directly called from the PCI layer as an interrupt
  9464. * service routine when device with SLI-3 interface spec is enabled with
  9465. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9466. * ring event in the HBA. However, when the device is enabled with either
  9467. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9468. * device-level interrupt handler. When the PCI slot is in error recovery
  9469. * or the HBA is undergoing initialization, the interrupt handler will not
  9470. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9471. * the intrrupt context. This function is called without any lock held.
  9472. * It gets the hbalock to access and update SLI data structures.
  9473. *
  9474. * This function returns IRQ_HANDLED when interrupt is handled else it
  9475. * returns IRQ_NONE.
  9476. **/
  9477. irqreturn_t
  9478. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9479. {
  9480. struct lpfc_hba *phba;
  9481. uint32_t ha_copy;
  9482. unsigned long status;
  9483. unsigned long iflag;
  9484. /* Get the driver's phba structure from the dev_id and
  9485. * assume the HBA is not interrupting.
  9486. */
  9487. phba = (struct lpfc_hba *) dev_id;
  9488. if (unlikely(!phba))
  9489. return IRQ_NONE;
  9490. /*
  9491. * Stuff needs to be attented to when this function is invoked as an
  9492. * individual interrupt handler in MSI-X multi-message interrupt mode
  9493. */
  9494. if (phba->intr_type == MSIX) {
  9495. /* Check device state for handling interrupt */
  9496. if (lpfc_intr_state_check(phba))
  9497. return IRQ_NONE;
  9498. /* Need to read HA REG for FCP ring and other ring events */
  9499. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9500. return IRQ_HANDLED;
  9501. /* Clear up only attention source related to fast-path */
  9502. spin_lock_irqsave(&phba->hbalock, iflag);
  9503. /*
  9504. * If there is deferred error attention, do not check for
  9505. * any interrupt.
  9506. */
  9507. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9508. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9509. return IRQ_NONE;
  9510. }
  9511. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9512. phba->HAregaddr);
  9513. readl(phba->HAregaddr); /* flush */
  9514. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9515. } else
  9516. ha_copy = phba->ha_copy;
  9517. /*
  9518. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9519. */
  9520. ha_copy &= ~(phba->work_ha_mask);
  9521. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9522. status >>= (4*LPFC_FCP_RING);
  9523. if (status & HA_RXMASK)
  9524. lpfc_sli_handle_fast_ring_event(phba,
  9525. &phba->sli.ring[LPFC_FCP_RING],
  9526. status);
  9527. if (phba->cfg_multi_ring_support == 2) {
  9528. /*
  9529. * Process all events on extra ring. Take the optimized path
  9530. * for extra ring IO.
  9531. */
  9532. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9533. status >>= (4*LPFC_EXTRA_RING);
  9534. if (status & HA_RXMASK) {
  9535. lpfc_sli_handle_fast_ring_event(phba,
  9536. &phba->sli.ring[LPFC_EXTRA_RING],
  9537. status);
  9538. }
  9539. }
  9540. return IRQ_HANDLED;
  9541. } /* lpfc_sli_fp_intr_handler */
  9542. /**
  9543. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9544. * @irq: Interrupt number.
  9545. * @dev_id: The device context pointer.
  9546. *
  9547. * This function is the HBA device-level interrupt handler to device with
  9548. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9549. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9550. * requires driver attention. This function invokes the slow-path interrupt
  9551. * attention handling function and fast-path interrupt attention handling
  9552. * function in turn to process the relevant HBA attention events. This
  9553. * function is called without any lock held. It gets the hbalock to access
  9554. * and update SLI data structures.
  9555. *
  9556. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9557. * returns IRQ_NONE.
  9558. **/
  9559. irqreturn_t
  9560. lpfc_sli_intr_handler(int irq, void *dev_id)
  9561. {
  9562. struct lpfc_hba *phba;
  9563. irqreturn_t sp_irq_rc, fp_irq_rc;
  9564. unsigned long status1, status2;
  9565. uint32_t hc_copy;
  9566. /*
  9567. * Get the driver's phba structure from the dev_id and
  9568. * assume the HBA is not interrupting.
  9569. */
  9570. phba = (struct lpfc_hba *) dev_id;
  9571. if (unlikely(!phba))
  9572. return IRQ_NONE;
  9573. /* Check device state for handling interrupt */
  9574. if (lpfc_intr_state_check(phba))
  9575. return IRQ_NONE;
  9576. spin_lock(&phba->hbalock);
  9577. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9578. spin_unlock(&phba->hbalock);
  9579. return IRQ_HANDLED;
  9580. }
  9581. if (unlikely(!phba->ha_copy)) {
  9582. spin_unlock(&phba->hbalock);
  9583. return IRQ_NONE;
  9584. } else if (phba->ha_copy & HA_ERATT) {
  9585. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9586. /* ERATT polling has handled ERATT */
  9587. phba->ha_copy &= ~HA_ERATT;
  9588. else
  9589. /* Indicate interrupt handler handles ERATT */
  9590. phba->hba_flag |= HBA_ERATT_HANDLED;
  9591. }
  9592. /*
  9593. * If there is deferred error attention, do not check for any interrupt.
  9594. */
  9595. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9596. spin_unlock(&phba->hbalock);
  9597. return IRQ_NONE;
  9598. }
  9599. /* Clear attention sources except link and error attentions */
  9600. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9601. spin_unlock(&phba->hbalock);
  9602. return IRQ_HANDLED;
  9603. }
  9604. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9605. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9606. phba->HCregaddr);
  9607. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9608. writel(hc_copy, phba->HCregaddr);
  9609. readl(phba->HAregaddr); /* flush */
  9610. spin_unlock(&phba->hbalock);
  9611. /*
  9612. * Invokes slow-path host attention interrupt handling as appropriate.
  9613. */
  9614. /* status of events with mailbox and link attention */
  9615. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9616. /* status of events with ELS ring */
  9617. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9618. status2 >>= (4*LPFC_ELS_RING);
  9619. if (status1 || (status2 & HA_RXMASK))
  9620. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9621. else
  9622. sp_irq_rc = IRQ_NONE;
  9623. /*
  9624. * Invoke fast-path host attention interrupt handling as appropriate.
  9625. */
  9626. /* status of events with FCP ring */
  9627. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9628. status1 >>= (4*LPFC_FCP_RING);
  9629. /* status of events with extra ring */
  9630. if (phba->cfg_multi_ring_support == 2) {
  9631. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9632. status2 >>= (4*LPFC_EXTRA_RING);
  9633. } else
  9634. status2 = 0;
  9635. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9636. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9637. else
  9638. fp_irq_rc = IRQ_NONE;
  9639. /* Return device-level interrupt handling status */
  9640. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9641. } /* lpfc_sli_intr_handler */
  9642. /**
  9643. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9644. * @phba: pointer to lpfc hba data structure.
  9645. *
  9646. * This routine is invoked by the worker thread to process all the pending
  9647. * SLI4 FCP abort XRI events.
  9648. **/
  9649. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9650. {
  9651. struct lpfc_cq_event *cq_event;
  9652. /* First, declare the fcp xri abort event has been handled */
  9653. spin_lock_irq(&phba->hbalock);
  9654. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9655. spin_unlock_irq(&phba->hbalock);
  9656. /* Now, handle all the fcp xri abort events */
  9657. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9658. /* Get the first event from the head of the event queue */
  9659. spin_lock_irq(&phba->hbalock);
  9660. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9661. cq_event, struct lpfc_cq_event, list);
  9662. spin_unlock_irq(&phba->hbalock);
  9663. /* Notify aborted XRI for FCP work queue */
  9664. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9665. /* Free the event processed back to the free pool */
  9666. lpfc_sli4_cq_event_release(phba, cq_event);
  9667. }
  9668. }
  9669. /**
  9670. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9671. * @phba: pointer to lpfc hba data structure.
  9672. *
  9673. * This routine is invoked by the worker thread to process all the pending
  9674. * SLI4 els abort xri events.
  9675. **/
  9676. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9677. {
  9678. struct lpfc_cq_event *cq_event;
  9679. /* First, declare the els xri abort event has been handled */
  9680. spin_lock_irq(&phba->hbalock);
  9681. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9682. spin_unlock_irq(&phba->hbalock);
  9683. /* Now, handle all the els xri abort events */
  9684. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9685. /* Get the first event from the head of the event queue */
  9686. spin_lock_irq(&phba->hbalock);
  9687. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9688. cq_event, struct lpfc_cq_event, list);
  9689. spin_unlock_irq(&phba->hbalock);
  9690. /* Notify aborted XRI for ELS work queue */
  9691. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9692. /* Free the event processed back to the free pool */
  9693. lpfc_sli4_cq_event_release(phba, cq_event);
  9694. }
  9695. }
  9696. /**
  9697. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9698. * @phba: pointer to lpfc hba data structure
  9699. * @pIocbIn: pointer to the rspiocbq
  9700. * @pIocbOut: pointer to the cmdiocbq
  9701. * @wcqe: pointer to the complete wcqe
  9702. *
  9703. * This routine transfers the fields of a command iocbq to a response iocbq
  9704. * by copying all the IOCB fields from command iocbq and transferring the
  9705. * completion status information from the complete wcqe.
  9706. **/
  9707. static void
  9708. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9709. struct lpfc_iocbq *pIocbIn,
  9710. struct lpfc_iocbq *pIocbOut,
  9711. struct lpfc_wcqe_complete *wcqe)
  9712. {
  9713. unsigned long iflags;
  9714. uint32_t status;
  9715. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9716. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9717. sizeof(struct lpfc_iocbq) - offset);
  9718. /* Map WCQE parameters into irspiocb parameters */
  9719. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9720. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9721. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9722. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9723. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9724. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9725. wcqe->total_data_placed;
  9726. else
  9727. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9728. else {
  9729. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9730. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9731. }
  9732. /* Convert BG errors for completion status */
  9733. if (status == CQE_STATUS_DI_ERROR) {
  9734. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9735. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9736. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9737. else
  9738. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9739. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9740. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9741. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9742. BGS_GUARD_ERR_MASK;
  9743. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9744. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9745. BGS_APPTAG_ERR_MASK;
  9746. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9747. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9748. BGS_REFTAG_ERR_MASK;
  9749. /* Check to see if there was any good data before the error */
  9750. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9751. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9752. BGS_HI_WATER_MARK_PRESENT_MASK;
  9753. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9754. wcqe->total_data_placed;
  9755. }
  9756. /*
  9757. * Set ALL the error bits to indicate we don't know what
  9758. * type of error it is.
  9759. */
  9760. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9761. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9762. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9763. BGS_GUARD_ERR_MASK);
  9764. }
  9765. /* Pick up HBA exchange busy condition */
  9766. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9767. spin_lock_irqsave(&phba->hbalock, iflags);
  9768. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9769. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9770. }
  9771. }
  9772. /**
  9773. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9774. * @phba: Pointer to HBA context object.
  9775. * @wcqe: Pointer to work-queue completion queue entry.
  9776. *
  9777. * This routine handles an ELS work-queue completion event and construct
  9778. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9779. * discovery engine to handle.
  9780. *
  9781. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9782. **/
  9783. static struct lpfc_iocbq *
  9784. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9785. struct lpfc_iocbq *irspiocbq)
  9786. {
  9787. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9788. struct lpfc_iocbq *cmdiocbq;
  9789. struct lpfc_wcqe_complete *wcqe;
  9790. unsigned long iflags;
  9791. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9792. spin_lock_irqsave(&phba->hbalock, iflags);
  9793. pring->stats.iocb_event++;
  9794. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9795. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9796. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9797. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9798. if (unlikely(!cmdiocbq)) {
  9799. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9800. "0386 ELS complete with no corresponding "
  9801. "cmdiocb: iotag (%d)\n",
  9802. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9803. lpfc_sli_release_iocbq(phba, irspiocbq);
  9804. return NULL;
  9805. }
  9806. /* Fake the irspiocbq and copy necessary response information */
  9807. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9808. return irspiocbq;
  9809. }
  9810. /**
  9811. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9812. * @phba: Pointer to HBA context object.
  9813. * @cqe: Pointer to mailbox completion queue entry.
  9814. *
  9815. * This routine process a mailbox completion queue entry with asynchrous
  9816. * event.
  9817. *
  9818. * Return: true if work posted to worker thread, otherwise false.
  9819. **/
  9820. static bool
  9821. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9822. {
  9823. struct lpfc_cq_event *cq_event;
  9824. unsigned long iflags;
  9825. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9826. "0392 Async Event: word0:x%x, word1:x%x, "
  9827. "word2:x%x, word3:x%x\n", mcqe->word0,
  9828. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9829. /* Allocate a new internal CQ_EVENT entry */
  9830. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9831. if (!cq_event) {
  9832. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9833. "0394 Failed to allocate CQ_EVENT entry\n");
  9834. return false;
  9835. }
  9836. /* Move the CQE into an asynchronous event entry */
  9837. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9838. spin_lock_irqsave(&phba->hbalock, iflags);
  9839. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9840. /* Set the async event flag */
  9841. phba->hba_flag |= ASYNC_EVENT;
  9842. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9843. return true;
  9844. }
  9845. /**
  9846. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9847. * @phba: Pointer to HBA context object.
  9848. * @cqe: Pointer to mailbox completion queue entry.
  9849. *
  9850. * This routine process a mailbox completion queue entry with mailbox
  9851. * completion event.
  9852. *
  9853. * Return: true if work posted to worker thread, otherwise false.
  9854. **/
  9855. static bool
  9856. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9857. {
  9858. uint32_t mcqe_status;
  9859. MAILBOX_t *mbox, *pmbox;
  9860. struct lpfc_mqe *mqe;
  9861. struct lpfc_vport *vport;
  9862. struct lpfc_nodelist *ndlp;
  9863. struct lpfc_dmabuf *mp;
  9864. unsigned long iflags;
  9865. LPFC_MBOXQ_t *pmb;
  9866. bool workposted = false;
  9867. int rc;
  9868. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  9869. if (!bf_get(lpfc_trailer_completed, mcqe))
  9870. goto out_no_mqe_complete;
  9871. /* Get the reference to the active mbox command */
  9872. spin_lock_irqsave(&phba->hbalock, iflags);
  9873. pmb = phba->sli.mbox_active;
  9874. if (unlikely(!pmb)) {
  9875. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9876. "1832 No pending MBOX command to handle\n");
  9877. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9878. goto out_no_mqe_complete;
  9879. }
  9880. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9881. mqe = &pmb->u.mqe;
  9882. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  9883. mbox = phba->mbox;
  9884. vport = pmb->vport;
  9885. /* Reset heartbeat timer */
  9886. phba->last_completion_time = jiffies;
  9887. del_timer(&phba->sli.mbox_tmo);
  9888. /* Move mbox data to caller's mailbox region, do endian swapping */
  9889. if (pmb->mbox_cmpl && mbox)
  9890. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  9891. /*
  9892. * For mcqe errors, conditionally move a modified error code to
  9893. * the mbox so that the error will not be missed.
  9894. */
  9895. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  9896. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  9897. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  9898. bf_set(lpfc_mqe_status, mqe,
  9899. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  9900. }
  9901. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9902. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9903. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  9904. "MBOX dflt rpi: status:x%x rpi:x%x",
  9905. mcqe_status,
  9906. pmbox->un.varWords[0], 0);
  9907. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  9908. mp = (struct lpfc_dmabuf *)(pmb->context1);
  9909. ndlp = (struct lpfc_nodelist *)pmb->context2;
  9910. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  9911. * RID of the PPI using the same mbox buffer.
  9912. */
  9913. lpfc_unreg_login(phba, vport->vpi,
  9914. pmbox->un.varWords[0], pmb);
  9915. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  9916. pmb->context1 = mp;
  9917. pmb->context2 = ndlp;
  9918. pmb->vport = vport;
  9919. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  9920. if (rc != MBX_BUSY)
  9921. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9922. LOG_SLI, "0385 rc should "
  9923. "have been MBX_BUSY\n");
  9924. if (rc != MBX_NOT_FINISHED)
  9925. goto send_current_mbox;
  9926. }
  9927. }
  9928. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  9929. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  9930. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  9931. /* There is mailbox completion work to do */
  9932. spin_lock_irqsave(&phba->hbalock, iflags);
  9933. __lpfc_mbox_cmpl_put(phba, pmb);
  9934. phba->work_ha |= HA_MBATT;
  9935. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9936. workposted = true;
  9937. send_current_mbox:
  9938. spin_lock_irqsave(&phba->hbalock, iflags);
  9939. /* Release the mailbox command posting token */
  9940. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  9941. /* Setting active mailbox pointer need to be in sync to flag clear */
  9942. phba->sli.mbox_active = NULL;
  9943. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9944. /* Wake up worker thread to post the next pending mailbox command */
  9945. lpfc_worker_wake_up(phba);
  9946. out_no_mqe_complete:
  9947. if (bf_get(lpfc_trailer_consumed, mcqe))
  9948. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  9949. return workposted;
  9950. }
  9951. /**
  9952. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  9953. * @phba: Pointer to HBA context object.
  9954. * @cqe: Pointer to mailbox completion queue entry.
  9955. *
  9956. * This routine process a mailbox completion queue entry, it invokes the
  9957. * proper mailbox complete handling or asynchrous event handling routine
  9958. * according to the MCQE's async bit.
  9959. *
  9960. * Return: true if work posted to worker thread, otherwise false.
  9961. **/
  9962. static bool
  9963. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  9964. {
  9965. struct lpfc_mcqe mcqe;
  9966. bool workposted;
  9967. /* Copy the mailbox MCQE and convert endian order as needed */
  9968. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  9969. /* Invoke the proper event handling routine */
  9970. if (!bf_get(lpfc_trailer_async, &mcqe))
  9971. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  9972. else
  9973. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  9974. return workposted;
  9975. }
  9976. /**
  9977. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  9978. * @phba: Pointer to HBA context object.
  9979. * @wcqe: Pointer to work-queue completion queue entry.
  9980. *
  9981. * This routine handles an ELS work-queue completion event.
  9982. *
  9983. * Return: true if work posted to worker thread, otherwise false.
  9984. **/
  9985. static bool
  9986. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  9987. struct lpfc_wcqe_complete *wcqe)
  9988. {
  9989. struct lpfc_iocbq *irspiocbq;
  9990. unsigned long iflags;
  9991. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  9992. /* Get an irspiocbq for later ELS response processing use */
  9993. irspiocbq = lpfc_sli_get_iocbq(phba);
  9994. if (!irspiocbq) {
  9995. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9996. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  9997. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  9998. pring->txq_cnt, phba->iocb_cnt,
  9999. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10000. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10001. return false;
  10002. }
  10003. /* Save off the slow-path queue event for work thread to process */
  10004. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10005. spin_lock_irqsave(&phba->hbalock, iflags);
  10006. list_add_tail(&irspiocbq->cq_event.list,
  10007. &phba->sli4_hba.sp_queue_event);
  10008. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10009. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10010. return true;
  10011. }
  10012. /**
  10013. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10014. * @phba: Pointer to HBA context object.
  10015. * @wcqe: Pointer to work-queue completion queue entry.
  10016. *
  10017. * This routine handles slow-path WQ entry comsumed event by invoking the
  10018. * proper WQ release routine to the slow-path WQ.
  10019. **/
  10020. static void
  10021. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10022. struct lpfc_wcqe_release *wcqe)
  10023. {
  10024. /* sanity check on queue memory */
  10025. if (unlikely(!phba->sli4_hba.els_wq))
  10026. return;
  10027. /* Check for the slow-path ELS work queue */
  10028. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10029. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10030. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10031. else
  10032. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10033. "2579 Slow-path wqe consume event carries "
  10034. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10035. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10036. phba->sli4_hba.els_wq->queue_id);
  10037. }
  10038. /**
  10039. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10040. * @phba: Pointer to HBA context object.
  10041. * @cq: Pointer to a WQ completion queue.
  10042. * @wcqe: Pointer to work-queue completion queue entry.
  10043. *
  10044. * This routine handles an XRI abort event.
  10045. *
  10046. * Return: true if work posted to worker thread, otherwise false.
  10047. **/
  10048. static bool
  10049. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10050. struct lpfc_queue *cq,
  10051. struct sli4_wcqe_xri_aborted *wcqe)
  10052. {
  10053. bool workposted = false;
  10054. struct lpfc_cq_event *cq_event;
  10055. unsigned long iflags;
  10056. /* Allocate a new internal CQ_EVENT entry */
  10057. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10058. if (!cq_event) {
  10059. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10060. "0602 Failed to allocate CQ_EVENT entry\n");
  10061. return false;
  10062. }
  10063. /* Move the CQE into the proper xri abort event list */
  10064. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10065. switch (cq->subtype) {
  10066. case LPFC_FCP:
  10067. spin_lock_irqsave(&phba->hbalock, iflags);
  10068. list_add_tail(&cq_event->list,
  10069. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10070. /* Set the fcp xri abort event flag */
  10071. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10072. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10073. workposted = true;
  10074. break;
  10075. case LPFC_ELS:
  10076. spin_lock_irqsave(&phba->hbalock, iflags);
  10077. list_add_tail(&cq_event->list,
  10078. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10079. /* Set the els xri abort event flag */
  10080. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10081. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10082. workposted = true;
  10083. break;
  10084. default:
  10085. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10086. "0603 Invalid work queue CQE subtype (x%x)\n",
  10087. cq->subtype);
  10088. workposted = false;
  10089. break;
  10090. }
  10091. return workposted;
  10092. }
  10093. /**
  10094. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10095. * @phba: Pointer to HBA context object.
  10096. * @rcqe: Pointer to receive-queue completion queue entry.
  10097. *
  10098. * This routine process a receive-queue completion queue entry.
  10099. *
  10100. * Return: true if work posted to worker thread, otherwise false.
  10101. **/
  10102. static bool
  10103. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10104. {
  10105. bool workposted = false;
  10106. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10107. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10108. struct hbq_dmabuf *dma_buf;
  10109. uint32_t status, rq_id;
  10110. unsigned long iflags;
  10111. /* sanity check on queue memory */
  10112. if (unlikely(!hrq) || unlikely(!drq))
  10113. return workposted;
  10114. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10115. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10116. else
  10117. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10118. if (rq_id != hrq->queue_id)
  10119. goto out;
  10120. status = bf_get(lpfc_rcqe_status, rcqe);
  10121. switch (status) {
  10122. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10123. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10124. "2537 Receive Frame Truncated!!\n");
  10125. case FC_STATUS_RQ_SUCCESS:
  10126. lpfc_sli4_rq_release(hrq, drq);
  10127. spin_lock_irqsave(&phba->hbalock, iflags);
  10128. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10129. if (!dma_buf) {
  10130. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10131. goto out;
  10132. }
  10133. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10134. /* save off the frame for the word thread to process */
  10135. list_add_tail(&dma_buf->cq_event.list,
  10136. &phba->sli4_hba.sp_queue_event);
  10137. /* Frame received */
  10138. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10139. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10140. workposted = true;
  10141. break;
  10142. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10143. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10144. /* Post more buffers if possible */
  10145. spin_lock_irqsave(&phba->hbalock, iflags);
  10146. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10147. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10148. workposted = true;
  10149. break;
  10150. }
  10151. out:
  10152. return workposted;
  10153. }
  10154. /**
  10155. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10156. * @phba: Pointer to HBA context object.
  10157. * @cq: Pointer to the completion queue.
  10158. * @wcqe: Pointer to a completion queue entry.
  10159. *
  10160. * This routine process a slow-path work-queue or receive queue completion queue
  10161. * entry.
  10162. *
  10163. * Return: true if work posted to worker thread, otherwise false.
  10164. **/
  10165. static bool
  10166. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10167. struct lpfc_cqe *cqe)
  10168. {
  10169. struct lpfc_cqe cqevt;
  10170. bool workposted = false;
  10171. /* Copy the work queue CQE and convert endian order if needed */
  10172. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10173. /* Check and process for different type of WCQE and dispatch */
  10174. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10175. case CQE_CODE_COMPL_WQE:
  10176. /* Process the WQ/RQ complete event */
  10177. phba->last_completion_time = jiffies;
  10178. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10179. (struct lpfc_wcqe_complete *)&cqevt);
  10180. break;
  10181. case CQE_CODE_RELEASE_WQE:
  10182. /* Process the WQ release event */
  10183. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10184. (struct lpfc_wcqe_release *)&cqevt);
  10185. break;
  10186. case CQE_CODE_XRI_ABORTED:
  10187. /* Process the WQ XRI abort event */
  10188. phba->last_completion_time = jiffies;
  10189. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10190. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10191. break;
  10192. case CQE_CODE_RECEIVE:
  10193. case CQE_CODE_RECEIVE_V1:
  10194. /* Process the RQ event */
  10195. phba->last_completion_time = jiffies;
  10196. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10197. (struct lpfc_rcqe *)&cqevt);
  10198. break;
  10199. default:
  10200. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10201. "0388 Not a valid WCQE code: x%x\n",
  10202. bf_get(lpfc_cqe_code, &cqevt));
  10203. break;
  10204. }
  10205. return workposted;
  10206. }
  10207. /**
  10208. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10209. * @phba: Pointer to HBA context object.
  10210. * @eqe: Pointer to fast-path event queue entry.
  10211. *
  10212. * This routine process a event queue entry from the slow-path event queue.
  10213. * It will check the MajorCode and MinorCode to determine this is for a
  10214. * completion event on a completion queue, if not, an error shall be logged
  10215. * and just return. Otherwise, it will get to the corresponding completion
  10216. * queue and process all the entries on that completion queue, rearm the
  10217. * completion queue, and then return.
  10218. *
  10219. **/
  10220. static void
  10221. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10222. {
  10223. struct lpfc_queue *cq = NULL, *childq, *speq;
  10224. struct lpfc_cqe *cqe;
  10225. bool workposted = false;
  10226. int ecount = 0;
  10227. uint16_t cqid;
  10228. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10229. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10230. "0359 Not a valid slow-path completion "
  10231. "event: majorcode=x%x, minorcode=x%x\n",
  10232. bf_get_le32(lpfc_eqe_major_code, eqe),
  10233. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10234. return;
  10235. }
  10236. /* Get the reference to the corresponding CQ */
  10237. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10238. /* Search for completion queue pointer matching this cqid */
  10239. speq = phba->sli4_hba.sp_eq;
  10240. /* sanity check on queue memory */
  10241. if (unlikely(!speq))
  10242. return;
  10243. list_for_each_entry(childq, &speq->child_list, list) {
  10244. if (childq->queue_id == cqid) {
  10245. cq = childq;
  10246. break;
  10247. }
  10248. }
  10249. if (unlikely(!cq)) {
  10250. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10251. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10252. "0365 Slow-path CQ identifier "
  10253. "(%d) does not exist\n", cqid);
  10254. return;
  10255. }
  10256. /* Process all the entries to the CQ */
  10257. switch (cq->type) {
  10258. case LPFC_MCQ:
  10259. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10260. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10261. if (!(++ecount % cq->entry_repost))
  10262. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10263. }
  10264. break;
  10265. case LPFC_WCQ:
  10266. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10267. if (cq->subtype == LPFC_FCP)
  10268. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10269. cqe);
  10270. else
  10271. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10272. cqe);
  10273. if (!(++ecount % cq->entry_repost))
  10274. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10275. }
  10276. break;
  10277. default:
  10278. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10279. "0370 Invalid completion queue type (%d)\n",
  10280. cq->type);
  10281. return;
  10282. }
  10283. /* Catch the no cq entry condition, log an error */
  10284. if (unlikely(ecount == 0))
  10285. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10286. "0371 No entry from the CQ: identifier "
  10287. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10288. /* In any case, flash and re-arm the RCQ */
  10289. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10290. /* wake up worker thread if there are works to be done */
  10291. if (workposted)
  10292. lpfc_worker_wake_up(phba);
  10293. }
  10294. /**
  10295. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10296. * @eqe: Pointer to fast-path completion queue entry.
  10297. *
  10298. * This routine process a fast-path work queue completion entry from fast-path
  10299. * event queue for FCP command response completion.
  10300. **/
  10301. static void
  10302. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10303. struct lpfc_wcqe_complete *wcqe)
  10304. {
  10305. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10306. struct lpfc_iocbq *cmdiocbq;
  10307. struct lpfc_iocbq irspiocbq;
  10308. unsigned long iflags;
  10309. spin_lock_irqsave(&phba->hbalock, iflags);
  10310. pring->stats.iocb_event++;
  10311. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10312. /* Check for response status */
  10313. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10314. /* If resource errors reported from HBA, reduce queue
  10315. * depth of the SCSI device.
  10316. */
  10317. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10318. IOSTAT_LOCAL_REJECT) &&
  10319. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10320. phba->lpfc_rampdown_queue_depth(phba);
  10321. }
  10322. /* Log the error status */
  10323. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10324. "0373 FCP complete error: status=x%x, "
  10325. "hw_status=x%x, total_data_specified=%d, "
  10326. "parameter=x%x, word3=x%x\n",
  10327. bf_get(lpfc_wcqe_c_status, wcqe),
  10328. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10329. wcqe->total_data_placed, wcqe->parameter,
  10330. wcqe->word3);
  10331. }
  10332. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10333. spin_lock_irqsave(&phba->hbalock, iflags);
  10334. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10335. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10336. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10337. if (unlikely(!cmdiocbq)) {
  10338. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10339. "0374 FCP complete with no corresponding "
  10340. "cmdiocb: iotag (%d)\n",
  10341. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10342. return;
  10343. }
  10344. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10345. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10346. "0375 FCP cmdiocb not callback function "
  10347. "iotag: (%d)\n",
  10348. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10349. return;
  10350. }
  10351. /* Fake the irspiocb and copy necessary response information */
  10352. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10353. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10354. spin_lock_irqsave(&phba->hbalock, iflags);
  10355. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10356. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10357. }
  10358. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10359. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10360. }
  10361. /**
  10362. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10363. * @phba: Pointer to HBA context object.
  10364. * @cq: Pointer to completion queue.
  10365. * @wcqe: Pointer to work-queue completion queue entry.
  10366. *
  10367. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10368. * proper WQ release routine to the slow-path WQ.
  10369. **/
  10370. static void
  10371. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10372. struct lpfc_wcqe_release *wcqe)
  10373. {
  10374. struct lpfc_queue *childwq;
  10375. bool wqid_matched = false;
  10376. uint16_t fcp_wqid;
  10377. /* Check for fast-path FCP work queue release */
  10378. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10379. list_for_each_entry(childwq, &cq->child_list, list) {
  10380. if (childwq->queue_id == fcp_wqid) {
  10381. lpfc_sli4_wq_release(childwq,
  10382. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10383. wqid_matched = true;
  10384. break;
  10385. }
  10386. }
  10387. /* Report warning log message if no match found */
  10388. if (wqid_matched != true)
  10389. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10390. "2580 Fast-path wqe consume event carries "
  10391. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10392. }
  10393. /**
  10394. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10395. * @cq: Pointer to the completion queue.
  10396. * @eqe: Pointer to fast-path completion queue entry.
  10397. *
  10398. * This routine process a fast-path work queue completion entry from fast-path
  10399. * event queue for FCP command response completion.
  10400. **/
  10401. static int
  10402. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10403. struct lpfc_cqe *cqe)
  10404. {
  10405. struct lpfc_wcqe_release wcqe;
  10406. bool workposted = false;
  10407. /* Copy the work queue CQE and convert endian order if needed */
  10408. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10409. /* Check and process for different type of WCQE and dispatch */
  10410. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10411. case CQE_CODE_COMPL_WQE:
  10412. /* Process the WQ complete event */
  10413. phba->last_completion_time = jiffies;
  10414. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10415. (struct lpfc_wcqe_complete *)&wcqe);
  10416. break;
  10417. case CQE_CODE_RELEASE_WQE:
  10418. /* Process the WQ release event */
  10419. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10420. (struct lpfc_wcqe_release *)&wcqe);
  10421. break;
  10422. case CQE_CODE_XRI_ABORTED:
  10423. /* Process the WQ XRI abort event */
  10424. phba->last_completion_time = jiffies;
  10425. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10426. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10427. break;
  10428. default:
  10429. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10430. "0144 Not a valid WCQE code: x%x\n",
  10431. bf_get(lpfc_wcqe_c_code, &wcqe));
  10432. break;
  10433. }
  10434. return workposted;
  10435. }
  10436. /**
  10437. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10438. * @phba: Pointer to HBA context object.
  10439. * @eqe: Pointer to fast-path event queue entry.
  10440. *
  10441. * This routine process a event queue entry from the fast-path event queue.
  10442. * It will check the MajorCode and MinorCode to determine this is for a
  10443. * completion event on a completion queue, if not, an error shall be logged
  10444. * and just return. Otherwise, it will get to the corresponding completion
  10445. * queue and process all the entries on the completion queue, rearm the
  10446. * completion queue, and then return.
  10447. **/
  10448. static void
  10449. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10450. uint32_t fcp_cqidx)
  10451. {
  10452. struct lpfc_queue *cq;
  10453. struct lpfc_cqe *cqe;
  10454. bool workposted = false;
  10455. uint16_t cqid;
  10456. int ecount = 0;
  10457. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10458. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10459. "0366 Not a valid fast-path completion "
  10460. "event: majorcode=x%x, minorcode=x%x\n",
  10461. bf_get_le32(lpfc_eqe_major_code, eqe),
  10462. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10463. return;
  10464. }
  10465. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10466. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10467. "3146 Fast-path completion queues "
  10468. "does not exist\n");
  10469. return;
  10470. }
  10471. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10472. if (unlikely(!cq)) {
  10473. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10474. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10475. "0367 Fast-path completion queue "
  10476. "(%d) does not exist\n", fcp_cqidx);
  10477. return;
  10478. }
  10479. /* Get the reference to the corresponding CQ */
  10480. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10481. if (unlikely(cqid != cq->queue_id)) {
  10482. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10483. "0368 Miss-matched fast-path completion "
  10484. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10485. cqid, cq->queue_id);
  10486. return;
  10487. }
  10488. /* Process all the entries to the CQ */
  10489. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10490. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10491. if (!(++ecount % cq->entry_repost))
  10492. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10493. }
  10494. /* Catch the no cq entry condition */
  10495. if (unlikely(ecount == 0))
  10496. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10497. "0369 No entry from fast-path completion "
  10498. "queue fcpcqid=%d\n", cq->queue_id);
  10499. /* In any case, flash and re-arm the CQ */
  10500. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10501. /* wake up worker thread if there are works to be done */
  10502. if (workposted)
  10503. lpfc_worker_wake_up(phba);
  10504. }
  10505. static void
  10506. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10507. {
  10508. struct lpfc_eqe *eqe;
  10509. /* walk all the EQ entries and drop on the floor */
  10510. while ((eqe = lpfc_sli4_eq_get(eq)))
  10511. ;
  10512. /* Clear and re-arm the EQ */
  10513. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10514. }
  10515. /**
  10516. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10517. * @irq: Interrupt number.
  10518. * @dev_id: The device context pointer.
  10519. *
  10520. * This function is directly called from the PCI layer as an interrupt
  10521. * service routine when device with SLI-4 interface spec is enabled with
  10522. * MSI-X multi-message interrupt mode and there are slow-path events in
  10523. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10524. * interrupt mode, this function is called as part of the device-level
  10525. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10526. * undergoing initialization, the interrupt handler will not process the
  10527. * interrupt. The link attention and ELS ring attention events are handled
  10528. * by the worker thread. The interrupt handler signals the worker thread
  10529. * and returns for these events. This function is called without any lock
  10530. * held. It gets the hbalock to access and update SLI data structures.
  10531. *
  10532. * This function returns IRQ_HANDLED when interrupt is handled else it
  10533. * returns IRQ_NONE.
  10534. **/
  10535. irqreturn_t
  10536. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10537. {
  10538. struct lpfc_hba *phba;
  10539. struct lpfc_queue *speq;
  10540. struct lpfc_eqe *eqe;
  10541. unsigned long iflag;
  10542. int ecount = 0;
  10543. /*
  10544. * Get the driver's phba structure from the dev_id
  10545. */
  10546. phba = (struct lpfc_hba *)dev_id;
  10547. if (unlikely(!phba))
  10548. return IRQ_NONE;
  10549. /* Get to the EQ struct associated with this vector */
  10550. speq = phba->sli4_hba.sp_eq;
  10551. if (unlikely(!speq))
  10552. return IRQ_NONE;
  10553. /* Check device state for handling interrupt */
  10554. if (unlikely(lpfc_intr_state_check(phba))) {
  10555. /* Check again for link_state with lock held */
  10556. spin_lock_irqsave(&phba->hbalock, iflag);
  10557. if (phba->link_state < LPFC_LINK_DOWN)
  10558. /* Flush, clear interrupt, and rearm the EQ */
  10559. lpfc_sli4_eq_flush(phba, speq);
  10560. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10561. return IRQ_NONE;
  10562. }
  10563. /*
  10564. * Process all the event on FCP slow-path EQ
  10565. */
  10566. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10567. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10568. if (!(++ecount % speq->entry_repost))
  10569. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10570. }
  10571. /* Always clear and re-arm the slow-path EQ */
  10572. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10573. /* Catch the no cq entry condition */
  10574. if (unlikely(ecount == 0)) {
  10575. if (phba->intr_type == MSIX)
  10576. /* MSI-X treated interrupt served as no EQ share INT */
  10577. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10578. "0357 MSI-X interrupt with no EQE\n");
  10579. else
  10580. /* Non MSI-X treated on interrupt as EQ share INT */
  10581. return IRQ_NONE;
  10582. }
  10583. return IRQ_HANDLED;
  10584. } /* lpfc_sli4_sp_intr_handler */
  10585. /**
  10586. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10587. * @irq: Interrupt number.
  10588. * @dev_id: The device context pointer.
  10589. *
  10590. * This function is directly called from the PCI layer as an interrupt
  10591. * service routine when device with SLI-4 interface spec is enabled with
  10592. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10593. * ring event in the HBA. However, when the device is enabled with either
  10594. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10595. * device-level interrupt handler. When the PCI slot is in error recovery
  10596. * or the HBA is undergoing initialization, the interrupt handler will not
  10597. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10598. * the intrrupt context. This function is called without any lock held.
  10599. * It gets the hbalock to access and update SLI data structures. Note that,
  10600. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10601. * equal to that of FCP CQ index.
  10602. *
  10603. * This function returns IRQ_HANDLED when interrupt is handled else it
  10604. * returns IRQ_NONE.
  10605. **/
  10606. irqreturn_t
  10607. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10608. {
  10609. struct lpfc_hba *phba;
  10610. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10611. struct lpfc_queue *fpeq;
  10612. struct lpfc_eqe *eqe;
  10613. unsigned long iflag;
  10614. int ecount = 0;
  10615. uint32_t fcp_eqidx;
  10616. /* Get the driver's phba structure from the dev_id */
  10617. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10618. phba = fcp_eq_hdl->phba;
  10619. fcp_eqidx = fcp_eq_hdl->idx;
  10620. if (unlikely(!phba))
  10621. return IRQ_NONE;
  10622. if (unlikely(!phba->sli4_hba.fp_eq))
  10623. return IRQ_NONE;
  10624. /* Get to the EQ struct associated with this vector */
  10625. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10626. if (unlikely(!fpeq))
  10627. return IRQ_NONE;
  10628. /* Check device state for handling interrupt */
  10629. if (unlikely(lpfc_intr_state_check(phba))) {
  10630. /* Check again for link_state with lock held */
  10631. spin_lock_irqsave(&phba->hbalock, iflag);
  10632. if (phba->link_state < LPFC_LINK_DOWN)
  10633. /* Flush, clear interrupt, and rearm the EQ */
  10634. lpfc_sli4_eq_flush(phba, fpeq);
  10635. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10636. return IRQ_NONE;
  10637. }
  10638. /*
  10639. * Process all the event on FCP fast-path EQ
  10640. */
  10641. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10642. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10643. if (!(++ecount % fpeq->entry_repost))
  10644. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10645. }
  10646. /* Always clear and re-arm the fast-path EQ */
  10647. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10648. if (unlikely(ecount == 0)) {
  10649. if (phba->intr_type == MSIX)
  10650. /* MSI-X treated interrupt served as no EQ share INT */
  10651. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10652. "0358 MSI-X interrupt with no EQE\n");
  10653. else
  10654. /* Non MSI-X treated on interrupt as EQ share INT */
  10655. return IRQ_NONE;
  10656. }
  10657. return IRQ_HANDLED;
  10658. } /* lpfc_sli4_fp_intr_handler */
  10659. /**
  10660. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10661. * @irq: Interrupt number.
  10662. * @dev_id: The device context pointer.
  10663. *
  10664. * This function is the device-level interrupt handler to device with SLI-4
  10665. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10666. * interrupt mode is enabled and there is an event in the HBA which requires
  10667. * driver attention. This function invokes the slow-path interrupt attention
  10668. * handling function and fast-path interrupt attention handling function in
  10669. * turn to process the relevant HBA attention events. This function is called
  10670. * without any lock held. It gets the hbalock to access and update SLI data
  10671. * structures.
  10672. *
  10673. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10674. * returns IRQ_NONE.
  10675. **/
  10676. irqreturn_t
  10677. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10678. {
  10679. struct lpfc_hba *phba;
  10680. irqreturn_t sp_irq_rc, fp_irq_rc;
  10681. bool fp_handled = false;
  10682. uint32_t fcp_eqidx;
  10683. /* Get the driver's phba structure from the dev_id */
  10684. phba = (struct lpfc_hba *)dev_id;
  10685. if (unlikely(!phba))
  10686. return IRQ_NONE;
  10687. /*
  10688. * Invokes slow-path host attention interrupt handling as appropriate.
  10689. */
  10690. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10691. /*
  10692. * Invoke fast-path host attention interrupt handling as appropriate.
  10693. */
  10694. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10695. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10696. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10697. if (fp_irq_rc == IRQ_HANDLED)
  10698. fp_handled |= true;
  10699. }
  10700. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10701. } /* lpfc_sli4_intr_handler */
  10702. /**
  10703. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10704. * @queue: The queue structure to free.
  10705. *
  10706. * This function frees a queue structure and the DMAable memory used for
  10707. * the host resident queue. This function must be called after destroying the
  10708. * queue on the HBA.
  10709. **/
  10710. void
  10711. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10712. {
  10713. struct lpfc_dmabuf *dmabuf;
  10714. if (!queue)
  10715. return;
  10716. while (!list_empty(&queue->page_list)) {
  10717. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10718. list);
  10719. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10720. dmabuf->virt, dmabuf->phys);
  10721. kfree(dmabuf);
  10722. }
  10723. kfree(queue);
  10724. return;
  10725. }
  10726. /**
  10727. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10728. * @phba: The HBA that this queue is being created on.
  10729. * @entry_size: The size of each queue entry for this queue.
  10730. * @entry count: The number of entries that this queue will handle.
  10731. *
  10732. * This function allocates a queue structure and the DMAable memory used for
  10733. * the host resident queue. This function must be called before creating the
  10734. * queue on the HBA.
  10735. **/
  10736. struct lpfc_queue *
  10737. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10738. uint32_t entry_count)
  10739. {
  10740. struct lpfc_queue *queue;
  10741. struct lpfc_dmabuf *dmabuf;
  10742. int x, total_qe_count;
  10743. void *dma_pointer;
  10744. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10745. if (!phba->sli4_hba.pc_sli4_params.supported)
  10746. hw_page_size = SLI4_PAGE_SIZE;
  10747. queue = kzalloc(sizeof(struct lpfc_queue) +
  10748. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10749. if (!queue)
  10750. return NULL;
  10751. queue->page_count = (ALIGN(entry_size * entry_count,
  10752. hw_page_size))/hw_page_size;
  10753. INIT_LIST_HEAD(&queue->list);
  10754. INIT_LIST_HEAD(&queue->page_list);
  10755. INIT_LIST_HEAD(&queue->child_list);
  10756. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10757. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10758. if (!dmabuf)
  10759. goto out_fail;
  10760. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10761. hw_page_size, &dmabuf->phys,
  10762. GFP_KERNEL);
  10763. if (!dmabuf->virt) {
  10764. kfree(dmabuf);
  10765. goto out_fail;
  10766. }
  10767. memset(dmabuf->virt, 0, hw_page_size);
  10768. dmabuf->buffer_tag = x;
  10769. list_add_tail(&dmabuf->list, &queue->page_list);
  10770. /* initialize queue's entry array */
  10771. dma_pointer = dmabuf->virt;
  10772. for (; total_qe_count < entry_count &&
  10773. dma_pointer < (hw_page_size + dmabuf->virt);
  10774. total_qe_count++, dma_pointer += entry_size) {
  10775. queue->qe[total_qe_count].address = dma_pointer;
  10776. }
  10777. }
  10778. queue->entry_size = entry_size;
  10779. queue->entry_count = entry_count;
  10780. /*
  10781. * entry_repost is calculated based on the number of entries in the
  10782. * queue. This works out except for RQs. If buffers are NOT initially
  10783. * posted for every RQE, entry_repost should be adjusted accordingly.
  10784. */
  10785. queue->entry_repost = (entry_count >> 3);
  10786. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10787. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10788. queue->phba = phba;
  10789. return queue;
  10790. out_fail:
  10791. lpfc_sli4_queue_free(queue);
  10792. return NULL;
  10793. }
  10794. /**
  10795. * lpfc_eq_create - Create an Event Queue on the HBA
  10796. * @phba: HBA structure that indicates port to create a queue on.
  10797. * @eq: The queue structure to use to create the event queue.
  10798. * @imax: The maximum interrupt per second limit.
  10799. *
  10800. * This function creates an event queue, as detailed in @eq, on a port,
  10801. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10802. *
  10803. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10804. * is used to get the entry count and entry size that are necessary to
  10805. * determine the number of pages to allocate and use for this queue. This
  10806. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10807. * event queue. This function is asynchronous and will wait for the mailbox
  10808. * command to finish before continuing.
  10809. *
  10810. * On success this function will return a zero. If unable to allocate enough
  10811. * memory this function will return -ENOMEM. If the queue create mailbox command
  10812. * fails this function will return -ENXIO.
  10813. **/
  10814. uint32_t
  10815. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10816. {
  10817. struct lpfc_mbx_eq_create *eq_create;
  10818. LPFC_MBOXQ_t *mbox;
  10819. int rc, length, status = 0;
  10820. struct lpfc_dmabuf *dmabuf;
  10821. uint32_t shdr_status, shdr_add_status;
  10822. union lpfc_sli4_cfg_shdr *shdr;
  10823. uint16_t dmult;
  10824. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10825. /* sanity check on queue memory */
  10826. if (!eq)
  10827. return -ENODEV;
  10828. if (!phba->sli4_hba.pc_sli4_params.supported)
  10829. hw_page_size = SLI4_PAGE_SIZE;
  10830. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10831. if (!mbox)
  10832. return -ENOMEM;
  10833. length = (sizeof(struct lpfc_mbx_eq_create) -
  10834. sizeof(struct lpfc_sli4_cfg_mhdr));
  10835. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10836. LPFC_MBOX_OPCODE_EQ_CREATE,
  10837. length, LPFC_SLI4_MBX_EMBED);
  10838. eq_create = &mbox->u.mqe.un.eq_create;
  10839. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10840. eq->page_count);
  10841. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10842. LPFC_EQE_SIZE);
  10843. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10844. /* Calculate delay multiper from maximum interrupt per second */
  10845. dmult = LPFC_DMULT_CONST/imax - 1;
  10846. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10847. dmult);
  10848. switch (eq->entry_count) {
  10849. default:
  10850. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10851. "0360 Unsupported EQ count. (%d)\n",
  10852. eq->entry_count);
  10853. if (eq->entry_count < 256)
  10854. return -EINVAL;
  10855. /* otherwise default to smallest count (drop through) */
  10856. case 256:
  10857. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10858. LPFC_EQ_CNT_256);
  10859. break;
  10860. case 512:
  10861. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10862. LPFC_EQ_CNT_512);
  10863. break;
  10864. case 1024:
  10865. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10866. LPFC_EQ_CNT_1024);
  10867. break;
  10868. case 2048:
  10869. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10870. LPFC_EQ_CNT_2048);
  10871. break;
  10872. case 4096:
  10873. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10874. LPFC_EQ_CNT_4096);
  10875. break;
  10876. }
  10877. list_for_each_entry(dmabuf, &eq->page_list, list) {
  10878. memset(dmabuf->virt, 0, hw_page_size);
  10879. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10880. putPaddrLow(dmabuf->phys);
  10881. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10882. putPaddrHigh(dmabuf->phys);
  10883. }
  10884. mbox->vport = phba->pport;
  10885. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  10886. mbox->context1 = NULL;
  10887. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10888. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  10889. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10890. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10891. if (shdr_status || shdr_add_status || rc) {
  10892. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10893. "2500 EQ_CREATE mailbox failed with "
  10894. "status x%x add_status x%x, mbx status x%x\n",
  10895. shdr_status, shdr_add_status, rc);
  10896. status = -ENXIO;
  10897. }
  10898. eq->type = LPFC_EQ;
  10899. eq->subtype = LPFC_NONE;
  10900. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  10901. if (eq->queue_id == 0xFFFF)
  10902. status = -ENXIO;
  10903. eq->host_index = 0;
  10904. eq->hba_index = 0;
  10905. mempool_free(mbox, phba->mbox_mem_pool);
  10906. return status;
  10907. }
  10908. /**
  10909. * lpfc_cq_create - Create a Completion Queue on the HBA
  10910. * @phba: HBA structure that indicates port to create a queue on.
  10911. * @cq: The queue structure to use to create the completion queue.
  10912. * @eq: The event queue to bind this completion queue to.
  10913. *
  10914. * This function creates a completion queue, as detailed in @wq, on a port,
  10915. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  10916. *
  10917. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10918. * is used to get the entry count and entry size that are necessary to
  10919. * determine the number of pages to allocate and use for this queue. The @eq
  10920. * is used to indicate which event queue to bind this completion queue to. This
  10921. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  10922. * completion queue. This function is asynchronous and will wait for the mailbox
  10923. * command to finish before continuing.
  10924. *
  10925. * On success this function will return a zero. If unable to allocate enough
  10926. * memory this function will return -ENOMEM. If the queue create mailbox command
  10927. * fails this function will return -ENXIO.
  10928. **/
  10929. uint32_t
  10930. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10931. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  10932. {
  10933. struct lpfc_mbx_cq_create *cq_create;
  10934. struct lpfc_dmabuf *dmabuf;
  10935. LPFC_MBOXQ_t *mbox;
  10936. int rc, length, status = 0;
  10937. uint32_t shdr_status, shdr_add_status;
  10938. union lpfc_sli4_cfg_shdr *shdr;
  10939. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10940. /* sanity check on queue memory */
  10941. if (!cq || !eq)
  10942. return -ENODEV;
  10943. if (!phba->sli4_hba.pc_sli4_params.supported)
  10944. hw_page_size = SLI4_PAGE_SIZE;
  10945. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10946. if (!mbox)
  10947. return -ENOMEM;
  10948. length = (sizeof(struct lpfc_mbx_cq_create) -
  10949. sizeof(struct lpfc_sli4_cfg_mhdr));
  10950. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10951. LPFC_MBOX_OPCODE_CQ_CREATE,
  10952. length, LPFC_SLI4_MBX_EMBED);
  10953. cq_create = &mbox->u.mqe.un.cq_create;
  10954. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  10955. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  10956. cq->page_count);
  10957. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  10958. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  10959. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10960. phba->sli4_hba.pc_sli4_params.cqv);
  10961. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  10962. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  10963. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  10964. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  10965. eq->queue_id);
  10966. } else {
  10967. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  10968. eq->queue_id);
  10969. }
  10970. switch (cq->entry_count) {
  10971. default:
  10972. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10973. "0361 Unsupported CQ count. (%d)\n",
  10974. cq->entry_count);
  10975. if (cq->entry_count < 256)
  10976. return -EINVAL;
  10977. /* otherwise default to smallest count (drop through) */
  10978. case 256:
  10979. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10980. LPFC_CQ_CNT_256);
  10981. break;
  10982. case 512:
  10983. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10984. LPFC_CQ_CNT_512);
  10985. break;
  10986. case 1024:
  10987. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10988. LPFC_CQ_CNT_1024);
  10989. break;
  10990. }
  10991. list_for_each_entry(dmabuf, &cq->page_list, list) {
  10992. memset(dmabuf->virt, 0, hw_page_size);
  10993. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10994. putPaddrLow(dmabuf->phys);
  10995. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10996. putPaddrHigh(dmabuf->phys);
  10997. }
  10998. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10999. /* The IOCTL status is embedded in the mailbox subheader. */
  11000. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11001. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11002. if (shdr_status || shdr_add_status || rc) {
  11003. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11004. "2501 CQ_CREATE mailbox failed with "
  11005. "status x%x add_status x%x, mbx status x%x\n",
  11006. shdr_status, shdr_add_status, rc);
  11007. status = -ENXIO;
  11008. goto out;
  11009. }
  11010. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11011. if (cq->queue_id == 0xFFFF) {
  11012. status = -ENXIO;
  11013. goto out;
  11014. }
  11015. /* link the cq onto the parent eq child list */
  11016. list_add_tail(&cq->list, &eq->child_list);
  11017. /* Set up completion queue's type and subtype */
  11018. cq->type = type;
  11019. cq->subtype = subtype;
  11020. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11021. cq->assoc_qid = eq->queue_id;
  11022. cq->host_index = 0;
  11023. cq->hba_index = 0;
  11024. out:
  11025. mempool_free(mbox, phba->mbox_mem_pool);
  11026. return status;
  11027. }
  11028. /**
  11029. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11030. * @phba: HBA structure that indicates port to create a queue on.
  11031. * @mq: The queue structure to use to create the mailbox queue.
  11032. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11033. * @cq: The completion queue to associate with this cq.
  11034. *
  11035. * This function provides failback (fb) functionality when the
  11036. * mq_create_ext fails on older FW generations. It's purpose is identical
  11037. * to mq_create_ext otherwise.
  11038. *
  11039. * This routine cannot fail as all attributes were previously accessed and
  11040. * initialized in mq_create_ext.
  11041. **/
  11042. static void
  11043. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11044. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11045. {
  11046. struct lpfc_mbx_mq_create *mq_create;
  11047. struct lpfc_dmabuf *dmabuf;
  11048. int length;
  11049. length = (sizeof(struct lpfc_mbx_mq_create) -
  11050. sizeof(struct lpfc_sli4_cfg_mhdr));
  11051. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11052. LPFC_MBOX_OPCODE_MQ_CREATE,
  11053. length, LPFC_SLI4_MBX_EMBED);
  11054. mq_create = &mbox->u.mqe.un.mq_create;
  11055. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11056. mq->page_count);
  11057. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11058. cq->queue_id);
  11059. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11060. switch (mq->entry_count) {
  11061. case 16:
  11062. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11063. LPFC_MQ_RING_SIZE_16);
  11064. break;
  11065. case 32:
  11066. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11067. LPFC_MQ_RING_SIZE_32);
  11068. break;
  11069. case 64:
  11070. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11071. LPFC_MQ_RING_SIZE_64);
  11072. break;
  11073. case 128:
  11074. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11075. LPFC_MQ_RING_SIZE_128);
  11076. break;
  11077. }
  11078. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11079. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11080. putPaddrLow(dmabuf->phys);
  11081. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11082. putPaddrHigh(dmabuf->phys);
  11083. }
  11084. }
  11085. /**
  11086. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11087. * @phba: HBA structure that indicates port to create a queue on.
  11088. * @mq: The queue structure to use to create the mailbox queue.
  11089. * @cq: The completion queue to associate with this cq.
  11090. * @subtype: The queue's subtype.
  11091. *
  11092. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11093. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11094. *
  11095. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11096. * is used to get the entry count and entry size that are necessary to
  11097. * determine the number of pages to allocate and use for this queue. This
  11098. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11099. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11100. * command to finish before continuing.
  11101. *
  11102. * On success this function will return a zero. If unable to allocate enough
  11103. * memory this function will return -ENOMEM. If the queue create mailbox command
  11104. * fails this function will return -ENXIO.
  11105. **/
  11106. int32_t
  11107. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11108. struct lpfc_queue *cq, uint32_t subtype)
  11109. {
  11110. struct lpfc_mbx_mq_create *mq_create;
  11111. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11112. struct lpfc_dmabuf *dmabuf;
  11113. LPFC_MBOXQ_t *mbox;
  11114. int rc, length, status = 0;
  11115. uint32_t shdr_status, shdr_add_status;
  11116. union lpfc_sli4_cfg_shdr *shdr;
  11117. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11118. /* sanity check on queue memory */
  11119. if (!mq || !cq)
  11120. return -ENODEV;
  11121. if (!phba->sli4_hba.pc_sli4_params.supported)
  11122. hw_page_size = SLI4_PAGE_SIZE;
  11123. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11124. if (!mbox)
  11125. return -ENOMEM;
  11126. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11127. sizeof(struct lpfc_sli4_cfg_mhdr));
  11128. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11129. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11130. length, LPFC_SLI4_MBX_EMBED);
  11131. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11132. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11133. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11134. &mq_create_ext->u.request, mq->page_count);
  11135. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11136. &mq_create_ext->u.request, 1);
  11137. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11138. &mq_create_ext->u.request, 1);
  11139. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11140. &mq_create_ext->u.request, 1);
  11141. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11142. &mq_create_ext->u.request, 1);
  11143. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11144. &mq_create_ext->u.request, 1);
  11145. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11146. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11147. phba->sli4_hba.pc_sli4_params.mqv);
  11148. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11149. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11150. cq->queue_id);
  11151. else
  11152. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11153. cq->queue_id);
  11154. switch (mq->entry_count) {
  11155. default:
  11156. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11157. "0362 Unsupported MQ count. (%d)\n",
  11158. mq->entry_count);
  11159. if (mq->entry_count < 16)
  11160. return -EINVAL;
  11161. /* otherwise default to smallest count (drop through) */
  11162. case 16:
  11163. bf_set(lpfc_mq_context_ring_size,
  11164. &mq_create_ext->u.request.context,
  11165. LPFC_MQ_RING_SIZE_16);
  11166. break;
  11167. case 32:
  11168. bf_set(lpfc_mq_context_ring_size,
  11169. &mq_create_ext->u.request.context,
  11170. LPFC_MQ_RING_SIZE_32);
  11171. break;
  11172. case 64:
  11173. bf_set(lpfc_mq_context_ring_size,
  11174. &mq_create_ext->u.request.context,
  11175. LPFC_MQ_RING_SIZE_64);
  11176. break;
  11177. case 128:
  11178. bf_set(lpfc_mq_context_ring_size,
  11179. &mq_create_ext->u.request.context,
  11180. LPFC_MQ_RING_SIZE_128);
  11181. break;
  11182. }
  11183. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11184. memset(dmabuf->virt, 0, hw_page_size);
  11185. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11186. putPaddrLow(dmabuf->phys);
  11187. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11188. putPaddrHigh(dmabuf->phys);
  11189. }
  11190. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11191. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11192. &mq_create_ext->u.response);
  11193. if (rc != MBX_SUCCESS) {
  11194. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11195. "2795 MQ_CREATE_EXT failed with "
  11196. "status x%x. Failback to MQ_CREATE.\n",
  11197. rc);
  11198. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11199. mq_create = &mbox->u.mqe.un.mq_create;
  11200. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11201. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11202. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11203. &mq_create->u.response);
  11204. }
  11205. /* The IOCTL status is embedded in the mailbox subheader. */
  11206. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11207. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11208. if (shdr_status || shdr_add_status || rc) {
  11209. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11210. "2502 MQ_CREATE mailbox failed with "
  11211. "status x%x add_status x%x, mbx status x%x\n",
  11212. shdr_status, shdr_add_status, rc);
  11213. status = -ENXIO;
  11214. goto out;
  11215. }
  11216. if (mq->queue_id == 0xFFFF) {
  11217. status = -ENXIO;
  11218. goto out;
  11219. }
  11220. mq->type = LPFC_MQ;
  11221. mq->assoc_qid = cq->queue_id;
  11222. mq->subtype = subtype;
  11223. mq->host_index = 0;
  11224. mq->hba_index = 0;
  11225. /* link the mq onto the parent cq child list */
  11226. list_add_tail(&mq->list, &cq->child_list);
  11227. out:
  11228. mempool_free(mbox, phba->mbox_mem_pool);
  11229. return status;
  11230. }
  11231. /**
  11232. * lpfc_wq_create - Create a Work Queue on the HBA
  11233. * @phba: HBA structure that indicates port to create a queue on.
  11234. * @wq: The queue structure to use to create the work queue.
  11235. * @cq: The completion queue to bind this work queue to.
  11236. * @subtype: The subtype of the work queue indicating its functionality.
  11237. *
  11238. * This function creates a work queue, as detailed in @wq, on a port, described
  11239. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11240. *
  11241. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11242. * is used to get the entry count and entry size that are necessary to
  11243. * determine the number of pages to allocate and use for this queue. The @cq
  11244. * is used to indicate which completion queue to bind this work queue to. This
  11245. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11246. * work queue. This function is asynchronous and will wait for the mailbox
  11247. * command to finish before continuing.
  11248. *
  11249. * On success this function will return a zero. If unable to allocate enough
  11250. * memory this function will return -ENOMEM. If the queue create mailbox command
  11251. * fails this function will return -ENXIO.
  11252. **/
  11253. uint32_t
  11254. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11255. struct lpfc_queue *cq, uint32_t subtype)
  11256. {
  11257. struct lpfc_mbx_wq_create *wq_create;
  11258. struct lpfc_dmabuf *dmabuf;
  11259. LPFC_MBOXQ_t *mbox;
  11260. int rc, length, status = 0;
  11261. uint32_t shdr_status, shdr_add_status;
  11262. union lpfc_sli4_cfg_shdr *shdr;
  11263. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11264. struct dma_address *page;
  11265. /* sanity check on queue memory */
  11266. if (!wq || !cq)
  11267. return -ENODEV;
  11268. if (!phba->sli4_hba.pc_sli4_params.supported)
  11269. hw_page_size = SLI4_PAGE_SIZE;
  11270. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11271. if (!mbox)
  11272. return -ENOMEM;
  11273. length = (sizeof(struct lpfc_mbx_wq_create) -
  11274. sizeof(struct lpfc_sli4_cfg_mhdr));
  11275. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11276. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11277. length, LPFC_SLI4_MBX_EMBED);
  11278. wq_create = &mbox->u.mqe.un.wq_create;
  11279. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11280. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11281. wq->page_count);
  11282. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11283. cq->queue_id);
  11284. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11285. phba->sli4_hba.pc_sli4_params.wqv);
  11286. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11287. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11288. wq->entry_count);
  11289. switch (wq->entry_size) {
  11290. default:
  11291. case 64:
  11292. bf_set(lpfc_mbx_wq_create_wqe_size,
  11293. &wq_create->u.request_1,
  11294. LPFC_WQ_WQE_SIZE_64);
  11295. break;
  11296. case 128:
  11297. bf_set(lpfc_mbx_wq_create_wqe_size,
  11298. &wq_create->u.request_1,
  11299. LPFC_WQ_WQE_SIZE_128);
  11300. break;
  11301. }
  11302. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11303. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11304. page = wq_create->u.request_1.page;
  11305. } else {
  11306. page = wq_create->u.request.page;
  11307. }
  11308. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11309. memset(dmabuf->virt, 0, hw_page_size);
  11310. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11311. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11312. }
  11313. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11314. /* The IOCTL status is embedded in the mailbox subheader. */
  11315. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11316. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11317. if (shdr_status || shdr_add_status || rc) {
  11318. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11319. "2503 WQ_CREATE mailbox failed with "
  11320. "status x%x add_status x%x, mbx status x%x\n",
  11321. shdr_status, shdr_add_status, rc);
  11322. status = -ENXIO;
  11323. goto out;
  11324. }
  11325. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11326. if (wq->queue_id == 0xFFFF) {
  11327. status = -ENXIO;
  11328. goto out;
  11329. }
  11330. wq->type = LPFC_WQ;
  11331. wq->assoc_qid = cq->queue_id;
  11332. wq->subtype = subtype;
  11333. wq->host_index = 0;
  11334. wq->hba_index = 0;
  11335. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11336. /* link the wq onto the parent cq child list */
  11337. list_add_tail(&wq->list, &cq->child_list);
  11338. out:
  11339. mempool_free(mbox, phba->mbox_mem_pool);
  11340. return status;
  11341. }
  11342. /**
  11343. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11344. * @phba: HBA structure that indicates port to create a queue on.
  11345. * @rq: The queue structure to use for the receive queue.
  11346. * @qno: The associated HBQ number
  11347. *
  11348. *
  11349. * For SLI4 we need to adjust the RQ repost value based on
  11350. * the number of buffers that are initially posted to the RQ.
  11351. */
  11352. void
  11353. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11354. {
  11355. uint32_t cnt;
  11356. /* sanity check on queue memory */
  11357. if (!rq)
  11358. return;
  11359. cnt = lpfc_hbq_defs[qno]->entry_count;
  11360. /* Recalc repost for RQs based on buffers initially posted */
  11361. cnt = (cnt >> 3);
  11362. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11363. cnt = LPFC_QUEUE_MIN_REPOST;
  11364. rq->entry_repost = cnt;
  11365. }
  11366. /**
  11367. * lpfc_rq_create - Create a Receive Queue on the HBA
  11368. * @phba: HBA structure that indicates port to create a queue on.
  11369. * @hrq: The queue structure to use to create the header receive queue.
  11370. * @drq: The queue structure to use to create the data receive queue.
  11371. * @cq: The completion queue to bind this work queue to.
  11372. *
  11373. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11374. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11375. * to the HBA.
  11376. *
  11377. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11378. * struct is used to get the entry count that is necessary to determine the
  11379. * number of pages to use for this queue. The @cq is used to indicate which
  11380. * completion queue to bind received buffers that are posted to these queues to.
  11381. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11382. * receive queue pair. This function is asynchronous and will wait for the
  11383. * mailbox command to finish before continuing.
  11384. *
  11385. * On success this function will return a zero. If unable to allocate enough
  11386. * memory this function will return -ENOMEM. If the queue create mailbox command
  11387. * fails this function will return -ENXIO.
  11388. **/
  11389. uint32_t
  11390. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11391. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11392. {
  11393. struct lpfc_mbx_rq_create *rq_create;
  11394. struct lpfc_dmabuf *dmabuf;
  11395. LPFC_MBOXQ_t *mbox;
  11396. int rc, length, status = 0;
  11397. uint32_t shdr_status, shdr_add_status;
  11398. union lpfc_sli4_cfg_shdr *shdr;
  11399. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11400. /* sanity check on queue memory */
  11401. if (!hrq || !drq || !cq)
  11402. return -ENODEV;
  11403. if (!phba->sli4_hba.pc_sli4_params.supported)
  11404. hw_page_size = SLI4_PAGE_SIZE;
  11405. if (hrq->entry_count != drq->entry_count)
  11406. return -EINVAL;
  11407. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11408. if (!mbox)
  11409. return -ENOMEM;
  11410. length = (sizeof(struct lpfc_mbx_rq_create) -
  11411. sizeof(struct lpfc_sli4_cfg_mhdr));
  11412. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11413. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11414. length, LPFC_SLI4_MBX_EMBED);
  11415. rq_create = &mbox->u.mqe.un.rq_create;
  11416. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11417. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11418. phba->sli4_hba.pc_sli4_params.rqv);
  11419. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11420. bf_set(lpfc_rq_context_rqe_count_1,
  11421. &rq_create->u.request.context,
  11422. hrq->entry_count);
  11423. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11424. bf_set(lpfc_rq_context_rqe_size,
  11425. &rq_create->u.request.context,
  11426. LPFC_RQE_SIZE_8);
  11427. bf_set(lpfc_rq_context_page_size,
  11428. &rq_create->u.request.context,
  11429. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11430. } else {
  11431. switch (hrq->entry_count) {
  11432. default:
  11433. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11434. "2535 Unsupported RQ count. (%d)\n",
  11435. hrq->entry_count);
  11436. if (hrq->entry_count < 512)
  11437. return -EINVAL;
  11438. /* otherwise default to smallest count (drop through) */
  11439. case 512:
  11440. bf_set(lpfc_rq_context_rqe_count,
  11441. &rq_create->u.request.context,
  11442. LPFC_RQ_RING_SIZE_512);
  11443. break;
  11444. case 1024:
  11445. bf_set(lpfc_rq_context_rqe_count,
  11446. &rq_create->u.request.context,
  11447. LPFC_RQ_RING_SIZE_1024);
  11448. break;
  11449. case 2048:
  11450. bf_set(lpfc_rq_context_rqe_count,
  11451. &rq_create->u.request.context,
  11452. LPFC_RQ_RING_SIZE_2048);
  11453. break;
  11454. case 4096:
  11455. bf_set(lpfc_rq_context_rqe_count,
  11456. &rq_create->u.request.context,
  11457. LPFC_RQ_RING_SIZE_4096);
  11458. break;
  11459. }
  11460. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11461. LPFC_HDR_BUF_SIZE);
  11462. }
  11463. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11464. cq->queue_id);
  11465. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11466. hrq->page_count);
  11467. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11468. memset(dmabuf->virt, 0, hw_page_size);
  11469. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11470. putPaddrLow(dmabuf->phys);
  11471. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11472. putPaddrHigh(dmabuf->phys);
  11473. }
  11474. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11475. /* The IOCTL status is embedded in the mailbox subheader. */
  11476. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11477. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11478. if (shdr_status || shdr_add_status || rc) {
  11479. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11480. "2504 RQ_CREATE mailbox failed with "
  11481. "status x%x add_status x%x, mbx status x%x\n",
  11482. shdr_status, shdr_add_status, rc);
  11483. status = -ENXIO;
  11484. goto out;
  11485. }
  11486. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11487. if (hrq->queue_id == 0xFFFF) {
  11488. status = -ENXIO;
  11489. goto out;
  11490. }
  11491. hrq->type = LPFC_HRQ;
  11492. hrq->assoc_qid = cq->queue_id;
  11493. hrq->subtype = subtype;
  11494. hrq->host_index = 0;
  11495. hrq->hba_index = 0;
  11496. /* now create the data queue */
  11497. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11498. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11499. length, LPFC_SLI4_MBX_EMBED);
  11500. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11501. phba->sli4_hba.pc_sli4_params.rqv);
  11502. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11503. bf_set(lpfc_rq_context_rqe_count_1,
  11504. &rq_create->u.request.context, hrq->entry_count);
  11505. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11506. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11507. LPFC_RQE_SIZE_8);
  11508. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11509. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11510. } else {
  11511. switch (drq->entry_count) {
  11512. default:
  11513. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11514. "2536 Unsupported RQ count. (%d)\n",
  11515. drq->entry_count);
  11516. if (drq->entry_count < 512)
  11517. return -EINVAL;
  11518. /* otherwise default to smallest count (drop through) */
  11519. case 512:
  11520. bf_set(lpfc_rq_context_rqe_count,
  11521. &rq_create->u.request.context,
  11522. LPFC_RQ_RING_SIZE_512);
  11523. break;
  11524. case 1024:
  11525. bf_set(lpfc_rq_context_rqe_count,
  11526. &rq_create->u.request.context,
  11527. LPFC_RQ_RING_SIZE_1024);
  11528. break;
  11529. case 2048:
  11530. bf_set(lpfc_rq_context_rqe_count,
  11531. &rq_create->u.request.context,
  11532. LPFC_RQ_RING_SIZE_2048);
  11533. break;
  11534. case 4096:
  11535. bf_set(lpfc_rq_context_rqe_count,
  11536. &rq_create->u.request.context,
  11537. LPFC_RQ_RING_SIZE_4096);
  11538. break;
  11539. }
  11540. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11541. LPFC_DATA_BUF_SIZE);
  11542. }
  11543. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11544. cq->queue_id);
  11545. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11546. drq->page_count);
  11547. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11548. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11549. putPaddrLow(dmabuf->phys);
  11550. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11551. putPaddrHigh(dmabuf->phys);
  11552. }
  11553. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11554. /* The IOCTL status is embedded in the mailbox subheader. */
  11555. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11556. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11557. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11558. if (shdr_status || shdr_add_status || rc) {
  11559. status = -ENXIO;
  11560. goto out;
  11561. }
  11562. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11563. if (drq->queue_id == 0xFFFF) {
  11564. status = -ENXIO;
  11565. goto out;
  11566. }
  11567. drq->type = LPFC_DRQ;
  11568. drq->assoc_qid = cq->queue_id;
  11569. drq->subtype = subtype;
  11570. drq->host_index = 0;
  11571. drq->hba_index = 0;
  11572. /* link the header and data RQs onto the parent cq child list */
  11573. list_add_tail(&hrq->list, &cq->child_list);
  11574. list_add_tail(&drq->list, &cq->child_list);
  11575. out:
  11576. mempool_free(mbox, phba->mbox_mem_pool);
  11577. return status;
  11578. }
  11579. /**
  11580. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11581. * @eq: The queue structure associated with the queue to destroy.
  11582. *
  11583. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11584. * command, specific to the type of queue, to the HBA.
  11585. *
  11586. * The @eq struct is used to get the queue ID of the queue to destroy.
  11587. *
  11588. * On success this function will return a zero. If the queue destroy mailbox
  11589. * command fails this function will return -ENXIO.
  11590. **/
  11591. uint32_t
  11592. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11593. {
  11594. LPFC_MBOXQ_t *mbox;
  11595. int rc, length, status = 0;
  11596. uint32_t shdr_status, shdr_add_status;
  11597. union lpfc_sli4_cfg_shdr *shdr;
  11598. /* sanity check on queue memory */
  11599. if (!eq)
  11600. return -ENODEV;
  11601. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11602. if (!mbox)
  11603. return -ENOMEM;
  11604. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11605. sizeof(struct lpfc_sli4_cfg_mhdr));
  11606. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11607. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11608. length, LPFC_SLI4_MBX_EMBED);
  11609. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11610. eq->queue_id);
  11611. mbox->vport = eq->phba->pport;
  11612. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11613. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11614. /* The IOCTL status is embedded in the mailbox subheader. */
  11615. shdr = (union lpfc_sli4_cfg_shdr *)
  11616. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11617. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11618. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11619. if (shdr_status || shdr_add_status || rc) {
  11620. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11621. "2505 EQ_DESTROY mailbox failed with "
  11622. "status x%x add_status x%x, mbx status x%x\n",
  11623. shdr_status, shdr_add_status, rc);
  11624. status = -ENXIO;
  11625. }
  11626. /* Remove eq from any list */
  11627. list_del_init(&eq->list);
  11628. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11629. return status;
  11630. }
  11631. /**
  11632. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11633. * @cq: The queue structure associated with the queue to destroy.
  11634. *
  11635. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11636. * command, specific to the type of queue, to the HBA.
  11637. *
  11638. * The @cq struct is used to get the queue ID of the queue to destroy.
  11639. *
  11640. * On success this function will return a zero. If the queue destroy mailbox
  11641. * command fails this function will return -ENXIO.
  11642. **/
  11643. uint32_t
  11644. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11645. {
  11646. LPFC_MBOXQ_t *mbox;
  11647. int rc, length, status = 0;
  11648. uint32_t shdr_status, shdr_add_status;
  11649. union lpfc_sli4_cfg_shdr *shdr;
  11650. /* sanity check on queue memory */
  11651. if (!cq)
  11652. return -ENODEV;
  11653. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11654. if (!mbox)
  11655. return -ENOMEM;
  11656. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11657. sizeof(struct lpfc_sli4_cfg_mhdr));
  11658. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11659. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11660. length, LPFC_SLI4_MBX_EMBED);
  11661. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11662. cq->queue_id);
  11663. mbox->vport = cq->phba->pport;
  11664. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11665. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11666. /* The IOCTL status is embedded in the mailbox subheader. */
  11667. shdr = (union lpfc_sli4_cfg_shdr *)
  11668. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11669. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11670. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11671. if (shdr_status || shdr_add_status || rc) {
  11672. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11673. "2506 CQ_DESTROY mailbox failed with "
  11674. "status x%x add_status x%x, mbx status x%x\n",
  11675. shdr_status, shdr_add_status, rc);
  11676. status = -ENXIO;
  11677. }
  11678. /* Remove cq from any list */
  11679. list_del_init(&cq->list);
  11680. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11681. return status;
  11682. }
  11683. /**
  11684. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11685. * @qm: The queue structure associated with the queue to destroy.
  11686. *
  11687. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11688. * command, specific to the type of queue, to the HBA.
  11689. *
  11690. * The @mq struct is used to get the queue ID of the queue to destroy.
  11691. *
  11692. * On success this function will return a zero. If the queue destroy mailbox
  11693. * command fails this function will return -ENXIO.
  11694. **/
  11695. uint32_t
  11696. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11697. {
  11698. LPFC_MBOXQ_t *mbox;
  11699. int rc, length, status = 0;
  11700. uint32_t shdr_status, shdr_add_status;
  11701. union lpfc_sli4_cfg_shdr *shdr;
  11702. /* sanity check on queue memory */
  11703. if (!mq)
  11704. return -ENODEV;
  11705. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11706. if (!mbox)
  11707. return -ENOMEM;
  11708. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11709. sizeof(struct lpfc_sli4_cfg_mhdr));
  11710. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11711. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11712. length, LPFC_SLI4_MBX_EMBED);
  11713. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11714. mq->queue_id);
  11715. mbox->vport = mq->phba->pport;
  11716. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11717. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11718. /* The IOCTL status is embedded in the mailbox subheader. */
  11719. shdr = (union lpfc_sli4_cfg_shdr *)
  11720. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11721. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11722. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11723. if (shdr_status || shdr_add_status || rc) {
  11724. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11725. "2507 MQ_DESTROY mailbox failed with "
  11726. "status x%x add_status x%x, mbx status x%x\n",
  11727. shdr_status, shdr_add_status, rc);
  11728. status = -ENXIO;
  11729. }
  11730. /* Remove mq from any list */
  11731. list_del_init(&mq->list);
  11732. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11733. return status;
  11734. }
  11735. /**
  11736. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11737. * @wq: The queue structure associated with the queue to destroy.
  11738. *
  11739. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11740. * command, specific to the type of queue, to the HBA.
  11741. *
  11742. * The @wq struct is used to get the queue ID of the queue to destroy.
  11743. *
  11744. * On success this function will return a zero. If the queue destroy mailbox
  11745. * command fails this function will return -ENXIO.
  11746. **/
  11747. uint32_t
  11748. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11749. {
  11750. LPFC_MBOXQ_t *mbox;
  11751. int rc, length, status = 0;
  11752. uint32_t shdr_status, shdr_add_status;
  11753. union lpfc_sli4_cfg_shdr *shdr;
  11754. /* sanity check on queue memory */
  11755. if (!wq)
  11756. return -ENODEV;
  11757. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11758. if (!mbox)
  11759. return -ENOMEM;
  11760. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11761. sizeof(struct lpfc_sli4_cfg_mhdr));
  11762. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11763. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11764. length, LPFC_SLI4_MBX_EMBED);
  11765. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11766. wq->queue_id);
  11767. mbox->vport = wq->phba->pport;
  11768. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11769. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11770. shdr = (union lpfc_sli4_cfg_shdr *)
  11771. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11772. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11773. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11774. if (shdr_status || shdr_add_status || rc) {
  11775. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11776. "2508 WQ_DESTROY mailbox failed with "
  11777. "status x%x add_status x%x, mbx status x%x\n",
  11778. shdr_status, shdr_add_status, rc);
  11779. status = -ENXIO;
  11780. }
  11781. /* Remove wq from any list */
  11782. list_del_init(&wq->list);
  11783. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11784. return status;
  11785. }
  11786. /**
  11787. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11788. * @rq: The queue structure associated with the queue to destroy.
  11789. *
  11790. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11791. * command, specific to the type of queue, to the HBA.
  11792. *
  11793. * The @rq struct is used to get the queue ID of the queue to destroy.
  11794. *
  11795. * On success this function will return a zero. If the queue destroy mailbox
  11796. * command fails this function will return -ENXIO.
  11797. **/
  11798. uint32_t
  11799. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11800. struct lpfc_queue *drq)
  11801. {
  11802. LPFC_MBOXQ_t *mbox;
  11803. int rc, length, status = 0;
  11804. uint32_t shdr_status, shdr_add_status;
  11805. union lpfc_sli4_cfg_shdr *shdr;
  11806. /* sanity check on queue memory */
  11807. if (!hrq || !drq)
  11808. return -ENODEV;
  11809. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11810. if (!mbox)
  11811. return -ENOMEM;
  11812. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11813. sizeof(struct lpfc_sli4_cfg_mhdr));
  11814. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11815. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11816. length, LPFC_SLI4_MBX_EMBED);
  11817. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11818. hrq->queue_id);
  11819. mbox->vport = hrq->phba->pport;
  11820. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11821. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11822. /* The IOCTL status is embedded in the mailbox subheader. */
  11823. shdr = (union lpfc_sli4_cfg_shdr *)
  11824. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11825. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11826. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11827. if (shdr_status || shdr_add_status || rc) {
  11828. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11829. "2509 RQ_DESTROY mailbox failed with "
  11830. "status x%x add_status x%x, mbx status x%x\n",
  11831. shdr_status, shdr_add_status, rc);
  11832. if (rc != MBX_TIMEOUT)
  11833. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11834. return -ENXIO;
  11835. }
  11836. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11837. drq->queue_id);
  11838. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11839. shdr = (union lpfc_sli4_cfg_shdr *)
  11840. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11841. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11842. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11843. if (shdr_status || shdr_add_status || rc) {
  11844. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11845. "2510 RQ_DESTROY mailbox failed with "
  11846. "status x%x add_status x%x, mbx status x%x\n",
  11847. shdr_status, shdr_add_status, rc);
  11848. status = -ENXIO;
  11849. }
  11850. list_del_init(&hrq->list);
  11851. list_del_init(&drq->list);
  11852. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11853. return status;
  11854. }
  11855. /**
  11856. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  11857. * @phba: The virtual port for which this call being executed.
  11858. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  11859. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  11860. * @xritag: the xritag that ties this io to the SGL pages.
  11861. *
  11862. * This routine will post the sgl pages for the IO that has the xritag
  11863. * that is in the iocbq structure. The xritag is assigned during iocbq
  11864. * creation and persists for as long as the driver is loaded.
  11865. * if the caller has fewer than 256 scatter gather segments to map then
  11866. * pdma_phys_addr1 should be 0.
  11867. * If the caller needs to map more than 256 scatter gather segment then
  11868. * pdma_phys_addr1 should be a valid physical address.
  11869. * physical address for SGLs must be 64 byte aligned.
  11870. * If you are going to map 2 SGL's then the first one must have 256 entries
  11871. * the second sgl can have between 1 and 256 entries.
  11872. *
  11873. * Return codes:
  11874. * 0 - Success
  11875. * -ENXIO, -ENOMEM - Failure
  11876. **/
  11877. int
  11878. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  11879. dma_addr_t pdma_phys_addr0,
  11880. dma_addr_t pdma_phys_addr1,
  11881. uint16_t xritag)
  11882. {
  11883. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  11884. LPFC_MBOXQ_t *mbox;
  11885. int rc;
  11886. uint32_t shdr_status, shdr_add_status;
  11887. uint32_t mbox_tmo;
  11888. union lpfc_sli4_cfg_shdr *shdr;
  11889. if (xritag == NO_XRI) {
  11890. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11891. "0364 Invalid param:\n");
  11892. return -EINVAL;
  11893. }
  11894. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11895. if (!mbox)
  11896. return -ENOMEM;
  11897. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11898. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11899. sizeof(struct lpfc_mbx_post_sgl_pages) -
  11900. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  11901. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  11902. &mbox->u.mqe.un.post_sgl_pages;
  11903. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  11904. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  11905. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  11906. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  11907. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  11908. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  11909. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  11910. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  11911. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  11912. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  11913. if (!phba->sli4_hba.intr_enable)
  11914. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11915. else {
  11916. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  11917. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11918. }
  11919. /* The IOCTL status is embedded in the mailbox subheader. */
  11920. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  11921. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11922. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11923. if (rc != MBX_TIMEOUT)
  11924. mempool_free(mbox, phba->mbox_mem_pool);
  11925. if (shdr_status || shdr_add_status || rc) {
  11926. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11927. "2511 POST_SGL mailbox failed with "
  11928. "status x%x add_status x%x, mbx status x%x\n",
  11929. shdr_status, shdr_add_status, rc);
  11930. rc = -ENXIO;
  11931. }
  11932. return 0;
  11933. }
  11934. /**
  11935. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  11936. * @phba: pointer to lpfc hba data structure.
  11937. *
  11938. * This routine is invoked to post rpi header templates to the
  11939. * HBA consistent with the SLI-4 interface spec. This routine
  11940. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  11941. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  11942. *
  11943. * Returns
  11944. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  11945. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  11946. **/
  11947. uint16_t
  11948. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  11949. {
  11950. unsigned long xri;
  11951. /*
  11952. * Fetch the next logical xri. Because this index is logical,
  11953. * the driver starts at 0 each time.
  11954. */
  11955. spin_lock_irq(&phba->hbalock);
  11956. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  11957. phba->sli4_hba.max_cfg_param.max_xri, 0);
  11958. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  11959. spin_unlock_irq(&phba->hbalock);
  11960. return NO_XRI;
  11961. } else {
  11962. set_bit(xri, phba->sli4_hba.xri_bmask);
  11963. phba->sli4_hba.max_cfg_param.xri_used++;
  11964. phba->sli4_hba.xri_count++;
  11965. }
  11966. spin_unlock_irq(&phba->hbalock);
  11967. return xri;
  11968. }
  11969. /**
  11970. * lpfc_sli4_free_xri - Release an xri for reuse.
  11971. * @phba: pointer to lpfc hba data structure.
  11972. *
  11973. * This routine is invoked to release an xri to the pool of
  11974. * available rpis maintained by the driver.
  11975. **/
  11976. void
  11977. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11978. {
  11979. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  11980. phba->sli4_hba.xri_count--;
  11981. phba->sli4_hba.max_cfg_param.xri_used--;
  11982. }
  11983. }
  11984. /**
  11985. * lpfc_sli4_free_xri - Release an xri for reuse.
  11986. * @phba: pointer to lpfc hba data structure.
  11987. *
  11988. * This routine is invoked to release an xri to the pool of
  11989. * available rpis maintained by the driver.
  11990. **/
  11991. void
  11992. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11993. {
  11994. spin_lock_irq(&phba->hbalock);
  11995. __lpfc_sli4_free_xri(phba, xri);
  11996. spin_unlock_irq(&phba->hbalock);
  11997. }
  11998. /**
  11999. * lpfc_sli4_next_xritag - Get an xritag for the io
  12000. * @phba: Pointer to HBA context object.
  12001. *
  12002. * This function gets an xritag for the iocb. If there is no unused xritag
  12003. * it will return 0xffff.
  12004. * The function returns the allocated xritag if successful, else returns zero.
  12005. * Zero is not a valid xritag.
  12006. * The caller is not required to hold any lock.
  12007. **/
  12008. uint16_t
  12009. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12010. {
  12011. uint16_t xri_index;
  12012. xri_index = lpfc_sli4_alloc_xri(phba);
  12013. if (xri_index != NO_XRI)
  12014. return xri_index;
  12015. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12016. "2004 Failed to allocate XRI.last XRITAG is %d"
  12017. " Max XRI is %d, Used XRI is %d\n",
  12018. xri_index,
  12019. phba->sli4_hba.max_cfg_param.max_xri,
  12020. phba->sli4_hba.max_cfg_param.xri_used);
  12021. return NO_XRI;
  12022. }
  12023. /**
  12024. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12025. * @phba: pointer to lpfc hba data structure.
  12026. *
  12027. * This routine is invoked to post a block of driver's sgl pages to the
  12028. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12029. * is only called when the driver is loading and after all IO has been
  12030. * stopped.
  12031. **/
  12032. int
  12033. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
  12034. {
  12035. struct lpfc_sglq *sglq_entry;
  12036. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12037. struct sgl_page_pairs *sgl_pg_pairs;
  12038. void *viraddr;
  12039. LPFC_MBOXQ_t *mbox;
  12040. uint32_t reqlen, alloclen, pg_pairs;
  12041. uint32_t mbox_tmo;
  12042. uint16_t xritag_start = 0, lxri = 0;
  12043. int els_xri_cnt, rc = 0;
  12044. uint32_t shdr_status, shdr_add_status;
  12045. union lpfc_sli4_cfg_shdr *shdr;
  12046. /* The number of sgls to be posted */
  12047. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12048. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12049. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12050. if (reqlen > SLI4_PAGE_SIZE) {
  12051. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12052. "2559 Block sgl registration required DMA "
  12053. "size (%d) great than a page\n", reqlen);
  12054. return -ENOMEM;
  12055. }
  12056. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12057. if (!mbox)
  12058. return -ENOMEM;
  12059. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12060. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12061. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12062. LPFC_SLI4_MBX_NEMBED);
  12063. if (alloclen < reqlen) {
  12064. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12065. "0285 Allocated DMA memory size (%d) is "
  12066. "less than the requested DMA memory "
  12067. "size (%d)\n", alloclen, reqlen);
  12068. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12069. return -ENOMEM;
  12070. }
  12071. /* Set up the SGL pages in the non-embedded DMA pages */
  12072. viraddr = mbox->sge_array->addr[0];
  12073. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12074. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12075. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  12076. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  12077. /*
  12078. * Assign the sglq a physical xri only if the driver has not
  12079. * initialized those resources. A port reset only needs
  12080. * the sglq's posted.
  12081. */
  12082. if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  12083. LPFC_XRI_RSRC_RDY) {
  12084. lxri = lpfc_sli4_next_xritag(phba);
  12085. if (lxri == NO_XRI) {
  12086. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12087. return -ENOMEM;
  12088. }
  12089. sglq_entry->sli4_lxritag = lxri;
  12090. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  12091. }
  12092. /* Set up the sge entry */
  12093. sgl_pg_pairs->sgl_pg0_addr_lo =
  12094. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12095. sgl_pg_pairs->sgl_pg0_addr_hi =
  12096. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12097. sgl_pg_pairs->sgl_pg1_addr_lo =
  12098. cpu_to_le32(putPaddrLow(0));
  12099. sgl_pg_pairs->sgl_pg1_addr_hi =
  12100. cpu_to_le32(putPaddrHigh(0));
  12101. /* Keep the first xritag on the list */
  12102. if (pg_pairs == 0)
  12103. xritag_start = sglq_entry->sli4_xritag;
  12104. sgl_pg_pairs++;
  12105. }
  12106. /* Complete initialization and perform endian conversion. */
  12107. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12108. bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
  12109. sgl->word0 = cpu_to_le32(sgl->word0);
  12110. if (!phba->sli4_hba.intr_enable)
  12111. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12112. else {
  12113. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12114. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12115. }
  12116. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12117. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12118. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12119. if (rc != MBX_TIMEOUT)
  12120. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12121. if (shdr_status || shdr_add_status || rc) {
  12122. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12123. "2513 POST_SGL_BLOCK mailbox command failed "
  12124. "status x%x add_status x%x mbx status x%x\n",
  12125. shdr_status, shdr_add_status, rc);
  12126. rc = -ENXIO;
  12127. }
  12128. if (rc == 0)
  12129. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12130. LPFC_XRI_RSRC_RDY);
  12131. return rc;
  12132. }
  12133. /**
  12134. * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
  12135. * @phba: pointer to lpfc hba data structure.
  12136. *
  12137. * This routine is invoked to post a block of driver's sgl pages to the
  12138. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12139. * is only called when the driver is loading and after all IO has been
  12140. * stopped.
  12141. **/
  12142. int
  12143. lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
  12144. {
  12145. struct lpfc_sglq *sglq_entry;
  12146. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12147. struct sgl_page_pairs *sgl_pg_pairs;
  12148. void *viraddr;
  12149. LPFC_MBOXQ_t *mbox;
  12150. uint32_t reqlen, alloclen, index;
  12151. uint32_t mbox_tmo;
  12152. uint16_t rsrc_start, rsrc_size, els_xri_cnt;
  12153. uint16_t xritag_start = 0, lxri = 0;
  12154. struct lpfc_rsrc_blks *rsrc_blk;
  12155. int cnt, ttl_cnt, rc = 0;
  12156. int loop_cnt;
  12157. uint32_t shdr_status, shdr_add_status;
  12158. union lpfc_sli4_cfg_shdr *shdr;
  12159. /* The number of sgls to be posted */
  12160. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12161. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12162. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12163. if (reqlen > SLI4_PAGE_SIZE) {
  12164. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12165. "2989 Block sgl registration required DMA "
  12166. "size (%d) great than a page\n", reqlen);
  12167. return -ENOMEM;
  12168. }
  12169. cnt = 0;
  12170. ttl_cnt = 0;
  12171. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12172. list) {
  12173. rsrc_start = rsrc_blk->rsrc_start;
  12174. rsrc_size = rsrc_blk->rsrc_size;
  12175. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12176. "3014 Working ELS Extent start %d, cnt %d\n",
  12177. rsrc_start, rsrc_size);
  12178. loop_cnt = min(els_xri_cnt, rsrc_size);
  12179. if (ttl_cnt + loop_cnt >= els_xri_cnt) {
  12180. loop_cnt = els_xri_cnt - ttl_cnt;
  12181. ttl_cnt = els_xri_cnt;
  12182. }
  12183. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12184. if (!mbox)
  12185. return -ENOMEM;
  12186. /*
  12187. * Allocate DMA memory and set up the non-embedded mailbox
  12188. * command.
  12189. */
  12190. alloclen = lpfc_sli4_config(phba, mbox,
  12191. LPFC_MBOX_SUBSYSTEM_FCOE,
  12192. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12193. reqlen, LPFC_SLI4_MBX_NEMBED);
  12194. if (alloclen < reqlen) {
  12195. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12196. "2987 Allocated DMA memory size (%d) "
  12197. "is less than the requested DMA memory "
  12198. "size (%d)\n", alloclen, reqlen);
  12199. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12200. return -ENOMEM;
  12201. }
  12202. /* Set up the SGL pages in the non-embedded DMA pages */
  12203. viraddr = mbox->sge_array->addr[0];
  12204. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12205. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12206. /*
  12207. * The starting resource may not begin at zero. Control
  12208. * the loop variants via the block resource parameters,
  12209. * but handle the sge pointers with a zero-based index
  12210. * that doesn't get reset per loop pass.
  12211. */
  12212. for (index = rsrc_start;
  12213. index < rsrc_start + loop_cnt;
  12214. index++) {
  12215. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
  12216. /*
  12217. * Assign the sglq a physical xri only if the driver
  12218. * has not initialized those resources. A port reset
  12219. * only needs the sglq's posted.
  12220. */
  12221. if (bf_get(lpfc_xri_rsrc_rdy,
  12222. &phba->sli4_hba.sli4_flags) !=
  12223. LPFC_XRI_RSRC_RDY) {
  12224. lxri = lpfc_sli4_next_xritag(phba);
  12225. if (lxri == NO_XRI) {
  12226. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12227. rc = -ENOMEM;
  12228. goto err_exit;
  12229. }
  12230. sglq_entry->sli4_lxritag = lxri;
  12231. sglq_entry->sli4_xritag =
  12232. phba->sli4_hba.xri_ids[lxri];
  12233. }
  12234. /* Set up the sge entry */
  12235. sgl_pg_pairs->sgl_pg0_addr_lo =
  12236. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12237. sgl_pg_pairs->sgl_pg0_addr_hi =
  12238. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12239. sgl_pg_pairs->sgl_pg1_addr_lo =
  12240. cpu_to_le32(putPaddrLow(0));
  12241. sgl_pg_pairs->sgl_pg1_addr_hi =
  12242. cpu_to_le32(putPaddrHigh(0));
  12243. /* Track the starting physical XRI for the mailbox. */
  12244. if (index == rsrc_start)
  12245. xritag_start = sglq_entry->sli4_xritag;
  12246. sgl_pg_pairs++;
  12247. cnt++;
  12248. }
  12249. /* Complete initialization and perform endian conversion. */
  12250. rsrc_blk->rsrc_used += loop_cnt;
  12251. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12252. bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
  12253. sgl->word0 = cpu_to_le32(sgl->word0);
  12254. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12255. "3015 Post ELS Extent SGL, start %d, "
  12256. "cnt %d, used %d\n",
  12257. xritag_start, loop_cnt, rsrc_blk->rsrc_used);
  12258. if (!phba->sli4_hba.intr_enable)
  12259. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12260. else {
  12261. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12262. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12263. }
  12264. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12265. shdr_status = bf_get(lpfc_mbox_hdr_status,
  12266. &shdr->response);
  12267. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12268. &shdr->response);
  12269. if (rc != MBX_TIMEOUT)
  12270. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12271. if (shdr_status || shdr_add_status || rc) {
  12272. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12273. "2988 POST_SGL_BLOCK mailbox "
  12274. "command failed status x%x "
  12275. "add_status x%x mbx status x%x\n",
  12276. shdr_status, shdr_add_status, rc);
  12277. rc = -ENXIO;
  12278. goto err_exit;
  12279. }
  12280. if (ttl_cnt >= els_xri_cnt)
  12281. break;
  12282. }
  12283. err_exit:
  12284. if (rc == 0)
  12285. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12286. LPFC_XRI_RSRC_RDY);
  12287. return rc;
  12288. }
  12289. /**
  12290. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12291. * @phba: pointer to lpfc hba data structure.
  12292. * @sblist: pointer to scsi buffer list.
  12293. * @count: number of scsi buffers on the list.
  12294. *
  12295. * This routine is invoked to post a block of @count scsi sgl pages from a
  12296. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12297. * No Lock is held.
  12298. *
  12299. **/
  12300. int
  12301. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  12302. int cnt)
  12303. {
  12304. struct lpfc_scsi_buf *psb;
  12305. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12306. struct sgl_page_pairs *sgl_pg_pairs;
  12307. void *viraddr;
  12308. LPFC_MBOXQ_t *mbox;
  12309. uint32_t reqlen, alloclen, pg_pairs;
  12310. uint32_t mbox_tmo;
  12311. uint16_t xritag_start = 0;
  12312. int rc = 0;
  12313. uint32_t shdr_status, shdr_add_status;
  12314. dma_addr_t pdma_phys_bpl1;
  12315. union lpfc_sli4_cfg_shdr *shdr;
  12316. /* Calculate the requested length of the dma memory */
  12317. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12318. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12319. if (reqlen > SLI4_PAGE_SIZE) {
  12320. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12321. "0217 Block sgl registration required DMA "
  12322. "size (%d) great than a page\n", reqlen);
  12323. return -ENOMEM;
  12324. }
  12325. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12326. if (!mbox) {
  12327. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12328. "0283 Failed to allocate mbox cmd memory\n");
  12329. return -ENOMEM;
  12330. }
  12331. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12332. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12333. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12334. LPFC_SLI4_MBX_NEMBED);
  12335. if (alloclen < reqlen) {
  12336. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12337. "2561 Allocated DMA memory size (%d) is "
  12338. "less than the requested DMA memory "
  12339. "size (%d)\n", alloclen, reqlen);
  12340. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12341. return -ENOMEM;
  12342. }
  12343. /* Get the first SGE entry from the non-embedded DMA memory */
  12344. viraddr = mbox->sge_array->addr[0];
  12345. /* Set up the SGL pages in the non-embedded DMA pages */
  12346. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12347. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12348. pg_pairs = 0;
  12349. list_for_each_entry(psb, sblist, list) {
  12350. /* Set up the sge entry */
  12351. sgl_pg_pairs->sgl_pg0_addr_lo =
  12352. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12353. sgl_pg_pairs->sgl_pg0_addr_hi =
  12354. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12355. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12356. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12357. else
  12358. pdma_phys_bpl1 = 0;
  12359. sgl_pg_pairs->sgl_pg1_addr_lo =
  12360. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12361. sgl_pg_pairs->sgl_pg1_addr_hi =
  12362. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12363. /* Keep the first xritag on the list */
  12364. if (pg_pairs == 0)
  12365. xritag_start = psb->cur_iocbq.sli4_xritag;
  12366. sgl_pg_pairs++;
  12367. pg_pairs++;
  12368. }
  12369. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12370. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12371. /* Perform endian conversion if necessary */
  12372. sgl->word0 = cpu_to_le32(sgl->word0);
  12373. if (!phba->sli4_hba.intr_enable)
  12374. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12375. else {
  12376. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12377. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12378. }
  12379. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12380. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12381. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12382. if (rc != MBX_TIMEOUT)
  12383. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12384. if (shdr_status || shdr_add_status || rc) {
  12385. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12386. "2564 POST_SGL_BLOCK mailbox command failed "
  12387. "status x%x add_status x%x mbx status x%x\n",
  12388. shdr_status, shdr_add_status, rc);
  12389. rc = -ENXIO;
  12390. }
  12391. return rc;
  12392. }
  12393. /**
  12394. * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
  12395. * @phba: pointer to lpfc hba data structure.
  12396. * @sblist: pointer to scsi buffer list.
  12397. * @count: number of scsi buffers on the list.
  12398. *
  12399. * This routine is invoked to post a block of @count scsi sgl pages from a
  12400. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12401. * No Lock is held.
  12402. *
  12403. **/
  12404. int
  12405. lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
  12406. int cnt)
  12407. {
  12408. struct lpfc_scsi_buf *psb = NULL;
  12409. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12410. struct sgl_page_pairs *sgl_pg_pairs;
  12411. void *viraddr;
  12412. LPFC_MBOXQ_t *mbox;
  12413. uint32_t reqlen, alloclen, pg_pairs;
  12414. uint32_t mbox_tmo;
  12415. uint16_t xri_start = 0, scsi_xri_start;
  12416. uint16_t rsrc_range;
  12417. int rc = 0, avail_cnt;
  12418. uint32_t shdr_status, shdr_add_status;
  12419. dma_addr_t pdma_phys_bpl1;
  12420. union lpfc_sli4_cfg_shdr *shdr;
  12421. struct lpfc_rsrc_blks *rsrc_blk;
  12422. uint32_t xri_cnt = 0;
  12423. /* Calculate the total requested length of the dma memory */
  12424. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12425. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12426. if (reqlen > SLI4_PAGE_SIZE) {
  12427. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12428. "2932 Block sgl registration required DMA "
  12429. "size (%d) great than a page\n", reqlen);
  12430. return -ENOMEM;
  12431. }
  12432. /*
  12433. * The use of extents requires the driver to post the sgl headers
  12434. * in multiple postings to meet the contiguous resource assignment.
  12435. */
  12436. psb = list_prepare_entry(psb, sblist, list);
  12437. scsi_xri_start = phba->sli4_hba.scsi_xri_start;
  12438. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12439. list) {
  12440. rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
  12441. if (rsrc_range < scsi_xri_start)
  12442. continue;
  12443. else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
  12444. continue;
  12445. else
  12446. avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
  12447. reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
  12448. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12449. /*
  12450. * Allocate DMA memory and set up the non-embedded mailbox
  12451. * command. The mbox is used to post an SGL page per loop
  12452. * but the DMA memory has a use-once semantic so the mailbox
  12453. * is used and freed per loop pass.
  12454. */
  12455. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12456. if (!mbox) {
  12457. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12458. "2933 Failed to allocate mbox cmd "
  12459. "memory\n");
  12460. return -ENOMEM;
  12461. }
  12462. alloclen = lpfc_sli4_config(phba, mbox,
  12463. LPFC_MBOX_SUBSYSTEM_FCOE,
  12464. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12465. reqlen,
  12466. LPFC_SLI4_MBX_NEMBED);
  12467. if (alloclen < reqlen) {
  12468. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12469. "2934 Allocated DMA memory size (%d) "
  12470. "is less than the requested DMA memory "
  12471. "size (%d)\n", alloclen, reqlen);
  12472. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12473. return -ENOMEM;
  12474. }
  12475. /* Get the first SGE entry from the non-embedded DMA memory */
  12476. viraddr = mbox->sge_array->addr[0];
  12477. /* Set up the SGL pages in the non-embedded DMA pages */
  12478. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12479. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12480. /* pg_pairs tracks posted SGEs per loop iteration. */
  12481. pg_pairs = 0;
  12482. list_for_each_entry_continue(psb, sblist, list) {
  12483. /* Set up the sge entry */
  12484. sgl_pg_pairs->sgl_pg0_addr_lo =
  12485. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12486. sgl_pg_pairs->sgl_pg0_addr_hi =
  12487. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12488. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12489. pdma_phys_bpl1 = psb->dma_phys_bpl +
  12490. SGL_PAGE_SIZE;
  12491. else
  12492. pdma_phys_bpl1 = 0;
  12493. sgl_pg_pairs->sgl_pg1_addr_lo =
  12494. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12495. sgl_pg_pairs->sgl_pg1_addr_hi =
  12496. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12497. /* Keep the first xri for this extent. */
  12498. if (pg_pairs == 0)
  12499. xri_start = psb->cur_iocbq.sli4_xritag;
  12500. sgl_pg_pairs++;
  12501. pg_pairs++;
  12502. xri_cnt++;
  12503. /*
  12504. * Track two exit conditions - the loop has constructed
  12505. * all of the caller's SGE pairs or all available
  12506. * resource IDs in this extent are consumed.
  12507. */
  12508. if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
  12509. break;
  12510. }
  12511. rsrc_blk->rsrc_used += pg_pairs;
  12512. bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
  12513. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12514. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12515. "3016 Post SCSI Extent SGL, start %d, cnt %d "
  12516. "blk use %d\n",
  12517. xri_start, pg_pairs, rsrc_blk->rsrc_used);
  12518. /* Perform endian conversion if necessary */
  12519. sgl->word0 = cpu_to_le32(sgl->word0);
  12520. if (!phba->sli4_hba.intr_enable)
  12521. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12522. else {
  12523. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12524. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12525. }
  12526. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12527. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12528. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12529. &shdr->response);
  12530. if (rc != MBX_TIMEOUT)
  12531. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12532. if (shdr_status || shdr_add_status || rc) {
  12533. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12534. "2935 POST_SGL_BLOCK mailbox command "
  12535. "failed status x%x add_status x%x "
  12536. "mbx status x%x\n",
  12537. shdr_status, shdr_add_status, rc);
  12538. return -ENXIO;
  12539. }
  12540. /* Post only what is requested. */
  12541. if (xri_cnt >= cnt)
  12542. break;
  12543. }
  12544. return rc;
  12545. }
  12546. /**
  12547. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12548. * @phba: pointer to lpfc_hba struct that the frame was received on
  12549. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12550. *
  12551. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12552. * valid type of frame that the LPFC driver will handle. This function will
  12553. * return a zero if the frame is a valid frame or a non zero value when the
  12554. * frame does not pass the check.
  12555. **/
  12556. static int
  12557. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12558. {
  12559. /* make rctl_names static to save stack space */
  12560. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12561. char *type_names[] = FC_TYPE_NAMES_INIT;
  12562. struct fc_vft_header *fc_vft_hdr;
  12563. uint32_t *header = (uint32_t *) fc_hdr;
  12564. switch (fc_hdr->fh_r_ctl) {
  12565. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12566. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12567. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12568. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12569. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12570. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12571. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12572. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12573. case FC_RCTL_ELS_REQ: /* extended link services request */
  12574. case FC_RCTL_ELS_REP: /* extended link services reply */
  12575. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12576. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12577. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12578. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12579. case FC_RCTL_BA_RMC: /* remove connection */
  12580. case FC_RCTL_BA_ACC: /* basic accept */
  12581. case FC_RCTL_BA_RJT: /* basic reject */
  12582. case FC_RCTL_BA_PRMT:
  12583. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12584. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12585. case FC_RCTL_P_RJT: /* port reject */
  12586. case FC_RCTL_F_RJT: /* fabric reject */
  12587. case FC_RCTL_P_BSY: /* port busy */
  12588. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12589. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12590. case FC_RCTL_LCR: /* link credit reset */
  12591. case FC_RCTL_END: /* end */
  12592. break;
  12593. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12594. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12595. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12596. return lpfc_fc_frame_check(phba, fc_hdr);
  12597. default:
  12598. goto drop;
  12599. }
  12600. switch (fc_hdr->fh_type) {
  12601. case FC_TYPE_BLS:
  12602. case FC_TYPE_ELS:
  12603. case FC_TYPE_FCP:
  12604. case FC_TYPE_CT:
  12605. break;
  12606. case FC_TYPE_IP:
  12607. case FC_TYPE_ILS:
  12608. default:
  12609. goto drop;
  12610. }
  12611. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12612. "2538 Received frame rctl:%s type:%s "
  12613. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12614. rctl_names[fc_hdr->fh_r_ctl],
  12615. type_names[fc_hdr->fh_type],
  12616. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12617. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12618. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12619. return 0;
  12620. drop:
  12621. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12622. "2539 Dropped frame rctl:%s type:%s\n",
  12623. rctl_names[fc_hdr->fh_r_ctl],
  12624. type_names[fc_hdr->fh_type]);
  12625. return 1;
  12626. }
  12627. /**
  12628. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12629. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12630. *
  12631. * This function processes the FC header to retrieve the VFI from the VF
  12632. * header, if one exists. This function will return the VFI if one exists
  12633. * or 0 if no VSAN Header exists.
  12634. **/
  12635. static uint32_t
  12636. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12637. {
  12638. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12639. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12640. return 0;
  12641. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12642. }
  12643. /**
  12644. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12645. * @phba: Pointer to the HBA structure to search for the vport on
  12646. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12647. * @fcfi: The FC Fabric ID that the frame came from
  12648. *
  12649. * This function searches the @phba for a vport that matches the content of the
  12650. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12651. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12652. * returns the matching vport pointer or NULL if unable to match frame to a
  12653. * vport.
  12654. **/
  12655. static struct lpfc_vport *
  12656. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12657. uint16_t fcfi)
  12658. {
  12659. struct lpfc_vport **vports;
  12660. struct lpfc_vport *vport = NULL;
  12661. int i;
  12662. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12663. fc_hdr->fh_d_id[1] << 8 |
  12664. fc_hdr->fh_d_id[2]);
  12665. if (did == Fabric_DID)
  12666. return phba->pport;
  12667. vports = lpfc_create_vport_work_array(phba);
  12668. if (vports != NULL)
  12669. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12670. if (phba->fcf.fcfi == fcfi &&
  12671. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12672. vports[i]->fc_myDID == did) {
  12673. vport = vports[i];
  12674. break;
  12675. }
  12676. }
  12677. lpfc_destroy_vport_work_array(phba, vports);
  12678. return vport;
  12679. }
  12680. /**
  12681. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12682. * @vport: The vport to work on.
  12683. *
  12684. * This function updates the receive sequence time stamp for this vport. The
  12685. * receive sequence time stamp indicates the time that the last frame of the
  12686. * the sequence that has been idle for the longest amount of time was received.
  12687. * the driver uses this time stamp to indicate if any received sequences have
  12688. * timed out.
  12689. **/
  12690. void
  12691. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12692. {
  12693. struct lpfc_dmabuf *h_buf;
  12694. struct hbq_dmabuf *dmabuf = NULL;
  12695. /* get the oldest sequence on the rcv list */
  12696. h_buf = list_get_first(&vport->rcv_buffer_list,
  12697. struct lpfc_dmabuf, list);
  12698. if (!h_buf)
  12699. return;
  12700. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12701. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12702. }
  12703. /**
  12704. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12705. * @vport: The vport that the received sequences were sent to.
  12706. *
  12707. * This function cleans up all outstanding received sequences. This is called
  12708. * by the driver when a link event or user action invalidates all the received
  12709. * sequences.
  12710. **/
  12711. void
  12712. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12713. {
  12714. struct lpfc_dmabuf *h_buf, *hnext;
  12715. struct lpfc_dmabuf *d_buf, *dnext;
  12716. struct hbq_dmabuf *dmabuf = NULL;
  12717. /* start with the oldest sequence on the rcv list */
  12718. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12719. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12720. list_del_init(&dmabuf->hbuf.list);
  12721. list_for_each_entry_safe(d_buf, dnext,
  12722. &dmabuf->dbuf.list, list) {
  12723. list_del_init(&d_buf->list);
  12724. lpfc_in_buf_free(vport->phba, d_buf);
  12725. }
  12726. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12727. }
  12728. }
  12729. /**
  12730. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12731. * @vport: The vport that the received sequences were sent to.
  12732. *
  12733. * This function determines whether any received sequences have timed out by
  12734. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12735. * indicates that there is at least one timed out sequence this routine will
  12736. * go through the received sequences one at a time from most inactive to most
  12737. * active to determine which ones need to be cleaned up. Once it has determined
  12738. * that a sequence needs to be cleaned up it will simply free up the resources
  12739. * without sending an abort.
  12740. **/
  12741. void
  12742. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12743. {
  12744. struct lpfc_dmabuf *h_buf, *hnext;
  12745. struct lpfc_dmabuf *d_buf, *dnext;
  12746. struct hbq_dmabuf *dmabuf = NULL;
  12747. unsigned long timeout;
  12748. int abort_count = 0;
  12749. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12750. vport->rcv_buffer_time_stamp);
  12751. if (list_empty(&vport->rcv_buffer_list) ||
  12752. time_before(jiffies, timeout))
  12753. return;
  12754. /* start with the oldest sequence on the rcv list */
  12755. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12756. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12757. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12758. dmabuf->time_stamp);
  12759. if (time_before(jiffies, timeout))
  12760. break;
  12761. abort_count++;
  12762. list_del_init(&dmabuf->hbuf.list);
  12763. list_for_each_entry_safe(d_buf, dnext,
  12764. &dmabuf->dbuf.list, list) {
  12765. list_del_init(&d_buf->list);
  12766. lpfc_in_buf_free(vport->phba, d_buf);
  12767. }
  12768. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12769. }
  12770. if (abort_count)
  12771. lpfc_update_rcv_time_stamp(vport);
  12772. }
  12773. /**
  12774. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12775. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12776. *
  12777. * This function searches through the existing incomplete sequences that have
  12778. * been sent to this @vport. If the frame matches one of the incomplete
  12779. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12780. * make up that sequence. If no sequence is found that matches this frame then
  12781. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12782. * This function returns a pointer to the first dmabuf in the sequence list that
  12783. * the frame was linked to.
  12784. **/
  12785. static struct hbq_dmabuf *
  12786. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12787. {
  12788. struct fc_frame_header *new_hdr;
  12789. struct fc_frame_header *temp_hdr;
  12790. struct lpfc_dmabuf *d_buf;
  12791. struct lpfc_dmabuf *h_buf;
  12792. struct hbq_dmabuf *seq_dmabuf = NULL;
  12793. struct hbq_dmabuf *temp_dmabuf = NULL;
  12794. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12795. dmabuf->time_stamp = jiffies;
  12796. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12797. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12798. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12799. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12800. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12801. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12802. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12803. continue;
  12804. /* found a pending sequence that matches this frame */
  12805. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12806. break;
  12807. }
  12808. if (!seq_dmabuf) {
  12809. /*
  12810. * This indicates first frame received for this sequence.
  12811. * Queue the buffer on the vport's rcv_buffer_list.
  12812. */
  12813. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12814. lpfc_update_rcv_time_stamp(vport);
  12815. return dmabuf;
  12816. }
  12817. temp_hdr = seq_dmabuf->hbuf.virt;
  12818. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12819. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12820. list_del_init(&seq_dmabuf->hbuf.list);
  12821. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12822. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12823. lpfc_update_rcv_time_stamp(vport);
  12824. return dmabuf;
  12825. }
  12826. /* move this sequence to the tail to indicate a young sequence */
  12827. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12828. seq_dmabuf->time_stamp = jiffies;
  12829. lpfc_update_rcv_time_stamp(vport);
  12830. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12831. temp_hdr = dmabuf->hbuf.virt;
  12832. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12833. return seq_dmabuf;
  12834. }
  12835. /* find the correct place in the sequence to insert this frame */
  12836. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12837. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12838. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12839. /*
  12840. * If the frame's sequence count is greater than the frame on
  12841. * the list then insert the frame right after this frame
  12842. */
  12843. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12844. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12845. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12846. return seq_dmabuf;
  12847. }
  12848. }
  12849. return NULL;
  12850. }
  12851. /**
  12852. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12853. * @vport: pointer to a vitural port
  12854. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12855. *
  12856. * This function tries to abort from the partially assembed sequence, described
  12857. * by the information from basic abbort @dmabuf. It checks to see whether such
  12858. * partially assembled sequence held by the driver. If so, it shall free up all
  12859. * the frames from the partially assembled sequence.
  12860. *
  12861. * Return
  12862. * true -- if there is matching partially assembled sequence present and all
  12863. * the frames freed with the sequence;
  12864. * false -- if there is no matching partially assembled sequence present so
  12865. * nothing got aborted in the lower layer driver
  12866. **/
  12867. static bool
  12868. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12869. struct hbq_dmabuf *dmabuf)
  12870. {
  12871. struct fc_frame_header *new_hdr;
  12872. struct fc_frame_header *temp_hdr;
  12873. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12874. struct hbq_dmabuf *seq_dmabuf = NULL;
  12875. /* Use the hdr_buf to find the sequence that matches this frame */
  12876. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12877. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12878. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12879. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12880. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12881. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12882. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12883. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12884. continue;
  12885. /* found a pending sequence that matches this frame */
  12886. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12887. break;
  12888. }
  12889. /* Free up all the frames from the partially assembled sequence */
  12890. if (seq_dmabuf) {
  12891. list_for_each_entry_safe(d_buf, n_buf,
  12892. &seq_dmabuf->dbuf.list, list) {
  12893. list_del_init(&d_buf->list);
  12894. lpfc_in_buf_free(vport->phba, d_buf);
  12895. }
  12896. return true;
  12897. }
  12898. return false;
  12899. }
  12900. /**
  12901. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12902. * @phba: Pointer to HBA context object.
  12903. * @cmd_iocbq: pointer to the command iocbq structure.
  12904. * @rsp_iocbq: pointer to the response iocbq structure.
  12905. *
  12906. * This function handles the sequence abort response iocb command complete
  12907. * event. It properly releases the memory allocated to the sequence abort
  12908. * accept iocb.
  12909. **/
  12910. static void
  12911. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12912. struct lpfc_iocbq *cmd_iocbq,
  12913. struct lpfc_iocbq *rsp_iocbq)
  12914. {
  12915. if (cmd_iocbq)
  12916. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12917. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12918. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12919. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12920. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12921. rsp_iocbq->iocb.ulpStatus,
  12922. rsp_iocbq->iocb.un.ulpWord[4]);
  12923. }
  12924. /**
  12925. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12926. * @phba: Pointer to HBA context object.
  12927. * @xri: xri id in transaction.
  12928. *
  12929. * This function validates the xri maps to the known range of XRIs allocated an
  12930. * used by the driver.
  12931. **/
  12932. uint16_t
  12933. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12934. uint16_t xri)
  12935. {
  12936. int i;
  12937. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12938. if (xri == phba->sli4_hba.xri_ids[i])
  12939. return i;
  12940. }
  12941. return NO_XRI;
  12942. }
  12943. /**
  12944. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12945. * @phba: Pointer to HBA context object.
  12946. * @fc_hdr: pointer to a FC frame header.
  12947. *
  12948. * This function sends a basic response to a previous unsol sequence abort
  12949. * event after aborting the sequence handling.
  12950. **/
  12951. static void
  12952. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12953. struct fc_frame_header *fc_hdr)
  12954. {
  12955. struct lpfc_iocbq *ctiocb = NULL;
  12956. struct lpfc_nodelist *ndlp;
  12957. uint16_t oxid, rxid;
  12958. uint32_t sid, fctl;
  12959. IOCB_t *icmd;
  12960. int rc;
  12961. if (!lpfc_is_link_up(phba))
  12962. return;
  12963. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12964. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12965. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12966. ndlp = lpfc_findnode_did(phba->pport, sid);
  12967. if (!ndlp) {
  12968. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12969. "1268 Find ndlp returned NULL for oxid:x%x "
  12970. "SID:x%x\n", oxid, sid);
  12971. return;
  12972. }
  12973. if (lpfc_sli4_xri_inrange(phba, rxid))
  12974. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12975. /* Allocate buffer for rsp iocb */
  12976. ctiocb = lpfc_sli_get_iocbq(phba);
  12977. if (!ctiocb)
  12978. return;
  12979. /* Extract the F_CTL field from FC_HDR */
  12980. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12981. icmd = &ctiocb->iocb;
  12982. icmd->un.xseq64.bdl.bdeSize = 0;
  12983. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12984. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12985. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12986. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12987. /* Fill in the rest of iocb fields */
  12988. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12989. icmd->ulpBdeCount = 0;
  12990. icmd->ulpLe = 1;
  12991. icmd->ulpClass = CLASS3;
  12992. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12993. ctiocb->context1 = ndlp;
  12994. ctiocb->iocb_cmpl = NULL;
  12995. ctiocb->vport = phba->pport;
  12996. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12997. ctiocb->sli4_lxritag = NO_XRI;
  12998. ctiocb->sli4_xritag = NO_XRI;
  12999. /* If the oxid maps to the FCP XRI range or if it is out of range,
  13000. * send a BLS_RJT. The driver no longer has that exchange.
  13001. * Override the IOCB for a BA_RJT.
  13002. */
  13003. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  13004. phba->sli4_hba.max_cfg_param.xri_base) ||
  13005. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  13006. phba->sli4_hba.max_cfg_param.xri_base)) {
  13007. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13008. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13009. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13010. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13011. }
  13012. if (fctl & FC_FC_EX_CTX) {
  13013. /* ABTS sent by responder to CT exchange, construction
  13014. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13015. * field and RX_ID from ABTS for RX_ID field.
  13016. */
  13017. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13018. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13019. } else {
  13020. /* ABTS sent by initiator to CT exchange, construction
  13021. * of BA_ACC will need to allocate a new XRI as for the
  13022. * XRI_TAG and RX_ID fields.
  13023. */
  13024. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13025. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
  13026. }
  13027. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13028. /* Xmit CT abts response on exchange <xid> */
  13029. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13030. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13031. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13032. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13033. if (rc == IOCB_ERROR) {
  13034. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  13035. "2925 Failed to issue CT ABTS RSP x%x on "
  13036. "xri x%x, Data x%x\n",
  13037. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13038. phba->link_state);
  13039. lpfc_sli_release_iocbq(phba, ctiocb);
  13040. }
  13041. }
  13042. /**
  13043. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13044. * @vport: Pointer to the vport on which this sequence was received
  13045. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13046. *
  13047. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13048. * receive sequence is only partially assembed by the driver, it shall abort
  13049. * the partially assembled frames for the sequence. Otherwise, if the
  13050. * unsolicited receive sequence has been completely assembled and passed to
  13051. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13052. * unsolicited sequence has been aborted. After that, it will issue a basic
  13053. * accept to accept the abort.
  13054. **/
  13055. void
  13056. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13057. struct hbq_dmabuf *dmabuf)
  13058. {
  13059. struct lpfc_hba *phba = vport->phba;
  13060. struct fc_frame_header fc_hdr;
  13061. uint32_t fctl;
  13062. bool abts_par;
  13063. /* Make a copy of fc_hdr before the dmabuf being released */
  13064. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13065. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13066. if (fctl & FC_FC_EX_CTX) {
  13067. /*
  13068. * ABTS sent by responder to exchange, just free the buffer
  13069. */
  13070. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13071. } else {
  13072. /*
  13073. * ABTS sent by initiator to exchange, need to do cleanup
  13074. */
  13075. /* Try to abort partially assembled seq */
  13076. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13077. /* Send abort to ULP if partially seq abort failed */
  13078. if (abts_par == false)
  13079. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13080. else
  13081. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13082. }
  13083. /* Send basic accept (BA_ACC) to the abort requester */
  13084. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13085. }
  13086. /**
  13087. * lpfc_seq_complete - Indicates if a sequence is complete
  13088. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13089. *
  13090. * This function checks the sequence, starting with the frame described by
  13091. * @dmabuf, to see if all the frames associated with this sequence are present.
  13092. * the frames associated with this sequence are linked to the @dmabuf using the
  13093. * dbuf list. This function looks for two major things. 1) That the first frame
  13094. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13095. * set. 3) That there are no holes in the sequence count. The function will
  13096. * return 1 when the sequence is complete, otherwise it will return 0.
  13097. **/
  13098. static int
  13099. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13100. {
  13101. struct fc_frame_header *hdr;
  13102. struct lpfc_dmabuf *d_buf;
  13103. struct hbq_dmabuf *seq_dmabuf;
  13104. uint32_t fctl;
  13105. int seq_count = 0;
  13106. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13107. /* make sure first fame of sequence has a sequence count of zero */
  13108. if (hdr->fh_seq_cnt != seq_count)
  13109. return 0;
  13110. fctl = (hdr->fh_f_ctl[0] << 16 |
  13111. hdr->fh_f_ctl[1] << 8 |
  13112. hdr->fh_f_ctl[2]);
  13113. /* If last frame of sequence we can return success. */
  13114. if (fctl & FC_FC_END_SEQ)
  13115. return 1;
  13116. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13117. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13118. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13119. /* If there is a hole in the sequence count then fail. */
  13120. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13121. return 0;
  13122. fctl = (hdr->fh_f_ctl[0] << 16 |
  13123. hdr->fh_f_ctl[1] << 8 |
  13124. hdr->fh_f_ctl[2]);
  13125. /* If last frame of sequence we can return success. */
  13126. if (fctl & FC_FC_END_SEQ)
  13127. return 1;
  13128. }
  13129. return 0;
  13130. }
  13131. /**
  13132. * lpfc_prep_seq - Prep sequence for ULP processing
  13133. * @vport: Pointer to the vport on which this sequence was received
  13134. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13135. *
  13136. * This function takes a sequence, described by a list of frames, and creates
  13137. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13138. * used to issue to the generic unsolicited sequence handler. This routine
  13139. * returns a pointer to the first iocbq in the list. If the function is unable
  13140. * to allocate an iocbq then it throw out the received frames that were not
  13141. * able to be described and return a pointer to the first iocbq. If unable to
  13142. * allocate any iocbqs (including the first) this function will return NULL.
  13143. **/
  13144. static struct lpfc_iocbq *
  13145. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13146. {
  13147. struct hbq_dmabuf *hbq_buf;
  13148. struct lpfc_dmabuf *d_buf, *n_buf;
  13149. struct lpfc_iocbq *first_iocbq, *iocbq;
  13150. struct fc_frame_header *fc_hdr;
  13151. uint32_t sid;
  13152. uint32_t len, tot_len;
  13153. struct ulp_bde64 *pbde;
  13154. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13155. /* remove from receive buffer list */
  13156. list_del_init(&seq_dmabuf->hbuf.list);
  13157. lpfc_update_rcv_time_stamp(vport);
  13158. /* get the Remote Port's SID */
  13159. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13160. tot_len = 0;
  13161. /* Get an iocbq struct to fill in. */
  13162. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13163. if (first_iocbq) {
  13164. /* Initialize the first IOCB. */
  13165. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13166. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13167. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13168. first_iocbq->iocb.ulpContext = NO_XRI;
  13169. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13170. be16_to_cpu(fc_hdr->fh_ox_id);
  13171. /* iocbq is prepped for internal consumption. Physical vpi. */
  13172. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13173. vport->phba->vpi_ids[vport->vpi];
  13174. /* put the first buffer into the first IOCBq */
  13175. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13176. first_iocbq->context3 = NULL;
  13177. first_iocbq->iocb.ulpBdeCount = 1;
  13178. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13179. LPFC_DATA_BUF_SIZE;
  13180. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13181. tot_len = bf_get(lpfc_rcqe_length,
  13182. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13183. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13184. }
  13185. iocbq = first_iocbq;
  13186. /*
  13187. * Each IOCBq can have two Buffers assigned, so go through the list
  13188. * of buffers for this sequence and save two buffers in each IOCBq
  13189. */
  13190. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13191. if (!iocbq) {
  13192. lpfc_in_buf_free(vport->phba, d_buf);
  13193. continue;
  13194. }
  13195. if (!iocbq->context3) {
  13196. iocbq->context3 = d_buf;
  13197. iocbq->iocb.ulpBdeCount++;
  13198. pbde = (struct ulp_bde64 *)
  13199. &iocbq->iocb.unsli3.sli3Words[4];
  13200. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13201. /* We need to get the size out of the right CQE */
  13202. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13203. len = bf_get(lpfc_rcqe_length,
  13204. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13205. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13206. tot_len += len;
  13207. } else {
  13208. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13209. if (!iocbq) {
  13210. if (first_iocbq) {
  13211. first_iocbq->iocb.ulpStatus =
  13212. IOSTAT_FCP_RSP_ERROR;
  13213. first_iocbq->iocb.un.ulpWord[4] =
  13214. IOERR_NO_RESOURCES;
  13215. }
  13216. lpfc_in_buf_free(vport->phba, d_buf);
  13217. continue;
  13218. }
  13219. iocbq->context2 = d_buf;
  13220. iocbq->context3 = NULL;
  13221. iocbq->iocb.ulpBdeCount = 1;
  13222. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13223. LPFC_DATA_BUF_SIZE;
  13224. /* We need to get the size out of the right CQE */
  13225. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13226. len = bf_get(lpfc_rcqe_length,
  13227. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13228. tot_len += len;
  13229. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13230. iocbq->iocb.un.rcvels.remoteID = sid;
  13231. list_add_tail(&iocbq->list, &first_iocbq->list);
  13232. }
  13233. }
  13234. return first_iocbq;
  13235. }
  13236. static void
  13237. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13238. struct hbq_dmabuf *seq_dmabuf)
  13239. {
  13240. struct fc_frame_header *fc_hdr;
  13241. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13242. struct lpfc_hba *phba = vport->phba;
  13243. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13244. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13245. if (!iocbq) {
  13246. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13247. "2707 Ring %d handler: Failed to allocate "
  13248. "iocb Rctl x%x Type x%x received\n",
  13249. LPFC_ELS_RING,
  13250. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13251. return;
  13252. }
  13253. if (!lpfc_complete_unsol_iocb(phba,
  13254. &phba->sli.ring[LPFC_ELS_RING],
  13255. iocbq, fc_hdr->fh_r_ctl,
  13256. fc_hdr->fh_type))
  13257. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13258. "2540 Ring %d handler: unexpected Rctl "
  13259. "x%x Type x%x received\n",
  13260. LPFC_ELS_RING,
  13261. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13262. /* Free iocb created in lpfc_prep_seq */
  13263. list_for_each_entry_safe(curr_iocb, next_iocb,
  13264. &iocbq->list, list) {
  13265. list_del_init(&curr_iocb->list);
  13266. lpfc_sli_release_iocbq(phba, curr_iocb);
  13267. }
  13268. lpfc_sli_release_iocbq(phba, iocbq);
  13269. }
  13270. /**
  13271. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13272. * @phba: Pointer to HBA context object.
  13273. *
  13274. * This function is called with no lock held. This function processes all
  13275. * the received buffers and gives it to upper layers when a received buffer
  13276. * indicates that it is the final frame in the sequence. The interrupt
  13277. * service routine processes received buffers at interrupt contexts and adds
  13278. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13279. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13280. * appropriate receive function when the final frame in a sequence is received.
  13281. **/
  13282. void
  13283. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13284. struct hbq_dmabuf *dmabuf)
  13285. {
  13286. struct hbq_dmabuf *seq_dmabuf;
  13287. struct fc_frame_header *fc_hdr;
  13288. struct lpfc_vport *vport;
  13289. uint32_t fcfi;
  13290. /* Process each received buffer */
  13291. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13292. /* check to see if this a valid type of frame */
  13293. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13294. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13295. return;
  13296. }
  13297. if ((bf_get(lpfc_cqe_code,
  13298. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13299. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13300. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13301. else
  13302. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13303. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13304. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13305. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13306. /* throw out the frame */
  13307. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13308. return;
  13309. }
  13310. /* Handle the basic abort sequence (BA_ABTS) event */
  13311. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13312. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13313. return;
  13314. }
  13315. /* Link this frame */
  13316. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13317. if (!seq_dmabuf) {
  13318. /* unable to add frame to vport - throw it out */
  13319. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13320. return;
  13321. }
  13322. /* If not last frame in sequence continue processing frames. */
  13323. if (!lpfc_seq_complete(seq_dmabuf))
  13324. return;
  13325. /* Send the complete sequence to the upper layer protocol */
  13326. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13327. }
  13328. /**
  13329. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13330. * @phba: pointer to lpfc hba data structure.
  13331. *
  13332. * This routine is invoked to post rpi header templates to the
  13333. * HBA consistent with the SLI-4 interface spec. This routine
  13334. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13335. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13336. *
  13337. * This routine does not require any locks. It's usage is expected
  13338. * to be driver load or reset recovery when the driver is
  13339. * sequential.
  13340. *
  13341. * Return codes
  13342. * 0 - successful
  13343. * -EIO - The mailbox failed to complete successfully.
  13344. * When this error occurs, the driver is not guaranteed
  13345. * to have any rpi regions posted to the device and
  13346. * must either attempt to repost the regions or take a
  13347. * fatal error.
  13348. **/
  13349. int
  13350. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13351. {
  13352. struct lpfc_rpi_hdr *rpi_page;
  13353. uint32_t rc = 0;
  13354. uint16_t lrpi = 0;
  13355. /* SLI4 ports that support extents do not require RPI headers. */
  13356. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13357. goto exit;
  13358. if (phba->sli4_hba.extents_in_use)
  13359. return -EIO;
  13360. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13361. /*
  13362. * Assign the rpi headers a physical rpi only if the driver
  13363. * has not initialized those resources. A port reset only
  13364. * needs the headers posted.
  13365. */
  13366. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13367. LPFC_RPI_RSRC_RDY)
  13368. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13369. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13370. if (rc != MBX_SUCCESS) {
  13371. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13372. "2008 Error %d posting all rpi "
  13373. "headers\n", rc);
  13374. rc = -EIO;
  13375. break;
  13376. }
  13377. }
  13378. exit:
  13379. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13380. LPFC_RPI_RSRC_RDY);
  13381. return rc;
  13382. }
  13383. /**
  13384. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13385. * @phba: pointer to lpfc hba data structure.
  13386. * @rpi_page: pointer to the rpi memory region.
  13387. *
  13388. * This routine is invoked to post a single rpi header to the
  13389. * HBA consistent with the SLI-4 interface spec. This memory region
  13390. * maps up to 64 rpi context regions.
  13391. *
  13392. * Return codes
  13393. * 0 - successful
  13394. * -ENOMEM - No available memory
  13395. * -EIO - The mailbox failed to complete successfully.
  13396. **/
  13397. int
  13398. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13399. {
  13400. LPFC_MBOXQ_t *mboxq;
  13401. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13402. uint32_t rc = 0;
  13403. uint32_t shdr_status, shdr_add_status;
  13404. union lpfc_sli4_cfg_shdr *shdr;
  13405. /* SLI4 ports that support extents do not require RPI headers. */
  13406. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13407. return rc;
  13408. if (phba->sli4_hba.extents_in_use)
  13409. return -EIO;
  13410. /* The port is notified of the header region via a mailbox command. */
  13411. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13412. if (!mboxq) {
  13413. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13414. "2001 Unable to allocate memory for issuing "
  13415. "SLI_CONFIG_SPECIAL mailbox command\n");
  13416. return -ENOMEM;
  13417. }
  13418. /* Post all rpi memory regions to the port. */
  13419. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13420. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13421. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13422. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13423. sizeof(struct lpfc_sli4_cfg_mhdr),
  13424. LPFC_SLI4_MBX_EMBED);
  13425. /* Post the physical rpi to the port for this rpi header. */
  13426. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13427. rpi_page->start_rpi);
  13428. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13429. hdr_tmpl, rpi_page->page_count);
  13430. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13431. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13432. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13433. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13434. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13435. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13436. if (rc != MBX_TIMEOUT)
  13437. mempool_free(mboxq, phba->mbox_mem_pool);
  13438. if (shdr_status || shdr_add_status || rc) {
  13439. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13440. "2514 POST_RPI_HDR mailbox failed with "
  13441. "status x%x add_status x%x, mbx status x%x\n",
  13442. shdr_status, shdr_add_status, rc);
  13443. rc = -ENXIO;
  13444. }
  13445. return rc;
  13446. }
  13447. /**
  13448. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13449. * @phba: pointer to lpfc hba data structure.
  13450. *
  13451. * This routine is invoked to post rpi header templates to the
  13452. * HBA consistent with the SLI-4 interface spec. This routine
  13453. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13454. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13455. *
  13456. * Returns
  13457. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13458. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13459. **/
  13460. int
  13461. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13462. {
  13463. unsigned long rpi;
  13464. uint16_t max_rpi, rpi_limit;
  13465. uint16_t rpi_remaining, lrpi = 0;
  13466. struct lpfc_rpi_hdr *rpi_hdr;
  13467. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13468. rpi_limit = phba->sli4_hba.next_rpi;
  13469. /*
  13470. * Fetch the next logical rpi. Because this index is logical,
  13471. * the driver starts at 0 each time.
  13472. */
  13473. spin_lock_irq(&phba->hbalock);
  13474. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13475. if (rpi >= rpi_limit)
  13476. rpi = LPFC_RPI_ALLOC_ERROR;
  13477. else {
  13478. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13479. phba->sli4_hba.max_cfg_param.rpi_used++;
  13480. phba->sli4_hba.rpi_count++;
  13481. }
  13482. /*
  13483. * Don't try to allocate more rpi header regions if the device limit
  13484. * has been exhausted.
  13485. */
  13486. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13487. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13488. spin_unlock_irq(&phba->hbalock);
  13489. return rpi;
  13490. }
  13491. /*
  13492. * RPI header postings are not required for SLI4 ports capable of
  13493. * extents.
  13494. */
  13495. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13496. spin_unlock_irq(&phba->hbalock);
  13497. return rpi;
  13498. }
  13499. /*
  13500. * If the driver is running low on rpi resources, allocate another
  13501. * page now. Note that the next_rpi value is used because
  13502. * it represents how many are actually in use whereas max_rpi notes
  13503. * how many are supported max by the device.
  13504. */
  13505. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13506. spin_unlock_irq(&phba->hbalock);
  13507. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13508. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13509. if (!rpi_hdr) {
  13510. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13511. "2002 Error Could not grow rpi "
  13512. "count\n");
  13513. } else {
  13514. lrpi = rpi_hdr->start_rpi;
  13515. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13516. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13517. }
  13518. }
  13519. return rpi;
  13520. }
  13521. /**
  13522. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13523. * @phba: pointer to lpfc hba data structure.
  13524. *
  13525. * This routine is invoked to release an rpi to the pool of
  13526. * available rpis maintained by the driver.
  13527. **/
  13528. void
  13529. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13530. {
  13531. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13532. phba->sli4_hba.rpi_count--;
  13533. phba->sli4_hba.max_cfg_param.rpi_used--;
  13534. }
  13535. }
  13536. /**
  13537. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13538. * @phba: pointer to lpfc hba data structure.
  13539. *
  13540. * This routine is invoked to release an rpi to the pool of
  13541. * available rpis maintained by the driver.
  13542. **/
  13543. void
  13544. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13545. {
  13546. spin_lock_irq(&phba->hbalock);
  13547. __lpfc_sli4_free_rpi(phba, rpi);
  13548. spin_unlock_irq(&phba->hbalock);
  13549. }
  13550. /**
  13551. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13552. * @phba: pointer to lpfc hba data structure.
  13553. *
  13554. * This routine is invoked to remove the memory region that
  13555. * provided rpi via a bitmask.
  13556. **/
  13557. void
  13558. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13559. {
  13560. kfree(phba->sli4_hba.rpi_bmask);
  13561. kfree(phba->sli4_hba.rpi_ids);
  13562. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13563. }
  13564. /**
  13565. * lpfc_sli4_resume_rpi - 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. int
  13572. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13573. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13574. {
  13575. LPFC_MBOXQ_t *mboxq;
  13576. struct lpfc_hba *phba = ndlp->phba;
  13577. int rc;
  13578. /* The port is notified of the header region via a mailbox command. */
  13579. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13580. if (!mboxq)
  13581. return -ENOMEM;
  13582. /* Post all rpi memory regions to the port. */
  13583. lpfc_resume_rpi(mboxq, ndlp);
  13584. if (cmpl) {
  13585. mboxq->mbox_cmpl = cmpl;
  13586. mboxq->context1 = arg;
  13587. mboxq->context2 = ndlp;
  13588. } else
  13589. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13590. mboxq->vport = ndlp->vport;
  13591. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13592. if (rc == MBX_NOT_FINISHED) {
  13593. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13594. "2010 Resume RPI Mailbox failed "
  13595. "status %d, mbxStatus x%x\n", rc,
  13596. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13597. mempool_free(mboxq, phba->mbox_mem_pool);
  13598. return -EIO;
  13599. }
  13600. return 0;
  13601. }
  13602. /**
  13603. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13604. * @vport: Pointer to the vport for which the vpi is being initialized
  13605. *
  13606. * This routine is invoked to activate a vpi with the port.
  13607. *
  13608. * Returns:
  13609. * 0 success
  13610. * -Evalue otherwise
  13611. **/
  13612. int
  13613. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13614. {
  13615. LPFC_MBOXQ_t *mboxq;
  13616. int rc = 0;
  13617. int retval = MBX_SUCCESS;
  13618. uint32_t mbox_tmo;
  13619. struct lpfc_hba *phba = vport->phba;
  13620. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13621. if (!mboxq)
  13622. return -ENOMEM;
  13623. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13624. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13625. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13626. if (rc != MBX_SUCCESS) {
  13627. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13628. "2022 INIT VPI Mailbox failed "
  13629. "status %d, mbxStatus x%x\n", rc,
  13630. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13631. retval = -EIO;
  13632. }
  13633. if (rc != MBX_TIMEOUT)
  13634. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13635. return retval;
  13636. }
  13637. /**
  13638. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13639. * @phba: pointer to lpfc hba data structure.
  13640. * @mboxq: Pointer to mailbox object.
  13641. *
  13642. * This routine is invoked to manually add a single FCF record. The caller
  13643. * must pass a completely initialized FCF_Record. This routine takes
  13644. * care of the nonembedded mailbox operations.
  13645. **/
  13646. static void
  13647. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13648. {
  13649. void *virt_addr;
  13650. union lpfc_sli4_cfg_shdr *shdr;
  13651. uint32_t shdr_status, shdr_add_status;
  13652. virt_addr = mboxq->sge_array->addr[0];
  13653. /* The IOCTL status is embedded in the mailbox subheader. */
  13654. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13655. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13656. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13657. if ((shdr_status || shdr_add_status) &&
  13658. (shdr_status != STATUS_FCF_IN_USE))
  13659. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13660. "2558 ADD_FCF_RECORD mailbox failed with "
  13661. "status x%x add_status x%x\n",
  13662. shdr_status, shdr_add_status);
  13663. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13664. }
  13665. /**
  13666. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13667. * @phba: pointer to lpfc hba data structure.
  13668. * @fcf_record: pointer to the initialized fcf record to add.
  13669. *
  13670. * This routine is invoked to manually add a single FCF record. The caller
  13671. * must pass a completely initialized FCF_Record. This routine takes
  13672. * care of the nonembedded mailbox operations.
  13673. **/
  13674. int
  13675. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13676. {
  13677. int rc = 0;
  13678. LPFC_MBOXQ_t *mboxq;
  13679. uint8_t *bytep;
  13680. void *virt_addr;
  13681. dma_addr_t phys_addr;
  13682. struct lpfc_mbx_sge sge;
  13683. uint32_t alloc_len, req_len;
  13684. uint32_t fcfindex;
  13685. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13686. if (!mboxq) {
  13687. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13688. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13689. return -ENOMEM;
  13690. }
  13691. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13692. sizeof(uint32_t);
  13693. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13694. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13695. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13696. req_len, LPFC_SLI4_MBX_NEMBED);
  13697. if (alloc_len < req_len) {
  13698. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13699. "2523 Allocated DMA memory size (x%x) is "
  13700. "less than the requested DMA memory "
  13701. "size (x%x)\n", alloc_len, req_len);
  13702. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13703. return -ENOMEM;
  13704. }
  13705. /*
  13706. * Get the first SGE entry from the non-embedded DMA memory. This
  13707. * routine only uses a single SGE.
  13708. */
  13709. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13710. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13711. virt_addr = mboxq->sge_array->addr[0];
  13712. /*
  13713. * Configure the FCF record for FCFI 0. This is the driver's
  13714. * hardcoded default and gets used in nonFIP mode.
  13715. */
  13716. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13717. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13718. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13719. /*
  13720. * Copy the fcf_index and the FCF Record Data. The data starts after
  13721. * the FCoE header plus word10. The data copy needs to be endian
  13722. * correct.
  13723. */
  13724. bytep += sizeof(uint32_t);
  13725. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13726. mboxq->vport = phba->pport;
  13727. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13728. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13729. if (rc == MBX_NOT_FINISHED) {
  13730. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13731. "2515 ADD_FCF_RECORD mailbox failed with "
  13732. "status 0x%x\n", rc);
  13733. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13734. rc = -EIO;
  13735. } else
  13736. rc = 0;
  13737. return rc;
  13738. }
  13739. /**
  13740. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13741. * @phba: pointer to lpfc hba data structure.
  13742. * @fcf_record: pointer to the fcf record to write the default data.
  13743. * @fcf_index: FCF table entry index.
  13744. *
  13745. * This routine is invoked to build the driver's default FCF record. The
  13746. * values used are hardcoded. This routine handles memory initialization.
  13747. *
  13748. **/
  13749. void
  13750. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13751. struct fcf_record *fcf_record,
  13752. uint16_t fcf_index)
  13753. {
  13754. memset(fcf_record, 0, sizeof(struct fcf_record));
  13755. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13756. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13757. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13758. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13759. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13760. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13761. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13762. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13763. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13764. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13765. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13766. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13767. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13768. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13769. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13770. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13771. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13772. /* Set the VLAN bit map */
  13773. if (phba->valid_vlan) {
  13774. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13775. = 1 << (phba->vlan_id % 8);
  13776. }
  13777. }
  13778. /**
  13779. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13780. * @phba: pointer to lpfc hba data structure.
  13781. * @fcf_index: FCF table entry offset.
  13782. *
  13783. * This routine is invoked to scan the entire FCF table by reading FCF
  13784. * record and processing it one at a time starting from the @fcf_index
  13785. * for initial FCF discovery or fast FCF failover rediscovery.
  13786. *
  13787. * Return 0 if the mailbox command is submitted successfully, none 0
  13788. * otherwise.
  13789. **/
  13790. int
  13791. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13792. {
  13793. int rc = 0, error;
  13794. LPFC_MBOXQ_t *mboxq;
  13795. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13796. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13797. if (!mboxq) {
  13798. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13799. "2000 Failed to allocate mbox for "
  13800. "READ_FCF cmd\n");
  13801. error = -ENOMEM;
  13802. goto fail_fcf_scan;
  13803. }
  13804. /* Construct the read FCF record mailbox command */
  13805. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13806. if (rc) {
  13807. error = -EINVAL;
  13808. goto fail_fcf_scan;
  13809. }
  13810. /* Issue the mailbox command asynchronously */
  13811. mboxq->vport = phba->pport;
  13812. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13813. spin_lock_irq(&phba->hbalock);
  13814. phba->hba_flag |= FCF_TS_INPROG;
  13815. spin_unlock_irq(&phba->hbalock);
  13816. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13817. if (rc == MBX_NOT_FINISHED)
  13818. error = -EIO;
  13819. else {
  13820. /* Reset eligible FCF count for new scan */
  13821. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13822. phba->fcf.eligible_fcf_cnt = 0;
  13823. error = 0;
  13824. }
  13825. fail_fcf_scan:
  13826. if (error) {
  13827. if (mboxq)
  13828. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13829. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13830. spin_lock_irq(&phba->hbalock);
  13831. phba->hba_flag &= ~FCF_TS_INPROG;
  13832. spin_unlock_irq(&phba->hbalock);
  13833. }
  13834. return error;
  13835. }
  13836. /**
  13837. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13838. * @phba: pointer to lpfc hba data structure.
  13839. * @fcf_index: FCF table entry offset.
  13840. *
  13841. * This routine is invoked to read an FCF record indicated by @fcf_index
  13842. * and to use it for FLOGI roundrobin FCF failover.
  13843. *
  13844. * Return 0 if the mailbox command is submitted successfully, none 0
  13845. * otherwise.
  13846. **/
  13847. int
  13848. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13849. {
  13850. int rc = 0, error;
  13851. LPFC_MBOXQ_t *mboxq;
  13852. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13853. if (!mboxq) {
  13854. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13855. "2763 Failed to allocate mbox for "
  13856. "READ_FCF cmd\n");
  13857. error = -ENOMEM;
  13858. goto fail_fcf_read;
  13859. }
  13860. /* Construct the read FCF record mailbox command */
  13861. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13862. if (rc) {
  13863. error = -EINVAL;
  13864. goto fail_fcf_read;
  13865. }
  13866. /* Issue the mailbox command asynchronously */
  13867. mboxq->vport = phba->pport;
  13868. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13869. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13870. if (rc == MBX_NOT_FINISHED)
  13871. error = -EIO;
  13872. else
  13873. error = 0;
  13874. fail_fcf_read:
  13875. if (error && mboxq)
  13876. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13877. return error;
  13878. }
  13879. /**
  13880. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13881. * @phba: pointer to lpfc hba data structure.
  13882. * @fcf_index: FCF table entry offset.
  13883. *
  13884. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13885. * determine whether it's eligible for FLOGI roundrobin failover list.
  13886. *
  13887. * Return 0 if the mailbox command is submitted successfully, none 0
  13888. * otherwise.
  13889. **/
  13890. int
  13891. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13892. {
  13893. int rc = 0, error;
  13894. LPFC_MBOXQ_t *mboxq;
  13895. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13896. if (!mboxq) {
  13897. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13898. "2758 Failed to allocate mbox for "
  13899. "READ_FCF cmd\n");
  13900. error = -ENOMEM;
  13901. goto fail_fcf_read;
  13902. }
  13903. /* Construct the read FCF record mailbox command */
  13904. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13905. if (rc) {
  13906. error = -EINVAL;
  13907. goto fail_fcf_read;
  13908. }
  13909. /* Issue the mailbox command asynchronously */
  13910. mboxq->vport = phba->pport;
  13911. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13912. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13913. if (rc == MBX_NOT_FINISHED)
  13914. error = -EIO;
  13915. else
  13916. error = 0;
  13917. fail_fcf_read:
  13918. if (error && mboxq)
  13919. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13920. return error;
  13921. }
  13922. /**
  13923. * lpfc_check_next_fcf_pri
  13924. * phba pointer to the lpfc_hba struct for this port.
  13925. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13926. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13927. * rr_bmask based on their priority level. Starting from the highest priority
  13928. * to the lowest. The most likely FCF candidate will be in the highest
  13929. * priority group. When this routine is called it searches the fcf_pri list for
  13930. * next lowest priority group and repopulates the rr_bmask with only those
  13931. * fcf_indexes.
  13932. * returns:
  13933. * 1=success 0=failure
  13934. **/
  13935. int
  13936. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13937. {
  13938. uint16_t next_fcf_pri;
  13939. uint16_t last_index;
  13940. struct lpfc_fcf_pri *fcf_pri;
  13941. int rc;
  13942. int ret = 0;
  13943. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13944. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13945. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13946. "3060 Last IDX %d\n", last_index);
  13947. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13948. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13949. "3061 Last IDX %d\n", last_index);
  13950. return 0; /* Empty rr list */
  13951. }
  13952. next_fcf_pri = 0;
  13953. /*
  13954. * Clear the rr_bmask and set all of the bits that are at this
  13955. * priority.
  13956. */
  13957. memset(phba->fcf.fcf_rr_bmask, 0,
  13958. sizeof(*phba->fcf.fcf_rr_bmask));
  13959. spin_lock_irq(&phba->hbalock);
  13960. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13961. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13962. continue;
  13963. /*
  13964. * the 1st priority that has not FLOGI failed
  13965. * will be the highest.
  13966. */
  13967. if (!next_fcf_pri)
  13968. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13969. spin_unlock_irq(&phba->hbalock);
  13970. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13971. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13972. fcf_pri->fcf_rec.fcf_index);
  13973. if (rc)
  13974. return 0;
  13975. }
  13976. spin_lock_irq(&phba->hbalock);
  13977. }
  13978. /*
  13979. * if next_fcf_pri was not set above and the list is not empty then
  13980. * we have failed flogis on all of them. So reset flogi failed
  13981. * and start at the begining.
  13982. */
  13983. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13984. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13985. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13986. /*
  13987. * the 1st priority that has not FLOGI failed
  13988. * will be the highest.
  13989. */
  13990. if (!next_fcf_pri)
  13991. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13992. spin_unlock_irq(&phba->hbalock);
  13993. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13994. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13995. fcf_pri->fcf_rec.fcf_index);
  13996. if (rc)
  13997. return 0;
  13998. }
  13999. spin_lock_irq(&phba->hbalock);
  14000. }
  14001. } else
  14002. ret = 1;
  14003. spin_unlock_irq(&phba->hbalock);
  14004. return ret;
  14005. }
  14006. /**
  14007. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14008. * @phba: pointer to lpfc hba data structure.
  14009. *
  14010. * This routine is to get the next eligible FCF record index in a round
  14011. * robin fashion. If the next eligible FCF record index equals to the
  14012. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14013. * shall be returned, otherwise, the next eligible FCF record's index
  14014. * shall be returned.
  14015. **/
  14016. uint16_t
  14017. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14018. {
  14019. uint16_t next_fcf_index;
  14020. /* Search start from next bit of currently registered FCF index */
  14021. next_priority:
  14022. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  14023. LPFC_SLI4_FCF_TBL_INDX_MAX;
  14024. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14025. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14026. next_fcf_index);
  14027. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14028. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14029. /*
  14030. * If we have wrapped then we need to clear the bits that
  14031. * have been tested so that we can detect when we should
  14032. * change the priority level.
  14033. */
  14034. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14035. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14036. }
  14037. /* Check roundrobin failover list empty condition */
  14038. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14039. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14040. /*
  14041. * If next fcf index is not found check if there are lower
  14042. * Priority level fcf's in the fcf_priority list.
  14043. * Set up the rr_bmask with all of the avaiable fcf bits
  14044. * at that level and continue the selection process.
  14045. */
  14046. if (lpfc_check_next_fcf_pri_level(phba))
  14047. goto next_priority;
  14048. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14049. "2844 No roundrobin failover FCF available\n");
  14050. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14051. return LPFC_FCOE_FCF_NEXT_NONE;
  14052. else {
  14053. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14054. "3063 Only FCF available idx %d, flag %x\n",
  14055. next_fcf_index,
  14056. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14057. return next_fcf_index;
  14058. }
  14059. }
  14060. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14061. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14062. LPFC_FCF_FLOGI_FAILED)
  14063. goto next_priority;
  14064. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14065. "2845 Get next roundrobin failover FCF (x%x)\n",
  14066. next_fcf_index);
  14067. return next_fcf_index;
  14068. }
  14069. /**
  14070. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14071. * @phba: pointer to lpfc hba data structure.
  14072. *
  14073. * This routine sets the FCF record index in to the eligible bmask for
  14074. * roundrobin failover search. It checks to make sure that the index
  14075. * does not go beyond the range of the driver allocated bmask dimension
  14076. * before setting the bit.
  14077. *
  14078. * Returns 0 if the index bit successfully set, otherwise, it returns
  14079. * -EINVAL.
  14080. **/
  14081. int
  14082. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14083. {
  14084. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14085. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14086. "2610 FCF (x%x) reached driver's book "
  14087. "keeping dimension:x%x\n",
  14088. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14089. return -EINVAL;
  14090. }
  14091. /* Set the eligible FCF record index bmask */
  14092. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14093. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14094. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14095. "bmask\n", fcf_index);
  14096. return 0;
  14097. }
  14098. /**
  14099. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14100. * @phba: pointer to lpfc hba data structure.
  14101. *
  14102. * This routine clears the FCF record index from the eligible bmask for
  14103. * roundrobin failover search. It checks to make sure that the index
  14104. * does not go beyond the range of the driver allocated bmask dimension
  14105. * before clearing the bit.
  14106. **/
  14107. void
  14108. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14109. {
  14110. struct lpfc_fcf_pri *fcf_pri;
  14111. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14112. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14113. "2762 FCF (x%x) reached driver's book "
  14114. "keeping dimension:x%x\n",
  14115. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14116. return;
  14117. }
  14118. /* Clear the eligible FCF record index bmask */
  14119. spin_lock_irq(&phba->hbalock);
  14120. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14121. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14122. list_del_init(&fcf_pri->list);
  14123. break;
  14124. }
  14125. }
  14126. spin_unlock_irq(&phba->hbalock);
  14127. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14128. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14129. "2791 Clear FCF (x%x) from roundrobin failover "
  14130. "bmask\n", fcf_index);
  14131. }
  14132. /**
  14133. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14134. * @phba: pointer to lpfc hba data structure.
  14135. *
  14136. * This routine is the completion routine for the rediscover FCF table mailbox
  14137. * command. If the mailbox command returned failure, it will try to stop the
  14138. * FCF rediscover wait timer.
  14139. **/
  14140. void
  14141. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14142. {
  14143. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14144. uint32_t shdr_status, shdr_add_status;
  14145. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14146. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14147. &redisc_fcf->header.cfg_shdr.response);
  14148. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14149. &redisc_fcf->header.cfg_shdr.response);
  14150. if (shdr_status || shdr_add_status) {
  14151. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14152. "2746 Requesting for FCF rediscovery failed "
  14153. "status x%x add_status x%x\n",
  14154. shdr_status, shdr_add_status);
  14155. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14156. spin_lock_irq(&phba->hbalock);
  14157. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14158. spin_unlock_irq(&phba->hbalock);
  14159. /*
  14160. * CVL event triggered FCF rediscover request failed,
  14161. * last resort to re-try current registered FCF entry.
  14162. */
  14163. lpfc_retry_pport_discovery(phba);
  14164. } else {
  14165. spin_lock_irq(&phba->hbalock);
  14166. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14167. spin_unlock_irq(&phba->hbalock);
  14168. /*
  14169. * DEAD FCF event triggered FCF rediscover request
  14170. * failed, last resort to fail over as a link down
  14171. * to FCF registration.
  14172. */
  14173. lpfc_sli4_fcf_dead_failthrough(phba);
  14174. }
  14175. } else {
  14176. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14177. "2775 Start FCF rediscover quiescent timer\n");
  14178. /*
  14179. * Start FCF rediscovery wait timer for pending FCF
  14180. * before rescan FCF record table.
  14181. */
  14182. lpfc_fcf_redisc_wait_start_timer(phba);
  14183. }
  14184. mempool_free(mbox, phba->mbox_mem_pool);
  14185. }
  14186. /**
  14187. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14188. * @phba: pointer to lpfc hba data structure.
  14189. *
  14190. * This routine is invoked to request for rediscovery of the entire FCF table
  14191. * by the port.
  14192. **/
  14193. int
  14194. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14195. {
  14196. LPFC_MBOXQ_t *mbox;
  14197. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14198. int rc, length;
  14199. /* Cancel retry delay timers to all vports before FCF rediscover */
  14200. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14201. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14202. if (!mbox) {
  14203. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14204. "2745 Failed to allocate mbox for "
  14205. "requesting FCF rediscover.\n");
  14206. return -ENOMEM;
  14207. }
  14208. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14209. sizeof(struct lpfc_sli4_cfg_mhdr));
  14210. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14211. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14212. length, LPFC_SLI4_MBX_EMBED);
  14213. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14214. /* Set count to 0 for invalidating the entire FCF database */
  14215. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14216. /* Issue the mailbox command asynchronously */
  14217. mbox->vport = phba->pport;
  14218. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14219. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14220. if (rc == MBX_NOT_FINISHED) {
  14221. mempool_free(mbox, phba->mbox_mem_pool);
  14222. return -EIO;
  14223. }
  14224. return 0;
  14225. }
  14226. /**
  14227. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14228. * @phba: pointer to lpfc hba data structure.
  14229. *
  14230. * This function is the failover routine as a last resort to the FCF DEAD
  14231. * event when driver failed to perform fast FCF failover.
  14232. **/
  14233. void
  14234. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14235. {
  14236. uint32_t link_state;
  14237. /*
  14238. * Last resort as FCF DEAD event failover will treat this as
  14239. * a link down, but save the link state because we don't want
  14240. * it to be changed to Link Down unless it is already down.
  14241. */
  14242. link_state = phba->link_state;
  14243. lpfc_linkdown(phba);
  14244. phba->link_state = link_state;
  14245. /* Unregister FCF if no devices connected to it */
  14246. lpfc_unregister_unused_fcf(phba);
  14247. }
  14248. /**
  14249. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14250. * @phba: pointer to lpfc hba data structure.
  14251. * @rgn23_data: pointer to configure region 23 data.
  14252. *
  14253. * This function gets SLI3 port configure region 23 data through memory dump
  14254. * mailbox command. When it successfully retrieves data, the size of the data
  14255. * will be returned, otherwise, 0 will be returned.
  14256. **/
  14257. static uint32_t
  14258. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14259. {
  14260. LPFC_MBOXQ_t *pmb = NULL;
  14261. MAILBOX_t *mb;
  14262. uint32_t offset = 0;
  14263. int rc;
  14264. if (!rgn23_data)
  14265. return 0;
  14266. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14267. if (!pmb) {
  14268. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14269. "2600 failed to allocate mailbox memory\n");
  14270. return 0;
  14271. }
  14272. mb = &pmb->u.mb;
  14273. do {
  14274. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14275. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14276. if (rc != MBX_SUCCESS) {
  14277. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14278. "2601 failed to read config "
  14279. "region 23, rc 0x%x Status 0x%x\n",
  14280. rc, mb->mbxStatus);
  14281. mb->un.varDmp.word_cnt = 0;
  14282. }
  14283. /*
  14284. * dump mem may return a zero when finished or we got a
  14285. * mailbox error, either way we are done.
  14286. */
  14287. if (mb->un.varDmp.word_cnt == 0)
  14288. break;
  14289. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14290. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14291. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14292. rgn23_data + offset,
  14293. mb->un.varDmp.word_cnt);
  14294. offset += mb->un.varDmp.word_cnt;
  14295. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14296. mempool_free(pmb, phba->mbox_mem_pool);
  14297. return offset;
  14298. }
  14299. /**
  14300. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14301. * @phba: pointer to lpfc hba data structure.
  14302. * @rgn23_data: pointer to configure region 23 data.
  14303. *
  14304. * This function gets SLI4 port configure region 23 data through memory dump
  14305. * mailbox command. When it successfully retrieves data, the size of the data
  14306. * will be returned, otherwise, 0 will be returned.
  14307. **/
  14308. static uint32_t
  14309. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14310. {
  14311. LPFC_MBOXQ_t *mboxq = NULL;
  14312. struct lpfc_dmabuf *mp = NULL;
  14313. struct lpfc_mqe *mqe;
  14314. uint32_t data_length = 0;
  14315. int rc;
  14316. if (!rgn23_data)
  14317. return 0;
  14318. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14319. if (!mboxq) {
  14320. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14321. "3105 failed to allocate mailbox memory\n");
  14322. return 0;
  14323. }
  14324. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14325. goto out;
  14326. mqe = &mboxq->u.mqe;
  14327. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14328. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14329. if (rc)
  14330. goto out;
  14331. data_length = mqe->un.mb_words[5];
  14332. if (data_length == 0)
  14333. goto out;
  14334. if (data_length > DMP_RGN23_SIZE) {
  14335. data_length = 0;
  14336. goto out;
  14337. }
  14338. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14339. out:
  14340. mempool_free(mboxq, phba->mbox_mem_pool);
  14341. if (mp) {
  14342. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14343. kfree(mp);
  14344. }
  14345. return data_length;
  14346. }
  14347. /**
  14348. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14349. * @phba: pointer to lpfc hba data structure.
  14350. *
  14351. * This function read region 23 and parse TLV for port status to
  14352. * decide if the user disaled the port. If the TLV indicates the
  14353. * port is disabled, the hba_flag is set accordingly.
  14354. **/
  14355. void
  14356. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14357. {
  14358. uint8_t *rgn23_data = NULL;
  14359. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14360. uint32_t offset = 0;
  14361. /* Get adapter Region 23 data */
  14362. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14363. if (!rgn23_data)
  14364. goto out;
  14365. if (phba->sli_rev < LPFC_SLI_REV4)
  14366. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14367. else {
  14368. if_type = bf_get(lpfc_sli_intf_if_type,
  14369. &phba->sli4_hba.sli_intf);
  14370. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14371. goto out;
  14372. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14373. }
  14374. if (!data_size)
  14375. goto out;
  14376. /* Check the region signature first */
  14377. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14378. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14379. "2619 Config region 23 has bad signature\n");
  14380. goto out;
  14381. }
  14382. offset += 4;
  14383. /* Check the data structure version */
  14384. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14385. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14386. "2620 Config region 23 has bad version\n");
  14387. goto out;
  14388. }
  14389. offset += 4;
  14390. /* Parse TLV entries in the region */
  14391. while (offset < data_size) {
  14392. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14393. break;
  14394. /*
  14395. * If the TLV is not driver specific TLV or driver id is
  14396. * not linux driver id, skip the record.
  14397. */
  14398. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14399. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14400. (rgn23_data[offset + 3] != 0)) {
  14401. offset += rgn23_data[offset + 1] * 4 + 4;
  14402. continue;
  14403. }
  14404. /* Driver found a driver specific TLV in the config region */
  14405. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14406. offset += 4;
  14407. tlv_offset = 0;
  14408. /*
  14409. * Search for configured port state sub-TLV.
  14410. */
  14411. while ((offset < data_size) &&
  14412. (tlv_offset < sub_tlv_len)) {
  14413. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14414. offset += 4;
  14415. tlv_offset += 4;
  14416. break;
  14417. }
  14418. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14419. offset += rgn23_data[offset + 1] * 4 + 4;
  14420. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14421. continue;
  14422. }
  14423. /* This HBA contains PORT_STE configured */
  14424. if (!rgn23_data[offset + 2])
  14425. phba->hba_flag |= LINK_DISABLED;
  14426. goto out;
  14427. }
  14428. }
  14429. out:
  14430. kfree(rgn23_data);
  14431. return;
  14432. }
  14433. /**
  14434. * lpfc_wr_object - write an object to the firmware
  14435. * @phba: HBA structure that indicates port to create a queue on.
  14436. * @dmabuf_list: list of dmabufs to write to the port.
  14437. * @size: the total byte value of the objects to write to the port.
  14438. * @offset: the current offset to be used to start the transfer.
  14439. *
  14440. * This routine will create a wr_object mailbox command to send to the port.
  14441. * the mailbox command will be constructed using the dma buffers described in
  14442. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14443. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14444. * used to indicate the starting offset of the transfer and will also return
  14445. * the offset after the write object mailbox has completed. @size is used to
  14446. * determine the end of the object and whether the eof bit should be set.
  14447. *
  14448. * Return 0 is successful and offset will contain the the new offset to use
  14449. * for the next write.
  14450. * Return negative value for error cases.
  14451. **/
  14452. int
  14453. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14454. uint32_t size, uint32_t *offset)
  14455. {
  14456. struct lpfc_mbx_wr_object *wr_object;
  14457. LPFC_MBOXQ_t *mbox;
  14458. int rc = 0, i = 0;
  14459. uint32_t shdr_status, shdr_add_status;
  14460. uint32_t mbox_tmo;
  14461. union lpfc_sli4_cfg_shdr *shdr;
  14462. struct lpfc_dmabuf *dmabuf;
  14463. uint32_t written = 0;
  14464. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14465. if (!mbox)
  14466. return -ENOMEM;
  14467. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14468. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14469. sizeof(struct lpfc_mbx_wr_object) -
  14470. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14471. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14472. wr_object->u.request.write_offset = *offset;
  14473. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14474. wr_object->u.request.object_name[0] =
  14475. cpu_to_le32(wr_object->u.request.object_name[0]);
  14476. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14477. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14478. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14479. break;
  14480. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14481. wr_object->u.request.bde[i].addrHigh =
  14482. putPaddrHigh(dmabuf->phys);
  14483. if (written + SLI4_PAGE_SIZE >= size) {
  14484. wr_object->u.request.bde[i].tus.f.bdeSize =
  14485. (size - written);
  14486. written += (size - written);
  14487. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14488. } else {
  14489. wr_object->u.request.bde[i].tus.f.bdeSize =
  14490. SLI4_PAGE_SIZE;
  14491. written += SLI4_PAGE_SIZE;
  14492. }
  14493. i++;
  14494. }
  14495. wr_object->u.request.bde_count = i;
  14496. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14497. if (!phba->sli4_hba.intr_enable)
  14498. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14499. else {
  14500. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14501. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14502. }
  14503. /* The IOCTL status is embedded in the mailbox subheader. */
  14504. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14505. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14506. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14507. if (rc != MBX_TIMEOUT)
  14508. mempool_free(mbox, phba->mbox_mem_pool);
  14509. if (shdr_status || shdr_add_status || rc) {
  14510. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14511. "3025 Write Object mailbox failed with "
  14512. "status x%x add_status x%x, mbx status x%x\n",
  14513. shdr_status, shdr_add_status, rc);
  14514. rc = -ENXIO;
  14515. } else
  14516. *offset += wr_object->u.response.actual_write_length;
  14517. return rc;
  14518. }
  14519. /**
  14520. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14521. * @vport: pointer to vport data structure.
  14522. *
  14523. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14524. * and REG_VPI mailbox commands associated with the vport. This function
  14525. * is called when driver want to restart discovery of the vport due to
  14526. * a Clear Virtual Link event.
  14527. **/
  14528. void
  14529. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14530. {
  14531. struct lpfc_hba *phba = vport->phba;
  14532. LPFC_MBOXQ_t *mb, *nextmb;
  14533. struct lpfc_dmabuf *mp;
  14534. struct lpfc_nodelist *ndlp;
  14535. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14536. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14537. LIST_HEAD(mbox_cmd_list);
  14538. uint8_t restart_loop;
  14539. /* Clean up internally queued mailbox commands with the vport */
  14540. spin_lock_irq(&phba->hbalock);
  14541. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14542. if (mb->vport != vport)
  14543. continue;
  14544. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14545. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14546. continue;
  14547. list_del(&mb->list);
  14548. list_add_tail(&mb->list, &mbox_cmd_list);
  14549. }
  14550. /* Clean up active mailbox command with the vport */
  14551. mb = phba->sli.mbox_active;
  14552. if (mb && (mb->vport == vport)) {
  14553. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14554. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14555. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14556. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14557. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14558. /* Put reference count for delayed processing */
  14559. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14560. /* Unregister the RPI when mailbox complete */
  14561. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14562. }
  14563. }
  14564. /* Cleanup any mailbox completions which are not yet processed */
  14565. do {
  14566. restart_loop = 0;
  14567. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14568. /*
  14569. * If this mailox is already processed or it is
  14570. * for another vport ignore it.
  14571. */
  14572. if ((mb->vport != vport) ||
  14573. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14574. continue;
  14575. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14576. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14577. continue;
  14578. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14579. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14580. ndlp = (struct lpfc_nodelist *)mb->context2;
  14581. /* Unregister the RPI when mailbox complete */
  14582. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14583. restart_loop = 1;
  14584. spin_unlock_irq(&phba->hbalock);
  14585. spin_lock(shost->host_lock);
  14586. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14587. spin_unlock(shost->host_lock);
  14588. spin_lock_irq(&phba->hbalock);
  14589. break;
  14590. }
  14591. }
  14592. } while (restart_loop);
  14593. spin_unlock_irq(&phba->hbalock);
  14594. /* Release the cleaned-up mailbox commands */
  14595. while (!list_empty(&mbox_cmd_list)) {
  14596. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14597. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14598. mp = (struct lpfc_dmabuf *) (mb->context1);
  14599. if (mp) {
  14600. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14601. kfree(mp);
  14602. }
  14603. ndlp = (struct lpfc_nodelist *) mb->context2;
  14604. mb->context2 = NULL;
  14605. if (ndlp) {
  14606. spin_lock(shost->host_lock);
  14607. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14608. spin_unlock(shost->host_lock);
  14609. lpfc_nlp_put(ndlp);
  14610. }
  14611. }
  14612. mempool_free(mb, phba->mbox_mem_pool);
  14613. }
  14614. /* Release the ndlp with the cleaned-up active mailbox command */
  14615. if (act_mbx_ndlp) {
  14616. spin_lock(shost->host_lock);
  14617. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14618. spin_unlock(shost->host_lock);
  14619. lpfc_nlp_put(act_mbx_ndlp);
  14620. }
  14621. }
  14622. /**
  14623. * lpfc_drain_txq - Drain the txq
  14624. * @phba: Pointer to HBA context object.
  14625. *
  14626. * This function attempt to submit IOCBs on the txq
  14627. * to the adapter. For SLI4 adapters, the txq contains
  14628. * ELS IOCBs that have been deferred because the there
  14629. * are no SGLs. This congestion can occur with large
  14630. * vport counts during node discovery.
  14631. **/
  14632. uint32_t
  14633. lpfc_drain_txq(struct lpfc_hba *phba)
  14634. {
  14635. LIST_HEAD(completions);
  14636. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14637. struct lpfc_iocbq *piocbq = 0;
  14638. unsigned long iflags = 0;
  14639. char *fail_msg = NULL;
  14640. struct lpfc_sglq *sglq;
  14641. union lpfc_wqe wqe;
  14642. spin_lock_irqsave(&phba->hbalock, iflags);
  14643. if (pring->txq_cnt > pring->txq_max)
  14644. pring->txq_max = pring->txq_cnt;
  14645. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14646. while (pring->txq_cnt) {
  14647. spin_lock_irqsave(&phba->hbalock, iflags);
  14648. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14649. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14650. if (!sglq) {
  14651. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14652. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14653. break;
  14654. } else {
  14655. if (!piocbq) {
  14656. /* The txq_cnt out of sync. This should
  14657. * never happen
  14658. */
  14659. sglq = __lpfc_clear_active_sglq(phba,
  14660. sglq->sli4_lxritag);
  14661. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14662. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14663. "2823 txq empty and txq_cnt is %d\n ",
  14664. pring->txq_cnt);
  14665. break;
  14666. }
  14667. }
  14668. /* The xri and iocb resources secured,
  14669. * attempt to issue request
  14670. */
  14671. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14672. piocbq->sli4_xritag = sglq->sli4_xritag;
  14673. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14674. fail_msg = "to convert bpl to sgl";
  14675. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14676. fail_msg = "to convert iocb to wqe";
  14677. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14678. fail_msg = " - Wq is full";
  14679. else
  14680. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14681. if (fail_msg) {
  14682. /* Failed means we can't issue and need to cancel */
  14683. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14684. "2822 IOCB failed %s iotag 0x%x "
  14685. "xri 0x%x\n",
  14686. fail_msg,
  14687. piocbq->iotag, piocbq->sli4_xritag);
  14688. list_add_tail(&piocbq->list, &completions);
  14689. }
  14690. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14691. }
  14692. /* Cancel all the IOCBs that cannot be issued */
  14693. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14694. IOERR_SLI_ABORTED);
  14695. return pring->txq_cnt;
  14696. }