lpfc_sli.c 492 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static IOCB_t *
  67. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  68. {
  69. return &iocbq->iocb;
  70. }
  71. /**
  72. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  73. * @q: The Work Queue to operate on.
  74. * @wqe: The work Queue Entry to put on the Work queue.
  75. *
  76. * This routine will copy the contents of @wqe to the next available entry on
  77. * the @q. This function will then ring the Work Queue Doorbell to signal the
  78. * HBA to start processing the Work Queue Entry. This function returns 0 if
  79. * successful. If no entries are available on @q then this function will return
  80. * -ENOMEM.
  81. * The caller is expected to hold the hbalock when calling this routine.
  82. **/
  83. static uint32_t
  84. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  85. {
  86. union lpfc_wqe *temp_wqe;
  87. struct lpfc_register doorbell;
  88. uint32_t host_index;
  89. /* sanity check on queue memory */
  90. if (unlikely(!q))
  91. return -ENOMEM;
  92. temp_wqe = q->qe[q->host_index].wqe;
  93. /* If the host has not yet processed the next entry then we are done */
  94. if (((q->host_index + 1) % q->entry_count) == q->hba_index) {
  95. q->WQ_overflow++;
  96. return -ENOMEM;
  97. }
  98. q->WQ_posted++;
  99. /* set consumption flag every once in a while */
  100. if (!((q->host_index + 1) % q->entry_repost))
  101. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  102. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  103. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  104. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  105. /* Update the host index before invoking device */
  106. host_index = q->host_index;
  107. q->host_index = ((q->host_index + 1) % q->entry_count);
  108. /* Ring Doorbell */
  109. doorbell.word0 = 0;
  110. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  111. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  112. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  113. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  114. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  115. return 0;
  116. }
  117. /**
  118. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  119. * @q: The Work Queue to operate on.
  120. * @index: The index to advance the hba index to.
  121. *
  122. * This routine will update the HBA index of a queue to reflect consumption of
  123. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  124. * an entry the host calls this function to update the queue's internal
  125. * pointers. This routine returns the number of entries that were consumed by
  126. * the HBA.
  127. **/
  128. static uint32_t
  129. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  130. {
  131. uint32_t released = 0;
  132. /* sanity check on queue memory */
  133. if (unlikely(!q))
  134. return 0;
  135. if (q->hba_index == index)
  136. return 0;
  137. do {
  138. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  139. released++;
  140. } while (q->hba_index != index);
  141. return released;
  142. }
  143. /**
  144. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  145. * @q: The Mailbox Queue to operate on.
  146. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  147. *
  148. * This routine will copy the contents of @mqe to the next available entry on
  149. * the @q. This function will then ring the Work Queue Doorbell to signal the
  150. * HBA to start processing the Work Queue Entry. This function returns 0 if
  151. * successful. If no entries are available on @q then this function will return
  152. * -ENOMEM.
  153. * The caller is expected to hold the hbalock when calling this routine.
  154. **/
  155. static uint32_t
  156. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  157. {
  158. struct lpfc_mqe *temp_mqe;
  159. struct lpfc_register doorbell;
  160. uint32_t host_index;
  161. /* sanity check on queue memory */
  162. if (unlikely(!q))
  163. return -ENOMEM;
  164. temp_mqe = q->qe[q->host_index].mqe;
  165. /* If the host has not yet processed the next entry then we are done */
  166. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  167. return -ENOMEM;
  168. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  169. /* Save off the mailbox pointer for completion */
  170. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  171. /* Update the host index before invoking device */
  172. host_index = q->host_index;
  173. q->host_index = ((q->host_index + 1) % q->entry_count);
  174. /* Ring Doorbell */
  175. doorbell.word0 = 0;
  176. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  177. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  178. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  179. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  180. return 0;
  181. }
  182. /**
  183. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  184. * @q: The Mailbox Queue to operate on.
  185. *
  186. * This routine will update the HBA index of a queue to reflect consumption of
  187. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  188. * an entry the host calls this function to update the queue's internal
  189. * pointers. This routine returns the number of entries that were consumed by
  190. * the HBA.
  191. **/
  192. static uint32_t
  193. lpfc_sli4_mq_release(struct lpfc_queue *q)
  194. {
  195. /* sanity check on queue memory */
  196. if (unlikely(!q))
  197. return 0;
  198. /* Clear the mailbox pointer for completion */
  199. q->phba->mbox = NULL;
  200. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  201. return 1;
  202. }
  203. /**
  204. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  205. * @q: The Event Queue to get the first valid EQE from
  206. *
  207. * This routine will get the first valid Event Queue Entry from @q, update
  208. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  209. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  210. * processed, but not popped back to the HBA then this routine will return NULL.
  211. **/
  212. static struct lpfc_eqe *
  213. lpfc_sli4_eq_get(struct lpfc_queue *q)
  214. {
  215. struct lpfc_eqe *eqe;
  216. /* sanity check on queue memory */
  217. if (unlikely(!q))
  218. return NULL;
  219. eqe = q->qe[q->hba_index].eqe;
  220. /* If the next EQE is not valid then we are done */
  221. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  222. return NULL;
  223. /* If the host has not yet processed the next entry then we are done */
  224. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  225. return NULL;
  226. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  227. return eqe;
  228. }
  229. /**
  230. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  231. * @q: The Event Queue that the host has completed processing for.
  232. * @arm: Indicates whether the host wants to arms this CQ.
  233. *
  234. * This routine will mark all Event Queue Entries on @q, from the last
  235. * known completed entry to the last entry that was processed, as completed
  236. * by clearing the valid bit for each completion queue entry. Then it will
  237. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  238. * The internal host index in the @q will be updated by this routine to indicate
  239. * that the host has finished processing the entries. The @arm parameter
  240. * indicates that the queue should be rearmed when ringing the doorbell.
  241. *
  242. * This function will return the number of EQEs that were popped.
  243. **/
  244. uint32_t
  245. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  246. {
  247. uint32_t released = 0;
  248. struct lpfc_eqe *temp_eqe;
  249. struct lpfc_register doorbell;
  250. /* sanity check on queue memory */
  251. if (unlikely(!q))
  252. return 0;
  253. /* while there are valid entries */
  254. while (q->hba_index != q->host_index) {
  255. temp_eqe = q->qe[q->host_index].eqe;
  256. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  257. released++;
  258. q->host_index = ((q->host_index + 1) % q->entry_count);
  259. }
  260. if (unlikely(released == 0 && !arm))
  261. return 0;
  262. /* ring doorbell for number popped */
  263. doorbell.word0 = 0;
  264. if (arm) {
  265. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  266. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  267. }
  268. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  269. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  270. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  271. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  272. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  273. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  274. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  275. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  276. readl(q->phba->sli4_hba.EQCQDBregaddr);
  277. return released;
  278. }
  279. /**
  280. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  281. * @q: The Completion Queue to get the first valid CQE from
  282. *
  283. * This routine will get the first valid Completion Queue Entry from @q, update
  284. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  285. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  286. * processed, but not popped back to the HBA then this routine will return NULL.
  287. **/
  288. static struct lpfc_cqe *
  289. lpfc_sli4_cq_get(struct lpfc_queue *q)
  290. {
  291. struct lpfc_cqe *cqe;
  292. /* sanity check on queue memory */
  293. if (unlikely(!q))
  294. return NULL;
  295. /* If the next CQE is not valid then we are done */
  296. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  297. return NULL;
  298. /* If the host has not yet processed the next entry then we are done */
  299. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  300. return NULL;
  301. cqe = q->qe[q->hba_index].cqe;
  302. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  303. return cqe;
  304. }
  305. /**
  306. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  307. * @q: The Completion Queue that the host has completed processing for.
  308. * @arm: Indicates whether the host wants to arms this CQ.
  309. *
  310. * This routine will mark all Completion queue entries on @q, from the last
  311. * known completed entry to the last entry that was processed, as completed
  312. * by clearing the valid bit for each completion queue entry. Then it will
  313. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  314. * The internal host index in the @q will be updated by this routine to indicate
  315. * that the host has finished processing the entries. The @arm parameter
  316. * indicates that the queue should be rearmed when ringing the doorbell.
  317. *
  318. * This function will return the number of CQEs that were released.
  319. **/
  320. uint32_t
  321. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  322. {
  323. uint32_t released = 0;
  324. struct lpfc_cqe *temp_qe;
  325. struct lpfc_register doorbell;
  326. /* sanity check on queue memory */
  327. if (unlikely(!q))
  328. return 0;
  329. /* while there are valid entries */
  330. while (q->hba_index != q->host_index) {
  331. temp_qe = q->qe[q->host_index].cqe;
  332. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  333. released++;
  334. q->host_index = ((q->host_index + 1) % q->entry_count);
  335. }
  336. if (unlikely(released == 0 && !arm))
  337. return 0;
  338. /* ring doorbell for number popped */
  339. doorbell.word0 = 0;
  340. if (arm)
  341. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  342. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  343. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  344. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  345. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  346. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  347. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  348. return released;
  349. }
  350. /**
  351. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  352. * @q: The Header Receive Queue to operate on.
  353. * @wqe: The Receive Queue Entry to put on the Receive queue.
  354. *
  355. * This routine will copy the contents of @wqe to the next available entry on
  356. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  357. * HBA to start processing the Receive Queue Entry. This function returns the
  358. * index that the rqe was copied to if successful. If no entries are available
  359. * on @q then this function will return -ENOMEM.
  360. * The caller is expected to hold the hbalock when calling this routine.
  361. **/
  362. static int
  363. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  364. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  365. {
  366. struct lpfc_rqe *temp_hrqe;
  367. struct lpfc_rqe *temp_drqe;
  368. struct lpfc_register doorbell;
  369. int put_index = hq->host_index;
  370. /* sanity check on queue memory */
  371. if (unlikely(!hq) || unlikely(!dq))
  372. return -ENOMEM;
  373. temp_hrqe = hq->qe[hq->host_index].rqe;
  374. temp_drqe = dq->qe[dq->host_index].rqe;
  375. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  376. return -EINVAL;
  377. if (hq->host_index != dq->host_index)
  378. return -EINVAL;
  379. /* If the host has not yet processed the next entry then we are done */
  380. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  381. return -EBUSY;
  382. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  383. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  384. /* Update the host index to point to the next slot */
  385. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  386. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  387. /* Ring The Header Receive Queue Doorbell */
  388. if (!(hq->host_index % hq->entry_repost)) {
  389. doorbell.word0 = 0;
  390. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  391. hq->entry_repost);
  392. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  393. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  394. }
  395. return put_index;
  396. }
  397. /**
  398. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  399. * @q: The Header Receive Queue to operate on.
  400. *
  401. * This routine will update the HBA index of a queue to reflect consumption of
  402. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  403. * consumed an entry the host calls this function to update the queue's
  404. * internal pointers. This routine returns the number of entries that were
  405. * consumed by the HBA.
  406. **/
  407. static uint32_t
  408. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  409. {
  410. /* sanity check on queue memory */
  411. if (unlikely(!hq) || unlikely(!dq))
  412. return 0;
  413. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  414. return 0;
  415. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  416. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  417. return 1;
  418. }
  419. /**
  420. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  421. * @phba: Pointer to HBA context object.
  422. * @pring: Pointer to driver SLI ring object.
  423. *
  424. * This function returns pointer to next command iocb entry
  425. * in the command ring. The caller must hold hbalock to prevent
  426. * other threads consume the next command iocb.
  427. * SLI-2/SLI-3 provide different sized iocbs.
  428. **/
  429. static inline IOCB_t *
  430. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  431. {
  432. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  433. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  434. }
  435. /**
  436. * lpfc_resp_iocb - Get next response iocb entry in the ring
  437. * @phba: Pointer to HBA context object.
  438. * @pring: Pointer to driver SLI ring object.
  439. *
  440. * This function returns pointer to next response iocb entry
  441. * in the response ring. The caller must hold hbalock to make sure
  442. * that no other thread consume the next response iocb.
  443. * SLI-2/SLI-3 provide different sized iocbs.
  444. **/
  445. static inline IOCB_t *
  446. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  447. {
  448. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  449. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  450. }
  451. /**
  452. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  453. * @phba: Pointer to HBA context object.
  454. *
  455. * This function is called with hbalock held. This function
  456. * allocates a new driver iocb object from the iocb pool. If the
  457. * allocation is successful, it returns pointer to the newly
  458. * allocated iocb object else it returns NULL.
  459. **/
  460. struct lpfc_iocbq *
  461. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  462. {
  463. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  464. struct lpfc_iocbq * iocbq = NULL;
  465. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  466. if (iocbq)
  467. phba->iocb_cnt++;
  468. if (phba->iocb_cnt > phba->iocb_max)
  469. phba->iocb_max = phba->iocb_cnt;
  470. return iocbq;
  471. }
  472. /**
  473. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  474. * @phba: Pointer to HBA context object.
  475. * @xritag: XRI value.
  476. *
  477. * This function clears the sglq pointer from the array of acive
  478. * sglq's. The xritag that is passed in is used to index into the
  479. * array. Before the xritag can be used it needs to be adjusted
  480. * by subtracting the xribase.
  481. *
  482. * Returns sglq ponter = success, NULL = Failure.
  483. **/
  484. static struct lpfc_sglq *
  485. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  486. {
  487. struct lpfc_sglq *sglq;
  488. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  489. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  490. return sglq;
  491. }
  492. /**
  493. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  494. * @phba: Pointer to HBA context object.
  495. * @xritag: XRI value.
  496. *
  497. * This function returns the sglq pointer from the array of acive
  498. * sglq's. The xritag that is passed in is used to index into the
  499. * array. Before the xritag can be used it needs to be adjusted
  500. * by subtracting the xribase.
  501. *
  502. * Returns sglq ponter = success, NULL = Failure.
  503. **/
  504. struct lpfc_sglq *
  505. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  506. {
  507. struct lpfc_sglq *sglq;
  508. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  509. return sglq;
  510. }
  511. /**
  512. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  513. * @phba: Pointer to HBA context object.
  514. * @xritag: xri used in this exchange.
  515. * @rrq: The RRQ to be cleared.
  516. *
  517. **/
  518. void
  519. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  520. uint16_t xritag,
  521. struct lpfc_node_rrq *rrq)
  522. {
  523. struct lpfc_nodelist *ndlp = NULL;
  524. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  525. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  526. /* The target DID could have been swapped (cable swap)
  527. * we should use the ndlp from the findnode if it is
  528. * available.
  529. */
  530. if ((!ndlp) && rrq->ndlp)
  531. ndlp = rrq->ndlp;
  532. if (!ndlp)
  533. goto out;
  534. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  535. rrq->send_rrq = 0;
  536. rrq->xritag = 0;
  537. rrq->rrq_stop_time = 0;
  538. }
  539. out:
  540. mempool_free(rrq, phba->rrq_pool);
  541. }
  542. /**
  543. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  544. * @phba: Pointer to HBA context object.
  545. *
  546. * This function is called with hbalock held. This function
  547. * Checks if stop_time (ratov from setting rrq active) has
  548. * been reached, if it has and the send_rrq flag is set then
  549. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  550. * then it will just call the routine to clear the rrq and
  551. * free the rrq resource.
  552. * The timer is set to the next rrq that is going to expire before
  553. * leaving the routine.
  554. *
  555. **/
  556. void
  557. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  558. {
  559. struct lpfc_node_rrq *rrq;
  560. struct lpfc_node_rrq *nextrrq;
  561. unsigned long next_time;
  562. unsigned long iflags;
  563. LIST_HEAD(send_rrq);
  564. spin_lock_irqsave(&phba->hbalock, iflags);
  565. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  566. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  567. list_for_each_entry_safe(rrq, nextrrq,
  568. &phba->active_rrq_list, list) {
  569. if (time_after(jiffies, rrq->rrq_stop_time))
  570. list_move(&rrq->list, &send_rrq);
  571. else if (time_before(rrq->rrq_stop_time, next_time))
  572. next_time = rrq->rrq_stop_time;
  573. }
  574. spin_unlock_irqrestore(&phba->hbalock, iflags);
  575. if (!list_empty(&phba->active_rrq_list))
  576. mod_timer(&phba->rrq_tmr, next_time);
  577. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  578. list_del(&rrq->list);
  579. if (!rrq->send_rrq)
  580. /* this call will free the rrq */
  581. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  582. else if (lpfc_send_rrq(phba, rrq)) {
  583. /* if we send the rrq then the completion handler
  584. * will clear the bit in the xribitmap.
  585. */
  586. lpfc_clr_rrq_active(phba, rrq->xritag,
  587. rrq);
  588. }
  589. }
  590. }
  591. /**
  592. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  593. * @vport: Pointer to vport context object.
  594. * @xri: The xri used in the exchange.
  595. * @did: The targets DID for this exchange.
  596. *
  597. * returns NULL = rrq not found in the phba->active_rrq_list.
  598. * rrq = rrq for this xri and target.
  599. **/
  600. struct lpfc_node_rrq *
  601. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  602. {
  603. struct lpfc_hba *phba = vport->phba;
  604. struct lpfc_node_rrq *rrq;
  605. struct lpfc_node_rrq *nextrrq;
  606. unsigned long iflags;
  607. if (phba->sli_rev != LPFC_SLI_REV4)
  608. return NULL;
  609. spin_lock_irqsave(&phba->hbalock, iflags);
  610. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  611. if (rrq->vport == vport && rrq->xritag == xri &&
  612. rrq->nlp_DID == did){
  613. list_del(&rrq->list);
  614. spin_unlock_irqrestore(&phba->hbalock, iflags);
  615. return rrq;
  616. }
  617. }
  618. spin_unlock_irqrestore(&phba->hbalock, iflags);
  619. return NULL;
  620. }
  621. /**
  622. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  623. * @vport: Pointer to vport context object.
  624. * @ndlp: Pointer to the lpfc_node_list structure.
  625. * If ndlp is NULL Remove all active RRQs for this vport from the
  626. * phba->active_rrq_list and clear the rrq.
  627. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  628. **/
  629. void
  630. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  631. {
  632. struct lpfc_hba *phba = vport->phba;
  633. struct lpfc_node_rrq *rrq;
  634. struct lpfc_node_rrq *nextrrq;
  635. unsigned long iflags;
  636. LIST_HEAD(rrq_list);
  637. if (phba->sli_rev != LPFC_SLI_REV4)
  638. return;
  639. if (!ndlp) {
  640. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  641. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  642. }
  643. spin_lock_irqsave(&phba->hbalock, iflags);
  644. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  645. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  646. list_move(&rrq->list, &rrq_list);
  647. spin_unlock_irqrestore(&phba->hbalock, iflags);
  648. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  649. list_del(&rrq->list);
  650. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  651. }
  652. }
  653. /**
  654. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  655. * @phba: Pointer to HBA context object.
  656. *
  657. * Remove all rrqs from the phba->active_rrq_list and free them by
  658. * calling __lpfc_clr_active_rrq
  659. *
  660. **/
  661. void
  662. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  663. {
  664. struct lpfc_node_rrq *rrq;
  665. struct lpfc_node_rrq *nextrrq;
  666. unsigned long next_time;
  667. unsigned long iflags;
  668. LIST_HEAD(rrq_list);
  669. if (phba->sli_rev != LPFC_SLI_REV4)
  670. return;
  671. spin_lock_irqsave(&phba->hbalock, iflags);
  672. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  673. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  674. list_splice_init(&phba->active_rrq_list, &rrq_list);
  675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  676. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  677. list_del(&rrq->list);
  678. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  679. }
  680. if (!list_empty(&phba->active_rrq_list))
  681. mod_timer(&phba->rrq_tmr, next_time);
  682. }
  683. /**
  684. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  685. * @phba: Pointer to HBA context object.
  686. * @ndlp: Targets nodelist pointer for this exchange.
  687. * @xritag the xri in the bitmap to test.
  688. *
  689. * This function is called with hbalock held. This function
  690. * returns 0 = rrq not active for this xri
  691. * 1 = rrq is valid for this xri.
  692. **/
  693. int
  694. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  695. uint16_t xritag)
  696. {
  697. if (!ndlp)
  698. return 0;
  699. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  700. return 1;
  701. else
  702. return 0;
  703. }
  704. /**
  705. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  706. * @phba: Pointer to HBA context object.
  707. * @ndlp: nodelist pointer for this target.
  708. * @xritag: xri used in this exchange.
  709. * @rxid: Remote Exchange ID.
  710. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  711. *
  712. * This function takes the hbalock.
  713. * The active bit is always set in the active rrq xri_bitmap even
  714. * if there is no slot avaiable for the other rrq information.
  715. *
  716. * returns 0 rrq actived for this xri
  717. * < 0 No memory or invalid ndlp.
  718. **/
  719. int
  720. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  721. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  722. {
  723. unsigned long iflags;
  724. struct lpfc_node_rrq *rrq;
  725. int empty;
  726. if (!ndlp)
  727. return -EINVAL;
  728. if (!phba->cfg_enable_rrq)
  729. return -EINVAL;
  730. spin_lock_irqsave(&phba->hbalock, iflags);
  731. if (phba->pport->load_flag & FC_UNLOADING) {
  732. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  733. goto out;
  734. }
  735. /*
  736. * set the active bit even if there is no mem available.
  737. */
  738. if (NLP_CHK_FREE_REQ(ndlp))
  739. goto out;
  740. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  741. goto out;
  742. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  743. goto out;
  744. spin_unlock_irqrestore(&phba->hbalock, iflags);
  745. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  746. if (!rrq) {
  747. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  748. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  749. " DID:0x%x Send:%d\n",
  750. xritag, rxid, ndlp->nlp_DID, send_rrq);
  751. return -EINVAL;
  752. }
  753. rrq->send_rrq = send_rrq;
  754. rrq->xritag = xritag;
  755. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  756. rrq->ndlp = ndlp;
  757. rrq->nlp_DID = ndlp->nlp_DID;
  758. rrq->vport = ndlp->vport;
  759. rrq->rxid = rxid;
  760. rrq->send_rrq = send_rrq;
  761. spin_lock_irqsave(&phba->hbalock, iflags);
  762. empty = list_empty(&phba->active_rrq_list);
  763. list_add_tail(&rrq->list, &phba->active_rrq_list);
  764. phba->hba_flag |= HBA_RRQ_ACTIVE;
  765. if (empty)
  766. lpfc_worker_wake_up(phba);
  767. spin_unlock_irqrestore(&phba->hbalock, iflags);
  768. return 0;
  769. out:
  770. spin_unlock_irqrestore(&phba->hbalock, iflags);
  771. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  772. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  773. " DID:0x%x Send:%d\n",
  774. xritag, rxid, ndlp->nlp_DID, send_rrq);
  775. return -EINVAL;
  776. }
  777. /**
  778. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  779. * @phba: Pointer to HBA context object.
  780. * @piocb: Pointer to the iocbq.
  781. *
  782. * This function is called with hbalock held. This function
  783. * gets a new driver sglq object from the sglq list. If the
  784. * list is not empty then it is successful, it returns pointer to the newly
  785. * allocated sglq object else it returns NULL.
  786. **/
  787. static struct lpfc_sglq *
  788. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  789. {
  790. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  791. struct lpfc_sglq *sglq = NULL;
  792. struct lpfc_sglq *start_sglq = NULL;
  793. struct lpfc_scsi_buf *lpfc_cmd;
  794. struct lpfc_nodelist *ndlp;
  795. int found = 0;
  796. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  797. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  798. ndlp = lpfc_cmd->rdata->pnode;
  799. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  800. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  801. ndlp = piocbq->context_un.ndlp;
  802. else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
  803. (piocbq->iocb_flag & LPFC_IO_LIBDFC))
  804. ndlp = piocbq->context_un.ndlp;
  805. else
  806. ndlp = piocbq->context1;
  807. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  808. start_sglq = sglq;
  809. while (!found) {
  810. if (!sglq)
  811. return NULL;
  812. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  813. /* This xri has an rrq outstanding for this DID.
  814. * put it back in the list and get another xri.
  815. */
  816. list_add_tail(&sglq->list, lpfc_sgl_list);
  817. sglq = NULL;
  818. list_remove_head(lpfc_sgl_list, sglq,
  819. struct lpfc_sglq, list);
  820. if (sglq == start_sglq) {
  821. sglq = NULL;
  822. break;
  823. } else
  824. continue;
  825. }
  826. sglq->ndlp = ndlp;
  827. found = 1;
  828. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  829. sglq->state = SGL_ALLOCATED;
  830. }
  831. return sglq;
  832. }
  833. /**
  834. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  835. * @phba: Pointer to HBA context object.
  836. *
  837. * This function is called with no lock held. This function
  838. * allocates a new driver iocb object from the iocb pool. If the
  839. * allocation is successful, it returns pointer to the newly
  840. * allocated iocb object else it returns NULL.
  841. **/
  842. struct lpfc_iocbq *
  843. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  844. {
  845. struct lpfc_iocbq * iocbq = NULL;
  846. unsigned long iflags;
  847. spin_lock_irqsave(&phba->hbalock, iflags);
  848. iocbq = __lpfc_sli_get_iocbq(phba);
  849. spin_unlock_irqrestore(&phba->hbalock, iflags);
  850. return iocbq;
  851. }
  852. /**
  853. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  854. * @phba: Pointer to HBA context object.
  855. * @iocbq: Pointer to driver iocb object.
  856. *
  857. * This function is called with hbalock held to release driver
  858. * iocb object to the iocb pool. The iotag in the iocb object
  859. * does not change for each use of the iocb object. This function
  860. * clears all other fields of the iocb object when it is freed.
  861. * The sqlq structure that holds the xritag and phys and virtual
  862. * mappings for the scatter gather list is retrieved from the
  863. * active array of sglq. The get of the sglq pointer also clears
  864. * the entry in the array. If the status of the IO indiactes that
  865. * this IO was aborted then the sglq entry it put on the
  866. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  867. * IO has good status or fails for any other reason then the sglq
  868. * entry is added to the free list (lpfc_sgl_list).
  869. **/
  870. static void
  871. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  872. {
  873. struct lpfc_sglq *sglq;
  874. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  875. unsigned long iflag = 0;
  876. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  877. if (iocbq->sli4_xritag == NO_XRI)
  878. sglq = NULL;
  879. else
  880. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  881. if (sglq) {
  882. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  883. (sglq->state != SGL_XRI_ABORTED)) {
  884. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  885. iflag);
  886. list_add(&sglq->list,
  887. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  888. spin_unlock_irqrestore(
  889. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  890. } else {
  891. sglq->state = SGL_FREED;
  892. sglq->ndlp = NULL;
  893. list_add_tail(&sglq->list,
  894. &phba->sli4_hba.lpfc_sgl_list);
  895. /* Check if TXQ queue needs to be serviced */
  896. if (pring->txq_cnt)
  897. lpfc_worker_wake_up(phba);
  898. }
  899. }
  900. /*
  901. * Clean all volatile data fields, preserve iotag and node struct.
  902. */
  903. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  904. iocbq->sli4_lxritag = NO_XRI;
  905. iocbq->sli4_xritag = NO_XRI;
  906. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  907. }
  908. /**
  909. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  910. * @phba: Pointer to HBA context object.
  911. * @iocbq: Pointer to driver iocb object.
  912. *
  913. * This function is called with hbalock held to release driver
  914. * iocb object to the iocb pool. The iotag in the iocb object
  915. * does not change for each use of the iocb object. This function
  916. * clears all other fields of the iocb object when it is freed.
  917. **/
  918. static void
  919. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  920. {
  921. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  922. /*
  923. * Clean all volatile data fields, preserve iotag and node struct.
  924. */
  925. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  926. iocbq->sli4_xritag = NO_XRI;
  927. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  928. }
  929. /**
  930. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  931. * @phba: Pointer to HBA context object.
  932. * @iocbq: Pointer to driver iocb object.
  933. *
  934. * This function is called with hbalock held to release driver
  935. * iocb object to the iocb pool. The iotag in the iocb object
  936. * does not change for each use of the iocb object. This function
  937. * clears all other fields of the iocb object when it is freed.
  938. **/
  939. static void
  940. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  941. {
  942. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  943. phba->iocb_cnt--;
  944. }
  945. /**
  946. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  947. * @phba: Pointer to HBA context object.
  948. * @iocbq: Pointer to driver iocb object.
  949. *
  950. * This function is called with no lock held to release the iocb to
  951. * iocb pool.
  952. **/
  953. void
  954. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  955. {
  956. unsigned long iflags;
  957. /*
  958. * Clean all volatile data fields, preserve iotag and node struct.
  959. */
  960. spin_lock_irqsave(&phba->hbalock, iflags);
  961. __lpfc_sli_release_iocbq(phba, iocbq);
  962. spin_unlock_irqrestore(&phba->hbalock, iflags);
  963. }
  964. /**
  965. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  966. * @phba: Pointer to HBA context object.
  967. * @iocblist: List of IOCBs.
  968. * @ulpstatus: ULP status in IOCB command field.
  969. * @ulpWord4: ULP word-4 in IOCB command field.
  970. *
  971. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  972. * on the list by invoking the complete callback function associated with the
  973. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  974. * fields.
  975. **/
  976. void
  977. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  978. uint32_t ulpstatus, uint32_t ulpWord4)
  979. {
  980. struct lpfc_iocbq *piocb;
  981. while (!list_empty(iocblist)) {
  982. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  983. if (!piocb->iocb_cmpl)
  984. lpfc_sli_release_iocbq(phba, piocb);
  985. else {
  986. piocb->iocb.ulpStatus = ulpstatus;
  987. piocb->iocb.un.ulpWord[4] = ulpWord4;
  988. (piocb->iocb_cmpl) (phba, piocb, piocb);
  989. }
  990. }
  991. return;
  992. }
  993. /**
  994. * lpfc_sli_iocb_cmd_type - Get the iocb type
  995. * @iocb_cmnd: iocb command code.
  996. *
  997. * This function is called by ring event handler function to get the iocb type.
  998. * This function translates the iocb command to an iocb command type used to
  999. * decide the final disposition of each completed IOCB.
  1000. * The function returns
  1001. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1002. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1003. * LPFC_ABORT_IOCB if it is an abort iocb
  1004. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1005. *
  1006. * The caller is not required to hold any lock.
  1007. **/
  1008. static lpfc_iocb_type
  1009. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1010. {
  1011. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1012. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1013. return 0;
  1014. switch (iocb_cmnd) {
  1015. case CMD_XMIT_SEQUENCE_CR:
  1016. case CMD_XMIT_SEQUENCE_CX:
  1017. case CMD_XMIT_BCAST_CN:
  1018. case CMD_XMIT_BCAST_CX:
  1019. case CMD_ELS_REQUEST_CR:
  1020. case CMD_ELS_REQUEST_CX:
  1021. case CMD_CREATE_XRI_CR:
  1022. case CMD_CREATE_XRI_CX:
  1023. case CMD_GET_RPI_CN:
  1024. case CMD_XMIT_ELS_RSP_CX:
  1025. case CMD_GET_RPI_CR:
  1026. case CMD_FCP_IWRITE_CR:
  1027. case CMD_FCP_IWRITE_CX:
  1028. case CMD_FCP_IREAD_CR:
  1029. case CMD_FCP_IREAD_CX:
  1030. case CMD_FCP_ICMND_CR:
  1031. case CMD_FCP_ICMND_CX:
  1032. case CMD_FCP_TSEND_CX:
  1033. case CMD_FCP_TRSP_CX:
  1034. case CMD_FCP_TRECEIVE_CX:
  1035. case CMD_FCP_AUTO_TRSP_CX:
  1036. case CMD_ADAPTER_MSG:
  1037. case CMD_ADAPTER_DUMP:
  1038. case CMD_XMIT_SEQUENCE64_CR:
  1039. case CMD_XMIT_SEQUENCE64_CX:
  1040. case CMD_XMIT_BCAST64_CN:
  1041. case CMD_XMIT_BCAST64_CX:
  1042. case CMD_ELS_REQUEST64_CR:
  1043. case CMD_ELS_REQUEST64_CX:
  1044. case CMD_FCP_IWRITE64_CR:
  1045. case CMD_FCP_IWRITE64_CX:
  1046. case CMD_FCP_IREAD64_CR:
  1047. case CMD_FCP_IREAD64_CX:
  1048. case CMD_FCP_ICMND64_CR:
  1049. case CMD_FCP_ICMND64_CX:
  1050. case CMD_FCP_TSEND64_CX:
  1051. case CMD_FCP_TRSP64_CX:
  1052. case CMD_FCP_TRECEIVE64_CX:
  1053. case CMD_GEN_REQUEST64_CR:
  1054. case CMD_GEN_REQUEST64_CX:
  1055. case CMD_XMIT_ELS_RSP64_CX:
  1056. case DSSCMD_IWRITE64_CR:
  1057. case DSSCMD_IWRITE64_CX:
  1058. case DSSCMD_IREAD64_CR:
  1059. case DSSCMD_IREAD64_CX:
  1060. type = LPFC_SOL_IOCB;
  1061. break;
  1062. case CMD_ABORT_XRI_CN:
  1063. case CMD_ABORT_XRI_CX:
  1064. case CMD_CLOSE_XRI_CN:
  1065. case CMD_CLOSE_XRI_CX:
  1066. case CMD_XRI_ABORTED_CX:
  1067. case CMD_ABORT_MXRI64_CN:
  1068. case CMD_XMIT_BLS_RSP64_CX:
  1069. type = LPFC_ABORT_IOCB;
  1070. break;
  1071. case CMD_RCV_SEQUENCE_CX:
  1072. case CMD_RCV_ELS_REQ_CX:
  1073. case CMD_RCV_SEQUENCE64_CX:
  1074. case CMD_RCV_ELS_REQ64_CX:
  1075. case CMD_ASYNC_STATUS:
  1076. case CMD_IOCB_RCV_SEQ64_CX:
  1077. case CMD_IOCB_RCV_ELS64_CX:
  1078. case CMD_IOCB_RCV_CONT64_CX:
  1079. case CMD_IOCB_RET_XRI64_CX:
  1080. type = LPFC_UNSOL_IOCB;
  1081. break;
  1082. case CMD_IOCB_XMIT_MSEQ64_CR:
  1083. case CMD_IOCB_XMIT_MSEQ64_CX:
  1084. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1085. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1086. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1087. case CMD_IOCB_ABORT_EXTENDED_CN:
  1088. case CMD_IOCB_RET_HBQE64_CN:
  1089. case CMD_IOCB_FCP_IBIDIR64_CR:
  1090. case CMD_IOCB_FCP_IBIDIR64_CX:
  1091. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1092. case CMD_IOCB_LOGENTRY_CN:
  1093. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1094. printk("%s - Unhandled SLI-3 Command x%x\n",
  1095. __func__, iocb_cmnd);
  1096. type = LPFC_UNKNOWN_IOCB;
  1097. break;
  1098. default:
  1099. type = LPFC_UNKNOWN_IOCB;
  1100. break;
  1101. }
  1102. return type;
  1103. }
  1104. /**
  1105. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1106. * @phba: Pointer to HBA context object.
  1107. *
  1108. * This function is called from SLI initialization code
  1109. * to configure every ring of the HBA's SLI interface. The
  1110. * caller is not required to hold any lock. This function issues
  1111. * a config_ring mailbox command for each ring.
  1112. * This function returns zero if successful else returns a negative
  1113. * error code.
  1114. **/
  1115. static int
  1116. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1117. {
  1118. struct lpfc_sli *psli = &phba->sli;
  1119. LPFC_MBOXQ_t *pmb;
  1120. MAILBOX_t *pmbox;
  1121. int i, rc, ret = 0;
  1122. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1123. if (!pmb)
  1124. return -ENOMEM;
  1125. pmbox = &pmb->u.mb;
  1126. phba->link_state = LPFC_INIT_MBX_CMDS;
  1127. for (i = 0; i < psli->num_rings; i++) {
  1128. lpfc_config_ring(phba, i, pmb);
  1129. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1130. if (rc != MBX_SUCCESS) {
  1131. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1132. "0446 Adapter failed to init (%d), "
  1133. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1134. "ring %d\n",
  1135. rc, pmbox->mbxCommand,
  1136. pmbox->mbxStatus, i);
  1137. phba->link_state = LPFC_HBA_ERROR;
  1138. ret = -ENXIO;
  1139. break;
  1140. }
  1141. }
  1142. mempool_free(pmb, phba->mbox_mem_pool);
  1143. return ret;
  1144. }
  1145. /**
  1146. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1147. * @phba: Pointer to HBA context object.
  1148. * @pring: Pointer to driver SLI ring object.
  1149. * @piocb: Pointer to the driver iocb object.
  1150. *
  1151. * This function is called with hbalock held. The function adds the
  1152. * new iocb to txcmplq of the given ring. This function always returns
  1153. * 0. If this function is called for ELS ring, this function checks if
  1154. * there is a vport associated with the ELS command. This function also
  1155. * starts els_tmofunc timer if this is an ELS command.
  1156. **/
  1157. static int
  1158. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1159. struct lpfc_iocbq *piocb)
  1160. {
  1161. list_add_tail(&piocb->list, &pring->txcmplq);
  1162. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1163. pring->txcmplq_cnt++;
  1164. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1165. pring->txcmplq_max = pring->txcmplq_cnt;
  1166. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1167. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1168. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1169. if (!piocb->vport)
  1170. BUG();
  1171. else
  1172. mod_timer(&piocb->vport->els_tmofunc,
  1173. jiffies + HZ * (phba->fc_ratov << 1));
  1174. }
  1175. return 0;
  1176. }
  1177. /**
  1178. * lpfc_sli_ringtx_get - Get first element of the txq
  1179. * @phba: Pointer to HBA context object.
  1180. * @pring: Pointer to driver SLI ring object.
  1181. *
  1182. * This function is called with hbalock held to get next
  1183. * iocb in txq of the given ring. If there is any iocb in
  1184. * the txq, the function returns first iocb in the list after
  1185. * removing the iocb from the list, else it returns NULL.
  1186. **/
  1187. struct lpfc_iocbq *
  1188. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1189. {
  1190. struct lpfc_iocbq *cmd_iocb;
  1191. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1192. if (cmd_iocb != NULL)
  1193. pring->txq_cnt--;
  1194. return cmd_iocb;
  1195. }
  1196. /**
  1197. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1198. * @phba: Pointer to HBA context object.
  1199. * @pring: Pointer to driver SLI ring object.
  1200. *
  1201. * This function is called with hbalock held and the caller must post the
  1202. * iocb without releasing the lock. If the caller releases the lock,
  1203. * iocb slot returned by the function is not guaranteed to be available.
  1204. * The function returns pointer to the next available iocb slot if there
  1205. * is available slot in the ring, else it returns NULL.
  1206. * If the get index of the ring is ahead of the put index, the function
  1207. * will post an error attention event to the worker thread to take the
  1208. * HBA to offline state.
  1209. **/
  1210. static IOCB_t *
  1211. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1212. {
  1213. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1214. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1215. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1216. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1217. pring->sli.sli3.next_cmdidx = 0;
  1218. if (unlikely(pring->sli.sli3.local_getidx ==
  1219. pring->sli.sli3.next_cmdidx)) {
  1220. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1221. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1222. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1223. "0315 Ring %d issue: portCmdGet %d "
  1224. "is bigger than cmd ring %d\n",
  1225. pring->ringno,
  1226. pring->sli.sli3.local_getidx,
  1227. max_cmd_idx);
  1228. phba->link_state = LPFC_HBA_ERROR;
  1229. /*
  1230. * All error attention handlers are posted to
  1231. * worker thread
  1232. */
  1233. phba->work_ha |= HA_ERATT;
  1234. phba->work_hs = HS_FFER3;
  1235. lpfc_worker_wake_up(phba);
  1236. return NULL;
  1237. }
  1238. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1239. return NULL;
  1240. }
  1241. return lpfc_cmd_iocb(phba, pring);
  1242. }
  1243. /**
  1244. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1245. * @phba: Pointer to HBA context object.
  1246. * @iocbq: Pointer to driver iocb object.
  1247. *
  1248. * This function gets an iotag for the iocb. If there is no unused iotag and
  1249. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1250. * array and assigns a new iotag.
  1251. * The function returns the allocated iotag if successful, else returns zero.
  1252. * Zero is not a valid iotag.
  1253. * The caller is not required to hold any lock.
  1254. **/
  1255. uint16_t
  1256. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1257. {
  1258. struct lpfc_iocbq **new_arr;
  1259. struct lpfc_iocbq **old_arr;
  1260. size_t new_len;
  1261. struct lpfc_sli *psli = &phba->sli;
  1262. uint16_t iotag;
  1263. spin_lock_irq(&phba->hbalock);
  1264. iotag = psli->last_iotag;
  1265. if(++iotag < psli->iocbq_lookup_len) {
  1266. psli->last_iotag = iotag;
  1267. psli->iocbq_lookup[iotag] = iocbq;
  1268. spin_unlock_irq(&phba->hbalock);
  1269. iocbq->iotag = iotag;
  1270. return iotag;
  1271. } else if (psli->iocbq_lookup_len < (0xffff
  1272. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1273. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1274. spin_unlock_irq(&phba->hbalock);
  1275. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1276. GFP_KERNEL);
  1277. if (new_arr) {
  1278. spin_lock_irq(&phba->hbalock);
  1279. old_arr = psli->iocbq_lookup;
  1280. if (new_len <= psli->iocbq_lookup_len) {
  1281. /* highly unprobable case */
  1282. kfree(new_arr);
  1283. iotag = psli->last_iotag;
  1284. if(++iotag < psli->iocbq_lookup_len) {
  1285. psli->last_iotag = iotag;
  1286. psli->iocbq_lookup[iotag] = iocbq;
  1287. spin_unlock_irq(&phba->hbalock);
  1288. iocbq->iotag = iotag;
  1289. return iotag;
  1290. }
  1291. spin_unlock_irq(&phba->hbalock);
  1292. return 0;
  1293. }
  1294. if (psli->iocbq_lookup)
  1295. memcpy(new_arr, old_arr,
  1296. ((psli->last_iotag + 1) *
  1297. sizeof (struct lpfc_iocbq *)));
  1298. psli->iocbq_lookup = new_arr;
  1299. psli->iocbq_lookup_len = new_len;
  1300. psli->last_iotag = iotag;
  1301. psli->iocbq_lookup[iotag] = iocbq;
  1302. spin_unlock_irq(&phba->hbalock);
  1303. iocbq->iotag = iotag;
  1304. kfree(old_arr);
  1305. return iotag;
  1306. }
  1307. } else
  1308. spin_unlock_irq(&phba->hbalock);
  1309. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1310. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1311. psli->last_iotag);
  1312. return 0;
  1313. }
  1314. /**
  1315. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1316. * @phba: Pointer to HBA context object.
  1317. * @pring: Pointer to driver SLI ring object.
  1318. * @iocb: Pointer to iocb slot in the ring.
  1319. * @nextiocb: Pointer to driver iocb object which need to be
  1320. * posted to firmware.
  1321. *
  1322. * This function is called with hbalock held to post a new iocb to
  1323. * the firmware. This function copies the new iocb to ring iocb slot and
  1324. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1325. * a completion call back for this iocb else the function will free the
  1326. * iocb object.
  1327. **/
  1328. static void
  1329. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1330. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1331. {
  1332. /*
  1333. * Set up an iotag
  1334. */
  1335. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1336. if (pring->ringno == LPFC_ELS_RING) {
  1337. lpfc_debugfs_slow_ring_trc(phba,
  1338. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1339. *(((uint32_t *) &nextiocb->iocb) + 4),
  1340. *(((uint32_t *) &nextiocb->iocb) + 6),
  1341. *(((uint32_t *) &nextiocb->iocb) + 7));
  1342. }
  1343. /*
  1344. * Issue iocb command to adapter
  1345. */
  1346. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1347. wmb();
  1348. pring->stats.iocb_cmd++;
  1349. /*
  1350. * If there is no completion routine to call, we can release the
  1351. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1352. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1353. */
  1354. if (nextiocb->iocb_cmpl)
  1355. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1356. else
  1357. __lpfc_sli_release_iocbq(phba, nextiocb);
  1358. /*
  1359. * Let the HBA know what IOCB slot will be the next one the
  1360. * driver will put a command into.
  1361. */
  1362. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1363. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1364. }
  1365. /**
  1366. * lpfc_sli_update_full_ring - Update the chip attention register
  1367. * @phba: Pointer to HBA context object.
  1368. * @pring: Pointer to driver SLI ring object.
  1369. *
  1370. * The caller is not required to hold any lock for calling this function.
  1371. * This function updates the chip attention bits for the ring to inform firmware
  1372. * that there are pending work to be done for this ring and requests an
  1373. * interrupt when there is space available in the ring. This function is
  1374. * called when the driver is unable to post more iocbs to the ring due
  1375. * to unavailability of space in the ring.
  1376. **/
  1377. static void
  1378. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1379. {
  1380. int ringno = pring->ringno;
  1381. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1382. wmb();
  1383. /*
  1384. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1385. * The HBA will tell us when an IOCB entry is available.
  1386. */
  1387. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1388. readl(phba->CAregaddr); /* flush */
  1389. pring->stats.iocb_cmd_full++;
  1390. }
  1391. /**
  1392. * lpfc_sli_update_ring - Update chip attention register
  1393. * @phba: Pointer to HBA context object.
  1394. * @pring: Pointer to driver SLI ring object.
  1395. *
  1396. * This function updates the chip attention register bit for the
  1397. * given ring to inform HBA that there is more work to be done
  1398. * in this ring. The caller is not required to hold any lock.
  1399. **/
  1400. static void
  1401. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1402. {
  1403. int ringno = pring->ringno;
  1404. /*
  1405. * Tell the HBA that there is work to do in this ring.
  1406. */
  1407. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1408. wmb();
  1409. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1410. readl(phba->CAregaddr); /* flush */
  1411. }
  1412. }
  1413. /**
  1414. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1415. * @phba: Pointer to HBA context object.
  1416. * @pring: Pointer to driver SLI ring object.
  1417. *
  1418. * This function is called with hbalock held to post pending iocbs
  1419. * in the txq to the firmware. This function is called when driver
  1420. * detects space available in the ring.
  1421. **/
  1422. static void
  1423. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1424. {
  1425. IOCB_t *iocb;
  1426. struct lpfc_iocbq *nextiocb;
  1427. /*
  1428. * Check to see if:
  1429. * (a) there is anything on the txq to send
  1430. * (b) link is up
  1431. * (c) link attention events can be processed (fcp ring only)
  1432. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1433. */
  1434. if (pring->txq_cnt &&
  1435. lpfc_is_link_up(phba) &&
  1436. (pring->ringno != phba->sli.fcp_ring ||
  1437. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1438. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1439. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1440. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1441. if (iocb)
  1442. lpfc_sli_update_ring(phba, pring);
  1443. else
  1444. lpfc_sli_update_full_ring(phba, pring);
  1445. }
  1446. return;
  1447. }
  1448. /**
  1449. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1450. * @phba: Pointer to HBA context object.
  1451. * @hbqno: HBQ number.
  1452. *
  1453. * This function is called with hbalock held to get the next
  1454. * available slot for the given HBQ. If there is free slot
  1455. * available for the HBQ it will return pointer to the next available
  1456. * HBQ entry else it will return NULL.
  1457. **/
  1458. static struct lpfc_hbq_entry *
  1459. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1460. {
  1461. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1462. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1463. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1464. hbqp->next_hbqPutIdx = 0;
  1465. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1466. uint32_t raw_index = phba->hbq_get[hbqno];
  1467. uint32_t getidx = le32_to_cpu(raw_index);
  1468. hbqp->local_hbqGetIdx = getidx;
  1469. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1470. lpfc_printf_log(phba, KERN_ERR,
  1471. LOG_SLI | LOG_VPORT,
  1472. "1802 HBQ %d: local_hbqGetIdx "
  1473. "%u is > than hbqp->entry_count %u\n",
  1474. hbqno, hbqp->local_hbqGetIdx,
  1475. hbqp->entry_count);
  1476. phba->link_state = LPFC_HBA_ERROR;
  1477. return NULL;
  1478. }
  1479. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1480. return NULL;
  1481. }
  1482. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1483. hbqp->hbqPutIdx;
  1484. }
  1485. /**
  1486. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1487. * @phba: Pointer to HBA context object.
  1488. *
  1489. * This function is called with no lock held to free all the
  1490. * hbq buffers while uninitializing the SLI interface. It also
  1491. * frees the HBQ buffers returned by the firmware but not yet
  1492. * processed by the upper layers.
  1493. **/
  1494. void
  1495. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1496. {
  1497. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1498. struct hbq_dmabuf *hbq_buf;
  1499. unsigned long flags;
  1500. int i, hbq_count;
  1501. uint32_t hbqno;
  1502. hbq_count = lpfc_sli_hbq_count();
  1503. /* Return all memory used by all HBQs */
  1504. spin_lock_irqsave(&phba->hbalock, flags);
  1505. for (i = 0; i < hbq_count; ++i) {
  1506. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1507. &phba->hbqs[i].hbq_buffer_list, list) {
  1508. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1509. list_del(&hbq_buf->dbuf.list);
  1510. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1511. }
  1512. phba->hbqs[i].buffer_count = 0;
  1513. }
  1514. /* Return all HBQ buffer that are in-fly */
  1515. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1516. list) {
  1517. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1518. list_del(&hbq_buf->dbuf.list);
  1519. if (hbq_buf->tag == -1) {
  1520. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1521. (phba, hbq_buf);
  1522. } else {
  1523. hbqno = hbq_buf->tag >> 16;
  1524. if (hbqno >= LPFC_MAX_HBQS)
  1525. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1526. (phba, hbq_buf);
  1527. else
  1528. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1529. hbq_buf);
  1530. }
  1531. }
  1532. /* Mark the HBQs not in use */
  1533. phba->hbq_in_use = 0;
  1534. spin_unlock_irqrestore(&phba->hbalock, flags);
  1535. }
  1536. /**
  1537. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1538. * @phba: Pointer to HBA context object.
  1539. * @hbqno: HBQ number.
  1540. * @hbq_buf: Pointer to HBQ buffer.
  1541. *
  1542. * This function is called with the hbalock held to post a
  1543. * hbq buffer to the firmware. If the function finds an empty
  1544. * slot in the HBQ, it will post the buffer. The function will return
  1545. * pointer to the hbq entry if it successfully post the buffer
  1546. * else it will return NULL.
  1547. **/
  1548. static int
  1549. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1550. struct hbq_dmabuf *hbq_buf)
  1551. {
  1552. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1553. }
  1554. /**
  1555. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1556. * @phba: Pointer to HBA context object.
  1557. * @hbqno: HBQ number.
  1558. * @hbq_buf: Pointer to HBQ buffer.
  1559. *
  1560. * This function is called with the hbalock held to post a hbq buffer to the
  1561. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1562. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1563. * it successfully post the buffer else it will return an error.
  1564. **/
  1565. static int
  1566. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1567. struct hbq_dmabuf *hbq_buf)
  1568. {
  1569. struct lpfc_hbq_entry *hbqe;
  1570. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1571. /* Get next HBQ entry slot to use */
  1572. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1573. if (hbqe) {
  1574. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1575. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1576. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1577. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1578. hbqe->bde.tus.f.bdeFlags = 0;
  1579. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1580. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1581. /* Sync SLIM */
  1582. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1583. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1584. /* flush */
  1585. readl(phba->hbq_put + hbqno);
  1586. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1587. return 0;
  1588. } else
  1589. return -ENOMEM;
  1590. }
  1591. /**
  1592. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1593. * @phba: Pointer to HBA context object.
  1594. * @hbqno: HBQ number.
  1595. * @hbq_buf: Pointer to HBQ buffer.
  1596. *
  1597. * This function is called with the hbalock held to post an RQE to the SLI4
  1598. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1599. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1600. **/
  1601. static int
  1602. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1603. struct hbq_dmabuf *hbq_buf)
  1604. {
  1605. int rc;
  1606. struct lpfc_rqe hrqe;
  1607. struct lpfc_rqe drqe;
  1608. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1609. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1610. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1611. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1612. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1613. &hrqe, &drqe);
  1614. if (rc < 0)
  1615. return rc;
  1616. hbq_buf->tag = rc;
  1617. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1618. return 0;
  1619. }
  1620. /* HBQ for ELS and CT traffic. */
  1621. static struct lpfc_hbq_init lpfc_els_hbq = {
  1622. .rn = 1,
  1623. .entry_count = 256,
  1624. .mask_count = 0,
  1625. .profile = 0,
  1626. .ring_mask = (1 << LPFC_ELS_RING),
  1627. .buffer_count = 0,
  1628. .init_count = 40,
  1629. .add_count = 40,
  1630. };
  1631. /* HBQ for the extra ring if needed */
  1632. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1633. .rn = 1,
  1634. .entry_count = 200,
  1635. .mask_count = 0,
  1636. .profile = 0,
  1637. .ring_mask = (1 << LPFC_EXTRA_RING),
  1638. .buffer_count = 0,
  1639. .init_count = 0,
  1640. .add_count = 5,
  1641. };
  1642. /* Array of HBQs */
  1643. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1644. &lpfc_els_hbq,
  1645. &lpfc_extra_hbq,
  1646. };
  1647. /**
  1648. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1649. * @phba: Pointer to HBA context object.
  1650. * @hbqno: HBQ number.
  1651. * @count: Number of HBQ buffers to be posted.
  1652. *
  1653. * This function is called with no lock held to post more hbq buffers to the
  1654. * given HBQ. The function returns the number of HBQ buffers successfully
  1655. * posted.
  1656. **/
  1657. static int
  1658. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1659. {
  1660. uint32_t i, posted = 0;
  1661. unsigned long flags;
  1662. struct hbq_dmabuf *hbq_buffer;
  1663. LIST_HEAD(hbq_buf_list);
  1664. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1665. return 0;
  1666. if ((phba->hbqs[hbqno].buffer_count + count) >
  1667. lpfc_hbq_defs[hbqno]->entry_count)
  1668. count = lpfc_hbq_defs[hbqno]->entry_count -
  1669. phba->hbqs[hbqno].buffer_count;
  1670. if (!count)
  1671. return 0;
  1672. /* Allocate HBQ entries */
  1673. for (i = 0; i < count; i++) {
  1674. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1675. if (!hbq_buffer)
  1676. break;
  1677. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1678. }
  1679. /* Check whether HBQ is still in use */
  1680. spin_lock_irqsave(&phba->hbalock, flags);
  1681. if (!phba->hbq_in_use)
  1682. goto err;
  1683. while (!list_empty(&hbq_buf_list)) {
  1684. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1685. dbuf.list);
  1686. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1687. (hbqno << 16));
  1688. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1689. phba->hbqs[hbqno].buffer_count++;
  1690. posted++;
  1691. } else
  1692. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1693. }
  1694. spin_unlock_irqrestore(&phba->hbalock, flags);
  1695. return posted;
  1696. err:
  1697. spin_unlock_irqrestore(&phba->hbalock, flags);
  1698. while (!list_empty(&hbq_buf_list)) {
  1699. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1700. dbuf.list);
  1701. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1702. }
  1703. return 0;
  1704. }
  1705. /**
  1706. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1707. * @phba: Pointer to HBA context object.
  1708. * @qno: HBQ number.
  1709. *
  1710. * This function posts more buffers to the HBQ. This function
  1711. * is called with no lock held. The function returns the number of HBQ entries
  1712. * successfully allocated.
  1713. **/
  1714. int
  1715. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1716. {
  1717. if (phba->sli_rev == LPFC_SLI_REV4)
  1718. return 0;
  1719. else
  1720. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1721. lpfc_hbq_defs[qno]->add_count);
  1722. }
  1723. /**
  1724. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1725. * @phba: Pointer to HBA context object.
  1726. * @qno: HBQ queue number.
  1727. *
  1728. * This function is called from SLI initialization code path with
  1729. * no lock held to post initial HBQ buffers to firmware. The
  1730. * function returns the number of HBQ entries successfully allocated.
  1731. **/
  1732. static int
  1733. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1734. {
  1735. if (phba->sli_rev == LPFC_SLI_REV4)
  1736. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1737. lpfc_hbq_defs[qno]->entry_count);
  1738. else
  1739. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1740. lpfc_hbq_defs[qno]->init_count);
  1741. }
  1742. /**
  1743. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1744. * @phba: Pointer to HBA context object.
  1745. * @hbqno: HBQ number.
  1746. *
  1747. * This function removes the first hbq buffer on an hbq list and returns a
  1748. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1749. **/
  1750. static struct hbq_dmabuf *
  1751. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1752. {
  1753. struct lpfc_dmabuf *d_buf;
  1754. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1755. if (!d_buf)
  1756. return NULL;
  1757. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1758. }
  1759. /**
  1760. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1761. * @phba: Pointer to HBA context object.
  1762. * @tag: Tag of the hbq buffer.
  1763. *
  1764. * This function is called with hbalock held. This function searches
  1765. * for the hbq buffer associated with the given tag in the hbq buffer
  1766. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1767. * it returns NULL.
  1768. **/
  1769. static struct hbq_dmabuf *
  1770. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1771. {
  1772. struct lpfc_dmabuf *d_buf;
  1773. struct hbq_dmabuf *hbq_buf;
  1774. uint32_t hbqno;
  1775. hbqno = tag >> 16;
  1776. if (hbqno >= LPFC_MAX_HBQS)
  1777. return NULL;
  1778. spin_lock_irq(&phba->hbalock);
  1779. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1780. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1781. if (hbq_buf->tag == tag) {
  1782. spin_unlock_irq(&phba->hbalock);
  1783. return hbq_buf;
  1784. }
  1785. }
  1786. spin_unlock_irq(&phba->hbalock);
  1787. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1788. "1803 Bad hbq tag. Data: x%x x%x\n",
  1789. tag, phba->hbqs[tag >> 16].buffer_count);
  1790. return NULL;
  1791. }
  1792. /**
  1793. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1794. * @phba: Pointer to HBA context object.
  1795. * @hbq_buffer: Pointer to HBQ buffer.
  1796. *
  1797. * This function is called with hbalock. This function gives back
  1798. * the hbq buffer to firmware. If the HBQ does not have space to
  1799. * post the buffer, it will free the buffer.
  1800. **/
  1801. void
  1802. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1803. {
  1804. uint32_t hbqno;
  1805. if (hbq_buffer) {
  1806. hbqno = hbq_buffer->tag >> 16;
  1807. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1808. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1809. }
  1810. }
  1811. /**
  1812. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1813. * @mbxCommand: mailbox command code.
  1814. *
  1815. * This function is called by the mailbox event handler function to verify
  1816. * that the completed mailbox command is a legitimate mailbox command. If the
  1817. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1818. * and the mailbox event handler will take the HBA offline.
  1819. **/
  1820. static int
  1821. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1822. {
  1823. uint8_t ret;
  1824. switch (mbxCommand) {
  1825. case MBX_LOAD_SM:
  1826. case MBX_READ_NV:
  1827. case MBX_WRITE_NV:
  1828. case MBX_WRITE_VPARMS:
  1829. case MBX_RUN_BIU_DIAG:
  1830. case MBX_INIT_LINK:
  1831. case MBX_DOWN_LINK:
  1832. case MBX_CONFIG_LINK:
  1833. case MBX_CONFIG_RING:
  1834. case MBX_RESET_RING:
  1835. case MBX_READ_CONFIG:
  1836. case MBX_READ_RCONFIG:
  1837. case MBX_READ_SPARM:
  1838. case MBX_READ_STATUS:
  1839. case MBX_READ_RPI:
  1840. case MBX_READ_XRI:
  1841. case MBX_READ_REV:
  1842. case MBX_READ_LNK_STAT:
  1843. case MBX_REG_LOGIN:
  1844. case MBX_UNREG_LOGIN:
  1845. case MBX_CLEAR_LA:
  1846. case MBX_DUMP_MEMORY:
  1847. case MBX_DUMP_CONTEXT:
  1848. case MBX_RUN_DIAGS:
  1849. case MBX_RESTART:
  1850. case MBX_UPDATE_CFG:
  1851. case MBX_DOWN_LOAD:
  1852. case MBX_DEL_LD_ENTRY:
  1853. case MBX_RUN_PROGRAM:
  1854. case MBX_SET_MASK:
  1855. case MBX_SET_VARIABLE:
  1856. case MBX_UNREG_D_ID:
  1857. case MBX_KILL_BOARD:
  1858. case MBX_CONFIG_FARP:
  1859. case MBX_BEACON:
  1860. case MBX_LOAD_AREA:
  1861. case MBX_RUN_BIU_DIAG64:
  1862. case MBX_CONFIG_PORT:
  1863. case MBX_READ_SPARM64:
  1864. case MBX_READ_RPI64:
  1865. case MBX_REG_LOGIN64:
  1866. case MBX_READ_TOPOLOGY:
  1867. case MBX_WRITE_WWN:
  1868. case MBX_SET_DEBUG:
  1869. case MBX_LOAD_EXP_ROM:
  1870. case MBX_ASYNCEVT_ENABLE:
  1871. case MBX_REG_VPI:
  1872. case MBX_UNREG_VPI:
  1873. case MBX_HEARTBEAT:
  1874. case MBX_PORT_CAPABILITIES:
  1875. case MBX_PORT_IOV_CONTROL:
  1876. case MBX_SLI4_CONFIG:
  1877. case MBX_SLI4_REQ_FTRS:
  1878. case MBX_REG_FCFI:
  1879. case MBX_UNREG_FCFI:
  1880. case MBX_REG_VFI:
  1881. case MBX_UNREG_VFI:
  1882. case MBX_INIT_VPI:
  1883. case MBX_INIT_VFI:
  1884. case MBX_RESUME_RPI:
  1885. case MBX_READ_EVENT_LOG_STATUS:
  1886. case MBX_READ_EVENT_LOG:
  1887. case MBX_SECURITY_MGMT:
  1888. case MBX_AUTH_PORT:
  1889. ret = mbxCommand;
  1890. break;
  1891. default:
  1892. ret = MBX_SHUTDOWN;
  1893. break;
  1894. }
  1895. return ret;
  1896. }
  1897. /**
  1898. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1899. * @phba: Pointer to HBA context object.
  1900. * @pmboxq: Pointer to mailbox command.
  1901. *
  1902. * This is completion handler function for mailbox commands issued from
  1903. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1904. * mailbox event handler function with no lock held. This function
  1905. * will wake up thread waiting on the wait queue pointed by context1
  1906. * of the mailbox.
  1907. **/
  1908. void
  1909. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1910. {
  1911. wait_queue_head_t *pdone_q;
  1912. unsigned long drvr_flag;
  1913. /*
  1914. * If pdone_q is empty, the driver thread gave up waiting and
  1915. * continued running.
  1916. */
  1917. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1918. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1919. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1920. if (pdone_q)
  1921. wake_up_interruptible(pdone_q);
  1922. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1923. return;
  1924. }
  1925. /**
  1926. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1927. * @phba: Pointer to HBA context object.
  1928. * @pmb: Pointer to mailbox object.
  1929. *
  1930. * This function is the default mailbox completion handler. It
  1931. * frees the memory resources associated with the completed mailbox
  1932. * command. If the completed command is a REG_LOGIN mailbox command,
  1933. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1934. **/
  1935. void
  1936. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1937. {
  1938. struct lpfc_vport *vport = pmb->vport;
  1939. struct lpfc_dmabuf *mp;
  1940. struct lpfc_nodelist *ndlp;
  1941. struct Scsi_Host *shost;
  1942. uint16_t rpi, vpi;
  1943. int rc;
  1944. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1945. if (mp) {
  1946. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1947. kfree(mp);
  1948. }
  1949. /*
  1950. * If a REG_LOGIN succeeded after node is destroyed or node
  1951. * is in re-discovery driver need to cleanup the RPI.
  1952. */
  1953. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1954. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1955. !pmb->u.mb.mbxStatus) {
  1956. rpi = pmb->u.mb.un.varWords[0];
  1957. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1958. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1959. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1960. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1961. if (rc != MBX_NOT_FINISHED)
  1962. return;
  1963. }
  1964. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1965. !(phba->pport->load_flag & FC_UNLOADING) &&
  1966. !pmb->u.mb.mbxStatus) {
  1967. shost = lpfc_shost_from_vport(vport);
  1968. spin_lock_irq(shost->host_lock);
  1969. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1970. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1971. spin_unlock_irq(shost->host_lock);
  1972. }
  1973. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1974. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1975. lpfc_nlp_put(ndlp);
  1976. pmb->context2 = NULL;
  1977. }
  1978. /* Check security permission status on INIT_LINK mailbox command */
  1979. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1980. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1981. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1982. "2860 SLI authentication is required "
  1983. "for INIT_LINK but has not done yet\n");
  1984. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1985. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1986. else
  1987. mempool_free(pmb, phba->mbox_mem_pool);
  1988. }
  1989. /**
  1990. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1991. * @phba: Pointer to HBA context object.
  1992. *
  1993. * This function is called with no lock held. This function processes all
  1994. * the completed mailbox commands and gives it to upper layers. The interrupt
  1995. * service routine processes mailbox completion interrupt and adds completed
  1996. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1997. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1998. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1999. * function returns the mailbox commands to the upper layer by calling the
  2000. * completion handler function of each mailbox.
  2001. **/
  2002. int
  2003. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2004. {
  2005. MAILBOX_t *pmbox;
  2006. LPFC_MBOXQ_t *pmb;
  2007. int rc;
  2008. LIST_HEAD(cmplq);
  2009. phba->sli.slistat.mbox_event++;
  2010. /* Get all completed mailboxe buffers into the cmplq */
  2011. spin_lock_irq(&phba->hbalock);
  2012. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2013. spin_unlock_irq(&phba->hbalock);
  2014. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2015. do {
  2016. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2017. if (pmb == NULL)
  2018. break;
  2019. pmbox = &pmb->u.mb;
  2020. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2021. if (pmb->vport) {
  2022. lpfc_debugfs_disc_trc(pmb->vport,
  2023. LPFC_DISC_TRC_MBOX_VPORT,
  2024. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2025. (uint32_t)pmbox->mbxCommand,
  2026. pmbox->un.varWords[0],
  2027. pmbox->un.varWords[1]);
  2028. }
  2029. else {
  2030. lpfc_debugfs_disc_trc(phba->pport,
  2031. LPFC_DISC_TRC_MBOX,
  2032. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2033. (uint32_t)pmbox->mbxCommand,
  2034. pmbox->un.varWords[0],
  2035. pmbox->un.varWords[1]);
  2036. }
  2037. }
  2038. /*
  2039. * It is a fatal error if unknown mbox command completion.
  2040. */
  2041. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2042. MBX_SHUTDOWN) {
  2043. /* Unknown mailbox command compl */
  2044. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2045. "(%d):0323 Unknown Mailbox command "
  2046. "x%x (x%x/x%x) Cmpl\n",
  2047. pmb->vport ? pmb->vport->vpi : 0,
  2048. pmbox->mbxCommand,
  2049. lpfc_sli_config_mbox_subsys_get(phba,
  2050. pmb),
  2051. lpfc_sli_config_mbox_opcode_get(phba,
  2052. pmb));
  2053. phba->link_state = LPFC_HBA_ERROR;
  2054. phba->work_hs = HS_FFER3;
  2055. lpfc_handle_eratt(phba);
  2056. continue;
  2057. }
  2058. if (pmbox->mbxStatus) {
  2059. phba->sli.slistat.mbox_stat_err++;
  2060. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2061. /* Mbox cmd cmpl error - RETRYing */
  2062. lpfc_printf_log(phba, KERN_INFO,
  2063. LOG_MBOX | LOG_SLI,
  2064. "(%d):0305 Mbox cmd cmpl "
  2065. "error - RETRYing Data: x%x "
  2066. "(x%x/x%x) x%x x%x x%x\n",
  2067. pmb->vport ? pmb->vport->vpi : 0,
  2068. pmbox->mbxCommand,
  2069. lpfc_sli_config_mbox_subsys_get(phba,
  2070. pmb),
  2071. lpfc_sli_config_mbox_opcode_get(phba,
  2072. pmb),
  2073. pmbox->mbxStatus,
  2074. pmbox->un.varWords[0],
  2075. pmb->vport->port_state);
  2076. pmbox->mbxStatus = 0;
  2077. pmbox->mbxOwner = OWN_HOST;
  2078. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2079. if (rc != MBX_NOT_FINISHED)
  2080. continue;
  2081. }
  2082. }
  2083. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2084. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2085. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2086. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2087. pmb->vport ? pmb->vport->vpi : 0,
  2088. pmbox->mbxCommand,
  2089. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2090. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2091. pmb->mbox_cmpl,
  2092. *((uint32_t *) pmbox),
  2093. pmbox->un.varWords[0],
  2094. pmbox->un.varWords[1],
  2095. pmbox->un.varWords[2],
  2096. pmbox->un.varWords[3],
  2097. pmbox->un.varWords[4],
  2098. pmbox->un.varWords[5],
  2099. pmbox->un.varWords[6],
  2100. pmbox->un.varWords[7]);
  2101. if (pmb->mbox_cmpl)
  2102. pmb->mbox_cmpl(phba,pmb);
  2103. } while (1);
  2104. return 0;
  2105. }
  2106. /**
  2107. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2108. * @phba: Pointer to HBA context object.
  2109. * @pring: Pointer to driver SLI ring object.
  2110. * @tag: buffer tag.
  2111. *
  2112. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2113. * is set in the tag the buffer is posted for a particular exchange,
  2114. * the function will return the buffer without replacing the buffer.
  2115. * If the buffer is for unsolicited ELS or CT traffic, this function
  2116. * returns the buffer and also posts another buffer to the firmware.
  2117. **/
  2118. static struct lpfc_dmabuf *
  2119. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2120. struct lpfc_sli_ring *pring,
  2121. uint32_t tag)
  2122. {
  2123. struct hbq_dmabuf *hbq_entry;
  2124. if (tag & QUE_BUFTAG_BIT)
  2125. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2126. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2127. if (!hbq_entry)
  2128. return NULL;
  2129. return &hbq_entry->dbuf;
  2130. }
  2131. /**
  2132. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2133. * @phba: Pointer to HBA context object.
  2134. * @pring: Pointer to driver SLI ring object.
  2135. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2136. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2137. * @fch_type: the type for the first frame of the sequence.
  2138. *
  2139. * This function is called with no lock held. This function uses the r_ctl and
  2140. * type of the received sequence to find the correct callback function to call
  2141. * to process the sequence.
  2142. **/
  2143. static int
  2144. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2145. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2146. uint32_t fch_type)
  2147. {
  2148. int i;
  2149. /* unSolicited Responses */
  2150. if (pring->prt[0].profile) {
  2151. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2152. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2153. saveq);
  2154. return 1;
  2155. }
  2156. /* We must search, based on rctl / type
  2157. for the right routine */
  2158. for (i = 0; i < pring->num_mask; i++) {
  2159. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2160. (pring->prt[i].type == fch_type)) {
  2161. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2162. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2163. (phba, pring, saveq);
  2164. return 1;
  2165. }
  2166. }
  2167. return 0;
  2168. }
  2169. /**
  2170. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2171. * @phba: Pointer to HBA context object.
  2172. * @pring: Pointer to driver SLI ring object.
  2173. * @saveq: Pointer to the unsolicited iocb.
  2174. *
  2175. * This function is called with no lock held by the ring event handler
  2176. * when there is an unsolicited iocb posted to the response ring by the
  2177. * firmware. This function gets the buffer associated with the iocbs
  2178. * and calls the event handler for the ring. This function handles both
  2179. * qring buffers and hbq buffers.
  2180. * When the function returns 1 the caller can free the iocb object otherwise
  2181. * upper layer functions will free the iocb objects.
  2182. **/
  2183. static int
  2184. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2185. struct lpfc_iocbq *saveq)
  2186. {
  2187. IOCB_t * irsp;
  2188. WORD5 * w5p;
  2189. uint32_t Rctl, Type;
  2190. uint32_t match;
  2191. struct lpfc_iocbq *iocbq;
  2192. struct lpfc_dmabuf *dmzbuf;
  2193. match = 0;
  2194. irsp = &(saveq->iocb);
  2195. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2196. if (pring->lpfc_sli_rcv_async_status)
  2197. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2198. else
  2199. lpfc_printf_log(phba,
  2200. KERN_WARNING,
  2201. LOG_SLI,
  2202. "0316 Ring %d handler: unexpected "
  2203. "ASYNC_STATUS iocb received evt_code "
  2204. "0x%x\n",
  2205. pring->ringno,
  2206. irsp->un.asyncstat.evt_code);
  2207. return 1;
  2208. }
  2209. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2210. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2211. if (irsp->ulpBdeCount > 0) {
  2212. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2213. irsp->un.ulpWord[3]);
  2214. lpfc_in_buf_free(phba, dmzbuf);
  2215. }
  2216. if (irsp->ulpBdeCount > 1) {
  2217. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2218. irsp->unsli3.sli3Words[3]);
  2219. lpfc_in_buf_free(phba, dmzbuf);
  2220. }
  2221. if (irsp->ulpBdeCount > 2) {
  2222. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2223. irsp->unsli3.sli3Words[7]);
  2224. lpfc_in_buf_free(phba, dmzbuf);
  2225. }
  2226. return 1;
  2227. }
  2228. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2229. if (irsp->ulpBdeCount != 0) {
  2230. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2231. irsp->un.ulpWord[3]);
  2232. if (!saveq->context2)
  2233. lpfc_printf_log(phba,
  2234. KERN_ERR,
  2235. LOG_SLI,
  2236. "0341 Ring %d Cannot find buffer for "
  2237. "an unsolicited iocb. tag 0x%x\n",
  2238. pring->ringno,
  2239. irsp->un.ulpWord[3]);
  2240. }
  2241. if (irsp->ulpBdeCount == 2) {
  2242. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2243. irsp->unsli3.sli3Words[7]);
  2244. if (!saveq->context3)
  2245. lpfc_printf_log(phba,
  2246. KERN_ERR,
  2247. LOG_SLI,
  2248. "0342 Ring %d Cannot find buffer for an"
  2249. " unsolicited iocb. tag 0x%x\n",
  2250. pring->ringno,
  2251. irsp->unsli3.sli3Words[7]);
  2252. }
  2253. list_for_each_entry(iocbq, &saveq->list, list) {
  2254. irsp = &(iocbq->iocb);
  2255. if (irsp->ulpBdeCount != 0) {
  2256. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2257. irsp->un.ulpWord[3]);
  2258. if (!iocbq->context2)
  2259. lpfc_printf_log(phba,
  2260. KERN_ERR,
  2261. LOG_SLI,
  2262. "0343 Ring %d Cannot find "
  2263. "buffer for an unsolicited iocb"
  2264. ". tag 0x%x\n", pring->ringno,
  2265. irsp->un.ulpWord[3]);
  2266. }
  2267. if (irsp->ulpBdeCount == 2) {
  2268. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2269. irsp->unsli3.sli3Words[7]);
  2270. if (!iocbq->context3)
  2271. lpfc_printf_log(phba,
  2272. KERN_ERR,
  2273. LOG_SLI,
  2274. "0344 Ring %d Cannot find "
  2275. "buffer for an unsolicited "
  2276. "iocb. tag 0x%x\n",
  2277. pring->ringno,
  2278. irsp->unsli3.sli3Words[7]);
  2279. }
  2280. }
  2281. }
  2282. if (irsp->ulpBdeCount != 0 &&
  2283. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2284. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2285. int found = 0;
  2286. /* search continue save q for same XRI */
  2287. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2288. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2289. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2290. list_add_tail(&saveq->list, &iocbq->list);
  2291. found = 1;
  2292. break;
  2293. }
  2294. }
  2295. if (!found)
  2296. list_add_tail(&saveq->clist,
  2297. &pring->iocb_continue_saveq);
  2298. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2299. list_del_init(&iocbq->clist);
  2300. saveq = iocbq;
  2301. irsp = &(saveq->iocb);
  2302. } else
  2303. return 0;
  2304. }
  2305. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2306. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2307. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2308. Rctl = FC_RCTL_ELS_REQ;
  2309. Type = FC_TYPE_ELS;
  2310. } else {
  2311. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2312. Rctl = w5p->hcsw.Rctl;
  2313. Type = w5p->hcsw.Type;
  2314. /* Firmware Workaround */
  2315. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2316. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2317. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2318. Rctl = FC_RCTL_ELS_REQ;
  2319. Type = FC_TYPE_ELS;
  2320. w5p->hcsw.Rctl = Rctl;
  2321. w5p->hcsw.Type = Type;
  2322. }
  2323. }
  2324. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2325. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2326. "0313 Ring %d handler: unexpected Rctl x%x "
  2327. "Type x%x received\n",
  2328. pring->ringno, Rctl, Type);
  2329. return 1;
  2330. }
  2331. /**
  2332. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2333. * @phba: Pointer to HBA context object.
  2334. * @pring: Pointer to driver SLI ring object.
  2335. * @prspiocb: Pointer to response iocb object.
  2336. *
  2337. * This function looks up the iocb_lookup table to get the command iocb
  2338. * corresponding to the given response iocb using the iotag of the
  2339. * response iocb. This function is called with the hbalock held.
  2340. * This function returns the command iocb object if it finds the command
  2341. * iocb else returns NULL.
  2342. **/
  2343. static struct lpfc_iocbq *
  2344. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2345. struct lpfc_sli_ring *pring,
  2346. struct lpfc_iocbq *prspiocb)
  2347. {
  2348. struct lpfc_iocbq *cmd_iocb = NULL;
  2349. uint16_t iotag;
  2350. iotag = prspiocb->iocb.ulpIoTag;
  2351. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2352. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2353. list_del_init(&cmd_iocb->list);
  2354. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2355. pring->txcmplq_cnt--;
  2356. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2357. }
  2358. return cmd_iocb;
  2359. }
  2360. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2361. "0317 iotag x%x is out off "
  2362. "range: max iotag x%x wd0 x%x\n",
  2363. iotag, phba->sli.last_iotag,
  2364. *(((uint32_t *) &prspiocb->iocb) + 7));
  2365. return NULL;
  2366. }
  2367. /**
  2368. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2369. * @phba: Pointer to HBA context object.
  2370. * @pring: Pointer to driver SLI ring object.
  2371. * @iotag: IOCB tag.
  2372. *
  2373. * This function looks up the iocb_lookup table to get the command iocb
  2374. * corresponding to the given iotag. This function is called with the
  2375. * hbalock held.
  2376. * This function returns the command iocb object if it finds the command
  2377. * iocb else returns NULL.
  2378. **/
  2379. static struct lpfc_iocbq *
  2380. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2381. struct lpfc_sli_ring *pring, uint16_t iotag)
  2382. {
  2383. struct lpfc_iocbq *cmd_iocb;
  2384. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2385. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2386. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2387. /* remove from txcmpl queue list */
  2388. list_del_init(&cmd_iocb->list);
  2389. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2390. pring->txcmplq_cnt--;
  2391. return cmd_iocb;
  2392. }
  2393. }
  2394. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2395. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2396. iotag, phba->sli.last_iotag);
  2397. return NULL;
  2398. }
  2399. /**
  2400. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2401. * @phba: Pointer to HBA context object.
  2402. * @pring: Pointer to driver SLI ring object.
  2403. * @saveq: Pointer to the response iocb to be processed.
  2404. *
  2405. * This function is called by the ring event handler for non-fcp
  2406. * rings when there is a new response iocb in the response ring.
  2407. * The caller is not required to hold any locks. This function
  2408. * gets the command iocb associated with the response iocb and
  2409. * calls the completion handler for the command iocb. If there
  2410. * is no completion handler, the function will free the resources
  2411. * associated with command iocb. If the response iocb is for
  2412. * an already aborted command iocb, the status of the completion
  2413. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2414. * This function always returns 1.
  2415. **/
  2416. static int
  2417. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2418. struct lpfc_iocbq *saveq)
  2419. {
  2420. struct lpfc_iocbq *cmdiocbp;
  2421. int rc = 1;
  2422. unsigned long iflag;
  2423. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2424. spin_lock_irqsave(&phba->hbalock, iflag);
  2425. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2426. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2427. if (cmdiocbp) {
  2428. if (cmdiocbp->iocb_cmpl) {
  2429. /*
  2430. * If an ELS command failed send an event to mgmt
  2431. * application.
  2432. */
  2433. if (saveq->iocb.ulpStatus &&
  2434. (pring->ringno == LPFC_ELS_RING) &&
  2435. (cmdiocbp->iocb.ulpCommand ==
  2436. CMD_ELS_REQUEST64_CR))
  2437. lpfc_send_els_failure_event(phba,
  2438. cmdiocbp, saveq);
  2439. /*
  2440. * Post all ELS completions to the worker thread.
  2441. * All other are passed to the completion callback.
  2442. */
  2443. if (pring->ringno == LPFC_ELS_RING) {
  2444. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2445. (cmdiocbp->iocb_flag &
  2446. LPFC_DRIVER_ABORTED)) {
  2447. spin_lock_irqsave(&phba->hbalock,
  2448. iflag);
  2449. cmdiocbp->iocb_flag &=
  2450. ~LPFC_DRIVER_ABORTED;
  2451. spin_unlock_irqrestore(&phba->hbalock,
  2452. iflag);
  2453. saveq->iocb.ulpStatus =
  2454. IOSTAT_LOCAL_REJECT;
  2455. saveq->iocb.un.ulpWord[4] =
  2456. IOERR_SLI_ABORTED;
  2457. /* Firmware could still be in progress
  2458. * of DMAing payload, so don't free data
  2459. * buffer till after a hbeat.
  2460. */
  2461. spin_lock_irqsave(&phba->hbalock,
  2462. iflag);
  2463. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2464. spin_unlock_irqrestore(&phba->hbalock,
  2465. iflag);
  2466. }
  2467. if (phba->sli_rev == LPFC_SLI_REV4) {
  2468. if (saveq->iocb_flag &
  2469. LPFC_EXCHANGE_BUSY) {
  2470. /* Set cmdiocb flag for the
  2471. * exchange busy so sgl (xri)
  2472. * will not be released until
  2473. * the abort xri is received
  2474. * from hba.
  2475. */
  2476. spin_lock_irqsave(
  2477. &phba->hbalock, iflag);
  2478. cmdiocbp->iocb_flag |=
  2479. LPFC_EXCHANGE_BUSY;
  2480. spin_unlock_irqrestore(
  2481. &phba->hbalock, iflag);
  2482. }
  2483. if (cmdiocbp->iocb_flag &
  2484. LPFC_DRIVER_ABORTED) {
  2485. /*
  2486. * Clear LPFC_DRIVER_ABORTED
  2487. * bit in case it was driver
  2488. * initiated abort.
  2489. */
  2490. spin_lock_irqsave(
  2491. &phba->hbalock, iflag);
  2492. cmdiocbp->iocb_flag &=
  2493. ~LPFC_DRIVER_ABORTED;
  2494. spin_unlock_irqrestore(
  2495. &phba->hbalock, iflag);
  2496. cmdiocbp->iocb.ulpStatus =
  2497. IOSTAT_LOCAL_REJECT;
  2498. cmdiocbp->iocb.un.ulpWord[4] =
  2499. IOERR_ABORT_REQUESTED;
  2500. /*
  2501. * For SLI4, irsiocb contains
  2502. * NO_XRI in sli_xritag, it
  2503. * shall not affect releasing
  2504. * sgl (xri) process.
  2505. */
  2506. saveq->iocb.ulpStatus =
  2507. IOSTAT_LOCAL_REJECT;
  2508. saveq->iocb.un.ulpWord[4] =
  2509. IOERR_SLI_ABORTED;
  2510. spin_lock_irqsave(
  2511. &phba->hbalock, iflag);
  2512. saveq->iocb_flag |=
  2513. LPFC_DELAY_MEM_FREE;
  2514. spin_unlock_irqrestore(
  2515. &phba->hbalock, iflag);
  2516. }
  2517. }
  2518. }
  2519. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2520. } else
  2521. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2522. } else {
  2523. /*
  2524. * Unknown initiating command based on the response iotag.
  2525. * This could be the case on the ELS ring because of
  2526. * lpfc_els_abort().
  2527. */
  2528. if (pring->ringno != LPFC_ELS_RING) {
  2529. /*
  2530. * Ring <ringno> handler: unexpected completion IoTag
  2531. * <IoTag>
  2532. */
  2533. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2534. "0322 Ring %d handler: "
  2535. "unexpected completion IoTag x%x "
  2536. "Data: x%x x%x x%x x%x\n",
  2537. pring->ringno,
  2538. saveq->iocb.ulpIoTag,
  2539. saveq->iocb.ulpStatus,
  2540. saveq->iocb.un.ulpWord[4],
  2541. saveq->iocb.ulpCommand,
  2542. saveq->iocb.ulpContext);
  2543. }
  2544. }
  2545. return rc;
  2546. }
  2547. /**
  2548. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2549. * @phba: Pointer to HBA context object.
  2550. * @pring: Pointer to driver SLI ring object.
  2551. *
  2552. * This function is called from the iocb ring event handlers when
  2553. * put pointer is ahead of the get pointer for a ring. This function signal
  2554. * an error attention condition to the worker thread and the worker
  2555. * thread will transition the HBA to offline state.
  2556. **/
  2557. static void
  2558. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2559. {
  2560. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2561. /*
  2562. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2563. * rsp ring <portRspMax>
  2564. */
  2565. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2566. "0312 Ring %d handler: portRspPut %d "
  2567. "is bigger than rsp ring %d\n",
  2568. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2569. pring->sli.sli3.numRiocb);
  2570. phba->link_state = LPFC_HBA_ERROR;
  2571. /*
  2572. * All error attention handlers are posted to
  2573. * worker thread
  2574. */
  2575. phba->work_ha |= HA_ERATT;
  2576. phba->work_hs = HS_FFER3;
  2577. lpfc_worker_wake_up(phba);
  2578. return;
  2579. }
  2580. /**
  2581. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2582. * @ptr: Pointer to address of HBA context object.
  2583. *
  2584. * This function is invoked by the Error Attention polling timer when the
  2585. * timer times out. It will check the SLI Error Attention register for
  2586. * possible attention events. If so, it will post an Error Attention event
  2587. * and wake up worker thread to process it. Otherwise, it will set up the
  2588. * Error Attention polling timer for the next poll.
  2589. **/
  2590. void lpfc_poll_eratt(unsigned long ptr)
  2591. {
  2592. struct lpfc_hba *phba;
  2593. uint32_t eratt = 0;
  2594. phba = (struct lpfc_hba *)ptr;
  2595. /* Check chip HA register for error event */
  2596. eratt = lpfc_sli_check_eratt(phba);
  2597. if (eratt)
  2598. /* Tell the worker thread there is work to do */
  2599. lpfc_worker_wake_up(phba);
  2600. else
  2601. /* Restart the timer for next eratt poll */
  2602. mod_timer(&phba->eratt_poll, jiffies +
  2603. HZ * LPFC_ERATT_POLL_INTERVAL);
  2604. return;
  2605. }
  2606. /**
  2607. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2608. * @phba: Pointer to HBA context object.
  2609. * @pring: Pointer to driver SLI ring object.
  2610. * @mask: Host attention register mask for this ring.
  2611. *
  2612. * This function is called from the interrupt context when there is a ring
  2613. * event for the fcp ring. The caller does not hold any lock.
  2614. * The function processes each response iocb in the response ring until it
  2615. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2616. * LE bit set. The function will call the completion handler of the command iocb
  2617. * if the response iocb indicates a completion for a command iocb or it is
  2618. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2619. * function if this is an unsolicited iocb.
  2620. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2621. * to check it explicitly.
  2622. */
  2623. int
  2624. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2625. struct lpfc_sli_ring *pring, uint32_t mask)
  2626. {
  2627. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2628. IOCB_t *irsp = NULL;
  2629. IOCB_t *entry = NULL;
  2630. struct lpfc_iocbq *cmdiocbq = NULL;
  2631. struct lpfc_iocbq rspiocbq;
  2632. uint32_t status;
  2633. uint32_t portRspPut, portRspMax;
  2634. int rc = 1;
  2635. lpfc_iocb_type type;
  2636. unsigned long iflag;
  2637. uint32_t rsp_cmpl = 0;
  2638. spin_lock_irqsave(&phba->hbalock, iflag);
  2639. pring->stats.iocb_event++;
  2640. /*
  2641. * The next available response entry should never exceed the maximum
  2642. * entries. If it does, treat it as an adapter hardware error.
  2643. */
  2644. portRspMax = pring->sli.sli3.numRiocb;
  2645. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2646. if (unlikely(portRspPut >= portRspMax)) {
  2647. lpfc_sli_rsp_pointers_error(phba, pring);
  2648. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2649. return 1;
  2650. }
  2651. if (phba->fcp_ring_in_use) {
  2652. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2653. return 1;
  2654. } else
  2655. phba->fcp_ring_in_use = 1;
  2656. rmb();
  2657. while (pring->sli.sli3.rspidx != portRspPut) {
  2658. /*
  2659. * Fetch an entry off the ring and copy it into a local data
  2660. * structure. The copy involves a byte-swap since the
  2661. * network byte order and pci byte orders are different.
  2662. */
  2663. entry = lpfc_resp_iocb(phba, pring);
  2664. phba->last_completion_time = jiffies;
  2665. if (++pring->sli.sli3.rspidx >= portRspMax)
  2666. pring->sli.sli3.rspidx = 0;
  2667. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2668. (uint32_t *) &rspiocbq.iocb,
  2669. phba->iocb_rsp_size);
  2670. INIT_LIST_HEAD(&(rspiocbq.list));
  2671. irsp = &rspiocbq.iocb;
  2672. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2673. pring->stats.iocb_rsp++;
  2674. rsp_cmpl++;
  2675. if (unlikely(irsp->ulpStatus)) {
  2676. /*
  2677. * If resource errors reported from HBA, reduce
  2678. * queuedepths of the SCSI device.
  2679. */
  2680. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2681. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2682. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2683. phba->lpfc_rampdown_queue_depth(phba);
  2684. spin_lock_irqsave(&phba->hbalock, iflag);
  2685. }
  2686. /* Rsp ring <ringno> error: IOCB */
  2687. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2688. "0336 Rsp Ring %d error: IOCB Data: "
  2689. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2690. pring->ringno,
  2691. irsp->un.ulpWord[0],
  2692. irsp->un.ulpWord[1],
  2693. irsp->un.ulpWord[2],
  2694. irsp->un.ulpWord[3],
  2695. irsp->un.ulpWord[4],
  2696. irsp->un.ulpWord[5],
  2697. *(uint32_t *)&irsp->un1,
  2698. *((uint32_t *)&irsp->un1 + 1));
  2699. }
  2700. switch (type) {
  2701. case LPFC_ABORT_IOCB:
  2702. case LPFC_SOL_IOCB:
  2703. /*
  2704. * Idle exchange closed via ABTS from port. No iocb
  2705. * resources need to be recovered.
  2706. */
  2707. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2708. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2709. "0333 IOCB cmd 0x%x"
  2710. " processed. Skipping"
  2711. " completion\n",
  2712. irsp->ulpCommand);
  2713. break;
  2714. }
  2715. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2716. &rspiocbq);
  2717. if (unlikely(!cmdiocbq))
  2718. break;
  2719. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2720. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2721. if (cmdiocbq->iocb_cmpl) {
  2722. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2723. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2724. &rspiocbq);
  2725. spin_lock_irqsave(&phba->hbalock, iflag);
  2726. }
  2727. break;
  2728. case LPFC_UNSOL_IOCB:
  2729. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2730. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2731. spin_lock_irqsave(&phba->hbalock, iflag);
  2732. break;
  2733. default:
  2734. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2735. char adaptermsg[LPFC_MAX_ADPTMSG];
  2736. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2737. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2738. MAX_MSG_DATA);
  2739. dev_warn(&((phba->pcidev)->dev),
  2740. "lpfc%d: %s\n",
  2741. phba->brd_no, adaptermsg);
  2742. } else {
  2743. /* Unknown IOCB command */
  2744. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2745. "0334 Unknown IOCB command "
  2746. "Data: x%x, x%x x%x x%x x%x\n",
  2747. type, irsp->ulpCommand,
  2748. irsp->ulpStatus,
  2749. irsp->ulpIoTag,
  2750. irsp->ulpContext);
  2751. }
  2752. break;
  2753. }
  2754. /*
  2755. * The response IOCB has been processed. Update the ring
  2756. * pointer in SLIM. If the port response put pointer has not
  2757. * been updated, sync the pgp->rspPutInx and fetch the new port
  2758. * response put pointer.
  2759. */
  2760. writel(pring->sli.sli3.rspidx,
  2761. &phba->host_gp[pring->ringno].rspGetInx);
  2762. if (pring->sli.sli3.rspidx == portRspPut)
  2763. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2764. }
  2765. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2766. pring->stats.iocb_rsp_full++;
  2767. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2768. writel(status, phba->CAregaddr);
  2769. readl(phba->CAregaddr);
  2770. }
  2771. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2772. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2773. pring->stats.iocb_cmd_empty++;
  2774. /* Force update of the local copy of cmdGetInx */
  2775. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2776. lpfc_sli_resume_iocb(phba, pring);
  2777. if ((pring->lpfc_sli_cmd_available))
  2778. (pring->lpfc_sli_cmd_available) (phba, pring);
  2779. }
  2780. phba->fcp_ring_in_use = 0;
  2781. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2782. return rc;
  2783. }
  2784. /**
  2785. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2786. * @phba: Pointer to HBA context object.
  2787. * @pring: Pointer to driver SLI ring object.
  2788. * @rspiocbp: Pointer to driver response IOCB object.
  2789. *
  2790. * This function is called from the worker thread when there is a slow-path
  2791. * response IOCB to process. This function chains all the response iocbs until
  2792. * seeing the iocb with the LE bit set. The function will call
  2793. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2794. * completion of a command iocb. The function will call the
  2795. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2796. * The function frees the resources or calls the completion handler if this
  2797. * iocb is an abort completion. The function returns NULL when the response
  2798. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2799. * this function shall chain the iocb on to the iocb_continueq and return the
  2800. * response iocb passed in.
  2801. **/
  2802. static struct lpfc_iocbq *
  2803. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2804. struct lpfc_iocbq *rspiocbp)
  2805. {
  2806. struct lpfc_iocbq *saveq;
  2807. struct lpfc_iocbq *cmdiocbp;
  2808. struct lpfc_iocbq *next_iocb;
  2809. IOCB_t *irsp = NULL;
  2810. uint32_t free_saveq;
  2811. uint8_t iocb_cmd_type;
  2812. lpfc_iocb_type type;
  2813. unsigned long iflag;
  2814. int rc;
  2815. spin_lock_irqsave(&phba->hbalock, iflag);
  2816. /* First add the response iocb to the countinueq list */
  2817. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2818. pring->iocb_continueq_cnt++;
  2819. /* Now, determine whether the list is completed for processing */
  2820. irsp = &rspiocbp->iocb;
  2821. if (irsp->ulpLe) {
  2822. /*
  2823. * By default, the driver expects to free all resources
  2824. * associated with this iocb completion.
  2825. */
  2826. free_saveq = 1;
  2827. saveq = list_get_first(&pring->iocb_continueq,
  2828. struct lpfc_iocbq, list);
  2829. irsp = &(saveq->iocb);
  2830. list_del_init(&pring->iocb_continueq);
  2831. pring->iocb_continueq_cnt = 0;
  2832. pring->stats.iocb_rsp++;
  2833. /*
  2834. * If resource errors reported from HBA, reduce
  2835. * queuedepths of the SCSI device.
  2836. */
  2837. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2838. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2839. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2840. phba->lpfc_rampdown_queue_depth(phba);
  2841. spin_lock_irqsave(&phba->hbalock, iflag);
  2842. }
  2843. if (irsp->ulpStatus) {
  2844. /* Rsp ring <ringno> error: IOCB */
  2845. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2846. "0328 Rsp Ring %d error: "
  2847. "IOCB Data: "
  2848. "x%x x%x x%x x%x "
  2849. "x%x x%x x%x x%x "
  2850. "x%x x%x x%x x%x "
  2851. "x%x x%x x%x x%x\n",
  2852. pring->ringno,
  2853. irsp->un.ulpWord[0],
  2854. irsp->un.ulpWord[1],
  2855. irsp->un.ulpWord[2],
  2856. irsp->un.ulpWord[3],
  2857. irsp->un.ulpWord[4],
  2858. irsp->un.ulpWord[5],
  2859. *(((uint32_t *) irsp) + 6),
  2860. *(((uint32_t *) irsp) + 7),
  2861. *(((uint32_t *) irsp) + 8),
  2862. *(((uint32_t *) irsp) + 9),
  2863. *(((uint32_t *) irsp) + 10),
  2864. *(((uint32_t *) irsp) + 11),
  2865. *(((uint32_t *) irsp) + 12),
  2866. *(((uint32_t *) irsp) + 13),
  2867. *(((uint32_t *) irsp) + 14),
  2868. *(((uint32_t *) irsp) + 15));
  2869. }
  2870. /*
  2871. * Fetch the IOCB command type and call the correct completion
  2872. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2873. * get freed back to the lpfc_iocb_list by the discovery
  2874. * kernel thread.
  2875. */
  2876. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2877. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2878. switch (type) {
  2879. case LPFC_SOL_IOCB:
  2880. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2881. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2882. spin_lock_irqsave(&phba->hbalock, iflag);
  2883. break;
  2884. case LPFC_UNSOL_IOCB:
  2885. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2886. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2887. spin_lock_irqsave(&phba->hbalock, iflag);
  2888. if (!rc)
  2889. free_saveq = 0;
  2890. break;
  2891. case LPFC_ABORT_IOCB:
  2892. cmdiocbp = NULL;
  2893. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2894. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2895. saveq);
  2896. if (cmdiocbp) {
  2897. /* Call the specified completion routine */
  2898. if (cmdiocbp->iocb_cmpl) {
  2899. spin_unlock_irqrestore(&phba->hbalock,
  2900. iflag);
  2901. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2902. saveq);
  2903. spin_lock_irqsave(&phba->hbalock,
  2904. iflag);
  2905. } else
  2906. __lpfc_sli_release_iocbq(phba,
  2907. cmdiocbp);
  2908. }
  2909. break;
  2910. case LPFC_UNKNOWN_IOCB:
  2911. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2912. char adaptermsg[LPFC_MAX_ADPTMSG];
  2913. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2914. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2915. MAX_MSG_DATA);
  2916. dev_warn(&((phba->pcidev)->dev),
  2917. "lpfc%d: %s\n",
  2918. phba->brd_no, adaptermsg);
  2919. } else {
  2920. /* Unknown IOCB command */
  2921. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2922. "0335 Unknown IOCB "
  2923. "command Data: x%x "
  2924. "x%x x%x x%x\n",
  2925. irsp->ulpCommand,
  2926. irsp->ulpStatus,
  2927. irsp->ulpIoTag,
  2928. irsp->ulpContext);
  2929. }
  2930. break;
  2931. }
  2932. if (free_saveq) {
  2933. list_for_each_entry_safe(rspiocbp, next_iocb,
  2934. &saveq->list, list) {
  2935. list_del(&rspiocbp->list);
  2936. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2937. }
  2938. __lpfc_sli_release_iocbq(phba, saveq);
  2939. }
  2940. rspiocbp = NULL;
  2941. }
  2942. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2943. return rspiocbp;
  2944. }
  2945. /**
  2946. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2947. * @phba: Pointer to HBA context object.
  2948. * @pring: Pointer to driver SLI ring object.
  2949. * @mask: Host attention register mask for this ring.
  2950. *
  2951. * This routine wraps the actual slow_ring event process routine from the
  2952. * API jump table function pointer from the lpfc_hba struct.
  2953. **/
  2954. void
  2955. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2956. struct lpfc_sli_ring *pring, uint32_t mask)
  2957. {
  2958. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2959. }
  2960. /**
  2961. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2962. * @phba: Pointer to HBA context object.
  2963. * @pring: Pointer to driver SLI ring object.
  2964. * @mask: Host attention register mask for this ring.
  2965. *
  2966. * This function is called from the worker thread when there is a ring event
  2967. * for non-fcp rings. The caller does not hold any lock. The function will
  2968. * remove each response iocb in the response ring and calls the handle
  2969. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2970. **/
  2971. static void
  2972. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2973. struct lpfc_sli_ring *pring, uint32_t mask)
  2974. {
  2975. struct lpfc_pgp *pgp;
  2976. IOCB_t *entry;
  2977. IOCB_t *irsp = NULL;
  2978. struct lpfc_iocbq *rspiocbp = NULL;
  2979. uint32_t portRspPut, portRspMax;
  2980. unsigned long iflag;
  2981. uint32_t status;
  2982. pgp = &phba->port_gp[pring->ringno];
  2983. spin_lock_irqsave(&phba->hbalock, iflag);
  2984. pring->stats.iocb_event++;
  2985. /*
  2986. * The next available response entry should never exceed the maximum
  2987. * entries. If it does, treat it as an adapter hardware error.
  2988. */
  2989. portRspMax = pring->sli.sli3.numRiocb;
  2990. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2991. if (portRspPut >= portRspMax) {
  2992. /*
  2993. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2994. * rsp ring <portRspMax>
  2995. */
  2996. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2997. "0303 Ring %d handler: portRspPut %d "
  2998. "is bigger than rsp ring %d\n",
  2999. pring->ringno, portRspPut, portRspMax);
  3000. phba->link_state = LPFC_HBA_ERROR;
  3001. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3002. phba->work_hs = HS_FFER3;
  3003. lpfc_handle_eratt(phba);
  3004. return;
  3005. }
  3006. rmb();
  3007. while (pring->sli.sli3.rspidx != portRspPut) {
  3008. /*
  3009. * Build a completion list and call the appropriate handler.
  3010. * The process is to get the next available response iocb, get
  3011. * a free iocb from the list, copy the response data into the
  3012. * free iocb, insert to the continuation list, and update the
  3013. * next response index to slim. This process makes response
  3014. * iocb's in the ring available to DMA as fast as possible but
  3015. * pays a penalty for a copy operation. Since the iocb is
  3016. * only 32 bytes, this penalty is considered small relative to
  3017. * the PCI reads for register values and a slim write. When
  3018. * the ulpLe field is set, the entire Command has been
  3019. * received.
  3020. */
  3021. entry = lpfc_resp_iocb(phba, pring);
  3022. phba->last_completion_time = jiffies;
  3023. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3024. if (rspiocbp == NULL) {
  3025. printk(KERN_ERR "%s: out of buffers! Failing "
  3026. "completion.\n", __func__);
  3027. break;
  3028. }
  3029. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3030. phba->iocb_rsp_size);
  3031. irsp = &rspiocbp->iocb;
  3032. if (++pring->sli.sli3.rspidx >= portRspMax)
  3033. pring->sli.sli3.rspidx = 0;
  3034. if (pring->ringno == LPFC_ELS_RING) {
  3035. lpfc_debugfs_slow_ring_trc(phba,
  3036. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3037. *(((uint32_t *) irsp) + 4),
  3038. *(((uint32_t *) irsp) + 6),
  3039. *(((uint32_t *) irsp) + 7));
  3040. }
  3041. writel(pring->sli.sli3.rspidx,
  3042. &phba->host_gp[pring->ringno].rspGetInx);
  3043. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3044. /* Handle the response IOCB */
  3045. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3046. spin_lock_irqsave(&phba->hbalock, iflag);
  3047. /*
  3048. * If the port response put pointer has not been updated, sync
  3049. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3050. * response put pointer.
  3051. */
  3052. if (pring->sli.sli3.rspidx == portRspPut) {
  3053. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3054. }
  3055. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3056. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3057. /* At least one response entry has been freed */
  3058. pring->stats.iocb_rsp_full++;
  3059. /* SET RxRE_RSP in Chip Att register */
  3060. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3061. writel(status, phba->CAregaddr);
  3062. readl(phba->CAregaddr); /* flush */
  3063. }
  3064. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3065. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3066. pring->stats.iocb_cmd_empty++;
  3067. /* Force update of the local copy of cmdGetInx */
  3068. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3069. lpfc_sli_resume_iocb(phba, pring);
  3070. if ((pring->lpfc_sli_cmd_available))
  3071. (pring->lpfc_sli_cmd_available) (phba, pring);
  3072. }
  3073. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3074. return;
  3075. }
  3076. /**
  3077. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3078. * @phba: Pointer to HBA context object.
  3079. * @pring: Pointer to driver SLI ring object.
  3080. * @mask: Host attention register mask for this ring.
  3081. *
  3082. * This function is called from the worker thread when there is a pending
  3083. * ELS response iocb on the driver internal slow-path response iocb worker
  3084. * queue. The caller does not hold any lock. The function will remove each
  3085. * response iocb from the response worker queue and calls the handle
  3086. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3087. **/
  3088. static void
  3089. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3090. struct lpfc_sli_ring *pring, uint32_t mask)
  3091. {
  3092. struct lpfc_iocbq *irspiocbq;
  3093. struct hbq_dmabuf *dmabuf;
  3094. struct lpfc_cq_event *cq_event;
  3095. unsigned long iflag;
  3096. spin_lock_irqsave(&phba->hbalock, iflag);
  3097. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3098. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3099. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3100. /* Get the response iocb from the head of work queue */
  3101. spin_lock_irqsave(&phba->hbalock, iflag);
  3102. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3103. cq_event, struct lpfc_cq_event, list);
  3104. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3105. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3106. case CQE_CODE_COMPL_WQE:
  3107. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3108. cq_event);
  3109. /* Translate ELS WCQE to response IOCBQ */
  3110. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3111. irspiocbq);
  3112. if (irspiocbq)
  3113. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3114. irspiocbq);
  3115. break;
  3116. case CQE_CODE_RECEIVE:
  3117. case CQE_CODE_RECEIVE_V1:
  3118. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3119. cq_event);
  3120. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3121. break;
  3122. default:
  3123. break;
  3124. }
  3125. }
  3126. }
  3127. /**
  3128. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3129. * @phba: Pointer to HBA context object.
  3130. * @pring: Pointer to driver SLI ring object.
  3131. *
  3132. * This function aborts all iocbs in the given ring and frees all the iocb
  3133. * objects in txq. This function issues an abort iocb for all the iocb commands
  3134. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3135. * the return of this function. The caller is not required to hold any locks.
  3136. **/
  3137. void
  3138. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3139. {
  3140. LIST_HEAD(completions);
  3141. struct lpfc_iocbq *iocb, *next_iocb;
  3142. if (pring->ringno == LPFC_ELS_RING) {
  3143. lpfc_fabric_abort_hba(phba);
  3144. }
  3145. /* Error everything on txq and txcmplq
  3146. * First do the txq.
  3147. */
  3148. spin_lock_irq(&phba->hbalock);
  3149. list_splice_init(&pring->txq, &completions);
  3150. pring->txq_cnt = 0;
  3151. /* Next issue ABTS for everything on the txcmplq */
  3152. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3153. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3154. spin_unlock_irq(&phba->hbalock);
  3155. /* Cancel all the IOCBs from the completions list */
  3156. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3157. IOERR_SLI_ABORTED);
  3158. }
  3159. /**
  3160. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3161. * @phba: Pointer to HBA context object.
  3162. *
  3163. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3164. * objects in txq and txcmplq. This function will not issue abort iocbs
  3165. * for all the iocb commands in txcmplq, they will just be returned with
  3166. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3167. * slot has been permanently disabled.
  3168. **/
  3169. void
  3170. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3171. {
  3172. LIST_HEAD(txq);
  3173. LIST_HEAD(txcmplq);
  3174. struct lpfc_sli *psli = &phba->sli;
  3175. struct lpfc_sli_ring *pring;
  3176. /* Currently, only one fcp ring */
  3177. pring = &psli->ring[psli->fcp_ring];
  3178. spin_lock_irq(&phba->hbalock);
  3179. /* Retrieve everything on txq */
  3180. list_splice_init(&pring->txq, &txq);
  3181. pring->txq_cnt = 0;
  3182. /* Retrieve everything on the txcmplq */
  3183. list_splice_init(&pring->txcmplq, &txcmplq);
  3184. pring->txcmplq_cnt = 0;
  3185. /* Indicate the I/O queues are flushed */
  3186. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3187. spin_unlock_irq(&phba->hbalock);
  3188. /* Flush the txq */
  3189. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3190. IOERR_SLI_DOWN);
  3191. /* Flush the txcmpq */
  3192. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3193. IOERR_SLI_DOWN);
  3194. }
  3195. /**
  3196. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3197. * @phba: Pointer to HBA context object.
  3198. * @mask: Bit mask to be checked.
  3199. *
  3200. * This function reads the host status register and compares
  3201. * with the provided bit mask to check if HBA completed
  3202. * the restart. This function will wait in a loop for the
  3203. * HBA to complete restart. If the HBA does not restart within
  3204. * 15 iterations, the function will reset the HBA again. The
  3205. * function returns 1 when HBA fail to restart otherwise returns
  3206. * zero.
  3207. **/
  3208. static int
  3209. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3210. {
  3211. uint32_t status;
  3212. int i = 0;
  3213. int retval = 0;
  3214. /* Read the HBA Host Status Register */
  3215. if (lpfc_readl(phba->HSregaddr, &status))
  3216. return 1;
  3217. /*
  3218. * Check status register every 100ms for 5 retries, then every
  3219. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3220. * every 2.5 sec for 4.
  3221. * Break our of the loop if errors occurred during init.
  3222. */
  3223. while (((status & mask) != mask) &&
  3224. !(status & HS_FFERM) &&
  3225. i++ < 20) {
  3226. if (i <= 5)
  3227. msleep(10);
  3228. else if (i <= 10)
  3229. msleep(500);
  3230. else
  3231. msleep(2500);
  3232. if (i == 15) {
  3233. /* Do post */
  3234. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3235. lpfc_sli_brdrestart(phba);
  3236. }
  3237. /* Read the HBA Host Status Register */
  3238. if (lpfc_readl(phba->HSregaddr, &status)) {
  3239. retval = 1;
  3240. break;
  3241. }
  3242. }
  3243. /* Check to see if any errors occurred during init */
  3244. if ((status & HS_FFERM) || (i >= 20)) {
  3245. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3246. "2751 Adapter failed to restart, "
  3247. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3248. status,
  3249. readl(phba->MBslimaddr + 0xa8),
  3250. readl(phba->MBslimaddr + 0xac));
  3251. phba->link_state = LPFC_HBA_ERROR;
  3252. retval = 1;
  3253. }
  3254. return retval;
  3255. }
  3256. /**
  3257. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3258. * @phba: Pointer to HBA context object.
  3259. * @mask: Bit mask to be checked.
  3260. *
  3261. * This function checks the host status register to check if HBA is
  3262. * ready. This function will wait in a loop for the HBA to be ready
  3263. * If the HBA is not ready , the function will will reset the HBA PCI
  3264. * function again. The function returns 1 when HBA fail to be ready
  3265. * otherwise returns zero.
  3266. **/
  3267. static int
  3268. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3269. {
  3270. uint32_t status;
  3271. int retval = 0;
  3272. /* Read the HBA Host Status Register */
  3273. status = lpfc_sli4_post_status_check(phba);
  3274. if (status) {
  3275. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3276. lpfc_sli_brdrestart(phba);
  3277. status = lpfc_sli4_post_status_check(phba);
  3278. }
  3279. /* Check to see if any errors occurred during init */
  3280. if (status) {
  3281. phba->link_state = LPFC_HBA_ERROR;
  3282. retval = 1;
  3283. } else
  3284. phba->sli4_hba.intr_enable = 0;
  3285. return retval;
  3286. }
  3287. /**
  3288. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3289. * @phba: Pointer to HBA context object.
  3290. * @mask: Bit mask to be checked.
  3291. *
  3292. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3293. * from the API jump table function pointer from the lpfc_hba struct.
  3294. **/
  3295. int
  3296. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3297. {
  3298. return phba->lpfc_sli_brdready(phba, mask);
  3299. }
  3300. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3301. /**
  3302. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3303. * @phba: Pointer to HBA context object.
  3304. *
  3305. * This function is called before resetting an HBA. This function is called
  3306. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3307. **/
  3308. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3309. {
  3310. uint32_t __iomem *resp_buf;
  3311. uint32_t __iomem *mbox_buf;
  3312. volatile uint32_t mbox;
  3313. uint32_t hc_copy, ha_copy, resp_data;
  3314. int i;
  3315. uint8_t hdrtype;
  3316. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3317. if (hdrtype != 0x80 ||
  3318. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3319. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3320. return;
  3321. /*
  3322. * Tell the other part of the chip to suspend temporarily all
  3323. * its DMA activity.
  3324. */
  3325. resp_buf = phba->MBslimaddr;
  3326. /* Disable the error attention */
  3327. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3328. return;
  3329. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3330. readl(phba->HCregaddr); /* flush */
  3331. phba->link_flag |= LS_IGNORE_ERATT;
  3332. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3333. return;
  3334. if (ha_copy & HA_ERATT) {
  3335. /* Clear Chip error bit */
  3336. writel(HA_ERATT, phba->HAregaddr);
  3337. phba->pport->stopped = 1;
  3338. }
  3339. mbox = 0;
  3340. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3341. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3342. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3343. mbox_buf = phba->MBslimaddr;
  3344. writel(mbox, mbox_buf);
  3345. for (i = 0; i < 50; i++) {
  3346. if (lpfc_readl((resp_buf + 1), &resp_data))
  3347. return;
  3348. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3349. mdelay(1);
  3350. else
  3351. break;
  3352. }
  3353. resp_data = 0;
  3354. if (lpfc_readl((resp_buf + 1), &resp_data))
  3355. return;
  3356. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3357. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3358. phba->pport->stopped)
  3359. goto restore_hc;
  3360. else
  3361. goto clear_errat;
  3362. }
  3363. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3364. resp_data = 0;
  3365. for (i = 0; i < 500; i++) {
  3366. if (lpfc_readl(resp_buf, &resp_data))
  3367. return;
  3368. if (resp_data != mbox)
  3369. mdelay(1);
  3370. else
  3371. break;
  3372. }
  3373. clear_errat:
  3374. while (++i < 500) {
  3375. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3376. return;
  3377. if (!(ha_copy & HA_ERATT))
  3378. mdelay(1);
  3379. else
  3380. break;
  3381. }
  3382. if (readl(phba->HAregaddr) & HA_ERATT) {
  3383. writel(HA_ERATT, phba->HAregaddr);
  3384. phba->pport->stopped = 1;
  3385. }
  3386. restore_hc:
  3387. phba->link_flag &= ~LS_IGNORE_ERATT;
  3388. writel(hc_copy, phba->HCregaddr);
  3389. readl(phba->HCregaddr); /* flush */
  3390. }
  3391. /**
  3392. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3393. * @phba: Pointer to HBA context object.
  3394. *
  3395. * This function issues a kill_board mailbox command and waits for
  3396. * the error attention interrupt. This function is called for stopping
  3397. * the firmware processing. The caller is not required to hold any
  3398. * locks. This function calls lpfc_hba_down_post function to free
  3399. * any pending commands after the kill. The function will return 1 when it
  3400. * fails to kill the board else will return 0.
  3401. **/
  3402. int
  3403. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3404. {
  3405. struct lpfc_sli *psli;
  3406. LPFC_MBOXQ_t *pmb;
  3407. uint32_t status;
  3408. uint32_t ha_copy;
  3409. int retval;
  3410. int i = 0;
  3411. psli = &phba->sli;
  3412. /* Kill HBA */
  3413. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3414. "0329 Kill HBA Data: x%x x%x\n",
  3415. phba->pport->port_state, psli->sli_flag);
  3416. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3417. if (!pmb)
  3418. return 1;
  3419. /* Disable the error attention */
  3420. spin_lock_irq(&phba->hbalock);
  3421. if (lpfc_readl(phba->HCregaddr, &status)) {
  3422. spin_unlock_irq(&phba->hbalock);
  3423. mempool_free(pmb, phba->mbox_mem_pool);
  3424. return 1;
  3425. }
  3426. status &= ~HC_ERINT_ENA;
  3427. writel(status, phba->HCregaddr);
  3428. readl(phba->HCregaddr); /* flush */
  3429. phba->link_flag |= LS_IGNORE_ERATT;
  3430. spin_unlock_irq(&phba->hbalock);
  3431. lpfc_kill_board(phba, pmb);
  3432. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3433. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3434. if (retval != MBX_SUCCESS) {
  3435. if (retval != MBX_BUSY)
  3436. mempool_free(pmb, phba->mbox_mem_pool);
  3437. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3438. "2752 KILL_BOARD command failed retval %d\n",
  3439. retval);
  3440. spin_lock_irq(&phba->hbalock);
  3441. phba->link_flag &= ~LS_IGNORE_ERATT;
  3442. spin_unlock_irq(&phba->hbalock);
  3443. return 1;
  3444. }
  3445. spin_lock_irq(&phba->hbalock);
  3446. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3447. spin_unlock_irq(&phba->hbalock);
  3448. mempool_free(pmb, phba->mbox_mem_pool);
  3449. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3450. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3451. * 3 seconds we still set HBA_ERROR state because the status of the
  3452. * board is now undefined.
  3453. */
  3454. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3455. return 1;
  3456. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3457. mdelay(100);
  3458. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3459. return 1;
  3460. }
  3461. del_timer_sync(&psli->mbox_tmo);
  3462. if (ha_copy & HA_ERATT) {
  3463. writel(HA_ERATT, phba->HAregaddr);
  3464. phba->pport->stopped = 1;
  3465. }
  3466. spin_lock_irq(&phba->hbalock);
  3467. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3468. psli->mbox_active = NULL;
  3469. phba->link_flag &= ~LS_IGNORE_ERATT;
  3470. spin_unlock_irq(&phba->hbalock);
  3471. lpfc_hba_down_post(phba);
  3472. phba->link_state = LPFC_HBA_ERROR;
  3473. return ha_copy & HA_ERATT ? 0 : 1;
  3474. }
  3475. /**
  3476. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3477. * @phba: Pointer to HBA context object.
  3478. *
  3479. * This function resets the HBA by writing HC_INITFF to the control
  3480. * register. After the HBA resets, this function resets all the iocb ring
  3481. * indices. This function disables PCI layer parity checking during
  3482. * the reset.
  3483. * This function returns 0 always.
  3484. * The caller is not required to hold any locks.
  3485. **/
  3486. int
  3487. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3488. {
  3489. struct lpfc_sli *psli;
  3490. struct lpfc_sli_ring *pring;
  3491. uint16_t cfg_value;
  3492. int i;
  3493. psli = &phba->sli;
  3494. /* Reset HBA */
  3495. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3496. "0325 Reset HBA Data: x%x x%x\n",
  3497. phba->pport->port_state, psli->sli_flag);
  3498. /* perform board reset */
  3499. phba->fc_eventTag = 0;
  3500. phba->link_events = 0;
  3501. phba->pport->fc_myDID = 0;
  3502. phba->pport->fc_prevDID = 0;
  3503. /* Turn off parity checking and serr during the physical reset */
  3504. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3505. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3506. (cfg_value &
  3507. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3508. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3509. /* Now toggle INITFF bit in the Host Control Register */
  3510. writel(HC_INITFF, phba->HCregaddr);
  3511. mdelay(1);
  3512. readl(phba->HCregaddr); /* flush */
  3513. writel(0, phba->HCregaddr);
  3514. readl(phba->HCregaddr); /* flush */
  3515. /* Restore PCI cmd register */
  3516. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3517. /* Initialize relevant SLI info */
  3518. for (i = 0; i < psli->num_rings; i++) {
  3519. pring = &psli->ring[i];
  3520. pring->flag = 0;
  3521. pring->sli.sli3.rspidx = 0;
  3522. pring->sli.sli3.next_cmdidx = 0;
  3523. pring->sli.sli3.local_getidx = 0;
  3524. pring->sli.sli3.cmdidx = 0;
  3525. pring->missbufcnt = 0;
  3526. }
  3527. phba->link_state = LPFC_WARM_START;
  3528. return 0;
  3529. }
  3530. /**
  3531. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3532. * @phba: Pointer to HBA context object.
  3533. *
  3534. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3535. * checking during resets the device. The caller is not required to hold
  3536. * any locks.
  3537. *
  3538. * This function returns 0 always.
  3539. **/
  3540. int
  3541. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3542. {
  3543. struct lpfc_sli *psli = &phba->sli;
  3544. uint16_t cfg_value;
  3545. int rc;
  3546. /* Reset HBA */
  3547. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3548. "0295 Reset HBA Data: x%x x%x\n",
  3549. phba->pport->port_state, psli->sli_flag);
  3550. /* perform board reset */
  3551. phba->fc_eventTag = 0;
  3552. phba->link_events = 0;
  3553. phba->pport->fc_myDID = 0;
  3554. phba->pport->fc_prevDID = 0;
  3555. spin_lock_irq(&phba->hbalock);
  3556. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3557. phba->fcf.fcf_flag = 0;
  3558. spin_unlock_irq(&phba->hbalock);
  3559. /* Now physically reset the device */
  3560. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3561. "0389 Performing PCI function reset!\n");
  3562. /* Turn off parity checking and serr during the physical reset */
  3563. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3564. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3565. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3566. /* Perform FCoE PCI function reset */
  3567. lpfc_sli4_queue_destroy(phba);
  3568. rc = lpfc_pci_function_reset(phba);
  3569. /* Restore PCI cmd register */
  3570. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3571. return rc;
  3572. }
  3573. /**
  3574. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3575. * @phba: Pointer to HBA context object.
  3576. *
  3577. * This function is called in the SLI initialization code path to
  3578. * restart the HBA. The caller is not required to hold any lock.
  3579. * This function writes MBX_RESTART mailbox command to the SLIM and
  3580. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3581. * function to free any pending commands. The function enables
  3582. * POST only during the first initialization. The function returns zero.
  3583. * The function does not guarantee completion of MBX_RESTART mailbox
  3584. * command before the return of this function.
  3585. **/
  3586. static int
  3587. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3588. {
  3589. MAILBOX_t *mb;
  3590. struct lpfc_sli *psli;
  3591. volatile uint32_t word0;
  3592. void __iomem *to_slim;
  3593. uint32_t hba_aer_enabled;
  3594. spin_lock_irq(&phba->hbalock);
  3595. /* Take PCIe device Advanced Error Reporting (AER) state */
  3596. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3597. psli = &phba->sli;
  3598. /* Restart HBA */
  3599. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3600. "0337 Restart HBA Data: x%x x%x\n",
  3601. phba->pport->port_state, psli->sli_flag);
  3602. word0 = 0;
  3603. mb = (MAILBOX_t *) &word0;
  3604. mb->mbxCommand = MBX_RESTART;
  3605. mb->mbxHc = 1;
  3606. lpfc_reset_barrier(phba);
  3607. to_slim = phba->MBslimaddr;
  3608. writel(*(uint32_t *) mb, to_slim);
  3609. readl(to_slim); /* flush */
  3610. /* Only skip post after fc_ffinit is completed */
  3611. if (phba->pport->port_state)
  3612. word0 = 1; /* This is really setting up word1 */
  3613. else
  3614. word0 = 0; /* This is really setting up word1 */
  3615. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3616. writel(*(uint32_t *) mb, to_slim);
  3617. readl(to_slim); /* flush */
  3618. lpfc_sli_brdreset(phba);
  3619. phba->pport->stopped = 0;
  3620. phba->link_state = LPFC_INIT_START;
  3621. phba->hba_flag = 0;
  3622. spin_unlock_irq(&phba->hbalock);
  3623. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3624. psli->stats_start = get_seconds();
  3625. /* Give the INITFF and Post time to settle. */
  3626. mdelay(100);
  3627. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3628. if (hba_aer_enabled)
  3629. pci_disable_pcie_error_reporting(phba->pcidev);
  3630. lpfc_hba_down_post(phba);
  3631. return 0;
  3632. }
  3633. /**
  3634. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3635. * @phba: Pointer to HBA context object.
  3636. *
  3637. * This function is called in the SLI initialization code path to restart
  3638. * a SLI4 HBA. The caller is not required to hold any lock.
  3639. * At the end of the function, it calls lpfc_hba_down_post function to
  3640. * free any pending commands.
  3641. **/
  3642. static int
  3643. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3644. {
  3645. struct lpfc_sli *psli = &phba->sli;
  3646. uint32_t hba_aer_enabled;
  3647. int rc;
  3648. /* Restart HBA */
  3649. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3650. "0296 Restart HBA Data: x%x x%x\n",
  3651. phba->pport->port_state, psli->sli_flag);
  3652. /* Take PCIe device Advanced Error Reporting (AER) state */
  3653. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3654. rc = lpfc_sli4_brdreset(phba);
  3655. spin_lock_irq(&phba->hbalock);
  3656. phba->pport->stopped = 0;
  3657. phba->link_state = LPFC_INIT_START;
  3658. phba->hba_flag = 0;
  3659. spin_unlock_irq(&phba->hbalock);
  3660. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3661. psli->stats_start = get_seconds();
  3662. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3663. if (hba_aer_enabled)
  3664. pci_disable_pcie_error_reporting(phba->pcidev);
  3665. lpfc_hba_down_post(phba);
  3666. return rc;
  3667. }
  3668. /**
  3669. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3670. * @phba: Pointer to HBA context object.
  3671. *
  3672. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3673. * API jump table function pointer from the lpfc_hba struct.
  3674. **/
  3675. int
  3676. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3677. {
  3678. return phba->lpfc_sli_brdrestart(phba);
  3679. }
  3680. /**
  3681. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3682. * @phba: Pointer to HBA context object.
  3683. *
  3684. * This function is called after a HBA restart to wait for successful
  3685. * restart of the HBA. Successful restart of the HBA is indicated by
  3686. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3687. * iteration, the function will restart the HBA again. The function returns
  3688. * zero if HBA successfully restarted else returns negative error code.
  3689. **/
  3690. static int
  3691. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3692. {
  3693. uint32_t status, i = 0;
  3694. /* Read the HBA Host Status Register */
  3695. if (lpfc_readl(phba->HSregaddr, &status))
  3696. return -EIO;
  3697. /* Check status register to see what current state is */
  3698. i = 0;
  3699. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3700. /* Check every 10ms for 10 retries, then every 100ms for 90
  3701. * retries, then every 1 sec for 50 retires for a total of
  3702. * ~60 seconds before reset the board again and check every
  3703. * 1 sec for 50 retries. The up to 60 seconds before the
  3704. * board ready is required by the Falcon FIPS zeroization
  3705. * complete, and any reset the board in between shall cause
  3706. * restart of zeroization, further delay the board ready.
  3707. */
  3708. if (i++ >= 200) {
  3709. /* Adapter failed to init, timeout, status reg
  3710. <status> */
  3711. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3712. "0436 Adapter failed to init, "
  3713. "timeout, status reg x%x, "
  3714. "FW Data: A8 x%x AC x%x\n", status,
  3715. readl(phba->MBslimaddr + 0xa8),
  3716. readl(phba->MBslimaddr + 0xac));
  3717. phba->link_state = LPFC_HBA_ERROR;
  3718. return -ETIMEDOUT;
  3719. }
  3720. /* Check to see if any errors occurred during init */
  3721. if (status & HS_FFERM) {
  3722. /* ERROR: During chipset initialization */
  3723. /* Adapter failed to init, chipset, status reg
  3724. <status> */
  3725. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3726. "0437 Adapter failed to init, "
  3727. "chipset, status reg x%x, "
  3728. "FW Data: A8 x%x AC x%x\n", status,
  3729. readl(phba->MBslimaddr + 0xa8),
  3730. readl(phba->MBslimaddr + 0xac));
  3731. phba->link_state = LPFC_HBA_ERROR;
  3732. return -EIO;
  3733. }
  3734. if (i <= 10)
  3735. msleep(10);
  3736. else if (i <= 100)
  3737. msleep(100);
  3738. else
  3739. msleep(1000);
  3740. if (i == 150) {
  3741. /* Do post */
  3742. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3743. lpfc_sli_brdrestart(phba);
  3744. }
  3745. /* Read the HBA Host Status Register */
  3746. if (lpfc_readl(phba->HSregaddr, &status))
  3747. return -EIO;
  3748. }
  3749. /* Check to see if any errors occurred during init */
  3750. if (status & HS_FFERM) {
  3751. /* ERROR: During chipset initialization */
  3752. /* Adapter failed to init, chipset, status reg <status> */
  3753. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3754. "0438 Adapter failed to init, chipset, "
  3755. "status reg x%x, "
  3756. "FW Data: A8 x%x AC x%x\n", status,
  3757. readl(phba->MBslimaddr + 0xa8),
  3758. readl(phba->MBslimaddr + 0xac));
  3759. phba->link_state = LPFC_HBA_ERROR;
  3760. return -EIO;
  3761. }
  3762. /* Clear all interrupt enable conditions */
  3763. writel(0, phba->HCregaddr);
  3764. readl(phba->HCregaddr); /* flush */
  3765. /* setup host attn register */
  3766. writel(0xffffffff, phba->HAregaddr);
  3767. readl(phba->HAregaddr); /* flush */
  3768. return 0;
  3769. }
  3770. /**
  3771. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3772. *
  3773. * This function calculates and returns the number of HBQs required to be
  3774. * configured.
  3775. **/
  3776. int
  3777. lpfc_sli_hbq_count(void)
  3778. {
  3779. return ARRAY_SIZE(lpfc_hbq_defs);
  3780. }
  3781. /**
  3782. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3783. *
  3784. * This function adds the number of hbq entries in every HBQ to get
  3785. * the total number of hbq entries required for the HBA and returns
  3786. * the total count.
  3787. **/
  3788. static int
  3789. lpfc_sli_hbq_entry_count(void)
  3790. {
  3791. int hbq_count = lpfc_sli_hbq_count();
  3792. int count = 0;
  3793. int i;
  3794. for (i = 0; i < hbq_count; ++i)
  3795. count += lpfc_hbq_defs[i]->entry_count;
  3796. return count;
  3797. }
  3798. /**
  3799. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3800. *
  3801. * This function calculates amount of memory required for all hbq entries
  3802. * to be configured and returns the total memory required.
  3803. **/
  3804. int
  3805. lpfc_sli_hbq_size(void)
  3806. {
  3807. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3808. }
  3809. /**
  3810. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3811. * @phba: Pointer to HBA context object.
  3812. *
  3813. * This function is called during the SLI initialization to configure
  3814. * all the HBQs and post buffers to the HBQ. The caller is not
  3815. * required to hold any locks. This function will return zero if successful
  3816. * else it will return negative error code.
  3817. **/
  3818. static int
  3819. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3820. {
  3821. int hbq_count = lpfc_sli_hbq_count();
  3822. LPFC_MBOXQ_t *pmb;
  3823. MAILBOX_t *pmbox;
  3824. uint32_t hbqno;
  3825. uint32_t hbq_entry_index;
  3826. /* Get a Mailbox buffer to setup mailbox
  3827. * commands for HBA initialization
  3828. */
  3829. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3830. if (!pmb)
  3831. return -ENOMEM;
  3832. pmbox = &pmb->u.mb;
  3833. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3834. phba->link_state = LPFC_INIT_MBX_CMDS;
  3835. phba->hbq_in_use = 1;
  3836. hbq_entry_index = 0;
  3837. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3838. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3839. phba->hbqs[hbqno].hbqPutIdx = 0;
  3840. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3841. phba->hbqs[hbqno].entry_count =
  3842. lpfc_hbq_defs[hbqno]->entry_count;
  3843. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3844. hbq_entry_index, pmb);
  3845. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3846. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3847. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3848. mbxStatus <status>, ring <num> */
  3849. lpfc_printf_log(phba, KERN_ERR,
  3850. LOG_SLI | LOG_VPORT,
  3851. "1805 Adapter failed to init. "
  3852. "Data: x%x x%x x%x\n",
  3853. pmbox->mbxCommand,
  3854. pmbox->mbxStatus, hbqno);
  3855. phba->link_state = LPFC_HBA_ERROR;
  3856. mempool_free(pmb, phba->mbox_mem_pool);
  3857. return -ENXIO;
  3858. }
  3859. }
  3860. phba->hbq_count = hbq_count;
  3861. mempool_free(pmb, phba->mbox_mem_pool);
  3862. /* Initially populate or replenish the HBQs */
  3863. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3864. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3865. return 0;
  3866. }
  3867. /**
  3868. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3869. * @phba: Pointer to HBA context object.
  3870. *
  3871. * This function is called during the SLI initialization to configure
  3872. * all the HBQs and post buffers to the HBQ. The caller is not
  3873. * required to hold any locks. This function will return zero if successful
  3874. * else it will return negative error code.
  3875. **/
  3876. static int
  3877. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3878. {
  3879. phba->hbq_in_use = 1;
  3880. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3881. phba->hbq_count = 1;
  3882. /* Initially populate or replenish the HBQs */
  3883. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3884. return 0;
  3885. }
  3886. /**
  3887. * lpfc_sli_config_port - Issue config port mailbox command
  3888. * @phba: Pointer to HBA context object.
  3889. * @sli_mode: sli mode - 2/3
  3890. *
  3891. * This function is called by the sli intialization code path
  3892. * to issue config_port mailbox command. This function restarts the
  3893. * HBA firmware and issues a config_port mailbox command to configure
  3894. * the SLI interface in the sli mode specified by sli_mode
  3895. * variable. The caller is not required to hold any locks.
  3896. * The function returns 0 if successful, else returns negative error
  3897. * code.
  3898. **/
  3899. int
  3900. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3901. {
  3902. LPFC_MBOXQ_t *pmb;
  3903. uint32_t resetcount = 0, rc = 0, done = 0;
  3904. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3905. if (!pmb) {
  3906. phba->link_state = LPFC_HBA_ERROR;
  3907. return -ENOMEM;
  3908. }
  3909. phba->sli_rev = sli_mode;
  3910. while (resetcount < 2 && !done) {
  3911. spin_lock_irq(&phba->hbalock);
  3912. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3913. spin_unlock_irq(&phba->hbalock);
  3914. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3915. lpfc_sli_brdrestart(phba);
  3916. rc = lpfc_sli_chipset_init(phba);
  3917. if (rc)
  3918. break;
  3919. spin_lock_irq(&phba->hbalock);
  3920. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3921. spin_unlock_irq(&phba->hbalock);
  3922. resetcount++;
  3923. /* Call pre CONFIG_PORT mailbox command initialization. A
  3924. * value of 0 means the call was successful. Any other
  3925. * nonzero value is a failure, but if ERESTART is returned,
  3926. * the driver may reset the HBA and try again.
  3927. */
  3928. rc = lpfc_config_port_prep(phba);
  3929. if (rc == -ERESTART) {
  3930. phba->link_state = LPFC_LINK_UNKNOWN;
  3931. continue;
  3932. } else if (rc)
  3933. break;
  3934. phba->link_state = LPFC_INIT_MBX_CMDS;
  3935. lpfc_config_port(phba, pmb);
  3936. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3937. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3938. LPFC_SLI3_HBQ_ENABLED |
  3939. LPFC_SLI3_CRP_ENABLED |
  3940. LPFC_SLI3_BG_ENABLED |
  3941. LPFC_SLI3_DSS_ENABLED);
  3942. if (rc != MBX_SUCCESS) {
  3943. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3944. "0442 Adapter failed to init, mbxCmd x%x "
  3945. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3946. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3947. spin_lock_irq(&phba->hbalock);
  3948. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3949. spin_unlock_irq(&phba->hbalock);
  3950. rc = -ENXIO;
  3951. } else {
  3952. /* Allow asynchronous mailbox command to go through */
  3953. spin_lock_irq(&phba->hbalock);
  3954. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3955. spin_unlock_irq(&phba->hbalock);
  3956. done = 1;
  3957. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3958. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3959. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3960. "3110 Port did not grant ASABT\n");
  3961. }
  3962. }
  3963. if (!done) {
  3964. rc = -EINVAL;
  3965. goto do_prep_failed;
  3966. }
  3967. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3968. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3969. rc = -ENXIO;
  3970. goto do_prep_failed;
  3971. }
  3972. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3973. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3974. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3975. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3976. phba->max_vpi : phba->max_vports;
  3977. } else
  3978. phba->max_vpi = 0;
  3979. phba->fips_level = 0;
  3980. phba->fips_spec_rev = 0;
  3981. if (pmb->u.mb.un.varCfgPort.gdss) {
  3982. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3983. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3984. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3985. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3986. "2850 Security Crypto Active. FIPS x%d "
  3987. "(Spec Rev: x%d)",
  3988. phba->fips_level, phba->fips_spec_rev);
  3989. }
  3990. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3991. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3992. "2856 Config Port Security Crypto "
  3993. "Error: x%x ",
  3994. pmb->u.mb.un.varCfgPort.sec_err);
  3995. }
  3996. if (pmb->u.mb.un.varCfgPort.gerbm)
  3997. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3998. if (pmb->u.mb.un.varCfgPort.gcrp)
  3999. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4000. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4001. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4002. if (phba->cfg_enable_bg) {
  4003. if (pmb->u.mb.un.varCfgPort.gbg)
  4004. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4005. else
  4006. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4007. "0443 Adapter did not grant "
  4008. "BlockGuard\n");
  4009. }
  4010. } else {
  4011. phba->hbq_get = NULL;
  4012. phba->port_gp = phba->mbox->us.s2.port;
  4013. phba->max_vpi = 0;
  4014. }
  4015. do_prep_failed:
  4016. mempool_free(pmb, phba->mbox_mem_pool);
  4017. return rc;
  4018. }
  4019. /**
  4020. * lpfc_sli_hba_setup - SLI intialization function
  4021. * @phba: Pointer to HBA context object.
  4022. *
  4023. * This function is the main SLI intialization function. This function
  4024. * is called by the HBA intialization code, HBA reset code and HBA
  4025. * error attention handler code. Caller is not required to hold any
  4026. * locks. This function issues config_port mailbox command to configure
  4027. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4028. * calls the config_port_post function to issue init_link mailbox
  4029. * command and to start the discovery. The function will return zero
  4030. * if successful, else it will return negative error code.
  4031. **/
  4032. int
  4033. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4034. {
  4035. uint32_t rc;
  4036. int mode = 3, i;
  4037. int longs;
  4038. switch (lpfc_sli_mode) {
  4039. case 2:
  4040. if (phba->cfg_enable_npiv) {
  4041. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4042. "1824 NPIV enabled: Override lpfc_sli_mode "
  4043. "parameter (%d) to auto (0).\n",
  4044. lpfc_sli_mode);
  4045. break;
  4046. }
  4047. mode = 2;
  4048. break;
  4049. case 0:
  4050. case 3:
  4051. break;
  4052. default:
  4053. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4054. "1819 Unrecognized lpfc_sli_mode "
  4055. "parameter: %d.\n", lpfc_sli_mode);
  4056. break;
  4057. }
  4058. rc = lpfc_sli_config_port(phba, mode);
  4059. if (rc && lpfc_sli_mode == 3)
  4060. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4061. "1820 Unable to select SLI-3. "
  4062. "Not supported by adapter.\n");
  4063. if (rc && mode != 2)
  4064. rc = lpfc_sli_config_port(phba, 2);
  4065. if (rc)
  4066. goto lpfc_sli_hba_setup_error;
  4067. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4068. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4069. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4070. if (!rc) {
  4071. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4072. "2709 This device supports "
  4073. "Advanced Error Reporting (AER)\n");
  4074. spin_lock_irq(&phba->hbalock);
  4075. phba->hba_flag |= HBA_AER_ENABLED;
  4076. spin_unlock_irq(&phba->hbalock);
  4077. } else {
  4078. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4079. "2708 This device does not support "
  4080. "Advanced Error Reporting (AER)\n");
  4081. phba->cfg_aer_support = 0;
  4082. }
  4083. }
  4084. if (phba->sli_rev == 3) {
  4085. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4086. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4087. } else {
  4088. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4089. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4090. phba->sli3_options = 0;
  4091. }
  4092. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4093. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4094. phba->sli_rev, phba->max_vpi);
  4095. rc = lpfc_sli_ring_map(phba);
  4096. if (rc)
  4097. goto lpfc_sli_hba_setup_error;
  4098. /* Initialize VPIs. */
  4099. if (phba->sli_rev == LPFC_SLI_REV3) {
  4100. /*
  4101. * The VPI bitmask and physical ID array are allocated
  4102. * and initialized once only - at driver load. A port
  4103. * reset doesn't need to reinitialize this memory.
  4104. */
  4105. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4106. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4107. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4108. GFP_KERNEL);
  4109. if (!phba->vpi_bmask) {
  4110. rc = -ENOMEM;
  4111. goto lpfc_sli_hba_setup_error;
  4112. }
  4113. phba->vpi_ids = kzalloc(
  4114. (phba->max_vpi+1) * sizeof(uint16_t),
  4115. GFP_KERNEL);
  4116. if (!phba->vpi_ids) {
  4117. kfree(phba->vpi_bmask);
  4118. rc = -ENOMEM;
  4119. goto lpfc_sli_hba_setup_error;
  4120. }
  4121. for (i = 0; i < phba->max_vpi; i++)
  4122. phba->vpi_ids[i] = i;
  4123. }
  4124. }
  4125. /* Init HBQs */
  4126. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4127. rc = lpfc_sli_hbq_setup(phba);
  4128. if (rc)
  4129. goto lpfc_sli_hba_setup_error;
  4130. }
  4131. spin_lock_irq(&phba->hbalock);
  4132. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4133. spin_unlock_irq(&phba->hbalock);
  4134. rc = lpfc_config_port_post(phba);
  4135. if (rc)
  4136. goto lpfc_sli_hba_setup_error;
  4137. return rc;
  4138. lpfc_sli_hba_setup_error:
  4139. phba->link_state = LPFC_HBA_ERROR;
  4140. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4141. "0445 Firmware initialization failed\n");
  4142. return rc;
  4143. }
  4144. /**
  4145. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4146. * @phba: Pointer to HBA context object.
  4147. * @mboxq: mailbox pointer.
  4148. * This function issue a dump mailbox command to read config region
  4149. * 23 and parse the records in the region and populate driver
  4150. * data structure.
  4151. **/
  4152. static int
  4153. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4154. {
  4155. LPFC_MBOXQ_t *mboxq;
  4156. struct lpfc_dmabuf *mp;
  4157. struct lpfc_mqe *mqe;
  4158. uint32_t data_length;
  4159. int rc;
  4160. /* Program the default value of vlan_id and fc_map */
  4161. phba->valid_vlan = 0;
  4162. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4163. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4164. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4165. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4166. if (!mboxq)
  4167. return -ENOMEM;
  4168. mqe = &mboxq->u.mqe;
  4169. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4170. rc = -ENOMEM;
  4171. goto out_free_mboxq;
  4172. }
  4173. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4174. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4175. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4176. "(%d):2571 Mailbox cmd x%x Status x%x "
  4177. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4178. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4179. "CQ: x%x x%x x%x x%x\n",
  4180. mboxq->vport ? mboxq->vport->vpi : 0,
  4181. bf_get(lpfc_mqe_command, mqe),
  4182. bf_get(lpfc_mqe_status, mqe),
  4183. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4184. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4185. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4186. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4187. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4188. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4189. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4190. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4191. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4192. mboxq->mcqe.word0,
  4193. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4194. mboxq->mcqe.trailer);
  4195. if (rc) {
  4196. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4197. kfree(mp);
  4198. rc = -EIO;
  4199. goto out_free_mboxq;
  4200. }
  4201. data_length = mqe->un.mb_words[5];
  4202. if (data_length > DMP_RGN23_SIZE) {
  4203. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4204. kfree(mp);
  4205. rc = -EIO;
  4206. goto out_free_mboxq;
  4207. }
  4208. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4209. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4210. kfree(mp);
  4211. rc = 0;
  4212. out_free_mboxq:
  4213. mempool_free(mboxq, phba->mbox_mem_pool);
  4214. return rc;
  4215. }
  4216. /**
  4217. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4218. * @phba: pointer to lpfc hba data structure.
  4219. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4220. * @vpd: pointer to the memory to hold resulting port vpd data.
  4221. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4222. * On output, the number of data bytes in @vpd.
  4223. *
  4224. * This routine executes a READ_REV SLI4 mailbox command. In
  4225. * addition, this routine gets the port vpd data.
  4226. *
  4227. * Return codes
  4228. * 0 - successful
  4229. * -ENOMEM - could not allocated memory.
  4230. **/
  4231. static int
  4232. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4233. uint8_t *vpd, uint32_t *vpd_size)
  4234. {
  4235. int rc = 0;
  4236. uint32_t dma_size;
  4237. struct lpfc_dmabuf *dmabuf;
  4238. struct lpfc_mqe *mqe;
  4239. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4240. if (!dmabuf)
  4241. return -ENOMEM;
  4242. /*
  4243. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4244. * mailbox command.
  4245. */
  4246. dma_size = *vpd_size;
  4247. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4248. dma_size,
  4249. &dmabuf->phys,
  4250. GFP_KERNEL);
  4251. if (!dmabuf->virt) {
  4252. kfree(dmabuf);
  4253. return -ENOMEM;
  4254. }
  4255. memset(dmabuf->virt, 0, dma_size);
  4256. /*
  4257. * The SLI4 implementation of READ_REV conflicts at word1,
  4258. * bits 31:16 and SLI4 adds vpd functionality not present
  4259. * in SLI3. This code corrects the conflicts.
  4260. */
  4261. lpfc_read_rev(phba, mboxq);
  4262. mqe = &mboxq->u.mqe;
  4263. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4264. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4265. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4266. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4267. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4268. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4269. if (rc) {
  4270. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4271. dmabuf->virt, dmabuf->phys);
  4272. kfree(dmabuf);
  4273. return -EIO;
  4274. }
  4275. /*
  4276. * The available vpd length cannot be bigger than the
  4277. * DMA buffer passed to the port. Catch the less than
  4278. * case and update the caller's size.
  4279. */
  4280. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4281. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4282. memcpy(vpd, dmabuf->virt, *vpd_size);
  4283. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4284. dmabuf->virt, dmabuf->phys);
  4285. kfree(dmabuf);
  4286. return 0;
  4287. }
  4288. /**
  4289. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4290. * @phba: pointer to lpfc hba data structure.
  4291. *
  4292. * This routine retrieves SLI4 device physical port name this PCI function
  4293. * is attached to.
  4294. *
  4295. * Return codes
  4296. * 0 - sucessful
  4297. * otherwise - failed to retrieve physical port name
  4298. **/
  4299. static int
  4300. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4301. {
  4302. LPFC_MBOXQ_t *mboxq;
  4303. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4304. struct lpfc_controller_attribute *cntl_attr;
  4305. struct lpfc_mbx_get_port_name *get_port_name;
  4306. void *virtaddr = NULL;
  4307. uint32_t alloclen, reqlen;
  4308. uint32_t shdr_status, shdr_add_status;
  4309. union lpfc_sli4_cfg_shdr *shdr;
  4310. char cport_name = 0;
  4311. int rc;
  4312. /* We assume nothing at this point */
  4313. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4314. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4315. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4316. if (!mboxq)
  4317. return -ENOMEM;
  4318. /* obtain link type and link number via READ_CONFIG */
  4319. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4320. lpfc_sli4_read_config(phba);
  4321. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4322. goto retrieve_ppname;
  4323. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4324. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4325. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4326. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4327. LPFC_SLI4_MBX_NEMBED);
  4328. if (alloclen < reqlen) {
  4329. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4330. "3084 Allocated DMA memory size (%d) is "
  4331. "less than the requested DMA memory size "
  4332. "(%d)\n", alloclen, reqlen);
  4333. rc = -ENOMEM;
  4334. goto out_free_mboxq;
  4335. }
  4336. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4337. virtaddr = mboxq->sge_array->addr[0];
  4338. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4339. shdr = &mbx_cntl_attr->cfg_shdr;
  4340. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4341. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4342. if (shdr_status || shdr_add_status || rc) {
  4343. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4344. "3085 Mailbox x%x (x%x/x%x) failed, "
  4345. "rc:x%x, status:x%x, add_status:x%x\n",
  4346. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4347. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4348. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4349. rc, shdr_status, shdr_add_status);
  4350. rc = -ENXIO;
  4351. goto out_free_mboxq;
  4352. }
  4353. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4354. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4355. phba->sli4_hba.lnk_info.lnk_tp =
  4356. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4357. phba->sli4_hba.lnk_info.lnk_no =
  4358. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4359. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4360. "3086 lnk_type:%d, lnk_numb:%d\n",
  4361. phba->sli4_hba.lnk_info.lnk_tp,
  4362. phba->sli4_hba.lnk_info.lnk_no);
  4363. retrieve_ppname:
  4364. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4365. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4366. sizeof(struct lpfc_mbx_get_port_name) -
  4367. sizeof(struct lpfc_sli4_cfg_mhdr),
  4368. LPFC_SLI4_MBX_EMBED);
  4369. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4370. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4371. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4372. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4373. phba->sli4_hba.lnk_info.lnk_tp);
  4374. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4375. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4376. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4377. if (shdr_status || shdr_add_status || rc) {
  4378. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4379. "3087 Mailbox x%x (x%x/x%x) failed: "
  4380. "rc:x%x, status:x%x, add_status:x%x\n",
  4381. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4382. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4383. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4384. rc, shdr_status, shdr_add_status);
  4385. rc = -ENXIO;
  4386. goto out_free_mboxq;
  4387. }
  4388. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4389. case LPFC_LINK_NUMBER_0:
  4390. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4391. &get_port_name->u.response);
  4392. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4393. break;
  4394. case LPFC_LINK_NUMBER_1:
  4395. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4396. &get_port_name->u.response);
  4397. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4398. break;
  4399. case LPFC_LINK_NUMBER_2:
  4400. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4401. &get_port_name->u.response);
  4402. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4403. break;
  4404. case LPFC_LINK_NUMBER_3:
  4405. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4406. &get_port_name->u.response);
  4407. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4408. break;
  4409. default:
  4410. break;
  4411. }
  4412. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4413. phba->Port[0] = cport_name;
  4414. phba->Port[1] = '\0';
  4415. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4416. "3091 SLI get port name: %s\n", phba->Port);
  4417. }
  4418. out_free_mboxq:
  4419. if (rc != MBX_TIMEOUT) {
  4420. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4421. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4422. else
  4423. mempool_free(mboxq, phba->mbox_mem_pool);
  4424. }
  4425. return rc;
  4426. }
  4427. /**
  4428. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4429. * @phba: pointer to lpfc hba data structure.
  4430. *
  4431. * This routine is called to explicitly arm the SLI4 device's completion and
  4432. * event queues
  4433. **/
  4434. static void
  4435. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4436. {
  4437. uint8_t fcp_eqidx;
  4438. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4439. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4440. fcp_eqidx = 0;
  4441. if (phba->sli4_hba.fcp_cq) {
  4442. do
  4443. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4444. LPFC_QUEUE_REARM);
  4445. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4446. }
  4447. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4448. if (phba->sli4_hba.fp_eq) {
  4449. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4450. fcp_eqidx++)
  4451. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4452. LPFC_QUEUE_REARM);
  4453. }
  4454. }
  4455. /**
  4456. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4457. * @phba: Pointer to HBA context object.
  4458. * @type: The resource extent type.
  4459. * @extnt_count: buffer to hold port available extent count.
  4460. * @extnt_size: buffer to hold element count per extent.
  4461. *
  4462. * This function calls the port and retrievs the number of available
  4463. * extents and their size for a particular extent type.
  4464. *
  4465. * Returns: 0 if successful. Nonzero otherwise.
  4466. **/
  4467. int
  4468. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4469. uint16_t *extnt_count, uint16_t *extnt_size)
  4470. {
  4471. int rc = 0;
  4472. uint32_t length;
  4473. uint32_t mbox_tmo;
  4474. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4475. LPFC_MBOXQ_t *mbox;
  4476. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4477. if (!mbox)
  4478. return -ENOMEM;
  4479. /* Find out how many extents are available for this resource type */
  4480. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4481. sizeof(struct lpfc_sli4_cfg_mhdr));
  4482. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4483. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4484. length, LPFC_SLI4_MBX_EMBED);
  4485. /* Send an extents count of 0 - the GET doesn't use it. */
  4486. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4487. LPFC_SLI4_MBX_EMBED);
  4488. if (unlikely(rc)) {
  4489. rc = -EIO;
  4490. goto err_exit;
  4491. }
  4492. if (!phba->sli4_hba.intr_enable)
  4493. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4494. else {
  4495. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4496. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4497. }
  4498. if (unlikely(rc)) {
  4499. rc = -EIO;
  4500. goto err_exit;
  4501. }
  4502. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4503. if (bf_get(lpfc_mbox_hdr_status,
  4504. &rsrc_info->header.cfg_shdr.response)) {
  4505. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4506. "2930 Failed to get resource extents "
  4507. "Status 0x%x Add'l Status 0x%x\n",
  4508. bf_get(lpfc_mbox_hdr_status,
  4509. &rsrc_info->header.cfg_shdr.response),
  4510. bf_get(lpfc_mbox_hdr_add_status,
  4511. &rsrc_info->header.cfg_shdr.response));
  4512. rc = -EIO;
  4513. goto err_exit;
  4514. }
  4515. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4516. &rsrc_info->u.rsp);
  4517. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4518. &rsrc_info->u.rsp);
  4519. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4520. "3162 Retrieved extents type-%d from port: count:%d, "
  4521. "size:%d\n", type, *extnt_count, *extnt_size);
  4522. err_exit:
  4523. mempool_free(mbox, phba->mbox_mem_pool);
  4524. return rc;
  4525. }
  4526. /**
  4527. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4528. * @phba: Pointer to HBA context object.
  4529. * @type: The extent type to check.
  4530. *
  4531. * This function reads the current available extents from the port and checks
  4532. * if the extent count or extent size has changed since the last access.
  4533. * Callers use this routine post port reset to understand if there is a
  4534. * extent reprovisioning requirement.
  4535. *
  4536. * Returns:
  4537. * -Error: error indicates problem.
  4538. * 1: Extent count or size has changed.
  4539. * 0: No changes.
  4540. **/
  4541. static int
  4542. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4543. {
  4544. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4545. uint16_t size_diff, rsrc_ext_size;
  4546. int rc = 0;
  4547. struct lpfc_rsrc_blks *rsrc_entry;
  4548. struct list_head *rsrc_blk_list = NULL;
  4549. size_diff = 0;
  4550. curr_ext_cnt = 0;
  4551. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4552. &rsrc_ext_cnt,
  4553. &rsrc_ext_size);
  4554. if (unlikely(rc))
  4555. return -EIO;
  4556. switch (type) {
  4557. case LPFC_RSC_TYPE_FCOE_RPI:
  4558. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4559. break;
  4560. case LPFC_RSC_TYPE_FCOE_VPI:
  4561. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4562. break;
  4563. case LPFC_RSC_TYPE_FCOE_XRI:
  4564. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4565. break;
  4566. case LPFC_RSC_TYPE_FCOE_VFI:
  4567. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4568. break;
  4569. default:
  4570. break;
  4571. }
  4572. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4573. curr_ext_cnt++;
  4574. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4575. size_diff++;
  4576. }
  4577. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4578. rc = 1;
  4579. return rc;
  4580. }
  4581. /**
  4582. * lpfc_sli4_cfg_post_extnts -
  4583. * @phba: Pointer to HBA context object.
  4584. * @extnt_cnt - number of available extents.
  4585. * @type - the extent type (rpi, xri, vfi, vpi).
  4586. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4587. * @mbox - pointer to the caller's allocated mailbox structure.
  4588. *
  4589. * This function executes the extents allocation request. It also
  4590. * takes care of the amount of memory needed to allocate or get the
  4591. * allocated extents. It is the caller's responsibility to evaluate
  4592. * the response.
  4593. *
  4594. * Returns:
  4595. * -Error: Error value describes the condition found.
  4596. * 0: if successful
  4597. **/
  4598. static int
  4599. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4600. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4601. {
  4602. int rc = 0;
  4603. uint32_t req_len;
  4604. uint32_t emb_len;
  4605. uint32_t alloc_len, mbox_tmo;
  4606. /* Calculate the total requested length of the dma memory */
  4607. req_len = extnt_cnt * sizeof(uint16_t);
  4608. /*
  4609. * Calculate the size of an embedded mailbox. The uint32_t
  4610. * accounts for extents-specific word.
  4611. */
  4612. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4613. sizeof(uint32_t);
  4614. /*
  4615. * Presume the allocation and response will fit into an embedded
  4616. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4617. */
  4618. *emb = LPFC_SLI4_MBX_EMBED;
  4619. if (req_len > emb_len) {
  4620. req_len = extnt_cnt * sizeof(uint16_t) +
  4621. sizeof(union lpfc_sli4_cfg_shdr) +
  4622. sizeof(uint32_t);
  4623. *emb = LPFC_SLI4_MBX_NEMBED;
  4624. }
  4625. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4626. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4627. req_len, *emb);
  4628. if (alloc_len < req_len) {
  4629. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4630. "2982 Allocated DMA memory size (x%x) is "
  4631. "less than the requested DMA memory "
  4632. "size (x%x)\n", alloc_len, req_len);
  4633. return -ENOMEM;
  4634. }
  4635. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4636. if (unlikely(rc))
  4637. return -EIO;
  4638. if (!phba->sli4_hba.intr_enable)
  4639. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4640. else {
  4641. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4642. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4643. }
  4644. if (unlikely(rc))
  4645. rc = -EIO;
  4646. return rc;
  4647. }
  4648. /**
  4649. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4650. * @phba: Pointer to HBA context object.
  4651. * @type: The resource extent type to allocate.
  4652. *
  4653. * This function allocates the number of elements for the specified
  4654. * resource type.
  4655. **/
  4656. static int
  4657. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4658. {
  4659. bool emb = false;
  4660. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4661. uint16_t rsrc_id, rsrc_start, j, k;
  4662. uint16_t *ids;
  4663. int i, rc;
  4664. unsigned long longs;
  4665. unsigned long *bmask;
  4666. struct lpfc_rsrc_blks *rsrc_blks;
  4667. LPFC_MBOXQ_t *mbox;
  4668. uint32_t length;
  4669. struct lpfc_id_range *id_array = NULL;
  4670. void *virtaddr = NULL;
  4671. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4672. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4673. struct list_head *ext_blk_list;
  4674. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4675. &rsrc_cnt,
  4676. &rsrc_size);
  4677. if (unlikely(rc))
  4678. return -EIO;
  4679. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4680. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4681. "3009 No available Resource Extents "
  4682. "for resource type 0x%x: Count: 0x%x, "
  4683. "Size 0x%x\n", type, rsrc_cnt,
  4684. rsrc_size);
  4685. return -ENOMEM;
  4686. }
  4687. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4688. "2903 Post resource extents type-0x%x: "
  4689. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4690. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4691. if (!mbox)
  4692. return -ENOMEM;
  4693. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4694. if (unlikely(rc)) {
  4695. rc = -EIO;
  4696. goto err_exit;
  4697. }
  4698. /*
  4699. * Figure out where the response is located. Then get local pointers
  4700. * to the response data. The port does not guarantee to respond to
  4701. * all extents counts request so update the local variable with the
  4702. * allocated count from the port.
  4703. */
  4704. if (emb == LPFC_SLI4_MBX_EMBED) {
  4705. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4706. id_array = &rsrc_ext->u.rsp.id[0];
  4707. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4708. } else {
  4709. virtaddr = mbox->sge_array->addr[0];
  4710. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4711. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4712. id_array = &n_rsrc->id;
  4713. }
  4714. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4715. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4716. /*
  4717. * Based on the resource size and count, correct the base and max
  4718. * resource values.
  4719. */
  4720. length = sizeof(struct lpfc_rsrc_blks);
  4721. switch (type) {
  4722. case LPFC_RSC_TYPE_FCOE_RPI:
  4723. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4724. sizeof(unsigned long),
  4725. GFP_KERNEL);
  4726. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4727. rc = -ENOMEM;
  4728. goto err_exit;
  4729. }
  4730. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4731. sizeof(uint16_t),
  4732. GFP_KERNEL);
  4733. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4734. kfree(phba->sli4_hba.rpi_bmask);
  4735. rc = -ENOMEM;
  4736. goto err_exit;
  4737. }
  4738. /*
  4739. * The next_rpi was initialized with the maximum available
  4740. * count but the port may allocate a smaller number. Catch
  4741. * that case and update the next_rpi.
  4742. */
  4743. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4744. /* Initialize local ptrs for common extent processing later. */
  4745. bmask = phba->sli4_hba.rpi_bmask;
  4746. ids = phba->sli4_hba.rpi_ids;
  4747. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4748. break;
  4749. case LPFC_RSC_TYPE_FCOE_VPI:
  4750. phba->vpi_bmask = kzalloc(longs *
  4751. sizeof(unsigned long),
  4752. GFP_KERNEL);
  4753. if (unlikely(!phba->vpi_bmask)) {
  4754. rc = -ENOMEM;
  4755. goto err_exit;
  4756. }
  4757. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4758. sizeof(uint16_t),
  4759. GFP_KERNEL);
  4760. if (unlikely(!phba->vpi_ids)) {
  4761. kfree(phba->vpi_bmask);
  4762. rc = -ENOMEM;
  4763. goto err_exit;
  4764. }
  4765. /* Initialize local ptrs for common extent processing later. */
  4766. bmask = phba->vpi_bmask;
  4767. ids = phba->vpi_ids;
  4768. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4769. break;
  4770. case LPFC_RSC_TYPE_FCOE_XRI:
  4771. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4772. sizeof(unsigned long),
  4773. GFP_KERNEL);
  4774. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4775. rc = -ENOMEM;
  4776. goto err_exit;
  4777. }
  4778. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4779. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4780. sizeof(uint16_t),
  4781. GFP_KERNEL);
  4782. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4783. kfree(phba->sli4_hba.xri_bmask);
  4784. rc = -ENOMEM;
  4785. goto err_exit;
  4786. }
  4787. /* Initialize local ptrs for common extent processing later. */
  4788. bmask = phba->sli4_hba.xri_bmask;
  4789. ids = phba->sli4_hba.xri_ids;
  4790. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4791. break;
  4792. case LPFC_RSC_TYPE_FCOE_VFI:
  4793. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4794. sizeof(unsigned long),
  4795. GFP_KERNEL);
  4796. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4797. rc = -ENOMEM;
  4798. goto err_exit;
  4799. }
  4800. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4801. sizeof(uint16_t),
  4802. GFP_KERNEL);
  4803. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4804. kfree(phba->sli4_hba.vfi_bmask);
  4805. rc = -ENOMEM;
  4806. goto err_exit;
  4807. }
  4808. /* Initialize local ptrs for common extent processing later. */
  4809. bmask = phba->sli4_hba.vfi_bmask;
  4810. ids = phba->sli4_hba.vfi_ids;
  4811. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4812. break;
  4813. default:
  4814. /* Unsupported Opcode. Fail call. */
  4815. id_array = NULL;
  4816. bmask = NULL;
  4817. ids = NULL;
  4818. ext_blk_list = NULL;
  4819. goto err_exit;
  4820. }
  4821. /*
  4822. * Complete initializing the extent configuration with the
  4823. * allocated ids assigned to this function. The bitmask serves
  4824. * as an index into the array and manages the available ids. The
  4825. * array just stores the ids communicated to the port via the wqes.
  4826. */
  4827. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4828. if ((i % 2) == 0)
  4829. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4830. &id_array[k]);
  4831. else
  4832. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4833. &id_array[k]);
  4834. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4835. if (unlikely(!rsrc_blks)) {
  4836. rc = -ENOMEM;
  4837. kfree(bmask);
  4838. kfree(ids);
  4839. goto err_exit;
  4840. }
  4841. rsrc_blks->rsrc_start = rsrc_id;
  4842. rsrc_blks->rsrc_size = rsrc_size;
  4843. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4844. rsrc_start = rsrc_id;
  4845. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4846. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4847. lpfc_sli4_get_els_iocb_cnt(phba);
  4848. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4849. ids[j] = rsrc_id;
  4850. rsrc_id++;
  4851. j++;
  4852. }
  4853. /* Entire word processed. Get next word.*/
  4854. if ((i % 2) == 1)
  4855. k++;
  4856. }
  4857. err_exit:
  4858. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4859. return rc;
  4860. }
  4861. /**
  4862. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4863. * @phba: Pointer to HBA context object.
  4864. * @type: the extent's type.
  4865. *
  4866. * This function deallocates all extents of a particular resource type.
  4867. * SLI4 does not allow for deallocating a particular extent range. It
  4868. * is the caller's responsibility to release all kernel memory resources.
  4869. **/
  4870. static int
  4871. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4872. {
  4873. int rc;
  4874. uint32_t length, mbox_tmo = 0;
  4875. LPFC_MBOXQ_t *mbox;
  4876. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4877. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4878. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4879. if (!mbox)
  4880. return -ENOMEM;
  4881. /*
  4882. * This function sends an embedded mailbox because it only sends the
  4883. * the resource type. All extents of this type are released by the
  4884. * port.
  4885. */
  4886. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4887. sizeof(struct lpfc_sli4_cfg_mhdr));
  4888. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4889. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4890. length, LPFC_SLI4_MBX_EMBED);
  4891. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4892. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4893. LPFC_SLI4_MBX_EMBED);
  4894. if (unlikely(rc)) {
  4895. rc = -EIO;
  4896. goto out_free_mbox;
  4897. }
  4898. if (!phba->sli4_hba.intr_enable)
  4899. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4900. else {
  4901. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4902. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4903. }
  4904. if (unlikely(rc)) {
  4905. rc = -EIO;
  4906. goto out_free_mbox;
  4907. }
  4908. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4909. if (bf_get(lpfc_mbox_hdr_status,
  4910. &dealloc_rsrc->header.cfg_shdr.response)) {
  4911. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4912. "2919 Failed to release resource extents "
  4913. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4914. "Resource memory not released.\n",
  4915. type,
  4916. bf_get(lpfc_mbox_hdr_status,
  4917. &dealloc_rsrc->header.cfg_shdr.response),
  4918. bf_get(lpfc_mbox_hdr_add_status,
  4919. &dealloc_rsrc->header.cfg_shdr.response));
  4920. rc = -EIO;
  4921. goto out_free_mbox;
  4922. }
  4923. /* Release kernel memory resources for the specific type. */
  4924. switch (type) {
  4925. case LPFC_RSC_TYPE_FCOE_VPI:
  4926. kfree(phba->vpi_bmask);
  4927. kfree(phba->vpi_ids);
  4928. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4929. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4930. &phba->lpfc_vpi_blk_list, list) {
  4931. list_del_init(&rsrc_blk->list);
  4932. kfree(rsrc_blk);
  4933. }
  4934. break;
  4935. case LPFC_RSC_TYPE_FCOE_XRI:
  4936. kfree(phba->sli4_hba.xri_bmask);
  4937. kfree(phba->sli4_hba.xri_ids);
  4938. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4939. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4940. list_del_init(&rsrc_blk->list);
  4941. kfree(rsrc_blk);
  4942. }
  4943. break;
  4944. case LPFC_RSC_TYPE_FCOE_VFI:
  4945. kfree(phba->sli4_hba.vfi_bmask);
  4946. kfree(phba->sli4_hba.vfi_ids);
  4947. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4948. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4949. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4950. list_del_init(&rsrc_blk->list);
  4951. kfree(rsrc_blk);
  4952. }
  4953. break;
  4954. case LPFC_RSC_TYPE_FCOE_RPI:
  4955. /* RPI bitmask and physical id array are cleaned up earlier. */
  4956. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4957. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4958. list_del_init(&rsrc_blk->list);
  4959. kfree(rsrc_blk);
  4960. }
  4961. break;
  4962. default:
  4963. break;
  4964. }
  4965. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4966. out_free_mbox:
  4967. mempool_free(mbox, phba->mbox_mem_pool);
  4968. return rc;
  4969. }
  4970. /**
  4971. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4972. * @phba: Pointer to HBA context object.
  4973. *
  4974. * This function allocates all SLI4 resource identifiers.
  4975. **/
  4976. int
  4977. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4978. {
  4979. int i, rc, error = 0;
  4980. uint16_t count, base;
  4981. unsigned long longs;
  4982. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4983. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4984. if (phba->sli4_hba.extents_in_use) {
  4985. /*
  4986. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4987. * resource extent count must be read and allocated before
  4988. * provisioning the resource id arrays.
  4989. */
  4990. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4991. LPFC_IDX_RSRC_RDY) {
  4992. /*
  4993. * Extent-based resources are set - the driver could
  4994. * be in a port reset. Figure out if any corrective
  4995. * actions need to be taken.
  4996. */
  4997. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4998. LPFC_RSC_TYPE_FCOE_VFI);
  4999. if (rc != 0)
  5000. error++;
  5001. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5002. LPFC_RSC_TYPE_FCOE_VPI);
  5003. if (rc != 0)
  5004. error++;
  5005. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5006. LPFC_RSC_TYPE_FCOE_XRI);
  5007. if (rc != 0)
  5008. error++;
  5009. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5010. LPFC_RSC_TYPE_FCOE_RPI);
  5011. if (rc != 0)
  5012. error++;
  5013. /*
  5014. * It's possible that the number of resources
  5015. * provided to this port instance changed between
  5016. * resets. Detect this condition and reallocate
  5017. * resources. Otherwise, there is no action.
  5018. */
  5019. if (error) {
  5020. lpfc_printf_log(phba, KERN_INFO,
  5021. LOG_MBOX | LOG_INIT,
  5022. "2931 Detected extent resource "
  5023. "change. Reallocating all "
  5024. "extents.\n");
  5025. rc = lpfc_sli4_dealloc_extent(phba,
  5026. LPFC_RSC_TYPE_FCOE_VFI);
  5027. rc = lpfc_sli4_dealloc_extent(phba,
  5028. LPFC_RSC_TYPE_FCOE_VPI);
  5029. rc = lpfc_sli4_dealloc_extent(phba,
  5030. LPFC_RSC_TYPE_FCOE_XRI);
  5031. rc = lpfc_sli4_dealloc_extent(phba,
  5032. LPFC_RSC_TYPE_FCOE_RPI);
  5033. } else
  5034. return 0;
  5035. }
  5036. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5037. if (unlikely(rc))
  5038. goto err_exit;
  5039. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5040. if (unlikely(rc))
  5041. goto err_exit;
  5042. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5043. if (unlikely(rc))
  5044. goto err_exit;
  5045. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5046. if (unlikely(rc))
  5047. goto err_exit;
  5048. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5049. LPFC_IDX_RSRC_RDY);
  5050. return rc;
  5051. } else {
  5052. /*
  5053. * The port does not support resource extents. The XRI, VPI,
  5054. * VFI, RPI resource ids were determined from READ_CONFIG.
  5055. * Just allocate the bitmasks and provision the resource id
  5056. * arrays. If a port reset is active, the resources don't
  5057. * need any action - just exit.
  5058. */
  5059. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5060. LPFC_IDX_RSRC_RDY) {
  5061. lpfc_sli4_dealloc_resource_identifiers(phba);
  5062. lpfc_sli4_remove_rpis(phba);
  5063. }
  5064. /* RPIs. */
  5065. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5066. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5067. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5068. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5069. sizeof(unsigned long),
  5070. GFP_KERNEL);
  5071. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5072. rc = -ENOMEM;
  5073. goto err_exit;
  5074. }
  5075. phba->sli4_hba.rpi_ids = kzalloc(count *
  5076. sizeof(uint16_t),
  5077. GFP_KERNEL);
  5078. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5079. rc = -ENOMEM;
  5080. goto free_rpi_bmask;
  5081. }
  5082. for (i = 0; i < count; i++)
  5083. phba->sli4_hba.rpi_ids[i] = base + i;
  5084. /* VPIs. */
  5085. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5086. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5087. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5088. phba->vpi_bmask = kzalloc(longs *
  5089. sizeof(unsigned long),
  5090. GFP_KERNEL);
  5091. if (unlikely(!phba->vpi_bmask)) {
  5092. rc = -ENOMEM;
  5093. goto free_rpi_ids;
  5094. }
  5095. phba->vpi_ids = kzalloc(count *
  5096. sizeof(uint16_t),
  5097. GFP_KERNEL);
  5098. if (unlikely(!phba->vpi_ids)) {
  5099. rc = -ENOMEM;
  5100. goto free_vpi_bmask;
  5101. }
  5102. for (i = 0; i < count; i++)
  5103. phba->vpi_ids[i] = base + i;
  5104. /* XRIs. */
  5105. count = phba->sli4_hba.max_cfg_param.max_xri;
  5106. base = phba->sli4_hba.max_cfg_param.xri_base;
  5107. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5108. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5109. sizeof(unsigned long),
  5110. GFP_KERNEL);
  5111. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5112. rc = -ENOMEM;
  5113. goto free_vpi_ids;
  5114. }
  5115. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5116. phba->sli4_hba.xri_ids = kzalloc(count *
  5117. sizeof(uint16_t),
  5118. GFP_KERNEL);
  5119. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5120. rc = -ENOMEM;
  5121. goto free_xri_bmask;
  5122. }
  5123. for (i = 0; i < count; i++)
  5124. phba->sli4_hba.xri_ids[i] = base + i;
  5125. /* VFIs. */
  5126. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5127. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5128. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5129. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5130. sizeof(unsigned long),
  5131. GFP_KERNEL);
  5132. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5133. rc = -ENOMEM;
  5134. goto free_xri_ids;
  5135. }
  5136. phba->sli4_hba.vfi_ids = kzalloc(count *
  5137. sizeof(uint16_t),
  5138. GFP_KERNEL);
  5139. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5140. rc = -ENOMEM;
  5141. goto free_vfi_bmask;
  5142. }
  5143. for (i = 0; i < count; i++)
  5144. phba->sli4_hba.vfi_ids[i] = base + i;
  5145. /*
  5146. * Mark all resources ready. An HBA reset doesn't need
  5147. * to reset the initialization.
  5148. */
  5149. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5150. LPFC_IDX_RSRC_RDY);
  5151. return 0;
  5152. }
  5153. free_vfi_bmask:
  5154. kfree(phba->sli4_hba.vfi_bmask);
  5155. free_xri_ids:
  5156. kfree(phba->sli4_hba.xri_ids);
  5157. free_xri_bmask:
  5158. kfree(phba->sli4_hba.xri_bmask);
  5159. free_vpi_ids:
  5160. kfree(phba->vpi_ids);
  5161. free_vpi_bmask:
  5162. kfree(phba->vpi_bmask);
  5163. free_rpi_ids:
  5164. kfree(phba->sli4_hba.rpi_ids);
  5165. free_rpi_bmask:
  5166. kfree(phba->sli4_hba.rpi_bmask);
  5167. err_exit:
  5168. return rc;
  5169. }
  5170. /**
  5171. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5172. * @phba: Pointer to HBA context object.
  5173. *
  5174. * This function allocates the number of elements for the specified
  5175. * resource type.
  5176. **/
  5177. int
  5178. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5179. {
  5180. if (phba->sli4_hba.extents_in_use) {
  5181. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5182. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5183. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5184. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5185. } else {
  5186. kfree(phba->vpi_bmask);
  5187. kfree(phba->vpi_ids);
  5188. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5189. kfree(phba->sli4_hba.xri_bmask);
  5190. kfree(phba->sli4_hba.xri_ids);
  5191. kfree(phba->sli4_hba.vfi_bmask);
  5192. kfree(phba->sli4_hba.vfi_ids);
  5193. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5194. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5195. }
  5196. return 0;
  5197. }
  5198. /**
  5199. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5200. * @phba: Pointer to HBA context object.
  5201. * @type: The resource extent type.
  5202. * @extnt_count: buffer to hold port extent count response
  5203. * @extnt_size: buffer to hold port extent size response.
  5204. *
  5205. * This function calls the port to read the host allocated extents
  5206. * for a particular type.
  5207. **/
  5208. int
  5209. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5210. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5211. {
  5212. bool emb;
  5213. int rc = 0;
  5214. uint16_t curr_blks = 0;
  5215. uint32_t req_len, emb_len;
  5216. uint32_t alloc_len, mbox_tmo;
  5217. struct list_head *blk_list_head;
  5218. struct lpfc_rsrc_blks *rsrc_blk;
  5219. LPFC_MBOXQ_t *mbox;
  5220. void *virtaddr = NULL;
  5221. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5222. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5223. union lpfc_sli4_cfg_shdr *shdr;
  5224. switch (type) {
  5225. case LPFC_RSC_TYPE_FCOE_VPI:
  5226. blk_list_head = &phba->lpfc_vpi_blk_list;
  5227. break;
  5228. case LPFC_RSC_TYPE_FCOE_XRI:
  5229. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5230. break;
  5231. case LPFC_RSC_TYPE_FCOE_VFI:
  5232. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5233. break;
  5234. case LPFC_RSC_TYPE_FCOE_RPI:
  5235. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5236. break;
  5237. default:
  5238. return -EIO;
  5239. }
  5240. /* Count the number of extents currently allocatd for this type. */
  5241. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5242. if (curr_blks == 0) {
  5243. /*
  5244. * The GET_ALLOCATED mailbox does not return the size,
  5245. * just the count. The size should be just the size
  5246. * stored in the current allocated block and all sizes
  5247. * for an extent type are the same so set the return
  5248. * value now.
  5249. */
  5250. *extnt_size = rsrc_blk->rsrc_size;
  5251. }
  5252. curr_blks++;
  5253. }
  5254. /* Calculate the total requested length of the dma memory. */
  5255. req_len = curr_blks * sizeof(uint16_t);
  5256. /*
  5257. * Calculate the size of an embedded mailbox. The uint32_t
  5258. * accounts for extents-specific word.
  5259. */
  5260. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5261. sizeof(uint32_t);
  5262. /*
  5263. * Presume the allocation and response will fit into an embedded
  5264. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5265. */
  5266. emb = LPFC_SLI4_MBX_EMBED;
  5267. req_len = emb_len;
  5268. if (req_len > emb_len) {
  5269. req_len = curr_blks * sizeof(uint16_t) +
  5270. sizeof(union lpfc_sli4_cfg_shdr) +
  5271. sizeof(uint32_t);
  5272. emb = LPFC_SLI4_MBX_NEMBED;
  5273. }
  5274. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5275. if (!mbox)
  5276. return -ENOMEM;
  5277. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5278. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5279. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5280. req_len, emb);
  5281. if (alloc_len < req_len) {
  5282. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5283. "2983 Allocated DMA memory size (x%x) is "
  5284. "less than the requested DMA memory "
  5285. "size (x%x)\n", alloc_len, req_len);
  5286. rc = -ENOMEM;
  5287. goto err_exit;
  5288. }
  5289. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5290. if (unlikely(rc)) {
  5291. rc = -EIO;
  5292. goto err_exit;
  5293. }
  5294. if (!phba->sli4_hba.intr_enable)
  5295. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5296. else {
  5297. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5298. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5299. }
  5300. if (unlikely(rc)) {
  5301. rc = -EIO;
  5302. goto err_exit;
  5303. }
  5304. /*
  5305. * Figure out where the response is located. Then get local pointers
  5306. * to the response data. The port does not guarantee to respond to
  5307. * all extents counts request so update the local variable with the
  5308. * allocated count from the port.
  5309. */
  5310. if (emb == LPFC_SLI4_MBX_EMBED) {
  5311. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5312. shdr = &rsrc_ext->header.cfg_shdr;
  5313. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5314. } else {
  5315. virtaddr = mbox->sge_array->addr[0];
  5316. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5317. shdr = &n_rsrc->cfg_shdr;
  5318. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5319. }
  5320. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5321. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5322. "2984 Failed to read allocated resources "
  5323. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5324. type,
  5325. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5326. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5327. rc = -EIO;
  5328. goto err_exit;
  5329. }
  5330. err_exit:
  5331. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5332. return rc;
  5333. }
  5334. /**
  5335. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5336. * @phba: pointer to lpfc hba data structure.
  5337. *
  5338. * This routine walks the list of els buffers that have been allocated and
  5339. * repost them to the port by using SGL block post. This is needed after a
  5340. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5341. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5342. * SGL block post mailbox commands to post them to the port. For single els
  5343. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5344. * mailbox command for posting.
  5345. *
  5346. * Returns: 0 = success, non-zero failure.
  5347. **/
  5348. static int
  5349. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5350. {
  5351. struct lpfc_sglq *sglq_entry = NULL;
  5352. struct lpfc_sglq *sglq_entry_next = NULL;
  5353. struct lpfc_sglq *sglq_entry_first = NULL;
  5354. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5355. int last_xritag = NO_XRI;
  5356. LIST_HEAD(prep_sgl_list);
  5357. LIST_HEAD(blck_sgl_list);
  5358. LIST_HEAD(allc_sgl_list);
  5359. LIST_HEAD(post_sgl_list);
  5360. LIST_HEAD(free_sgl_list);
  5361. spin_lock(&phba->hbalock);
  5362. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5363. spin_unlock(&phba->hbalock);
  5364. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5365. &allc_sgl_list, list) {
  5366. list_del_init(&sglq_entry->list);
  5367. block_cnt++;
  5368. if ((last_xritag != NO_XRI) &&
  5369. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5370. /* a hole in xri block, form a sgl posting block */
  5371. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5372. post_cnt = block_cnt - 1;
  5373. /* prepare list for next posting block */
  5374. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5375. block_cnt = 1;
  5376. } else {
  5377. /* prepare list for next posting block */
  5378. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5379. /* enough sgls for non-embed sgl mbox command */
  5380. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5381. list_splice_init(&prep_sgl_list,
  5382. &blck_sgl_list);
  5383. post_cnt = block_cnt;
  5384. block_cnt = 0;
  5385. }
  5386. }
  5387. num_posted++;
  5388. /* keep track of last sgl's xritag */
  5389. last_xritag = sglq_entry->sli4_xritag;
  5390. /* end of repost sgl list condition for els buffers */
  5391. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5392. if (post_cnt == 0) {
  5393. list_splice_init(&prep_sgl_list,
  5394. &blck_sgl_list);
  5395. post_cnt = block_cnt;
  5396. } else if (block_cnt == 1) {
  5397. status = lpfc_sli4_post_sgl(phba,
  5398. sglq_entry->phys, 0,
  5399. sglq_entry->sli4_xritag);
  5400. if (!status) {
  5401. /* successful, put sgl to posted list */
  5402. list_add_tail(&sglq_entry->list,
  5403. &post_sgl_list);
  5404. } else {
  5405. /* Failure, put sgl to free list */
  5406. lpfc_printf_log(phba, KERN_WARNING,
  5407. LOG_SLI,
  5408. "3159 Failed to post els "
  5409. "sgl, xritag:x%x\n",
  5410. sglq_entry->sli4_xritag);
  5411. list_add_tail(&sglq_entry->list,
  5412. &free_sgl_list);
  5413. spin_lock_irq(&phba->hbalock);
  5414. phba->sli4_hba.els_xri_cnt--;
  5415. spin_unlock_irq(&phba->hbalock);
  5416. }
  5417. }
  5418. }
  5419. /* continue until a nembed page worth of sgls */
  5420. if (post_cnt == 0)
  5421. continue;
  5422. /* post the els buffer list sgls as a block */
  5423. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5424. post_cnt);
  5425. if (!status) {
  5426. /* success, put sgl list to posted sgl list */
  5427. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5428. } else {
  5429. /* Failure, put sgl list to free sgl list */
  5430. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5431. struct lpfc_sglq,
  5432. list);
  5433. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5434. "3160 Failed to post els sgl-list, "
  5435. "xritag:x%x-x%x\n",
  5436. sglq_entry_first->sli4_xritag,
  5437. (sglq_entry_first->sli4_xritag +
  5438. post_cnt - 1));
  5439. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5440. spin_lock_irq(&phba->hbalock);
  5441. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5442. spin_unlock_irq(&phba->hbalock);
  5443. }
  5444. /* don't reset xirtag due to hole in xri block */
  5445. if (block_cnt == 0)
  5446. last_xritag = NO_XRI;
  5447. /* reset els sgl post count for next round of posting */
  5448. post_cnt = 0;
  5449. }
  5450. /* free the els sgls failed to post */
  5451. lpfc_free_sgl_list(phba, &free_sgl_list);
  5452. /* push els sgls posted to the availble list */
  5453. if (!list_empty(&post_sgl_list)) {
  5454. spin_lock(&phba->hbalock);
  5455. list_splice_init(&post_sgl_list,
  5456. &phba->sli4_hba.lpfc_sgl_list);
  5457. spin_unlock(&phba->hbalock);
  5458. } else {
  5459. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5460. "3161 Failure to post els sgl to port.\n");
  5461. return -EIO;
  5462. }
  5463. return 0;
  5464. }
  5465. /**
  5466. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5467. * @phba: Pointer to HBA context object.
  5468. *
  5469. * This function is the main SLI4 device intialization PCI function. This
  5470. * function is called by the HBA intialization code, HBA reset code and
  5471. * HBA error attention handler code. Caller is not required to hold any
  5472. * locks.
  5473. **/
  5474. int
  5475. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5476. {
  5477. int rc;
  5478. LPFC_MBOXQ_t *mboxq;
  5479. struct lpfc_mqe *mqe;
  5480. uint8_t *vpd;
  5481. uint32_t vpd_size;
  5482. uint32_t ftr_rsp = 0;
  5483. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5484. struct lpfc_vport *vport = phba->pport;
  5485. struct lpfc_dmabuf *mp;
  5486. /* Perform a PCI function reset to start from clean */
  5487. rc = lpfc_pci_function_reset(phba);
  5488. if (unlikely(rc))
  5489. return -ENODEV;
  5490. /* Check the HBA Host Status Register for readyness */
  5491. rc = lpfc_sli4_post_status_check(phba);
  5492. if (unlikely(rc))
  5493. return -ENODEV;
  5494. else {
  5495. spin_lock_irq(&phba->hbalock);
  5496. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5497. spin_unlock_irq(&phba->hbalock);
  5498. }
  5499. /*
  5500. * Allocate a single mailbox container for initializing the
  5501. * port.
  5502. */
  5503. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5504. if (!mboxq)
  5505. return -ENOMEM;
  5506. /* Issue READ_REV to collect vpd and FW information. */
  5507. vpd_size = SLI4_PAGE_SIZE;
  5508. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5509. if (!vpd) {
  5510. rc = -ENOMEM;
  5511. goto out_free_mbox;
  5512. }
  5513. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5514. if (unlikely(rc)) {
  5515. kfree(vpd);
  5516. goto out_free_mbox;
  5517. }
  5518. mqe = &mboxq->u.mqe;
  5519. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5520. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5521. phba->hba_flag |= HBA_FCOE_MODE;
  5522. else
  5523. phba->hba_flag &= ~HBA_FCOE_MODE;
  5524. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5525. LPFC_DCBX_CEE_MODE)
  5526. phba->hba_flag |= HBA_FIP_SUPPORT;
  5527. else
  5528. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5529. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5530. if (phba->sli_rev != LPFC_SLI_REV4) {
  5531. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5532. "0376 READ_REV Error. SLI Level %d "
  5533. "FCoE enabled %d\n",
  5534. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5535. rc = -EIO;
  5536. kfree(vpd);
  5537. goto out_free_mbox;
  5538. }
  5539. /*
  5540. * Continue initialization with default values even if driver failed
  5541. * to read FCoE param config regions, only read parameters if the
  5542. * board is FCoE
  5543. */
  5544. if (phba->hba_flag & HBA_FCOE_MODE &&
  5545. lpfc_sli4_read_fcoe_params(phba))
  5546. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5547. "2570 Failed to read FCoE parameters\n");
  5548. /*
  5549. * Retrieve sli4 device physical port name, failure of doing it
  5550. * is considered as non-fatal.
  5551. */
  5552. rc = lpfc_sli4_retrieve_pport_name(phba);
  5553. if (!rc)
  5554. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5555. "3080 Successful retrieving SLI4 device "
  5556. "physical port name: %s.\n", phba->Port);
  5557. /*
  5558. * Evaluate the read rev and vpd data. Populate the driver
  5559. * state with the results. If this routine fails, the failure
  5560. * is not fatal as the driver will use generic values.
  5561. */
  5562. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5563. if (unlikely(!rc)) {
  5564. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5565. "0377 Error %d parsing vpd. "
  5566. "Using defaults.\n", rc);
  5567. rc = 0;
  5568. }
  5569. kfree(vpd);
  5570. /* Save information as VPD data */
  5571. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5572. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5573. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5574. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5575. &mqe->un.read_rev);
  5576. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5577. &mqe->un.read_rev);
  5578. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5579. &mqe->un.read_rev);
  5580. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5581. &mqe->un.read_rev);
  5582. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5583. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5584. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5585. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5586. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5587. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5588. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5589. "(%d):0380 READ_REV Status x%x "
  5590. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5591. mboxq->vport ? mboxq->vport->vpi : 0,
  5592. bf_get(lpfc_mqe_status, mqe),
  5593. phba->vpd.rev.opFwName,
  5594. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5595. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5596. /*
  5597. * Discover the port's supported feature set and match it against the
  5598. * hosts requests.
  5599. */
  5600. lpfc_request_features(phba, mboxq);
  5601. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5602. if (unlikely(rc)) {
  5603. rc = -EIO;
  5604. goto out_free_mbox;
  5605. }
  5606. /*
  5607. * The port must support FCP initiator mode as this is the
  5608. * only mode running in the host.
  5609. */
  5610. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5611. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5612. "0378 No support for fcpi mode.\n");
  5613. ftr_rsp++;
  5614. }
  5615. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5616. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5617. else
  5618. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5619. /*
  5620. * If the port cannot support the host's requested features
  5621. * then turn off the global config parameters to disable the
  5622. * feature in the driver. This is not a fatal error.
  5623. */
  5624. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5625. if (phba->cfg_enable_bg) {
  5626. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5627. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5628. else
  5629. ftr_rsp++;
  5630. }
  5631. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5632. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5633. ftr_rsp++;
  5634. if (ftr_rsp) {
  5635. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5636. "0379 Feature Mismatch Data: x%08x %08x "
  5637. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5638. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5639. phba->cfg_enable_npiv, phba->max_vpi);
  5640. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5641. phba->cfg_enable_bg = 0;
  5642. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5643. phba->cfg_enable_npiv = 0;
  5644. }
  5645. /* These SLI3 features are assumed in SLI4 */
  5646. spin_lock_irq(&phba->hbalock);
  5647. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5648. spin_unlock_irq(&phba->hbalock);
  5649. /*
  5650. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5651. * calls depends on these resources to complete port setup.
  5652. */
  5653. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5654. if (rc) {
  5655. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5656. "2920 Failed to alloc Resource IDs "
  5657. "rc = x%x\n", rc);
  5658. goto out_free_mbox;
  5659. }
  5660. /* Read the port's service parameters. */
  5661. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5662. if (rc) {
  5663. phba->link_state = LPFC_HBA_ERROR;
  5664. rc = -ENOMEM;
  5665. goto out_free_mbox;
  5666. }
  5667. mboxq->vport = vport;
  5668. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5669. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5670. if (rc == MBX_SUCCESS) {
  5671. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5672. rc = 0;
  5673. }
  5674. /*
  5675. * This memory was allocated by the lpfc_read_sparam routine. Release
  5676. * it to the mbuf pool.
  5677. */
  5678. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5679. kfree(mp);
  5680. mboxq->context1 = NULL;
  5681. if (unlikely(rc)) {
  5682. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5683. "0382 READ_SPARAM command failed "
  5684. "status %d, mbxStatus x%x\n",
  5685. rc, bf_get(lpfc_mqe_status, mqe));
  5686. phba->link_state = LPFC_HBA_ERROR;
  5687. rc = -EIO;
  5688. goto out_free_mbox;
  5689. }
  5690. lpfc_update_vport_wwn(vport);
  5691. /* Update the fc_host data structures with new wwn. */
  5692. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5693. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5694. /* update host els and scsi xri-sgl sizes and mappings */
  5695. rc = lpfc_sli4_xri_sgl_update(phba);
  5696. if (unlikely(rc)) {
  5697. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5698. "1400 Failed to update xri-sgl size and "
  5699. "mapping: %d\n", rc);
  5700. goto out_free_mbox;
  5701. }
  5702. /* register the els sgl pool to the port */
  5703. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5704. if (unlikely(rc)) {
  5705. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5706. "0582 Error %d during els sgl post "
  5707. "operation\n", rc);
  5708. rc = -ENODEV;
  5709. goto out_free_mbox;
  5710. }
  5711. /* register the allocated scsi sgl pool to the port */
  5712. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5713. if (unlikely(rc)) {
  5714. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5715. "0383 Error %d during scsi sgl post "
  5716. "operation\n", rc);
  5717. /* Some Scsi buffers were moved to the abort scsi list */
  5718. /* A pci function reset will repost them */
  5719. rc = -ENODEV;
  5720. goto out_free_mbox;
  5721. }
  5722. /* Post the rpi header region to the device. */
  5723. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5724. if (unlikely(rc)) {
  5725. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5726. "0393 Error %d during rpi post operation\n",
  5727. rc);
  5728. rc = -ENODEV;
  5729. goto out_free_mbox;
  5730. }
  5731. lpfc_sli4_node_prep(phba);
  5732. /* Create all the SLI4 queues */
  5733. rc = lpfc_sli4_queue_create(phba);
  5734. if (rc) {
  5735. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5736. "3089 Failed to allocate queues\n");
  5737. rc = -ENODEV;
  5738. goto out_stop_timers;
  5739. }
  5740. /* Set up all the queues to the device */
  5741. rc = lpfc_sli4_queue_setup(phba);
  5742. if (unlikely(rc)) {
  5743. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5744. "0381 Error %d during queue setup.\n ", rc);
  5745. goto out_destroy_queue;
  5746. }
  5747. /* Arm the CQs and then EQs on device */
  5748. lpfc_sli4_arm_cqeq_intr(phba);
  5749. /* Indicate device interrupt mode */
  5750. phba->sli4_hba.intr_enable = 1;
  5751. /* Allow asynchronous mailbox command to go through */
  5752. spin_lock_irq(&phba->hbalock);
  5753. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5754. spin_unlock_irq(&phba->hbalock);
  5755. /* Post receive buffers to the device */
  5756. lpfc_sli4_rb_setup(phba);
  5757. /* Reset HBA FCF states after HBA reset */
  5758. phba->fcf.fcf_flag = 0;
  5759. phba->fcf.current_rec.flag = 0;
  5760. /* Start the ELS watchdog timer */
  5761. mod_timer(&vport->els_tmofunc,
  5762. jiffies + HZ * (phba->fc_ratov * 2));
  5763. /* Start heart beat timer */
  5764. mod_timer(&phba->hb_tmofunc,
  5765. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5766. phba->hb_outstanding = 0;
  5767. phba->last_completion_time = jiffies;
  5768. /* Start error attention (ERATT) polling timer */
  5769. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5770. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5771. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5772. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5773. if (!rc) {
  5774. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5775. "2829 This device supports "
  5776. "Advanced Error Reporting (AER)\n");
  5777. spin_lock_irq(&phba->hbalock);
  5778. phba->hba_flag |= HBA_AER_ENABLED;
  5779. spin_unlock_irq(&phba->hbalock);
  5780. } else {
  5781. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5782. "2830 This device does not support "
  5783. "Advanced Error Reporting (AER)\n");
  5784. phba->cfg_aer_support = 0;
  5785. }
  5786. rc = 0;
  5787. }
  5788. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5789. /*
  5790. * The FC Port needs to register FCFI (index 0)
  5791. */
  5792. lpfc_reg_fcfi(phba, mboxq);
  5793. mboxq->vport = phba->pport;
  5794. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5795. if (rc != MBX_SUCCESS)
  5796. goto out_unset_queue;
  5797. rc = 0;
  5798. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5799. &mboxq->u.mqe.un.reg_fcfi);
  5800. /* Check if the port is configured to be disabled */
  5801. lpfc_sli_read_link_ste(phba);
  5802. }
  5803. /*
  5804. * The port is ready, set the host's link state to LINK_DOWN
  5805. * in preparation for link interrupts.
  5806. */
  5807. spin_lock_irq(&phba->hbalock);
  5808. phba->link_state = LPFC_LINK_DOWN;
  5809. spin_unlock_irq(&phba->hbalock);
  5810. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5811. (phba->hba_flag & LINK_DISABLED)) {
  5812. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5813. "3103 Adapter Link is disabled.\n");
  5814. lpfc_down_link(phba, mboxq);
  5815. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5816. if (rc != MBX_SUCCESS) {
  5817. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5818. "3104 Adapter failed to issue "
  5819. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5820. goto out_unset_queue;
  5821. }
  5822. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5823. /* don't perform init_link on SLI4 FC port loopback test */
  5824. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5825. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5826. if (rc)
  5827. goto out_unset_queue;
  5828. }
  5829. }
  5830. mempool_free(mboxq, phba->mbox_mem_pool);
  5831. return rc;
  5832. out_unset_queue:
  5833. /* Unset all the queues set up in this routine when error out */
  5834. lpfc_sli4_queue_unset(phba);
  5835. out_destroy_queue:
  5836. lpfc_sli4_queue_destroy(phba);
  5837. out_stop_timers:
  5838. lpfc_stop_hba_timers(phba);
  5839. out_free_mbox:
  5840. mempool_free(mboxq, phba->mbox_mem_pool);
  5841. return rc;
  5842. }
  5843. /**
  5844. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5845. * @ptr: context object - pointer to hba structure.
  5846. *
  5847. * This is the callback function for mailbox timer. The mailbox
  5848. * timer is armed when a new mailbox command is issued and the timer
  5849. * is deleted when the mailbox complete. The function is called by
  5850. * the kernel timer code when a mailbox does not complete within
  5851. * expected time. This function wakes up the worker thread to
  5852. * process the mailbox timeout and returns. All the processing is
  5853. * done by the worker thread function lpfc_mbox_timeout_handler.
  5854. **/
  5855. void
  5856. lpfc_mbox_timeout(unsigned long ptr)
  5857. {
  5858. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5859. unsigned long iflag;
  5860. uint32_t tmo_posted;
  5861. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5862. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5863. if (!tmo_posted)
  5864. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5865. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5866. if (!tmo_posted)
  5867. lpfc_worker_wake_up(phba);
  5868. return;
  5869. }
  5870. /**
  5871. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5872. * @phba: Pointer to HBA context object.
  5873. *
  5874. * This function is called from worker thread when a mailbox command times out.
  5875. * The caller is not required to hold any locks. This function will reset the
  5876. * HBA and recover all the pending commands.
  5877. **/
  5878. void
  5879. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5880. {
  5881. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5882. MAILBOX_t *mb = &pmbox->u.mb;
  5883. struct lpfc_sli *psli = &phba->sli;
  5884. struct lpfc_sli_ring *pring;
  5885. /* Check the pmbox pointer first. There is a race condition
  5886. * between the mbox timeout handler getting executed in the
  5887. * worklist and the mailbox actually completing. When this
  5888. * race condition occurs, the mbox_active will be NULL.
  5889. */
  5890. spin_lock_irq(&phba->hbalock);
  5891. if (pmbox == NULL) {
  5892. lpfc_printf_log(phba, KERN_WARNING,
  5893. LOG_MBOX | LOG_SLI,
  5894. "0353 Active Mailbox cleared - mailbox timeout "
  5895. "exiting\n");
  5896. spin_unlock_irq(&phba->hbalock);
  5897. return;
  5898. }
  5899. /* Mbox cmd <mbxCommand> timeout */
  5900. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5901. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5902. mb->mbxCommand,
  5903. phba->pport->port_state,
  5904. phba->sli.sli_flag,
  5905. phba->sli.mbox_active);
  5906. spin_unlock_irq(&phba->hbalock);
  5907. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5908. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5909. * it to fail all outstanding SCSI IO.
  5910. */
  5911. spin_lock_irq(&phba->pport->work_port_lock);
  5912. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5913. spin_unlock_irq(&phba->pport->work_port_lock);
  5914. spin_lock_irq(&phba->hbalock);
  5915. phba->link_state = LPFC_LINK_UNKNOWN;
  5916. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5917. spin_unlock_irq(&phba->hbalock);
  5918. pring = &psli->ring[psli->fcp_ring];
  5919. lpfc_sli_abort_iocb_ring(phba, pring);
  5920. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5921. "0345 Resetting board due to mailbox timeout\n");
  5922. /* Reset the HBA device */
  5923. lpfc_reset_hba(phba);
  5924. }
  5925. /**
  5926. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5927. * @phba: Pointer to HBA context object.
  5928. * @pmbox: Pointer to mailbox object.
  5929. * @flag: Flag indicating how the mailbox need to be processed.
  5930. *
  5931. * This function is called by discovery code and HBA management code
  5932. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5933. * function gets the hbalock to protect the data structures.
  5934. * The mailbox command can be submitted in polling mode, in which case
  5935. * this function will wait in a polling loop for the completion of the
  5936. * mailbox.
  5937. * If the mailbox is submitted in no_wait mode (not polling) the
  5938. * function will submit the command and returns immediately without waiting
  5939. * for the mailbox completion. The no_wait is supported only when HBA
  5940. * is in SLI2/SLI3 mode - interrupts are enabled.
  5941. * The SLI interface allows only one mailbox pending at a time. If the
  5942. * mailbox is issued in polling mode and there is already a mailbox
  5943. * pending, then the function will return an error. If the mailbox is issued
  5944. * in NO_WAIT mode and there is a mailbox pending already, the function
  5945. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5946. * The sli layer owns the mailbox object until the completion of mailbox
  5947. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5948. * return codes the caller owns the mailbox command after the return of
  5949. * the function.
  5950. **/
  5951. static int
  5952. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5953. uint32_t flag)
  5954. {
  5955. MAILBOX_t *mb;
  5956. struct lpfc_sli *psli = &phba->sli;
  5957. uint32_t status, evtctr;
  5958. uint32_t ha_copy, hc_copy;
  5959. int i;
  5960. unsigned long timeout;
  5961. unsigned long drvr_flag = 0;
  5962. uint32_t word0, ldata;
  5963. void __iomem *to_slim;
  5964. int processing_queue = 0;
  5965. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5966. if (!pmbox) {
  5967. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5968. /* processing mbox queue from intr_handler */
  5969. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5970. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5971. return MBX_SUCCESS;
  5972. }
  5973. processing_queue = 1;
  5974. pmbox = lpfc_mbox_get(phba);
  5975. if (!pmbox) {
  5976. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5977. return MBX_SUCCESS;
  5978. }
  5979. }
  5980. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5981. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5982. if(!pmbox->vport) {
  5983. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5984. lpfc_printf_log(phba, KERN_ERR,
  5985. LOG_MBOX | LOG_VPORT,
  5986. "1806 Mbox x%x failed. No vport\n",
  5987. pmbox->u.mb.mbxCommand);
  5988. dump_stack();
  5989. goto out_not_finished;
  5990. }
  5991. }
  5992. /* If the PCI channel is in offline state, do not post mbox. */
  5993. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5994. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5995. goto out_not_finished;
  5996. }
  5997. /* If HBA has a deferred error attention, fail the iocb. */
  5998. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5999. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6000. goto out_not_finished;
  6001. }
  6002. psli = &phba->sli;
  6003. mb = &pmbox->u.mb;
  6004. status = MBX_SUCCESS;
  6005. if (phba->link_state == LPFC_HBA_ERROR) {
  6006. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6007. /* Mbox command <mbxCommand> cannot issue */
  6008. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6009. "(%d):0311 Mailbox command x%x cannot "
  6010. "issue Data: x%x x%x\n",
  6011. pmbox->vport ? pmbox->vport->vpi : 0,
  6012. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6013. goto out_not_finished;
  6014. }
  6015. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6016. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6017. !(hc_copy & HC_MBINT_ENA)) {
  6018. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6019. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6020. "(%d):2528 Mailbox command x%x cannot "
  6021. "issue Data: x%x x%x\n",
  6022. pmbox->vport ? pmbox->vport->vpi : 0,
  6023. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6024. goto out_not_finished;
  6025. }
  6026. }
  6027. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6028. /* Polling for a mbox command when another one is already active
  6029. * is not allowed in SLI. Also, the driver must have established
  6030. * SLI2 mode to queue and process multiple mbox commands.
  6031. */
  6032. if (flag & MBX_POLL) {
  6033. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6034. /* Mbox command <mbxCommand> cannot issue */
  6035. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6036. "(%d):2529 Mailbox command x%x "
  6037. "cannot issue Data: x%x x%x\n",
  6038. pmbox->vport ? pmbox->vport->vpi : 0,
  6039. pmbox->u.mb.mbxCommand,
  6040. psli->sli_flag, flag);
  6041. goto out_not_finished;
  6042. }
  6043. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6044. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6045. /* Mbox command <mbxCommand> cannot issue */
  6046. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6047. "(%d):2530 Mailbox command x%x "
  6048. "cannot issue Data: x%x x%x\n",
  6049. pmbox->vport ? pmbox->vport->vpi : 0,
  6050. pmbox->u.mb.mbxCommand,
  6051. psli->sli_flag, flag);
  6052. goto out_not_finished;
  6053. }
  6054. /* Another mailbox command is still being processed, queue this
  6055. * command to be processed later.
  6056. */
  6057. lpfc_mbox_put(phba, pmbox);
  6058. /* Mbox cmd issue - BUSY */
  6059. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6060. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6061. "x%x x%x x%x x%x\n",
  6062. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6063. mb->mbxCommand, phba->pport->port_state,
  6064. psli->sli_flag, flag);
  6065. psli->slistat.mbox_busy++;
  6066. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6067. if (pmbox->vport) {
  6068. lpfc_debugfs_disc_trc(pmbox->vport,
  6069. LPFC_DISC_TRC_MBOX_VPORT,
  6070. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6071. (uint32_t)mb->mbxCommand,
  6072. mb->un.varWords[0], mb->un.varWords[1]);
  6073. }
  6074. else {
  6075. lpfc_debugfs_disc_trc(phba->pport,
  6076. LPFC_DISC_TRC_MBOX,
  6077. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6078. (uint32_t)mb->mbxCommand,
  6079. mb->un.varWords[0], mb->un.varWords[1]);
  6080. }
  6081. return MBX_BUSY;
  6082. }
  6083. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6084. /* If we are not polling, we MUST be in SLI2 mode */
  6085. if (flag != MBX_POLL) {
  6086. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6087. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6088. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6089. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6090. /* Mbox command <mbxCommand> cannot issue */
  6091. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6092. "(%d):2531 Mailbox command x%x "
  6093. "cannot issue Data: x%x x%x\n",
  6094. pmbox->vport ? pmbox->vport->vpi : 0,
  6095. pmbox->u.mb.mbxCommand,
  6096. psli->sli_flag, flag);
  6097. goto out_not_finished;
  6098. }
  6099. /* timeout active mbox command */
  6100. mod_timer(&psli->mbox_tmo, (jiffies +
  6101. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6102. }
  6103. /* Mailbox cmd <cmd> issue */
  6104. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6105. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6106. "x%x\n",
  6107. pmbox->vport ? pmbox->vport->vpi : 0,
  6108. mb->mbxCommand, phba->pport->port_state,
  6109. psli->sli_flag, flag);
  6110. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6111. if (pmbox->vport) {
  6112. lpfc_debugfs_disc_trc(pmbox->vport,
  6113. LPFC_DISC_TRC_MBOX_VPORT,
  6114. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6115. (uint32_t)mb->mbxCommand,
  6116. mb->un.varWords[0], mb->un.varWords[1]);
  6117. }
  6118. else {
  6119. lpfc_debugfs_disc_trc(phba->pport,
  6120. LPFC_DISC_TRC_MBOX,
  6121. "MBOX Send: cmd:x%x mb:x%x x%x",
  6122. (uint32_t)mb->mbxCommand,
  6123. mb->un.varWords[0], mb->un.varWords[1]);
  6124. }
  6125. }
  6126. psli->slistat.mbox_cmd++;
  6127. evtctr = psli->slistat.mbox_event;
  6128. /* next set own bit for the adapter and copy over command word */
  6129. mb->mbxOwner = OWN_CHIP;
  6130. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6131. /* Populate mbox extension offset word. */
  6132. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6133. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6134. = (uint8_t *)phba->mbox_ext
  6135. - (uint8_t *)phba->mbox;
  6136. }
  6137. /* Copy the mailbox extension data */
  6138. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6139. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6140. (uint8_t *)phba->mbox_ext,
  6141. pmbox->in_ext_byte_len);
  6142. }
  6143. /* Copy command data to host SLIM area */
  6144. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6145. } else {
  6146. /* Populate mbox extension offset word. */
  6147. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6148. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6149. = MAILBOX_HBA_EXT_OFFSET;
  6150. /* Copy the mailbox extension data */
  6151. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6152. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6153. MAILBOX_HBA_EXT_OFFSET,
  6154. pmbox->context2, pmbox->in_ext_byte_len);
  6155. }
  6156. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6157. /* copy command data into host mbox for cmpl */
  6158. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6159. }
  6160. /* First copy mbox command data to HBA SLIM, skip past first
  6161. word */
  6162. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6163. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6164. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6165. /* Next copy over first word, with mbxOwner set */
  6166. ldata = *((uint32_t *)mb);
  6167. to_slim = phba->MBslimaddr;
  6168. writel(ldata, to_slim);
  6169. readl(to_slim); /* flush */
  6170. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6171. /* switch over to host mailbox */
  6172. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6173. }
  6174. }
  6175. wmb();
  6176. switch (flag) {
  6177. case MBX_NOWAIT:
  6178. /* Set up reference to mailbox command */
  6179. psli->mbox_active = pmbox;
  6180. /* Interrupt board to do it */
  6181. writel(CA_MBATT, phba->CAregaddr);
  6182. readl(phba->CAregaddr); /* flush */
  6183. /* Don't wait for it to finish, just return */
  6184. break;
  6185. case MBX_POLL:
  6186. /* Set up null reference to mailbox command */
  6187. psli->mbox_active = NULL;
  6188. /* Interrupt board to do it */
  6189. writel(CA_MBATT, phba->CAregaddr);
  6190. readl(phba->CAregaddr); /* flush */
  6191. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6192. /* First read mbox status word */
  6193. word0 = *((uint32_t *)phba->mbox);
  6194. word0 = le32_to_cpu(word0);
  6195. } else {
  6196. /* First read mbox status word */
  6197. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6198. spin_unlock_irqrestore(&phba->hbalock,
  6199. drvr_flag);
  6200. goto out_not_finished;
  6201. }
  6202. }
  6203. /* Read the HBA Host Attention Register */
  6204. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6205. spin_unlock_irqrestore(&phba->hbalock,
  6206. drvr_flag);
  6207. goto out_not_finished;
  6208. }
  6209. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6210. 1000) + jiffies;
  6211. i = 0;
  6212. /* Wait for command to complete */
  6213. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6214. (!(ha_copy & HA_MBATT) &&
  6215. (phba->link_state > LPFC_WARM_START))) {
  6216. if (time_after(jiffies, timeout)) {
  6217. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6218. spin_unlock_irqrestore(&phba->hbalock,
  6219. drvr_flag);
  6220. goto out_not_finished;
  6221. }
  6222. /* Check if we took a mbox interrupt while we were
  6223. polling */
  6224. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6225. && (evtctr != psli->slistat.mbox_event))
  6226. break;
  6227. if (i++ > 10) {
  6228. spin_unlock_irqrestore(&phba->hbalock,
  6229. drvr_flag);
  6230. msleep(1);
  6231. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6232. }
  6233. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6234. /* First copy command data */
  6235. word0 = *((uint32_t *)phba->mbox);
  6236. word0 = le32_to_cpu(word0);
  6237. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6238. MAILBOX_t *slimmb;
  6239. uint32_t slimword0;
  6240. /* Check real SLIM for any errors */
  6241. slimword0 = readl(phba->MBslimaddr);
  6242. slimmb = (MAILBOX_t *) & slimword0;
  6243. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6244. && slimmb->mbxStatus) {
  6245. psli->sli_flag &=
  6246. ~LPFC_SLI_ACTIVE;
  6247. word0 = slimword0;
  6248. }
  6249. }
  6250. } else {
  6251. /* First copy command data */
  6252. word0 = readl(phba->MBslimaddr);
  6253. }
  6254. /* Read the HBA Host Attention Register */
  6255. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6256. spin_unlock_irqrestore(&phba->hbalock,
  6257. drvr_flag);
  6258. goto out_not_finished;
  6259. }
  6260. }
  6261. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6262. /* copy results back to user */
  6263. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6264. /* Copy the mailbox extension data */
  6265. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6266. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6267. pmbox->context2,
  6268. pmbox->out_ext_byte_len);
  6269. }
  6270. } else {
  6271. /* First copy command data */
  6272. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6273. MAILBOX_CMD_SIZE);
  6274. /* Copy the mailbox extension data */
  6275. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6276. lpfc_memcpy_from_slim(pmbox->context2,
  6277. phba->MBslimaddr +
  6278. MAILBOX_HBA_EXT_OFFSET,
  6279. pmbox->out_ext_byte_len);
  6280. }
  6281. }
  6282. writel(HA_MBATT, phba->HAregaddr);
  6283. readl(phba->HAregaddr); /* flush */
  6284. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6285. status = mb->mbxStatus;
  6286. }
  6287. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6288. return status;
  6289. out_not_finished:
  6290. if (processing_queue) {
  6291. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6292. lpfc_mbox_cmpl_put(phba, pmbox);
  6293. }
  6294. return MBX_NOT_FINISHED;
  6295. }
  6296. /**
  6297. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6298. * @phba: Pointer to HBA context object.
  6299. *
  6300. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6301. * the driver internal pending mailbox queue. It will then try to wait out the
  6302. * possible outstanding mailbox command before return.
  6303. *
  6304. * Returns:
  6305. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6306. * the outstanding mailbox command timed out.
  6307. **/
  6308. static int
  6309. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6310. {
  6311. struct lpfc_sli *psli = &phba->sli;
  6312. int rc = 0;
  6313. unsigned long timeout = 0;
  6314. /* Mark the asynchronous mailbox command posting as blocked */
  6315. spin_lock_irq(&phba->hbalock);
  6316. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6317. /* Determine how long we might wait for the active mailbox
  6318. * command to be gracefully completed by firmware.
  6319. */
  6320. if (phba->sli.mbox_active)
  6321. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6322. phba->sli.mbox_active) *
  6323. 1000) + jiffies;
  6324. spin_unlock_irq(&phba->hbalock);
  6325. /* Wait for the outstnading mailbox command to complete */
  6326. while (phba->sli.mbox_active) {
  6327. /* Check active mailbox complete status every 2ms */
  6328. msleep(2);
  6329. if (time_after(jiffies, timeout)) {
  6330. /* Timeout, marked the outstanding cmd not complete */
  6331. rc = 1;
  6332. break;
  6333. }
  6334. }
  6335. /* Can not cleanly block async mailbox command, fails it */
  6336. if (rc) {
  6337. spin_lock_irq(&phba->hbalock);
  6338. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6339. spin_unlock_irq(&phba->hbalock);
  6340. }
  6341. return rc;
  6342. }
  6343. /**
  6344. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6345. * @phba: Pointer to HBA context object.
  6346. *
  6347. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6348. * commands from the driver internal pending mailbox queue. It makes sure
  6349. * that there is no outstanding mailbox command before resuming posting
  6350. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6351. * mailbox command, it will try to wait it out before resuming asynchronous
  6352. * mailbox command posting.
  6353. **/
  6354. static void
  6355. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6356. {
  6357. struct lpfc_sli *psli = &phba->sli;
  6358. spin_lock_irq(&phba->hbalock);
  6359. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6360. /* Asynchronous mailbox posting is not blocked, do nothing */
  6361. spin_unlock_irq(&phba->hbalock);
  6362. return;
  6363. }
  6364. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6365. * successful or timeout, after timing-out the outstanding mailbox
  6366. * command shall always be removed, so just unblock posting async
  6367. * mailbox command and resume
  6368. */
  6369. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6370. spin_unlock_irq(&phba->hbalock);
  6371. /* wake up worker thread to post asynchronlous mailbox command */
  6372. lpfc_worker_wake_up(phba);
  6373. }
  6374. /**
  6375. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6376. * @phba: Pointer to HBA context object.
  6377. * @mboxq: Pointer to mailbox object.
  6378. *
  6379. * The function posts a mailbox to the port. The mailbox is expected
  6380. * to be comletely filled in and ready for the port to operate on it.
  6381. * This routine executes a synchronous completion operation on the
  6382. * mailbox by polling for its completion.
  6383. *
  6384. * The caller must not be holding any locks when calling this routine.
  6385. *
  6386. * Returns:
  6387. * MBX_SUCCESS - mailbox posted successfully
  6388. * Any of the MBX error values.
  6389. **/
  6390. static int
  6391. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6392. {
  6393. int rc = MBX_SUCCESS;
  6394. unsigned long iflag;
  6395. uint32_t db_ready;
  6396. uint32_t mcqe_status;
  6397. uint32_t mbx_cmnd;
  6398. unsigned long timeout;
  6399. struct lpfc_sli *psli = &phba->sli;
  6400. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6401. struct lpfc_bmbx_create *mbox_rgn;
  6402. struct dma_address *dma_address;
  6403. struct lpfc_register bmbx_reg;
  6404. /*
  6405. * Only one mailbox can be active to the bootstrap mailbox region
  6406. * at a time and there is no queueing provided.
  6407. */
  6408. spin_lock_irqsave(&phba->hbalock, iflag);
  6409. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6410. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6411. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6412. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6413. "cannot issue Data: x%x x%x\n",
  6414. mboxq->vport ? mboxq->vport->vpi : 0,
  6415. mboxq->u.mb.mbxCommand,
  6416. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6417. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6418. psli->sli_flag, MBX_POLL);
  6419. return MBXERR_ERROR;
  6420. }
  6421. /* The server grabs the token and owns it until release */
  6422. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6423. phba->sli.mbox_active = mboxq;
  6424. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6425. /*
  6426. * Initialize the bootstrap memory region to avoid stale data areas
  6427. * in the mailbox post. Then copy the caller's mailbox contents to
  6428. * the bmbx mailbox region.
  6429. */
  6430. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6431. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6432. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6433. sizeof(struct lpfc_mqe));
  6434. /* Post the high mailbox dma address to the port and wait for ready. */
  6435. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6436. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6437. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6438. * 1000) + jiffies;
  6439. do {
  6440. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6441. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6442. if (!db_ready)
  6443. msleep(2);
  6444. if (time_after(jiffies, timeout)) {
  6445. rc = MBXERR_ERROR;
  6446. goto exit;
  6447. }
  6448. } while (!db_ready);
  6449. /* Post the low mailbox dma address to the port. */
  6450. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6451. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6452. * 1000) + jiffies;
  6453. do {
  6454. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6455. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6456. if (!db_ready)
  6457. msleep(2);
  6458. if (time_after(jiffies, timeout)) {
  6459. rc = MBXERR_ERROR;
  6460. goto exit;
  6461. }
  6462. } while (!db_ready);
  6463. /*
  6464. * Read the CQ to ensure the mailbox has completed.
  6465. * If so, update the mailbox status so that the upper layers
  6466. * can complete the request normally.
  6467. */
  6468. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6469. sizeof(struct lpfc_mqe));
  6470. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6471. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6472. sizeof(struct lpfc_mcqe));
  6473. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6474. /*
  6475. * When the CQE status indicates a failure and the mailbox status
  6476. * indicates success then copy the CQE status into the mailbox status
  6477. * (and prefix it with x4000).
  6478. */
  6479. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6480. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6481. bf_set(lpfc_mqe_status, mb,
  6482. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6483. rc = MBXERR_ERROR;
  6484. } else
  6485. lpfc_sli4_swap_str(phba, mboxq);
  6486. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6487. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6488. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6489. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6490. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6491. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6492. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6493. bf_get(lpfc_mqe_status, mb),
  6494. mb->un.mb_words[0], mb->un.mb_words[1],
  6495. mb->un.mb_words[2], mb->un.mb_words[3],
  6496. mb->un.mb_words[4], mb->un.mb_words[5],
  6497. mb->un.mb_words[6], mb->un.mb_words[7],
  6498. mb->un.mb_words[8], mb->un.mb_words[9],
  6499. mb->un.mb_words[10], mb->un.mb_words[11],
  6500. mb->un.mb_words[12], mboxq->mcqe.word0,
  6501. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6502. mboxq->mcqe.trailer);
  6503. exit:
  6504. /* We are holding the token, no needed for lock when release */
  6505. spin_lock_irqsave(&phba->hbalock, iflag);
  6506. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6507. phba->sli.mbox_active = NULL;
  6508. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6509. return rc;
  6510. }
  6511. /**
  6512. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6513. * @phba: Pointer to HBA context object.
  6514. * @pmbox: Pointer to mailbox object.
  6515. * @flag: Flag indicating how the mailbox need to be processed.
  6516. *
  6517. * This function is called by discovery code and HBA management code to submit
  6518. * a mailbox command to firmware with SLI-4 interface spec.
  6519. *
  6520. * Return codes the caller owns the mailbox command after the return of the
  6521. * function.
  6522. **/
  6523. static int
  6524. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6525. uint32_t flag)
  6526. {
  6527. struct lpfc_sli *psli = &phba->sli;
  6528. unsigned long iflags;
  6529. int rc;
  6530. /* dump from issue mailbox command if setup */
  6531. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6532. rc = lpfc_mbox_dev_check(phba);
  6533. if (unlikely(rc)) {
  6534. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6535. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6536. "cannot issue Data: x%x x%x\n",
  6537. mboxq->vport ? mboxq->vport->vpi : 0,
  6538. mboxq->u.mb.mbxCommand,
  6539. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6540. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6541. psli->sli_flag, flag);
  6542. goto out_not_finished;
  6543. }
  6544. /* Detect polling mode and jump to a handler */
  6545. if (!phba->sli4_hba.intr_enable) {
  6546. if (flag == MBX_POLL)
  6547. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6548. else
  6549. rc = -EIO;
  6550. if (rc != MBX_SUCCESS)
  6551. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6552. "(%d):2541 Mailbox command x%x "
  6553. "(x%x/x%x) failure: "
  6554. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6555. "Data: x%x x%x\n,",
  6556. mboxq->vport ? mboxq->vport->vpi : 0,
  6557. mboxq->u.mb.mbxCommand,
  6558. lpfc_sli_config_mbox_subsys_get(phba,
  6559. mboxq),
  6560. lpfc_sli_config_mbox_opcode_get(phba,
  6561. mboxq),
  6562. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6563. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6564. bf_get(lpfc_mcqe_ext_status,
  6565. &mboxq->mcqe),
  6566. psli->sli_flag, flag);
  6567. return rc;
  6568. } else if (flag == MBX_POLL) {
  6569. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6570. "(%d):2542 Try to issue mailbox command "
  6571. "x%x (x%x/x%x) synchronously ahead of async"
  6572. "mailbox command queue: 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, flag);
  6578. /* Try to block the asynchronous mailbox posting */
  6579. rc = lpfc_sli4_async_mbox_block(phba);
  6580. if (!rc) {
  6581. /* Successfully blocked, now issue sync mbox cmd */
  6582. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6583. if (rc != MBX_SUCCESS)
  6584. lpfc_printf_log(phba, KERN_WARNING,
  6585. LOG_MBOX | LOG_SLI,
  6586. "(%d):2597 Sync Mailbox command "
  6587. "x%x (x%x/x%x) failure: "
  6588. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6589. "Data: x%x x%x\n,",
  6590. mboxq->vport ? mboxq->vport->vpi : 0,
  6591. mboxq->u.mb.mbxCommand,
  6592. lpfc_sli_config_mbox_subsys_get(phba,
  6593. mboxq),
  6594. lpfc_sli_config_mbox_opcode_get(phba,
  6595. mboxq),
  6596. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6597. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6598. bf_get(lpfc_mcqe_ext_status,
  6599. &mboxq->mcqe),
  6600. psli->sli_flag, flag);
  6601. /* Unblock the async mailbox posting afterward */
  6602. lpfc_sli4_async_mbox_unblock(phba);
  6603. }
  6604. return rc;
  6605. }
  6606. /* Now, interrupt mode asynchrous mailbox command */
  6607. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6608. if (rc) {
  6609. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6610. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6611. "cannot issue Data: x%x x%x\n",
  6612. mboxq->vport ? mboxq->vport->vpi : 0,
  6613. mboxq->u.mb.mbxCommand,
  6614. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6615. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6616. psli->sli_flag, flag);
  6617. goto out_not_finished;
  6618. }
  6619. /* Put the mailbox command to the driver internal FIFO */
  6620. psli->slistat.mbox_busy++;
  6621. spin_lock_irqsave(&phba->hbalock, iflags);
  6622. lpfc_mbox_put(phba, mboxq);
  6623. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6624. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6625. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6626. "x%x (x%x/x%x) x%x x%x x%x\n",
  6627. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6628. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6629. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6630. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6631. phba->pport->port_state,
  6632. psli->sli_flag, MBX_NOWAIT);
  6633. /* Wake up worker thread to transport mailbox command from head */
  6634. lpfc_worker_wake_up(phba);
  6635. return MBX_BUSY;
  6636. out_not_finished:
  6637. return MBX_NOT_FINISHED;
  6638. }
  6639. /**
  6640. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6641. * @phba: Pointer to HBA context object.
  6642. *
  6643. * This function is called by worker thread to send a mailbox command to
  6644. * SLI4 HBA firmware.
  6645. *
  6646. **/
  6647. int
  6648. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6649. {
  6650. struct lpfc_sli *psli = &phba->sli;
  6651. LPFC_MBOXQ_t *mboxq;
  6652. int rc = MBX_SUCCESS;
  6653. unsigned long iflags;
  6654. struct lpfc_mqe *mqe;
  6655. uint32_t mbx_cmnd;
  6656. /* Check interrupt mode before post async mailbox command */
  6657. if (unlikely(!phba->sli4_hba.intr_enable))
  6658. return MBX_NOT_FINISHED;
  6659. /* Check for mailbox command service token */
  6660. spin_lock_irqsave(&phba->hbalock, iflags);
  6661. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6662. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6663. return MBX_NOT_FINISHED;
  6664. }
  6665. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6666. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6667. return MBX_NOT_FINISHED;
  6668. }
  6669. if (unlikely(phba->sli.mbox_active)) {
  6670. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6671. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6672. "0384 There is pending active mailbox cmd\n");
  6673. return MBX_NOT_FINISHED;
  6674. }
  6675. /* Take the mailbox command service token */
  6676. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6677. /* Get the next mailbox command from head of queue */
  6678. mboxq = lpfc_mbox_get(phba);
  6679. /* If no more mailbox command waiting for post, we're done */
  6680. if (!mboxq) {
  6681. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6682. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6683. return MBX_SUCCESS;
  6684. }
  6685. phba->sli.mbox_active = mboxq;
  6686. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6687. /* Check device readiness for posting mailbox command */
  6688. rc = lpfc_mbox_dev_check(phba);
  6689. if (unlikely(rc))
  6690. /* Driver clean routine will clean up pending mailbox */
  6691. goto out_not_finished;
  6692. /* Prepare the mbox command to be posted */
  6693. mqe = &mboxq->u.mqe;
  6694. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6695. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6696. mod_timer(&psli->mbox_tmo, (jiffies +
  6697. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6698. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6699. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6700. "x%x x%x\n",
  6701. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6702. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6703. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6704. phba->pport->port_state, psli->sli_flag);
  6705. if (mbx_cmnd != MBX_HEARTBEAT) {
  6706. if (mboxq->vport) {
  6707. lpfc_debugfs_disc_trc(mboxq->vport,
  6708. LPFC_DISC_TRC_MBOX_VPORT,
  6709. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6710. mbx_cmnd, mqe->un.mb_words[0],
  6711. mqe->un.mb_words[1]);
  6712. } else {
  6713. lpfc_debugfs_disc_trc(phba->pport,
  6714. LPFC_DISC_TRC_MBOX,
  6715. "MBOX Send: cmd:x%x mb:x%x x%x",
  6716. mbx_cmnd, mqe->un.mb_words[0],
  6717. mqe->un.mb_words[1]);
  6718. }
  6719. }
  6720. psli->slistat.mbox_cmd++;
  6721. /* Post the mailbox command to the port */
  6722. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6723. if (rc != MBX_SUCCESS) {
  6724. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6725. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6726. "cannot issue Data: x%x x%x\n",
  6727. mboxq->vport ? mboxq->vport->vpi : 0,
  6728. mboxq->u.mb.mbxCommand,
  6729. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6730. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6731. psli->sli_flag, MBX_NOWAIT);
  6732. goto out_not_finished;
  6733. }
  6734. return rc;
  6735. out_not_finished:
  6736. spin_lock_irqsave(&phba->hbalock, iflags);
  6737. if (phba->sli.mbox_active) {
  6738. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6739. __lpfc_mbox_cmpl_put(phba, mboxq);
  6740. /* Release the token */
  6741. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6742. phba->sli.mbox_active = NULL;
  6743. }
  6744. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6745. return MBX_NOT_FINISHED;
  6746. }
  6747. /**
  6748. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6749. * @phba: Pointer to HBA context object.
  6750. * @pmbox: Pointer to mailbox object.
  6751. * @flag: Flag indicating how the mailbox need to be processed.
  6752. *
  6753. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6754. * the API jump table function pointer from the lpfc_hba struct.
  6755. *
  6756. * Return codes the caller owns the mailbox command after the return of the
  6757. * function.
  6758. **/
  6759. int
  6760. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6761. {
  6762. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6763. }
  6764. /**
  6765. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6766. * @phba: The hba struct for which this call is being executed.
  6767. * @dev_grp: The HBA PCI-Device group number.
  6768. *
  6769. * This routine sets up the mbox interface API function jump table in @phba
  6770. * struct.
  6771. * Returns: 0 - success, -ENODEV - failure.
  6772. **/
  6773. int
  6774. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6775. {
  6776. switch (dev_grp) {
  6777. case LPFC_PCI_DEV_LP:
  6778. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6779. phba->lpfc_sli_handle_slow_ring_event =
  6780. lpfc_sli_handle_slow_ring_event_s3;
  6781. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6782. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6783. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6784. break;
  6785. case LPFC_PCI_DEV_OC:
  6786. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6787. phba->lpfc_sli_handle_slow_ring_event =
  6788. lpfc_sli_handle_slow_ring_event_s4;
  6789. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6790. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6791. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6792. break;
  6793. default:
  6794. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6795. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6796. dev_grp);
  6797. return -ENODEV;
  6798. break;
  6799. }
  6800. return 0;
  6801. }
  6802. /**
  6803. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6804. * @phba: Pointer to HBA context object.
  6805. * @pring: Pointer to driver SLI ring object.
  6806. * @piocb: Pointer to address of newly added command iocb.
  6807. *
  6808. * This function is called with hbalock held to add a command
  6809. * iocb to the txq when SLI layer cannot submit the command iocb
  6810. * to the ring.
  6811. **/
  6812. void
  6813. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6814. struct lpfc_iocbq *piocb)
  6815. {
  6816. /* Insert the caller's iocb in the txq tail for later processing. */
  6817. list_add_tail(&piocb->list, &pring->txq);
  6818. pring->txq_cnt++;
  6819. }
  6820. /**
  6821. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6822. * @phba: Pointer to HBA context object.
  6823. * @pring: Pointer to driver SLI ring object.
  6824. * @piocb: Pointer to address of newly added command iocb.
  6825. *
  6826. * This function is called with hbalock held before a new
  6827. * iocb is submitted to the firmware. This function checks
  6828. * txq to flush the iocbs in txq to Firmware before
  6829. * submitting new iocbs to the Firmware.
  6830. * If there are iocbs in the txq which need to be submitted
  6831. * to firmware, lpfc_sli_next_iocb returns the first element
  6832. * of the txq after dequeuing it from txq.
  6833. * If there is no iocb in the txq then the function will return
  6834. * *piocb and *piocb is set to NULL. Caller needs to check
  6835. * *piocb to find if there are more commands in the txq.
  6836. **/
  6837. static struct lpfc_iocbq *
  6838. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6839. struct lpfc_iocbq **piocb)
  6840. {
  6841. struct lpfc_iocbq * nextiocb;
  6842. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6843. if (!nextiocb) {
  6844. nextiocb = *piocb;
  6845. *piocb = NULL;
  6846. }
  6847. return nextiocb;
  6848. }
  6849. /**
  6850. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6851. * @phba: Pointer to HBA context object.
  6852. * @ring_number: SLI ring number to issue iocb on.
  6853. * @piocb: Pointer to command iocb.
  6854. * @flag: Flag indicating if this command can be put into txq.
  6855. *
  6856. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6857. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6858. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6859. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6860. * this function allows only iocbs for posting buffers. This function finds
  6861. * next available slot in the command ring and posts the command to the
  6862. * available slot and writes the port attention register to request HBA start
  6863. * processing new iocb. If there is no slot available in the ring and
  6864. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6865. * the function returns IOCB_BUSY.
  6866. *
  6867. * This function is called with hbalock held. The function will return success
  6868. * after it successfully submit the iocb to firmware or after adding to the
  6869. * txq.
  6870. **/
  6871. static int
  6872. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6873. struct lpfc_iocbq *piocb, uint32_t flag)
  6874. {
  6875. struct lpfc_iocbq *nextiocb;
  6876. IOCB_t *iocb;
  6877. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6878. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6879. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6880. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6881. lpfc_printf_log(phba, KERN_ERR,
  6882. LOG_SLI | LOG_VPORT,
  6883. "1807 IOCB x%x failed. No vport\n",
  6884. piocb->iocb.ulpCommand);
  6885. dump_stack();
  6886. return IOCB_ERROR;
  6887. }
  6888. /* If the PCI channel is in offline state, do not post iocbs. */
  6889. if (unlikely(pci_channel_offline(phba->pcidev)))
  6890. return IOCB_ERROR;
  6891. /* If HBA has a deferred error attention, fail the iocb. */
  6892. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6893. return IOCB_ERROR;
  6894. /*
  6895. * We should never get an IOCB if we are in a < LINK_DOWN state
  6896. */
  6897. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6898. return IOCB_ERROR;
  6899. /*
  6900. * Check to see if we are blocking IOCB processing because of a
  6901. * outstanding event.
  6902. */
  6903. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6904. goto iocb_busy;
  6905. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6906. /*
  6907. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6908. * can be issued if the link is not up.
  6909. */
  6910. switch (piocb->iocb.ulpCommand) {
  6911. case CMD_GEN_REQUEST64_CR:
  6912. case CMD_GEN_REQUEST64_CX:
  6913. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6914. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6915. FC_RCTL_DD_UNSOL_CMD) ||
  6916. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6917. MENLO_TRANSPORT_TYPE))
  6918. goto iocb_busy;
  6919. break;
  6920. case CMD_QUE_RING_BUF_CN:
  6921. case CMD_QUE_RING_BUF64_CN:
  6922. /*
  6923. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6924. * completion, iocb_cmpl MUST be 0.
  6925. */
  6926. if (piocb->iocb_cmpl)
  6927. piocb->iocb_cmpl = NULL;
  6928. /*FALLTHROUGH*/
  6929. case CMD_CREATE_XRI_CR:
  6930. case CMD_CLOSE_XRI_CN:
  6931. case CMD_CLOSE_XRI_CX:
  6932. break;
  6933. default:
  6934. goto iocb_busy;
  6935. }
  6936. /*
  6937. * For FCP commands, we must be in a state where we can process link
  6938. * attention events.
  6939. */
  6940. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6941. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6942. goto iocb_busy;
  6943. }
  6944. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6945. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6946. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6947. if (iocb)
  6948. lpfc_sli_update_ring(phba, pring);
  6949. else
  6950. lpfc_sli_update_full_ring(phba, pring);
  6951. if (!piocb)
  6952. return IOCB_SUCCESS;
  6953. goto out_busy;
  6954. iocb_busy:
  6955. pring->stats.iocb_cmd_delay++;
  6956. out_busy:
  6957. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6958. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6959. return IOCB_SUCCESS;
  6960. }
  6961. return IOCB_BUSY;
  6962. }
  6963. /**
  6964. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6965. * @phba: Pointer to HBA context object.
  6966. * @piocb: Pointer to command iocb.
  6967. * @sglq: Pointer to the scatter gather queue object.
  6968. *
  6969. * This routine converts the bpl or bde that is in the IOCB
  6970. * to a sgl list for the sli4 hardware. The physical address
  6971. * of the bpl/bde is converted back to a virtual address.
  6972. * If the IOCB contains a BPL then the list of BDE's is
  6973. * converted to sli4_sge's. If the IOCB contains a single
  6974. * BDE then it is converted to a single sli_sge.
  6975. * The IOCB is still in cpu endianess so the contents of
  6976. * the bpl can be used without byte swapping.
  6977. *
  6978. * Returns valid XRI = Success, NO_XRI = Failure.
  6979. **/
  6980. static uint16_t
  6981. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6982. struct lpfc_sglq *sglq)
  6983. {
  6984. uint16_t xritag = NO_XRI;
  6985. struct ulp_bde64 *bpl = NULL;
  6986. struct ulp_bde64 bde;
  6987. struct sli4_sge *sgl = NULL;
  6988. struct lpfc_dmabuf *dmabuf;
  6989. IOCB_t *icmd;
  6990. int numBdes = 0;
  6991. int i = 0;
  6992. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6993. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6994. if (!piocbq || !sglq)
  6995. return xritag;
  6996. sgl = (struct sli4_sge *)sglq->sgl;
  6997. icmd = &piocbq->iocb;
  6998. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6999. return sglq->sli4_xritag;
  7000. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7001. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7002. sizeof(struct ulp_bde64);
  7003. /* The addrHigh and addrLow fields within the IOCB
  7004. * have not been byteswapped yet so there is no
  7005. * need to swap them back.
  7006. */
  7007. if (piocbq->context3)
  7008. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7009. else
  7010. return xritag;
  7011. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7012. if (!bpl)
  7013. return xritag;
  7014. for (i = 0; i < numBdes; i++) {
  7015. /* Should already be byte swapped. */
  7016. sgl->addr_hi = bpl->addrHigh;
  7017. sgl->addr_lo = bpl->addrLow;
  7018. sgl->word2 = le32_to_cpu(sgl->word2);
  7019. if ((i+1) == numBdes)
  7020. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7021. else
  7022. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7023. /* swap the size field back to the cpu so we
  7024. * can assign it to the sgl.
  7025. */
  7026. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7027. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7028. /* The offsets in the sgl need to be accumulated
  7029. * separately for the request and reply lists.
  7030. * The request is always first, the reply follows.
  7031. */
  7032. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7033. /* add up the reply sg entries */
  7034. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7035. inbound++;
  7036. /* first inbound? reset the offset */
  7037. if (inbound == 1)
  7038. offset = 0;
  7039. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7040. bf_set(lpfc_sli4_sge_type, sgl,
  7041. LPFC_SGE_TYPE_DATA);
  7042. offset += bde.tus.f.bdeSize;
  7043. }
  7044. sgl->word2 = cpu_to_le32(sgl->word2);
  7045. bpl++;
  7046. sgl++;
  7047. }
  7048. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7049. /* The addrHigh and addrLow fields of the BDE have not
  7050. * been byteswapped yet so they need to be swapped
  7051. * before putting them in the sgl.
  7052. */
  7053. sgl->addr_hi =
  7054. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7055. sgl->addr_lo =
  7056. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7057. sgl->word2 = le32_to_cpu(sgl->word2);
  7058. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7059. sgl->word2 = cpu_to_le32(sgl->word2);
  7060. sgl->sge_len =
  7061. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7062. }
  7063. return sglq->sli4_xritag;
  7064. }
  7065. /**
  7066. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7067. * @phba: Pointer to HBA context object.
  7068. *
  7069. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7070. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7071. * held.
  7072. *
  7073. * Return: index into SLI4 fast-path FCP queue index.
  7074. **/
  7075. static uint32_t
  7076. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7077. {
  7078. ++phba->fcp_qidx;
  7079. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  7080. phba->fcp_qidx = 0;
  7081. return phba->fcp_qidx;
  7082. }
  7083. /**
  7084. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7085. * @phba: Pointer to HBA context object.
  7086. * @piocb: Pointer to command iocb.
  7087. * @wqe: Pointer to the work queue entry.
  7088. *
  7089. * This routine converts the iocb command to its Work Queue Entry
  7090. * equivalent. The wqe pointer should not have any fields set when
  7091. * this routine is called because it will memcpy over them.
  7092. * This routine does not set the CQ_ID or the WQEC bits in the
  7093. * wqe.
  7094. *
  7095. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7096. **/
  7097. static int
  7098. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7099. union lpfc_wqe *wqe)
  7100. {
  7101. uint32_t xmit_len = 0, total_len = 0;
  7102. uint8_t ct = 0;
  7103. uint32_t fip;
  7104. uint32_t abort_tag;
  7105. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7106. uint8_t cmnd;
  7107. uint16_t xritag;
  7108. uint16_t abrt_iotag;
  7109. struct lpfc_iocbq *abrtiocbq;
  7110. struct ulp_bde64 *bpl = NULL;
  7111. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7112. int numBdes, i;
  7113. struct ulp_bde64 bde;
  7114. struct lpfc_nodelist *ndlp;
  7115. uint32_t *pcmd;
  7116. uint32_t if_type;
  7117. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7118. /* The fcp commands will set command type */
  7119. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7120. command_type = FCP_COMMAND;
  7121. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7122. command_type = ELS_COMMAND_FIP;
  7123. else
  7124. command_type = ELS_COMMAND_NON_FIP;
  7125. /* Some of the fields are in the right position already */
  7126. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7127. abort_tag = (uint32_t) iocbq->iotag;
  7128. xritag = iocbq->sli4_xritag;
  7129. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7130. /* words0-2 bpl convert bde */
  7131. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7132. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7133. sizeof(struct ulp_bde64);
  7134. bpl = (struct ulp_bde64 *)
  7135. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7136. if (!bpl)
  7137. return IOCB_ERROR;
  7138. /* Should already be byte swapped. */
  7139. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7140. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7141. /* swap the size field back to the cpu so we
  7142. * can assign it to the sgl.
  7143. */
  7144. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7145. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7146. total_len = 0;
  7147. for (i = 0; i < numBdes; i++) {
  7148. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7149. total_len += bde.tus.f.bdeSize;
  7150. }
  7151. } else
  7152. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7153. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7154. cmnd = iocbq->iocb.ulpCommand;
  7155. switch (iocbq->iocb.ulpCommand) {
  7156. case CMD_ELS_REQUEST64_CR:
  7157. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7158. ndlp = iocbq->context_un.ndlp;
  7159. else
  7160. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7161. if (!iocbq->iocb.ulpLe) {
  7162. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7163. "2007 Only Limited Edition cmd Format"
  7164. " supported 0x%x\n",
  7165. iocbq->iocb.ulpCommand);
  7166. return IOCB_ERROR;
  7167. }
  7168. wqe->els_req.payload_len = xmit_len;
  7169. /* Els_reguest64 has a TMO */
  7170. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7171. iocbq->iocb.ulpTimeout);
  7172. /* Need a VF for word 4 set the vf bit*/
  7173. bf_set(els_req64_vf, &wqe->els_req, 0);
  7174. /* And a VFID for word 12 */
  7175. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7176. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7177. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7178. iocbq->iocb.ulpContext);
  7179. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7180. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7181. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7182. if (command_type == ELS_COMMAND_FIP)
  7183. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7184. >> LPFC_FIP_ELS_ID_SHIFT);
  7185. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7186. iocbq->context2)->virt);
  7187. if_type = bf_get(lpfc_sli_intf_if_type,
  7188. &phba->sli4_hba.sli_intf);
  7189. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7190. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7191. *pcmd == ELS_CMD_SCR ||
  7192. *pcmd == ELS_CMD_FDISC ||
  7193. *pcmd == ELS_CMD_LOGO ||
  7194. *pcmd == ELS_CMD_PLOGI)) {
  7195. bf_set(els_req64_sp, &wqe->els_req, 1);
  7196. bf_set(els_req64_sid, &wqe->els_req,
  7197. iocbq->vport->fc_myDID);
  7198. if ((*pcmd == ELS_CMD_FLOGI) &&
  7199. !(phba->fc_topology ==
  7200. LPFC_TOPOLOGY_LOOP))
  7201. bf_set(els_req64_sid, &wqe->els_req, 0);
  7202. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7203. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7204. phba->vpi_ids[iocbq->vport->vpi]);
  7205. } else if (pcmd && iocbq->context1) {
  7206. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7207. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7208. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7209. }
  7210. }
  7211. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7212. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7213. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7214. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7215. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7216. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7217. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7218. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7219. break;
  7220. case CMD_XMIT_SEQUENCE64_CX:
  7221. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7222. iocbq->iocb.un.ulpWord[3]);
  7223. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7224. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7225. /* The entire sequence is transmitted for this IOCB */
  7226. xmit_len = total_len;
  7227. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7228. if (phba->link_flag & LS_LOOPBACK_MODE)
  7229. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7230. case CMD_XMIT_SEQUENCE64_CR:
  7231. /* word3 iocb=io_tag32 wqe=reserved */
  7232. wqe->xmit_sequence.rsvd3 = 0;
  7233. /* word4 relative_offset memcpy */
  7234. /* word5 r_ctl/df_ctl memcpy */
  7235. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7236. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7237. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7238. LPFC_WQE_IOD_WRITE);
  7239. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7240. LPFC_WQE_LENLOC_WORD12);
  7241. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7242. wqe->xmit_sequence.xmit_len = xmit_len;
  7243. command_type = OTHER_COMMAND;
  7244. break;
  7245. case CMD_XMIT_BCAST64_CN:
  7246. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7247. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7248. /* word4 iocb=rsvd wqe=rsvd */
  7249. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7250. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7251. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7252. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7253. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7254. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7255. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7256. LPFC_WQE_LENLOC_WORD3);
  7257. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7258. break;
  7259. case CMD_FCP_IWRITE64_CR:
  7260. command_type = FCP_COMMAND_DATA_OUT;
  7261. /* word3 iocb=iotag wqe=payload_offset_len */
  7262. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7263. wqe->fcp_iwrite.payload_offset_len =
  7264. xmit_len + sizeof(struct fcp_rsp);
  7265. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7266. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7267. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7268. iocbq->iocb.ulpFCP2Rcvy);
  7269. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7270. /* Always open the exchange */
  7271. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7272. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7273. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7274. LPFC_WQE_LENLOC_WORD4);
  7275. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7276. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7277. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7278. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7279. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7280. }
  7281. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7282. break;
  7283. case CMD_FCP_IREAD64_CR:
  7284. /* word3 iocb=iotag wqe=payload_offset_len */
  7285. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7286. wqe->fcp_iread.payload_offset_len =
  7287. xmit_len + sizeof(struct fcp_rsp);
  7288. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7289. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7290. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7291. iocbq->iocb.ulpFCP2Rcvy);
  7292. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7293. /* Always open the exchange */
  7294. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7295. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7296. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7297. LPFC_WQE_LENLOC_WORD4);
  7298. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7299. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7300. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7301. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7302. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7303. }
  7304. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7305. break;
  7306. case CMD_FCP_ICMND64_CR:
  7307. /* word3 iocb=IO_TAG wqe=reserved */
  7308. wqe->fcp_icmd.rsrvd3 = 0;
  7309. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7310. /* Always open the exchange */
  7311. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7312. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7313. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7314. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7315. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7316. LPFC_WQE_LENLOC_NONE);
  7317. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7318. break;
  7319. case CMD_GEN_REQUEST64_CR:
  7320. /* For this command calculate the xmit length of the
  7321. * request bde.
  7322. */
  7323. xmit_len = 0;
  7324. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7325. sizeof(struct ulp_bde64);
  7326. for (i = 0; i < numBdes; i++) {
  7327. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7328. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7329. break;
  7330. xmit_len += bde.tus.f.bdeSize;
  7331. }
  7332. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7333. wqe->gen_req.request_payload_len = xmit_len;
  7334. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7335. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7336. /* word6 context tag copied in memcpy */
  7337. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7338. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7339. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7340. "2015 Invalid CT %x command 0x%x\n",
  7341. ct, iocbq->iocb.ulpCommand);
  7342. return IOCB_ERROR;
  7343. }
  7344. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7345. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7346. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7347. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7348. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7349. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7350. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7351. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7352. command_type = OTHER_COMMAND;
  7353. break;
  7354. case CMD_XMIT_ELS_RSP64_CX:
  7355. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7356. /* words0-2 BDE memcpy */
  7357. /* word3 iocb=iotag32 wqe=response_payload_len */
  7358. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7359. /* word4 */
  7360. wqe->xmit_els_rsp.word4 = 0;
  7361. /* word5 iocb=rsvd wge=did */
  7362. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7363. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7364. if_type = bf_get(lpfc_sli_intf_if_type,
  7365. &phba->sli4_hba.sli_intf);
  7366. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7367. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7368. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7369. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7370. iocbq->vport->fc_myDID);
  7371. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7372. bf_set(wqe_els_did,
  7373. &wqe->xmit_els_rsp.wqe_dest, 0);
  7374. }
  7375. }
  7376. }
  7377. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7378. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7379. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7380. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7381. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7382. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7383. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7384. phba->vpi_ids[iocbq->vport->vpi]);
  7385. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7386. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7387. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7388. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7389. LPFC_WQE_LENLOC_WORD3);
  7390. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7391. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7392. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7393. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7394. iocbq->context2)->virt);
  7395. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7396. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7397. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7398. iocbq->vport->fc_myDID);
  7399. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7400. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7401. phba->vpi_ids[phba->pport->vpi]);
  7402. }
  7403. command_type = OTHER_COMMAND;
  7404. break;
  7405. case CMD_CLOSE_XRI_CN:
  7406. case CMD_ABORT_XRI_CN:
  7407. case CMD_ABORT_XRI_CX:
  7408. /* words 0-2 memcpy should be 0 rserved */
  7409. /* port will send abts */
  7410. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7411. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7412. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7413. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7414. } else
  7415. fip = 0;
  7416. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7417. /*
  7418. * The link is down, or the command was ELS_FIP
  7419. * so the fw does not need to send abts
  7420. * on the wire.
  7421. */
  7422. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7423. else
  7424. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7425. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7426. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7427. wqe->abort_cmd.rsrvd5 = 0;
  7428. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7429. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7430. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7431. /*
  7432. * The abort handler will send us CMD_ABORT_XRI_CN or
  7433. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7434. */
  7435. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7436. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7437. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7438. LPFC_WQE_LENLOC_NONE);
  7439. cmnd = CMD_ABORT_XRI_CX;
  7440. command_type = OTHER_COMMAND;
  7441. xritag = 0;
  7442. break;
  7443. case CMD_XMIT_BLS_RSP64_CX:
  7444. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7445. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7446. * we re-construct this WQE here based on information in
  7447. * iocbq from scratch.
  7448. */
  7449. memset(wqe, 0, sizeof(union lpfc_wqe));
  7450. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7451. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7452. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7453. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7454. LPFC_ABTS_UNSOL_INT) {
  7455. /* ABTS sent by initiator to CT exchange, the
  7456. * RX_ID field will be filled with the newly
  7457. * allocated responder XRI.
  7458. */
  7459. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7460. iocbq->sli4_xritag);
  7461. } else {
  7462. /* ABTS sent by responder to CT exchange, the
  7463. * RX_ID field will be filled with the responder
  7464. * RX_ID from ABTS.
  7465. */
  7466. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7467. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7468. }
  7469. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7470. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7471. /* Use CT=VPI */
  7472. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7473. ndlp->nlp_DID);
  7474. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7475. iocbq->iocb.ulpContext);
  7476. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7477. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7478. phba->vpi_ids[phba->pport->vpi]);
  7479. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7480. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7481. LPFC_WQE_LENLOC_NONE);
  7482. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7483. command_type = OTHER_COMMAND;
  7484. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7485. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7486. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7487. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7488. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7489. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7490. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7491. }
  7492. break;
  7493. case CMD_XRI_ABORTED_CX:
  7494. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7495. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7496. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7497. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7498. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7499. default:
  7500. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7501. "2014 Invalid command 0x%x\n",
  7502. iocbq->iocb.ulpCommand);
  7503. return IOCB_ERROR;
  7504. break;
  7505. }
  7506. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7507. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7508. wqe->generic.wqe_com.abort_tag = abort_tag;
  7509. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7510. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7511. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7512. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7513. return 0;
  7514. }
  7515. /**
  7516. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7517. * @phba: Pointer to HBA context object.
  7518. * @ring_number: SLI ring number to issue iocb on.
  7519. * @piocb: Pointer to command iocb.
  7520. * @flag: Flag indicating if this command can be put into txq.
  7521. *
  7522. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7523. * an iocb command to an HBA with SLI-4 interface spec.
  7524. *
  7525. * This function is called with hbalock held. The function will return success
  7526. * after it successfully submit the iocb to firmware or after adding to the
  7527. * txq.
  7528. **/
  7529. static int
  7530. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7531. struct lpfc_iocbq *piocb, uint32_t flag)
  7532. {
  7533. struct lpfc_sglq *sglq;
  7534. union lpfc_wqe wqe;
  7535. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7536. if (piocb->sli4_xritag == NO_XRI) {
  7537. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7538. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7539. sglq = NULL;
  7540. else {
  7541. if (pring->txq_cnt) {
  7542. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7543. __lpfc_sli_ringtx_put(phba,
  7544. pring, piocb);
  7545. return IOCB_SUCCESS;
  7546. } else {
  7547. return IOCB_BUSY;
  7548. }
  7549. } else {
  7550. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7551. if (!sglq) {
  7552. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7553. __lpfc_sli_ringtx_put(phba,
  7554. pring,
  7555. piocb);
  7556. return IOCB_SUCCESS;
  7557. } else
  7558. return IOCB_BUSY;
  7559. }
  7560. }
  7561. }
  7562. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7563. /* These IO's already have an XRI and a mapped sgl. */
  7564. sglq = NULL;
  7565. } else {
  7566. /*
  7567. * This is a continuation of a commandi,(CX) so this
  7568. * sglq is on the active list
  7569. */
  7570. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7571. if (!sglq)
  7572. return IOCB_ERROR;
  7573. }
  7574. if (sglq) {
  7575. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7576. piocb->sli4_xritag = sglq->sli4_xritag;
  7577. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7578. return IOCB_ERROR;
  7579. }
  7580. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7581. return IOCB_ERROR;
  7582. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7583. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7584. /*
  7585. * For FCP command IOCB, get a new WQ index to distribute
  7586. * WQE across the WQsr. On the other hand, for abort IOCB,
  7587. * it carries the same WQ index to the original command
  7588. * IOCB.
  7589. */
  7590. if (piocb->iocb_flag & LPFC_IO_FCP)
  7591. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7592. if (unlikely(!phba->sli4_hba.fcp_wq))
  7593. return IOCB_ERROR;
  7594. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7595. &wqe))
  7596. return IOCB_ERROR;
  7597. } else {
  7598. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7599. return IOCB_ERROR;
  7600. }
  7601. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7602. return 0;
  7603. }
  7604. /**
  7605. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7606. *
  7607. * This routine wraps the actual lockless version for issusing IOCB function
  7608. * pointer from the lpfc_hba struct.
  7609. *
  7610. * Return codes:
  7611. * IOCB_ERROR - Error
  7612. * IOCB_SUCCESS - Success
  7613. * IOCB_BUSY - Busy
  7614. **/
  7615. int
  7616. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7617. struct lpfc_iocbq *piocb, uint32_t flag)
  7618. {
  7619. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7620. }
  7621. /**
  7622. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7623. * @phba: The hba struct for which this call is being executed.
  7624. * @dev_grp: The HBA PCI-Device group number.
  7625. *
  7626. * This routine sets up the SLI interface API function jump table in @phba
  7627. * struct.
  7628. * Returns: 0 - success, -ENODEV - failure.
  7629. **/
  7630. int
  7631. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7632. {
  7633. switch (dev_grp) {
  7634. case LPFC_PCI_DEV_LP:
  7635. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7636. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7637. break;
  7638. case LPFC_PCI_DEV_OC:
  7639. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7640. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7641. break;
  7642. default:
  7643. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7644. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7645. dev_grp);
  7646. return -ENODEV;
  7647. break;
  7648. }
  7649. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7650. return 0;
  7651. }
  7652. /**
  7653. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7654. * @phba: Pointer to HBA context object.
  7655. * @pring: Pointer to driver SLI ring object.
  7656. * @piocb: Pointer to command iocb.
  7657. * @flag: Flag indicating if this command can be put into txq.
  7658. *
  7659. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7660. * function. This function gets the hbalock and calls
  7661. * __lpfc_sli_issue_iocb function and will return the error returned
  7662. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7663. * functions which do not hold hbalock.
  7664. **/
  7665. int
  7666. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7667. struct lpfc_iocbq *piocb, uint32_t flag)
  7668. {
  7669. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  7670. unsigned long iflags;
  7671. int rc;
  7672. if (phba->sli_rev == LPFC_SLI_REV4) {
  7673. pring = &phba->sli.ring[ring_number];
  7674. spin_lock_irqsave(&pring->ring_lock, iflags);
  7675. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7676. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7677. } else {
  7678. /* For now, SLI2/3 will still use hbalock */
  7679. spin_lock_irqsave(&phba->hbalock, iflags);
  7680. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7681. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7682. }
  7683. return rc;
  7684. }
  7685. /**
  7686. * lpfc_extra_ring_setup - Extra ring setup function
  7687. * @phba: Pointer to HBA context object.
  7688. *
  7689. * This function is called while driver attaches with the
  7690. * HBA to setup the extra ring. The extra ring is used
  7691. * only when driver needs to support target mode functionality
  7692. * or IP over FC functionalities.
  7693. *
  7694. * This function is called with no lock held.
  7695. **/
  7696. static int
  7697. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7698. {
  7699. struct lpfc_sli *psli;
  7700. struct lpfc_sli_ring *pring;
  7701. psli = &phba->sli;
  7702. /* Adjust cmd/rsp ring iocb entries more evenly */
  7703. /* Take some away from the FCP ring */
  7704. pring = &psli->ring[psli->fcp_ring];
  7705. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7706. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7707. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7708. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7709. /* and give them to the extra ring */
  7710. pring = &psli->ring[psli->extra_ring];
  7711. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7712. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7713. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7714. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7715. /* Setup default profile for this ring */
  7716. pring->iotag_max = 4096;
  7717. pring->num_mask = 1;
  7718. pring->prt[0].profile = 0; /* Mask 0 */
  7719. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7720. pring->prt[0].type = phba->cfg_multi_ring_type;
  7721. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7722. return 0;
  7723. }
  7724. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7725. * @vport: pointer to virtual port object.
  7726. * @ndlp: nodelist pointer for the impacted rport.
  7727. *
  7728. * The driver calls this routine in response to a XRI ABORT CQE
  7729. * event from the port. In this event, the driver is required to
  7730. * recover its login to the rport even though its login may be valid
  7731. * from the driver's perspective. The failed ABTS notice from the
  7732. * port indicates the rport is not responding.
  7733. */
  7734. static void
  7735. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7736. struct lpfc_nodelist *ndlp)
  7737. {
  7738. struct Scsi_Host *shost;
  7739. struct lpfc_hba *phba;
  7740. unsigned long flags = 0;
  7741. shost = lpfc_shost_from_vport(vport);
  7742. phba = vport->phba;
  7743. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7744. lpfc_printf_log(phba, KERN_INFO,
  7745. LOG_SLI, "3093 No rport recovery needed. "
  7746. "rport in state 0x%x\n",
  7747. ndlp->nlp_state);
  7748. return;
  7749. }
  7750. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7751. "3094 Start rport recovery on shost id 0x%x "
  7752. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7753. "flags 0x%x\n",
  7754. shost->host_no, ndlp->nlp_DID,
  7755. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7756. ndlp->nlp_flag);
  7757. /*
  7758. * The rport is not responding. Don't attempt ADISC recovery.
  7759. * Remove the FCP-2 flag to force a PLOGI.
  7760. */
  7761. spin_lock_irqsave(shost->host_lock, flags);
  7762. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7763. spin_unlock_irqrestore(shost->host_lock, flags);
  7764. lpfc_disc_state_machine(vport, ndlp, NULL,
  7765. NLP_EVT_DEVICE_RECOVERY);
  7766. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7767. spin_lock_irqsave(shost->host_lock, flags);
  7768. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7769. spin_unlock_irqrestore(shost->host_lock, flags);
  7770. lpfc_disc_start(vport);
  7771. }
  7772. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7773. * @phba: Pointer to HBA context object.
  7774. * @iocbq: Pointer to iocb object.
  7775. *
  7776. * The async_event handler calls this routine when it receives
  7777. * an ASYNC_STATUS_CN event from the port. The port generates
  7778. * this event when an Abort Sequence request to an rport fails
  7779. * twice in succession. The abort could be originated by the
  7780. * driver or by the port. The ABTS could have been for an ELS
  7781. * or FCP IO. The port only generates this event when an ABTS
  7782. * fails to complete after one retry.
  7783. */
  7784. static void
  7785. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7786. struct lpfc_iocbq *iocbq)
  7787. {
  7788. struct lpfc_nodelist *ndlp = NULL;
  7789. uint16_t rpi = 0, vpi = 0;
  7790. struct lpfc_vport *vport = NULL;
  7791. /* The rpi in the ulpContext is vport-sensitive. */
  7792. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7793. rpi = iocbq->iocb.ulpContext;
  7794. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7795. "3092 Port generated ABTS async event "
  7796. "on vpi %d rpi %d status 0x%x\n",
  7797. vpi, rpi, iocbq->iocb.ulpStatus);
  7798. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7799. if (!vport)
  7800. goto err_exit;
  7801. ndlp = lpfc_findnode_rpi(vport, rpi);
  7802. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7803. goto err_exit;
  7804. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7805. lpfc_sli_abts_recover_port(vport, ndlp);
  7806. return;
  7807. err_exit:
  7808. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7809. "3095 Event Context not found, no "
  7810. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7811. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7812. vpi, rpi);
  7813. }
  7814. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7815. * @phba: pointer to HBA context object.
  7816. * @ndlp: nodelist pointer for the impacted rport.
  7817. * @axri: pointer to the wcqe containing the failed exchange.
  7818. *
  7819. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7820. * port. The port generates this event when an abort exchange request to an
  7821. * rport fails twice in succession with no reply. The abort could be originated
  7822. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7823. */
  7824. void
  7825. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7826. struct lpfc_nodelist *ndlp,
  7827. struct sli4_wcqe_xri_aborted *axri)
  7828. {
  7829. struct lpfc_vport *vport;
  7830. uint32_t ext_status = 0;
  7831. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7832. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7833. "3115 Node Context not found, driver "
  7834. "ignoring abts err event\n");
  7835. return;
  7836. }
  7837. vport = ndlp->vport;
  7838. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7839. "3116 Port generated FCP XRI ABORT event on "
  7840. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7841. ndlp->vport->vpi, ndlp->nlp_rpi,
  7842. bf_get(lpfc_wcqe_xa_xri, axri),
  7843. bf_get(lpfc_wcqe_xa_status, axri),
  7844. axri->parameter);
  7845. /*
  7846. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7847. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7848. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7849. */
  7850. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7851. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7852. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7853. lpfc_sli_abts_recover_port(vport, ndlp);
  7854. }
  7855. /**
  7856. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7857. * @phba: Pointer to HBA context object.
  7858. * @pring: Pointer to driver SLI ring object.
  7859. * @iocbq: Pointer to iocb object.
  7860. *
  7861. * This function is called by the slow ring event handler
  7862. * function when there is an ASYNC event iocb in the ring.
  7863. * This function is called with no lock held.
  7864. * Currently this function handles only temperature related
  7865. * ASYNC events. The function decodes the temperature sensor
  7866. * event message and posts events for the management applications.
  7867. **/
  7868. static void
  7869. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7870. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7871. {
  7872. IOCB_t *icmd;
  7873. uint16_t evt_code;
  7874. struct temp_event temp_event_data;
  7875. struct Scsi_Host *shost;
  7876. uint32_t *iocb_w;
  7877. icmd = &iocbq->iocb;
  7878. evt_code = icmd->un.asyncstat.evt_code;
  7879. switch (evt_code) {
  7880. case ASYNC_TEMP_WARN:
  7881. case ASYNC_TEMP_SAFE:
  7882. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7883. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7884. if (evt_code == ASYNC_TEMP_WARN) {
  7885. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7886. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7887. "0347 Adapter is very hot, please take "
  7888. "corrective action. temperature : %d Celsius\n",
  7889. (uint32_t) icmd->ulpContext);
  7890. } else {
  7891. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7892. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7893. "0340 Adapter temperature is OK now. "
  7894. "temperature : %d Celsius\n",
  7895. (uint32_t) icmd->ulpContext);
  7896. }
  7897. /* Send temperature change event to applications */
  7898. shost = lpfc_shost_from_vport(phba->pport);
  7899. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7900. sizeof(temp_event_data), (char *) &temp_event_data,
  7901. LPFC_NL_VENDOR_ID);
  7902. break;
  7903. case ASYNC_STATUS_CN:
  7904. lpfc_sli_abts_err_handler(phba, iocbq);
  7905. break;
  7906. default:
  7907. iocb_w = (uint32_t *) icmd;
  7908. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7909. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7910. " evt_code 0x%x\n"
  7911. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7912. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7913. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7914. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7915. pring->ringno, icmd->un.asyncstat.evt_code,
  7916. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7917. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7918. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7919. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7920. break;
  7921. }
  7922. }
  7923. /**
  7924. * lpfc_sli_setup - SLI ring setup function
  7925. * @phba: Pointer to HBA context object.
  7926. *
  7927. * lpfc_sli_setup sets up rings of the SLI interface with
  7928. * number of iocbs per ring and iotags. This function is
  7929. * called while driver attach to the HBA and before the
  7930. * interrupts are enabled. So there is no need for locking.
  7931. *
  7932. * This function always returns 0.
  7933. **/
  7934. int
  7935. lpfc_sli_setup(struct lpfc_hba *phba)
  7936. {
  7937. int i, totiocbsize = 0;
  7938. struct lpfc_sli *psli = &phba->sli;
  7939. struct lpfc_sli_ring *pring;
  7940. psli->num_rings = MAX_CONFIGURED_RINGS;
  7941. psli->sli_flag = 0;
  7942. psli->fcp_ring = LPFC_FCP_RING;
  7943. psli->next_ring = LPFC_FCP_NEXT_RING;
  7944. psli->extra_ring = LPFC_EXTRA_RING;
  7945. psli->iocbq_lookup = NULL;
  7946. psli->iocbq_lookup_len = 0;
  7947. psli->last_iotag = 0;
  7948. for (i = 0; i < psli->num_rings; i++) {
  7949. pring = &psli->ring[i];
  7950. switch (i) {
  7951. case LPFC_FCP_RING: /* ring 0 - FCP */
  7952. /* numCiocb and numRiocb are used in config_port */
  7953. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7954. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7955. pring->sli.sli3.numCiocb +=
  7956. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7957. pring->sli.sli3.numRiocb +=
  7958. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7959. pring->sli.sli3.numCiocb +=
  7960. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7961. pring->sli.sli3.numRiocb +=
  7962. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7963. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  7964. SLI3_IOCB_CMD_SIZE :
  7965. SLI2_IOCB_CMD_SIZE;
  7966. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  7967. SLI3_IOCB_RSP_SIZE :
  7968. SLI2_IOCB_RSP_SIZE;
  7969. pring->iotag_ctr = 0;
  7970. pring->iotag_max =
  7971. (phba->cfg_hba_queue_depth * 2);
  7972. pring->fast_iotag = pring->iotag_max;
  7973. pring->num_mask = 0;
  7974. break;
  7975. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7976. /* numCiocb and numRiocb are used in config_port */
  7977. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7978. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7979. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  7980. SLI3_IOCB_CMD_SIZE :
  7981. SLI2_IOCB_CMD_SIZE;
  7982. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  7983. SLI3_IOCB_RSP_SIZE :
  7984. SLI2_IOCB_RSP_SIZE;
  7985. pring->iotag_max = phba->cfg_hba_queue_depth;
  7986. pring->num_mask = 0;
  7987. break;
  7988. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7989. /* numCiocb and numRiocb are used in config_port */
  7990. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7991. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7992. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  7993. SLI3_IOCB_CMD_SIZE :
  7994. SLI2_IOCB_CMD_SIZE;
  7995. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  7996. SLI3_IOCB_RSP_SIZE :
  7997. SLI2_IOCB_RSP_SIZE;
  7998. pring->fast_iotag = 0;
  7999. pring->iotag_ctr = 0;
  8000. pring->iotag_max = 4096;
  8001. pring->lpfc_sli_rcv_async_status =
  8002. lpfc_sli_async_event_handler;
  8003. pring->num_mask = LPFC_MAX_RING_MASK;
  8004. pring->prt[0].profile = 0; /* Mask 0 */
  8005. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8006. pring->prt[0].type = FC_TYPE_ELS;
  8007. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8008. lpfc_els_unsol_event;
  8009. pring->prt[1].profile = 0; /* Mask 1 */
  8010. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8011. pring->prt[1].type = FC_TYPE_ELS;
  8012. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8013. lpfc_els_unsol_event;
  8014. pring->prt[2].profile = 0; /* Mask 2 */
  8015. /* NameServer Inquiry */
  8016. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8017. /* NameServer */
  8018. pring->prt[2].type = FC_TYPE_CT;
  8019. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8020. lpfc_ct_unsol_event;
  8021. pring->prt[3].profile = 0; /* Mask 3 */
  8022. /* NameServer response */
  8023. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8024. /* NameServer */
  8025. pring->prt[3].type = FC_TYPE_CT;
  8026. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8027. lpfc_ct_unsol_event;
  8028. /* abort unsolicited sequence */
  8029. pring->prt[4].profile = 0; /* Mask 4 */
  8030. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  8031. pring->prt[4].type = FC_TYPE_BLS;
  8032. pring->prt[4].lpfc_sli_rcv_unsol_event =
  8033. lpfc_sli4_ct_abort_unsol_event;
  8034. break;
  8035. }
  8036. totiocbsize += (pring->sli.sli3.numCiocb *
  8037. pring->sli.sli3.sizeCiocb) +
  8038. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8039. }
  8040. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8041. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8042. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8043. "SLI2 SLIM Data: x%x x%lx\n",
  8044. phba->brd_no, totiocbsize,
  8045. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8046. }
  8047. if (phba->cfg_multi_ring_support == 2)
  8048. lpfc_extra_ring_setup(phba);
  8049. return 0;
  8050. }
  8051. /**
  8052. * lpfc_sli_queue_setup - Queue initialization function
  8053. * @phba: Pointer to HBA context object.
  8054. *
  8055. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8056. * ring. This function also initializes ring indices of each ring.
  8057. * This function is called during the initialization of the SLI
  8058. * interface of an HBA.
  8059. * This function is called with no lock held and always returns
  8060. * 1.
  8061. **/
  8062. int
  8063. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8064. {
  8065. struct lpfc_sli *psli;
  8066. struct lpfc_sli_ring *pring;
  8067. int i;
  8068. psli = &phba->sli;
  8069. spin_lock_irq(&phba->hbalock);
  8070. INIT_LIST_HEAD(&psli->mboxq);
  8071. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8072. /* Initialize list headers for txq and txcmplq as double linked lists */
  8073. for (i = 0; i < psli->num_rings; i++) {
  8074. pring = &psli->ring[i];
  8075. pring->ringno = i;
  8076. pring->sli.sli3.next_cmdidx = 0;
  8077. pring->sli.sli3.local_getidx = 0;
  8078. pring->sli.sli3.cmdidx = 0;
  8079. INIT_LIST_HEAD(&pring->txq);
  8080. INIT_LIST_HEAD(&pring->txcmplq);
  8081. INIT_LIST_HEAD(&pring->iocb_continueq);
  8082. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8083. INIT_LIST_HEAD(&pring->postbufq);
  8084. spin_lock_init(&pring->ring_lock);
  8085. }
  8086. spin_unlock_irq(&phba->hbalock);
  8087. return 1;
  8088. }
  8089. /**
  8090. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8091. * @phba: Pointer to HBA context object.
  8092. *
  8093. * This routine flushes the mailbox command subsystem. It will unconditionally
  8094. * flush all the mailbox commands in the three possible stages in the mailbox
  8095. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8096. * command; and completed mailbox command queue. It is caller's responsibility
  8097. * to make sure that the driver is in the proper state to flush the mailbox
  8098. * command sub-system. Namely, the posting of mailbox commands into the
  8099. * pending mailbox command queue from the various clients must be stopped;
  8100. * either the HBA is in a state that it will never works on the outstanding
  8101. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8102. * mailbox command has been completed.
  8103. **/
  8104. static void
  8105. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8106. {
  8107. LIST_HEAD(completions);
  8108. struct lpfc_sli *psli = &phba->sli;
  8109. LPFC_MBOXQ_t *pmb;
  8110. unsigned long iflag;
  8111. /* Flush all the mailbox commands in the mbox system */
  8112. spin_lock_irqsave(&phba->hbalock, iflag);
  8113. /* The pending mailbox command queue */
  8114. list_splice_init(&phba->sli.mboxq, &completions);
  8115. /* The outstanding active mailbox command */
  8116. if (psli->mbox_active) {
  8117. list_add_tail(&psli->mbox_active->list, &completions);
  8118. psli->mbox_active = NULL;
  8119. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8120. }
  8121. /* The completed mailbox command queue */
  8122. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8123. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8124. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8125. while (!list_empty(&completions)) {
  8126. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8127. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8128. if (pmb->mbox_cmpl)
  8129. pmb->mbox_cmpl(phba, pmb);
  8130. }
  8131. }
  8132. /**
  8133. * lpfc_sli_host_down - Vport cleanup function
  8134. * @vport: Pointer to virtual port object.
  8135. *
  8136. * lpfc_sli_host_down is called to clean up the resources
  8137. * associated with a vport before destroying virtual
  8138. * port data structures.
  8139. * This function does following operations:
  8140. * - Free discovery resources associated with this virtual
  8141. * port.
  8142. * - Free iocbs associated with this virtual port in
  8143. * the txq.
  8144. * - Send abort for all iocb commands associated with this
  8145. * vport in txcmplq.
  8146. *
  8147. * This function is called with no lock held and always returns 1.
  8148. **/
  8149. int
  8150. lpfc_sli_host_down(struct lpfc_vport *vport)
  8151. {
  8152. LIST_HEAD(completions);
  8153. struct lpfc_hba *phba = vport->phba;
  8154. struct lpfc_sli *psli = &phba->sli;
  8155. struct lpfc_sli_ring *pring;
  8156. struct lpfc_iocbq *iocb, *next_iocb;
  8157. int i;
  8158. unsigned long flags = 0;
  8159. uint16_t prev_pring_flag;
  8160. lpfc_cleanup_discovery_resources(vport);
  8161. spin_lock_irqsave(&phba->hbalock, flags);
  8162. for (i = 0; i < psli->num_rings; i++) {
  8163. pring = &psli->ring[i];
  8164. prev_pring_flag = pring->flag;
  8165. /* Only slow rings */
  8166. if (pring->ringno == LPFC_ELS_RING) {
  8167. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8168. /* Set the lpfc data pending flag */
  8169. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8170. }
  8171. /*
  8172. * Error everything on the txq since these iocbs have not been
  8173. * given to the FW yet.
  8174. */
  8175. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8176. if (iocb->vport != vport)
  8177. continue;
  8178. list_move_tail(&iocb->list, &completions);
  8179. pring->txq_cnt--;
  8180. }
  8181. /* Next issue ABTS for everything on the txcmplq */
  8182. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8183. list) {
  8184. if (iocb->vport != vport)
  8185. continue;
  8186. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8187. }
  8188. pring->flag = prev_pring_flag;
  8189. }
  8190. spin_unlock_irqrestore(&phba->hbalock, flags);
  8191. /* Cancel all the IOCBs from the completions list */
  8192. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8193. IOERR_SLI_DOWN);
  8194. return 1;
  8195. }
  8196. /**
  8197. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8198. * @phba: Pointer to HBA context object.
  8199. *
  8200. * This function cleans up all iocb, buffers, mailbox commands
  8201. * while shutting down the HBA. This function is called with no
  8202. * lock held and always returns 1.
  8203. * This function does the following to cleanup driver resources:
  8204. * - Free discovery resources for each virtual port
  8205. * - Cleanup any pending fabric iocbs
  8206. * - Iterate through the iocb txq and free each entry
  8207. * in the list.
  8208. * - Free up any buffer posted to the HBA
  8209. * - Free mailbox commands in the mailbox queue.
  8210. **/
  8211. int
  8212. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8213. {
  8214. LIST_HEAD(completions);
  8215. struct lpfc_sli *psli = &phba->sli;
  8216. struct lpfc_sli_ring *pring;
  8217. struct lpfc_dmabuf *buf_ptr;
  8218. unsigned long flags = 0;
  8219. int i;
  8220. /* Shutdown the mailbox command sub-system */
  8221. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8222. lpfc_hba_down_prep(phba);
  8223. lpfc_fabric_abort_hba(phba);
  8224. spin_lock_irqsave(&phba->hbalock, flags);
  8225. for (i = 0; i < psli->num_rings; i++) {
  8226. pring = &psli->ring[i];
  8227. /* Only slow rings */
  8228. if (pring->ringno == LPFC_ELS_RING) {
  8229. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8230. /* Set the lpfc data pending flag */
  8231. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8232. }
  8233. /*
  8234. * Error everything on the txq since these iocbs have not been
  8235. * given to the FW yet.
  8236. */
  8237. list_splice_init(&pring->txq, &completions);
  8238. pring->txq_cnt = 0;
  8239. }
  8240. spin_unlock_irqrestore(&phba->hbalock, flags);
  8241. /* Cancel all the IOCBs from the completions list */
  8242. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8243. IOERR_SLI_DOWN);
  8244. spin_lock_irqsave(&phba->hbalock, flags);
  8245. list_splice_init(&phba->elsbuf, &completions);
  8246. phba->elsbuf_cnt = 0;
  8247. phba->elsbuf_prev_cnt = 0;
  8248. spin_unlock_irqrestore(&phba->hbalock, flags);
  8249. while (!list_empty(&completions)) {
  8250. list_remove_head(&completions, buf_ptr,
  8251. struct lpfc_dmabuf, list);
  8252. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8253. kfree(buf_ptr);
  8254. }
  8255. /* Return any active mbox cmds */
  8256. del_timer_sync(&psli->mbox_tmo);
  8257. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8258. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8259. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8260. return 1;
  8261. }
  8262. /**
  8263. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8264. * @srcp: Source memory pointer.
  8265. * @destp: Destination memory pointer.
  8266. * @cnt: Number of words required to be copied.
  8267. *
  8268. * This function is used for copying data between driver memory
  8269. * and the SLI memory. This function also changes the endianness
  8270. * of each word if native endianness is different from SLI
  8271. * endianness. This function can be called with or without
  8272. * lock.
  8273. **/
  8274. void
  8275. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8276. {
  8277. uint32_t *src = srcp;
  8278. uint32_t *dest = destp;
  8279. uint32_t ldata;
  8280. int i;
  8281. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8282. ldata = *src;
  8283. ldata = le32_to_cpu(ldata);
  8284. *dest = ldata;
  8285. src++;
  8286. dest++;
  8287. }
  8288. }
  8289. /**
  8290. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8291. * @srcp: Source memory pointer.
  8292. * @destp: Destination memory pointer.
  8293. * @cnt: Number of words required to be copied.
  8294. *
  8295. * This function is used for copying data between a data structure
  8296. * with big endian representation to local endianness.
  8297. * This function can be called with or without lock.
  8298. **/
  8299. void
  8300. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8301. {
  8302. uint32_t *src = srcp;
  8303. uint32_t *dest = destp;
  8304. uint32_t ldata;
  8305. int i;
  8306. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8307. ldata = *src;
  8308. ldata = be32_to_cpu(ldata);
  8309. *dest = ldata;
  8310. src++;
  8311. dest++;
  8312. }
  8313. }
  8314. /**
  8315. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8316. * @phba: Pointer to HBA context object.
  8317. * @pring: Pointer to driver SLI ring object.
  8318. * @mp: Pointer to driver buffer object.
  8319. *
  8320. * This function is called with no lock held.
  8321. * It always return zero after adding the buffer to the postbufq
  8322. * buffer list.
  8323. **/
  8324. int
  8325. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8326. struct lpfc_dmabuf *mp)
  8327. {
  8328. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8329. later */
  8330. spin_lock_irq(&phba->hbalock);
  8331. list_add_tail(&mp->list, &pring->postbufq);
  8332. pring->postbufq_cnt++;
  8333. spin_unlock_irq(&phba->hbalock);
  8334. return 0;
  8335. }
  8336. /**
  8337. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8338. * @phba: Pointer to HBA context object.
  8339. *
  8340. * When HBQ is enabled, buffers are searched based on tags. This function
  8341. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8342. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8343. * does not conflict with tags of buffer posted for unsolicited events.
  8344. * The function returns the allocated tag. The function is called with
  8345. * no locks held.
  8346. **/
  8347. uint32_t
  8348. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8349. {
  8350. spin_lock_irq(&phba->hbalock);
  8351. phba->buffer_tag_count++;
  8352. /*
  8353. * Always set the QUE_BUFTAG_BIT to distiguish between
  8354. * a tag assigned by HBQ.
  8355. */
  8356. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8357. spin_unlock_irq(&phba->hbalock);
  8358. return phba->buffer_tag_count;
  8359. }
  8360. /**
  8361. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8362. * @phba: Pointer to HBA context object.
  8363. * @pring: Pointer to driver SLI ring object.
  8364. * @tag: Buffer tag.
  8365. *
  8366. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8367. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8368. * iocb is posted to the response ring with the tag of the buffer.
  8369. * This function searches the pring->postbufq list using the tag
  8370. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8371. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8372. * buffer is returned to the caller else NULL is returned.
  8373. * This function is called with no lock held.
  8374. **/
  8375. struct lpfc_dmabuf *
  8376. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8377. uint32_t tag)
  8378. {
  8379. struct lpfc_dmabuf *mp, *next_mp;
  8380. struct list_head *slp = &pring->postbufq;
  8381. /* Search postbufq, from the beginning, looking for a match on tag */
  8382. spin_lock_irq(&phba->hbalock);
  8383. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8384. if (mp->buffer_tag == tag) {
  8385. list_del_init(&mp->list);
  8386. pring->postbufq_cnt--;
  8387. spin_unlock_irq(&phba->hbalock);
  8388. return mp;
  8389. }
  8390. }
  8391. spin_unlock_irq(&phba->hbalock);
  8392. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8393. "0402 Cannot find virtual addr for buffer tag on "
  8394. "ring %d Data x%lx x%p x%p x%x\n",
  8395. pring->ringno, (unsigned long) tag,
  8396. slp->next, slp->prev, pring->postbufq_cnt);
  8397. return NULL;
  8398. }
  8399. /**
  8400. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8401. * @phba: Pointer to HBA context object.
  8402. * @pring: Pointer to driver SLI ring object.
  8403. * @phys: DMA address of the buffer.
  8404. *
  8405. * This function searches the buffer list using the dma_address
  8406. * of unsolicited event to find the driver's lpfc_dmabuf object
  8407. * corresponding to the dma_address. The function returns the
  8408. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8409. * This function is called by the ct and els unsolicited event
  8410. * handlers to get the buffer associated with the unsolicited
  8411. * event.
  8412. *
  8413. * This function is called with no lock held.
  8414. **/
  8415. struct lpfc_dmabuf *
  8416. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8417. dma_addr_t phys)
  8418. {
  8419. struct lpfc_dmabuf *mp, *next_mp;
  8420. struct list_head *slp = &pring->postbufq;
  8421. /* Search postbufq, from the beginning, looking for a match on phys */
  8422. spin_lock_irq(&phba->hbalock);
  8423. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8424. if (mp->phys == phys) {
  8425. list_del_init(&mp->list);
  8426. pring->postbufq_cnt--;
  8427. spin_unlock_irq(&phba->hbalock);
  8428. return mp;
  8429. }
  8430. }
  8431. spin_unlock_irq(&phba->hbalock);
  8432. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8433. "0410 Cannot find virtual addr for mapped buf on "
  8434. "ring %d Data x%llx x%p x%p x%x\n",
  8435. pring->ringno, (unsigned long long)phys,
  8436. slp->next, slp->prev, pring->postbufq_cnt);
  8437. return NULL;
  8438. }
  8439. /**
  8440. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8441. * @phba: Pointer to HBA context object.
  8442. * @cmdiocb: Pointer to driver command iocb object.
  8443. * @rspiocb: Pointer to driver response iocb object.
  8444. *
  8445. * This function is the completion handler for the abort iocbs for
  8446. * ELS commands. This function is called from the ELS ring event
  8447. * handler with no lock held. This function frees memory resources
  8448. * associated with the abort iocb.
  8449. **/
  8450. static void
  8451. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8452. struct lpfc_iocbq *rspiocb)
  8453. {
  8454. IOCB_t *irsp = &rspiocb->iocb;
  8455. uint16_t abort_iotag, abort_context;
  8456. struct lpfc_iocbq *abort_iocb = NULL;
  8457. if (irsp->ulpStatus) {
  8458. /*
  8459. * Assume that the port already completed and returned, or
  8460. * will return the iocb. Just Log the message.
  8461. */
  8462. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8463. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8464. spin_lock_irq(&phba->hbalock);
  8465. if (phba->sli_rev < LPFC_SLI_REV4) {
  8466. if (abort_iotag != 0 &&
  8467. abort_iotag <= phba->sli.last_iotag)
  8468. abort_iocb =
  8469. phba->sli.iocbq_lookup[abort_iotag];
  8470. } else
  8471. /* For sli4 the abort_tag is the XRI,
  8472. * so the abort routine puts the iotag of the iocb
  8473. * being aborted in the context field of the abort
  8474. * IOCB.
  8475. */
  8476. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8477. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8478. "0327 Cannot abort els iocb %p "
  8479. "with tag %x context %x, abort status %x, "
  8480. "abort code %x\n",
  8481. abort_iocb, abort_iotag, abort_context,
  8482. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8483. spin_unlock_irq(&phba->hbalock);
  8484. }
  8485. lpfc_sli_release_iocbq(phba, cmdiocb);
  8486. return;
  8487. }
  8488. /**
  8489. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8490. * @phba: Pointer to HBA context object.
  8491. * @cmdiocb: Pointer to driver command iocb object.
  8492. * @rspiocb: Pointer to driver response iocb object.
  8493. *
  8494. * The function is called from SLI ring event handler with no
  8495. * lock held. This function is the completion handler for ELS commands
  8496. * which are aborted. The function frees memory resources used for
  8497. * the aborted ELS commands.
  8498. **/
  8499. static void
  8500. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8501. struct lpfc_iocbq *rspiocb)
  8502. {
  8503. IOCB_t *irsp = &rspiocb->iocb;
  8504. /* ELS cmd tag <ulpIoTag> completes */
  8505. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8506. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8507. "x%x x%x x%x\n",
  8508. irsp->ulpIoTag, irsp->ulpStatus,
  8509. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8510. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8511. lpfc_ct_free_iocb(phba, cmdiocb);
  8512. else
  8513. lpfc_els_free_iocb(phba, cmdiocb);
  8514. return;
  8515. }
  8516. /**
  8517. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8518. * @phba: Pointer to HBA context object.
  8519. * @pring: Pointer to driver SLI ring object.
  8520. * @cmdiocb: Pointer to driver command iocb object.
  8521. *
  8522. * This function issues an abort iocb for the provided command iocb down to
  8523. * the port. Other than the case the outstanding command iocb is an abort
  8524. * request, this function issues abort out unconditionally. This function is
  8525. * called with hbalock held. The function returns 0 when it fails due to
  8526. * memory allocation failure or when the command iocb is an abort request.
  8527. **/
  8528. static int
  8529. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8530. struct lpfc_iocbq *cmdiocb)
  8531. {
  8532. struct lpfc_vport *vport = cmdiocb->vport;
  8533. struct lpfc_iocbq *abtsiocbp;
  8534. IOCB_t *icmd = NULL;
  8535. IOCB_t *iabt = NULL;
  8536. int retval;
  8537. unsigned long iflags;
  8538. /*
  8539. * There are certain command types we don't want to abort. And we
  8540. * don't want to abort commands that are already in the process of
  8541. * being aborted.
  8542. */
  8543. icmd = &cmdiocb->iocb;
  8544. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8545. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8546. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8547. return 0;
  8548. /* issue ABTS for this IOCB based on iotag */
  8549. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8550. if (abtsiocbp == NULL)
  8551. return 0;
  8552. /* This signals the response to set the correct status
  8553. * before calling the completion handler
  8554. */
  8555. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8556. iabt = &abtsiocbp->iocb;
  8557. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8558. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8559. if (phba->sli_rev == LPFC_SLI_REV4) {
  8560. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8561. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8562. }
  8563. else
  8564. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8565. iabt->ulpLe = 1;
  8566. iabt->ulpClass = icmd->ulpClass;
  8567. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8568. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8569. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8570. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8571. if (phba->link_state >= LPFC_LINK_UP)
  8572. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8573. else
  8574. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8575. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8576. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8577. "0339 Abort xri x%x, original iotag x%x, "
  8578. "abort cmd iotag x%x\n",
  8579. iabt->un.acxri.abortIoTag,
  8580. iabt->un.acxri.abortContextTag,
  8581. abtsiocbp->iotag);
  8582. if (phba->sli_rev == LPFC_SLI_REV4) {
  8583. /* Note: both hbalock and ring_lock need to be set here */
  8584. spin_lock_irqsave(&pring->ring_lock, iflags);
  8585. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8586. abtsiocbp, 0);
  8587. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8588. } else {
  8589. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8590. abtsiocbp, 0);
  8591. }
  8592. if (retval)
  8593. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8594. /*
  8595. * Caller to this routine should check for IOCB_ERROR
  8596. * and handle it properly. This routine no longer removes
  8597. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8598. */
  8599. return retval;
  8600. }
  8601. /**
  8602. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8603. * @phba: Pointer to HBA context object.
  8604. * @pring: Pointer to driver SLI ring object.
  8605. * @cmdiocb: Pointer to driver command iocb object.
  8606. *
  8607. * This function issues an abort iocb for the provided command iocb. In case
  8608. * of unloading, the abort iocb will not be issued to commands on the ELS
  8609. * ring. Instead, the callback function shall be changed to those commands
  8610. * so that nothing happens when them finishes. This function is called with
  8611. * hbalock held. The function returns 0 when the command iocb is an abort
  8612. * request.
  8613. **/
  8614. int
  8615. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8616. struct lpfc_iocbq *cmdiocb)
  8617. {
  8618. struct lpfc_vport *vport = cmdiocb->vport;
  8619. int retval = IOCB_ERROR;
  8620. IOCB_t *icmd = NULL;
  8621. /*
  8622. * There are certain command types we don't want to abort. And we
  8623. * don't want to abort commands that are already in the process of
  8624. * being aborted.
  8625. */
  8626. icmd = &cmdiocb->iocb;
  8627. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8628. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8629. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8630. return 0;
  8631. /*
  8632. * If we're unloading, don't abort iocb on the ELS ring, but change
  8633. * the callback so that nothing happens when it finishes.
  8634. */
  8635. if ((vport->load_flag & FC_UNLOADING) &&
  8636. (pring->ringno == LPFC_ELS_RING)) {
  8637. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8638. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8639. else
  8640. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8641. goto abort_iotag_exit;
  8642. }
  8643. /* Now, we try to issue the abort to the cmdiocb out */
  8644. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8645. abort_iotag_exit:
  8646. /*
  8647. * Caller to this routine should check for IOCB_ERROR
  8648. * and handle it properly. This routine no longer removes
  8649. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8650. */
  8651. return retval;
  8652. }
  8653. /**
  8654. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8655. * @phba: Pointer to HBA context object.
  8656. * @pring: Pointer to driver SLI ring object.
  8657. *
  8658. * This function aborts all iocbs in the given ring and frees all the iocb
  8659. * objects in txq. This function issues abort iocbs unconditionally for all
  8660. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8661. * to complete before the return of this function. The caller is not required
  8662. * to hold any locks.
  8663. **/
  8664. static void
  8665. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8666. {
  8667. LIST_HEAD(completions);
  8668. struct lpfc_iocbq *iocb, *next_iocb;
  8669. if (pring->ringno == LPFC_ELS_RING)
  8670. lpfc_fabric_abort_hba(phba);
  8671. spin_lock_irq(&phba->hbalock);
  8672. /* Take off all the iocbs on txq for cancelling */
  8673. list_splice_init(&pring->txq, &completions);
  8674. pring->txq_cnt = 0;
  8675. /* Next issue ABTS for everything on the txcmplq */
  8676. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8677. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8678. spin_unlock_irq(&phba->hbalock);
  8679. /* Cancel all the IOCBs from the completions list */
  8680. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8681. IOERR_SLI_ABORTED);
  8682. }
  8683. /**
  8684. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8685. * @phba: pointer to lpfc HBA data structure.
  8686. *
  8687. * This routine will abort all pending and outstanding iocbs to an HBA.
  8688. **/
  8689. void
  8690. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8691. {
  8692. struct lpfc_sli *psli = &phba->sli;
  8693. struct lpfc_sli_ring *pring;
  8694. int i;
  8695. for (i = 0; i < psli->num_rings; i++) {
  8696. pring = &psli->ring[i];
  8697. lpfc_sli_iocb_ring_abort(phba, pring);
  8698. }
  8699. }
  8700. /**
  8701. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8702. * @iocbq: Pointer to driver iocb object.
  8703. * @vport: Pointer to driver virtual port object.
  8704. * @tgt_id: SCSI ID of the target.
  8705. * @lun_id: LUN ID of the scsi device.
  8706. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8707. *
  8708. * This function acts as an iocb filter for functions which abort or count
  8709. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8710. * 0 if the filtering criteria is met for the given iocb and will return
  8711. * 1 if the filtering criteria is not met.
  8712. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8713. * given iocb is for the SCSI device specified by vport, tgt_id and
  8714. * lun_id parameter.
  8715. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8716. * given iocb is for the SCSI target specified by vport and tgt_id
  8717. * parameters.
  8718. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8719. * given iocb is for the SCSI host associated with the given vport.
  8720. * This function is called with no locks held.
  8721. **/
  8722. static int
  8723. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8724. uint16_t tgt_id, uint64_t lun_id,
  8725. lpfc_ctx_cmd ctx_cmd)
  8726. {
  8727. struct lpfc_scsi_buf *lpfc_cmd;
  8728. int rc = 1;
  8729. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8730. return rc;
  8731. if (iocbq->vport != vport)
  8732. return rc;
  8733. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8734. if (lpfc_cmd->pCmd == NULL)
  8735. return rc;
  8736. switch (ctx_cmd) {
  8737. case LPFC_CTX_LUN:
  8738. if ((lpfc_cmd->rdata->pnode) &&
  8739. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8740. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8741. rc = 0;
  8742. break;
  8743. case LPFC_CTX_TGT:
  8744. if ((lpfc_cmd->rdata->pnode) &&
  8745. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8746. rc = 0;
  8747. break;
  8748. case LPFC_CTX_HOST:
  8749. rc = 0;
  8750. break;
  8751. default:
  8752. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8753. __func__, ctx_cmd);
  8754. break;
  8755. }
  8756. return rc;
  8757. }
  8758. /**
  8759. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8760. * @vport: Pointer to virtual port.
  8761. * @tgt_id: SCSI ID of the target.
  8762. * @lun_id: LUN ID of the scsi device.
  8763. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8764. *
  8765. * This function returns number of FCP commands pending for the vport.
  8766. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8767. * commands pending on the vport associated with SCSI device specified
  8768. * by tgt_id and lun_id parameters.
  8769. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8770. * commands pending on the vport associated with SCSI target specified
  8771. * by tgt_id parameter.
  8772. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8773. * commands pending on the vport.
  8774. * This function returns the number of iocbs which satisfy the filter.
  8775. * This function is called without any lock held.
  8776. **/
  8777. int
  8778. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8779. lpfc_ctx_cmd ctx_cmd)
  8780. {
  8781. struct lpfc_hba *phba = vport->phba;
  8782. struct lpfc_iocbq *iocbq;
  8783. int sum, i;
  8784. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8785. iocbq = phba->sli.iocbq_lookup[i];
  8786. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8787. ctx_cmd) == 0)
  8788. sum++;
  8789. }
  8790. return sum;
  8791. }
  8792. /**
  8793. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8794. * @phba: Pointer to HBA context object
  8795. * @cmdiocb: Pointer to command iocb object.
  8796. * @rspiocb: Pointer to response iocb object.
  8797. *
  8798. * This function is called when an aborted FCP iocb completes. This
  8799. * function is called by the ring event handler with no lock held.
  8800. * This function frees the iocb.
  8801. **/
  8802. void
  8803. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8804. struct lpfc_iocbq *rspiocb)
  8805. {
  8806. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8807. "3096 ABORT_XRI_CN completing on xri x%x "
  8808. "original iotag x%x, abort cmd iotag x%x "
  8809. "status 0x%x, reason 0x%x\n",
  8810. cmdiocb->iocb.un.acxri.abortContextTag,
  8811. cmdiocb->iocb.un.acxri.abortIoTag,
  8812. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8813. rspiocb->iocb.un.ulpWord[4]);
  8814. lpfc_sli_release_iocbq(phba, cmdiocb);
  8815. return;
  8816. }
  8817. /**
  8818. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8819. * @vport: Pointer to virtual port.
  8820. * @pring: Pointer to driver SLI ring object.
  8821. * @tgt_id: SCSI ID of the target.
  8822. * @lun_id: LUN ID of the scsi device.
  8823. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8824. *
  8825. * This function sends an abort command for every SCSI command
  8826. * associated with the given virtual port pending on the ring
  8827. * filtered by lpfc_sli_validate_fcp_iocb function.
  8828. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8829. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8830. * parameters
  8831. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8832. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8833. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8834. * FCP iocbs associated with virtual port.
  8835. * This function returns number of iocbs it failed to abort.
  8836. * This function is called with no locks held.
  8837. **/
  8838. int
  8839. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8840. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8841. {
  8842. struct lpfc_hba *phba = vport->phba;
  8843. struct lpfc_iocbq *iocbq;
  8844. struct lpfc_iocbq *abtsiocb;
  8845. IOCB_t *cmd = NULL;
  8846. int errcnt = 0, ret_val = 0;
  8847. int i;
  8848. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8849. iocbq = phba->sli.iocbq_lookup[i];
  8850. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8851. abort_cmd) != 0)
  8852. continue;
  8853. /* issue ABTS for this IOCB based on iotag */
  8854. abtsiocb = lpfc_sli_get_iocbq(phba);
  8855. if (abtsiocb == NULL) {
  8856. errcnt++;
  8857. continue;
  8858. }
  8859. cmd = &iocbq->iocb;
  8860. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8861. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8862. if (phba->sli_rev == LPFC_SLI_REV4)
  8863. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8864. else
  8865. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8866. abtsiocb->iocb.ulpLe = 1;
  8867. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8868. abtsiocb->vport = phba->pport;
  8869. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8870. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8871. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8872. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8873. if (lpfc_is_link_up(phba))
  8874. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8875. else
  8876. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8877. /* Setup callback routine and issue the command. */
  8878. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8879. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8880. abtsiocb, 0);
  8881. if (ret_val == IOCB_ERROR) {
  8882. lpfc_sli_release_iocbq(phba, abtsiocb);
  8883. errcnt++;
  8884. continue;
  8885. }
  8886. }
  8887. return errcnt;
  8888. }
  8889. /**
  8890. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8891. * @phba: Pointer to HBA context object.
  8892. * @cmdiocbq: Pointer to command iocb.
  8893. * @rspiocbq: Pointer to response iocb.
  8894. *
  8895. * This function is the completion handler for iocbs issued using
  8896. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8897. * ring event handler function without any lock held. This function
  8898. * can be called from both worker thread context and interrupt
  8899. * context. This function also can be called from other thread which
  8900. * cleans up the SLI layer objects.
  8901. * This function copy the contents of the response iocb to the
  8902. * response iocb memory object provided by the caller of
  8903. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8904. * sleeps for the iocb completion.
  8905. **/
  8906. static void
  8907. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8908. struct lpfc_iocbq *cmdiocbq,
  8909. struct lpfc_iocbq *rspiocbq)
  8910. {
  8911. wait_queue_head_t *pdone_q;
  8912. unsigned long iflags;
  8913. struct lpfc_scsi_buf *lpfc_cmd;
  8914. spin_lock_irqsave(&phba->hbalock, iflags);
  8915. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8916. if (cmdiocbq->context2 && rspiocbq)
  8917. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8918. &rspiocbq->iocb, sizeof(IOCB_t));
  8919. /* Set the exchange busy flag for task management commands */
  8920. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8921. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8922. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8923. cur_iocbq);
  8924. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8925. }
  8926. pdone_q = cmdiocbq->context_un.wait_queue;
  8927. if (pdone_q)
  8928. wake_up(pdone_q);
  8929. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8930. return;
  8931. }
  8932. /**
  8933. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8934. * @phba: Pointer to HBA context object..
  8935. * @piocbq: Pointer to command iocb.
  8936. * @flag: Flag to test.
  8937. *
  8938. * This routine grabs the hbalock and then test the iocb_flag to
  8939. * see if the passed in flag is set.
  8940. * Returns:
  8941. * 1 if flag is set.
  8942. * 0 if flag is not set.
  8943. **/
  8944. static int
  8945. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8946. struct lpfc_iocbq *piocbq, uint32_t flag)
  8947. {
  8948. unsigned long iflags;
  8949. int ret;
  8950. spin_lock_irqsave(&phba->hbalock, iflags);
  8951. ret = piocbq->iocb_flag & flag;
  8952. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8953. return ret;
  8954. }
  8955. /**
  8956. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8957. * @phba: Pointer to HBA context object..
  8958. * @pring: Pointer to sli ring.
  8959. * @piocb: Pointer to command iocb.
  8960. * @prspiocbq: Pointer to response iocb.
  8961. * @timeout: Timeout in number of seconds.
  8962. *
  8963. * This function issues the iocb to firmware and waits for the
  8964. * iocb to complete. If the iocb command is not
  8965. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8966. * Caller should not free the iocb resources if this function
  8967. * returns IOCB_TIMEDOUT.
  8968. * The function waits for the iocb completion using an
  8969. * non-interruptible wait.
  8970. * This function will sleep while waiting for iocb completion.
  8971. * So, this function should not be called from any context which
  8972. * does not allow sleeping. Due to the same reason, this function
  8973. * cannot be called with interrupt disabled.
  8974. * This function assumes that the iocb completions occur while
  8975. * this function sleep. So, this function cannot be called from
  8976. * the thread which process iocb completion for this ring.
  8977. * This function clears the iocb_flag of the iocb object before
  8978. * issuing the iocb and the iocb completion handler sets this
  8979. * flag and wakes this thread when the iocb completes.
  8980. * The contents of the response iocb will be copied to prspiocbq
  8981. * by the completion handler when the command completes.
  8982. * This function returns IOCB_SUCCESS when success.
  8983. * This function is called with no lock held.
  8984. **/
  8985. int
  8986. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8987. uint32_t ring_number,
  8988. struct lpfc_iocbq *piocb,
  8989. struct lpfc_iocbq *prspiocbq,
  8990. uint32_t timeout)
  8991. {
  8992. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8993. long timeleft, timeout_req = 0;
  8994. int retval = IOCB_SUCCESS;
  8995. uint32_t creg_val;
  8996. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8997. /*
  8998. * If the caller has provided a response iocbq buffer, then context2
  8999. * is NULL or its an error.
  9000. */
  9001. if (prspiocbq) {
  9002. if (piocb->context2)
  9003. return IOCB_ERROR;
  9004. piocb->context2 = prspiocbq;
  9005. }
  9006. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9007. piocb->context_un.wait_queue = &done_q;
  9008. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  9009. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9010. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9011. return IOCB_ERROR;
  9012. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9013. writel(creg_val, phba->HCregaddr);
  9014. readl(phba->HCregaddr); /* flush */
  9015. }
  9016. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9017. SLI_IOCB_RET_IOCB);
  9018. if (retval == IOCB_SUCCESS) {
  9019. timeout_req = timeout * HZ;
  9020. timeleft = wait_event_timeout(done_q,
  9021. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9022. timeout_req);
  9023. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  9024. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9025. "0331 IOCB wake signaled\n");
  9026. } else if (timeleft == 0) {
  9027. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9028. "0338 IOCB wait timeout error - no "
  9029. "wake response Data x%x\n", timeout);
  9030. retval = IOCB_TIMEDOUT;
  9031. } else {
  9032. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9033. "0330 IOCB wake NOT set, "
  9034. "Data x%x x%lx\n",
  9035. timeout, (timeleft / jiffies));
  9036. retval = IOCB_TIMEDOUT;
  9037. }
  9038. } else if (retval == IOCB_BUSY) {
  9039. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9040. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9041. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  9042. return retval;
  9043. } else {
  9044. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9045. "0332 IOCB wait issue failed, Data x%x\n",
  9046. retval);
  9047. retval = IOCB_ERROR;
  9048. }
  9049. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9050. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9051. return IOCB_ERROR;
  9052. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9053. writel(creg_val, phba->HCregaddr);
  9054. readl(phba->HCregaddr); /* flush */
  9055. }
  9056. if (prspiocbq)
  9057. piocb->context2 = NULL;
  9058. piocb->context_un.wait_queue = NULL;
  9059. piocb->iocb_cmpl = NULL;
  9060. return retval;
  9061. }
  9062. /**
  9063. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9064. * @phba: Pointer to HBA context object.
  9065. * @pmboxq: Pointer to driver mailbox object.
  9066. * @timeout: Timeout in number of seconds.
  9067. *
  9068. * This function issues the mailbox to firmware and waits for the
  9069. * mailbox command to complete. If the mailbox command is not
  9070. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9071. * The function waits for the mailbox completion using an
  9072. * interruptible wait. If the thread is woken up due to a
  9073. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9074. * should not free the mailbox resources, if this function returns
  9075. * MBX_TIMEOUT.
  9076. * This function will sleep while waiting for mailbox completion.
  9077. * So, this function should not be called from any context which
  9078. * does not allow sleeping. Due to the same reason, this function
  9079. * cannot be called with interrupt disabled.
  9080. * This function assumes that the mailbox completion occurs while
  9081. * this function sleep. So, this function cannot be called from
  9082. * the worker thread which processes mailbox completion.
  9083. * This function is called in the context of HBA management
  9084. * applications.
  9085. * This function returns MBX_SUCCESS when successful.
  9086. * This function is called with no lock held.
  9087. **/
  9088. int
  9089. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9090. uint32_t timeout)
  9091. {
  9092. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9093. int retval;
  9094. unsigned long flag;
  9095. /* The caller must leave context1 empty. */
  9096. if (pmboxq->context1)
  9097. return MBX_NOT_FINISHED;
  9098. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9099. /* setup wake call as IOCB callback */
  9100. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9101. /* setup context field to pass wait_queue pointer to wake function */
  9102. pmboxq->context1 = &done_q;
  9103. /* now issue the command */
  9104. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9105. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9106. wait_event_interruptible_timeout(done_q,
  9107. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9108. timeout * HZ);
  9109. spin_lock_irqsave(&phba->hbalock, flag);
  9110. pmboxq->context1 = NULL;
  9111. /*
  9112. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9113. * else do not free the resources.
  9114. */
  9115. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9116. retval = MBX_SUCCESS;
  9117. lpfc_sli4_swap_str(phba, pmboxq);
  9118. } else {
  9119. retval = MBX_TIMEOUT;
  9120. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9121. }
  9122. spin_unlock_irqrestore(&phba->hbalock, flag);
  9123. }
  9124. return retval;
  9125. }
  9126. /**
  9127. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9128. * @phba: Pointer to HBA context.
  9129. *
  9130. * This function is called to shutdown the driver's mailbox sub-system.
  9131. * It first marks the mailbox sub-system is in a block state to prevent
  9132. * the asynchronous mailbox command from issued off the pending mailbox
  9133. * command queue. If the mailbox command sub-system shutdown is due to
  9134. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9135. * the mailbox sub-system flush routine to forcefully bring down the
  9136. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9137. * as with offline or HBA function reset), this routine will wait for the
  9138. * outstanding mailbox command to complete before invoking the mailbox
  9139. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9140. **/
  9141. void
  9142. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9143. {
  9144. struct lpfc_sli *psli = &phba->sli;
  9145. unsigned long timeout;
  9146. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9147. /* delay 100ms for port state */
  9148. msleep(100);
  9149. lpfc_sli_mbox_sys_flush(phba);
  9150. return;
  9151. }
  9152. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9153. spin_lock_irq(&phba->hbalock);
  9154. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9155. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9156. /* Determine how long we might wait for the active mailbox
  9157. * command to be gracefully completed by firmware.
  9158. */
  9159. if (phba->sli.mbox_active)
  9160. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9161. phba->sli.mbox_active) *
  9162. 1000) + jiffies;
  9163. spin_unlock_irq(&phba->hbalock);
  9164. while (phba->sli.mbox_active) {
  9165. /* Check active mailbox complete status every 2ms */
  9166. msleep(2);
  9167. if (time_after(jiffies, timeout))
  9168. /* Timeout, let the mailbox flush routine to
  9169. * forcefully release active mailbox command
  9170. */
  9171. break;
  9172. }
  9173. } else
  9174. spin_unlock_irq(&phba->hbalock);
  9175. lpfc_sli_mbox_sys_flush(phba);
  9176. }
  9177. /**
  9178. * lpfc_sli_eratt_read - read sli-3 error attention events
  9179. * @phba: Pointer to HBA context.
  9180. *
  9181. * This function is called to read the SLI3 device error attention registers
  9182. * for possible error attention events. The caller must hold the hostlock
  9183. * with spin_lock_irq().
  9184. *
  9185. * This function returns 1 when there is Error Attention in the Host Attention
  9186. * Register and returns 0 otherwise.
  9187. **/
  9188. static int
  9189. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9190. {
  9191. uint32_t ha_copy;
  9192. /* Read chip Host Attention (HA) register */
  9193. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9194. goto unplug_err;
  9195. if (ha_copy & HA_ERATT) {
  9196. /* Read host status register to retrieve error event */
  9197. if (lpfc_sli_read_hs(phba))
  9198. goto unplug_err;
  9199. /* Check if there is a deferred error condition is active */
  9200. if ((HS_FFER1 & phba->work_hs) &&
  9201. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9202. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9203. phba->hba_flag |= DEFER_ERATT;
  9204. /* Clear all interrupt enable conditions */
  9205. writel(0, phba->HCregaddr);
  9206. readl(phba->HCregaddr);
  9207. }
  9208. /* Set the driver HA work bitmap */
  9209. phba->work_ha |= HA_ERATT;
  9210. /* Indicate polling handles this ERATT */
  9211. phba->hba_flag |= HBA_ERATT_HANDLED;
  9212. return 1;
  9213. }
  9214. return 0;
  9215. unplug_err:
  9216. /* Set the driver HS work bitmap */
  9217. phba->work_hs |= UNPLUG_ERR;
  9218. /* Set the driver HA work bitmap */
  9219. phba->work_ha |= HA_ERATT;
  9220. /* Indicate polling handles this ERATT */
  9221. phba->hba_flag |= HBA_ERATT_HANDLED;
  9222. return 1;
  9223. }
  9224. /**
  9225. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9226. * @phba: Pointer to HBA context.
  9227. *
  9228. * This function is called to read the SLI4 device error attention registers
  9229. * for possible error attention events. The caller must hold the hostlock
  9230. * with spin_lock_irq().
  9231. *
  9232. * This function returns 1 when there is Error Attention in the Host Attention
  9233. * Register and returns 0 otherwise.
  9234. **/
  9235. static int
  9236. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9237. {
  9238. uint32_t uerr_sta_hi, uerr_sta_lo;
  9239. uint32_t if_type, portsmphr;
  9240. struct lpfc_register portstat_reg;
  9241. /*
  9242. * For now, use the SLI4 device internal unrecoverable error
  9243. * registers for error attention. This can be changed later.
  9244. */
  9245. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9246. switch (if_type) {
  9247. case LPFC_SLI_INTF_IF_TYPE_0:
  9248. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9249. &uerr_sta_lo) ||
  9250. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9251. &uerr_sta_hi)) {
  9252. phba->work_hs |= UNPLUG_ERR;
  9253. phba->work_ha |= HA_ERATT;
  9254. phba->hba_flag |= HBA_ERATT_HANDLED;
  9255. return 1;
  9256. }
  9257. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9258. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9259. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9260. "1423 HBA Unrecoverable error: "
  9261. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9262. "ue_mask_lo_reg=0x%x, "
  9263. "ue_mask_hi_reg=0x%x\n",
  9264. uerr_sta_lo, uerr_sta_hi,
  9265. phba->sli4_hba.ue_mask_lo,
  9266. phba->sli4_hba.ue_mask_hi);
  9267. phba->work_status[0] = uerr_sta_lo;
  9268. phba->work_status[1] = uerr_sta_hi;
  9269. phba->work_ha |= HA_ERATT;
  9270. phba->hba_flag |= HBA_ERATT_HANDLED;
  9271. return 1;
  9272. }
  9273. break;
  9274. case LPFC_SLI_INTF_IF_TYPE_2:
  9275. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9276. &portstat_reg.word0) ||
  9277. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9278. &portsmphr)){
  9279. phba->work_hs |= UNPLUG_ERR;
  9280. phba->work_ha |= HA_ERATT;
  9281. phba->hba_flag |= HBA_ERATT_HANDLED;
  9282. return 1;
  9283. }
  9284. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9285. phba->work_status[0] =
  9286. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9287. phba->work_status[1] =
  9288. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9289. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9290. "2885 Port Status Event: "
  9291. "port status reg 0x%x, "
  9292. "port smphr reg 0x%x, "
  9293. "error 1=0x%x, error 2=0x%x\n",
  9294. portstat_reg.word0,
  9295. portsmphr,
  9296. phba->work_status[0],
  9297. phba->work_status[1]);
  9298. phba->work_ha |= HA_ERATT;
  9299. phba->hba_flag |= HBA_ERATT_HANDLED;
  9300. return 1;
  9301. }
  9302. break;
  9303. case LPFC_SLI_INTF_IF_TYPE_1:
  9304. default:
  9305. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9306. "2886 HBA Error Attention on unsupported "
  9307. "if type %d.", if_type);
  9308. return 1;
  9309. }
  9310. return 0;
  9311. }
  9312. /**
  9313. * lpfc_sli_check_eratt - check error attention events
  9314. * @phba: Pointer to HBA context.
  9315. *
  9316. * This function is called from timer soft interrupt context to check HBA's
  9317. * error attention register bit for error attention events.
  9318. *
  9319. * This function returns 1 when there is Error Attention in the Host Attention
  9320. * Register and returns 0 otherwise.
  9321. **/
  9322. int
  9323. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9324. {
  9325. uint32_t ha_copy;
  9326. /* If somebody is waiting to handle an eratt, don't process it
  9327. * here. The brdkill function will do this.
  9328. */
  9329. if (phba->link_flag & LS_IGNORE_ERATT)
  9330. return 0;
  9331. /* Check if interrupt handler handles this ERATT */
  9332. spin_lock_irq(&phba->hbalock);
  9333. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9334. /* Interrupt handler has handled ERATT */
  9335. spin_unlock_irq(&phba->hbalock);
  9336. return 0;
  9337. }
  9338. /*
  9339. * If there is deferred error attention, do not check for error
  9340. * attention
  9341. */
  9342. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9343. spin_unlock_irq(&phba->hbalock);
  9344. return 0;
  9345. }
  9346. /* If PCI channel is offline, don't process it */
  9347. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9348. spin_unlock_irq(&phba->hbalock);
  9349. return 0;
  9350. }
  9351. switch (phba->sli_rev) {
  9352. case LPFC_SLI_REV2:
  9353. case LPFC_SLI_REV3:
  9354. /* Read chip Host Attention (HA) register */
  9355. ha_copy = lpfc_sli_eratt_read(phba);
  9356. break;
  9357. case LPFC_SLI_REV4:
  9358. /* Read device Uncoverable Error (UERR) registers */
  9359. ha_copy = lpfc_sli4_eratt_read(phba);
  9360. break;
  9361. default:
  9362. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9363. "0299 Invalid SLI revision (%d)\n",
  9364. phba->sli_rev);
  9365. ha_copy = 0;
  9366. break;
  9367. }
  9368. spin_unlock_irq(&phba->hbalock);
  9369. return ha_copy;
  9370. }
  9371. /**
  9372. * lpfc_intr_state_check - Check device state for interrupt handling
  9373. * @phba: Pointer to HBA context.
  9374. *
  9375. * This inline routine checks whether a device or its PCI slot is in a state
  9376. * that the interrupt should be handled.
  9377. *
  9378. * This function returns 0 if the device or the PCI slot is in a state that
  9379. * interrupt should be handled, otherwise -EIO.
  9380. */
  9381. static inline int
  9382. lpfc_intr_state_check(struct lpfc_hba *phba)
  9383. {
  9384. /* If the pci channel is offline, ignore all the interrupts */
  9385. if (unlikely(pci_channel_offline(phba->pcidev)))
  9386. return -EIO;
  9387. /* Update device level interrupt statistics */
  9388. phba->sli.slistat.sli_intr++;
  9389. /* Ignore all interrupts during initialization. */
  9390. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9391. return -EIO;
  9392. return 0;
  9393. }
  9394. /**
  9395. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9396. * @irq: Interrupt number.
  9397. * @dev_id: The device context pointer.
  9398. *
  9399. * This function is directly called from the PCI layer as an interrupt
  9400. * service routine when device with SLI-3 interface spec is enabled with
  9401. * MSI-X multi-message interrupt mode and there are slow-path events in
  9402. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9403. * interrupt mode, this function is called as part of the device-level
  9404. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9405. * is undergoing initialization, the interrupt handler will not process
  9406. * the interrupt. The link attention and ELS ring attention events are
  9407. * handled by the worker thread. The interrupt handler signals the worker
  9408. * thread and returns for these events. This function is called without
  9409. * any lock held. It gets the hbalock to access and update SLI data
  9410. * structures.
  9411. *
  9412. * This function returns IRQ_HANDLED when interrupt is handled else it
  9413. * returns IRQ_NONE.
  9414. **/
  9415. irqreturn_t
  9416. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9417. {
  9418. struct lpfc_hba *phba;
  9419. uint32_t ha_copy, hc_copy;
  9420. uint32_t work_ha_copy;
  9421. unsigned long status;
  9422. unsigned long iflag;
  9423. uint32_t control;
  9424. MAILBOX_t *mbox, *pmbox;
  9425. struct lpfc_vport *vport;
  9426. struct lpfc_nodelist *ndlp;
  9427. struct lpfc_dmabuf *mp;
  9428. LPFC_MBOXQ_t *pmb;
  9429. int rc;
  9430. /*
  9431. * Get the driver's phba structure from the dev_id and
  9432. * assume the HBA is not interrupting.
  9433. */
  9434. phba = (struct lpfc_hba *)dev_id;
  9435. if (unlikely(!phba))
  9436. return IRQ_NONE;
  9437. /*
  9438. * Stuff needs to be attented to when this function is invoked as an
  9439. * individual interrupt handler in MSI-X multi-message interrupt mode
  9440. */
  9441. if (phba->intr_type == MSIX) {
  9442. /* Check device state for handling interrupt */
  9443. if (lpfc_intr_state_check(phba))
  9444. return IRQ_NONE;
  9445. /* Need to read HA REG for slow-path events */
  9446. spin_lock_irqsave(&phba->hbalock, iflag);
  9447. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9448. goto unplug_error;
  9449. /* If somebody is waiting to handle an eratt don't process it
  9450. * here. The brdkill function will do this.
  9451. */
  9452. if (phba->link_flag & LS_IGNORE_ERATT)
  9453. ha_copy &= ~HA_ERATT;
  9454. /* Check the need for handling ERATT in interrupt handler */
  9455. if (ha_copy & HA_ERATT) {
  9456. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9457. /* ERATT polling has handled ERATT */
  9458. ha_copy &= ~HA_ERATT;
  9459. else
  9460. /* Indicate interrupt handler handles ERATT */
  9461. phba->hba_flag |= HBA_ERATT_HANDLED;
  9462. }
  9463. /*
  9464. * If there is deferred error attention, do not check for any
  9465. * interrupt.
  9466. */
  9467. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9468. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9469. return IRQ_NONE;
  9470. }
  9471. /* Clear up only attention source related to slow-path */
  9472. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9473. goto unplug_error;
  9474. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9475. HC_LAINT_ENA | HC_ERINT_ENA),
  9476. phba->HCregaddr);
  9477. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9478. phba->HAregaddr);
  9479. writel(hc_copy, phba->HCregaddr);
  9480. readl(phba->HAregaddr); /* flush */
  9481. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9482. } else
  9483. ha_copy = phba->ha_copy;
  9484. work_ha_copy = ha_copy & phba->work_ha_mask;
  9485. if (work_ha_copy) {
  9486. if (work_ha_copy & HA_LATT) {
  9487. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9488. /*
  9489. * Turn off Link Attention interrupts
  9490. * until CLEAR_LA done
  9491. */
  9492. spin_lock_irqsave(&phba->hbalock, iflag);
  9493. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9494. if (lpfc_readl(phba->HCregaddr, &control))
  9495. goto unplug_error;
  9496. control &= ~HC_LAINT_ENA;
  9497. writel(control, phba->HCregaddr);
  9498. readl(phba->HCregaddr); /* flush */
  9499. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9500. }
  9501. else
  9502. work_ha_copy &= ~HA_LATT;
  9503. }
  9504. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9505. /*
  9506. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9507. * the only slow ring.
  9508. */
  9509. status = (work_ha_copy &
  9510. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9511. status >>= (4*LPFC_ELS_RING);
  9512. if (status & HA_RXMASK) {
  9513. spin_lock_irqsave(&phba->hbalock, iflag);
  9514. if (lpfc_readl(phba->HCregaddr, &control))
  9515. goto unplug_error;
  9516. lpfc_debugfs_slow_ring_trc(phba,
  9517. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9518. control, status,
  9519. (uint32_t)phba->sli.slistat.sli_intr);
  9520. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9521. lpfc_debugfs_slow_ring_trc(phba,
  9522. "ISR Disable ring:"
  9523. "pwork:x%x hawork:x%x wait:x%x",
  9524. phba->work_ha, work_ha_copy,
  9525. (uint32_t)((unsigned long)
  9526. &phba->work_waitq));
  9527. control &=
  9528. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9529. writel(control, phba->HCregaddr);
  9530. readl(phba->HCregaddr); /* flush */
  9531. }
  9532. else {
  9533. lpfc_debugfs_slow_ring_trc(phba,
  9534. "ISR slow ring: pwork:"
  9535. "x%x hawork:x%x wait:x%x",
  9536. phba->work_ha, work_ha_copy,
  9537. (uint32_t)((unsigned long)
  9538. &phba->work_waitq));
  9539. }
  9540. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9541. }
  9542. }
  9543. spin_lock_irqsave(&phba->hbalock, iflag);
  9544. if (work_ha_copy & HA_ERATT) {
  9545. if (lpfc_sli_read_hs(phba))
  9546. goto unplug_error;
  9547. /*
  9548. * Check if there is a deferred error condition
  9549. * is active
  9550. */
  9551. if ((HS_FFER1 & phba->work_hs) &&
  9552. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9553. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9554. phba->work_hs)) {
  9555. phba->hba_flag |= DEFER_ERATT;
  9556. /* Clear all interrupt enable conditions */
  9557. writel(0, phba->HCregaddr);
  9558. readl(phba->HCregaddr);
  9559. }
  9560. }
  9561. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9562. pmb = phba->sli.mbox_active;
  9563. pmbox = &pmb->u.mb;
  9564. mbox = phba->mbox;
  9565. vport = pmb->vport;
  9566. /* First check out the status word */
  9567. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9568. if (pmbox->mbxOwner != OWN_HOST) {
  9569. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9570. /*
  9571. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9572. * mbxStatus <status>
  9573. */
  9574. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9575. LOG_SLI,
  9576. "(%d):0304 Stray Mailbox "
  9577. "Interrupt mbxCommand x%x "
  9578. "mbxStatus x%x\n",
  9579. (vport ? vport->vpi : 0),
  9580. pmbox->mbxCommand,
  9581. pmbox->mbxStatus);
  9582. /* clear mailbox attention bit */
  9583. work_ha_copy &= ~HA_MBATT;
  9584. } else {
  9585. phba->sli.mbox_active = NULL;
  9586. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9587. phba->last_completion_time = jiffies;
  9588. del_timer(&phba->sli.mbox_tmo);
  9589. if (pmb->mbox_cmpl) {
  9590. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9591. MAILBOX_CMD_SIZE);
  9592. if (pmb->out_ext_byte_len &&
  9593. pmb->context2)
  9594. lpfc_sli_pcimem_bcopy(
  9595. phba->mbox_ext,
  9596. pmb->context2,
  9597. pmb->out_ext_byte_len);
  9598. }
  9599. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9600. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9601. lpfc_debugfs_disc_trc(vport,
  9602. LPFC_DISC_TRC_MBOX_VPORT,
  9603. "MBOX dflt rpi: : "
  9604. "status:x%x rpi:x%x",
  9605. (uint32_t)pmbox->mbxStatus,
  9606. pmbox->un.varWords[0], 0);
  9607. if (!pmbox->mbxStatus) {
  9608. mp = (struct lpfc_dmabuf *)
  9609. (pmb->context1);
  9610. ndlp = (struct lpfc_nodelist *)
  9611. pmb->context2;
  9612. /* Reg_LOGIN of dflt RPI was
  9613. * successful. new lets get
  9614. * rid of the RPI using the
  9615. * same mbox buffer.
  9616. */
  9617. lpfc_unreg_login(phba,
  9618. vport->vpi,
  9619. pmbox->un.varWords[0],
  9620. pmb);
  9621. pmb->mbox_cmpl =
  9622. lpfc_mbx_cmpl_dflt_rpi;
  9623. pmb->context1 = mp;
  9624. pmb->context2 = ndlp;
  9625. pmb->vport = vport;
  9626. rc = lpfc_sli_issue_mbox(phba,
  9627. pmb,
  9628. MBX_NOWAIT);
  9629. if (rc != MBX_BUSY)
  9630. lpfc_printf_log(phba,
  9631. KERN_ERR,
  9632. LOG_MBOX | LOG_SLI,
  9633. "0350 rc should have"
  9634. "been MBX_BUSY\n");
  9635. if (rc != MBX_NOT_FINISHED)
  9636. goto send_current_mbox;
  9637. }
  9638. }
  9639. spin_lock_irqsave(
  9640. &phba->pport->work_port_lock,
  9641. iflag);
  9642. phba->pport->work_port_events &=
  9643. ~WORKER_MBOX_TMO;
  9644. spin_unlock_irqrestore(
  9645. &phba->pport->work_port_lock,
  9646. iflag);
  9647. lpfc_mbox_cmpl_put(phba, pmb);
  9648. }
  9649. } else
  9650. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9651. if ((work_ha_copy & HA_MBATT) &&
  9652. (phba->sli.mbox_active == NULL)) {
  9653. send_current_mbox:
  9654. /* Process next mailbox command if there is one */
  9655. do {
  9656. rc = lpfc_sli_issue_mbox(phba, NULL,
  9657. MBX_NOWAIT);
  9658. } while (rc == MBX_NOT_FINISHED);
  9659. if (rc != MBX_SUCCESS)
  9660. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9661. LOG_SLI, "0349 rc should be "
  9662. "MBX_SUCCESS\n");
  9663. }
  9664. spin_lock_irqsave(&phba->hbalock, iflag);
  9665. phba->work_ha |= work_ha_copy;
  9666. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9667. lpfc_worker_wake_up(phba);
  9668. }
  9669. return IRQ_HANDLED;
  9670. unplug_error:
  9671. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9672. return IRQ_HANDLED;
  9673. } /* lpfc_sli_sp_intr_handler */
  9674. /**
  9675. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9676. * @irq: Interrupt number.
  9677. * @dev_id: The device context pointer.
  9678. *
  9679. * This function is directly called from the PCI layer as an interrupt
  9680. * service routine when device with SLI-3 interface spec is enabled with
  9681. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9682. * ring event in the HBA. However, when the device is enabled with either
  9683. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9684. * device-level interrupt handler. When the PCI slot is in error recovery
  9685. * or the HBA is undergoing initialization, the interrupt handler will not
  9686. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9687. * the intrrupt context. This function is called without any lock held.
  9688. * It gets the hbalock to access and update SLI data structures.
  9689. *
  9690. * This function returns IRQ_HANDLED when interrupt is handled else it
  9691. * returns IRQ_NONE.
  9692. **/
  9693. irqreturn_t
  9694. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9695. {
  9696. struct lpfc_hba *phba;
  9697. uint32_t ha_copy;
  9698. unsigned long status;
  9699. unsigned long iflag;
  9700. /* Get the driver's phba structure from the dev_id and
  9701. * assume the HBA is not interrupting.
  9702. */
  9703. phba = (struct lpfc_hba *) dev_id;
  9704. if (unlikely(!phba))
  9705. return IRQ_NONE;
  9706. /*
  9707. * Stuff needs to be attented to when this function is invoked as an
  9708. * individual interrupt handler in MSI-X multi-message interrupt mode
  9709. */
  9710. if (phba->intr_type == MSIX) {
  9711. /* Check device state for handling interrupt */
  9712. if (lpfc_intr_state_check(phba))
  9713. return IRQ_NONE;
  9714. /* Need to read HA REG for FCP ring and other ring events */
  9715. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9716. return IRQ_HANDLED;
  9717. /* Clear up only attention source related to fast-path */
  9718. spin_lock_irqsave(&phba->hbalock, iflag);
  9719. /*
  9720. * If there is deferred error attention, do not check for
  9721. * any interrupt.
  9722. */
  9723. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9724. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9725. return IRQ_NONE;
  9726. }
  9727. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9728. phba->HAregaddr);
  9729. readl(phba->HAregaddr); /* flush */
  9730. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9731. } else
  9732. ha_copy = phba->ha_copy;
  9733. /*
  9734. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9735. */
  9736. ha_copy &= ~(phba->work_ha_mask);
  9737. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9738. status >>= (4*LPFC_FCP_RING);
  9739. if (status & HA_RXMASK)
  9740. lpfc_sli_handle_fast_ring_event(phba,
  9741. &phba->sli.ring[LPFC_FCP_RING],
  9742. status);
  9743. if (phba->cfg_multi_ring_support == 2) {
  9744. /*
  9745. * Process all events on extra ring. Take the optimized path
  9746. * for extra ring IO.
  9747. */
  9748. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9749. status >>= (4*LPFC_EXTRA_RING);
  9750. if (status & HA_RXMASK) {
  9751. lpfc_sli_handle_fast_ring_event(phba,
  9752. &phba->sli.ring[LPFC_EXTRA_RING],
  9753. status);
  9754. }
  9755. }
  9756. return IRQ_HANDLED;
  9757. } /* lpfc_sli_fp_intr_handler */
  9758. /**
  9759. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9760. * @irq: Interrupt number.
  9761. * @dev_id: The device context pointer.
  9762. *
  9763. * This function is the HBA device-level interrupt handler to device with
  9764. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9765. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9766. * requires driver attention. This function invokes the slow-path interrupt
  9767. * attention handling function and fast-path interrupt attention handling
  9768. * function in turn to process the relevant HBA attention events. This
  9769. * function is called without any lock held. It gets the hbalock to access
  9770. * and update SLI data structures.
  9771. *
  9772. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9773. * returns IRQ_NONE.
  9774. **/
  9775. irqreturn_t
  9776. lpfc_sli_intr_handler(int irq, void *dev_id)
  9777. {
  9778. struct lpfc_hba *phba;
  9779. irqreturn_t sp_irq_rc, fp_irq_rc;
  9780. unsigned long status1, status2;
  9781. uint32_t hc_copy;
  9782. /*
  9783. * Get the driver's phba structure from the dev_id and
  9784. * assume the HBA is not interrupting.
  9785. */
  9786. phba = (struct lpfc_hba *) dev_id;
  9787. if (unlikely(!phba))
  9788. return IRQ_NONE;
  9789. /* Check device state for handling interrupt */
  9790. if (lpfc_intr_state_check(phba))
  9791. return IRQ_NONE;
  9792. spin_lock(&phba->hbalock);
  9793. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9794. spin_unlock(&phba->hbalock);
  9795. return IRQ_HANDLED;
  9796. }
  9797. if (unlikely(!phba->ha_copy)) {
  9798. spin_unlock(&phba->hbalock);
  9799. return IRQ_NONE;
  9800. } else if (phba->ha_copy & HA_ERATT) {
  9801. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9802. /* ERATT polling has handled ERATT */
  9803. phba->ha_copy &= ~HA_ERATT;
  9804. else
  9805. /* Indicate interrupt handler handles ERATT */
  9806. phba->hba_flag |= HBA_ERATT_HANDLED;
  9807. }
  9808. /*
  9809. * If there is deferred error attention, do not check for any interrupt.
  9810. */
  9811. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9812. spin_unlock(&phba->hbalock);
  9813. return IRQ_NONE;
  9814. }
  9815. /* Clear attention sources except link and error attentions */
  9816. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9817. spin_unlock(&phba->hbalock);
  9818. return IRQ_HANDLED;
  9819. }
  9820. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9821. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9822. phba->HCregaddr);
  9823. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9824. writel(hc_copy, phba->HCregaddr);
  9825. readl(phba->HAregaddr); /* flush */
  9826. spin_unlock(&phba->hbalock);
  9827. /*
  9828. * Invokes slow-path host attention interrupt handling as appropriate.
  9829. */
  9830. /* status of events with mailbox and link attention */
  9831. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9832. /* status of events with ELS ring */
  9833. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9834. status2 >>= (4*LPFC_ELS_RING);
  9835. if (status1 || (status2 & HA_RXMASK))
  9836. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9837. else
  9838. sp_irq_rc = IRQ_NONE;
  9839. /*
  9840. * Invoke fast-path host attention interrupt handling as appropriate.
  9841. */
  9842. /* status of events with FCP ring */
  9843. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9844. status1 >>= (4*LPFC_FCP_RING);
  9845. /* status of events with extra ring */
  9846. if (phba->cfg_multi_ring_support == 2) {
  9847. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9848. status2 >>= (4*LPFC_EXTRA_RING);
  9849. } else
  9850. status2 = 0;
  9851. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9852. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9853. else
  9854. fp_irq_rc = IRQ_NONE;
  9855. /* Return device-level interrupt handling status */
  9856. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9857. } /* lpfc_sli_intr_handler */
  9858. /**
  9859. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9860. * @phba: pointer to lpfc hba data structure.
  9861. *
  9862. * This routine is invoked by the worker thread to process all the pending
  9863. * SLI4 FCP abort XRI events.
  9864. **/
  9865. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9866. {
  9867. struct lpfc_cq_event *cq_event;
  9868. /* First, declare the fcp xri abort event has been handled */
  9869. spin_lock_irq(&phba->hbalock);
  9870. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9871. spin_unlock_irq(&phba->hbalock);
  9872. /* Now, handle all the fcp xri abort events */
  9873. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9874. /* Get the first event from the head of the event queue */
  9875. spin_lock_irq(&phba->hbalock);
  9876. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9877. cq_event, struct lpfc_cq_event, list);
  9878. spin_unlock_irq(&phba->hbalock);
  9879. /* Notify aborted XRI for FCP work queue */
  9880. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9881. /* Free the event processed back to the free pool */
  9882. lpfc_sli4_cq_event_release(phba, cq_event);
  9883. }
  9884. }
  9885. /**
  9886. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9887. * @phba: pointer to lpfc hba data structure.
  9888. *
  9889. * This routine is invoked by the worker thread to process all the pending
  9890. * SLI4 els abort xri events.
  9891. **/
  9892. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9893. {
  9894. struct lpfc_cq_event *cq_event;
  9895. /* First, declare the els xri abort event has been handled */
  9896. spin_lock_irq(&phba->hbalock);
  9897. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9898. spin_unlock_irq(&phba->hbalock);
  9899. /* Now, handle all the els xri abort events */
  9900. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9901. /* Get the first event from the head of the event queue */
  9902. spin_lock_irq(&phba->hbalock);
  9903. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9904. cq_event, struct lpfc_cq_event, list);
  9905. spin_unlock_irq(&phba->hbalock);
  9906. /* Notify aborted XRI for ELS work queue */
  9907. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9908. /* Free the event processed back to the free pool */
  9909. lpfc_sli4_cq_event_release(phba, cq_event);
  9910. }
  9911. }
  9912. /**
  9913. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9914. * @phba: pointer to lpfc hba data structure
  9915. * @pIocbIn: pointer to the rspiocbq
  9916. * @pIocbOut: pointer to the cmdiocbq
  9917. * @wcqe: pointer to the complete wcqe
  9918. *
  9919. * This routine transfers the fields of a command iocbq to a response iocbq
  9920. * by copying all the IOCB fields from command iocbq and transferring the
  9921. * completion status information from the complete wcqe.
  9922. **/
  9923. static void
  9924. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9925. struct lpfc_iocbq *pIocbIn,
  9926. struct lpfc_iocbq *pIocbOut,
  9927. struct lpfc_wcqe_complete *wcqe)
  9928. {
  9929. unsigned long iflags;
  9930. uint32_t status;
  9931. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9932. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9933. sizeof(struct lpfc_iocbq) - offset);
  9934. /* Map WCQE parameters into irspiocb parameters */
  9935. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9936. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9937. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9938. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9939. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9940. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9941. wcqe->total_data_placed;
  9942. else
  9943. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9944. else {
  9945. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9946. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9947. }
  9948. /* Convert BG errors for completion status */
  9949. if (status == CQE_STATUS_DI_ERROR) {
  9950. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9951. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9952. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9953. else
  9954. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9955. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9956. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9957. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9958. BGS_GUARD_ERR_MASK;
  9959. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9960. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9961. BGS_APPTAG_ERR_MASK;
  9962. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9963. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9964. BGS_REFTAG_ERR_MASK;
  9965. /* Check to see if there was any good data before the error */
  9966. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9967. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9968. BGS_HI_WATER_MARK_PRESENT_MASK;
  9969. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9970. wcqe->total_data_placed;
  9971. }
  9972. /*
  9973. * Set ALL the error bits to indicate we don't know what
  9974. * type of error it is.
  9975. */
  9976. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9977. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9978. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9979. BGS_GUARD_ERR_MASK);
  9980. }
  9981. /* Pick up HBA exchange busy condition */
  9982. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9983. spin_lock_irqsave(&phba->hbalock, iflags);
  9984. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9985. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9986. }
  9987. }
  9988. /**
  9989. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9990. * @phba: Pointer to HBA context object.
  9991. * @wcqe: Pointer to work-queue completion queue entry.
  9992. *
  9993. * This routine handles an ELS work-queue completion event and construct
  9994. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9995. * discovery engine to handle.
  9996. *
  9997. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9998. **/
  9999. static struct lpfc_iocbq *
  10000. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10001. struct lpfc_iocbq *irspiocbq)
  10002. {
  10003. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10004. struct lpfc_iocbq *cmdiocbq;
  10005. struct lpfc_wcqe_complete *wcqe;
  10006. unsigned long iflags;
  10007. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10008. spin_lock_irqsave(&pring->ring_lock, iflags);
  10009. pring->stats.iocb_event++;
  10010. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10011. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10012. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10013. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10014. if (unlikely(!cmdiocbq)) {
  10015. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10016. "0386 ELS complete with no corresponding "
  10017. "cmdiocb: iotag (%d)\n",
  10018. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10019. lpfc_sli_release_iocbq(phba, irspiocbq);
  10020. return NULL;
  10021. }
  10022. /* Fake the irspiocbq and copy necessary response information */
  10023. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10024. return irspiocbq;
  10025. }
  10026. /**
  10027. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10028. * @phba: Pointer to HBA context object.
  10029. * @cqe: Pointer to mailbox completion queue entry.
  10030. *
  10031. * This routine process a mailbox completion queue entry with asynchrous
  10032. * event.
  10033. *
  10034. * Return: true if work posted to worker thread, otherwise false.
  10035. **/
  10036. static bool
  10037. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10038. {
  10039. struct lpfc_cq_event *cq_event;
  10040. unsigned long iflags;
  10041. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10042. "0392 Async Event: word0:x%x, word1:x%x, "
  10043. "word2:x%x, word3:x%x\n", mcqe->word0,
  10044. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10045. /* Allocate a new internal CQ_EVENT entry */
  10046. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10047. if (!cq_event) {
  10048. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10049. "0394 Failed to allocate CQ_EVENT entry\n");
  10050. return false;
  10051. }
  10052. /* Move the CQE into an asynchronous event entry */
  10053. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10054. spin_lock_irqsave(&phba->hbalock, iflags);
  10055. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10056. /* Set the async event flag */
  10057. phba->hba_flag |= ASYNC_EVENT;
  10058. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10059. return true;
  10060. }
  10061. /**
  10062. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10063. * @phba: Pointer to HBA context object.
  10064. * @cqe: Pointer to mailbox completion queue entry.
  10065. *
  10066. * This routine process a mailbox completion queue entry with mailbox
  10067. * completion event.
  10068. *
  10069. * Return: true if work posted to worker thread, otherwise false.
  10070. **/
  10071. static bool
  10072. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10073. {
  10074. uint32_t mcqe_status;
  10075. MAILBOX_t *mbox, *pmbox;
  10076. struct lpfc_mqe *mqe;
  10077. struct lpfc_vport *vport;
  10078. struct lpfc_nodelist *ndlp;
  10079. struct lpfc_dmabuf *mp;
  10080. unsigned long iflags;
  10081. LPFC_MBOXQ_t *pmb;
  10082. bool workposted = false;
  10083. int rc;
  10084. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10085. if (!bf_get(lpfc_trailer_completed, mcqe))
  10086. goto out_no_mqe_complete;
  10087. /* Get the reference to the active mbox command */
  10088. spin_lock_irqsave(&phba->hbalock, iflags);
  10089. pmb = phba->sli.mbox_active;
  10090. if (unlikely(!pmb)) {
  10091. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10092. "1832 No pending MBOX command to handle\n");
  10093. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10094. goto out_no_mqe_complete;
  10095. }
  10096. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10097. mqe = &pmb->u.mqe;
  10098. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10099. mbox = phba->mbox;
  10100. vport = pmb->vport;
  10101. /* Reset heartbeat timer */
  10102. phba->last_completion_time = jiffies;
  10103. del_timer(&phba->sli.mbox_tmo);
  10104. /* Move mbox data to caller's mailbox region, do endian swapping */
  10105. if (pmb->mbox_cmpl && mbox)
  10106. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10107. /*
  10108. * For mcqe errors, conditionally move a modified error code to
  10109. * the mbox so that the error will not be missed.
  10110. */
  10111. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10112. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10113. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10114. bf_set(lpfc_mqe_status, mqe,
  10115. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10116. }
  10117. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10118. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10119. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10120. "MBOX dflt rpi: status:x%x rpi:x%x",
  10121. mcqe_status,
  10122. pmbox->un.varWords[0], 0);
  10123. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10124. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10125. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10126. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10127. * RID of the PPI using the same mbox buffer.
  10128. */
  10129. lpfc_unreg_login(phba, vport->vpi,
  10130. pmbox->un.varWords[0], pmb);
  10131. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10132. pmb->context1 = mp;
  10133. pmb->context2 = ndlp;
  10134. pmb->vport = vport;
  10135. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10136. if (rc != MBX_BUSY)
  10137. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10138. LOG_SLI, "0385 rc should "
  10139. "have been MBX_BUSY\n");
  10140. if (rc != MBX_NOT_FINISHED)
  10141. goto send_current_mbox;
  10142. }
  10143. }
  10144. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10145. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10146. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10147. /* There is mailbox completion work to do */
  10148. spin_lock_irqsave(&phba->hbalock, iflags);
  10149. __lpfc_mbox_cmpl_put(phba, pmb);
  10150. phba->work_ha |= HA_MBATT;
  10151. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10152. workposted = true;
  10153. send_current_mbox:
  10154. spin_lock_irqsave(&phba->hbalock, iflags);
  10155. /* Release the mailbox command posting token */
  10156. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10157. /* Setting active mailbox pointer need to be in sync to flag clear */
  10158. phba->sli.mbox_active = NULL;
  10159. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10160. /* Wake up worker thread to post the next pending mailbox command */
  10161. lpfc_worker_wake_up(phba);
  10162. out_no_mqe_complete:
  10163. if (bf_get(lpfc_trailer_consumed, mcqe))
  10164. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10165. return workposted;
  10166. }
  10167. /**
  10168. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10169. * @phba: Pointer to HBA context object.
  10170. * @cqe: Pointer to mailbox completion queue entry.
  10171. *
  10172. * This routine process a mailbox completion queue entry, it invokes the
  10173. * proper mailbox complete handling or asynchrous event handling routine
  10174. * according to the MCQE's async bit.
  10175. *
  10176. * Return: true if work posted to worker thread, otherwise false.
  10177. **/
  10178. static bool
  10179. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10180. {
  10181. struct lpfc_mcqe mcqe;
  10182. bool workposted;
  10183. /* Copy the mailbox MCQE and convert endian order as needed */
  10184. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10185. /* Invoke the proper event handling routine */
  10186. if (!bf_get(lpfc_trailer_async, &mcqe))
  10187. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10188. else
  10189. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10190. return workposted;
  10191. }
  10192. /**
  10193. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10194. * @phba: Pointer to HBA context object.
  10195. * @wcqe: Pointer to work-queue completion queue entry.
  10196. *
  10197. * This routine handles an ELS work-queue completion event.
  10198. *
  10199. * Return: true if work posted to worker thread, otherwise false.
  10200. **/
  10201. static bool
  10202. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10203. struct lpfc_wcqe_complete *wcqe)
  10204. {
  10205. struct lpfc_iocbq *irspiocbq;
  10206. unsigned long iflags;
  10207. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10208. /* Get an irspiocbq for later ELS response processing use */
  10209. irspiocbq = lpfc_sli_get_iocbq(phba);
  10210. if (!irspiocbq) {
  10211. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10212. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10213. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10214. pring->txq_cnt, phba->iocb_cnt,
  10215. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10216. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10217. return false;
  10218. }
  10219. /* Save off the slow-path queue event for work thread to process */
  10220. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10221. spin_lock_irqsave(&phba->hbalock, iflags);
  10222. list_add_tail(&irspiocbq->cq_event.list,
  10223. &phba->sli4_hba.sp_queue_event);
  10224. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10225. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10226. return true;
  10227. }
  10228. /**
  10229. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10230. * @phba: Pointer to HBA context object.
  10231. * @wcqe: Pointer to work-queue completion queue entry.
  10232. *
  10233. * This routine handles slow-path WQ entry comsumed event by invoking the
  10234. * proper WQ release routine to the slow-path WQ.
  10235. **/
  10236. static void
  10237. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10238. struct lpfc_wcqe_release *wcqe)
  10239. {
  10240. /* sanity check on queue memory */
  10241. if (unlikely(!phba->sli4_hba.els_wq))
  10242. return;
  10243. /* Check for the slow-path ELS work queue */
  10244. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10245. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10246. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10247. else
  10248. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10249. "2579 Slow-path wqe consume event carries "
  10250. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10251. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10252. phba->sli4_hba.els_wq->queue_id);
  10253. }
  10254. /**
  10255. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10256. * @phba: Pointer to HBA context object.
  10257. * @cq: Pointer to a WQ completion queue.
  10258. * @wcqe: Pointer to work-queue completion queue entry.
  10259. *
  10260. * This routine handles an XRI abort event.
  10261. *
  10262. * Return: true if work posted to worker thread, otherwise false.
  10263. **/
  10264. static bool
  10265. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10266. struct lpfc_queue *cq,
  10267. struct sli4_wcqe_xri_aborted *wcqe)
  10268. {
  10269. bool workposted = false;
  10270. struct lpfc_cq_event *cq_event;
  10271. unsigned long iflags;
  10272. /* Allocate a new internal CQ_EVENT entry */
  10273. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10274. if (!cq_event) {
  10275. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10276. "0602 Failed to allocate CQ_EVENT entry\n");
  10277. return false;
  10278. }
  10279. /* Move the CQE into the proper xri abort event list */
  10280. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10281. switch (cq->subtype) {
  10282. case LPFC_FCP:
  10283. spin_lock_irqsave(&phba->hbalock, iflags);
  10284. list_add_tail(&cq_event->list,
  10285. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10286. /* Set the fcp xri abort event flag */
  10287. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10288. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10289. workposted = true;
  10290. break;
  10291. case LPFC_ELS:
  10292. spin_lock_irqsave(&phba->hbalock, iflags);
  10293. list_add_tail(&cq_event->list,
  10294. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10295. /* Set the els xri abort event flag */
  10296. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10297. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10298. workposted = true;
  10299. break;
  10300. default:
  10301. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10302. "0603 Invalid work queue CQE subtype (x%x)\n",
  10303. cq->subtype);
  10304. workposted = false;
  10305. break;
  10306. }
  10307. return workposted;
  10308. }
  10309. /**
  10310. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10311. * @phba: Pointer to HBA context object.
  10312. * @rcqe: Pointer to receive-queue completion queue entry.
  10313. *
  10314. * This routine process a receive-queue completion queue entry.
  10315. *
  10316. * Return: true if work posted to worker thread, otherwise false.
  10317. **/
  10318. static bool
  10319. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10320. {
  10321. bool workposted = false;
  10322. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10323. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10324. struct hbq_dmabuf *dma_buf;
  10325. uint32_t status, rq_id;
  10326. unsigned long iflags;
  10327. /* sanity check on queue memory */
  10328. if (unlikely(!hrq) || unlikely(!drq))
  10329. return workposted;
  10330. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10331. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10332. else
  10333. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10334. if (rq_id != hrq->queue_id)
  10335. goto out;
  10336. status = bf_get(lpfc_rcqe_status, rcqe);
  10337. switch (status) {
  10338. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10339. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10340. "2537 Receive Frame Truncated!!\n");
  10341. hrq->RQ_buf_trunc++;
  10342. case FC_STATUS_RQ_SUCCESS:
  10343. lpfc_sli4_rq_release(hrq, drq);
  10344. spin_lock_irqsave(&phba->hbalock, iflags);
  10345. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10346. if (!dma_buf) {
  10347. hrq->RQ_no_buf_found++;
  10348. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10349. goto out;
  10350. }
  10351. hrq->RQ_rcv_buf++;
  10352. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10353. /* save off the frame for the word thread to process */
  10354. list_add_tail(&dma_buf->cq_event.list,
  10355. &phba->sli4_hba.sp_queue_event);
  10356. /* Frame received */
  10357. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10358. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10359. workposted = true;
  10360. break;
  10361. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10362. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10363. hrq->RQ_no_posted_buf++;
  10364. /* Post more buffers if possible */
  10365. spin_lock_irqsave(&phba->hbalock, iflags);
  10366. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10367. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10368. workposted = true;
  10369. break;
  10370. }
  10371. out:
  10372. return workposted;
  10373. }
  10374. /**
  10375. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10376. * @phba: Pointer to HBA context object.
  10377. * @cq: Pointer to the completion queue.
  10378. * @wcqe: Pointer to a completion queue entry.
  10379. *
  10380. * This routine process a slow-path work-queue or receive queue completion queue
  10381. * entry.
  10382. *
  10383. * Return: true if work posted to worker thread, otherwise false.
  10384. **/
  10385. static bool
  10386. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10387. struct lpfc_cqe *cqe)
  10388. {
  10389. struct lpfc_cqe cqevt;
  10390. bool workposted = false;
  10391. /* Copy the work queue CQE and convert endian order if needed */
  10392. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10393. /* Check and process for different type of WCQE and dispatch */
  10394. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10395. case CQE_CODE_COMPL_WQE:
  10396. /* Process the WQ/RQ complete event */
  10397. phba->last_completion_time = jiffies;
  10398. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10399. (struct lpfc_wcqe_complete *)&cqevt);
  10400. break;
  10401. case CQE_CODE_RELEASE_WQE:
  10402. /* Process the WQ release event */
  10403. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10404. (struct lpfc_wcqe_release *)&cqevt);
  10405. break;
  10406. case CQE_CODE_XRI_ABORTED:
  10407. /* Process the WQ XRI abort event */
  10408. phba->last_completion_time = jiffies;
  10409. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10410. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10411. break;
  10412. case CQE_CODE_RECEIVE:
  10413. case CQE_CODE_RECEIVE_V1:
  10414. /* Process the RQ event */
  10415. phba->last_completion_time = jiffies;
  10416. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10417. (struct lpfc_rcqe *)&cqevt);
  10418. break;
  10419. default:
  10420. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10421. "0388 Not a valid WCQE code: x%x\n",
  10422. bf_get(lpfc_cqe_code, &cqevt));
  10423. break;
  10424. }
  10425. return workposted;
  10426. }
  10427. /**
  10428. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10429. * @phba: Pointer to HBA context object.
  10430. * @eqe: Pointer to fast-path event queue entry.
  10431. *
  10432. * This routine process a event queue entry from the slow-path event queue.
  10433. * It will check the MajorCode and MinorCode to determine this is for a
  10434. * completion event on a completion queue, if not, an error shall be logged
  10435. * and just return. Otherwise, it will get to the corresponding completion
  10436. * queue and process all the entries on that completion queue, rearm the
  10437. * completion queue, and then return.
  10438. *
  10439. **/
  10440. static void
  10441. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10442. {
  10443. struct lpfc_queue *cq = NULL, *childq, *speq;
  10444. struct lpfc_cqe *cqe;
  10445. bool workposted = false;
  10446. int ecount = 0;
  10447. uint16_t cqid;
  10448. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10449. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10450. "0359 Not a valid slow-path completion "
  10451. "event: majorcode=x%x, minorcode=x%x\n",
  10452. bf_get_le32(lpfc_eqe_major_code, eqe),
  10453. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10454. return;
  10455. }
  10456. /* Get the reference to the corresponding CQ */
  10457. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10458. /* Search for completion queue pointer matching this cqid */
  10459. speq = phba->sli4_hba.sp_eq;
  10460. /* sanity check on queue memory */
  10461. if (unlikely(!speq))
  10462. return;
  10463. list_for_each_entry(childq, &speq->child_list, list) {
  10464. if (childq->queue_id == cqid) {
  10465. cq = childq;
  10466. break;
  10467. }
  10468. }
  10469. if (unlikely(!cq)) {
  10470. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10471. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10472. "0365 Slow-path CQ identifier "
  10473. "(%d) does not exist\n", cqid);
  10474. return;
  10475. }
  10476. /* Process all the entries to the CQ */
  10477. switch (cq->type) {
  10478. case LPFC_MCQ:
  10479. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10480. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10481. if (!(++ecount % cq->entry_repost))
  10482. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10483. cq->CQ_mbox++;
  10484. }
  10485. break;
  10486. case LPFC_WCQ:
  10487. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10488. if (cq->subtype == LPFC_FCP)
  10489. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10490. cqe);
  10491. else
  10492. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10493. cqe);
  10494. if (!(++ecount % cq->entry_repost))
  10495. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10496. }
  10497. /* Track the max number of CQEs processed in 1 EQ */
  10498. if (ecount > cq->CQ_max_cqe)
  10499. cq->CQ_max_cqe = ecount;
  10500. break;
  10501. default:
  10502. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10503. "0370 Invalid completion queue type (%d)\n",
  10504. cq->type);
  10505. return;
  10506. }
  10507. /* Catch the no cq entry condition, log an error */
  10508. if (unlikely(ecount == 0))
  10509. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10510. "0371 No entry from the CQ: identifier "
  10511. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10512. /* In any case, flash and re-arm the RCQ */
  10513. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10514. /* wake up worker thread if there are works to be done */
  10515. if (workposted)
  10516. lpfc_worker_wake_up(phba);
  10517. }
  10518. /**
  10519. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10520. * @eqe: Pointer to fast-path completion queue entry.
  10521. *
  10522. * This routine process a fast-path work queue completion entry from fast-path
  10523. * event queue for FCP command response completion.
  10524. **/
  10525. static void
  10526. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10527. struct lpfc_wcqe_complete *wcqe)
  10528. {
  10529. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10530. struct lpfc_iocbq *cmdiocbq;
  10531. struct lpfc_iocbq irspiocbq;
  10532. unsigned long iflags;
  10533. /* Check for response status */
  10534. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10535. /* If resource errors reported from HBA, reduce queue
  10536. * depth of the SCSI device.
  10537. */
  10538. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10539. IOSTAT_LOCAL_REJECT) &&
  10540. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10541. phba->lpfc_rampdown_queue_depth(phba);
  10542. }
  10543. /* Log the error status */
  10544. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10545. "0373 FCP complete error: status=x%x, "
  10546. "hw_status=x%x, total_data_specified=%d, "
  10547. "parameter=x%x, word3=x%x\n",
  10548. bf_get(lpfc_wcqe_c_status, wcqe),
  10549. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10550. wcqe->total_data_placed, wcqe->parameter,
  10551. wcqe->word3);
  10552. }
  10553. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10554. spin_lock_irqsave(&pring->ring_lock, iflags);
  10555. pring->stats.iocb_event++;
  10556. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10557. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10558. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10559. if (unlikely(!cmdiocbq)) {
  10560. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10561. "0374 FCP complete with no corresponding "
  10562. "cmdiocb: iotag (%d)\n",
  10563. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10564. return;
  10565. }
  10566. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10567. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10568. "0375 FCP cmdiocb not callback function "
  10569. "iotag: (%d)\n",
  10570. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10571. return;
  10572. }
  10573. /* Fake the irspiocb and copy necessary response information */
  10574. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10575. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10576. spin_lock_irqsave(&phba->hbalock, iflags);
  10577. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10578. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10579. }
  10580. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10581. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10582. }
  10583. /**
  10584. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10585. * @phba: Pointer to HBA context object.
  10586. * @cq: Pointer to completion queue.
  10587. * @wcqe: Pointer to work-queue completion queue entry.
  10588. *
  10589. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10590. * proper WQ release routine to the slow-path WQ.
  10591. **/
  10592. static void
  10593. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10594. struct lpfc_wcqe_release *wcqe)
  10595. {
  10596. struct lpfc_queue *childwq;
  10597. bool wqid_matched = false;
  10598. uint16_t fcp_wqid;
  10599. /* Check for fast-path FCP work queue release */
  10600. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10601. list_for_each_entry(childwq, &cq->child_list, list) {
  10602. if (childwq->queue_id == fcp_wqid) {
  10603. lpfc_sli4_wq_release(childwq,
  10604. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10605. wqid_matched = true;
  10606. break;
  10607. }
  10608. }
  10609. /* Report warning log message if no match found */
  10610. if (wqid_matched != true)
  10611. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10612. "2580 Fast-path wqe consume event carries "
  10613. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10614. }
  10615. /**
  10616. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10617. * @cq: Pointer to the completion queue.
  10618. * @eqe: Pointer to fast-path completion queue entry.
  10619. *
  10620. * This routine process a fast-path work queue completion entry from fast-path
  10621. * event queue for FCP command response completion.
  10622. **/
  10623. static int
  10624. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10625. struct lpfc_cqe *cqe)
  10626. {
  10627. struct lpfc_wcqe_release wcqe;
  10628. bool workposted = false;
  10629. /* Copy the work queue CQE and convert endian order if needed */
  10630. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10631. /* Check and process for different type of WCQE and dispatch */
  10632. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10633. case CQE_CODE_COMPL_WQE:
  10634. cq->CQ_wq++;
  10635. /* Process the WQ complete event */
  10636. phba->last_completion_time = jiffies;
  10637. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10638. (struct lpfc_wcqe_complete *)&wcqe);
  10639. break;
  10640. case CQE_CODE_RELEASE_WQE:
  10641. cq->CQ_release_wqe++;
  10642. /* Process the WQ release event */
  10643. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10644. (struct lpfc_wcqe_release *)&wcqe);
  10645. break;
  10646. case CQE_CODE_XRI_ABORTED:
  10647. cq->CQ_xri_aborted++;
  10648. /* Process the WQ XRI abort event */
  10649. phba->last_completion_time = jiffies;
  10650. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10651. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10652. break;
  10653. default:
  10654. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10655. "0144 Not a valid WCQE code: x%x\n",
  10656. bf_get(lpfc_wcqe_c_code, &wcqe));
  10657. break;
  10658. }
  10659. return workposted;
  10660. }
  10661. /**
  10662. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10663. * @phba: Pointer to HBA context object.
  10664. * @eqe: Pointer to fast-path event queue entry.
  10665. *
  10666. * This routine process a event queue entry from the fast-path event queue.
  10667. * It will check the MajorCode and MinorCode to determine this is for a
  10668. * completion event on a completion queue, if not, an error shall be logged
  10669. * and just return. Otherwise, it will get to the corresponding completion
  10670. * queue and process all the entries on the completion queue, rearm the
  10671. * completion queue, and then return.
  10672. **/
  10673. static void
  10674. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10675. uint32_t fcp_cqidx)
  10676. {
  10677. struct lpfc_queue *cq;
  10678. struct lpfc_cqe *cqe;
  10679. bool workposted = false;
  10680. uint16_t cqid;
  10681. int ecount = 0;
  10682. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10683. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10684. "0366 Not a valid fast-path completion "
  10685. "event: majorcode=x%x, minorcode=x%x\n",
  10686. bf_get_le32(lpfc_eqe_major_code, eqe),
  10687. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10688. return;
  10689. }
  10690. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10691. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10692. "3146 Fast-path completion queues "
  10693. "does not exist\n");
  10694. return;
  10695. }
  10696. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10697. if (unlikely(!cq)) {
  10698. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10699. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10700. "0367 Fast-path completion queue "
  10701. "(%d) does not exist\n", fcp_cqidx);
  10702. return;
  10703. }
  10704. /* Get the reference to the corresponding CQ */
  10705. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10706. if (unlikely(cqid != cq->queue_id)) {
  10707. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10708. "0368 Miss-matched fast-path completion "
  10709. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10710. cqid, cq->queue_id);
  10711. return;
  10712. }
  10713. /* Process all the entries to the CQ */
  10714. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10715. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10716. if (!(++ecount % cq->entry_repost))
  10717. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10718. }
  10719. /* Track the max number of CQEs processed in 1 EQ */
  10720. if (ecount > cq->CQ_max_cqe)
  10721. cq->CQ_max_cqe = ecount;
  10722. /* Catch the no cq entry condition */
  10723. if (unlikely(ecount == 0))
  10724. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10725. "0369 No entry from fast-path completion "
  10726. "queue fcpcqid=%d\n", cq->queue_id);
  10727. /* In any case, flash and re-arm the CQ */
  10728. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10729. /* wake up worker thread if there are works to be done */
  10730. if (workposted)
  10731. lpfc_worker_wake_up(phba);
  10732. }
  10733. static void
  10734. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10735. {
  10736. struct lpfc_eqe *eqe;
  10737. /* walk all the EQ entries and drop on the floor */
  10738. while ((eqe = lpfc_sli4_eq_get(eq)))
  10739. ;
  10740. /* Clear and re-arm the EQ */
  10741. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10742. }
  10743. /**
  10744. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10745. * @irq: Interrupt number.
  10746. * @dev_id: The device context pointer.
  10747. *
  10748. * This function is directly called from the PCI layer as an interrupt
  10749. * service routine when device with SLI-4 interface spec is enabled with
  10750. * MSI-X multi-message interrupt mode and there are slow-path events in
  10751. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10752. * interrupt mode, this function is called as part of the device-level
  10753. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10754. * undergoing initialization, the interrupt handler will not process the
  10755. * interrupt. The link attention and ELS ring attention events are handled
  10756. * by the worker thread. The interrupt handler signals the worker thread
  10757. * and returns for these events. This function is called without any lock
  10758. * held. It gets the hbalock to access and update SLI data structures.
  10759. *
  10760. * This function returns IRQ_HANDLED when interrupt is handled else it
  10761. * returns IRQ_NONE.
  10762. **/
  10763. irqreturn_t
  10764. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10765. {
  10766. struct lpfc_hba *phba;
  10767. struct lpfc_queue *speq;
  10768. struct lpfc_eqe *eqe;
  10769. unsigned long iflag;
  10770. int ecount = 0;
  10771. /*
  10772. * Get the driver's phba structure from the dev_id
  10773. */
  10774. phba = (struct lpfc_hba *)dev_id;
  10775. if (unlikely(!phba))
  10776. return IRQ_NONE;
  10777. /* Get to the EQ struct associated with this vector */
  10778. speq = phba->sli4_hba.sp_eq;
  10779. if (unlikely(!speq))
  10780. return IRQ_NONE;
  10781. /* Check device state for handling interrupt */
  10782. if (unlikely(lpfc_intr_state_check(phba))) {
  10783. speq->EQ_badstate++;
  10784. /* Check again for link_state with lock held */
  10785. spin_lock_irqsave(&phba->hbalock, iflag);
  10786. if (phba->link_state < LPFC_LINK_DOWN)
  10787. /* Flush, clear interrupt, and rearm the EQ */
  10788. lpfc_sli4_eq_flush(phba, speq);
  10789. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10790. return IRQ_NONE;
  10791. }
  10792. /*
  10793. * Process all the event on FCP slow-path EQ
  10794. */
  10795. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10796. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10797. if (!(++ecount % speq->entry_repost))
  10798. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10799. speq->EQ_processed++;
  10800. }
  10801. /* Track the max number of EQEs processed in 1 intr */
  10802. if (ecount > speq->EQ_max_eqe)
  10803. speq->EQ_max_eqe = ecount;
  10804. /* Always clear and re-arm the slow-path EQ */
  10805. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10806. /* Catch the no cq entry condition */
  10807. if (unlikely(ecount == 0)) {
  10808. speq->EQ_no_entry++;
  10809. if (phba->intr_type == MSIX)
  10810. /* MSI-X treated interrupt served as no EQ share INT */
  10811. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10812. "0357 MSI-X interrupt with no EQE\n");
  10813. else
  10814. /* Non MSI-X treated on interrupt as EQ share INT */
  10815. return IRQ_NONE;
  10816. }
  10817. return IRQ_HANDLED;
  10818. } /* lpfc_sli4_sp_intr_handler */
  10819. /**
  10820. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10821. * @irq: Interrupt number.
  10822. * @dev_id: The device context pointer.
  10823. *
  10824. * This function is directly called from the PCI layer as an interrupt
  10825. * service routine when device with SLI-4 interface spec is enabled with
  10826. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10827. * ring event in the HBA. However, when the device is enabled with either
  10828. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10829. * device-level interrupt handler. When the PCI slot is in error recovery
  10830. * or the HBA is undergoing initialization, the interrupt handler will not
  10831. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10832. * the intrrupt context. This function is called without any lock held.
  10833. * It gets the hbalock to access and update SLI data structures. Note that,
  10834. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10835. * equal to that of FCP CQ index.
  10836. *
  10837. * This function returns IRQ_HANDLED when interrupt is handled else it
  10838. * returns IRQ_NONE.
  10839. **/
  10840. irqreturn_t
  10841. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10842. {
  10843. struct lpfc_hba *phba;
  10844. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10845. struct lpfc_queue *fpeq;
  10846. struct lpfc_eqe *eqe;
  10847. unsigned long iflag;
  10848. int ecount = 0;
  10849. uint32_t fcp_eqidx;
  10850. /* Get the driver's phba structure from the dev_id */
  10851. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10852. phba = fcp_eq_hdl->phba;
  10853. fcp_eqidx = fcp_eq_hdl->idx;
  10854. if (unlikely(!phba))
  10855. return IRQ_NONE;
  10856. if (unlikely(!phba->sli4_hba.fp_eq))
  10857. return IRQ_NONE;
  10858. /* Get to the EQ struct associated with this vector */
  10859. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10860. if (unlikely(!fpeq))
  10861. return IRQ_NONE;
  10862. /* Check device state for handling interrupt */
  10863. if (unlikely(lpfc_intr_state_check(phba))) {
  10864. fpeq->EQ_badstate++;
  10865. /* Check again for link_state with lock held */
  10866. spin_lock_irqsave(&phba->hbalock, iflag);
  10867. if (phba->link_state < LPFC_LINK_DOWN)
  10868. /* Flush, clear interrupt, and rearm the EQ */
  10869. lpfc_sli4_eq_flush(phba, fpeq);
  10870. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10871. return IRQ_NONE;
  10872. }
  10873. /*
  10874. * Process all the event on FCP fast-path EQ
  10875. */
  10876. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10877. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10878. if (!(++ecount % fpeq->entry_repost))
  10879. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10880. fpeq->EQ_processed++;
  10881. }
  10882. /* Track the max number of EQEs processed in 1 intr */
  10883. if (ecount > fpeq->EQ_max_eqe)
  10884. fpeq->EQ_max_eqe = ecount;
  10885. /* Always clear and re-arm the fast-path EQ */
  10886. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10887. if (unlikely(ecount == 0)) {
  10888. fpeq->EQ_no_entry++;
  10889. if (phba->intr_type == MSIX)
  10890. /* MSI-X treated interrupt served as no EQ share INT */
  10891. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10892. "0358 MSI-X interrupt with no EQE\n");
  10893. else
  10894. /* Non MSI-X treated on interrupt as EQ share INT */
  10895. return IRQ_NONE;
  10896. }
  10897. return IRQ_HANDLED;
  10898. } /* lpfc_sli4_fp_intr_handler */
  10899. /**
  10900. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10901. * @irq: Interrupt number.
  10902. * @dev_id: The device context pointer.
  10903. *
  10904. * This function is the device-level interrupt handler to device with SLI-4
  10905. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10906. * interrupt mode is enabled and there is an event in the HBA which requires
  10907. * driver attention. This function invokes the slow-path interrupt attention
  10908. * handling function and fast-path interrupt attention handling function in
  10909. * turn to process the relevant HBA attention events. This function is called
  10910. * without any lock held. It gets the hbalock to access and update SLI data
  10911. * structures.
  10912. *
  10913. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10914. * returns IRQ_NONE.
  10915. **/
  10916. irqreturn_t
  10917. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10918. {
  10919. struct lpfc_hba *phba;
  10920. irqreturn_t sp_irq_rc, fp_irq_rc;
  10921. bool fp_handled = false;
  10922. uint32_t fcp_eqidx;
  10923. /* Get the driver's phba structure from the dev_id */
  10924. phba = (struct lpfc_hba *)dev_id;
  10925. if (unlikely(!phba))
  10926. return IRQ_NONE;
  10927. /*
  10928. * Invokes slow-path host attention interrupt handling as appropriate.
  10929. */
  10930. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10931. /*
  10932. * Invoke fast-path host attention interrupt handling as appropriate.
  10933. */
  10934. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10935. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10936. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10937. if (fp_irq_rc == IRQ_HANDLED)
  10938. fp_handled |= true;
  10939. }
  10940. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10941. } /* lpfc_sli4_intr_handler */
  10942. /**
  10943. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10944. * @queue: The queue structure to free.
  10945. *
  10946. * This function frees a queue structure and the DMAable memory used for
  10947. * the host resident queue. This function must be called after destroying the
  10948. * queue on the HBA.
  10949. **/
  10950. void
  10951. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10952. {
  10953. struct lpfc_dmabuf *dmabuf;
  10954. if (!queue)
  10955. return;
  10956. while (!list_empty(&queue->page_list)) {
  10957. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10958. list);
  10959. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10960. dmabuf->virt, dmabuf->phys);
  10961. kfree(dmabuf);
  10962. }
  10963. kfree(queue);
  10964. return;
  10965. }
  10966. /**
  10967. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10968. * @phba: The HBA that this queue is being created on.
  10969. * @entry_size: The size of each queue entry for this queue.
  10970. * @entry count: The number of entries that this queue will handle.
  10971. *
  10972. * This function allocates a queue structure and the DMAable memory used for
  10973. * the host resident queue. This function must be called before creating the
  10974. * queue on the HBA.
  10975. **/
  10976. struct lpfc_queue *
  10977. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10978. uint32_t entry_count)
  10979. {
  10980. struct lpfc_queue *queue;
  10981. struct lpfc_dmabuf *dmabuf;
  10982. int x, total_qe_count;
  10983. void *dma_pointer;
  10984. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10985. if (!phba->sli4_hba.pc_sli4_params.supported)
  10986. hw_page_size = SLI4_PAGE_SIZE;
  10987. queue = kzalloc(sizeof(struct lpfc_queue) +
  10988. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10989. if (!queue)
  10990. return NULL;
  10991. queue->page_count = (ALIGN(entry_size * entry_count,
  10992. hw_page_size))/hw_page_size;
  10993. INIT_LIST_HEAD(&queue->list);
  10994. INIT_LIST_HEAD(&queue->page_list);
  10995. INIT_LIST_HEAD(&queue->child_list);
  10996. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10997. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10998. if (!dmabuf)
  10999. goto out_fail;
  11000. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  11001. hw_page_size, &dmabuf->phys,
  11002. GFP_KERNEL);
  11003. if (!dmabuf->virt) {
  11004. kfree(dmabuf);
  11005. goto out_fail;
  11006. }
  11007. memset(dmabuf->virt, 0, hw_page_size);
  11008. dmabuf->buffer_tag = x;
  11009. list_add_tail(&dmabuf->list, &queue->page_list);
  11010. /* initialize queue's entry array */
  11011. dma_pointer = dmabuf->virt;
  11012. for (; total_qe_count < entry_count &&
  11013. dma_pointer < (hw_page_size + dmabuf->virt);
  11014. total_qe_count++, dma_pointer += entry_size) {
  11015. queue->qe[total_qe_count].address = dma_pointer;
  11016. }
  11017. }
  11018. queue->entry_size = entry_size;
  11019. queue->entry_count = entry_count;
  11020. /*
  11021. * entry_repost is calculated based on the number of entries in the
  11022. * queue. This works out except for RQs. If buffers are NOT initially
  11023. * posted for every RQE, entry_repost should be adjusted accordingly.
  11024. */
  11025. queue->entry_repost = (entry_count >> 3);
  11026. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11027. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11028. queue->phba = phba;
  11029. return queue;
  11030. out_fail:
  11031. lpfc_sli4_queue_free(queue);
  11032. return NULL;
  11033. }
  11034. /**
  11035. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11036. * @phba: HBA structure that indicates port to create a queue on.
  11037. * @startq: The starting FCP EQ to modify
  11038. *
  11039. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11040. *
  11041. * The @phba struct is used to send mailbox command to HBA. The @startq
  11042. * is used to get the starting FCP EQ to change.
  11043. * This function is asynchronous and will wait for the mailbox
  11044. * command to finish before continuing.
  11045. *
  11046. * On success this function will return a zero. If unable to allocate enough
  11047. * memory this function will return -ENOMEM. If the queue create mailbox command
  11048. * fails this function will return -ENXIO.
  11049. **/
  11050. uint32_t
  11051. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  11052. {
  11053. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11054. LPFC_MBOXQ_t *mbox;
  11055. struct lpfc_queue *eq;
  11056. int cnt, rc, length, status = 0;
  11057. uint32_t shdr_status, shdr_add_status;
  11058. int fcp_eqidx;
  11059. union lpfc_sli4_cfg_shdr *shdr;
  11060. uint16_t dmult;
  11061. if (startq >= phba->cfg_fcp_eq_count)
  11062. return 0;
  11063. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11064. if (!mbox)
  11065. return -ENOMEM;
  11066. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11067. sizeof(struct lpfc_sli4_cfg_mhdr));
  11068. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11069. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11070. length, LPFC_SLI4_MBX_EMBED);
  11071. eq_delay = &mbox->u.mqe.un.eq_delay;
  11072. /* Calculate delay multiper from maximum interrupt per second */
  11073. dmult = LPFC_DMULT_CONST/phba->cfg_fcp_imax - 1;
  11074. cnt = 0;
  11075. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_eq_count;
  11076. fcp_eqidx++) {
  11077. eq = phba->sli4_hba.fp_eq[fcp_eqidx];
  11078. if (!eq)
  11079. continue;
  11080. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11081. eq_delay->u.request.eq[cnt].phase = 0;
  11082. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11083. cnt++;
  11084. if (cnt >= LPFC_MAX_EQ_DELAY)
  11085. break;
  11086. }
  11087. eq_delay->u.request.num_eq = cnt;
  11088. mbox->vport = phba->pport;
  11089. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11090. mbox->context1 = NULL;
  11091. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11092. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11093. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11094. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11095. if (shdr_status || shdr_add_status || rc) {
  11096. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11097. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11098. "status x%x add_status x%x, mbx status x%x\n",
  11099. shdr_status, shdr_add_status, rc);
  11100. status = -ENXIO;
  11101. }
  11102. mempool_free(mbox, phba->mbox_mem_pool);
  11103. return status;
  11104. }
  11105. /**
  11106. * lpfc_eq_create - Create an Event Queue on the HBA
  11107. * @phba: HBA structure that indicates port to create a queue on.
  11108. * @eq: The queue structure to use to create the event queue.
  11109. * @imax: The maximum interrupt per second limit.
  11110. *
  11111. * This function creates an event queue, as detailed in @eq, on a port,
  11112. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11113. *
  11114. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11115. * is used to get the entry count and entry size that are necessary to
  11116. * determine the number of pages to allocate and use for this queue. This
  11117. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11118. * event queue. This function is asynchronous and will wait for the mailbox
  11119. * command to finish before continuing.
  11120. *
  11121. * On success this function will return a zero. If unable to allocate enough
  11122. * memory this function will return -ENOMEM. If the queue create mailbox command
  11123. * fails this function will return -ENXIO.
  11124. **/
  11125. uint32_t
  11126. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  11127. {
  11128. struct lpfc_mbx_eq_create *eq_create;
  11129. LPFC_MBOXQ_t *mbox;
  11130. int rc, length, status = 0;
  11131. struct lpfc_dmabuf *dmabuf;
  11132. uint32_t shdr_status, shdr_add_status;
  11133. union lpfc_sli4_cfg_shdr *shdr;
  11134. uint16_t dmult;
  11135. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11136. /* sanity check on queue memory */
  11137. if (!eq)
  11138. return -ENODEV;
  11139. if (!phba->sli4_hba.pc_sli4_params.supported)
  11140. hw_page_size = SLI4_PAGE_SIZE;
  11141. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11142. if (!mbox)
  11143. return -ENOMEM;
  11144. length = (sizeof(struct lpfc_mbx_eq_create) -
  11145. sizeof(struct lpfc_sli4_cfg_mhdr));
  11146. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11147. LPFC_MBOX_OPCODE_EQ_CREATE,
  11148. length, LPFC_SLI4_MBX_EMBED);
  11149. eq_create = &mbox->u.mqe.un.eq_create;
  11150. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11151. eq->page_count);
  11152. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11153. LPFC_EQE_SIZE);
  11154. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11155. /* Calculate delay multiper from maximum interrupt per second */
  11156. dmult = LPFC_DMULT_CONST/imax - 1;
  11157. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11158. dmult);
  11159. switch (eq->entry_count) {
  11160. default:
  11161. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11162. "0360 Unsupported EQ count. (%d)\n",
  11163. eq->entry_count);
  11164. if (eq->entry_count < 256)
  11165. return -EINVAL;
  11166. /* otherwise default to smallest count (drop through) */
  11167. case 256:
  11168. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11169. LPFC_EQ_CNT_256);
  11170. break;
  11171. case 512:
  11172. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11173. LPFC_EQ_CNT_512);
  11174. break;
  11175. case 1024:
  11176. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11177. LPFC_EQ_CNT_1024);
  11178. break;
  11179. case 2048:
  11180. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11181. LPFC_EQ_CNT_2048);
  11182. break;
  11183. case 4096:
  11184. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11185. LPFC_EQ_CNT_4096);
  11186. break;
  11187. }
  11188. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11189. memset(dmabuf->virt, 0, hw_page_size);
  11190. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11191. putPaddrLow(dmabuf->phys);
  11192. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11193. putPaddrHigh(dmabuf->phys);
  11194. }
  11195. mbox->vport = phba->pport;
  11196. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11197. mbox->context1 = NULL;
  11198. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11199. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11200. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11201. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11202. if (shdr_status || shdr_add_status || rc) {
  11203. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11204. "2500 EQ_CREATE mailbox failed with "
  11205. "status x%x add_status x%x, mbx status x%x\n",
  11206. shdr_status, shdr_add_status, rc);
  11207. status = -ENXIO;
  11208. }
  11209. eq->type = LPFC_EQ;
  11210. eq->subtype = LPFC_NONE;
  11211. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11212. if (eq->queue_id == 0xFFFF)
  11213. status = -ENXIO;
  11214. eq->host_index = 0;
  11215. eq->hba_index = 0;
  11216. mempool_free(mbox, phba->mbox_mem_pool);
  11217. return status;
  11218. }
  11219. /**
  11220. * lpfc_cq_create - Create a Completion Queue on the HBA
  11221. * @phba: HBA structure that indicates port to create a queue on.
  11222. * @cq: The queue structure to use to create the completion queue.
  11223. * @eq: The event queue to bind this completion queue to.
  11224. *
  11225. * This function creates a completion queue, as detailed in @wq, on a port,
  11226. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11227. *
  11228. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11229. * is used to get the entry count and entry size that are necessary to
  11230. * determine the number of pages to allocate and use for this queue. The @eq
  11231. * is used to indicate which event queue to bind this completion queue to. This
  11232. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11233. * completion queue. This function is asynchronous and will wait for the mailbox
  11234. * command to finish before continuing.
  11235. *
  11236. * On success this function will return a zero. If unable to allocate enough
  11237. * memory this function will return -ENOMEM. If the queue create mailbox command
  11238. * fails this function will return -ENXIO.
  11239. **/
  11240. uint32_t
  11241. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11242. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11243. {
  11244. struct lpfc_mbx_cq_create *cq_create;
  11245. struct lpfc_dmabuf *dmabuf;
  11246. LPFC_MBOXQ_t *mbox;
  11247. int rc, length, status = 0;
  11248. uint32_t shdr_status, shdr_add_status;
  11249. union lpfc_sli4_cfg_shdr *shdr;
  11250. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11251. /* sanity check on queue memory */
  11252. if (!cq || !eq)
  11253. return -ENODEV;
  11254. if (!phba->sli4_hba.pc_sli4_params.supported)
  11255. hw_page_size = SLI4_PAGE_SIZE;
  11256. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11257. if (!mbox)
  11258. return -ENOMEM;
  11259. length = (sizeof(struct lpfc_mbx_cq_create) -
  11260. sizeof(struct lpfc_sli4_cfg_mhdr));
  11261. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11262. LPFC_MBOX_OPCODE_CQ_CREATE,
  11263. length, LPFC_SLI4_MBX_EMBED);
  11264. cq_create = &mbox->u.mqe.un.cq_create;
  11265. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11266. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11267. cq->page_count);
  11268. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11269. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11270. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11271. phba->sli4_hba.pc_sli4_params.cqv);
  11272. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11273. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11274. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11275. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11276. eq->queue_id);
  11277. } else {
  11278. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11279. eq->queue_id);
  11280. }
  11281. switch (cq->entry_count) {
  11282. default:
  11283. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11284. "0361 Unsupported CQ count. (%d)\n",
  11285. cq->entry_count);
  11286. if (cq->entry_count < 256) {
  11287. status = -EINVAL;
  11288. goto out;
  11289. }
  11290. /* otherwise default to smallest count (drop through) */
  11291. case 256:
  11292. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11293. LPFC_CQ_CNT_256);
  11294. break;
  11295. case 512:
  11296. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11297. LPFC_CQ_CNT_512);
  11298. break;
  11299. case 1024:
  11300. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11301. LPFC_CQ_CNT_1024);
  11302. break;
  11303. }
  11304. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11305. memset(dmabuf->virt, 0, hw_page_size);
  11306. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11307. putPaddrLow(dmabuf->phys);
  11308. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11309. putPaddrHigh(dmabuf->phys);
  11310. }
  11311. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11312. /* The IOCTL status is embedded in the mailbox subheader. */
  11313. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11314. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11315. if (shdr_status || shdr_add_status || rc) {
  11316. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11317. "2501 CQ_CREATE mailbox failed with "
  11318. "status x%x add_status x%x, mbx status x%x\n",
  11319. shdr_status, shdr_add_status, rc);
  11320. status = -ENXIO;
  11321. goto out;
  11322. }
  11323. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11324. if (cq->queue_id == 0xFFFF) {
  11325. status = -ENXIO;
  11326. goto out;
  11327. }
  11328. /* link the cq onto the parent eq child list */
  11329. list_add_tail(&cq->list, &eq->child_list);
  11330. /* Set up completion queue's type and subtype */
  11331. cq->type = type;
  11332. cq->subtype = subtype;
  11333. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11334. cq->assoc_qid = eq->queue_id;
  11335. cq->host_index = 0;
  11336. cq->hba_index = 0;
  11337. out:
  11338. mempool_free(mbox, phba->mbox_mem_pool);
  11339. return status;
  11340. }
  11341. /**
  11342. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11343. * @phba: HBA structure that indicates port to create a queue on.
  11344. * @mq: The queue structure to use to create the mailbox queue.
  11345. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11346. * @cq: The completion queue to associate with this cq.
  11347. *
  11348. * This function provides failback (fb) functionality when the
  11349. * mq_create_ext fails on older FW generations. It's purpose is identical
  11350. * to mq_create_ext otherwise.
  11351. *
  11352. * This routine cannot fail as all attributes were previously accessed and
  11353. * initialized in mq_create_ext.
  11354. **/
  11355. static void
  11356. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11357. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11358. {
  11359. struct lpfc_mbx_mq_create *mq_create;
  11360. struct lpfc_dmabuf *dmabuf;
  11361. int length;
  11362. length = (sizeof(struct lpfc_mbx_mq_create) -
  11363. sizeof(struct lpfc_sli4_cfg_mhdr));
  11364. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11365. LPFC_MBOX_OPCODE_MQ_CREATE,
  11366. length, LPFC_SLI4_MBX_EMBED);
  11367. mq_create = &mbox->u.mqe.un.mq_create;
  11368. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11369. mq->page_count);
  11370. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11371. cq->queue_id);
  11372. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11373. switch (mq->entry_count) {
  11374. case 16:
  11375. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11376. LPFC_MQ_RING_SIZE_16);
  11377. break;
  11378. case 32:
  11379. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11380. LPFC_MQ_RING_SIZE_32);
  11381. break;
  11382. case 64:
  11383. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11384. LPFC_MQ_RING_SIZE_64);
  11385. break;
  11386. case 128:
  11387. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11388. LPFC_MQ_RING_SIZE_128);
  11389. break;
  11390. }
  11391. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11392. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11393. putPaddrLow(dmabuf->phys);
  11394. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11395. putPaddrHigh(dmabuf->phys);
  11396. }
  11397. }
  11398. /**
  11399. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11400. * @phba: HBA structure that indicates port to create a queue on.
  11401. * @mq: The queue structure to use to create the mailbox queue.
  11402. * @cq: The completion queue to associate with this cq.
  11403. * @subtype: The queue's subtype.
  11404. *
  11405. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11406. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11407. *
  11408. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11409. * is used to get the entry count and entry size that are necessary to
  11410. * determine the number of pages to allocate and use for this queue. This
  11411. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11412. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11413. * command to finish before continuing.
  11414. *
  11415. * On success this function will return a zero. If unable to allocate enough
  11416. * memory this function will return -ENOMEM. If the queue create mailbox command
  11417. * fails this function will return -ENXIO.
  11418. **/
  11419. int32_t
  11420. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11421. struct lpfc_queue *cq, uint32_t subtype)
  11422. {
  11423. struct lpfc_mbx_mq_create *mq_create;
  11424. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11425. struct lpfc_dmabuf *dmabuf;
  11426. LPFC_MBOXQ_t *mbox;
  11427. int rc, length, status = 0;
  11428. uint32_t shdr_status, shdr_add_status;
  11429. union lpfc_sli4_cfg_shdr *shdr;
  11430. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11431. /* sanity check on queue memory */
  11432. if (!mq || !cq)
  11433. return -ENODEV;
  11434. if (!phba->sli4_hba.pc_sli4_params.supported)
  11435. hw_page_size = SLI4_PAGE_SIZE;
  11436. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11437. if (!mbox)
  11438. return -ENOMEM;
  11439. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11440. sizeof(struct lpfc_sli4_cfg_mhdr));
  11441. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11442. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11443. length, LPFC_SLI4_MBX_EMBED);
  11444. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11445. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11446. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11447. &mq_create_ext->u.request, mq->page_count);
  11448. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11449. &mq_create_ext->u.request, 1);
  11450. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11451. &mq_create_ext->u.request, 1);
  11452. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11453. &mq_create_ext->u.request, 1);
  11454. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11455. &mq_create_ext->u.request, 1);
  11456. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11457. &mq_create_ext->u.request, 1);
  11458. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11459. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11460. phba->sli4_hba.pc_sli4_params.mqv);
  11461. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11462. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11463. cq->queue_id);
  11464. else
  11465. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11466. cq->queue_id);
  11467. switch (mq->entry_count) {
  11468. default:
  11469. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11470. "0362 Unsupported MQ count. (%d)\n",
  11471. mq->entry_count);
  11472. if (mq->entry_count < 16) {
  11473. status = -EINVAL;
  11474. goto out;
  11475. }
  11476. /* otherwise default to smallest count (drop through) */
  11477. case 16:
  11478. bf_set(lpfc_mq_context_ring_size,
  11479. &mq_create_ext->u.request.context,
  11480. LPFC_MQ_RING_SIZE_16);
  11481. break;
  11482. case 32:
  11483. bf_set(lpfc_mq_context_ring_size,
  11484. &mq_create_ext->u.request.context,
  11485. LPFC_MQ_RING_SIZE_32);
  11486. break;
  11487. case 64:
  11488. bf_set(lpfc_mq_context_ring_size,
  11489. &mq_create_ext->u.request.context,
  11490. LPFC_MQ_RING_SIZE_64);
  11491. break;
  11492. case 128:
  11493. bf_set(lpfc_mq_context_ring_size,
  11494. &mq_create_ext->u.request.context,
  11495. LPFC_MQ_RING_SIZE_128);
  11496. break;
  11497. }
  11498. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11499. memset(dmabuf->virt, 0, hw_page_size);
  11500. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11501. putPaddrLow(dmabuf->phys);
  11502. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11503. putPaddrHigh(dmabuf->phys);
  11504. }
  11505. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11506. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11507. &mq_create_ext->u.response);
  11508. if (rc != MBX_SUCCESS) {
  11509. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11510. "2795 MQ_CREATE_EXT failed with "
  11511. "status x%x. Failback to MQ_CREATE.\n",
  11512. rc);
  11513. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11514. mq_create = &mbox->u.mqe.un.mq_create;
  11515. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11516. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11517. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11518. &mq_create->u.response);
  11519. }
  11520. /* The IOCTL status is embedded in the mailbox subheader. */
  11521. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11522. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11523. if (shdr_status || shdr_add_status || rc) {
  11524. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11525. "2502 MQ_CREATE mailbox failed with "
  11526. "status x%x add_status x%x, mbx status x%x\n",
  11527. shdr_status, shdr_add_status, rc);
  11528. status = -ENXIO;
  11529. goto out;
  11530. }
  11531. if (mq->queue_id == 0xFFFF) {
  11532. status = -ENXIO;
  11533. goto out;
  11534. }
  11535. mq->type = LPFC_MQ;
  11536. mq->assoc_qid = cq->queue_id;
  11537. mq->subtype = subtype;
  11538. mq->host_index = 0;
  11539. mq->hba_index = 0;
  11540. /* link the mq onto the parent cq child list */
  11541. list_add_tail(&mq->list, &cq->child_list);
  11542. out:
  11543. mempool_free(mbox, phba->mbox_mem_pool);
  11544. return status;
  11545. }
  11546. /**
  11547. * lpfc_wq_create - Create a Work Queue on the HBA
  11548. * @phba: HBA structure that indicates port to create a queue on.
  11549. * @wq: The queue structure to use to create the work queue.
  11550. * @cq: The completion queue to bind this work queue to.
  11551. * @subtype: The subtype of the work queue indicating its functionality.
  11552. *
  11553. * This function creates a work queue, as detailed in @wq, on a port, described
  11554. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11555. *
  11556. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11557. * is used to get the entry count and entry size that are necessary to
  11558. * determine the number of pages to allocate and use for this queue. The @cq
  11559. * is used to indicate which completion queue to bind this work queue to. This
  11560. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11561. * work queue. This function is asynchronous and will wait for the mailbox
  11562. * command to finish before continuing.
  11563. *
  11564. * On success this function will return a zero. If unable to allocate enough
  11565. * memory this function will return -ENOMEM. If the queue create mailbox command
  11566. * fails this function will return -ENXIO.
  11567. **/
  11568. uint32_t
  11569. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11570. struct lpfc_queue *cq, uint32_t subtype)
  11571. {
  11572. struct lpfc_mbx_wq_create *wq_create;
  11573. struct lpfc_dmabuf *dmabuf;
  11574. LPFC_MBOXQ_t *mbox;
  11575. int rc, length, status = 0;
  11576. uint32_t shdr_status, shdr_add_status;
  11577. union lpfc_sli4_cfg_shdr *shdr;
  11578. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11579. struct dma_address *page;
  11580. /* sanity check on queue memory */
  11581. if (!wq || !cq)
  11582. return -ENODEV;
  11583. if (!phba->sli4_hba.pc_sli4_params.supported)
  11584. hw_page_size = SLI4_PAGE_SIZE;
  11585. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11586. if (!mbox)
  11587. return -ENOMEM;
  11588. length = (sizeof(struct lpfc_mbx_wq_create) -
  11589. sizeof(struct lpfc_sli4_cfg_mhdr));
  11590. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11591. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11592. length, LPFC_SLI4_MBX_EMBED);
  11593. wq_create = &mbox->u.mqe.un.wq_create;
  11594. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11595. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11596. wq->page_count);
  11597. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11598. cq->queue_id);
  11599. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11600. phba->sli4_hba.pc_sli4_params.wqv);
  11601. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11602. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11603. wq->entry_count);
  11604. switch (wq->entry_size) {
  11605. default:
  11606. case 64:
  11607. bf_set(lpfc_mbx_wq_create_wqe_size,
  11608. &wq_create->u.request_1,
  11609. LPFC_WQ_WQE_SIZE_64);
  11610. break;
  11611. case 128:
  11612. bf_set(lpfc_mbx_wq_create_wqe_size,
  11613. &wq_create->u.request_1,
  11614. LPFC_WQ_WQE_SIZE_128);
  11615. break;
  11616. }
  11617. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11618. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11619. page = wq_create->u.request_1.page;
  11620. } else {
  11621. page = wq_create->u.request.page;
  11622. }
  11623. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11624. memset(dmabuf->virt, 0, hw_page_size);
  11625. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11626. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11627. }
  11628. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11629. /* The IOCTL status is embedded in the mailbox subheader. */
  11630. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11631. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11632. if (shdr_status || shdr_add_status || rc) {
  11633. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11634. "2503 WQ_CREATE mailbox failed with "
  11635. "status x%x add_status x%x, mbx status x%x\n",
  11636. shdr_status, shdr_add_status, rc);
  11637. status = -ENXIO;
  11638. goto out;
  11639. }
  11640. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11641. if (wq->queue_id == 0xFFFF) {
  11642. status = -ENXIO;
  11643. goto out;
  11644. }
  11645. wq->type = LPFC_WQ;
  11646. wq->assoc_qid = cq->queue_id;
  11647. wq->subtype = subtype;
  11648. wq->host_index = 0;
  11649. wq->hba_index = 0;
  11650. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11651. /* link the wq onto the parent cq child list */
  11652. list_add_tail(&wq->list, &cq->child_list);
  11653. out:
  11654. mempool_free(mbox, phba->mbox_mem_pool);
  11655. return status;
  11656. }
  11657. /**
  11658. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11659. * @phba: HBA structure that indicates port to create a queue on.
  11660. * @rq: The queue structure to use for the receive queue.
  11661. * @qno: The associated HBQ number
  11662. *
  11663. *
  11664. * For SLI4 we need to adjust the RQ repost value based on
  11665. * the number of buffers that are initially posted to the RQ.
  11666. */
  11667. void
  11668. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11669. {
  11670. uint32_t cnt;
  11671. /* sanity check on queue memory */
  11672. if (!rq)
  11673. return;
  11674. cnt = lpfc_hbq_defs[qno]->entry_count;
  11675. /* Recalc repost for RQs based on buffers initially posted */
  11676. cnt = (cnt >> 3);
  11677. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11678. cnt = LPFC_QUEUE_MIN_REPOST;
  11679. rq->entry_repost = cnt;
  11680. }
  11681. /**
  11682. * lpfc_rq_create - Create a Receive Queue on the HBA
  11683. * @phba: HBA structure that indicates port to create a queue on.
  11684. * @hrq: The queue structure to use to create the header receive queue.
  11685. * @drq: The queue structure to use to create the data receive queue.
  11686. * @cq: The completion queue to bind this work queue to.
  11687. *
  11688. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11689. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11690. * to the HBA.
  11691. *
  11692. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11693. * struct is used to get the entry count that is necessary to determine the
  11694. * number of pages to use for this queue. The @cq is used to indicate which
  11695. * completion queue to bind received buffers that are posted to these queues to.
  11696. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11697. * receive queue pair. This function is asynchronous and will wait for the
  11698. * mailbox command to finish before continuing.
  11699. *
  11700. * On success this function will return a zero. If unable to allocate enough
  11701. * memory this function will return -ENOMEM. If the queue create mailbox command
  11702. * fails this function will return -ENXIO.
  11703. **/
  11704. uint32_t
  11705. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11706. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11707. {
  11708. struct lpfc_mbx_rq_create *rq_create;
  11709. struct lpfc_dmabuf *dmabuf;
  11710. LPFC_MBOXQ_t *mbox;
  11711. int rc, length, status = 0;
  11712. uint32_t shdr_status, shdr_add_status;
  11713. union lpfc_sli4_cfg_shdr *shdr;
  11714. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11715. /* sanity check on queue memory */
  11716. if (!hrq || !drq || !cq)
  11717. return -ENODEV;
  11718. if (!phba->sli4_hba.pc_sli4_params.supported)
  11719. hw_page_size = SLI4_PAGE_SIZE;
  11720. if (hrq->entry_count != drq->entry_count)
  11721. return -EINVAL;
  11722. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11723. if (!mbox)
  11724. return -ENOMEM;
  11725. length = (sizeof(struct lpfc_mbx_rq_create) -
  11726. sizeof(struct lpfc_sli4_cfg_mhdr));
  11727. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11728. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11729. length, LPFC_SLI4_MBX_EMBED);
  11730. rq_create = &mbox->u.mqe.un.rq_create;
  11731. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11732. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11733. phba->sli4_hba.pc_sli4_params.rqv);
  11734. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11735. bf_set(lpfc_rq_context_rqe_count_1,
  11736. &rq_create->u.request.context,
  11737. hrq->entry_count);
  11738. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11739. bf_set(lpfc_rq_context_rqe_size,
  11740. &rq_create->u.request.context,
  11741. LPFC_RQE_SIZE_8);
  11742. bf_set(lpfc_rq_context_page_size,
  11743. &rq_create->u.request.context,
  11744. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11745. } else {
  11746. switch (hrq->entry_count) {
  11747. default:
  11748. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11749. "2535 Unsupported RQ count. (%d)\n",
  11750. hrq->entry_count);
  11751. if (hrq->entry_count < 512) {
  11752. status = -EINVAL;
  11753. goto out;
  11754. }
  11755. /* otherwise default to smallest count (drop through) */
  11756. case 512:
  11757. bf_set(lpfc_rq_context_rqe_count,
  11758. &rq_create->u.request.context,
  11759. LPFC_RQ_RING_SIZE_512);
  11760. break;
  11761. case 1024:
  11762. bf_set(lpfc_rq_context_rqe_count,
  11763. &rq_create->u.request.context,
  11764. LPFC_RQ_RING_SIZE_1024);
  11765. break;
  11766. case 2048:
  11767. bf_set(lpfc_rq_context_rqe_count,
  11768. &rq_create->u.request.context,
  11769. LPFC_RQ_RING_SIZE_2048);
  11770. break;
  11771. case 4096:
  11772. bf_set(lpfc_rq_context_rqe_count,
  11773. &rq_create->u.request.context,
  11774. LPFC_RQ_RING_SIZE_4096);
  11775. break;
  11776. }
  11777. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11778. LPFC_HDR_BUF_SIZE);
  11779. }
  11780. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11781. cq->queue_id);
  11782. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11783. hrq->page_count);
  11784. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11785. memset(dmabuf->virt, 0, hw_page_size);
  11786. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11787. putPaddrLow(dmabuf->phys);
  11788. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11789. putPaddrHigh(dmabuf->phys);
  11790. }
  11791. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11792. /* The IOCTL status is embedded in the mailbox subheader. */
  11793. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11794. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11795. if (shdr_status || shdr_add_status || rc) {
  11796. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11797. "2504 RQ_CREATE mailbox failed with "
  11798. "status x%x add_status x%x, mbx status x%x\n",
  11799. shdr_status, shdr_add_status, rc);
  11800. status = -ENXIO;
  11801. goto out;
  11802. }
  11803. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11804. if (hrq->queue_id == 0xFFFF) {
  11805. status = -ENXIO;
  11806. goto out;
  11807. }
  11808. hrq->type = LPFC_HRQ;
  11809. hrq->assoc_qid = cq->queue_id;
  11810. hrq->subtype = subtype;
  11811. hrq->host_index = 0;
  11812. hrq->hba_index = 0;
  11813. /* now create the data queue */
  11814. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11815. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11816. length, LPFC_SLI4_MBX_EMBED);
  11817. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11818. phba->sli4_hba.pc_sli4_params.rqv);
  11819. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11820. bf_set(lpfc_rq_context_rqe_count_1,
  11821. &rq_create->u.request.context, hrq->entry_count);
  11822. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11823. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11824. LPFC_RQE_SIZE_8);
  11825. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11826. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11827. } else {
  11828. switch (drq->entry_count) {
  11829. default:
  11830. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11831. "2536 Unsupported RQ count. (%d)\n",
  11832. drq->entry_count);
  11833. if (drq->entry_count < 512) {
  11834. status = -EINVAL;
  11835. goto out;
  11836. }
  11837. /* otherwise default to smallest count (drop through) */
  11838. case 512:
  11839. bf_set(lpfc_rq_context_rqe_count,
  11840. &rq_create->u.request.context,
  11841. LPFC_RQ_RING_SIZE_512);
  11842. break;
  11843. case 1024:
  11844. bf_set(lpfc_rq_context_rqe_count,
  11845. &rq_create->u.request.context,
  11846. LPFC_RQ_RING_SIZE_1024);
  11847. break;
  11848. case 2048:
  11849. bf_set(lpfc_rq_context_rqe_count,
  11850. &rq_create->u.request.context,
  11851. LPFC_RQ_RING_SIZE_2048);
  11852. break;
  11853. case 4096:
  11854. bf_set(lpfc_rq_context_rqe_count,
  11855. &rq_create->u.request.context,
  11856. LPFC_RQ_RING_SIZE_4096);
  11857. break;
  11858. }
  11859. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11860. LPFC_DATA_BUF_SIZE);
  11861. }
  11862. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11863. cq->queue_id);
  11864. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11865. drq->page_count);
  11866. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11867. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11868. putPaddrLow(dmabuf->phys);
  11869. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11870. putPaddrHigh(dmabuf->phys);
  11871. }
  11872. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11873. /* The IOCTL status is embedded in the mailbox subheader. */
  11874. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11875. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11876. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11877. if (shdr_status || shdr_add_status || rc) {
  11878. status = -ENXIO;
  11879. goto out;
  11880. }
  11881. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11882. if (drq->queue_id == 0xFFFF) {
  11883. status = -ENXIO;
  11884. goto out;
  11885. }
  11886. drq->type = LPFC_DRQ;
  11887. drq->assoc_qid = cq->queue_id;
  11888. drq->subtype = subtype;
  11889. drq->host_index = 0;
  11890. drq->hba_index = 0;
  11891. /* link the header and data RQs onto the parent cq child list */
  11892. list_add_tail(&hrq->list, &cq->child_list);
  11893. list_add_tail(&drq->list, &cq->child_list);
  11894. out:
  11895. mempool_free(mbox, phba->mbox_mem_pool);
  11896. return status;
  11897. }
  11898. /**
  11899. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11900. * @eq: The queue structure associated with the queue to destroy.
  11901. *
  11902. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11903. * command, specific to the type of queue, to the HBA.
  11904. *
  11905. * The @eq struct is used to get the queue ID of the queue to destroy.
  11906. *
  11907. * On success this function will return a zero. If the queue destroy mailbox
  11908. * command fails this function will return -ENXIO.
  11909. **/
  11910. uint32_t
  11911. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11912. {
  11913. LPFC_MBOXQ_t *mbox;
  11914. int rc, length, status = 0;
  11915. uint32_t shdr_status, shdr_add_status;
  11916. union lpfc_sli4_cfg_shdr *shdr;
  11917. /* sanity check on queue memory */
  11918. if (!eq)
  11919. return -ENODEV;
  11920. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11921. if (!mbox)
  11922. return -ENOMEM;
  11923. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11924. sizeof(struct lpfc_sli4_cfg_mhdr));
  11925. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11926. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11927. length, LPFC_SLI4_MBX_EMBED);
  11928. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11929. eq->queue_id);
  11930. mbox->vport = eq->phba->pport;
  11931. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11932. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11933. /* The IOCTL status is embedded in the mailbox subheader. */
  11934. shdr = (union lpfc_sli4_cfg_shdr *)
  11935. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11936. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11937. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11938. if (shdr_status || shdr_add_status || rc) {
  11939. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11940. "2505 EQ_DESTROY mailbox failed with "
  11941. "status x%x add_status x%x, mbx status x%x\n",
  11942. shdr_status, shdr_add_status, rc);
  11943. status = -ENXIO;
  11944. }
  11945. /* Remove eq from any list */
  11946. list_del_init(&eq->list);
  11947. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11948. return status;
  11949. }
  11950. /**
  11951. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11952. * @cq: The queue structure associated with the queue to destroy.
  11953. *
  11954. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11955. * command, specific to the type of queue, to the HBA.
  11956. *
  11957. * The @cq struct is used to get the queue ID of the queue to destroy.
  11958. *
  11959. * On success this function will return a zero. If the queue destroy mailbox
  11960. * command fails this function will return -ENXIO.
  11961. **/
  11962. uint32_t
  11963. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11964. {
  11965. LPFC_MBOXQ_t *mbox;
  11966. int rc, length, status = 0;
  11967. uint32_t shdr_status, shdr_add_status;
  11968. union lpfc_sli4_cfg_shdr *shdr;
  11969. /* sanity check on queue memory */
  11970. if (!cq)
  11971. return -ENODEV;
  11972. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11973. if (!mbox)
  11974. return -ENOMEM;
  11975. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11976. sizeof(struct lpfc_sli4_cfg_mhdr));
  11977. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11978. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11979. length, LPFC_SLI4_MBX_EMBED);
  11980. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11981. cq->queue_id);
  11982. mbox->vport = cq->phba->pport;
  11983. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11984. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11985. /* The IOCTL status is embedded in the mailbox subheader. */
  11986. shdr = (union lpfc_sli4_cfg_shdr *)
  11987. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11988. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11989. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11990. if (shdr_status || shdr_add_status || rc) {
  11991. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11992. "2506 CQ_DESTROY mailbox failed with "
  11993. "status x%x add_status x%x, mbx status x%x\n",
  11994. shdr_status, shdr_add_status, rc);
  11995. status = -ENXIO;
  11996. }
  11997. /* Remove cq from any list */
  11998. list_del_init(&cq->list);
  11999. mempool_free(mbox, cq->phba->mbox_mem_pool);
  12000. return status;
  12001. }
  12002. /**
  12003. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  12004. * @qm: The queue structure associated with the queue to destroy.
  12005. *
  12006. * This function destroys a queue, as detailed in @mq by sending an mailbox
  12007. * command, specific to the type of queue, to the HBA.
  12008. *
  12009. * The @mq struct is used to get the queue ID of the queue to destroy.
  12010. *
  12011. * On success this function will return a zero. If the queue destroy mailbox
  12012. * command fails this function will return -ENXIO.
  12013. **/
  12014. uint32_t
  12015. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  12016. {
  12017. LPFC_MBOXQ_t *mbox;
  12018. int rc, length, status = 0;
  12019. uint32_t shdr_status, shdr_add_status;
  12020. union lpfc_sli4_cfg_shdr *shdr;
  12021. /* sanity check on queue memory */
  12022. if (!mq)
  12023. return -ENODEV;
  12024. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  12025. if (!mbox)
  12026. return -ENOMEM;
  12027. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12028. sizeof(struct lpfc_sli4_cfg_mhdr));
  12029. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12030. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12031. length, LPFC_SLI4_MBX_EMBED);
  12032. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12033. mq->queue_id);
  12034. mbox->vport = mq->phba->pport;
  12035. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12036. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12037. /* The IOCTL status is embedded in the mailbox subheader. */
  12038. shdr = (union lpfc_sli4_cfg_shdr *)
  12039. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12040. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12041. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12042. if (shdr_status || shdr_add_status || rc) {
  12043. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12044. "2507 MQ_DESTROY mailbox failed with "
  12045. "status x%x add_status x%x, mbx status x%x\n",
  12046. shdr_status, shdr_add_status, rc);
  12047. status = -ENXIO;
  12048. }
  12049. /* Remove mq from any list */
  12050. list_del_init(&mq->list);
  12051. mempool_free(mbox, mq->phba->mbox_mem_pool);
  12052. return status;
  12053. }
  12054. /**
  12055. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12056. * @wq: The queue structure associated with the queue to destroy.
  12057. *
  12058. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12059. * command, specific to the type of queue, to the HBA.
  12060. *
  12061. * The @wq struct is used to get the queue ID of the queue to destroy.
  12062. *
  12063. * On success this function will return a zero. If the queue destroy mailbox
  12064. * command fails this function will return -ENXIO.
  12065. **/
  12066. uint32_t
  12067. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12068. {
  12069. LPFC_MBOXQ_t *mbox;
  12070. int rc, length, status = 0;
  12071. uint32_t shdr_status, shdr_add_status;
  12072. union lpfc_sli4_cfg_shdr *shdr;
  12073. /* sanity check on queue memory */
  12074. if (!wq)
  12075. return -ENODEV;
  12076. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12077. if (!mbox)
  12078. return -ENOMEM;
  12079. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12080. sizeof(struct lpfc_sli4_cfg_mhdr));
  12081. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12082. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12083. length, LPFC_SLI4_MBX_EMBED);
  12084. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12085. wq->queue_id);
  12086. mbox->vport = wq->phba->pport;
  12087. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12088. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12089. shdr = (union lpfc_sli4_cfg_shdr *)
  12090. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12091. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12092. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12093. if (shdr_status || shdr_add_status || rc) {
  12094. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12095. "2508 WQ_DESTROY mailbox failed with "
  12096. "status x%x add_status x%x, mbx status x%x\n",
  12097. shdr_status, shdr_add_status, rc);
  12098. status = -ENXIO;
  12099. }
  12100. /* Remove wq from any list */
  12101. list_del_init(&wq->list);
  12102. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12103. return status;
  12104. }
  12105. /**
  12106. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12107. * @rq: The queue structure associated with the queue to destroy.
  12108. *
  12109. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12110. * command, specific to the type of queue, to the HBA.
  12111. *
  12112. * The @rq struct is used to get the queue ID of the queue to destroy.
  12113. *
  12114. * On success this function will return a zero. If the queue destroy mailbox
  12115. * command fails this function will return -ENXIO.
  12116. **/
  12117. uint32_t
  12118. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12119. struct lpfc_queue *drq)
  12120. {
  12121. LPFC_MBOXQ_t *mbox;
  12122. int rc, length, status = 0;
  12123. uint32_t shdr_status, shdr_add_status;
  12124. union lpfc_sli4_cfg_shdr *shdr;
  12125. /* sanity check on queue memory */
  12126. if (!hrq || !drq)
  12127. return -ENODEV;
  12128. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12129. if (!mbox)
  12130. return -ENOMEM;
  12131. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12132. sizeof(struct lpfc_sli4_cfg_mhdr));
  12133. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12134. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12135. length, LPFC_SLI4_MBX_EMBED);
  12136. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12137. hrq->queue_id);
  12138. mbox->vport = hrq->phba->pport;
  12139. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12140. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12141. /* The IOCTL status is embedded in the mailbox subheader. */
  12142. shdr = (union lpfc_sli4_cfg_shdr *)
  12143. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12144. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12145. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12146. if (shdr_status || shdr_add_status || rc) {
  12147. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12148. "2509 RQ_DESTROY mailbox failed with "
  12149. "status x%x add_status x%x, mbx status x%x\n",
  12150. shdr_status, shdr_add_status, rc);
  12151. if (rc != MBX_TIMEOUT)
  12152. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12153. return -ENXIO;
  12154. }
  12155. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12156. drq->queue_id);
  12157. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12158. shdr = (union lpfc_sli4_cfg_shdr *)
  12159. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12160. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12161. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12162. if (shdr_status || shdr_add_status || rc) {
  12163. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12164. "2510 RQ_DESTROY mailbox failed with "
  12165. "status x%x add_status x%x, mbx status x%x\n",
  12166. shdr_status, shdr_add_status, rc);
  12167. status = -ENXIO;
  12168. }
  12169. list_del_init(&hrq->list);
  12170. list_del_init(&drq->list);
  12171. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12172. return status;
  12173. }
  12174. /**
  12175. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12176. * @phba: The virtual port for which this call being executed.
  12177. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12178. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12179. * @xritag: the xritag that ties this io to the SGL pages.
  12180. *
  12181. * This routine will post the sgl pages for the IO that has the xritag
  12182. * that is in the iocbq structure. The xritag is assigned during iocbq
  12183. * creation and persists for as long as the driver is loaded.
  12184. * if the caller has fewer than 256 scatter gather segments to map then
  12185. * pdma_phys_addr1 should be 0.
  12186. * If the caller needs to map more than 256 scatter gather segment then
  12187. * pdma_phys_addr1 should be a valid physical address.
  12188. * physical address for SGLs must be 64 byte aligned.
  12189. * If you are going to map 2 SGL's then the first one must have 256 entries
  12190. * the second sgl can have between 1 and 256 entries.
  12191. *
  12192. * Return codes:
  12193. * 0 - Success
  12194. * -ENXIO, -ENOMEM - Failure
  12195. **/
  12196. int
  12197. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12198. dma_addr_t pdma_phys_addr0,
  12199. dma_addr_t pdma_phys_addr1,
  12200. uint16_t xritag)
  12201. {
  12202. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12203. LPFC_MBOXQ_t *mbox;
  12204. int rc;
  12205. uint32_t shdr_status, shdr_add_status;
  12206. uint32_t mbox_tmo;
  12207. union lpfc_sli4_cfg_shdr *shdr;
  12208. if (xritag == NO_XRI) {
  12209. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12210. "0364 Invalid param:\n");
  12211. return -EINVAL;
  12212. }
  12213. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12214. if (!mbox)
  12215. return -ENOMEM;
  12216. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12217. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12218. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12219. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12220. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12221. &mbox->u.mqe.un.post_sgl_pages;
  12222. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12223. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12224. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12225. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12226. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12227. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12228. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12229. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12230. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12231. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12232. if (!phba->sli4_hba.intr_enable)
  12233. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12234. else {
  12235. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12236. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12237. }
  12238. /* The IOCTL status is embedded in the mailbox subheader. */
  12239. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12240. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12241. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12242. if (rc != MBX_TIMEOUT)
  12243. mempool_free(mbox, phba->mbox_mem_pool);
  12244. if (shdr_status || shdr_add_status || rc) {
  12245. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12246. "2511 POST_SGL mailbox failed with "
  12247. "status x%x add_status x%x, mbx status x%x\n",
  12248. shdr_status, shdr_add_status, rc);
  12249. rc = -ENXIO;
  12250. }
  12251. return 0;
  12252. }
  12253. /**
  12254. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12255. * @phba: pointer to lpfc hba data structure.
  12256. *
  12257. * This routine is invoked to post rpi header templates to the
  12258. * HBA consistent with the SLI-4 interface spec. This routine
  12259. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12260. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12261. *
  12262. * Returns
  12263. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12264. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12265. **/
  12266. uint16_t
  12267. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12268. {
  12269. unsigned long xri;
  12270. /*
  12271. * Fetch the next logical xri. Because this index is logical,
  12272. * the driver starts at 0 each time.
  12273. */
  12274. spin_lock_irq(&phba->hbalock);
  12275. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12276. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12277. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12278. spin_unlock_irq(&phba->hbalock);
  12279. return NO_XRI;
  12280. } else {
  12281. set_bit(xri, phba->sli4_hba.xri_bmask);
  12282. phba->sli4_hba.max_cfg_param.xri_used++;
  12283. }
  12284. spin_unlock_irq(&phba->hbalock);
  12285. return xri;
  12286. }
  12287. /**
  12288. * lpfc_sli4_free_xri - Release an xri for reuse.
  12289. * @phba: pointer to lpfc hba data structure.
  12290. *
  12291. * This routine is invoked to release an xri to the pool of
  12292. * available rpis maintained by the driver.
  12293. **/
  12294. void
  12295. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12296. {
  12297. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12298. phba->sli4_hba.max_cfg_param.xri_used--;
  12299. }
  12300. }
  12301. /**
  12302. * lpfc_sli4_free_xri - Release an xri for reuse.
  12303. * @phba: pointer to lpfc hba data structure.
  12304. *
  12305. * This routine is invoked to release an xri to the pool of
  12306. * available rpis maintained by the driver.
  12307. **/
  12308. void
  12309. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12310. {
  12311. spin_lock_irq(&phba->hbalock);
  12312. __lpfc_sli4_free_xri(phba, xri);
  12313. spin_unlock_irq(&phba->hbalock);
  12314. }
  12315. /**
  12316. * lpfc_sli4_next_xritag - Get an xritag for the io
  12317. * @phba: Pointer to HBA context object.
  12318. *
  12319. * This function gets an xritag for the iocb. If there is no unused xritag
  12320. * it will return 0xffff.
  12321. * The function returns the allocated xritag if successful, else returns zero.
  12322. * Zero is not a valid xritag.
  12323. * The caller is not required to hold any lock.
  12324. **/
  12325. uint16_t
  12326. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12327. {
  12328. uint16_t xri_index;
  12329. xri_index = lpfc_sli4_alloc_xri(phba);
  12330. if (xri_index == NO_XRI)
  12331. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12332. "2004 Failed to allocate XRI.last XRITAG is %d"
  12333. " Max XRI is %d, Used XRI is %d\n",
  12334. xri_index,
  12335. phba->sli4_hba.max_cfg_param.max_xri,
  12336. phba->sli4_hba.max_cfg_param.xri_used);
  12337. return xri_index;
  12338. }
  12339. /**
  12340. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12341. * @phba: pointer to lpfc hba data structure.
  12342. * @post_sgl_list: pointer to els sgl entry list.
  12343. * @count: number of els sgl entries on the list.
  12344. *
  12345. * This routine is invoked to post a block of driver's sgl pages to the
  12346. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12347. * is only called when the driver is loading and after all IO has been
  12348. * stopped.
  12349. **/
  12350. static int
  12351. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12352. struct list_head *post_sgl_list,
  12353. int post_cnt)
  12354. {
  12355. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12356. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12357. struct sgl_page_pairs *sgl_pg_pairs;
  12358. void *viraddr;
  12359. LPFC_MBOXQ_t *mbox;
  12360. uint32_t reqlen, alloclen, pg_pairs;
  12361. uint32_t mbox_tmo;
  12362. uint16_t xritag_start = 0;
  12363. int rc = 0;
  12364. uint32_t shdr_status, shdr_add_status;
  12365. union lpfc_sli4_cfg_shdr *shdr;
  12366. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12367. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12368. if (reqlen > SLI4_PAGE_SIZE) {
  12369. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12370. "2559 Block sgl registration required DMA "
  12371. "size (%d) great than a page\n", reqlen);
  12372. return -ENOMEM;
  12373. }
  12374. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12375. if (!mbox)
  12376. return -ENOMEM;
  12377. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12378. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12379. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12380. LPFC_SLI4_MBX_NEMBED);
  12381. if (alloclen < reqlen) {
  12382. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12383. "0285 Allocated DMA memory size (%d) is "
  12384. "less than the requested DMA memory "
  12385. "size (%d)\n", alloclen, reqlen);
  12386. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12387. return -ENOMEM;
  12388. }
  12389. /* Set up the SGL pages in the non-embedded DMA pages */
  12390. viraddr = mbox->sge_array->addr[0];
  12391. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12392. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12393. pg_pairs = 0;
  12394. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12395. /* Set up the sge entry */
  12396. sgl_pg_pairs->sgl_pg0_addr_lo =
  12397. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12398. sgl_pg_pairs->sgl_pg0_addr_hi =
  12399. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12400. sgl_pg_pairs->sgl_pg1_addr_lo =
  12401. cpu_to_le32(putPaddrLow(0));
  12402. sgl_pg_pairs->sgl_pg1_addr_hi =
  12403. cpu_to_le32(putPaddrHigh(0));
  12404. /* Keep the first xritag on the list */
  12405. if (pg_pairs == 0)
  12406. xritag_start = sglq_entry->sli4_xritag;
  12407. sgl_pg_pairs++;
  12408. pg_pairs++;
  12409. }
  12410. /* Complete initialization and perform endian conversion. */
  12411. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12412. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12413. sgl->word0 = cpu_to_le32(sgl->word0);
  12414. if (!phba->sli4_hba.intr_enable)
  12415. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12416. else {
  12417. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12418. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12419. }
  12420. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12421. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12422. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12423. if (rc != MBX_TIMEOUT)
  12424. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12425. if (shdr_status || shdr_add_status || rc) {
  12426. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12427. "2513 POST_SGL_BLOCK mailbox command failed "
  12428. "status x%x add_status x%x mbx status x%x\n",
  12429. shdr_status, shdr_add_status, rc);
  12430. rc = -ENXIO;
  12431. }
  12432. return rc;
  12433. }
  12434. /**
  12435. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12436. * @phba: pointer to lpfc hba data structure.
  12437. * @sblist: pointer to scsi buffer list.
  12438. * @count: number of scsi buffers on the list.
  12439. *
  12440. * This routine is invoked to post a block of @count scsi sgl pages from a
  12441. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12442. * No Lock is held.
  12443. *
  12444. **/
  12445. int
  12446. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12447. struct list_head *sblist,
  12448. int count)
  12449. {
  12450. struct lpfc_scsi_buf *psb;
  12451. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12452. struct sgl_page_pairs *sgl_pg_pairs;
  12453. void *viraddr;
  12454. LPFC_MBOXQ_t *mbox;
  12455. uint32_t reqlen, alloclen, pg_pairs;
  12456. uint32_t mbox_tmo;
  12457. uint16_t xritag_start = 0;
  12458. int rc = 0;
  12459. uint32_t shdr_status, shdr_add_status;
  12460. dma_addr_t pdma_phys_bpl1;
  12461. union lpfc_sli4_cfg_shdr *shdr;
  12462. /* Calculate the requested length of the dma memory */
  12463. reqlen = count * sizeof(struct sgl_page_pairs) +
  12464. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12465. if (reqlen > SLI4_PAGE_SIZE) {
  12466. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12467. "0217 Block sgl registration required DMA "
  12468. "size (%d) great than a page\n", reqlen);
  12469. return -ENOMEM;
  12470. }
  12471. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12472. if (!mbox) {
  12473. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12474. "0283 Failed to allocate mbox cmd memory\n");
  12475. return -ENOMEM;
  12476. }
  12477. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12478. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12479. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12480. LPFC_SLI4_MBX_NEMBED);
  12481. if (alloclen < reqlen) {
  12482. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12483. "2561 Allocated DMA memory size (%d) is "
  12484. "less than the requested DMA memory "
  12485. "size (%d)\n", alloclen, reqlen);
  12486. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12487. return -ENOMEM;
  12488. }
  12489. /* Get the first SGE entry from the non-embedded DMA memory */
  12490. viraddr = mbox->sge_array->addr[0];
  12491. /* Set up the SGL pages in the non-embedded DMA pages */
  12492. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12493. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12494. pg_pairs = 0;
  12495. list_for_each_entry(psb, sblist, list) {
  12496. /* Set up the sge entry */
  12497. sgl_pg_pairs->sgl_pg0_addr_lo =
  12498. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12499. sgl_pg_pairs->sgl_pg0_addr_hi =
  12500. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12501. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12502. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12503. else
  12504. pdma_phys_bpl1 = 0;
  12505. sgl_pg_pairs->sgl_pg1_addr_lo =
  12506. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12507. sgl_pg_pairs->sgl_pg1_addr_hi =
  12508. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12509. /* Keep the first xritag on the list */
  12510. if (pg_pairs == 0)
  12511. xritag_start = psb->cur_iocbq.sli4_xritag;
  12512. sgl_pg_pairs++;
  12513. pg_pairs++;
  12514. }
  12515. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12516. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12517. /* Perform endian conversion if necessary */
  12518. sgl->word0 = cpu_to_le32(sgl->word0);
  12519. if (!phba->sli4_hba.intr_enable)
  12520. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12521. else {
  12522. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12523. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12524. }
  12525. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12526. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12527. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12528. if (rc != MBX_TIMEOUT)
  12529. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12530. if (shdr_status || shdr_add_status || rc) {
  12531. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12532. "2564 POST_SGL_BLOCK mailbox command failed "
  12533. "status x%x add_status x%x mbx status x%x\n",
  12534. shdr_status, shdr_add_status, rc);
  12535. rc = -ENXIO;
  12536. }
  12537. return rc;
  12538. }
  12539. /**
  12540. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12541. * @phba: pointer to lpfc_hba struct that the frame was received on
  12542. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12543. *
  12544. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12545. * valid type of frame that the LPFC driver will handle. This function will
  12546. * return a zero if the frame is a valid frame or a non zero value when the
  12547. * frame does not pass the check.
  12548. **/
  12549. static int
  12550. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12551. {
  12552. /* make rctl_names static to save stack space */
  12553. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12554. char *type_names[] = FC_TYPE_NAMES_INIT;
  12555. struct fc_vft_header *fc_vft_hdr;
  12556. uint32_t *header = (uint32_t *) fc_hdr;
  12557. switch (fc_hdr->fh_r_ctl) {
  12558. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12559. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12560. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12561. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12562. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12563. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12564. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12565. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12566. case FC_RCTL_ELS_REQ: /* extended link services request */
  12567. case FC_RCTL_ELS_REP: /* extended link services reply */
  12568. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12569. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12570. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12571. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12572. case FC_RCTL_BA_RMC: /* remove connection */
  12573. case FC_RCTL_BA_ACC: /* basic accept */
  12574. case FC_RCTL_BA_RJT: /* basic reject */
  12575. case FC_RCTL_BA_PRMT:
  12576. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12577. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12578. case FC_RCTL_P_RJT: /* port reject */
  12579. case FC_RCTL_F_RJT: /* fabric reject */
  12580. case FC_RCTL_P_BSY: /* port busy */
  12581. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12582. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12583. case FC_RCTL_LCR: /* link credit reset */
  12584. case FC_RCTL_END: /* end */
  12585. break;
  12586. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12587. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12588. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12589. return lpfc_fc_frame_check(phba, fc_hdr);
  12590. default:
  12591. goto drop;
  12592. }
  12593. switch (fc_hdr->fh_type) {
  12594. case FC_TYPE_BLS:
  12595. case FC_TYPE_ELS:
  12596. case FC_TYPE_FCP:
  12597. case FC_TYPE_CT:
  12598. break;
  12599. case FC_TYPE_IP:
  12600. case FC_TYPE_ILS:
  12601. default:
  12602. goto drop;
  12603. }
  12604. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12605. "2538 Received frame rctl:%s type:%s "
  12606. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12607. rctl_names[fc_hdr->fh_r_ctl],
  12608. type_names[fc_hdr->fh_type],
  12609. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12610. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12611. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12612. return 0;
  12613. drop:
  12614. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12615. "2539 Dropped frame rctl:%s type:%s\n",
  12616. rctl_names[fc_hdr->fh_r_ctl],
  12617. type_names[fc_hdr->fh_type]);
  12618. return 1;
  12619. }
  12620. /**
  12621. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12622. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12623. *
  12624. * This function processes the FC header to retrieve the VFI from the VF
  12625. * header, if one exists. This function will return the VFI if one exists
  12626. * or 0 if no VSAN Header exists.
  12627. **/
  12628. static uint32_t
  12629. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12630. {
  12631. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12632. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12633. return 0;
  12634. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12635. }
  12636. /**
  12637. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12638. * @phba: Pointer to the HBA structure to search for the vport on
  12639. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12640. * @fcfi: The FC Fabric ID that the frame came from
  12641. *
  12642. * This function searches the @phba for a vport that matches the content of the
  12643. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12644. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12645. * returns the matching vport pointer or NULL if unable to match frame to a
  12646. * vport.
  12647. **/
  12648. static struct lpfc_vport *
  12649. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12650. uint16_t fcfi)
  12651. {
  12652. struct lpfc_vport **vports;
  12653. struct lpfc_vport *vport = NULL;
  12654. int i;
  12655. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12656. fc_hdr->fh_d_id[1] << 8 |
  12657. fc_hdr->fh_d_id[2]);
  12658. if (did == Fabric_DID)
  12659. return phba->pport;
  12660. if ((phba->pport->fc_flag & FC_PT2PT) &&
  12661. !(phba->link_state == LPFC_HBA_READY))
  12662. return phba->pport;
  12663. vports = lpfc_create_vport_work_array(phba);
  12664. if (vports != NULL)
  12665. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12666. if (phba->fcf.fcfi == fcfi &&
  12667. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12668. vports[i]->fc_myDID == did) {
  12669. vport = vports[i];
  12670. break;
  12671. }
  12672. }
  12673. lpfc_destroy_vport_work_array(phba, vports);
  12674. return vport;
  12675. }
  12676. /**
  12677. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12678. * @vport: The vport to work on.
  12679. *
  12680. * This function updates the receive sequence time stamp for this vport. The
  12681. * receive sequence time stamp indicates the time that the last frame of the
  12682. * the sequence that has been idle for the longest amount of time was received.
  12683. * the driver uses this time stamp to indicate if any received sequences have
  12684. * timed out.
  12685. **/
  12686. void
  12687. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12688. {
  12689. struct lpfc_dmabuf *h_buf;
  12690. struct hbq_dmabuf *dmabuf = NULL;
  12691. /* get the oldest sequence on the rcv list */
  12692. h_buf = list_get_first(&vport->rcv_buffer_list,
  12693. struct lpfc_dmabuf, list);
  12694. if (!h_buf)
  12695. return;
  12696. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12697. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12698. }
  12699. /**
  12700. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12701. * @vport: The vport that the received sequences were sent to.
  12702. *
  12703. * This function cleans up all outstanding received sequences. This is called
  12704. * by the driver when a link event or user action invalidates all the received
  12705. * sequences.
  12706. **/
  12707. void
  12708. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12709. {
  12710. struct lpfc_dmabuf *h_buf, *hnext;
  12711. struct lpfc_dmabuf *d_buf, *dnext;
  12712. struct hbq_dmabuf *dmabuf = NULL;
  12713. /* start with the oldest sequence on the rcv list */
  12714. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12715. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12716. list_del_init(&dmabuf->hbuf.list);
  12717. list_for_each_entry_safe(d_buf, dnext,
  12718. &dmabuf->dbuf.list, list) {
  12719. list_del_init(&d_buf->list);
  12720. lpfc_in_buf_free(vport->phba, d_buf);
  12721. }
  12722. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12723. }
  12724. }
  12725. /**
  12726. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12727. * @vport: The vport that the received sequences were sent to.
  12728. *
  12729. * This function determines whether any received sequences have timed out by
  12730. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12731. * indicates that there is at least one timed out sequence this routine will
  12732. * go through the received sequences one at a time from most inactive to most
  12733. * active to determine which ones need to be cleaned up. Once it has determined
  12734. * that a sequence needs to be cleaned up it will simply free up the resources
  12735. * without sending an abort.
  12736. **/
  12737. void
  12738. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12739. {
  12740. struct lpfc_dmabuf *h_buf, *hnext;
  12741. struct lpfc_dmabuf *d_buf, *dnext;
  12742. struct hbq_dmabuf *dmabuf = NULL;
  12743. unsigned long timeout;
  12744. int abort_count = 0;
  12745. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12746. vport->rcv_buffer_time_stamp);
  12747. if (list_empty(&vport->rcv_buffer_list) ||
  12748. time_before(jiffies, timeout))
  12749. return;
  12750. /* start with the oldest sequence on the rcv list */
  12751. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12752. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12753. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12754. dmabuf->time_stamp);
  12755. if (time_before(jiffies, timeout))
  12756. break;
  12757. abort_count++;
  12758. list_del_init(&dmabuf->hbuf.list);
  12759. list_for_each_entry_safe(d_buf, dnext,
  12760. &dmabuf->dbuf.list, list) {
  12761. list_del_init(&d_buf->list);
  12762. lpfc_in_buf_free(vport->phba, d_buf);
  12763. }
  12764. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12765. }
  12766. if (abort_count)
  12767. lpfc_update_rcv_time_stamp(vport);
  12768. }
  12769. /**
  12770. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12771. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12772. *
  12773. * This function searches through the existing incomplete sequences that have
  12774. * been sent to this @vport. If the frame matches one of the incomplete
  12775. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12776. * make up that sequence. If no sequence is found that matches this frame then
  12777. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12778. * This function returns a pointer to the first dmabuf in the sequence list that
  12779. * the frame was linked to.
  12780. **/
  12781. static struct hbq_dmabuf *
  12782. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12783. {
  12784. struct fc_frame_header *new_hdr;
  12785. struct fc_frame_header *temp_hdr;
  12786. struct lpfc_dmabuf *d_buf;
  12787. struct lpfc_dmabuf *h_buf;
  12788. struct hbq_dmabuf *seq_dmabuf = NULL;
  12789. struct hbq_dmabuf *temp_dmabuf = NULL;
  12790. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12791. dmabuf->time_stamp = jiffies;
  12792. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12793. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12794. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12795. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12796. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12797. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12798. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12799. continue;
  12800. /* found a pending sequence that matches this frame */
  12801. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12802. break;
  12803. }
  12804. if (!seq_dmabuf) {
  12805. /*
  12806. * This indicates first frame received for this sequence.
  12807. * Queue the buffer on the vport's rcv_buffer_list.
  12808. */
  12809. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12810. lpfc_update_rcv_time_stamp(vport);
  12811. return dmabuf;
  12812. }
  12813. temp_hdr = seq_dmabuf->hbuf.virt;
  12814. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12815. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12816. list_del_init(&seq_dmabuf->hbuf.list);
  12817. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12818. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12819. lpfc_update_rcv_time_stamp(vport);
  12820. return dmabuf;
  12821. }
  12822. /* move this sequence to the tail to indicate a young sequence */
  12823. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12824. seq_dmabuf->time_stamp = jiffies;
  12825. lpfc_update_rcv_time_stamp(vport);
  12826. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12827. temp_hdr = dmabuf->hbuf.virt;
  12828. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12829. return seq_dmabuf;
  12830. }
  12831. /* find the correct place in the sequence to insert this frame */
  12832. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12833. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12834. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12835. /*
  12836. * If the frame's sequence count is greater than the frame on
  12837. * the list then insert the frame right after this frame
  12838. */
  12839. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12840. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12841. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12842. return seq_dmabuf;
  12843. }
  12844. }
  12845. return NULL;
  12846. }
  12847. /**
  12848. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12849. * @vport: pointer to a vitural port
  12850. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12851. *
  12852. * This function tries to abort from the partially assembed sequence, described
  12853. * by the information from basic abbort @dmabuf. It checks to see whether such
  12854. * partially assembled sequence held by the driver. If so, it shall free up all
  12855. * the frames from the partially assembled sequence.
  12856. *
  12857. * Return
  12858. * true -- if there is matching partially assembled sequence present and all
  12859. * the frames freed with the sequence;
  12860. * false -- if there is no matching partially assembled sequence present so
  12861. * nothing got aborted in the lower layer driver
  12862. **/
  12863. static bool
  12864. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12865. struct hbq_dmabuf *dmabuf)
  12866. {
  12867. struct fc_frame_header *new_hdr;
  12868. struct fc_frame_header *temp_hdr;
  12869. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12870. struct hbq_dmabuf *seq_dmabuf = NULL;
  12871. /* Use the hdr_buf to find the sequence that matches this frame */
  12872. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12873. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12874. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12875. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12876. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12877. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12878. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12879. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12880. continue;
  12881. /* found a pending sequence that matches this frame */
  12882. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12883. break;
  12884. }
  12885. /* Free up all the frames from the partially assembled sequence */
  12886. if (seq_dmabuf) {
  12887. list_for_each_entry_safe(d_buf, n_buf,
  12888. &seq_dmabuf->dbuf.list, list) {
  12889. list_del_init(&d_buf->list);
  12890. lpfc_in_buf_free(vport->phba, d_buf);
  12891. }
  12892. return true;
  12893. }
  12894. return false;
  12895. }
  12896. /**
  12897. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12898. * @phba: Pointer to HBA context object.
  12899. * @cmd_iocbq: pointer to the command iocbq structure.
  12900. * @rsp_iocbq: pointer to the response iocbq structure.
  12901. *
  12902. * This function handles the sequence abort response iocb command complete
  12903. * event. It properly releases the memory allocated to the sequence abort
  12904. * accept iocb.
  12905. **/
  12906. static void
  12907. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12908. struct lpfc_iocbq *cmd_iocbq,
  12909. struct lpfc_iocbq *rsp_iocbq)
  12910. {
  12911. if (cmd_iocbq)
  12912. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12913. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12914. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12915. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12916. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12917. rsp_iocbq->iocb.ulpStatus,
  12918. rsp_iocbq->iocb.un.ulpWord[4]);
  12919. }
  12920. /**
  12921. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12922. * @phba: Pointer to HBA context object.
  12923. * @xri: xri id in transaction.
  12924. *
  12925. * This function validates the xri maps to the known range of XRIs allocated an
  12926. * used by the driver.
  12927. **/
  12928. uint16_t
  12929. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12930. uint16_t xri)
  12931. {
  12932. int i;
  12933. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12934. if (xri == phba->sli4_hba.xri_ids[i])
  12935. return i;
  12936. }
  12937. return NO_XRI;
  12938. }
  12939. /**
  12940. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12941. * @phba: Pointer to HBA context object.
  12942. * @fc_hdr: pointer to a FC frame header.
  12943. *
  12944. * This function sends a basic response to a previous unsol sequence abort
  12945. * event after aborting the sequence handling.
  12946. **/
  12947. static void
  12948. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12949. struct fc_frame_header *fc_hdr)
  12950. {
  12951. struct lpfc_iocbq *ctiocb = NULL;
  12952. struct lpfc_nodelist *ndlp;
  12953. uint16_t oxid, rxid, xri, lxri;
  12954. uint32_t sid, fctl;
  12955. IOCB_t *icmd;
  12956. int rc;
  12957. if (!lpfc_is_link_up(phba))
  12958. return;
  12959. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12960. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12961. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12962. ndlp = lpfc_findnode_did(phba->pport, sid);
  12963. if (!ndlp) {
  12964. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12965. "1268 Find ndlp returned NULL for oxid:x%x "
  12966. "SID:x%x\n", oxid, sid);
  12967. return;
  12968. }
  12969. /* Allocate buffer for rsp iocb */
  12970. ctiocb = lpfc_sli_get_iocbq(phba);
  12971. if (!ctiocb)
  12972. return;
  12973. /* Extract the F_CTL field from FC_HDR */
  12974. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12975. icmd = &ctiocb->iocb;
  12976. icmd->un.xseq64.bdl.bdeSize = 0;
  12977. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12978. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12979. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12980. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12981. /* Fill in the rest of iocb fields */
  12982. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12983. icmd->ulpBdeCount = 0;
  12984. icmd->ulpLe = 1;
  12985. icmd->ulpClass = CLASS3;
  12986. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12987. ctiocb->context1 = ndlp;
  12988. ctiocb->iocb_cmpl = NULL;
  12989. ctiocb->vport = phba->pport;
  12990. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12991. ctiocb->sli4_lxritag = NO_XRI;
  12992. ctiocb->sli4_xritag = NO_XRI;
  12993. if (fctl & FC_FC_EX_CTX)
  12994. /* Exchange responder sent the abort so we
  12995. * own the oxid.
  12996. */
  12997. xri = oxid;
  12998. else
  12999. xri = rxid;
  13000. lxri = lpfc_sli4_xri_inrange(phba, xri);
  13001. if (lxri != NO_XRI)
  13002. lpfc_set_rrq_active(phba, ndlp, lxri,
  13003. (xri == oxid) ? rxid : oxid, 0);
  13004. /* If the oxid maps to the FCP XRI range or if it is out of range,
  13005. * send a BLS_RJT. The driver no longer has that exchange.
  13006. * Override the IOCB for a BA_RJT.
  13007. */
  13008. if (xri > (phba->sli4_hba.max_cfg_param.max_xri +
  13009. phba->sli4_hba.max_cfg_param.xri_base) ||
  13010. xri > (lpfc_sli4_get_els_iocb_cnt(phba) +
  13011. phba->sli4_hba.max_cfg_param.xri_base)) {
  13012. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13013. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13014. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13015. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13016. }
  13017. if (fctl & FC_FC_EX_CTX) {
  13018. /* ABTS sent by responder to CT exchange, construction
  13019. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13020. * field and RX_ID from ABTS for RX_ID field.
  13021. */
  13022. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13023. } else {
  13024. /* ABTS sent by initiator to CT exchange, construction
  13025. * of BA_ACC will need to allocate a new XRI as for the
  13026. * XRI_TAG field.
  13027. */
  13028. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13029. }
  13030. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13031. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13032. /* Xmit CT abts response on exchange <xid> */
  13033. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13034. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13035. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13036. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13037. if (rc == IOCB_ERROR) {
  13038. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  13039. "2925 Failed to issue CT ABTS RSP x%x on "
  13040. "xri x%x, Data x%x\n",
  13041. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13042. phba->link_state);
  13043. lpfc_sli_release_iocbq(phba, ctiocb);
  13044. }
  13045. }
  13046. /**
  13047. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13048. * @vport: Pointer to the vport on which this sequence was received
  13049. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13050. *
  13051. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13052. * receive sequence is only partially assembed by the driver, it shall abort
  13053. * the partially assembled frames for the sequence. Otherwise, if the
  13054. * unsolicited receive sequence has been completely assembled and passed to
  13055. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13056. * unsolicited sequence has been aborted. After that, it will issue a basic
  13057. * accept to accept the abort.
  13058. **/
  13059. void
  13060. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13061. struct hbq_dmabuf *dmabuf)
  13062. {
  13063. struct lpfc_hba *phba = vport->phba;
  13064. struct fc_frame_header fc_hdr;
  13065. uint32_t fctl;
  13066. bool abts_par;
  13067. /* Make a copy of fc_hdr before the dmabuf being released */
  13068. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13069. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13070. if (fctl & FC_FC_EX_CTX) {
  13071. /*
  13072. * ABTS sent by responder to exchange, just free the buffer
  13073. */
  13074. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13075. } else {
  13076. /*
  13077. * ABTS sent by initiator to exchange, need to do cleanup
  13078. */
  13079. /* Try to abort partially assembled seq */
  13080. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13081. /* Send abort to ULP if partially seq abort failed */
  13082. if (abts_par == false)
  13083. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13084. else
  13085. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13086. }
  13087. /* Send basic accept (BA_ACC) to the abort requester */
  13088. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13089. }
  13090. /**
  13091. * lpfc_seq_complete - Indicates if a sequence is complete
  13092. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13093. *
  13094. * This function checks the sequence, starting with the frame described by
  13095. * @dmabuf, to see if all the frames associated with this sequence are present.
  13096. * the frames associated with this sequence are linked to the @dmabuf using the
  13097. * dbuf list. This function looks for two major things. 1) That the first frame
  13098. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13099. * set. 3) That there are no holes in the sequence count. The function will
  13100. * return 1 when the sequence is complete, otherwise it will return 0.
  13101. **/
  13102. static int
  13103. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13104. {
  13105. struct fc_frame_header *hdr;
  13106. struct lpfc_dmabuf *d_buf;
  13107. struct hbq_dmabuf *seq_dmabuf;
  13108. uint32_t fctl;
  13109. int seq_count = 0;
  13110. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13111. /* make sure first fame of sequence has a sequence count of zero */
  13112. if (hdr->fh_seq_cnt != seq_count)
  13113. return 0;
  13114. fctl = (hdr->fh_f_ctl[0] << 16 |
  13115. hdr->fh_f_ctl[1] << 8 |
  13116. hdr->fh_f_ctl[2]);
  13117. /* If last frame of sequence we can return success. */
  13118. if (fctl & FC_FC_END_SEQ)
  13119. return 1;
  13120. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13121. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13122. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13123. /* If there is a hole in the sequence count then fail. */
  13124. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13125. return 0;
  13126. fctl = (hdr->fh_f_ctl[0] << 16 |
  13127. hdr->fh_f_ctl[1] << 8 |
  13128. hdr->fh_f_ctl[2]);
  13129. /* If last frame of sequence we can return success. */
  13130. if (fctl & FC_FC_END_SEQ)
  13131. return 1;
  13132. }
  13133. return 0;
  13134. }
  13135. /**
  13136. * lpfc_prep_seq - Prep sequence for ULP processing
  13137. * @vport: Pointer to the vport on which this sequence was received
  13138. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13139. *
  13140. * This function takes a sequence, described by a list of frames, and creates
  13141. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13142. * used to issue to the generic unsolicited sequence handler. This routine
  13143. * returns a pointer to the first iocbq in the list. If the function is unable
  13144. * to allocate an iocbq then it throw out the received frames that were not
  13145. * able to be described and return a pointer to the first iocbq. If unable to
  13146. * allocate any iocbqs (including the first) this function will return NULL.
  13147. **/
  13148. static struct lpfc_iocbq *
  13149. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13150. {
  13151. struct hbq_dmabuf *hbq_buf;
  13152. struct lpfc_dmabuf *d_buf, *n_buf;
  13153. struct lpfc_iocbq *first_iocbq, *iocbq;
  13154. struct fc_frame_header *fc_hdr;
  13155. uint32_t sid;
  13156. uint32_t len, tot_len;
  13157. struct ulp_bde64 *pbde;
  13158. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13159. /* remove from receive buffer list */
  13160. list_del_init(&seq_dmabuf->hbuf.list);
  13161. lpfc_update_rcv_time_stamp(vport);
  13162. /* get the Remote Port's SID */
  13163. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13164. tot_len = 0;
  13165. /* Get an iocbq struct to fill in. */
  13166. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13167. if (first_iocbq) {
  13168. /* Initialize the first IOCB. */
  13169. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13170. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13171. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13172. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13173. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13174. first_iocbq->iocb.un.rcvels.parmRo =
  13175. sli4_did_from_fc_hdr(fc_hdr);
  13176. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13177. } else
  13178. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13179. first_iocbq->iocb.ulpContext = NO_XRI;
  13180. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13181. be16_to_cpu(fc_hdr->fh_ox_id);
  13182. /* iocbq is prepped for internal consumption. Physical vpi. */
  13183. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13184. vport->phba->vpi_ids[vport->vpi];
  13185. /* put the first buffer into the first IOCBq */
  13186. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13187. first_iocbq->context3 = NULL;
  13188. first_iocbq->iocb.ulpBdeCount = 1;
  13189. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13190. LPFC_DATA_BUF_SIZE;
  13191. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13192. tot_len = bf_get(lpfc_rcqe_length,
  13193. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13194. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13195. }
  13196. iocbq = first_iocbq;
  13197. /*
  13198. * Each IOCBq can have two Buffers assigned, so go through the list
  13199. * of buffers for this sequence and save two buffers in each IOCBq
  13200. */
  13201. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13202. if (!iocbq) {
  13203. lpfc_in_buf_free(vport->phba, d_buf);
  13204. continue;
  13205. }
  13206. if (!iocbq->context3) {
  13207. iocbq->context3 = d_buf;
  13208. iocbq->iocb.ulpBdeCount++;
  13209. pbde = (struct ulp_bde64 *)
  13210. &iocbq->iocb.unsli3.sli3Words[4];
  13211. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13212. /* We need to get the size out of the right CQE */
  13213. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13214. len = bf_get(lpfc_rcqe_length,
  13215. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13216. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13217. tot_len += len;
  13218. } else {
  13219. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13220. if (!iocbq) {
  13221. if (first_iocbq) {
  13222. first_iocbq->iocb.ulpStatus =
  13223. IOSTAT_FCP_RSP_ERROR;
  13224. first_iocbq->iocb.un.ulpWord[4] =
  13225. IOERR_NO_RESOURCES;
  13226. }
  13227. lpfc_in_buf_free(vport->phba, d_buf);
  13228. continue;
  13229. }
  13230. iocbq->context2 = d_buf;
  13231. iocbq->context3 = NULL;
  13232. iocbq->iocb.ulpBdeCount = 1;
  13233. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13234. LPFC_DATA_BUF_SIZE;
  13235. /* We need to get the size out of the right CQE */
  13236. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13237. len = bf_get(lpfc_rcqe_length,
  13238. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13239. tot_len += len;
  13240. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13241. iocbq->iocb.un.rcvels.remoteID = sid;
  13242. list_add_tail(&iocbq->list, &first_iocbq->list);
  13243. }
  13244. }
  13245. return first_iocbq;
  13246. }
  13247. static void
  13248. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13249. struct hbq_dmabuf *seq_dmabuf)
  13250. {
  13251. struct fc_frame_header *fc_hdr;
  13252. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13253. struct lpfc_hba *phba = vport->phba;
  13254. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13255. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13256. if (!iocbq) {
  13257. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13258. "2707 Ring %d handler: Failed to allocate "
  13259. "iocb Rctl x%x Type x%x received\n",
  13260. LPFC_ELS_RING,
  13261. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13262. return;
  13263. }
  13264. if (!lpfc_complete_unsol_iocb(phba,
  13265. &phba->sli.ring[LPFC_ELS_RING],
  13266. iocbq, fc_hdr->fh_r_ctl,
  13267. fc_hdr->fh_type))
  13268. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13269. "2540 Ring %d handler: unexpected Rctl "
  13270. "x%x Type x%x received\n",
  13271. LPFC_ELS_RING,
  13272. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13273. /* Free iocb created in lpfc_prep_seq */
  13274. list_for_each_entry_safe(curr_iocb, next_iocb,
  13275. &iocbq->list, list) {
  13276. list_del_init(&curr_iocb->list);
  13277. lpfc_sli_release_iocbq(phba, curr_iocb);
  13278. }
  13279. lpfc_sli_release_iocbq(phba, iocbq);
  13280. }
  13281. /**
  13282. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13283. * @phba: Pointer to HBA context object.
  13284. *
  13285. * This function is called with no lock held. This function processes all
  13286. * the received buffers and gives it to upper layers when a received buffer
  13287. * indicates that it is the final frame in the sequence. The interrupt
  13288. * service routine processes received buffers at interrupt contexts and adds
  13289. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13290. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13291. * appropriate receive function when the final frame in a sequence is received.
  13292. **/
  13293. void
  13294. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13295. struct hbq_dmabuf *dmabuf)
  13296. {
  13297. struct hbq_dmabuf *seq_dmabuf;
  13298. struct fc_frame_header *fc_hdr;
  13299. struct lpfc_vport *vport;
  13300. uint32_t fcfi;
  13301. uint32_t did;
  13302. /* Process each received buffer */
  13303. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13304. /* check to see if this a valid type of frame */
  13305. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13306. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13307. return;
  13308. }
  13309. if ((bf_get(lpfc_cqe_code,
  13310. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13311. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13312. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13313. else
  13314. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13315. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13316. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13317. if (!vport) {
  13318. /* throw out the frame */
  13319. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13320. return;
  13321. }
  13322. /* d_id this frame is directed to */
  13323. did = sli4_did_from_fc_hdr(fc_hdr);
  13324. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13325. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13326. (did != Fabric_DID)) {
  13327. /*
  13328. * Throw out the frame if we are not pt2pt.
  13329. * The pt2pt protocol allows for discovery frames
  13330. * to be received without a registered VPI.
  13331. */
  13332. if (!(vport->fc_flag & FC_PT2PT) ||
  13333. (phba->link_state == LPFC_HBA_READY)) {
  13334. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13335. return;
  13336. }
  13337. }
  13338. /* Handle the basic abort sequence (BA_ABTS) event */
  13339. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13340. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13341. return;
  13342. }
  13343. /* Link this frame */
  13344. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13345. if (!seq_dmabuf) {
  13346. /* unable to add frame to vport - throw it out */
  13347. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13348. return;
  13349. }
  13350. /* If not last frame in sequence continue processing frames. */
  13351. if (!lpfc_seq_complete(seq_dmabuf))
  13352. return;
  13353. /* Send the complete sequence to the upper layer protocol */
  13354. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13355. }
  13356. /**
  13357. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13358. * @phba: pointer to lpfc hba data structure.
  13359. *
  13360. * This routine is invoked to post rpi header templates to the
  13361. * HBA consistent with the SLI-4 interface spec. This routine
  13362. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13363. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13364. *
  13365. * This routine does not require any locks. It's usage is expected
  13366. * to be driver load or reset recovery when the driver is
  13367. * sequential.
  13368. *
  13369. * Return codes
  13370. * 0 - successful
  13371. * -EIO - The mailbox failed to complete successfully.
  13372. * When this error occurs, the driver is not guaranteed
  13373. * to have any rpi regions posted to the device and
  13374. * must either attempt to repost the regions or take a
  13375. * fatal error.
  13376. **/
  13377. int
  13378. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13379. {
  13380. struct lpfc_rpi_hdr *rpi_page;
  13381. uint32_t rc = 0;
  13382. uint16_t lrpi = 0;
  13383. /* SLI4 ports that support extents do not require RPI headers. */
  13384. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13385. goto exit;
  13386. if (phba->sli4_hba.extents_in_use)
  13387. return -EIO;
  13388. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13389. /*
  13390. * Assign the rpi headers a physical rpi only if the driver
  13391. * has not initialized those resources. A port reset only
  13392. * needs the headers posted.
  13393. */
  13394. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13395. LPFC_RPI_RSRC_RDY)
  13396. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13397. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13398. if (rc != MBX_SUCCESS) {
  13399. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13400. "2008 Error %d posting all rpi "
  13401. "headers\n", rc);
  13402. rc = -EIO;
  13403. break;
  13404. }
  13405. }
  13406. exit:
  13407. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13408. LPFC_RPI_RSRC_RDY);
  13409. return rc;
  13410. }
  13411. /**
  13412. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13413. * @phba: pointer to lpfc hba data structure.
  13414. * @rpi_page: pointer to the rpi memory region.
  13415. *
  13416. * This routine is invoked to post a single rpi header to the
  13417. * HBA consistent with the SLI-4 interface spec. This memory region
  13418. * maps up to 64 rpi context regions.
  13419. *
  13420. * Return codes
  13421. * 0 - successful
  13422. * -ENOMEM - No available memory
  13423. * -EIO - The mailbox failed to complete successfully.
  13424. **/
  13425. int
  13426. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13427. {
  13428. LPFC_MBOXQ_t *mboxq;
  13429. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13430. uint32_t rc = 0;
  13431. uint32_t shdr_status, shdr_add_status;
  13432. union lpfc_sli4_cfg_shdr *shdr;
  13433. /* SLI4 ports that support extents do not require RPI headers. */
  13434. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13435. return rc;
  13436. if (phba->sli4_hba.extents_in_use)
  13437. return -EIO;
  13438. /* The port is notified of the header region via a mailbox command. */
  13439. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13440. if (!mboxq) {
  13441. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13442. "2001 Unable to allocate memory for issuing "
  13443. "SLI_CONFIG_SPECIAL mailbox command\n");
  13444. return -ENOMEM;
  13445. }
  13446. /* Post all rpi memory regions to the port. */
  13447. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13448. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13449. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13450. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13451. sizeof(struct lpfc_sli4_cfg_mhdr),
  13452. LPFC_SLI4_MBX_EMBED);
  13453. /* Post the physical rpi to the port for this rpi header. */
  13454. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13455. rpi_page->start_rpi);
  13456. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13457. hdr_tmpl, rpi_page->page_count);
  13458. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13459. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13460. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13461. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13462. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13463. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13464. if (rc != MBX_TIMEOUT)
  13465. mempool_free(mboxq, phba->mbox_mem_pool);
  13466. if (shdr_status || shdr_add_status || rc) {
  13467. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13468. "2514 POST_RPI_HDR mailbox failed with "
  13469. "status x%x add_status x%x, mbx status x%x\n",
  13470. shdr_status, shdr_add_status, rc);
  13471. rc = -ENXIO;
  13472. }
  13473. return rc;
  13474. }
  13475. /**
  13476. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13477. * @phba: pointer to lpfc hba data structure.
  13478. *
  13479. * This routine is invoked to post rpi header templates to the
  13480. * HBA consistent with the SLI-4 interface spec. This routine
  13481. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13482. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13483. *
  13484. * Returns
  13485. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13486. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13487. **/
  13488. int
  13489. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13490. {
  13491. unsigned long rpi;
  13492. uint16_t max_rpi, rpi_limit;
  13493. uint16_t rpi_remaining, lrpi = 0;
  13494. struct lpfc_rpi_hdr *rpi_hdr;
  13495. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13496. rpi_limit = phba->sli4_hba.next_rpi;
  13497. /*
  13498. * Fetch the next logical rpi. Because this index is logical,
  13499. * the driver starts at 0 each time.
  13500. */
  13501. spin_lock_irq(&phba->hbalock);
  13502. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13503. if (rpi >= rpi_limit)
  13504. rpi = LPFC_RPI_ALLOC_ERROR;
  13505. else {
  13506. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13507. phba->sli4_hba.max_cfg_param.rpi_used++;
  13508. phba->sli4_hba.rpi_count++;
  13509. }
  13510. /*
  13511. * Don't try to allocate more rpi header regions if the device limit
  13512. * has been exhausted.
  13513. */
  13514. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13515. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13516. spin_unlock_irq(&phba->hbalock);
  13517. return rpi;
  13518. }
  13519. /*
  13520. * RPI header postings are not required for SLI4 ports capable of
  13521. * extents.
  13522. */
  13523. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13524. spin_unlock_irq(&phba->hbalock);
  13525. return rpi;
  13526. }
  13527. /*
  13528. * If the driver is running low on rpi resources, allocate another
  13529. * page now. Note that the next_rpi value is used because
  13530. * it represents how many are actually in use whereas max_rpi notes
  13531. * how many are supported max by the device.
  13532. */
  13533. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13534. spin_unlock_irq(&phba->hbalock);
  13535. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13536. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13537. if (!rpi_hdr) {
  13538. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13539. "2002 Error Could not grow rpi "
  13540. "count\n");
  13541. } else {
  13542. lrpi = rpi_hdr->start_rpi;
  13543. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13544. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13545. }
  13546. }
  13547. return rpi;
  13548. }
  13549. /**
  13550. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13551. * @phba: pointer to lpfc hba data structure.
  13552. *
  13553. * This routine is invoked to release an rpi to the pool of
  13554. * available rpis maintained by the driver.
  13555. **/
  13556. void
  13557. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13558. {
  13559. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13560. phba->sli4_hba.rpi_count--;
  13561. phba->sli4_hba.max_cfg_param.rpi_used--;
  13562. }
  13563. }
  13564. /**
  13565. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13566. * @phba: pointer to lpfc hba data structure.
  13567. *
  13568. * This routine is invoked to release an rpi to the pool of
  13569. * available rpis maintained by the driver.
  13570. **/
  13571. void
  13572. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13573. {
  13574. spin_lock_irq(&phba->hbalock);
  13575. __lpfc_sli4_free_rpi(phba, rpi);
  13576. spin_unlock_irq(&phba->hbalock);
  13577. }
  13578. /**
  13579. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13580. * @phba: pointer to lpfc hba data structure.
  13581. *
  13582. * This routine is invoked to remove the memory region that
  13583. * provided rpi via a bitmask.
  13584. **/
  13585. void
  13586. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13587. {
  13588. kfree(phba->sli4_hba.rpi_bmask);
  13589. kfree(phba->sli4_hba.rpi_ids);
  13590. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13591. }
  13592. /**
  13593. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13594. * @phba: pointer to lpfc hba data structure.
  13595. *
  13596. * This routine is invoked to remove the memory region that
  13597. * provided rpi via a bitmask.
  13598. **/
  13599. int
  13600. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13601. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13602. {
  13603. LPFC_MBOXQ_t *mboxq;
  13604. struct lpfc_hba *phba = ndlp->phba;
  13605. int rc;
  13606. /* The port is notified of the header region via a mailbox command. */
  13607. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13608. if (!mboxq)
  13609. return -ENOMEM;
  13610. /* Post all rpi memory regions to the port. */
  13611. lpfc_resume_rpi(mboxq, ndlp);
  13612. if (cmpl) {
  13613. mboxq->mbox_cmpl = cmpl;
  13614. mboxq->context1 = arg;
  13615. mboxq->context2 = ndlp;
  13616. } else
  13617. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13618. mboxq->vport = ndlp->vport;
  13619. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13620. if (rc == MBX_NOT_FINISHED) {
  13621. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13622. "2010 Resume RPI Mailbox failed "
  13623. "status %d, mbxStatus x%x\n", rc,
  13624. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13625. mempool_free(mboxq, phba->mbox_mem_pool);
  13626. return -EIO;
  13627. }
  13628. return 0;
  13629. }
  13630. /**
  13631. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13632. * @vport: Pointer to the vport for which the vpi is being initialized
  13633. *
  13634. * This routine is invoked to activate a vpi with the port.
  13635. *
  13636. * Returns:
  13637. * 0 success
  13638. * -Evalue otherwise
  13639. **/
  13640. int
  13641. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13642. {
  13643. LPFC_MBOXQ_t *mboxq;
  13644. int rc = 0;
  13645. int retval = MBX_SUCCESS;
  13646. uint32_t mbox_tmo;
  13647. struct lpfc_hba *phba = vport->phba;
  13648. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13649. if (!mboxq)
  13650. return -ENOMEM;
  13651. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13652. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13653. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13654. if (rc != MBX_SUCCESS) {
  13655. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13656. "2022 INIT VPI Mailbox failed "
  13657. "status %d, mbxStatus x%x\n", rc,
  13658. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13659. retval = -EIO;
  13660. }
  13661. if (rc != MBX_TIMEOUT)
  13662. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13663. return retval;
  13664. }
  13665. /**
  13666. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13667. * @phba: pointer to lpfc hba data structure.
  13668. * @mboxq: Pointer to mailbox object.
  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. static void
  13675. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13676. {
  13677. void *virt_addr;
  13678. union lpfc_sli4_cfg_shdr *shdr;
  13679. uint32_t shdr_status, shdr_add_status;
  13680. virt_addr = mboxq->sge_array->addr[0];
  13681. /* The IOCTL status is embedded in the mailbox subheader. */
  13682. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13683. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13684. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13685. if ((shdr_status || shdr_add_status) &&
  13686. (shdr_status != STATUS_FCF_IN_USE))
  13687. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13688. "2558 ADD_FCF_RECORD mailbox failed with "
  13689. "status x%x add_status x%x\n",
  13690. shdr_status, shdr_add_status);
  13691. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13692. }
  13693. /**
  13694. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13695. * @phba: pointer to lpfc hba data structure.
  13696. * @fcf_record: pointer to the initialized fcf record to add.
  13697. *
  13698. * This routine is invoked to manually add a single FCF record. The caller
  13699. * must pass a completely initialized FCF_Record. This routine takes
  13700. * care of the nonembedded mailbox operations.
  13701. **/
  13702. int
  13703. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13704. {
  13705. int rc = 0;
  13706. LPFC_MBOXQ_t *mboxq;
  13707. uint8_t *bytep;
  13708. void *virt_addr;
  13709. dma_addr_t phys_addr;
  13710. struct lpfc_mbx_sge sge;
  13711. uint32_t alloc_len, req_len;
  13712. uint32_t fcfindex;
  13713. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13714. if (!mboxq) {
  13715. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13716. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13717. return -ENOMEM;
  13718. }
  13719. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13720. sizeof(uint32_t);
  13721. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13722. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13723. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13724. req_len, LPFC_SLI4_MBX_NEMBED);
  13725. if (alloc_len < req_len) {
  13726. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13727. "2523 Allocated DMA memory size (x%x) is "
  13728. "less than the requested DMA memory "
  13729. "size (x%x)\n", alloc_len, req_len);
  13730. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13731. return -ENOMEM;
  13732. }
  13733. /*
  13734. * Get the first SGE entry from the non-embedded DMA memory. This
  13735. * routine only uses a single SGE.
  13736. */
  13737. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13738. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13739. virt_addr = mboxq->sge_array->addr[0];
  13740. /*
  13741. * Configure the FCF record for FCFI 0. This is the driver's
  13742. * hardcoded default and gets used in nonFIP mode.
  13743. */
  13744. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13745. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13746. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13747. /*
  13748. * Copy the fcf_index and the FCF Record Data. The data starts after
  13749. * the FCoE header plus word10. The data copy needs to be endian
  13750. * correct.
  13751. */
  13752. bytep += sizeof(uint32_t);
  13753. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13754. mboxq->vport = phba->pport;
  13755. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13756. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13757. if (rc == MBX_NOT_FINISHED) {
  13758. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13759. "2515 ADD_FCF_RECORD mailbox failed with "
  13760. "status 0x%x\n", rc);
  13761. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13762. rc = -EIO;
  13763. } else
  13764. rc = 0;
  13765. return rc;
  13766. }
  13767. /**
  13768. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13769. * @phba: pointer to lpfc hba data structure.
  13770. * @fcf_record: pointer to the fcf record to write the default data.
  13771. * @fcf_index: FCF table entry index.
  13772. *
  13773. * This routine is invoked to build the driver's default FCF record. The
  13774. * values used are hardcoded. This routine handles memory initialization.
  13775. *
  13776. **/
  13777. void
  13778. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13779. struct fcf_record *fcf_record,
  13780. uint16_t fcf_index)
  13781. {
  13782. memset(fcf_record, 0, sizeof(struct fcf_record));
  13783. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13784. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13785. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13786. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13787. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13788. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13789. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13790. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13791. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13792. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13793. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13794. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13795. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13796. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13797. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13798. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13799. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13800. /* Set the VLAN bit map */
  13801. if (phba->valid_vlan) {
  13802. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13803. = 1 << (phba->vlan_id % 8);
  13804. }
  13805. }
  13806. /**
  13807. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13808. * @phba: pointer to lpfc hba data structure.
  13809. * @fcf_index: FCF table entry offset.
  13810. *
  13811. * This routine is invoked to scan the entire FCF table by reading FCF
  13812. * record and processing it one at a time starting from the @fcf_index
  13813. * for initial FCF discovery or fast FCF failover rediscovery.
  13814. *
  13815. * Return 0 if the mailbox command is submitted successfully, none 0
  13816. * otherwise.
  13817. **/
  13818. int
  13819. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13820. {
  13821. int rc = 0, error;
  13822. LPFC_MBOXQ_t *mboxq;
  13823. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13824. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13825. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13826. if (!mboxq) {
  13827. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13828. "2000 Failed to allocate mbox for "
  13829. "READ_FCF cmd\n");
  13830. error = -ENOMEM;
  13831. goto fail_fcf_scan;
  13832. }
  13833. /* Construct the read FCF record mailbox command */
  13834. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13835. if (rc) {
  13836. error = -EINVAL;
  13837. goto fail_fcf_scan;
  13838. }
  13839. /* Issue the mailbox command asynchronously */
  13840. mboxq->vport = phba->pport;
  13841. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13842. spin_lock_irq(&phba->hbalock);
  13843. phba->hba_flag |= FCF_TS_INPROG;
  13844. spin_unlock_irq(&phba->hbalock);
  13845. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13846. if (rc == MBX_NOT_FINISHED)
  13847. error = -EIO;
  13848. else {
  13849. /* Reset eligible FCF count for new scan */
  13850. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13851. phba->fcf.eligible_fcf_cnt = 0;
  13852. error = 0;
  13853. }
  13854. fail_fcf_scan:
  13855. if (error) {
  13856. if (mboxq)
  13857. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13858. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13859. spin_lock_irq(&phba->hbalock);
  13860. phba->hba_flag &= ~FCF_TS_INPROG;
  13861. spin_unlock_irq(&phba->hbalock);
  13862. }
  13863. return error;
  13864. }
  13865. /**
  13866. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13867. * @phba: pointer to lpfc hba data structure.
  13868. * @fcf_index: FCF table entry offset.
  13869. *
  13870. * This routine is invoked to read an FCF record indicated by @fcf_index
  13871. * and to use it for FLOGI roundrobin FCF failover.
  13872. *
  13873. * Return 0 if the mailbox command is submitted successfully, none 0
  13874. * otherwise.
  13875. **/
  13876. int
  13877. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13878. {
  13879. int rc = 0, error;
  13880. LPFC_MBOXQ_t *mboxq;
  13881. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13882. if (!mboxq) {
  13883. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13884. "2763 Failed to allocate mbox for "
  13885. "READ_FCF cmd\n");
  13886. error = -ENOMEM;
  13887. goto fail_fcf_read;
  13888. }
  13889. /* Construct the read FCF record mailbox command */
  13890. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13891. if (rc) {
  13892. error = -EINVAL;
  13893. goto fail_fcf_read;
  13894. }
  13895. /* Issue the mailbox command asynchronously */
  13896. mboxq->vport = phba->pport;
  13897. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13898. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13899. if (rc == MBX_NOT_FINISHED)
  13900. error = -EIO;
  13901. else
  13902. error = 0;
  13903. fail_fcf_read:
  13904. if (error && mboxq)
  13905. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13906. return error;
  13907. }
  13908. /**
  13909. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13910. * @phba: pointer to lpfc hba data structure.
  13911. * @fcf_index: FCF table entry offset.
  13912. *
  13913. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13914. * determine whether it's eligible for FLOGI roundrobin failover list.
  13915. *
  13916. * Return 0 if the mailbox command is submitted successfully, none 0
  13917. * otherwise.
  13918. **/
  13919. int
  13920. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13921. {
  13922. int rc = 0, error;
  13923. LPFC_MBOXQ_t *mboxq;
  13924. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13925. if (!mboxq) {
  13926. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13927. "2758 Failed to allocate mbox for "
  13928. "READ_FCF cmd\n");
  13929. error = -ENOMEM;
  13930. goto fail_fcf_read;
  13931. }
  13932. /* Construct the read FCF record mailbox command */
  13933. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13934. if (rc) {
  13935. error = -EINVAL;
  13936. goto fail_fcf_read;
  13937. }
  13938. /* Issue the mailbox command asynchronously */
  13939. mboxq->vport = phba->pport;
  13940. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13941. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13942. if (rc == MBX_NOT_FINISHED)
  13943. error = -EIO;
  13944. else
  13945. error = 0;
  13946. fail_fcf_read:
  13947. if (error && mboxq)
  13948. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13949. return error;
  13950. }
  13951. /**
  13952. * lpfc_check_next_fcf_pri
  13953. * phba pointer to the lpfc_hba struct for this port.
  13954. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13955. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13956. * rr_bmask based on their priority level. Starting from the highest priority
  13957. * to the lowest. The most likely FCF candidate will be in the highest
  13958. * priority group. When this routine is called it searches the fcf_pri list for
  13959. * next lowest priority group and repopulates the rr_bmask with only those
  13960. * fcf_indexes.
  13961. * returns:
  13962. * 1=success 0=failure
  13963. **/
  13964. int
  13965. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13966. {
  13967. uint16_t next_fcf_pri;
  13968. uint16_t last_index;
  13969. struct lpfc_fcf_pri *fcf_pri;
  13970. int rc;
  13971. int ret = 0;
  13972. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13973. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13974. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13975. "3060 Last IDX %d\n", last_index);
  13976. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13977. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13978. "3061 Last IDX %d\n", last_index);
  13979. return 0; /* Empty rr list */
  13980. }
  13981. next_fcf_pri = 0;
  13982. /*
  13983. * Clear the rr_bmask and set all of the bits that are at this
  13984. * priority.
  13985. */
  13986. memset(phba->fcf.fcf_rr_bmask, 0,
  13987. sizeof(*phba->fcf.fcf_rr_bmask));
  13988. spin_lock_irq(&phba->hbalock);
  13989. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13990. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13991. continue;
  13992. /*
  13993. * the 1st priority that has not FLOGI failed
  13994. * will be the highest.
  13995. */
  13996. if (!next_fcf_pri)
  13997. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13998. spin_unlock_irq(&phba->hbalock);
  13999. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14000. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14001. fcf_pri->fcf_rec.fcf_index);
  14002. if (rc)
  14003. return 0;
  14004. }
  14005. spin_lock_irq(&phba->hbalock);
  14006. }
  14007. /*
  14008. * if next_fcf_pri was not set above and the list is not empty then
  14009. * we have failed flogis on all of them. So reset flogi failed
  14010. * and start at the begining.
  14011. */
  14012. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  14013. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14014. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14015. /*
  14016. * the 1st priority that has not FLOGI failed
  14017. * will be the highest.
  14018. */
  14019. if (!next_fcf_pri)
  14020. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14021. spin_unlock_irq(&phba->hbalock);
  14022. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14023. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14024. fcf_pri->fcf_rec.fcf_index);
  14025. if (rc)
  14026. return 0;
  14027. }
  14028. spin_lock_irq(&phba->hbalock);
  14029. }
  14030. } else
  14031. ret = 1;
  14032. spin_unlock_irq(&phba->hbalock);
  14033. return ret;
  14034. }
  14035. /**
  14036. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14037. * @phba: pointer to lpfc hba data structure.
  14038. *
  14039. * This routine is to get the next eligible FCF record index in a round
  14040. * robin fashion. If the next eligible FCF record index equals to the
  14041. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14042. * shall be returned, otherwise, the next eligible FCF record's index
  14043. * shall be returned.
  14044. **/
  14045. uint16_t
  14046. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14047. {
  14048. uint16_t next_fcf_index;
  14049. /* Search start from next bit of currently registered FCF index */
  14050. next_priority:
  14051. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  14052. LPFC_SLI4_FCF_TBL_INDX_MAX;
  14053. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14054. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14055. next_fcf_index);
  14056. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14057. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14058. /*
  14059. * If we have wrapped then we need to clear the bits that
  14060. * have been tested so that we can detect when we should
  14061. * change the priority level.
  14062. */
  14063. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14064. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14065. }
  14066. /* Check roundrobin failover list empty condition */
  14067. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14068. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14069. /*
  14070. * If next fcf index is not found check if there are lower
  14071. * Priority level fcf's in the fcf_priority list.
  14072. * Set up the rr_bmask with all of the avaiable fcf bits
  14073. * at that level and continue the selection process.
  14074. */
  14075. if (lpfc_check_next_fcf_pri_level(phba))
  14076. goto next_priority;
  14077. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14078. "2844 No roundrobin failover FCF available\n");
  14079. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14080. return LPFC_FCOE_FCF_NEXT_NONE;
  14081. else {
  14082. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14083. "3063 Only FCF available idx %d, flag %x\n",
  14084. next_fcf_index,
  14085. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14086. return next_fcf_index;
  14087. }
  14088. }
  14089. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14090. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14091. LPFC_FCF_FLOGI_FAILED)
  14092. goto next_priority;
  14093. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14094. "2845 Get next roundrobin failover FCF (x%x)\n",
  14095. next_fcf_index);
  14096. return next_fcf_index;
  14097. }
  14098. /**
  14099. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14100. * @phba: pointer to lpfc hba data structure.
  14101. *
  14102. * This routine sets the FCF record index in to 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 setting the bit.
  14106. *
  14107. * Returns 0 if the index bit successfully set, otherwise, it returns
  14108. * -EINVAL.
  14109. **/
  14110. int
  14111. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14112. {
  14113. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14114. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14115. "2610 FCF (x%x) reached driver's book "
  14116. "keeping dimension:x%x\n",
  14117. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14118. return -EINVAL;
  14119. }
  14120. /* Set the eligible FCF record index bmask */
  14121. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14122. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14123. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14124. "bmask\n", fcf_index);
  14125. return 0;
  14126. }
  14127. /**
  14128. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14129. * @phba: pointer to lpfc hba data structure.
  14130. *
  14131. * This routine clears the FCF record index from the eligible bmask for
  14132. * roundrobin failover search. It checks to make sure that the index
  14133. * does not go beyond the range of the driver allocated bmask dimension
  14134. * before clearing the bit.
  14135. **/
  14136. void
  14137. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14138. {
  14139. struct lpfc_fcf_pri *fcf_pri;
  14140. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14141. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14142. "2762 FCF (x%x) reached driver's book "
  14143. "keeping dimension:x%x\n",
  14144. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14145. return;
  14146. }
  14147. /* Clear the eligible FCF record index bmask */
  14148. spin_lock_irq(&phba->hbalock);
  14149. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14150. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14151. list_del_init(&fcf_pri->list);
  14152. break;
  14153. }
  14154. }
  14155. spin_unlock_irq(&phba->hbalock);
  14156. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14157. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14158. "2791 Clear FCF (x%x) from roundrobin failover "
  14159. "bmask\n", fcf_index);
  14160. }
  14161. /**
  14162. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14163. * @phba: pointer to lpfc hba data structure.
  14164. *
  14165. * This routine is the completion routine for the rediscover FCF table mailbox
  14166. * command. If the mailbox command returned failure, it will try to stop the
  14167. * FCF rediscover wait timer.
  14168. **/
  14169. void
  14170. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14171. {
  14172. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14173. uint32_t shdr_status, shdr_add_status;
  14174. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14175. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14176. &redisc_fcf->header.cfg_shdr.response);
  14177. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14178. &redisc_fcf->header.cfg_shdr.response);
  14179. if (shdr_status || shdr_add_status) {
  14180. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14181. "2746 Requesting for FCF rediscovery failed "
  14182. "status x%x add_status x%x\n",
  14183. shdr_status, shdr_add_status);
  14184. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14185. spin_lock_irq(&phba->hbalock);
  14186. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14187. spin_unlock_irq(&phba->hbalock);
  14188. /*
  14189. * CVL event triggered FCF rediscover request failed,
  14190. * last resort to re-try current registered FCF entry.
  14191. */
  14192. lpfc_retry_pport_discovery(phba);
  14193. } else {
  14194. spin_lock_irq(&phba->hbalock);
  14195. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14196. spin_unlock_irq(&phba->hbalock);
  14197. /*
  14198. * DEAD FCF event triggered FCF rediscover request
  14199. * failed, last resort to fail over as a link down
  14200. * to FCF registration.
  14201. */
  14202. lpfc_sli4_fcf_dead_failthrough(phba);
  14203. }
  14204. } else {
  14205. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14206. "2775 Start FCF rediscover quiescent timer\n");
  14207. /*
  14208. * Start FCF rediscovery wait timer for pending FCF
  14209. * before rescan FCF record table.
  14210. */
  14211. lpfc_fcf_redisc_wait_start_timer(phba);
  14212. }
  14213. mempool_free(mbox, phba->mbox_mem_pool);
  14214. }
  14215. /**
  14216. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14217. * @phba: pointer to lpfc hba data structure.
  14218. *
  14219. * This routine is invoked to request for rediscovery of the entire FCF table
  14220. * by the port.
  14221. **/
  14222. int
  14223. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14224. {
  14225. LPFC_MBOXQ_t *mbox;
  14226. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14227. int rc, length;
  14228. /* Cancel retry delay timers to all vports before FCF rediscover */
  14229. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14230. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14231. if (!mbox) {
  14232. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14233. "2745 Failed to allocate mbox for "
  14234. "requesting FCF rediscover.\n");
  14235. return -ENOMEM;
  14236. }
  14237. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14238. sizeof(struct lpfc_sli4_cfg_mhdr));
  14239. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14240. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14241. length, LPFC_SLI4_MBX_EMBED);
  14242. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14243. /* Set count to 0 for invalidating the entire FCF database */
  14244. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14245. /* Issue the mailbox command asynchronously */
  14246. mbox->vport = phba->pport;
  14247. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14248. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14249. if (rc == MBX_NOT_FINISHED) {
  14250. mempool_free(mbox, phba->mbox_mem_pool);
  14251. return -EIO;
  14252. }
  14253. return 0;
  14254. }
  14255. /**
  14256. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14257. * @phba: pointer to lpfc hba data structure.
  14258. *
  14259. * This function is the failover routine as a last resort to the FCF DEAD
  14260. * event when driver failed to perform fast FCF failover.
  14261. **/
  14262. void
  14263. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14264. {
  14265. uint32_t link_state;
  14266. /*
  14267. * Last resort as FCF DEAD event failover will treat this as
  14268. * a link down, but save the link state because we don't want
  14269. * it to be changed to Link Down unless it is already down.
  14270. */
  14271. link_state = phba->link_state;
  14272. lpfc_linkdown(phba);
  14273. phba->link_state = link_state;
  14274. /* Unregister FCF if no devices connected to it */
  14275. lpfc_unregister_unused_fcf(phba);
  14276. }
  14277. /**
  14278. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14279. * @phba: pointer to lpfc hba data structure.
  14280. * @rgn23_data: pointer to configure region 23 data.
  14281. *
  14282. * This function gets SLI3 port configure region 23 data through memory dump
  14283. * mailbox command. When it successfully retrieves data, the size of the data
  14284. * will be returned, otherwise, 0 will be returned.
  14285. **/
  14286. static uint32_t
  14287. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14288. {
  14289. LPFC_MBOXQ_t *pmb = NULL;
  14290. MAILBOX_t *mb;
  14291. uint32_t offset = 0;
  14292. int rc;
  14293. if (!rgn23_data)
  14294. return 0;
  14295. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14296. if (!pmb) {
  14297. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14298. "2600 failed to allocate mailbox memory\n");
  14299. return 0;
  14300. }
  14301. mb = &pmb->u.mb;
  14302. do {
  14303. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14304. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14305. if (rc != MBX_SUCCESS) {
  14306. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14307. "2601 failed to read config "
  14308. "region 23, rc 0x%x Status 0x%x\n",
  14309. rc, mb->mbxStatus);
  14310. mb->un.varDmp.word_cnt = 0;
  14311. }
  14312. /*
  14313. * dump mem may return a zero when finished or we got a
  14314. * mailbox error, either way we are done.
  14315. */
  14316. if (mb->un.varDmp.word_cnt == 0)
  14317. break;
  14318. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14319. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14320. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14321. rgn23_data + offset,
  14322. mb->un.varDmp.word_cnt);
  14323. offset += mb->un.varDmp.word_cnt;
  14324. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14325. mempool_free(pmb, phba->mbox_mem_pool);
  14326. return offset;
  14327. }
  14328. /**
  14329. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14330. * @phba: pointer to lpfc hba data structure.
  14331. * @rgn23_data: pointer to configure region 23 data.
  14332. *
  14333. * This function gets SLI4 port configure region 23 data through memory dump
  14334. * mailbox command. When it successfully retrieves data, the size of the data
  14335. * will be returned, otherwise, 0 will be returned.
  14336. **/
  14337. static uint32_t
  14338. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14339. {
  14340. LPFC_MBOXQ_t *mboxq = NULL;
  14341. struct lpfc_dmabuf *mp = NULL;
  14342. struct lpfc_mqe *mqe;
  14343. uint32_t data_length = 0;
  14344. int rc;
  14345. if (!rgn23_data)
  14346. return 0;
  14347. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14348. if (!mboxq) {
  14349. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14350. "3105 failed to allocate mailbox memory\n");
  14351. return 0;
  14352. }
  14353. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14354. goto out;
  14355. mqe = &mboxq->u.mqe;
  14356. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14357. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14358. if (rc)
  14359. goto out;
  14360. data_length = mqe->un.mb_words[5];
  14361. if (data_length == 0)
  14362. goto out;
  14363. if (data_length > DMP_RGN23_SIZE) {
  14364. data_length = 0;
  14365. goto out;
  14366. }
  14367. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14368. out:
  14369. mempool_free(mboxq, phba->mbox_mem_pool);
  14370. if (mp) {
  14371. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14372. kfree(mp);
  14373. }
  14374. return data_length;
  14375. }
  14376. /**
  14377. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14378. * @phba: pointer to lpfc hba data structure.
  14379. *
  14380. * This function read region 23 and parse TLV for port status to
  14381. * decide if the user disaled the port. If the TLV indicates the
  14382. * port is disabled, the hba_flag is set accordingly.
  14383. **/
  14384. void
  14385. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14386. {
  14387. uint8_t *rgn23_data = NULL;
  14388. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14389. uint32_t offset = 0;
  14390. /* Get adapter Region 23 data */
  14391. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14392. if (!rgn23_data)
  14393. goto out;
  14394. if (phba->sli_rev < LPFC_SLI_REV4)
  14395. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14396. else {
  14397. if_type = bf_get(lpfc_sli_intf_if_type,
  14398. &phba->sli4_hba.sli_intf);
  14399. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14400. goto out;
  14401. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14402. }
  14403. if (!data_size)
  14404. goto out;
  14405. /* Check the region signature first */
  14406. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14407. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14408. "2619 Config region 23 has bad signature\n");
  14409. goto out;
  14410. }
  14411. offset += 4;
  14412. /* Check the data structure version */
  14413. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14414. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14415. "2620 Config region 23 has bad version\n");
  14416. goto out;
  14417. }
  14418. offset += 4;
  14419. /* Parse TLV entries in the region */
  14420. while (offset < data_size) {
  14421. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14422. break;
  14423. /*
  14424. * If the TLV is not driver specific TLV or driver id is
  14425. * not linux driver id, skip the record.
  14426. */
  14427. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14428. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14429. (rgn23_data[offset + 3] != 0)) {
  14430. offset += rgn23_data[offset + 1] * 4 + 4;
  14431. continue;
  14432. }
  14433. /* Driver found a driver specific TLV in the config region */
  14434. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14435. offset += 4;
  14436. tlv_offset = 0;
  14437. /*
  14438. * Search for configured port state sub-TLV.
  14439. */
  14440. while ((offset < data_size) &&
  14441. (tlv_offset < sub_tlv_len)) {
  14442. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14443. offset += 4;
  14444. tlv_offset += 4;
  14445. break;
  14446. }
  14447. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14448. offset += rgn23_data[offset + 1] * 4 + 4;
  14449. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14450. continue;
  14451. }
  14452. /* This HBA contains PORT_STE configured */
  14453. if (!rgn23_data[offset + 2])
  14454. phba->hba_flag |= LINK_DISABLED;
  14455. goto out;
  14456. }
  14457. }
  14458. out:
  14459. kfree(rgn23_data);
  14460. return;
  14461. }
  14462. /**
  14463. * lpfc_wr_object - write an object to the firmware
  14464. * @phba: HBA structure that indicates port to create a queue on.
  14465. * @dmabuf_list: list of dmabufs to write to the port.
  14466. * @size: the total byte value of the objects to write to the port.
  14467. * @offset: the current offset to be used to start the transfer.
  14468. *
  14469. * This routine will create a wr_object mailbox command to send to the port.
  14470. * the mailbox command will be constructed using the dma buffers described in
  14471. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14472. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14473. * used to indicate the starting offset of the transfer and will also return
  14474. * the offset after the write object mailbox has completed. @size is used to
  14475. * determine the end of the object and whether the eof bit should be set.
  14476. *
  14477. * Return 0 is successful and offset will contain the the new offset to use
  14478. * for the next write.
  14479. * Return negative value for error cases.
  14480. **/
  14481. int
  14482. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14483. uint32_t size, uint32_t *offset)
  14484. {
  14485. struct lpfc_mbx_wr_object *wr_object;
  14486. LPFC_MBOXQ_t *mbox;
  14487. int rc = 0, i = 0;
  14488. uint32_t shdr_status, shdr_add_status;
  14489. uint32_t mbox_tmo;
  14490. union lpfc_sli4_cfg_shdr *shdr;
  14491. struct lpfc_dmabuf *dmabuf;
  14492. uint32_t written = 0;
  14493. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14494. if (!mbox)
  14495. return -ENOMEM;
  14496. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14497. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14498. sizeof(struct lpfc_mbx_wr_object) -
  14499. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14500. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14501. wr_object->u.request.write_offset = *offset;
  14502. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14503. wr_object->u.request.object_name[0] =
  14504. cpu_to_le32(wr_object->u.request.object_name[0]);
  14505. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14506. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14507. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14508. break;
  14509. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14510. wr_object->u.request.bde[i].addrHigh =
  14511. putPaddrHigh(dmabuf->phys);
  14512. if (written + SLI4_PAGE_SIZE >= size) {
  14513. wr_object->u.request.bde[i].tus.f.bdeSize =
  14514. (size - written);
  14515. written += (size - written);
  14516. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14517. } else {
  14518. wr_object->u.request.bde[i].tus.f.bdeSize =
  14519. SLI4_PAGE_SIZE;
  14520. written += SLI4_PAGE_SIZE;
  14521. }
  14522. i++;
  14523. }
  14524. wr_object->u.request.bde_count = i;
  14525. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14526. if (!phba->sli4_hba.intr_enable)
  14527. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14528. else {
  14529. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14530. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14531. }
  14532. /* The IOCTL status is embedded in the mailbox subheader. */
  14533. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14534. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14535. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14536. if (rc != MBX_TIMEOUT)
  14537. mempool_free(mbox, phba->mbox_mem_pool);
  14538. if (shdr_status || shdr_add_status || rc) {
  14539. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14540. "3025 Write Object mailbox failed with "
  14541. "status x%x add_status x%x, mbx status x%x\n",
  14542. shdr_status, shdr_add_status, rc);
  14543. rc = -ENXIO;
  14544. } else
  14545. *offset += wr_object->u.response.actual_write_length;
  14546. return rc;
  14547. }
  14548. /**
  14549. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14550. * @vport: pointer to vport data structure.
  14551. *
  14552. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14553. * and REG_VPI mailbox commands associated with the vport. This function
  14554. * is called when driver want to restart discovery of the vport due to
  14555. * a Clear Virtual Link event.
  14556. **/
  14557. void
  14558. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14559. {
  14560. struct lpfc_hba *phba = vport->phba;
  14561. LPFC_MBOXQ_t *mb, *nextmb;
  14562. struct lpfc_dmabuf *mp;
  14563. struct lpfc_nodelist *ndlp;
  14564. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14565. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14566. LIST_HEAD(mbox_cmd_list);
  14567. uint8_t restart_loop;
  14568. /* Clean up internally queued mailbox commands with the vport */
  14569. spin_lock_irq(&phba->hbalock);
  14570. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14571. if (mb->vport != vport)
  14572. continue;
  14573. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14574. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14575. continue;
  14576. list_del(&mb->list);
  14577. list_add_tail(&mb->list, &mbox_cmd_list);
  14578. }
  14579. /* Clean up active mailbox command with the vport */
  14580. mb = phba->sli.mbox_active;
  14581. if (mb && (mb->vport == vport)) {
  14582. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14583. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14584. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14585. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14586. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14587. /* Put reference count for delayed processing */
  14588. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14589. /* Unregister the RPI when mailbox complete */
  14590. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14591. }
  14592. }
  14593. /* Cleanup any mailbox completions which are not yet processed */
  14594. do {
  14595. restart_loop = 0;
  14596. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14597. /*
  14598. * If this mailox is already processed or it is
  14599. * for another vport ignore it.
  14600. */
  14601. if ((mb->vport != vport) ||
  14602. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14603. continue;
  14604. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14605. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14606. continue;
  14607. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14608. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14609. ndlp = (struct lpfc_nodelist *)mb->context2;
  14610. /* Unregister the RPI when mailbox complete */
  14611. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14612. restart_loop = 1;
  14613. spin_unlock_irq(&phba->hbalock);
  14614. spin_lock(shost->host_lock);
  14615. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14616. spin_unlock(shost->host_lock);
  14617. spin_lock_irq(&phba->hbalock);
  14618. break;
  14619. }
  14620. }
  14621. } while (restart_loop);
  14622. spin_unlock_irq(&phba->hbalock);
  14623. /* Release the cleaned-up mailbox commands */
  14624. while (!list_empty(&mbox_cmd_list)) {
  14625. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14626. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14627. mp = (struct lpfc_dmabuf *) (mb->context1);
  14628. if (mp) {
  14629. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14630. kfree(mp);
  14631. }
  14632. ndlp = (struct lpfc_nodelist *) mb->context2;
  14633. mb->context2 = NULL;
  14634. if (ndlp) {
  14635. spin_lock(shost->host_lock);
  14636. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14637. spin_unlock(shost->host_lock);
  14638. lpfc_nlp_put(ndlp);
  14639. }
  14640. }
  14641. mempool_free(mb, phba->mbox_mem_pool);
  14642. }
  14643. /* Release the ndlp with the cleaned-up active mailbox command */
  14644. if (act_mbx_ndlp) {
  14645. spin_lock(shost->host_lock);
  14646. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14647. spin_unlock(shost->host_lock);
  14648. lpfc_nlp_put(act_mbx_ndlp);
  14649. }
  14650. }
  14651. /**
  14652. * lpfc_drain_txq - Drain the txq
  14653. * @phba: Pointer to HBA context object.
  14654. *
  14655. * This function attempt to submit IOCBs on the txq
  14656. * to the adapter. For SLI4 adapters, the txq contains
  14657. * ELS IOCBs that have been deferred because the there
  14658. * are no SGLs. This congestion can occur with large
  14659. * vport counts during node discovery.
  14660. **/
  14661. uint32_t
  14662. lpfc_drain_txq(struct lpfc_hba *phba)
  14663. {
  14664. LIST_HEAD(completions);
  14665. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14666. struct lpfc_iocbq *piocbq = 0;
  14667. unsigned long iflags = 0;
  14668. char *fail_msg = NULL;
  14669. struct lpfc_sglq *sglq;
  14670. union lpfc_wqe wqe;
  14671. spin_lock_irqsave(&phba->hbalock, iflags);
  14672. if (pring->txq_cnt > pring->txq_max)
  14673. pring->txq_max = pring->txq_cnt;
  14674. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14675. while (pring->txq_cnt) {
  14676. spin_lock_irqsave(&phba->hbalock, iflags);
  14677. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14678. if (!piocbq) {
  14679. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14680. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14681. "2823 txq empty and txq_cnt is %d\n ",
  14682. pring->txq_cnt);
  14683. break;
  14684. }
  14685. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14686. if (!sglq) {
  14687. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14688. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14689. break;
  14690. }
  14691. /* The xri and iocb resources secured,
  14692. * attempt to issue request
  14693. */
  14694. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14695. piocbq->sli4_xritag = sglq->sli4_xritag;
  14696. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14697. fail_msg = "to convert bpl to sgl";
  14698. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14699. fail_msg = "to convert iocb to wqe";
  14700. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14701. fail_msg = " - Wq is full";
  14702. else
  14703. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14704. if (fail_msg) {
  14705. /* Failed means we can't issue and need to cancel */
  14706. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14707. "2822 IOCB failed %s iotag 0x%x "
  14708. "xri 0x%x\n",
  14709. fail_msg,
  14710. piocbq->iotag, piocbq->sli4_xritag);
  14711. list_add_tail(&piocbq->list, &completions);
  14712. }
  14713. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14714. }
  14715. /* Cancel all the IOCBs that cannot be issued */
  14716. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14717. IOERR_SLI_ABORTED);
  14718. return pring->txq_cnt;
  14719. }