lpfc_sli.c 491 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static IOCB_t *
  65. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  66. {
  67. return &iocbq->iocb;
  68. }
  69. /**
  70. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  71. * @q: The Work Queue to operate on.
  72. * @wqe: The work Queue Entry to put on the Work queue.
  73. *
  74. * This routine will copy the contents of @wqe to the next available entry on
  75. * the @q. This function will then ring the Work Queue Doorbell to signal the
  76. * HBA to start processing the Work Queue Entry. This function returns 0 if
  77. * successful. If no entries are available on @q then this function will return
  78. * -ENOMEM.
  79. * The caller is expected to hold the hbalock when calling this routine.
  80. **/
  81. static uint32_t
  82. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  83. {
  84. union lpfc_wqe *temp_wqe;
  85. struct lpfc_register doorbell;
  86. uint32_t host_index;
  87. /* sanity check on queue memory */
  88. if (unlikely(!q))
  89. return -ENOMEM;
  90. temp_wqe = q->qe[q->host_index].wqe;
  91. /* If the host has not yet processed the next entry then we are done */
  92. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  93. return -ENOMEM;
  94. /* set consumption flag every once in a while */
  95. if (!((q->host_index + 1) % q->entry_repost))
  96. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  97. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  98. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  99. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  100. /* Update the host index before invoking device */
  101. host_index = q->host_index;
  102. q->host_index = ((q->host_index + 1) % q->entry_count);
  103. /* Ring Doorbell */
  104. doorbell.word0 = 0;
  105. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  106. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  107. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  108. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  109. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  110. return 0;
  111. }
  112. /**
  113. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  114. * @q: The Work Queue to operate on.
  115. * @index: The index to advance the hba index to.
  116. *
  117. * This routine will update the HBA index of a queue to reflect consumption of
  118. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  119. * an entry the host calls this function to update the queue's internal
  120. * pointers. This routine returns the number of entries that were consumed by
  121. * the HBA.
  122. **/
  123. static uint32_t
  124. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  125. {
  126. uint32_t released = 0;
  127. /* sanity check on queue memory */
  128. if (unlikely(!q))
  129. return 0;
  130. if (q->hba_index == index)
  131. return 0;
  132. do {
  133. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  134. released++;
  135. } while (q->hba_index != index);
  136. return released;
  137. }
  138. /**
  139. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  140. * @q: The Mailbox Queue to operate on.
  141. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  142. *
  143. * This routine will copy the contents of @mqe to the next available entry on
  144. * the @q. This function will then ring the Work Queue Doorbell to signal the
  145. * HBA to start processing the Work Queue Entry. This function returns 0 if
  146. * successful. If no entries are available on @q then this function will return
  147. * -ENOMEM.
  148. * The caller is expected to hold the hbalock when calling this routine.
  149. **/
  150. static uint32_t
  151. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  152. {
  153. struct lpfc_mqe *temp_mqe;
  154. struct lpfc_register doorbell;
  155. uint32_t host_index;
  156. /* sanity check on queue memory */
  157. if (unlikely(!q))
  158. return -ENOMEM;
  159. temp_mqe = q->qe[q->host_index].mqe;
  160. /* If the host has not yet processed the next entry then we are done */
  161. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  162. return -ENOMEM;
  163. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  164. /* Save off the mailbox pointer for completion */
  165. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  166. /* Update the host index before invoking device */
  167. host_index = q->host_index;
  168. q->host_index = ((q->host_index + 1) % q->entry_count);
  169. /* Ring Doorbell */
  170. doorbell.word0 = 0;
  171. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  172. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  173. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  174. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  175. return 0;
  176. }
  177. /**
  178. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  179. * @q: The Mailbox Queue to operate on.
  180. *
  181. * This routine will update the HBA index of a queue to reflect consumption of
  182. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  183. * an entry the host calls this function to update the queue's internal
  184. * pointers. This routine returns the number of entries that were consumed by
  185. * the HBA.
  186. **/
  187. static uint32_t
  188. lpfc_sli4_mq_release(struct lpfc_queue *q)
  189. {
  190. /* sanity check on queue memory */
  191. if (unlikely(!q))
  192. return 0;
  193. /* Clear the mailbox pointer for completion */
  194. q->phba->mbox = NULL;
  195. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  196. return 1;
  197. }
  198. /**
  199. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  200. * @q: The Event Queue to get the first valid EQE from
  201. *
  202. * This routine will get the first valid Event Queue Entry from @q, update
  203. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  204. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  205. * processed, but not popped back to the HBA then this routine will return NULL.
  206. **/
  207. static struct lpfc_eqe *
  208. lpfc_sli4_eq_get(struct lpfc_queue *q)
  209. {
  210. struct lpfc_eqe *eqe;
  211. /* sanity check on queue memory */
  212. if (unlikely(!q))
  213. return NULL;
  214. eqe = q->qe[q->hba_index].eqe;
  215. /* If the next EQE is not valid then we are done */
  216. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  217. return NULL;
  218. /* If the host has not yet processed the next entry then we are done */
  219. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  220. return NULL;
  221. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  222. return eqe;
  223. }
  224. /**
  225. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  226. * @q: The Event Queue that the host has completed processing for.
  227. * @arm: Indicates whether the host wants to arms this CQ.
  228. *
  229. * This routine will mark all Event Queue Entries on @q, from the last
  230. * known completed entry to the last entry that was processed, as completed
  231. * by clearing the valid bit for each completion queue entry. Then it will
  232. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  233. * The internal host index in the @q will be updated by this routine to indicate
  234. * that the host has finished processing the entries. The @arm parameter
  235. * indicates that the queue should be rearmed when ringing the doorbell.
  236. *
  237. * This function will return the number of EQEs that were popped.
  238. **/
  239. uint32_t
  240. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  241. {
  242. uint32_t released = 0;
  243. struct lpfc_eqe *temp_eqe;
  244. struct lpfc_register doorbell;
  245. /* sanity check on queue memory */
  246. if (unlikely(!q))
  247. return 0;
  248. /* while there are valid entries */
  249. while (q->hba_index != q->host_index) {
  250. temp_eqe = q->qe[q->host_index].eqe;
  251. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  252. released++;
  253. q->host_index = ((q->host_index + 1) % q->entry_count);
  254. }
  255. if (unlikely(released == 0 && !arm))
  256. return 0;
  257. /* ring doorbell for number popped */
  258. doorbell.word0 = 0;
  259. if (arm) {
  260. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  261. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  262. }
  263. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  264. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  265. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  266. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  267. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  268. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  269. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  270. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  271. readl(q->phba->sli4_hba.EQCQDBregaddr);
  272. return released;
  273. }
  274. /**
  275. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  276. * @q: The Completion Queue to get the first valid CQE from
  277. *
  278. * This routine will get the first valid Completion Queue Entry from @q, update
  279. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  280. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  281. * processed, but not popped back to the HBA then this routine will return NULL.
  282. **/
  283. static struct lpfc_cqe *
  284. lpfc_sli4_cq_get(struct lpfc_queue *q)
  285. {
  286. struct lpfc_cqe *cqe;
  287. /* sanity check on queue memory */
  288. if (unlikely(!q))
  289. return NULL;
  290. /* If the next CQE is not valid then we are done */
  291. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  292. return NULL;
  293. /* If the host has not yet processed the next entry then we are done */
  294. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  295. return NULL;
  296. cqe = q->qe[q->hba_index].cqe;
  297. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  298. return cqe;
  299. }
  300. /**
  301. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  302. * @q: The Completion Queue that the host has completed processing for.
  303. * @arm: Indicates whether the host wants to arms this CQ.
  304. *
  305. * This routine will mark all Completion queue entries on @q, from the last
  306. * known completed entry to the last entry that was processed, as completed
  307. * by clearing the valid bit for each completion queue entry. Then it will
  308. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  309. * The internal host index in the @q will be updated by this routine to indicate
  310. * that the host has finished processing the entries. The @arm parameter
  311. * indicates that the queue should be rearmed when ringing the doorbell.
  312. *
  313. * This function will return the number of CQEs that were released.
  314. **/
  315. uint32_t
  316. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  317. {
  318. uint32_t released = 0;
  319. struct lpfc_cqe *temp_qe;
  320. struct lpfc_register doorbell;
  321. /* sanity check on queue memory */
  322. if (unlikely(!q))
  323. return 0;
  324. /* while there are valid entries */
  325. while (q->hba_index != q->host_index) {
  326. temp_qe = q->qe[q->host_index].cqe;
  327. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  328. released++;
  329. q->host_index = ((q->host_index + 1) % q->entry_count);
  330. }
  331. if (unlikely(released == 0 && !arm))
  332. return 0;
  333. /* ring doorbell for number popped */
  334. doorbell.word0 = 0;
  335. if (arm)
  336. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  337. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  338. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  339. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  340. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  341. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  342. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  343. return released;
  344. }
  345. /**
  346. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  347. * @q: The Header Receive Queue to operate on.
  348. * @wqe: The Receive Queue Entry to put on the Receive queue.
  349. *
  350. * This routine will copy the contents of @wqe to the next available entry on
  351. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  352. * HBA to start processing the Receive Queue Entry. This function returns the
  353. * index that the rqe was copied to if successful. If no entries are available
  354. * on @q then this function will return -ENOMEM.
  355. * The caller is expected to hold the hbalock when calling this routine.
  356. **/
  357. static int
  358. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  359. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  360. {
  361. struct lpfc_rqe *temp_hrqe;
  362. struct lpfc_rqe *temp_drqe;
  363. struct lpfc_register doorbell;
  364. int put_index = hq->host_index;
  365. /* sanity check on queue memory */
  366. if (unlikely(!hq) || unlikely(!dq))
  367. return -ENOMEM;
  368. temp_hrqe = hq->qe[hq->host_index].rqe;
  369. temp_drqe = dq->qe[dq->host_index].rqe;
  370. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  371. return -EINVAL;
  372. if (hq->host_index != dq->host_index)
  373. return -EINVAL;
  374. /* If the host has not yet processed the next entry then we are done */
  375. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  376. return -EBUSY;
  377. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  378. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  379. /* Update the host index to point to the next slot */
  380. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  381. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  382. /* Ring The Header Receive Queue Doorbell */
  383. if (!(hq->host_index % hq->entry_repost)) {
  384. doorbell.word0 = 0;
  385. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  386. hq->entry_repost);
  387. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  388. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  389. }
  390. return put_index;
  391. }
  392. /**
  393. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  394. * @q: The Header Receive Queue to operate on.
  395. *
  396. * This routine will update the HBA index of a queue to reflect consumption of
  397. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  398. * consumed an entry the host calls this function to update the queue's
  399. * internal pointers. This routine returns the number of entries that were
  400. * consumed by the HBA.
  401. **/
  402. static uint32_t
  403. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  404. {
  405. /* sanity check on queue memory */
  406. if (unlikely(!hq) || unlikely(!dq))
  407. return 0;
  408. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  409. return 0;
  410. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  411. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  412. return 1;
  413. }
  414. /**
  415. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  416. * @phba: Pointer to HBA context object.
  417. * @pring: Pointer to driver SLI ring object.
  418. *
  419. * This function returns pointer to next command iocb entry
  420. * in the command ring. The caller must hold hbalock to prevent
  421. * other threads consume the next command iocb.
  422. * SLI-2/SLI-3 provide different sized iocbs.
  423. **/
  424. static inline IOCB_t *
  425. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  426. {
  427. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  428. pring->cmdidx * phba->iocb_cmd_size);
  429. }
  430. /**
  431. * lpfc_resp_iocb - Get next response iocb entry in the ring
  432. * @phba: Pointer to HBA context object.
  433. * @pring: Pointer to driver SLI ring object.
  434. *
  435. * This function returns pointer to next response iocb entry
  436. * in the response ring. The caller must hold hbalock to make sure
  437. * that no other thread consume the next response iocb.
  438. * SLI-2/SLI-3 provide different sized iocbs.
  439. **/
  440. static inline IOCB_t *
  441. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  442. {
  443. return (IOCB_t *) (((char *) pring->rspringaddr) +
  444. pring->rspidx * phba->iocb_rsp_size);
  445. }
  446. /**
  447. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  448. * @phba: Pointer to HBA context object.
  449. *
  450. * This function is called with hbalock held. This function
  451. * allocates a new driver iocb object from the iocb pool. If the
  452. * allocation is successful, it returns pointer to the newly
  453. * allocated iocb object else it returns NULL.
  454. **/
  455. static struct lpfc_iocbq *
  456. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  457. {
  458. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  459. struct lpfc_iocbq * iocbq = NULL;
  460. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  461. if (iocbq)
  462. phba->iocb_cnt++;
  463. if (phba->iocb_cnt > phba->iocb_max)
  464. phba->iocb_max = phba->iocb_cnt;
  465. return iocbq;
  466. }
  467. /**
  468. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  469. * @phba: Pointer to HBA context object.
  470. * @xritag: XRI value.
  471. *
  472. * This function clears the sglq pointer from the array of acive
  473. * sglq's. The xritag that is passed in is used to index into the
  474. * array. Before the xritag can be used it needs to be adjusted
  475. * by subtracting the xribase.
  476. *
  477. * Returns sglq ponter = success, NULL = Failure.
  478. **/
  479. static struct lpfc_sglq *
  480. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  481. {
  482. struct lpfc_sglq *sglq;
  483. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  484. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  485. return sglq;
  486. }
  487. /**
  488. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  489. * @phba: Pointer to HBA context object.
  490. * @xritag: XRI value.
  491. *
  492. * This function returns the sglq pointer from the array of acive
  493. * sglq's. The xritag that is passed in is used to index into the
  494. * array. Before the xritag can be used it needs to be adjusted
  495. * by subtracting the xribase.
  496. *
  497. * Returns sglq ponter = success, NULL = Failure.
  498. **/
  499. struct lpfc_sglq *
  500. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  501. {
  502. struct lpfc_sglq *sglq;
  503. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  504. return sglq;
  505. }
  506. /**
  507. * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
  508. * @phba: Pointer to HBA context object.
  509. * @ndlp: nodelist pointer for this target.
  510. * @xritag: xri used in this exchange.
  511. * @rxid: Remote Exchange ID.
  512. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  513. *
  514. * This function is called with hbalock held.
  515. * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
  516. * rrq struct and adds it to the active_rrq_list.
  517. *
  518. * returns 0 for rrq slot for this xri
  519. * < 0 Were not able to get rrq mem or invalid parameter.
  520. **/
  521. static int
  522. __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  523. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  524. {
  525. struct lpfc_node_rrq *rrq;
  526. int empty;
  527. uint32_t did = 0;
  528. if (!ndlp)
  529. return -EINVAL;
  530. if (!phba->cfg_enable_rrq)
  531. return -EINVAL;
  532. if (phba->pport->load_flag & FC_UNLOADING) {
  533. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  534. goto out;
  535. }
  536. did = ndlp->nlp_DID;
  537. /*
  538. * set the active bit even if there is no mem available.
  539. */
  540. if (NLP_CHK_FREE_REQ(ndlp))
  541. goto out;
  542. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  543. goto out;
  544. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  545. goto out;
  546. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  547. if (rrq) {
  548. rrq->send_rrq = send_rrq;
  549. rrq->xritag = xritag;
  550. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  551. rrq->ndlp = ndlp;
  552. rrq->nlp_DID = ndlp->nlp_DID;
  553. rrq->vport = ndlp->vport;
  554. rrq->rxid = rxid;
  555. empty = list_empty(&phba->active_rrq_list);
  556. rrq->send_rrq = send_rrq;
  557. list_add_tail(&rrq->list, &phba->active_rrq_list);
  558. if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
  559. phba->hba_flag |= HBA_RRQ_ACTIVE;
  560. if (empty)
  561. lpfc_worker_wake_up(phba);
  562. }
  563. return 0;
  564. }
  565. out:
  566. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  567. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  568. " DID:0x%x Send:%d\n",
  569. xritag, rxid, did, send_rrq);
  570. return -EINVAL;
  571. }
  572. /**
  573. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  574. * @phba: Pointer to HBA context object.
  575. * @xritag: xri used in this exchange.
  576. * @rrq: The RRQ to be cleared.
  577. *
  578. **/
  579. void
  580. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  581. uint16_t xritag,
  582. struct lpfc_node_rrq *rrq)
  583. {
  584. struct lpfc_nodelist *ndlp = NULL;
  585. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  586. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  587. /* The target DID could have been swapped (cable swap)
  588. * we should use the ndlp from the findnode if it is
  589. * available.
  590. */
  591. if ((!ndlp) && rrq->ndlp)
  592. ndlp = rrq->ndlp;
  593. if (!ndlp)
  594. goto out;
  595. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  596. rrq->send_rrq = 0;
  597. rrq->xritag = 0;
  598. rrq->rrq_stop_time = 0;
  599. }
  600. out:
  601. mempool_free(rrq, phba->rrq_pool);
  602. }
  603. /**
  604. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  605. * @phba: Pointer to HBA context object.
  606. *
  607. * This function is called with hbalock held. This function
  608. * Checks if stop_time (ratov from setting rrq active) has
  609. * been reached, if it has and the send_rrq flag is set then
  610. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  611. * then it will just call the routine to clear the rrq and
  612. * free the rrq resource.
  613. * The timer is set to the next rrq that is going to expire before
  614. * leaving the routine.
  615. *
  616. **/
  617. void
  618. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  619. {
  620. struct lpfc_node_rrq *rrq;
  621. struct lpfc_node_rrq *nextrrq;
  622. unsigned long next_time;
  623. unsigned long iflags;
  624. LIST_HEAD(send_rrq);
  625. spin_lock_irqsave(&phba->hbalock, iflags);
  626. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  627. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  628. list_for_each_entry_safe(rrq, nextrrq,
  629. &phba->active_rrq_list, list) {
  630. if (time_after(jiffies, rrq->rrq_stop_time))
  631. list_move(&rrq->list, &send_rrq);
  632. else if (time_before(rrq->rrq_stop_time, next_time))
  633. next_time = rrq->rrq_stop_time;
  634. }
  635. spin_unlock_irqrestore(&phba->hbalock, iflags);
  636. if (!list_empty(&phba->active_rrq_list))
  637. mod_timer(&phba->rrq_tmr, next_time);
  638. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  639. list_del(&rrq->list);
  640. if (!rrq->send_rrq)
  641. /* this call will free the rrq */
  642. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  643. else if (lpfc_send_rrq(phba, rrq)) {
  644. /* if we send the rrq then the completion handler
  645. * will clear the bit in the xribitmap.
  646. */
  647. lpfc_clr_rrq_active(phba, rrq->xritag,
  648. rrq);
  649. }
  650. }
  651. }
  652. /**
  653. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  654. * @vport: Pointer to vport context object.
  655. * @xri: The xri used in the exchange.
  656. * @did: The targets DID for this exchange.
  657. *
  658. * returns NULL = rrq not found in the phba->active_rrq_list.
  659. * rrq = rrq for this xri and target.
  660. **/
  661. struct lpfc_node_rrq *
  662. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  663. {
  664. struct lpfc_hba *phba = vport->phba;
  665. struct lpfc_node_rrq *rrq;
  666. struct lpfc_node_rrq *nextrrq;
  667. unsigned long iflags;
  668. if (phba->sli_rev != LPFC_SLI_REV4)
  669. return NULL;
  670. spin_lock_irqsave(&phba->hbalock, iflags);
  671. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  672. if (rrq->vport == vport && rrq->xritag == xri &&
  673. rrq->nlp_DID == did){
  674. list_del(&rrq->list);
  675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  676. return rrq;
  677. }
  678. }
  679. spin_unlock_irqrestore(&phba->hbalock, iflags);
  680. return NULL;
  681. }
  682. /**
  683. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  684. * @vport: Pointer to vport context object.
  685. * @ndlp: Pointer to the lpfc_node_list structure.
  686. * If ndlp is NULL Remove all active RRQs for this vport from the
  687. * phba->active_rrq_list and clear the rrq.
  688. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  689. **/
  690. void
  691. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  692. {
  693. struct lpfc_hba *phba = vport->phba;
  694. struct lpfc_node_rrq *rrq;
  695. struct lpfc_node_rrq *nextrrq;
  696. unsigned long iflags;
  697. LIST_HEAD(rrq_list);
  698. if (phba->sli_rev != LPFC_SLI_REV4)
  699. return;
  700. if (!ndlp) {
  701. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  702. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  703. }
  704. spin_lock_irqsave(&phba->hbalock, iflags);
  705. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  706. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  707. list_move(&rrq->list, &rrq_list);
  708. spin_unlock_irqrestore(&phba->hbalock, iflags);
  709. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  710. list_del(&rrq->list);
  711. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  712. }
  713. }
  714. /**
  715. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  716. * @phba: Pointer to HBA context object.
  717. *
  718. * Remove all rrqs from the phba->active_rrq_list and free them by
  719. * calling __lpfc_clr_active_rrq
  720. *
  721. **/
  722. void
  723. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  724. {
  725. struct lpfc_node_rrq *rrq;
  726. struct lpfc_node_rrq *nextrrq;
  727. unsigned long next_time;
  728. unsigned long iflags;
  729. LIST_HEAD(rrq_list);
  730. if (phba->sli_rev != LPFC_SLI_REV4)
  731. return;
  732. spin_lock_irqsave(&phba->hbalock, iflags);
  733. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  734. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  735. list_splice_init(&phba->active_rrq_list, &rrq_list);
  736. spin_unlock_irqrestore(&phba->hbalock, iflags);
  737. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  738. list_del(&rrq->list);
  739. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  740. }
  741. if (!list_empty(&phba->active_rrq_list))
  742. mod_timer(&phba->rrq_tmr, next_time);
  743. }
  744. /**
  745. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  746. * @phba: Pointer to HBA context object.
  747. * @ndlp: Targets nodelist pointer for this exchange.
  748. * @xritag the xri in the bitmap to test.
  749. *
  750. * This function is called with hbalock held. This function
  751. * returns 0 = rrq not active for this xri
  752. * 1 = rrq is valid for this xri.
  753. **/
  754. int
  755. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  756. uint16_t xritag)
  757. {
  758. if (!ndlp)
  759. return 0;
  760. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  761. return 1;
  762. else
  763. return 0;
  764. }
  765. /**
  766. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  767. * @phba: Pointer to HBA context object.
  768. * @ndlp: nodelist pointer for this target.
  769. * @xritag: xri used in this exchange.
  770. * @rxid: Remote Exchange ID.
  771. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  772. *
  773. * This function takes the hbalock.
  774. * The active bit is always set in the active rrq xri_bitmap even
  775. * if there is no slot avaiable for the other rrq information.
  776. *
  777. * returns 0 rrq actived for this xri
  778. * < 0 No memory or invalid ndlp.
  779. **/
  780. int
  781. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  782. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  783. {
  784. int ret;
  785. unsigned long iflags;
  786. spin_lock_irqsave(&phba->hbalock, iflags);
  787. ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
  788. spin_unlock_irqrestore(&phba->hbalock, iflags);
  789. return ret;
  790. }
  791. /**
  792. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  793. * @phba: Pointer to HBA context object.
  794. * @piocb: Pointer to the iocbq.
  795. *
  796. * This function is called with hbalock held. This function
  797. * gets a new driver sglq object from the sglq list. If the
  798. * list is not empty then it is successful, it returns pointer to the newly
  799. * allocated sglq object else it returns NULL.
  800. **/
  801. static struct lpfc_sglq *
  802. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  803. {
  804. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  805. struct lpfc_sglq *sglq = NULL;
  806. struct lpfc_sglq *start_sglq = NULL;
  807. struct lpfc_scsi_buf *lpfc_cmd;
  808. struct lpfc_nodelist *ndlp;
  809. int found = 0;
  810. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  811. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  812. ndlp = lpfc_cmd->rdata->pnode;
  813. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  814. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  815. ndlp = piocbq->context_un.ndlp;
  816. else
  817. ndlp = piocbq->context1;
  818. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  819. start_sglq = sglq;
  820. while (!found) {
  821. if (!sglq)
  822. return NULL;
  823. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
  824. /* This xri has an rrq outstanding for this DID.
  825. * put it back in the list and get another xri.
  826. */
  827. list_add_tail(&sglq->list, lpfc_sgl_list);
  828. sglq = NULL;
  829. list_remove_head(lpfc_sgl_list, sglq,
  830. struct lpfc_sglq, list);
  831. if (sglq == start_sglq) {
  832. sglq = NULL;
  833. break;
  834. } else
  835. continue;
  836. }
  837. sglq->ndlp = ndlp;
  838. found = 1;
  839. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  840. sglq->state = SGL_ALLOCATED;
  841. }
  842. return sglq;
  843. }
  844. /**
  845. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  846. * @phba: Pointer to HBA context object.
  847. *
  848. * This function is called with no lock held. This function
  849. * allocates a new driver iocb object from the iocb pool. If the
  850. * allocation is successful, it returns pointer to the newly
  851. * allocated iocb object else it returns NULL.
  852. **/
  853. struct lpfc_iocbq *
  854. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  855. {
  856. struct lpfc_iocbq * iocbq = NULL;
  857. unsigned long iflags;
  858. spin_lock_irqsave(&phba->hbalock, iflags);
  859. iocbq = __lpfc_sli_get_iocbq(phba);
  860. spin_unlock_irqrestore(&phba->hbalock, iflags);
  861. return iocbq;
  862. }
  863. /**
  864. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  865. * @phba: Pointer to HBA context object.
  866. * @iocbq: Pointer to driver iocb object.
  867. *
  868. * This function is called with hbalock held to release driver
  869. * iocb object to the iocb pool. The iotag in the iocb object
  870. * does not change for each use of the iocb object. This function
  871. * clears all other fields of the iocb object when it is freed.
  872. * The sqlq structure that holds the xritag and phys and virtual
  873. * mappings for the scatter gather list is retrieved from the
  874. * active array of sglq. The get of the sglq pointer also clears
  875. * the entry in the array. If the status of the IO indiactes that
  876. * this IO was aborted then the sglq entry it put on the
  877. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  878. * IO has good status or fails for any other reason then the sglq
  879. * entry is added to the free list (lpfc_sgl_list).
  880. **/
  881. static void
  882. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  883. {
  884. struct lpfc_sglq *sglq;
  885. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  886. unsigned long iflag = 0;
  887. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  888. if (iocbq->sli4_xritag == NO_XRI)
  889. sglq = NULL;
  890. else
  891. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  892. if (sglq) {
  893. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  894. (sglq->state != SGL_XRI_ABORTED)) {
  895. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  896. iflag);
  897. list_add(&sglq->list,
  898. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  899. spin_unlock_irqrestore(
  900. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  901. } else {
  902. sglq->state = SGL_FREED;
  903. sglq->ndlp = NULL;
  904. list_add_tail(&sglq->list,
  905. &phba->sli4_hba.lpfc_sgl_list);
  906. /* Check if TXQ queue needs to be serviced */
  907. if (pring->txq_cnt)
  908. lpfc_worker_wake_up(phba);
  909. }
  910. }
  911. /*
  912. * Clean all volatile data fields, preserve iotag and node struct.
  913. */
  914. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  915. iocbq->sli4_lxritag = NO_XRI;
  916. iocbq->sli4_xritag = NO_XRI;
  917. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  918. }
  919. /**
  920. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  921. * @phba: Pointer to HBA context object.
  922. * @iocbq: Pointer to driver iocb object.
  923. *
  924. * This function is called with hbalock held to release driver
  925. * iocb object to the iocb pool. The iotag in the iocb object
  926. * does not change for each use of the iocb object. This function
  927. * clears all other fields of the iocb object when it is freed.
  928. **/
  929. static void
  930. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  931. {
  932. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  933. /*
  934. * Clean all volatile data fields, preserve iotag and node struct.
  935. */
  936. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  937. iocbq->sli4_xritag = NO_XRI;
  938. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  939. }
  940. /**
  941. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  942. * @phba: Pointer to HBA context object.
  943. * @iocbq: Pointer to driver iocb object.
  944. *
  945. * This function is called with hbalock held to release driver
  946. * iocb object to the iocb pool. The iotag in the iocb object
  947. * does not change for each use of the iocb object. This function
  948. * clears all other fields of the iocb object when it is freed.
  949. **/
  950. static void
  951. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  952. {
  953. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  954. phba->iocb_cnt--;
  955. }
  956. /**
  957. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  958. * @phba: Pointer to HBA context object.
  959. * @iocbq: Pointer to driver iocb object.
  960. *
  961. * This function is called with no lock held to release the iocb to
  962. * iocb pool.
  963. **/
  964. void
  965. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  966. {
  967. unsigned long iflags;
  968. /*
  969. * Clean all volatile data fields, preserve iotag and node struct.
  970. */
  971. spin_lock_irqsave(&phba->hbalock, iflags);
  972. __lpfc_sli_release_iocbq(phba, iocbq);
  973. spin_unlock_irqrestore(&phba->hbalock, iflags);
  974. }
  975. /**
  976. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  977. * @phba: Pointer to HBA context object.
  978. * @iocblist: List of IOCBs.
  979. * @ulpstatus: ULP status in IOCB command field.
  980. * @ulpWord4: ULP word-4 in IOCB command field.
  981. *
  982. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  983. * on the list by invoking the complete callback function associated with the
  984. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  985. * fields.
  986. **/
  987. void
  988. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  989. uint32_t ulpstatus, uint32_t ulpWord4)
  990. {
  991. struct lpfc_iocbq *piocb;
  992. while (!list_empty(iocblist)) {
  993. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  994. if (!piocb->iocb_cmpl)
  995. lpfc_sli_release_iocbq(phba, piocb);
  996. else {
  997. piocb->iocb.ulpStatus = ulpstatus;
  998. piocb->iocb.un.ulpWord[4] = ulpWord4;
  999. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1000. }
  1001. }
  1002. return;
  1003. }
  1004. /**
  1005. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1006. * @iocb_cmnd: iocb command code.
  1007. *
  1008. * This function is called by ring event handler function to get the iocb type.
  1009. * This function translates the iocb command to an iocb command type used to
  1010. * decide the final disposition of each completed IOCB.
  1011. * The function returns
  1012. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1013. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1014. * LPFC_ABORT_IOCB if it is an abort iocb
  1015. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1016. *
  1017. * The caller is not required to hold any lock.
  1018. **/
  1019. static lpfc_iocb_type
  1020. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1021. {
  1022. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1023. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1024. return 0;
  1025. switch (iocb_cmnd) {
  1026. case CMD_XMIT_SEQUENCE_CR:
  1027. case CMD_XMIT_SEQUENCE_CX:
  1028. case CMD_XMIT_BCAST_CN:
  1029. case CMD_XMIT_BCAST_CX:
  1030. case CMD_ELS_REQUEST_CR:
  1031. case CMD_ELS_REQUEST_CX:
  1032. case CMD_CREATE_XRI_CR:
  1033. case CMD_CREATE_XRI_CX:
  1034. case CMD_GET_RPI_CN:
  1035. case CMD_XMIT_ELS_RSP_CX:
  1036. case CMD_GET_RPI_CR:
  1037. case CMD_FCP_IWRITE_CR:
  1038. case CMD_FCP_IWRITE_CX:
  1039. case CMD_FCP_IREAD_CR:
  1040. case CMD_FCP_IREAD_CX:
  1041. case CMD_FCP_ICMND_CR:
  1042. case CMD_FCP_ICMND_CX:
  1043. case CMD_FCP_TSEND_CX:
  1044. case CMD_FCP_TRSP_CX:
  1045. case CMD_FCP_TRECEIVE_CX:
  1046. case CMD_FCP_AUTO_TRSP_CX:
  1047. case CMD_ADAPTER_MSG:
  1048. case CMD_ADAPTER_DUMP:
  1049. case CMD_XMIT_SEQUENCE64_CR:
  1050. case CMD_XMIT_SEQUENCE64_CX:
  1051. case CMD_XMIT_BCAST64_CN:
  1052. case CMD_XMIT_BCAST64_CX:
  1053. case CMD_ELS_REQUEST64_CR:
  1054. case CMD_ELS_REQUEST64_CX:
  1055. case CMD_FCP_IWRITE64_CR:
  1056. case CMD_FCP_IWRITE64_CX:
  1057. case CMD_FCP_IREAD64_CR:
  1058. case CMD_FCP_IREAD64_CX:
  1059. case CMD_FCP_ICMND64_CR:
  1060. case CMD_FCP_ICMND64_CX:
  1061. case CMD_FCP_TSEND64_CX:
  1062. case CMD_FCP_TRSP64_CX:
  1063. case CMD_FCP_TRECEIVE64_CX:
  1064. case CMD_GEN_REQUEST64_CR:
  1065. case CMD_GEN_REQUEST64_CX:
  1066. case CMD_XMIT_ELS_RSP64_CX:
  1067. case DSSCMD_IWRITE64_CR:
  1068. case DSSCMD_IWRITE64_CX:
  1069. case DSSCMD_IREAD64_CR:
  1070. case DSSCMD_IREAD64_CX:
  1071. type = LPFC_SOL_IOCB;
  1072. break;
  1073. case CMD_ABORT_XRI_CN:
  1074. case CMD_ABORT_XRI_CX:
  1075. case CMD_CLOSE_XRI_CN:
  1076. case CMD_CLOSE_XRI_CX:
  1077. case CMD_XRI_ABORTED_CX:
  1078. case CMD_ABORT_MXRI64_CN:
  1079. case CMD_XMIT_BLS_RSP64_CX:
  1080. type = LPFC_ABORT_IOCB;
  1081. break;
  1082. case CMD_RCV_SEQUENCE_CX:
  1083. case CMD_RCV_ELS_REQ_CX:
  1084. case CMD_RCV_SEQUENCE64_CX:
  1085. case CMD_RCV_ELS_REQ64_CX:
  1086. case CMD_ASYNC_STATUS:
  1087. case CMD_IOCB_RCV_SEQ64_CX:
  1088. case CMD_IOCB_RCV_ELS64_CX:
  1089. case CMD_IOCB_RCV_CONT64_CX:
  1090. case CMD_IOCB_RET_XRI64_CX:
  1091. type = LPFC_UNSOL_IOCB;
  1092. break;
  1093. case CMD_IOCB_XMIT_MSEQ64_CR:
  1094. case CMD_IOCB_XMIT_MSEQ64_CX:
  1095. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1096. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1097. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1098. case CMD_IOCB_ABORT_EXTENDED_CN:
  1099. case CMD_IOCB_RET_HBQE64_CN:
  1100. case CMD_IOCB_FCP_IBIDIR64_CR:
  1101. case CMD_IOCB_FCP_IBIDIR64_CX:
  1102. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1103. case CMD_IOCB_LOGENTRY_CN:
  1104. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1105. printk("%s - Unhandled SLI-3 Command x%x\n",
  1106. __func__, iocb_cmnd);
  1107. type = LPFC_UNKNOWN_IOCB;
  1108. break;
  1109. default:
  1110. type = LPFC_UNKNOWN_IOCB;
  1111. break;
  1112. }
  1113. return type;
  1114. }
  1115. /**
  1116. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1117. * @phba: Pointer to HBA context object.
  1118. *
  1119. * This function is called from SLI initialization code
  1120. * to configure every ring of the HBA's SLI interface. The
  1121. * caller is not required to hold any lock. This function issues
  1122. * a config_ring mailbox command for each ring.
  1123. * This function returns zero if successful else returns a negative
  1124. * error code.
  1125. **/
  1126. static int
  1127. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1128. {
  1129. struct lpfc_sli *psli = &phba->sli;
  1130. LPFC_MBOXQ_t *pmb;
  1131. MAILBOX_t *pmbox;
  1132. int i, rc, ret = 0;
  1133. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1134. if (!pmb)
  1135. return -ENOMEM;
  1136. pmbox = &pmb->u.mb;
  1137. phba->link_state = LPFC_INIT_MBX_CMDS;
  1138. for (i = 0; i < psli->num_rings; i++) {
  1139. lpfc_config_ring(phba, i, pmb);
  1140. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1141. if (rc != MBX_SUCCESS) {
  1142. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1143. "0446 Adapter failed to init (%d), "
  1144. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1145. "ring %d\n",
  1146. rc, pmbox->mbxCommand,
  1147. pmbox->mbxStatus, i);
  1148. phba->link_state = LPFC_HBA_ERROR;
  1149. ret = -ENXIO;
  1150. break;
  1151. }
  1152. }
  1153. mempool_free(pmb, phba->mbox_mem_pool);
  1154. return ret;
  1155. }
  1156. /**
  1157. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1158. * @phba: Pointer to HBA context object.
  1159. * @pring: Pointer to driver SLI ring object.
  1160. * @piocb: Pointer to the driver iocb object.
  1161. *
  1162. * This function is called with hbalock held. The function adds the
  1163. * new iocb to txcmplq of the given ring. This function always returns
  1164. * 0. If this function is called for ELS ring, this function checks if
  1165. * there is a vport associated with the ELS command. This function also
  1166. * starts els_tmofunc timer if this is an ELS command.
  1167. **/
  1168. static int
  1169. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1170. struct lpfc_iocbq *piocb)
  1171. {
  1172. list_add_tail(&piocb->list, &pring->txcmplq);
  1173. piocb->iocb_flag |= LPFC_IO_ON_Q;
  1174. pring->txcmplq_cnt++;
  1175. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1176. pring->txcmplq_max = pring->txcmplq_cnt;
  1177. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1178. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1179. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1180. if (!piocb->vport)
  1181. BUG();
  1182. else
  1183. mod_timer(&piocb->vport->els_tmofunc,
  1184. jiffies + HZ * (phba->fc_ratov << 1));
  1185. }
  1186. return 0;
  1187. }
  1188. /**
  1189. * lpfc_sli_ringtx_get - Get first element of the txq
  1190. * @phba: Pointer to HBA context object.
  1191. * @pring: Pointer to driver SLI ring object.
  1192. *
  1193. * This function is called with hbalock held to get next
  1194. * iocb in txq of the given ring. If there is any iocb in
  1195. * the txq, the function returns first iocb in the list after
  1196. * removing the iocb from the list, else it returns NULL.
  1197. **/
  1198. struct lpfc_iocbq *
  1199. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1200. {
  1201. struct lpfc_iocbq *cmd_iocb;
  1202. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1203. if (cmd_iocb != NULL)
  1204. pring->txq_cnt--;
  1205. return cmd_iocb;
  1206. }
  1207. /**
  1208. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1209. * @phba: Pointer to HBA context object.
  1210. * @pring: Pointer to driver SLI ring object.
  1211. *
  1212. * This function is called with hbalock held and the caller must post the
  1213. * iocb without releasing the lock. If the caller releases the lock,
  1214. * iocb slot returned by the function is not guaranteed to be available.
  1215. * The function returns pointer to the next available iocb slot if there
  1216. * is available slot in the ring, else it returns NULL.
  1217. * If the get index of the ring is ahead of the put index, the function
  1218. * will post an error attention event to the worker thread to take the
  1219. * HBA to offline state.
  1220. **/
  1221. static IOCB_t *
  1222. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1223. {
  1224. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1225. uint32_t max_cmd_idx = pring->numCiocb;
  1226. if ((pring->next_cmdidx == pring->cmdidx) &&
  1227. (++pring->next_cmdidx >= max_cmd_idx))
  1228. pring->next_cmdidx = 0;
  1229. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1230. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1231. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1232. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1233. "0315 Ring %d issue: portCmdGet %d "
  1234. "is bigger than cmd ring %d\n",
  1235. pring->ringno,
  1236. pring->local_getidx, max_cmd_idx);
  1237. phba->link_state = LPFC_HBA_ERROR;
  1238. /*
  1239. * All error attention handlers are posted to
  1240. * worker thread
  1241. */
  1242. phba->work_ha |= HA_ERATT;
  1243. phba->work_hs = HS_FFER3;
  1244. lpfc_worker_wake_up(phba);
  1245. return NULL;
  1246. }
  1247. if (pring->local_getidx == pring->next_cmdidx)
  1248. return NULL;
  1249. }
  1250. return lpfc_cmd_iocb(phba, pring);
  1251. }
  1252. /**
  1253. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1254. * @phba: Pointer to HBA context object.
  1255. * @iocbq: Pointer to driver iocb object.
  1256. *
  1257. * This function gets an iotag for the iocb. If there is no unused iotag and
  1258. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1259. * array and assigns a new iotag.
  1260. * The function returns the allocated iotag if successful, else returns zero.
  1261. * Zero is not a valid iotag.
  1262. * The caller is not required to hold any lock.
  1263. **/
  1264. uint16_t
  1265. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1266. {
  1267. struct lpfc_iocbq **new_arr;
  1268. struct lpfc_iocbq **old_arr;
  1269. size_t new_len;
  1270. struct lpfc_sli *psli = &phba->sli;
  1271. uint16_t iotag;
  1272. spin_lock_irq(&phba->hbalock);
  1273. iotag = psli->last_iotag;
  1274. if(++iotag < psli->iocbq_lookup_len) {
  1275. psli->last_iotag = iotag;
  1276. psli->iocbq_lookup[iotag] = iocbq;
  1277. spin_unlock_irq(&phba->hbalock);
  1278. iocbq->iotag = iotag;
  1279. return iotag;
  1280. } else if (psli->iocbq_lookup_len < (0xffff
  1281. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1282. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1283. spin_unlock_irq(&phba->hbalock);
  1284. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1285. GFP_KERNEL);
  1286. if (new_arr) {
  1287. spin_lock_irq(&phba->hbalock);
  1288. old_arr = psli->iocbq_lookup;
  1289. if (new_len <= psli->iocbq_lookup_len) {
  1290. /* highly unprobable case */
  1291. kfree(new_arr);
  1292. iotag = psli->last_iotag;
  1293. if(++iotag < psli->iocbq_lookup_len) {
  1294. psli->last_iotag = iotag;
  1295. psli->iocbq_lookup[iotag] = iocbq;
  1296. spin_unlock_irq(&phba->hbalock);
  1297. iocbq->iotag = iotag;
  1298. return iotag;
  1299. }
  1300. spin_unlock_irq(&phba->hbalock);
  1301. return 0;
  1302. }
  1303. if (psli->iocbq_lookup)
  1304. memcpy(new_arr, old_arr,
  1305. ((psli->last_iotag + 1) *
  1306. sizeof (struct lpfc_iocbq *)));
  1307. psli->iocbq_lookup = new_arr;
  1308. psli->iocbq_lookup_len = new_len;
  1309. psli->last_iotag = iotag;
  1310. psli->iocbq_lookup[iotag] = iocbq;
  1311. spin_unlock_irq(&phba->hbalock);
  1312. iocbq->iotag = iotag;
  1313. kfree(old_arr);
  1314. return iotag;
  1315. }
  1316. } else
  1317. spin_unlock_irq(&phba->hbalock);
  1318. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1319. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1320. psli->last_iotag);
  1321. return 0;
  1322. }
  1323. /**
  1324. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1325. * @phba: Pointer to HBA context object.
  1326. * @pring: Pointer to driver SLI ring object.
  1327. * @iocb: Pointer to iocb slot in the ring.
  1328. * @nextiocb: Pointer to driver iocb object which need to be
  1329. * posted to firmware.
  1330. *
  1331. * This function is called with hbalock held to post a new iocb to
  1332. * the firmware. This function copies the new iocb to ring iocb slot and
  1333. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1334. * a completion call back for this iocb else the function will free the
  1335. * iocb object.
  1336. **/
  1337. static void
  1338. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1339. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1340. {
  1341. /*
  1342. * Set up an iotag
  1343. */
  1344. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1345. if (pring->ringno == LPFC_ELS_RING) {
  1346. lpfc_debugfs_slow_ring_trc(phba,
  1347. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1348. *(((uint32_t *) &nextiocb->iocb) + 4),
  1349. *(((uint32_t *) &nextiocb->iocb) + 6),
  1350. *(((uint32_t *) &nextiocb->iocb) + 7));
  1351. }
  1352. /*
  1353. * Issue iocb command to adapter
  1354. */
  1355. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1356. wmb();
  1357. pring->stats.iocb_cmd++;
  1358. /*
  1359. * If there is no completion routine to call, we can release the
  1360. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1361. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1362. */
  1363. if (nextiocb->iocb_cmpl)
  1364. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1365. else
  1366. __lpfc_sli_release_iocbq(phba, nextiocb);
  1367. /*
  1368. * Let the HBA know what IOCB slot will be the next one the
  1369. * driver will put a command into.
  1370. */
  1371. pring->cmdidx = pring->next_cmdidx;
  1372. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1373. }
  1374. /**
  1375. * lpfc_sli_update_full_ring - Update the chip attention register
  1376. * @phba: Pointer to HBA context object.
  1377. * @pring: Pointer to driver SLI ring object.
  1378. *
  1379. * The caller is not required to hold any lock for calling this function.
  1380. * This function updates the chip attention bits for the ring to inform firmware
  1381. * that there are pending work to be done for this ring and requests an
  1382. * interrupt when there is space available in the ring. This function is
  1383. * called when the driver is unable to post more iocbs to the ring due
  1384. * to unavailability of space in the ring.
  1385. **/
  1386. static void
  1387. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1388. {
  1389. int ringno = pring->ringno;
  1390. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1391. wmb();
  1392. /*
  1393. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1394. * The HBA will tell us when an IOCB entry is available.
  1395. */
  1396. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1397. readl(phba->CAregaddr); /* flush */
  1398. pring->stats.iocb_cmd_full++;
  1399. }
  1400. /**
  1401. * lpfc_sli_update_ring - Update chip attention register
  1402. * @phba: Pointer to HBA context object.
  1403. * @pring: Pointer to driver SLI ring object.
  1404. *
  1405. * This function updates the chip attention register bit for the
  1406. * given ring to inform HBA that there is more work to be done
  1407. * in this ring. The caller is not required to hold any lock.
  1408. **/
  1409. static void
  1410. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1411. {
  1412. int ringno = pring->ringno;
  1413. /*
  1414. * Tell the HBA that there is work to do in this ring.
  1415. */
  1416. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1417. wmb();
  1418. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1419. readl(phba->CAregaddr); /* flush */
  1420. }
  1421. }
  1422. /**
  1423. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1424. * @phba: Pointer to HBA context object.
  1425. * @pring: Pointer to driver SLI ring object.
  1426. *
  1427. * This function is called with hbalock held to post pending iocbs
  1428. * in the txq to the firmware. This function is called when driver
  1429. * detects space available in the ring.
  1430. **/
  1431. static void
  1432. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1433. {
  1434. IOCB_t *iocb;
  1435. struct lpfc_iocbq *nextiocb;
  1436. /*
  1437. * Check to see if:
  1438. * (a) there is anything on the txq to send
  1439. * (b) link is up
  1440. * (c) link attention events can be processed (fcp ring only)
  1441. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1442. */
  1443. if (pring->txq_cnt &&
  1444. lpfc_is_link_up(phba) &&
  1445. (pring->ringno != phba->sli.fcp_ring ||
  1446. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1447. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1448. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1449. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1450. if (iocb)
  1451. lpfc_sli_update_ring(phba, pring);
  1452. else
  1453. lpfc_sli_update_full_ring(phba, pring);
  1454. }
  1455. return;
  1456. }
  1457. /**
  1458. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1459. * @phba: Pointer to HBA context object.
  1460. * @hbqno: HBQ number.
  1461. *
  1462. * This function is called with hbalock held to get the next
  1463. * available slot for the given HBQ. If there is free slot
  1464. * available for the HBQ it will return pointer to the next available
  1465. * HBQ entry else it will return NULL.
  1466. **/
  1467. static struct lpfc_hbq_entry *
  1468. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1469. {
  1470. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1471. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1472. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1473. hbqp->next_hbqPutIdx = 0;
  1474. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1475. uint32_t raw_index = phba->hbq_get[hbqno];
  1476. uint32_t getidx = le32_to_cpu(raw_index);
  1477. hbqp->local_hbqGetIdx = getidx;
  1478. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1479. lpfc_printf_log(phba, KERN_ERR,
  1480. LOG_SLI | LOG_VPORT,
  1481. "1802 HBQ %d: local_hbqGetIdx "
  1482. "%u is > than hbqp->entry_count %u\n",
  1483. hbqno, hbqp->local_hbqGetIdx,
  1484. hbqp->entry_count);
  1485. phba->link_state = LPFC_HBA_ERROR;
  1486. return NULL;
  1487. }
  1488. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1489. return NULL;
  1490. }
  1491. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1492. hbqp->hbqPutIdx;
  1493. }
  1494. /**
  1495. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1496. * @phba: Pointer to HBA context object.
  1497. *
  1498. * This function is called with no lock held to free all the
  1499. * hbq buffers while uninitializing the SLI interface. It also
  1500. * frees the HBQ buffers returned by the firmware but not yet
  1501. * processed by the upper layers.
  1502. **/
  1503. void
  1504. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1505. {
  1506. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1507. struct hbq_dmabuf *hbq_buf;
  1508. unsigned long flags;
  1509. int i, hbq_count;
  1510. uint32_t hbqno;
  1511. hbq_count = lpfc_sli_hbq_count();
  1512. /* Return all memory used by all HBQs */
  1513. spin_lock_irqsave(&phba->hbalock, flags);
  1514. for (i = 0; i < hbq_count; ++i) {
  1515. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1516. &phba->hbqs[i].hbq_buffer_list, list) {
  1517. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1518. list_del(&hbq_buf->dbuf.list);
  1519. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1520. }
  1521. phba->hbqs[i].buffer_count = 0;
  1522. }
  1523. /* Return all HBQ buffer that are in-fly */
  1524. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1525. list) {
  1526. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1527. list_del(&hbq_buf->dbuf.list);
  1528. if (hbq_buf->tag == -1) {
  1529. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1530. (phba, hbq_buf);
  1531. } else {
  1532. hbqno = hbq_buf->tag >> 16;
  1533. if (hbqno >= LPFC_MAX_HBQS)
  1534. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1535. (phba, hbq_buf);
  1536. else
  1537. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1538. hbq_buf);
  1539. }
  1540. }
  1541. /* Mark the HBQs not in use */
  1542. phba->hbq_in_use = 0;
  1543. spin_unlock_irqrestore(&phba->hbalock, flags);
  1544. }
  1545. /**
  1546. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1547. * @phba: Pointer to HBA context object.
  1548. * @hbqno: HBQ number.
  1549. * @hbq_buf: Pointer to HBQ buffer.
  1550. *
  1551. * This function is called with the hbalock held to post a
  1552. * hbq buffer to the firmware. If the function finds an empty
  1553. * slot in the HBQ, it will post the buffer. The function will return
  1554. * pointer to the hbq entry if it successfully post the buffer
  1555. * else it will return NULL.
  1556. **/
  1557. static int
  1558. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1559. struct hbq_dmabuf *hbq_buf)
  1560. {
  1561. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1562. }
  1563. /**
  1564. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1565. * @phba: Pointer to HBA context object.
  1566. * @hbqno: HBQ number.
  1567. * @hbq_buf: Pointer to HBQ buffer.
  1568. *
  1569. * This function is called with the hbalock held to post a hbq buffer to the
  1570. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1571. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1572. * it successfully post the buffer else it will return an error.
  1573. **/
  1574. static int
  1575. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1576. struct hbq_dmabuf *hbq_buf)
  1577. {
  1578. struct lpfc_hbq_entry *hbqe;
  1579. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1580. /* Get next HBQ entry slot to use */
  1581. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1582. if (hbqe) {
  1583. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1584. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1585. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1586. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1587. hbqe->bde.tus.f.bdeFlags = 0;
  1588. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1589. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1590. /* Sync SLIM */
  1591. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1592. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1593. /* flush */
  1594. readl(phba->hbq_put + hbqno);
  1595. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1596. return 0;
  1597. } else
  1598. return -ENOMEM;
  1599. }
  1600. /**
  1601. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1602. * @phba: Pointer to HBA context object.
  1603. * @hbqno: HBQ number.
  1604. * @hbq_buf: Pointer to HBQ buffer.
  1605. *
  1606. * This function is called with the hbalock held to post an RQE to the SLI4
  1607. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1608. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1609. **/
  1610. static int
  1611. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1612. struct hbq_dmabuf *hbq_buf)
  1613. {
  1614. int rc;
  1615. struct lpfc_rqe hrqe;
  1616. struct lpfc_rqe drqe;
  1617. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1618. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1619. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1620. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1621. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1622. &hrqe, &drqe);
  1623. if (rc < 0)
  1624. return rc;
  1625. hbq_buf->tag = rc;
  1626. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1627. return 0;
  1628. }
  1629. /* HBQ for ELS and CT traffic. */
  1630. static struct lpfc_hbq_init lpfc_els_hbq = {
  1631. .rn = 1,
  1632. .entry_count = 256,
  1633. .mask_count = 0,
  1634. .profile = 0,
  1635. .ring_mask = (1 << LPFC_ELS_RING),
  1636. .buffer_count = 0,
  1637. .init_count = 40,
  1638. .add_count = 40,
  1639. };
  1640. /* HBQ for the extra ring if needed */
  1641. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1642. .rn = 1,
  1643. .entry_count = 200,
  1644. .mask_count = 0,
  1645. .profile = 0,
  1646. .ring_mask = (1 << LPFC_EXTRA_RING),
  1647. .buffer_count = 0,
  1648. .init_count = 0,
  1649. .add_count = 5,
  1650. };
  1651. /* Array of HBQs */
  1652. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1653. &lpfc_els_hbq,
  1654. &lpfc_extra_hbq,
  1655. };
  1656. /**
  1657. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1658. * @phba: Pointer to HBA context object.
  1659. * @hbqno: HBQ number.
  1660. * @count: Number of HBQ buffers to be posted.
  1661. *
  1662. * This function is called with no lock held to post more hbq buffers to the
  1663. * given HBQ. The function returns the number of HBQ buffers successfully
  1664. * posted.
  1665. **/
  1666. static int
  1667. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1668. {
  1669. uint32_t i, posted = 0;
  1670. unsigned long flags;
  1671. struct hbq_dmabuf *hbq_buffer;
  1672. LIST_HEAD(hbq_buf_list);
  1673. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1674. return 0;
  1675. if ((phba->hbqs[hbqno].buffer_count + count) >
  1676. lpfc_hbq_defs[hbqno]->entry_count)
  1677. count = lpfc_hbq_defs[hbqno]->entry_count -
  1678. phba->hbqs[hbqno].buffer_count;
  1679. if (!count)
  1680. return 0;
  1681. /* Allocate HBQ entries */
  1682. for (i = 0; i < count; i++) {
  1683. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1684. if (!hbq_buffer)
  1685. break;
  1686. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1687. }
  1688. /* Check whether HBQ is still in use */
  1689. spin_lock_irqsave(&phba->hbalock, flags);
  1690. if (!phba->hbq_in_use)
  1691. goto err;
  1692. while (!list_empty(&hbq_buf_list)) {
  1693. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1694. dbuf.list);
  1695. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1696. (hbqno << 16));
  1697. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1698. phba->hbqs[hbqno].buffer_count++;
  1699. posted++;
  1700. } else
  1701. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1702. }
  1703. spin_unlock_irqrestore(&phba->hbalock, flags);
  1704. return posted;
  1705. err:
  1706. spin_unlock_irqrestore(&phba->hbalock, flags);
  1707. while (!list_empty(&hbq_buf_list)) {
  1708. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1709. dbuf.list);
  1710. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1711. }
  1712. return 0;
  1713. }
  1714. /**
  1715. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1716. * @phba: Pointer to HBA context object.
  1717. * @qno: HBQ number.
  1718. *
  1719. * This function posts more buffers to the HBQ. This function
  1720. * is called with no lock held. The function returns the number of HBQ entries
  1721. * successfully allocated.
  1722. **/
  1723. int
  1724. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1725. {
  1726. if (phba->sli_rev == LPFC_SLI_REV4)
  1727. return 0;
  1728. else
  1729. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1730. lpfc_hbq_defs[qno]->add_count);
  1731. }
  1732. /**
  1733. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1734. * @phba: Pointer to HBA context object.
  1735. * @qno: HBQ queue number.
  1736. *
  1737. * This function is called from SLI initialization code path with
  1738. * no lock held to post initial HBQ buffers to firmware. The
  1739. * function returns the number of HBQ entries successfully allocated.
  1740. **/
  1741. static int
  1742. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1743. {
  1744. if (phba->sli_rev == LPFC_SLI_REV4)
  1745. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1746. lpfc_hbq_defs[qno]->entry_count);
  1747. else
  1748. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1749. lpfc_hbq_defs[qno]->init_count);
  1750. }
  1751. /**
  1752. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1753. * @phba: Pointer to HBA context object.
  1754. * @hbqno: HBQ number.
  1755. *
  1756. * This function removes the first hbq buffer on an hbq list and returns a
  1757. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1758. **/
  1759. static struct hbq_dmabuf *
  1760. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1761. {
  1762. struct lpfc_dmabuf *d_buf;
  1763. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1764. if (!d_buf)
  1765. return NULL;
  1766. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1767. }
  1768. /**
  1769. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1770. * @phba: Pointer to HBA context object.
  1771. * @tag: Tag of the hbq buffer.
  1772. *
  1773. * This function is called with hbalock held. This function searches
  1774. * for the hbq buffer associated with the given tag in the hbq buffer
  1775. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1776. * it returns NULL.
  1777. **/
  1778. static struct hbq_dmabuf *
  1779. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1780. {
  1781. struct lpfc_dmabuf *d_buf;
  1782. struct hbq_dmabuf *hbq_buf;
  1783. uint32_t hbqno;
  1784. hbqno = tag >> 16;
  1785. if (hbqno >= LPFC_MAX_HBQS)
  1786. return NULL;
  1787. spin_lock_irq(&phba->hbalock);
  1788. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1789. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1790. if (hbq_buf->tag == tag) {
  1791. spin_unlock_irq(&phba->hbalock);
  1792. return hbq_buf;
  1793. }
  1794. }
  1795. spin_unlock_irq(&phba->hbalock);
  1796. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1797. "1803 Bad hbq tag. Data: x%x x%x\n",
  1798. tag, phba->hbqs[tag >> 16].buffer_count);
  1799. return NULL;
  1800. }
  1801. /**
  1802. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1803. * @phba: Pointer to HBA context object.
  1804. * @hbq_buffer: Pointer to HBQ buffer.
  1805. *
  1806. * This function is called with hbalock. This function gives back
  1807. * the hbq buffer to firmware. If the HBQ does not have space to
  1808. * post the buffer, it will free the buffer.
  1809. **/
  1810. void
  1811. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1812. {
  1813. uint32_t hbqno;
  1814. if (hbq_buffer) {
  1815. hbqno = hbq_buffer->tag >> 16;
  1816. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1817. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1818. }
  1819. }
  1820. /**
  1821. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1822. * @mbxCommand: mailbox command code.
  1823. *
  1824. * This function is called by the mailbox event handler function to verify
  1825. * that the completed mailbox command is a legitimate mailbox command. If the
  1826. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1827. * and the mailbox event handler will take the HBA offline.
  1828. **/
  1829. static int
  1830. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1831. {
  1832. uint8_t ret;
  1833. switch (mbxCommand) {
  1834. case MBX_LOAD_SM:
  1835. case MBX_READ_NV:
  1836. case MBX_WRITE_NV:
  1837. case MBX_WRITE_VPARMS:
  1838. case MBX_RUN_BIU_DIAG:
  1839. case MBX_INIT_LINK:
  1840. case MBX_DOWN_LINK:
  1841. case MBX_CONFIG_LINK:
  1842. case MBX_CONFIG_RING:
  1843. case MBX_RESET_RING:
  1844. case MBX_READ_CONFIG:
  1845. case MBX_READ_RCONFIG:
  1846. case MBX_READ_SPARM:
  1847. case MBX_READ_STATUS:
  1848. case MBX_READ_RPI:
  1849. case MBX_READ_XRI:
  1850. case MBX_READ_REV:
  1851. case MBX_READ_LNK_STAT:
  1852. case MBX_REG_LOGIN:
  1853. case MBX_UNREG_LOGIN:
  1854. case MBX_CLEAR_LA:
  1855. case MBX_DUMP_MEMORY:
  1856. case MBX_DUMP_CONTEXT:
  1857. case MBX_RUN_DIAGS:
  1858. case MBX_RESTART:
  1859. case MBX_UPDATE_CFG:
  1860. case MBX_DOWN_LOAD:
  1861. case MBX_DEL_LD_ENTRY:
  1862. case MBX_RUN_PROGRAM:
  1863. case MBX_SET_MASK:
  1864. case MBX_SET_VARIABLE:
  1865. case MBX_UNREG_D_ID:
  1866. case MBX_KILL_BOARD:
  1867. case MBX_CONFIG_FARP:
  1868. case MBX_BEACON:
  1869. case MBX_LOAD_AREA:
  1870. case MBX_RUN_BIU_DIAG64:
  1871. case MBX_CONFIG_PORT:
  1872. case MBX_READ_SPARM64:
  1873. case MBX_READ_RPI64:
  1874. case MBX_REG_LOGIN64:
  1875. case MBX_READ_TOPOLOGY:
  1876. case MBX_WRITE_WWN:
  1877. case MBX_SET_DEBUG:
  1878. case MBX_LOAD_EXP_ROM:
  1879. case MBX_ASYNCEVT_ENABLE:
  1880. case MBX_REG_VPI:
  1881. case MBX_UNREG_VPI:
  1882. case MBX_HEARTBEAT:
  1883. case MBX_PORT_CAPABILITIES:
  1884. case MBX_PORT_IOV_CONTROL:
  1885. case MBX_SLI4_CONFIG:
  1886. case MBX_SLI4_REQ_FTRS:
  1887. case MBX_REG_FCFI:
  1888. case MBX_UNREG_FCFI:
  1889. case MBX_REG_VFI:
  1890. case MBX_UNREG_VFI:
  1891. case MBX_INIT_VPI:
  1892. case MBX_INIT_VFI:
  1893. case MBX_RESUME_RPI:
  1894. case MBX_READ_EVENT_LOG_STATUS:
  1895. case MBX_READ_EVENT_LOG:
  1896. case MBX_SECURITY_MGMT:
  1897. case MBX_AUTH_PORT:
  1898. ret = mbxCommand;
  1899. break;
  1900. default:
  1901. ret = MBX_SHUTDOWN;
  1902. break;
  1903. }
  1904. return ret;
  1905. }
  1906. /**
  1907. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1908. * @phba: Pointer to HBA context object.
  1909. * @pmboxq: Pointer to mailbox command.
  1910. *
  1911. * This is completion handler function for mailbox commands issued from
  1912. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1913. * mailbox event handler function with no lock held. This function
  1914. * will wake up thread waiting on the wait queue pointed by context1
  1915. * of the mailbox.
  1916. **/
  1917. void
  1918. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1919. {
  1920. wait_queue_head_t *pdone_q;
  1921. unsigned long drvr_flag;
  1922. /*
  1923. * If pdone_q is empty, the driver thread gave up waiting and
  1924. * continued running.
  1925. */
  1926. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1927. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1928. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1929. if (pdone_q)
  1930. wake_up_interruptible(pdone_q);
  1931. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1932. return;
  1933. }
  1934. /**
  1935. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1936. * @phba: Pointer to HBA context object.
  1937. * @pmb: Pointer to mailbox object.
  1938. *
  1939. * This function is the default mailbox completion handler. It
  1940. * frees the memory resources associated with the completed mailbox
  1941. * command. If the completed command is a REG_LOGIN mailbox command,
  1942. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1943. **/
  1944. void
  1945. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1946. {
  1947. struct lpfc_vport *vport = pmb->vport;
  1948. struct lpfc_dmabuf *mp;
  1949. struct lpfc_nodelist *ndlp;
  1950. struct Scsi_Host *shost;
  1951. uint16_t rpi, vpi;
  1952. int rc;
  1953. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1954. if (mp) {
  1955. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1956. kfree(mp);
  1957. }
  1958. /*
  1959. * If a REG_LOGIN succeeded after node is destroyed or node
  1960. * is in re-discovery driver need to cleanup the RPI.
  1961. */
  1962. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1963. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1964. !pmb->u.mb.mbxStatus) {
  1965. rpi = pmb->u.mb.un.varWords[0];
  1966. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1967. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1968. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1969. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1970. if (rc != MBX_NOT_FINISHED)
  1971. return;
  1972. }
  1973. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1974. !(phba->pport->load_flag & FC_UNLOADING) &&
  1975. !pmb->u.mb.mbxStatus) {
  1976. shost = lpfc_shost_from_vport(vport);
  1977. spin_lock_irq(shost->host_lock);
  1978. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1979. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1980. spin_unlock_irq(shost->host_lock);
  1981. }
  1982. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1983. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1984. lpfc_nlp_put(ndlp);
  1985. pmb->context2 = NULL;
  1986. }
  1987. /* Check security permission status on INIT_LINK mailbox command */
  1988. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1989. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1990. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1991. "2860 SLI authentication is required "
  1992. "for INIT_LINK but has not done yet\n");
  1993. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1994. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1995. else
  1996. mempool_free(pmb, phba->mbox_mem_pool);
  1997. }
  1998. /**
  1999. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2000. * @phba: Pointer to HBA context object.
  2001. *
  2002. * This function is called with no lock held. This function processes all
  2003. * the completed mailbox commands and gives it to upper layers. The interrupt
  2004. * service routine processes mailbox completion interrupt and adds completed
  2005. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2006. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2007. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2008. * function returns the mailbox commands to the upper layer by calling the
  2009. * completion handler function of each mailbox.
  2010. **/
  2011. int
  2012. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2013. {
  2014. MAILBOX_t *pmbox;
  2015. LPFC_MBOXQ_t *pmb;
  2016. int rc;
  2017. LIST_HEAD(cmplq);
  2018. phba->sli.slistat.mbox_event++;
  2019. /* Get all completed mailboxe buffers into the cmplq */
  2020. spin_lock_irq(&phba->hbalock);
  2021. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2022. spin_unlock_irq(&phba->hbalock);
  2023. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2024. do {
  2025. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2026. if (pmb == NULL)
  2027. break;
  2028. pmbox = &pmb->u.mb;
  2029. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2030. if (pmb->vport) {
  2031. lpfc_debugfs_disc_trc(pmb->vport,
  2032. LPFC_DISC_TRC_MBOX_VPORT,
  2033. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2034. (uint32_t)pmbox->mbxCommand,
  2035. pmbox->un.varWords[0],
  2036. pmbox->un.varWords[1]);
  2037. }
  2038. else {
  2039. lpfc_debugfs_disc_trc(phba->pport,
  2040. LPFC_DISC_TRC_MBOX,
  2041. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2042. (uint32_t)pmbox->mbxCommand,
  2043. pmbox->un.varWords[0],
  2044. pmbox->un.varWords[1]);
  2045. }
  2046. }
  2047. /*
  2048. * It is a fatal error if unknown mbox command completion.
  2049. */
  2050. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2051. MBX_SHUTDOWN) {
  2052. /* Unknown mailbox command compl */
  2053. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2054. "(%d):0323 Unknown Mailbox command "
  2055. "x%x (x%x/x%x) Cmpl\n",
  2056. pmb->vport ? pmb->vport->vpi : 0,
  2057. pmbox->mbxCommand,
  2058. lpfc_sli_config_mbox_subsys_get(phba,
  2059. pmb),
  2060. lpfc_sli_config_mbox_opcode_get(phba,
  2061. pmb));
  2062. phba->link_state = LPFC_HBA_ERROR;
  2063. phba->work_hs = HS_FFER3;
  2064. lpfc_handle_eratt(phba);
  2065. continue;
  2066. }
  2067. if (pmbox->mbxStatus) {
  2068. phba->sli.slistat.mbox_stat_err++;
  2069. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2070. /* Mbox cmd cmpl error - RETRYing */
  2071. lpfc_printf_log(phba, KERN_INFO,
  2072. LOG_MBOX | LOG_SLI,
  2073. "(%d):0305 Mbox cmd cmpl "
  2074. "error - RETRYing Data: x%x "
  2075. "(x%x/x%x) x%x x%x x%x\n",
  2076. pmb->vport ? pmb->vport->vpi : 0,
  2077. pmbox->mbxCommand,
  2078. lpfc_sli_config_mbox_subsys_get(phba,
  2079. pmb),
  2080. lpfc_sli_config_mbox_opcode_get(phba,
  2081. pmb),
  2082. pmbox->mbxStatus,
  2083. pmbox->un.varWords[0],
  2084. pmb->vport->port_state);
  2085. pmbox->mbxStatus = 0;
  2086. pmbox->mbxOwner = OWN_HOST;
  2087. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2088. if (rc != MBX_NOT_FINISHED)
  2089. continue;
  2090. }
  2091. }
  2092. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2093. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2094. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2095. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2096. pmb->vport ? pmb->vport->vpi : 0,
  2097. pmbox->mbxCommand,
  2098. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2099. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2100. pmb->mbox_cmpl,
  2101. *((uint32_t *) pmbox),
  2102. pmbox->un.varWords[0],
  2103. pmbox->un.varWords[1],
  2104. pmbox->un.varWords[2],
  2105. pmbox->un.varWords[3],
  2106. pmbox->un.varWords[4],
  2107. pmbox->un.varWords[5],
  2108. pmbox->un.varWords[6],
  2109. pmbox->un.varWords[7]);
  2110. if (pmb->mbox_cmpl)
  2111. pmb->mbox_cmpl(phba,pmb);
  2112. } while (1);
  2113. return 0;
  2114. }
  2115. /**
  2116. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2117. * @phba: Pointer to HBA context object.
  2118. * @pring: Pointer to driver SLI ring object.
  2119. * @tag: buffer tag.
  2120. *
  2121. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2122. * is set in the tag the buffer is posted for a particular exchange,
  2123. * the function will return the buffer without replacing the buffer.
  2124. * If the buffer is for unsolicited ELS or CT traffic, this function
  2125. * returns the buffer and also posts another buffer to the firmware.
  2126. **/
  2127. static struct lpfc_dmabuf *
  2128. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2129. struct lpfc_sli_ring *pring,
  2130. uint32_t tag)
  2131. {
  2132. struct hbq_dmabuf *hbq_entry;
  2133. if (tag & QUE_BUFTAG_BIT)
  2134. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2135. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2136. if (!hbq_entry)
  2137. return NULL;
  2138. return &hbq_entry->dbuf;
  2139. }
  2140. /**
  2141. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2142. * @phba: Pointer to HBA context object.
  2143. * @pring: Pointer to driver SLI ring object.
  2144. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2145. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2146. * @fch_type: the type for the first frame of the sequence.
  2147. *
  2148. * This function is called with no lock held. This function uses the r_ctl and
  2149. * type of the received sequence to find the correct callback function to call
  2150. * to process the sequence.
  2151. **/
  2152. static int
  2153. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2154. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2155. uint32_t fch_type)
  2156. {
  2157. int i;
  2158. /* unSolicited Responses */
  2159. if (pring->prt[0].profile) {
  2160. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2161. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2162. saveq);
  2163. return 1;
  2164. }
  2165. /* We must search, based on rctl / type
  2166. for the right routine */
  2167. for (i = 0; i < pring->num_mask; i++) {
  2168. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2169. (pring->prt[i].type == fch_type)) {
  2170. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2171. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2172. (phba, pring, saveq);
  2173. return 1;
  2174. }
  2175. }
  2176. return 0;
  2177. }
  2178. /**
  2179. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2180. * @phba: Pointer to HBA context object.
  2181. * @pring: Pointer to driver SLI ring object.
  2182. * @saveq: Pointer to the unsolicited iocb.
  2183. *
  2184. * This function is called with no lock held by the ring event handler
  2185. * when there is an unsolicited iocb posted to the response ring by the
  2186. * firmware. This function gets the buffer associated with the iocbs
  2187. * and calls the event handler for the ring. This function handles both
  2188. * qring buffers and hbq buffers.
  2189. * When the function returns 1 the caller can free the iocb object otherwise
  2190. * upper layer functions will free the iocb objects.
  2191. **/
  2192. static int
  2193. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2194. struct lpfc_iocbq *saveq)
  2195. {
  2196. IOCB_t * irsp;
  2197. WORD5 * w5p;
  2198. uint32_t Rctl, Type;
  2199. uint32_t match;
  2200. struct lpfc_iocbq *iocbq;
  2201. struct lpfc_dmabuf *dmzbuf;
  2202. match = 0;
  2203. irsp = &(saveq->iocb);
  2204. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2205. if (pring->lpfc_sli_rcv_async_status)
  2206. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2207. else
  2208. lpfc_printf_log(phba,
  2209. KERN_WARNING,
  2210. LOG_SLI,
  2211. "0316 Ring %d handler: unexpected "
  2212. "ASYNC_STATUS iocb received evt_code "
  2213. "0x%x\n",
  2214. pring->ringno,
  2215. irsp->un.asyncstat.evt_code);
  2216. return 1;
  2217. }
  2218. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2219. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2220. if (irsp->ulpBdeCount > 0) {
  2221. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2222. irsp->un.ulpWord[3]);
  2223. lpfc_in_buf_free(phba, dmzbuf);
  2224. }
  2225. if (irsp->ulpBdeCount > 1) {
  2226. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2227. irsp->unsli3.sli3Words[3]);
  2228. lpfc_in_buf_free(phba, dmzbuf);
  2229. }
  2230. if (irsp->ulpBdeCount > 2) {
  2231. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2232. irsp->unsli3.sli3Words[7]);
  2233. lpfc_in_buf_free(phba, dmzbuf);
  2234. }
  2235. return 1;
  2236. }
  2237. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2238. if (irsp->ulpBdeCount != 0) {
  2239. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2240. irsp->un.ulpWord[3]);
  2241. if (!saveq->context2)
  2242. lpfc_printf_log(phba,
  2243. KERN_ERR,
  2244. LOG_SLI,
  2245. "0341 Ring %d Cannot find buffer for "
  2246. "an unsolicited iocb. tag 0x%x\n",
  2247. pring->ringno,
  2248. irsp->un.ulpWord[3]);
  2249. }
  2250. if (irsp->ulpBdeCount == 2) {
  2251. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2252. irsp->unsli3.sli3Words[7]);
  2253. if (!saveq->context3)
  2254. lpfc_printf_log(phba,
  2255. KERN_ERR,
  2256. LOG_SLI,
  2257. "0342 Ring %d Cannot find buffer for an"
  2258. " unsolicited iocb. tag 0x%x\n",
  2259. pring->ringno,
  2260. irsp->unsli3.sli3Words[7]);
  2261. }
  2262. list_for_each_entry(iocbq, &saveq->list, list) {
  2263. irsp = &(iocbq->iocb);
  2264. if (irsp->ulpBdeCount != 0) {
  2265. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2266. irsp->un.ulpWord[3]);
  2267. if (!iocbq->context2)
  2268. lpfc_printf_log(phba,
  2269. KERN_ERR,
  2270. LOG_SLI,
  2271. "0343 Ring %d Cannot find "
  2272. "buffer for an unsolicited iocb"
  2273. ". tag 0x%x\n", pring->ringno,
  2274. irsp->un.ulpWord[3]);
  2275. }
  2276. if (irsp->ulpBdeCount == 2) {
  2277. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2278. irsp->unsli3.sli3Words[7]);
  2279. if (!iocbq->context3)
  2280. lpfc_printf_log(phba,
  2281. KERN_ERR,
  2282. LOG_SLI,
  2283. "0344 Ring %d Cannot find "
  2284. "buffer for an unsolicited "
  2285. "iocb. tag 0x%x\n",
  2286. pring->ringno,
  2287. irsp->unsli3.sli3Words[7]);
  2288. }
  2289. }
  2290. }
  2291. if (irsp->ulpBdeCount != 0 &&
  2292. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2293. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2294. int found = 0;
  2295. /* search continue save q for same XRI */
  2296. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2297. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2298. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2299. list_add_tail(&saveq->list, &iocbq->list);
  2300. found = 1;
  2301. break;
  2302. }
  2303. }
  2304. if (!found)
  2305. list_add_tail(&saveq->clist,
  2306. &pring->iocb_continue_saveq);
  2307. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2308. list_del_init(&iocbq->clist);
  2309. saveq = iocbq;
  2310. irsp = &(saveq->iocb);
  2311. } else
  2312. return 0;
  2313. }
  2314. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2315. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2316. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2317. Rctl = FC_RCTL_ELS_REQ;
  2318. Type = FC_TYPE_ELS;
  2319. } else {
  2320. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2321. Rctl = w5p->hcsw.Rctl;
  2322. Type = w5p->hcsw.Type;
  2323. /* Firmware Workaround */
  2324. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2325. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2326. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2327. Rctl = FC_RCTL_ELS_REQ;
  2328. Type = FC_TYPE_ELS;
  2329. w5p->hcsw.Rctl = Rctl;
  2330. w5p->hcsw.Type = Type;
  2331. }
  2332. }
  2333. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2334. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2335. "0313 Ring %d handler: unexpected Rctl x%x "
  2336. "Type x%x received\n",
  2337. pring->ringno, Rctl, Type);
  2338. return 1;
  2339. }
  2340. /**
  2341. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2342. * @phba: Pointer to HBA context object.
  2343. * @pring: Pointer to driver SLI ring object.
  2344. * @prspiocb: Pointer to response iocb object.
  2345. *
  2346. * This function looks up the iocb_lookup table to get the command iocb
  2347. * corresponding to the given response iocb using the iotag of the
  2348. * response iocb. This function is called with the hbalock held.
  2349. * This function returns the command iocb object if it finds the command
  2350. * iocb else returns NULL.
  2351. **/
  2352. static struct lpfc_iocbq *
  2353. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2354. struct lpfc_sli_ring *pring,
  2355. struct lpfc_iocbq *prspiocb)
  2356. {
  2357. struct lpfc_iocbq *cmd_iocb = NULL;
  2358. uint16_t iotag;
  2359. iotag = prspiocb->iocb.ulpIoTag;
  2360. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2361. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2362. list_del_init(&cmd_iocb->list);
  2363. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2364. pring->txcmplq_cnt--;
  2365. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2366. }
  2367. return cmd_iocb;
  2368. }
  2369. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2370. "0317 iotag x%x is out off "
  2371. "range: max iotag x%x wd0 x%x\n",
  2372. iotag, phba->sli.last_iotag,
  2373. *(((uint32_t *) &prspiocb->iocb) + 7));
  2374. return NULL;
  2375. }
  2376. /**
  2377. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2378. * @phba: Pointer to HBA context object.
  2379. * @pring: Pointer to driver SLI ring object.
  2380. * @iotag: IOCB tag.
  2381. *
  2382. * This function looks up the iocb_lookup table to get the command iocb
  2383. * corresponding to the given iotag. This function is called with the
  2384. * hbalock held.
  2385. * This function returns the command iocb object if it finds the command
  2386. * iocb else returns NULL.
  2387. **/
  2388. static struct lpfc_iocbq *
  2389. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2390. struct lpfc_sli_ring *pring, uint16_t iotag)
  2391. {
  2392. struct lpfc_iocbq *cmd_iocb;
  2393. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2394. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2395. list_del_init(&cmd_iocb->list);
  2396. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2397. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2398. pring->txcmplq_cnt--;
  2399. }
  2400. return cmd_iocb;
  2401. }
  2402. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2403. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2404. iotag, phba->sli.last_iotag);
  2405. return NULL;
  2406. }
  2407. /**
  2408. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2409. * @phba: Pointer to HBA context object.
  2410. * @pring: Pointer to driver SLI ring object.
  2411. * @saveq: Pointer to the response iocb to be processed.
  2412. *
  2413. * This function is called by the ring event handler for non-fcp
  2414. * rings when there is a new response iocb in the response ring.
  2415. * The caller is not required to hold any locks. This function
  2416. * gets the command iocb associated with the response iocb and
  2417. * calls the completion handler for the command iocb. If there
  2418. * is no completion handler, the function will free the resources
  2419. * associated with command iocb. If the response iocb is for
  2420. * an already aborted command iocb, the status of the completion
  2421. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2422. * This function always returns 1.
  2423. **/
  2424. static int
  2425. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2426. struct lpfc_iocbq *saveq)
  2427. {
  2428. struct lpfc_iocbq *cmdiocbp;
  2429. int rc = 1;
  2430. unsigned long iflag;
  2431. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2432. spin_lock_irqsave(&phba->hbalock, iflag);
  2433. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2434. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2435. if (cmdiocbp) {
  2436. if (cmdiocbp->iocb_cmpl) {
  2437. /*
  2438. * If an ELS command failed send an event to mgmt
  2439. * application.
  2440. */
  2441. if (saveq->iocb.ulpStatus &&
  2442. (pring->ringno == LPFC_ELS_RING) &&
  2443. (cmdiocbp->iocb.ulpCommand ==
  2444. CMD_ELS_REQUEST64_CR))
  2445. lpfc_send_els_failure_event(phba,
  2446. cmdiocbp, saveq);
  2447. /*
  2448. * Post all ELS completions to the worker thread.
  2449. * All other are passed to the completion callback.
  2450. */
  2451. if (pring->ringno == LPFC_ELS_RING) {
  2452. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2453. (cmdiocbp->iocb_flag &
  2454. LPFC_DRIVER_ABORTED)) {
  2455. spin_lock_irqsave(&phba->hbalock,
  2456. iflag);
  2457. cmdiocbp->iocb_flag &=
  2458. ~LPFC_DRIVER_ABORTED;
  2459. spin_unlock_irqrestore(&phba->hbalock,
  2460. iflag);
  2461. saveq->iocb.ulpStatus =
  2462. IOSTAT_LOCAL_REJECT;
  2463. saveq->iocb.un.ulpWord[4] =
  2464. IOERR_SLI_ABORTED;
  2465. /* Firmware could still be in progress
  2466. * of DMAing payload, so don't free data
  2467. * buffer till after a hbeat.
  2468. */
  2469. spin_lock_irqsave(&phba->hbalock,
  2470. iflag);
  2471. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2472. spin_unlock_irqrestore(&phba->hbalock,
  2473. iflag);
  2474. }
  2475. if (phba->sli_rev == LPFC_SLI_REV4) {
  2476. if (saveq->iocb_flag &
  2477. LPFC_EXCHANGE_BUSY) {
  2478. /* Set cmdiocb flag for the
  2479. * exchange busy so sgl (xri)
  2480. * will not be released until
  2481. * the abort xri is received
  2482. * from hba.
  2483. */
  2484. spin_lock_irqsave(
  2485. &phba->hbalock, iflag);
  2486. cmdiocbp->iocb_flag |=
  2487. LPFC_EXCHANGE_BUSY;
  2488. spin_unlock_irqrestore(
  2489. &phba->hbalock, iflag);
  2490. }
  2491. if (cmdiocbp->iocb_flag &
  2492. LPFC_DRIVER_ABORTED) {
  2493. /*
  2494. * Clear LPFC_DRIVER_ABORTED
  2495. * bit in case it was driver
  2496. * initiated abort.
  2497. */
  2498. spin_lock_irqsave(
  2499. &phba->hbalock, iflag);
  2500. cmdiocbp->iocb_flag &=
  2501. ~LPFC_DRIVER_ABORTED;
  2502. spin_unlock_irqrestore(
  2503. &phba->hbalock, iflag);
  2504. cmdiocbp->iocb.ulpStatus =
  2505. IOSTAT_LOCAL_REJECT;
  2506. cmdiocbp->iocb.un.ulpWord[4] =
  2507. IOERR_ABORT_REQUESTED;
  2508. /*
  2509. * For SLI4, irsiocb contains
  2510. * NO_XRI in sli_xritag, it
  2511. * shall not affect releasing
  2512. * sgl (xri) process.
  2513. */
  2514. saveq->iocb.ulpStatus =
  2515. IOSTAT_LOCAL_REJECT;
  2516. saveq->iocb.un.ulpWord[4] =
  2517. IOERR_SLI_ABORTED;
  2518. spin_lock_irqsave(
  2519. &phba->hbalock, iflag);
  2520. saveq->iocb_flag |=
  2521. LPFC_DELAY_MEM_FREE;
  2522. spin_unlock_irqrestore(
  2523. &phba->hbalock, iflag);
  2524. }
  2525. }
  2526. }
  2527. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2528. } else
  2529. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2530. } else {
  2531. /*
  2532. * Unknown initiating command based on the response iotag.
  2533. * This could be the case on the ELS ring because of
  2534. * lpfc_els_abort().
  2535. */
  2536. if (pring->ringno != LPFC_ELS_RING) {
  2537. /*
  2538. * Ring <ringno> handler: unexpected completion IoTag
  2539. * <IoTag>
  2540. */
  2541. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2542. "0322 Ring %d handler: "
  2543. "unexpected completion IoTag x%x "
  2544. "Data: x%x x%x x%x x%x\n",
  2545. pring->ringno,
  2546. saveq->iocb.ulpIoTag,
  2547. saveq->iocb.ulpStatus,
  2548. saveq->iocb.un.ulpWord[4],
  2549. saveq->iocb.ulpCommand,
  2550. saveq->iocb.ulpContext);
  2551. }
  2552. }
  2553. return rc;
  2554. }
  2555. /**
  2556. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2557. * @phba: Pointer to HBA context object.
  2558. * @pring: Pointer to driver SLI ring object.
  2559. *
  2560. * This function is called from the iocb ring event handlers when
  2561. * put pointer is ahead of the get pointer for a ring. This function signal
  2562. * an error attention condition to the worker thread and the worker
  2563. * thread will transition the HBA to offline state.
  2564. **/
  2565. static void
  2566. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2567. {
  2568. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2569. /*
  2570. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2571. * rsp ring <portRspMax>
  2572. */
  2573. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2574. "0312 Ring %d handler: portRspPut %d "
  2575. "is bigger than rsp ring %d\n",
  2576. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2577. pring->numRiocb);
  2578. phba->link_state = LPFC_HBA_ERROR;
  2579. /*
  2580. * All error attention handlers are posted to
  2581. * worker thread
  2582. */
  2583. phba->work_ha |= HA_ERATT;
  2584. phba->work_hs = HS_FFER3;
  2585. lpfc_worker_wake_up(phba);
  2586. return;
  2587. }
  2588. /**
  2589. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2590. * @ptr: Pointer to address of HBA context object.
  2591. *
  2592. * This function is invoked by the Error Attention polling timer when the
  2593. * timer times out. It will check the SLI Error Attention register for
  2594. * possible attention events. If so, it will post an Error Attention event
  2595. * and wake up worker thread to process it. Otherwise, it will set up the
  2596. * Error Attention polling timer for the next poll.
  2597. **/
  2598. void lpfc_poll_eratt(unsigned long ptr)
  2599. {
  2600. struct lpfc_hba *phba;
  2601. uint32_t eratt = 0;
  2602. phba = (struct lpfc_hba *)ptr;
  2603. /* Check chip HA register for error event */
  2604. eratt = lpfc_sli_check_eratt(phba);
  2605. if (eratt)
  2606. /* Tell the worker thread there is work to do */
  2607. lpfc_worker_wake_up(phba);
  2608. else
  2609. /* Restart the timer for next eratt poll */
  2610. mod_timer(&phba->eratt_poll, jiffies +
  2611. HZ * LPFC_ERATT_POLL_INTERVAL);
  2612. return;
  2613. }
  2614. /**
  2615. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2616. * @phba: Pointer to HBA context object.
  2617. * @pring: Pointer to driver SLI ring object.
  2618. * @mask: Host attention register mask for this ring.
  2619. *
  2620. * This function is called from the interrupt context when there is a ring
  2621. * event for the fcp ring. The caller does not hold any lock.
  2622. * The function processes each response iocb in the response ring until it
  2623. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2624. * LE bit set. The function will call the completion handler of the command iocb
  2625. * if the response iocb indicates a completion for a command iocb or it is
  2626. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2627. * function if this is an unsolicited iocb.
  2628. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2629. * to check it explicitly.
  2630. */
  2631. int
  2632. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2633. struct lpfc_sli_ring *pring, uint32_t mask)
  2634. {
  2635. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2636. IOCB_t *irsp = NULL;
  2637. IOCB_t *entry = NULL;
  2638. struct lpfc_iocbq *cmdiocbq = NULL;
  2639. struct lpfc_iocbq rspiocbq;
  2640. uint32_t status;
  2641. uint32_t portRspPut, portRspMax;
  2642. int rc = 1;
  2643. lpfc_iocb_type type;
  2644. unsigned long iflag;
  2645. uint32_t rsp_cmpl = 0;
  2646. spin_lock_irqsave(&phba->hbalock, iflag);
  2647. pring->stats.iocb_event++;
  2648. /*
  2649. * The next available response entry should never exceed the maximum
  2650. * entries. If it does, treat it as an adapter hardware error.
  2651. */
  2652. portRspMax = pring->numRiocb;
  2653. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2654. if (unlikely(portRspPut >= portRspMax)) {
  2655. lpfc_sli_rsp_pointers_error(phba, pring);
  2656. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2657. return 1;
  2658. }
  2659. if (phba->fcp_ring_in_use) {
  2660. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2661. return 1;
  2662. } else
  2663. phba->fcp_ring_in_use = 1;
  2664. rmb();
  2665. while (pring->rspidx != portRspPut) {
  2666. /*
  2667. * Fetch an entry off the ring and copy it into a local data
  2668. * structure. The copy involves a byte-swap since the
  2669. * network byte order and pci byte orders are different.
  2670. */
  2671. entry = lpfc_resp_iocb(phba, pring);
  2672. phba->last_completion_time = jiffies;
  2673. if (++pring->rspidx >= portRspMax)
  2674. pring->rspidx = 0;
  2675. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2676. (uint32_t *) &rspiocbq.iocb,
  2677. phba->iocb_rsp_size);
  2678. INIT_LIST_HEAD(&(rspiocbq.list));
  2679. irsp = &rspiocbq.iocb;
  2680. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2681. pring->stats.iocb_rsp++;
  2682. rsp_cmpl++;
  2683. if (unlikely(irsp->ulpStatus)) {
  2684. /*
  2685. * If resource errors reported from HBA, reduce
  2686. * queuedepths of the SCSI device.
  2687. */
  2688. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2689. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2690. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2691. phba->lpfc_rampdown_queue_depth(phba);
  2692. spin_lock_irqsave(&phba->hbalock, iflag);
  2693. }
  2694. /* Rsp ring <ringno> error: IOCB */
  2695. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2696. "0336 Rsp Ring %d error: IOCB Data: "
  2697. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2698. pring->ringno,
  2699. irsp->un.ulpWord[0],
  2700. irsp->un.ulpWord[1],
  2701. irsp->un.ulpWord[2],
  2702. irsp->un.ulpWord[3],
  2703. irsp->un.ulpWord[4],
  2704. irsp->un.ulpWord[5],
  2705. *(uint32_t *)&irsp->un1,
  2706. *((uint32_t *)&irsp->un1 + 1));
  2707. }
  2708. switch (type) {
  2709. case LPFC_ABORT_IOCB:
  2710. case LPFC_SOL_IOCB:
  2711. /*
  2712. * Idle exchange closed via ABTS from port. No iocb
  2713. * resources need to be recovered.
  2714. */
  2715. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2716. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2717. "0333 IOCB cmd 0x%x"
  2718. " processed. Skipping"
  2719. " completion\n",
  2720. irsp->ulpCommand);
  2721. break;
  2722. }
  2723. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2724. &rspiocbq);
  2725. if (unlikely(!cmdiocbq))
  2726. break;
  2727. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2728. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2729. if (cmdiocbq->iocb_cmpl) {
  2730. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2731. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2732. &rspiocbq);
  2733. spin_lock_irqsave(&phba->hbalock, iflag);
  2734. }
  2735. break;
  2736. case LPFC_UNSOL_IOCB:
  2737. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2738. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2739. spin_lock_irqsave(&phba->hbalock, iflag);
  2740. break;
  2741. default:
  2742. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2743. char adaptermsg[LPFC_MAX_ADPTMSG];
  2744. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2745. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2746. MAX_MSG_DATA);
  2747. dev_warn(&((phba->pcidev)->dev),
  2748. "lpfc%d: %s\n",
  2749. phba->brd_no, adaptermsg);
  2750. } else {
  2751. /* Unknown IOCB command */
  2752. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2753. "0334 Unknown IOCB command "
  2754. "Data: x%x, x%x x%x x%x x%x\n",
  2755. type, irsp->ulpCommand,
  2756. irsp->ulpStatus,
  2757. irsp->ulpIoTag,
  2758. irsp->ulpContext);
  2759. }
  2760. break;
  2761. }
  2762. /*
  2763. * The response IOCB has been processed. Update the ring
  2764. * pointer in SLIM. If the port response put pointer has not
  2765. * been updated, sync the pgp->rspPutInx and fetch the new port
  2766. * response put pointer.
  2767. */
  2768. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2769. if (pring->rspidx == portRspPut)
  2770. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2771. }
  2772. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2773. pring->stats.iocb_rsp_full++;
  2774. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2775. writel(status, phba->CAregaddr);
  2776. readl(phba->CAregaddr);
  2777. }
  2778. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2779. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2780. pring->stats.iocb_cmd_empty++;
  2781. /* Force update of the local copy of cmdGetInx */
  2782. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2783. lpfc_sli_resume_iocb(phba, pring);
  2784. if ((pring->lpfc_sli_cmd_available))
  2785. (pring->lpfc_sli_cmd_available) (phba, pring);
  2786. }
  2787. phba->fcp_ring_in_use = 0;
  2788. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2789. return rc;
  2790. }
  2791. /**
  2792. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2793. * @phba: Pointer to HBA context object.
  2794. * @pring: Pointer to driver SLI ring object.
  2795. * @rspiocbp: Pointer to driver response IOCB object.
  2796. *
  2797. * This function is called from the worker thread when there is a slow-path
  2798. * response IOCB to process. This function chains all the response iocbs until
  2799. * seeing the iocb with the LE bit set. The function will call
  2800. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2801. * completion of a command iocb. The function will call the
  2802. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2803. * The function frees the resources or calls the completion handler if this
  2804. * iocb is an abort completion. The function returns NULL when the response
  2805. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2806. * this function shall chain the iocb on to the iocb_continueq and return the
  2807. * response iocb passed in.
  2808. **/
  2809. static struct lpfc_iocbq *
  2810. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2811. struct lpfc_iocbq *rspiocbp)
  2812. {
  2813. struct lpfc_iocbq *saveq;
  2814. struct lpfc_iocbq *cmdiocbp;
  2815. struct lpfc_iocbq *next_iocb;
  2816. IOCB_t *irsp = NULL;
  2817. uint32_t free_saveq;
  2818. uint8_t iocb_cmd_type;
  2819. lpfc_iocb_type type;
  2820. unsigned long iflag;
  2821. int rc;
  2822. spin_lock_irqsave(&phba->hbalock, iflag);
  2823. /* First add the response iocb to the countinueq list */
  2824. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2825. pring->iocb_continueq_cnt++;
  2826. /* Now, determine whether the list is completed for processing */
  2827. irsp = &rspiocbp->iocb;
  2828. if (irsp->ulpLe) {
  2829. /*
  2830. * By default, the driver expects to free all resources
  2831. * associated with this iocb completion.
  2832. */
  2833. free_saveq = 1;
  2834. saveq = list_get_first(&pring->iocb_continueq,
  2835. struct lpfc_iocbq, list);
  2836. irsp = &(saveq->iocb);
  2837. list_del_init(&pring->iocb_continueq);
  2838. pring->iocb_continueq_cnt = 0;
  2839. pring->stats.iocb_rsp++;
  2840. /*
  2841. * If resource errors reported from HBA, reduce
  2842. * queuedepths of the SCSI device.
  2843. */
  2844. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2845. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2846. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2847. phba->lpfc_rampdown_queue_depth(phba);
  2848. spin_lock_irqsave(&phba->hbalock, iflag);
  2849. }
  2850. if (irsp->ulpStatus) {
  2851. /* Rsp ring <ringno> error: IOCB */
  2852. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2853. "0328 Rsp Ring %d error: "
  2854. "IOCB Data: "
  2855. "x%x x%x x%x x%x "
  2856. "x%x x%x x%x x%x "
  2857. "x%x x%x x%x x%x "
  2858. "x%x x%x x%x x%x\n",
  2859. pring->ringno,
  2860. irsp->un.ulpWord[0],
  2861. irsp->un.ulpWord[1],
  2862. irsp->un.ulpWord[2],
  2863. irsp->un.ulpWord[3],
  2864. irsp->un.ulpWord[4],
  2865. irsp->un.ulpWord[5],
  2866. *(((uint32_t *) irsp) + 6),
  2867. *(((uint32_t *) irsp) + 7),
  2868. *(((uint32_t *) irsp) + 8),
  2869. *(((uint32_t *) irsp) + 9),
  2870. *(((uint32_t *) irsp) + 10),
  2871. *(((uint32_t *) irsp) + 11),
  2872. *(((uint32_t *) irsp) + 12),
  2873. *(((uint32_t *) irsp) + 13),
  2874. *(((uint32_t *) irsp) + 14),
  2875. *(((uint32_t *) irsp) + 15));
  2876. }
  2877. /*
  2878. * Fetch the IOCB command type and call the correct completion
  2879. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2880. * get freed back to the lpfc_iocb_list by the discovery
  2881. * kernel thread.
  2882. */
  2883. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2884. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2885. switch (type) {
  2886. case LPFC_SOL_IOCB:
  2887. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2888. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2889. spin_lock_irqsave(&phba->hbalock, iflag);
  2890. break;
  2891. case LPFC_UNSOL_IOCB:
  2892. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2893. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2894. spin_lock_irqsave(&phba->hbalock, iflag);
  2895. if (!rc)
  2896. free_saveq = 0;
  2897. break;
  2898. case LPFC_ABORT_IOCB:
  2899. cmdiocbp = NULL;
  2900. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2901. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2902. saveq);
  2903. if (cmdiocbp) {
  2904. /* Call the specified completion routine */
  2905. if (cmdiocbp->iocb_cmpl) {
  2906. spin_unlock_irqrestore(&phba->hbalock,
  2907. iflag);
  2908. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2909. saveq);
  2910. spin_lock_irqsave(&phba->hbalock,
  2911. iflag);
  2912. } else
  2913. __lpfc_sli_release_iocbq(phba,
  2914. cmdiocbp);
  2915. }
  2916. break;
  2917. case LPFC_UNKNOWN_IOCB:
  2918. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2919. char adaptermsg[LPFC_MAX_ADPTMSG];
  2920. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2921. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2922. MAX_MSG_DATA);
  2923. dev_warn(&((phba->pcidev)->dev),
  2924. "lpfc%d: %s\n",
  2925. phba->brd_no, adaptermsg);
  2926. } else {
  2927. /* Unknown IOCB command */
  2928. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2929. "0335 Unknown IOCB "
  2930. "command Data: x%x "
  2931. "x%x x%x x%x\n",
  2932. irsp->ulpCommand,
  2933. irsp->ulpStatus,
  2934. irsp->ulpIoTag,
  2935. irsp->ulpContext);
  2936. }
  2937. break;
  2938. }
  2939. if (free_saveq) {
  2940. list_for_each_entry_safe(rspiocbp, next_iocb,
  2941. &saveq->list, list) {
  2942. list_del(&rspiocbp->list);
  2943. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2944. }
  2945. __lpfc_sli_release_iocbq(phba, saveq);
  2946. }
  2947. rspiocbp = NULL;
  2948. }
  2949. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2950. return rspiocbp;
  2951. }
  2952. /**
  2953. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2954. * @phba: Pointer to HBA context object.
  2955. * @pring: Pointer to driver SLI ring object.
  2956. * @mask: Host attention register mask for this ring.
  2957. *
  2958. * This routine wraps the actual slow_ring event process routine from the
  2959. * API jump table function pointer from the lpfc_hba struct.
  2960. **/
  2961. void
  2962. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2963. struct lpfc_sli_ring *pring, uint32_t mask)
  2964. {
  2965. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2966. }
  2967. /**
  2968. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2969. * @phba: Pointer to HBA context object.
  2970. * @pring: Pointer to driver SLI ring object.
  2971. * @mask: Host attention register mask for this ring.
  2972. *
  2973. * This function is called from the worker thread when there is a ring event
  2974. * for non-fcp rings. The caller does not hold any lock. The function will
  2975. * remove each response iocb in the response ring and calls the handle
  2976. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2977. **/
  2978. static void
  2979. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2980. struct lpfc_sli_ring *pring, uint32_t mask)
  2981. {
  2982. struct lpfc_pgp *pgp;
  2983. IOCB_t *entry;
  2984. IOCB_t *irsp = NULL;
  2985. struct lpfc_iocbq *rspiocbp = NULL;
  2986. uint32_t portRspPut, portRspMax;
  2987. unsigned long iflag;
  2988. uint32_t status;
  2989. pgp = &phba->port_gp[pring->ringno];
  2990. spin_lock_irqsave(&phba->hbalock, iflag);
  2991. pring->stats.iocb_event++;
  2992. /*
  2993. * The next available response entry should never exceed the maximum
  2994. * entries. If it does, treat it as an adapter hardware error.
  2995. */
  2996. portRspMax = pring->numRiocb;
  2997. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2998. if (portRspPut >= portRspMax) {
  2999. /*
  3000. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3001. * rsp ring <portRspMax>
  3002. */
  3003. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3004. "0303 Ring %d handler: portRspPut %d "
  3005. "is bigger than rsp ring %d\n",
  3006. pring->ringno, portRspPut, portRspMax);
  3007. phba->link_state = LPFC_HBA_ERROR;
  3008. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3009. phba->work_hs = HS_FFER3;
  3010. lpfc_handle_eratt(phba);
  3011. return;
  3012. }
  3013. rmb();
  3014. while (pring->rspidx != portRspPut) {
  3015. /*
  3016. * Build a completion list and call the appropriate handler.
  3017. * The process is to get the next available response iocb, get
  3018. * a free iocb from the list, copy the response data into the
  3019. * free iocb, insert to the continuation list, and update the
  3020. * next response index to slim. This process makes response
  3021. * iocb's in the ring available to DMA as fast as possible but
  3022. * pays a penalty for a copy operation. Since the iocb is
  3023. * only 32 bytes, this penalty is considered small relative to
  3024. * the PCI reads for register values and a slim write. When
  3025. * the ulpLe field is set, the entire Command has been
  3026. * received.
  3027. */
  3028. entry = lpfc_resp_iocb(phba, pring);
  3029. phba->last_completion_time = jiffies;
  3030. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3031. if (rspiocbp == NULL) {
  3032. printk(KERN_ERR "%s: out of buffers! Failing "
  3033. "completion.\n", __func__);
  3034. break;
  3035. }
  3036. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3037. phba->iocb_rsp_size);
  3038. irsp = &rspiocbp->iocb;
  3039. if (++pring->rspidx >= portRspMax)
  3040. pring->rspidx = 0;
  3041. if (pring->ringno == LPFC_ELS_RING) {
  3042. lpfc_debugfs_slow_ring_trc(phba,
  3043. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3044. *(((uint32_t *) irsp) + 4),
  3045. *(((uint32_t *) irsp) + 6),
  3046. *(((uint32_t *) irsp) + 7));
  3047. }
  3048. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3049. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3050. /* Handle the response IOCB */
  3051. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3052. spin_lock_irqsave(&phba->hbalock, iflag);
  3053. /*
  3054. * If the port response put pointer has not been updated, sync
  3055. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3056. * response put pointer.
  3057. */
  3058. if (pring->rspidx == portRspPut) {
  3059. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3060. }
  3061. } /* while (pring->rspidx != portRspPut) */
  3062. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3063. /* At least one response entry has been freed */
  3064. pring->stats.iocb_rsp_full++;
  3065. /* SET RxRE_RSP in Chip Att register */
  3066. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3067. writel(status, phba->CAregaddr);
  3068. readl(phba->CAregaddr); /* flush */
  3069. }
  3070. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3071. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3072. pring->stats.iocb_cmd_empty++;
  3073. /* Force update of the local copy of cmdGetInx */
  3074. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3075. lpfc_sli_resume_iocb(phba, pring);
  3076. if ((pring->lpfc_sli_cmd_available))
  3077. (pring->lpfc_sli_cmd_available) (phba, pring);
  3078. }
  3079. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3080. return;
  3081. }
  3082. /**
  3083. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3084. * @phba: Pointer to HBA context object.
  3085. * @pring: Pointer to driver SLI ring object.
  3086. * @mask: Host attention register mask for this ring.
  3087. *
  3088. * This function is called from the worker thread when there is a pending
  3089. * ELS response iocb on the driver internal slow-path response iocb worker
  3090. * queue. The caller does not hold any lock. The function will remove each
  3091. * response iocb from the response worker queue and calls the handle
  3092. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3093. **/
  3094. static void
  3095. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3096. struct lpfc_sli_ring *pring, uint32_t mask)
  3097. {
  3098. struct lpfc_iocbq *irspiocbq;
  3099. struct hbq_dmabuf *dmabuf;
  3100. struct lpfc_cq_event *cq_event;
  3101. unsigned long iflag;
  3102. spin_lock_irqsave(&phba->hbalock, iflag);
  3103. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3104. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3105. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3106. /* Get the response iocb from the head of work queue */
  3107. spin_lock_irqsave(&phba->hbalock, iflag);
  3108. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3109. cq_event, struct lpfc_cq_event, list);
  3110. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3111. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3112. case CQE_CODE_COMPL_WQE:
  3113. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3114. cq_event);
  3115. /* Translate ELS WCQE to response IOCBQ */
  3116. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3117. irspiocbq);
  3118. if (irspiocbq)
  3119. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3120. irspiocbq);
  3121. break;
  3122. case CQE_CODE_RECEIVE:
  3123. case CQE_CODE_RECEIVE_V1:
  3124. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3125. cq_event);
  3126. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3127. break;
  3128. default:
  3129. break;
  3130. }
  3131. }
  3132. }
  3133. /**
  3134. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3135. * @phba: Pointer to HBA context object.
  3136. * @pring: Pointer to driver SLI ring object.
  3137. *
  3138. * This function aborts all iocbs in the given ring and frees all the iocb
  3139. * objects in txq. This function issues an abort iocb for all the iocb commands
  3140. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3141. * the return of this function. The caller is not required to hold any locks.
  3142. **/
  3143. void
  3144. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3145. {
  3146. LIST_HEAD(completions);
  3147. struct lpfc_iocbq *iocb, *next_iocb;
  3148. if (pring->ringno == LPFC_ELS_RING) {
  3149. lpfc_fabric_abort_hba(phba);
  3150. }
  3151. /* Error everything on txq and txcmplq
  3152. * First do the txq.
  3153. */
  3154. spin_lock_irq(&phba->hbalock);
  3155. list_splice_init(&pring->txq, &completions);
  3156. pring->txq_cnt = 0;
  3157. /* Next issue ABTS for everything on the txcmplq */
  3158. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3159. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3160. spin_unlock_irq(&phba->hbalock);
  3161. /* Cancel all the IOCBs from the completions list */
  3162. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3163. IOERR_SLI_ABORTED);
  3164. }
  3165. /**
  3166. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3167. * @phba: Pointer to HBA context object.
  3168. *
  3169. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3170. * objects in txq and txcmplq. This function will not issue abort iocbs
  3171. * for all the iocb commands in txcmplq, they will just be returned with
  3172. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3173. * slot has been permanently disabled.
  3174. **/
  3175. void
  3176. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3177. {
  3178. LIST_HEAD(txq);
  3179. LIST_HEAD(txcmplq);
  3180. struct lpfc_sli *psli = &phba->sli;
  3181. struct lpfc_sli_ring *pring;
  3182. /* Currently, only one fcp ring */
  3183. pring = &psli->ring[psli->fcp_ring];
  3184. spin_lock_irq(&phba->hbalock);
  3185. /* Retrieve everything on txq */
  3186. list_splice_init(&pring->txq, &txq);
  3187. pring->txq_cnt = 0;
  3188. /* Retrieve everything on the txcmplq */
  3189. list_splice_init(&pring->txcmplq, &txcmplq);
  3190. pring->txcmplq_cnt = 0;
  3191. spin_unlock_irq(&phba->hbalock);
  3192. /* Flush the txq */
  3193. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3194. IOERR_SLI_DOWN);
  3195. /* Flush the txcmpq */
  3196. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3197. IOERR_SLI_DOWN);
  3198. }
  3199. /**
  3200. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3201. * @phba: Pointer to HBA context object.
  3202. * @mask: Bit mask to be checked.
  3203. *
  3204. * This function reads the host status register and compares
  3205. * with the provided bit mask to check if HBA completed
  3206. * the restart. This function will wait in a loop for the
  3207. * HBA to complete restart. If the HBA does not restart within
  3208. * 15 iterations, the function will reset the HBA again. The
  3209. * function returns 1 when HBA fail to restart otherwise returns
  3210. * zero.
  3211. **/
  3212. static int
  3213. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3214. {
  3215. uint32_t status;
  3216. int i = 0;
  3217. int retval = 0;
  3218. /* Read the HBA Host Status Register */
  3219. if (lpfc_readl(phba->HSregaddr, &status))
  3220. return 1;
  3221. /*
  3222. * Check status register every 100ms for 5 retries, then every
  3223. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3224. * every 2.5 sec for 4.
  3225. * Break our of the loop if errors occurred during init.
  3226. */
  3227. while (((status & mask) != mask) &&
  3228. !(status & HS_FFERM) &&
  3229. i++ < 20) {
  3230. if (i <= 5)
  3231. msleep(10);
  3232. else if (i <= 10)
  3233. msleep(500);
  3234. else
  3235. msleep(2500);
  3236. if (i == 15) {
  3237. /* Do post */
  3238. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3239. lpfc_sli_brdrestart(phba);
  3240. }
  3241. /* Read the HBA Host Status Register */
  3242. if (lpfc_readl(phba->HSregaddr, &status)) {
  3243. retval = 1;
  3244. break;
  3245. }
  3246. }
  3247. /* Check to see if any errors occurred during init */
  3248. if ((status & HS_FFERM) || (i >= 20)) {
  3249. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3250. "2751 Adapter failed to restart, "
  3251. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3252. status,
  3253. readl(phba->MBslimaddr + 0xa8),
  3254. readl(phba->MBslimaddr + 0xac));
  3255. phba->link_state = LPFC_HBA_ERROR;
  3256. retval = 1;
  3257. }
  3258. return retval;
  3259. }
  3260. /**
  3261. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3262. * @phba: Pointer to HBA context object.
  3263. * @mask: Bit mask to be checked.
  3264. *
  3265. * This function checks the host status register to check if HBA is
  3266. * ready. This function will wait in a loop for the HBA to be ready
  3267. * If the HBA is not ready , the function will will reset the HBA PCI
  3268. * function again. The function returns 1 when HBA fail to be ready
  3269. * otherwise returns zero.
  3270. **/
  3271. static int
  3272. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3273. {
  3274. uint32_t status;
  3275. int retval = 0;
  3276. /* Read the HBA Host Status Register */
  3277. status = lpfc_sli4_post_status_check(phba);
  3278. if (status) {
  3279. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3280. lpfc_sli_brdrestart(phba);
  3281. status = lpfc_sli4_post_status_check(phba);
  3282. }
  3283. /* Check to see if any errors occurred during init */
  3284. if (status) {
  3285. phba->link_state = LPFC_HBA_ERROR;
  3286. retval = 1;
  3287. } else
  3288. phba->sli4_hba.intr_enable = 0;
  3289. return retval;
  3290. }
  3291. /**
  3292. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3293. * @phba: Pointer to HBA context object.
  3294. * @mask: Bit mask to be checked.
  3295. *
  3296. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3297. * from the API jump table function pointer from the lpfc_hba struct.
  3298. **/
  3299. int
  3300. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3301. {
  3302. return phba->lpfc_sli_brdready(phba, mask);
  3303. }
  3304. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3305. /**
  3306. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3307. * @phba: Pointer to HBA context object.
  3308. *
  3309. * This function is called before resetting an HBA. This function is called
  3310. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3311. **/
  3312. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3313. {
  3314. uint32_t __iomem *resp_buf;
  3315. uint32_t __iomem *mbox_buf;
  3316. volatile uint32_t mbox;
  3317. uint32_t hc_copy, ha_copy, resp_data;
  3318. int i;
  3319. uint8_t hdrtype;
  3320. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3321. if (hdrtype != 0x80 ||
  3322. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3323. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3324. return;
  3325. /*
  3326. * Tell the other part of the chip to suspend temporarily all
  3327. * its DMA activity.
  3328. */
  3329. resp_buf = phba->MBslimaddr;
  3330. /* Disable the error attention */
  3331. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3332. return;
  3333. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3334. readl(phba->HCregaddr); /* flush */
  3335. phba->link_flag |= LS_IGNORE_ERATT;
  3336. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3337. return;
  3338. if (ha_copy & HA_ERATT) {
  3339. /* Clear Chip error bit */
  3340. writel(HA_ERATT, phba->HAregaddr);
  3341. phba->pport->stopped = 1;
  3342. }
  3343. mbox = 0;
  3344. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3345. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3346. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3347. mbox_buf = phba->MBslimaddr;
  3348. writel(mbox, mbox_buf);
  3349. for (i = 0; i < 50; i++) {
  3350. if (lpfc_readl((resp_buf + 1), &resp_data))
  3351. return;
  3352. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3353. mdelay(1);
  3354. else
  3355. break;
  3356. }
  3357. resp_data = 0;
  3358. if (lpfc_readl((resp_buf + 1), &resp_data))
  3359. return;
  3360. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3361. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3362. phba->pport->stopped)
  3363. goto restore_hc;
  3364. else
  3365. goto clear_errat;
  3366. }
  3367. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3368. resp_data = 0;
  3369. for (i = 0; i < 500; i++) {
  3370. if (lpfc_readl(resp_buf, &resp_data))
  3371. return;
  3372. if (resp_data != mbox)
  3373. mdelay(1);
  3374. else
  3375. break;
  3376. }
  3377. clear_errat:
  3378. while (++i < 500) {
  3379. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3380. return;
  3381. if (!(ha_copy & HA_ERATT))
  3382. mdelay(1);
  3383. else
  3384. break;
  3385. }
  3386. if (readl(phba->HAregaddr) & HA_ERATT) {
  3387. writel(HA_ERATT, phba->HAregaddr);
  3388. phba->pport->stopped = 1;
  3389. }
  3390. restore_hc:
  3391. phba->link_flag &= ~LS_IGNORE_ERATT;
  3392. writel(hc_copy, phba->HCregaddr);
  3393. readl(phba->HCregaddr); /* flush */
  3394. }
  3395. /**
  3396. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3397. * @phba: Pointer to HBA context object.
  3398. *
  3399. * This function issues a kill_board mailbox command and waits for
  3400. * the error attention interrupt. This function is called for stopping
  3401. * the firmware processing. The caller is not required to hold any
  3402. * locks. This function calls lpfc_hba_down_post function to free
  3403. * any pending commands after the kill. The function will return 1 when it
  3404. * fails to kill the board else will return 0.
  3405. **/
  3406. int
  3407. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3408. {
  3409. struct lpfc_sli *psli;
  3410. LPFC_MBOXQ_t *pmb;
  3411. uint32_t status;
  3412. uint32_t ha_copy;
  3413. int retval;
  3414. int i = 0;
  3415. psli = &phba->sli;
  3416. /* Kill HBA */
  3417. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3418. "0329 Kill HBA Data: x%x x%x\n",
  3419. phba->pport->port_state, psli->sli_flag);
  3420. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3421. if (!pmb)
  3422. return 1;
  3423. /* Disable the error attention */
  3424. spin_lock_irq(&phba->hbalock);
  3425. if (lpfc_readl(phba->HCregaddr, &status)) {
  3426. spin_unlock_irq(&phba->hbalock);
  3427. mempool_free(pmb, phba->mbox_mem_pool);
  3428. return 1;
  3429. }
  3430. status &= ~HC_ERINT_ENA;
  3431. writel(status, phba->HCregaddr);
  3432. readl(phba->HCregaddr); /* flush */
  3433. phba->link_flag |= LS_IGNORE_ERATT;
  3434. spin_unlock_irq(&phba->hbalock);
  3435. lpfc_kill_board(phba, pmb);
  3436. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3437. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3438. if (retval != MBX_SUCCESS) {
  3439. if (retval != MBX_BUSY)
  3440. mempool_free(pmb, phba->mbox_mem_pool);
  3441. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3442. "2752 KILL_BOARD command failed retval %d\n",
  3443. retval);
  3444. spin_lock_irq(&phba->hbalock);
  3445. phba->link_flag &= ~LS_IGNORE_ERATT;
  3446. spin_unlock_irq(&phba->hbalock);
  3447. return 1;
  3448. }
  3449. spin_lock_irq(&phba->hbalock);
  3450. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3451. spin_unlock_irq(&phba->hbalock);
  3452. mempool_free(pmb, phba->mbox_mem_pool);
  3453. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3454. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3455. * 3 seconds we still set HBA_ERROR state because the status of the
  3456. * board is now undefined.
  3457. */
  3458. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3459. return 1;
  3460. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3461. mdelay(100);
  3462. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3463. return 1;
  3464. }
  3465. del_timer_sync(&psli->mbox_tmo);
  3466. if (ha_copy & HA_ERATT) {
  3467. writel(HA_ERATT, phba->HAregaddr);
  3468. phba->pport->stopped = 1;
  3469. }
  3470. spin_lock_irq(&phba->hbalock);
  3471. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3472. psli->mbox_active = NULL;
  3473. phba->link_flag &= ~LS_IGNORE_ERATT;
  3474. spin_unlock_irq(&phba->hbalock);
  3475. lpfc_hba_down_post(phba);
  3476. phba->link_state = LPFC_HBA_ERROR;
  3477. return ha_copy & HA_ERATT ? 0 : 1;
  3478. }
  3479. /**
  3480. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3481. * @phba: Pointer to HBA context object.
  3482. *
  3483. * This function resets the HBA by writing HC_INITFF to the control
  3484. * register. After the HBA resets, this function resets all the iocb ring
  3485. * indices. This function disables PCI layer parity checking during
  3486. * the reset.
  3487. * This function returns 0 always.
  3488. * The caller is not required to hold any locks.
  3489. **/
  3490. int
  3491. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3492. {
  3493. struct lpfc_sli *psli;
  3494. struct lpfc_sli_ring *pring;
  3495. uint16_t cfg_value;
  3496. int i;
  3497. psli = &phba->sli;
  3498. /* Reset HBA */
  3499. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3500. "0325 Reset HBA Data: x%x x%x\n",
  3501. phba->pport->port_state, psli->sli_flag);
  3502. /* perform board reset */
  3503. phba->fc_eventTag = 0;
  3504. phba->link_events = 0;
  3505. phba->pport->fc_myDID = 0;
  3506. phba->pport->fc_prevDID = 0;
  3507. /* Turn off parity checking and serr during the physical reset */
  3508. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3509. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3510. (cfg_value &
  3511. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3512. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3513. /* Now toggle INITFF bit in the Host Control Register */
  3514. writel(HC_INITFF, phba->HCregaddr);
  3515. mdelay(1);
  3516. readl(phba->HCregaddr); /* flush */
  3517. writel(0, phba->HCregaddr);
  3518. readl(phba->HCregaddr); /* flush */
  3519. /* Restore PCI cmd register */
  3520. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3521. /* Initialize relevant SLI info */
  3522. for (i = 0; i < psli->num_rings; i++) {
  3523. pring = &psli->ring[i];
  3524. pring->flag = 0;
  3525. pring->rspidx = 0;
  3526. pring->next_cmdidx = 0;
  3527. pring->local_getidx = 0;
  3528. pring->cmdidx = 0;
  3529. pring->missbufcnt = 0;
  3530. }
  3531. phba->link_state = LPFC_WARM_START;
  3532. return 0;
  3533. }
  3534. /**
  3535. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3536. * @phba: Pointer to HBA context object.
  3537. *
  3538. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3539. * checking during resets the device. The caller is not required to hold
  3540. * any locks.
  3541. *
  3542. * This function returns 0 always.
  3543. **/
  3544. int
  3545. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3546. {
  3547. struct lpfc_sli *psli = &phba->sli;
  3548. uint16_t cfg_value;
  3549. /* Reset HBA */
  3550. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3551. "0295 Reset HBA Data: x%x x%x\n",
  3552. phba->pport->port_state, psli->sli_flag);
  3553. /* perform board reset */
  3554. phba->fc_eventTag = 0;
  3555. phba->link_events = 0;
  3556. phba->pport->fc_myDID = 0;
  3557. phba->pport->fc_prevDID = 0;
  3558. spin_lock_irq(&phba->hbalock);
  3559. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3560. phba->fcf.fcf_flag = 0;
  3561. spin_unlock_irq(&phba->hbalock);
  3562. /* Now physically reset the device */
  3563. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3564. "0389 Performing PCI function reset!\n");
  3565. /* Turn off parity checking and serr during the physical reset */
  3566. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3567. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3568. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3569. /* Perform FCoE PCI function reset */
  3570. lpfc_sli4_queue_destroy(phba);
  3571. lpfc_pci_function_reset(phba);
  3572. /* Restore PCI cmd register */
  3573. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3574. return 0;
  3575. }
  3576. /**
  3577. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3578. * @phba: Pointer to HBA context object.
  3579. *
  3580. * This function is called in the SLI initialization code path to
  3581. * restart the HBA. The caller is not required to hold any lock.
  3582. * This function writes MBX_RESTART mailbox command to the SLIM and
  3583. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3584. * function to free any pending commands. The function enables
  3585. * POST only during the first initialization. The function returns zero.
  3586. * The function does not guarantee completion of MBX_RESTART mailbox
  3587. * command before the return of this function.
  3588. **/
  3589. static int
  3590. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3591. {
  3592. MAILBOX_t *mb;
  3593. struct lpfc_sli *psli;
  3594. volatile uint32_t word0;
  3595. void __iomem *to_slim;
  3596. uint32_t hba_aer_enabled;
  3597. spin_lock_irq(&phba->hbalock);
  3598. /* Take PCIe device Advanced Error Reporting (AER) state */
  3599. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3600. psli = &phba->sli;
  3601. /* Restart HBA */
  3602. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3603. "0337 Restart HBA Data: x%x x%x\n",
  3604. phba->pport->port_state, psli->sli_flag);
  3605. word0 = 0;
  3606. mb = (MAILBOX_t *) &word0;
  3607. mb->mbxCommand = MBX_RESTART;
  3608. mb->mbxHc = 1;
  3609. lpfc_reset_barrier(phba);
  3610. to_slim = phba->MBslimaddr;
  3611. writel(*(uint32_t *) mb, to_slim);
  3612. readl(to_slim); /* flush */
  3613. /* Only skip post after fc_ffinit is completed */
  3614. if (phba->pport->port_state)
  3615. word0 = 1; /* This is really setting up word1 */
  3616. else
  3617. word0 = 0; /* This is really setting up word1 */
  3618. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3619. writel(*(uint32_t *) mb, to_slim);
  3620. readl(to_slim); /* flush */
  3621. lpfc_sli_brdreset(phba);
  3622. phba->pport->stopped = 0;
  3623. phba->link_state = LPFC_INIT_START;
  3624. phba->hba_flag = 0;
  3625. spin_unlock_irq(&phba->hbalock);
  3626. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3627. psli->stats_start = get_seconds();
  3628. /* Give the INITFF and Post time to settle. */
  3629. mdelay(100);
  3630. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3631. if (hba_aer_enabled)
  3632. pci_disable_pcie_error_reporting(phba->pcidev);
  3633. lpfc_hba_down_post(phba);
  3634. return 0;
  3635. }
  3636. /**
  3637. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3638. * @phba: Pointer to HBA context object.
  3639. *
  3640. * This function is called in the SLI initialization code path to restart
  3641. * a SLI4 HBA. The caller is not required to hold any lock.
  3642. * At the end of the function, it calls lpfc_hba_down_post function to
  3643. * free any pending commands.
  3644. **/
  3645. static int
  3646. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3647. {
  3648. struct lpfc_sli *psli = &phba->sli;
  3649. uint32_t hba_aer_enabled;
  3650. /* Restart HBA */
  3651. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3652. "0296 Restart HBA Data: x%x x%x\n",
  3653. phba->pport->port_state, psli->sli_flag);
  3654. /* Take PCIe device Advanced Error Reporting (AER) state */
  3655. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3656. lpfc_sli4_brdreset(phba);
  3657. spin_lock_irq(&phba->hbalock);
  3658. phba->pport->stopped = 0;
  3659. phba->link_state = LPFC_INIT_START;
  3660. phba->hba_flag = 0;
  3661. spin_unlock_irq(&phba->hbalock);
  3662. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3663. psli->stats_start = get_seconds();
  3664. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3665. if (hba_aer_enabled)
  3666. pci_disable_pcie_error_reporting(phba->pcidev);
  3667. lpfc_hba_down_post(phba);
  3668. return 0;
  3669. }
  3670. /**
  3671. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3672. * @phba: Pointer to HBA context object.
  3673. *
  3674. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3675. * API jump table function pointer from the lpfc_hba struct.
  3676. **/
  3677. int
  3678. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3679. {
  3680. return phba->lpfc_sli_brdrestart(phba);
  3681. }
  3682. /**
  3683. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3684. * @phba: Pointer to HBA context object.
  3685. *
  3686. * This function is called after a HBA restart to wait for successful
  3687. * restart of the HBA. Successful restart of the HBA is indicated by
  3688. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3689. * iteration, the function will restart the HBA again. The function returns
  3690. * zero if HBA successfully restarted else returns negative error code.
  3691. **/
  3692. static int
  3693. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3694. {
  3695. uint32_t status, i = 0;
  3696. /* Read the HBA Host Status Register */
  3697. if (lpfc_readl(phba->HSregaddr, &status))
  3698. return -EIO;
  3699. /* Check status register to see what current state is */
  3700. i = 0;
  3701. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3702. /* Check every 10ms for 10 retries, then every 100ms for 90
  3703. * retries, then every 1 sec for 50 retires for a total of
  3704. * ~60 seconds before reset the board again and check every
  3705. * 1 sec for 50 retries. The up to 60 seconds before the
  3706. * board ready is required by the Falcon FIPS zeroization
  3707. * complete, and any reset the board in between shall cause
  3708. * restart of zeroization, further delay the board ready.
  3709. */
  3710. if (i++ >= 200) {
  3711. /* Adapter failed to init, timeout, status reg
  3712. <status> */
  3713. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3714. "0436 Adapter failed to init, "
  3715. "timeout, status reg x%x, "
  3716. "FW Data: A8 x%x AC x%x\n", status,
  3717. readl(phba->MBslimaddr + 0xa8),
  3718. readl(phba->MBslimaddr + 0xac));
  3719. phba->link_state = LPFC_HBA_ERROR;
  3720. return -ETIMEDOUT;
  3721. }
  3722. /* Check to see if any errors occurred during init */
  3723. if (status & HS_FFERM) {
  3724. /* ERROR: During chipset initialization */
  3725. /* Adapter failed to init, chipset, status reg
  3726. <status> */
  3727. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3728. "0437 Adapter failed to init, "
  3729. "chipset, status reg x%x, "
  3730. "FW Data: A8 x%x AC x%x\n", status,
  3731. readl(phba->MBslimaddr + 0xa8),
  3732. readl(phba->MBslimaddr + 0xac));
  3733. phba->link_state = LPFC_HBA_ERROR;
  3734. return -EIO;
  3735. }
  3736. if (i <= 10)
  3737. msleep(10);
  3738. else if (i <= 100)
  3739. msleep(100);
  3740. else
  3741. msleep(1000);
  3742. if (i == 150) {
  3743. /* Do post */
  3744. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3745. lpfc_sli_brdrestart(phba);
  3746. }
  3747. /* Read the HBA Host Status Register */
  3748. if (lpfc_readl(phba->HSregaddr, &status))
  3749. return -EIO;
  3750. }
  3751. /* Check to see if any errors occurred during init */
  3752. if (status & HS_FFERM) {
  3753. /* ERROR: During chipset initialization */
  3754. /* Adapter failed to init, chipset, status reg <status> */
  3755. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3756. "0438 Adapter failed to init, chipset, "
  3757. "status reg x%x, "
  3758. "FW Data: A8 x%x AC x%x\n", status,
  3759. readl(phba->MBslimaddr + 0xa8),
  3760. readl(phba->MBslimaddr + 0xac));
  3761. phba->link_state = LPFC_HBA_ERROR;
  3762. return -EIO;
  3763. }
  3764. /* Clear all interrupt enable conditions */
  3765. writel(0, phba->HCregaddr);
  3766. readl(phba->HCregaddr); /* flush */
  3767. /* setup host attn register */
  3768. writel(0xffffffff, phba->HAregaddr);
  3769. readl(phba->HAregaddr); /* flush */
  3770. return 0;
  3771. }
  3772. /**
  3773. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3774. *
  3775. * This function calculates and returns the number of HBQs required to be
  3776. * configured.
  3777. **/
  3778. int
  3779. lpfc_sli_hbq_count(void)
  3780. {
  3781. return ARRAY_SIZE(lpfc_hbq_defs);
  3782. }
  3783. /**
  3784. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3785. *
  3786. * This function adds the number of hbq entries in every HBQ to get
  3787. * the total number of hbq entries required for the HBA and returns
  3788. * the total count.
  3789. **/
  3790. static int
  3791. lpfc_sli_hbq_entry_count(void)
  3792. {
  3793. int hbq_count = lpfc_sli_hbq_count();
  3794. int count = 0;
  3795. int i;
  3796. for (i = 0; i < hbq_count; ++i)
  3797. count += lpfc_hbq_defs[i]->entry_count;
  3798. return count;
  3799. }
  3800. /**
  3801. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3802. *
  3803. * This function calculates amount of memory required for all hbq entries
  3804. * to be configured and returns the total memory required.
  3805. **/
  3806. int
  3807. lpfc_sli_hbq_size(void)
  3808. {
  3809. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3810. }
  3811. /**
  3812. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3813. * @phba: Pointer to HBA context object.
  3814. *
  3815. * This function is called during the SLI initialization to configure
  3816. * all the HBQs and post buffers to the HBQ. The caller is not
  3817. * required to hold any locks. This function will return zero if successful
  3818. * else it will return negative error code.
  3819. **/
  3820. static int
  3821. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3822. {
  3823. int hbq_count = lpfc_sli_hbq_count();
  3824. LPFC_MBOXQ_t *pmb;
  3825. MAILBOX_t *pmbox;
  3826. uint32_t hbqno;
  3827. uint32_t hbq_entry_index;
  3828. /* Get a Mailbox buffer to setup mailbox
  3829. * commands for HBA initialization
  3830. */
  3831. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3832. if (!pmb)
  3833. return -ENOMEM;
  3834. pmbox = &pmb->u.mb;
  3835. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3836. phba->link_state = LPFC_INIT_MBX_CMDS;
  3837. phba->hbq_in_use = 1;
  3838. hbq_entry_index = 0;
  3839. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3840. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3841. phba->hbqs[hbqno].hbqPutIdx = 0;
  3842. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3843. phba->hbqs[hbqno].entry_count =
  3844. lpfc_hbq_defs[hbqno]->entry_count;
  3845. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3846. hbq_entry_index, pmb);
  3847. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3848. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3849. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3850. mbxStatus <status>, ring <num> */
  3851. lpfc_printf_log(phba, KERN_ERR,
  3852. LOG_SLI | LOG_VPORT,
  3853. "1805 Adapter failed to init. "
  3854. "Data: x%x x%x x%x\n",
  3855. pmbox->mbxCommand,
  3856. pmbox->mbxStatus, hbqno);
  3857. phba->link_state = LPFC_HBA_ERROR;
  3858. mempool_free(pmb, phba->mbox_mem_pool);
  3859. return -ENXIO;
  3860. }
  3861. }
  3862. phba->hbq_count = hbq_count;
  3863. mempool_free(pmb, phba->mbox_mem_pool);
  3864. /* Initially populate or replenish the HBQs */
  3865. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3866. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3867. return 0;
  3868. }
  3869. /**
  3870. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3871. * @phba: Pointer to HBA context object.
  3872. *
  3873. * This function is called during the SLI initialization to configure
  3874. * all the HBQs and post buffers to the HBQ. The caller is not
  3875. * required to hold any locks. This function will return zero if successful
  3876. * else it will return negative error code.
  3877. **/
  3878. static int
  3879. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3880. {
  3881. phba->hbq_in_use = 1;
  3882. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3883. phba->hbq_count = 1;
  3884. /* Initially populate or replenish the HBQs */
  3885. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3886. return 0;
  3887. }
  3888. /**
  3889. * lpfc_sli_config_port - Issue config port mailbox command
  3890. * @phba: Pointer to HBA context object.
  3891. * @sli_mode: sli mode - 2/3
  3892. *
  3893. * This function is called by the sli intialization code path
  3894. * to issue config_port mailbox command. This function restarts the
  3895. * HBA firmware and issues a config_port mailbox command to configure
  3896. * the SLI interface in the sli mode specified by sli_mode
  3897. * variable. The caller is not required to hold any locks.
  3898. * The function returns 0 if successful, else returns negative error
  3899. * code.
  3900. **/
  3901. int
  3902. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3903. {
  3904. LPFC_MBOXQ_t *pmb;
  3905. uint32_t resetcount = 0, rc = 0, done = 0;
  3906. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3907. if (!pmb) {
  3908. phba->link_state = LPFC_HBA_ERROR;
  3909. return -ENOMEM;
  3910. }
  3911. phba->sli_rev = sli_mode;
  3912. while (resetcount < 2 && !done) {
  3913. spin_lock_irq(&phba->hbalock);
  3914. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3915. spin_unlock_irq(&phba->hbalock);
  3916. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3917. lpfc_sli_brdrestart(phba);
  3918. rc = lpfc_sli_chipset_init(phba);
  3919. if (rc)
  3920. break;
  3921. spin_lock_irq(&phba->hbalock);
  3922. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3923. spin_unlock_irq(&phba->hbalock);
  3924. resetcount++;
  3925. /* Call pre CONFIG_PORT mailbox command initialization. A
  3926. * value of 0 means the call was successful. Any other
  3927. * nonzero value is a failure, but if ERESTART is returned,
  3928. * the driver may reset the HBA and try again.
  3929. */
  3930. rc = lpfc_config_port_prep(phba);
  3931. if (rc == -ERESTART) {
  3932. phba->link_state = LPFC_LINK_UNKNOWN;
  3933. continue;
  3934. } else if (rc)
  3935. break;
  3936. phba->link_state = LPFC_INIT_MBX_CMDS;
  3937. lpfc_config_port(phba, pmb);
  3938. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3939. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3940. LPFC_SLI3_HBQ_ENABLED |
  3941. LPFC_SLI3_CRP_ENABLED |
  3942. LPFC_SLI3_BG_ENABLED |
  3943. LPFC_SLI3_DSS_ENABLED);
  3944. if (rc != MBX_SUCCESS) {
  3945. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3946. "0442 Adapter failed to init, mbxCmd x%x "
  3947. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3948. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3949. spin_lock_irq(&phba->hbalock);
  3950. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3951. spin_unlock_irq(&phba->hbalock);
  3952. rc = -ENXIO;
  3953. } else {
  3954. /* Allow asynchronous mailbox command to go through */
  3955. spin_lock_irq(&phba->hbalock);
  3956. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3957. spin_unlock_irq(&phba->hbalock);
  3958. done = 1;
  3959. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3960. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3961. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3962. "3110 Port did not grant ASABT\n");
  3963. }
  3964. }
  3965. if (!done) {
  3966. rc = -EINVAL;
  3967. goto do_prep_failed;
  3968. }
  3969. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3970. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3971. rc = -ENXIO;
  3972. goto do_prep_failed;
  3973. }
  3974. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3975. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3976. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3977. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3978. phba->max_vpi : phba->max_vports;
  3979. } else
  3980. phba->max_vpi = 0;
  3981. phba->fips_level = 0;
  3982. phba->fips_spec_rev = 0;
  3983. if (pmb->u.mb.un.varCfgPort.gdss) {
  3984. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3985. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3986. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3987. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3988. "2850 Security Crypto Active. FIPS x%d "
  3989. "(Spec Rev: x%d)",
  3990. phba->fips_level, phba->fips_spec_rev);
  3991. }
  3992. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3993. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3994. "2856 Config Port Security Crypto "
  3995. "Error: x%x ",
  3996. pmb->u.mb.un.varCfgPort.sec_err);
  3997. }
  3998. if (pmb->u.mb.un.varCfgPort.gerbm)
  3999. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4000. if (pmb->u.mb.un.varCfgPort.gcrp)
  4001. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4002. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4003. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4004. if (phba->cfg_enable_bg) {
  4005. if (pmb->u.mb.un.varCfgPort.gbg)
  4006. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4007. else
  4008. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4009. "0443 Adapter did not grant "
  4010. "BlockGuard\n");
  4011. }
  4012. } else {
  4013. phba->hbq_get = NULL;
  4014. phba->port_gp = phba->mbox->us.s2.port;
  4015. phba->max_vpi = 0;
  4016. }
  4017. do_prep_failed:
  4018. mempool_free(pmb, phba->mbox_mem_pool);
  4019. return rc;
  4020. }
  4021. /**
  4022. * lpfc_sli_hba_setup - SLI intialization function
  4023. * @phba: Pointer to HBA context object.
  4024. *
  4025. * This function is the main SLI intialization function. This function
  4026. * is called by the HBA intialization code, HBA reset code and HBA
  4027. * error attention handler code. Caller is not required to hold any
  4028. * locks. This function issues config_port mailbox command to configure
  4029. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4030. * calls the config_port_post function to issue init_link mailbox
  4031. * command and to start the discovery. The function will return zero
  4032. * if successful, else it will return negative error code.
  4033. **/
  4034. int
  4035. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4036. {
  4037. uint32_t rc;
  4038. int mode = 3, i;
  4039. int longs;
  4040. switch (lpfc_sli_mode) {
  4041. case 2:
  4042. if (phba->cfg_enable_npiv) {
  4043. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4044. "1824 NPIV enabled: Override lpfc_sli_mode "
  4045. "parameter (%d) to auto (0).\n",
  4046. lpfc_sli_mode);
  4047. break;
  4048. }
  4049. mode = 2;
  4050. break;
  4051. case 0:
  4052. case 3:
  4053. break;
  4054. default:
  4055. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4056. "1819 Unrecognized lpfc_sli_mode "
  4057. "parameter: %d.\n", lpfc_sli_mode);
  4058. break;
  4059. }
  4060. rc = lpfc_sli_config_port(phba, mode);
  4061. if (rc && lpfc_sli_mode == 3)
  4062. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4063. "1820 Unable to select SLI-3. "
  4064. "Not supported by adapter.\n");
  4065. if (rc && mode != 2)
  4066. rc = lpfc_sli_config_port(phba, 2);
  4067. if (rc)
  4068. goto lpfc_sli_hba_setup_error;
  4069. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4070. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4071. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4072. if (!rc) {
  4073. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4074. "2709 This device supports "
  4075. "Advanced Error Reporting (AER)\n");
  4076. spin_lock_irq(&phba->hbalock);
  4077. phba->hba_flag |= HBA_AER_ENABLED;
  4078. spin_unlock_irq(&phba->hbalock);
  4079. } else {
  4080. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4081. "2708 This device does not support "
  4082. "Advanced Error Reporting (AER)\n");
  4083. phba->cfg_aer_support = 0;
  4084. }
  4085. }
  4086. if (phba->sli_rev == 3) {
  4087. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4088. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4089. } else {
  4090. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4091. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4092. phba->sli3_options = 0;
  4093. }
  4094. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4095. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4096. phba->sli_rev, phba->max_vpi);
  4097. rc = lpfc_sli_ring_map(phba);
  4098. if (rc)
  4099. goto lpfc_sli_hba_setup_error;
  4100. /* Initialize VPIs. */
  4101. if (phba->sli_rev == LPFC_SLI_REV3) {
  4102. /*
  4103. * The VPI bitmask and physical ID array are allocated
  4104. * and initialized once only - at driver load. A port
  4105. * reset doesn't need to reinitialize this memory.
  4106. */
  4107. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4108. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4109. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4110. GFP_KERNEL);
  4111. if (!phba->vpi_bmask) {
  4112. rc = -ENOMEM;
  4113. goto lpfc_sli_hba_setup_error;
  4114. }
  4115. phba->vpi_ids = kzalloc(
  4116. (phba->max_vpi+1) * sizeof(uint16_t),
  4117. GFP_KERNEL);
  4118. if (!phba->vpi_ids) {
  4119. kfree(phba->vpi_bmask);
  4120. rc = -ENOMEM;
  4121. goto lpfc_sli_hba_setup_error;
  4122. }
  4123. for (i = 0; i < phba->max_vpi; i++)
  4124. phba->vpi_ids[i] = i;
  4125. }
  4126. }
  4127. /* Init HBQs */
  4128. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4129. rc = lpfc_sli_hbq_setup(phba);
  4130. if (rc)
  4131. goto lpfc_sli_hba_setup_error;
  4132. }
  4133. spin_lock_irq(&phba->hbalock);
  4134. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4135. spin_unlock_irq(&phba->hbalock);
  4136. rc = lpfc_config_port_post(phba);
  4137. if (rc)
  4138. goto lpfc_sli_hba_setup_error;
  4139. return rc;
  4140. lpfc_sli_hba_setup_error:
  4141. phba->link_state = LPFC_HBA_ERROR;
  4142. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4143. "0445 Firmware initialization failed\n");
  4144. return rc;
  4145. }
  4146. /**
  4147. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4148. * @phba: Pointer to HBA context object.
  4149. * @mboxq: mailbox pointer.
  4150. * This function issue a dump mailbox command to read config region
  4151. * 23 and parse the records in the region and populate driver
  4152. * data structure.
  4153. **/
  4154. static int
  4155. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4156. {
  4157. LPFC_MBOXQ_t *mboxq;
  4158. struct lpfc_dmabuf *mp;
  4159. struct lpfc_mqe *mqe;
  4160. uint32_t data_length;
  4161. int rc;
  4162. /* Program the default value of vlan_id and fc_map */
  4163. phba->valid_vlan = 0;
  4164. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4165. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4166. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4167. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4168. if (!mboxq)
  4169. return -ENOMEM;
  4170. mqe = &mboxq->u.mqe;
  4171. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4172. rc = -ENOMEM;
  4173. goto out_free_mboxq;
  4174. }
  4175. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4176. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4177. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4178. "(%d):2571 Mailbox cmd x%x Status x%x "
  4179. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4180. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4181. "CQ: x%x x%x x%x x%x\n",
  4182. mboxq->vport ? mboxq->vport->vpi : 0,
  4183. bf_get(lpfc_mqe_command, mqe),
  4184. bf_get(lpfc_mqe_status, mqe),
  4185. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4186. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4187. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4188. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4189. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4190. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4191. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4192. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4193. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4194. mboxq->mcqe.word0,
  4195. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4196. mboxq->mcqe.trailer);
  4197. if (rc) {
  4198. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4199. kfree(mp);
  4200. rc = -EIO;
  4201. goto out_free_mboxq;
  4202. }
  4203. data_length = mqe->un.mb_words[5];
  4204. if (data_length > DMP_RGN23_SIZE) {
  4205. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4206. kfree(mp);
  4207. rc = -EIO;
  4208. goto out_free_mboxq;
  4209. }
  4210. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4211. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4212. kfree(mp);
  4213. rc = 0;
  4214. out_free_mboxq:
  4215. mempool_free(mboxq, phba->mbox_mem_pool);
  4216. return rc;
  4217. }
  4218. /**
  4219. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4220. * @phba: pointer to lpfc hba data structure.
  4221. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4222. * @vpd: pointer to the memory to hold resulting port vpd data.
  4223. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4224. * On output, the number of data bytes in @vpd.
  4225. *
  4226. * This routine executes a READ_REV SLI4 mailbox command. In
  4227. * addition, this routine gets the port vpd data.
  4228. *
  4229. * Return codes
  4230. * 0 - successful
  4231. * -ENOMEM - could not allocated memory.
  4232. **/
  4233. static int
  4234. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4235. uint8_t *vpd, uint32_t *vpd_size)
  4236. {
  4237. int rc = 0;
  4238. uint32_t dma_size;
  4239. struct lpfc_dmabuf *dmabuf;
  4240. struct lpfc_mqe *mqe;
  4241. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4242. if (!dmabuf)
  4243. return -ENOMEM;
  4244. /*
  4245. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4246. * mailbox command.
  4247. */
  4248. dma_size = *vpd_size;
  4249. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4250. dma_size,
  4251. &dmabuf->phys,
  4252. GFP_KERNEL);
  4253. if (!dmabuf->virt) {
  4254. kfree(dmabuf);
  4255. return -ENOMEM;
  4256. }
  4257. memset(dmabuf->virt, 0, dma_size);
  4258. /*
  4259. * The SLI4 implementation of READ_REV conflicts at word1,
  4260. * bits 31:16 and SLI4 adds vpd functionality not present
  4261. * in SLI3. This code corrects the conflicts.
  4262. */
  4263. lpfc_read_rev(phba, mboxq);
  4264. mqe = &mboxq->u.mqe;
  4265. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4266. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4267. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4268. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4269. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4270. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4271. if (rc) {
  4272. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4273. dmabuf->virt, dmabuf->phys);
  4274. kfree(dmabuf);
  4275. return -EIO;
  4276. }
  4277. /*
  4278. * The available vpd length cannot be bigger than the
  4279. * DMA buffer passed to the port. Catch the less than
  4280. * case and update the caller's size.
  4281. */
  4282. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4283. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4284. memcpy(vpd, dmabuf->virt, *vpd_size);
  4285. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4286. dmabuf->virt, dmabuf->phys);
  4287. kfree(dmabuf);
  4288. return 0;
  4289. }
  4290. /**
  4291. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4292. * @phba: pointer to lpfc hba data structure.
  4293. *
  4294. * This routine retrieves SLI4 device physical port name this PCI function
  4295. * is attached to.
  4296. *
  4297. * Return codes
  4298. * 0 - sucessful
  4299. * otherwise - failed to retrieve physical port name
  4300. **/
  4301. static int
  4302. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4303. {
  4304. LPFC_MBOXQ_t *mboxq;
  4305. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4306. struct lpfc_controller_attribute *cntl_attr;
  4307. struct lpfc_mbx_get_port_name *get_port_name;
  4308. void *virtaddr = NULL;
  4309. uint32_t alloclen, reqlen;
  4310. uint32_t shdr_status, shdr_add_status;
  4311. union lpfc_sli4_cfg_shdr *shdr;
  4312. char cport_name = 0;
  4313. int rc;
  4314. /* We assume nothing at this point */
  4315. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4316. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4317. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4318. if (!mboxq)
  4319. return -ENOMEM;
  4320. /* obtain link type and link number via READ_CONFIG */
  4321. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4322. lpfc_sli4_read_config(phba);
  4323. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4324. goto retrieve_ppname;
  4325. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4326. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4327. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4328. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4329. LPFC_SLI4_MBX_NEMBED);
  4330. if (alloclen < reqlen) {
  4331. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4332. "3084 Allocated DMA memory size (%d) is "
  4333. "less than the requested DMA memory size "
  4334. "(%d)\n", alloclen, reqlen);
  4335. rc = -ENOMEM;
  4336. goto out_free_mboxq;
  4337. }
  4338. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4339. virtaddr = mboxq->sge_array->addr[0];
  4340. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4341. shdr = &mbx_cntl_attr->cfg_shdr;
  4342. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4343. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4344. if (shdr_status || shdr_add_status || rc) {
  4345. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4346. "3085 Mailbox x%x (x%x/x%x) failed, "
  4347. "rc:x%x, status:x%x, add_status:x%x\n",
  4348. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4349. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4350. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4351. rc, shdr_status, shdr_add_status);
  4352. rc = -ENXIO;
  4353. goto out_free_mboxq;
  4354. }
  4355. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4356. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4357. phba->sli4_hba.lnk_info.lnk_tp =
  4358. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4359. phba->sli4_hba.lnk_info.lnk_no =
  4360. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4361. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4362. "3086 lnk_type:%d, lnk_numb:%d\n",
  4363. phba->sli4_hba.lnk_info.lnk_tp,
  4364. phba->sli4_hba.lnk_info.lnk_no);
  4365. retrieve_ppname:
  4366. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4367. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4368. sizeof(struct lpfc_mbx_get_port_name) -
  4369. sizeof(struct lpfc_sli4_cfg_mhdr),
  4370. LPFC_SLI4_MBX_EMBED);
  4371. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4372. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4373. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4374. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4375. phba->sli4_hba.lnk_info.lnk_tp);
  4376. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4377. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4378. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4379. if (shdr_status || shdr_add_status || rc) {
  4380. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4381. "3087 Mailbox x%x (x%x/x%x) failed: "
  4382. "rc:x%x, status:x%x, add_status:x%x\n",
  4383. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4384. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4385. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4386. rc, shdr_status, shdr_add_status);
  4387. rc = -ENXIO;
  4388. goto out_free_mboxq;
  4389. }
  4390. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4391. case LPFC_LINK_NUMBER_0:
  4392. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4393. &get_port_name->u.response);
  4394. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4395. break;
  4396. case LPFC_LINK_NUMBER_1:
  4397. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4398. &get_port_name->u.response);
  4399. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4400. break;
  4401. case LPFC_LINK_NUMBER_2:
  4402. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4403. &get_port_name->u.response);
  4404. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4405. break;
  4406. case LPFC_LINK_NUMBER_3:
  4407. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4408. &get_port_name->u.response);
  4409. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4410. break;
  4411. default:
  4412. break;
  4413. }
  4414. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4415. phba->Port[0] = cport_name;
  4416. phba->Port[1] = '\0';
  4417. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4418. "3091 SLI get port name: %s\n", phba->Port);
  4419. }
  4420. out_free_mboxq:
  4421. if (rc != MBX_TIMEOUT) {
  4422. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4423. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4424. else
  4425. mempool_free(mboxq, phba->mbox_mem_pool);
  4426. }
  4427. return rc;
  4428. }
  4429. /**
  4430. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4431. * @phba: pointer to lpfc hba data structure.
  4432. *
  4433. * This routine is called to explicitly arm the SLI4 device's completion and
  4434. * event queues
  4435. **/
  4436. static void
  4437. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4438. {
  4439. uint8_t fcp_eqidx;
  4440. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4441. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4442. fcp_eqidx = 0;
  4443. if (phba->sli4_hba.fcp_cq) {
  4444. do
  4445. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4446. LPFC_QUEUE_REARM);
  4447. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4448. }
  4449. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4450. if (phba->sli4_hba.fp_eq) {
  4451. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4452. fcp_eqidx++)
  4453. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4454. LPFC_QUEUE_REARM);
  4455. }
  4456. }
  4457. /**
  4458. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4459. * @phba: Pointer to HBA context object.
  4460. * @type: The resource extent type.
  4461. * @extnt_count: buffer to hold port available extent count.
  4462. * @extnt_size: buffer to hold element count per extent.
  4463. *
  4464. * This function calls the port and retrievs the number of available
  4465. * extents and their size for a particular extent type.
  4466. *
  4467. * Returns: 0 if successful. Nonzero otherwise.
  4468. **/
  4469. int
  4470. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4471. uint16_t *extnt_count, uint16_t *extnt_size)
  4472. {
  4473. int rc = 0;
  4474. uint32_t length;
  4475. uint32_t mbox_tmo;
  4476. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4477. LPFC_MBOXQ_t *mbox;
  4478. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4479. if (!mbox)
  4480. return -ENOMEM;
  4481. /* Find out how many extents are available for this resource type */
  4482. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4483. sizeof(struct lpfc_sli4_cfg_mhdr));
  4484. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4485. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4486. length, LPFC_SLI4_MBX_EMBED);
  4487. /* Send an extents count of 0 - the GET doesn't use it. */
  4488. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4489. LPFC_SLI4_MBX_EMBED);
  4490. if (unlikely(rc)) {
  4491. rc = -EIO;
  4492. goto err_exit;
  4493. }
  4494. if (!phba->sli4_hba.intr_enable)
  4495. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4496. else {
  4497. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4498. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4499. }
  4500. if (unlikely(rc)) {
  4501. rc = -EIO;
  4502. goto err_exit;
  4503. }
  4504. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4505. if (bf_get(lpfc_mbox_hdr_status,
  4506. &rsrc_info->header.cfg_shdr.response)) {
  4507. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4508. "2930 Failed to get resource extents "
  4509. "Status 0x%x Add'l Status 0x%x\n",
  4510. bf_get(lpfc_mbox_hdr_status,
  4511. &rsrc_info->header.cfg_shdr.response),
  4512. bf_get(lpfc_mbox_hdr_add_status,
  4513. &rsrc_info->header.cfg_shdr.response));
  4514. rc = -EIO;
  4515. goto err_exit;
  4516. }
  4517. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4518. &rsrc_info->u.rsp);
  4519. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4520. &rsrc_info->u.rsp);
  4521. err_exit:
  4522. mempool_free(mbox, phba->mbox_mem_pool);
  4523. return rc;
  4524. }
  4525. /**
  4526. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4527. * @phba: Pointer to HBA context object.
  4528. * @type: The extent type to check.
  4529. *
  4530. * This function reads the current available extents from the port and checks
  4531. * if the extent count or extent size has changed since the last access.
  4532. * Callers use this routine post port reset to understand if there is a
  4533. * extent reprovisioning requirement.
  4534. *
  4535. * Returns:
  4536. * -Error: error indicates problem.
  4537. * 1: Extent count or size has changed.
  4538. * 0: No changes.
  4539. **/
  4540. static int
  4541. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4542. {
  4543. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4544. uint16_t size_diff, rsrc_ext_size;
  4545. int rc = 0;
  4546. struct lpfc_rsrc_blks *rsrc_entry;
  4547. struct list_head *rsrc_blk_list = NULL;
  4548. size_diff = 0;
  4549. curr_ext_cnt = 0;
  4550. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4551. &rsrc_ext_cnt,
  4552. &rsrc_ext_size);
  4553. if (unlikely(rc))
  4554. return -EIO;
  4555. switch (type) {
  4556. case LPFC_RSC_TYPE_FCOE_RPI:
  4557. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4558. break;
  4559. case LPFC_RSC_TYPE_FCOE_VPI:
  4560. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4561. break;
  4562. case LPFC_RSC_TYPE_FCOE_XRI:
  4563. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4564. break;
  4565. case LPFC_RSC_TYPE_FCOE_VFI:
  4566. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4567. break;
  4568. default:
  4569. break;
  4570. }
  4571. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4572. curr_ext_cnt++;
  4573. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4574. size_diff++;
  4575. }
  4576. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4577. rc = 1;
  4578. return rc;
  4579. }
  4580. /**
  4581. * lpfc_sli4_cfg_post_extnts -
  4582. * @phba: Pointer to HBA context object.
  4583. * @extnt_cnt - number of available extents.
  4584. * @type - the extent type (rpi, xri, vfi, vpi).
  4585. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4586. * @mbox - pointer to the caller's allocated mailbox structure.
  4587. *
  4588. * This function executes the extents allocation request. It also
  4589. * takes care of the amount of memory needed to allocate or get the
  4590. * allocated extents. It is the caller's responsibility to evaluate
  4591. * the response.
  4592. *
  4593. * Returns:
  4594. * -Error: Error value describes the condition found.
  4595. * 0: if successful
  4596. **/
  4597. static int
  4598. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
  4599. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4600. {
  4601. int rc = 0;
  4602. uint32_t req_len;
  4603. uint32_t emb_len;
  4604. uint32_t alloc_len, mbox_tmo;
  4605. /* Calculate the total requested length of the dma memory */
  4606. req_len = *extnt_cnt * sizeof(uint16_t);
  4607. /*
  4608. * Calculate the size of an embedded mailbox. The uint32_t
  4609. * accounts for extents-specific word.
  4610. */
  4611. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4612. sizeof(uint32_t);
  4613. /*
  4614. * Presume the allocation and response will fit into an embedded
  4615. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4616. */
  4617. *emb = LPFC_SLI4_MBX_EMBED;
  4618. if (req_len > emb_len) {
  4619. req_len = *extnt_cnt * sizeof(uint16_t) +
  4620. sizeof(union lpfc_sli4_cfg_shdr) +
  4621. sizeof(uint32_t);
  4622. *emb = LPFC_SLI4_MBX_NEMBED;
  4623. }
  4624. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4625. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4626. req_len, *emb);
  4627. if (alloc_len < req_len) {
  4628. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4629. "2982 Allocated DMA memory size (x%x) is "
  4630. "less than the requested DMA memory "
  4631. "size (x%x)\n", alloc_len, req_len);
  4632. return -ENOMEM;
  4633. }
  4634. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
  4635. if (unlikely(rc))
  4636. return -EIO;
  4637. if (!phba->sli4_hba.intr_enable)
  4638. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4639. else {
  4640. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4641. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4642. }
  4643. if (unlikely(rc))
  4644. rc = -EIO;
  4645. return rc;
  4646. }
  4647. /**
  4648. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4649. * @phba: Pointer to HBA context object.
  4650. * @type: The resource extent type to allocate.
  4651. *
  4652. * This function allocates the number of elements for the specified
  4653. * resource type.
  4654. **/
  4655. static int
  4656. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4657. {
  4658. bool emb = false;
  4659. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4660. uint16_t rsrc_id, rsrc_start, j, k;
  4661. uint16_t *ids;
  4662. int i, rc;
  4663. unsigned long longs;
  4664. unsigned long *bmask;
  4665. struct lpfc_rsrc_blks *rsrc_blks;
  4666. LPFC_MBOXQ_t *mbox;
  4667. uint32_t length;
  4668. struct lpfc_id_range *id_array = NULL;
  4669. void *virtaddr = NULL;
  4670. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4671. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4672. struct list_head *ext_blk_list;
  4673. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4674. &rsrc_cnt,
  4675. &rsrc_size);
  4676. if (unlikely(rc))
  4677. return -EIO;
  4678. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4679. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4680. "3009 No available Resource Extents "
  4681. "for resource type 0x%x: Count: 0x%x, "
  4682. "Size 0x%x\n", type, rsrc_cnt,
  4683. rsrc_size);
  4684. return -ENOMEM;
  4685. }
  4686. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
  4687. "2903 Available Resource Extents "
  4688. "for resource type 0x%x: Count: 0x%x, "
  4689. "Size 0x%x\n", type, rsrc_cnt,
  4690. rsrc_size);
  4691. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4692. if (!mbox)
  4693. return -ENOMEM;
  4694. rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
  4695. if (unlikely(rc)) {
  4696. rc = -EIO;
  4697. goto err_exit;
  4698. }
  4699. /*
  4700. * Figure out where the response is located. Then get local pointers
  4701. * to the response data. The port does not guarantee to respond to
  4702. * all extents counts request so update the local variable with the
  4703. * allocated count from the port.
  4704. */
  4705. if (emb == LPFC_SLI4_MBX_EMBED) {
  4706. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4707. id_array = &rsrc_ext->u.rsp.id[0];
  4708. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4709. } else {
  4710. virtaddr = mbox->sge_array->addr[0];
  4711. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4712. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4713. id_array = &n_rsrc->id;
  4714. }
  4715. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4716. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4717. /*
  4718. * Based on the resource size and count, correct the base and max
  4719. * resource values.
  4720. */
  4721. length = sizeof(struct lpfc_rsrc_blks);
  4722. switch (type) {
  4723. case LPFC_RSC_TYPE_FCOE_RPI:
  4724. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4725. sizeof(unsigned long),
  4726. GFP_KERNEL);
  4727. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4728. rc = -ENOMEM;
  4729. goto err_exit;
  4730. }
  4731. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4732. sizeof(uint16_t),
  4733. GFP_KERNEL);
  4734. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4735. kfree(phba->sli4_hba.rpi_bmask);
  4736. rc = -ENOMEM;
  4737. goto err_exit;
  4738. }
  4739. /*
  4740. * The next_rpi was initialized with the maximum available
  4741. * count but the port may allocate a smaller number. Catch
  4742. * that case and update the next_rpi.
  4743. */
  4744. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4745. /* Initialize local ptrs for common extent processing later. */
  4746. bmask = phba->sli4_hba.rpi_bmask;
  4747. ids = phba->sli4_hba.rpi_ids;
  4748. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4749. break;
  4750. case LPFC_RSC_TYPE_FCOE_VPI:
  4751. phba->vpi_bmask = kzalloc(longs *
  4752. sizeof(unsigned long),
  4753. GFP_KERNEL);
  4754. if (unlikely(!phba->vpi_bmask)) {
  4755. rc = -ENOMEM;
  4756. goto err_exit;
  4757. }
  4758. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4759. sizeof(uint16_t),
  4760. GFP_KERNEL);
  4761. if (unlikely(!phba->vpi_ids)) {
  4762. kfree(phba->vpi_bmask);
  4763. rc = -ENOMEM;
  4764. goto err_exit;
  4765. }
  4766. /* Initialize local ptrs for common extent processing later. */
  4767. bmask = phba->vpi_bmask;
  4768. ids = phba->vpi_ids;
  4769. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4770. break;
  4771. case LPFC_RSC_TYPE_FCOE_XRI:
  4772. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4773. sizeof(unsigned long),
  4774. GFP_KERNEL);
  4775. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4776. rc = -ENOMEM;
  4777. goto err_exit;
  4778. }
  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. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4939. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4940. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4941. list_del_init(&rsrc_blk->list);
  4942. kfree(rsrc_blk);
  4943. }
  4944. break;
  4945. case LPFC_RSC_TYPE_FCOE_VFI:
  4946. kfree(phba->sli4_hba.vfi_bmask);
  4947. kfree(phba->sli4_hba.vfi_ids);
  4948. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4949. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4950. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4951. list_del_init(&rsrc_blk->list);
  4952. kfree(rsrc_blk);
  4953. }
  4954. break;
  4955. case LPFC_RSC_TYPE_FCOE_RPI:
  4956. /* RPI bitmask and physical id array are cleaned up earlier. */
  4957. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4958. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4959. list_del_init(&rsrc_blk->list);
  4960. kfree(rsrc_blk);
  4961. }
  4962. break;
  4963. default:
  4964. break;
  4965. }
  4966. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4967. out_free_mbox:
  4968. mempool_free(mbox, phba->mbox_mem_pool);
  4969. return rc;
  4970. }
  4971. /**
  4972. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4973. * @phba: Pointer to HBA context object.
  4974. *
  4975. * This function allocates all SLI4 resource identifiers.
  4976. **/
  4977. int
  4978. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4979. {
  4980. int i, rc, error = 0;
  4981. uint16_t count, base;
  4982. unsigned long longs;
  4983. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4984. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4985. if (phba->sli4_hba.extents_in_use) {
  4986. /*
  4987. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4988. * resource extent count must be read and allocated before
  4989. * provisioning the resource id arrays.
  4990. */
  4991. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4992. LPFC_IDX_RSRC_RDY) {
  4993. /*
  4994. * Extent-based resources are set - the driver could
  4995. * be in a port reset. Figure out if any corrective
  4996. * actions need to be taken.
  4997. */
  4998. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4999. LPFC_RSC_TYPE_FCOE_VFI);
  5000. if (rc != 0)
  5001. error++;
  5002. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5003. LPFC_RSC_TYPE_FCOE_VPI);
  5004. if (rc != 0)
  5005. error++;
  5006. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5007. LPFC_RSC_TYPE_FCOE_XRI);
  5008. if (rc != 0)
  5009. error++;
  5010. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5011. LPFC_RSC_TYPE_FCOE_RPI);
  5012. if (rc != 0)
  5013. error++;
  5014. /*
  5015. * It's possible that the number of resources
  5016. * provided to this port instance changed between
  5017. * resets. Detect this condition and reallocate
  5018. * resources. Otherwise, there is no action.
  5019. */
  5020. if (error) {
  5021. lpfc_printf_log(phba, KERN_INFO,
  5022. LOG_MBOX | LOG_INIT,
  5023. "2931 Detected extent resource "
  5024. "change. Reallocating all "
  5025. "extents.\n");
  5026. rc = lpfc_sli4_dealloc_extent(phba,
  5027. LPFC_RSC_TYPE_FCOE_VFI);
  5028. rc = lpfc_sli4_dealloc_extent(phba,
  5029. LPFC_RSC_TYPE_FCOE_VPI);
  5030. rc = lpfc_sli4_dealloc_extent(phba,
  5031. LPFC_RSC_TYPE_FCOE_XRI);
  5032. rc = lpfc_sli4_dealloc_extent(phba,
  5033. LPFC_RSC_TYPE_FCOE_RPI);
  5034. } else
  5035. return 0;
  5036. }
  5037. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5038. if (unlikely(rc))
  5039. goto err_exit;
  5040. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5041. if (unlikely(rc))
  5042. goto err_exit;
  5043. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5044. if (unlikely(rc))
  5045. goto err_exit;
  5046. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5047. if (unlikely(rc))
  5048. goto err_exit;
  5049. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5050. LPFC_IDX_RSRC_RDY);
  5051. return rc;
  5052. } else {
  5053. /*
  5054. * The port does not support resource extents. The XRI, VPI,
  5055. * VFI, RPI resource ids were determined from READ_CONFIG.
  5056. * Just allocate the bitmasks and provision the resource id
  5057. * arrays. If a port reset is active, the resources don't
  5058. * need any action - just exit.
  5059. */
  5060. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5061. LPFC_IDX_RSRC_RDY) {
  5062. lpfc_sli4_dealloc_resource_identifiers(phba);
  5063. lpfc_sli4_remove_rpis(phba);
  5064. }
  5065. /* RPIs. */
  5066. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5067. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5068. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5069. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5070. sizeof(unsigned long),
  5071. GFP_KERNEL);
  5072. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5073. rc = -ENOMEM;
  5074. goto err_exit;
  5075. }
  5076. phba->sli4_hba.rpi_ids = kzalloc(count *
  5077. sizeof(uint16_t),
  5078. GFP_KERNEL);
  5079. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5080. rc = -ENOMEM;
  5081. goto free_rpi_bmask;
  5082. }
  5083. for (i = 0; i < count; i++)
  5084. phba->sli4_hba.rpi_ids[i] = base + i;
  5085. lpfc_sli4_node_prep(phba);
  5086. /* VPIs. */
  5087. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5088. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5089. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5090. phba->vpi_bmask = kzalloc(longs *
  5091. sizeof(unsigned long),
  5092. GFP_KERNEL);
  5093. if (unlikely(!phba->vpi_bmask)) {
  5094. rc = -ENOMEM;
  5095. goto free_rpi_ids;
  5096. }
  5097. phba->vpi_ids = kzalloc(count *
  5098. sizeof(uint16_t),
  5099. GFP_KERNEL);
  5100. if (unlikely(!phba->vpi_ids)) {
  5101. rc = -ENOMEM;
  5102. goto free_vpi_bmask;
  5103. }
  5104. for (i = 0; i < count; i++)
  5105. phba->vpi_ids[i] = base + i;
  5106. /* XRIs. */
  5107. count = phba->sli4_hba.max_cfg_param.max_xri;
  5108. base = phba->sli4_hba.max_cfg_param.xri_base;
  5109. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5110. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5111. sizeof(unsigned long),
  5112. GFP_KERNEL);
  5113. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5114. rc = -ENOMEM;
  5115. goto free_vpi_ids;
  5116. }
  5117. phba->sli4_hba.xri_ids = kzalloc(count *
  5118. sizeof(uint16_t),
  5119. GFP_KERNEL);
  5120. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5121. rc = -ENOMEM;
  5122. goto free_xri_bmask;
  5123. }
  5124. for (i = 0; i < count; i++)
  5125. phba->sli4_hba.xri_ids[i] = base + i;
  5126. /* VFIs. */
  5127. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5128. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5129. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5130. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5131. sizeof(unsigned long),
  5132. GFP_KERNEL);
  5133. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5134. rc = -ENOMEM;
  5135. goto free_xri_ids;
  5136. }
  5137. phba->sli4_hba.vfi_ids = kzalloc(count *
  5138. sizeof(uint16_t),
  5139. GFP_KERNEL);
  5140. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5141. rc = -ENOMEM;
  5142. goto free_vfi_bmask;
  5143. }
  5144. for (i = 0; i < count; i++)
  5145. phba->sli4_hba.vfi_ids[i] = base + i;
  5146. /*
  5147. * Mark all resources ready. An HBA reset doesn't need
  5148. * to reset the initialization.
  5149. */
  5150. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5151. LPFC_IDX_RSRC_RDY);
  5152. return 0;
  5153. }
  5154. free_vfi_bmask:
  5155. kfree(phba->sli4_hba.vfi_bmask);
  5156. free_xri_ids:
  5157. kfree(phba->sli4_hba.xri_ids);
  5158. free_xri_bmask:
  5159. kfree(phba->sli4_hba.xri_bmask);
  5160. free_vpi_ids:
  5161. kfree(phba->vpi_ids);
  5162. free_vpi_bmask:
  5163. kfree(phba->vpi_bmask);
  5164. free_rpi_ids:
  5165. kfree(phba->sli4_hba.rpi_ids);
  5166. free_rpi_bmask:
  5167. kfree(phba->sli4_hba.rpi_bmask);
  5168. err_exit:
  5169. return rc;
  5170. }
  5171. /**
  5172. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5173. * @phba: Pointer to HBA context object.
  5174. *
  5175. * This function allocates the number of elements for the specified
  5176. * resource type.
  5177. **/
  5178. int
  5179. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5180. {
  5181. if (phba->sli4_hba.extents_in_use) {
  5182. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5183. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5184. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5185. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5186. } else {
  5187. kfree(phba->vpi_bmask);
  5188. kfree(phba->vpi_ids);
  5189. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5190. kfree(phba->sli4_hba.xri_bmask);
  5191. kfree(phba->sli4_hba.xri_ids);
  5192. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5193. kfree(phba->sli4_hba.vfi_bmask);
  5194. kfree(phba->sli4_hba.vfi_ids);
  5195. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5196. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5197. }
  5198. return 0;
  5199. }
  5200. /**
  5201. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5202. * @phba: Pointer to HBA context object.
  5203. * @type: The resource extent type.
  5204. * @extnt_count: buffer to hold port extent count response
  5205. * @extnt_size: buffer to hold port extent size response.
  5206. *
  5207. * This function calls the port to read the host allocated extents
  5208. * for a particular type.
  5209. **/
  5210. int
  5211. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5212. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5213. {
  5214. bool emb;
  5215. int rc = 0;
  5216. uint16_t curr_blks = 0;
  5217. uint32_t req_len, emb_len;
  5218. uint32_t alloc_len, mbox_tmo;
  5219. struct list_head *blk_list_head;
  5220. struct lpfc_rsrc_blks *rsrc_blk;
  5221. LPFC_MBOXQ_t *mbox;
  5222. void *virtaddr = NULL;
  5223. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5224. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5225. union lpfc_sli4_cfg_shdr *shdr;
  5226. switch (type) {
  5227. case LPFC_RSC_TYPE_FCOE_VPI:
  5228. blk_list_head = &phba->lpfc_vpi_blk_list;
  5229. break;
  5230. case LPFC_RSC_TYPE_FCOE_XRI:
  5231. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5232. break;
  5233. case LPFC_RSC_TYPE_FCOE_VFI:
  5234. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5235. break;
  5236. case LPFC_RSC_TYPE_FCOE_RPI:
  5237. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5238. break;
  5239. default:
  5240. return -EIO;
  5241. }
  5242. /* Count the number of extents currently allocatd for this type. */
  5243. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5244. if (curr_blks == 0) {
  5245. /*
  5246. * The GET_ALLOCATED mailbox does not return the size,
  5247. * just the count. The size should be just the size
  5248. * stored in the current allocated block and all sizes
  5249. * for an extent type are the same so set the return
  5250. * value now.
  5251. */
  5252. *extnt_size = rsrc_blk->rsrc_size;
  5253. }
  5254. curr_blks++;
  5255. }
  5256. /* Calculate the total requested length of the dma memory. */
  5257. req_len = curr_blks * sizeof(uint16_t);
  5258. /*
  5259. * Calculate the size of an embedded mailbox. The uint32_t
  5260. * accounts for extents-specific word.
  5261. */
  5262. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5263. sizeof(uint32_t);
  5264. /*
  5265. * Presume the allocation and response will fit into an embedded
  5266. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5267. */
  5268. emb = LPFC_SLI4_MBX_EMBED;
  5269. req_len = emb_len;
  5270. if (req_len > emb_len) {
  5271. req_len = curr_blks * sizeof(uint16_t) +
  5272. sizeof(union lpfc_sli4_cfg_shdr) +
  5273. sizeof(uint32_t);
  5274. emb = LPFC_SLI4_MBX_NEMBED;
  5275. }
  5276. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5277. if (!mbox)
  5278. return -ENOMEM;
  5279. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5280. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5281. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5282. req_len, emb);
  5283. if (alloc_len < req_len) {
  5284. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5285. "2983 Allocated DMA memory size (x%x) is "
  5286. "less than the requested DMA memory "
  5287. "size (x%x)\n", alloc_len, req_len);
  5288. rc = -ENOMEM;
  5289. goto err_exit;
  5290. }
  5291. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5292. if (unlikely(rc)) {
  5293. rc = -EIO;
  5294. goto err_exit;
  5295. }
  5296. if (!phba->sli4_hba.intr_enable)
  5297. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5298. else {
  5299. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5300. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5301. }
  5302. if (unlikely(rc)) {
  5303. rc = -EIO;
  5304. goto err_exit;
  5305. }
  5306. /*
  5307. * Figure out where the response is located. Then get local pointers
  5308. * to the response data. The port does not guarantee to respond to
  5309. * all extents counts request so update the local variable with the
  5310. * allocated count from the port.
  5311. */
  5312. if (emb == LPFC_SLI4_MBX_EMBED) {
  5313. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5314. shdr = &rsrc_ext->header.cfg_shdr;
  5315. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5316. } else {
  5317. virtaddr = mbox->sge_array->addr[0];
  5318. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5319. shdr = &n_rsrc->cfg_shdr;
  5320. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5321. }
  5322. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5323. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5324. "2984 Failed to read allocated resources "
  5325. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5326. type,
  5327. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5328. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5329. rc = -EIO;
  5330. goto err_exit;
  5331. }
  5332. err_exit:
  5333. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5334. return rc;
  5335. }
  5336. /**
  5337. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5338. * @phba: Pointer to HBA context object.
  5339. *
  5340. * This function is the main SLI4 device intialization PCI function. This
  5341. * function is called by the HBA intialization code, HBA reset code and
  5342. * HBA error attention handler code. Caller is not required to hold any
  5343. * locks.
  5344. **/
  5345. int
  5346. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5347. {
  5348. int rc;
  5349. LPFC_MBOXQ_t *mboxq;
  5350. struct lpfc_mqe *mqe;
  5351. uint8_t *vpd;
  5352. uint32_t vpd_size;
  5353. uint32_t ftr_rsp = 0;
  5354. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5355. struct lpfc_vport *vport = phba->pport;
  5356. struct lpfc_dmabuf *mp;
  5357. /* Perform a PCI function reset to start from clean */
  5358. rc = lpfc_pci_function_reset(phba);
  5359. if (unlikely(rc))
  5360. return -ENODEV;
  5361. /* Check the HBA Host Status Register for readyness */
  5362. rc = lpfc_sli4_post_status_check(phba);
  5363. if (unlikely(rc))
  5364. return -ENODEV;
  5365. else {
  5366. spin_lock_irq(&phba->hbalock);
  5367. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5368. spin_unlock_irq(&phba->hbalock);
  5369. }
  5370. /*
  5371. * Allocate a single mailbox container for initializing the
  5372. * port.
  5373. */
  5374. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5375. if (!mboxq)
  5376. return -ENOMEM;
  5377. /* Issue READ_REV to collect vpd and FW information. */
  5378. vpd_size = SLI4_PAGE_SIZE;
  5379. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5380. if (!vpd) {
  5381. rc = -ENOMEM;
  5382. goto out_free_mbox;
  5383. }
  5384. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5385. if (unlikely(rc)) {
  5386. kfree(vpd);
  5387. goto out_free_mbox;
  5388. }
  5389. mqe = &mboxq->u.mqe;
  5390. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5391. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5392. phba->hba_flag |= HBA_FCOE_MODE;
  5393. else
  5394. phba->hba_flag &= ~HBA_FCOE_MODE;
  5395. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5396. LPFC_DCBX_CEE_MODE)
  5397. phba->hba_flag |= HBA_FIP_SUPPORT;
  5398. else
  5399. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5400. if (phba->sli_rev != LPFC_SLI_REV4) {
  5401. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5402. "0376 READ_REV Error. SLI Level %d "
  5403. "FCoE enabled %d\n",
  5404. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5405. rc = -EIO;
  5406. kfree(vpd);
  5407. goto out_free_mbox;
  5408. }
  5409. /*
  5410. * Continue initialization with default values even if driver failed
  5411. * to read FCoE param config regions, only read parameters if the
  5412. * board is FCoE
  5413. */
  5414. if (phba->hba_flag & HBA_FCOE_MODE &&
  5415. lpfc_sli4_read_fcoe_params(phba))
  5416. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5417. "2570 Failed to read FCoE parameters\n");
  5418. /*
  5419. * Retrieve sli4 device physical port name, failure of doing it
  5420. * is considered as non-fatal.
  5421. */
  5422. rc = lpfc_sli4_retrieve_pport_name(phba);
  5423. if (!rc)
  5424. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5425. "3080 Successful retrieving SLI4 device "
  5426. "physical port name: %s.\n", phba->Port);
  5427. /*
  5428. * Evaluate the read rev and vpd data. Populate the driver
  5429. * state with the results. If this routine fails, the failure
  5430. * is not fatal as the driver will use generic values.
  5431. */
  5432. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5433. if (unlikely(!rc)) {
  5434. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5435. "0377 Error %d parsing vpd. "
  5436. "Using defaults.\n", rc);
  5437. rc = 0;
  5438. }
  5439. kfree(vpd);
  5440. /* Save information as VPD data */
  5441. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5442. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5443. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5444. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5445. &mqe->un.read_rev);
  5446. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5447. &mqe->un.read_rev);
  5448. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5449. &mqe->un.read_rev);
  5450. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5451. &mqe->un.read_rev);
  5452. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5453. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5454. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5455. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5456. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5457. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5458. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5459. "(%d):0380 READ_REV Status x%x "
  5460. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5461. mboxq->vport ? mboxq->vport->vpi : 0,
  5462. bf_get(lpfc_mqe_status, mqe),
  5463. phba->vpd.rev.opFwName,
  5464. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5465. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5466. /*
  5467. * Discover the port's supported feature set and match it against the
  5468. * hosts requests.
  5469. */
  5470. lpfc_request_features(phba, mboxq);
  5471. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5472. if (unlikely(rc)) {
  5473. rc = -EIO;
  5474. goto out_free_mbox;
  5475. }
  5476. /*
  5477. * The port must support FCP initiator mode as this is the
  5478. * only mode running in the host.
  5479. */
  5480. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5481. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5482. "0378 No support for fcpi mode.\n");
  5483. ftr_rsp++;
  5484. }
  5485. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5486. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5487. else
  5488. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5489. /*
  5490. * If the port cannot support the host's requested features
  5491. * then turn off the global config parameters to disable the
  5492. * feature in the driver. This is not a fatal error.
  5493. */
  5494. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5495. if (phba->cfg_enable_bg) {
  5496. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5497. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5498. else
  5499. ftr_rsp++;
  5500. }
  5501. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5502. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5503. ftr_rsp++;
  5504. if (ftr_rsp) {
  5505. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5506. "0379 Feature Mismatch Data: x%08x %08x "
  5507. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5508. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5509. phba->cfg_enable_npiv, phba->max_vpi);
  5510. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5511. phba->cfg_enable_bg = 0;
  5512. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5513. phba->cfg_enable_npiv = 0;
  5514. }
  5515. /* These SLI3 features are assumed in SLI4 */
  5516. spin_lock_irq(&phba->hbalock);
  5517. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5518. spin_unlock_irq(&phba->hbalock);
  5519. /*
  5520. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5521. * calls depends on these resources to complete port setup.
  5522. */
  5523. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5524. if (rc) {
  5525. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5526. "2920 Failed to alloc Resource IDs "
  5527. "rc = x%x\n", rc);
  5528. goto out_free_mbox;
  5529. }
  5530. /* update physical xri mappings in the scsi buffers */
  5531. lpfc_scsi_buf_update(phba);
  5532. /* Read the port's service parameters. */
  5533. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5534. if (rc) {
  5535. phba->link_state = LPFC_HBA_ERROR;
  5536. rc = -ENOMEM;
  5537. goto out_free_mbox;
  5538. }
  5539. mboxq->vport = vport;
  5540. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5541. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5542. if (rc == MBX_SUCCESS) {
  5543. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5544. rc = 0;
  5545. }
  5546. /*
  5547. * This memory was allocated by the lpfc_read_sparam routine. Release
  5548. * it to the mbuf pool.
  5549. */
  5550. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5551. kfree(mp);
  5552. mboxq->context1 = NULL;
  5553. if (unlikely(rc)) {
  5554. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5555. "0382 READ_SPARAM command failed "
  5556. "status %d, mbxStatus x%x\n",
  5557. rc, bf_get(lpfc_mqe_status, mqe));
  5558. phba->link_state = LPFC_HBA_ERROR;
  5559. rc = -EIO;
  5560. goto out_free_mbox;
  5561. }
  5562. lpfc_update_vport_wwn(vport);
  5563. /* Update the fc_host data structures with new wwn. */
  5564. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5565. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5566. /* Register SGL pool to the device using non-embedded mailbox command */
  5567. if (!phba->sli4_hba.extents_in_use) {
  5568. rc = lpfc_sli4_post_els_sgl_list(phba);
  5569. if (unlikely(rc)) {
  5570. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5571. "0582 Error %d during els sgl post "
  5572. "operation\n", rc);
  5573. rc = -ENODEV;
  5574. goto out_free_mbox;
  5575. }
  5576. } else {
  5577. rc = lpfc_sli4_post_els_sgl_list_ext(phba);
  5578. if (unlikely(rc)) {
  5579. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5580. "2560 Error %d during els sgl post "
  5581. "operation\n", rc);
  5582. rc = -ENODEV;
  5583. goto out_free_mbox;
  5584. }
  5585. }
  5586. /* Register SCSI SGL pool to the device */
  5587. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5588. if (unlikely(rc)) {
  5589. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5590. "0383 Error %d during scsi sgl post "
  5591. "operation\n", rc);
  5592. /* Some Scsi buffers were moved to the abort scsi list */
  5593. /* A pci function reset will repost them */
  5594. rc = -ENODEV;
  5595. goto out_free_mbox;
  5596. }
  5597. /* Post the rpi header region to the device. */
  5598. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5599. if (unlikely(rc)) {
  5600. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5601. "0393 Error %d during rpi post operation\n",
  5602. rc);
  5603. rc = -ENODEV;
  5604. goto out_free_mbox;
  5605. }
  5606. /* Create all the SLI4 queues */
  5607. rc = lpfc_sli4_queue_create(phba);
  5608. if (rc) {
  5609. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5610. "3089 Failed to allocate queues\n");
  5611. rc = -ENODEV;
  5612. goto out_stop_timers;
  5613. }
  5614. /* Set up all the queues to the device */
  5615. rc = lpfc_sli4_queue_setup(phba);
  5616. if (unlikely(rc)) {
  5617. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5618. "0381 Error %d during queue setup.\n ", rc);
  5619. goto out_destroy_queue;
  5620. }
  5621. /* Arm the CQs and then EQs on device */
  5622. lpfc_sli4_arm_cqeq_intr(phba);
  5623. /* Indicate device interrupt mode */
  5624. phba->sli4_hba.intr_enable = 1;
  5625. /* Allow asynchronous mailbox command to go through */
  5626. spin_lock_irq(&phba->hbalock);
  5627. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5628. spin_unlock_irq(&phba->hbalock);
  5629. /* Post receive buffers to the device */
  5630. lpfc_sli4_rb_setup(phba);
  5631. /* Reset HBA FCF states after HBA reset */
  5632. phba->fcf.fcf_flag = 0;
  5633. phba->fcf.current_rec.flag = 0;
  5634. /* Start the ELS watchdog timer */
  5635. mod_timer(&vport->els_tmofunc,
  5636. jiffies + HZ * (phba->fc_ratov * 2));
  5637. /* Start heart beat timer */
  5638. mod_timer(&phba->hb_tmofunc,
  5639. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5640. phba->hb_outstanding = 0;
  5641. phba->last_completion_time = jiffies;
  5642. /* Start error attention (ERATT) polling timer */
  5643. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5644. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5645. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5646. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5647. if (!rc) {
  5648. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5649. "2829 This device supports "
  5650. "Advanced Error Reporting (AER)\n");
  5651. spin_lock_irq(&phba->hbalock);
  5652. phba->hba_flag |= HBA_AER_ENABLED;
  5653. spin_unlock_irq(&phba->hbalock);
  5654. } else {
  5655. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5656. "2830 This device does not support "
  5657. "Advanced Error Reporting (AER)\n");
  5658. phba->cfg_aer_support = 0;
  5659. }
  5660. rc = 0;
  5661. }
  5662. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5663. /*
  5664. * The FC Port needs to register FCFI (index 0)
  5665. */
  5666. lpfc_reg_fcfi(phba, mboxq);
  5667. mboxq->vport = phba->pport;
  5668. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5669. if (rc != MBX_SUCCESS)
  5670. goto out_unset_queue;
  5671. rc = 0;
  5672. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5673. &mboxq->u.mqe.un.reg_fcfi);
  5674. /* Check if the port is configured to be disabled */
  5675. lpfc_sli_read_link_ste(phba);
  5676. }
  5677. /*
  5678. * The port is ready, set the host's link state to LINK_DOWN
  5679. * in preparation for link interrupts.
  5680. */
  5681. spin_lock_irq(&phba->hbalock);
  5682. phba->link_state = LPFC_LINK_DOWN;
  5683. spin_unlock_irq(&phba->hbalock);
  5684. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5685. (phba->hba_flag & LINK_DISABLED)) {
  5686. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5687. "3103 Adapter Link is disabled.\n");
  5688. lpfc_down_link(phba, mboxq);
  5689. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5690. if (rc != MBX_SUCCESS) {
  5691. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5692. "3104 Adapter failed to issue "
  5693. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5694. goto out_unset_queue;
  5695. }
  5696. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5697. /* don't perform init_link on SLI4 FC port loopback test */
  5698. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5699. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5700. if (rc)
  5701. goto out_unset_queue;
  5702. }
  5703. }
  5704. mempool_free(mboxq, phba->mbox_mem_pool);
  5705. return rc;
  5706. out_unset_queue:
  5707. /* Unset all the queues set up in this routine when error out */
  5708. lpfc_sli4_queue_unset(phba);
  5709. out_destroy_queue:
  5710. lpfc_sli4_queue_destroy(phba);
  5711. out_stop_timers:
  5712. lpfc_stop_hba_timers(phba);
  5713. out_free_mbox:
  5714. mempool_free(mboxq, phba->mbox_mem_pool);
  5715. return rc;
  5716. }
  5717. /**
  5718. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5719. * @ptr: context object - pointer to hba structure.
  5720. *
  5721. * This is the callback function for mailbox timer. The mailbox
  5722. * timer is armed when a new mailbox command is issued and the timer
  5723. * is deleted when the mailbox complete. The function is called by
  5724. * the kernel timer code when a mailbox does not complete within
  5725. * expected time. This function wakes up the worker thread to
  5726. * process the mailbox timeout and returns. All the processing is
  5727. * done by the worker thread function lpfc_mbox_timeout_handler.
  5728. **/
  5729. void
  5730. lpfc_mbox_timeout(unsigned long ptr)
  5731. {
  5732. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5733. unsigned long iflag;
  5734. uint32_t tmo_posted;
  5735. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5736. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5737. if (!tmo_posted)
  5738. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5739. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5740. if (!tmo_posted)
  5741. lpfc_worker_wake_up(phba);
  5742. return;
  5743. }
  5744. /**
  5745. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5746. * @phba: Pointer to HBA context object.
  5747. *
  5748. * This function is called from worker thread when a mailbox command times out.
  5749. * The caller is not required to hold any locks. This function will reset the
  5750. * HBA and recover all the pending commands.
  5751. **/
  5752. void
  5753. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5754. {
  5755. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5756. MAILBOX_t *mb = &pmbox->u.mb;
  5757. struct lpfc_sli *psli = &phba->sli;
  5758. struct lpfc_sli_ring *pring;
  5759. /* Check the pmbox pointer first. There is a race condition
  5760. * between the mbox timeout handler getting executed in the
  5761. * worklist and the mailbox actually completing. When this
  5762. * race condition occurs, the mbox_active will be NULL.
  5763. */
  5764. spin_lock_irq(&phba->hbalock);
  5765. if (pmbox == NULL) {
  5766. lpfc_printf_log(phba, KERN_WARNING,
  5767. LOG_MBOX | LOG_SLI,
  5768. "0353 Active Mailbox cleared - mailbox timeout "
  5769. "exiting\n");
  5770. spin_unlock_irq(&phba->hbalock);
  5771. return;
  5772. }
  5773. /* Mbox cmd <mbxCommand> timeout */
  5774. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5775. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5776. mb->mbxCommand,
  5777. phba->pport->port_state,
  5778. phba->sli.sli_flag,
  5779. phba->sli.mbox_active);
  5780. spin_unlock_irq(&phba->hbalock);
  5781. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5782. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5783. * it to fail all outstanding SCSI IO.
  5784. */
  5785. spin_lock_irq(&phba->pport->work_port_lock);
  5786. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5787. spin_unlock_irq(&phba->pport->work_port_lock);
  5788. spin_lock_irq(&phba->hbalock);
  5789. phba->link_state = LPFC_LINK_UNKNOWN;
  5790. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5791. spin_unlock_irq(&phba->hbalock);
  5792. pring = &psli->ring[psli->fcp_ring];
  5793. lpfc_sli_abort_iocb_ring(phba, pring);
  5794. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5795. "0345 Resetting board due to mailbox timeout\n");
  5796. /* Reset the HBA device */
  5797. lpfc_reset_hba(phba);
  5798. }
  5799. /**
  5800. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5801. * @phba: Pointer to HBA context object.
  5802. * @pmbox: Pointer to mailbox object.
  5803. * @flag: Flag indicating how the mailbox need to be processed.
  5804. *
  5805. * This function is called by discovery code and HBA management code
  5806. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5807. * function gets the hbalock to protect the data structures.
  5808. * The mailbox command can be submitted in polling mode, in which case
  5809. * this function will wait in a polling loop for the completion of the
  5810. * mailbox.
  5811. * If the mailbox is submitted in no_wait mode (not polling) the
  5812. * function will submit the command and returns immediately without waiting
  5813. * for the mailbox completion. The no_wait is supported only when HBA
  5814. * is in SLI2/SLI3 mode - interrupts are enabled.
  5815. * The SLI interface allows only one mailbox pending at a time. If the
  5816. * mailbox is issued in polling mode and there is already a mailbox
  5817. * pending, then the function will return an error. If the mailbox is issued
  5818. * in NO_WAIT mode and there is a mailbox pending already, the function
  5819. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5820. * The sli layer owns the mailbox object until the completion of mailbox
  5821. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5822. * return codes the caller owns the mailbox command after the return of
  5823. * the function.
  5824. **/
  5825. static int
  5826. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5827. uint32_t flag)
  5828. {
  5829. MAILBOX_t *mb;
  5830. struct lpfc_sli *psli = &phba->sli;
  5831. uint32_t status, evtctr;
  5832. uint32_t ha_copy, hc_copy;
  5833. int i;
  5834. unsigned long timeout;
  5835. unsigned long drvr_flag = 0;
  5836. uint32_t word0, ldata;
  5837. void __iomem *to_slim;
  5838. int processing_queue = 0;
  5839. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5840. if (!pmbox) {
  5841. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5842. /* processing mbox queue from intr_handler */
  5843. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5844. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5845. return MBX_SUCCESS;
  5846. }
  5847. processing_queue = 1;
  5848. pmbox = lpfc_mbox_get(phba);
  5849. if (!pmbox) {
  5850. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5851. return MBX_SUCCESS;
  5852. }
  5853. }
  5854. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5855. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5856. if(!pmbox->vport) {
  5857. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5858. lpfc_printf_log(phba, KERN_ERR,
  5859. LOG_MBOX | LOG_VPORT,
  5860. "1806 Mbox x%x failed. No vport\n",
  5861. pmbox->u.mb.mbxCommand);
  5862. dump_stack();
  5863. goto out_not_finished;
  5864. }
  5865. }
  5866. /* If the PCI channel is in offline state, do not post mbox. */
  5867. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5868. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5869. goto out_not_finished;
  5870. }
  5871. /* If HBA has a deferred error attention, fail the iocb. */
  5872. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5873. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5874. goto out_not_finished;
  5875. }
  5876. psli = &phba->sli;
  5877. mb = &pmbox->u.mb;
  5878. status = MBX_SUCCESS;
  5879. if (phba->link_state == LPFC_HBA_ERROR) {
  5880. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5881. /* Mbox command <mbxCommand> cannot issue */
  5882. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5883. "(%d):0311 Mailbox command x%x cannot "
  5884. "issue Data: x%x x%x\n",
  5885. pmbox->vport ? pmbox->vport->vpi : 0,
  5886. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5887. goto out_not_finished;
  5888. }
  5889. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  5890. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  5891. !(hc_copy & HC_MBINT_ENA)) {
  5892. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5893. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5894. "(%d):2528 Mailbox command x%x cannot "
  5895. "issue Data: x%x x%x\n",
  5896. pmbox->vport ? pmbox->vport->vpi : 0,
  5897. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5898. goto out_not_finished;
  5899. }
  5900. }
  5901. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  5902. /* Polling for a mbox command when another one is already active
  5903. * is not allowed in SLI. Also, the driver must have established
  5904. * SLI2 mode to queue and process multiple mbox commands.
  5905. */
  5906. if (flag & MBX_POLL) {
  5907. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5908. /* Mbox command <mbxCommand> cannot issue */
  5909. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5910. "(%d):2529 Mailbox command x%x "
  5911. "cannot issue Data: x%x x%x\n",
  5912. pmbox->vport ? pmbox->vport->vpi : 0,
  5913. pmbox->u.mb.mbxCommand,
  5914. psli->sli_flag, flag);
  5915. goto out_not_finished;
  5916. }
  5917. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  5918. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5919. /* Mbox command <mbxCommand> cannot issue */
  5920. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5921. "(%d):2530 Mailbox command x%x "
  5922. "cannot issue Data: x%x x%x\n",
  5923. pmbox->vport ? pmbox->vport->vpi : 0,
  5924. pmbox->u.mb.mbxCommand,
  5925. psli->sli_flag, flag);
  5926. goto out_not_finished;
  5927. }
  5928. /* Another mailbox command is still being processed, queue this
  5929. * command to be processed later.
  5930. */
  5931. lpfc_mbox_put(phba, pmbox);
  5932. /* Mbox cmd issue - BUSY */
  5933. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5934. "(%d):0308 Mbox cmd issue - BUSY Data: "
  5935. "x%x x%x x%x x%x\n",
  5936. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  5937. mb->mbxCommand, phba->pport->port_state,
  5938. psli->sli_flag, flag);
  5939. psli->slistat.mbox_busy++;
  5940. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5941. if (pmbox->vport) {
  5942. lpfc_debugfs_disc_trc(pmbox->vport,
  5943. LPFC_DISC_TRC_MBOX_VPORT,
  5944. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  5945. (uint32_t)mb->mbxCommand,
  5946. mb->un.varWords[0], mb->un.varWords[1]);
  5947. }
  5948. else {
  5949. lpfc_debugfs_disc_trc(phba->pport,
  5950. LPFC_DISC_TRC_MBOX,
  5951. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  5952. (uint32_t)mb->mbxCommand,
  5953. mb->un.varWords[0], mb->un.varWords[1]);
  5954. }
  5955. return MBX_BUSY;
  5956. }
  5957. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  5958. /* If we are not polling, we MUST be in SLI2 mode */
  5959. if (flag != MBX_POLL) {
  5960. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  5961. (mb->mbxCommand != MBX_KILL_BOARD)) {
  5962. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5963. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5964. /* Mbox command <mbxCommand> cannot issue */
  5965. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5966. "(%d):2531 Mailbox command x%x "
  5967. "cannot issue Data: x%x x%x\n",
  5968. pmbox->vport ? pmbox->vport->vpi : 0,
  5969. pmbox->u.mb.mbxCommand,
  5970. psli->sli_flag, flag);
  5971. goto out_not_finished;
  5972. }
  5973. /* timeout active mbox command */
  5974. mod_timer(&psli->mbox_tmo, (jiffies +
  5975. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  5976. }
  5977. /* Mailbox cmd <cmd> issue */
  5978. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5979. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  5980. "x%x\n",
  5981. pmbox->vport ? pmbox->vport->vpi : 0,
  5982. mb->mbxCommand, phba->pport->port_state,
  5983. psli->sli_flag, flag);
  5984. if (mb->mbxCommand != MBX_HEARTBEAT) {
  5985. if (pmbox->vport) {
  5986. lpfc_debugfs_disc_trc(pmbox->vport,
  5987. LPFC_DISC_TRC_MBOX_VPORT,
  5988. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  5989. (uint32_t)mb->mbxCommand,
  5990. mb->un.varWords[0], mb->un.varWords[1]);
  5991. }
  5992. else {
  5993. lpfc_debugfs_disc_trc(phba->pport,
  5994. LPFC_DISC_TRC_MBOX,
  5995. "MBOX Send: cmd:x%x mb:x%x x%x",
  5996. (uint32_t)mb->mbxCommand,
  5997. mb->un.varWords[0], mb->un.varWords[1]);
  5998. }
  5999. }
  6000. psli->slistat.mbox_cmd++;
  6001. evtctr = psli->slistat.mbox_event;
  6002. /* next set own bit for the adapter and copy over command word */
  6003. mb->mbxOwner = OWN_CHIP;
  6004. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6005. /* Populate mbox extension offset word. */
  6006. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6007. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6008. = (uint8_t *)phba->mbox_ext
  6009. - (uint8_t *)phba->mbox;
  6010. }
  6011. /* Copy the mailbox extension data */
  6012. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6013. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6014. (uint8_t *)phba->mbox_ext,
  6015. pmbox->in_ext_byte_len);
  6016. }
  6017. /* Copy command data to host SLIM area */
  6018. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6019. } else {
  6020. /* Populate mbox extension offset word. */
  6021. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6022. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6023. = MAILBOX_HBA_EXT_OFFSET;
  6024. /* Copy the mailbox extension data */
  6025. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6026. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6027. MAILBOX_HBA_EXT_OFFSET,
  6028. pmbox->context2, pmbox->in_ext_byte_len);
  6029. }
  6030. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6031. /* copy command data into host mbox for cmpl */
  6032. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6033. }
  6034. /* First copy mbox command data to HBA SLIM, skip past first
  6035. word */
  6036. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6037. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6038. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6039. /* Next copy over first word, with mbxOwner set */
  6040. ldata = *((uint32_t *)mb);
  6041. to_slim = phba->MBslimaddr;
  6042. writel(ldata, to_slim);
  6043. readl(to_slim); /* flush */
  6044. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6045. /* switch over to host mailbox */
  6046. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6047. }
  6048. }
  6049. wmb();
  6050. switch (flag) {
  6051. case MBX_NOWAIT:
  6052. /* Set up reference to mailbox command */
  6053. psli->mbox_active = pmbox;
  6054. /* Interrupt board to do it */
  6055. writel(CA_MBATT, phba->CAregaddr);
  6056. readl(phba->CAregaddr); /* flush */
  6057. /* Don't wait for it to finish, just return */
  6058. break;
  6059. case MBX_POLL:
  6060. /* Set up null reference to mailbox command */
  6061. psli->mbox_active = NULL;
  6062. /* Interrupt board to do it */
  6063. writel(CA_MBATT, phba->CAregaddr);
  6064. readl(phba->CAregaddr); /* flush */
  6065. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6066. /* First read mbox status word */
  6067. word0 = *((uint32_t *)phba->mbox);
  6068. word0 = le32_to_cpu(word0);
  6069. } else {
  6070. /* First read mbox status word */
  6071. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6072. spin_unlock_irqrestore(&phba->hbalock,
  6073. drvr_flag);
  6074. goto out_not_finished;
  6075. }
  6076. }
  6077. /* Read the HBA Host Attention Register */
  6078. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6079. spin_unlock_irqrestore(&phba->hbalock,
  6080. drvr_flag);
  6081. goto out_not_finished;
  6082. }
  6083. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6084. 1000) + jiffies;
  6085. i = 0;
  6086. /* Wait for command to complete */
  6087. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6088. (!(ha_copy & HA_MBATT) &&
  6089. (phba->link_state > LPFC_WARM_START))) {
  6090. if (time_after(jiffies, timeout)) {
  6091. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6092. spin_unlock_irqrestore(&phba->hbalock,
  6093. drvr_flag);
  6094. goto out_not_finished;
  6095. }
  6096. /* Check if we took a mbox interrupt while we were
  6097. polling */
  6098. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6099. && (evtctr != psli->slistat.mbox_event))
  6100. break;
  6101. if (i++ > 10) {
  6102. spin_unlock_irqrestore(&phba->hbalock,
  6103. drvr_flag);
  6104. msleep(1);
  6105. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6106. }
  6107. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6108. /* First copy command data */
  6109. word0 = *((uint32_t *)phba->mbox);
  6110. word0 = le32_to_cpu(word0);
  6111. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6112. MAILBOX_t *slimmb;
  6113. uint32_t slimword0;
  6114. /* Check real SLIM for any errors */
  6115. slimword0 = readl(phba->MBslimaddr);
  6116. slimmb = (MAILBOX_t *) & slimword0;
  6117. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6118. && slimmb->mbxStatus) {
  6119. psli->sli_flag &=
  6120. ~LPFC_SLI_ACTIVE;
  6121. word0 = slimword0;
  6122. }
  6123. }
  6124. } else {
  6125. /* First copy command data */
  6126. word0 = readl(phba->MBslimaddr);
  6127. }
  6128. /* Read the HBA Host Attention Register */
  6129. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6130. spin_unlock_irqrestore(&phba->hbalock,
  6131. drvr_flag);
  6132. goto out_not_finished;
  6133. }
  6134. }
  6135. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6136. /* copy results back to user */
  6137. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6138. /* Copy the mailbox extension data */
  6139. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6140. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6141. pmbox->context2,
  6142. pmbox->out_ext_byte_len);
  6143. }
  6144. } else {
  6145. /* First copy command data */
  6146. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6147. MAILBOX_CMD_SIZE);
  6148. /* Copy the mailbox extension data */
  6149. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6150. lpfc_memcpy_from_slim(pmbox->context2,
  6151. phba->MBslimaddr +
  6152. MAILBOX_HBA_EXT_OFFSET,
  6153. pmbox->out_ext_byte_len);
  6154. }
  6155. }
  6156. writel(HA_MBATT, phba->HAregaddr);
  6157. readl(phba->HAregaddr); /* flush */
  6158. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6159. status = mb->mbxStatus;
  6160. }
  6161. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6162. return status;
  6163. out_not_finished:
  6164. if (processing_queue) {
  6165. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6166. lpfc_mbox_cmpl_put(phba, pmbox);
  6167. }
  6168. return MBX_NOT_FINISHED;
  6169. }
  6170. /**
  6171. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6172. * @phba: Pointer to HBA context object.
  6173. *
  6174. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6175. * the driver internal pending mailbox queue. It will then try to wait out the
  6176. * possible outstanding mailbox command before return.
  6177. *
  6178. * Returns:
  6179. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6180. * the outstanding mailbox command timed out.
  6181. **/
  6182. static int
  6183. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6184. {
  6185. struct lpfc_sli *psli = &phba->sli;
  6186. int rc = 0;
  6187. unsigned long timeout = 0;
  6188. /* Mark the asynchronous mailbox command posting as blocked */
  6189. spin_lock_irq(&phba->hbalock);
  6190. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6191. /* Determine how long we might wait for the active mailbox
  6192. * command to be gracefully completed by firmware.
  6193. */
  6194. if (phba->sli.mbox_active)
  6195. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6196. phba->sli.mbox_active) *
  6197. 1000) + jiffies;
  6198. spin_unlock_irq(&phba->hbalock);
  6199. /* Wait for the outstnading mailbox command to complete */
  6200. while (phba->sli.mbox_active) {
  6201. /* Check active mailbox complete status every 2ms */
  6202. msleep(2);
  6203. if (time_after(jiffies, timeout)) {
  6204. /* Timeout, marked the outstanding cmd not complete */
  6205. rc = 1;
  6206. break;
  6207. }
  6208. }
  6209. /* Can not cleanly block async mailbox command, fails it */
  6210. if (rc) {
  6211. spin_lock_irq(&phba->hbalock);
  6212. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6213. spin_unlock_irq(&phba->hbalock);
  6214. }
  6215. return rc;
  6216. }
  6217. /**
  6218. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6219. * @phba: Pointer to HBA context object.
  6220. *
  6221. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6222. * commands from the driver internal pending mailbox queue. It makes sure
  6223. * that there is no outstanding mailbox command before resuming posting
  6224. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6225. * mailbox command, it will try to wait it out before resuming asynchronous
  6226. * mailbox command posting.
  6227. **/
  6228. static void
  6229. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6230. {
  6231. struct lpfc_sli *psli = &phba->sli;
  6232. spin_lock_irq(&phba->hbalock);
  6233. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6234. /* Asynchronous mailbox posting is not blocked, do nothing */
  6235. spin_unlock_irq(&phba->hbalock);
  6236. return;
  6237. }
  6238. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6239. * successful or timeout, after timing-out the outstanding mailbox
  6240. * command shall always be removed, so just unblock posting async
  6241. * mailbox command and resume
  6242. */
  6243. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6244. spin_unlock_irq(&phba->hbalock);
  6245. /* wake up worker thread to post asynchronlous mailbox command */
  6246. lpfc_worker_wake_up(phba);
  6247. }
  6248. /**
  6249. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6250. * @phba: Pointer to HBA context object.
  6251. * @mboxq: Pointer to mailbox object.
  6252. *
  6253. * The function posts a mailbox to the port. The mailbox is expected
  6254. * to be comletely filled in and ready for the port to operate on it.
  6255. * This routine executes a synchronous completion operation on the
  6256. * mailbox by polling for its completion.
  6257. *
  6258. * The caller must not be holding any locks when calling this routine.
  6259. *
  6260. * Returns:
  6261. * MBX_SUCCESS - mailbox posted successfully
  6262. * Any of the MBX error values.
  6263. **/
  6264. static int
  6265. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6266. {
  6267. int rc = MBX_SUCCESS;
  6268. unsigned long iflag;
  6269. uint32_t db_ready;
  6270. uint32_t mcqe_status;
  6271. uint32_t mbx_cmnd;
  6272. unsigned long timeout;
  6273. struct lpfc_sli *psli = &phba->sli;
  6274. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6275. struct lpfc_bmbx_create *mbox_rgn;
  6276. struct dma_address *dma_address;
  6277. struct lpfc_register bmbx_reg;
  6278. /*
  6279. * Only one mailbox can be active to the bootstrap mailbox region
  6280. * at a time and there is no queueing provided.
  6281. */
  6282. spin_lock_irqsave(&phba->hbalock, iflag);
  6283. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6284. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6285. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6286. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6287. "cannot issue Data: x%x x%x\n",
  6288. mboxq->vport ? mboxq->vport->vpi : 0,
  6289. mboxq->u.mb.mbxCommand,
  6290. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6291. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6292. psli->sli_flag, MBX_POLL);
  6293. return MBXERR_ERROR;
  6294. }
  6295. /* The server grabs the token and owns it until release */
  6296. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6297. phba->sli.mbox_active = mboxq;
  6298. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6299. /*
  6300. * Initialize the bootstrap memory region to avoid stale data areas
  6301. * in the mailbox post. Then copy the caller's mailbox contents to
  6302. * the bmbx mailbox region.
  6303. */
  6304. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6305. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6306. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6307. sizeof(struct lpfc_mqe));
  6308. /* Post the high mailbox dma address to the port and wait for ready. */
  6309. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6310. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6311. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6312. * 1000) + jiffies;
  6313. do {
  6314. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6315. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6316. if (!db_ready)
  6317. msleep(2);
  6318. if (time_after(jiffies, timeout)) {
  6319. rc = MBXERR_ERROR;
  6320. goto exit;
  6321. }
  6322. } while (!db_ready);
  6323. /* Post the low mailbox dma address to the port. */
  6324. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6325. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6326. * 1000) + jiffies;
  6327. do {
  6328. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6329. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6330. if (!db_ready)
  6331. msleep(2);
  6332. if (time_after(jiffies, timeout)) {
  6333. rc = MBXERR_ERROR;
  6334. goto exit;
  6335. }
  6336. } while (!db_ready);
  6337. /*
  6338. * Read the CQ to ensure the mailbox has completed.
  6339. * If so, update the mailbox status so that the upper layers
  6340. * can complete the request normally.
  6341. */
  6342. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6343. sizeof(struct lpfc_mqe));
  6344. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6345. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6346. sizeof(struct lpfc_mcqe));
  6347. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6348. /*
  6349. * When the CQE status indicates a failure and the mailbox status
  6350. * indicates success then copy the CQE status into the mailbox status
  6351. * (and prefix it with x4000).
  6352. */
  6353. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6354. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6355. bf_set(lpfc_mqe_status, mb,
  6356. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6357. rc = MBXERR_ERROR;
  6358. } else
  6359. lpfc_sli4_swap_str(phba, mboxq);
  6360. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6361. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6362. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6363. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6364. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6365. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6366. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6367. bf_get(lpfc_mqe_status, mb),
  6368. mb->un.mb_words[0], mb->un.mb_words[1],
  6369. mb->un.mb_words[2], mb->un.mb_words[3],
  6370. mb->un.mb_words[4], mb->un.mb_words[5],
  6371. mb->un.mb_words[6], mb->un.mb_words[7],
  6372. mb->un.mb_words[8], mb->un.mb_words[9],
  6373. mb->un.mb_words[10], mb->un.mb_words[11],
  6374. mb->un.mb_words[12], mboxq->mcqe.word0,
  6375. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6376. mboxq->mcqe.trailer);
  6377. exit:
  6378. /* We are holding the token, no needed for lock when release */
  6379. spin_lock_irqsave(&phba->hbalock, iflag);
  6380. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6381. phba->sli.mbox_active = NULL;
  6382. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6383. return rc;
  6384. }
  6385. /**
  6386. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6387. * @phba: Pointer to HBA context object.
  6388. * @pmbox: Pointer to mailbox object.
  6389. * @flag: Flag indicating how the mailbox need to be processed.
  6390. *
  6391. * This function is called by discovery code and HBA management code to submit
  6392. * a mailbox command to firmware with SLI-4 interface spec.
  6393. *
  6394. * Return codes the caller owns the mailbox command after the return of the
  6395. * function.
  6396. **/
  6397. static int
  6398. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6399. uint32_t flag)
  6400. {
  6401. struct lpfc_sli *psli = &phba->sli;
  6402. unsigned long iflags;
  6403. int rc;
  6404. /* dump from issue mailbox command if setup */
  6405. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6406. rc = lpfc_mbox_dev_check(phba);
  6407. if (unlikely(rc)) {
  6408. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6409. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6410. "cannot issue Data: x%x x%x\n",
  6411. mboxq->vport ? mboxq->vport->vpi : 0,
  6412. mboxq->u.mb.mbxCommand,
  6413. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6414. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6415. psli->sli_flag, flag);
  6416. goto out_not_finished;
  6417. }
  6418. /* Detect polling mode and jump to a handler */
  6419. if (!phba->sli4_hba.intr_enable) {
  6420. if (flag == MBX_POLL)
  6421. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6422. else
  6423. rc = -EIO;
  6424. if (rc != MBX_SUCCESS)
  6425. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6426. "(%d):2541 Mailbox command x%x "
  6427. "(x%x/x%x) cannot issue Data: "
  6428. "x%x x%x\n",
  6429. mboxq->vport ? mboxq->vport->vpi : 0,
  6430. mboxq->u.mb.mbxCommand,
  6431. lpfc_sli_config_mbox_subsys_get(phba,
  6432. mboxq),
  6433. lpfc_sli_config_mbox_opcode_get(phba,
  6434. mboxq),
  6435. psli->sli_flag, flag);
  6436. return rc;
  6437. } else if (flag == MBX_POLL) {
  6438. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6439. "(%d):2542 Try to issue mailbox command "
  6440. "x%x (x%x/x%x) synchronously ahead of async"
  6441. "mailbox command queue: x%x x%x\n",
  6442. mboxq->vport ? mboxq->vport->vpi : 0,
  6443. mboxq->u.mb.mbxCommand,
  6444. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6445. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6446. psli->sli_flag, flag);
  6447. /* Try to block the asynchronous mailbox posting */
  6448. rc = lpfc_sli4_async_mbox_block(phba);
  6449. if (!rc) {
  6450. /* Successfully blocked, now issue sync mbox cmd */
  6451. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6452. if (rc != MBX_SUCCESS)
  6453. lpfc_printf_log(phba, KERN_ERR,
  6454. LOG_MBOX | LOG_SLI,
  6455. "(%d):2597 Mailbox command "
  6456. "x%x (x%x/x%x) cannot issue "
  6457. "Data: x%x x%x\n",
  6458. mboxq->vport ?
  6459. mboxq->vport->vpi : 0,
  6460. mboxq->u.mb.mbxCommand,
  6461. lpfc_sli_config_mbox_subsys_get(phba,
  6462. mboxq),
  6463. lpfc_sli_config_mbox_opcode_get(phba,
  6464. mboxq),
  6465. psli->sli_flag, flag);
  6466. /* Unblock the async mailbox posting afterward */
  6467. lpfc_sli4_async_mbox_unblock(phba);
  6468. }
  6469. return rc;
  6470. }
  6471. /* Now, interrupt mode asynchrous mailbox command */
  6472. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6473. if (rc) {
  6474. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6475. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6476. "cannot issue Data: x%x x%x\n",
  6477. mboxq->vport ? mboxq->vport->vpi : 0,
  6478. mboxq->u.mb.mbxCommand,
  6479. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6480. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6481. psli->sli_flag, flag);
  6482. goto out_not_finished;
  6483. }
  6484. /* Put the mailbox command to the driver internal FIFO */
  6485. psli->slistat.mbox_busy++;
  6486. spin_lock_irqsave(&phba->hbalock, iflags);
  6487. lpfc_mbox_put(phba, mboxq);
  6488. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6489. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6490. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6491. "x%x (x%x/x%x) x%x x%x x%x\n",
  6492. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6493. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6494. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6495. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6496. phba->pport->port_state,
  6497. psli->sli_flag, MBX_NOWAIT);
  6498. /* Wake up worker thread to transport mailbox command from head */
  6499. lpfc_worker_wake_up(phba);
  6500. return MBX_BUSY;
  6501. out_not_finished:
  6502. return MBX_NOT_FINISHED;
  6503. }
  6504. /**
  6505. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6506. * @phba: Pointer to HBA context object.
  6507. *
  6508. * This function is called by worker thread to send a mailbox command to
  6509. * SLI4 HBA firmware.
  6510. *
  6511. **/
  6512. int
  6513. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6514. {
  6515. struct lpfc_sli *psli = &phba->sli;
  6516. LPFC_MBOXQ_t *mboxq;
  6517. int rc = MBX_SUCCESS;
  6518. unsigned long iflags;
  6519. struct lpfc_mqe *mqe;
  6520. uint32_t mbx_cmnd;
  6521. /* Check interrupt mode before post async mailbox command */
  6522. if (unlikely(!phba->sli4_hba.intr_enable))
  6523. return MBX_NOT_FINISHED;
  6524. /* Check for mailbox command service token */
  6525. spin_lock_irqsave(&phba->hbalock, iflags);
  6526. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6527. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6528. return MBX_NOT_FINISHED;
  6529. }
  6530. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6531. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6532. return MBX_NOT_FINISHED;
  6533. }
  6534. if (unlikely(phba->sli.mbox_active)) {
  6535. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6536. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6537. "0384 There is pending active mailbox cmd\n");
  6538. return MBX_NOT_FINISHED;
  6539. }
  6540. /* Take the mailbox command service token */
  6541. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6542. /* Get the next mailbox command from head of queue */
  6543. mboxq = lpfc_mbox_get(phba);
  6544. /* If no more mailbox command waiting for post, we're done */
  6545. if (!mboxq) {
  6546. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6547. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6548. return MBX_SUCCESS;
  6549. }
  6550. phba->sli.mbox_active = mboxq;
  6551. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6552. /* Check device readiness for posting mailbox command */
  6553. rc = lpfc_mbox_dev_check(phba);
  6554. if (unlikely(rc))
  6555. /* Driver clean routine will clean up pending mailbox */
  6556. goto out_not_finished;
  6557. /* Prepare the mbox command to be posted */
  6558. mqe = &mboxq->u.mqe;
  6559. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6560. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6561. mod_timer(&psli->mbox_tmo, (jiffies +
  6562. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6563. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6564. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6565. "x%x x%x\n",
  6566. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6567. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6568. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6569. phba->pport->port_state, psli->sli_flag);
  6570. if (mbx_cmnd != MBX_HEARTBEAT) {
  6571. if (mboxq->vport) {
  6572. lpfc_debugfs_disc_trc(mboxq->vport,
  6573. LPFC_DISC_TRC_MBOX_VPORT,
  6574. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6575. mbx_cmnd, mqe->un.mb_words[0],
  6576. mqe->un.mb_words[1]);
  6577. } else {
  6578. lpfc_debugfs_disc_trc(phba->pport,
  6579. LPFC_DISC_TRC_MBOX,
  6580. "MBOX Send: cmd:x%x mb:x%x x%x",
  6581. mbx_cmnd, mqe->un.mb_words[0],
  6582. mqe->un.mb_words[1]);
  6583. }
  6584. }
  6585. psli->slistat.mbox_cmd++;
  6586. /* Post the mailbox command to the port */
  6587. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6588. if (rc != MBX_SUCCESS) {
  6589. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6590. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6591. "cannot issue Data: x%x x%x\n",
  6592. mboxq->vport ? mboxq->vport->vpi : 0,
  6593. mboxq->u.mb.mbxCommand,
  6594. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6595. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6596. psli->sli_flag, MBX_NOWAIT);
  6597. goto out_not_finished;
  6598. }
  6599. return rc;
  6600. out_not_finished:
  6601. spin_lock_irqsave(&phba->hbalock, iflags);
  6602. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6603. __lpfc_mbox_cmpl_put(phba, mboxq);
  6604. /* Release the token */
  6605. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6606. phba->sli.mbox_active = NULL;
  6607. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6608. return MBX_NOT_FINISHED;
  6609. }
  6610. /**
  6611. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6612. * @phba: Pointer to HBA context object.
  6613. * @pmbox: Pointer to mailbox object.
  6614. * @flag: Flag indicating how the mailbox need to be processed.
  6615. *
  6616. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6617. * the API jump table function pointer from the lpfc_hba struct.
  6618. *
  6619. * Return codes the caller owns the mailbox command after the return of the
  6620. * function.
  6621. **/
  6622. int
  6623. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6624. {
  6625. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6626. }
  6627. /**
  6628. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6629. * @phba: The hba struct for which this call is being executed.
  6630. * @dev_grp: The HBA PCI-Device group number.
  6631. *
  6632. * This routine sets up the mbox interface API function jump table in @phba
  6633. * struct.
  6634. * Returns: 0 - success, -ENODEV - failure.
  6635. **/
  6636. int
  6637. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6638. {
  6639. switch (dev_grp) {
  6640. case LPFC_PCI_DEV_LP:
  6641. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6642. phba->lpfc_sli_handle_slow_ring_event =
  6643. lpfc_sli_handle_slow_ring_event_s3;
  6644. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6645. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6646. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6647. break;
  6648. case LPFC_PCI_DEV_OC:
  6649. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6650. phba->lpfc_sli_handle_slow_ring_event =
  6651. lpfc_sli_handle_slow_ring_event_s4;
  6652. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6653. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6654. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6655. break;
  6656. default:
  6657. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6658. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6659. dev_grp);
  6660. return -ENODEV;
  6661. break;
  6662. }
  6663. return 0;
  6664. }
  6665. /**
  6666. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6667. * @phba: Pointer to HBA context object.
  6668. * @pring: Pointer to driver SLI ring object.
  6669. * @piocb: Pointer to address of newly added command iocb.
  6670. *
  6671. * This function is called with hbalock held to add a command
  6672. * iocb to the txq when SLI layer cannot submit the command iocb
  6673. * to the ring.
  6674. **/
  6675. void
  6676. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6677. struct lpfc_iocbq *piocb)
  6678. {
  6679. /* Insert the caller's iocb in the txq tail for later processing. */
  6680. list_add_tail(&piocb->list, &pring->txq);
  6681. pring->txq_cnt++;
  6682. }
  6683. /**
  6684. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6685. * @phba: Pointer to HBA context object.
  6686. * @pring: Pointer to driver SLI ring object.
  6687. * @piocb: Pointer to address of newly added command iocb.
  6688. *
  6689. * This function is called with hbalock held before a new
  6690. * iocb is submitted to the firmware. This function checks
  6691. * txq to flush the iocbs in txq to Firmware before
  6692. * submitting new iocbs to the Firmware.
  6693. * If there are iocbs in the txq which need to be submitted
  6694. * to firmware, lpfc_sli_next_iocb returns the first element
  6695. * of the txq after dequeuing it from txq.
  6696. * If there is no iocb in the txq then the function will return
  6697. * *piocb and *piocb is set to NULL. Caller needs to check
  6698. * *piocb to find if there are more commands in the txq.
  6699. **/
  6700. static struct lpfc_iocbq *
  6701. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6702. struct lpfc_iocbq **piocb)
  6703. {
  6704. struct lpfc_iocbq * nextiocb;
  6705. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6706. if (!nextiocb) {
  6707. nextiocb = *piocb;
  6708. *piocb = NULL;
  6709. }
  6710. return nextiocb;
  6711. }
  6712. /**
  6713. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6714. * @phba: Pointer to HBA context object.
  6715. * @ring_number: SLI ring number to issue iocb on.
  6716. * @piocb: Pointer to command iocb.
  6717. * @flag: Flag indicating if this command can be put into txq.
  6718. *
  6719. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6720. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6721. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6722. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6723. * this function allows only iocbs for posting buffers. This function finds
  6724. * next available slot in the command ring and posts the command to the
  6725. * available slot and writes the port attention register to request HBA start
  6726. * processing new iocb. If there is no slot available in the ring and
  6727. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6728. * the function returns IOCB_BUSY.
  6729. *
  6730. * This function is called with hbalock held. The function will return success
  6731. * after it successfully submit the iocb to firmware or after adding to the
  6732. * txq.
  6733. **/
  6734. static int
  6735. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6736. struct lpfc_iocbq *piocb, uint32_t flag)
  6737. {
  6738. struct lpfc_iocbq *nextiocb;
  6739. IOCB_t *iocb;
  6740. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6741. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6742. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6743. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6744. lpfc_printf_log(phba, KERN_ERR,
  6745. LOG_SLI | LOG_VPORT,
  6746. "1807 IOCB x%x failed. No vport\n",
  6747. piocb->iocb.ulpCommand);
  6748. dump_stack();
  6749. return IOCB_ERROR;
  6750. }
  6751. /* If the PCI channel is in offline state, do not post iocbs. */
  6752. if (unlikely(pci_channel_offline(phba->pcidev)))
  6753. return IOCB_ERROR;
  6754. /* If HBA has a deferred error attention, fail the iocb. */
  6755. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6756. return IOCB_ERROR;
  6757. /*
  6758. * We should never get an IOCB if we are in a < LINK_DOWN state
  6759. */
  6760. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6761. return IOCB_ERROR;
  6762. /*
  6763. * Check to see if we are blocking IOCB processing because of a
  6764. * outstanding event.
  6765. */
  6766. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6767. goto iocb_busy;
  6768. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6769. /*
  6770. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6771. * can be issued if the link is not up.
  6772. */
  6773. switch (piocb->iocb.ulpCommand) {
  6774. case CMD_GEN_REQUEST64_CR:
  6775. case CMD_GEN_REQUEST64_CX:
  6776. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6777. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6778. FC_RCTL_DD_UNSOL_CMD) ||
  6779. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6780. MENLO_TRANSPORT_TYPE))
  6781. goto iocb_busy;
  6782. break;
  6783. case CMD_QUE_RING_BUF_CN:
  6784. case CMD_QUE_RING_BUF64_CN:
  6785. /*
  6786. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6787. * completion, iocb_cmpl MUST be 0.
  6788. */
  6789. if (piocb->iocb_cmpl)
  6790. piocb->iocb_cmpl = NULL;
  6791. /*FALLTHROUGH*/
  6792. case CMD_CREATE_XRI_CR:
  6793. case CMD_CLOSE_XRI_CN:
  6794. case CMD_CLOSE_XRI_CX:
  6795. break;
  6796. default:
  6797. goto iocb_busy;
  6798. }
  6799. /*
  6800. * For FCP commands, we must be in a state where we can process link
  6801. * attention events.
  6802. */
  6803. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6804. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6805. goto iocb_busy;
  6806. }
  6807. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6808. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6809. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6810. if (iocb)
  6811. lpfc_sli_update_ring(phba, pring);
  6812. else
  6813. lpfc_sli_update_full_ring(phba, pring);
  6814. if (!piocb)
  6815. return IOCB_SUCCESS;
  6816. goto out_busy;
  6817. iocb_busy:
  6818. pring->stats.iocb_cmd_delay++;
  6819. out_busy:
  6820. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6821. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6822. return IOCB_SUCCESS;
  6823. }
  6824. return IOCB_BUSY;
  6825. }
  6826. /**
  6827. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6828. * @phba: Pointer to HBA context object.
  6829. * @piocb: Pointer to command iocb.
  6830. * @sglq: Pointer to the scatter gather queue object.
  6831. *
  6832. * This routine converts the bpl or bde that is in the IOCB
  6833. * to a sgl list for the sli4 hardware. The physical address
  6834. * of the bpl/bde is converted back to a virtual address.
  6835. * If the IOCB contains a BPL then the list of BDE's is
  6836. * converted to sli4_sge's. If the IOCB contains a single
  6837. * BDE then it is converted to a single sli_sge.
  6838. * The IOCB is still in cpu endianess so the contents of
  6839. * the bpl can be used without byte swapping.
  6840. *
  6841. * Returns valid XRI = Success, NO_XRI = Failure.
  6842. **/
  6843. static uint16_t
  6844. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6845. struct lpfc_sglq *sglq)
  6846. {
  6847. uint16_t xritag = NO_XRI;
  6848. struct ulp_bde64 *bpl = NULL;
  6849. struct ulp_bde64 bde;
  6850. struct sli4_sge *sgl = NULL;
  6851. struct lpfc_dmabuf *dmabuf;
  6852. IOCB_t *icmd;
  6853. int numBdes = 0;
  6854. int i = 0;
  6855. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6856. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6857. if (!piocbq || !sglq)
  6858. return xritag;
  6859. sgl = (struct sli4_sge *)sglq->sgl;
  6860. icmd = &piocbq->iocb;
  6861. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6862. return sglq->sli4_xritag;
  6863. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6864. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6865. sizeof(struct ulp_bde64);
  6866. /* The addrHigh and addrLow fields within the IOCB
  6867. * have not been byteswapped yet so there is no
  6868. * need to swap them back.
  6869. */
  6870. if (piocbq->context3)
  6871. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  6872. else
  6873. return xritag;
  6874. bpl = (struct ulp_bde64 *)dmabuf->virt;
  6875. if (!bpl)
  6876. return xritag;
  6877. for (i = 0; i < numBdes; i++) {
  6878. /* Should already be byte swapped. */
  6879. sgl->addr_hi = bpl->addrHigh;
  6880. sgl->addr_lo = bpl->addrLow;
  6881. sgl->word2 = le32_to_cpu(sgl->word2);
  6882. if ((i+1) == numBdes)
  6883. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6884. else
  6885. bf_set(lpfc_sli4_sge_last, sgl, 0);
  6886. /* swap the size field back to the cpu so we
  6887. * can assign it to the sgl.
  6888. */
  6889. bde.tus.w = le32_to_cpu(bpl->tus.w);
  6890. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  6891. /* The offsets in the sgl need to be accumulated
  6892. * separately for the request and reply lists.
  6893. * The request is always first, the reply follows.
  6894. */
  6895. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  6896. /* add up the reply sg entries */
  6897. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  6898. inbound++;
  6899. /* first inbound? reset the offset */
  6900. if (inbound == 1)
  6901. offset = 0;
  6902. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  6903. bf_set(lpfc_sli4_sge_type, sgl,
  6904. LPFC_SGE_TYPE_DATA);
  6905. offset += bde.tus.f.bdeSize;
  6906. }
  6907. sgl->word2 = cpu_to_le32(sgl->word2);
  6908. bpl++;
  6909. sgl++;
  6910. }
  6911. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  6912. /* The addrHigh and addrLow fields of the BDE have not
  6913. * been byteswapped yet so they need to be swapped
  6914. * before putting them in the sgl.
  6915. */
  6916. sgl->addr_hi =
  6917. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  6918. sgl->addr_lo =
  6919. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  6920. sgl->word2 = le32_to_cpu(sgl->word2);
  6921. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6922. sgl->word2 = cpu_to_le32(sgl->word2);
  6923. sgl->sge_len =
  6924. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  6925. }
  6926. return sglq->sli4_xritag;
  6927. }
  6928. /**
  6929. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  6930. * @phba: Pointer to HBA context object.
  6931. *
  6932. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  6933. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  6934. * held.
  6935. *
  6936. * Return: index into SLI4 fast-path FCP queue index.
  6937. **/
  6938. static uint32_t
  6939. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  6940. {
  6941. ++phba->fcp_qidx;
  6942. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  6943. phba->fcp_qidx = 0;
  6944. return phba->fcp_qidx;
  6945. }
  6946. /**
  6947. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  6948. * @phba: Pointer to HBA context object.
  6949. * @piocb: Pointer to command iocb.
  6950. * @wqe: Pointer to the work queue entry.
  6951. *
  6952. * This routine converts the iocb command to its Work Queue Entry
  6953. * equivalent. The wqe pointer should not have any fields set when
  6954. * this routine is called because it will memcpy over them.
  6955. * This routine does not set the CQ_ID or the WQEC bits in the
  6956. * wqe.
  6957. *
  6958. * Returns: 0 = Success, IOCB_ERROR = Failure.
  6959. **/
  6960. static int
  6961. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  6962. union lpfc_wqe *wqe)
  6963. {
  6964. uint32_t xmit_len = 0, total_len = 0;
  6965. uint8_t ct = 0;
  6966. uint32_t fip;
  6967. uint32_t abort_tag;
  6968. uint8_t command_type = ELS_COMMAND_NON_FIP;
  6969. uint8_t cmnd;
  6970. uint16_t xritag;
  6971. uint16_t abrt_iotag;
  6972. struct lpfc_iocbq *abrtiocbq;
  6973. struct ulp_bde64 *bpl = NULL;
  6974. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  6975. int numBdes, i;
  6976. struct ulp_bde64 bde;
  6977. struct lpfc_nodelist *ndlp;
  6978. uint32_t *pcmd;
  6979. uint32_t if_type;
  6980. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  6981. /* The fcp commands will set command type */
  6982. if (iocbq->iocb_flag & LPFC_IO_FCP)
  6983. command_type = FCP_COMMAND;
  6984. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  6985. command_type = ELS_COMMAND_FIP;
  6986. else
  6987. command_type = ELS_COMMAND_NON_FIP;
  6988. /* Some of the fields are in the right position already */
  6989. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  6990. abort_tag = (uint32_t) iocbq->iotag;
  6991. xritag = iocbq->sli4_xritag;
  6992. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  6993. /* words0-2 bpl convert bde */
  6994. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6995. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  6996. sizeof(struct ulp_bde64);
  6997. bpl = (struct ulp_bde64 *)
  6998. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  6999. if (!bpl)
  7000. return IOCB_ERROR;
  7001. /* Should already be byte swapped. */
  7002. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7003. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7004. /* swap the size field back to the cpu so we
  7005. * can assign it to the sgl.
  7006. */
  7007. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7008. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7009. total_len = 0;
  7010. for (i = 0; i < numBdes; i++) {
  7011. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7012. total_len += bde.tus.f.bdeSize;
  7013. }
  7014. } else
  7015. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7016. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7017. cmnd = iocbq->iocb.ulpCommand;
  7018. switch (iocbq->iocb.ulpCommand) {
  7019. case CMD_ELS_REQUEST64_CR:
  7020. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7021. if (!iocbq->iocb.ulpLe) {
  7022. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7023. "2007 Only Limited Edition cmd Format"
  7024. " supported 0x%x\n",
  7025. iocbq->iocb.ulpCommand);
  7026. return IOCB_ERROR;
  7027. }
  7028. wqe->els_req.payload_len = xmit_len;
  7029. /* Els_reguest64 has a TMO */
  7030. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7031. iocbq->iocb.ulpTimeout);
  7032. /* Need a VF for word 4 set the vf bit*/
  7033. bf_set(els_req64_vf, &wqe->els_req, 0);
  7034. /* And a VFID for word 12 */
  7035. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7036. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7037. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7038. iocbq->iocb.ulpContext);
  7039. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7040. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7041. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7042. if (command_type == ELS_COMMAND_FIP)
  7043. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7044. >> LPFC_FIP_ELS_ID_SHIFT);
  7045. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7046. iocbq->context2)->virt);
  7047. if_type = bf_get(lpfc_sli_intf_if_type,
  7048. &phba->sli4_hba.sli_intf);
  7049. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7050. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7051. *pcmd == ELS_CMD_SCR ||
  7052. *pcmd == ELS_CMD_FDISC ||
  7053. *pcmd == ELS_CMD_PLOGI)) {
  7054. bf_set(els_req64_sp, &wqe->els_req, 1);
  7055. bf_set(els_req64_sid, &wqe->els_req,
  7056. iocbq->vport->fc_myDID);
  7057. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7058. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7059. phba->vpi_ids[phba->pport->vpi]);
  7060. } else if (pcmd && iocbq->context1) {
  7061. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7062. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7063. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7064. }
  7065. }
  7066. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7067. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7068. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7069. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7070. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7071. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7072. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7073. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7074. break;
  7075. case CMD_XMIT_SEQUENCE64_CX:
  7076. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7077. iocbq->iocb.un.ulpWord[3]);
  7078. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7079. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7080. /* The entire sequence is transmitted for this IOCB */
  7081. xmit_len = total_len;
  7082. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7083. if (phba->link_flag & LS_LOOPBACK_MODE)
  7084. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7085. case CMD_XMIT_SEQUENCE64_CR:
  7086. /* word3 iocb=io_tag32 wqe=reserved */
  7087. wqe->xmit_sequence.rsvd3 = 0;
  7088. /* word4 relative_offset memcpy */
  7089. /* word5 r_ctl/df_ctl memcpy */
  7090. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7091. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7092. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7093. LPFC_WQE_IOD_WRITE);
  7094. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7095. LPFC_WQE_LENLOC_WORD12);
  7096. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7097. wqe->xmit_sequence.xmit_len = xmit_len;
  7098. command_type = OTHER_COMMAND;
  7099. break;
  7100. case CMD_XMIT_BCAST64_CN:
  7101. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7102. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7103. /* word4 iocb=rsvd wqe=rsvd */
  7104. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7105. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7106. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7107. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7108. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7109. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7110. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7111. LPFC_WQE_LENLOC_WORD3);
  7112. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7113. break;
  7114. case CMD_FCP_IWRITE64_CR:
  7115. command_type = FCP_COMMAND_DATA_OUT;
  7116. /* word3 iocb=iotag wqe=payload_offset_len */
  7117. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7118. wqe->fcp_iwrite.payload_offset_len =
  7119. xmit_len + sizeof(struct fcp_rsp);
  7120. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7121. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7122. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7123. iocbq->iocb.ulpFCP2Rcvy);
  7124. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7125. /* Always open the exchange */
  7126. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7127. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7128. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7129. LPFC_WQE_LENLOC_WORD4);
  7130. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7131. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7132. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7133. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7134. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7135. }
  7136. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7137. break;
  7138. case CMD_FCP_IREAD64_CR:
  7139. /* word3 iocb=iotag wqe=payload_offset_len */
  7140. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7141. wqe->fcp_iread.payload_offset_len =
  7142. xmit_len + sizeof(struct fcp_rsp);
  7143. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7144. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7145. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7146. iocbq->iocb.ulpFCP2Rcvy);
  7147. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7148. /* Always open the exchange */
  7149. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7150. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7151. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7152. LPFC_WQE_LENLOC_WORD4);
  7153. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7154. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7155. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7156. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7157. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7158. }
  7159. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7160. break;
  7161. case CMD_FCP_ICMND64_CR:
  7162. /* word3 iocb=IO_TAG wqe=reserved */
  7163. wqe->fcp_icmd.rsrvd3 = 0;
  7164. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7165. /* Always open the exchange */
  7166. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7167. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7168. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7169. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7170. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7171. LPFC_WQE_LENLOC_NONE);
  7172. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7173. break;
  7174. case CMD_GEN_REQUEST64_CR:
  7175. /* For this command calculate the xmit length of the
  7176. * request bde.
  7177. */
  7178. xmit_len = 0;
  7179. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7180. sizeof(struct ulp_bde64);
  7181. for (i = 0; i < numBdes; i++) {
  7182. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7183. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7184. break;
  7185. xmit_len += bde.tus.f.bdeSize;
  7186. }
  7187. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7188. wqe->gen_req.request_payload_len = xmit_len;
  7189. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7190. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7191. /* word6 context tag copied in memcpy */
  7192. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7193. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7194. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7195. "2015 Invalid CT %x command 0x%x\n",
  7196. ct, iocbq->iocb.ulpCommand);
  7197. return IOCB_ERROR;
  7198. }
  7199. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7200. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7201. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7202. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7203. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7204. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7205. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7206. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7207. command_type = OTHER_COMMAND;
  7208. break;
  7209. case CMD_XMIT_ELS_RSP64_CX:
  7210. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7211. /* words0-2 BDE memcpy */
  7212. /* word3 iocb=iotag32 wqe=response_payload_len */
  7213. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7214. /* word4 iocb=did wge=rsvd. */
  7215. wqe->xmit_els_rsp.rsvd4 = 0;
  7216. /* word5 iocb=rsvd wge=did */
  7217. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7218. iocbq->iocb.un.elsreq64.remoteID);
  7219. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7220. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7221. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7222. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7223. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7224. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7225. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7226. phba->vpi_ids[iocbq->vport->vpi]);
  7227. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7228. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7229. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7230. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7231. LPFC_WQE_LENLOC_WORD3);
  7232. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7233. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7234. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7235. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7236. iocbq->context2)->virt);
  7237. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7238. bf_set(els_req64_sp, &wqe->els_req, 1);
  7239. bf_set(els_req64_sid, &wqe->els_req,
  7240. iocbq->vport->fc_myDID);
  7241. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7242. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7243. phba->vpi_ids[phba->pport->vpi]);
  7244. }
  7245. command_type = OTHER_COMMAND;
  7246. break;
  7247. case CMD_CLOSE_XRI_CN:
  7248. case CMD_ABORT_XRI_CN:
  7249. case CMD_ABORT_XRI_CX:
  7250. /* words 0-2 memcpy should be 0 rserved */
  7251. /* port will send abts */
  7252. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7253. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7254. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7255. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7256. } else
  7257. fip = 0;
  7258. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7259. /*
  7260. * The link is down, or the command was ELS_FIP
  7261. * so the fw does not need to send abts
  7262. * on the wire.
  7263. */
  7264. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7265. else
  7266. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7267. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7268. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7269. wqe->abort_cmd.rsrvd5 = 0;
  7270. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7271. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7272. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7273. /*
  7274. * The abort handler will send us CMD_ABORT_XRI_CN or
  7275. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7276. */
  7277. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7278. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7279. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7280. LPFC_WQE_LENLOC_NONE);
  7281. cmnd = CMD_ABORT_XRI_CX;
  7282. command_type = OTHER_COMMAND;
  7283. xritag = 0;
  7284. break;
  7285. case CMD_XMIT_BLS_RSP64_CX:
  7286. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7287. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7288. * we re-construct this WQE here based on information in
  7289. * iocbq from scratch.
  7290. */
  7291. memset(wqe, 0, sizeof(union lpfc_wqe));
  7292. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7293. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7294. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7295. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7296. LPFC_ABTS_UNSOL_INT) {
  7297. /* ABTS sent by initiator to CT exchange, the
  7298. * RX_ID field will be filled with the newly
  7299. * allocated responder XRI.
  7300. */
  7301. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7302. iocbq->sli4_xritag);
  7303. } else {
  7304. /* ABTS sent by responder to CT exchange, the
  7305. * RX_ID field will be filled with the responder
  7306. * RX_ID from ABTS.
  7307. */
  7308. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7309. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7310. }
  7311. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7312. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7313. /* Use CT=VPI */
  7314. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7315. ndlp->nlp_DID);
  7316. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7317. iocbq->iocb.ulpContext);
  7318. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7319. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7320. phba->vpi_ids[phba->pport->vpi]);
  7321. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7322. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7323. LPFC_WQE_LENLOC_NONE);
  7324. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7325. command_type = OTHER_COMMAND;
  7326. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7327. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7328. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7329. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7330. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7331. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7332. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7333. }
  7334. break;
  7335. case CMD_XRI_ABORTED_CX:
  7336. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7337. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7338. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7339. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7340. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7341. default:
  7342. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7343. "2014 Invalid command 0x%x\n",
  7344. iocbq->iocb.ulpCommand);
  7345. return IOCB_ERROR;
  7346. break;
  7347. }
  7348. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7349. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7350. wqe->generic.wqe_com.abort_tag = abort_tag;
  7351. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7352. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7353. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7354. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7355. return 0;
  7356. }
  7357. /**
  7358. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7359. * @phba: Pointer to HBA context object.
  7360. * @ring_number: SLI ring number to issue iocb on.
  7361. * @piocb: Pointer to command iocb.
  7362. * @flag: Flag indicating if this command can be put into txq.
  7363. *
  7364. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7365. * an iocb command to an HBA with SLI-4 interface spec.
  7366. *
  7367. * This function is called with hbalock held. The function will return success
  7368. * after it successfully submit the iocb to firmware or after adding to the
  7369. * txq.
  7370. **/
  7371. static int
  7372. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7373. struct lpfc_iocbq *piocb, uint32_t flag)
  7374. {
  7375. struct lpfc_sglq *sglq;
  7376. union lpfc_wqe wqe;
  7377. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7378. if (piocb->sli4_xritag == NO_XRI) {
  7379. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7380. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7381. sglq = NULL;
  7382. else {
  7383. if (pring->txq_cnt) {
  7384. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7385. __lpfc_sli_ringtx_put(phba,
  7386. pring, piocb);
  7387. return IOCB_SUCCESS;
  7388. } else {
  7389. return IOCB_BUSY;
  7390. }
  7391. } else {
  7392. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7393. if (!sglq) {
  7394. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7395. __lpfc_sli_ringtx_put(phba,
  7396. pring,
  7397. piocb);
  7398. return IOCB_SUCCESS;
  7399. } else
  7400. return IOCB_BUSY;
  7401. }
  7402. }
  7403. }
  7404. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7405. /* These IO's already have an XRI and a mapped sgl. */
  7406. sglq = NULL;
  7407. } else {
  7408. /*
  7409. * This is a continuation of a commandi,(CX) so this
  7410. * sglq is on the active list
  7411. */
  7412. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7413. if (!sglq)
  7414. return IOCB_ERROR;
  7415. }
  7416. if (sglq) {
  7417. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7418. piocb->sli4_xritag = sglq->sli4_xritag;
  7419. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7420. return IOCB_ERROR;
  7421. }
  7422. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7423. return IOCB_ERROR;
  7424. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7425. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7426. /*
  7427. * For FCP command IOCB, get a new WQ index to distribute
  7428. * WQE across the WQsr. On the other hand, for abort IOCB,
  7429. * it carries the same WQ index to the original command
  7430. * IOCB.
  7431. */
  7432. if (piocb->iocb_flag & LPFC_IO_FCP)
  7433. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7434. if (unlikely(!phba->sli4_hba.fcp_wq))
  7435. return IOCB_ERROR;
  7436. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7437. &wqe))
  7438. return IOCB_ERROR;
  7439. } else {
  7440. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7441. return IOCB_ERROR;
  7442. }
  7443. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7444. return 0;
  7445. }
  7446. /**
  7447. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7448. *
  7449. * This routine wraps the actual lockless version for issusing IOCB function
  7450. * pointer from the lpfc_hba struct.
  7451. *
  7452. * Return codes:
  7453. * IOCB_ERROR - Error
  7454. * IOCB_SUCCESS - Success
  7455. * IOCB_BUSY - Busy
  7456. **/
  7457. int
  7458. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7459. struct lpfc_iocbq *piocb, uint32_t flag)
  7460. {
  7461. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7462. }
  7463. /**
  7464. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7465. * @phba: The hba struct for which this call is being executed.
  7466. * @dev_grp: The HBA PCI-Device group number.
  7467. *
  7468. * This routine sets up the SLI interface API function jump table in @phba
  7469. * struct.
  7470. * Returns: 0 - success, -ENODEV - failure.
  7471. **/
  7472. int
  7473. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7474. {
  7475. switch (dev_grp) {
  7476. case LPFC_PCI_DEV_LP:
  7477. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7478. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7479. break;
  7480. case LPFC_PCI_DEV_OC:
  7481. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7482. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7483. break;
  7484. default:
  7485. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7486. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7487. dev_grp);
  7488. return -ENODEV;
  7489. break;
  7490. }
  7491. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7492. return 0;
  7493. }
  7494. /**
  7495. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7496. * @phba: Pointer to HBA context object.
  7497. * @pring: Pointer to driver SLI ring object.
  7498. * @piocb: Pointer to command iocb.
  7499. * @flag: Flag indicating if this command can be put into txq.
  7500. *
  7501. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7502. * function. This function gets the hbalock and calls
  7503. * __lpfc_sli_issue_iocb function and will return the error returned
  7504. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7505. * functions which do not hold hbalock.
  7506. **/
  7507. int
  7508. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7509. struct lpfc_iocbq *piocb, uint32_t flag)
  7510. {
  7511. unsigned long iflags;
  7512. int rc;
  7513. spin_lock_irqsave(&phba->hbalock, iflags);
  7514. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7515. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7516. return rc;
  7517. }
  7518. /**
  7519. * lpfc_extra_ring_setup - Extra ring setup function
  7520. * @phba: Pointer to HBA context object.
  7521. *
  7522. * This function is called while driver attaches with the
  7523. * HBA to setup the extra ring. The extra ring is used
  7524. * only when driver needs to support target mode functionality
  7525. * or IP over FC functionalities.
  7526. *
  7527. * This function is called with no lock held.
  7528. **/
  7529. static int
  7530. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7531. {
  7532. struct lpfc_sli *psli;
  7533. struct lpfc_sli_ring *pring;
  7534. psli = &phba->sli;
  7535. /* Adjust cmd/rsp ring iocb entries more evenly */
  7536. /* Take some away from the FCP ring */
  7537. pring = &psli->ring[psli->fcp_ring];
  7538. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7539. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7540. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7541. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7542. /* and give them to the extra ring */
  7543. pring = &psli->ring[psli->extra_ring];
  7544. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7545. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7546. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7547. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7548. /* Setup default profile for this ring */
  7549. pring->iotag_max = 4096;
  7550. pring->num_mask = 1;
  7551. pring->prt[0].profile = 0; /* Mask 0 */
  7552. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7553. pring->prt[0].type = phba->cfg_multi_ring_type;
  7554. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7555. return 0;
  7556. }
  7557. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7558. * @vport: pointer to virtual port object.
  7559. * @ndlp: nodelist pointer for the impacted rport.
  7560. *
  7561. * The driver calls this routine in response to a XRI ABORT CQE
  7562. * event from the port. In this event, the driver is required to
  7563. * recover its login to the rport even though its login may be valid
  7564. * from the driver's perspective. The failed ABTS notice from the
  7565. * port indicates the rport is not responding.
  7566. */
  7567. static void
  7568. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7569. struct lpfc_nodelist *ndlp)
  7570. {
  7571. struct Scsi_Host *shost;
  7572. struct lpfc_hba *phba;
  7573. unsigned long flags = 0;
  7574. shost = lpfc_shost_from_vport(vport);
  7575. phba = vport->phba;
  7576. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7577. lpfc_printf_log(phba, KERN_INFO,
  7578. LOG_SLI, "3093 No rport recovery needed. "
  7579. "rport in state 0x%x\n",
  7580. ndlp->nlp_state);
  7581. return;
  7582. }
  7583. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7584. "3094 Start rport recovery on shost id 0x%x "
  7585. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7586. "flags 0x%x\n",
  7587. shost->host_no, ndlp->nlp_DID,
  7588. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7589. ndlp->nlp_flag);
  7590. /*
  7591. * The rport is not responding. Don't attempt ADISC recovery.
  7592. * Remove the FCP-2 flag to force a PLOGI.
  7593. */
  7594. spin_lock_irqsave(shost->host_lock, flags);
  7595. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7596. spin_unlock_irqrestore(shost->host_lock, flags);
  7597. lpfc_disc_state_machine(vport, ndlp, NULL,
  7598. NLP_EVT_DEVICE_RECOVERY);
  7599. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7600. spin_lock_irqsave(shost->host_lock, flags);
  7601. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7602. spin_unlock_irqrestore(shost->host_lock, flags);
  7603. lpfc_disc_start(vport);
  7604. }
  7605. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7606. * @phba: Pointer to HBA context object.
  7607. * @iocbq: Pointer to iocb object.
  7608. *
  7609. * The async_event handler calls this routine when it receives
  7610. * an ASYNC_STATUS_CN event from the port. The port generates
  7611. * this event when an Abort Sequence request to an rport fails
  7612. * twice in succession. The abort could be originated by the
  7613. * driver or by the port. The ABTS could have been for an ELS
  7614. * or FCP IO. The port only generates this event when an ABTS
  7615. * fails to complete after one retry.
  7616. */
  7617. static void
  7618. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7619. struct lpfc_iocbq *iocbq)
  7620. {
  7621. struct lpfc_nodelist *ndlp = NULL;
  7622. uint16_t rpi = 0, vpi = 0;
  7623. struct lpfc_vport *vport = NULL;
  7624. /* The rpi in the ulpContext is vport-sensitive. */
  7625. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7626. rpi = iocbq->iocb.ulpContext;
  7627. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7628. "3092 Port generated ABTS async event "
  7629. "on vpi %d rpi %d status 0x%x\n",
  7630. vpi, rpi, iocbq->iocb.ulpStatus);
  7631. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7632. if (!vport)
  7633. goto err_exit;
  7634. ndlp = lpfc_findnode_rpi(vport, rpi);
  7635. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7636. goto err_exit;
  7637. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7638. lpfc_sli_abts_recover_port(vport, ndlp);
  7639. return;
  7640. err_exit:
  7641. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7642. "3095 Event Context not found, no "
  7643. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7644. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7645. vpi, rpi);
  7646. }
  7647. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7648. * @phba: pointer to HBA context object.
  7649. * @ndlp: nodelist pointer for the impacted rport.
  7650. * @axri: pointer to the wcqe containing the failed exchange.
  7651. *
  7652. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7653. * port. The port generates this event when an abort exchange request to an
  7654. * rport fails twice in succession with no reply. The abort could be originated
  7655. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7656. */
  7657. void
  7658. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7659. struct lpfc_nodelist *ndlp,
  7660. struct sli4_wcqe_xri_aborted *axri)
  7661. {
  7662. struct lpfc_vport *vport;
  7663. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7664. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7665. "3115 Node Context not found, driver "
  7666. "ignoring abts err event\n");
  7667. return;
  7668. }
  7669. vport = ndlp->vport;
  7670. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7671. "3116 Port generated FCP XRI ABORT event on "
  7672. "vpi %d rpi %d xri x%x status 0x%x\n",
  7673. ndlp->vport->vpi, ndlp->nlp_rpi,
  7674. bf_get(lpfc_wcqe_xa_xri, axri),
  7675. bf_get(lpfc_wcqe_xa_status, axri));
  7676. if (bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT)
  7677. lpfc_sli_abts_recover_port(vport, ndlp);
  7678. }
  7679. /**
  7680. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7681. * @phba: Pointer to HBA context object.
  7682. * @pring: Pointer to driver SLI ring object.
  7683. * @iocbq: Pointer to iocb object.
  7684. *
  7685. * This function is called by the slow ring event handler
  7686. * function when there is an ASYNC event iocb in the ring.
  7687. * This function is called with no lock held.
  7688. * Currently this function handles only temperature related
  7689. * ASYNC events. The function decodes the temperature sensor
  7690. * event message and posts events for the management applications.
  7691. **/
  7692. static void
  7693. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7694. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7695. {
  7696. IOCB_t *icmd;
  7697. uint16_t evt_code;
  7698. struct temp_event temp_event_data;
  7699. struct Scsi_Host *shost;
  7700. uint32_t *iocb_w;
  7701. icmd = &iocbq->iocb;
  7702. evt_code = icmd->un.asyncstat.evt_code;
  7703. switch (evt_code) {
  7704. case ASYNC_TEMP_WARN:
  7705. case ASYNC_TEMP_SAFE:
  7706. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7707. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7708. if (evt_code == ASYNC_TEMP_WARN) {
  7709. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7710. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7711. "0347 Adapter is very hot, please take "
  7712. "corrective action. temperature : %d Celsius\n",
  7713. (uint32_t) icmd->ulpContext);
  7714. } else {
  7715. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7716. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7717. "0340 Adapter temperature is OK now. "
  7718. "temperature : %d Celsius\n",
  7719. (uint32_t) icmd->ulpContext);
  7720. }
  7721. /* Send temperature change event to applications */
  7722. shost = lpfc_shost_from_vport(phba->pport);
  7723. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7724. sizeof(temp_event_data), (char *) &temp_event_data,
  7725. LPFC_NL_VENDOR_ID);
  7726. break;
  7727. case ASYNC_STATUS_CN:
  7728. lpfc_sli_abts_err_handler(phba, iocbq);
  7729. break;
  7730. default:
  7731. iocb_w = (uint32_t *) icmd;
  7732. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7733. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7734. " evt_code 0x%x\n"
  7735. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7736. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7737. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7738. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7739. pring->ringno, icmd->un.asyncstat.evt_code,
  7740. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7741. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7742. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7743. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7744. break;
  7745. }
  7746. }
  7747. /**
  7748. * lpfc_sli_setup - SLI ring setup function
  7749. * @phba: Pointer to HBA context object.
  7750. *
  7751. * lpfc_sli_setup sets up rings of the SLI interface with
  7752. * number of iocbs per ring and iotags. This function is
  7753. * called while driver attach to the HBA and before the
  7754. * interrupts are enabled. So there is no need for locking.
  7755. *
  7756. * This function always returns 0.
  7757. **/
  7758. int
  7759. lpfc_sli_setup(struct lpfc_hba *phba)
  7760. {
  7761. int i, totiocbsize = 0;
  7762. struct lpfc_sli *psli = &phba->sli;
  7763. struct lpfc_sli_ring *pring;
  7764. psli->num_rings = MAX_CONFIGURED_RINGS;
  7765. psli->sli_flag = 0;
  7766. psli->fcp_ring = LPFC_FCP_RING;
  7767. psli->next_ring = LPFC_FCP_NEXT_RING;
  7768. psli->extra_ring = LPFC_EXTRA_RING;
  7769. psli->iocbq_lookup = NULL;
  7770. psli->iocbq_lookup_len = 0;
  7771. psli->last_iotag = 0;
  7772. for (i = 0; i < psli->num_rings; i++) {
  7773. pring = &psli->ring[i];
  7774. switch (i) {
  7775. case LPFC_FCP_RING: /* ring 0 - FCP */
  7776. /* numCiocb and numRiocb are used in config_port */
  7777. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7778. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7779. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7780. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7781. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7782. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7783. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7784. SLI3_IOCB_CMD_SIZE :
  7785. SLI2_IOCB_CMD_SIZE;
  7786. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7787. SLI3_IOCB_RSP_SIZE :
  7788. SLI2_IOCB_RSP_SIZE;
  7789. pring->iotag_ctr = 0;
  7790. pring->iotag_max =
  7791. (phba->cfg_hba_queue_depth * 2);
  7792. pring->fast_iotag = pring->iotag_max;
  7793. pring->num_mask = 0;
  7794. break;
  7795. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7796. /* numCiocb and numRiocb are used in config_port */
  7797. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7798. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7799. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7800. SLI3_IOCB_CMD_SIZE :
  7801. SLI2_IOCB_CMD_SIZE;
  7802. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7803. SLI3_IOCB_RSP_SIZE :
  7804. SLI2_IOCB_RSP_SIZE;
  7805. pring->iotag_max = phba->cfg_hba_queue_depth;
  7806. pring->num_mask = 0;
  7807. break;
  7808. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7809. /* numCiocb and numRiocb are used in config_port */
  7810. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7811. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7812. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7813. SLI3_IOCB_CMD_SIZE :
  7814. SLI2_IOCB_CMD_SIZE;
  7815. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7816. SLI3_IOCB_RSP_SIZE :
  7817. SLI2_IOCB_RSP_SIZE;
  7818. pring->fast_iotag = 0;
  7819. pring->iotag_ctr = 0;
  7820. pring->iotag_max = 4096;
  7821. pring->lpfc_sli_rcv_async_status =
  7822. lpfc_sli_async_event_handler;
  7823. pring->num_mask = LPFC_MAX_RING_MASK;
  7824. pring->prt[0].profile = 0; /* Mask 0 */
  7825. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7826. pring->prt[0].type = FC_TYPE_ELS;
  7827. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7828. lpfc_els_unsol_event;
  7829. pring->prt[1].profile = 0; /* Mask 1 */
  7830. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7831. pring->prt[1].type = FC_TYPE_ELS;
  7832. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7833. lpfc_els_unsol_event;
  7834. pring->prt[2].profile = 0; /* Mask 2 */
  7835. /* NameServer Inquiry */
  7836. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7837. /* NameServer */
  7838. pring->prt[2].type = FC_TYPE_CT;
  7839. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7840. lpfc_ct_unsol_event;
  7841. pring->prt[3].profile = 0; /* Mask 3 */
  7842. /* NameServer response */
  7843. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7844. /* NameServer */
  7845. pring->prt[3].type = FC_TYPE_CT;
  7846. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7847. lpfc_ct_unsol_event;
  7848. /* abort unsolicited sequence */
  7849. pring->prt[4].profile = 0; /* Mask 4 */
  7850. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7851. pring->prt[4].type = FC_TYPE_BLS;
  7852. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7853. lpfc_sli4_ct_abort_unsol_event;
  7854. break;
  7855. }
  7856. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7857. (pring->numRiocb * pring->sizeRiocb);
  7858. }
  7859. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7860. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7861. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7862. "SLI2 SLIM Data: x%x x%lx\n",
  7863. phba->brd_no, totiocbsize,
  7864. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7865. }
  7866. if (phba->cfg_multi_ring_support == 2)
  7867. lpfc_extra_ring_setup(phba);
  7868. return 0;
  7869. }
  7870. /**
  7871. * lpfc_sli_queue_setup - Queue initialization function
  7872. * @phba: Pointer to HBA context object.
  7873. *
  7874. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  7875. * ring. This function also initializes ring indices of each ring.
  7876. * This function is called during the initialization of the SLI
  7877. * interface of an HBA.
  7878. * This function is called with no lock held and always returns
  7879. * 1.
  7880. **/
  7881. int
  7882. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  7883. {
  7884. struct lpfc_sli *psli;
  7885. struct lpfc_sli_ring *pring;
  7886. int i;
  7887. psli = &phba->sli;
  7888. spin_lock_irq(&phba->hbalock);
  7889. INIT_LIST_HEAD(&psli->mboxq);
  7890. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  7891. /* Initialize list headers for txq and txcmplq as double linked lists */
  7892. for (i = 0; i < psli->num_rings; i++) {
  7893. pring = &psli->ring[i];
  7894. pring->ringno = i;
  7895. pring->next_cmdidx = 0;
  7896. pring->local_getidx = 0;
  7897. pring->cmdidx = 0;
  7898. INIT_LIST_HEAD(&pring->txq);
  7899. INIT_LIST_HEAD(&pring->txcmplq);
  7900. INIT_LIST_HEAD(&pring->iocb_continueq);
  7901. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  7902. INIT_LIST_HEAD(&pring->postbufq);
  7903. }
  7904. spin_unlock_irq(&phba->hbalock);
  7905. return 1;
  7906. }
  7907. /**
  7908. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  7909. * @phba: Pointer to HBA context object.
  7910. *
  7911. * This routine flushes the mailbox command subsystem. It will unconditionally
  7912. * flush all the mailbox commands in the three possible stages in the mailbox
  7913. * command sub-system: pending mailbox command queue; the outstanding mailbox
  7914. * command; and completed mailbox command queue. It is caller's responsibility
  7915. * to make sure that the driver is in the proper state to flush the mailbox
  7916. * command sub-system. Namely, the posting of mailbox commands into the
  7917. * pending mailbox command queue from the various clients must be stopped;
  7918. * either the HBA is in a state that it will never works on the outstanding
  7919. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  7920. * mailbox command has been completed.
  7921. **/
  7922. static void
  7923. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  7924. {
  7925. LIST_HEAD(completions);
  7926. struct lpfc_sli *psli = &phba->sli;
  7927. LPFC_MBOXQ_t *pmb;
  7928. unsigned long iflag;
  7929. /* Flush all the mailbox commands in the mbox system */
  7930. spin_lock_irqsave(&phba->hbalock, iflag);
  7931. /* The pending mailbox command queue */
  7932. list_splice_init(&phba->sli.mboxq, &completions);
  7933. /* The outstanding active mailbox command */
  7934. if (psli->mbox_active) {
  7935. list_add_tail(&psli->mbox_active->list, &completions);
  7936. psli->mbox_active = NULL;
  7937. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7938. }
  7939. /* The completed mailbox command queue */
  7940. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  7941. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7942. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  7943. while (!list_empty(&completions)) {
  7944. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  7945. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7946. if (pmb->mbox_cmpl)
  7947. pmb->mbox_cmpl(phba, pmb);
  7948. }
  7949. }
  7950. /**
  7951. * lpfc_sli_host_down - Vport cleanup function
  7952. * @vport: Pointer to virtual port object.
  7953. *
  7954. * lpfc_sli_host_down is called to clean up the resources
  7955. * associated with a vport before destroying virtual
  7956. * port data structures.
  7957. * This function does following operations:
  7958. * - Free discovery resources associated with this virtual
  7959. * port.
  7960. * - Free iocbs associated with this virtual port in
  7961. * the txq.
  7962. * - Send abort for all iocb commands associated with this
  7963. * vport in txcmplq.
  7964. *
  7965. * This function is called with no lock held and always returns 1.
  7966. **/
  7967. int
  7968. lpfc_sli_host_down(struct lpfc_vport *vport)
  7969. {
  7970. LIST_HEAD(completions);
  7971. struct lpfc_hba *phba = vport->phba;
  7972. struct lpfc_sli *psli = &phba->sli;
  7973. struct lpfc_sli_ring *pring;
  7974. struct lpfc_iocbq *iocb, *next_iocb;
  7975. int i;
  7976. unsigned long flags = 0;
  7977. uint16_t prev_pring_flag;
  7978. lpfc_cleanup_discovery_resources(vport);
  7979. spin_lock_irqsave(&phba->hbalock, flags);
  7980. for (i = 0; i < psli->num_rings; i++) {
  7981. pring = &psli->ring[i];
  7982. prev_pring_flag = pring->flag;
  7983. /* Only slow rings */
  7984. if (pring->ringno == LPFC_ELS_RING) {
  7985. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7986. /* Set the lpfc data pending flag */
  7987. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7988. }
  7989. /*
  7990. * Error everything on the txq since these iocbs have not been
  7991. * given to the FW yet.
  7992. */
  7993. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  7994. if (iocb->vport != vport)
  7995. continue;
  7996. list_move_tail(&iocb->list, &completions);
  7997. pring->txq_cnt--;
  7998. }
  7999. /* Next issue ABTS for everything on the txcmplq */
  8000. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8001. list) {
  8002. if (iocb->vport != vport)
  8003. continue;
  8004. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8005. }
  8006. pring->flag = prev_pring_flag;
  8007. }
  8008. spin_unlock_irqrestore(&phba->hbalock, flags);
  8009. /* Cancel all the IOCBs from the completions list */
  8010. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8011. IOERR_SLI_DOWN);
  8012. return 1;
  8013. }
  8014. /**
  8015. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8016. * @phba: Pointer to HBA context object.
  8017. *
  8018. * This function cleans up all iocb, buffers, mailbox commands
  8019. * while shutting down the HBA. This function is called with no
  8020. * lock held and always returns 1.
  8021. * This function does the following to cleanup driver resources:
  8022. * - Free discovery resources for each virtual port
  8023. * - Cleanup any pending fabric iocbs
  8024. * - Iterate through the iocb txq and free each entry
  8025. * in the list.
  8026. * - Free up any buffer posted to the HBA
  8027. * - Free mailbox commands in the mailbox queue.
  8028. **/
  8029. int
  8030. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8031. {
  8032. LIST_HEAD(completions);
  8033. struct lpfc_sli *psli = &phba->sli;
  8034. struct lpfc_sli_ring *pring;
  8035. struct lpfc_dmabuf *buf_ptr;
  8036. unsigned long flags = 0;
  8037. int i;
  8038. /* Shutdown the mailbox command sub-system */
  8039. lpfc_sli_mbox_sys_shutdown(phba);
  8040. lpfc_hba_down_prep(phba);
  8041. lpfc_fabric_abort_hba(phba);
  8042. spin_lock_irqsave(&phba->hbalock, flags);
  8043. for (i = 0; i < psli->num_rings; i++) {
  8044. pring = &psli->ring[i];
  8045. /* Only slow rings */
  8046. if (pring->ringno == LPFC_ELS_RING) {
  8047. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8048. /* Set the lpfc data pending flag */
  8049. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8050. }
  8051. /*
  8052. * Error everything on the txq since these iocbs have not been
  8053. * given to the FW yet.
  8054. */
  8055. list_splice_init(&pring->txq, &completions);
  8056. pring->txq_cnt = 0;
  8057. }
  8058. spin_unlock_irqrestore(&phba->hbalock, flags);
  8059. /* Cancel all the IOCBs from the completions list */
  8060. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8061. IOERR_SLI_DOWN);
  8062. spin_lock_irqsave(&phba->hbalock, flags);
  8063. list_splice_init(&phba->elsbuf, &completions);
  8064. phba->elsbuf_cnt = 0;
  8065. phba->elsbuf_prev_cnt = 0;
  8066. spin_unlock_irqrestore(&phba->hbalock, flags);
  8067. while (!list_empty(&completions)) {
  8068. list_remove_head(&completions, buf_ptr,
  8069. struct lpfc_dmabuf, list);
  8070. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8071. kfree(buf_ptr);
  8072. }
  8073. /* Return any active mbox cmds */
  8074. del_timer_sync(&psli->mbox_tmo);
  8075. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8076. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8077. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8078. return 1;
  8079. }
  8080. /**
  8081. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8082. * @srcp: Source memory pointer.
  8083. * @destp: Destination memory pointer.
  8084. * @cnt: Number of words required to be copied.
  8085. *
  8086. * This function is used for copying data between driver memory
  8087. * and the SLI memory. This function also changes the endianness
  8088. * of each word if native endianness is different from SLI
  8089. * endianness. This function can be called with or without
  8090. * lock.
  8091. **/
  8092. void
  8093. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8094. {
  8095. uint32_t *src = srcp;
  8096. uint32_t *dest = destp;
  8097. uint32_t ldata;
  8098. int i;
  8099. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8100. ldata = *src;
  8101. ldata = le32_to_cpu(ldata);
  8102. *dest = ldata;
  8103. src++;
  8104. dest++;
  8105. }
  8106. }
  8107. /**
  8108. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8109. * @srcp: Source memory pointer.
  8110. * @destp: Destination memory pointer.
  8111. * @cnt: Number of words required to be copied.
  8112. *
  8113. * This function is used for copying data between a data structure
  8114. * with big endian representation to local endianness.
  8115. * This function can be called with or without lock.
  8116. **/
  8117. void
  8118. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8119. {
  8120. uint32_t *src = srcp;
  8121. uint32_t *dest = destp;
  8122. uint32_t ldata;
  8123. int i;
  8124. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8125. ldata = *src;
  8126. ldata = be32_to_cpu(ldata);
  8127. *dest = ldata;
  8128. src++;
  8129. dest++;
  8130. }
  8131. }
  8132. /**
  8133. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8134. * @phba: Pointer to HBA context object.
  8135. * @pring: Pointer to driver SLI ring object.
  8136. * @mp: Pointer to driver buffer object.
  8137. *
  8138. * This function is called with no lock held.
  8139. * It always return zero after adding the buffer to the postbufq
  8140. * buffer list.
  8141. **/
  8142. int
  8143. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8144. struct lpfc_dmabuf *mp)
  8145. {
  8146. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8147. later */
  8148. spin_lock_irq(&phba->hbalock);
  8149. list_add_tail(&mp->list, &pring->postbufq);
  8150. pring->postbufq_cnt++;
  8151. spin_unlock_irq(&phba->hbalock);
  8152. return 0;
  8153. }
  8154. /**
  8155. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8156. * @phba: Pointer to HBA context object.
  8157. *
  8158. * When HBQ is enabled, buffers are searched based on tags. This function
  8159. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8160. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8161. * does not conflict with tags of buffer posted for unsolicited events.
  8162. * The function returns the allocated tag. The function is called with
  8163. * no locks held.
  8164. **/
  8165. uint32_t
  8166. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8167. {
  8168. spin_lock_irq(&phba->hbalock);
  8169. phba->buffer_tag_count++;
  8170. /*
  8171. * Always set the QUE_BUFTAG_BIT to distiguish between
  8172. * a tag assigned by HBQ.
  8173. */
  8174. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8175. spin_unlock_irq(&phba->hbalock);
  8176. return phba->buffer_tag_count;
  8177. }
  8178. /**
  8179. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8180. * @phba: Pointer to HBA context object.
  8181. * @pring: Pointer to driver SLI ring object.
  8182. * @tag: Buffer tag.
  8183. *
  8184. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8185. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8186. * iocb is posted to the response ring with the tag of the buffer.
  8187. * This function searches the pring->postbufq list using the tag
  8188. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8189. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8190. * buffer is returned to the caller else NULL is returned.
  8191. * This function is called with no lock held.
  8192. **/
  8193. struct lpfc_dmabuf *
  8194. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8195. uint32_t tag)
  8196. {
  8197. struct lpfc_dmabuf *mp, *next_mp;
  8198. struct list_head *slp = &pring->postbufq;
  8199. /* Search postbufq, from the beginning, looking for a match on tag */
  8200. spin_lock_irq(&phba->hbalock);
  8201. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8202. if (mp->buffer_tag == tag) {
  8203. list_del_init(&mp->list);
  8204. pring->postbufq_cnt--;
  8205. spin_unlock_irq(&phba->hbalock);
  8206. return mp;
  8207. }
  8208. }
  8209. spin_unlock_irq(&phba->hbalock);
  8210. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8211. "0402 Cannot find virtual addr for buffer tag on "
  8212. "ring %d Data x%lx x%p x%p x%x\n",
  8213. pring->ringno, (unsigned long) tag,
  8214. slp->next, slp->prev, pring->postbufq_cnt);
  8215. return NULL;
  8216. }
  8217. /**
  8218. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8219. * @phba: Pointer to HBA context object.
  8220. * @pring: Pointer to driver SLI ring object.
  8221. * @phys: DMA address of the buffer.
  8222. *
  8223. * This function searches the buffer list using the dma_address
  8224. * of unsolicited event to find the driver's lpfc_dmabuf object
  8225. * corresponding to the dma_address. The function returns the
  8226. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8227. * This function is called by the ct and els unsolicited event
  8228. * handlers to get the buffer associated with the unsolicited
  8229. * event.
  8230. *
  8231. * This function is called with no lock held.
  8232. **/
  8233. struct lpfc_dmabuf *
  8234. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8235. dma_addr_t phys)
  8236. {
  8237. struct lpfc_dmabuf *mp, *next_mp;
  8238. struct list_head *slp = &pring->postbufq;
  8239. /* Search postbufq, from the beginning, looking for a match on phys */
  8240. spin_lock_irq(&phba->hbalock);
  8241. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8242. if (mp->phys == phys) {
  8243. list_del_init(&mp->list);
  8244. pring->postbufq_cnt--;
  8245. spin_unlock_irq(&phba->hbalock);
  8246. return mp;
  8247. }
  8248. }
  8249. spin_unlock_irq(&phba->hbalock);
  8250. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8251. "0410 Cannot find virtual addr for mapped buf on "
  8252. "ring %d Data x%llx x%p x%p x%x\n",
  8253. pring->ringno, (unsigned long long)phys,
  8254. slp->next, slp->prev, pring->postbufq_cnt);
  8255. return NULL;
  8256. }
  8257. /**
  8258. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8259. * @phba: Pointer to HBA context object.
  8260. * @cmdiocb: Pointer to driver command iocb object.
  8261. * @rspiocb: Pointer to driver response iocb object.
  8262. *
  8263. * This function is the completion handler for the abort iocbs for
  8264. * ELS commands. This function is called from the ELS ring event
  8265. * handler with no lock held. This function frees memory resources
  8266. * associated with the abort iocb.
  8267. **/
  8268. static void
  8269. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8270. struct lpfc_iocbq *rspiocb)
  8271. {
  8272. IOCB_t *irsp = &rspiocb->iocb;
  8273. uint16_t abort_iotag, abort_context;
  8274. struct lpfc_iocbq *abort_iocb = NULL;
  8275. if (irsp->ulpStatus) {
  8276. /*
  8277. * Assume that the port already completed and returned, or
  8278. * will return the iocb. Just Log the message.
  8279. */
  8280. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8281. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8282. spin_lock_irq(&phba->hbalock);
  8283. if (phba->sli_rev < LPFC_SLI_REV4) {
  8284. if (abort_iotag != 0 &&
  8285. abort_iotag <= phba->sli.last_iotag)
  8286. abort_iocb =
  8287. phba->sli.iocbq_lookup[abort_iotag];
  8288. } else
  8289. /* For sli4 the abort_tag is the XRI,
  8290. * so the abort routine puts the iotag of the iocb
  8291. * being aborted in the context field of the abort
  8292. * IOCB.
  8293. */
  8294. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8295. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8296. "0327 Cannot abort els iocb %p "
  8297. "with tag %x context %x, abort status %x, "
  8298. "abort code %x\n",
  8299. abort_iocb, abort_iotag, abort_context,
  8300. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8301. spin_unlock_irq(&phba->hbalock);
  8302. }
  8303. lpfc_sli_release_iocbq(phba, cmdiocb);
  8304. return;
  8305. }
  8306. /**
  8307. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8308. * @phba: Pointer to HBA context object.
  8309. * @cmdiocb: Pointer to driver command iocb object.
  8310. * @rspiocb: Pointer to driver response iocb object.
  8311. *
  8312. * The function is called from SLI ring event handler with no
  8313. * lock held. This function is the completion handler for ELS commands
  8314. * which are aborted. The function frees memory resources used for
  8315. * the aborted ELS commands.
  8316. **/
  8317. static void
  8318. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8319. struct lpfc_iocbq *rspiocb)
  8320. {
  8321. IOCB_t *irsp = &rspiocb->iocb;
  8322. /* ELS cmd tag <ulpIoTag> completes */
  8323. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8324. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8325. "x%x x%x x%x\n",
  8326. irsp->ulpIoTag, irsp->ulpStatus,
  8327. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8328. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8329. lpfc_ct_free_iocb(phba, cmdiocb);
  8330. else
  8331. lpfc_els_free_iocb(phba, cmdiocb);
  8332. return;
  8333. }
  8334. /**
  8335. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8336. * @phba: Pointer to HBA context object.
  8337. * @pring: Pointer to driver SLI ring object.
  8338. * @cmdiocb: Pointer to driver command iocb object.
  8339. *
  8340. * This function issues an abort iocb for the provided command iocb down to
  8341. * the port. Other than the case the outstanding command iocb is an abort
  8342. * request, this function issues abort out unconditionally. This function is
  8343. * called with hbalock held. The function returns 0 when it fails due to
  8344. * memory allocation failure or when the command iocb is an abort request.
  8345. **/
  8346. static int
  8347. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8348. struct lpfc_iocbq *cmdiocb)
  8349. {
  8350. struct lpfc_vport *vport = cmdiocb->vport;
  8351. struct lpfc_iocbq *abtsiocbp;
  8352. IOCB_t *icmd = NULL;
  8353. IOCB_t *iabt = NULL;
  8354. int retval;
  8355. /*
  8356. * There are certain command types we don't want to abort. And we
  8357. * don't want to abort commands that are already in the process of
  8358. * being aborted.
  8359. */
  8360. icmd = &cmdiocb->iocb;
  8361. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8362. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8363. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8364. return 0;
  8365. /* issue ABTS for this IOCB based on iotag */
  8366. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8367. if (abtsiocbp == NULL)
  8368. return 0;
  8369. /* This signals the response to set the correct status
  8370. * before calling the completion handler
  8371. */
  8372. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8373. iabt = &abtsiocbp->iocb;
  8374. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8375. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8376. if (phba->sli_rev == LPFC_SLI_REV4) {
  8377. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8378. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8379. }
  8380. else
  8381. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8382. iabt->ulpLe = 1;
  8383. iabt->ulpClass = icmd->ulpClass;
  8384. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8385. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8386. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8387. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8388. if (phba->link_state >= LPFC_LINK_UP)
  8389. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8390. else
  8391. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8392. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8393. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8394. "0339 Abort xri x%x, original iotag x%x, "
  8395. "abort cmd iotag x%x\n",
  8396. iabt->un.acxri.abortIoTag,
  8397. iabt->un.acxri.abortContextTag,
  8398. abtsiocbp->iotag);
  8399. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8400. if (retval)
  8401. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8402. /*
  8403. * Caller to this routine should check for IOCB_ERROR
  8404. * and handle it properly. This routine no longer removes
  8405. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8406. */
  8407. return retval;
  8408. }
  8409. /**
  8410. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8411. * @phba: Pointer to HBA context object.
  8412. * @pring: Pointer to driver SLI ring object.
  8413. * @cmdiocb: Pointer to driver command iocb object.
  8414. *
  8415. * This function issues an abort iocb for the provided command iocb. In case
  8416. * of unloading, the abort iocb will not be issued to commands on the ELS
  8417. * ring. Instead, the callback function shall be changed to those commands
  8418. * so that nothing happens when them finishes. This function is called with
  8419. * hbalock held. The function returns 0 when the command iocb is an abort
  8420. * request.
  8421. **/
  8422. int
  8423. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8424. struct lpfc_iocbq *cmdiocb)
  8425. {
  8426. struct lpfc_vport *vport = cmdiocb->vport;
  8427. int retval = IOCB_ERROR;
  8428. IOCB_t *icmd = NULL;
  8429. /*
  8430. * There are certain command types we don't want to abort. And we
  8431. * don't want to abort commands that are already in the process of
  8432. * being aborted.
  8433. */
  8434. icmd = &cmdiocb->iocb;
  8435. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8436. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8437. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8438. return 0;
  8439. /*
  8440. * If we're unloading, don't abort iocb on the ELS ring, but change
  8441. * the callback so that nothing happens when it finishes.
  8442. */
  8443. if ((vport->load_flag & FC_UNLOADING) &&
  8444. (pring->ringno == LPFC_ELS_RING)) {
  8445. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8446. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8447. else
  8448. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8449. goto abort_iotag_exit;
  8450. }
  8451. /* Now, we try to issue the abort to the cmdiocb out */
  8452. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8453. abort_iotag_exit:
  8454. /*
  8455. * Caller to this routine should check for IOCB_ERROR
  8456. * and handle it properly. This routine no longer removes
  8457. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8458. */
  8459. return retval;
  8460. }
  8461. /**
  8462. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8463. * @phba: Pointer to HBA context object.
  8464. * @pring: Pointer to driver SLI ring object.
  8465. *
  8466. * This function aborts all iocbs in the given ring and frees all the iocb
  8467. * objects in txq. This function issues abort iocbs unconditionally for all
  8468. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8469. * to complete before the return of this function. The caller is not required
  8470. * to hold any locks.
  8471. **/
  8472. static void
  8473. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8474. {
  8475. LIST_HEAD(completions);
  8476. struct lpfc_iocbq *iocb, *next_iocb;
  8477. if (pring->ringno == LPFC_ELS_RING)
  8478. lpfc_fabric_abort_hba(phba);
  8479. spin_lock_irq(&phba->hbalock);
  8480. /* Take off all the iocbs on txq for cancelling */
  8481. list_splice_init(&pring->txq, &completions);
  8482. pring->txq_cnt = 0;
  8483. /* Next issue ABTS for everything on the txcmplq */
  8484. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8485. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8486. spin_unlock_irq(&phba->hbalock);
  8487. /* Cancel all the IOCBs from the completions list */
  8488. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8489. IOERR_SLI_ABORTED);
  8490. }
  8491. /**
  8492. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8493. * @phba: pointer to lpfc HBA data structure.
  8494. *
  8495. * This routine will abort all pending and outstanding iocbs to an HBA.
  8496. **/
  8497. void
  8498. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8499. {
  8500. struct lpfc_sli *psli = &phba->sli;
  8501. struct lpfc_sli_ring *pring;
  8502. int i;
  8503. for (i = 0; i < psli->num_rings; i++) {
  8504. pring = &psli->ring[i];
  8505. lpfc_sli_iocb_ring_abort(phba, pring);
  8506. }
  8507. }
  8508. /**
  8509. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8510. * @iocbq: Pointer to driver iocb object.
  8511. * @vport: Pointer to driver virtual port object.
  8512. * @tgt_id: SCSI ID of the target.
  8513. * @lun_id: LUN ID of the scsi device.
  8514. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8515. *
  8516. * This function acts as an iocb filter for functions which abort or count
  8517. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8518. * 0 if the filtering criteria is met for the given iocb and will return
  8519. * 1 if the filtering criteria is not met.
  8520. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8521. * given iocb is for the SCSI device specified by vport, tgt_id and
  8522. * lun_id parameter.
  8523. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8524. * given iocb is for the SCSI target specified by vport and tgt_id
  8525. * parameters.
  8526. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8527. * given iocb is for the SCSI host associated with the given vport.
  8528. * This function is called with no locks held.
  8529. **/
  8530. static int
  8531. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8532. uint16_t tgt_id, uint64_t lun_id,
  8533. lpfc_ctx_cmd ctx_cmd)
  8534. {
  8535. struct lpfc_scsi_buf *lpfc_cmd;
  8536. int rc = 1;
  8537. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8538. return rc;
  8539. if (iocbq->vport != vport)
  8540. return rc;
  8541. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8542. if (lpfc_cmd->pCmd == NULL)
  8543. return rc;
  8544. switch (ctx_cmd) {
  8545. case LPFC_CTX_LUN:
  8546. if ((lpfc_cmd->rdata->pnode) &&
  8547. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8548. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8549. rc = 0;
  8550. break;
  8551. case LPFC_CTX_TGT:
  8552. if ((lpfc_cmd->rdata->pnode) &&
  8553. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8554. rc = 0;
  8555. break;
  8556. case LPFC_CTX_HOST:
  8557. rc = 0;
  8558. break;
  8559. default:
  8560. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8561. __func__, ctx_cmd);
  8562. break;
  8563. }
  8564. return rc;
  8565. }
  8566. /**
  8567. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8568. * @vport: Pointer to virtual port.
  8569. * @tgt_id: SCSI ID of the target.
  8570. * @lun_id: LUN ID of the scsi device.
  8571. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8572. *
  8573. * This function returns number of FCP commands pending for the vport.
  8574. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8575. * commands pending on the vport associated with SCSI device specified
  8576. * by tgt_id and lun_id parameters.
  8577. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8578. * commands pending on the vport associated with SCSI target specified
  8579. * by tgt_id parameter.
  8580. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8581. * commands pending on the vport.
  8582. * This function returns the number of iocbs which satisfy the filter.
  8583. * This function is called without any lock held.
  8584. **/
  8585. int
  8586. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8587. lpfc_ctx_cmd ctx_cmd)
  8588. {
  8589. struct lpfc_hba *phba = vport->phba;
  8590. struct lpfc_iocbq *iocbq;
  8591. int sum, i;
  8592. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8593. iocbq = phba->sli.iocbq_lookup[i];
  8594. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8595. ctx_cmd) == 0)
  8596. sum++;
  8597. }
  8598. return sum;
  8599. }
  8600. /**
  8601. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8602. * @phba: Pointer to HBA context object
  8603. * @cmdiocb: Pointer to command iocb object.
  8604. * @rspiocb: Pointer to response iocb object.
  8605. *
  8606. * This function is called when an aborted FCP iocb completes. This
  8607. * function is called by the ring event handler with no lock held.
  8608. * This function frees the iocb.
  8609. **/
  8610. void
  8611. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8612. struct lpfc_iocbq *rspiocb)
  8613. {
  8614. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8615. "3096 ABORT_XRI_CN completing on xri x%x "
  8616. "original iotag x%x, abort cmd iotag x%x "
  8617. "status 0x%x, reason 0x%x\n",
  8618. cmdiocb->iocb.un.acxri.abortContextTag,
  8619. cmdiocb->iocb.un.acxri.abortIoTag,
  8620. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8621. rspiocb->iocb.un.ulpWord[4]);
  8622. lpfc_sli_release_iocbq(phba, cmdiocb);
  8623. return;
  8624. }
  8625. /**
  8626. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8627. * @vport: Pointer to virtual port.
  8628. * @pring: Pointer to driver SLI ring object.
  8629. * @tgt_id: SCSI ID of the target.
  8630. * @lun_id: LUN ID of the scsi device.
  8631. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8632. *
  8633. * This function sends an abort command for every SCSI command
  8634. * associated with the given virtual port pending on the ring
  8635. * filtered by lpfc_sli_validate_fcp_iocb function.
  8636. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8637. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8638. * parameters
  8639. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8640. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8641. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8642. * FCP iocbs associated with virtual port.
  8643. * This function returns number of iocbs it failed to abort.
  8644. * This function is called with no locks held.
  8645. **/
  8646. int
  8647. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8648. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8649. {
  8650. struct lpfc_hba *phba = vport->phba;
  8651. struct lpfc_iocbq *iocbq;
  8652. struct lpfc_iocbq *abtsiocb;
  8653. IOCB_t *cmd = NULL;
  8654. int errcnt = 0, ret_val = 0;
  8655. int i;
  8656. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8657. iocbq = phba->sli.iocbq_lookup[i];
  8658. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8659. abort_cmd) != 0)
  8660. continue;
  8661. /* issue ABTS for this IOCB based on iotag */
  8662. abtsiocb = lpfc_sli_get_iocbq(phba);
  8663. if (abtsiocb == NULL) {
  8664. errcnt++;
  8665. continue;
  8666. }
  8667. cmd = &iocbq->iocb;
  8668. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8669. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8670. if (phba->sli_rev == LPFC_SLI_REV4)
  8671. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8672. else
  8673. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8674. abtsiocb->iocb.ulpLe = 1;
  8675. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8676. abtsiocb->vport = phba->pport;
  8677. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8678. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8679. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8680. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8681. if (lpfc_is_link_up(phba))
  8682. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8683. else
  8684. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8685. /* Setup callback routine and issue the command. */
  8686. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8687. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8688. abtsiocb, 0);
  8689. if (ret_val == IOCB_ERROR) {
  8690. lpfc_sli_release_iocbq(phba, abtsiocb);
  8691. errcnt++;
  8692. continue;
  8693. }
  8694. }
  8695. return errcnt;
  8696. }
  8697. /**
  8698. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8699. * @phba: Pointer to HBA context object.
  8700. * @cmdiocbq: Pointer to command iocb.
  8701. * @rspiocbq: Pointer to response iocb.
  8702. *
  8703. * This function is the completion handler for iocbs issued using
  8704. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8705. * ring event handler function without any lock held. This function
  8706. * can be called from both worker thread context and interrupt
  8707. * context. This function also can be called from other thread which
  8708. * cleans up the SLI layer objects.
  8709. * This function copy the contents of the response iocb to the
  8710. * response iocb memory object provided by the caller of
  8711. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8712. * sleeps for the iocb completion.
  8713. **/
  8714. static void
  8715. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8716. struct lpfc_iocbq *cmdiocbq,
  8717. struct lpfc_iocbq *rspiocbq)
  8718. {
  8719. wait_queue_head_t *pdone_q;
  8720. unsigned long iflags;
  8721. struct lpfc_scsi_buf *lpfc_cmd;
  8722. spin_lock_irqsave(&phba->hbalock, iflags);
  8723. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8724. if (cmdiocbq->context2 && rspiocbq)
  8725. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8726. &rspiocbq->iocb, sizeof(IOCB_t));
  8727. /* Set the exchange busy flag for task management commands */
  8728. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8729. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8730. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8731. cur_iocbq);
  8732. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8733. }
  8734. pdone_q = cmdiocbq->context_un.wait_queue;
  8735. if (pdone_q)
  8736. wake_up(pdone_q);
  8737. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8738. return;
  8739. }
  8740. /**
  8741. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8742. * @phba: Pointer to HBA context object..
  8743. * @piocbq: Pointer to command iocb.
  8744. * @flag: Flag to test.
  8745. *
  8746. * This routine grabs the hbalock and then test the iocb_flag to
  8747. * see if the passed in flag is set.
  8748. * Returns:
  8749. * 1 if flag is set.
  8750. * 0 if flag is not set.
  8751. **/
  8752. static int
  8753. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8754. struct lpfc_iocbq *piocbq, uint32_t flag)
  8755. {
  8756. unsigned long iflags;
  8757. int ret;
  8758. spin_lock_irqsave(&phba->hbalock, iflags);
  8759. ret = piocbq->iocb_flag & flag;
  8760. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8761. return ret;
  8762. }
  8763. /**
  8764. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8765. * @phba: Pointer to HBA context object..
  8766. * @pring: Pointer to sli ring.
  8767. * @piocb: Pointer to command iocb.
  8768. * @prspiocbq: Pointer to response iocb.
  8769. * @timeout: Timeout in number of seconds.
  8770. *
  8771. * This function issues the iocb to firmware and waits for the
  8772. * iocb to complete. If the iocb command is not
  8773. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8774. * Caller should not free the iocb resources if this function
  8775. * returns IOCB_TIMEDOUT.
  8776. * The function waits for the iocb completion using an
  8777. * non-interruptible wait.
  8778. * This function will sleep while waiting for iocb completion.
  8779. * So, this function should not be called from any context which
  8780. * does not allow sleeping. Due to the same reason, this function
  8781. * cannot be called with interrupt disabled.
  8782. * This function assumes that the iocb completions occur while
  8783. * this function sleep. So, this function cannot be called from
  8784. * the thread which process iocb completion for this ring.
  8785. * This function clears the iocb_flag of the iocb object before
  8786. * issuing the iocb and the iocb completion handler sets this
  8787. * flag and wakes this thread when the iocb completes.
  8788. * The contents of the response iocb will be copied to prspiocbq
  8789. * by the completion handler when the command completes.
  8790. * This function returns IOCB_SUCCESS when success.
  8791. * This function is called with no lock held.
  8792. **/
  8793. int
  8794. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8795. uint32_t ring_number,
  8796. struct lpfc_iocbq *piocb,
  8797. struct lpfc_iocbq *prspiocbq,
  8798. uint32_t timeout)
  8799. {
  8800. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8801. long timeleft, timeout_req = 0;
  8802. int retval = IOCB_SUCCESS;
  8803. uint32_t creg_val;
  8804. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8805. /*
  8806. * If the caller has provided a response iocbq buffer, then context2
  8807. * is NULL or its an error.
  8808. */
  8809. if (prspiocbq) {
  8810. if (piocb->context2)
  8811. return IOCB_ERROR;
  8812. piocb->context2 = prspiocbq;
  8813. }
  8814. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8815. piocb->context_un.wait_queue = &done_q;
  8816. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8817. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8818. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8819. return IOCB_ERROR;
  8820. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8821. writel(creg_val, phba->HCregaddr);
  8822. readl(phba->HCregaddr); /* flush */
  8823. }
  8824. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8825. SLI_IOCB_RET_IOCB);
  8826. if (retval == IOCB_SUCCESS) {
  8827. timeout_req = timeout * HZ;
  8828. timeleft = wait_event_timeout(done_q,
  8829. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8830. timeout_req);
  8831. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8832. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8833. "0331 IOCB wake signaled\n");
  8834. } else if (timeleft == 0) {
  8835. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8836. "0338 IOCB wait timeout error - no "
  8837. "wake response Data x%x\n", timeout);
  8838. retval = IOCB_TIMEDOUT;
  8839. } else {
  8840. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8841. "0330 IOCB wake NOT set, "
  8842. "Data x%x x%lx\n",
  8843. timeout, (timeleft / jiffies));
  8844. retval = IOCB_TIMEDOUT;
  8845. }
  8846. } else if (retval == IOCB_BUSY) {
  8847. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8848. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8849. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8850. return retval;
  8851. } else {
  8852. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8853. "0332 IOCB wait issue failed, Data x%x\n",
  8854. retval);
  8855. retval = IOCB_ERROR;
  8856. }
  8857. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8858. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8859. return IOCB_ERROR;
  8860. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8861. writel(creg_val, phba->HCregaddr);
  8862. readl(phba->HCregaddr); /* flush */
  8863. }
  8864. if (prspiocbq)
  8865. piocb->context2 = NULL;
  8866. piocb->context_un.wait_queue = NULL;
  8867. piocb->iocb_cmpl = NULL;
  8868. return retval;
  8869. }
  8870. /**
  8871. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8872. * @phba: Pointer to HBA context object.
  8873. * @pmboxq: Pointer to driver mailbox object.
  8874. * @timeout: Timeout in number of seconds.
  8875. *
  8876. * This function issues the mailbox to firmware and waits for the
  8877. * mailbox command to complete. If the mailbox command is not
  8878. * completed within timeout seconds, it returns MBX_TIMEOUT.
  8879. * The function waits for the mailbox completion using an
  8880. * interruptible wait. If the thread is woken up due to a
  8881. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  8882. * should not free the mailbox resources, if this function returns
  8883. * MBX_TIMEOUT.
  8884. * This function will sleep while waiting for mailbox completion.
  8885. * So, this function should not be called from any context which
  8886. * does not allow sleeping. Due to the same reason, this function
  8887. * cannot be called with interrupt disabled.
  8888. * This function assumes that the mailbox completion occurs while
  8889. * this function sleep. So, this function cannot be called from
  8890. * the worker thread which processes mailbox completion.
  8891. * This function is called in the context of HBA management
  8892. * applications.
  8893. * This function returns MBX_SUCCESS when successful.
  8894. * This function is called with no lock held.
  8895. **/
  8896. int
  8897. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  8898. uint32_t timeout)
  8899. {
  8900. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8901. int retval;
  8902. unsigned long flag;
  8903. /* The caller must leave context1 empty. */
  8904. if (pmboxq->context1)
  8905. return MBX_NOT_FINISHED;
  8906. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  8907. /* setup wake call as IOCB callback */
  8908. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  8909. /* setup context field to pass wait_queue pointer to wake function */
  8910. pmboxq->context1 = &done_q;
  8911. /* now issue the command */
  8912. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  8913. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  8914. wait_event_interruptible_timeout(done_q,
  8915. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  8916. timeout * HZ);
  8917. spin_lock_irqsave(&phba->hbalock, flag);
  8918. pmboxq->context1 = NULL;
  8919. /*
  8920. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  8921. * else do not free the resources.
  8922. */
  8923. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  8924. retval = MBX_SUCCESS;
  8925. lpfc_sli4_swap_str(phba, pmboxq);
  8926. } else {
  8927. retval = MBX_TIMEOUT;
  8928. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8929. }
  8930. spin_unlock_irqrestore(&phba->hbalock, flag);
  8931. }
  8932. return retval;
  8933. }
  8934. /**
  8935. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  8936. * @phba: Pointer to HBA context.
  8937. *
  8938. * This function is called to shutdown the driver's mailbox sub-system.
  8939. * It first marks the mailbox sub-system is in a block state to prevent
  8940. * the asynchronous mailbox command from issued off the pending mailbox
  8941. * command queue. If the mailbox command sub-system shutdown is due to
  8942. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  8943. * the mailbox sub-system flush routine to forcefully bring down the
  8944. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  8945. * as with offline or HBA function reset), this routine will wait for the
  8946. * outstanding mailbox command to complete before invoking the mailbox
  8947. * sub-system flush routine to gracefully bring down mailbox sub-system.
  8948. **/
  8949. void
  8950. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  8951. {
  8952. struct lpfc_sli *psli = &phba->sli;
  8953. unsigned long timeout;
  8954. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  8955. spin_lock_irq(&phba->hbalock);
  8956. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  8957. spin_unlock_irq(&phba->hbalock);
  8958. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  8959. spin_lock_irq(&phba->hbalock);
  8960. /* Determine how long we might wait for the active mailbox
  8961. * command to be gracefully completed by firmware.
  8962. */
  8963. if (phba->sli.mbox_active)
  8964. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  8965. phba->sli.mbox_active) *
  8966. 1000) + jiffies;
  8967. spin_unlock_irq(&phba->hbalock);
  8968. while (phba->sli.mbox_active) {
  8969. /* Check active mailbox complete status every 2ms */
  8970. msleep(2);
  8971. if (time_after(jiffies, timeout))
  8972. /* Timeout, let the mailbox flush routine to
  8973. * forcefully release active mailbox command
  8974. */
  8975. break;
  8976. }
  8977. }
  8978. lpfc_sli_mbox_sys_flush(phba);
  8979. }
  8980. /**
  8981. * lpfc_sli_eratt_read - read sli-3 error attention events
  8982. * @phba: Pointer to HBA context.
  8983. *
  8984. * This function is called to read the SLI3 device error attention registers
  8985. * for possible error attention events. The caller must hold the hostlock
  8986. * with spin_lock_irq().
  8987. *
  8988. * This function returns 1 when there is Error Attention in the Host Attention
  8989. * Register and returns 0 otherwise.
  8990. **/
  8991. static int
  8992. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  8993. {
  8994. uint32_t ha_copy;
  8995. /* Read chip Host Attention (HA) register */
  8996. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  8997. goto unplug_err;
  8998. if (ha_copy & HA_ERATT) {
  8999. /* Read host status register to retrieve error event */
  9000. if (lpfc_sli_read_hs(phba))
  9001. goto unplug_err;
  9002. /* Check if there is a deferred error condition is active */
  9003. if ((HS_FFER1 & phba->work_hs) &&
  9004. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9005. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9006. phba->hba_flag |= DEFER_ERATT;
  9007. /* Clear all interrupt enable conditions */
  9008. writel(0, phba->HCregaddr);
  9009. readl(phba->HCregaddr);
  9010. }
  9011. /* Set the driver HA work bitmap */
  9012. phba->work_ha |= HA_ERATT;
  9013. /* Indicate polling handles this ERATT */
  9014. phba->hba_flag |= HBA_ERATT_HANDLED;
  9015. return 1;
  9016. }
  9017. return 0;
  9018. unplug_err:
  9019. /* Set the driver HS work bitmap */
  9020. phba->work_hs |= UNPLUG_ERR;
  9021. /* Set the driver HA work bitmap */
  9022. phba->work_ha |= HA_ERATT;
  9023. /* Indicate polling handles this ERATT */
  9024. phba->hba_flag |= HBA_ERATT_HANDLED;
  9025. return 1;
  9026. }
  9027. /**
  9028. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9029. * @phba: Pointer to HBA context.
  9030. *
  9031. * This function is called to read the SLI4 device error attention registers
  9032. * for possible error attention events. The caller must hold the hostlock
  9033. * with spin_lock_irq().
  9034. *
  9035. * This function returns 1 when there is Error Attention in the Host Attention
  9036. * Register and returns 0 otherwise.
  9037. **/
  9038. static int
  9039. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9040. {
  9041. uint32_t uerr_sta_hi, uerr_sta_lo;
  9042. uint32_t if_type, portsmphr;
  9043. struct lpfc_register portstat_reg;
  9044. /*
  9045. * For now, use the SLI4 device internal unrecoverable error
  9046. * registers for error attention. This can be changed later.
  9047. */
  9048. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9049. switch (if_type) {
  9050. case LPFC_SLI_INTF_IF_TYPE_0:
  9051. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9052. &uerr_sta_lo) ||
  9053. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9054. &uerr_sta_hi)) {
  9055. phba->work_hs |= UNPLUG_ERR;
  9056. phba->work_ha |= HA_ERATT;
  9057. phba->hba_flag |= HBA_ERATT_HANDLED;
  9058. return 1;
  9059. }
  9060. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9061. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9062. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9063. "1423 HBA Unrecoverable error: "
  9064. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9065. "ue_mask_lo_reg=0x%x, "
  9066. "ue_mask_hi_reg=0x%x\n",
  9067. uerr_sta_lo, uerr_sta_hi,
  9068. phba->sli4_hba.ue_mask_lo,
  9069. phba->sli4_hba.ue_mask_hi);
  9070. phba->work_status[0] = uerr_sta_lo;
  9071. phba->work_status[1] = uerr_sta_hi;
  9072. phba->work_ha |= HA_ERATT;
  9073. phba->hba_flag |= HBA_ERATT_HANDLED;
  9074. return 1;
  9075. }
  9076. break;
  9077. case LPFC_SLI_INTF_IF_TYPE_2:
  9078. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9079. &portstat_reg.word0) ||
  9080. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9081. &portsmphr)){
  9082. phba->work_hs |= UNPLUG_ERR;
  9083. phba->work_ha |= HA_ERATT;
  9084. phba->hba_flag |= HBA_ERATT_HANDLED;
  9085. return 1;
  9086. }
  9087. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9088. phba->work_status[0] =
  9089. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9090. phba->work_status[1] =
  9091. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9092. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9093. "2885 Port Status Event: "
  9094. "port status reg 0x%x, "
  9095. "port smphr reg 0x%x, "
  9096. "error 1=0x%x, error 2=0x%x\n",
  9097. portstat_reg.word0,
  9098. portsmphr,
  9099. phba->work_status[0],
  9100. phba->work_status[1]);
  9101. phba->work_ha |= HA_ERATT;
  9102. phba->hba_flag |= HBA_ERATT_HANDLED;
  9103. return 1;
  9104. }
  9105. break;
  9106. case LPFC_SLI_INTF_IF_TYPE_1:
  9107. default:
  9108. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9109. "2886 HBA Error Attention on unsupported "
  9110. "if type %d.", if_type);
  9111. return 1;
  9112. }
  9113. return 0;
  9114. }
  9115. /**
  9116. * lpfc_sli_check_eratt - check error attention events
  9117. * @phba: Pointer to HBA context.
  9118. *
  9119. * This function is called from timer soft interrupt context to check HBA's
  9120. * error attention register bit for error attention events.
  9121. *
  9122. * This function returns 1 when there is Error Attention in the Host Attention
  9123. * Register and returns 0 otherwise.
  9124. **/
  9125. int
  9126. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9127. {
  9128. uint32_t ha_copy;
  9129. /* If somebody is waiting to handle an eratt, don't process it
  9130. * here. The brdkill function will do this.
  9131. */
  9132. if (phba->link_flag & LS_IGNORE_ERATT)
  9133. return 0;
  9134. /* Check if interrupt handler handles this ERATT */
  9135. spin_lock_irq(&phba->hbalock);
  9136. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9137. /* Interrupt handler has handled ERATT */
  9138. spin_unlock_irq(&phba->hbalock);
  9139. return 0;
  9140. }
  9141. /*
  9142. * If there is deferred error attention, do not check for error
  9143. * attention
  9144. */
  9145. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9146. spin_unlock_irq(&phba->hbalock);
  9147. return 0;
  9148. }
  9149. /* If PCI channel is offline, don't process it */
  9150. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9151. spin_unlock_irq(&phba->hbalock);
  9152. return 0;
  9153. }
  9154. switch (phba->sli_rev) {
  9155. case LPFC_SLI_REV2:
  9156. case LPFC_SLI_REV3:
  9157. /* Read chip Host Attention (HA) register */
  9158. ha_copy = lpfc_sli_eratt_read(phba);
  9159. break;
  9160. case LPFC_SLI_REV4:
  9161. /* Read device Uncoverable Error (UERR) registers */
  9162. ha_copy = lpfc_sli4_eratt_read(phba);
  9163. break;
  9164. default:
  9165. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9166. "0299 Invalid SLI revision (%d)\n",
  9167. phba->sli_rev);
  9168. ha_copy = 0;
  9169. break;
  9170. }
  9171. spin_unlock_irq(&phba->hbalock);
  9172. return ha_copy;
  9173. }
  9174. /**
  9175. * lpfc_intr_state_check - Check device state for interrupt handling
  9176. * @phba: Pointer to HBA context.
  9177. *
  9178. * This inline routine checks whether a device or its PCI slot is in a state
  9179. * that the interrupt should be handled.
  9180. *
  9181. * This function returns 0 if the device or the PCI slot is in a state that
  9182. * interrupt should be handled, otherwise -EIO.
  9183. */
  9184. static inline int
  9185. lpfc_intr_state_check(struct lpfc_hba *phba)
  9186. {
  9187. /* If the pci channel is offline, ignore all the interrupts */
  9188. if (unlikely(pci_channel_offline(phba->pcidev)))
  9189. return -EIO;
  9190. /* Update device level interrupt statistics */
  9191. phba->sli.slistat.sli_intr++;
  9192. /* Ignore all interrupts during initialization. */
  9193. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9194. return -EIO;
  9195. return 0;
  9196. }
  9197. /**
  9198. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9199. * @irq: Interrupt number.
  9200. * @dev_id: The device context pointer.
  9201. *
  9202. * This function is directly called from the PCI layer as an interrupt
  9203. * service routine when device with SLI-3 interface spec is enabled with
  9204. * MSI-X multi-message interrupt mode and there are slow-path events in
  9205. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9206. * interrupt mode, this function is called as part of the device-level
  9207. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9208. * is undergoing initialization, the interrupt handler will not process
  9209. * the interrupt. The link attention and ELS ring attention events are
  9210. * handled by the worker thread. The interrupt handler signals the worker
  9211. * thread and returns for these events. This function is called without
  9212. * any lock held. It gets the hbalock to access and update SLI data
  9213. * structures.
  9214. *
  9215. * This function returns IRQ_HANDLED when interrupt is handled else it
  9216. * returns IRQ_NONE.
  9217. **/
  9218. irqreturn_t
  9219. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9220. {
  9221. struct lpfc_hba *phba;
  9222. uint32_t ha_copy, hc_copy;
  9223. uint32_t work_ha_copy;
  9224. unsigned long status;
  9225. unsigned long iflag;
  9226. uint32_t control;
  9227. MAILBOX_t *mbox, *pmbox;
  9228. struct lpfc_vport *vport;
  9229. struct lpfc_nodelist *ndlp;
  9230. struct lpfc_dmabuf *mp;
  9231. LPFC_MBOXQ_t *pmb;
  9232. int rc;
  9233. /*
  9234. * Get the driver's phba structure from the dev_id and
  9235. * assume the HBA is not interrupting.
  9236. */
  9237. phba = (struct lpfc_hba *)dev_id;
  9238. if (unlikely(!phba))
  9239. return IRQ_NONE;
  9240. /*
  9241. * Stuff needs to be attented to when this function is invoked as an
  9242. * individual interrupt handler in MSI-X multi-message interrupt mode
  9243. */
  9244. if (phba->intr_type == MSIX) {
  9245. /* Check device state for handling interrupt */
  9246. if (lpfc_intr_state_check(phba))
  9247. return IRQ_NONE;
  9248. /* Need to read HA REG for slow-path events */
  9249. spin_lock_irqsave(&phba->hbalock, iflag);
  9250. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9251. goto unplug_error;
  9252. /* If somebody is waiting to handle an eratt don't process it
  9253. * here. The brdkill function will do this.
  9254. */
  9255. if (phba->link_flag & LS_IGNORE_ERATT)
  9256. ha_copy &= ~HA_ERATT;
  9257. /* Check the need for handling ERATT in interrupt handler */
  9258. if (ha_copy & HA_ERATT) {
  9259. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9260. /* ERATT polling has handled ERATT */
  9261. ha_copy &= ~HA_ERATT;
  9262. else
  9263. /* Indicate interrupt handler handles ERATT */
  9264. phba->hba_flag |= HBA_ERATT_HANDLED;
  9265. }
  9266. /*
  9267. * If there is deferred error attention, do not check for any
  9268. * interrupt.
  9269. */
  9270. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9271. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9272. return IRQ_NONE;
  9273. }
  9274. /* Clear up only attention source related to slow-path */
  9275. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9276. goto unplug_error;
  9277. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9278. HC_LAINT_ENA | HC_ERINT_ENA),
  9279. phba->HCregaddr);
  9280. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9281. phba->HAregaddr);
  9282. writel(hc_copy, phba->HCregaddr);
  9283. readl(phba->HAregaddr); /* flush */
  9284. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9285. } else
  9286. ha_copy = phba->ha_copy;
  9287. work_ha_copy = ha_copy & phba->work_ha_mask;
  9288. if (work_ha_copy) {
  9289. if (work_ha_copy & HA_LATT) {
  9290. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9291. /*
  9292. * Turn off Link Attention interrupts
  9293. * until CLEAR_LA done
  9294. */
  9295. spin_lock_irqsave(&phba->hbalock, iflag);
  9296. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9297. if (lpfc_readl(phba->HCregaddr, &control))
  9298. goto unplug_error;
  9299. control &= ~HC_LAINT_ENA;
  9300. writel(control, phba->HCregaddr);
  9301. readl(phba->HCregaddr); /* flush */
  9302. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9303. }
  9304. else
  9305. work_ha_copy &= ~HA_LATT;
  9306. }
  9307. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9308. /*
  9309. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9310. * the only slow ring.
  9311. */
  9312. status = (work_ha_copy &
  9313. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9314. status >>= (4*LPFC_ELS_RING);
  9315. if (status & HA_RXMASK) {
  9316. spin_lock_irqsave(&phba->hbalock, iflag);
  9317. if (lpfc_readl(phba->HCregaddr, &control))
  9318. goto unplug_error;
  9319. lpfc_debugfs_slow_ring_trc(phba,
  9320. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9321. control, status,
  9322. (uint32_t)phba->sli.slistat.sli_intr);
  9323. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9324. lpfc_debugfs_slow_ring_trc(phba,
  9325. "ISR Disable ring:"
  9326. "pwork:x%x hawork:x%x wait:x%x",
  9327. phba->work_ha, work_ha_copy,
  9328. (uint32_t)((unsigned long)
  9329. &phba->work_waitq));
  9330. control &=
  9331. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9332. writel(control, phba->HCregaddr);
  9333. readl(phba->HCregaddr); /* flush */
  9334. }
  9335. else {
  9336. lpfc_debugfs_slow_ring_trc(phba,
  9337. "ISR slow ring: pwork:"
  9338. "x%x hawork:x%x wait:x%x",
  9339. phba->work_ha, work_ha_copy,
  9340. (uint32_t)((unsigned long)
  9341. &phba->work_waitq));
  9342. }
  9343. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9344. }
  9345. }
  9346. spin_lock_irqsave(&phba->hbalock, iflag);
  9347. if (work_ha_copy & HA_ERATT) {
  9348. if (lpfc_sli_read_hs(phba))
  9349. goto unplug_error;
  9350. /*
  9351. * Check if there is a deferred error condition
  9352. * is active
  9353. */
  9354. if ((HS_FFER1 & phba->work_hs) &&
  9355. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9356. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9357. phba->work_hs)) {
  9358. phba->hba_flag |= DEFER_ERATT;
  9359. /* Clear all interrupt enable conditions */
  9360. writel(0, phba->HCregaddr);
  9361. readl(phba->HCregaddr);
  9362. }
  9363. }
  9364. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9365. pmb = phba->sli.mbox_active;
  9366. pmbox = &pmb->u.mb;
  9367. mbox = phba->mbox;
  9368. vport = pmb->vport;
  9369. /* First check out the status word */
  9370. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9371. if (pmbox->mbxOwner != OWN_HOST) {
  9372. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9373. /*
  9374. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9375. * mbxStatus <status>
  9376. */
  9377. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9378. LOG_SLI,
  9379. "(%d):0304 Stray Mailbox "
  9380. "Interrupt mbxCommand x%x "
  9381. "mbxStatus x%x\n",
  9382. (vport ? vport->vpi : 0),
  9383. pmbox->mbxCommand,
  9384. pmbox->mbxStatus);
  9385. /* clear mailbox attention bit */
  9386. work_ha_copy &= ~HA_MBATT;
  9387. } else {
  9388. phba->sli.mbox_active = NULL;
  9389. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9390. phba->last_completion_time = jiffies;
  9391. del_timer(&phba->sli.mbox_tmo);
  9392. if (pmb->mbox_cmpl) {
  9393. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9394. MAILBOX_CMD_SIZE);
  9395. if (pmb->out_ext_byte_len &&
  9396. pmb->context2)
  9397. lpfc_sli_pcimem_bcopy(
  9398. phba->mbox_ext,
  9399. pmb->context2,
  9400. pmb->out_ext_byte_len);
  9401. }
  9402. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9403. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9404. lpfc_debugfs_disc_trc(vport,
  9405. LPFC_DISC_TRC_MBOX_VPORT,
  9406. "MBOX dflt rpi: : "
  9407. "status:x%x rpi:x%x",
  9408. (uint32_t)pmbox->mbxStatus,
  9409. pmbox->un.varWords[0], 0);
  9410. if (!pmbox->mbxStatus) {
  9411. mp = (struct lpfc_dmabuf *)
  9412. (pmb->context1);
  9413. ndlp = (struct lpfc_nodelist *)
  9414. pmb->context2;
  9415. /* Reg_LOGIN of dflt RPI was
  9416. * successful. new lets get
  9417. * rid of the RPI using the
  9418. * same mbox buffer.
  9419. */
  9420. lpfc_unreg_login(phba,
  9421. vport->vpi,
  9422. pmbox->un.varWords[0],
  9423. pmb);
  9424. pmb->mbox_cmpl =
  9425. lpfc_mbx_cmpl_dflt_rpi;
  9426. pmb->context1 = mp;
  9427. pmb->context2 = ndlp;
  9428. pmb->vport = vport;
  9429. rc = lpfc_sli_issue_mbox(phba,
  9430. pmb,
  9431. MBX_NOWAIT);
  9432. if (rc != MBX_BUSY)
  9433. lpfc_printf_log(phba,
  9434. KERN_ERR,
  9435. LOG_MBOX | LOG_SLI,
  9436. "0350 rc should have"
  9437. "been MBX_BUSY\n");
  9438. if (rc != MBX_NOT_FINISHED)
  9439. goto send_current_mbox;
  9440. }
  9441. }
  9442. spin_lock_irqsave(
  9443. &phba->pport->work_port_lock,
  9444. iflag);
  9445. phba->pport->work_port_events &=
  9446. ~WORKER_MBOX_TMO;
  9447. spin_unlock_irqrestore(
  9448. &phba->pport->work_port_lock,
  9449. iflag);
  9450. lpfc_mbox_cmpl_put(phba, pmb);
  9451. }
  9452. } else
  9453. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9454. if ((work_ha_copy & HA_MBATT) &&
  9455. (phba->sli.mbox_active == NULL)) {
  9456. send_current_mbox:
  9457. /* Process next mailbox command if there is one */
  9458. do {
  9459. rc = lpfc_sli_issue_mbox(phba, NULL,
  9460. MBX_NOWAIT);
  9461. } while (rc == MBX_NOT_FINISHED);
  9462. if (rc != MBX_SUCCESS)
  9463. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9464. LOG_SLI, "0349 rc should be "
  9465. "MBX_SUCCESS\n");
  9466. }
  9467. spin_lock_irqsave(&phba->hbalock, iflag);
  9468. phba->work_ha |= work_ha_copy;
  9469. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9470. lpfc_worker_wake_up(phba);
  9471. }
  9472. return IRQ_HANDLED;
  9473. unplug_error:
  9474. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9475. return IRQ_HANDLED;
  9476. } /* lpfc_sli_sp_intr_handler */
  9477. /**
  9478. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9479. * @irq: Interrupt number.
  9480. * @dev_id: The device context pointer.
  9481. *
  9482. * This function is directly called from the PCI layer as an interrupt
  9483. * service routine when device with SLI-3 interface spec is enabled with
  9484. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9485. * ring event in the HBA. However, when the device is enabled with either
  9486. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9487. * device-level interrupt handler. When the PCI slot is in error recovery
  9488. * or the HBA is undergoing initialization, the interrupt handler will not
  9489. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9490. * the intrrupt context. This function is called without any lock held.
  9491. * It gets the hbalock to access and update SLI data structures.
  9492. *
  9493. * This function returns IRQ_HANDLED when interrupt is handled else it
  9494. * returns IRQ_NONE.
  9495. **/
  9496. irqreturn_t
  9497. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9498. {
  9499. struct lpfc_hba *phba;
  9500. uint32_t ha_copy;
  9501. unsigned long status;
  9502. unsigned long iflag;
  9503. /* Get the driver's phba structure from the dev_id and
  9504. * assume the HBA is not interrupting.
  9505. */
  9506. phba = (struct lpfc_hba *) dev_id;
  9507. if (unlikely(!phba))
  9508. return IRQ_NONE;
  9509. /*
  9510. * Stuff needs to be attented to when this function is invoked as an
  9511. * individual interrupt handler in MSI-X multi-message interrupt mode
  9512. */
  9513. if (phba->intr_type == MSIX) {
  9514. /* Check device state for handling interrupt */
  9515. if (lpfc_intr_state_check(phba))
  9516. return IRQ_NONE;
  9517. /* Need to read HA REG for FCP ring and other ring events */
  9518. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9519. return IRQ_HANDLED;
  9520. /* Clear up only attention source related to fast-path */
  9521. spin_lock_irqsave(&phba->hbalock, iflag);
  9522. /*
  9523. * If there is deferred error attention, do not check for
  9524. * any interrupt.
  9525. */
  9526. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9527. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9528. return IRQ_NONE;
  9529. }
  9530. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9531. phba->HAregaddr);
  9532. readl(phba->HAregaddr); /* flush */
  9533. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9534. } else
  9535. ha_copy = phba->ha_copy;
  9536. /*
  9537. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9538. */
  9539. ha_copy &= ~(phba->work_ha_mask);
  9540. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9541. status >>= (4*LPFC_FCP_RING);
  9542. if (status & HA_RXMASK)
  9543. lpfc_sli_handle_fast_ring_event(phba,
  9544. &phba->sli.ring[LPFC_FCP_RING],
  9545. status);
  9546. if (phba->cfg_multi_ring_support == 2) {
  9547. /*
  9548. * Process all events on extra ring. Take the optimized path
  9549. * for extra ring IO.
  9550. */
  9551. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9552. status >>= (4*LPFC_EXTRA_RING);
  9553. if (status & HA_RXMASK) {
  9554. lpfc_sli_handle_fast_ring_event(phba,
  9555. &phba->sli.ring[LPFC_EXTRA_RING],
  9556. status);
  9557. }
  9558. }
  9559. return IRQ_HANDLED;
  9560. } /* lpfc_sli_fp_intr_handler */
  9561. /**
  9562. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9563. * @irq: Interrupt number.
  9564. * @dev_id: The device context pointer.
  9565. *
  9566. * This function is the HBA device-level interrupt handler to device with
  9567. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9568. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9569. * requires driver attention. This function invokes the slow-path interrupt
  9570. * attention handling function and fast-path interrupt attention handling
  9571. * function in turn to process the relevant HBA attention events. This
  9572. * function is called without any lock held. It gets the hbalock to access
  9573. * and update SLI data structures.
  9574. *
  9575. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9576. * returns IRQ_NONE.
  9577. **/
  9578. irqreturn_t
  9579. lpfc_sli_intr_handler(int irq, void *dev_id)
  9580. {
  9581. struct lpfc_hba *phba;
  9582. irqreturn_t sp_irq_rc, fp_irq_rc;
  9583. unsigned long status1, status2;
  9584. uint32_t hc_copy;
  9585. /*
  9586. * Get the driver's phba structure from the dev_id and
  9587. * assume the HBA is not interrupting.
  9588. */
  9589. phba = (struct lpfc_hba *) dev_id;
  9590. if (unlikely(!phba))
  9591. return IRQ_NONE;
  9592. /* Check device state for handling interrupt */
  9593. if (lpfc_intr_state_check(phba))
  9594. return IRQ_NONE;
  9595. spin_lock(&phba->hbalock);
  9596. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9597. spin_unlock(&phba->hbalock);
  9598. return IRQ_HANDLED;
  9599. }
  9600. if (unlikely(!phba->ha_copy)) {
  9601. spin_unlock(&phba->hbalock);
  9602. return IRQ_NONE;
  9603. } else if (phba->ha_copy & HA_ERATT) {
  9604. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9605. /* ERATT polling has handled ERATT */
  9606. phba->ha_copy &= ~HA_ERATT;
  9607. else
  9608. /* Indicate interrupt handler handles ERATT */
  9609. phba->hba_flag |= HBA_ERATT_HANDLED;
  9610. }
  9611. /*
  9612. * If there is deferred error attention, do not check for any interrupt.
  9613. */
  9614. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9615. spin_unlock(&phba->hbalock);
  9616. return IRQ_NONE;
  9617. }
  9618. /* Clear attention sources except link and error attentions */
  9619. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9620. spin_unlock(&phba->hbalock);
  9621. return IRQ_HANDLED;
  9622. }
  9623. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9624. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9625. phba->HCregaddr);
  9626. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9627. writel(hc_copy, phba->HCregaddr);
  9628. readl(phba->HAregaddr); /* flush */
  9629. spin_unlock(&phba->hbalock);
  9630. /*
  9631. * Invokes slow-path host attention interrupt handling as appropriate.
  9632. */
  9633. /* status of events with mailbox and link attention */
  9634. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9635. /* status of events with ELS ring */
  9636. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9637. status2 >>= (4*LPFC_ELS_RING);
  9638. if (status1 || (status2 & HA_RXMASK))
  9639. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9640. else
  9641. sp_irq_rc = IRQ_NONE;
  9642. /*
  9643. * Invoke fast-path host attention interrupt handling as appropriate.
  9644. */
  9645. /* status of events with FCP ring */
  9646. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9647. status1 >>= (4*LPFC_FCP_RING);
  9648. /* status of events with extra ring */
  9649. if (phba->cfg_multi_ring_support == 2) {
  9650. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9651. status2 >>= (4*LPFC_EXTRA_RING);
  9652. } else
  9653. status2 = 0;
  9654. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9655. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9656. else
  9657. fp_irq_rc = IRQ_NONE;
  9658. /* Return device-level interrupt handling status */
  9659. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9660. } /* lpfc_sli_intr_handler */
  9661. /**
  9662. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9663. * @phba: pointer to lpfc hba data structure.
  9664. *
  9665. * This routine is invoked by the worker thread to process all the pending
  9666. * SLI4 FCP abort XRI events.
  9667. **/
  9668. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9669. {
  9670. struct lpfc_cq_event *cq_event;
  9671. /* First, declare the fcp xri abort event has been handled */
  9672. spin_lock_irq(&phba->hbalock);
  9673. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9674. spin_unlock_irq(&phba->hbalock);
  9675. /* Now, handle all the fcp xri abort events */
  9676. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9677. /* Get the first event from the head of the event queue */
  9678. spin_lock_irq(&phba->hbalock);
  9679. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9680. cq_event, struct lpfc_cq_event, list);
  9681. spin_unlock_irq(&phba->hbalock);
  9682. /* Notify aborted XRI for FCP work queue */
  9683. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9684. /* Free the event processed back to the free pool */
  9685. lpfc_sli4_cq_event_release(phba, cq_event);
  9686. }
  9687. }
  9688. /**
  9689. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9690. * @phba: pointer to lpfc hba data structure.
  9691. *
  9692. * This routine is invoked by the worker thread to process all the pending
  9693. * SLI4 els abort xri events.
  9694. **/
  9695. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9696. {
  9697. struct lpfc_cq_event *cq_event;
  9698. /* First, declare the els xri abort event has been handled */
  9699. spin_lock_irq(&phba->hbalock);
  9700. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9701. spin_unlock_irq(&phba->hbalock);
  9702. /* Now, handle all the els xri abort events */
  9703. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9704. /* Get the first event from the head of the event queue */
  9705. spin_lock_irq(&phba->hbalock);
  9706. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9707. cq_event, struct lpfc_cq_event, list);
  9708. spin_unlock_irq(&phba->hbalock);
  9709. /* Notify aborted XRI for ELS work queue */
  9710. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9711. /* Free the event processed back to the free pool */
  9712. lpfc_sli4_cq_event_release(phba, cq_event);
  9713. }
  9714. }
  9715. /**
  9716. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9717. * @phba: pointer to lpfc hba data structure
  9718. * @pIocbIn: pointer to the rspiocbq
  9719. * @pIocbOut: pointer to the cmdiocbq
  9720. * @wcqe: pointer to the complete wcqe
  9721. *
  9722. * This routine transfers the fields of a command iocbq to a response iocbq
  9723. * by copying all the IOCB fields from command iocbq and transferring the
  9724. * completion status information from the complete wcqe.
  9725. **/
  9726. static void
  9727. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9728. struct lpfc_iocbq *pIocbIn,
  9729. struct lpfc_iocbq *pIocbOut,
  9730. struct lpfc_wcqe_complete *wcqe)
  9731. {
  9732. unsigned long iflags;
  9733. uint32_t status;
  9734. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9735. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9736. sizeof(struct lpfc_iocbq) - offset);
  9737. /* Map WCQE parameters into irspiocb parameters */
  9738. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9739. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9740. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9741. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9742. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9743. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9744. wcqe->total_data_placed;
  9745. else
  9746. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9747. else {
  9748. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9749. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9750. }
  9751. /* Convert BG errors for completion status */
  9752. if (status == CQE_STATUS_DI_ERROR) {
  9753. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9754. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9755. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9756. else
  9757. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9758. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9759. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9760. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9761. BGS_GUARD_ERR_MASK;
  9762. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9763. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9764. BGS_APPTAG_ERR_MASK;
  9765. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9766. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9767. BGS_REFTAG_ERR_MASK;
  9768. /* Check to see if there was any good data before the error */
  9769. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9770. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9771. BGS_HI_WATER_MARK_PRESENT_MASK;
  9772. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9773. wcqe->total_data_placed;
  9774. }
  9775. /*
  9776. * Set ALL the error bits to indicate we don't know what
  9777. * type of error it is.
  9778. */
  9779. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9780. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9781. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9782. BGS_GUARD_ERR_MASK);
  9783. }
  9784. /* Pick up HBA exchange busy condition */
  9785. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9786. spin_lock_irqsave(&phba->hbalock, iflags);
  9787. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9788. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9789. }
  9790. }
  9791. /**
  9792. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9793. * @phba: Pointer to HBA context object.
  9794. * @wcqe: Pointer to work-queue completion queue entry.
  9795. *
  9796. * This routine handles an ELS work-queue completion event and construct
  9797. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9798. * discovery engine to handle.
  9799. *
  9800. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9801. **/
  9802. static struct lpfc_iocbq *
  9803. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9804. struct lpfc_iocbq *irspiocbq)
  9805. {
  9806. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9807. struct lpfc_iocbq *cmdiocbq;
  9808. struct lpfc_wcqe_complete *wcqe;
  9809. unsigned long iflags;
  9810. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9811. spin_lock_irqsave(&phba->hbalock, iflags);
  9812. pring->stats.iocb_event++;
  9813. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9814. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9815. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9816. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9817. if (unlikely(!cmdiocbq)) {
  9818. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9819. "0386 ELS complete with no corresponding "
  9820. "cmdiocb: iotag (%d)\n",
  9821. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9822. lpfc_sli_release_iocbq(phba, irspiocbq);
  9823. return NULL;
  9824. }
  9825. /* Fake the irspiocbq and copy necessary response information */
  9826. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9827. return irspiocbq;
  9828. }
  9829. /**
  9830. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9831. * @phba: Pointer to HBA context object.
  9832. * @cqe: Pointer to mailbox completion queue entry.
  9833. *
  9834. * This routine process a mailbox completion queue entry with asynchrous
  9835. * event.
  9836. *
  9837. * Return: true if work posted to worker thread, otherwise false.
  9838. **/
  9839. static bool
  9840. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9841. {
  9842. struct lpfc_cq_event *cq_event;
  9843. unsigned long iflags;
  9844. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9845. "0392 Async Event: word0:x%x, word1:x%x, "
  9846. "word2:x%x, word3:x%x\n", mcqe->word0,
  9847. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9848. /* Allocate a new internal CQ_EVENT entry */
  9849. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9850. if (!cq_event) {
  9851. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9852. "0394 Failed to allocate CQ_EVENT entry\n");
  9853. return false;
  9854. }
  9855. /* Move the CQE into an asynchronous event entry */
  9856. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9857. spin_lock_irqsave(&phba->hbalock, iflags);
  9858. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9859. /* Set the async event flag */
  9860. phba->hba_flag |= ASYNC_EVENT;
  9861. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9862. return true;
  9863. }
  9864. /**
  9865. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9866. * @phba: Pointer to HBA context object.
  9867. * @cqe: Pointer to mailbox completion queue entry.
  9868. *
  9869. * This routine process a mailbox completion queue entry with mailbox
  9870. * completion event.
  9871. *
  9872. * Return: true if work posted to worker thread, otherwise false.
  9873. **/
  9874. static bool
  9875. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9876. {
  9877. uint32_t mcqe_status;
  9878. MAILBOX_t *mbox, *pmbox;
  9879. struct lpfc_mqe *mqe;
  9880. struct lpfc_vport *vport;
  9881. struct lpfc_nodelist *ndlp;
  9882. struct lpfc_dmabuf *mp;
  9883. unsigned long iflags;
  9884. LPFC_MBOXQ_t *pmb;
  9885. bool workposted = false;
  9886. int rc;
  9887. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  9888. if (!bf_get(lpfc_trailer_completed, mcqe))
  9889. goto out_no_mqe_complete;
  9890. /* Get the reference to the active mbox command */
  9891. spin_lock_irqsave(&phba->hbalock, iflags);
  9892. pmb = phba->sli.mbox_active;
  9893. if (unlikely(!pmb)) {
  9894. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9895. "1832 No pending MBOX command to handle\n");
  9896. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9897. goto out_no_mqe_complete;
  9898. }
  9899. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9900. mqe = &pmb->u.mqe;
  9901. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  9902. mbox = phba->mbox;
  9903. vport = pmb->vport;
  9904. /* Reset heartbeat timer */
  9905. phba->last_completion_time = jiffies;
  9906. del_timer(&phba->sli.mbox_tmo);
  9907. /* Move mbox data to caller's mailbox region, do endian swapping */
  9908. if (pmb->mbox_cmpl && mbox)
  9909. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  9910. /*
  9911. * For mcqe errors, conditionally move a modified error code to
  9912. * the mbox so that the error will not be missed.
  9913. */
  9914. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  9915. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  9916. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  9917. bf_set(lpfc_mqe_status, mqe,
  9918. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  9919. }
  9920. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9921. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9922. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  9923. "MBOX dflt rpi: status:x%x rpi:x%x",
  9924. mcqe_status,
  9925. pmbox->un.varWords[0], 0);
  9926. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  9927. mp = (struct lpfc_dmabuf *)(pmb->context1);
  9928. ndlp = (struct lpfc_nodelist *)pmb->context2;
  9929. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  9930. * RID of the PPI using the same mbox buffer.
  9931. */
  9932. lpfc_unreg_login(phba, vport->vpi,
  9933. pmbox->un.varWords[0], pmb);
  9934. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  9935. pmb->context1 = mp;
  9936. pmb->context2 = ndlp;
  9937. pmb->vport = vport;
  9938. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  9939. if (rc != MBX_BUSY)
  9940. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9941. LOG_SLI, "0385 rc should "
  9942. "have been MBX_BUSY\n");
  9943. if (rc != MBX_NOT_FINISHED)
  9944. goto send_current_mbox;
  9945. }
  9946. }
  9947. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  9948. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  9949. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  9950. /* There is mailbox completion work to do */
  9951. spin_lock_irqsave(&phba->hbalock, iflags);
  9952. __lpfc_mbox_cmpl_put(phba, pmb);
  9953. phba->work_ha |= HA_MBATT;
  9954. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9955. workposted = true;
  9956. send_current_mbox:
  9957. spin_lock_irqsave(&phba->hbalock, iflags);
  9958. /* Release the mailbox command posting token */
  9959. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  9960. /* Setting active mailbox pointer need to be in sync to flag clear */
  9961. phba->sli.mbox_active = NULL;
  9962. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9963. /* Wake up worker thread to post the next pending mailbox command */
  9964. lpfc_worker_wake_up(phba);
  9965. out_no_mqe_complete:
  9966. if (bf_get(lpfc_trailer_consumed, mcqe))
  9967. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  9968. return workposted;
  9969. }
  9970. /**
  9971. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  9972. * @phba: Pointer to HBA context object.
  9973. * @cqe: Pointer to mailbox completion queue entry.
  9974. *
  9975. * This routine process a mailbox completion queue entry, it invokes the
  9976. * proper mailbox complete handling or asynchrous event handling routine
  9977. * according to the MCQE's async bit.
  9978. *
  9979. * Return: true if work posted to worker thread, otherwise false.
  9980. **/
  9981. static bool
  9982. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  9983. {
  9984. struct lpfc_mcqe mcqe;
  9985. bool workposted;
  9986. /* Copy the mailbox MCQE and convert endian order as needed */
  9987. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  9988. /* Invoke the proper event handling routine */
  9989. if (!bf_get(lpfc_trailer_async, &mcqe))
  9990. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  9991. else
  9992. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  9993. return workposted;
  9994. }
  9995. /**
  9996. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  9997. * @phba: Pointer to HBA context object.
  9998. * @wcqe: Pointer to work-queue completion queue entry.
  9999. *
  10000. * This routine handles an ELS work-queue completion event.
  10001. *
  10002. * Return: true if work posted to worker thread, otherwise false.
  10003. **/
  10004. static bool
  10005. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10006. struct lpfc_wcqe_complete *wcqe)
  10007. {
  10008. struct lpfc_iocbq *irspiocbq;
  10009. unsigned long iflags;
  10010. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10011. /* Get an irspiocbq for later ELS response processing use */
  10012. irspiocbq = lpfc_sli_get_iocbq(phba);
  10013. if (!irspiocbq) {
  10014. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10015. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10016. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10017. pring->txq_cnt, phba->iocb_cnt,
  10018. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10019. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10020. return false;
  10021. }
  10022. /* Save off the slow-path queue event for work thread to process */
  10023. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10024. spin_lock_irqsave(&phba->hbalock, iflags);
  10025. list_add_tail(&irspiocbq->cq_event.list,
  10026. &phba->sli4_hba.sp_queue_event);
  10027. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10028. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10029. return true;
  10030. }
  10031. /**
  10032. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10033. * @phba: Pointer to HBA context object.
  10034. * @wcqe: Pointer to work-queue completion queue entry.
  10035. *
  10036. * This routine handles slow-path WQ entry comsumed event by invoking the
  10037. * proper WQ release routine to the slow-path WQ.
  10038. **/
  10039. static void
  10040. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10041. struct lpfc_wcqe_release *wcqe)
  10042. {
  10043. /* sanity check on queue memory */
  10044. if (unlikely(!phba->sli4_hba.els_wq))
  10045. return;
  10046. /* Check for the slow-path ELS work queue */
  10047. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10048. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10049. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10050. else
  10051. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10052. "2579 Slow-path wqe consume event carries "
  10053. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10054. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10055. phba->sli4_hba.els_wq->queue_id);
  10056. }
  10057. /**
  10058. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10059. * @phba: Pointer to HBA context object.
  10060. * @cq: Pointer to a WQ completion queue.
  10061. * @wcqe: Pointer to work-queue completion queue entry.
  10062. *
  10063. * This routine handles an XRI abort event.
  10064. *
  10065. * Return: true if work posted to worker thread, otherwise false.
  10066. **/
  10067. static bool
  10068. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10069. struct lpfc_queue *cq,
  10070. struct sli4_wcqe_xri_aborted *wcqe)
  10071. {
  10072. bool workposted = false;
  10073. struct lpfc_cq_event *cq_event;
  10074. unsigned long iflags;
  10075. /* Allocate a new internal CQ_EVENT entry */
  10076. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10077. if (!cq_event) {
  10078. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10079. "0602 Failed to allocate CQ_EVENT entry\n");
  10080. return false;
  10081. }
  10082. /* Move the CQE into the proper xri abort event list */
  10083. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10084. switch (cq->subtype) {
  10085. case LPFC_FCP:
  10086. spin_lock_irqsave(&phba->hbalock, iflags);
  10087. list_add_tail(&cq_event->list,
  10088. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10089. /* Set the fcp xri abort event flag */
  10090. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10091. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10092. workposted = true;
  10093. break;
  10094. case LPFC_ELS:
  10095. spin_lock_irqsave(&phba->hbalock, iflags);
  10096. list_add_tail(&cq_event->list,
  10097. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10098. /* Set the els xri abort event flag */
  10099. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10100. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10101. workposted = true;
  10102. break;
  10103. default:
  10104. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10105. "0603 Invalid work queue CQE subtype (x%x)\n",
  10106. cq->subtype);
  10107. workposted = false;
  10108. break;
  10109. }
  10110. return workposted;
  10111. }
  10112. /**
  10113. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10114. * @phba: Pointer to HBA context object.
  10115. * @rcqe: Pointer to receive-queue completion queue entry.
  10116. *
  10117. * This routine process a receive-queue completion queue entry.
  10118. *
  10119. * Return: true if work posted to worker thread, otherwise false.
  10120. **/
  10121. static bool
  10122. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10123. {
  10124. bool workposted = false;
  10125. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10126. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10127. struct hbq_dmabuf *dma_buf;
  10128. uint32_t status, rq_id;
  10129. unsigned long iflags;
  10130. /* sanity check on queue memory */
  10131. if (unlikely(!hrq) || unlikely(!drq))
  10132. return workposted;
  10133. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10134. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10135. else
  10136. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10137. if (rq_id != hrq->queue_id)
  10138. goto out;
  10139. status = bf_get(lpfc_rcqe_status, rcqe);
  10140. switch (status) {
  10141. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10142. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10143. "2537 Receive Frame Truncated!!\n");
  10144. case FC_STATUS_RQ_SUCCESS:
  10145. lpfc_sli4_rq_release(hrq, drq);
  10146. spin_lock_irqsave(&phba->hbalock, iflags);
  10147. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10148. if (!dma_buf) {
  10149. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10150. goto out;
  10151. }
  10152. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10153. /* save off the frame for the word thread to process */
  10154. list_add_tail(&dma_buf->cq_event.list,
  10155. &phba->sli4_hba.sp_queue_event);
  10156. /* Frame received */
  10157. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10158. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10159. workposted = true;
  10160. break;
  10161. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10162. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10163. /* Post more buffers if possible */
  10164. spin_lock_irqsave(&phba->hbalock, iflags);
  10165. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10166. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10167. workposted = true;
  10168. break;
  10169. }
  10170. out:
  10171. return workposted;
  10172. }
  10173. /**
  10174. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10175. * @phba: Pointer to HBA context object.
  10176. * @cq: Pointer to the completion queue.
  10177. * @wcqe: Pointer to a completion queue entry.
  10178. *
  10179. * This routine process a slow-path work-queue or receive queue completion queue
  10180. * entry.
  10181. *
  10182. * Return: true if work posted to worker thread, otherwise false.
  10183. **/
  10184. static bool
  10185. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10186. struct lpfc_cqe *cqe)
  10187. {
  10188. struct lpfc_cqe cqevt;
  10189. bool workposted = false;
  10190. /* Copy the work queue CQE and convert endian order if needed */
  10191. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10192. /* Check and process for different type of WCQE and dispatch */
  10193. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10194. case CQE_CODE_COMPL_WQE:
  10195. /* Process the WQ/RQ complete event */
  10196. phba->last_completion_time = jiffies;
  10197. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10198. (struct lpfc_wcqe_complete *)&cqevt);
  10199. break;
  10200. case CQE_CODE_RELEASE_WQE:
  10201. /* Process the WQ release event */
  10202. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10203. (struct lpfc_wcqe_release *)&cqevt);
  10204. break;
  10205. case CQE_CODE_XRI_ABORTED:
  10206. /* Process the WQ XRI abort event */
  10207. phba->last_completion_time = jiffies;
  10208. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10209. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10210. break;
  10211. case CQE_CODE_RECEIVE:
  10212. case CQE_CODE_RECEIVE_V1:
  10213. /* Process the RQ event */
  10214. phba->last_completion_time = jiffies;
  10215. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10216. (struct lpfc_rcqe *)&cqevt);
  10217. break;
  10218. default:
  10219. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10220. "0388 Not a valid WCQE code: x%x\n",
  10221. bf_get(lpfc_cqe_code, &cqevt));
  10222. break;
  10223. }
  10224. return workposted;
  10225. }
  10226. /**
  10227. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10228. * @phba: Pointer to HBA context object.
  10229. * @eqe: Pointer to fast-path event queue entry.
  10230. *
  10231. * This routine process a event queue entry from the slow-path event queue.
  10232. * It will check the MajorCode and MinorCode to determine this is for a
  10233. * completion event on a completion queue, if not, an error shall be logged
  10234. * and just return. Otherwise, it will get to the corresponding completion
  10235. * queue and process all the entries on that completion queue, rearm the
  10236. * completion queue, and then return.
  10237. *
  10238. **/
  10239. static void
  10240. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10241. {
  10242. struct lpfc_queue *cq = NULL, *childq, *speq;
  10243. struct lpfc_cqe *cqe;
  10244. bool workposted = false;
  10245. int ecount = 0;
  10246. uint16_t cqid;
  10247. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10248. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10249. "0359 Not a valid slow-path completion "
  10250. "event: majorcode=x%x, minorcode=x%x\n",
  10251. bf_get_le32(lpfc_eqe_major_code, eqe),
  10252. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10253. return;
  10254. }
  10255. /* Get the reference to the corresponding CQ */
  10256. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10257. /* Search for completion queue pointer matching this cqid */
  10258. speq = phba->sli4_hba.sp_eq;
  10259. /* sanity check on queue memory */
  10260. if (unlikely(!speq))
  10261. return;
  10262. list_for_each_entry(childq, &speq->child_list, list) {
  10263. if (childq->queue_id == cqid) {
  10264. cq = childq;
  10265. break;
  10266. }
  10267. }
  10268. if (unlikely(!cq)) {
  10269. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10270. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10271. "0365 Slow-path CQ identifier "
  10272. "(%d) does not exist\n", cqid);
  10273. return;
  10274. }
  10275. /* Process all the entries to the CQ */
  10276. switch (cq->type) {
  10277. case LPFC_MCQ:
  10278. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10279. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10280. if (!(++ecount % cq->entry_repost))
  10281. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10282. }
  10283. break;
  10284. case LPFC_WCQ:
  10285. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10286. if (cq->subtype == LPFC_FCP)
  10287. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10288. cqe);
  10289. else
  10290. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10291. cqe);
  10292. if (!(++ecount % cq->entry_repost))
  10293. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10294. }
  10295. break;
  10296. default:
  10297. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10298. "0370 Invalid completion queue type (%d)\n",
  10299. cq->type);
  10300. return;
  10301. }
  10302. /* Catch the no cq entry condition, log an error */
  10303. if (unlikely(ecount == 0))
  10304. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10305. "0371 No entry from the CQ: identifier "
  10306. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10307. /* In any case, flash and re-arm the RCQ */
  10308. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10309. /* wake up worker thread if there are works to be done */
  10310. if (workposted)
  10311. lpfc_worker_wake_up(phba);
  10312. }
  10313. /**
  10314. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10315. * @eqe: Pointer to fast-path completion queue entry.
  10316. *
  10317. * This routine process a fast-path work queue completion entry from fast-path
  10318. * event queue for FCP command response completion.
  10319. **/
  10320. static void
  10321. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10322. struct lpfc_wcqe_complete *wcqe)
  10323. {
  10324. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10325. struct lpfc_iocbq *cmdiocbq;
  10326. struct lpfc_iocbq irspiocbq;
  10327. unsigned long iflags;
  10328. spin_lock_irqsave(&phba->hbalock, iflags);
  10329. pring->stats.iocb_event++;
  10330. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10331. /* Check for response status */
  10332. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10333. /* If resource errors reported from HBA, reduce queue
  10334. * depth of the SCSI device.
  10335. */
  10336. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10337. IOSTAT_LOCAL_REJECT) &&
  10338. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10339. phba->lpfc_rampdown_queue_depth(phba);
  10340. }
  10341. /* Log the error status */
  10342. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10343. "0373 FCP complete error: status=x%x, "
  10344. "hw_status=x%x, total_data_specified=%d, "
  10345. "parameter=x%x, word3=x%x\n",
  10346. bf_get(lpfc_wcqe_c_status, wcqe),
  10347. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10348. wcqe->total_data_placed, wcqe->parameter,
  10349. wcqe->word3);
  10350. }
  10351. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10352. spin_lock_irqsave(&phba->hbalock, iflags);
  10353. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10354. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10355. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10356. if (unlikely(!cmdiocbq)) {
  10357. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10358. "0374 FCP complete with no corresponding "
  10359. "cmdiocb: iotag (%d)\n",
  10360. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10361. return;
  10362. }
  10363. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10364. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10365. "0375 FCP cmdiocb not callback function "
  10366. "iotag: (%d)\n",
  10367. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10368. return;
  10369. }
  10370. /* Fake the irspiocb and copy necessary response information */
  10371. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10372. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10373. spin_lock_irqsave(&phba->hbalock, iflags);
  10374. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10375. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10376. }
  10377. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10378. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10379. }
  10380. /**
  10381. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10382. * @phba: Pointer to HBA context object.
  10383. * @cq: Pointer to completion queue.
  10384. * @wcqe: Pointer to work-queue completion queue entry.
  10385. *
  10386. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10387. * proper WQ release routine to the slow-path WQ.
  10388. **/
  10389. static void
  10390. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10391. struct lpfc_wcqe_release *wcqe)
  10392. {
  10393. struct lpfc_queue *childwq;
  10394. bool wqid_matched = false;
  10395. uint16_t fcp_wqid;
  10396. /* Check for fast-path FCP work queue release */
  10397. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10398. list_for_each_entry(childwq, &cq->child_list, list) {
  10399. if (childwq->queue_id == fcp_wqid) {
  10400. lpfc_sli4_wq_release(childwq,
  10401. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10402. wqid_matched = true;
  10403. break;
  10404. }
  10405. }
  10406. /* Report warning log message if no match found */
  10407. if (wqid_matched != true)
  10408. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10409. "2580 Fast-path wqe consume event carries "
  10410. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10411. }
  10412. /**
  10413. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10414. * @cq: Pointer to the completion queue.
  10415. * @eqe: Pointer to fast-path completion queue entry.
  10416. *
  10417. * This routine process a fast-path work queue completion entry from fast-path
  10418. * event queue for FCP command response completion.
  10419. **/
  10420. static int
  10421. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10422. struct lpfc_cqe *cqe)
  10423. {
  10424. struct lpfc_wcqe_release wcqe;
  10425. bool workposted = false;
  10426. /* Copy the work queue CQE and convert endian order if needed */
  10427. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10428. /* Check and process for different type of WCQE and dispatch */
  10429. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10430. case CQE_CODE_COMPL_WQE:
  10431. /* Process the WQ complete event */
  10432. phba->last_completion_time = jiffies;
  10433. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10434. (struct lpfc_wcqe_complete *)&wcqe);
  10435. break;
  10436. case CQE_CODE_RELEASE_WQE:
  10437. /* Process the WQ release event */
  10438. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10439. (struct lpfc_wcqe_release *)&wcqe);
  10440. break;
  10441. case CQE_CODE_XRI_ABORTED:
  10442. /* Process the WQ XRI abort event */
  10443. phba->last_completion_time = jiffies;
  10444. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10445. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10446. break;
  10447. default:
  10448. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10449. "0144 Not a valid WCQE code: x%x\n",
  10450. bf_get(lpfc_wcqe_c_code, &wcqe));
  10451. break;
  10452. }
  10453. return workposted;
  10454. }
  10455. /**
  10456. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10457. * @phba: Pointer to HBA context object.
  10458. * @eqe: Pointer to fast-path event queue entry.
  10459. *
  10460. * This routine process a event queue entry from the fast-path event queue.
  10461. * It will check the MajorCode and MinorCode to determine this is for a
  10462. * completion event on a completion queue, if not, an error shall be logged
  10463. * and just return. Otherwise, it will get to the corresponding completion
  10464. * queue and process all the entries on the completion queue, rearm the
  10465. * completion queue, and then return.
  10466. **/
  10467. static void
  10468. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10469. uint32_t fcp_cqidx)
  10470. {
  10471. struct lpfc_queue *cq;
  10472. struct lpfc_cqe *cqe;
  10473. bool workposted = false;
  10474. uint16_t cqid;
  10475. int ecount = 0;
  10476. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10477. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10478. "0366 Not a valid fast-path completion "
  10479. "event: majorcode=x%x, minorcode=x%x\n",
  10480. bf_get_le32(lpfc_eqe_major_code, eqe),
  10481. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10482. return;
  10483. }
  10484. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10485. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10486. "3146 Fast-path completion queues "
  10487. "does not exist\n");
  10488. return;
  10489. }
  10490. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10491. if (unlikely(!cq)) {
  10492. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10493. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10494. "0367 Fast-path completion queue "
  10495. "(%d) does not exist\n", fcp_cqidx);
  10496. return;
  10497. }
  10498. /* Get the reference to the corresponding CQ */
  10499. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10500. if (unlikely(cqid != cq->queue_id)) {
  10501. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10502. "0368 Miss-matched fast-path completion "
  10503. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10504. cqid, cq->queue_id);
  10505. return;
  10506. }
  10507. /* Process all the entries to the CQ */
  10508. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10509. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10510. if (!(++ecount % cq->entry_repost))
  10511. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10512. }
  10513. /* Catch the no cq entry condition */
  10514. if (unlikely(ecount == 0))
  10515. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10516. "0369 No entry from fast-path completion "
  10517. "queue fcpcqid=%d\n", cq->queue_id);
  10518. /* In any case, flash and re-arm the CQ */
  10519. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10520. /* wake up worker thread if there are works to be done */
  10521. if (workposted)
  10522. lpfc_worker_wake_up(phba);
  10523. }
  10524. static void
  10525. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10526. {
  10527. struct lpfc_eqe *eqe;
  10528. /* walk all the EQ entries and drop on the floor */
  10529. while ((eqe = lpfc_sli4_eq_get(eq)))
  10530. ;
  10531. /* Clear and re-arm the EQ */
  10532. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10533. }
  10534. /**
  10535. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10536. * @irq: Interrupt number.
  10537. * @dev_id: The device context pointer.
  10538. *
  10539. * This function is directly called from the PCI layer as an interrupt
  10540. * service routine when device with SLI-4 interface spec is enabled with
  10541. * MSI-X multi-message interrupt mode and there are slow-path events in
  10542. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10543. * interrupt mode, this function is called as part of the device-level
  10544. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10545. * undergoing initialization, the interrupt handler will not process the
  10546. * interrupt. The link attention and ELS ring attention events are handled
  10547. * by the worker thread. The interrupt handler signals the worker thread
  10548. * and returns for these events. This function is called without any lock
  10549. * held. It gets the hbalock to access and update SLI data structures.
  10550. *
  10551. * This function returns IRQ_HANDLED when interrupt is handled else it
  10552. * returns IRQ_NONE.
  10553. **/
  10554. irqreturn_t
  10555. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10556. {
  10557. struct lpfc_hba *phba;
  10558. struct lpfc_queue *speq;
  10559. struct lpfc_eqe *eqe;
  10560. unsigned long iflag;
  10561. int ecount = 0;
  10562. /*
  10563. * Get the driver's phba structure from the dev_id
  10564. */
  10565. phba = (struct lpfc_hba *)dev_id;
  10566. if (unlikely(!phba))
  10567. return IRQ_NONE;
  10568. /* Get to the EQ struct associated with this vector */
  10569. speq = phba->sli4_hba.sp_eq;
  10570. if (unlikely(!speq))
  10571. return IRQ_NONE;
  10572. /* Check device state for handling interrupt */
  10573. if (unlikely(lpfc_intr_state_check(phba))) {
  10574. /* Check again for link_state with lock held */
  10575. spin_lock_irqsave(&phba->hbalock, iflag);
  10576. if (phba->link_state < LPFC_LINK_DOWN)
  10577. /* Flush, clear interrupt, and rearm the EQ */
  10578. lpfc_sli4_eq_flush(phba, speq);
  10579. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10580. return IRQ_NONE;
  10581. }
  10582. /*
  10583. * Process all the event on FCP slow-path EQ
  10584. */
  10585. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10586. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10587. if (!(++ecount % speq->entry_repost))
  10588. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10589. }
  10590. /* Always clear and re-arm the slow-path EQ */
  10591. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10592. /* Catch the no cq entry condition */
  10593. if (unlikely(ecount == 0)) {
  10594. if (phba->intr_type == MSIX)
  10595. /* MSI-X treated interrupt served as no EQ share INT */
  10596. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10597. "0357 MSI-X interrupt with no EQE\n");
  10598. else
  10599. /* Non MSI-X treated on interrupt as EQ share INT */
  10600. return IRQ_NONE;
  10601. }
  10602. return IRQ_HANDLED;
  10603. } /* lpfc_sli4_sp_intr_handler */
  10604. /**
  10605. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10606. * @irq: Interrupt number.
  10607. * @dev_id: The device context pointer.
  10608. *
  10609. * This function is directly called from the PCI layer as an interrupt
  10610. * service routine when device with SLI-4 interface spec is enabled with
  10611. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10612. * ring event in the HBA. However, when the device is enabled with either
  10613. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10614. * device-level interrupt handler. When the PCI slot is in error recovery
  10615. * or the HBA is undergoing initialization, the interrupt handler will not
  10616. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10617. * the intrrupt context. This function is called without any lock held.
  10618. * It gets the hbalock to access and update SLI data structures. Note that,
  10619. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10620. * equal to that of FCP CQ index.
  10621. *
  10622. * This function returns IRQ_HANDLED when interrupt is handled else it
  10623. * returns IRQ_NONE.
  10624. **/
  10625. irqreturn_t
  10626. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10627. {
  10628. struct lpfc_hba *phba;
  10629. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10630. struct lpfc_queue *fpeq;
  10631. struct lpfc_eqe *eqe;
  10632. unsigned long iflag;
  10633. int ecount = 0;
  10634. uint32_t fcp_eqidx;
  10635. /* Get the driver's phba structure from the dev_id */
  10636. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10637. phba = fcp_eq_hdl->phba;
  10638. fcp_eqidx = fcp_eq_hdl->idx;
  10639. if (unlikely(!phba))
  10640. return IRQ_NONE;
  10641. if (unlikely(!phba->sli4_hba.fp_eq))
  10642. return IRQ_NONE;
  10643. /* Get to the EQ struct associated with this vector */
  10644. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10645. if (unlikely(!fpeq))
  10646. return IRQ_NONE;
  10647. /* Check device state for handling interrupt */
  10648. if (unlikely(lpfc_intr_state_check(phba))) {
  10649. /* Check again for link_state with lock held */
  10650. spin_lock_irqsave(&phba->hbalock, iflag);
  10651. if (phba->link_state < LPFC_LINK_DOWN)
  10652. /* Flush, clear interrupt, and rearm the EQ */
  10653. lpfc_sli4_eq_flush(phba, fpeq);
  10654. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10655. return IRQ_NONE;
  10656. }
  10657. /*
  10658. * Process all the event on FCP fast-path EQ
  10659. */
  10660. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10661. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10662. if (!(++ecount % fpeq->entry_repost))
  10663. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10664. }
  10665. /* Always clear and re-arm the fast-path EQ */
  10666. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10667. if (unlikely(ecount == 0)) {
  10668. if (phba->intr_type == MSIX)
  10669. /* MSI-X treated interrupt served as no EQ share INT */
  10670. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10671. "0358 MSI-X interrupt with no EQE\n");
  10672. else
  10673. /* Non MSI-X treated on interrupt as EQ share INT */
  10674. return IRQ_NONE;
  10675. }
  10676. return IRQ_HANDLED;
  10677. } /* lpfc_sli4_fp_intr_handler */
  10678. /**
  10679. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10680. * @irq: Interrupt number.
  10681. * @dev_id: The device context pointer.
  10682. *
  10683. * This function is the device-level interrupt handler to device with SLI-4
  10684. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10685. * interrupt mode is enabled and there is an event in the HBA which requires
  10686. * driver attention. This function invokes the slow-path interrupt attention
  10687. * handling function and fast-path interrupt attention handling function in
  10688. * turn to process the relevant HBA attention events. This function is called
  10689. * without any lock held. It gets the hbalock to access and update SLI data
  10690. * structures.
  10691. *
  10692. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10693. * returns IRQ_NONE.
  10694. **/
  10695. irqreturn_t
  10696. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10697. {
  10698. struct lpfc_hba *phba;
  10699. irqreturn_t sp_irq_rc, fp_irq_rc;
  10700. bool fp_handled = false;
  10701. uint32_t fcp_eqidx;
  10702. /* Get the driver's phba structure from the dev_id */
  10703. phba = (struct lpfc_hba *)dev_id;
  10704. if (unlikely(!phba))
  10705. return IRQ_NONE;
  10706. /*
  10707. * Invokes slow-path host attention interrupt handling as appropriate.
  10708. */
  10709. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10710. /*
  10711. * Invoke fast-path host attention interrupt handling as appropriate.
  10712. */
  10713. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10714. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10715. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10716. if (fp_irq_rc == IRQ_HANDLED)
  10717. fp_handled |= true;
  10718. }
  10719. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10720. } /* lpfc_sli4_intr_handler */
  10721. /**
  10722. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10723. * @queue: The queue structure to free.
  10724. *
  10725. * This function frees a queue structure and the DMAable memory used for
  10726. * the host resident queue. This function must be called after destroying the
  10727. * queue on the HBA.
  10728. **/
  10729. void
  10730. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10731. {
  10732. struct lpfc_dmabuf *dmabuf;
  10733. if (!queue)
  10734. return;
  10735. while (!list_empty(&queue->page_list)) {
  10736. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10737. list);
  10738. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10739. dmabuf->virt, dmabuf->phys);
  10740. kfree(dmabuf);
  10741. }
  10742. kfree(queue);
  10743. return;
  10744. }
  10745. /**
  10746. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10747. * @phba: The HBA that this queue is being created on.
  10748. * @entry_size: The size of each queue entry for this queue.
  10749. * @entry count: The number of entries that this queue will handle.
  10750. *
  10751. * This function allocates a queue structure and the DMAable memory used for
  10752. * the host resident queue. This function must be called before creating the
  10753. * queue on the HBA.
  10754. **/
  10755. struct lpfc_queue *
  10756. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10757. uint32_t entry_count)
  10758. {
  10759. struct lpfc_queue *queue;
  10760. struct lpfc_dmabuf *dmabuf;
  10761. int x, total_qe_count;
  10762. void *dma_pointer;
  10763. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10764. if (!phba->sli4_hba.pc_sli4_params.supported)
  10765. hw_page_size = SLI4_PAGE_SIZE;
  10766. queue = kzalloc(sizeof(struct lpfc_queue) +
  10767. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10768. if (!queue)
  10769. return NULL;
  10770. queue->page_count = (ALIGN(entry_size * entry_count,
  10771. hw_page_size))/hw_page_size;
  10772. INIT_LIST_HEAD(&queue->list);
  10773. INIT_LIST_HEAD(&queue->page_list);
  10774. INIT_LIST_HEAD(&queue->child_list);
  10775. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10776. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10777. if (!dmabuf)
  10778. goto out_fail;
  10779. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10780. hw_page_size, &dmabuf->phys,
  10781. GFP_KERNEL);
  10782. if (!dmabuf->virt) {
  10783. kfree(dmabuf);
  10784. goto out_fail;
  10785. }
  10786. memset(dmabuf->virt, 0, hw_page_size);
  10787. dmabuf->buffer_tag = x;
  10788. list_add_tail(&dmabuf->list, &queue->page_list);
  10789. /* initialize queue's entry array */
  10790. dma_pointer = dmabuf->virt;
  10791. for (; total_qe_count < entry_count &&
  10792. dma_pointer < (hw_page_size + dmabuf->virt);
  10793. total_qe_count++, dma_pointer += entry_size) {
  10794. queue->qe[total_qe_count].address = dma_pointer;
  10795. }
  10796. }
  10797. queue->entry_size = entry_size;
  10798. queue->entry_count = entry_count;
  10799. /*
  10800. * entry_repost is calculated based on the number of entries in the
  10801. * queue. This works out except for RQs. If buffers are NOT initially
  10802. * posted for every RQE, entry_repost should be adjusted accordingly.
  10803. */
  10804. queue->entry_repost = (entry_count >> 3);
  10805. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10806. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10807. queue->phba = phba;
  10808. return queue;
  10809. out_fail:
  10810. lpfc_sli4_queue_free(queue);
  10811. return NULL;
  10812. }
  10813. /**
  10814. * lpfc_eq_create - Create an Event Queue on the HBA
  10815. * @phba: HBA structure that indicates port to create a queue on.
  10816. * @eq: The queue structure to use to create the event queue.
  10817. * @imax: The maximum interrupt per second limit.
  10818. *
  10819. * This function creates an event queue, as detailed in @eq, on a port,
  10820. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10821. *
  10822. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10823. * is used to get the entry count and entry size that are necessary to
  10824. * determine the number of pages to allocate and use for this queue. This
  10825. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10826. * event queue. This function is asynchronous and will wait for the mailbox
  10827. * command to finish before continuing.
  10828. *
  10829. * On success this function will return a zero. If unable to allocate enough
  10830. * memory this function will return -ENOMEM. If the queue create mailbox command
  10831. * fails this function will return -ENXIO.
  10832. **/
  10833. uint32_t
  10834. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10835. {
  10836. struct lpfc_mbx_eq_create *eq_create;
  10837. LPFC_MBOXQ_t *mbox;
  10838. int rc, length, status = 0;
  10839. struct lpfc_dmabuf *dmabuf;
  10840. uint32_t shdr_status, shdr_add_status;
  10841. union lpfc_sli4_cfg_shdr *shdr;
  10842. uint16_t dmult;
  10843. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10844. /* sanity check on queue memory */
  10845. if (!eq)
  10846. return -ENODEV;
  10847. if (!phba->sli4_hba.pc_sli4_params.supported)
  10848. hw_page_size = SLI4_PAGE_SIZE;
  10849. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10850. if (!mbox)
  10851. return -ENOMEM;
  10852. length = (sizeof(struct lpfc_mbx_eq_create) -
  10853. sizeof(struct lpfc_sli4_cfg_mhdr));
  10854. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10855. LPFC_MBOX_OPCODE_EQ_CREATE,
  10856. length, LPFC_SLI4_MBX_EMBED);
  10857. eq_create = &mbox->u.mqe.un.eq_create;
  10858. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10859. eq->page_count);
  10860. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10861. LPFC_EQE_SIZE);
  10862. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10863. /* Calculate delay multiper from maximum interrupt per second */
  10864. dmult = LPFC_DMULT_CONST/imax - 1;
  10865. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10866. dmult);
  10867. switch (eq->entry_count) {
  10868. default:
  10869. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10870. "0360 Unsupported EQ count. (%d)\n",
  10871. eq->entry_count);
  10872. if (eq->entry_count < 256)
  10873. return -EINVAL;
  10874. /* otherwise default to smallest count (drop through) */
  10875. case 256:
  10876. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10877. LPFC_EQ_CNT_256);
  10878. break;
  10879. case 512:
  10880. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10881. LPFC_EQ_CNT_512);
  10882. break;
  10883. case 1024:
  10884. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10885. LPFC_EQ_CNT_1024);
  10886. break;
  10887. case 2048:
  10888. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10889. LPFC_EQ_CNT_2048);
  10890. break;
  10891. case 4096:
  10892. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10893. LPFC_EQ_CNT_4096);
  10894. break;
  10895. }
  10896. list_for_each_entry(dmabuf, &eq->page_list, list) {
  10897. memset(dmabuf->virt, 0, hw_page_size);
  10898. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10899. putPaddrLow(dmabuf->phys);
  10900. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10901. putPaddrHigh(dmabuf->phys);
  10902. }
  10903. mbox->vport = phba->pport;
  10904. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  10905. mbox->context1 = NULL;
  10906. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10907. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  10908. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10909. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10910. if (shdr_status || shdr_add_status || rc) {
  10911. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10912. "2500 EQ_CREATE mailbox failed with "
  10913. "status x%x add_status x%x, mbx status x%x\n",
  10914. shdr_status, shdr_add_status, rc);
  10915. status = -ENXIO;
  10916. }
  10917. eq->type = LPFC_EQ;
  10918. eq->subtype = LPFC_NONE;
  10919. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  10920. if (eq->queue_id == 0xFFFF)
  10921. status = -ENXIO;
  10922. eq->host_index = 0;
  10923. eq->hba_index = 0;
  10924. mempool_free(mbox, phba->mbox_mem_pool);
  10925. return status;
  10926. }
  10927. /**
  10928. * lpfc_cq_create - Create a Completion Queue on the HBA
  10929. * @phba: HBA structure that indicates port to create a queue on.
  10930. * @cq: The queue structure to use to create the completion queue.
  10931. * @eq: The event queue to bind this completion queue to.
  10932. *
  10933. * This function creates a completion queue, as detailed in @wq, on a port,
  10934. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  10935. *
  10936. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10937. * is used to get the entry count and entry size that are necessary to
  10938. * determine the number of pages to allocate and use for this queue. The @eq
  10939. * is used to indicate which event queue to bind this completion queue to. This
  10940. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  10941. * completion queue. This function is asynchronous and will wait for the mailbox
  10942. * command to finish before continuing.
  10943. *
  10944. * On success this function will return a zero. If unable to allocate enough
  10945. * memory this function will return -ENOMEM. If the queue create mailbox command
  10946. * fails this function will return -ENXIO.
  10947. **/
  10948. uint32_t
  10949. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10950. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  10951. {
  10952. struct lpfc_mbx_cq_create *cq_create;
  10953. struct lpfc_dmabuf *dmabuf;
  10954. LPFC_MBOXQ_t *mbox;
  10955. int rc, length, status = 0;
  10956. uint32_t shdr_status, shdr_add_status;
  10957. union lpfc_sli4_cfg_shdr *shdr;
  10958. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10959. /* sanity check on queue memory */
  10960. if (!cq || !eq)
  10961. return -ENODEV;
  10962. if (!phba->sli4_hba.pc_sli4_params.supported)
  10963. hw_page_size = SLI4_PAGE_SIZE;
  10964. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10965. if (!mbox)
  10966. return -ENOMEM;
  10967. length = (sizeof(struct lpfc_mbx_cq_create) -
  10968. sizeof(struct lpfc_sli4_cfg_mhdr));
  10969. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10970. LPFC_MBOX_OPCODE_CQ_CREATE,
  10971. length, LPFC_SLI4_MBX_EMBED);
  10972. cq_create = &mbox->u.mqe.un.cq_create;
  10973. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  10974. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  10975. cq->page_count);
  10976. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  10977. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  10978. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10979. phba->sli4_hba.pc_sli4_params.cqv);
  10980. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  10981. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  10982. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  10983. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  10984. eq->queue_id);
  10985. } else {
  10986. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  10987. eq->queue_id);
  10988. }
  10989. switch (cq->entry_count) {
  10990. default:
  10991. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10992. "0361 Unsupported CQ count. (%d)\n",
  10993. cq->entry_count);
  10994. if (cq->entry_count < 256)
  10995. return -EINVAL;
  10996. /* otherwise default to smallest count (drop through) */
  10997. case 256:
  10998. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10999. LPFC_CQ_CNT_256);
  11000. break;
  11001. case 512:
  11002. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11003. LPFC_CQ_CNT_512);
  11004. break;
  11005. case 1024:
  11006. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11007. LPFC_CQ_CNT_1024);
  11008. break;
  11009. }
  11010. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11011. memset(dmabuf->virt, 0, hw_page_size);
  11012. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11013. putPaddrLow(dmabuf->phys);
  11014. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11015. putPaddrHigh(dmabuf->phys);
  11016. }
  11017. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11018. /* The IOCTL status is embedded in the mailbox subheader. */
  11019. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11020. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11021. if (shdr_status || shdr_add_status || rc) {
  11022. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11023. "2501 CQ_CREATE mailbox failed with "
  11024. "status x%x add_status x%x, mbx status x%x\n",
  11025. shdr_status, shdr_add_status, rc);
  11026. status = -ENXIO;
  11027. goto out;
  11028. }
  11029. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11030. if (cq->queue_id == 0xFFFF) {
  11031. status = -ENXIO;
  11032. goto out;
  11033. }
  11034. /* link the cq onto the parent eq child list */
  11035. list_add_tail(&cq->list, &eq->child_list);
  11036. /* Set up completion queue's type and subtype */
  11037. cq->type = type;
  11038. cq->subtype = subtype;
  11039. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11040. cq->assoc_qid = eq->queue_id;
  11041. cq->host_index = 0;
  11042. cq->hba_index = 0;
  11043. out:
  11044. mempool_free(mbox, phba->mbox_mem_pool);
  11045. return status;
  11046. }
  11047. /**
  11048. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11049. * @phba: HBA structure that indicates port to create a queue on.
  11050. * @mq: The queue structure to use to create the mailbox queue.
  11051. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11052. * @cq: The completion queue to associate with this cq.
  11053. *
  11054. * This function provides failback (fb) functionality when the
  11055. * mq_create_ext fails on older FW generations. It's purpose is identical
  11056. * to mq_create_ext otherwise.
  11057. *
  11058. * This routine cannot fail as all attributes were previously accessed and
  11059. * initialized in mq_create_ext.
  11060. **/
  11061. static void
  11062. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11063. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11064. {
  11065. struct lpfc_mbx_mq_create *mq_create;
  11066. struct lpfc_dmabuf *dmabuf;
  11067. int length;
  11068. length = (sizeof(struct lpfc_mbx_mq_create) -
  11069. sizeof(struct lpfc_sli4_cfg_mhdr));
  11070. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11071. LPFC_MBOX_OPCODE_MQ_CREATE,
  11072. length, LPFC_SLI4_MBX_EMBED);
  11073. mq_create = &mbox->u.mqe.un.mq_create;
  11074. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11075. mq->page_count);
  11076. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11077. cq->queue_id);
  11078. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11079. switch (mq->entry_count) {
  11080. case 16:
  11081. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11082. LPFC_MQ_RING_SIZE_16);
  11083. break;
  11084. case 32:
  11085. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11086. LPFC_MQ_RING_SIZE_32);
  11087. break;
  11088. case 64:
  11089. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11090. LPFC_MQ_RING_SIZE_64);
  11091. break;
  11092. case 128:
  11093. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11094. LPFC_MQ_RING_SIZE_128);
  11095. break;
  11096. }
  11097. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11098. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11099. putPaddrLow(dmabuf->phys);
  11100. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11101. putPaddrHigh(dmabuf->phys);
  11102. }
  11103. }
  11104. /**
  11105. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11106. * @phba: HBA structure that indicates port to create a queue on.
  11107. * @mq: The queue structure to use to create the mailbox queue.
  11108. * @cq: The completion queue to associate with this cq.
  11109. * @subtype: The queue's subtype.
  11110. *
  11111. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11112. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11113. *
  11114. * The @phba struct is used to send mailbox command to HBA. The @cq 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 MQ_CREATE mailbox command to the HBA to setup the
  11118. * mailbox 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. int32_t
  11126. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11127. struct lpfc_queue *cq, uint32_t subtype)
  11128. {
  11129. struct lpfc_mbx_mq_create *mq_create;
  11130. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11131. struct lpfc_dmabuf *dmabuf;
  11132. LPFC_MBOXQ_t *mbox;
  11133. int rc, length, status = 0;
  11134. uint32_t shdr_status, shdr_add_status;
  11135. union lpfc_sli4_cfg_shdr *shdr;
  11136. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11137. /* sanity check on queue memory */
  11138. if (!mq || !cq)
  11139. return -ENODEV;
  11140. if (!phba->sli4_hba.pc_sli4_params.supported)
  11141. hw_page_size = SLI4_PAGE_SIZE;
  11142. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11143. if (!mbox)
  11144. return -ENOMEM;
  11145. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11146. sizeof(struct lpfc_sli4_cfg_mhdr));
  11147. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11148. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11149. length, LPFC_SLI4_MBX_EMBED);
  11150. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11151. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11152. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11153. &mq_create_ext->u.request, mq->page_count);
  11154. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11155. &mq_create_ext->u.request, 1);
  11156. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11157. &mq_create_ext->u.request, 1);
  11158. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11159. &mq_create_ext->u.request, 1);
  11160. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11161. &mq_create_ext->u.request, 1);
  11162. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11163. &mq_create_ext->u.request, 1);
  11164. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11165. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11166. phba->sli4_hba.pc_sli4_params.mqv);
  11167. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11168. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11169. cq->queue_id);
  11170. else
  11171. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11172. cq->queue_id);
  11173. switch (mq->entry_count) {
  11174. default:
  11175. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11176. "0362 Unsupported MQ count. (%d)\n",
  11177. mq->entry_count);
  11178. if (mq->entry_count < 16)
  11179. return -EINVAL;
  11180. /* otherwise default to smallest count (drop through) */
  11181. case 16:
  11182. bf_set(lpfc_mq_context_ring_size,
  11183. &mq_create_ext->u.request.context,
  11184. LPFC_MQ_RING_SIZE_16);
  11185. break;
  11186. case 32:
  11187. bf_set(lpfc_mq_context_ring_size,
  11188. &mq_create_ext->u.request.context,
  11189. LPFC_MQ_RING_SIZE_32);
  11190. break;
  11191. case 64:
  11192. bf_set(lpfc_mq_context_ring_size,
  11193. &mq_create_ext->u.request.context,
  11194. LPFC_MQ_RING_SIZE_64);
  11195. break;
  11196. case 128:
  11197. bf_set(lpfc_mq_context_ring_size,
  11198. &mq_create_ext->u.request.context,
  11199. LPFC_MQ_RING_SIZE_128);
  11200. break;
  11201. }
  11202. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11203. memset(dmabuf->virt, 0, hw_page_size);
  11204. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11205. putPaddrLow(dmabuf->phys);
  11206. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11207. putPaddrHigh(dmabuf->phys);
  11208. }
  11209. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11210. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11211. &mq_create_ext->u.response);
  11212. if (rc != MBX_SUCCESS) {
  11213. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11214. "2795 MQ_CREATE_EXT failed with "
  11215. "status x%x. Failback to MQ_CREATE.\n",
  11216. rc);
  11217. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11218. mq_create = &mbox->u.mqe.un.mq_create;
  11219. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11220. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11221. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11222. &mq_create->u.response);
  11223. }
  11224. /* The IOCTL status is embedded in the mailbox subheader. */
  11225. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11226. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11227. if (shdr_status || shdr_add_status || rc) {
  11228. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11229. "2502 MQ_CREATE mailbox failed with "
  11230. "status x%x add_status x%x, mbx status x%x\n",
  11231. shdr_status, shdr_add_status, rc);
  11232. status = -ENXIO;
  11233. goto out;
  11234. }
  11235. if (mq->queue_id == 0xFFFF) {
  11236. status = -ENXIO;
  11237. goto out;
  11238. }
  11239. mq->type = LPFC_MQ;
  11240. mq->assoc_qid = cq->queue_id;
  11241. mq->subtype = subtype;
  11242. mq->host_index = 0;
  11243. mq->hba_index = 0;
  11244. /* link the mq onto the parent cq child list */
  11245. list_add_tail(&mq->list, &cq->child_list);
  11246. out:
  11247. mempool_free(mbox, phba->mbox_mem_pool);
  11248. return status;
  11249. }
  11250. /**
  11251. * lpfc_wq_create - Create a Work Queue on the HBA
  11252. * @phba: HBA structure that indicates port to create a queue on.
  11253. * @wq: The queue structure to use to create the work queue.
  11254. * @cq: The completion queue to bind this work queue to.
  11255. * @subtype: The subtype of the work queue indicating its functionality.
  11256. *
  11257. * This function creates a work queue, as detailed in @wq, on a port, described
  11258. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11259. *
  11260. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11261. * is used to get the entry count and entry size that are necessary to
  11262. * determine the number of pages to allocate and use for this queue. The @cq
  11263. * is used to indicate which completion queue to bind this work queue to. This
  11264. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11265. * work queue. This function is asynchronous and will wait for the mailbox
  11266. * command to finish before continuing.
  11267. *
  11268. * On success this function will return a zero. If unable to allocate enough
  11269. * memory this function will return -ENOMEM. If the queue create mailbox command
  11270. * fails this function will return -ENXIO.
  11271. **/
  11272. uint32_t
  11273. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11274. struct lpfc_queue *cq, uint32_t subtype)
  11275. {
  11276. struct lpfc_mbx_wq_create *wq_create;
  11277. struct lpfc_dmabuf *dmabuf;
  11278. LPFC_MBOXQ_t *mbox;
  11279. int rc, length, status = 0;
  11280. uint32_t shdr_status, shdr_add_status;
  11281. union lpfc_sli4_cfg_shdr *shdr;
  11282. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11283. struct dma_address *page;
  11284. /* sanity check on queue memory */
  11285. if (!wq || !cq)
  11286. return -ENODEV;
  11287. if (!phba->sli4_hba.pc_sli4_params.supported)
  11288. hw_page_size = SLI4_PAGE_SIZE;
  11289. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11290. if (!mbox)
  11291. return -ENOMEM;
  11292. length = (sizeof(struct lpfc_mbx_wq_create) -
  11293. sizeof(struct lpfc_sli4_cfg_mhdr));
  11294. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11295. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11296. length, LPFC_SLI4_MBX_EMBED);
  11297. wq_create = &mbox->u.mqe.un.wq_create;
  11298. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11299. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11300. wq->page_count);
  11301. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11302. cq->queue_id);
  11303. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11304. phba->sli4_hba.pc_sli4_params.wqv);
  11305. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11306. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11307. wq->entry_count);
  11308. switch (wq->entry_size) {
  11309. default:
  11310. case 64:
  11311. bf_set(lpfc_mbx_wq_create_wqe_size,
  11312. &wq_create->u.request_1,
  11313. LPFC_WQ_WQE_SIZE_64);
  11314. break;
  11315. case 128:
  11316. bf_set(lpfc_mbx_wq_create_wqe_size,
  11317. &wq_create->u.request_1,
  11318. LPFC_WQ_WQE_SIZE_128);
  11319. break;
  11320. }
  11321. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11322. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11323. page = wq_create->u.request_1.page;
  11324. } else {
  11325. page = wq_create->u.request.page;
  11326. }
  11327. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11328. memset(dmabuf->virt, 0, hw_page_size);
  11329. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11330. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11331. }
  11332. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11333. /* The IOCTL status is embedded in the mailbox subheader. */
  11334. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11335. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11336. if (shdr_status || shdr_add_status || rc) {
  11337. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11338. "2503 WQ_CREATE mailbox failed with "
  11339. "status x%x add_status x%x, mbx status x%x\n",
  11340. shdr_status, shdr_add_status, rc);
  11341. status = -ENXIO;
  11342. goto out;
  11343. }
  11344. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11345. if (wq->queue_id == 0xFFFF) {
  11346. status = -ENXIO;
  11347. goto out;
  11348. }
  11349. wq->type = LPFC_WQ;
  11350. wq->assoc_qid = cq->queue_id;
  11351. wq->subtype = subtype;
  11352. wq->host_index = 0;
  11353. wq->hba_index = 0;
  11354. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11355. /* link the wq onto the parent cq child list */
  11356. list_add_tail(&wq->list, &cq->child_list);
  11357. out:
  11358. mempool_free(mbox, phba->mbox_mem_pool);
  11359. return status;
  11360. }
  11361. /**
  11362. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11363. * @phba: HBA structure that indicates port to create a queue on.
  11364. * @rq: The queue structure to use for the receive queue.
  11365. * @qno: The associated HBQ number
  11366. *
  11367. *
  11368. * For SLI4 we need to adjust the RQ repost value based on
  11369. * the number of buffers that are initially posted to the RQ.
  11370. */
  11371. void
  11372. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11373. {
  11374. uint32_t cnt;
  11375. /* sanity check on queue memory */
  11376. if (!rq)
  11377. return;
  11378. cnt = lpfc_hbq_defs[qno]->entry_count;
  11379. /* Recalc repost for RQs based on buffers initially posted */
  11380. cnt = (cnt >> 3);
  11381. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11382. cnt = LPFC_QUEUE_MIN_REPOST;
  11383. rq->entry_repost = cnt;
  11384. }
  11385. /**
  11386. * lpfc_rq_create - Create a Receive Queue on the HBA
  11387. * @phba: HBA structure that indicates port to create a queue on.
  11388. * @hrq: The queue structure to use to create the header receive queue.
  11389. * @drq: The queue structure to use to create the data receive queue.
  11390. * @cq: The completion queue to bind this work queue to.
  11391. *
  11392. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11393. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11394. * to the HBA.
  11395. *
  11396. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11397. * struct is used to get the entry count that is necessary to determine the
  11398. * number of pages to use for this queue. The @cq is used to indicate which
  11399. * completion queue to bind received buffers that are posted to these queues to.
  11400. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11401. * receive queue pair. This function is asynchronous and will wait for the
  11402. * mailbox command to finish before continuing.
  11403. *
  11404. * On success this function will return a zero. If unable to allocate enough
  11405. * memory this function will return -ENOMEM. If the queue create mailbox command
  11406. * fails this function will return -ENXIO.
  11407. **/
  11408. uint32_t
  11409. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11410. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11411. {
  11412. struct lpfc_mbx_rq_create *rq_create;
  11413. struct lpfc_dmabuf *dmabuf;
  11414. LPFC_MBOXQ_t *mbox;
  11415. int rc, length, status = 0;
  11416. uint32_t shdr_status, shdr_add_status;
  11417. union lpfc_sli4_cfg_shdr *shdr;
  11418. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11419. /* sanity check on queue memory */
  11420. if (!hrq || !drq || !cq)
  11421. return -ENODEV;
  11422. if (!phba->sli4_hba.pc_sli4_params.supported)
  11423. hw_page_size = SLI4_PAGE_SIZE;
  11424. if (hrq->entry_count != drq->entry_count)
  11425. return -EINVAL;
  11426. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11427. if (!mbox)
  11428. return -ENOMEM;
  11429. length = (sizeof(struct lpfc_mbx_rq_create) -
  11430. sizeof(struct lpfc_sli4_cfg_mhdr));
  11431. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11432. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11433. length, LPFC_SLI4_MBX_EMBED);
  11434. rq_create = &mbox->u.mqe.un.rq_create;
  11435. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11436. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11437. phba->sli4_hba.pc_sli4_params.rqv);
  11438. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11439. bf_set(lpfc_rq_context_rqe_count_1,
  11440. &rq_create->u.request.context,
  11441. hrq->entry_count);
  11442. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11443. bf_set(lpfc_rq_context_rqe_size,
  11444. &rq_create->u.request.context,
  11445. LPFC_RQE_SIZE_8);
  11446. bf_set(lpfc_rq_context_page_size,
  11447. &rq_create->u.request.context,
  11448. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11449. } else {
  11450. switch (hrq->entry_count) {
  11451. default:
  11452. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11453. "2535 Unsupported RQ count. (%d)\n",
  11454. hrq->entry_count);
  11455. if (hrq->entry_count < 512)
  11456. return -EINVAL;
  11457. /* otherwise default to smallest count (drop through) */
  11458. case 512:
  11459. bf_set(lpfc_rq_context_rqe_count,
  11460. &rq_create->u.request.context,
  11461. LPFC_RQ_RING_SIZE_512);
  11462. break;
  11463. case 1024:
  11464. bf_set(lpfc_rq_context_rqe_count,
  11465. &rq_create->u.request.context,
  11466. LPFC_RQ_RING_SIZE_1024);
  11467. break;
  11468. case 2048:
  11469. bf_set(lpfc_rq_context_rqe_count,
  11470. &rq_create->u.request.context,
  11471. LPFC_RQ_RING_SIZE_2048);
  11472. break;
  11473. case 4096:
  11474. bf_set(lpfc_rq_context_rqe_count,
  11475. &rq_create->u.request.context,
  11476. LPFC_RQ_RING_SIZE_4096);
  11477. break;
  11478. }
  11479. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11480. LPFC_HDR_BUF_SIZE);
  11481. }
  11482. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11483. cq->queue_id);
  11484. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11485. hrq->page_count);
  11486. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11487. memset(dmabuf->virt, 0, hw_page_size);
  11488. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11489. putPaddrLow(dmabuf->phys);
  11490. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11491. putPaddrHigh(dmabuf->phys);
  11492. }
  11493. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11494. /* The IOCTL status is embedded in the mailbox subheader. */
  11495. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11496. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11497. if (shdr_status || shdr_add_status || rc) {
  11498. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11499. "2504 RQ_CREATE mailbox failed with "
  11500. "status x%x add_status x%x, mbx status x%x\n",
  11501. shdr_status, shdr_add_status, rc);
  11502. status = -ENXIO;
  11503. goto out;
  11504. }
  11505. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11506. if (hrq->queue_id == 0xFFFF) {
  11507. status = -ENXIO;
  11508. goto out;
  11509. }
  11510. hrq->type = LPFC_HRQ;
  11511. hrq->assoc_qid = cq->queue_id;
  11512. hrq->subtype = subtype;
  11513. hrq->host_index = 0;
  11514. hrq->hba_index = 0;
  11515. /* now create the data queue */
  11516. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11517. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11518. length, LPFC_SLI4_MBX_EMBED);
  11519. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11520. phba->sli4_hba.pc_sli4_params.rqv);
  11521. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11522. bf_set(lpfc_rq_context_rqe_count_1,
  11523. &rq_create->u.request.context, hrq->entry_count);
  11524. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11525. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11526. LPFC_RQE_SIZE_8);
  11527. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11528. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11529. } else {
  11530. switch (drq->entry_count) {
  11531. default:
  11532. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11533. "2536 Unsupported RQ count. (%d)\n",
  11534. drq->entry_count);
  11535. if (drq->entry_count < 512)
  11536. return -EINVAL;
  11537. /* otherwise default to smallest count (drop through) */
  11538. case 512:
  11539. bf_set(lpfc_rq_context_rqe_count,
  11540. &rq_create->u.request.context,
  11541. LPFC_RQ_RING_SIZE_512);
  11542. break;
  11543. case 1024:
  11544. bf_set(lpfc_rq_context_rqe_count,
  11545. &rq_create->u.request.context,
  11546. LPFC_RQ_RING_SIZE_1024);
  11547. break;
  11548. case 2048:
  11549. bf_set(lpfc_rq_context_rqe_count,
  11550. &rq_create->u.request.context,
  11551. LPFC_RQ_RING_SIZE_2048);
  11552. break;
  11553. case 4096:
  11554. bf_set(lpfc_rq_context_rqe_count,
  11555. &rq_create->u.request.context,
  11556. LPFC_RQ_RING_SIZE_4096);
  11557. break;
  11558. }
  11559. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11560. LPFC_DATA_BUF_SIZE);
  11561. }
  11562. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11563. cq->queue_id);
  11564. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11565. drq->page_count);
  11566. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11567. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11568. putPaddrLow(dmabuf->phys);
  11569. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11570. putPaddrHigh(dmabuf->phys);
  11571. }
  11572. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11573. /* The IOCTL status is embedded in the mailbox subheader. */
  11574. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11575. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11576. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11577. if (shdr_status || shdr_add_status || rc) {
  11578. status = -ENXIO;
  11579. goto out;
  11580. }
  11581. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11582. if (drq->queue_id == 0xFFFF) {
  11583. status = -ENXIO;
  11584. goto out;
  11585. }
  11586. drq->type = LPFC_DRQ;
  11587. drq->assoc_qid = cq->queue_id;
  11588. drq->subtype = subtype;
  11589. drq->host_index = 0;
  11590. drq->hba_index = 0;
  11591. /* link the header and data RQs onto the parent cq child list */
  11592. list_add_tail(&hrq->list, &cq->child_list);
  11593. list_add_tail(&drq->list, &cq->child_list);
  11594. out:
  11595. mempool_free(mbox, phba->mbox_mem_pool);
  11596. return status;
  11597. }
  11598. /**
  11599. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11600. * @eq: The queue structure associated with the queue to destroy.
  11601. *
  11602. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11603. * command, specific to the type of queue, to the HBA.
  11604. *
  11605. * The @eq struct is used to get the queue ID of the queue to destroy.
  11606. *
  11607. * On success this function will return a zero. If the queue destroy mailbox
  11608. * command fails this function will return -ENXIO.
  11609. **/
  11610. uint32_t
  11611. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11612. {
  11613. LPFC_MBOXQ_t *mbox;
  11614. int rc, length, status = 0;
  11615. uint32_t shdr_status, shdr_add_status;
  11616. union lpfc_sli4_cfg_shdr *shdr;
  11617. /* sanity check on queue memory */
  11618. if (!eq)
  11619. return -ENODEV;
  11620. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11621. if (!mbox)
  11622. return -ENOMEM;
  11623. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11624. sizeof(struct lpfc_sli4_cfg_mhdr));
  11625. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11626. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11627. length, LPFC_SLI4_MBX_EMBED);
  11628. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11629. eq->queue_id);
  11630. mbox->vport = eq->phba->pport;
  11631. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11632. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11633. /* The IOCTL status is embedded in the mailbox subheader. */
  11634. shdr = (union lpfc_sli4_cfg_shdr *)
  11635. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11636. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11637. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11638. if (shdr_status || shdr_add_status || rc) {
  11639. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11640. "2505 EQ_DESTROY mailbox failed with "
  11641. "status x%x add_status x%x, mbx status x%x\n",
  11642. shdr_status, shdr_add_status, rc);
  11643. status = -ENXIO;
  11644. }
  11645. /* Remove eq from any list */
  11646. list_del_init(&eq->list);
  11647. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11648. return status;
  11649. }
  11650. /**
  11651. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11652. * @cq: The queue structure associated with the queue to destroy.
  11653. *
  11654. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11655. * command, specific to the type of queue, to the HBA.
  11656. *
  11657. * The @cq struct is used to get the queue ID of the queue to destroy.
  11658. *
  11659. * On success this function will return a zero. If the queue destroy mailbox
  11660. * command fails this function will return -ENXIO.
  11661. **/
  11662. uint32_t
  11663. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11664. {
  11665. LPFC_MBOXQ_t *mbox;
  11666. int rc, length, status = 0;
  11667. uint32_t shdr_status, shdr_add_status;
  11668. union lpfc_sli4_cfg_shdr *shdr;
  11669. /* sanity check on queue memory */
  11670. if (!cq)
  11671. return -ENODEV;
  11672. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11673. if (!mbox)
  11674. return -ENOMEM;
  11675. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11676. sizeof(struct lpfc_sli4_cfg_mhdr));
  11677. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11678. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11679. length, LPFC_SLI4_MBX_EMBED);
  11680. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11681. cq->queue_id);
  11682. mbox->vport = cq->phba->pport;
  11683. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11684. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11685. /* The IOCTL status is embedded in the mailbox subheader. */
  11686. shdr = (union lpfc_sli4_cfg_shdr *)
  11687. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11688. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11689. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11690. if (shdr_status || shdr_add_status || rc) {
  11691. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11692. "2506 CQ_DESTROY mailbox failed with "
  11693. "status x%x add_status x%x, mbx status x%x\n",
  11694. shdr_status, shdr_add_status, rc);
  11695. status = -ENXIO;
  11696. }
  11697. /* Remove cq from any list */
  11698. list_del_init(&cq->list);
  11699. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11700. return status;
  11701. }
  11702. /**
  11703. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11704. * @qm: The queue structure associated with the queue to destroy.
  11705. *
  11706. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11707. * command, specific to the type of queue, to the HBA.
  11708. *
  11709. * The @mq struct is used to get the queue ID of the queue to destroy.
  11710. *
  11711. * On success this function will return a zero. If the queue destroy mailbox
  11712. * command fails this function will return -ENXIO.
  11713. **/
  11714. uint32_t
  11715. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11716. {
  11717. LPFC_MBOXQ_t *mbox;
  11718. int rc, length, status = 0;
  11719. uint32_t shdr_status, shdr_add_status;
  11720. union lpfc_sli4_cfg_shdr *shdr;
  11721. /* sanity check on queue memory */
  11722. if (!mq)
  11723. return -ENODEV;
  11724. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11725. if (!mbox)
  11726. return -ENOMEM;
  11727. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11728. sizeof(struct lpfc_sli4_cfg_mhdr));
  11729. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11730. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11731. length, LPFC_SLI4_MBX_EMBED);
  11732. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11733. mq->queue_id);
  11734. mbox->vport = mq->phba->pport;
  11735. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11736. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11737. /* The IOCTL status is embedded in the mailbox subheader. */
  11738. shdr = (union lpfc_sli4_cfg_shdr *)
  11739. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11740. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11741. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11742. if (shdr_status || shdr_add_status || rc) {
  11743. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11744. "2507 MQ_DESTROY mailbox failed with "
  11745. "status x%x add_status x%x, mbx status x%x\n",
  11746. shdr_status, shdr_add_status, rc);
  11747. status = -ENXIO;
  11748. }
  11749. /* Remove mq from any list */
  11750. list_del_init(&mq->list);
  11751. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11752. return status;
  11753. }
  11754. /**
  11755. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11756. * @wq: The queue structure associated with the queue to destroy.
  11757. *
  11758. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11759. * command, specific to the type of queue, to the HBA.
  11760. *
  11761. * The @wq struct is used to get the queue ID of the queue to destroy.
  11762. *
  11763. * On success this function will return a zero. If the queue destroy mailbox
  11764. * command fails this function will return -ENXIO.
  11765. **/
  11766. uint32_t
  11767. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11768. {
  11769. LPFC_MBOXQ_t *mbox;
  11770. int rc, length, status = 0;
  11771. uint32_t shdr_status, shdr_add_status;
  11772. union lpfc_sli4_cfg_shdr *shdr;
  11773. /* sanity check on queue memory */
  11774. if (!wq)
  11775. return -ENODEV;
  11776. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11777. if (!mbox)
  11778. return -ENOMEM;
  11779. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11780. sizeof(struct lpfc_sli4_cfg_mhdr));
  11781. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11782. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11783. length, LPFC_SLI4_MBX_EMBED);
  11784. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11785. wq->queue_id);
  11786. mbox->vport = wq->phba->pport;
  11787. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11788. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11789. shdr = (union lpfc_sli4_cfg_shdr *)
  11790. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11791. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11792. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11793. if (shdr_status || shdr_add_status || rc) {
  11794. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11795. "2508 WQ_DESTROY mailbox failed with "
  11796. "status x%x add_status x%x, mbx status x%x\n",
  11797. shdr_status, shdr_add_status, rc);
  11798. status = -ENXIO;
  11799. }
  11800. /* Remove wq from any list */
  11801. list_del_init(&wq->list);
  11802. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11803. return status;
  11804. }
  11805. /**
  11806. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11807. * @rq: The queue structure associated with the queue to destroy.
  11808. *
  11809. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11810. * command, specific to the type of queue, to the HBA.
  11811. *
  11812. * The @rq struct is used to get the queue ID of the queue to destroy.
  11813. *
  11814. * On success this function will return a zero. If the queue destroy mailbox
  11815. * command fails this function will return -ENXIO.
  11816. **/
  11817. uint32_t
  11818. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11819. struct lpfc_queue *drq)
  11820. {
  11821. LPFC_MBOXQ_t *mbox;
  11822. int rc, length, status = 0;
  11823. uint32_t shdr_status, shdr_add_status;
  11824. union lpfc_sli4_cfg_shdr *shdr;
  11825. /* sanity check on queue memory */
  11826. if (!hrq || !drq)
  11827. return -ENODEV;
  11828. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11829. if (!mbox)
  11830. return -ENOMEM;
  11831. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11832. sizeof(struct lpfc_sli4_cfg_mhdr));
  11833. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11834. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11835. length, LPFC_SLI4_MBX_EMBED);
  11836. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11837. hrq->queue_id);
  11838. mbox->vport = hrq->phba->pport;
  11839. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11840. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11841. /* The IOCTL status is embedded in the mailbox subheader. */
  11842. shdr = (union lpfc_sli4_cfg_shdr *)
  11843. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11844. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11845. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11846. if (shdr_status || shdr_add_status || rc) {
  11847. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11848. "2509 RQ_DESTROY mailbox failed with "
  11849. "status x%x add_status x%x, mbx status x%x\n",
  11850. shdr_status, shdr_add_status, rc);
  11851. if (rc != MBX_TIMEOUT)
  11852. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11853. return -ENXIO;
  11854. }
  11855. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11856. drq->queue_id);
  11857. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11858. shdr = (union lpfc_sli4_cfg_shdr *)
  11859. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11860. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11861. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11862. if (shdr_status || shdr_add_status || rc) {
  11863. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11864. "2510 RQ_DESTROY mailbox failed with "
  11865. "status x%x add_status x%x, mbx status x%x\n",
  11866. shdr_status, shdr_add_status, rc);
  11867. status = -ENXIO;
  11868. }
  11869. list_del_init(&hrq->list);
  11870. list_del_init(&drq->list);
  11871. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11872. return status;
  11873. }
  11874. /**
  11875. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  11876. * @phba: The virtual port for which this call being executed.
  11877. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  11878. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  11879. * @xritag: the xritag that ties this io to the SGL pages.
  11880. *
  11881. * This routine will post the sgl pages for the IO that has the xritag
  11882. * that is in the iocbq structure. The xritag is assigned during iocbq
  11883. * creation and persists for as long as the driver is loaded.
  11884. * if the caller has fewer than 256 scatter gather segments to map then
  11885. * pdma_phys_addr1 should be 0.
  11886. * If the caller needs to map more than 256 scatter gather segment then
  11887. * pdma_phys_addr1 should be a valid physical address.
  11888. * physical address for SGLs must be 64 byte aligned.
  11889. * If you are going to map 2 SGL's then the first one must have 256 entries
  11890. * the second sgl can have between 1 and 256 entries.
  11891. *
  11892. * Return codes:
  11893. * 0 - Success
  11894. * -ENXIO, -ENOMEM - Failure
  11895. **/
  11896. int
  11897. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  11898. dma_addr_t pdma_phys_addr0,
  11899. dma_addr_t pdma_phys_addr1,
  11900. uint16_t xritag)
  11901. {
  11902. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  11903. LPFC_MBOXQ_t *mbox;
  11904. int rc;
  11905. uint32_t shdr_status, shdr_add_status;
  11906. uint32_t mbox_tmo;
  11907. union lpfc_sli4_cfg_shdr *shdr;
  11908. if (xritag == NO_XRI) {
  11909. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11910. "0364 Invalid param:\n");
  11911. return -EINVAL;
  11912. }
  11913. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11914. if (!mbox)
  11915. return -ENOMEM;
  11916. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11917. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11918. sizeof(struct lpfc_mbx_post_sgl_pages) -
  11919. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  11920. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  11921. &mbox->u.mqe.un.post_sgl_pages;
  11922. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  11923. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  11924. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  11925. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  11926. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  11927. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  11928. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  11929. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  11930. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  11931. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  11932. if (!phba->sli4_hba.intr_enable)
  11933. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11934. else {
  11935. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  11936. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11937. }
  11938. /* The IOCTL status is embedded in the mailbox subheader. */
  11939. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  11940. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11941. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11942. if (rc != MBX_TIMEOUT)
  11943. mempool_free(mbox, phba->mbox_mem_pool);
  11944. if (shdr_status || shdr_add_status || rc) {
  11945. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11946. "2511 POST_SGL mailbox failed with "
  11947. "status x%x add_status x%x, mbx status x%x\n",
  11948. shdr_status, shdr_add_status, rc);
  11949. rc = -ENXIO;
  11950. }
  11951. return 0;
  11952. }
  11953. /**
  11954. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  11955. * @phba: pointer to lpfc hba data structure.
  11956. *
  11957. * This routine is invoked to post rpi header templates to the
  11958. * HBA consistent with the SLI-4 interface spec. This routine
  11959. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  11960. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  11961. *
  11962. * Returns
  11963. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  11964. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  11965. **/
  11966. uint16_t
  11967. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  11968. {
  11969. unsigned long xri;
  11970. /*
  11971. * Fetch the next logical xri. Because this index is logical,
  11972. * the driver starts at 0 each time.
  11973. */
  11974. spin_lock_irq(&phba->hbalock);
  11975. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  11976. phba->sli4_hba.max_cfg_param.max_xri, 0);
  11977. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  11978. spin_unlock_irq(&phba->hbalock);
  11979. return NO_XRI;
  11980. } else {
  11981. set_bit(xri, phba->sli4_hba.xri_bmask);
  11982. phba->sli4_hba.max_cfg_param.xri_used++;
  11983. phba->sli4_hba.xri_count++;
  11984. }
  11985. spin_unlock_irq(&phba->hbalock);
  11986. return xri;
  11987. }
  11988. /**
  11989. * lpfc_sli4_free_xri - Release an xri for reuse.
  11990. * @phba: pointer to lpfc hba data structure.
  11991. *
  11992. * This routine is invoked to release an xri to the pool of
  11993. * available rpis maintained by the driver.
  11994. **/
  11995. void
  11996. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11997. {
  11998. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  11999. phba->sli4_hba.xri_count--;
  12000. phba->sli4_hba.max_cfg_param.xri_used--;
  12001. }
  12002. }
  12003. /**
  12004. * lpfc_sli4_free_xri - Release an xri for reuse.
  12005. * @phba: pointer to lpfc hba data structure.
  12006. *
  12007. * This routine is invoked to release an xri to the pool of
  12008. * available rpis maintained by the driver.
  12009. **/
  12010. void
  12011. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12012. {
  12013. spin_lock_irq(&phba->hbalock);
  12014. __lpfc_sli4_free_xri(phba, xri);
  12015. spin_unlock_irq(&phba->hbalock);
  12016. }
  12017. /**
  12018. * lpfc_sli4_next_xritag - Get an xritag for the io
  12019. * @phba: Pointer to HBA context object.
  12020. *
  12021. * This function gets an xritag for the iocb. If there is no unused xritag
  12022. * it will return 0xffff.
  12023. * The function returns the allocated xritag if successful, else returns zero.
  12024. * Zero is not a valid xritag.
  12025. * The caller is not required to hold any lock.
  12026. **/
  12027. uint16_t
  12028. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12029. {
  12030. uint16_t xri_index;
  12031. xri_index = lpfc_sli4_alloc_xri(phba);
  12032. if (xri_index != NO_XRI)
  12033. return xri_index;
  12034. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12035. "2004 Failed to allocate XRI.last XRITAG is %d"
  12036. " Max XRI is %d, Used XRI is %d\n",
  12037. xri_index,
  12038. phba->sli4_hba.max_cfg_param.max_xri,
  12039. phba->sli4_hba.max_cfg_param.xri_used);
  12040. return NO_XRI;
  12041. }
  12042. /**
  12043. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12044. * @phba: pointer to lpfc hba data structure.
  12045. *
  12046. * This routine is invoked to post a block of driver's sgl pages to the
  12047. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12048. * is only called when the driver is loading and after all IO has been
  12049. * stopped.
  12050. **/
  12051. int
  12052. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
  12053. {
  12054. struct lpfc_sglq *sglq_entry;
  12055. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12056. struct sgl_page_pairs *sgl_pg_pairs;
  12057. void *viraddr;
  12058. LPFC_MBOXQ_t *mbox;
  12059. uint32_t reqlen, alloclen, pg_pairs;
  12060. uint32_t mbox_tmo;
  12061. uint16_t xritag_start = 0, lxri = 0;
  12062. int els_xri_cnt, rc = 0;
  12063. uint32_t shdr_status, shdr_add_status;
  12064. union lpfc_sli4_cfg_shdr *shdr;
  12065. /* The number of sgls to be posted */
  12066. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12067. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12068. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12069. if (reqlen > SLI4_PAGE_SIZE) {
  12070. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12071. "2559 Block sgl registration required DMA "
  12072. "size (%d) great than a page\n", reqlen);
  12073. return -ENOMEM;
  12074. }
  12075. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12076. if (!mbox)
  12077. return -ENOMEM;
  12078. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12079. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12080. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12081. LPFC_SLI4_MBX_NEMBED);
  12082. if (alloclen < reqlen) {
  12083. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12084. "0285 Allocated DMA memory size (%d) is "
  12085. "less than the requested DMA memory "
  12086. "size (%d)\n", alloclen, reqlen);
  12087. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12088. return -ENOMEM;
  12089. }
  12090. /* Set up the SGL pages in the non-embedded DMA pages */
  12091. viraddr = mbox->sge_array->addr[0];
  12092. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12093. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12094. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  12095. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  12096. /*
  12097. * Assign the sglq a physical xri only if the driver has not
  12098. * initialized those resources. A port reset only needs
  12099. * the sglq's posted.
  12100. */
  12101. if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  12102. LPFC_XRI_RSRC_RDY) {
  12103. lxri = lpfc_sli4_next_xritag(phba);
  12104. if (lxri == NO_XRI) {
  12105. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12106. return -ENOMEM;
  12107. }
  12108. sglq_entry->sli4_lxritag = lxri;
  12109. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  12110. }
  12111. /* Set up the sge entry */
  12112. sgl_pg_pairs->sgl_pg0_addr_lo =
  12113. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12114. sgl_pg_pairs->sgl_pg0_addr_hi =
  12115. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12116. sgl_pg_pairs->sgl_pg1_addr_lo =
  12117. cpu_to_le32(putPaddrLow(0));
  12118. sgl_pg_pairs->sgl_pg1_addr_hi =
  12119. cpu_to_le32(putPaddrHigh(0));
  12120. /* Keep the first xritag on the list */
  12121. if (pg_pairs == 0)
  12122. xritag_start = sglq_entry->sli4_xritag;
  12123. sgl_pg_pairs++;
  12124. }
  12125. /* Complete initialization and perform endian conversion. */
  12126. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12127. bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
  12128. sgl->word0 = cpu_to_le32(sgl->word0);
  12129. if (!phba->sli4_hba.intr_enable)
  12130. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12131. else {
  12132. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12133. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12134. }
  12135. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12136. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12137. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12138. if (rc != MBX_TIMEOUT)
  12139. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12140. if (shdr_status || shdr_add_status || rc) {
  12141. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12142. "2513 POST_SGL_BLOCK mailbox command failed "
  12143. "status x%x add_status x%x mbx status x%x\n",
  12144. shdr_status, shdr_add_status, rc);
  12145. rc = -ENXIO;
  12146. }
  12147. if (rc == 0)
  12148. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12149. LPFC_XRI_RSRC_RDY);
  12150. return rc;
  12151. }
  12152. /**
  12153. * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
  12154. * @phba: pointer to lpfc hba data structure.
  12155. *
  12156. * This routine is invoked to post a block of driver's sgl pages to the
  12157. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12158. * is only called when the driver is loading and after all IO has been
  12159. * stopped.
  12160. **/
  12161. int
  12162. lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
  12163. {
  12164. struct lpfc_sglq *sglq_entry;
  12165. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12166. struct sgl_page_pairs *sgl_pg_pairs;
  12167. void *viraddr;
  12168. LPFC_MBOXQ_t *mbox;
  12169. uint32_t reqlen, alloclen, index;
  12170. uint32_t mbox_tmo;
  12171. uint16_t rsrc_start, rsrc_size, els_xri_cnt;
  12172. uint16_t xritag_start = 0, lxri = 0;
  12173. struct lpfc_rsrc_blks *rsrc_blk;
  12174. int cnt, ttl_cnt, rc = 0;
  12175. int loop_cnt;
  12176. uint32_t shdr_status, shdr_add_status;
  12177. union lpfc_sli4_cfg_shdr *shdr;
  12178. /* The number of sgls to be posted */
  12179. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  12180. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12181. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12182. if (reqlen > SLI4_PAGE_SIZE) {
  12183. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12184. "2989 Block sgl registration required DMA "
  12185. "size (%d) great than a page\n", reqlen);
  12186. return -ENOMEM;
  12187. }
  12188. cnt = 0;
  12189. ttl_cnt = 0;
  12190. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12191. list) {
  12192. rsrc_start = rsrc_blk->rsrc_start;
  12193. rsrc_size = rsrc_blk->rsrc_size;
  12194. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12195. "3014 Working ELS Extent start %d, cnt %d\n",
  12196. rsrc_start, rsrc_size);
  12197. loop_cnt = min(els_xri_cnt, rsrc_size);
  12198. if (ttl_cnt + loop_cnt >= els_xri_cnt) {
  12199. loop_cnt = els_xri_cnt - ttl_cnt;
  12200. ttl_cnt = els_xri_cnt;
  12201. }
  12202. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12203. if (!mbox)
  12204. return -ENOMEM;
  12205. /*
  12206. * Allocate DMA memory and set up the non-embedded mailbox
  12207. * command.
  12208. */
  12209. alloclen = lpfc_sli4_config(phba, mbox,
  12210. LPFC_MBOX_SUBSYSTEM_FCOE,
  12211. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12212. reqlen, LPFC_SLI4_MBX_NEMBED);
  12213. if (alloclen < reqlen) {
  12214. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12215. "2987 Allocated DMA memory size (%d) "
  12216. "is less than the requested DMA memory "
  12217. "size (%d)\n", alloclen, reqlen);
  12218. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12219. return -ENOMEM;
  12220. }
  12221. /* Set up the SGL pages in the non-embedded DMA pages */
  12222. viraddr = mbox->sge_array->addr[0];
  12223. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12224. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12225. /*
  12226. * The starting resource may not begin at zero. Control
  12227. * the loop variants via the block resource parameters,
  12228. * but handle the sge pointers with a zero-based index
  12229. * that doesn't get reset per loop pass.
  12230. */
  12231. for (index = rsrc_start;
  12232. index < rsrc_start + loop_cnt;
  12233. index++) {
  12234. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
  12235. /*
  12236. * Assign the sglq a physical xri only if the driver
  12237. * has not initialized those resources. A port reset
  12238. * only needs the sglq's posted.
  12239. */
  12240. if (bf_get(lpfc_xri_rsrc_rdy,
  12241. &phba->sli4_hba.sli4_flags) !=
  12242. LPFC_XRI_RSRC_RDY) {
  12243. lxri = lpfc_sli4_next_xritag(phba);
  12244. if (lxri == NO_XRI) {
  12245. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12246. rc = -ENOMEM;
  12247. goto err_exit;
  12248. }
  12249. sglq_entry->sli4_lxritag = lxri;
  12250. sglq_entry->sli4_xritag =
  12251. phba->sli4_hba.xri_ids[lxri];
  12252. }
  12253. /* Set up the sge entry */
  12254. sgl_pg_pairs->sgl_pg0_addr_lo =
  12255. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12256. sgl_pg_pairs->sgl_pg0_addr_hi =
  12257. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12258. sgl_pg_pairs->sgl_pg1_addr_lo =
  12259. cpu_to_le32(putPaddrLow(0));
  12260. sgl_pg_pairs->sgl_pg1_addr_hi =
  12261. cpu_to_le32(putPaddrHigh(0));
  12262. /* Track the starting physical XRI for the mailbox. */
  12263. if (index == rsrc_start)
  12264. xritag_start = sglq_entry->sli4_xritag;
  12265. sgl_pg_pairs++;
  12266. cnt++;
  12267. }
  12268. /* Complete initialization and perform endian conversion. */
  12269. rsrc_blk->rsrc_used += loop_cnt;
  12270. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12271. bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
  12272. sgl->word0 = cpu_to_le32(sgl->word0);
  12273. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12274. "3015 Post ELS Extent SGL, start %d, "
  12275. "cnt %d, used %d\n",
  12276. xritag_start, loop_cnt, rsrc_blk->rsrc_used);
  12277. if (!phba->sli4_hba.intr_enable)
  12278. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12279. else {
  12280. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12281. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12282. }
  12283. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12284. shdr_status = bf_get(lpfc_mbox_hdr_status,
  12285. &shdr->response);
  12286. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12287. &shdr->response);
  12288. if (rc != MBX_TIMEOUT)
  12289. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12290. if (shdr_status || shdr_add_status || rc) {
  12291. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12292. "2988 POST_SGL_BLOCK mailbox "
  12293. "command failed status x%x "
  12294. "add_status x%x mbx status x%x\n",
  12295. shdr_status, shdr_add_status, rc);
  12296. rc = -ENXIO;
  12297. goto err_exit;
  12298. }
  12299. if (ttl_cnt >= els_xri_cnt)
  12300. break;
  12301. }
  12302. err_exit:
  12303. if (rc == 0)
  12304. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12305. LPFC_XRI_RSRC_RDY);
  12306. return rc;
  12307. }
  12308. /**
  12309. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12310. * @phba: pointer to lpfc hba data structure.
  12311. * @sblist: pointer to scsi buffer list.
  12312. * @count: number of scsi buffers on the list.
  12313. *
  12314. * This routine is invoked to post a block of @count scsi sgl pages from a
  12315. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12316. * No Lock is held.
  12317. *
  12318. **/
  12319. int
  12320. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  12321. int cnt)
  12322. {
  12323. struct lpfc_scsi_buf *psb;
  12324. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12325. struct sgl_page_pairs *sgl_pg_pairs;
  12326. void *viraddr;
  12327. LPFC_MBOXQ_t *mbox;
  12328. uint32_t reqlen, alloclen, pg_pairs;
  12329. uint32_t mbox_tmo;
  12330. uint16_t xritag_start = 0;
  12331. int rc = 0;
  12332. uint32_t shdr_status, shdr_add_status;
  12333. dma_addr_t pdma_phys_bpl1;
  12334. union lpfc_sli4_cfg_shdr *shdr;
  12335. /* Calculate the requested length of the dma memory */
  12336. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12337. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12338. if (reqlen > SLI4_PAGE_SIZE) {
  12339. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12340. "0217 Block sgl registration required DMA "
  12341. "size (%d) great than a page\n", reqlen);
  12342. return -ENOMEM;
  12343. }
  12344. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12345. if (!mbox) {
  12346. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12347. "0283 Failed to allocate mbox cmd memory\n");
  12348. return -ENOMEM;
  12349. }
  12350. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12351. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12352. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12353. LPFC_SLI4_MBX_NEMBED);
  12354. if (alloclen < reqlen) {
  12355. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12356. "2561 Allocated DMA memory size (%d) is "
  12357. "less than the requested DMA memory "
  12358. "size (%d)\n", alloclen, reqlen);
  12359. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12360. return -ENOMEM;
  12361. }
  12362. /* Get the first SGE entry from the non-embedded DMA memory */
  12363. viraddr = mbox->sge_array->addr[0];
  12364. /* Set up the SGL pages in the non-embedded DMA pages */
  12365. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12366. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12367. pg_pairs = 0;
  12368. list_for_each_entry(psb, sblist, list) {
  12369. /* Set up the sge entry */
  12370. sgl_pg_pairs->sgl_pg0_addr_lo =
  12371. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12372. sgl_pg_pairs->sgl_pg0_addr_hi =
  12373. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12374. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12375. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12376. else
  12377. pdma_phys_bpl1 = 0;
  12378. sgl_pg_pairs->sgl_pg1_addr_lo =
  12379. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12380. sgl_pg_pairs->sgl_pg1_addr_hi =
  12381. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12382. /* Keep the first xritag on the list */
  12383. if (pg_pairs == 0)
  12384. xritag_start = psb->cur_iocbq.sli4_xritag;
  12385. sgl_pg_pairs++;
  12386. pg_pairs++;
  12387. }
  12388. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12389. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12390. /* Perform endian conversion if necessary */
  12391. sgl->word0 = cpu_to_le32(sgl->word0);
  12392. if (!phba->sli4_hba.intr_enable)
  12393. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12394. else {
  12395. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12396. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12397. }
  12398. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12399. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12400. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12401. if (rc != MBX_TIMEOUT)
  12402. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12403. if (shdr_status || shdr_add_status || rc) {
  12404. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12405. "2564 POST_SGL_BLOCK mailbox command failed "
  12406. "status x%x add_status x%x mbx status x%x\n",
  12407. shdr_status, shdr_add_status, rc);
  12408. rc = -ENXIO;
  12409. }
  12410. return rc;
  12411. }
  12412. /**
  12413. * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
  12414. * @phba: pointer to lpfc hba data structure.
  12415. * @sblist: pointer to scsi buffer list.
  12416. * @count: number of scsi buffers on the list.
  12417. *
  12418. * This routine is invoked to post a block of @count scsi sgl pages from a
  12419. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12420. * No Lock is held.
  12421. *
  12422. **/
  12423. int
  12424. lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
  12425. int cnt)
  12426. {
  12427. struct lpfc_scsi_buf *psb = NULL;
  12428. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12429. struct sgl_page_pairs *sgl_pg_pairs;
  12430. void *viraddr;
  12431. LPFC_MBOXQ_t *mbox;
  12432. uint32_t reqlen, alloclen, pg_pairs;
  12433. uint32_t mbox_tmo;
  12434. uint16_t xri_start = 0, scsi_xri_start;
  12435. uint16_t rsrc_range;
  12436. int rc = 0, avail_cnt;
  12437. uint32_t shdr_status, shdr_add_status;
  12438. dma_addr_t pdma_phys_bpl1;
  12439. union lpfc_sli4_cfg_shdr *shdr;
  12440. struct lpfc_rsrc_blks *rsrc_blk;
  12441. uint32_t xri_cnt = 0;
  12442. /* Calculate the total requested length of the dma memory */
  12443. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12444. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12445. if (reqlen > SLI4_PAGE_SIZE) {
  12446. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12447. "2932 Block sgl registration required DMA "
  12448. "size (%d) great than a page\n", reqlen);
  12449. return -ENOMEM;
  12450. }
  12451. /*
  12452. * The use of extents requires the driver to post the sgl headers
  12453. * in multiple postings to meet the contiguous resource assignment.
  12454. */
  12455. psb = list_prepare_entry(psb, sblist, list);
  12456. scsi_xri_start = phba->sli4_hba.scsi_xri_start;
  12457. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12458. list) {
  12459. rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
  12460. if (rsrc_range < scsi_xri_start)
  12461. continue;
  12462. else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
  12463. continue;
  12464. else
  12465. avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
  12466. reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
  12467. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12468. /*
  12469. * Allocate DMA memory and set up the non-embedded mailbox
  12470. * command. The mbox is used to post an SGL page per loop
  12471. * but the DMA memory has a use-once semantic so the mailbox
  12472. * is used and freed per loop pass.
  12473. */
  12474. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12475. if (!mbox) {
  12476. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12477. "2933 Failed to allocate mbox cmd "
  12478. "memory\n");
  12479. return -ENOMEM;
  12480. }
  12481. alloclen = lpfc_sli4_config(phba, mbox,
  12482. LPFC_MBOX_SUBSYSTEM_FCOE,
  12483. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12484. reqlen,
  12485. LPFC_SLI4_MBX_NEMBED);
  12486. if (alloclen < reqlen) {
  12487. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12488. "2934 Allocated DMA memory size (%d) "
  12489. "is less than the requested DMA memory "
  12490. "size (%d)\n", alloclen, reqlen);
  12491. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12492. return -ENOMEM;
  12493. }
  12494. /* Get the first SGE entry from the non-embedded DMA memory */
  12495. viraddr = mbox->sge_array->addr[0];
  12496. /* Set up the SGL pages in the non-embedded DMA pages */
  12497. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12498. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12499. /* pg_pairs tracks posted SGEs per loop iteration. */
  12500. pg_pairs = 0;
  12501. list_for_each_entry_continue(psb, sblist, list) {
  12502. /* Set up the sge entry */
  12503. sgl_pg_pairs->sgl_pg0_addr_lo =
  12504. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12505. sgl_pg_pairs->sgl_pg0_addr_hi =
  12506. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12507. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12508. pdma_phys_bpl1 = psb->dma_phys_bpl +
  12509. SGL_PAGE_SIZE;
  12510. else
  12511. pdma_phys_bpl1 = 0;
  12512. sgl_pg_pairs->sgl_pg1_addr_lo =
  12513. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12514. sgl_pg_pairs->sgl_pg1_addr_hi =
  12515. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12516. /* Keep the first xri for this extent. */
  12517. if (pg_pairs == 0)
  12518. xri_start = psb->cur_iocbq.sli4_xritag;
  12519. sgl_pg_pairs++;
  12520. pg_pairs++;
  12521. xri_cnt++;
  12522. /*
  12523. * Track two exit conditions - the loop has constructed
  12524. * all of the caller's SGE pairs or all available
  12525. * resource IDs in this extent are consumed.
  12526. */
  12527. if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
  12528. break;
  12529. }
  12530. rsrc_blk->rsrc_used += pg_pairs;
  12531. bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
  12532. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12533. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12534. "3016 Post SCSI Extent SGL, start %d, cnt %d "
  12535. "blk use %d\n",
  12536. xri_start, pg_pairs, rsrc_blk->rsrc_used);
  12537. /* Perform endian conversion if necessary */
  12538. sgl->word0 = cpu_to_le32(sgl->word0);
  12539. if (!phba->sli4_hba.intr_enable)
  12540. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12541. else {
  12542. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12543. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12544. }
  12545. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12546. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12547. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12548. &shdr->response);
  12549. if (rc != MBX_TIMEOUT)
  12550. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12551. if (shdr_status || shdr_add_status || rc) {
  12552. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12553. "2935 POST_SGL_BLOCK mailbox command "
  12554. "failed status x%x add_status x%x "
  12555. "mbx status x%x\n",
  12556. shdr_status, shdr_add_status, rc);
  12557. return -ENXIO;
  12558. }
  12559. /* Post only what is requested. */
  12560. if (xri_cnt >= cnt)
  12561. break;
  12562. }
  12563. return rc;
  12564. }
  12565. /**
  12566. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12567. * @phba: pointer to lpfc_hba struct that the frame was received on
  12568. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12569. *
  12570. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12571. * valid type of frame that the LPFC driver will handle. This function will
  12572. * return a zero if the frame is a valid frame or a non zero value when the
  12573. * frame does not pass the check.
  12574. **/
  12575. static int
  12576. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12577. {
  12578. /* make rctl_names static to save stack space */
  12579. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12580. char *type_names[] = FC_TYPE_NAMES_INIT;
  12581. struct fc_vft_header *fc_vft_hdr;
  12582. uint32_t *header = (uint32_t *) fc_hdr;
  12583. switch (fc_hdr->fh_r_ctl) {
  12584. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12585. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12586. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12587. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12588. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12589. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12590. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12591. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12592. case FC_RCTL_ELS_REQ: /* extended link services request */
  12593. case FC_RCTL_ELS_REP: /* extended link services reply */
  12594. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12595. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12596. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12597. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12598. case FC_RCTL_BA_RMC: /* remove connection */
  12599. case FC_RCTL_BA_ACC: /* basic accept */
  12600. case FC_RCTL_BA_RJT: /* basic reject */
  12601. case FC_RCTL_BA_PRMT:
  12602. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12603. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12604. case FC_RCTL_P_RJT: /* port reject */
  12605. case FC_RCTL_F_RJT: /* fabric reject */
  12606. case FC_RCTL_P_BSY: /* port busy */
  12607. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12608. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12609. case FC_RCTL_LCR: /* link credit reset */
  12610. case FC_RCTL_END: /* end */
  12611. break;
  12612. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12613. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12614. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12615. return lpfc_fc_frame_check(phba, fc_hdr);
  12616. default:
  12617. goto drop;
  12618. }
  12619. switch (fc_hdr->fh_type) {
  12620. case FC_TYPE_BLS:
  12621. case FC_TYPE_ELS:
  12622. case FC_TYPE_FCP:
  12623. case FC_TYPE_CT:
  12624. break;
  12625. case FC_TYPE_IP:
  12626. case FC_TYPE_ILS:
  12627. default:
  12628. goto drop;
  12629. }
  12630. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12631. "2538 Received frame rctl:%s type:%s "
  12632. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12633. rctl_names[fc_hdr->fh_r_ctl],
  12634. type_names[fc_hdr->fh_type],
  12635. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12636. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12637. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12638. return 0;
  12639. drop:
  12640. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12641. "2539 Dropped frame rctl:%s type:%s\n",
  12642. rctl_names[fc_hdr->fh_r_ctl],
  12643. type_names[fc_hdr->fh_type]);
  12644. return 1;
  12645. }
  12646. /**
  12647. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12648. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12649. *
  12650. * This function processes the FC header to retrieve the VFI from the VF
  12651. * header, if one exists. This function will return the VFI if one exists
  12652. * or 0 if no VSAN Header exists.
  12653. **/
  12654. static uint32_t
  12655. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12656. {
  12657. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12658. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12659. return 0;
  12660. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12661. }
  12662. /**
  12663. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12664. * @phba: Pointer to the HBA structure to search for the vport on
  12665. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12666. * @fcfi: The FC Fabric ID that the frame came from
  12667. *
  12668. * This function searches the @phba for a vport that matches the content of the
  12669. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12670. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12671. * returns the matching vport pointer or NULL if unable to match frame to a
  12672. * vport.
  12673. **/
  12674. static struct lpfc_vport *
  12675. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12676. uint16_t fcfi)
  12677. {
  12678. struct lpfc_vport **vports;
  12679. struct lpfc_vport *vport = NULL;
  12680. int i;
  12681. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12682. fc_hdr->fh_d_id[1] << 8 |
  12683. fc_hdr->fh_d_id[2]);
  12684. if (did == Fabric_DID)
  12685. return phba->pport;
  12686. vports = lpfc_create_vport_work_array(phba);
  12687. if (vports != NULL)
  12688. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12689. if (phba->fcf.fcfi == fcfi &&
  12690. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12691. vports[i]->fc_myDID == did) {
  12692. vport = vports[i];
  12693. break;
  12694. }
  12695. }
  12696. lpfc_destroy_vport_work_array(phba, vports);
  12697. return vport;
  12698. }
  12699. /**
  12700. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12701. * @vport: The vport to work on.
  12702. *
  12703. * This function updates the receive sequence time stamp for this vport. The
  12704. * receive sequence time stamp indicates the time that the last frame of the
  12705. * the sequence that has been idle for the longest amount of time was received.
  12706. * the driver uses this time stamp to indicate if any received sequences have
  12707. * timed out.
  12708. **/
  12709. void
  12710. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12711. {
  12712. struct lpfc_dmabuf *h_buf;
  12713. struct hbq_dmabuf *dmabuf = NULL;
  12714. /* get the oldest sequence on the rcv list */
  12715. h_buf = list_get_first(&vport->rcv_buffer_list,
  12716. struct lpfc_dmabuf, list);
  12717. if (!h_buf)
  12718. return;
  12719. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12720. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12721. }
  12722. /**
  12723. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12724. * @vport: The vport that the received sequences were sent to.
  12725. *
  12726. * This function cleans up all outstanding received sequences. This is called
  12727. * by the driver when a link event or user action invalidates all the received
  12728. * sequences.
  12729. **/
  12730. void
  12731. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12732. {
  12733. struct lpfc_dmabuf *h_buf, *hnext;
  12734. struct lpfc_dmabuf *d_buf, *dnext;
  12735. struct hbq_dmabuf *dmabuf = NULL;
  12736. /* start with the oldest sequence on the rcv list */
  12737. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12738. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12739. list_del_init(&dmabuf->hbuf.list);
  12740. list_for_each_entry_safe(d_buf, dnext,
  12741. &dmabuf->dbuf.list, list) {
  12742. list_del_init(&d_buf->list);
  12743. lpfc_in_buf_free(vport->phba, d_buf);
  12744. }
  12745. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12746. }
  12747. }
  12748. /**
  12749. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12750. * @vport: The vport that the received sequences were sent to.
  12751. *
  12752. * This function determines whether any received sequences have timed out by
  12753. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12754. * indicates that there is at least one timed out sequence this routine will
  12755. * go through the received sequences one at a time from most inactive to most
  12756. * active to determine which ones need to be cleaned up. Once it has determined
  12757. * that a sequence needs to be cleaned up it will simply free up the resources
  12758. * without sending an abort.
  12759. **/
  12760. void
  12761. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12762. {
  12763. struct lpfc_dmabuf *h_buf, *hnext;
  12764. struct lpfc_dmabuf *d_buf, *dnext;
  12765. struct hbq_dmabuf *dmabuf = NULL;
  12766. unsigned long timeout;
  12767. int abort_count = 0;
  12768. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12769. vport->rcv_buffer_time_stamp);
  12770. if (list_empty(&vport->rcv_buffer_list) ||
  12771. time_before(jiffies, timeout))
  12772. return;
  12773. /* start with the oldest sequence on the rcv list */
  12774. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12775. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12776. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12777. dmabuf->time_stamp);
  12778. if (time_before(jiffies, timeout))
  12779. break;
  12780. abort_count++;
  12781. list_del_init(&dmabuf->hbuf.list);
  12782. list_for_each_entry_safe(d_buf, dnext,
  12783. &dmabuf->dbuf.list, list) {
  12784. list_del_init(&d_buf->list);
  12785. lpfc_in_buf_free(vport->phba, d_buf);
  12786. }
  12787. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12788. }
  12789. if (abort_count)
  12790. lpfc_update_rcv_time_stamp(vport);
  12791. }
  12792. /**
  12793. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12794. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12795. *
  12796. * This function searches through the existing incomplete sequences that have
  12797. * been sent to this @vport. If the frame matches one of the incomplete
  12798. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12799. * make up that sequence. If no sequence is found that matches this frame then
  12800. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12801. * This function returns a pointer to the first dmabuf in the sequence list that
  12802. * the frame was linked to.
  12803. **/
  12804. static struct hbq_dmabuf *
  12805. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12806. {
  12807. struct fc_frame_header *new_hdr;
  12808. struct fc_frame_header *temp_hdr;
  12809. struct lpfc_dmabuf *d_buf;
  12810. struct lpfc_dmabuf *h_buf;
  12811. struct hbq_dmabuf *seq_dmabuf = NULL;
  12812. struct hbq_dmabuf *temp_dmabuf = NULL;
  12813. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12814. dmabuf->time_stamp = jiffies;
  12815. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12816. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12817. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12818. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12819. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12820. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12821. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12822. continue;
  12823. /* found a pending sequence that matches this frame */
  12824. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12825. break;
  12826. }
  12827. if (!seq_dmabuf) {
  12828. /*
  12829. * This indicates first frame received for this sequence.
  12830. * Queue the buffer on the vport's rcv_buffer_list.
  12831. */
  12832. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12833. lpfc_update_rcv_time_stamp(vport);
  12834. return dmabuf;
  12835. }
  12836. temp_hdr = seq_dmabuf->hbuf.virt;
  12837. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12838. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12839. list_del_init(&seq_dmabuf->hbuf.list);
  12840. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12841. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12842. lpfc_update_rcv_time_stamp(vport);
  12843. return dmabuf;
  12844. }
  12845. /* move this sequence to the tail to indicate a young sequence */
  12846. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12847. seq_dmabuf->time_stamp = jiffies;
  12848. lpfc_update_rcv_time_stamp(vport);
  12849. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12850. temp_hdr = dmabuf->hbuf.virt;
  12851. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12852. return seq_dmabuf;
  12853. }
  12854. /* find the correct place in the sequence to insert this frame */
  12855. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12856. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12857. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12858. /*
  12859. * If the frame's sequence count is greater than the frame on
  12860. * the list then insert the frame right after this frame
  12861. */
  12862. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12863. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12864. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12865. return seq_dmabuf;
  12866. }
  12867. }
  12868. return NULL;
  12869. }
  12870. /**
  12871. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12872. * @vport: pointer to a vitural port
  12873. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12874. *
  12875. * This function tries to abort from the partially assembed sequence, described
  12876. * by the information from basic abbort @dmabuf. It checks to see whether such
  12877. * partially assembled sequence held by the driver. If so, it shall free up all
  12878. * the frames from the partially assembled sequence.
  12879. *
  12880. * Return
  12881. * true -- if there is matching partially assembled sequence present and all
  12882. * the frames freed with the sequence;
  12883. * false -- if there is no matching partially assembled sequence present so
  12884. * nothing got aborted in the lower layer driver
  12885. **/
  12886. static bool
  12887. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12888. struct hbq_dmabuf *dmabuf)
  12889. {
  12890. struct fc_frame_header *new_hdr;
  12891. struct fc_frame_header *temp_hdr;
  12892. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12893. struct hbq_dmabuf *seq_dmabuf = NULL;
  12894. /* Use the hdr_buf to find the sequence that matches this frame */
  12895. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12896. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12897. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12898. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12899. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12900. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12901. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12902. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12903. continue;
  12904. /* found a pending sequence that matches this frame */
  12905. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12906. break;
  12907. }
  12908. /* Free up all the frames from the partially assembled sequence */
  12909. if (seq_dmabuf) {
  12910. list_for_each_entry_safe(d_buf, n_buf,
  12911. &seq_dmabuf->dbuf.list, list) {
  12912. list_del_init(&d_buf->list);
  12913. lpfc_in_buf_free(vport->phba, d_buf);
  12914. }
  12915. return true;
  12916. }
  12917. return false;
  12918. }
  12919. /**
  12920. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12921. * @phba: Pointer to HBA context object.
  12922. * @cmd_iocbq: pointer to the command iocbq structure.
  12923. * @rsp_iocbq: pointer to the response iocbq structure.
  12924. *
  12925. * This function handles the sequence abort response iocb command complete
  12926. * event. It properly releases the memory allocated to the sequence abort
  12927. * accept iocb.
  12928. **/
  12929. static void
  12930. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12931. struct lpfc_iocbq *cmd_iocbq,
  12932. struct lpfc_iocbq *rsp_iocbq)
  12933. {
  12934. if (cmd_iocbq)
  12935. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12936. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12937. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12938. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12939. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12940. rsp_iocbq->iocb.ulpStatus,
  12941. rsp_iocbq->iocb.un.ulpWord[4]);
  12942. }
  12943. /**
  12944. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12945. * @phba: Pointer to HBA context object.
  12946. * @xri: xri id in transaction.
  12947. *
  12948. * This function validates the xri maps to the known range of XRIs allocated an
  12949. * used by the driver.
  12950. **/
  12951. uint16_t
  12952. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12953. uint16_t xri)
  12954. {
  12955. int i;
  12956. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12957. if (xri == phba->sli4_hba.xri_ids[i])
  12958. return i;
  12959. }
  12960. return NO_XRI;
  12961. }
  12962. /**
  12963. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12964. * @phba: Pointer to HBA context object.
  12965. * @fc_hdr: pointer to a FC frame header.
  12966. *
  12967. * This function sends a basic response to a previous unsol sequence abort
  12968. * event after aborting the sequence handling.
  12969. **/
  12970. static void
  12971. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12972. struct fc_frame_header *fc_hdr)
  12973. {
  12974. struct lpfc_iocbq *ctiocb = NULL;
  12975. struct lpfc_nodelist *ndlp;
  12976. uint16_t oxid, rxid;
  12977. uint32_t sid, fctl;
  12978. IOCB_t *icmd;
  12979. int rc;
  12980. if (!lpfc_is_link_up(phba))
  12981. return;
  12982. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12983. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12984. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12985. ndlp = lpfc_findnode_did(phba->pport, sid);
  12986. if (!ndlp) {
  12987. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12988. "1268 Find ndlp returned NULL for oxid:x%x "
  12989. "SID:x%x\n", oxid, sid);
  12990. return;
  12991. }
  12992. if (lpfc_sli4_xri_inrange(phba, rxid))
  12993. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12994. /* Allocate buffer for rsp iocb */
  12995. ctiocb = lpfc_sli_get_iocbq(phba);
  12996. if (!ctiocb)
  12997. return;
  12998. /* Extract the F_CTL field from FC_HDR */
  12999. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  13000. icmd = &ctiocb->iocb;
  13001. icmd->un.xseq64.bdl.bdeSize = 0;
  13002. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  13003. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13004. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13005. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13006. /* Fill in the rest of iocb fields */
  13007. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13008. icmd->ulpBdeCount = 0;
  13009. icmd->ulpLe = 1;
  13010. icmd->ulpClass = CLASS3;
  13011. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13012. ctiocb->context1 = ndlp;
  13013. ctiocb->iocb_cmpl = NULL;
  13014. ctiocb->vport = phba->pport;
  13015. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  13016. ctiocb->sli4_lxritag = NO_XRI;
  13017. ctiocb->sli4_xritag = NO_XRI;
  13018. /* If the oxid maps to the FCP XRI range or if it is out of range,
  13019. * send a BLS_RJT. The driver no longer has that exchange.
  13020. * Override the IOCB for a BA_RJT.
  13021. */
  13022. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  13023. phba->sli4_hba.max_cfg_param.xri_base) ||
  13024. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  13025. phba->sli4_hba.max_cfg_param.xri_base)) {
  13026. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13027. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13028. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13029. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13030. }
  13031. if (fctl & FC_FC_EX_CTX) {
  13032. /* ABTS sent by responder to CT exchange, construction
  13033. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13034. * field and RX_ID from ABTS for RX_ID field.
  13035. */
  13036. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13037. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13038. } else {
  13039. /* ABTS sent by initiator to CT exchange, construction
  13040. * of BA_ACC will need to allocate a new XRI as for the
  13041. * XRI_TAG and RX_ID fields.
  13042. */
  13043. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13044. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
  13045. }
  13046. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13047. /* Xmit CT abts response on exchange <xid> */
  13048. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13049. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13050. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13051. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13052. if (rc == IOCB_ERROR) {
  13053. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  13054. "2925 Failed to issue CT ABTS RSP x%x on "
  13055. "xri x%x, Data x%x\n",
  13056. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13057. phba->link_state);
  13058. lpfc_sli_release_iocbq(phba, ctiocb);
  13059. }
  13060. }
  13061. /**
  13062. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13063. * @vport: Pointer to the vport on which this sequence was received
  13064. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13065. *
  13066. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13067. * receive sequence is only partially assembed by the driver, it shall abort
  13068. * the partially assembled frames for the sequence. Otherwise, if the
  13069. * unsolicited receive sequence has been completely assembled and passed to
  13070. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13071. * unsolicited sequence has been aborted. After that, it will issue a basic
  13072. * accept to accept the abort.
  13073. **/
  13074. void
  13075. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13076. struct hbq_dmabuf *dmabuf)
  13077. {
  13078. struct lpfc_hba *phba = vport->phba;
  13079. struct fc_frame_header fc_hdr;
  13080. uint32_t fctl;
  13081. bool abts_par;
  13082. /* Make a copy of fc_hdr before the dmabuf being released */
  13083. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13084. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13085. if (fctl & FC_FC_EX_CTX) {
  13086. /*
  13087. * ABTS sent by responder to exchange, just free the buffer
  13088. */
  13089. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13090. } else {
  13091. /*
  13092. * ABTS sent by initiator to exchange, need to do cleanup
  13093. */
  13094. /* Try to abort partially assembled seq */
  13095. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13096. /* Send abort to ULP if partially seq abort failed */
  13097. if (abts_par == false)
  13098. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  13099. else
  13100. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13101. }
  13102. /* Send basic accept (BA_ACC) to the abort requester */
  13103. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  13104. }
  13105. /**
  13106. * lpfc_seq_complete - Indicates if a sequence is complete
  13107. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13108. *
  13109. * This function checks the sequence, starting with the frame described by
  13110. * @dmabuf, to see if all the frames associated with this sequence are present.
  13111. * the frames associated with this sequence are linked to the @dmabuf using the
  13112. * dbuf list. This function looks for two major things. 1) That the first frame
  13113. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13114. * set. 3) That there are no holes in the sequence count. The function will
  13115. * return 1 when the sequence is complete, otherwise it will return 0.
  13116. **/
  13117. static int
  13118. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13119. {
  13120. struct fc_frame_header *hdr;
  13121. struct lpfc_dmabuf *d_buf;
  13122. struct hbq_dmabuf *seq_dmabuf;
  13123. uint32_t fctl;
  13124. int seq_count = 0;
  13125. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13126. /* make sure first fame of sequence has a sequence count of zero */
  13127. if (hdr->fh_seq_cnt != seq_count)
  13128. return 0;
  13129. fctl = (hdr->fh_f_ctl[0] << 16 |
  13130. hdr->fh_f_ctl[1] << 8 |
  13131. hdr->fh_f_ctl[2]);
  13132. /* If last frame of sequence we can return success. */
  13133. if (fctl & FC_FC_END_SEQ)
  13134. return 1;
  13135. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13136. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13137. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13138. /* If there is a hole in the sequence count then fail. */
  13139. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13140. return 0;
  13141. fctl = (hdr->fh_f_ctl[0] << 16 |
  13142. hdr->fh_f_ctl[1] << 8 |
  13143. hdr->fh_f_ctl[2]);
  13144. /* If last frame of sequence we can return success. */
  13145. if (fctl & FC_FC_END_SEQ)
  13146. return 1;
  13147. }
  13148. return 0;
  13149. }
  13150. /**
  13151. * lpfc_prep_seq - Prep sequence for ULP processing
  13152. * @vport: Pointer to the vport on which this sequence was received
  13153. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13154. *
  13155. * This function takes a sequence, described by a list of frames, and creates
  13156. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13157. * used to issue to the generic unsolicited sequence handler. This routine
  13158. * returns a pointer to the first iocbq in the list. If the function is unable
  13159. * to allocate an iocbq then it throw out the received frames that were not
  13160. * able to be described and return a pointer to the first iocbq. If unable to
  13161. * allocate any iocbqs (including the first) this function will return NULL.
  13162. **/
  13163. static struct lpfc_iocbq *
  13164. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13165. {
  13166. struct hbq_dmabuf *hbq_buf;
  13167. struct lpfc_dmabuf *d_buf, *n_buf;
  13168. struct lpfc_iocbq *first_iocbq, *iocbq;
  13169. struct fc_frame_header *fc_hdr;
  13170. uint32_t sid;
  13171. uint32_t len, tot_len;
  13172. struct ulp_bde64 *pbde;
  13173. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13174. /* remove from receive buffer list */
  13175. list_del_init(&seq_dmabuf->hbuf.list);
  13176. lpfc_update_rcv_time_stamp(vport);
  13177. /* get the Remote Port's SID */
  13178. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13179. tot_len = 0;
  13180. /* Get an iocbq struct to fill in. */
  13181. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13182. if (first_iocbq) {
  13183. /* Initialize the first IOCB. */
  13184. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13185. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13186. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13187. first_iocbq->iocb.ulpContext = NO_XRI;
  13188. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13189. be16_to_cpu(fc_hdr->fh_ox_id);
  13190. /* iocbq is prepped for internal consumption. Physical vpi. */
  13191. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13192. vport->phba->vpi_ids[vport->vpi];
  13193. /* put the first buffer into the first IOCBq */
  13194. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13195. first_iocbq->context3 = NULL;
  13196. first_iocbq->iocb.ulpBdeCount = 1;
  13197. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13198. LPFC_DATA_BUF_SIZE;
  13199. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13200. tot_len = bf_get(lpfc_rcqe_length,
  13201. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13202. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13203. }
  13204. iocbq = first_iocbq;
  13205. /*
  13206. * Each IOCBq can have two Buffers assigned, so go through the list
  13207. * of buffers for this sequence and save two buffers in each IOCBq
  13208. */
  13209. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13210. if (!iocbq) {
  13211. lpfc_in_buf_free(vport->phba, d_buf);
  13212. continue;
  13213. }
  13214. if (!iocbq->context3) {
  13215. iocbq->context3 = d_buf;
  13216. iocbq->iocb.ulpBdeCount++;
  13217. pbde = (struct ulp_bde64 *)
  13218. &iocbq->iocb.unsli3.sli3Words[4];
  13219. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13220. /* We need to get the size out of the right CQE */
  13221. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13222. len = bf_get(lpfc_rcqe_length,
  13223. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13224. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13225. tot_len += len;
  13226. } else {
  13227. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13228. if (!iocbq) {
  13229. if (first_iocbq) {
  13230. first_iocbq->iocb.ulpStatus =
  13231. IOSTAT_FCP_RSP_ERROR;
  13232. first_iocbq->iocb.un.ulpWord[4] =
  13233. IOERR_NO_RESOURCES;
  13234. }
  13235. lpfc_in_buf_free(vport->phba, d_buf);
  13236. continue;
  13237. }
  13238. iocbq->context2 = d_buf;
  13239. iocbq->context3 = NULL;
  13240. iocbq->iocb.ulpBdeCount = 1;
  13241. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13242. LPFC_DATA_BUF_SIZE;
  13243. /* We need to get the size out of the right CQE */
  13244. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13245. len = bf_get(lpfc_rcqe_length,
  13246. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13247. tot_len += len;
  13248. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13249. iocbq->iocb.un.rcvels.remoteID = sid;
  13250. list_add_tail(&iocbq->list, &first_iocbq->list);
  13251. }
  13252. }
  13253. return first_iocbq;
  13254. }
  13255. static void
  13256. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13257. struct hbq_dmabuf *seq_dmabuf)
  13258. {
  13259. struct fc_frame_header *fc_hdr;
  13260. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13261. struct lpfc_hba *phba = vport->phba;
  13262. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13263. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13264. if (!iocbq) {
  13265. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13266. "2707 Ring %d handler: Failed to allocate "
  13267. "iocb Rctl x%x Type x%x received\n",
  13268. LPFC_ELS_RING,
  13269. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13270. return;
  13271. }
  13272. if (!lpfc_complete_unsol_iocb(phba,
  13273. &phba->sli.ring[LPFC_ELS_RING],
  13274. iocbq, fc_hdr->fh_r_ctl,
  13275. fc_hdr->fh_type))
  13276. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13277. "2540 Ring %d handler: unexpected Rctl "
  13278. "x%x Type x%x received\n",
  13279. LPFC_ELS_RING,
  13280. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13281. /* Free iocb created in lpfc_prep_seq */
  13282. list_for_each_entry_safe(curr_iocb, next_iocb,
  13283. &iocbq->list, list) {
  13284. list_del_init(&curr_iocb->list);
  13285. lpfc_sli_release_iocbq(phba, curr_iocb);
  13286. }
  13287. lpfc_sli_release_iocbq(phba, iocbq);
  13288. }
  13289. /**
  13290. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13291. * @phba: Pointer to HBA context object.
  13292. *
  13293. * This function is called with no lock held. This function processes all
  13294. * the received buffers and gives it to upper layers when a received buffer
  13295. * indicates that it is the final frame in the sequence. The interrupt
  13296. * service routine processes received buffers at interrupt contexts and adds
  13297. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13298. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13299. * appropriate receive function when the final frame in a sequence is received.
  13300. **/
  13301. void
  13302. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13303. struct hbq_dmabuf *dmabuf)
  13304. {
  13305. struct hbq_dmabuf *seq_dmabuf;
  13306. struct fc_frame_header *fc_hdr;
  13307. struct lpfc_vport *vport;
  13308. uint32_t fcfi;
  13309. /* Process each received buffer */
  13310. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13311. /* check to see if this a valid type of frame */
  13312. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13313. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13314. return;
  13315. }
  13316. if ((bf_get(lpfc_cqe_code,
  13317. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13318. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13319. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13320. else
  13321. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13322. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13323. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13324. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13325. /* throw out the frame */
  13326. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13327. return;
  13328. }
  13329. /* Handle the basic abort sequence (BA_ABTS) event */
  13330. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13331. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13332. return;
  13333. }
  13334. /* Link this frame */
  13335. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13336. if (!seq_dmabuf) {
  13337. /* unable to add frame to vport - throw it out */
  13338. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13339. return;
  13340. }
  13341. /* If not last frame in sequence continue processing frames. */
  13342. if (!lpfc_seq_complete(seq_dmabuf))
  13343. return;
  13344. /* Send the complete sequence to the upper layer protocol */
  13345. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13346. }
  13347. /**
  13348. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13349. * @phba: pointer to lpfc hba data structure.
  13350. *
  13351. * This routine is invoked to post rpi header templates to the
  13352. * HBA consistent with the SLI-4 interface spec. This routine
  13353. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13354. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13355. *
  13356. * This routine does not require any locks. It's usage is expected
  13357. * to be driver load or reset recovery when the driver is
  13358. * sequential.
  13359. *
  13360. * Return codes
  13361. * 0 - successful
  13362. * -EIO - The mailbox failed to complete successfully.
  13363. * When this error occurs, the driver is not guaranteed
  13364. * to have any rpi regions posted to the device and
  13365. * must either attempt to repost the regions or take a
  13366. * fatal error.
  13367. **/
  13368. int
  13369. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13370. {
  13371. struct lpfc_rpi_hdr *rpi_page;
  13372. uint32_t rc = 0;
  13373. uint16_t lrpi = 0;
  13374. /* SLI4 ports that support extents do not require RPI headers. */
  13375. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13376. goto exit;
  13377. if (phba->sli4_hba.extents_in_use)
  13378. return -EIO;
  13379. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13380. /*
  13381. * Assign the rpi headers a physical rpi only if the driver
  13382. * has not initialized those resources. A port reset only
  13383. * needs the headers posted.
  13384. */
  13385. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13386. LPFC_RPI_RSRC_RDY)
  13387. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13388. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13389. if (rc != MBX_SUCCESS) {
  13390. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13391. "2008 Error %d posting all rpi "
  13392. "headers\n", rc);
  13393. rc = -EIO;
  13394. break;
  13395. }
  13396. }
  13397. exit:
  13398. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13399. LPFC_RPI_RSRC_RDY);
  13400. return rc;
  13401. }
  13402. /**
  13403. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13404. * @phba: pointer to lpfc hba data structure.
  13405. * @rpi_page: pointer to the rpi memory region.
  13406. *
  13407. * This routine is invoked to post a single rpi header to the
  13408. * HBA consistent with the SLI-4 interface spec. This memory region
  13409. * maps up to 64 rpi context regions.
  13410. *
  13411. * Return codes
  13412. * 0 - successful
  13413. * -ENOMEM - No available memory
  13414. * -EIO - The mailbox failed to complete successfully.
  13415. **/
  13416. int
  13417. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13418. {
  13419. LPFC_MBOXQ_t *mboxq;
  13420. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13421. uint32_t rc = 0;
  13422. uint32_t shdr_status, shdr_add_status;
  13423. union lpfc_sli4_cfg_shdr *shdr;
  13424. /* SLI4 ports that support extents do not require RPI headers. */
  13425. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13426. return rc;
  13427. if (phba->sli4_hba.extents_in_use)
  13428. return -EIO;
  13429. /* The port is notified of the header region via a mailbox command. */
  13430. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13431. if (!mboxq) {
  13432. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13433. "2001 Unable to allocate memory for issuing "
  13434. "SLI_CONFIG_SPECIAL mailbox command\n");
  13435. return -ENOMEM;
  13436. }
  13437. /* Post all rpi memory regions to the port. */
  13438. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13439. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13440. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13441. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13442. sizeof(struct lpfc_sli4_cfg_mhdr),
  13443. LPFC_SLI4_MBX_EMBED);
  13444. /* Post the physical rpi to the port for this rpi header. */
  13445. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13446. rpi_page->start_rpi);
  13447. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13448. hdr_tmpl, rpi_page->page_count);
  13449. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13450. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13451. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13452. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13453. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13454. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13455. if (rc != MBX_TIMEOUT)
  13456. mempool_free(mboxq, phba->mbox_mem_pool);
  13457. if (shdr_status || shdr_add_status || rc) {
  13458. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13459. "2514 POST_RPI_HDR mailbox failed with "
  13460. "status x%x add_status x%x, mbx status x%x\n",
  13461. shdr_status, shdr_add_status, rc);
  13462. rc = -ENXIO;
  13463. }
  13464. return rc;
  13465. }
  13466. /**
  13467. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13468. * @phba: pointer to lpfc hba data structure.
  13469. *
  13470. * This routine is invoked to post rpi header templates to the
  13471. * HBA consistent with the SLI-4 interface spec. This routine
  13472. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13473. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13474. *
  13475. * Returns
  13476. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13477. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13478. **/
  13479. int
  13480. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13481. {
  13482. unsigned long rpi;
  13483. uint16_t max_rpi, rpi_limit;
  13484. uint16_t rpi_remaining, lrpi = 0;
  13485. struct lpfc_rpi_hdr *rpi_hdr;
  13486. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13487. rpi_limit = phba->sli4_hba.next_rpi;
  13488. /*
  13489. * Fetch the next logical rpi. Because this index is logical,
  13490. * the driver starts at 0 each time.
  13491. */
  13492. spin_lock_irq(&phba->hbalock);
  13493. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13494. if (rpi >= rpi_limit)
  13495. rpi = LPFC_RPI_ALLOC_ERROR;
  13496. else {
  13497. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13498. phba->sli4_hba.max_cfg_param.rpi_used++;
  13499. phba->sli4_hba.rpi_count++;
  13500. }
  13501. /*
  13502. * Don't try to allocate more rpi header regions if the device limit
  13503. * has been exhausted.
  13504. */
  13505. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13506. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13507. spin_unlock_irq(&phba->hbalock);
  13508. return rpi;
  13509. }
  13510. /*
  13511. * RPI header postings are not required for SLI4 ports capable of
  13512. * extents.
  13513. */
  13514. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13515. spin_unlock_irq(&phba->hbalock);
  13516. return rpi;
  13517. }
  13518. /*
  13519. * If the driver is running low on rpi resources, allocate another
  13520. * page now. Note that the next_rpi value is used because
  13521. * it represents how many are actually in use whereas max_rpi notes
  13522. * how many are supported max by the device.
  13523. */
  13524. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13525. spin_unlock_irq(&phba->hbalock);
  13526. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13527. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13528. if (!rpi_hdr) {
  13529. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13530. "2002 Error Could not grow rpi "
  13531. "count\n");
  13532. } else {
  13533. lrpi = rpi_hdr->start_rpi;
  13534. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13535. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13536. }
  13537. }
  13538. return rpi;
  13539. }
  13540. /**
  13541. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13542. * @phba: pointer to lpfc hba data structure.
  13543. *
  13544. * This routine is invoked to release an rpi to the pool of
  13545. * available rpis maintained by the driver.
  13546. **/
  13547. void
  13548. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13549. {
  13550. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13551. phba->sli4_hba.rpi_count--;
  13552. phba->sli4_hba.max_cfg_param.rpi_used--;
  13553. }
  13554. }
  13555. /**
  13556. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13557. * @phba: pointer to lpfc hba data structure.
  13558. *
  13559. * This routine is invoked to release an rpi to the pool of
  13560. * available rpis maintained by the driver.
  13561. **/
  13562. void
  13563. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13564. {
  13565. spin_lock_irq(&phba->hbalock);
  13566. __lpfc_sli4_free_rpi(phba, rpi);
  13567. spin_unlock_irq(&phba->hbalock);
  13568. }
  13569. /**
  13570. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13571. * @phba: pointer to lpfc hba data structure.
  13572. *
  13573. * This routine is invoked to remove the memory region that
  13574. * provided rpi via a bitmask.
  13575. **/
  13576. void
  13577. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13578. {
  13579. kfree(phba->sli4_hba.rpi_bmask);
  13580. kfree(phba->sli4_hba.rpi_ids);
  13581. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13582. }
  13583. /**
  13584. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13585. * @phba: pointer to lpfc hba data structure.
  13586. *
  13587. * This routine is invoked to remove the memory region that
  13588. * provided rpi via a bitmask.
  13589. **/
  13590. int
  13591. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13592. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13593. {
  13594. LPFC_MBOXQ_t *mboxq;
  13595. struct lpfc_hba *phba = ndlp->phba;
  13596. int rc;
  13597. /* The port is notified of the header region via a mailbox command. */
  13598. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13599. if (!mboxq)
  13600. return -ENOMEM;
  13601. /* Post all rpi memory regions to the port. */
  13602. lpfc_resume_rpi(mboxq, ndlp);
  13603. if (cmpl) {
  13604. mboxq->mbox_cmpl = cmpl;
  13605. mboxq->context1 = arg;
  13606. mboxq->context2 = ndlp;
  13607. } else
  13608. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13609. mboxq->vport = ndlp->vport;
  13610. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13611. if (rc == MBX_NOT_FINISHED) {
  13612. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13613. "2010 Resume RPI Mailbox failed "
  13614. "status %d, mbxStatus x%x\n", rc,
  13615. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13616. mempool_free(mboxq, phba->mbox_mem_pool);
  13617. return -EIO;
  13618. }
  13619. return 0;
  13620. }
  13621. /**
  13622. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13623. * @vport: Pointer to the vport for which the vpi is being initialized
  13624. *
  13625. * This routine is invoked to activate a vpi with the port.
  13626. *
  13627. * Returns:
  13628. * 0 success
  13629. * -Evalue otherwise
  13630. **/
  13631. int
  13632. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13633. {
  13634. LPFC_MBOXQ_t *mboxq;
  13635. int rc = 0;
  13636. int retval = MBX_SUCCESS;
  13637. uint32_t mbox_tmo;
  13638. struct lpfc_hba *phba = vport->phba;
  13639. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13640. if (!mboxq)
  13641. return -ENOMEM;
  13642. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13643. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13644. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13645. if (rc != MBX_SUCCESS) {
  13646. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13647. "2022 INIT VPI Mailbox failed "
  13648. "status %d, mbxStatus x%x\n", rc,
  13649. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13650. retval = -EIO;
  13651. }
  13652. if (rc != MBX_TIMEOUT)
  13653. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13654. return retval;
  13655. }
  13656. /**
  13657. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13658. * @phba: pointer to lpfc hba data structure.
  13659. * @mboxq: Pointer to mailbox object.
  13660. *
  13661. * This routine is invoked to manually add a single FCF record. The caller
  13662. * must pass a completely initialized FCF_Record. This routine takes
  13663. * care of the nonembedded mailbox operations.
  13664. **/
  13665. static void
  13666. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13667. {
  13668. void *virt_addr;
  13669. union lpfc_sli4_cfg_shdr *shdr;
  13670. uint32_t shdr_status, shdr_add_status;
  13671. virt_addr = mboxq->sge_array->addr[0];
  13672. /* The IOCTL status is embedded in the mailbox subheader. */
  13673. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13674. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13675. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13676. if ((shdr_status || shdr_add_status) &&
  13677. (shdr_status != STATUS_FCF_IN_USE))
  13678. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13679. "2558 ADD_FCF_RECORD mailbox failed with "
  13680. "status x%x add_status x%x\n",
  13681. shdr_status, shdr_add_status);
  13682. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13683. }
  13684. /**
  13685. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13686. * @phba: pointer to lpfc hba data structure.
  13687. * @fcf_record: pointer to the initialized fcf record to add.
  13688. *
  13689. * This routine is invoked to manually add a single FCF record. The caller
  13690. * must pass a completely initialized FCF_Record. This routine takes
  13691. * care of the nonembedded mailbox operations.
  13692. **/
  13693. int
  13694. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13695. {
  13696. int rc = 0;
  13697. LPFC_MBOXQ_t *mboxq;
  13698. uint8_t *bytep;
  13699. void *virt_addr;
  13700. dma_addr_t phys_addr;
  13701. struct lpfc_mbx_sge sge;
  13702. uint32_t alloc_len, req_len;
  13703. uint32_t fcfindex;
  13704. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13705. if (!mboxq) {
  13706. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13707. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13708. return -ENOMEM;
  13709. }
  13710. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13711. sizeof(uint32_t);
  13712. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13713. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13714. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13715. req_len, LPFC_SLI4_MBX_NEMBED);
  13716. if (alloc_len < req_len) {
  13717. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13718. "2523 Allocated DMA memory size (x%x) is "
  13719. "less than the requested DMA memory "
  13720. "size (x%x)\n", alloc_len, req_len);
  13721. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13722. return -ENOMEM;
  13723. }
  13724. /*
  13725. * Get the first SGE entry from the non-embedded DMA memory. This
  13726. * routine only uses a single SGE.
  13727. */
  13728. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13729. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13730. virt_addr = mboxq->sge_array->addr[0];
  13731. /*
  13732. * Configure the FCF record for FCFI 0. This is the driver's
  13733. * hardcoded default and gets used in nonFIP mode.
  13734. */
  13735. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13736. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13737. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13738. /*
  13739. * Copy the fcf_index and the FCF Record Data. The data starts after
  13740. * the FCoE header plus word10. The data copy needs to be endian
  13741. * correct.
  13742. */
  13743. bytep += sizeof(uint32_t);
  13744. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13745. mboxq->vport = phba->pport;
  13746. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13747. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13748. if (rc == MBX_NOT_FINISHED) {
  13749. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13750. "2515 ADD_FCF_RECORD mailbox failed with "
  13751. "status 0x%x\n", rc);
  13752. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13753. rc = -EIO;
  13754. } else
  13755. rc = 0;
  13756. return rc;
  13757. }
  13758. /**
  13759. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13760. * @phba: pointer to lpfc hba data structure.
  13761. * @fcf_record: pointer to the fcf record to write the default data.
  13762. * @fcf_index: FCF table entry index.
  13763. *
  13764. * This routine is invoked to build the driver's default FCF record. The
  13765. * values used are hardcoded. This routine handles memory initialization.
  13766. *
  13767. **/
  13768. void
  13769. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13770. struct fcf_record *fcf_record,
  13771. uint16_t fcf_index)
  13772. {
  13773. memset(fcf_record, 0, sizeof(struct fcf_record));
  13774. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13775. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13776. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13777. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13778. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13779. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13780. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13781. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13782. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13783. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13784. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13785. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13786. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13787. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13788. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13789. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13790. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13791. /* Set the VLAN bit map */
  13792. if (phba->valid_vlan) {
  13793. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13794. = 1 << (phba->vlan_id % 8);
  13795. }
  13796. }
  13797. /**
  13798. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13799. * @phba: pointer to lpfc hba data structure.
  13800. * @fcf_index: FCF table entry offset.
  13801. *
  13802. * This routine is invoked to scan the entire FCF table by reading FCF
  13803. * record and processing it one at a time starting from the @fcf_index
  13804. * for initial FCF discovery or fast FCF failover rediscovery.
  13805. *
  13806. * Return 0 if the mailbox command is submitted successfully, none 0
  13807. * otherwise.
  13808. **/
  13809. int
  13810. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13811. {
  13812. int rc = 0, error;
  13813. LPFC_MBOXQ_t *mboxq;
  13814. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13815. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13816. if (!mboxq) {
  13817. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13818. "2000 Failed to allocate mbox for "
  13819. "READ_FCF cmd\n");
  13820. error = -ENOMEM;
  13821. goto fail_fcf_scan;
  13822. }
  13823. /* Construct the read FCF record mailbox command */
  13824. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13825. if (rc) {
  13826. error = -EINVAL;
  13827. goto fail_fcf_scan;
  13828. }
  13829. /* Issue the mailbox command asynchronously */
  13830. mboxq->vport = phba->pport;
  13831. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13832. spin_lock_irq(&phba->hbalock);
  13833. phba->hba_flag |= FCF_TS_INPROG;
  13834. spin_unlock_irq(&phba->hbalock);
  13835. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13836. if (rc == MBX_NOT_FINISHED)
  13837. error = -EIO;
  13838. else {
  13839. /* Reset eligible FCF count for new scan */
  13840. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13841. phba->fcf.eligible_fcf_cnt = 0;
  13842. error = 0;
  13843. }
  13844. fail_fcf_scan:
  13845. if (error) {
  13846. if (mboxq)
  13847. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13848. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13849. spin_lock_irq(&phba->hbalock);
  13850. phba->hba_flag &= ~FCF_TS_INPROG;
  13851. spin_unlock_irq(&phba->hbalock);
  13852. }
  13853. return error;
  13854. }
  13855. /**
  13856. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13857. * @phba: pointer to lpfc hba data structure.
  13858. * @fcf_index: FCF table entry offset.
  13859. *
  13860. * This routine is invoked to read an FCF record indicated by @fcf_index
  13861. * and to use it for FLOGI roundrobin FCF failover.
  13862. *
  13863. * Return 0 if the mailbox command is submitted successfully, none 0
  13864. * otherwise.
  13865. **/
  13866. int
  13867. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13868. {
  13869. int rc = 0, error;
  13870. LPFC_MBOXQ_t *mboxq;
  13871. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13872. if (!mboxq) {
  13873. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13874. "2763 Failed to allocate mbox for "
  13875. "READ_FCF cmd\n");
  13876. error = -ENOMEM;
  13877. goto fail_fcf_read;
  13878. }
  13879. /* Construct the read FCF record mailbox command */
  13880. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13881. if (rc) {
  13882. error = -EINVAL;
  13883. goto fail_fcf_read;
  13884. }
  13885. /* Issue the mailbox command asynchronously */
  13886. mboxq->vport = phba->pport;
  13887. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13888. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13889. if (rc == MBX_NOT_FINISHED)
  13890. error = -EIO;
  13891. else
  13892. error = 0;
  13893. fail_fcf_read:
  13894. if (error && mboxq)
  13895. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13896. return error;
  13897. }
  13898. /**
  13899. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13900. * @phba: pointer to lpfc hba data structure.
  13901. * @fcf_index: FCF table entry offset.
  13902. *
  13903. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13904. * determine whether it's eligible for FLOGI roundrobin failover list.
  13905. *
  13906. * Return 0 if the mailbox command is submitted successfully, none 0
  13907. * otherwise.
  13908. **/
  13909. int
  13910. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13911. {
  13912. int rc = 0, error;
  13913. LPFC_MBOXQ_t *mboxq;
  13914. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13915. if (!mboxq) {
  13916. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13917. "2758 Failed to allocate mbox for "
  13918. "READ_FCF cmd\n");
  13919. error = -ENOMEM;
  13920. goto fail_fcf_read;
  13921. }
  13922. /* Construct the read FCF record mailbox command */
  13923. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13924. if (rc) {
  13925. error = -EINVAL;
  13926. goto fail_fcf_read;
  13927. }
  13928. /* Issue the mailbox command asynchronously */
  13929. mboxq->vport = phba->pport;
  13930. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13931. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13932. if (rc == MBX_NOT_FINISHED)
  13933. error = -EIO;
  13934. else
  13935. error = 0;
  13936. fail_fcf_read:
  13937. if (error && mboxq)
  13938. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13939. return error;
  13940. }
  13941. /**
  13942. * lpfc_check_next_fcf_pri
  13943. * phba pointer to the lpfc_hba struct for this port.
  13944. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13945. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13946. * rr_bmask based on their priority level. Starting from the highest priority
  13947. * to the lowest. The most likely FCF candidate will be in the highest
  13948. * priority group. When this routine is called it searches the fcf_pri list for
  13949. * next lowest priority group and repopulates the rr_bmask with only those
  13950. * fcf_indexes.
  13951. * returns:
  13952. * 1=success 0=failure
  13953. **/
  13954. int
  13955. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13956. {
  13957. uint16_t next_fcf_pri;
  13958. uint16_t last_index;
  13959. struct lpfc_fcf_pri *fcf_pri;
  13960. int rc;
  13961. int ret = 0;
  13962. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13963. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13964. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13965. "3060 Last IDX %d\n", last_index);
  13966. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13967. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13968. "3061 Last IDX %d\n", last_index);
  13969. return 0; /* Empty rr list */
  13970. }
  13971. next_fcf_pri = 0;
  13972. /*
  13973. * Clear the rr_bmask and set all of the bits that are at this
  13974. * priority.
  13975. */
  13976. memset(phba->fcf.fcf_rr_bmask, 0,
  13977. sizeof(*phba->fcf.fcf_rr_bmask));
  13978. spin_lock_irq(&phba->hbalock);
  13979. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13980. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13981. continue;
  13982. /*
  13983. * the 1st priority that has not FLOGI failed
  13984. * will be the highest.
  13985. */
  13986. if (!next_fcf_pri)
  13987. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13988. spin_unlock_irq(&phba->hbalock);
  13989. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13990. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13991. fcf_pri->fcf_rec.fcf_index);
  13992. if (rc)
  13993. return 0;
  13994. }
  13995. spin_lock_irq(&phba->hbalock);
  13996. }
  13997. /*
  13998. * if next_fcf_pri was not set above and the list is not empty then
  13999. * we have failed flogis on all of them. So reset flogi failed
  14000. * and start at the begining.
  14001. */
  14002. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  14003. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14004. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14005. /*
  14006. * the 1st priority that has not FLOGI failed
  14007. * will be the highest.
  14008. */
  14009. if (!next_fcf_pri)
  14010. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14011. spin_unlock_irq(&phba->hbalock);
  14012. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14013. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14014. fcf_pri->fcf_rec.fcf_index);
  14015. if (rc)
  14016. return 0;
  14017. }
  14018. spin_lock_irq(&phba->hbalock);
  14019. }
  14020. } else
  14021. ret = 1;
  14022. spin_unlock_irq(&phba->hbalock);
  14023. return ret;
  14024. }
  14025. /**
  14026. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14027. * @phba: pointer to lpfc hba data structure.
  14028. *
  14029. * This routine is to get the next eligible FCF record index in a round
  14030. * robin fashion. If the next eligible FCF record index equals to the
  14031. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14032. * shall be returned, otherwise, the next eligible FCF record's index
  14033. * shall be returned.
  14034. **/
  14035. uint16_t
  14036. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14037. {
  14038. uint16_t next_fcf_index;
  14039. /* Search start from next bit of currently registered FCF index */
  14040. next_priority:
  14041. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  14042. LPFC_SLI4_FCF_TBL_INDX_MAX;
  14043. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14044. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14045. next_fcf_index);
  14046. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14047. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14048. /*
  14049. * If we have wrapped then we need to clear the bits that
  14050. * have been tested so that we can detect when we should
  14051. * change the priority level.
  14052. */
  14053. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14054. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14055. }
  14056. /* Check roundrobin failover list empty condition */
  14057. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14058. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14059. /*
  14060. * If next fcf index is not found check if there are lower
  14061. * Priority level fcf's in the fcf_priority list.
  14062. * Set up the rr_bmask with all of the avaiable fcf bits
  14063. * at that level and continue the selection process.
  14064. */
  14065. if (lpfc_check_next_fcf_pri_level(phba))
  14066. goto next_priority;
  14067. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14068. "2844 No roundrobin failover FCF available\n");
  14069. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14070. return LPFC_FCOE_FCF_NEXT_NONE;
  14071. else {
  14072. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14073. "3063 Only FCF available idx %d, flag %x\n",
  14074. next_fcf_index,
  14075. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14076. return next_fcf_index;
  14077. }
  14078. }
  14079. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14080. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14081. LPFC_FCF_FLOGI_FAILED)
  14082. goto next_priority;
  14083. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14084. "2845 Get next roundrobin failover FCF (x%x)\n",
  14085. next_fcf_index);
  14086. return next_fcf_index;
  14087. }
  14088. /**
  14089. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14090. * @phba: pointer to lpfc hba data structure.
  14091. *
  14092. * This routine sets the FCF record index in to the eligible bmask for
  14093. * roundrobin failover search. It checks to make sure that the index
  14094. * does not go beyond the range of the driver allocated bmask dimension
  14095. * before setting the bit.
  14096. *
  14097. * Returns 0 if the index bit successfully set, otherwise, it returns
  14098. * -EINVAL.
  14099. **/
  14100. int
  14101. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14102. {
  14103. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14104. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14105. "2610 FCF (x%x) reached driver's book "
  14106. "keeping dimension:x%x\n",
  14107. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14108. return -EINVAL;
  14109. }
  14110. /* Set the eligible FCF record index bmask */
  14111. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14112. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14113. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14114. "bmask\n", fcf_index);
  14115. return 0;
  14116. }
  14117. /**
  14118. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14119. * @phba: pointer to lpfc hba data structure.
  14120. *
  14121. * This routine clears the FCF record index from the eligible bmask for
  14122. * roundrobin failover search. It checks to make sure that the index
  14123. * does not go beyond the range of the driver allocated bmask dimension
  14124. * before clearing the bit.
  14125. **/
  14126. void
  14127. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14128. {
  14129. struct lpfc_fcf_pri *fcf_pri;
  14130. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14131. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14132. "2762 FCF (x%x) reached driver's book "
  14133. "keeping dimension:x%x\n",
  14134. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14135. return;
  14136. }
  14137. /* Clear the eligible FCF record index bmask */
  14138. spin_lock_irq(&phba->hbalock);
  14139. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14140. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14141. list_del_init(&fcf_pri->list);
  14142. break;
  14143. }
  14144. }
  14145. spin_unlock_irq(&phba->hbalock);
  14146. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14147. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14148. "2791 Clear FCF (x%x) from roundrobin failover "
  14149. "bmask\n", fcf_index);
  14150. }
  14151. /**
  14152. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14153. * @phba: pointer to lpfc hba data structure.
  14154. *
  14155. * This routine is the completion routine for the rediscover FCF table mailbox
  14156. * command. If the mailbox command returned failure, it will try to stop the
  14157. * FCF rediscover wait timer.
  14158. **/
  14159. void
  14160. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14161. {
  14162. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14163. uint32_t shdr_status, shdr_add_status;
  14164. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14165. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14166. &redisc_fcf->header.cfg_shdr.response);
  14167. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14168. &redisc_fcf->header.cfg_shdr.response);
  14169. if (shdr_status || shdr_add_status) {
  14170. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14171. "2746 Requesting for FCF rediscovery failed "
  14172. "status x%x add_status x%x\n",
  14173. shdr_status, shdr_add_status);
  14174. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14175. spin_lock_irq(&phba->hbalock);
  14176. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14177. spin_unlock_irq(&phba->hbalock);
  14178. /*
  14179. * CVL event triggered FCF rediscover request failed,
  14180. * last resort to re-try current registered FCF entry.
  14181. */
  14182. lpfc_retry_pport_discovery(phba);
  14183. } else {
  14184. spin_lock_irq(&phba->hbalock);
  14185. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14186. spin_unlock_irq(&phba->hbalock);
  14187. /*
  14188. * DEAD FCF event triggered FCF rediscover request
  14189. * failed, last resort to fail over as a link down
  14190. * to FCF registration.
  14191. */
  14192. lpfc_sli4_fcf_dead_failthrough(phba);
  14193. }
  14194. } else {
  14195. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14196. "2775 Start FCF rediscover quiescent timer\n");
  14197. /*
  14198. * Start FCF rediscovery wait timer for pending FCF
  14199. * before rescan FCF record table.
  14200. */
  14201. lpfc_fcf_redisc_wait_start_timer(phba);
  14202. }
  14203. mempool_free(mbox, phba->mbox_mem_pool);
  14204. }
  14205. /**
  14206. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14207. * @phba: pointer to lpfc hba data structure.
  14208. *
  14209. * This routine is invoked to request for rediscovery of the entire FCF table
  14210. * by the port.
  14211. **/
  14212. int
  14213. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14214. {
  14215. LPFC_MBOXQ_t *mbox;
  14216. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14217. int rc, length;
  14218. /* Cancel retry delay timers to all vports before FCF rediscover */
  14219. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14220. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14221. if (!mbox) {
  14222. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14223. "2745 Failed to allocate mbox for "
  14224. "requesting FCF rediscover.\n");
  14225. return -ENOMEM;
  14226. }
  14227. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14228. sizeof(struct lpfc_sli4_cfg_mhdr));
  14229. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14230. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14231. length, LPFC_SLI4_MBX_EMBED);
  14232. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14233. /* Set count to 0 for invalidating the entire FCF database */
  14234. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14235. /* Issue the mailbox command asynchronously */
  14236. mbox->vport = phba->pport;
  14237. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14238. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14239. if (rc == MBX_NOT_FINISHED) {
  14240. mempool_free(mbox, phba->mbox_mem_pool);
  14241. return -EIO;
  14242. }
  14243. return 0;
  14244. }
  14245. /**
  14246. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14247. * @phba: pointer to lpfc hba data structure.
  14248. *
  14249. * This function is the failover routine as a last resort to the FCF DEAD
  14250. * event when driver failed to perform fast FCF failover.
  14251. **/
  14252. void
  14253. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14254. {
  14255. uint32_t link_state;
  14256. /*
  14257. * Last resort as FCF DEAD event failover will treat this as
  14258. * a link down, but save the link state because we don't want
  14259. * it to be changed to Link Down unless it is already down.
  14260. */
  14261. link_state = phba->link_state;
  14262. lpfc_linkdown(phba);
  14263. phba->link_state = link_state;
  14264. /* Unregister FCF if no devices connected to it */
  14265. lpfc_unregister_unused_fcf(phba);
  14266. }
  14267. /**
  14268. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14269. * @phba: pointer to lpfc hba data structure.
  14270. * @rgn23_data: pointer to configure region 23 data.
  14271. *
  14272. * This function gets SLI3 port configure region 23 data through memory dump
  14273. * mailbox command. When it successfully retrieves data, the size of the data
  14274. * will be returned, otherwise, 0 will be returned.
  14275. **/
  14276. static uint32_t
  14277. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14278. {
  14279. LPFC_MBOXQ_t *pmb = NULL;
  14280. MAILBOX_t *mb;
  14281. uint32_t offset = 0;
  14282. int rc;
  14283. if (!rgn23_data)
  14284. return 0;
  14285. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14286. if (!pmb) {
  14287. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14288. "2600 failed to allocate mailbox memory\n");
  14289. return 0;
  14290. }
  14291. mb = &pmb->u.mb;
  14292. do {
  14293. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14294. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14295. if (rc != MBX_SUCCESS) {
  14296. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14297. "2601 failed to read config "
  14298. "region 23, rc 0x%x Status 0x%x\n",
  14299. rc, mb->mbxStatus);
  14300. mb->un.varDmp.word_cnt = 0;
  14301. }
  14302. /*
  14303. * dump mem may return a zero when finished or we got a
  14304. * mailbox error, either way we are done.
  14305. */
  14306. if (mb->un.varDmp.word_cnt == 0)
  14307. break;
  14308. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14309. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14310. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14311. rgn23_data + offset,
  14312. mb->un.varDmp.word_cnt);
  14313. offset += mb->un.varDmp.word_cnt;
  14314. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14315. mempool_free(pmb, phba->mbox_mem_pool);
  14316. return offset;
  14317. }
  14318. /**
  14319. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14320. * @phba: pointer to lpfc hba data structure.
  14321. * @rgn23_data: pointer to configure region 23 data.
  14322. *
  14323. * This function gets SLI4 port configure region 23 data through memory dump
  14324. * mailbox command. When it successfully retrieves data, the size of the data
  14325. * will be returned, otherwise, 0 will be returned.
  14326. **/
  14327. static uint32_t
  14328. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14329. {
  14330. LPFC_MBOXQ_t *mboxq = NULL;
  14331. struct lpfc_dmabuf *mp = NULL;
  14332. struct lpfc_mqe *mqe;
  14333. uint32_t data_length = 0;
  14334. int rc;
  14335. if (!rgn23_data)
  14336. return 0;
  14337. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14338. if (!mboxq) {
  14339. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14340. "3105 failed to allocate mailbox memory\n");
  14341. return 0;
  14342. }
  14343. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14344. goto out;
  14345. mqe = &mboxq->u.mqe;
  14346. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14347. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14348. if (rc)
  14349. goto out;
  14350. data_length = mqe->un.mb_words[5];
  14351. if (data_length == 0)
  14352. goto out;
  14353. if (data_length > DMP_RGN23_SIZE) {
  14354. data_length = 0;
  14355. goto out;
  14356. }
  14357. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14358. out:
  14359. mempool_free(mboxq, phba->mbox_mem_pool);
  14360. if (mp) {
  14361. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14362. kfree(mp);
  14363. }
  14364. return data_length;
  14365. }
  14366. /**
  14367. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14368. * @phba: pointer to lpfc hba data structure.
  14369. *
  14370. * This function read region 23 and parse TLV for port status to
  14371. * decide if the user disaled the port. If the TLV indicates the
  14372. * port is disabled, the hba_flag is set accordingly.
  14373. **/
  14374. void
  14375. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14376. {
  14377. uint8_t *rgn23_data = NULL;
  14378. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14379. uint32_t offset = 0;
  14380. /* Get adapter Region 23 data */
  14381. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14382. if (!rgn23_data)
  14383. goto out;
  14384. if (phba->sli_rev < LPFC_SLI_REV4)
  14385. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14386. else {
  14387. if_type = bf_get(lpfc_sli_intf_if_type,
  14388. &phba->sli4_hba.sli_intf);
  14389. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14390. goto out;
  14391. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14392. }
  14393. if (!data_size)
  14394. goto out;
  14395. /* Check the region signature first */
  14396. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14397. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14398. "2619 Config region 23 has bad signature\n");
  14399. goto out;
  14400. }
  14401. offset += 4;
  14402. /* Check the data structure version */
  14403. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14404. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14405. "2620 Config region 23 has bad version\n");
  14406. goto out;
  14407. }
  14408. offset += 4;
  14409. /* Parse TLV entries in the region */
  14410. while (offset < data_size) {
  14411. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14412. break;
  14413. /*
  14414. * If the TLV is not driver specific TLV or driver id is
  14415. * not linux driver id, skip the record.
  14416. */
  14417. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14418. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14419. (rgn23_data[offset + 3] != 0)) {
  14420. offset += rgn23_data[offset + 1] * 4 + 4;
  14421. continue;
  14422. }
  14423. /* Driver found a driver specific TLV in the config region */
  14424. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14425. offset += 4;
  14426. tlv_offset = 0;
  14427. /*
  14428. * Search for configured port state sub-TLV.
  14429. */
  14430. while ((offset < data_size) &&
  14431. (tlv_offset < sub_tlv_len)) {
  14432. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14433. offset += 4;
  14434. tlv_offset += 4;
  14435. break;
  14436. }
  14437. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14438. offset += rgn23_data[offset + 1] * 4 + 4;
  14439. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14440. continue;
  14441. }
  14442. /* This HBA contains PORT_STE configured */
  14443. if (!rgn23_data[offset + 2])
  14444. phba->hba_flag |= LINK_DISABLED;
  14445. goto out;
  14446. }
  14447. }
  14448. out:
  14449. kfree(rgn23_data);
  14450. return;
  14451. }
  14452. /**
  14453. * lpfc_wr_object - write an object to the firmware
  14454. * @phba: HBA structure that indicates port to create a queue on.
  14455. * @dmabuf_list: list of dmabufs to write to the port.
  14456. * @size: the total byte value of the objects to write to the port.
  14457. * @offset: the current offset to be used to start the transfer.
  14458. *
  14459. * This routine will create a wr_object mailbox command to send to the port.
  14460. * the mailbox command will be constructed using the dma buffers described in
  14461. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14462. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14463. * used to indicate the starting offset of the transfer and will also return
  14464. * the offset after the write object mailbox has completed. @size is used to
  14465. * determine the end of the object and whether the eof bit should be set.
  14466. *
  14467. * Return 0 is successful and offset will contain the the new offset to use
  14468. * for the next write.
  14469. * Return negative value for error cases.
  14470. **/
  14471. int
  14472. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14473. uint32_t size, uint32_t *offset)
  14474. {
  14475. struct lpfc_mbx_wr_object *wr_object;
  14476. LPFC_MBOXQ_t *mbox;
  14477. int rc = 0, i = 0;
  14478. uint32_t shdr_status, shdr_add_status;
  14479. uint32_t mbox_tmo;
  14480. union lpfc_sli4_cfg_shdr *shdr;
  14481. struct lpfc_dmabuf *dmabuf;
  14482. uint32_t written = 0;
  14483. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14484. if (!mbox)
  14485. return -ENOMEM;
  14486. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14487. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14488. sizeof(struct lpfc_mbx_wr_object) -
  14489. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14490. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14491. wr_object->u.request.write_offset = *offset;
  14492. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14493. wr_object->u.request.object_name[0] =
  14494. cpu_to_le32(wr_object->u.request.object_name[0]);
  14495. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14496. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14497. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14498. break;
  14499. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14500. wr_object->u.request.bde[i].addrHigh =
  14501. putPaddrHigh(dmabuf->phys);
  14502. if (written + SLI4_PAGE_SIZE >= size) {
  14503. wr_object->u.request.bde[i].tus.f.bdeSize =
  14504. (size - written);
  14505. written += (size - written);
  14506. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14507. } else {
  14508. wr_object->u.request.bde[i].tus.f.bdeSize =
  14509. SLI4_PAGE_SIZE;
  14510. written += SLI4_PAGE_SIZE;
  14511. }
  14512. i++;
  14513. }
  14514. wr_object->u.request.bde_count = i;
  14515. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14516. if (!phba->sli4_hba.intr_enable)
  14517. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14518. else {
  14519. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14520. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14521. }
  14522. /* The IOCTL status is embedded in the mailbox subheader. */
  14523. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14524. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14525. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14526. if (rc != MBX_TIMEOUT)
  14527. mempool_free(mbox, phba->mbox_mem_pool);
  14528. if (shdr_status || shdr_add_status || rc) {
  14529. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14530. "3025 Write Object mailbox failed with "
  14531. "status x%x add_status x%x, mbx status x%x\n",
  14532. shdr_status, shdr_add_status, rc);
  14533. rc = -ENXIO;
  14534. } else
  14535. *offset += wr_object->u.response.actual_write_length;
  14536. return rc;
  14537. }
  14538. /**
  14539. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14540. * @vport: pointer to vport data structure.
  14541. *
  14542. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14543. * and REG_VPI mailbox commands associated with the vport. This function
  14544. * is called when driver want to restart discovery of the vport due to
  14545. * a Clear Virtual Link event.
  14546. **/
  14547. void
  14548. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14549. {
  14550. struct lpfc_hba *phba = vport->phba;
  14551. LPFC_MBOXQ_t *mb, *nextmb;
  14552. struct lpfc_dmabuf *mp;
  14553. struct lpfc_nodelist *ndlp;
  14554. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14555. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14556. LIST_HEAD(mbox_cmd_list);
  14557. uint8_t restart_loop;
  14558. /* Clean up internally queued mailbox commands with the vport */
  14559. spin_lock_irq(&phba->hbalock);
  14560. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14561. if (mb->vport != vport)
  14562. continue;
  14563. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14564. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14565. continue;
  14566. list_del(&mb->list);
  14567. list_add_tail(&mb->list, &mbox_cmd_list);
  14568. }
  14569. /* Clean up active mailbox command with the vport */
  14570. mb = phba->sli.mbox_active;
  14571. if (mb && (mb->vport == vport)) {
  14572. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14573. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14574. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14575. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14576. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14577. /* Put reference count for delayed processing */
  14578. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14579. /* Unregister the RPI when mailbox complete */
  14580. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14581. }
  14582. }
  14583. /* Cleanup any mailbox completions which are not yet processed */
  14584. do {
  14585. restart_loop = 0;
  14586. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14587. /*
  14588. * If this mailox is already processed or it is
  14589. * for another vport ignore it.
  14590. */
  14591. if ((mb->vport != vport) ||
  14592. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14593. continue;
  14594. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14595. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14596. continue;
  14597. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14598. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14599. ndlp = (struct lpfc_nodelist *)mb->context2;
  14600. /* Unregister the RPI when mailbox complete */
  14601. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14602. restart_loop = 1;
  14603. spin_unlock_irq(&phba->hbalock);
  14604. spin_lock(shost->host_lock);
  14605. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14606. spin_unlock(shost->host_lock);
  14607. spin_lock_irq(&phba->hbalock);
  14608. break;
  14609. }
  14610. }
  14611. } while (restart_loop);
  14612. spin_unlock_irq(&phba->hbalock);
  14613. /* Release the cleaned-up mailbox commands */
  14614. while (!list_empty(&mbox_cmd_list)) {
  14615. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14616. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14617. mp = (struct lpfc_dmabuf *) (mb->context1);
  14618. if (mp) {
  14619. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14620. kfree(mp);
  14621. }
  14622. ndlp = (struct lpfc_nodelist *) mb->context2;
  14623. mb->context2 = NULL;
  14624. if (ndlp) {
  14625. spin_lock(shost->host_lock);
  14626. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14627. spin_unlock(shost->host_lock);
  14628. lpfc_nlp_put(ndlp);
  14629. }
  14630. }
  14631. mempool_free(mb, phba->mbox_mem_pool);
  14632. }
  14633. /* Release the ndlp with the cleaned-up active mailbox command */
  14634. if (act_mbx_ndlp) {
  14635. spin_lock(shost->host_lock);
  14636. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14637. spin_unlock(shost->host_lock);
  14638. lpfc_nlp_put(act_mbx_ndlp);
  14639. }
  14640. }
  14641. /**
  14642. * lpfc_drain_txq - Drain the txq
  14643. * @phba: Pointer to HBA context object.
  14644. *
  14645. * This function attempt to submit IOCBs on the txq
  14646. * to the adapter. For SLI4 adapters, the txq contains
  14647. * ELS IOCBs that have been deferred because the there
  14648. * are no SGLs. This congestion can occur with large
  14649. * vport counts during node discovery.
  14650. **/
  14651. uint32_t
  14652. lpfc_drain_txq(struct lpfc_hba *phba)
  14653. {
  14654. LIST_HEAD(completions);
  14655. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14656. struct lpfc_iocbq *piocbq = 0;
  14657. unsigned long iflags = 0;
  14658. char *fail_msg = NULL;
  14659. struct lpfc_sglq *sglq;
  14660. union lpfc_wqe wqe;
  14661. spin_lock_irqsave(&phba->hbalock, iflags);
  14662. if (pring->txq_cnt > pring->txq_max)
  14663. pring->txq_max = pring->txq_cnt;
  14664. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14665. while (pring->txq_cnt) {
  14666. spin_lock_irqsave(&phba->hbalock, iflags);
  14667. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14668. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14669. if (!sglq) {
  14670. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14671. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14672. break;
  14673. } else {
  14674. if (!piocbq) {
  14675. /* The txq_cnt out of sync. This should
  14676. * never happen
  14677. */
  14678. sglq = __lpfc_clear_active_sglq(phba,
  14679. sglq->sli4_lxritag);
  14680. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14681. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14682. "2823 txq empty and txq_cnt is %d\n ",
  14683. pring->txq_cnt);
  14684. break;
  14685. }
  14686. }
  14687. /* The xri and iocb resources secured,
  14688. * attempt to issue request
  14689. */
  14690. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14691. piocbq->sli4_xritag = sglq->sli4_xritag;
  14692. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14693. fail_msg = "to convert bpl to sgl";
  14694. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14695. fail_msg = "to convert iocb to wqe";
  14696. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14697. fail_msg = " - Wq is full";
  14698. else
  14699. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14700. if (fail_msg) {
  14701. /* Failed means we can't issue and need to cancel */
  14702. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14703. "2822 IOCB failed %s iotag 0x%x "
  14704. "xri 0x%x\n",
  14705. fail_msg,
  14706. piocbq->iotag, piocbq->sli4_xritag);
  14707. list_add_tail(&piocbq->list, &completions);
  14708. }
  14709. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14710. }
  14711. /* Cancel all the IOCBs that cannot be issued */
  14712. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14713. IOERR_SLI_ABORTED);
  14714. return pring->txq_cnt;
  14715. }