lpfc_sli.c 507 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2013 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
  67. uint32_t);
  68. static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
  69. static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
  70. static IOCB_t *
  71. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  72. {
  73. return &iocbq->iocb;
  74. }
  75. /**
  76. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  77. * @q: The Work Queue to operate on.
  78. * @wqe: The work Queue Entry to put on the Work queue.
  79. *
  80. * This routine will copy the contents of @wqe to the next available entry on
  81. * the @q. This function will then ring the Work Queue Doorbell to signal the
  82. * HBA to start processing the Work Queue Entry. This function returns 0 if
  83. * successful. If no entries are available on @q then this function will return
  84. * -ENOMEM.
  85. * The caller is expected to hold the hbalock when calling this routine.
  86. **/
  87. static uint32_t
  88. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  89. {
  90. union lpfc_wqe *temp_wqe;
  91. struct lpfc_register doorbell;
  92. uint32_t host_index;
  93. uint32_t idx;
  94. /* sanity check on queue memory */
  95. if (unlikely(!q))
  96. return -ENOMEM;
  97. temp_wqe = q->qe[q->host_index].wqe;
  98. /* If the host has not yet processed the next entry then we are done */
  99. idx = ((q->host_index + 1) % q->entry_count);
  100. if (idx == q->hba_index) {
  101. q->WQ_overflow++;
  102. return -ENOMEM;
  103. }
  104. q->WQ_posted++;
  105. /* set consumption flag every once in a while */
  106. if (!((q->host_index + 1) % q->entry_repost))
  107. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  108. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  109. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  110. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  111. /* Update the host index before invoking device */
  112. host_index = q->host_index;
  113. q->host_index = idx;
  114. /* Ring Doorbell */
  115. doorbell.word0 = 0;
  116. if (q->db_format == LPFC_DB_LIST_FORMAT) {
  117. bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
  118. bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
  119. bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
  120. } else if (q->db_format == LPFC_DB_RING_FORMAT) {
  121. bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
  122. bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
  123. } else {
  124. return -EINVAL;
  125. }
  126. writel(doorbell.word0, q->db_regaddr);
  127. return 0;
  128. }
  129. /**
  130. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  131. * @q: The Work Queue to operate on.
  132. * @index: The index to advance the hba index to.
  133. *
  134. * This routine will update the HBA index of a queue to reflect consumption of
  135. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  136. * an entry the host calls this function to update the queue's internal
  137. * pointers. This routine returns the number of entries that were consumed by
  138. * the HBA.
  139. **/
  140. static uint32_t
  141. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  142. {
  143. uint32_t released = 0;
  144. /* sanity check on queue memory */
  145. if (unlikely(!q))
  146. return 0;
  147. if (q->hba_index == index)
  148. return 0;
  149. do {
  150. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  151. released++;
  152. } while (q->hba_index != index);
  153. return released;
  154. }
  155. /**
  156. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  157. * @q: The Mailbox Queue to operate on.
  158. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  159. *
  160. * This routine will copy the contents of @mqe to the next available entry on
  161. * the @q. This function will then ring the Work Queue Doorbell to signal the
  162. * HBA to start processing the Work Queue Entry. This function returns 0 if
  163. * successful. If no entries are available on @q then this function will return
  164. * -ENOMEM.
  165. * The caller is expected to hold the hbalock when calling this routine.
  166. **/
  167. static uint32_t
  168. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  169. {
  170. struct lpfc_mqe *temp_mqe;
  171. struct lpfc_register doorbell;
  172. uint32_t host_index;
  173. /* sanity check on queue memory */
  174. if (unlikely(!q))
  175. return -ENOMEM;
  176. temp_mqe = q->qe[q->host_index].mqe;
  177. /* If the host has not yet processed the next entry then we are done */
  178. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  179. return -ENOMEM;
  180. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  181. /* Save off the mailbox pointer for completion */
  182. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  183. /* Update the host index before invoking device */
  184. host_index = q->host_index;
  185. q->host_index = ((q->host_index + 1) % q->entry_count);
  186. /* Ring Doorbell */
  187. doorbell.word0 = 0;
  188. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  189. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  190. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  191. return 0;
  192. }
  193. /**
  194. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  195. * @q: The Mailbox Queue to operate on.
  196. *
  197. * This routine will update the HBA index of a queue to reflect consumption of
  198. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  199. * an entry the host calls this function to update the queue's internal
  200. * pointers. This routine returns the number of entries that were consumed by
  201. * the HBA.
  202. **/
  203. static uint32_t
  204. lpfc_sli4_mq_release(struct lpfc_queue *q)
  205. {
  206. /* sanity check on queue memory */
  207. if (unlikely(!q))
  208. return 0;
  209. /* Clear the mailbox pointer for completion */
  210. q->phba->mbox = NULL;
  211. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  212. return 1;
  213. }
  214. /**
  215. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  216. * @q: The Event Queue to get the first valid EQE from
  217. *
  218. * This routine will get the first valid Event Queue Entry from @q, update
  219. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  220. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  221. * processed, but not popped back to the HBA then this routine will return NULL.
  222. **/
  223. static struct lpfc_eqe *
  224. lpfc_sli4_eq_get(struct lpfc_queue *q)
  225. {
  226. struct lpfc_eqe *eqe;
  227. uint32_t idx;
  228. /* sanity check on queue memory */
  229. if (unlikely(!q))
  230. return NULL;
  231. eqe = q->qe[q->hba_index].eqe;
  232. /* If the next EQE is not valid then we are done */
  233. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  234. return NULL;
  235. /* If the host has not yet processed the next entry then we are done */
  236. idx = ((q->hba_index + 1) % q->entry_count);
  237. if (idx == q->host_index)
  238. return NULL;
  239. q->hba_index = idx;
  240. return eqe;
  241. }
  242. /**
  243. * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
  244. * @q: The Event Queue to disable interrupts
  245. *
  246. **/
  247. static inline void
  248. lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
  249. {
  250. struct lpfc_register doorbell;
  251. doorbell.word0 = 0;
  252. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  253. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  254. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  255. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  256. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  257. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  258. }
  259. /**
  260. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  261. * @q: The Event Queue that the host has completed processing for.
  262. * @arm: Indicates whether the host wants to arms this CQ.
  263. *
  264. * This routine will mark all Event Queue Entries on @q, from the last
  265. * known completed entry to the last entry that was processed, as completed
  266. * by clearing the valid bit for each completion queue entry. Then it will
  267. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  268. * The internal host index in the @q will be updated by this routine to indicate
  269. * that the host has finished processing the entries. The @arm parameter
  270. * indicates that the queue should be rearmed when ringing the doorbell.
  271. *
  272. * This function will return the number of EQEs that were popped.
  273. **/
  274. uint32_t
  275. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  276. {
  277. uint32_t released = 0;
  278. struct lpfc_eqe *temp_eqe;
  279. struct lpfc_register doorbell;
  280. /* sanity check on queue memory */
  281. if (unlikely(!q))
  282. return 0;
  283. /* while there are valid entries */
  284. while (q->hba_index != q->host_index) {
  285. temp_eqe = q->qe[q->host_index].eqe;
  286. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  287. released++;
  288. q->host_index = ((q->host_index + 1) % q->entry_count);
  289. }
  290. if (unlikely(released == 0 && !arm))
  291. return 0;
  292. /* ring doorbell for number popped */
  293. doorbell.word0 = 0;
  294. if (arm) {
  295. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  296. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  297. }
  298. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  299. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  300. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  301. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  302. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  303. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  304. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  305. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  306. readl(q->phba->sli4_hba.EQCQDBregaddr);
  307. return released;
  308. }
  309. /**
  310. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  311. * @q: The Completion Queue to get the first valid CQE from
  312. *
  313. * This routine will get the first valid Completion Queue Entry from @q, update
  314. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  315. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  316. * processed, but not popped back to the HBA then this routine will return NULL.
  317. **/
  318. static struct lpfc_cqe *
  319. lpfc_sli4_cq_get(struct lpfc_queue *q)
  320. {
  321. struct lpfc_cqe *cqe;
  322. uint32_t idx;
  323. /* sanity check on queue memory */
  324. if (unlikely(!q))
  325. return NULL;
  326. /* If the next CQE is not valid then we are done */
  327. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  328. return NULL;
  329. /* If the host has not yet processed the next entry then we are done */
  330. idx = ((q->hba_index + 1) % q->entry_count);
  331. if (idx == q->host_index)
  332. return NULL;
  333. cqe = q->qe[q->hba_index].cqe;
  334. q->hba_index = idx;
  335. return cqe;
  336. }
  337. /**
  338. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  339. * @q: The Completion Queue that the host has completed processing for.
  340. * @arm: Indicates whether the host wants to arms this CQ.
  341. *
  342. * This routine will mark all Completion queue entries on @q, from the last
  343. * known completed entry to the last entry that was processed, as completed
  344. * by clearing the valid bit for each completion queue entry. Then it will
  345. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  346. * The internal host index in the @q will be updated by this routine to indicate
  347. * that the host has finished processing the entries. The @arm parameter
  348. * indicates that the queue should be rearmed when ringing the doorbell.
  349. *
  350. * This function will return the number of CQEs that were released.
  351. **/
  352. uint32_t
  353. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  354. {
  355. uint32_t released = 0;
  356. struct lpfc_cqe *temp_qe;
  357. struct lpfc_register doorbell;
  358. /* sanity check on queue memory */
  359. if (unlikely(!q))
  360. return 0;
  361. /* while there are valid entries */
  362. while (q->hba_index != q->host_index) {
  363. temp_qe = q->qe[q->host_index].cqe;
  364. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  365. released++;
  366. q->host_index = ((q->host_index + 1) % q->entry_count);
  367. }
  368. if (unlikely(released == 0 && !arm))
  369. return 0;
  370. /* ring doorbell for number popped */
  371. doorbell.word0 = 0;
  372. if (arm)
  373. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  374. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  375. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  376. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  377. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  378. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  379. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  380. return released;
  381. }
  382. /**
  383. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  384. * @q: The Header Receive Queue to operate on.
  385. * @wqe: The Receive Queue Entry to put on the Receive queue.
  386. *
  387. * This routine will copy the contents of @wqe to the next available entry on
  388. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  389. * HBA to start processing the Receive Queue Entry. This function returns the
  390. * index that the rqe was copied to if successful. If no entries are available
  391. * on @q then this function will return -ENOMEM.
  392. * The caller is expected to hold the hbalock when calling this routine.
  393. **/
  394. static int
  395. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  396. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  397. {
  398. struct lpfc_rqe *temp_hrqe;
  399. struct lpfc_rqe *temp_drqe;
  400. struct lpfc_register doorbell;
  401. int put_index;
  402. /* sanity check on queue memory */
  403. if (unlikely(!hq) || unlikely(!dq))
  404. return -ENOMEM;
  405. put_index = hq->host_index;
  406. temp_hrqe = hq->qe[hq->host_index].rqe;
  407. temp_drqe = dq->qe[dq->host_index].rqe;
  408. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  409. return -EINVAL;
  410. if (hq->host_index != dq->host_index)
  411. return -EINVAL;
  412. /* If the host has not yet processed the next entry then we are done */
  413. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  414. return -EBUSY;
  415. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  416. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  417. /* Update the host index to point to the next slot */
  418. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  419. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  420. /* Ring The Header Receive Queue Doorbell */
  421. if (!(hq->host_index % hq->entry_repost)) {
  422. doorbell.word0 = 0;
  423. if (hq->db_format == LPFC_DB_RING_FORMAT) {
  424. bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  425. hq->entry_repost);
  426. bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  427. } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  428. bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  429. hq->entry_repost);
  430. bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  431. hq->host_index);
  432. bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  433. } else {
  434. return -EINVAL;
  435. }
  436. writel(doorbell.word0, hq->db_regaddr);
  437. }
  438. return put_index;
  439. }
  440. /**
  441. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  442. * @q: The Header Receive Queue to operate on.
  443. *
  444. * This routine will update the HBA index of a queue to reflect consumption of
  445. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  446. * consumed an entry the host calls this function to update the queue's
  447. * internal pointers. This routine returns the number of entries that were
  448. * consumed by the HBA.
  449. **/
  450. static uint32_t
  451. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  452. {
  453. /* sanity check on queue memory */
  454. if (unlikely(!hq) || unlikely(!dq))
  455. return 0;
  456. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  457. return 0;
  458. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  459. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  460. return 1;
  461. }
  462. /**
  463. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  464. * @phba: Pointer to HBA context object.
  465. * @pring: Pointer to driver SLI ring object.
  466. *
  467. * This function returns pointer to next command iocb entry
  468. * in the command ring. The caller must hold hbalock to prevent
  469. * other threads consume the next command iocb.
  470. * SLI-2/SLI-3 provide different sized iocbs.
  471. **/
  472. static inline IOCB_t *
  473. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  474. {
  475. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  476. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  477. }
  478. /**
  479. * lpfc_resp_iocb - Get next response iocb entry in the ring
  480. * @phba: Pointer to HBA context object.
  481. * @pring: Pointer to driver SLI ring object.
  482. *
  483. * This function returns pointer to next response iocb entry
  484. * in the response ring. The caller must hold hbalock to make sure
  485. * that no other thread consume the next response iocb.
  486. * SLI-2/SLI-3 provide different sized iocbs.
  487. **/
  488. static inline IOCB_t *
  489. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  490. {
  491. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  492. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  493. }
  494. /**
  495. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  496. * @phba: Pointer to HBA context object.
  497. *
  498. * This function is called with hbalock held. This function
  499. * allocates a new driver iocb object from the iocb pool. If the
  500. * allocation is successful, it returns pointer to the newly
  501. * allocated iocb object else it returns NULL.
  502. **/
  503. struct lpfc_iocbq *
  504. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  505. {
  506. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  507. struct lpfc_iocbq * iocbq = NULL;
  508. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  509. if (iocbq)
  510. phba->iocb_cnt++;
  511. if (phba->iocb_cnt > phba->iocb_max)
  512. phba->iocb_max = phba->iocb_cnt;
  513. return iocbq;
  514. }
  515. /**
  516. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  517. * @phba: Pointer to HBA context object.
  518. * @xritag: XRI value.
  519. *
  520. * This function clears the sglq pointer from the array of acive
  521. * sglq's. The xritag that is passed in is used to index into the
  522. * array. Before the xritag can be used it needs to be adjusted
  523. * by subtracting the xribase.
  524. *
  525. * Returns sglq ponter = success, NULL = Failure.
  526. **/
  527. static struct lpfc_sglq *
  528. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  529. {
  530. struct lpfc_sglq *sglq;
  531. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  532. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  533. return sglq;
  534. }
  535. /**
  536. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  537. * @phba: Pointer to HBA context object.
  538. * @xritag: XRI value.
  539. *
  540. * This function returns the sglq pointer from the array of acive
  541. * sglq's. The xritag that is passed in is used to index into the
  542. * array. Before the xritag can be used it needs to be adjusted
  543. * by subtracting the xribase.
  544. *
  545. * Returns sglq ponter = success, NULL = Failure.
  546. **/
  547. struct lpfc_sglq *
  548. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  549. {
  550. struct lpfc_sglq *sglq;
  551. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  552. return sglq;
  553. }
  554. /**
  555. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  556. * @phba: Pointer to HBA context object.
  557. * @xritag: xri used in this exchange.
  558. * @rrq: The RRQ to be cleared.
  559. *
  560. **/
  561. void
  562. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  563. uint16_t xritag,
  564. struct lpfc_node_rrq *rrq)
  565. {
  566. struct lpfc_nodelist *ndlp = NULL;
  567. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  568. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  569. /* The target DID could have been swapped (cable swap)
  570. * we should use the ndlp from the findnode if it is
  571. * available.
  572. */
  573. if ((!ndlp) && rrq->ndlp)
  574. ndlp = rrq->ndlp;
  575. if (!ndlp)
  576. goto out;
  577. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  578. rrq->send_rrq = 0;
  579. rrq->xritag = 0;
  580. rrq->rrq_stop_time = 0;
  581. }
  582. out:
  583. mempool_free(rrq, phba->rrq_pool);
  584. }
  585. /**
  586. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  587. * @phba: Pointer to HBA context object.
  588. *
  589. * This function is called with hbalock held. This function
  590. * Checks if stop_time (ratov from setting rrq active) has
  591. * been reached, if it has and the send_rrq flag is set then
  592. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  593. * then it will just call the routine to clear the rrq and
  594. * free the rrq resource.
  595. * The timer is set to the next rrq that is going to expire before
  596. * leaving the routine.
  597. *
  598. **/
  599. void
  600. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  601. {
  602. struct lpfc_node_rrq *rrq;
  603. struct lpfc_node_rrq *nextrrq;
  604. unsigned long next_time;
  605. unsigned long iflags;
  606. LIST_HEAD(send_rrq);
  607. spin_lock_irqsave(&phba->hbalock, iflags);
  608. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  609. next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  610. list_for_each_entry_safe(rrq, nextrrq,
  611. &phba->active_rrq_list, list) {
  612. if (time_after(jiffies, rrq->rrq_stop_time))
  613. list_move(&rrq->list, &send_rrq);
  614. else if (time_before(rrq->rrq_stop_time, next_time))
  615. next_time = rrq->rrq_stop_time;
  616. }
  617. spin_unlock_irqrestore(&phba->hbalock, iflags);
  618. if (!list_empty(&phba->active_rrq_list))
  619. mod_timer(&phba->rrq_tmr, next_time);
  620. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  621. list_del(&rrq->list);
  622. if (!rrq->send_rrq)
  623. /* this call will free the rrq */
  624. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  625. else if (lpfc_send_rrq(phba, rrq)) {
  626. /* if we send the rrq then the completion handler
  627. * will clear the bit in the xribitmap.
  628. */
  629. lpfc_clr_rrq_active(phba, rrq->xritag,
  630. rrq);
  631. }
  632. }
  633. }
  634. /**
  635. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  636. * @vport: Pointer to vport context object.
  637. * @xri: The xri used in the exchange.
  638. * @did: The targets DID for this exchange.
  639. *
  640. * returns NULL = rrq not found in the phba->active_rrq_list.
  641. * rrq = rrq for this xri and target.
  642. **/
  643. struct lpfc_node_rrq *
  644. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  645. {
  646. struct lpfc_hba *phba = vport->phba;
  647. struct lpfc_node_rrq *rrq;
  648. struct lpfc_node_rrq *nextrrq;
  649. unsigned long iflags;
  650. if (phba->sli_rev != LPFC_SLI_REV4)
  651. return NULL;
  652. spin_lock_irqsave(&phba->hbalock, iflags);
  653. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  654. if (rrq->vport == vport && rrq->xritag == xri &&
  655. rrq->nlp_DID == did){
  656. list_del(&rrq->list);
  657. spin_unlock_irqrestore(&phba->hbalock, iflags);
  658. return rrq;
  659. }
  660. }
  661. spin_unlock_irqrestore(&phba->hbalock, iflags);
  662. return NULL;
  663. }
  664. /**
  665. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  666. * @vport: Pointer to vport context object.
  667. * @ndlp: Pointer to the lpfc_node_list structure.
  668. * If ndlp is NULL Remove all active RRQs for this vport from the
  669. * phba->active_rrq_list and clear the rrq.
  670. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  671. **/
  672. void
  673. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  674. {
  675. struct lpfc_hba *phba = vport->phba;
  676. struct lpfc_node_rrq *rrq;
  677. struct lpfc_node_rrq *nextrrq;
  678. unsigned long iflags;
  679. LIST_HEAD(rrq_list);
  680. if (phba->sli_rev != LPFC_SLI_REV4)
  681. return;
  682. if (!ndlp) {
  683. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  684. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  685. }
  686. spin_lock_irqsave(&phba->hbalock, iflags);
  687. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  688. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  689. list_move(&rrq->list, &rrq_list);
  690. spin_unlock_irqrestore(&phba->hbalock, iflags);
  691. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  692. list_del(&rrq->list);
  693. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  694. }
  695. }
  696. /**
  697. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  698. * @phba: Pointer to HBA context object.
  699. *
  700. * Remove all rrqs from the phba->active_rrq_list and free them by
  701. * calling __lpfc_clr_active_rrq
  702. *
  703. **/
  704. void
  705. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  706. {
  707. struct lpfc_node_rrq *rrq;
  708. struct lpfc_node_rrq *nextrrq;
  709. unsigned long next_time;
  710. unsigned long iflags;
  711. LIST_HEAD(rrq_list);
  712. if (phba->sli_rev != LPFC_SLI_REV4)
  713. return;
  714. spin_lock_irqsave(&phba->hbalock, iflags);
  715. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  716. next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2));
  717. list_splice_init(&phba->active_rrq_list, &rrq_list);
  718. spin_unlock_irqrestore(&phba->hbalock, iflags);
  719. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  720. list_del(&rrq->list);
  721. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  722. }
  723. if (!list_empty(&phba->active_rrq_list))
  724. mod_timer(&phba->rrq_tmr, next_time);
  725. }
  726. /**
  727. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  728. * @phba: Pointer to HBA context object.
  729. * @ndlp: Targets nodelist pointer for this exchange.
  730. * @xritag the xri in the bitmap to test.
  731. *
  732. * This function is called with hbalock held. This function
  733. * returns 0 = rrq not active for this xri
  734. * 1 = rrq is valid for this xri.
  735. **/
  736. int
  737. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  738. uint16_t xritag)
  739. {
  740. if (!ndlp)
  741. return 0;
  742. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  743. return 1;
  744. else
  745. return 0;
  746. }
  747. /**
  748. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  749. * @phba: Pointer to HBA context object.
  750. * @ndlp: nodelist pointer for this target.
  751. * @xritag: xri used in this exchange.
  752. * @rxid: Remote Exchange ID.
  753. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  754. *
  755. * This function takes the hbalock.
  756. * The active bit is always set in the active rrq xri_bitmap even
  757. * if there is no slot avaiable for the other rrq information.
  758. *
  759. * returns 0 rrq actived for this xri
  760. * < 0 No memory or invalid ndlp.
  761. **/
  762. int
  763. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  764. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  765. {
  766. unsigned long iflags;
  767. struct lpfc_node_rrq *rrq;
  768. int empty;
  769. if (!ndlp)
  770. return -EINVAL;
  771. if (!phba->cfg_enable_rrq)
  772. return -EINVAL;
  773. spin_lock_irqsave(&phba->hbalock, iflags);
  774. if (phba->pport->load_flag & FC_UNLOADING) {
  775. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  776. goto out;
  777. }
  778. /*
  779. * set the active bit even if there is no mem available.
  780. */
  781. if (NLP_CHK_FREE_REQ(ndlp))
  782. goto out;
  783. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  784. goto out;
  785. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  786. goto out;
  787. spin_unlock_irqrestore(&phba->hbalock, iflags);
  788. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  789. if (!rrq) {
  790. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  791. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  792. " DID:0x%x Send:%d\n",
  793. xritag, rxid, ndlp->nlp_DID, send_rrq);
  794. return -EINVAL;
  795. }
  796. if (phba->cfg_enable_rrq == 1)
  797. rrq->send_rrq = send_rrq;
  798. else
  799. rrq->send_rrq = 0;
  800. rrq->xritag = xritag;
  801. rrq->rrq_stop_time = jiffies +
  802. msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  803. rrq->ndlp = ndlp;
  804. rrq->nlp_DID = ndlp->nlp_DID;
  805. rrq->vport = ndlp->vport;
  806. rrq->rxid = rxid;
  807. spin_lock_irqsave(&phba->hbalock, iflags);
  808. empty = list_empty(&phba->active_rrq_list);
  809. list_add_tail(&rrq->list, &phba->active_rrq_list);
  810. phba->hba_flag |= HBA_RRQ_ACTIVE;
  811. if (empty)
  812. lpfc_worker_wake_up(phba);
  813. spin_unlock_irqrestore(&phba->hbalock, iflags);
  814. return 0;
  815. out:
  816. spin_unlock_irqrestore(&phba->hbalock, iflags);
  817. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  818. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  819. " DID:0x%x Send:%d\n",
  820. xritag, rxid, ndlp->nlp_DID, send_rrq);
  821. return -EINVAL;
  822. }
  823. /**
  824. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  825. * @phba: Pointer to HBA context object.
  826. * @piocb: Pointer to the iocbq.
  827. *
  828. * This function is called with hbalock held. This function
  829. * gets a new driver sglq object from the sglq list. If the
  830. * list is not empty then it is successful, it returns pointer to the newly
  831. * allocated sglq object else it returns NULL.
  832. **/
  833. static struct lpfc_sglq *
  834. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  835. {
  836. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  837. struct lpfc_sglq *sglq = NULL;
  838. struct lpfc_sglq *start_sglq = NULL;
  839. struct lpfc_scsi_buf *lpfc_cmd;
  840. struct lpfc_nodelist *ndlp;
  841. int found = 0;
  842. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  843. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  844. ndlp = lpfc_cmd->rdata->pnode;
  845. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  846. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  847. ndlp = piocbq->context_un.ndlp;
  848. else if (piocbq->iocb_flag & LPFC_IO_LIBDFC)
  849. ndlp = piocbq->context_un.ndlp;
  850. else
  851. ndlp = piocbq->context1;
  852. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  853. start_sglq = sglq;
  854. while (!found) {
  855. if (!sglq)
  856. return NULL;
  857. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  858. /* This xri has an rrq outstanding for this DID.
  859. * put it back in the list and get another xri.
  860. */
  861. list_add_tail(&sglq->list, lpfc_sgl_list);
  862. sglq = NULL;
  863. list_remove_head(lpfc_sgl_list, sglq,
  864. struct lpfc_sglq, list);
  865. if (sglq == start_sglq) {
  866. sglq = NULL;
  867. break;
  868. } else
  869. continue;
  870. }
  871. sglq->ndlp = ndlp;
  872. found = 1;
  873. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  874. sglq->state = SGL_ALLOCATED;
  875. }
  876. return sglq;
  877. }
  878. /**
  879. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  880. * @phba: Pointer to HBA context object.
  881. *
  882. * This function is called with no lock held. This function
  883. * allocates a new driver iocb object from the iocb pool. If the
  884. * allocation is successful, it returns pointer to the newly
  885. * allocated iocb object else it returns NULL.
  886. **/
  887. struct lpfc_iocbq *
  888. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  889. {
  890. struct lpfc_iocbq * iocbq = NULL;
  891. unsigned long iflags;
  892. spin_lock_irqsave(&phba->hbalock, iflags);
  893. iocbq = __lpfc_sli_get_iocbq(phba);
  894. spin_unlock_irqrestore(&phba->hbalock, iflags);
  895. return iocbq;
  896. }
  897. /**
  898. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  899. * @phba: Pointer to HBA context object.
  900. * @iocbq: Pointer to driver iocb object.
  901. *
  902. * This function is called with hbalock held to release driver
  903. * iocb object to the iocb pool. The iotag in the iocb object
  904. * does not change for each use of the iocb object. This function
  905. * clears all other fields of the iocb object when it is freed.
  906. * The sqlq structure that holds the xritag and phys and virtual
  907. * mappings for the scatter gather list is retrieved from the
  908. * active array of sglq. The get of the sglq pointer also clears
  909. * the entry in the array. If the status of the IO indiactes that
  910. * this IO was aborted then the sglq entry it put on the
  911. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  912. * IO has good status or fails for any other reason then the sglq
  913. * entry is added to the free list (lpfc_sgl_list).
  914. **/
  915. static void
  916. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  917. {
  918. struct lpfc_sglq *sglq;
  919. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  920. unsigned long iflag = 0;
  921. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  922. if (iocbq->sli4_xritag == NO_XRI)
  923. sglq = NULL;
  924. else
  925. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  926. if (sglq) {
  927. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  928. (sglq->state != SGL_XRI_ABORTED)) {
  929. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  930. iflag);
  931. list_add(&sglq->list,
  932. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  933. spin_unlock_irqrestore(
  934. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  935. } else {
  936. sglq->state = SGL_FREED;
  937. sglq->ndlp = NULL;
  938. list_add_tail(&sglq->list,
  939. &phba->sli4_hba.lpfc_sgl_list);
  940. /* Check if TXQ queue needs to be serviced */
  941. if (!list_empty(&pring->txq))
  942. lpfc_worker_wake_up(phba);
  943. }
  944. }
  945. /*
  946. * Clean all volatile data fields, preserve iotag and node struct.
  947. */
  948. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  949. iocbq->sli4_lxritag = NO_XRI;
  950. iocbq->sli4_xritag = NO_XRI;
  951. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  952. }
  953. /**
  954. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  955. * @phba: Pointer to HBA context object.
  956. * @iocbq: Pointer to driver iocb object.
  957. *
  958. * This function is called with hbalock held to release driver
  959. * iocb object to the iocb pool. The iotag in the iocb object
  960. * does not change for each use of the iocb object. This function
  961. * clears all other fields of the iocb object when it is freed.
  962. **/
  963. static void
  964. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  965. {
  966. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  967. /*
  968. * Clean all volatile data fields, preserve iotag and node struct.
  969. */
  970. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  971. iocbq->sli4_xritag = NO_XRI;
  972. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  973. }
  974. /**
  975. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  976. * @phba: Pointer to HBA context object.
  977. * @iocbq: Pointer to driver iocb object.
  978. *
  979. * This function is called with hbalock held to release driver
  980. * iocb object to the iocb pool. The iotag in the iocb object
  981. * does not change for each use of the iocb object. This function
  982. * clears all other fields of the iocb object when it is freed.
  983. **/
  984. static void
  985. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  986. {
  987. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  988. phba->iocb_cnt--;
  989. }
  990. /**
  991. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  992. * @phba: Pointer to HBA context object.
  993. * @iocbq: Pointer to driver iocb object.
  994. *
  995. * This function is called with no lock held to release the iocb to
  996. * iocb pool.
  997. **/
  998. void
  999. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1000. {
  1001. unsigned long iflags;
  1002. /*
  1003. * Clean all volatile data fields, preserve iotag and node struct.
  1004. */
  1005. spin_lock_irqsave(&phba->hbalock, iflags);
  1006. __lpfc_sli_release_iocbq(phba, iocbq);
  1007. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1008. }
  1009. /**
  1010. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  1011. * @phba: Pointer to HBA context object.
  1012. * @iocblist: List of IOCBs.
  1013. * @ulpstatus: ULP status in IOCB command field.
  1014. * @ulpWord4: ULP word-4 in IOCB command field.
  1015. *
  1016. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  1017. * on the list by invoking the complete callback function associated with the
  1018. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  1019. * fields.
  1020. **/
  1021. void
  1022. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  1023. uint32_t ulpstatus, uint32_t ulpWord4)
  1024. {
  1025. struct lpfc_iocbq *piocb;
  1026. while (!list_empty(iocblist)) {
  1027. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  1028. if (!piocb->iocb_cmpl)
  1029. lpfc_sli_release_iocbq(phba, piocb);
  1030. else {
  1031. piocb->iocb.ulpStatus = ulpstatus;
  1032. piocb->iocb.un.ulpWord[4] = ulpWord4;
  1033. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1034. }
  1035. }
  1036. return;
  1037. }
  1038. /**
  1039. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1040. * @iocb_cmnd: iocb command code.
  1041. *
  1042. * This function is called by ring event handler function to get the iocb type.
  1043. * This function translates the iocb command to an iocb command type used to
  1044. * decide the final disposition of each completed IOCB.
  1045. * The function returns
  1046. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1047. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1048. * LPFC_ABORT_IOCB if it is an abort iocb
  1049. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1050. *
  1051. * The caller is not required to hold any lock.
  1052. **/
  1053. static lpfc_iocb_type
  1054. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1055. {
  1056. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1057. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1058. return 0;
  1059. switch (iocb_cmnd) {
  1060. case CMD_XMIT_SEQUENCE_CR:
  1061. case CMD_XMIT_SEQUENCE_CX:
  1062. case CMD_XMIT_BCAST_CN:
  1063. case CMD_XMIT_BCAST_CX:
  1064. case CMD_ELS_REQUEST_CR:
  1065. case CMD_ELS_REQUEST_CX:
  1066. case CMD_CREATE_XRI_CR:
  1067. case CMD_CREATE_XRI_CX:
  1068. case CMD_GET_RPI_CN:
  1069. case CMD_XMIT_ELS_RSP_CX:
  1070. case CMD_GET_RPI_CR:
  1071. case CMD_FCP_IWRITE_CR:
  1072. case CMD_FCP_IWRITE_CX:
  1073. case CMD_FCP_IREAD_CR:
  1074. case CMD_FCP_IREAD_CX:
  1075. case CMD_FCP_ICMND_CR:
  1076. case CMD_FCP_ICMND_CX:
  1077. case CMD_FCP_TSEND_CX:
  1078. case CMD_FCP_TRSP_CX:
  1079. case CMD_FCP_TRECEIVE_CX:
  1080. case CMD_FCP_AUTO_TRSP_CX:
  1081. case CMD_ADAPTER_MSG:
  1082. case CMD_ADAPTER_DUMP:
  1083. case CMD_XMIT_SEQUENCE64_CR:
  1084. case CMD_XMIT_SEQUENCE64_CX:
  1085. case CMD_XMIT_BCAST64_CN:
  1086. case CMD_XMIT_BCAST64_CX:
  1087. case CMD_ELS_REQUEST64_CR:
  1088. case CMD_ELS_REQUEST64_CX:
  1089. case CMD_FCP_IWRITE64_CR:
  1090. case CMD_FCP_IWRITE64_CX:
  1091. case CMD_FCP_IREAD64_CR:
  1092. case CMD_FCP_IREAD64_CX:
  1093. case CMD_FCP_ICMND64_CR:
  1094. case CMD_FCP_ICMND64_CX:
  1095. case CMD_FCP_TSEND64_CX:
  1096. case CMD_FCP_TRSP64_CX:
  1097. case CMD_FCP_TRECEIVE64_CX:
  1098. case CMD_GEN_REQUEST64_CR:
  1099. case CMD_GEN_REQUEST64_CX:
  1100. case CMD_XMIT_ELS_RSP64_CX:
  1101. case DSSCMD_IWRITE64_CR:
  1102. case DSSCMD_IWRITE64_CX:
  1103. case DSSCMD_IREAD64_CR:
  1104. case DSSCMD_IREAD64_CX:
  1105. type = LPFC_SOL_IOCB;
  1106. break;
  1107. case CMD_ABORT_XRI_CN:
  1108. case CMD_ABORT_XRI_CX:
  1109. case CMD_CLOSE_XRI_CN:
  1110. case CMD_CLOSE_XRI_CX:
  1111. case CMD_XRI_ABORTED_CX:
  1112. case CMD_ABORT_MXRI64_CN:
  1113. case CMD_XMIT_BLS_RSP64_CX:
  1114. type = LPFC_ABORT_IOCB;
  1115. break;
  1116. case CMD_RCV_SEQUENCE_CX:
  1117. case CMD_RCV_ELS_REQ_CX:
  1118. case CMD_RCV_SEQUENCE64_CX:
  1119. case CMD_RCV_ELS_REQ64_CX:
  1120. case CMD_ASYNC_STATUS:
  1121. case CMD_IOCB_RCV_SEQ64_CX:
  1122. case CMD_IOCB_RCV_ELS64_CX:
  1123. case CMD_IOCB_RCV_CONT64_CX:
  1124. case CMD_IOCB_RET_XRI64_CX:
  1125. type = LPFC_UNSOL_IOCB;
  1126. break;
  1127. case CMD_IOCB_XMIT_MSEQ64_CR:
  1128. case CMD_IOCB_XMIT_MSEQ64_CX:
  1129. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1130. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1131. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1132. case CMD_IOCB_ABORT_EXTENDED_CN:
  1133. case CMD_IOCB_RET_HBQE64_CN:
  1134. case CMD_IOCB_FCP_IBIDIR64_CR:
  1135. case CMD_IOCB_FCP_IBIDIR64_CX:
  1136. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1137. case CMD_IOCB_LOGENTRY_CN:
  1138. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1139. printk("%s - Unhandled SLI-3 Command x%x\n",
  1140. __func__, iocb_cmnd);
  1141. type = LPFC_UNKNOWN_IOCB;
  1142. break;
  1143. default:
  1144. type = LPFC_UNKNOWN_IOCB;
  1145. break;
  1146. }
  1147. return type;
  1148. }
  1149. /**
  1150. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1151. * @phba: Pointer to HBA context object.
  1152. *
  1153. * This function is called from SLI initialization code
  1154. * to configure every ring of the HBA's SLI interface. The
  1155. * caller is not required to hold any lock. This function issues
  1156. * a config_ring mailbox command for each ring.
  1157. * This function returns zero if successful else returns a negative
  1158. * error code.
  1159. **/
  1160. static int
  1161. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1162. {
  1163. struct lpfc_sli *psli = &phba->sli;
  1164. LPFC_MBOXQ_t *pmb;
  1165. MAILBOX_t *pmbox;
  1166. int i, rc, ret = 0;
  1167. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1168. if (!pmb)
  1169. return -ENOMEM;
  1170. pmbox = &pmb->u.mb;
  1171. phba->link_state = LPFC_INIT_MBX_CMDS;
  1172. for (i = 0; i < psli->num_rings; i++) {
  1173. lpfc_config_ring(phba, i, pmb);
  1174. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1175. if (rc != MBX_SUCCESS) {
  1176. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1177. "0446 Adapter failed to init (%d), "
  1178. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1179. "ring %d\n",
  1180. rc, pmbox->mbxCommand,
  1181. pmbox->mbxStatus, i);
  1182. phba->link_state = LPFC_HBA_ERROR;
  1183. ret = -ENXIO;
  1184. break;
  1185. }
  1186. }
  1187. mempool_free(pmb, phba->mbox_mem_pool);
  1188. return ret;
  1189. }
  1190. /**
  1191. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1192. * @phba: Pointer to HBA context object.
  1193. * @pring: Pointer to driver SLI ring object.
  1194. * @piocb: Pointer to the driver iocb object.
  1195. *
  1196. * This function is called with hbalock held. The function adds the
  1197. * new iocb to txcmplq of the given ring. This function always returns
  1198. * 0. If this function is called for ELS ring, this function checks if
  1199. * there is a vport associated with the ELS command. This function also
  1200. * starts els_tmofunc timer if this is an ELS command.
  1201. **/
  1202. static int
  1203. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1204. struct lpfc_iocbq *piocb)
  1205. {
  1206. list_add_tail(&piocb->list, &pring->txcmplq);
  1207. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1208. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1209. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1210. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1211. if (!piocb->vport)
  1212. BUG();
  1213. else
  1214. mod_timer(&piocb->vport->els_tmofunc,
  1215. jiffies +
  1216. msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
  1217. }
  1218. return 0;
  1219. }
  1220. /**
  1221. * lpfc_sli_ringtx_get - Get first element of the txq
  1222. * @phba: Pointer to HBA context object.
  1223. * @pring: Pointer to driver SLI ring object.
  1224. *
  1225. * This function is called with hbalock held to get next
  1226. * iocb in txq of the given ring. If there is any iocb in
  1227. * the txq, the function returns first iocb in the list after
  1228. * removing the iocb from the list, else it returns NULL.
  1229. **/
  1230. struct lpfc_iocbq *
  1231. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1232. {
  1233. struct lpfc_iocbq *cmd_iocb;
  1234. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1235. return cmd_iocb;
  1236. }
  1237. /**
  1238. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1239. * @phba: Pointer to HBA context object.
  1240. * @pring: Pointer to driver SLI ring object.
  1241. *
  1242. * This function is called with hbalock held and the caller must post the
  1243. * iocb without releasing the lock. If the caller releases the lock,
  1244. * iocb slot returned by the function is not guaranteed to be available.
  1245. * The function returns pointer to the next available iocb slot if there
  1246. * is available slot in the ring, else it returns NULL.
  1247. * If the get index of the ring is ahead of the put index, the function
  1248. * will post an error attention event to the worker thread to take the
  1249. * HBA to offline state.
  1250. **/
  1251. static IOCB_t *
  1252. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1253. {
  1254. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1255. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1256. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1257. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1258. pring->sli.sli3.next_cmdidx = 0;
  1259. if (unlikely(pring->sli.sli3.local_getidx ==
  1260. pring->sli.sli3.next_cmdidx)) {
  1261. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1262. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1263. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1264. "0315 Ring %d issue: portCmdGet %d "
  1265. "is bigger than cmd ring %d\n",
  1266. pring->ringno,
  1267. pring->sli.sli3.local_getidx,
  1268. max_cmd_idx);
  1269. phba->link_state = LPFC_HBA_ERROR;
  1270. /*
  1271. * All error attention handlers are posted to
  1272. * worker thread
  1273. */
  1274. phba->work_ha |= HA_ERATT;
  1275. phba->work_hs = HS_FFER3;
  1276. lpfc_worker_wake_up(phba);
  1277. return NULL;
  1278. }
  1279. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1280. return NULL;
  1281. }
  1282. return lpfc_cmd_iocb(phba, pring);
  1283. }
  1284. /**
  1285. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1286. * @phba: Pointer to HBA context object.
  1287. * @iocbq: Pointer to driver iocb object.
  1288. *
  1289. * This function gets an iotag for the iocb. If there is no unused iotag and
  1290. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1291. * array and assigns a new iotag.
  1292. * The function returns the allocated iotag if successful, else returns zero.
  1293. * Zero is not a valid iotag.
  1294. * The caller is not required to hold any lock.
  1295. **/
  1296. uint16_t
  1297. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1298. {
  1299. struct lpfc_iocbq **new_arr;
  1300. struct lpfc_iocbq **old_arr;
  1301. size_t new_len;
  1302. struct lpfc_sli *psli = &phba->sli;
  1303. uint16_t iotag;
  1304. spin_lock_irq(&phba->hbalock);
  1305. iotag = psli->last_iotag;
  1306. if(++iotag < psli->iocbq_lookup_len) {
  1307. psli->last_iotag = iotag;
  1308. psli->iocbq_lookup[iotag] = iocbq;
  1309. spin_unlock_irq(&phba->hbalock);
  1310. iocbq->iotag = iotag;
  1311. return iotag;
  1312. } else if (psli->iocbq_lookup_len < (0xffff
  1313. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1314. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1315. spin_unlock_irq(&phba->hbalock);
  1316. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1317. GFP_KERNEL);
  1318. if (new_arr) {
  1319. spin_lock_irq(&phba->hbalock);
  1320. old_arr = psli->iocbq_lookup;
  1321. if (new_len <= psli->iocbq_lookup_len) {
  1322. /* highly unprobable case */
  1323. kfree(new_arr);
  1324. iotag = psli->last_iotag;
  1325. if(++iotag < psli->iocbq_lookup_len) {
  1326. psli->last_iotag = iotag;
  1327. psli->iocbq_lookup[iotag] = iocbq;
  1328. spin_unlock_irq(&phba->hbalock);
  1329. iocbq->iotag = iotag;
  1330. return iotag;
  1331. }
  1332. spin_unlock_irq(&phba->hbalock);
  1333. return 0;
  1334. }
  1335. if (psli->iocbq_lookup)
  1336. memcpy(new_arr, old_arr,
  1337. ((psli->last_iotag + 1) *
  1338. sizeof (struct lpfc_iocbq *)));
  1339. psli->iocbq_lookup = new_arr;
  1340. psli->iocbq_lookup_len = new_len;
  1341. psli->last_iotag = iotag;
  1342. psli->iocbq_lookup[iotag] = iocbq;
  1343. spin_unlock_irq(&phba->hbalock);
  1344. iocbq->iotag = iotag;
  1345. kfree(old_arr);
  1346. return iotag;
  1347. }
  1348. } else
  1349. spin_unlock_irq(&phba->hbalock);
  1350. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1351. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1352. psli->last_iotag);
  1353. return 0;
  1354. }
  1355. /**
  1356. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1357. * @phba: Pointer to HBA context object.
  1358. * @pring: Pointer to driver SLI ring object.
  1359. * @iocb: Pointer to iocb slot in the ring.
  1360. * @nextiocb: Pointer to driver iocb object which need to be
  1361. * posted to firmware.
  1362. *
  1363. * This function is called with hbalock held to post a new iocb to
  1364. * the firmware. This function copies the new iocb to ring iocb slot and
  1365. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1366. * a completion call back for this iocb else the function will free the
  1367. * iocb object.
  1368. **/
  1369. static void
  1370. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1371. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1372. {
  1373. /*
  1374. * Set up an iotag
  1375. */
  1376. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1377. if (pring->ringno == LPFC_ELS_RING) {
  1378. lpfc_debugfs_slow_ring_trc(phba,
  1379. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1380. *(((uint32_t *) &nextiocb->iocb) + 4),
  1381. *(((uint32_t *) &nextiocb->iocb) + 6),
  1382. *(((uint32_t *) &nextiocb->iocb) + 7));
  1383. }
  1384. /*
  1385. * Issue iocb command to adapter
  1386. */
  1387. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1388. wmb();
  1389. pring->stats.iocb_cmd++;
  1390. /*
  1391. * If there is no completion routine to call, we can release the
  1392. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1393. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1394. */
  1395. if (nextiocb->iocb_cmpl)
  1396. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1397. else
  1398. __lpfc_sli_release_iocbq(phba, nextiocb);
  1399. /*
  1400. * Let the HBA know what IOCB slot will be the next one the
  1401. * driver will put a command into.
  1402. */
  1403. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1404. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1405. }
  1406. /**
  1407. * lpfc_sli_update_full_ring - Update the chip attention register
  1408. * @phba: Pointer to HBA context object.
  1409. * @pring: Pointer to driver SLI ring object.
  1410. *
  1411. * The caller is not required to hold any lock for calling this function.
  1412. * This function updates the chip attention bits for the ring to inform firmware
  1413. * that there are pending work to be done for this ring and requests an
  1414. * interrupt when there is space available in the ring. This function is
  1415. * called when the driver is unable to post more iocbs to the ring due
  1416. * to unavailability of space in the ring.
  1417. **/
  1418. static void
  1419. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1420. {
  1421. int ringno = pring->ringno;
  1422. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1423. wmb();
  1424. /*
  1425. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1426. * The HBA will tell us when an IOCB entry is available.
  1427. */
  1428. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1429. readl(phba->CAregaddr); /* flush */
  1430. pring->stats.iocb_cmd_full++;
  1431. }
  1432. /**
  1433. * lpfc_sli_update_ring - Update chip attention register
  1434. * @phba: Pointer to HBA context object.
  1435. * @pring: Pointer to driver SLI ring object.
  1436. *
  1437. * This function updates the chip attention register bit for the
  1438. * given ring to inform HBA that there is more work to be done
  1439. * in this ring. The caller is not required to hold any lock.
  1440. **/
  1441. static void
  1442. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1443. {
  1444. int ringno = pring->ringno;
  1445. /*
  1446. * Tell the HBA that there is work to do in this ring.
  1447. */
  1448. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1449. wmb();
  1450. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1451. readl(phba->CAregaddr); /* flush */
  1452. }
  1453. }
  1454. /**
  1455. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1456. * @phba: Pointer to HBA context object.
  1457. * @pring: Pointer to driver SLI ring object.
  1458. *
  1459. * This function is called with hbalock held to post pending iocbs
  1460. * in the txq to the firmware. This function is called when driver
  1461. * detects space available in the ring.
  1462. **/
  1463. static void
  1464. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1465. {
  1466. IOCB_t *iocb;
  1467. struct lpfc_iocbq *nextiocb;
  1468. /*
  1469. * Check to see if:
  1470. * (a) there is anything on the txq to send
  1471. * (b) link is up
  1472. * (c) link attention events can be processed (fcp ring only)
  1473. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1474. */
  1475. if (lpfc_is_link_up(phba) &&
  1476. (!list_empty(&pring->txq)) &&
  1477. (pring->ringno != phba->sli.fcp_ring ||
  1478. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1479. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1480. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1481. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1482. if (iocb)
  1483. lpfc_sli_update_ring(phba, pring);
  1484. else
  1485. lpfc_sli_update_full_ring(phba, pring);
  1486. }
  1487. return;
  1488. }
  1489. /**
  1490. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1491. * @phba: Pointer to HBA context object.
  1492. * @hbqno: HBQ number.
  1493. *
  1494. * This function is called with hbalock held to get the next
  1495. * available slot for the given HBQ. If there is free slot
  1496. * available for the HBQ it will return pointer to the next available
  1497. * HBQ entry else it will return NULL.
  1498. **/
  1499. static struct lpfc_hbq_entry *
  1500. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1501. {
  1502. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1503. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1504. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1505. hbqp->next_hbqPutIdx = 0;
  1506. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1507. uint32_t raw_index = phba->hbq_get[hbqno];
  1508. uint32_t getidx = le32_to_cpu(raw_index);
  1509. hbqp->local_hbqGetIdx = getidx;
  1510. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1511. lpfc_printf_log(phba, KERN_ERR,
  1512. LOG_SLI | LOG_VPORT,
  1513. "1802 HBQ %d: local_hbqGetIdx "
  1514. "%u is > than hbqp->entry_count %u\n",
  1515. hbqno, hbqp->local_hbqGetIdx,
  1516. hbqp->entry_count);
  1517. phba->link_state = LPFC_HBA_ERROR;
  1518. return NULL;
  1519. }
  1520. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1521. return NULL;
  1522. }
  1523. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1524. hbqp->hbqPutIdx;
  1525. }
  1526. /**
  1527. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1528. * @phba: Pointer to HBA context object.
  1529. *
  1530. * This function is called with no lock held to free all the
  1531. * hbq buffers while uninitializing the SLI interface. It also
  1532. * frees the HBQ buffers returned by the firmware but not yet
  1533. * processed by the upper layers.
  1534. **/
  1535. void
  1536. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1537. {
  1538. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1539. struct hbq_dmabuf *hbq_buf;
  1540. unsigned long flags;
  1541. int i, hbq_count;
  1542. uint32_t hbqno;
  1543. hbq_count = lpfc_sli_hbq_count();
  1544. /* Return all memory used by all HBQs */
  1545. spin_lock_irqsave(&phba->hbalock, flags);
  1546. for (i = 0; i < hbq_count; ++i) {
  1547. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1548. &phba->hbqs[i].hbq_buffer_list, list) {
  1549. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1550. list_del(&hbq_buf->dbuf.list);
  1551. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1552. }
  1553. phba->hbqs[i].buffer_count = 0;
  1554. }
  1555. /* Return all HBQ buffer that are in-fly */
  1556. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1557. list) {
  1558. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1559. list_del(&hbq_buf->dbuf.list);
  1560. if (hbq_buf->tag == -1) {
  1561. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1562. (phba, hbq_buf);
  1563. } else {
  1564. hbqno = hbq_buf->tag >> 16;
  1565. if (hbqno >= LPFC_MAX_HBQS)
  1566. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1567. (phba, hbq_buf);
  1568. else
  1569. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1570. hbq_buf);
  1571. }
  1572. }
  1573. /* Mark the HBQs not in use */
  1574. phba->hbq_in_use = 0;
  1575. spin_unlock_irqrestore(&phba->hbalock, flags);
  1576. }
  1577. /**
  1578. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1579. * @phba: Pointer to HBA context object.
  1580. * @hbqno: HBQ number.
  1581. * @hbq_buf: Pointer to HBQ buffer.
  1582. *
  1583. * This function is called with the hbalock held to post a
  1584. * hbq buffer to the firmware. If the function finds an empty
  1585. * slot in the HBQ, it will post the buffer. The function will return
  1586. * pointer to the hbq entry if it successfully post the buffer
  1587. * else it will return NULL.
  1588. **/
  1589. static int
  1590. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1591. struct hbq_dmabuf *hbq_buf)
  1592. {
  1593. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1594. }
  1595. /**
  1596. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1597. * @phba: Pointer to HBA context object.
  1598. * @hbqno: HBQ number.
  1599. * @hbq_buf: Pointer to HBQ buffer.
  1600. *
  1601. * This function is called with the hbalock held to post a hbq buffer to the
  1602. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1603. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1604. * it successfully post the buffer else it will return an error.
  1605. **/
  1606. static int
  1607. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1608. struct hbq_dmabuf *hbq_buf)
  1609. {
  1610. struct lpfc_hbq_entry *hbqe;
  1611. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1612. /* Get next HBQ entry slot to use */
  1613. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1614. if (hbqe) {
  1615. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1616. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1617. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1618. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1619. hbqe->bde.tus.f.bdeFlags = 0;
  1620. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1621. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1622. /* Sync SLIM */
  1623. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1624. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1625. /* flush */
  1626. readl(phba->hbq_put + hbqno);
  1627. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1628. return 0;
  1629. } else
  1630. return -ENOMEM;
  1631. }
  1632. /**
  1633. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1634. * @phba: Pointer to HBA context object.
  1635. * @hbqno: HBQ number.
  1636. * @hbq_buf: Pointer to HBQ buffer.
  1637. *
  1638. * This function is called with the hbalock held to post an RQE to the SLI4
  1639. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1640. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1641. **/
  1642. static int
  1643. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1644. struct hbq_dmabuf *hbq_buf)
  1645. {
  1646. int rc;
  1647. struct lpfc_rqe hrqe;
  1648. struct lpfc_rqe drqe;
  1649. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1650. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1651. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1652. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1653. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1654. &hrqe, &drqe);
  1655. if (rc < 0)
  1656. return rc;
  1657. hbq_buf->tag = rc;
  1658. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1659. return 0;
  1660. }
  1661. /* HBQ for ELS and CT traffic. */
  1662. static struct lpfc_hbq_init lpfc_els_hbq = {
  1663. .rn = 1,
  1664. .entry_count = 256,
  1665. .mask_count = 0,
  1666. .profile = 0,
  1667. .ring_mask = (1 << LPFC_ELS_RING),
  1668. .buffer_count = 0,
  1669. .init_count = 40,
  1670. .add_count = 40,
  1671. };
  1672. /* HBQ for the extra ring if needed */
  1673. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1674. .rn = 1,
  1675. .entry_count = 200,
  1676. .mask_count = 0,
  1677. .profile = 0,
  1678. .ring_mask = (1 << LPFC_EXTRA_RING),
  1679. .buffer_count = 0,
  1680. .init_count = 0,
  1681. .add_count = 5,
  1682. };
  1683. /* Array of HBQs */
  1684. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1685. &lpfc_els_hbq,
  1686. &lpfc_extra_hbq,
  1687. };
  1688. /**
  1689. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1690. * @phba: Pointer to HBA context object.
  1691. * @hbqno: HBQ number.
  1692. * @count: Number of HBQ buffers to be posted.
  1693. *
  1694. * This function is called with no lock held to post more hbq buffers to the
  1695. * given HBQ. The function returns the number of HBQ buffers successfully
  1696. * posted.
  1697. **/
  1698. static int
  1699. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1700. {
  1701. uint32_t i, posted = 0;
  1702. unsigned long flags;
  1703. struct hbq_dmabuf *hbq_buffer;
  1704. LIST_HEAD(hbq_buf_list);
  1705. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1706. return 0;
  1707. if ((phba->hbqs[hbqno].buffer_count + count) >
  1708. lpfc_hbq_defs[hbqno]->entry_count)
  1709. count = lpfc_hbq_defs[hbqno]->entry_count -
  1710. phba->hbqs[hbqno].buffer_count;
  1711. if (!count)
  1712. return 0;
  1713. /* Allocate HBQ entries */
  1714. for (i = 0; i < count; i++) {
  1715. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1716. if (!hbq_buffer)
  1717. break;
  1718. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1719. }
  1720. /* Check whether HBQ is still in use */
  1721. spin_lock_irqsave(&phba->hbalock, flags);
  1722. if (!phba->hbq_in_use)
  1723. goto err;
  1724. while (!list_empty(&hbq_buf_list)) {
  1725. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1726. dbuf.list);
  1727. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1728. (hbqno << 16));
  1729. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1730. phba->hbqs[hbqno].buffer_count++;
  1731. posted++;
  1732. } else
  1733. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1734. }
  1735. spin_unlock_irqrestore(&phba->hbalock, flags);
  1736. return posted;
  1737. err:
  1738. spin_unlock_irqrestore(&phba->hbalock, flags);
  1739. while (!list_empty(&hbq_buf_list)) {
  1740. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1741. dbuf.list);
  1742. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1743. }
  1744. return 0;
  1745. }
  1746. /**
  1747. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1748. * @phba: Pointer to HBA context object.
  1749. * @qno: HBQ number.
  1750. *
  1751. * This function posts more buffers to the HBQ. This function
  1752. * is called with no lock held. The function returns the number of HBQ entries
  1753. * successfully allocated.
  1754. **/
  1755. int
  1756. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1757. {
  1758. if (phba->sli_rev == LPFC_SLI_REV4)
  1759. return 0;
  1760. else
  1761. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1762. lpfc_hbq_defs[qno]->add_count);
  1763. }
  1764. /**
  1765. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1766. * @phba: Pointer to HBA context object.
  1767. * @qno: HBQ queue number.
  1768. *
  1769. * This function is called from SLI initialization code path with
  1770. * no lock held to post initial HBQ buffers to firmware. The
  1771. * function returns the number of HBQ entries successfully allocated.
  1772. **/
  1773. static int
  1774. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1775. {
  1776. if (phba->sli_rev == LPFC_SLI_REV4)
  1777. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1778. lpfc_hbq_defs[qno]->entry_count);
  1779. else
  1780. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1781. lpfc_hbq_defs[qno]->init_count);
  1782. }
  1783. /**
  1784. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1785. * @phba: Pointer to HBA context object.
  1786. * @hbqno: HBQ number.
  1787. *
  1788. * This function removes the first hbq buffer on an hbq list and returns a
  1789. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1790. **/
  1791. static struct hbq_dmabuf *
  1792. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1793. {
  1794. struct lpfc_dmabuf *d_buf;
  1795. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1796. if (!d_buf)
  1797. return NULL;
  1798. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1799. }
  1800. /**
  1801. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1802. * @phba: Pointer to HBA context object.
  1803. * @tag: Tag of the hbq buffer.
  1804. *
  1805. * This function is called with hbalock held. This function searches
  1806. * for the hbq buffer associated with the given tag in the hbq buffer
  1807. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1808. * it returns NULL.
  1809. **/
  1810. static struct hbq_dmabuf *
  1811. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1812. {
  1813. struct lpfc_dmabuf *d_buf;
  1814. struct hbq_dmabuf *hbq_buf;
  1815. uint32_t hbqno;
  1816. hbqno = tag >> 16;
  1817. if (hbqno >= LPFC_MAX_HBQS)
  1818. return NULL;
  1819. spin_lock_irq(&phba->hbalock);
  1820. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1821. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1822. if (hbq_buf->tag == tag) {
  1823. spin_unlock_irq(&phba->hbalock);
  1824. return hbq_buf;
  1825. }
  1826. }
  1827. spin_unlock_irq(&phba->hbalock);
  1828. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1829. "1803 Bad hbq tag. Data: x%x x%x\n",
  1830. tag, phba->hbqs[tag >> 16].buffer_count);
  1831. return NULL;
  1832. }
  1833. /**
  1834. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1835. * @phba: Pointer to HBA context object.
  1836. * @hbq_buffer: Pointer to HBQ buffer.
  1837. *
  1838. * This function is called with hbalock. This function gives back
  1839. * the hbq buffer to firmware. If the HBQ does not have space to
  1840. * post the buffer, it will free the buffer.
  1841. **/
  1842. void
  1843. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1844. {
  1845. uint32_t hbqno;
  1846. if (hbq_buffer) {
  1847. hbqno = hbq_buffer->tag >> 16;
  1848. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1849. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1850. }
  1851. }
  1852. /**
  1853. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1854. * @mbxCommand: mailbox command code.
  1855. *
  1856. * This function is called by the mailbox event handler function to verify
  1857. * that the completed mailbox command is a legitimate mailbox command. If the
  1858. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1859. * and the mailbox event handler will take the HBA offline.
  1860. **/
  1861. static int
  1862. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1863. {
  1864. uint8_t ret;
  1865. switch (mbxCommand) {
  1866. case MBX_LOAD_SM:
  1867. case MBX_READ_NV:
  1868. case MBX_WRITE_NV:
  1869. case MBX_WRITE_VPARMS:
  1870. case MBX_RUN_BIU_DIAG:
  1871. case MBX_INIT_LINK:
  1872. case MBX_DOWN_LINK:
  1873. case MBX_CONFIG_LINK:
  1874. case MBX_CONFIG_RING:
  1875. case MBX_RESET_RING:
  1876. case MBX_READ_CONFIG:
  1877. case MBX_READ_RCONFIG:
  1878. case MBX_READ_SPARM:
  1879. case MBX_READ_STATUS:
  1880. case MBX_READ_RPI:
  1881. case MBX_READ_XRI:
  1882. case MBX_READ_REV:
  1883. case MBX_READ_LNK_STAT:
  1884. case MBX_REG_LOGIN:
  1885. case MBX_UNREG_LOGIN:
  1886. case MBX_CLEAR_LA:
  1887. case MBX_DUMP_MEMORY:
  1888. case MBX_DUMP_CONTEXT:
  1889. case MBX_RUN_DIAGS:
  1890. case MBX_RESTART:
  1891. case MBX_UPDATE_CFG:
  1892. case MBX_DOWN_LOAD:
  1893. case MBX_DEL_LD_ENTRY:
  1894. case MBX_RUN_PROGRAM:
  1895. case MBX_SET_MASK:
  1896. case MBX_SET_VARIABLE:
  1897. case MBX_UNREG_D_ID:
  1898. case MBX_KILL_BOARD:
  1899. case MBX_CONFIG_FARP:
  1900. case MBX_BEACON:
  1901. case MBX_LOAD_AREA:
  1902. case MBX_RUN_BIU_DIAG64:
  1903. case MBX_CONFIG_PORT:
  1904. case MBX_READ_SPARM64:
  1905. case MBX_READ_RPI64:
  1906. case MBX_REG_LOGIN64:
  1907. case MBX_READ_TOPOLOGY:
  1908. case MBX_WRITE_WWN:
  1909. case MBX_SET_DEBUG:
  1910. case MBX_LOAD_EXP_ROM:
  1911. case MBX_ASYNCEVT_ENABLE:
  1912. case MBX_REG_VPI:
  1913. case MBX_UNREG_VPI:
  1914. case MBX_HEARTBEAT:
  1915. case MBX_PORT_CAPABILITIES:
  1916. case MBX_PORT_IOV_CONTROL:
  1917. case MBX_SLI4_CONFIG:
  1918. case MBX_SLI4_REQ_FTRS:
  1919. case MBX_REG_FCFI:
  1920. case MBX_UNREG_FCFI:
  1921. case MBX_REG_VFI:
  1922. case MBX_UNREG_VFI:
  1923. case MBX_INIT_VPI:
  1924. case MBX_INIT_VFI:
  1925. case MBX_RESUME_RPI:
  1926. case MBX_READ_EVENT_LOG_STATUS:
  1927. case MBX_READ_EVENT_LOG:
  1928. case MBX_SECURITY_MGMT:
  1929. case MBX_AUTH_PORT:
  1930. case MBX_ACCESS_VDATA:
  1931. ret = mbxCommand;
  1932. break;
  1933. default:
  1934. ret = MBX_SHUTDOWN;
  1935. break;
  1936. }
  1937. return ret;
  1938. }
  1939. /**
  1940. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1941. * @phba: Pointer to HBA context object.
  1942. * @pmboxq: Pointer to mailbox command.
  1943. *
  1944. * This is completion handler function for mailbox commands issued from
  1945. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1946. * mailbox event handler function with no lock held. This function
  1947. * will wake up thread waiting on the wait queue pointed by context1
  1948. * of the mailbox.
  1949. **/
  1950. void
  1951. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1952. {
  1953. wait_queue_head_t *pdone_q;
  1954. unsigned long drvr_flag;
  1955. /*
  1956. * If pdone_q is empty, the driver thread gave up waiting and
  1957. * continued running.
  1958. */
  1959. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1960. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1961. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1962. if (pdone_q)
  1963. wake_up_interruptible(pdone_q);
  1964. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1965. return;
  1966. }
  1967. /**
  1968. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1969. * @phba: Pointer to HBA context object.
  1970. * @pmb: Pointer to mailbox object.
  1971. *
  1972. * This function is the default mailbox completion handler. It
  1973. * frees the memory resources associated with the completed mailbox
  1974. * command. If the completed command is a REG_LOGIN mailbox command,
  1975. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1976. **/
  1977. void
  1978. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1979. {
  1980. struct lpfc_vport *vport = pmb->vport;
  1981. struct lpfc_dmabuf *mp;
  1982. struct lpfc_nodelist *ndlp;
  1983. struct Scsi_Host *shost;
  1984. uint16_t rpi, vpi;
  1985. int rc;
  1986. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1987. if (mp) {
  1988. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1989. kfree(mp);
  1990. }
  1991. /*
  1992. * If a REG_LOGIN succeeded after node is destroyed or node
  1993. * is in re-discovery driver need to cleanup the RPI.
  1994. */
  1995. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1996. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1997. !pmb->u.mb.mbxStatus) {
  1998. rpi = pmb->u.mb.un.varWords[0];
  1999. vpi = pmb->u.mb.un.varRegLogin.vpi;
  2000. lpfc_unreg_login(phba, vpi, rpi, pmb);
  2001. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2002. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2003. if (rc != MBX_NOT_FINISHED)
  2004. return;
  2005. }
  2006. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  2007. !(phba->pport->load_flag & FC_UNLOADING) &&
  2008. !pmb->u.mb.mbxStatus) {
  2009. shost = lpfc_shost_from_vport(vport);
  2010. spin_lock_irq(shost->host_lock);
  2011. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2012. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2013. spin_unlock_irq(shost->host_lock);
  2014. }
  2015. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  2016. ndlp = (struct lpfc_nodelist *)pmb->context2;
  2017. lpfc_nlp_put(ndlp);
  2018. pmb->context2 = NULL;
  2019. }
  2020. /* Check security permission status on INIT_LINK mailbox command */
  2021. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  2022. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  2023. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2024. "2860 SLI authentication is required "
  2025. "for INIT_LINK but has not done yet\n");
  2026. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  2027. lpfc_sli4_mbox_cmd_free(phba, pmb);
  2028. else
  2029. mempool_free(pmb, phba->mbox_mem_pool);
  2030. }
  2031. /**
  2032. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2033. * @phba: Pointer to HBA context object.
  2034. *
  2035. * This function is called with no lock held. This function processes all
  2036. * the completed mailbox commands and gives it to upper layers. The interrupt
  2037. * service routine processes mailbox completion interrupt and adds completed
  2038. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2039. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2040. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2041. * function returns the mailbox commands to the upper layer by calling the
  2042. * completion handler function of each mailbox.
  2043. **/
  2044. int
  2045. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2046. {
  2047. MAILBOX_t *pmbox;
  2048. LPFC_MBOXQ_t *pmb;
  2049. int rc;
  2050. LIST_HEAD(cmplq);
  2051. phba->sli.slistat.mbox_event++;
  2052. /* Get all completed mailboxe buffers into the cmplq */
  2053. spin_lock_irq(&phba->hbalock);
  2054. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2055. spin_unlock_irq(&phba->hbalock);
  2056. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2057. do {
  2058. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2059. if (pmb == NULL)
  2060. break;
  2061. pmbox = &pmb->u.mb;
  2062. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2063. if (pmb->vport) {
  2064. lpfc_debugfs_disc_trc(pmb->vport,
  2065. LPFC_DISC_TRC_MBOX_VPORT,
  2066. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2067. (uint32_t)pmbox->mbxCommand,
  2068. pmbox->un.varWords[0],
  2069. pmbox->un.varWords[1]);
  2070. }
  2071. else {
  2072. lpfc_debugfs_disc_trc(phba->pport,
  2073. LPFC_DISC_TRC_MBOX,
  2074. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2075. (uint32_t)pmbox->mbxCommand,
  2076. pmbox->un.varWords[0],
  2077. pmbox->un.varWords[1]);
  2078. }
  2079. }
  2080. /*
  2081. * It is a fatal error if unknown mbox command completion.
  2082. */
  2083. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2084. MBX_SHUTDOWN) {
  2085. /* Unknown mailbox command compl */
  2086. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2087. "(%d):0323 Unknown Mailbox command "
  2088. "x%x (x%x/x%x) Cmpl\n",
  2089. pmb->vport ? pmb->vport->vpi : 0,
  2090. pmbox->mbxCommand,
  2091. lpfc_sli_config_mbox_subsys_get(phba,
  2092. pmb),
  2093. lpfc_sli_config_mbox_opcode_get(phba,
  2094. pmb));
  2095. phba->link_state = LPFC_HBA_ERROR;
  2096. phba->work_hs = HS_FFER3;
  2097. lpfc_handle_eratt(phba);
  2098. continue;
  2099. }
  2100. if (pmbox->mbxStatus) {
  2101. phba->sli.slistat.mbox_stat_err++;
  2102. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2103. /* Mbox cmd cmpl error - RETRYing */
  2104. lpfc_printf_log(phba, KERN_INFO,
  2105. LOG_MBOX | LOG_SLI,
  2106. "(%d):0305 Mbox cmd cmpl "
  2107. "error - RETRYing Data: x%x "
  2108. "(x%x/x%x) x%x x%x x%x\n",
  2109. pmb->vport ? pmb->vport->vpi : 0,
  2110. pmbox->mbxCommand,
  2111. lpfc_sli_config_mbox_subsys_get(phba,
  2112. pmb),
  2113. lpfc_sli_config_mbox_opcode_get(phba,
  2114. pmb),
  2115. pmbox->mbxStatus,
  2116. pmbox->un.varWords[0],
  2117. pmb->vport->port_state);
  2118. pmbox->mbxStatus = 0;
  2119. pmbox->mbxOwner = OWN_HOST;
  2120. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2121. if (rc != MBX_NOT_FINISHED)
  2122. continue;
  2123. }
  2124. }
  2125. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2126. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2127. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2128. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  2129. "x%x x%x x%x\n",
  2130. pmb->vport ? pmb->vport->vpi : 0,
  2131. pmbox->mbxCommand,
  2132. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2133. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2134. pmb->mbox_cmpl,
  2135. *((uint32_t *) pmbox),
  2136. pmbox->un.varWords[0],
  2137. pmbox->un.varWords[1],
  2138. pmbox->un.varWords[2],
  2139. pmbox->un.varWords[3],
  2140. pmbox->un.varWords[4],
  2141. pmbox->un.varWords[5],
  2142. pmbox->un.varWords[6],
  2143. pmbox->un.varWords[7],
  2144. pmbox->un.varWords[8],
  2145. pmbox->un.varWords[9],
  2146. pmbox->un.varWords[10]);
  2147. if (pmb->mbox_cmpl)
  2148. pmb->mbox_cmpl(phba,pmb);
  2149. } while (1);
  2150. return 0;
  2151. }
  2152. /**
  2153. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2154. * @phba: Pointer to HBA context object.
  2155. * @pring: Pointer to driver SLI ring object.
  2156. * @tag: buffer tag.
  2157. *
  2158. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2159. * is set in the tag the buffer is posted for a particular exchange,
  2160. * the function will return the buffer without replacing the buffer.
  2161. * If the buffer is for unsolicited ELS or CT traffic, this function
  2162. * returns the buffer and also posts another buffer to the firmware.
  2163. **/
  2164. static struct lpfc_dmabuf *
  2165. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2166. struct lpfc_sli_ring *pring,
  2167. uint32_t tag)
  2168. {
  2169. struct hbq_dmabuf *hbq_entry;
  2170. if (tag & QUE_BUFTAG_BIT)
  2171. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2172. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2173. if (!hbq_entry)
  2174. return NULL;
  2175. return &hbq_entry->dbuf;
  2176. }
  2177. /**
  2178. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2179. * @phba: Pointer to HBA context object.
  2180. * @pring: Pointer to driver SLI ring object.
  2181. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2182. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2183. * @fch_type: the type for the first frame of the sequence.
  2184. *
  2185. * This function is called with no lock held. This function uses the r_ctl and
  2186. * type of the received sequence to find the correct callback function to call
  2187. * to process the sequence.
  2188. **/
  2189. static int
  2190. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2191. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2192. uint32_t fch_type)
  2193. {
  2194. int i;
  2195. /* unSolicited Responses */
  2196. if (pring->prt[0].profile) {
  2197. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2198. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2199. saveq);
  2200. return 1;
  2201. }
  2202. /* We must search, based on rctl / type
  2203. for the right routine */
  2204. for (i = 0; i < pring->num_mask; i++) {
  2205. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2206. (pring->prt[i].type == fch_type)) {
  2207. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2208. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2209. (phba, pring, saveq);
  2210. return 1;
  2211. }
  2212. }
  2213. return 0;
  2214. }
  2215. /**
  2216. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2217. * @phba: Pointer to HBA context object.
  2218. * @pring: Pointer to driver SLI ring object.
  2219. * @saveq: Pointer to the unsolicited iocb.
  2220. *
  2221. * This function is called with no lock held by the ring event handler
  2222. * when there is an unsolicited iocb posted to the response ring by the
  2223. * firmware. This function gets the buffer associated with the iocbs
  2224. * and calls the event handler for the ring. This function handles both
  2225. * qring buffers and hbq buffers.
  2226. * When the function returns 1 the caller can free the iocb object otherwise
  2227. * upper layer functions will free the iocb objects.
  2228. **/
  2229. static int
  2230. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2231. struct lpfc_iocbq *saveq)
  2232. {
  2233. IOCB_t * irsp;
  2234. WORD5 * w5p;
  2235. uint32_t Rctl, Type;
  2236. uint32_t match;
  2237. struct lpfc_iocbq *iocbq;
  2238. struct lpfc_dmabuf *dmzbuf;
  2239. match = 0;
  2240. irsp = &(saveq->iocb);
  2241. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2242. if (pring->lpfc_sli_rcv_async_status)
  2243. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2244. else
  2245. lpfc_printf_log(phba,
  2246. KERN_WARNING,
  2247. LOG_SLI,
  2248. "0316 Ring %d handler: unexpected "
  2249. "ASYNC_STATUS iocb received evt_code "
  2250. "0x%x\n",
  2251. pring->ringno,
  2252. irsp->un.asyncstat.evt_code);
  2253. return 1;
  2254. }
  2255. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2256. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2257. if (irsp->ulpBdeCount > 0) {
  2258. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2259. irsp->un.ulpWord[3]);
  2260. lpfc_in_buf_free(phba, dmzbuf);
  2261. }
  2262. if (irsp->ulpBdeCount > 1) {
  2263. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2264. irsp->unsli3.sli3Words[3]);
  2265. lpfc_in_buf_free(phba, dmzbuf);
  2266. }
  2267. if (irsp->ulpBdeCount > 2) {
  2268. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2269. irsp->unsli3.sli3Words[7]);
  2270. lpfc_in_buf_free(phba, dmzbuf);
  2271. }
  2272. return 1;
  2273. }
  2274. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2275. if (irsp->ulpBdeCount != 0) {
  2276. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2277. irsp->un.ulpWord[3]);
  2278. if (!saveq->context2)
  2279. lpfc_printf_log(phba,
  2280. KERN_ERR,
  2281. LOG_SLI,
  2282. "0341 Ring %d Cannot find buffer for "
  2283. "an unsolicited iocb. tag 0x%x\n",
  2284. pring->ringno,
  2285. irsp->un.ulpWord[3]);
  2286. }
  2287. if (irsp->ulpBdeCount == 2) {
  2288. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2289. irsp->unsli3.sli3Words[7]);
  2290. if (!saveq->context3)
  2291. lpfc_printf_log(phba,
  2292. KERN_ERR,
  2293. LOG_SLI,
  2294. "0342 Ring %d Cannot find buffer for an"
  2295. " unsolicited iocb. tag 0x%x\n",
  2296. pring->ringno,
  2297. irsp->unsli3.sli3Words[7]);
  2298. }
  2299. list_for_each_entry(iocbq, &saveq->list, list) {
  2300. irsp = &(iocbq->iocb);
  2301. if (irsp->ulpBdeCount != 0) {
  2302. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2303. irsp->un.ulpWord[3]);
  2304. if (!iocbq->context2)
  2305. lpfc_printf_log(phba,
  2306. KERN_ERR,
  2307. LOG_SLI,
  2308. "0343 Ring %d Cannot find "
  2309. "buffer for an unsolicited iocb"
  2310. ". tag 0x%x\n", pring->ringno,
  2311. irsp->un.ulpWord[3]);
  2312. }
  2313. if (irsp->ulpBdeCount == 2) {
  2314. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2315. irsp->unsli3.sli3Words[7]);
  2316. if (!iocbq->context3)
  2317. lpfc_printf_log(phba,
  2318. KERN_ERR,
  2319. LOG_SLI,
  2320. "0344 Ring %d Cannot find "
  2321. "buffer for an unsolicited "
  2322. "iocb. tag 0x%x\n",
  2323. pring->ringno,
  2324. irsp->unsli3.sli3Words[7]);
  2325. }
  2326. }
  2327. }
  2328. if (irsp->ulpBdeCount != 0 &&
  2329. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2330. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2331. int found = 0;
  2332. /* search continue save q for same XRI */
  2333. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2334. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2335. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2336. list_add_tail(&saveq->list, &iocbq->list);
  2337. found = 1;
  2338. break;
  2339. }
  2340. }
  2341. if (!found)
  2342. list_add_tail(&saveq->clist,
  2343. &pring->iocb_continue_saveq);
  2344. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2345. list_del_init(&iocbq->clist);
  2346. saveq = iocbq;
  2347. irsp = &(saveq->iocb);
  2348. } else
  2349. return 0;
  2350. }
  2351. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2352. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2353. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2354. Rctl = FC_RCTL_ELS_REQ;
  2355. Type = FC_TYPE_ELS;
  2356. } else {
  2357. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2358. Rctl = w5p->hcsw.Rctl;
  2359. Type = w5p->hcsw.Type;
  2360. /* Firmware Workaround */
  2361. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2362. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2363. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2364. Rctl = FC_RCTL_ELS_REQ;
  2365. Type = FC_TYPE_ELS;
  2366. w5p->hcsw.Rctl = Rctl;
  2367. w5p->hcsw.Type = Type;
  2368. }
  2369. }
  2370. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2371. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2372. "0313 Ring %d handler: unexpected Rctl x%x "
  2373. "Type x%x received\n",
  2374. pring->ringno, Rctl, Type);
  2375. return 1;
  2376. }
  2377. /**
  2378. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2379. * @phba: Pointer to HBA context object.
  2380. * @pring: Pointer to driver SLI ring object.
  2381. * @prspiocb: Pointer to response iocb object.
  2382. *
  2383. * This function looks up the iocb_lookup table to get the command iocb
  2384. * corresponding to the given response iocb using the iotag of the
  2385. * response iocb. This function is called with the hbalock held.
  2386. * This function returns the command iocb object if it finds the command
  2387. * iocb else returns NULL.
  2388. **/
  2389. static struct lpfc_iocbq *
  2390. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2391. struct lpfc_sli_ring *pring,
  2392. struct lpfc_iocbq *prspiocb)
  2393. {
  2394. struct lpfc_iocbq *cmd_iocb = NULL;
  2395. uint16_t iotag;
  2396. iotag = prspiocb->iocb.ulpIoTag;
  2397. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2398. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2399. list_del_init(&cmd_iocb->list);
  2400. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2401. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2402. }
  2403. return cmd_iocb;
  2404. }
  2405. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2406. "0317 iotag x%x is out off "
  2407. "range: max iotag x%x wd0 x%x\n",
  2408. iotag, phba->sli.last_iotag,
  2409. *(((uint32_t *) &prspiocb->iocb) + 7));
  2410. return NULL;
  2411. }
  2412. /**
  2413. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2414. * @phba: Pointer to HBA context object.
  2415. * @pring: Pointer to driver SLI ring object.
  2416. * @iotag: IOCB tag.
  2417. *
  2418. * This function looks up the iocb_lookup table to get the command iocb
  2419. * corresponding to the given iotag. This function is called with the
  2420. * hbalock held.
  2421. * This function returns the command iocb object if it finds the command
  2422. * iocb else returns NULL.
  2423. **/
  2424. static struct lpfc_iocbq *
  2425. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2426. struct lpfc_sli_ring *pring, uint16_t iotag)
  2427. {
  2428. struct lpfc_iocbq *cmd_iocb;
  2429. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2430. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2431. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2432. /* remove from txcmpl queue list */
  2433. list_del_init(&cmd_iocb->list);
  2434. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2435. return cmd_iocb;
  2436. }
  2437. }
  2438. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2439. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2440. iotag, phba->sli.last_iotag);
  2441. return NULL;
  2442. }
  2443. /**
  2444. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2445. * @phba: Pointer to HBA context object.
  2446. * @pring: Pointer to driver SLI ring object.
  2447. * @saveq: Pointer to the response iocb to be processed.
  2448. *
  2449. * This function is called by the ring event handler for non-fcp
  2450. * rings when there is a new response iocb in the response ring.
  2451. * The caller is not required to hold any locks. This function
  2452. * gets the command iocb associated with the response iocb and
  2453. * calls the completion handler for the command iocb. If there
  2454. * is no completion handler, the function will free the resources
  2455. * associated with command iocb. If the response iocb is for
  2456. * an already aborted command iocb, the status of the completion
  2457. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2458. * This function always returns 1.
  2459. **/
  2460. static int
  2461. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2462. struct lpfc_iocbq *saveq)
  2463. {
  2464. struct lpfc_iocbq *cmdiocbp;
  2465. int rc = 1;
  2466. unsigned long iflag;
  2467. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2468. spin_lock_irqsave(&phba->hbalock, iflag);
  2469. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2470. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2471. if (cmdiocbp) {
  2472. if (cmdiocbp->iocb_cmpl) {
  2473. /*
  2474. * If an ELS command failed send an event to mgmt
  2475. * application.
  2476. */
  2477. if (saveq->iocb.ulpStatus &&
  2478. (pring->ringno == LPFC_ELS_RING) &&
  2479. (cmdiocbp->iocb.ulpCommand ==
  2480. CMD_ELS_REQUEST64_CR))
  2481. lpfc_send_els_failure_event(phba,
  2482. cmdiocbp, saveq);
  2483. /*
  2484. * Post all ELS completions to the worker thread.
  2485. * All other are passed to the completion callback.
  2486. */
  2487. if (pring->ringno == LPFC_ELS_RING) {
  2488. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2489. (cmdiocbp->iocb_flag &
  2490. LPFC_DRIVER_ABORTED)) {
  2491. spin_lock_irqsave(&phba->hbalock,
  2492. iflag);
  2493. cmdiocbp->iocb_flag &=
  2494. ~LPFC_DRIVER_ABORTED;
  2495. spin_unlock_irqrestore(&phba->hbalock,
  2496. iflag);
  2497. saveq->iocb.ulpStatus =
  2498. IOSTAT_LOCAL_REJECT;
  2499. saveq->iocb.un.ulpWord[4] =
  2500. IOERR_SLI_ABORTED;
  2501. /* Firmware could still be in progress
  2502. * of DMAing payload, so don't free data
  2503. * buffer till after a hbeat.
  2504. */
  2505. spin_lock_irqsave(&phba->hbalock,
  2506. iflag);
  2507. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2508. spin_unlock_irqrestore(&phba->hbalock,
  2509. iflag);
  2510. }
  2511. if (phba->sli_rev == LPFC_SLI_REV4) {
  2512. if (saveq->iocb_flag &
  2513. LPFC_EXCHANGE_BUSY) {
  2514. /* Set cmdiocb flag for the
  2515. * exchange busy so sgl (xri)
  2516. * will not be released until
  2517. * the abort xri is received
  2518. * from hba.
  2519. */
  2520. spin_lock_irqsave(
  2521. &phba->hbalock, iflag);
  2522. cmdiocbp->iocb_flag |=
  2523. LPFC_EXCHANGE_BUSY;
  2524. spin_unlock_irqrestore(
  2525. &phba->hbalock, iflag);
  2526. }
  2527. if (cmdiocbp->iocb_flag &
  2528. LPFC_DRIVER_ABORTED) {
  2529. /*
  2530. * Clear LPFC_DRIVER_ABORTED
  2531. * bit in case it was driver
  2532. * initiated abort.
  2533. */
  2534. spin_lock_irqsave(
  2535. &phba->hbalock, iflag);
  2536. cmdiocbp->iocb_flag &=
  2537. ~LPFC_DRIVER_ABORTED;
  2538. spin_unlock_irqrestore(
  2539. &phba->hbalock, iflag);
  2540. cmdiocbp->iocb.ulpStatus =
  2541. IOSTAT_LOCAL_REJECT;
  2542. cmdiocbp->iocb.un.ulpWord[4] =
  2543. IOERR_ABORT_REQUESTED;
  2544. /*
  2545. * For SLI4, irsiocb contains
  2546. * NO_XRI in sli_xritag, it
  2547. * shall not affect releasing
  2548. * sgl (xri) process.
  2549. */
  2550. saveq->iocb.ulpStatus =
  2551. IOSTAT_LOCAL_REJECT;
  2552. saveq->iocb.un.ulpWord[4] =
  2553. IOERR_SLI_ABORTED;
  2554. spin_lock_irqsave(
  2555. &phba->hbalock, iflag);
  2556. saveq->iocb_flag |=
  2557. LPFC_DELAY_MEM_FREE;
  2558. spin_unlock_irqrestore(
  2559. &phba->hbalock, iflag);
  2560. }
  2561. }
  2562. }
  2563. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2564. } else
  2565. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2566. } else {
  2567. /*
  2568. * Unknown initiating command based on the response iotag.
  2569. * This could be the case on the ELS ring because of
  2570. * lpfc_els_abort().
  2571. */
  2572. if (pring->ringno != LPFC_ELS_RING) {
  2573. /*
  2574. * Ring <ringno> handler: unexpected completion IoTag
  2575. * <IoTag>
  2576. */
  2577. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2578. "0322 Ring %d handler: "
  2579. "unexpected completion IoTag x%x "
  2580. "Data: x%x x%x x%x x%x\n",
  2581. pring->ringno,
  2582. saveq->iocb.ulpIoTag,
  2583. saveq->iocb.ulpStatus,
  2584. saveq->iocb.un.ulpWord[4],
  2585. saveq->iocb.ulpCommand,
  2586. saveq->iocb.ulpContext);
  2587. }
  2588. }
  2589. return rc;
  2590. }
  2591. /**
  2592. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2593. * @phba: Pointer to HBA context object.
  2594. * @pring: Pointer to driver SLI ring object.
  2595. *
  2596. * This function is called from the iocb ring event handlers when
  2597. * put pointer is ahead of the get pointer for a ring. This function signal
  2598. * an error attention condition to the worker thread and the worker
  2599. * thread will transition the HBA to offline state.
  2600. **/
  2601. static void
  2602. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2603. {
  2604. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2605. /*
  2606. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2607. * rsp ring <portRspMax>
  2608. */
  2609. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2610. "0312 Ring %d handler: portRspPut %d "
  2611. "is bigger than rsp ring %d\n",
  2612. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2613. pring->sli.sli3.numRiocb);
  2614. phba->link_state = LPFC_HBA_ERROR;
  2615. /*
  2616. * All error attention handlers are posted to
  2617. * worker thread
  2618. */
  2619. phba->work_ha |= HA_ERATT;
  2620. phba->work_hs = HS_FFER3;
  2621. lpfc_worker_wake_up(phba);
  2622. return;
  2623. }
  2624. /**
  2625. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2626. * @ptr: Pointer to address of HBA context object.
  2627. *
  2628. * This function is invoked by the Error Attention polling timer when the
  2629. * timer times out. It will check the SLI Error Attention register for
  2630. * possible attention events. If so, it will post an Error Attention event
  2631. * and wake up worker thread to process it. Otherwise, it will set up the
  2632. * Error Attention polling timer for the next poll.
  2633. **/
  2634. void lpfc_poll_eratt(unsigned long ptr)
  2635. {
  2636. struct lpfc_hba *phba;
  2637. uint32_t eratt = 0, rem;
  2638. uint64_t sli_intr, cnt;
  2639. phba = (struct lpfc_hba *)ptr;
  2640. /* Here we will also keep track of interrupts per sec of the hba */
  2641. sli_intr = phba->sli.slistat.sli_intr;
  2642. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2643. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2644. sli_intr);
  2645. else
  2646. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2647. /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
  2648. rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
  2649. phba->sli.slistat.sli_ips = cnt;
  2650. phba->sli.slistat.sli_prev_intr = sli_intr;
  2651. /* Check chip HA register for error event */
  2652. eratt = lpfc_sli_check_eratt(phba);
  2653. if (eratt)
  2654. /* Tell the worker thread there is work to do */
  2655. lpfc_worker_wake_up(phba);
  2656. else
  2657. /* Restart the timer for next eratt poll */
  2658. mod_timer(&phba->eratt_poll,
  2659. jiffies +
  2660. msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
  2661. return;
  2662. }
  2663. /**
  2664. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2665. * @phba: Pointer to HBA context object.
  2666. * @pring: Pointer to driver SLI ring object.
  2667. * @mask: Host attention register mask for this ring.
  2668. *
  2669. * This function is called from the interrupt context when there is a ring
  2670. * event for the fcp ring. The caller does not hold any lock.
  2671. * The function processes each response iocb in the response ring until it
  2672. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2673. * LE bit set. The function will call the completion handler of the command iocb
  2674. * if the response iocb indicates a completion for a command iocb or it is
  2675. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2676. * function if this is an unsolicited iocb.
  2677. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2678. * to check it explicitly.
  2679. */
  2680. int
  2681. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2682. struct lpfc_sli_ring *pring, uint32_t mask)
  2683. {
  2684. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2685. IOCB_t *irsp = NULL;
  2686. IOCB_t *entry = NULL;
  2687. struct lpfc_iocbq *cmdiocbq = NULL;
  2688. struct lpfc_iocbq rspiocbq;
  2689. uint32_t status;
  2690. uint32_t portRspPut, portRspMax;
  2691. int rc = 1;
  2692. lpfc_iocb_type type;
  2693. unsigned long iflag;
  2694. uint32_t rsp_cmpl = 0;
  2695. spin_lock_irqsave(&phba->hbalock, iflag);
  2696. pring->stats.iocb_event++;
  2697. /*
  2698. * The next available response entry should never exceed the maximum
  2699. * entries. If it does, treat it as an adapter hardware error.
  2700. */
  2701. portRspMax = pring->sli.sli3.numRiocb;
  2702. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2703. if (unlikely(portRspPut >= portRspMax)) {
  2704. lpfc_sli_rsp_pointers_error(phba, pring);
  2705. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2706. return 1;
  2707. }
  2708. if (phba->fcp_ring_in_use) {
  2709. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2710. return 1;
  2711. } else
  2712. phba->fcp_ring_in_use = 1;
  2713. rmb();
  2714. while (pring->sli.sli3.rspidx != portRspPut) {
  2715. /*
  2716. * Fetch an entry off the ring and copy it into a local data
  2717. * structure. The copy involves a byte-swap since the
  2718. * network byte order and pci byte orders are different.
  2719. */
  2720. entry = lpfc_resp_iocb(phba, pring);
  2721. phba->last_completion_time = jiffies;
  2722. if (++pring->sli.sli3.rspidx >= portRspMax)
  2723. pring->sli.sli3.rspidx = 0;
  2724. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2725. (uint32_t *) &rspiocbq.iocb,
  2726. phba->iocb_rsp_size);
  2727. INIT_LIST_HEAD(&(rspiocbq.list));
  2728. irsp = &rspiocbq.iocb;
  2729. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2730. pring->stats.iocb_rsp++;
  2731. rsp_cmpl++;
  2732. if (unlikely(irsp->ulpStatus)) {
  2733. /*
  2734. * If resource errors reported from HBA, reduce
  2735. * queuedepths of the SCSI device.
  2736. */
  2737. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2738. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2739. IOERR_NO_RESOURCES)) {
  2740. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2741. phba->lpfc_rampdown_queue_depth(phba);
  2742. spin_lock_irqsave(&phba->hbalock, iflag);
  2743. }
  2744. /* Rsp ring <ringno> error: IOCB */
  2745. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2746. "0336 Rsp Ring %d error: IOCB Data: "
  2747. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2748. pring->ringno,
  2749. irsp->un.ulpWord[0],
  2750. irsp->un.ulpWord[1],
  2751. irsp->un.ulpWord[2],
  2752. irsp->un.ulpWord[3],
  2753. irsp->un.ulpWord[4],
  2754. irsp->un.ulpWord[5],
  2755. *(uint32_t *)&irsp->un1,
  2756. *((uint32_t *)&irsp->un1 + 1));
  2757. }
  2758. switch (type) {
  2759. case LPFC_ABORT_IOCB:
  2760. case LPFC_SOL_IOCB:
  2761. /*
  2762. * Idle exchange closed via ABTS from port. No iocb
  2763. * resources need to be recovered.
  2764. */
  2765. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2766. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2767. "0333 IOCB cmd 0x%x"
  2768. " processed. Skipping"
  2769. " completion\n",
  2770. irsp->ulpCommand);
  2771. break;
  2772. }
  2773. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2774. &rspiocbq);
  2775. if (unlikely(!cmdiocbq))
  2776. break;
  2777. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2778. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2779. if (cmdiocbq->iocb_cmpl) {
  2780. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2781. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2782. &rspiocbq);
  2783. spin_lock_irqsave(&phba->hbalock, iflag);
  2784. }
  2785. break;
  2786. case LPFC_UNSOL_IOCB:
  2787. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2788. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2789. spin_lock_irqsave(&phba->hbalock, iflag);
  2790. break;
  2791. default:
  2792. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2793. char adaptermsg[LPFC_MAX_ADPTMSG];
  2794. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2795. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2796. MAX_MSG_DATA);
  2797. dev_warn(&((phba->pcidev)->dev),
  2798. "lpfc%d: %s\n",
  2799. phba->brd_no, adaptermsg);
  2800. } else {
  2801. /* Unknown IOCB command */
  2802. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2803. "0334 Unknown IOCB command "
  2804. "Data: x%x, x%x x%x x%x x%x\n",
  2805. type, irsp->ulpCommand,
  2806. irsp->ulpStatus,
  2807. irsp->ulpIoTag,
  2808. irsp->ulpContext);
  2809. }
  2810. break;
  2811. }
  2812. /*
  2813. * The response IOCB has been processed. Update the ring
  2814. * pointer in SLIM. If the port response put pointer has not
  2815. * been updated, sync the pgp->rspPutInx and fetch the new port
  2816. * response put pointer.
  2817. */
  2818. writel(pring->sli.sli3.rspidx,
  2819. &phba->host_gp[pring->ringno].rspGetInx);
  2820. if (pring->sli.sli3.rspidx == portRspPut)
  2821. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2822. }
  2823. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2824. pring->stats.iocb_rsp_full++;
  2825. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2826. writel(status, phba->CAregaddr);
  2827. readl(phba->CAregaddr);
  2828. }
  2829. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2830. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2831. pring->stats.iocb_cmd_empty++;
  2832. /* Force update of the local copy of cmdGetInx */
  2833. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2834. lpfc_sli_resume_iocb(phba, pring);
  2835. if ((pring->lpfc_sli_cmd_available))
  2836. (pring->lpfc_sli_cmd_available) (phba, pring);
  2837. }
  2838. phba->fcp_ring_in_use = 0;
  2839. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2840. return rc;
  2841. }
  2842. /**
  2843. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2844. * @phba: Pointer to HBA context object.
  2845. * @pring: Pointer to driver SLI ring object.
  2846. * @rspiocbp: Pointer to driver response IOCB object.
  2847. *
  2848. * This function is called from the worker thread when there is a slow-path
  2849. * response IOCB to process. This function chains all the response iocbs until
  2850. * seeing the iocb with the LE bit set. The function will call
  2851. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2852. * completion of a command iocb. The function will call the
  2853. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2854. * The function frees the resources or calls the completion handler if this
  2855. * iocb is an abort completion. The function returns NULL when the response
  2856. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2857. * this function shall chain the iocb on to the iocb_continueq and return the
  2858. * response iocb passed in.
  2859. **/
  2860. static struct lpfc_iocbq *
  2861. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2862. struct lpfc_iocbq *rspiocbp)
  2863. {
  2864. struct lpfc_iocbq *saveq;
  2865. struct lpfc_iocbq *cmdiocbp;
  2866. struct lpfc_iocbq *next_iocb;
  2867. IOCB_t *irsp = NULL;
  2868. uint32_t free_saveq;
  2869. uint8_t iocb_cmd_type;
  2870. lpfc_iocb_type type;
  2871. unsigned long iflag;
  2872. int rc;
  2873. spin_lock_irqsave(&phba->hbalock, iflag);
  2874. /* First add the response iocb to the countinueq list */
  2875. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2876. pring->iocb_continueq_cnt++;
  2877. /* Now, determine whether the list is completed for processing */
  2878. irsp = &rspiocbp->iocb;
  2879. if (irsp->ulpLe) {
  2880. /*
  2881. * By default, the driver expects to free all resources
  2882. * associated with this iocb completion.
  2883. */
  2884. free_saveq = 1;
  2885. saveq = list_get_first(&pring->iocb_continueq,
  2886. struct lpfc_iocbq, list);
  2887. irsp = &(saveq->iocb);
  2888. list_del_init(&pring->iocb_continueq);
  2889. pring->iocb_continueq_cnt = 0;
  2890. pring->stats.iocb_rsp++;
  2891. /*
  2892. * If resource errors reported from HBA, reduce
  2893. * queuedepths of the SCSI device.
  2894. */
  2895. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2896. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2897. IOERR_NO_RESOURCES)) {
  2898. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2899. phba->lpfc_rampdown_queue_depth(phba);
  2900. spin_lock_irqsave(&phba->hbalock, iflag);
  2901. }
  2902. if (irsp->ulpStatus) {
  2903. /* Rsp ring <ringno> error: IOCB */
  2904. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2905. "0328 Rsp Ring %d error: "
  2906. "IOCB Data: "
  2907. "x%x x%x x%x x%x "
  2908. "x%x x%x x%x x%x "
  2909. "x%x x%x x%x x%x "
  2910. "x%x x%x x%x x%x\n",
  2911. pring->ringno,
  2912. irsp->un.ulpWord[0],
  2913. irsp->un.ulpWord[1],
  2914. irsp->un.ulpWord[2],
  2915. irsp->un.ulpWord[3],
  2916. irsp->un.ulpWord[4],
  2917. irsp->un.ulpWord[5],
  2918. *(((uint32_t *) irsp) + 6),
  2919. *(((uint32_t *) irsp) + 7),
  2920. *(((uint32_t *) irsp) + 8),
  2921. *(((uint32_t *) irsp) + 9),
  2922. *(((uint32_t *) irsp) + 10),
  2923. *(((uint32_t *) irsp) + 11),
  2924. *(((uint32_t *) irsp) + 12),
  2925. *(((uint32_t *) irsp) + 13),
  2926. *(((uint32_t *) irsp) + 14),
  2927. *(((uint32_t *) irsp) + 15));
  2928. }
  2929. /*
  2930. * Fetch the IOCB command type and call the correct completion
  2931. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2932. * get freed back to the lpfc_iocb_list by the discovery
  2933. * kernel thread.
  2934. */
  2935. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2936. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2937. switch (type) {
  2938. case LPFC_SOL_IOCB:
  2939. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2940. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2941. spin_lock_irqsave(&phba->hbalock, iflag);
  2942. break;
  2943. case LPFC_UNSOL_IOCB:
  2944. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2945. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2946. spin_lock_irqsave(&phba->hbalock, iflag);
  2947. if (!rc)
  2948. free_saveq = 0;
  2949. break;
  2950. case LPFC_ABORT_IOCB:
  2951. cmdiocbp = NULL;
  2952. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2953. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2954. saveq);
  2955. if (cmdiocbp) {
  2956. /* Call the specified completion routine */
  2957. if (cmdiocbp->iocb_cmpl) {
  2958. spin_unlock_irqrestore(&phba->hbalock,
  2959. iflag);
  2960. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2961. saveq);
  2962. spin_lock_irqsave(&phba->hbalock,
  2963. iflag);
  2964. } else
  2965. __lpfc_sli_release_iocbq(phba,
  2966. cmdiocbp);
  2967. }
  2968. break;
  2969. case LPFC_UNKNOWN_IOCB:
  2970. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2971. char adaptermsg[LPFC_MAX_ADPTMSG];
  2972. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2973. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2974. MAX_MSG_DATA);
  2975. dev_warn(&((phba->pcidev)->dev),
  2976. "lpfc%d: %s\n",
  2977. phba->brd_no, adaptermsg);
  2978. } else {
  2979. /* Unknown IOCB command */
  2980. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2981. "0335 Unknown IOCB "
  2982. "command Data: x%x "
  2983. "x%x x%x x%x\n",
  2984. irsp->ulpCommand,
  2985. irsp->ulpStatus,
  2986. irsp->ulpIoTag,
  2987. irsp->ulpContext);
  2988. }
  2989. break;
  2990. }
  2991. if (free_saveq) {
  2992. list_for_each_entry_safe(rspiocbp, next_iocb,
  2993. &saveq->list, list) {
  2994. list_del_init(&rspiocbp->list);
  2995. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2996. }
  2997. __lpfc_sli_release_iocbq(phba, saveq);
  2998. }
  2999. rspiocbp = NULL;
  3000. }
  3001. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3002. return rspiocbp;
  3003. }
  3004. /**
  3005. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  3006. * @phba: Pointer to HBA context object.
  3007. * @pring: Pointer to driver SLI ring object.
  3008. * @mask: Host attention register mask for this ring.
  3009. *
  3010. * This routine wraps the actual slow_ring event process routine from the
  3011. * API jump table function pointer from the lpfc_hba struct.
  3012. **/
  3013. void
  3014. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  3015. struct lpfc_sli_ring *pring, uint32_t mask)
  3016. {
  3017. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  3018. }
  3019. /**
  3020. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  3021. * @phba: Pointer to HBA context object.
  3022. * @pring: Pointer to driver SLI ring object.
  3023. * @mask: Host attention register mask for this ring.
  3024. *
  3025. * This function is called from the worker thread when there is a ring event
  3026. * for non-fcp rings. The caller does not hold any lock. The function will
  3027. * remove each response iocb in the response ring and calls the handle
  3028. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3029. **/
  3030. static void
  3031. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  3032. struct lpfc_sli_ring *pring, uint32_t mask)
  3033. {
  3034. struct lpfc_pgp *pgp;
  3035. IOCB_t *entry;
  3036. IOCB_t *irsp = NULL;
  3037. struct lpfc_iocbq *rspiocbp = NULL;
  3038. uint32_t portRspPut, portRspMax;
  3039. unsigned long iflag;
  3040. uint32_t status;
  3041. pgp = &phba->port_gp[pring->ringno];
  3042. spin_lock_irqsave(&phba->hbalock, iflag);
  3043. pring->stats.iocb_event++;
  3044. /*
  3045. * The next available response entry should never exceed the maximum
  3046. * entries. If it does, treat it as an adapter hardware error.
  3047. */
  3048. portRspMax = pring->sli.sli3.numRiocb;
  3049. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3050. if (portRspPut >= portRspMax) {
  3051. /*
  3052. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3053. * rsp ring <portRspMax>
  3054. */
  3055. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3056. "0303 Ring %d handler: portRspPut %d "
  3057. "is bigger than rsp ring %d\n",
  3058. pring->ringno, portRspPut, portRspMax);
  3059. phba->link_state = LPFC_HBA_ERROR;
  3060. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3061. phba->work_hs = HS_FFER3;
  3062. lpfc_handle_eratt(phba);
  3063. return;
  3064. }
  3065. rmb();
  3066. while (pring->sli.sli3.rspidx != portRspPut) {
  3067. /*
  3068. * Build a completion list and call the appropriate handler.
  3069. * The process is to get the next available response iocb, get
  3070. * a free iocb from the list, copy the response data into the
  3071. * free iocb, insert to the continuation list, and update the
  3072. * next response index to slim. This process makes response
  3073. * iocb's in the ring available to DMA as fast as possible but
  3074. * pays a penalty for a copy operation. Since the iocb is
  3075. * only 32 bytes, this penalty is considered small relative to
  3076. * the PCI reads for register values and a slim write. When
  3077. * the ulpLe field is set, the entire Command has been
  3078. * received.
  3079. */
  3080. entry = lpfc_resp_iocb(phba, pring);
  3081. phba->last_completion_time = jiffies;
  3082. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3083. if (rspiocbp == NULL) {
  3084. printk(KERN_ERR "%s: out of buffers! Failing "
  3085. "completion.\n", __func__);
  3086. break;
  3087. }
  3088. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3089. phba->iocb_rsp_size);
  3090. irsp = &rspiocbp->iocb;
  3091. if (++pring->sli.sli3.rspidx >= portRspMax)
  3092. pring->sli.sli3.rspidx = 0;
  3093. if (pring->ringno == LPFC_ELS_RING) {
  3094. lpfc_debugfs_slow_ring_trc(phba,
  3095. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3096. *(((uint32_t *) irsp) + 4),
  3097. *(((uint32_t *) irsp) + 6),
  3098. *(((uint32_t *) irsp) + 7));
  3099. }
  3100. writel(pring->sli.sli3.rspidx,
  3101. &phba->host_gp[pring->ringno].rspGetInx);
  3102. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3103. /* Handle the response IOCB */
  3104. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3105. spin_lock_irqsave(&phba->hbalock, iflag);
  3106. /*
  3107. * If the port response put pointer has not been updated, sync
  3108. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3109. * response put pointer.
  3110. */
  3111. if (pring->sli.sli3.rspidx == portRspPut) {
  3112. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3113. }
  3114. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3115. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3116. /* At least one response entry has been freed */
  3117. pring->stats.iocb_rsp_full++;
  3118. /* SET RxRE_RSP in Chip Att register */
  3119. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3120. writel(status, phba->CAregaddr);
  3121. readl(phba->CAregaddr); /* flush */
  3122. }
  3123. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3124. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3125. pring->stats.iocb_cmd_empty++;
  3126. /* Force update of the local copy of cmdGetInx */
  3127. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3128. lpfc_sli_resume_iocb(phba, pring);
  3129. if ((pring->lpfc_sli_cmd_available))
  3130. (pring->lpfc_sli_cmd_available) (phba, pring);
  3131. }
  3132. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3133. return;
  3134. }
  3135. /**
  3136. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3137. * @phba: Pointer to HBA context object.
  3138. * @pring: Pointer to driver SLI ring object.
  3139. * @mask: Host attention register mask for this ring.
  3140. *
  3141. * This function is called from the worker thread when there is a pending
  3142. * ELS response iocb on the driver internal slow-path response iocb worker
  3143. * queue. The caller does not hold any lock. The function will remove each
  3144. * response iocb from the response worker queue and calls the handle
  3145. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3146. **/
  3147. static void
  3148. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3149. struct lpfc_sli_ring *pring, uint32_t mask)
  3150. {
  3151. struct lpfc_iocbq *irspiocbq;
  3152. struct hbq_dmabuf *dmabuf;
  3153. struct lpfc_cq_event *cq_event;
  3154. unsigned long iflag;
  3155. spin_lock_irqsave(&phba->hbalock, iflag);
  3156. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3157. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3158. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3159. /* Get the response iocb from the head of work queue */
  3160. spin_lock_irqsave(&phba->hbalock, iflag);
  3161. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3162. cq_event, struct lpfc_cq_event, list);
  3163. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3164. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3165. case CQE_CODE_COMPL_WQE:
  3166. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3167. cq_event);
  3168. /* Translate ELS WCQE to response IOCBQ */
  3169. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3170. irspiocbq);
  3171. if (irspiocbq)
  3172. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3173. irspiocbq);
  3174. break;
  3175. case CQE_CODE_RECEIVE:
  3176. case CQE_CODE_RECEIVE_V1:
  3177. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3178. cq_event);
  3179. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3180. break;
  3181. default:
  3182. break;
  3183. }
  3184. }
  3185. }
  3186. /**
  3187. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3188. * @phba: Pointer to HBA context object.
  3189. * @pring: Pointer to driver SLI ring object.
  3190. *
  3191. * This function aborts all iocbs in the given ring and frees all the iocb
  3192. * objects in txq. This function issues an abort iocb for all the iocb commands
  3193. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3194. * the return of this function. The caller is not required to hold any locks.
  3195. **/
  3196. void
  3197. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3198. {
  3199. LIST_HEAD(completions);
  3200. struct lpfc_iocbq *iocb, *next_iocb;
  3201. if (pring->ringno == LPFC_ELS_RING) {
  3202. lpfc_fabric_abort_hba(phba);
  3203. }
  3204. /* Error everything on txq and txcmplq
  3205. * First do the txq.
  3206. */
  3207. spin_lock_irq(&phba->hbalock);
  3208. list_splice_init(&pring->txq, &completions);
  3209. /* Next issue ABTS for everything on the txcmplq */
  3210. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3211. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3212. spin_unlock_irq(&phba->hbalock);
  3213. /* Cancel all the IOCBs from the completions list */
  3214. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3215. IOERR_SLI_ABORTED);
  3216. }
  3217. /**
  3218. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3219. * @phba: Pointer to HBA context object.
  3220. *
  3221. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3222. * objects in txq and txcmplq. This function will not issue abort iocbs
  3223. * for all the iocb commands in txcmplq, they will just be returned with
  3224. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3225. * slot has been permanently disabled.
  3226. **/
  3227. void
  3228. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3229. {
  3230. LIST_HEAD(txq);
  3231. LIST_HEAD(txcmplq);
  3232. struct lpfc_sli *psli = &phba->sli;
  3233. struct lpfc_sli_ring *pring;
  3234. /* Currently, only one fcp ring */
  3235. pring = &psli->ring[psli->fcp_ring];
  3236. spin_lock_irq(&phba->hbalock);
  3237. /* Retrieve everything on txq */
  3238. list_splice_init(&pring->txq, &txq);
  3239. /* Retrieve everything on the txcmplq */
  3240. list_splice_init(&pring->txcmplq, &txcmplq);
  3241. /* Indicate the I/O queues are flushed */
  3242. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3243. spin_unlock_irq(&phba->hbalock);
  3244. /* Flush the txq */
  3245. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3246. IOERR_SLI_DOWN);
  3247. /* Flush the txcmpq */
  3248. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3249. IOERR_SLI_DOWN);
  3250. }
  3251. /**
  3252. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3253. * @phba: Pointer to HBA context object.
  3254. * @mask: Bit mask to be checked.
  3255. *
  3256. * This function reads the host status register and compares
  3257. * with the provided bit mask to check if HBA completed
  3258. * the restart. This function will wait in a loop for the
  3259. * HBA to complete restart. If the HBA does not restart within
  3260. * 15 iterations, the function will reset the HBA again. The
  3261. * function returns 1 when HBA fail to restart otherwise returns
  3262. * zero.
  3263. **/
  3264. static int
  3265. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3266. {
  3267. uint32_t status;
  3268. int i = 0;
  3269. int retval = 0;
  3270. /* Read the HBA Host Status Register */
  3271. if (lpfc_readl(phba->HSregaddr, &status))
  3272. return 1;
  3273. /*
  3274. * Check status register every 100ms for 5 retries, then every
  3275. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3276. * every 2.5 sec for 4.
  3277. * Break our of the loop if errors occurred during init.
  3278. */
  3279. while (((status & mask) != mask) &&
  3280. !(status & HS_FFERM) &&
  3281. i++ < 20) {
  3282. if (i <= 5)
  3283. msleep(10);
  3284. else if (i <= 10)
  3285. msleep(500);
  3286. else
  3287. msleep(2500);
  3288. if (i == 15) {
  3289. /* Do post */
  3290. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3291. lpfc_sli_brdrestart(phba);
  3292. }
  3293. /* Read the HBA Host Status Register */
  3294. if (lpfc_readl(phba->HSregaddr, &status)) {
  3295. retval = 1;
  3296. break;
  3297. }
  3298. }
  3299. /* Check to see if any errors occurred during init */
  3300. if ((status & HS_FFERM) || (i >= 20)) {
  3301. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3302. "2751 Adapter failed to restart, "
  3303. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3304. status,
  3305. readl(phba->MBslimaddr + 0xa8),
  3306. readl(phba->MBslimaddr + 0xac));
  3307. phba->link_state = LPFC_HBA_ERROR;
  3308. retval = 1;
  3309. }
  3310. return retval;
  3311. }
  3312. /**
  3313. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3314. * @phba: Pointer to HBA context object.
  3315. * @mask: Bit mask to be checked.
  3316. *
  3317. * This function checks the host status register to check if HBA is
  3318. * ready. This function will wait in a loop for the HBA to be ready
  3319. * If the HBA is not ready , the function will will reset the HBA PCI
  3320. * function again. The function returns 1 when HBA fail to be ready
  3321. * otherwise returns zero.
  3322. **/
  3323. static int
  3324. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3325. {
  3326. uint32_t status;
  3327. int retval = 0;
  3328. /* Read the HBA Host Status Register */
  3329. status = lpfc_sli4_post_status_check(phba);
  3330. if (status) {
  3331. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3332. lpfc_sli_brdrestart(phba);
  3333. status = lpfc_sli4_post_status_check(phba);
  3334. }
  3335. /* Check to see if any errors occurred during init */
  3336. if (status) {
  3337. phba->link_state = LPFC_HBA_ERROR;
  3338. retval = 1;
  3339. } else
  3340. phba->sli4_hba.intr_enable = 0;
  3341. return retval;
  3342. }
  3343. /**
  3344. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3345. * @phba: Pointer to HBA context object.
  3346. * @mask: Bit mask to be checked.
  3347. *
  3348. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3349. * from the API jump table function pointer from the lpfc_hba struct.
  3350. **/
  3351. int
  3352. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3353. {
  3354. return phba->lpfc_sli_brdready(phba, mask);
  3355. }
  3356. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3357. /**
  3358. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3359. * @phba: Pointer to HBA context object.
  3360. *
  3361. * This function is called before resetting an HBA. This function is called
  3362. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3363. **/
  3364. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3365. {
  3366. uint32_t __iomem *resp_buf;
  3367. uint32_t __iomem *mbox_buf;
  3368. volatile uint32_t mbox;
  3369. uint32_t hc_copy, ha_copy, resp_data;
  3370. int i;
  3371. uint8_t hdrtype;
  3372. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3373. if (hdrtype != 0x80 ||
  3374. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3375. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3376. return;
  3377. /*
  3378. * Tell the other part of the chip to suspend temporarily all
  3379. * its DMA activity.
  3380. */
  3381. resp_buf = phba->MBslimaddr;
  3382. /* Disable the error attention */
  3383. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3384. return;
  3385. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3386. readl(phba->HCregaddr); /* flush */
  3387. phba->link_flag |= LS_IGNORE_ERATT;
  3388. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3389. return;
  3390. if (ha_copy & HA_ERATT) {
  3391. /* Clear Chip error bit */
  3392. writel(HA_ERATT, phba->HAregaddr);
  3393. phba->pport->stopped = 1;
  3394. }
  3395. mbox = 0;
  3396. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3397. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3398. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3399. mbox_buf = phba->MBslimaddr;
  3400. writel(mbox, mbox_buf);
  3401. for (i = 0; i < 50; i++) {
  3402. if (lpfc_readl((resp_buf + 1), &resp_data))
  3403. return;
  3404. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3405. mdelay(1);
  3406. else
  3407. break;
  3408. }
  3409. resp_data = 0;
  3410. if (lpfc_readl((resp_buf + 1), &resp_data))
  3411. return;
  3412. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3413. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3414. phba->pport->stopped)
  3415. goto restore_hc;
  3416. else
  3417. goto clear_errat;
  3418. }
  3419. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3420. resp_data = 0;
  3421. for (i = 0; i < 500; i++) {
  3422. if (lpfc_readl(resp_buf, &resp_data))
  3423. return;
  3424. if (resp_data != mbox)
  3425. mdelay(1);
  3426. else
  3427. break;
  3428. }
  3429. clear_errat:
  3430. while (++i < 500) {
  3431. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3432. return;
  3433. if (!(ha_copy & HA_ERATT))
  3434. mdelay(1);
  3435. else
  3436. break;
  3437. }
  3438. if (readl(phba->HAregaddr) & HA_ERATT) {
  3439. writel(HA_ERATT, phba->HAregaddr);
  3440. phba->pport->stopped = 1;
  3441. }
  3442. restore_hc:
  3443. phba->link_flag &= ~LS_IGNORE_ERATT;
  3444. writel(hc_copy, phba->HCregaddr);
  3445. readl(phba->HCregaddr); /* flush */
  3446. }
  3447. /**
  3448. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3449. * @phba: Pointer to HBA context object.
  3450. *
  3451. * This function issues a kill_board mailbox command and waits for
  3452. * the error attention interrupt. This function is called for stopping
  3453. * the firmware processing. The caller is not required to hold any
  3454. * locks. This function calls lpfc_hba_down_post function to free
  3455. * any pending commands after the kill. The function will return 1 when it
  3456. * fails to kill the board else will return 0.
  3457. **/
  3458. int
  3459. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3460. {
  3461. struct lpfc_sli *psli;
  3462. LPFC_MBOXQ_t *pmb;
  3463. uint32_t status;
  3464. uint32_t ha_copy;
  3465. int retval;
  3466. int i = 0;
  3467. psli = &phba->sli;
  3468. /* Kill HBA */
  3469. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3470. "0329 Kill HBA Data: x%x x%x\n",
  3471. phba->pport->port_state, psli->sli_flag);
  3472. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3473. if (!pmb)
  3474. return 1;
  3475. /* Disable the error attention */
  3476. spin_lock_irq(&phba->hbalock);
  3477. if (lpfc_readl(phba->HCregaddr, &status)) {
  3478. spin_unlock_irq(&phba->hbalock);
  3479. mempool_free(pmb, phba->mbox_mem_pool);
  3480. return 1;
  3481. }
  3482. status &= ~HC_ERINT_ENA;
  3483. writel(status, phba->HCregaddr);
  3484. readl(phba->HCregaddr); /* flush */
  3485. phba->link_flag |= LS_IGNORE_ERATT;
  3486. spin_unlock_irq(&phba->hbalock);
  3487. lpfc_kill_board(phba, pmb);
  3488. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3489. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3490. if (retval != MBX_SUCCESS) {
  3491. if (retval != MBX_BUSY)
  3492. mempool_free(pmb, phba->mbox_mem_pool);
  3493. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3494. "2752 KILL_BOARD command failed retval %d\n",
  3495. retval);
  3496. spin_lock_irq(&phba->hbalock);
  3497. phba->link_flag &= ~LS_IGNORE_ERATT;
  3498. spin_unlock_irq(&phba->hbalock);
  3499. return 1;
  3500. }
  3501. spin_lock_irq(&phba->hbalock);
  3502. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3503. spin_unlock_irq(&phba->hbalock);
  3504. mempool_free(pmb, phba->mbox_mem_pool);
  3505. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3506. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3507. * 3 seconds we still set HBA_ERROR state because the status of the
  3508. * board is now undefined.
  3509. */
  3510. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3511. return 1;
  3512. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3513. mdelay(100);
  3514. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3515. return 1;
  3516. }
  3517. del_timer_sync(&psli->mbox_tmo);
  3518. if (ha_copy & HA_ERATT) {
  3519. writel(HA_ERATT, phba->HAregaddr);
  3520. phba->pport->stopped = 1;
  3521. }
  3522. spin_lock_irq(&phba->hbalock);
  3523. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3524. psli->mbox_active = NULL;
  3525. phba->link_flag &= ~LS_IGNORE_ERATT;
  3526. spin_unlock_irq(&phba->hbalock);
  3527. lpfc_hba_down_post(phba);
  3528. phba->link_state = LPFC_HBA_ERROR;
  3529. return ha_copy & HA_ERATT ? 0 : 1;
  3530. }
  3531. /**
  3532. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3533. * @phba: Pointer to HBA context object.
  3534. *
  3535. * This function resets the HBA by writing HC_INITFF to the control
  3536. * register. After the HBA resets, this function resets all the iocb ring
  3537. * indices. This function disables PCI layer parity checking during
  3538. * the reset.
  3539. * This function returns 0 always.
  3540. * The caller is not required to hold any locks.
  3541. **/
  3542. int
  3543. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3544. {
  3545. struct lpfc_sli *psli;
  3546. struct lpfc_sli_ring *pring;
  3547. uint16_t cfg_value;
  3548. int i;
  3549. psli = &phba->sli;
  3550. /* Reset HBA */
  3551. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3552. "0325 Reset HBA Data: x%x x%x\n",
  3553. phba->pport->port_state, psli->sli_flag);
  3554. /* perform board reset */
  3555. phba->fc_eventTag = 0;
  3556. phba->link_events = 0;
  3557. phba->pport->fc_myDID = 0;
  3558. phba->pport->fc_prevDID = 0;
  3559. /* Turn off parity checking and serr during the physical reset */
  3560. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3561. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3562. (cfg_value &
  3563. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3564. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3565. /* Now toggle INITFF bit in the Host Control Register */
  3566. writel(HC_INITFF, phba->HCregaddr);
  3567. mdelay(1);
  3568. readl(phba->HCregaddr); /* flush */
  3569. writel(0, phba->HCregaddr);
  3570. readl(phba->HCregaddr); /* flush */
  3571. /* Restore PCI cmd register */
  3572. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3573. /* Initialize relevant SLI info */
  3574. for (i = 0; i < psli->num_rings; i++) {
  3575. pring = &psli->ring[i];
  3576. pring->flag = 0;
  3577. pring->sli.sli3.rspidx = 0;
  3578. pring->sli.sli3.next_cmdidx = 0;
  3579. pring->sli.sli3.local_getidx = 0;
  3580. pring->sli.sli3.cmdidx = 0;
  3581. pring->missbufcnt = 0;
  3582. }
  3583. phba->link_state = LPFC_WARM_START;
  3584. return 0;
  3585. }
  3586. /**
  3587. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3588. * @phba: Pointer to HBA context object.
  3589. *
  3590. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3591. * checking during resets the device. The caller is not required to hold
  3592. * any locks.
  3593. *
  3594. * This function returns 0 always.
  3595. **/
  3596. int
  3597. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3598. {
  3599. struct lpfc_sli *psli = &phba->sli;
  3600. uint16_t cfg_value;
  3601. int rc;
  3602. /* Reset HBA */
  3603. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3604. "0295 Reset HBA Data: x%x x%x\n",
  3605. phba->pport->port_state, psli->sli_flag);
  3606. /* perform board reset */
  3607. phba->fc_eventTag = 0;
  3608. phba->link_events = 0;
  3609. phba->pport->fc_myDID = 0;
  3610. phba->pport->fc_prevDID = 0;
  3611. spin_lock_irq(&phba->hbalock);
  3612. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3613. phba->fcf.fcf_flag = 0;
  3614. spin_unlock_irq(&phba->hbalock);
  3615. /* Now physically reset the device */
  3616. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3617. "0389 Performing PCI function reset!\n");
  3618. /* Turn off parity checking and serr during the physical reset */
  3619. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3620. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3621. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3622. /* Perform FCoE PCI function reset before freeing queue memory */
  3623. rc = lpfc_pci_function_reset(phba);
  3624. lpfc_sli4_queue_destroy(phba);
  3625. /* Restore PCI cmd register */
  3626. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3627. return rc;
  3628. }
  3629. /**
  3630. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3631. * @phba: Pointer to HBA context object.
  3632. *
  3633. * This function is called in the SLI initialization code path to
  3634. * restart the HBA. The caller is not required to hold any lock.
  3635. * This function writes MBX_RESTART mailbox command to the SLIM and
  3636. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3637. * function to free any pending commands. The function enables
  3638. * POST only during the first initialization. The function returns zero.
  3639. * The function does not guarantee completion of MBX_RESTART mailbox
  3640. * command before the return of this function.
  3641. **/
  3642. static int
  3643. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3644. {
  3645. MAILBOX_t *mb;
  3646. struct lpfc_sli *psli;
  3647. volatile uint32_t word0;
  3648. void __iomem *to_slim;
  3649. uint32_t hba_aer_enabled;
  3650. spin_lock_irq(&phba->hbalock);
  3651. /* Take PCIe device Advanced Error Reporting (AER) state */
  3652. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3653. psli = &phba->sli;
  3654. /* Restart HBA */
  3655. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3656. "0337 Restart HBA Data: x%x x%x\n",
  3657. phba->pport->port_state, psli->sli_flag);
  3658. word0 = 0;
  3659. mb = (MAILBOX_t *) &word0;
  3660. mb->mbxCommand = MBX_RESTART;
  3661. mb->mbxHc = 1;
  3662. lpfc_reset_barrier(phba);
  3663. to_slim = phba->MBslimaddr;
  3664. writel(*(uint32_t *) mb, to_slim);
  3665. readl(to_slim); /* flush */
  3666. /* Only skip post after fc_ffinit is completed */
  3667. if (phba->pport->port_state)
  3668. word0 = 1; /* This is really setting up word1 */
  3669. else
  3670. word0 = 0; /* This is really setting up word1 */
  3671. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3672. writel(*(uint32_t *) mb, to_slim);
  3673. readl(to_slim); /* flush */
  3674. lpfc_sli_brdreset(phba);
  3675. phba->pport->stopped = 0;
  3676. phba->link_state = LPFC_INIT_START;
  3677. phba->hba_flag = 0;
  3678. spin_unlock_irq(&phba->hbalock);
  3679. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3680. psli->stats_start = get_seconds();
  3681. /* Give the INITFF and Post time to settle. */
  3682. mdelay(100);
  3683. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3684. if (hba_aer_enabled)
  3685. pci_disable_pcie_error_reporting(phba->pcidev);
  3686. lpfc_hba_down_post(phba);
  3687. return 0;
  3688. }
  3689. /**
  3690. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3691. * @phba: Pointer to HBA context object.
  3692. *
  3693. * This function is called in the SLI initialization code path to restart
  3694. * a SLI4 HBA. The caller is not required to hold any lock.
  3695. * At the end of the function, it calls lpfc_hba_down_post function to
  3696. * free any pending commands.
  3697. **/
  3698. static int
  3699. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3700. {
  3701. struct lpfc_sli *psli = &phba->sli;
  3702. uint32_t hba_aer_enabled;
  3703. int rc;
  3704. /* Restart HBA */
  3705. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3706. "0296 Restart HBA Data: x%x x%x\n",
  3707. phba->pport->port_state, psli->sli_flag);
  3708. /* Take PCIe device Advanced Error Reporting (AER) state */
  3709. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3710. rc = lpfc_sli4_brdreset(phba);
  3711. spin_lock_irq(&phba->hbalock);
  3712. phba->pport->stopped = 0;
  3713. phba->link_state = LPFC_INIT_START;
  3714. phba->hba_flag = 0;
  3715. spin_unlock_irq(&phba->hbalock);
  3716. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3717. psli->stats_start = get_seconds();
  3718. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3719. if (hba_aer_enabled)
  3720. pci_disable_pcie_error_reporting(phba->pcidev);
  3721. lpfc_hba_down_post(phba);
  3722. return rc;
  3723. }
  3724. /**
  3725. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3726. * @phba: Pointer to HBA context object.
  3727. *
  3728. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3729. * API jump table function pointer from the lpfc_hba struct.
  3730. **/
  3731. int
  3732. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3733. {
  3734. return phba->lpfc_sli_brdrestart(phba);
  3735. }
  3736. /**
  3737. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3738. * @phba: Pointer to HBA context object.
  3739. *
  3740. * This function is called after a HBA restart to wait for successful
  3741. * restart of the HBA. Successful restart of the HBA is indicated by
  3742. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3743. * iteration, the function will restart the HBA again. The function returns
  3744. * zero if HBA successfully restarted else returns negative error code.
  3745. **/
  3746. static int
  3747. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3748. {
  3749. uint32_t status, i = 0;
  3750. /* Read the HBA Host Status Register */
  3751. if (lpfc_readl(phba->HSregaddr, &status))
  3752. return -EIO;
  3753. /* Check status register to see what current state is */
  3754. i = 0;
  3755. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3756. /* Check every 10ms for 10 retries, then every 100ms for 90
  3757. * retries, then every 1 sec for 50 retires for a total of
  3758. * ~60 seconds before reset the board again and check every
  3759. * 1 sec for 50 retries. The up to 60 seconds before the
  3760. * board ready is required by the Falcon FIPS zeroization
  3761. * complete, and any reset the board in between shall cause
  3762. * restart of zeroization, further delay the board ready.
  3763. */
  3764. if (i++ >= 200) {
  3765. /* Adapter failed to init, timeout, status reg
  3766. <status> */
  3767. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3768. "0436 Adapter failed to init, "
  3769. "timeout, status reg x%x, "
  3770. "FW Data: A8 x%x AC x%x\n", status,
  3771. readl(phba->MBslimaddr + 0xa8),
  3772. readl(phba->MBslimaddr + 0xac));
  3773. phba->link_state = LPFC_HBA_ERROR;
  3774. return -ETIMEDOUT;
  3775. }
  3776. /* Check to see if any errors occurred during init */
  3777. if (status & HS_FFERM) {
  3778. /* ERROR: During chipset initialization */
  3779. /* Adapter failed to init, chipset, status reg
  3780. <status> */
  3781. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3782. "0437 Adapter failed to init, "
  3783. "chipset, status reg x%x, "
  3784. "FW Data: A8 x%x AC x%x\n", status,
  3785. readl(phba->MBslimaddr + 0xa8),
  3786. readl(phba->MBslimaddr + 0xac));
  3787. phba->link_state = LPFC_HBA_ERROR;
  3788. return -EIO;
  3789. }
  3790. if (i <= 10)
  3791. msleep(10);
  3792. else if (i <= 100)
  3793. msleep(100);
  3794. else
  3795. msleep(1000);
  3796. if (i == 150) {
  3797. /* Do post */
  3798. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3799. lpfc_sli_brdrestart(phba);
  3800. }
  3801. /* Read the HBA Host Status Register */
  3802. if (lpfc_readl(phba->HSregaddr, &status))
  3803. return -EIO;
  3804. }
  3805. /* Check to see if any errors occurred during init */
  3806. if (status & HS_FFERM) {
  3807. /* ERROR: During chipset initialization */
  3808. /* Adapter failed to init, chipset, status reg <status> */
  3809. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3810. "0438 Adapter failed to init, chipset, "
  3811. "status reg x%x, "
  3812. "FW Data: A8 x%x AC x%x\n", status,
  3813. readl(phba->MBslimaddr + 0xa8),
  3814. readl(phba->MBslimaddr + 0xac));
  3815. phba->link_state = LPFC_HBA_ERROR;
  3816. return -EIO;
  3817. }
  3818. /* Clear all interrupt enable conditions */
  3819. writel(0, phba->HCregaddr);
  3820. readl(phba->HCregaddr); /* flush */
  3821. /* setup host attn register */
  3822. writel(0xffffffff, phba->HAregaddr);
  3823. readl(phba->HAregaddr); /* flush */
  3824. return 0;
  3825. }
  3826. /**
  3827. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3828. *
  3829. * This function calculates and returns the number of HBQs required to be
  3830. * configured.
  3831. **/
  3832. int
  3833. lpfc_sli_hbq_count(void)
  3834. {
  3835. return ARRAY_SIZE(lpfc_hbq_defs);
  3836. }
  3837. /**
  3838. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3839. *
  3840. * This function adds the number of hbq entries in every HBQ to get
  3841. * the total number of hbq entries required for the HBA and returns
  3842. * the total count.
  3843. **/
  3844. static int
  3845. lpfc_sli_hbq_entry_count(void)
  3846. {
  3847. int hbq_count = lpfc_sli_hbq_count();
  3848. int count = 0;
  3849. int i;
  3850. for (i = 0; i < hbq_count; ++i)
  3851. count += lpfc_hbq_defs[i]->entry_count;
  3852. return count;
  3853. }
  3854. /**
  3855. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3856. *
  3857. * This function calculates amount of memory required for all hbq entries
  3858. * to be configured and returns the total memory required.
  3859. **/
  3860. int
  3861. lpfc_sli_hbq_size(void)
  3862. {
  3863. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3864. }
  3865. /**
  3866. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3867. * @phba: Pointer to HBA context object.
  3868. *
  3869. * This function is called during the SLI initialization to configure
  3870. * all the HBQs and post buffers to the HBQ. The caller is not
  3871. * required to hold any locks. This function will return zero if successful
  3872. * else it will return negative error code.
  3873. **/
  3874. static int
  3875. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3876. {
  3877. int hbq_count = lpfc_sli_hbq_count();
  3878. LPFC_MBOXQ_t *pmb;
  3879. MAILBOX_t *pmbox;
  3880. uint32_t hbqno;
  3881. uint32_t hbq_entry_index;
  3882. /* Get a Mailbox buffer to setup mailbox
  3883. * commands for HBA initialization
  3884. */
  3885. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3886. if (!pmb)
  3887. return -ENOMEM;
  3888. pmbox = &pmb->u.mb;
  3889. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3890. phba->link_state = LPFC_INIT_MBX_CMDS;
  3891. phba->hbq_in_use = 1;
  3892. hbq_entry_index = 0;
  3893. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3894. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3895. phba->hbqs[hbqno].hbqPutIdx = 0;
  3896. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3897. phba->hbqs[hbqno].entry_count =
  3898. lpfc_hbq_defs[hbqno]->entry_count;
  3899. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3900. hbq_entry_index, pmb);
  3901. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3902. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3903. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3904. mbxStatus <status>, ring <num> */
  3905. lpfc_printf_log(phba, KERN_ERR,
  3906. LOG_SLI | LOG_VPORT,
  3907. "1805 Adapter failed to init. "
  3908. "Data: x%x x%x x%x\n",
  3909. pmbox->mbxCommand,
  3910. pmbox->mbxStatus, hbqno);
  3911. phba->link_state = LPFC_HBA_ERROR;
  3912. mempool_free(pmb, phba->mbox_mem_pool);
  3913. return -ENXIO;
  3914. }
  3915. }
  3916. phba->hbq_count = hbq_count;
  3917. mempool_free(pmb, phba->mbox_mem_pool);
  3918. /* Initially populate or replenish the HBQs */
  3919. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3920. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3921. return 0;
  3922. }
  3923. /**
  3924. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3925. * @phba: Pointer to HBA context object.
  3926. *
  3927. * This function is called during the SLI initialization to configure
  3928. * all the HBQs and post buffers to the HBQ. The caller is not
  3929. * required to hold any locks. This function will return zero if successful
  3930. * else it will return negative error code.
  3931. **/
  3932. static int
  3933. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3934. {
  3935. phba->hbq_in_use = 1;
  3936. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3937. phba->hbq_count = 1;
  3938. /* Initially populate or replenish the HBQs */
  3939. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3940. return 0;
  3941. }
  3942. /**
  3943. * lpfc_sli_config_port - Issue config port mailbox command
  3944. * @phba: Pointer to HBA context object.
  3945. * @sli_mode: sli mode - 2/3
  3946. *
  3947. * This function is called by the sli intialization code path
  3948. * to issue config_port mailbox command. This function restarts the
  3949. * HBA firmware and issues a config_port mailbox command to configure
  3950. * the SLI interface in the sli mode specified by sli_mode
  3951. * variable. The caller is not required to hold any locks.
  3952. * The function returns 0 if successful, else returns negative error
  3953. * code.
  3954. **/
  3955. int
  3956. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3957. {
  3958. LPFC_MBOXQ_t *pmb;
  3959. uint32_t resetcount = 0, rc = 0, done = 0;
  3960. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3961. if (!pmb) {
  3962. phba->link_state = LPFC_HBA_ERROR;
  3963. return -ENOMEM;
  3964. }
  3965. phba->sli_rev = sli_mode;
  3966. while (resetcount < 2 && !done) {
  3967. spin_lock_irq(&phba->hbalock);
  3968. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3969. spin_unlock_irq(&phba->hbalock);
  3970. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3971. lpfc_sli_brdrestart(phba);
  3972. rc = lpfc_sli_chipset_init(phba);
  3973. if (rc)
  3974. break;
  3975. spin_lock_irq(&phba->hbalock);
  3976. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3977. spin_unlock_irq(&phba->hbalock);
  3978. resetcount++;
  3979. /* Call pre CONFIG_PORT mailbox command initialization. A
  3980. * value of 0 means the call was successful. Any other
  3981. * nonzero value is a failure, but if ERESTART is returned,
  3982. * the driver may reset the HBA and try again.
  3983. */
  3984. rc = lpfc_config_port_prep(phba);
  3985. if (rc == -ERESTART) {
  3986. phba->link_state = LPFC_LINK_UNKNOWN;
  3987. continue;
  3988. } else if (rc)
  3989. break;
  3990. phba->link_state = LPFC_INIT_MBX_CMDS;
  3991. lpfc_config_port(phba, pmb);
  3992. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3993. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3994. LPFC_SLI3_HBQ_ENABLED |
  3995. LPFC_SLI3_CRP_ENABLED |
  3996. LPFC_SLI3_BG_ENABLED |
  3997. LPFC_SLI3_DSS_ENABLED);
  3998. if (rc != MBX_SUCCESS) {
  3999. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4000. "0442 Adapter failed to init, mbxCmd x%x "
  4001. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  4002. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  4003. spin_lock_irq(&phba->hbalock);
  4004. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  4005. spin_unlock_irq(&phba->hbalock);
  4006. rc = -ENXIO;
  4007. } else {
  4008. /* Allow asynchronous mailbox command to go through */
  4009. spin_lock_irq(&phba->hbalock);
  4010. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4011. spin_unlock_irq(&phba->hbalock);
  4012. done = 1;
  4013. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  4014. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  4015. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  4016. "3110 Port did not grant ASABT\n");
  4017. }
  4018. }
  4019. if (!done) {
  4020. rc = -EINVAL;
  4021. goto do_prep_failed;
  4022. }
  4023. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  4024. if (!pmb->u.mb.un.varCfgPort.cMA) {
  4025. rc = -ENXIO;
  4026. goto do_prep_failed;
  4027. }
  4028. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  4029. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  4030. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  4031. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  4032. phba->max_vpi : phba->max_vports;
  4033. } else
  4034. phba->max_vpi = 0;
  4035. phba->fips_level = 0;
  4036. phba->fips_spec_rev = 0;
  4037. if (pmb->u.mb.un.varCfgPort.gdss) {
  4038. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  4039. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4040. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4041. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4042. "2850 Security Crypto Active. FIPS x%d "
  4043. "(Spec Rev: x%d)",
  4044. phba->fips_level, phba->fips_spec_rev);
  4045. }
  4046. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4047. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4048. "2856 Config Port Security Crypto "
  4049. "Error: x%x ",
  4050. pmb->u.mb.un.varCfgPort.sec_err);
  4051. }
  4052. if (pmb->u.mb.un.varCfgPort.gerbm)
  4053. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4054. if (pmb->u.mb.un.varCfgPort.gcrp)
  4055. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4056. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4057. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4058. if (phba->cfg_enable_bg) {
  4059. if (pmb->u.mb.un.varCfgPort.gbg)
  4060. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4061. else
  4062. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4063. "0443 Adapter did not grant "
  4064. "BlockGuard\n");
  4065. }
  4066. } else {
  4067. phba->hbq_get = NULL;
  4068. phba->port_gp = phba->mbox->us.s2.port;
  4069. phba->max_vpi = 0;
  4070. }
  4071. do_prep_failed:
  4072. mempool_free(pmb, phba->mbox_mem_pool);
  4073. return rc;
  4074. }
  4075. /**
  4076. * lpfc_sli_hba_setup - SLI intialization function
  4077. * @phba: Pointer to HBA context object.
  4078. *
  4079. * This function is the main SLI intialization function. This function
  4080. * is called by the HBA intialization code, HBA reset code and HBA
  4081. * error attention handler code. Caller is not required to hold any
  4082. * locks. This function issues config_port mailbox command to configure
  4083. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4084. * calls the config_port_post function to issue init_link mailbox
  4085. * command and to start the discovery. The function will return zero
  4086. * if successful, else it will return negative error code.
  4087. **/
  4088. int
  4089. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4090. {
  4091. uint32_t rc;
  4092. int mode = 3, i;
  4093. int longs;
  4094. switch (lpfc_sli_mode) {
  4095. case 2:
  4096. if (phba->cfg_enable_npiv) {
  4097. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4098. "1824 NPIV enabled: Override lpfc_sli_mode "
  4099. "parameter (%d) to auto (0).\n",
  4100. lpfc_sli_mode);
  4101. break;
  4102. }
  4103. mode = 2;
  4104. break;
  4105. case 0:
  4106. case 3:
  4107. break;
  4108. default:
  4109. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4110. "1819 Unrecognized lpfc_sli_mode "
  4111. "parameter: %d.\n", lpfc_sli_mode);
  4112. break;
  4113. }
  4114. rc = lpfc_sli_config_port(phba, mode);
  4115. if (rc && lpfc_sli_mode == 3)
  4116. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4117. "1820 Unable to select SLI-3. "
  4118. "Not supported by adapter.\n");
  4119. if (rc && mode != 2)
  4120. rc = lpfc_sli_config_port(phba, 2);
  4121. if (rc)
  4122. goto lpfc_sli_hba_setup_error;
  4123. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4124. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4125. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4126. if (!rc) {
  4127. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4128. "2709 This device supports "
  4129. "Advanced Error Reporting (AER)\n");
  4130. spin_lock_irq(&phba->hbalock);
  4131. phba->hba_flag |= HBA_AER_ENABLED;
  4132. spin_unlock_irq(&phba->hbalock);
  4133. } else {
  4134. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4135. "2708 This device does not support "
  4136. "Advanced Error Reporting (AER): %d\n",
  4137. rc);
  4138. phba->cfg_aer_support = 0;
  4139. }
  4140. }
  4141. if (phba->sli_rev == 3) {
  4142. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4143. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4144. } else {
  4145. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4146. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4147. phba->sli3_options = 0;
  4148. }
  4149. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4150. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4151. phba->sli_rev, phba->max_vpi);
  4152. rc = lpfc_sli_ring_map(phba);
  4153. if (rc)
  4154. goto lpfc_sli_hba_setup_error;
  4155. /* Initialize VPIs. */
  4156. if (phba->sli_rev == LPFC_SLI_REV3) {
  4157. /*
  4158. * The VPI bitmask and physical ID array are allocated
  4159. * and initialized once only - at driver load. A port
  4160. * reset doesn't need to reinitialize this memory.
  4161. */
  4162. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4163. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4164. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4165. GFP_KERNEL);
  4166. if (!phba->vpi_bmask) {
  4167. rc = -ENOMEM;
  4168. goto lpfc_sli_hba_setup_error;
  4169. }
  4170. phba->vpi_ids = kzalloc(
  4171. (phba->max_vpi+1) * sizeof(uint16_t),
  4172. GFP_KERNEL);
  4173. if (!phba->vpi_ids) {
  4174. kfree(phba->vpi_bmask);
  4175. rc = -ENOMEM;
  4176. goto lpfc_sli_hba_setup_error;
  4177. }
  4178. for (i = 0; i < phba->max_vpi; i++)
  4179. phba->vpi_ids[i] = i;
  4180. }
  4181. }
  4182. /* Init HBQs */
  4183. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4184. rc = lpfc_sli_hbq_setup(phba);
  4185. if (rc)
  4186. goto lpfc_sli_hba_setup_error;
  4187. }
  4188. spin_lock_irq(&phba->hbalock);
  4189. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4190. spin_unlock_irq(&phba->hbalock);
  4191. rc = lpfc_config_port_post(phba);
  4192. if (rc)
  4193. goto lpfc_sli_hba_setup_error;
  4194. return rc;
  4195. lpfc_sli_hba_setup_error:
  4196. phba->link_state = LPFC_HBA_ERROR;
  4197. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4198. "0445 Firmware initialization failed\n");
  4199. return rc;
  4200. }
  4201. /**
  4202. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4203. * @phba: Pointer to HBA context object.
  4204. * @mboxq: mailbox pointer.
  4205. * This function issue a dump mailbox command to read config region
  4206. * 23 and parse the records in the region and populate driver
  4207. * data structure.
  4208. **/
  4209. static int
  4210. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4211. {
  4212. LPFC_MBOXQ_t *mboxq;
  4213. struct lpfc_dmabuf *mp;
  4214. struct lpfc_mqe *mqe;
  4215. uint32_t data_length;
  4216. int rc;
  4217. /* Program the default value of vlan_id and fc_map */
  4218. phba->valid_vlan = 0;
  4219. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4220. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4221. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4222. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4223. if (!mboxq)
  4224. return -ENOMEM;
  4225. mqe = &mboxq->u.mqe;
  4226. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4227. rc = -ENOMEM;
  4228. goto out_free_mboxq;
  4229. }
  4230. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4231. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4232. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4233. "(%d):2571 Mailbox cmd x%x Status x%x "
  4234. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4235. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4236. "CQ: x%x x%x x%x x%x\n",
  4237. mboxq->vport ? mboxq->vport->vpi : 0,
  4238. bf_get(lpfc_mqe_command, mqe),
  4239. bf_get(lpfc_mqe_status, mqe),
  4240. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4241. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4242. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4243. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4244. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4245. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4246. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4247. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4248. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4249. mboxq->mcqe.word0,
  4250. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4251. mboxq->mcqe.trailer);
  4252. if (rc) {
  4253. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4254. kfree(mp);
  4255. rc = -EIO;
  4256. goto out_free_mboxq;
  4257. }
  4258. data_length = mqe->un.mb_words[5];
  4259. if (data_length > DMP_RGN23_SIZE) {
  4260. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4261. kfree(mp);
  4262. rc = -EIO;
  4263. goto out_free_mboxq;
  4264. }
  4265. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4266. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4267. kfree(mp);
  4268. rc = 0;
  4269. out_free_mboxq:
  4270. mempool_free(mboxq, phba->mbox_mem_pool);
  4271. return rc;
  4272. }
  4273. /**
  4274. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4275. * @phba: pointer to lpfc hba data structure.
  4276. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4277. * @vpd: pointer to the memory to hold resulting port vpd data.
  4278. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4279. * On output, the number of data bytes in @vpd.
  4280. *
  4281. * This routine executes a READ_REV SLI4 mailbox command. In
  4282. * addition, this routine gets the port vpd data.
  4283. *
  4284. * Return codes
  4285. * 0 - successful
  4286. * -ENOMEM - could not allocated memory.
  4287. **/
  4288. static int
  4289. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4290. uint8_t *vpd, uint32_t *vpd_size)
  4291. {
  4292. int rc = 0;
  4293. uint32_t dma_size;
  4294. struct lpfc_dmabuf *dmabuf;
  4295. struct lpfc_mqe *mqe;
  4296. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4297. if (!dmabuf)
  4298. return -ENOMEM;
  4299. /*
  4300. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4301. * mailbox command.
  4302. */
  4303. dma_size = *vpd_size;
  4304. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4305. dma_size,
  4306. &dmabuf->phys,
  4307. GFP_KERNEL);
  4308. if (!dmabuf->virt) {
  4309. kfree(dmabuf);
  4310. return -ENOMEM;
  4311. }
  4312. memset(dmabuf->virt, 0, dma_size);
  4313. /*
  4314. * The SLI4 implementation of READ_REV conflicts at word1,
  4315. * bits 31:16 and SLI4 adds vpd functionality not present
  4316. * in SLI3. This code corrects the conflicts.
  4317. */
  4318. lpfc_read_rev(phba, mboxq);
  4319. mqe = &mboxq->u.mqe;
  4320. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4321. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4322. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4323. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4324. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4325. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4326. if (rc) {
  4327. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4328. dmabuf->virt, dmabuf->phys);
  4329. kfree(dmabuf);
  4330. return -EIO;
  4331. }
  4332. /*
  4333. * The available vpd length cannot be bigger than the
  4334. * DMA buffer passed to the port. Catch the less than
  4335. * case and update the caller's size.
  4336. */
  4337. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4338. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4339. memcpy(vpd, dmabuf->virt, *vpd_size);
  4340. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4341. dmabuf->virt, dmabuf->phys);
  4342. kfree(dmabuf);
  4343. return 0;
  4344. }
  4345. /**
  4346. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4347. * @phba: pointer to lpfc hba data structure.
  4348. *
  4349. * This routine retrieves SLI4 device physical port name this PCI function
  4350. * is attached to.
  4351. *
  4352. * Return codes
  4353. * 0 - successful
  4354. * otherwise - failed to retrieve physical port name
  4355. **/
  4356. static int
  4357. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4358. {
  4359. LPFC_MBOXQ_t *mboxq;
  4360. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4361. struct lpfc_controller_attribute *cntl_attr;
  4362. struct lpfc_mbx_get_port_name *get_port_name;
  4363. void *virtaddr = NULL;
  4364. uint32_t alloclen, reqlen;
  4365. uint32_t shdr_status, shdr_add_status;
  4366. union lpfc_sli4_cfg_shdr *shdr;
  4367. char cport_name = 0;
  4368. int rc;
  4369. /* We assume nothing at this point */
  4370. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4371. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4372. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4373. if (!mboxq)
  4374. return -ENOMEM;
  4375. /* obtain link type and link number via READ_CONFIG */
  4376. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4377. lpfc_sli4_read_config(phba);
  4378. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4379. goto retrieve_ppname;
  4380. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4381. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4382. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4383. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4384. LPFC_SLI4_MBX_NEMBED);
  4385. if (alloclen < reqlen) {
  4386. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4387. "3084 Allocated DMA memory size (%d) is "
  4388. "less than the requested DMA memory size "
  4389. "(%d)\n", alloclen, reqlen);
  4390. rc = -ENOMEM;
  4391. goto out_free_mboxq;
  4392. }
  4393. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4394. virtaddr = mboxq->sge_array->addr[0];
  4395. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4396. shdr = &mbx_cntl_attr->cfg_shdr;
  4397. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4398. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4399. if (shdr_status || shdr_add_status || rc) {
  4400. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4401. "3085 Mailbox x%x (x%x/x%x) failed, "
  4402. "rc:x%x, status:x%x, add_status:x%x\n",
  4403. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4404. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4405. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4406. rc, shdr_status, shdr_add_status);
  4407. rc = -ENXIO;
  4408. goto out_free_mboxq;
  4409. }
  4410. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4411. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4412. phba->sli4_hba.lnk_info.lnk_tp =
  4413. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4414. phba->sli4_hba.lnk_info.lnk_no =
  4415. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4416. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4417. "3086 lnk_type:%d, lnk_numb:%d\n",
  4418. phba->sli4_hba.lnk_info.lnk_tp,
  4419. phba->sli4_hba.lnk_info.lnk_no);
  4420. retrieve_ppname:
  4421. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4422. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4423. sizeof(struct lpfc_mbx_get_port_name) -
  4424. sizeof(struct lpfc_sli4_cfg_mhdr),
  4425. LPFC_SLI4_MBX_EMBED);
  4426. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4427. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4428. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4429. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4430. phba->sli4_hba.lnk_info.lnk_tp);
  4431. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4432. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4433. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4434. if (shdr_status || shdr_add_status || rc) {
  4435. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4436. "3087 Mailbox x%x (x%x/x%x) failed: "
  4437. "rc:x%x, status:x%x, add_status:x%x\n",
  4438. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4439. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4440. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4441. rc, shdr_status, shdr_add_status);
  4442. rc = -ENXIO;
  4443. goto out_free_mboxq;
  4444. }
  4445. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4446. case LPFC_LINK_NUMBER_0:
  4447. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4448. &get_port_name->u.response);
  4449. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4450. break;
  4451. case LPFC_LINK_NUMBER_1:
  4452. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4453. &get_port_name->u.response);
  4454. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4455. break;
  4456. case LPFC_LINK_NUMBER_2:
  4457. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4458. &get_port_name->u.response);
  4459. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4460. break;
  4461. case LPFC_LINK_NUMBER_3:
  4462. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4463. &get_port_name->u.response);
  4464. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4465. break;
  4466. default:
  4467. break;
  4468. }
  4469. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4470. phba->Port[0] = cport_name;
  4471. phba->Port[1] = '\0';
  4472. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4473. "3091 SLI get port name: %s\n", phba->Port);
  4474. }
  4475. out_free_mboxq:
  4476. if (rc != MBX_TIMEOUT) {
  4477. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4478. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4479. else
  4480. mempool_free(mboxq, phba->mbox_mem_pool);
  4481. }
  4482. return rc;
  4483. }
  4484. /**
  4485. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4486. * @phba: pointer to lpfc hba data structure.
  4487. *
  4488. * This routine is called to explicitly arm the SLI4 device's completion and
  4489. * event queues
  4490. **/
  4491. static void
  4492. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4493. {
  4494. int fcp_eqidx;
  4495. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4496. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4497. fcp_eqidx = 0;
  4498. if (phba->sli4_hba.fcp_cq) {
  4499. do {
  4500. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4501. LPFC_QUEUE_REARM);
  4502. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4503. }
  4504. if (phba->sli4_hba.hba_eq) {
  4505. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4506. fcp_eqidx++)
  4507. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4508. LPFC_QUEUE_REARM);
  4509. }
  4510. }
  4511. /**
  4512. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4513. * @phba: Pointer to HBA context object.
  4514. * @type: The resource extent type.
  4515. * @extnt_count: buffer to hold port available extent count.
  4516. * @extnt_size: buffer to hold element count per extent.
  4517. *
  4518. * This function calls the port and retrievs the number of available
  4519. * extents and their size for a particular extent type.
  4520. *
  4521. * Returns: 0 if successful. Nonzero otherwise.
  4522. **/
  4523. int
  4524. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4525. uint16_t *extnt_count, uint16_t *extnt_size)
  4526. {
  4527. int rc = 0;
  4528. uint32_t length;
  4529. uint32_t mbox_tmo;
  4530. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4531. LPFC_MBOXQ_t *mbox;
  4532. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4533. if (!mbox)
  4534. return -ENOMEM;
  4535. /* Find out how many extents are available for this resource type */
  4536. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4537. sizeof(struct lpfc_sli4_cfg_mhdr));
  4538. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4539. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4540. length, LPFC_SLI4_MBX_EMBED);
  4541. /* Send an extents count of 0 - the GET doesn't use it. */
  4542. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4543. LPFC_SLI4_MBX_EMBED);
  4544. if (unlikely(rc)) {
  4545. rc = -EIO;
  4546. goto err_exit;
  4547. }
  4548. if (!phba->sli4_hba.intr_enable)
  4549. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4550. else {
  4551. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4552. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4553. }
  4554. if (unlikely(rc)) {
  4555. rc = -EIO;
  4556. goto err_exit;
  4557. }
  4558. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4559. if (bf_get(lpfc_mbox_hdr_status,
  4560. &rsrc_info->header.cfg_shdr.response)) {
  4561. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4562. "2930 Failed to get resource extents "
  4563. "Status 0x%x Add'l Status 0x%x\n",
  4564. bf_get(lpfc_mbox_hdr_status,
  4565. &rsrc_info->header.cfg_shdr.response),
  4566. bf_get(lpfc_mbox_hdr_add_status,
  4567. &rsrc_info->header.cfg_shdr.response));
  4568. rc = -EIO;
  4569. goto err_exit;
  4570. }
  4571. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4572. &rsrc_info->u.rsp);
  4573. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4574. &rsrc_info->u.rsp);
  4575. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4576. "3162 Retrieved extents type-%d from port: count:%d, "
  4577. "size:%d\n", type, *extnt_count, *extnt_size);
  4578. err_exit:
  4579. mempool_free(mbox, phba->mbox_mem_pool);
  4580. return rc;
  4581. }
  4582. /**
  4583. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4584. * @phba: Pointer to HBA context object.
  4585. * @type: The extent type to check.
  4586. *
  4587. * This function reads the current available extents from the port and checks
  4588. * if the extent count or extent size has changed since the last access.
  4589. * Callers use this routine post port reset to understand if there is a
  4590. * extent reprovisioning requirement.
  4591. *
  4592. * Returns:
  4593. * -Error: error indicates problem.
  4594. * 1: Extent count or size has changed.
  4595. * 0: No changes.
  4596. **/
  4597. static int
  4598. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4599. {
  4600. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4601. uint16_t size_diff, rsrc_ext_size;
  4602. int rc = 0;
  4603. struct lpfc_rsrc_blks *rsrc_entry;
  4604. struct list_head *rsrc_blk_list = NULL;
  4605. size_diff = 0;
  4606. curr_ext_cnt = 0;
  4607. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4608. &rsrc_ext_cnt,
  4609. &rsrc_ext_size);
  4610. if (unlikely(rc))
  4611. return -EIO;
  4612. switch (type) {
  4613. case LPFC_RSC_TYPE_FCOE_RPI:
  4614. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4615. break;
  4616. case LPFC_RSC_TYPE_FCOE_VPI:
  4617. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4618. break;
  4619. case LPFC_RSC_TYPE_FCOE_XRI:
  4620. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4621. break;
  4622. case LPFC_RSC_TYPE_FCOE_VFI:
  4623. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4624. break;
  4625. default:
  4626. break;
  4627. }
  4628. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4629. curr_ext_cnt++;
  4630. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4631. size_diff++;
  4632. }
  4633. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4634. rc = 1;
  4635. return rc;
  4636. }
  4637. /**
  4638. * lpfc_sli4_cfg_post_extnts -
  4639. * @phba: Pointer to HBA context object.
  4640. * @extnt_cnt - number of available extents.
  4641. * @type - the extent type (rpi, xri, vfi, vpi).
  4642. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4643. * @mbox - pointer to the caller's allocated mailbox structure.
  4644. *
  4645. * This function executes the extents allocation request. It also
  4646. * takes care of the amount of memory needed to allocate or get the
  4647. * allocated extents. It is the caller's responsibility to evaluate
  4648. * the response.
  4649. *
  4650. * Returns:
  4651. * -Error: Error value describes the condition found.
  4652. * 0: if successful
  4653. **/
  4654. static int
  4655. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4656. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4657. {
  4658. int rc = 0;
  4659. uint32_t req_len;
  4660. uint32_t emb_len;
  4661. uint32_t alloc_len, mbox_tmo;
  4662. /* Calculate the total requested length of the dma memory */
  4663. req_len = extnt_cnt * sizeof(uint16_t);
  4664. /*
  4665. * Calculate the size of an embedded mailbox. The uint32_t
  4666. * accounts for extents-specific word.
  4667. */
  4668. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4669. sizeof(uint32_t);
  4670. /*
  4671. * Presume the allocation and response will fit into an embedded
  4672. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4673. */
  4674. *emb = LPFC_SLI4_MBX_EMBED;
  4675. if (req_len > emb_len) {
  4676. req_len = extnt_cnt * sizeof(uint16_t) +
  4677. sizeof(union lpfc_sli4_cfg_shdr) +
  4678. sizeof(uint32_t);
  4679. *emb = LPFC_SLI4_MBX_NEMBED;
  4680. }
  4681. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4682. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4683. req_len, *emb);
  4684. if (alloc_len < req_len) {
  4685. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4686. "2982 Allocated DMA memory size (x%x) is "
  4687. "less than the requested DMA memory "
  4688. "size (x%x)\n", alloc_len, req_len);
  4689. return -ENOMEM;
  4690. }
  4691. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4692. if (unlikely(rc))
  4693. return -EIO;
  4694. if (!phba->sli4_hba.intr_enable)
  4695. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4696. else {
  4697. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4698. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4699. }
  4700. if (unlikely(rc))
  4701. rc = -EIO;
  4702. return rc;
  4703. }
  4704. /**
  4705. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4706. * @phba: Pointer to HBA context object.
  4707. * @type: The resource extent type to allocate.
  4708. *
  4709. * This function allocates the number of elements for the specified
  4710. * resource type.
  4711. **/
  4712. static int
  4713. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4714. {
  4715. bool emb = false;
  4716. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4717. uint16_t rsrc_id, rsrc_start, j, k;
  4718. uint16_t *ids;
  4719. int i, rc;
  4720. unsigned long longs;
  4721. unsigned long *bmask;
  4722. struct lpfc_rsrc_blks *rsrc_blks;
  4723. LPFC_MBOXQ_t *mbox;
  4724. uint32_t length;
  4725. struct lpfc_id_range *id_array = NULL;
  4726. void *virtaddr = NULL;
  4727. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4728. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4729. struct list_head *ext_blk_list;
  4730. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4731. &rsrc_cnt,
  4732. &rsrc_size);
  4733. if (unlikely(rc))
  4734. return -EIO;
  4735. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4736. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4737. "3009 No available Resource Extents "
  4738. "for resource type 0x%x: Count: 0x%x, "
  4739. "Size 0x%x\n", type, rsrc_cnt,
  4740. rsrc_size);
  4741. return -ENOMEM;
  4742. }
  4743. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4744. "2903 Post resource extents type-0x%x: "
  4745. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4746. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4747. if (!mbox)
  4748. return -ENOMEM;
  4749. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4750. if (unlikely(rc)) {
  4751. rc = -EIO;
  4752. goto err_exit;
  4753. }
  4754. /*
  4755. * Figure out where the response is located. Then get local pointers
  4756. * to the response data. The port does not guarantee to respond to
  4757. * all extents counts request so update the local variable with the
  4758. * allocated count from the port.
  4759. */
  4760. if (emb == LPFC_SLI4_MBX_EMBED) {
  4761. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4762. id_array = &rsrc_ext->u.rsp.id[0];
  4763. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4764. } else {
  4765. virtaddr = mbox->sge_array->addr[0];
  4766. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4767. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4768. id_array = &n_rsrc->id;
  4769. }
  4770. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4771. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4772. /*
  4773. * Based on the resource size and count, correct the base and max
  4774. * resource values.
  4775. */
  4776. length = sizeof(struct lpfc_rsrc_blks);
  4777. switch (type) {
  4778. case LPFC_RSC_TYPE_FCOE_RPI:
  4779. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4780. sizeof(unsigned long),
  4781. GFP_KERNEL);
  4782. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4783. rc = -ENOMEM;
  4784. goto err_exit;
  4785. }
  4786. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4787. sizeof(uint16_t),
  4788. GFP_KERNEL);
  4789. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4790. kfree(phba->sli4_hba.rpi_bmask);
  4791. rc = -ENOMEM;
  4792. goto err_exit;
  4793. }
  4794. /*
  4795. * The next_rpi was initialized with the maximum available
  4796. * count but the port may allocate a smaller number. Catch
  4797. * that case and update the next_rpi.
  4798. */
  4799. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4800. /* Initialize local ptrs for common extent processing later. */
  4801. bmask = phba->sli4_hba.rpi_bmask;
  4802. ids = phba->sli4_hba.rpi_ids;
  4803. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4804. break;
  4805. case LPFC_RSC_TYPE_FCOE_VPI:
  4806. phba->vpi_bmask = kzalloc(longs *
  4807. sizeof(unsigned long),
  4808. GFP_KERNEL);
  4809. if (unlikely(!phba->vpi_bmask)) {
  4810. rc = -ENOMEM;
  4811. goto err_exit;
  4812. }
  4813. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4814. sizeof(uint16_t),
  4815. GFP_KERNEL);
  4816. if (unlikely(!phba->vpi_ids)) {
  4817. kfree(phba->vpi_bmask);
  4818. rc = -ENOMEM;
  4819. goto err_exit;
  4820. }
  4821. /* Initialize local ptrs for common extent processing later. */
  4822. bmask = phba->vpi_bmask;
  4823. ids = phba->vpi_ids;
  4824. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4825. break;
  4826. case LPFC_RSC_TYPE_FCOE_XRI:
  4827. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4828. sizeof(unsigned long),
  4829. GFP_KERNEL);
  4830. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4831. rc = -ENOMEM;
  4832. goto err_exit;
  4833. }
  4834. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4835. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4836. sizeof(uint16_t),
  4837. GFP_KERNEL);
  4838. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4839. kfree(phba->sli4_hba.xri_bmask);
  4840. rc = -ENOMEM;
  4841. goto err_exit;
  4842. }
  4843. /* Initialize local ptrs for common extent processing later. */
  4844. bmask = phba->sli4_hba.xri_bmask;
  4845. ids = phba->sli4_hba.xri_ids;
  4846. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4847. break;
  4848. case LPFC_RSC_TYPE_FCOE_VFI:
  4849. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4850. sizeof(unsigned long),
  4851. GFP_KERNEL);
  4852. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4853. rc = -ENOMEM;
  4854. goto err_exit;
  4855. }
  4856. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4857. sizeof(uint16_t),
  4858. GFP_KERNEL);
  4859. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4860. kfree(phba->sli4_hba.vfi_bmask);
  4861. rc = -ENOMEM;
  4862. goto err_exit;
  4863. }
  4864. /* Initialize local ptrs for common extent processing later. */
  4865. bmask = phba->sli4_hba.vfi_bmask;
  4866. ids = phba->sli4_hba.vfi_ids;
  4867. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4868. break;
  4869. default:
  4870. /* Unsupported Opcode. Fail call. */
  4871. id_array = NULL;
  4872. bmask = NULL;
  4873. ids = NULL;
  4874. ext_blk_list = NULL;
  4875. goto err_exit;
  4876. }
  4877. /*
  4878. * Complete initializing the extent configuration with the
  4879. * allocated ids assigned to this function. The bitmask serves
  4880. * as an index into the array and manages the available ids. The
  4881. * array just stores the ids communicated to the port via the wqes.
  4882. */
  4883. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4884. if ((i % 2) == 0)
  4885. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4886. &id_array[k]);
  4887. else
  4888. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4889. &id_array[k]);
  4890. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4891. if (unlikely(!rsrc_blks)) {
  4892. rc = -ENOMEM;
  4893. kfree(bmask);
  4894. kfree(ids);
  4895. goto err_exit;
  4896. }
  4897. rsrc_blks->rsrc_start = rsrc_id;
  4898. rsrc_blks->rsrc_size = rsrc_size;
  4899. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4900. rsrc_start = rsrc_id;
  4901. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4902. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4903. lpfc_sli4_get_els_iocb_cnt(phba);
  4904. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4905. ids[j] = rsrc_id;
  4906. rsrc_id++;
  4907. j++;
  4908. }
  4909. /* Entire word processed. Get next word.*/
  4910. if ((i % 2) == 1)
  4911. k++;
  4912. }
  4913. err_exit:
  4914. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4915. return rc;
  4916. }
  4917. /**
  4918. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4919. * @phba: Pointer to HBA context object.
  4920. * @type: the extent's type.
  4921. *
  4922. * This function deallocates all extents of a particular resource type.
  4923. * SLI4 does not allow for deallocating a particular extent range. It
  4924. * is the caller's responsibility to release all kernel memory resources.
  4925. **/
  4926. static int
  4927. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4928. {
  4929. int rc;
  4930. uint32_t length, mbox_tmo = 0;
  4931. LPFC_MBOXQ_t *mbox;
  4932. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4933. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4934. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4935. if (!mbox)
  4936. return -ENOMEM;
  4937. /*
  4938. * This function sends an embedded mailbox because it only sends the
  4939. * the resource type. All extents of this type are released by the
  4940. * port.
  4941. */
  4942. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4943. sizeof(struct lpfc_sli4_cfg_mhdr));
  4944. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4945. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4946. length, LPFC_SLI4_MBX_EMBED);
  4947. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4948. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4949. LPFC_SLI4_MBX_EMBED);
  4950. if (unlikely(rc)) {
  4951. rc = -EIO;
  4952. goto out_free_mbox;
  4953. }
  4954. if (!phba->sli4_hba.intr_enable)
  4955. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4956. else {
  4957. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4958. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4959. }
  4960. if (unlikely(rc)) {
  4961. rc = -EIO;
  4962. goto out_free_mbox;
  4963. }
  4964. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4965. if (bf_get(lpfc_mbox_hdr_status,
  4966. &dealloc_rsrc->header.cfg_shdr.response)) {
  4967. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4968. "2919 Failed to release resource extents "
  4969. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4970. "Resource memory not released.\n",
  4971. type,
  4972. bf_get(lpfc_mbox_hdr_status,
  4973. &dealloc_rsrc->header.cfg_shdr.response),
  4974. bf_get(lpfc_mbox_hdr_add_status,
  4975. &dealloc_rsrc->header.cfg_shdr.response));
  4976. rc = -EIO;
  4977. goto out_free_mbox;
  4978. }
  4979. /* Release kernel memory resources for the specific type. */
  4980. switch (type) {
  4981. case LPFC_RSC_TYPE_FCOE_VPI:
  4982. kfree(phba->vpi_bmask);
  4983. kfree(phba->vpi_ids);
  4984. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4985. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4986. &phba->lpfc_vpi_blk_list, list) {
  4987. list_del_init(&rsrc_blk->list);
  4988. kfree(rsrc_blk);
  4989. }
  4990. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  4991. break;
  4992. case LPFC_RSC_TYPE_FCOE_XRI:
  4993. kfree(phba->sli4_hba.xri_bmask);
  4994. kfree(phba->sli4_hba.xri_ids);
  4995. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4996. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4997. list_del_init(&rsrc_blk->list);
  4998. kfree(rsrc_blk);
  4999. }
  5000. break;
  5001. case LPFC_RSC_TYPE_FCOE_VFI:
  5002. kfree(phba->sli4_hba.vfi_bmask);
  5003. kfree(phba->sli4_hba.vfi_ids);
  5004. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5005. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5006. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  5007. list_del_init(&rsrc_blk->list);
  5008. kfree(rsrc_blk);
  5009. }
  5010. break;
  5011. case LPFC_RSC_TYPE_FCOE_RPI:
  5012. /* RPI bitmask and physical id array are cleaned up earlier. */
  5013. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5014. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  5015. list_del_init(&rsrc_blk->list);
  5016. kfree(rsrc_blk);
  5017. }
  5018. break;
  5019. default:
  5020. break;
  5021. }
  5022. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5023. out_free_mbox:
  5024. mempool_free(mbox, phba->mbox_mem_pool);
  5025. return rc;
  5026. }
  5027. /**
  5028. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  5029. * @phba: Pointer to HBA context object.
  5030. *
  5031. * This function allocates all SLI4 resource identifiers.
  5032. **/
  5033. int
  5034. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  5035. {
  5036. int i, rc, error = 0;
  5037. uint16_t count, base;
  5038. unsigned long longs;
  5039. if (!phba->sli4_hba.rpi_hdrs_in_use)
  5040. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  5041. if (phba->sli4_hba.extents_in_use) {
  5042. /*
  5043. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5044. * resource extent count must be read and allocated before
  5045. * provisioning the resource id arrays.
  5046. */
  5047. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5048. LPFC_IDX_RSRC_RDY) {
  5049. /*
  5050. * Extent-based resources are set - the driver could
  5051. * be in a port reset. Figure out if any corrective
  5052. * actions need to be taken.
  5053. */
  5054. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5055. LPFC_RSC_TYPE_FCOE_VFI);
  5056. if (rc != 0)
  5057. error++;
  5058. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5059. LPFC_RSC_TYPE_FCOE_VPI);
  5060. if (rc != 0)
  5061. error++;
  5062. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5063. LPFC_RSC_TYPE_FCOE_XRI);
  5064. if (rc != 0)
  5065. error++;
  5066. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5067. LPFC_RSC_TYPE_FCOE_RPI);
  5068. if (rc != 0)
  5069. error++;
  5070. /*
  5071. * It's possible that the number of resources
  5072. * provided to this port instance changed between
  5073. * resets. Detect this condition and reallocate
  5074. * resources. Otherwise, there is no action.
  5075. */
  5076. if (error) {
  5077. lpfc_printf_log(phba, KERN_INFO,
  5078. LOG_MBOX | LOG_INIT,
  5079. "2931 Detected extent resource "
  5080. "change. Reallocating all "
  5081. "extents.\n");
  5082. rc = lpfc_sli4_dealloc_extent(phba,
  5083. LPFC_RSC_TYPE_FCOE_VFI);
  5084. rc = lpfc_sli4_dealloc_extent(phba,
  5085. LPFC_RSC_TYPE_FCOE_VPI);
  5086. rc = lpfc_sli4_dealloc_extent(phba,
  5087. LPFC_RSC_TYPE_FCOE_XRI);
  5088. rc = lpfc_sli4_dealloc_extent(phba,
  5089. LPFC_RSC_TYPE_FCOE_RPI);
  5090. } else
  5091. return 0;
  5092. }
  5093. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5094. if (unlikely(rc))
  5095. goto err_exit;
  5096. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5097. if (unlikely(rc))
  5098. goto err_exit;
  5099. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5100. if (unlikely(rc))
  5101. goto err_exit;
  5102. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5103. if (unlikely(rc))
  5104. goto err_exit;
  5105. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5106. LPFC_IDX_RSRC_RDY);
  5107. return rc;
  5108. } else {
  5109. /*
  5110. * The port does not support resource extents. The XRI, VPI,
  5111. * VFI, RPI resource ids were determined from READ_CONFIG.
  5112. * Just allocate the bitmasks and provision the resource id
  5113. * arrays. If a port reset is active, the resources don't
  5114. * need any action - just exit.
  5115. */
  5116. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5117. LPFC_IDX_RSRC_RDY) {
  5118. lpfc_sli4_dealloc_resource_identifiers(phba);
  5119. lpfc_sli4_remove_rpis(phba);
  5120. }
  5121. /* RPIs. */
  5122. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5123. if (count <= 0) {
  5124. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5125. "3279 Invalid provisioning of "
  5126. "rpi:%d\n", count);
  5127. rc = -EINVAL;
  5128. goto err_exit;
  5129. }
  5130. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5131. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5132. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5133. sizeof(unsigned long),
  5134. GFP_KERNEL);
  5135. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5136. rc = -ENOMEM;
  5137. goto err_exit;
  5138. }
  5139. phba->sli4_hba.rpi_ids = kzalloc(count *
  5140. sizeof(uint16_t),
  5141. GFP_KERNEL);
  5142. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5143. rc = -ENOMEM;
  5144. goto free_rpi_bmask;
  5145. }
  5146. for (i = 0; i < count; i++)
  5147. phba->sli4_hba.rpi_ids[i] = base + i;
  5148. /* VPIs. */
  5149. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5150. if (count <= 0) {
  5151. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5152. "3280 Invalid provisioning of "
  5153. "vpi:%d\n", count);
  5154. rc = -EINVAL;
  5155. goto free_rpi_ids;
  5156. }
  5157. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5158. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5159. phba->vpi_bmask = kzalloc(longs *
  5160. sizeof(unsigned long),
  5161. GFP_KERNEL);
  5162. if (unlikely(!phba->vpi_bmask)) {
  5163. rc = -ENOMEM;
  5164. goto free_rpi_ids;
  5165. }
  5166. phba->vpi_ids = kzalloc(count *
  5167. sizeof(uint16_t),
  5168. GFP_KERNEL);
  5169. if (unlikely(!phba->vpi_ids)) {
  5170. rc = -ENOMEM;
  5171. goto free_vpi_bmask;
  5172. }
  5173. for (i = 0; i < count; i++)
  5174. phba->vpi_ids[i] = base + i;
  5175. /* XRIs. */
  5176. count = phba->sli4_hba.max_cfg_param.max_xri;
  5177. if (count <= 0) {
  5178. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5179. "3281 Invalid provisioning of "
  5180. "xri:%d\n", count);
  5181. rc = -EINVAL;
  5182. goto free_vpi_ids;
  5183. }
  5184. base = phba->sli4_hba.max_cfg_param.xri_base;
  5185. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5186. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5187. sizeof(unsigned long),
  5188. GFP_KERNEL);
  5189. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5190. rc = -ENOMEM;
  5191. goto free_vpi_ids;
  5192. }
  5193. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5194. phba->sli4_hba.xri_ids = kzalloc(count *
  5195. sizeof(uint16_t),
  5196. GFP_KERNEL);
  5197. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5198. rc = -ENOMEM;
  5199. goto free_xri_bmask;
  5200. }
  5201. for (i = 0; i < count; i++)
  5202. phba->sli4_hba.xri_ids[i] = base + i;
  5203. /* VFIs. */
  5204. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5205. if (count <= 0) {
  5206. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5207. "3282 Invalid provisioning of "
  5208. "vfi:%d\n", count);
  5209. rc = -EINVAL;
  5210. goto free_xri_ids;
  5211. }
  5212. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5213. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5214. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5215. sizeof(unsigned long),
  5216. GFP_KERNEL);
  5217. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5218. rc = -ENOMEM;
  5219. goto free_xri_ids;
  5220. }
  5221. phba->sli4_hba.vfi_ids = kzalloc(count *
  5222. sizeof(uint16_t),
  5223. GFP_KERNEL);
  5224. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5225. rc = -ENOMEM;
  5226. goto free_vfi_bmask;
  5227. }
  5228. for (i = 0; i < count; i++)
  5229. phba->sli4_hba.vfi_ids[i] = base + i;
  5230. /*
  5231. * Mark all resources ready. An HBA reset doesn't need
  5232. * to reset the initialization.
  5233. */
  5234. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5235. LPFC_IDX_RSRC_RDY);
  5236. return 0;
  5237. }
  5238. free_vfi_bmask:
  5239. kfree(phba->sli4_hba.vfi_bmask);
  5240. free_xri_ids:
  5241. kfree(phba->sli4_hba.xri_ids);
  5242. free_xri_bmask:
  5243. kfree(phba->sli4_hba.xri_bmask);
  5244. free_vpi_ids:
  5245. kfree(phba->vpi_ids);
  5246. free_vpi_bmask:
  5247. kfree(phba->vpi_bmask);
  5248. free_rpi_ids:
  5249. kfree(phba->sli4_hba.rpi_ids);
  5250. free_rpi_bmask:
  5251. kfree(phba->sli4_hba.rpi_bmask);
  5252. err_exit:
  5253. return rc;
  5254. }
  5255. /**
  5256. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5257. * @phba: Pointer to HBA context object.
  5258. *
  5259. * This function allocates the number of elements for the specified
  5260. * resource type.
  5261. **/
  5262. int
  5263. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5264. {
  5265. if (phba->sli4_hba.extents_in_use) {
  5266. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5267. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5268. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5269. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5270. } else {
  5271. kfree(phba->vpi_bmask);
  5272. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5273. kfree(phba->vpi_ids);
  5274. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5275. kfree(phba->sli4_hba.xri_bmask);
  5276. kfree(phba->sli4_hba.xri_ids);
  5277. kfree(phba->sli4_hba.vfi_bmask);
  5278. kfree(phba->sli4_hba.vfi_ids);
  5279. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5280. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5281. }
  5282. return 0;
  5283. }
  5284. /**
  5285. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5286. * @phba: Pointer to HBA context object.
  5287. * @type: The resource extent type.
  5288. * @extnt_count: buffer to hold port extent count response
  5289. * @extnt_size: buffer to hold port extent size response.
  5290. *
  5291. * This function calls the port to read the host allocated extents
  5292. * for a particular type.
  5293. **/
  5294. int
  5295. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5296. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5297. {
  5298. bool emb;
  5299. int rc = 0;
  5300. uint16_t curr_blks = 0;
  5301. uint32_t req_len, emb_len;
  5302. uint32_t alloc_len, mbox_tmo;
  5303. struct list_head *blk_list_head;
  5304. struct lpfc_rsrc_blks *rsrc_blk;
  5305. LPFC_MBOXQ_t *mbox;
  5306. void *virtaddr = NULL;
  5307. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5308. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5309. union lpfc_sli4_cfg_shdr *shdr;
  5310. switch (type) {
  5311. case LPFC_RSC_TYPE_FCOE_VPI:
  5312. blk_list_head = &phba->lpfc_vpi_blk_list;
  5313. break;
  5314. case LPFC_RSC_TYPE_FCOE_XRI:
  5315. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5316. break;
  5317. case LPFC_RSC_TYPE_FCOE_VFI:
  5318. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5319. break;
  5320. case LPFC_RSC_TYPE_FCOE_RPI:
  5321. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5322. break;
  5323. default:
  5324. return -EIO;
  5325. }
  5326. /* Count the number of extents currently allocatd for this type. */
  5327. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5328. if (curr_blks == 0) {
  5329. /*
  5330. * The GET_ALLOCATED mailbox does not return the size,
  5331. * just the count. The size should be just the size
  5332. * stored in the current allocated block and all sizes
  5333. * for an extent type are the same so set the return
  5334. * value now.
  5335. */
  5336. *extnt_size = rsrc_blk->rsrc_size;
  5337. }
  5338. curr_blks++;
  5339. }
  5340. /* Calculate the total requested length of the dma memory. */
  5341. req_len = curr_blks * sizeof(uint16_t);
  5342. /*
  5343. * Calculate the size of an embedded mailbox. The uint32_t
  5344. * accounts for extents-specific word.
  5345. */
  5346. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5347. sizeof(uint32_t);
  5348. /*
  5349. * Presume the allocation and response will fit into an embedded
  5350. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5351. */
  5352. emb = LPFC_SLI4_MBX_EMBED;
  5353. req_len = emb_len;
  5354. if (req_len > emb_len) {
  5355. req_len = curr_blks * sizeof(uint16_t) +
  5356. sizeof(union lpfc_sli4_cfg_shdr) +
  5357. sizeof(uint32_t);
  5358. emb = LPFC_SLI4_MBX_NEMBED;
  5359. }
  5360. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5361. if (!mbox)
  5362. return -ENOMEM;
  5363. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5364. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5365. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5366. req_len, emb);
  5367. if (alloc_len < req_len) {
  5368. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5369. "2983 Allocated DMA memory size (x%x) is "
  5370. "less than the requested DMA memory "
  5371. "size (x%x)\n", alloc_len, req_len);
  5372. rc = -ENOMEM;
  5373. goto err_exit;
  5374. }
  5375. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5376. if (unlikely(rc)) {
  5377. rc = -EIO;
  5378. goto err_exit;
  5379. }
  5380. if (!phba->sli4_hba.intr_enable)
  5381. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5382. else {
  5383. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5384. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5385. }
  5386. if (unlikely(rc)) {
  5387. rc = -EIO;
  5388. goto err_exit;
  5389. }
  5390. /*
  5391. * Figure out where the response is located. Then get local pointers
  5392. * to the response data. The port does not guarantee to respond to
  5393. * all extents counts request so update the local variable with the
  5394. * allocated count from the port.
  5395. */
  5396. if (emb == LPFC_SLI4_MBX_EMBED) {
  5397. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5398. shdr = &rsrc_ext->header.cfg_shdr;
  5399. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5400. } else {
  5401. virtaddr = mbox->sge_array->addr[0];
  5402. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5403. shdr = &n_rsrc->cfg_shdr;
  5404. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5405. }
  5406. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5407. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5408. "2984 Failed to read allocated resources "
  5409. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5410. type,
  5411. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5412. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5413. rc = -EIO;
  5414. goto err_exit;
  5415. }
  5416. err_exit:
  5417. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5418. return rc;
  5419. }
  5420. /**
  5421. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5422. * @phba: pointer to lpfc hba data structure.
  5423. *
  5424. * This routine walks the list of els buffers that have been allocated and
  5425. * repost them to the port by using SGL block post. This is needed after a
  5426. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5427. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5428. * SGL block post mailbox commands to post them to the port. For single els
  5429. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5430. * mailbox command for posting.
  5431. *
  5432. * Returns: 0 = success, non-zero failure.
  5433. **/
  5434. static int
  5435. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5436. {
  5437. struct lpfc_sglq *sglq_entry = NULL;
  5438. struct lpfc_sglq *sglq_entry_next = NULL;
  5439. struct lpfc_sglq *sglq_entry_first = NULL;
  5440. int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5441. int last_xritag = NO_XRI;
  5442. LIST_HEAD(prep_sgl_list);
  5443. LIST_HEAD(blck_sgl_list);
  5444. LIST_HEAD(allc_sgl_list);
  5445. LIST_HEAD(post_sgl_list);
  5446. LIST_HEAD(free_sgl_list);
  5447. spin_lock_irq(&phba->hbalock);
  5448. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5449. spin_unlock_irq(&phba->hbalock);
  5450. total_cnt = phba->sli4_hba.els_xri_cnt;
  5451. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5452. &allc_sgl_list, list) {
  5453. list_del_init(&sglq_entry->list);
  5454. block_cnt++;
  5455. if ((last_xritag != NO_XRI) &&
  5456. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5457. /* a hole in xri block, form a sgl posting block */
  5458. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5459. post_cnt = block_cnt - 1;
  5460. /* prepare list for next posting block */
  5461. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5462. block_cnt = 1;
  5463. } else {
  5464. /* prepare list for next posting block */
  5465. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5466. /* enough sgls for non-embed sgl mbox command */
  5467. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5468. list_splice_init(&prep_sgl_list,
  5469. &blck_sgl_list);
  5470. post_cnt = block_cnt;
  5471. block_cnt = 0;
  5472. }
  5473. }
  5474. num_posted++;
  5475. /* keep track of last sgl's xritag */
  5476. last_xritag = sglq_entry->sli4_xritag;
  5477. /* end of repost sgl list condition for els buffers */
  5478. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5479. if (post_cnt == 0) {
  5480. list_splice_init(&prep_sgl_list,
  5481. &blck_sgl_list);
  5482. post_cnt = block_cnt;
  5483. } else if (block_cnt == 1) {
  5484. status = lpfc_sli4_post_sgl(phba,
  5485. sglq_entry->phys, 0,
  5486. sglq_entry->sli4_xritag);
  5487. if (!status) {
  5488. /* successful, put sgl to posted list */
  5489. list_add_tail(&sglq_entry->list,
  5490. &post_sgl_list);
  5491. } else {
  5492. /* Failure, put sgl to free list */
  5493. lpfc_printf_log(phba, KERN_WARNING,
  5494. LOG_SLI,
  5495. "3159 Failed to post els "
  5496. "sgl, xritag:x%x\n",
  5497. sglq_entry->sli4_xritag);
  5498. list_add_tail(&sglq_entry->list,
  5499. &free_sgl_list);
  5500. total_cnt--;
  5501. }
  5502. }
  5503. }
  5504. /* continue until a nembed page worth of sgls */
  5505. if (post_cnt == 0)
  5506. continue;
  5507. /* post the els buffer list sgls as a block */
  5508. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5509. post_cnt);
  5510. if (!status) {
  5511. /* success, put sgl list to posted sgl list */
  5512. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5513. } else {
  5514. /* Failure, put sgl list to free sgl list */
  5515. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5516. struct lpfc_sglq,
  5517. list);
  5518. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5519. "3160 Failed to post els sgl-list, "
  5520. "xritag:x%x-x%x\n",
  5521. sglq_entry_first->sli4_xritag,
  5522. (sglq_entry_first->sli4_xritag +
  5523. post_cnt - 1));
  5524. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5525. total_cnt -= post_cnt;
  5526. }
  5527. /* don't reset xirtag due to hole in xri block */
  5528. if (block_cnt == 0)
  5529. last_xritag = NO_XRI;
  5530. /* reset els sgl post count for next round of posting */
  5531. post_cnt = 0;
  5532. }
  5533. /* update the number of XRIs posted for ELS */
  5534. phba->sli4_hba.els_xri_cnt = total_cnt;
  5535. /* free the els sgls failed to post */
  5536. lpfc_free_sgl_list(phba, &free_sgl_list);
  5537. /* push els sgls posted to the availble list */
  5538. if (!list_empty(&post_sgl_list)) {
  5539. spin_lock_irq(&phba->hbalock);
  5540. list_splice_init(&post_sgl_list,
  5541. &phba->sli4_hba.lpfc_sgl_list);
  5542. spin_unlock_irq(&phba->hbalock);
  5543. } else {
  5544. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5545. "3161 Failure to post els sgl to port.\n");
  5546. return -EIO;
  5547. }
  5548. return 0;
  5549. }
  5550. /**
  5551. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5552. * @phba: Pointer to HBA context object.
  5553. *
  5554. * This function is the main SLI4 device intialization PCI function. This
  5555. * function is called by the HBA intialization code, HBA reset code and
  5556. * HBA error attention handler code. Caller is not required to hold any
  5557. * locks.
  5558. **/
  5559. int
  5560. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5561. {
  5562. int rc;
  5563. LPFC_MBOXQ_t *mboxq;
  5564. struct lpfc_mqe *mqe;
  5565. uint8_t *vpd;
  5566. uint32_t vpd_size;
  5567. uint32_t ftr_rsp = 0;
  5568. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5569. struct lpfc_vport *vport = phba->pport;
  5570. struct lpfc_dmabuf *mp;
  5571. /* Perform a PCI function reset to start from clean */
  5572. rc = lpfc_pci_function_reset(phba);
  5573. if (unlikely(rc))
  5574. return -ENODEV;
  5575. /* Check the HBA Host Status Register for readyness */
  5576. rc = lpfc_sli4_post_status_check(phba);
  5577. if (unlikely(rc))
  5578. return -ENODEV;
  5579. else {
  5580. spin_lock_irq(&phba->hbalock);
  5581. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5582. spin_unlock_irq(&phba->hbalock);
  5583. }
  5584. /*
  5585. * Allocate a single mailbox container for initializing the
  5586. * port.
  5587. */
  5588. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5589. if (!mboxq)
  5590. return -ENOMEM;
  5591. /* Issue READ_REV to collect vpd and FW information. */
  5592. vpd_size = SLI4_PAGE_SIZE;
  5593. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5594. if (!vpd) {
  5595. rc = -ENOMEM;
  5596. goto out_free_mbox;
  5597. }
  5598. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5599. if (unlikely(rc)) {
  5600. kfree(vpd);
  5601. goto out_free_mbox;
  5602. }
  5603. mqe = &mboxq->u.mqe;
  5604. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5605. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5606. phba->hba_flag |= HBA_FCOE_MODE;
  5607. else
  5608. phba->hba_flag &= ~HBA_FCOE_MODE;
  5609. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5610. LPFC_DCBX_CEE_MODE)
  5611. phba->hba_flag |= HBA_FIP_SUPPORT;
  5612. else
  5613. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5614. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5615. if (phba->sli_rev != LPFC_SLI_REV4) {
  5616. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5617. "0376 READ_REV Error. SLI Level %d "
  5618. "FCoE enabled %d\n",
  5619. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5620. rc = -EIO;
  5621. kfree(vpd);
  5622. goto out_free_mbox;
  5623. }
  5624. /*
  5625. * Continue initialization with default values even if driver failed
  5626. * to read FCoE param config regions, only read parameters if the
  5627. * board is FCoE
  5628. */
  5629. if (phba->hba_flag & HBA_FCOE_MODE &&
  5630. lpfc_sli4_read_fcoe_params(phba))
  5631. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5632. "2570 Failed to read FCoE parameters\n");
  5633. /*
  5634. * Retrieve sli4 device physical port name, failure of doing it
  5635. * is considered as non-fatal.
  5636. */
  5637. rc = lpfc_sli4_retrieve_pport_name(phba);
  5638. if (!rc)
  5639. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5640. "3080 Successful retrieving SLI4 device "
  5641. "physical port name: %s.\n", phba->Port);
  5642. /*
  5643. * Evaluate the read rev and vpd data. Populate the driver
  5644. * state with the results. If this routine fails, the failure
  5645. * is not fatal as the driver will use generic values.
  5646. */
  5647. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5648. if (unlikely(!rc)) {
  5649. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5650. "0377 Error %d parsing vpd. "
  5651. "Using defaults.\n", rc);
  5652. rc = 0;
  5653. }
  5654. kfree(vpd);
  5655. /* Save information as VPD data */
  5656. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5657. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5658. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5659. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5660. &mqe->un.read_rev);
  5661. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5662. &mqe->un.read_rev);
  5663. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5664. &mqe->un.read_rev);
  5665. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5666. &mqe->un.read_rev);
  5667. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5668. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5669. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5670. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5671. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5672. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5673. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5674. "(%d):0380 READ_REV Status x%x "
  5675. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5676. mboxq->vport ? mboxq->vport->vpi : 0,
  5677. bf_get(lpfc_mqe_status, mqe),
  5678. phba->vpd.rev.opFwName,
  5679. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5680. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5681. /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
  5682. rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
  5683. if (phba->pport->cfg_lun_queue_depth > rc) {
  5684. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  5685. "3362 LUN queue depth changed from %d to %d\n",
  5686. phba->pport->cfg_lun_queue_depth, rc);
  5687. phba->pport->cfg_lun_queue_depth = rc;
  5688. }
  5689. /*
  5690. * Discover the port's supported feature set and match it against the
  5691. * hosts requests.
  5692. */
  5693. lpfc_request_features(phba, mboxq);
  5694. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5695. if (unlikely(rc)) {
  5696. rc = -EIO;
  5697. goto out_free_mbox;
  5698. }
  5699. /*
  5700. * The port must support FCP initiator mode as this is the
  5701. * only mode running in the host.
  5702. */
  5703. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5704. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5705. "0378 No support for fcpi mode.\n");
  5706. ftr_rsp++;
  5707. }
  5708. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5709. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5710. else
  5711. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5712. /*
  5713. * If the port cannot support the host's requested features
  5714. * then turn off the global config parameters to disable the
  5715. * feature in the driver. This is not a fatal error.
  5716. */
  5717. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5718. if (phba->cfg_enable_bg) {
  5719. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5720. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5721. else
  5722. ftr_rsp++;
  5723. }
  5724. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5725. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5726. ftr_rsp++;
  5727. if (ftr_rsp) {
  5728. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5729. "0379 Feature Mismatch Data: x%08x %08x "
  5730. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5731. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5732. phba->cfg_enable_npiv, phba->max_vpi);
  5733. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5734. phba->cfg_enable_bg = 0;
  5735. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5736. phba->cfg_enable_npiv = 0;
  5737. }
  5738. /* These SLI3 features are assumed in SLI4 */
  5739. spin_lock_irq(&phba->hbalock);
  5740. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5741. spin_unlock_irq(&phba->hbalock);
  5742. /*
  5743. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5744. * calls depends on these resources to complete port setup.
  5745. */
  5746. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5747. if (rc) {
  5748. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5749. "2920 Failed to alloc Resource IDs "
  5750. "rc = x%x\n", rc);
  5751. goto out_free_mbox;
  5752. }
  5753. /* Read the port's service parameters. */
  5754. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5755. if (rc) {
  5756. phba->link_state = LPFC_HBA_ERROR;
  5757. rc = -ENOMEM;
  5758. goto out_free_mbox;
  5759. }
  5760. mboxq->vport = vport;
  5761. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5762. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5763. if (rc == MBX_SUCCESS) {
  5764. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5765. rc = 0;
  5766. }
  5767. /*
  5768. * This memory was allocated by the lpfc_read_sparam routine. Release
  5769. * it to the mbuf pool.
  5770. */
  5771. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5772. kfree(mp);
  5773. mboxq->context1 = NULL;
  5774. if (unlikely(rc)) {
  5775. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5776. "0382 READ_SPARAM command failed "
  5777. "status %d, mbxStatus x%x\n",
  5778. rc, bf_get(lpfc_mqe_status, mqe));
  5779. phba->link_state = LPFC_HBA_ERROR;
  5780. rc = -EIO;
  5781. goto out_free_mbox;
  5782. }
  5783. lpfc_update_vport_wwn(vport);
  5784. /* Update the fc_host data structures with new wwn. */
  5785. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5786. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5787. /* update host els and scsi xri-sgl sizes and mappings */
  5788. rc = lpfc_sli4_xri_sgl_update(phba);
  5789. if (unlikely(rc)) {
  5790. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5791. "1400 Failed to update xri-sgl size and "
  5792. "mapping: %d\n", rc);
  5793. goto out_free_mbox;
  5794. }
  5795. /* register the els sgl pool to the port */
  5796. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5797. if (unlikely(rc)) {
  5798. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5799. "0582 Error %d during els sgl post "
  5800. "operation\n", rc);
  5801. rc = -ENODEV;
  5802. goto out_free_mbox;
  5803. }
  5804. /* register the allocated scsi sgl pool to the port */
  5805. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5806. if (unlikely(rc)) {
  5807. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5808. "0383 Error %d during scsi sgl post "
  5809. "operation\n", rc);
  5810. /* Some Scsi buffers were moved to the abort scsi list */
  5811. /* A pci function reset will repost them */
  5812. rc = -ENODEV;
  5813. goto out_free_mbox;
  5814. }
  5815. /* Post the rpi header region to the device. */
  5816. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5817. if (unlikely(rc)) {
  5818. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5819. "0393 Error %d during rpi post operation\n",
  5820. rc);
  5821. rc = -ENODEV;
  5822. goto out_free_mbox;
  5823. }
  5824. lpfc_sli4_node_prep(phba);
  5825. /* Create all the SLI4 queues */
  5826. rc = lpfc_sli4_queue_create(phba);
  5827. if (rc) {
  5828. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5829. "3089 Failed to allocate queues\n");
  5830. rc = -ENODEV;
  5831. goto out_stop_timers;
  5832. }
  5833. /* Set up all the queues to the device */
  5834. rc = lpfc_sli4_queue_setup(phba);
  5835. if (unlikely(rc)) {
  5836. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5837. "0381 Error %d during queue setup.\n ", rc);
  5838. goto out_destroy_queue;
  5839. }
  5840. /* Arm the CQs and then EQs on device */
  5841. lpfc_sli4_arm_cqeq_intr(phba);
  5842. /* Indicate device interrupt mode */
  5843. phba->sli4_hba.intr_enable = 1;
  5844. /* Allow asynchronous mailbox command to go through */
  5845. spin_lock_irq(&phba->hbalock);
  5846. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5847. spin_unlock_irq(&phba->hbalock);
  5848. /* Post receive buffers to the device */
  5849. lpfc_sli4_rb_setup(phba);
  5850. /* Reset HBA FCF states after HBA reset */
  5851. phba->fcf.fcf_flag = 0;
  5852. phba->fcf.current_rec.flag = 0;
  5853. /* Start the ELS watchdog timer */
  5854. mod_timer(&vport->els_tmofunc,
  5855. jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
  5856. /* Start heart beat timer */
  5857. mod_timer(&phba->hb_tmofunc,
  5858. jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  5859. phba->hb_outstanding = 0;
  5860. phba->last_completion_time = jiffies;
  5861. /* Start error attention (ERATT) polling timer */
  5862. mod_timer(&phba->eratt_poll,
  5863. jiffies + msecs_to_jiffies(1000 * LPFC_ERATT_POLL_INTERVAL));
  5864. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5865. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5866. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5867. if (!rc) {
  5868. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5869. "2829 This device supports "
  5870. "Advanced Error Reporting (AER)\n");
  5871. spin_lock_irq(&phba->hbalock);
  5872. phba->hba_flag |= HBA_AER_ENABLED;
  5873. spin_unlock_irq(&phba->hbalock);
  5874. } else {
  5875. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5876. "2830 This device does not support "
  5877. "Advanced Error Reporting (AER)\n");
  5878. phba->cfg_aer_support = 0;
  5879. }
  5880. rc = 0;
  5881. }
  5882. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5883. /*
  5884. * The FC Port needs to register FCFI (index 0)
  5885. */
  5886. lpfc_reg_fcfi(phba, mboxq);
  5887. mboxq->vport = phba->pport;
  5888. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5889. if (rc != MBX_SUCCESS)
  5890. goto out_unset_queue;
  5891. rc = 0;
  5892. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5893. &mboxq->u.mqe.un.reg_fcfi);
  5894. /* Check if the port is configured to be disabled */
  5895. lpfc_sli_read_link_ste(phba);
  5896. }
  5897. /*
  5898. * The port is ready, set the host's link state to LINK_DOWN
  5899. * in preparation for link interrupts.
  5900. */
  5901. spin_lock_irq(&phba->hbalock);
  5902. phba->link_state = LPFC_LINK_DOWN;
  5903. spin_unlock_irq(&phba->hbalock);
  5904. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5905. (phba->hba_flag & LINK_DISABLED)) {
  5906. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5907. "3103 Adapter Link is disabled.\n");
  5908. lpfc_down_link(phba, mboxq);
  5909. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5910. if (rc != MBX_SUCCESS) {
  5911. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5912. "3104 Adapter failed to issue "
  5913. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5914. goto out_unset_queue;
  5915. }
  5916. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5917. /* don't perform init_link on SLI4 FC port loopback test */
  5918. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5919. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5920. if (rc)
  5921. goto out_unset_queue;
  5922. }
  5923. }
  5924. mempool_free(mboxq, phba->mbox_mem_pool);
  5925. return rc;
  5926. out_unset_queue:
  5927. /* Unset all the queues set up in this routine when error out */
  5928. lpfc_sli4_queue_unset(phba);
  5929. out_destroy_queue:
  5930. lpfc_sli4_queue_destroy(phba);
  5931. out_stop_timers:
  5932. lpfc_stop_hba_timers(phba);
  5933. out_free_mbox:
  5934. mempool_free(mboxq, phba->mbox_mem_pool);
  5935. return rc;
  5936. }
  5937. /**
  5938. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5939. * @ptr: context object - pointer to hba structure.
  5940. *
  5941. * This is the callback function for mailbox timer. The mailbox
  5942. * timer is armed when a new mailbox command is issued and the timer
  5943. * is deleted when the mailbox complete. The function is called by
  5944. * the kernel timer code when a mailbox does not complete within
  5945. * expected time. This function wakes up the worker thread to
  5946. * process the mailbox timeout and returns. All the processing is
  5947. * done by the worker thread function lpfc_mbox_timeout_handler.
  5948. **/
  5949. void
  5950. lpfc_mbox_timeout(unsigned long ptr)
  5951. {
  5952. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5953. unsigned long iflag;
  5954. uint32_t tmo_posted;
  5955. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5956. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5957. if (!tmo_posted)
  5958. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5959. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5960. if (!tmo_posted)
  5961. lpfc_worker_wake_up(phba);
  5962. return;
  5963. }
  5964. /**
  5965. * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
  5966. * are pending
  5967. * @phba: Pointer to HBA context object.
  5968. *
  5969. * This function checks if any mailbox completions are present on the mailbox
  5970. * completion queue.
  5971. **/
  5972. bool
  5973. lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
  5974. {
  5975. uint32_t idx;
  5976. struct lpfc_queue *mcq;
  5977. struct lpfc_mcqe *mcqe;
  5978. bool pending_completions = false;
  5979. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  5980. return false;
  5981. /* Check for completions on mailbox completion queue */
  5982. mcq = phba->sli4_hba.mbx_cq;
  5983. idx = mcq->hba_index;
  5984. while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
  5985. mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
  5986. if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
  5987. (!bf_get_le32(lpfc_trailer_async, mcqe))) {
  5988. pending_completions = true;
  5989. break;
  5990. }
  5991. idx = (idx + 1) % mcq->entry_count;
  5992. if (mcq->hba_index == idx)
  5993. break;
  5994. }
  5995. return pending_completions;
  5996. }
  5997. /**
  5998. * lpfc_sli4_process_missed_mbox_completions - process mbox completions
  5999. * that were missed.
  6000. * @phba: Pointer to HBA context object.
  6001. *
  6002. * For sli4, it is possible to miss an interrupt. As such mbox completions
  6003. * maybe missed causing erroneous mailbox timeouts to occur. This function
  6004. * checks to see if mbox completions are on the mailbox completion queue
  6005. * and will process all the completions associated with the eq for the
  6006. * mailbox completion queue.
  6007. **/
  6008. bool
  6009. lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
  6010. {
  6011. uint32_t eqidx;
  6012. struct lpfc_queue *fpeq = NULL;
  6013. struct lpfc_eqe *eqe;
  6014. bool mbox_pending;
  6015. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6016. return false;
  6017. /* Find the eq associated with the mcq */
  6018. if (phba->sli4_hba.hba_eq)
  6019. for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
  6020. if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
  6021. phba->sli4_hba.mbx_cq->assoc_qid) {
  6022. fpeq = phba->sli4_hba.hba_eq[eqidx];
  6023. break;
  6024. }
  6025. if (!fpeq)
  6026. return false;
  6027. /* Turn off interrupts from this EQ */
  6028. lpfc_sli4_eq_clr_intr(fpeq);
  6029. /* Check to see if a mbox completion is pending */
  6030. mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
  6031. /*
  6032. * If a mbox completion is pending, process all the events on EQ
  6033. * associated with the mbox completion queue (this could include
  6034. * mailbox commands, async events, els commands, receive queue data
  6035. * and fcp commands)
  6036. */
  6037. if (mbox_pending)
  6038. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  6039. lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
  6040. fpeq->EQ_processed++;
  6041. }
  6042. /* Always clear and re-arm the EQ */
  6043. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  6044. return mbox_pending;
  6045. }
  6046. /**
  6047. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  6048. * @phba: Pointer to HBA context object.
  6049. *
  6050. * This function is called from worker thread when a mailbox command times out.
  6051. * The caller is not required to hold any locks. This function will reset the
  6052. * HBA and recover all the pending commands.
  6053. **/
  6054. void
  6055. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  6056. {
  6057. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  6058. MAILBOX_t *mb = &pmbox->u.mb;
  6059. struct lpfc_sli *psli = &phba->sli;
  6060. struct lpfc_sli_ring *pring;
  6061. /* If the mailbox completed, process the completion and return */
  6062. if (lpfc_sli4_process_missed_mbox_completions(phba))
  6063. return;
  6064. /* Check the pmbox pointer first. There is a race condition
  6065. * between the mbox timeout handler getting executed in the
  6066. * worklist and the mailbox actually completing. When this
  6067. * race condition occurs, the mbox_active will be NULL.
  6068. */
  6069. spin_lock_irq(&phba->hbalock);
  6070. if (pmbox == NULL) {
  6071. lpfc_printf_log(phba, KERN_WARNING,
  6072. LOG_MBOX | LOG_SLI,
  6073. "0353 Active Mailbox cleared - mailbox timeout "
  6074. "exiting\n");
  6075. spin_unlock_irq(&phba->hbalock);
  6076. return;
  6077. }
  6078. /* Mbox cmd <mbxCommand> timeout */
  6079. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6080. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  6081. mb->mbxCommand,
  6082. phba->pport->port_state,
  6083. phba->sli.sli_flag,
  6084. phba->sli.mbox_active);
  6085. spin_unlock_irq(&phba->hbalock);
  6086. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  6087. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  6088. * it to fail all outstanding SCSI IO.
  6089. */
  6090. spin_lock_irq(&phba->pport->work_port_lock);
  6091. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  6092. spin_unlock_irq(&phba->pport->work_port_lock);
  6093. spin_lock_irq(&phba->hbalock);
  6094. phba->link_state = LPFC_LINK_UNKNOWN;
  6095. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  6096. spin_unlock_irq(&phba->hbalock);
  6097. pring = &psli->ring[psli->fcp_ring];
  6098. lpfc_sli_abort_iocb_ring(phba, pring);
  6099. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6100. "0345 Resetting board due to mailbox timeout\n");
  6101. /* Reset the HBA device */
  6102. lpfc_reset_hba(phba);
  6103. }
  6104. /**
  6105. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  6106. * @phba: Pointer to HBA context object.
  6107. * @pmbox: Pointer to mailbox object.
  6108. * @flag: Flag indicating how the mailbox need to be processed.
  6109. *
  6110. * This function is called by discovery code and HBA management code
  6111. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  6112. * function gets the hbalock to protect the data structures.
  6113. * The mailbox command can be submitted in polling mode, in which case
  6114. * this function will wait in a polling loop for the completion of the
  6115. * mailbox.
  6116. * If the mailbox is submitted in no_wait mode (not polling) the
  6117. * function will submit the command and returns immediately without waiting
  6118. * for the mailbox completion. The no_wait is supported only when HBA
  6119. * is in SLI2/SLI3 mode - interrupts are enabled.
  6120. * The SLI interface allows only one mailbox pending at a time. If the
  6121. * mailbox is issued in polling mode and there is already a mailbox
  6122. * pending, then the function will return an error. If the mailbox is issued
  6123. * in NO_WAIT mode and there is a mailbox pending already, the function
  6124. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  6125. * The sli layer owns the mailbox object until the completion of mailbox
  6126. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  6127. * return codes the caller owns the mailbox command after the return of
  6128. * the function.
  6129. **/
  6130. static int
  6131. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  6132. uint32_t flag)
  6133. {
  6134. MAILBOX_t *mbx;
  6135. struct lpfc_sli *psli = &phba->sli;
  6136. uint32_t status, evtctr;
  6137. uint32_t ha_copy, hc_copy;
  6138. int i;
  6139. unsigned long timeout;
  6140. unsigned long drvr_flag = 0;
  6141. uint32_t word0, ldata;
  6142. void __iomem *to_slim;
  6143. int processing_queue = 0;
  6144. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6145. if (!pmbox) {
  6146. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6147. /* processing mbox queue from intr_handler */
  6148. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6149. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6150. return MBX_SUCCESS;
  6151. }
  6152. processing_queue = 1;
  6153. pmbox = lpfc_mbox_get(phba);
  6154. if (!pmbox) {
  6155. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6156. return MBX_SUCCESS;
  6157. }
  6158. }
  6159. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  6160. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  6161. if(!pmbox->vport) {
  6162. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6163. lpfc_printf_log(phba, KERN_ERR,
  6164. LOG_MBOX | LOG_VPORT,
  6165. "1806 Mbox x%x failed. No vport\n",
  6166. pmbox->u.mb.mbxCommand);
  6167. dump_stack();
  6168. goto out_not_finished;
  6169. }
  6170. }
  6171. /* If the PCI channel is in offline state, do not post mbox. */
  6172. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6173. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6174. goto out_not_finished;
  6175. }
  6176. /* If HBA has a deferred error attention, fail the iocb. */
  6177. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6178. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6179. goto out_not_finished;
  6180. }
  6181. psli = &phba->sli;
  6182. mbx = &pmbox->u.mb;
  6183. status = MBX_SUCCESS;
  6184. if (phba->link_state == LPFC_HBA_ERROR) {
  6185. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6186. /* Mbox command <mbxCommand> cannot issue */
  6187. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6188. "(%d):0311 Mailbox command x%x cannot "
  6189. "issue Data: x%x x%x\n",
  6190. pmbox->vport ? pmbox->vport->vpi : 0,
  6191. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6192. goto out_not_finished;
  6193. }
  6194. if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6195. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6196. !(hc_copy & HC_MBINT_ENA)) {
  6197. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6198. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6199. "(%d):2528 Mailbox command x%x cannot "
  6200. "issue Data: x%x x%x\n",
  6201. pmbox->vport ? pmbox->vport->vpi : 0,
  6202. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6203. goto out_not_finished;
  6204. }
  6205. }
  6206. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6207. /* Polling for a mbox command when another one is already active
  6208. * is not allowed in SLI. Also, the driver must have established
  6209. * SLI2 mode to queue and process multiple mbox commands.
  6210. */
  6211. if (flag & MBX_POLL) {
  6212. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6213. /* Mbox command <mbxCommand> cannot issue */
  6214. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6215. "(%d):2529 Mailbox command x%x "
  6216. "cannot issue Data: x%x x%x\n",
  6217. pmbox->vport ? pmbox->vport->vpi : 0,
  6218. pmbox->u.mb.mbxCommand,
  6219. psli->sli_flag, flag);
  6220. goto out_not_finished;
  6221. }
  6222. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6223. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6224. /* Mbox command <mbxCommand> cannot issue */
  6225. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6226. "(%d):2530 Mailbox command x%x "
  6227. "cannot issue Data: x%x x%x\n",
  6228. pmbox->vport ? pmbox->vport->vpi : 0,
  6229. pmbox->u.mb.mbxCommand,
  6230. psli->sli_flag, flag);
  6231. goto out_not_finished;
  6232. }
  6233. /* Another mailbox command is still being processed, queue this
  6234. * command to be processed later.
  6235. */
  6236. lpfc_mbox_put(phba, pmbox);
  6237. /* Mbox cmd issue - BUSY */
  6238. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6239. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6240. "x%x x%x x%x x%x\n",
  6241. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6242. mbx->mbxCommand, phba->pport->port_state,
  6243. psli->sli_flag, flag);
  6244. psli->slistat.mbox_busy++;
  6245. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6246. if (pmbox->vport) {
  6247. lpfc_debugfs_disc_trc(pmbox->vport,
  6248. LPFC_DISC_TRC_MBOX_VPORT,
  6249. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6250. (uint32_t)mbx->mbxCommand,
  6251. mbx->un.varWords[0], mbx->un.varWords[1]);
  6252. }
  6253. else {
  6254. lpfc_debugfs_disc_trc(phba->pport,
  6255. LPFC_DISC_TRC_MBOX,
  6256. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6257. (uint32_t)mbx->mbxCommand,
  6258. mbx->un.varWords[0], mbx->un.varWords[1]);
  6259. }
  6260. return MBX_BUSY;
  6261. }
  6262. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6263. /* If we are not polling, we MUST be in SLI2 mode */
  6264. if (flag != MBX_POLL) {
  6265. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6266. (mbx->mbxCommand != MBX_KILL_BOARD)) {
  6267. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6268. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6269. /* Mbox command <mbxCommand> cannot issue */
  6270. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6271. "(%d):2531 Mailbox command x%x "
  6272. "cannot issue Data: x%x x%x\n",
  6273. pmbox->vport ? pmbox->vport->vpi : 0,
  6274. pmbox->u.mb.mbxCommand,
  6275. psli->sli_flag, flag);
  6276. goto out_not_finished;
  6277. }
  6278. /* timeout active mbox command */
  6279. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6280. 1000);
  6281. mod_timer(&psli->mbox_tmo, jiffies + timeout);
  6282. }
  6283. /* Mailbox cmd <cmd> issue */
  6284. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6285. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6286. "x%x\n",
  6287. pmbox->vport ? pmbox->vport->vpi : 0,
  6288. mbx->mbxCommand, phba->pport->port_state,
  6289. psli->sli_flag, flag);
  6290. if (mbx->mbxCommand != MBX_HEARTBEAT) {
  6291. if (pmbox->vport) {
  6292. lpfc_debugfs_disc_trc(pmbox->vport,
  6293. LPFC_DISC_TRC_MBOX_VPORT,
  6294. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6295. (uint32_t)mbx->mbxCommand,
  6296. mbx->un.varWords[0], mbx->un.varWords[1]);
  6297. }
  6298. else {
  6299. lpfc_debugfs_disc_trc(phba->pport,
  6300. LPFC_DISC_TRC_MBOX,
  6301. "MBOX Send: cmd:x%x mb:x%x x%x",
  6302. (uint32_t)mbx->mbxCommand,
  6303. mbx->un.varWords[0], mbx->un.varWords[1]);
  6304. }
  6305. }
  6306. psli->slistat.mbox_cmd++;
  6307. evtctr = psli->slistat.mbox_event;
  6308. /* next set own bit for the adapter and copy over command word */
  6309. mbx->mbxOwner = OWN_CHIP;
  6310. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6311. /* Populate mbox extension offset word. */
  6312. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6313. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6314. = (uint8_t *)phba->mbox_ext
  6315. - (uint8_t *)phba->mbox;
  6316. }
  6317. /* Copy the mailbox extension data */
  6318. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6319. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6320. (uint8_t *)phba->mbox_ext,
  6321. pmbox->in_ext_byte_len);
  6322. }
  6323. /* Copy command data to host SLIM area */
  6324. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6325. } else {
  6326. /* Populate mbox extension offset word. */
  6327. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6328. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6329. = MAILBOX_HBA_EXT_OFFSET;
  6330. /* Copy the mailbox extension data */
  6331. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6332. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6333. MAILBOX_HBA_EXT_OFFSET,
  6334. pmbox->context2, pmbox->in_ext_byte_len);
  6335. }
  6336. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6337. /* copy command data into host mbox for cmpl */
  6338. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6339. }
  6340. /* First copy mbox command data to HBA SLIM, skip past first
  6341. word */
  6342. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6343. lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
  6344. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6345. /* Next copy over first word, with mbxOwner set */
  6346. ldata = *((uint32_t *)mbx);
  6347. to_slim = phba->MBslimaddr;
  6348. writel(ldata, to_slim);
  6349. readl(to_slim); /* flush */
  6350. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6351. /* switch over to host mailbox */
  6352. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6353. }
  6354. }
  6355. wmb();
  6356. switch (flag) {
  6357. case MBX_NOWAIT:
  6358. /* Set up reference to mailbox command */
  6359. psli->mbox_active = pmbox;
  6360. /* Interrupt board to do it */
  6361. writel(CA_MBATT, phba->CAregaddr);
  6362. readl(phba->CAregaddr); /* flush */
  6363. /* Don't wait for it to finish, just return */
  6364. break;
  6365. case MBX_POLL:
  6366. /* Set up null reference to mailbox command */
  6367. psli->mbox_active = NULL;
  6368. /* Interrupt board to do it */
  6369. writel(CA_MBATT, phba->CAregaddr);
  6370. readl(phba->CAregaddr); /* flush */
  6371. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6372. /* First read mbox status word */
  6373. word0 = *((uint32_t *)phba->mbox);
  6374. word0 = le32_to_cpu(word0);
  6375. } else {
  6376. /* First read mbox status word */
  6377. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6378. spin_unlock_irqrestore(&phba->hbalock,
  6379. drvr_flag);
  6380. goto out_not_finished;
  6381. }
  6382. }
  6383. /* Read the HBA Host Attention Register */
  6384. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6385. spin_unlock_irqrestore(&phba->hbalock,
  6386. drvr_flag);
  6387. goto out_not_finished;
  6388. }
  6389. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6390. 1000) + jiffies;
  6391. i = 0;
  6392. /* Wait for command to complete */
  6393. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6394. (!(ha_copy & HA_MBATT) &&
  6395. (phba->link_state > LPFC_WARM_START))) {
  6396. if (time_after(jiffies, timeout)) {
  6397. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6398. spin_unlock_irqrestore(&phba->hbalock,
  6399. drvr_flag);
  6400. goto out_not_finished;
  6401. }
  6402. /* Check if we took a mbox interrupt while we were
  6403. polling */
  6404. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6405. && (evtctr != psli->slistat.mbox_event))
  6406. break;
  6407. if (i++ > 10) {
  6408. spin_unlock_irqrestore(&phba->hbalock,
  6409. drvr_flag);
  6410. msleep(1);
  6411. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6412. }
  6413. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6414. /* First copy command data */
  6415. word0 = *((uint32_t *)phba->mbox);
  6416. word0 = le32_to_cpu(word0);
  6417. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6418. MAILBOX_t *slimmb;
  6419. uint32_t slimword0;
  6420. /* Check real SLIM for any errors */
  6421. slimword0 = readl(phba->MBslimaddr);
  6422. slimmb = (MAILBOX_t *) & slimword0;
  6423. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6424. && slimmb->mbxStatus) {
  6425. psli->sli_flag &=
  6426. ~LPFC_SLI_ACTIVE;
  6427. word0 = slimword0;
  6428. }
  6429. }
  6430. } else {
  6431. /* First copy command data */
  6432. word0 = readl(phba->MBslimaddr);
  6433. }
  6434. /* Read the HBA Host Attention Register */
  6435. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6436. spin_unlock_irqrestore(&phba->hbalock,
  6437. drvr_flag);
  6438. goto out_not_finished;
  6439. }
  6440. }
  6441. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6442. /* copy results back to user */
  6443. lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
  6444. /* Copy the mailbox extension data */
  6445. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6446. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6447. pmbox->context2,
  6448. pmbox->out_ext_byte_len);
  6449. }
  6450. } else {
  6451. /* First copy command data */
  6452. lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
  6453. MAILBOX_CMD_SIZE);
  6454. /* Copy the mailbox extension data */
  6455. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6456. lpfc_memcpy_from_slim(pmbox->context2,
  6457. phba->MBslimaddr +
  6458. MAILBOX_HBA_EXT_OFFSET,
  6459. pmbox->out_ext_byte_len);
  6460. }
  6461. }
  6462. writel(HA_MBATT, phba->HAregaddr);
  6463. readl(phba->HAregaddr); /* flush */
  6464. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6465. status = mbx->mbxStatus;
  6466. }
  6467. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6468. return status;
  6469. out_not_finished:
  6470. if (processing_queue) {
  6471. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6472. lpfc_mbox_cmpl_put(phba, pmbox);
  6473. }
  6474. return MBX_NOT_FINISHED;
  6475. }
  6476. /**
  6477. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6478. * @phba: Pointer to HBA context object.
  6479. *
  6480. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6481. * the driver internal pending mailbox queue. It will then try to wait out the
  6482. * possible outstanding mailbox command before return.
  6483. *
  6484. * Returns:
  6485. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6486. * the outstanding mailbox command timed out.
  6487. **/
  6488. static int
  6489. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6490. {
  6491. struct lpfc_sli *psli = &phba->sli;
  6492. int rc = 0;
  6493. unsigned long timeout = 0;
  6494. /* Mark the asynchronous mailbox command posting as blocked */
  6495. spin_lock_irq(&phba->hbalock);
  6496. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6497. /* Determine how long we might wait for the active mailbox
  6498. * command to be gracefully completed by firmware.
  6499. */
  6500. if (phba->sli.mbox_active)
  6501. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6502. phba->sli.mbox_active) *
  6503. 1000) + jiffies;
  6504. spin_unlock_irq(&phba->hbalock);
  6505. /* Make sure the mailbox is really active */
  6506. if (timeout)
  6507. lpfc_sli4_process_missed_mbox_completions(phba);
  6508. /* Wait for the outstnading mailbox command to complete */
  6509. while (phba->sli.mbox_active) {
  6510. /* Check active mailbox complete status every 2ms */
  6511. msleep(2);
  6512. if (time_after(jiffies, timeout)) {
  6513. /* Timeout, marked the outstanding cmd not complete */
  6514. rc = 1;
  6515. break;
  6516. }
  6517. }
  6518. /* Can not cleanly block async mailbox command, fails it */
  6519. if (rc) {
  6520. spin_lock_irq(&phba->hbalock);
  6521. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6522. spin_unlock_irq(&phba->hbalock);
  6523. }
  6524. return rc;
  6525. }
  6526. /**
  6527. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6528. * @phba: Pointer to HBA context object.
  6529. *
  6530. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6531. * commands from the driver internal pending mailbox queue. It makes sure
  6532. * that there is no outstanding mailbox command before resuming posting
  6533. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6534. * mailbox command, it will try to wait it out before resuming asynchronous
  6535. * mailbox command posting.
  6536. **/
  6537. static void
  6538. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6539. {
  6540. struct lpfc_sli *psli = &phba->sli;
  6541. spin_lock_irq(&phba->hbalock);
  6542. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6543. /* Asynchronous mailbox posting is not blocked, do nothing */
  6544. spin_unlock_irq(&phba->hbalock);
  6545. return;
  6546. }
  6547. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6548. * successful or timeout, after timing-out the outstanding mailbox
  6549. * command shall always be removed, so just unblock posting async
  6550. * mailbox command and resume
  6551. */
  6552. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6553. spin_unlock_irq(&phba->hbalock);
  6554. /* wake up worker thread to post asynchronlous mailbox command */
  6555. lpfc_worker_wake_up(phba);
  6556. }
  6557. /**
  6558. * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
  6559. * @phba: Pointer to HBA context object.
  6560. * @mboxq: Pointer to mailbox object.
  6561. *
  6562. * The function waits for the bootstrap mailbox register ready bit from
  6563. * port for twice the regular mailbox command timeout value.
  6564. *
  6565. * 0 - no timeout on waiting for bootstrap mailbox register ready.
  6566. * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
  6567. **/
  6568. static int
  6569. lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6570. {
  6571. uint32_t db_ready;
  6572. unsigned long timeout;
  6573. struct lpfc_register bmbx_reg;
  6574. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6575. * 1000) + jiffies;
  6576. do {
  6577. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6578. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6579. if (!db_ready)
  6580. msleep(2);
  6581. if (time_after(jiffies, timeout))
  6582. return MBXERR_ERROR;
  6583. } while (!db_ready);
  6584. return 0;
  6585. }
  6586. /**
  6587. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6588. * @phba: Pointer to HBA context object.
  6589. * @mboxq: Pointer to mailbox object.
  6590. *
  6591. * The function posts a mailbox to the port. The mailbox is expected
  6592. * to be comletely filled in and ready for the port to operate on it.
  6593. * This routine executes a synchronous completion operation on the
  6594. * mailbox by polling for its completion.
  6595. *
  6596. * The caller must not be holding any locks when calling this routine.
  6597. *
  6598. * Returns:
  6599. * MBX_SUCCESS - mailbox posted successfully
  6600. * Any of the MBX error values.
  6601. **/
  6602. static int
  6603. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6604. {
  6605. int rc = MBX_SUCCESS;
  6606. unsigned long iflag;
  6607. uint32_t mcqe_status;
  6608. uint32_t mbx_cmnd;
  6609. struct lpfc_sli *psli = &phba->sli;
  6610. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6611. struct lpfc_bmbx_create *mbox_rgn;
  6612. struct dma_address *dma_address;
  6613. /*
  6614. * Only one mailbox can be active to the bootstrap mailbox region
  6615. * at a time and there is no queueing provided.
  6616. */
  6617. spin_lock_irqsave(&phba->hbalock, iflag);
  6618. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6619. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6620. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6621. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6622. "cannot issue Data: x%x x%x\n",
  6623. mboxq->vport ? mboxq->vport->vpi : 0,
  6624. mboxq->u.mb.mbxCommand,
  6625. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6626. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6627. psli->sli_flag, MBX_POLL);
  6628. return MBXERR_ERROR;
  6629. }
  6630. /* The server grabs the token and owns it until release */
  6631. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6632. phba->sli.mbox_active = mboxq;
  6633. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6634. /* wait for bootstrap mbox register for readyness */
  6635. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6636. if (rc)
  6637. goto exit;
  6638. /*
  6639. * Initialize the bootstrap memory region to avoid stale data areas
  6640. * in the mailbox post. Then copy the caller's mailbox contents to
  6641. * the bmbx mailbox region.
  6642. */
  6643. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6644. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6645. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6646. sizeof(struct lpfc_mqe));
  6647. /* Post the high mailbox dma address to the port and wait for ready. */
  6648. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6649. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6650. /* wait for bootstrap mbox register for hi-address write done */
  6651. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6652. if (rc)
  6653. goto exit;
  6654. /* Post the low mailbox dma address to the port. */
  6655. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6656. /* wait for bootstrap mbox register for low address write done */
  6657. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6658. if (rc)
  6659. goto exit;
  6660. /*
  6661. * Read the CQ to ensure the mailbox has completed.
  6662. * If so, update the mailbox status so that the upper layers
  6663. * can complete the request normally.
  6664. */
  6665. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6666. sizeof(struct lpfc_mqe));
  6667. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6668. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6669. sizeof(struct lpfc_mcqe));
  6670. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6671. /*
  6672. * When the CQE status indicates a failure and the mailbox status
  6673. * indicates success then copy the CQE status into the mailbox status
  6674. * (and prefix it with x4000).
  6675. */
  6676. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6677. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6678. bf_set(lpfc_mqe_status, mb,
  6679. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6680. rc = MBXERR_ERROR;
  6681. } else
  6682. lpfc_sli4_swap_str(phba, mboxq);
  6683. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6684. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6685. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6686. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6687. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6688. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6689. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6690. bf_get(lpfc_mqe_status, mb),
  6691. mb->un.mb_words[0], mb->un.mb_words[1],
  6692. mb->un.mb_words[2], mb->un.mb_words[3],
  6693. mb->un.mb_words[4], mb->un.mb_words[5],
  6694. mb->un.mb_words[6], mb->un.mb_words[7],
  6695. mb->un.mb_words[8], mb->un.mb_words[9],
  6696. mb->un.mb_words[10], mb->un.mb_words[11],
  6697. mb->un.mb_words[12], mboxq->mcqe.word0,
  6698. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6699. mboxq->mcqe.trailer);
  6700. exit:
  6701. /* We are holding the token, no needed for lock when release */
  6702. spin_lock_irqsave(&phba->hbalock, iflag);
  6703. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6704. phba->sli.mbox_active = NULL;
  6705. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6706. return rc;
  6707. }
  6708. /**
  6709. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6710. * @phba: Pointer to HBA context object.
  6711. * @pmbox: Pointer to mailbox object.
  6712. * @flag: Flag indicating how the mailbox need to be processed.
  6713. *
  6714. * This function is called by discovery code and HBA management code to submit
  6715. * a mailbox command to firmware with SLI-4 interface spec.
  6716. *
  6717. * Return codes the caller owns the mailbox command after the return of the
  6718. * function.
  6719. **/
  6720. static int
  6721. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6722. uint32_t flag)
  6723. {
  6724. struct lpfc_sli *psli = &phba->sli;
  6725. unsigned long iflags;
  6726. int rc;
  6727. /* dump from issue mailbox command if setup */
  6728. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6729. rc = lpfc_mbox_dev_check(phba);
  6730. if (unlikely(rc)) {
  6731. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6732. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6733. "cannot issue Data: x%x x%x\n",
  6734. mboxq->vport ? mboxq->vport->vpi : 0,
  6735. mboxq->u.mb.mbxCommand,
  6736. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6737. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6738. psli->sli_flag, flag);
  6739. goto out_not_finished;
  6740. }
  6741. /* Detect polling mode and jump to a handler */
  6742. if (!phba->sli4_hba.intr_enable) {
  6743. if (flag == MBX_POLL)
  6744. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6745. else
  6746. rc = -EIO;
  6747. if (rc != MBX_SUCCESS)
  6748. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6749. "(%d):2541 Mailbox command x%x "
  6750. "(x%x/x%x) failure: "
  6751. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6752. "Data: x%x x%x\n,",
  6753. mboxq->vport ? mboxq->vport->vpi : 0,
  6754. mboxq->u.mb.mbxCommand,
  6755. lpfc_sli_config_mbox_subsys_get(phba,
  6756. mboxq),
  6757. lpfc_sli_config_mbox_opcode_get(phba,
  6758. mboxq),
  6759. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6760. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6761. bf_get(lpfc_mcqe_ext_status,
  6762. &mboxq->mcqe),
  6763. psli->sli_flag, flag);
  6764. return rc;
  6765. } else if (flag == MBX_POLL) {
  6766. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6767. "(%d):2542 Try to issue mailbox command "
  6768. "x%x (x%x/x%x) synchronously ahead of async"
  6769. "mailbox command queue: x%x x%x\n",
  6770. mboxq->vport ? mboxq->vport->vpi : 0,
  6771. mboxq->u.mb.mbxCommand,
  6772. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6773. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6774. psli->sli_flag, flag);
  6775. /* Try to block the asynchronous mailbox posting */
  6776. rc = lpfc_sli4_async_mbox_block(phba);
  6777. if (!rc) {
  6778. /* Successfully blocked, now issue sync mbox cmd */
  6779. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6780. if (rc != MBX_SUCCESS)
  6781. lpfc_printf_log(phba, KERN_WARNING,
  6782. LOG_MBOX | LOG_SLI,
  6783. "(%d):2597 Sync Mailbox command "
  6784. "x%x (x%x/x%x) failure: "
  6785. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6786. "Data: x%x x%x\n,",
  6787. mboxq->vport ? mboxq->vport->vpi : 0,
  6788. mboxq->u.mb.mbxCommand,
  6789. lpfc_sli_config_mbox_subsys_get(phba,
  6790. mboxq),
  6791. lpfc_sli_config_mbox_opcode_get(phba,
  6792. mboxq),
  6793. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6794. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6795. bf_get(lpfc_mcqe_ext_status,
  6796. &mboxq->mcqe),
  6797. psli->sli_flag, flag);
  6798. /* Unblock the async mailbox posting afterward */
  6799. lpfc_sli4_async_mbox_unblock(phba);
  6800. }
  6801. return rc;
  6802. }
  6803. /* Now, interrupt mode asynchrous mailbox command */
  6804. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6805. if (rc) {
  6806. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6807. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6808. "cannot issue Data: x%x x%x\n",
  6809. mboxq->vport ? mboxq->vport->vpi : 0,
  6810. mboxq->u.mb.mbxCommand,
  6811. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6812. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6813. psli->sli_flag, flag);
  6814. goto out_not_finished;
  6815. }
  6816. /* Put the mailbox command to the driver internal FIFO */
  6817. psli->slistat.mbox_busy++;
  6818. spin_lock_irqsave(&phba->hbalock, iflags);
  6819. lpfc_mbox_put(phba, mboxq);
  6820. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6821. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6822. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6823. "x%x (x%x/x%x) x%x x%x x%x\n",
  6824. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6825. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6826. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6827. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6828. phba->pport->port_state,
  6829. psli->sli_flag, MBX_NOWAIT);
  6830. /* Wake up worker thread to transport mailbox command from head */
  6831. lpfc_worker_wake_up(phba);
  6832. return MBX_BUSY;
  6833. out_not_finished:
  6834. return MBX_NOT_FINISHED;
  6835. }
  6836. /**
  6837. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6838. * @phba: Pointer to HBA context object.
  6839. *
  6840. * This function is called by worker thread to send a mailbox command to
  6841. * SLI4 HBA firmware.
  6842. *
  6843. **/
  6844. int
  6845. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6846. {
  6847. struct lpfc_sli *psli = &phba->sli;
  6848. LPFC_MBOXQ_t *mboxq;
  6849. int rc = MBX_SUCCESS;
  6850. unsigned long iflags;
  6851. struct lpfc_mqe *mqe;
  6852. uint32_t mbx_cmnd;
  6853. /* Check interrupt mode before post async mailbox command */
  6854. if (unlikely(!phba->sli4_hba.intr_enable))
  6855. return MBX_NOT_FINISHED;
  6856. /* Check for mailbox command service token */
  6857. spin_lock_irqsave(&phba->hbalock, iflags);
  6858. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6859. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6860. return MBX_NOT_FINISHED;
  6861. }
  6862. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6863. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6864. return MBX_NOT_FINISHED;
  6865. }
  6866. if (unlikely(phba->sli.mbox_active)) {
  6867. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6868. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6869. "0384 There is pending active mailbox cmd\n");
  6870. return MBX_NOT_FINISHED;
  6871. }
  6872. /* Take the mailbox command service token */
  6873. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6874. /* Get the next mailbox command from head of queue */
  6875. mboxq = lpfc_mbox_get(phba);
  6876. /* If no more mailbox command waiting for post, we're done */
  6877. if (!mboxq) {
  6878. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6879. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6880. return MBX_SUCCESS;
  6881. }
  6882. phba->sli.mbox_active = mboxq;
  6883. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6884. /* Check device readiness for posting mailbox command */
  6885. rc = lpfc_mbox_dev_check(phba);
  6886. if (unlikely(rc))
  6887. /* Driver clean routine will clean up pending mailbox */
  6888. goto out_not_finished;
  6889. /* Prepare the mbox command to be posted */
  6890. mqe = &mboxq->u.mqe;
  6891. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6892. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6893. mod_timer(&psli->mbox_tmo, (jiffies +
  6894. msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
  6895. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6896. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6897. "x%x x%x\n",
  6898. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6899. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6900. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6901. phba->pport->port_state, psli->sli_flag);
  6902. if (mbx_cmnd != MBX_HEARTBEAT) {
  6903. if (mboxq->vport) {
  6904. lpfc_debugfs_disc_trc(mboxq->vport,
  6905. LPFC_DISC_TRC_MBOX_VPORT,
  6906. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6907. mbx_cmnd, mqe->un.mb_words[0],
  6908. mqe->un.mb_words[1]);
  6909. } else {
  6910. lpfc_debugfs_disc_trc(phba->pport,
  6911. LPFC_DISC_TRC_MBOX,
  6912. "MBOX Send: cmd:x%x mb:x%x x%x",
  6913. mbx_cmnd, mqe->un.mb_words[0],
  6914. mqe->un.mb_words[1]);
  6915. }
  6916. }
  6917. psli->slistat.mbox_cmd++;
  6918. /* Post the mailbox command to the port */
  6919. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6920. if (rc != MBX_SUCCESS) {
  6921. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6922. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6923. "cannot issue Data: x%x x%x\n",
  6924. mboxq->vport ? mboxq->vport->vpi : 0,
  6925. mboxq->u.mb.mbxCommand,
  6926. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6927. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6928. psli->sli_flag, MBX_NOWAIT);
  6929. goto out_not_finished;
  6930. }
  6931. return rc;
  6932. out_not_finished:
  6933. spin_lock_irqsave(&phba->hbalock, iflags);
  6934. if (phba->sli.mbox_active) {
  6935. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6936. __lpfc_mbox_cmpl_put(phba, mboxq);
  6937. /* Release the token */
  6938. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6939. phba->sli.mbox_active = NULL;
  6940. }
  6941. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6942. return MBX_NOT_FINISHED;
  6943. }
  6944. /**
  6945. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6946. * @phba: Pointer to HBA context object.
  6947. * @pmbox: Pointer to mailbox object.
  6948. * @flag: Flag indicating how the mailbox need to be processed.
  6949. *
  6950. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6951. * the API jump table function pointer from the lpfc_hba struct.
  6952. *
  6953. * Return codes the caller owns the mailbox command after the return of the
  6954. * function.
  6955. **/
  6956. int
  6957. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6958. {
  6959. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6960. }
  6961. /**
  6962. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6963. * @phba: The hba struct for which this call is being executed.
  6964. * @dev_grp: The HBA PCI-Device group number.
  6965. *
  6966. * This routine sets up the mbox interface API function jump table in @phba
  6967. * struct.
  6968. * Returns: 0 - success, -ENODEV - failure.
  6969. **/
  6970. int
  6971. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6972. {
  6973. switch (dev_grp) {
  6974. case LPFC_PCI_DEV_LP:
  6975. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6976. phba->lpfc_sli_handle_slow_ring_event =
  6977. lpfc_sli_handle_slow_ring_event_s3;
  6978. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6979. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6980. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6981. break;
  6982. case LPFC_PCI_DEV_OC:
  6983. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6984. phba->lpfc_sli_handle_slow_ring_event =
  6985. lpfc_sli_handle_slow_ring_event_s4;
  6986. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6987. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6988. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6989. break;
  6990. default:
  6991. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6992. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6993. dev_grp);
  6994. return -ENODEV;
  6995. break;
  6996. }
  6997. return 0;
  6998. }
  6999. /**
  7000. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  7001. * @phba: Pointer to HBA context object.
  7002. * @pring: Pointer to driver SLI ring object.
  7003. * @piocb: Pointer to address of newly added command iocb.
  7004. *
  7005. * This function is called with hbalock held to add a command
  7006. * iocb to the txq when SLI layer cannot submit the command iocb
  7007. * to the ring.
  7008. **/
  7009. void
  7010. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7011. struct lpfc_iocbq *piocb)
  7012. {
  7013. /* Insert the caller's iocb in the txq tail for later processing. */
  7014. list_add_tail(&piocb->list, &pring->txq);
  7015. }
  7016. /**
  7017. * lpfc_sli_next_iocb - Get the next iocb in the txq
  7018. * @phba: Pointer to HBA context object.
  7019. * @pring: Pointer to driver SLI ring object.
  7020. * @piocb: Pointer to address of newly added command iocb.
  7021. *
  7022. * This function is called with hbalock held before a new
  7023. * iocb is submitted to the firmware. This function checks
  7024. * txq to flush the iocbs in txq to Firmware before
  7025. * submitting new iocbs to the Firmware.
  7026. * If there are iocbs in the txq which need to be submitted
  7027. * to firmware, lpfc_sli_next_iocb returns the first element
  7028. * of the txq after dequeuing it from txq.
  7029. * If there is no iocb in the txq then the function will return
  7030. * *piocb and *piocb is set to NULL. Caller needs to check
  7031. * *piocb to find if there are more commands in the txq.
  7032. **/
  7033. static struct lpfc_iocbq *
  7034. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7035. struct lpfc_iocbq **piocb)
  7036. {
  7037. struct lpfc_iocbq * nextiocb;
  7038. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  7039. if (!nextiocb) {
  7040. nextiocb = *piocb;
  7041. *piocb = NULL;
  7042. }
  7043. return nextiocb;
  7044. }
  7045. /**
  7046. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  7047. * @phba: Pointer to HBA context object.
  7048. * @ring_number: SLI ring number to issue iocb on.
  7049. * @piocb: Pointer to command iocb.
  7050. * @flag: Flag indicating if this command can be put into txq.
  7051. *
  7052. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  7053. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  7054. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  7055. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  7056. * this function allows only iocbs for posting buffers. This function finds
  7057. * next available slot in the command ring and posts the command to the
  7058. * available slot and writes the port attention register to request HBA start
  7059. * processing new iocb. If there is no slot available in the ring and
  7060. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  7061. * the function returns IOCB_BUSY.
  7062. *
  7063. * This function is called with hbalock held. The function will return success
  7064. * after it successfully submit the iocb to firmware or after adding to the
  7065. * txq.
  7066. **/
  7067. static int
  7068. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  7069. struct lpfc_iocbq *piocb, uint32_t flag)
  7070. {
  7071. struct lpfc_iocbq *nextiocb;
  7072. IOCB_t *iocb;
  7073. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7074. if (piocb->iocb_cmpl && (!piocb->vport) &&
  7075. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  7076. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  7077. lpfc_printf_log(phba, KERN_ERR,
  7078. LOG_SLI | LOG_VPORT,
  7079. "1807 IOCB x%x failed. No vport\n",
  7080. piocb->iocb.ulpCommand);
  7081. dump_stack();
  7082. return IOCB_ERROR;
  7083. }
  7084. /* If the PCI channel is in offline state, do not post iocbs. */
  7085. if (unlikely(pci_channel_offline(phba->pcidev)))
  7086. return IOCB_ERROR;
  7087. /* If HBA has a deferred error attention, fail the iocb. */
  7088. if (unlikely(phba->hba_flag & DEFER_ERATT))
  7089. return IOCB_ERROR;
  7090. /*
  7091. * We should never get an IOCB if we are in a < LINK_DOWN state
  7092. */
  7093. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  7094. return IOCB_ERROR;
  7095. /*
  7096. * Check to see if we are blocking IOCB processing because of a
  7097. * outstanding event.
  7098. */
  7099. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  7100. goto iocb_busy;
  7101. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  7102. /*
  7103. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  7104. * can be issued if the link is not up.
  7105. */
  7106. switch (piocb->iocb.ulpCommand) {
  7107. case CMD_GEN_REQUEST64_CR:
  7108. case CMD_GEN_REQUEST64_CX:
  7109. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  7110. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  7111. FC_RCTL_DD_UNSOL_CMD) ||
  7112. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  7113. MENLO_TRANSPORT_TYPE))
  7114. goto iocb_busy;
  7115. break;
  7116. case CMD_QUE_RING_BUF_CN:
  7117. case CMD_QUE_RING_BUF64_CN:
  7118. /*
  7119. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  7120. * completion, iocb_cmpl MUST be 0.
  7121. */
  7122. if (piocb->iocb_cmpl)
  7123. piocb->iocb_cmpl = NULL;
  7124. /*FALLTHROUGH*/
  7125. case CMD_CREATE_XRI_CR:
  7126. case CMD_CLOSE_XRI_CN:
  7127. case CMD_CLOSE_XRI_CX:
  7128. break;
  7129. default:
  7130. goto iocb_busy;
  7131. }
  7132. /*
  7133. * For FCP commands, we must be in a state where we can process link
  7134. * attention events.
  7135. */
  7136. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  7137. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  7138. goto iocb_busy;
  7139. }
  7140. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  7141. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  7142. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  7143. if (iocb)
  7144. lpfc_sli_update_ring(phba, pring);
  7145. else
  7146. lpfc_sli_update_full_ring(phba, pring);
  7147. if (!piocb)
  7148. return IOCB_SUCCESS;
  7149. goto out_busy;
  7150. iocb_busy:
  7151. pring->stats.iocb_cmd_delay++;
  7152. out_busy:
  7153. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7154. __lpfc_sli_ringtx_put(phba, pring, piocb);
  7155. return IOCB_SUCCESS;
  7156. }
  7157. return IOCB_BUSY;
  7158. }
  7159. /**
  7160. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  7161. * @phba: Pointer to HBA context object.
  7162. * @piocb: Pointer to command iocb.
  7163. * @sglq: Pointer to the scatter gather queue object.
  7164. *
  7165. * This routine converts the bpl or bde that is in the IOCB
  7166. * to a sgl list for the sli4 hardware. The physical address
  7167. * of the bpl/bde is converted back to a virtual address.
  7168. * If the IOCB contains a BPL then the list of BDE's is
  7169. * converted to sli4_sge's. If the IOCB contains a single
  7170. * BDE then it is converted to a single sli_sge.
  7171. * The IOCB is still in cpu endianess so the contents of
  7172. * the bpl can be used without byte swapping.
  7173. *
  7174. * Returns valid XRI = Success, NO_XRI = Failure.
  7175. **/
  7176. static uint16_t
  7177. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  7178. struct lpfc_sglq *sglq)
  7179. {
  7180. uint16_t xritag = NO_XRI;
  7181. struct ulp_bde64 *bpl = NULL;
  7182. struct ulp_bde64 bde;
  7183. struct sli4_sge *sgl = NULL;
  7184. struct lpfc_dmabuf *dmabuf;
  7185. IOCB_t *icmd;
  7186. int numBdes = 0;
  7187. int i = 0;
  7188. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7189. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7190. if (!piocbq || !sglq)
  7191. return xritag;
  7192. sgl = (struct sli4_sge *)sglq->sgl;
  7193. icmd = &piocbq->iocb;
  7194. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7195. return sglq->sli4_xritag;
  7196. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7197. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7198. sizeof(struct ulp_bde64);
  7199. /* The addrHigh and addrLow fields within the IOCB
  7200. * have not been byteswapped yet so there is no
  7201. * need to swap them back.
  7202. */
  7203. if (piocbq->context3)
  7204. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7205. else
  7206. return xritag;
  7207. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7208. if (!bpl)
  7209. return xritag;
  7210. for (i = 0; i < numBdes; i++) {
  7211. /* Should already be byte swapped. */
  7212. sgl->addr_hi = bpl->addrHigh;
  7213. sgl->addr_lo = bpl->addrLow;
  7214. sgl->word2 = le32_to_cpu(sgl->word2);
  7215. if ((i+1) == numBdes)
  7216. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7217. else
  7218. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7219. /* swap the size field back to the cpu so we
  7220. * can assign it to the sgl.
  7221. */
  7222. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7223. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7224. /* The offsets in the sgl need to be accumulated
  7225. * separately for the request and reply lists.
  7226. * The request is always first, the reply follows.
  7227. */
  7228. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7229. /* add up the reply sg entries */
  7230. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7231. inbound++;
  7232. /* first inbound? reset the offset */
  7233. if (inbound == 1)
  7234. offset = 0;
  7235. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7236. bf_set(lpfc_sli4_sge_type, sgl,
  7237. LPFC_SGE_TYPE_DATA);
  7238. offset += bde.tus.f.bdeSize;
  7239. }
  7240. sgl->word2 = cpu_to_le32(sgl->word2);
  7241. bpl++;
  7242. sgl++;
  7243. }
  7244. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7245. /* The addrHigh and addrLow fields of the BDE have not
  7246. * been byteswapped yet so they need to be swapped
  7247. * before putting them in the sgl.
  7248. */
  7249. sgl->addr_hi =
  7250. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7251. sgl->addr_lo =
  7252. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7253. sgl->word2 = le32_to_cpu(sgl->word2);
  7254. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7255. sgl->word2 = cpu_to_le32(sgl->word2);
  7256. sgl->sge_len =
  7257. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7258. }
  7259. return sglq->sli4_xritag;
  7260. }
  7261. /**
  7262. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7263. * @phba: Pointer to HBA context object.
  7264. *
  7265. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7266. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7267. * held.
  7268. *
  7269. * Return: index into SLI4 fast-path FCP queue index.
  7270. **/
  7271. static inline uint32_t
  7272. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7273. {
  7274. struct lpfc_vector_map_info *cpup;
  7275. int chann, cpu;
  7276. if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU) {
  7277. cpu = smp_processor_id();
  7278. if (cpu < phba->sli4_hba.num_present_cpu) {
  7279. cpup = phba->sli4_hba.cpu_map;
  7280. cpup += cpu;
  7281. return cpup->channel_id;
  7282. }
  7283. chann = cpu;
  7284. }
  7285. chann = atomic_add_return(1, &phba->fcp_qidx);
  7286. chann = (chann % phba->cfg_fcp_io_channel);
  7287. return chann;
  7288. }
  7289. /**
  7290. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7291. * @phba: Pointer to HBA context object.
  7292. * @piocb: Pointer to command iocb.
  7293. * @wqe: Pointer to the work queue entry.
  7294. *
  7295. * This routine converts the iocb command to its Work Queue Entry
  7296. * equivalent. The wqe pointer should not have any fields set when
  7297. * this routine is called because it will memcpy over them.
  7298. * This routine does not set the CQ_ID or the WQEC bits in the
  7299. * wqe.
  7300. *
  7301. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7302. **/
  7303. static int
  7304. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7305. union lpfc_wqe *wqe)
  7306. {
  7307. uint32_t xmit_len = 0, total_len = 0;
  7308. uint8_t ct = 0;
  7309. uint32_t fip;
  7310. uint32_t abort_tag;
  7311. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7312. uint8_t cmnd;
  7313. uint16_t xritag;
  7314. uint16_t abrt_iotag;
  7315. struct lpfc_iocbq *abrtiocbq;
  7316. struct ulp_bde64 *bpl = NULL;
  7317. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7318. int numBdes, i;
  7319. struct ulp_bde64 bde;
  7320. struct lpfc_nodelist *ndlp;
  7321. uint32_t *pcmd;
  7322. uint32_t if_type;
  7323. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7324. /* The fcp commands will set command type */
  7325. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7326. command_type = FCP_COMMAND;
  7327. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7328. command_type = ELS_COMMAND_FIP;
  7329. else
  7330. command_type = ELS_COMMAND_NON_FIP;
  7331. /* Some of the fields are in the right position already */
  7332. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7333. abort_tag = (uint32_t) iocbq->iotag;
  7334. xritag = iocbq->sli4_xritag;
  7335. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7336. /* words0-2 bpl convert bde */
  7337. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7338. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7339. sizeof(struct ulp_bde64);
  7340. bpl = (struct ulp_bde64 *)
  7341. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7342. if (!bpl)
  7343. return IOCB_ERROR;
  7344. /* Should already be byte swapped. */
  7345. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7346. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7347. /* swap the size field back to the cpu so we
  7348. * can assign it to the sgl.
  7349. */
  7350. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7351. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7352. total_len = 0;
  7353. for (i = 0; i < numBdes; i++) {
  7354. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7355. total_len += bde.tus.f.bdeSize;
  7356. }
  7357. } else
  7358. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7359. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7360. cmnd = iocbq->iocb.ulpCommand;
  7361. switch (iocbq->iocb.ulpCommand) {
  7362. case CMD_ELS_REQUEST64_CR:
  7363. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7364. ndlp = iocbq->context_un.ndlp;
  7365. else
  7366. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7367. if (!iocbq->iocb.ulpLe) {
  7368. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7369. "2007 Only Limited Edition cmd Format"
  7370. " supported 0x%x\n",
  7371. iocbq->iocb.ulpCommand);
  7372. return IOCB_ERROR;
  7373. }
  7374. wqe->els_req.payload_len = xmit_len;
  7375. /* Els_reguest64 has a TMO */
  7376. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7377. iocbq->iocb.ulpTimeout);
  7378. /* Need a VF for word 4 set the vf bit*/
  7379. bf_set(els_req64_vf, &wqe->els_req, 0);
  7380. /* And a VFID for word 12 */
  7381. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7382. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7383. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7384. iocbq->iocb.ulpContext);
  7385. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7386. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7387. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7388. if (command_type == ELS_COMMAND_FIP)
  7389. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7390. >> LPFC_FIP_ELS_ID_SHIFT);
  7391. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7392. iocbq->context2)->virt);
  7393. if_type = bf_get(lpfc_sli_intf_if_type,
  7394. &phba->sli4_hba.sli_intf);
  7395. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7396. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7397. *pcmd == ELS_CMD_SCR ||
  7398. *pcmd == ELS_CMD_FDISC ||
  7399. *pcmd == ELS_CMD_LOGO ||
  7400. *pcmd == ELS_CMD_PLOGI)) {
  7401. bf_set(els_req64_sp, &wqe->els_req, 1);
  7402. bf_set(els_req64_sid, &wqe->els_req,
  7403. iocbq->vport->fc_myDID);
  7404. if ((*pcmd == ELS_CMD_FLOGI) &&
  7405. !(phba->fc_topology ==
  7406. LPFC_TOPOLOGY_LOOP))
  7407. bf_set(els_req64_sid, &wqe->els_req, 0);
  7408. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7409. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7410. phba->vpi_ids[iocbq->vport->vpi]);
  7411. } else if (pcmd && iocbq->context1) {
  7412. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7413. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7414. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7415. }
  7416. }
  7417. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7418. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7419. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7420. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7421. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7422. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7423. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7424. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7425. wqe->els_req.max_response_payload_len = total_len - xmit_len;
  7426. break;
  7427. case CMD_XMIT_SEQUENCE64_CX:
  7428. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7429. iocbq->iocb.un.ulpWord[3]);
  7430. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7431. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7432. /* The entire sequence is transmitted for this IOCB */
  7433. xmit_len = total_len;
  7434. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7435. if (phba->link_flag & LS_LOOPBACK_MODE)
  7436. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7437. case CMD_XMIT_SEQUENCE64_CR:
  7438. /* word3 iocb=io_tag32 wqe=reserved */
  7439. wqe->xmit_sequence.rsvd3 = 0;
  7440. /* word4 relative_offset memcpy */
  7441. /* word5 r_ctl/df_ctl memcpy */
  7442. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7443. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7444. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7445. LPFC_WQE_IOD_WRITE);
  7446. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7447. LPFC_WQE_LENLOC_WORD12);
  7448. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7449. wqe->xmit_sequence.xmit_len = xmit_len;
  7450. command_type = OTHER_COMMAND;
  7451. break;
  7452. case CMD_XMIT_BCAST64_CN:
  7453. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7454. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7455. /* word4 iocb=rsvd wqe=rsvd */
  7456. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7457. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7458. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7459. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7460. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7461. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7462. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7463. LPFC_WQE_LENLOC_WORD3);
  7464. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7465. break;
  7466. case CMD_FCP_IWRITE64_CR:
  7467. command_type = FCP_COMMAND_DATA_OUT;
  7468. /* word3 iocb=iotag wqe=payload_offset_len */
  7469. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7470. bf_set(payload_offset_len, &wqe->fcp_iwrite,
  7471. xmit_len + sizeof(struct fcp_rsp));
  7472. bf_set(cmd_buff_len, &wqe->fcp_iwrite,
  7473. 0);
  7474. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7475. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7476. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7477. iocbq->iocb.ulpFCP2Rcvy);
  7478. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7479. /* Always open the exchange */
  7480. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7481. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7482. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7483. LPFC_WQE_LENLOC_WORD4);
  7484. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7485. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7486. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7487. break;
  7488. case CMD_FCP_IREAD64_CR:
  7489. /* word3 iocb=iotag wqe=payload_offset_len */
  7490. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7491. bf_set(payload_offset_len, &wqe->fcp_iread,
  7492. xmit_len + sizeof(struct fcp_rsp));
  7493. bf_set(cmd_buff_len, &wqe->fcp_iread,
  7494. 0);
  7495. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7496. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7497. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7498. iocbq->iocb.ulpFCP2Rcvy);
  7499. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7500. /* Always open the exchange */
  7501. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7502. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7503. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7504. LPFC_WQE_LENLOC_WORD4);
  7505. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7506. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7507. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7508. break;
  7509. case CMD_FCP_ICMND64_CR:
  7510. /* word3 iocb=iotag wqe=payload_offset_len */
  7511. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7512. bf_set(payload_offset_len, &wqe->fcp_icmd,
  7513. xmit_len + sizeof(struct fcp_rsp));
  7514. bf_set(cmd_buff_len, &wqe->fcp_icmd,
  7515. 0);
  7516. /* word3 iocb=IO_TAG wqe=reserved */
  7517. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7518. /* Always open the exchange */
  7519. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7520. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7521. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7522. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7523. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7524. LPFC_WQE_LENLOC_NONE);
  7525. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7526. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  7527. iocbq->iocb.ulpFCP2Rcvy);
  7528. break;
  7529. case CMD_GEN_REQUEST64_CR:
  7530. /* For this command calculate the xmit length of the
  7531. * request bde.
  7532. */
  7533. xmit_len = 0;
  7534. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7535. sizeof(struct ulp_bde64);
  7536. for (i = 0; i < numBdes; i++) {
  7537. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7538. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7539. break;
  7540. xmit_len += bde.tus.f.bdeSize;
  7541. }
  7542. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7543. wqe->gen_req.request_payload_len = xmit_len;
  7544. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7545. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7546. /* word6 context tag copied in memcpy */
  7547. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7548. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7549. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7550. "2015 Invalid CT %x command 0x%x\n",
  7551. ct, iocbq->iocb.ulpCommand);
  7552. return IOCB_ERROR;
  7553. }
  7554. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7555. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7556. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7557. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7558. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7559. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7560. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7561. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7562. wqe->gen_req.max_response_payload_len = total_len - xmit_len;
  7563. command_type = OTHER_COMMAND;
  7564. break;
  7565. case CMD_XMIT_ELS_RSP64_CX:
  7566. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7567. /* words0-2 BDE memcpy */
  7568. /* word3 iocb=iotag32 wqe=response_payload_len */
  7569. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7570. /* word4 */
  7571. wqe->xmit_els_rsp.word4 = 0;
  7572. /* word5 iocb=rsvd wge=did */
  7573. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7574. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7575. if_type = bf_get(lpfc_sli_intf_if_type,
  7576. &phba->sli4_hba.sli_intf);
  7577. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7578. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7579. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7580. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7581. iocbq->vport->fc_myDID);
  7582. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7583. bf_set(wqe_els_did,
  7584. &wqe->xmit_els_rsp.wqe_dest, 0);
  7585. }
  7586. }
  7587. }
  7588. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7589. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7590. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7591. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7592. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7593. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7594. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7595. phba->vpi_ids[iocbq->vport->vpi]);
  7596. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7597. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7598. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7599. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7600. LPFC_WQE_LENLOC_WORD3);
  7601. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7602. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7603. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7604. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7605. iocbq->context2)->virt);
  7606. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7607. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7608. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7609. iocbq->vport->fc_myDID);
  7610. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7611. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7612. phba->vpi_ids[phba->pport->vpi]);
  7613. }
  7614. command_type = OTHER_COMMAND;
  7615. break;
  7616. case CMD_CLOSE_XRI_CN:
  7617. case CMD_ABORT_XRI_CN:
  7618. case CMD_ABORT_XRI_CX:
  7619. /* words 0-2 memcpy should be 0 rserved */
  7620. /* port will send abts */
  7621. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7622. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7623. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7624. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7625. } else
  7626. fip = 0;
  7627. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7628. /*
  7629. * The link is down, or the command was ELS_FIP
  7630. * so the fw does not need to send abts
  7631. * on the wire.
  7632. */
  7633. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7634. else
  7635. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7636. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7637. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7638. wqe->abort_cmd.rsrvd5 = 0;
  7639. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7640. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7641. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7642. /*
  7643. * The abort handler will send us CMD_ABORT_XRI_CN or
  7644. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7645. */
  7646. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7647. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7648. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7649. LPFC_WQE_LENLOC_NONE);
  7650. cmnd = CMD_ABORT_XRI_CX;
  7651. command_type = OTHER_COMMAND;
  7652. xritag = 0;
  7653. break;
  7654. case CMD_XMIT_BLS_RSP64_CX:
  7655. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7656. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7657. * we re-construct this WQE here based on information in
  7658. * iocbq from scratch.
  7659. */
  7660. memset(wqe, 0, sizeof(union lpfc_wqe));
  7661. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7662. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7663. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7664. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7665. LPFC_ABTS_UNSOL_INT) {
  7666. /* ABTS sent by initiator to CT exchange, the
  7667. * RX_ID field will be filled with the newly
  7668. * allocated responder XRI.
  7669. */
  7670. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7671. iocbq->sli4_xritag);
  7672. } else {
  7673. /* ABTS sent by responder to CT exchange, the
  7674. * RX_ID field will be filled with the responder
  7675. * RX_ID from ABTS.
  7676. */
  7677. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7678. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7679. }
  7680. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7681. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7682. /* Use CT=VPI */
  7683. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7684. ndlp->nlp_DID);
  7685. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7686. iocbq->iocb.ulpContext);
  7687. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7688. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7689. phba->vpi_ids[phba->pport->vpi]);
  7690. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7691. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7692. LPFC_WQE_LENLOC_NONE);
  7693. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7694. command_type = OTHER_COMMAND;
  7695. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7696. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7697. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7698. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7699. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7700. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7701. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7702. }
  7703. break;
  7704. case CMD_XRI_ABORTED_CX:
  7705. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7706. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7707. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7708. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7709. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7710. default:
  7711. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7712. "2014 Invalid command 0x%x\n",
  7713. iocbq->iocb.ulpCommand);
  7714. return IOCB_ERROR;
  7715. break;
  7716. }
  7717. if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
  7718. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  7719. else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
  7720. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  7721. else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
  7722. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  7723. iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
  7724. LPFC_IO_DIF_INSERT);
  7725. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7726. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7727. wqe->generic.wqe_com.abort_tag = abort_tag;
  7728. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7729. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7730. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7731. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7732. return 0;
  7733. }
  7734. /**
  7735. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7736. * @phba: Pointer to HBA context object.
  7737. * @ring_number: SLI ring number to issue iocb on.
  7738. * @piocb: Pointer to command iocb.
  7739. * @flag: Flag indicating if this command can be put into txq.
  7740. *
  7741. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7742. * an iocb command to an HBA with SLI-4 interface spec.
  7743. *
  7744. * This function is called with hbalock held. The function will return success
  7745. * after it successfully submit the iocb to firmware or after adding to the
  7746. * txq.
  7747. **/
  7748. static int
  7749. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7750. struct lpfc_iocbq *piocb, uint32_t flag)
  7751. {
  7752. struct lpfc_sglq *sglq;
  7753. union lpfc_wqe wqe;
  7754. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7755. if (piocb->sli4_xritag == NO_XRI) {
  7756. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7757. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7758. sglq = NULL;
  7759. else {
  7760. if (!list_empty(&pring->txq)) {
  7761. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7762. __lpfc_sli_ringtx_put(phba,
  7763. pring, piocb);
  7764. return IOCB_SUCCESS;
  7765. } else {
  7766. return IOCB_BUSY;
  7767. }
  7768. } else {
  7769. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7770. if (!sglq) {
  7771. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7772. __lpfc_sli_ringtx_put(phba,
  7773. pring,
  7774. piocb);
  7775. return IOCB_SUCCESS;
  7776. } else
  7777. return IOCB_BUSY;
  7778. }
  7779. }
  7780. }
  7781. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7782. /* These IO's already have an XRI and a mapped sgl. */
  7783. sglq = NULL;
  7784. } else {
  7785. /*
  7786. * This is a continuation of a commandi,(CX) so this
  7787. * sglq is on the active list
  7788. */
  7789. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
  7790. if (!sglq)
  7791. return IOCB_ERROR;
  7792. }
  7793. if (sglq) {
  7794. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7795. piocb->sli4_xritag = sglq->sli4_xritag;
  7796. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7797. return IOCB_ERROR;
  7798. }
  7799. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7800. return IOCB_ERROR;
  7801. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7802. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7803. if (unlikely(!phba->sli4_hba.fcp_wq))
  7804. return IOCB_ERROR;
  7805. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7806. &wqe))
  7807. return IOCB_ERROR;
  7808. } else {
  7809. if (unlikely(!phba->sli4_hba.els_wq))
  7810. return IOCB_ERROR;
  7811. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7812. return IOCB_ERROR;
  7813. }
  7814. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7815. return 0;
  7816. }
  7817. /**
  7818. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7819. *
  7820. * This routine wraps the actual lockless version for issusing IOCB function
  7821. * pointer from the lpfc_hba struct.
  7822. *
  7823. * Return codes:
  7824. * IOCB_ERROR - Error
  7825. * IOCB_SUCCESS - Success
  7826. * IOCB_BUSY - Busy
  7827. **/
  7828. int
  7829. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7830. struct lpfc_iocbq *piocb, uint32_t flag)
  7831. {
  7832. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7833. }
  7834. /**
  7835. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7836. * @phba: The hba struct for which this call is being executed.
  7837. * @dev_grp: The HBA PCI-Device group number.
  7838. *
  7839. * This routine sets up the SLI interface API function jump table in @phba
  7840. * struct.
  7841. * Returns: 0 - success, -ENODEV - failure.
  7842. **/
  7843. int
  7844. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7845. {
  7846. switch (dev_grp) {
  7847. case LPFC_PCI_DEV_LP:
  7848. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7849. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7850. break;
  7851. case LPFC_PCI_DEV_OC:
  7852. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7853. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7854. break;
  7855. default:
  7856. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7857. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7858. dev_grp);
  7859. return -ENODEV;
  7860. break;
  7861. }
  7862. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7863. return 0;
  7864. }
  7865. /**
  7866. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7867. * @phba: Pointer to HBA context object.
  7868. * @pring: Pointer to driver SLI ring object.
  7869. * @piocb: Pointer to command iocb.
  7870. * @flag: Flag indicating if this command can be put into txq.
  7871. *
  7872. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7873. * function. This function gets the hbalock and calls
  7874. * __lpfc_sli_issue_iocb function and will return the error returned
  7875. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7876. * functions which do not hold hbalock.
  7877. **/
  7878. int
  7879. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7880. struct lpfc_iocbq *piocb, uint32_t flag)
  7881. {
  7882. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  7883. struct lpfc_sli_ring *pring;
  7884. struct lpfc_queue *fpeq;
  7885. struct lpfc_eqe *eqe;
  7886. unsigned long iflags;
  7887. int rc, idx;
  7888. if (phba->sli_rev == LPFC_SLI_REV4) {
  7889. if (piocb->iocb_flag & LPFC_IO_FCP) {
  7890. if (unlikely(!phba->sli4_hba.fcp_wq))
  7891. return IOCB_ERROR;
  7892. idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7893. piocb->fcp_wqidx = idx;
  7894. ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
  7895. pring = &phba->sli.ring[ring_number];
  7896. spin_lock_irqsave(&pring->ring_lock, iflags);
  7897. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
  7898. flag);
  7899. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7900. if (lpfc_fcp_look_ahead) {
  7901. fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
  7902. if (atomic_dec_and_test(&fcp_eq_hdl->
  7903. fcp_eq_in_use)) {
  7904. /* Get associated EQ with this index */
  7905. fpeq = phba->sli4_hba.hba_eq[idx];
  7906. /* Turn off interrupts from this EQ */
  7907. lpfc_sli4_eq_clr_intr(fpeq);
  7908. /*
  7909. * Process all the events on FCP EQ
  7910. */
  7911. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  7912. lpfc_sli4_hba_handle_eqe(phba,
  7913. eqe, idx);
  7914. fpeq->EQ_processed++;
  7915. }
  7916. /* Always clear and re-arm the EQ */
  7917. lpfc_sli4_eq_release(fpeq,
  7918. LPFC_QUEUE_REARM);
  7919. }
  7920. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  7921. }
  7922. } else {
  7923. pring = &phba->sli.ring[ring_number];
  7924. spin_lock_irqsave(&pring->ring_lock, iflags);
  7925. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
  7926. flag);
  7927. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7928. }
  7929. } else {
  7930. /* For now, SLI2/3 will still use hbalock */
  7931. spin_lock_irqsave(&phba->hbalock, iflags);
  7932. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7933. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7934. }
  7935. return rc;
  7936. }
  7937. /**
  7938. * lpfc_extra_ring_setup - Extra ring setup function
  7939. * @phba: Pointer to HBA context object.
  7940. *
  7941. * This function is called while driver attaches with the
  7942. * HBA to setup the extra ring. The extra ring is used
  7943. * only when driver needs to support target mode functionality
  7944. * or IP over FC functionalities.
  7945. *
  7946. * This function is called with no lock held.
  7947. **/
  7948. static int
  7949. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7950. {
  7951. struct lpfc_sli *psli;
  7952. struct lpfc_sli_ring *pring;
  7953. psli = &phba->sli;
  7954. /* Adjust cmd/rsp ring iocb entries more evenly */
  7955. /* Take some away from the FCP ring */
  7956. pring = &psli->ring[psli->fcp_ring];
  7957. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7958. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7959. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7960. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7961. /* and give them to the extra ring */
  7962. pring = &psli->ring[psli->extra_ring];
  7963. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7964. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7965. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7966. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7967. /* Setup default profile for this ring */
  7968. pring->iotag_max = 4096;
  7969. pring->num_mask = 1;
  7970. pring->prt[0].profile = 0; /* Mask 0 */
  7971. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7972. pring->prt[0].type = phba->cfg_multi_ring_type;
  7973. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7974. return 0;
  7975. }
  7976. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7977. * @phba: Pointer to HBA context object.
  7978. * @iocbq: Pointer to iocb object.
  7979. *
  7980. * The async_event handler calls this routine when it receives
  7981. * an ASYNC_STATUS_CN event from the port. The port generates
  7982. * this event when an Abort Sequence request to an rport fails
  7983. * twice in succession. The abort could be originated by the
  7984. * driver or by the port. The ABTS could have been for an ELS
  7985. * or FCP IO. The port only generates this event when an ABTS
  7986. * fails to complete after one retry.
  7987. */
  7988. static void
  7989. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7990. struct lpfc_iocbq *iocbq)
  7991. {
  7992. struct lpfc_nodelist *ndlp = NULL;
  7993. uint16_t rpi = 0, vpi = 0;
  7994. struct lpfc_vport *vport = NULL;
  7995. /* The rpi in the ulpContext is vport-sensitive. */
  7996. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7997. rpi = iocbq->iocb.ulpContext;
  7998. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7999. "3092 Port generated ABTS async event "
  8000. "on vpi %d rpi %d status 0x%x\n",
  8001. vpi, rpi, iocbq->iocb.ulpStatus);
  8002. vport = lpfc_find_vport_by_vpid(phba, vpi);
  8003. if (!vport)
  8004. goto err_exit;
  8005. ndlp = lpfc_findnode_rpi(vport, rpi);
  8006. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  8007. goto err_exit;
  8008. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  8009. lpfc_sli_abts_recover_port(vport, ndlp);
  8010. return;
  8011. err_exit:
  8012. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8013. "3095 Event Context not found, no "
  8014. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  8015. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  8016. vpi, rpi);
  8017. }
  8018. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  8019. * @phba: pointer to HBA context object.
  8020. * @ndlp: nodelist pointer for the impacted rport.
  8021. * @axri: pointer to the wcqe containing the failed exchange.
  8022. *
  8023. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  8024. * port. The port generates this event when an abort exchange request to an
  8025. * rport fails twice in succession with no reply. The abort could be originated
  8026. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  8027. */
  8028. void
  8029. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  8030. struct lpfc_nodelist *ndlp,
  8031. struct sli4_wcqe_xri_aborted *axri)
  8032. {
  8033. struct lpfc_vport *vport;
  8034. uint32_t ext_status = 0;
  8035. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  8036. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8037. "3115 Node Context not found, driver "
  8038. "ignoring abts err event\n");
  8039. return;
  8040. }
  8041. vport = ndlp->vport;
  8042. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8043. "3116 Port generated FCP XRI ABORT event on "
  8044. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  8045. ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
  8046. bf_get(lpfc_wcqe_xa_xri, axri),
  8047. bf_get(lpfc_wcqe_xa_status, axri),
  8048. axri->parameter);
  8049. /*
  8050. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  8051. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  8052. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  8053. */
  8054. ext_status = axri->parameter & IOERR_PARAM_MASK;
  8055. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  8056. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  8057. lpfc_sli_abts_recover_port(vport, ndlp);
  8058. }
  8059. /**
  8060. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  8061. * @phba: Pointer to HBA context object.
  8062. * @pring: Pointer to driver SLI ring object.
  8063. * @iocbq: Pointer to iocb object.
  8064. *
  8065. * This function is called by the slow ring event handler
  8066. * function when there is an ASYNC event iocb in the ring.
  8067. * This function is called with no lock held.
  8068. * Currently this function handles only temperature related
  8069. * ASYNC events. The function decodes the temperature sensor
  8070. * event message and posts events for the management applications.
  8071. **/
  8072. static void
  8073. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  8074. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  8075. {
  8076. IOCB_t *icmd;
  8077. uint16_t evt_code;
  8078. struct temp_event temp_event_data;
  8079. struct Scsi_Host *shost;
  8080. uint32_t *iocb_w;
  8081. icmd = &iocbq->iocb;
  8082. evt_code = icmd->un.asyncstat.evt_code;
  8083. switch (evt_code) {
  8084. case ASYNC_TEMP_WARN:
  8085. case ASYNC_TEMP_SAFE:
  8086. temp_event_data.data = (uint32_t) icmd->ulpContext;
  8087. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  8088. if (evt_code == ASYNC_TEMP_WARN) {
  8089. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  8090. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8091. "0347 Adapter is very hot, please take "
  8092. "corrective action. temperature : %d Celsius\n",
  8093. (uint32_t) icmd->ulpContext);
  8094. } else {
  8095. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  8096. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  8097. "0340 Adapter temperature is OK now. "
  8098. "temperature : %d Celsius\n",
  8099. (uint32_t) icmd->ulpContext);
  8100. }
  8101. /* Send temperature change event to applications */
  8102. shost = lpfc_shost_from_vport(phba->pport);
  8103. fc_host_post_vendor_event(shost, fc_get_event_number(),
  8104. sizeof(temp_event_data), (char *) &temp_event_data,
  8105. LPFC_NL_VENDOR_ID);
  8106. break;
  8107. case ASYNC_STATUS_CN:
  8108. lpfc_sli_abts_err_handler(phba, iocbq);
  8109. break;
  8110. default:
  8111. iocb_w = (uint32_t *) icmd;
  8112. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8113. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  8114. " evt_code 0x%x\n"
  8115. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  8116. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  8117. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  8118. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  8119. pring->ringno, icmd->un.asyncstat.evt_code,
  8120. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  8121. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  8122. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  8123. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  8124. break;
  8125. }
  8126. }
  8127. /**
  8128. * lpfc_sli_setup - SLI ring setup function
  8129. * @phba: Pointer to HBA context object.
  8130. *
  8131. * lpfc_sli_setup sets up rings of the SLI interface with
  8132. * number of iocbs per ring and iotags. This function is
  8133. * called while driver attach to the HBA and before the
  8134. * interrupts are enabled. So there is no need for locking.
  8135. *
  8136. * This function always returns 0.
  8137. **/
  8138. int
  8139. lpfc_sli_setup(struct lpfc_hba *phba)
  8140. {
  8141. int i, totiocbsize = 0;
  8142. struct lpfc_sli *psli = &phba->sli;
  8143. struct lpfc_sli_ring *pring;
  8144. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  8145. if (phba->sli_rev == LPFC_SLI_REV4)
  8146. psli->num_rings += phba->cfg_fcp_io_channel;
  8147. psli->sli_flag = 0;
  8148. psli->fcp_ring = LPFC_FCP_RING;
  8149. psli->next_ring = LPFC_FCP_NEXT_RING;
  8150. psli->extra_ring = LPFC_EXTRA_RING;
  8151. psli->iocbq_lookup = NULL;
  8152. psli->iocbq_lookup_len = 0;
  8153. psli->last_iotag = 0;
  8154. for (i = 0; i < psli->num_rings; i++) {
  8155. pring = &psli->ring[i];
  8156. switch (i) {
  8157. case LPFC_FCP_RING: /* ring 0 - FCP */
  8158. /* numCiocb and numRiocb are used in config_port */
  8159. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  8160. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  8161. pring->sli.sli3.numCiocb +=
  8162. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8163. pring->sli.sli3.numRiocb +=
  8164. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8165. pring->sli.sli3.numCiocb +=
  8166. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8167. pring->sli.sli3.numRiocb +=
  8168. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8169. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8170. SLI3_IOCB_CMD_SIZE :
  8171. SLI2_IOCB_CMD_SIZE;
  8172. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8173. SLI3_IOCB_RSP_SIZE :
  8174. SLI2_IOCB_RSP_SIZE;
  8175. pring->iotag_ctr = 0;
  8176. pring->iotag_max =
  8177. (phba->cfg_hba_queue_depth * 2);
  8178. pring->fast_iotag = pring->iotag_max;
  8179. pring->num_mask = 0;
  8180. break;
  8181. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  8182. /* numCiocb and numRiocb are used in config_port */
  8183. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  8184. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  8185. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8186. SLI3_IOCB_CMD_SIZE :
  8187. SLI2_IOCB_CMD_SIZE;
  8188. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8189. SLI3_IOCB_RSP_SIZE :
  8190. SLI2_IOCB_RSP_SIZE;
  8191. pring->iotag_max = phba->cfg_hba_queue_depth;
  8192. pring->num_mask = 0;
  8193. break;
  8194. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8195. /* numCiocb and numRiocb are used in config_port */
  8196. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8197. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8198. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8199. SLI3_IOCB_CMD_SIZE :
  8200. SLI2_IOCB_CMD_SIZE;
  8201. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8202. SLI3_IOCB_RSP_SIZE :
  8203. SLI2_IOCB_RSP_SIZE;
  8204. pring->fast_iotag = 0;
  8205. pring->iotag_ctr = 0;
  8206. pring->iotag_max = 4096;
  8207. pring->lpfc_sli_rcv_async_status =
  8208. lpfc_sli_async_event_handler;
  8209. pring->num_mask = LPFC_MAX_RING_MASK;
  8210. pring->prt[0].profile = 0; /* Mask 0 */
  8211. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8212. pring->prt[0].type = FC_TYPE_ELS;
  8213. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8214. lpfc_els_unsol_event;
  8215. pring->prt[1].profile = 0; /* Mask 1 */
  8216. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8217. pring->prt[1].type = FC_TYPE_ELS;
  8218. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8219. lpfc_els_unsol_event;
  8220. pring->prt[2].profile = 0; /* Mask 2 */
  8221. /* NameServer Inquiry */
  8222. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8223. /* NameServer */
  8224. pring->prt[2].type = FC_TYPE_CT;
  8225. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8226. lpfc_ct_unsol_event;
  8227. pring->prt[3].profile = 0; /* Mask 3 */
  8228. /* NameServer response */
  8229. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8230. /* NameServer */
  8231. pring->prt[3].type = FC_TYPE_CT;
  8232. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8233. lpfc_ct_unsol_event;
  8234. break;
  8235. }
  8236. totiocbsize += (pring->sli.sli3.numCiocb *
  8237. pring->sli.sli3.sizeCiocb) +
  8238. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8239. }
  8240. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8241. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8242. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8243. "SLI2 SLIM Data: x%x x%lx\n",
  8244. phba->brd_no, totiocbsize,
  8245. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8246. }
  8247. if (phba->cfg_multi_ring_support == 2)
  8248. lpfc_extra_ring_setup(phba);
  8249. return 0;
  8250. }
  8251. /**
  8252. * lpfc_sli_queue_setup - Queue initialization function
  8253. * @phba: Pointer to HBA context object.
  8254. *
  8255. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8256. * ring. This function also initializes ring indices of each ring.
  8257. * This function is called during the initialization of the SLI
  8258. * interface of an HBA.
  8259. * This function is called with no lock held and always returns
  8260. * 1.
  8261. **/
  8262. int
  8263. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8264. {
  8265. struct lpfc_sli *psli;
  8266. struct lpfc_sli_ring *pring;
  8267. int i;
  8268. psli = &phba->sli;
  8269. spin_lock_irq(&phba->hbalock);
  8270. INIT_LIST_HEAD(&psli->mboxq);
  8271. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8272. /* Initialize list headers for txq and txcmplq as double linked lists */
  8273. for (i = 0; i < psli->num_rings; i++) {
  8274. pring = &psli->ring[i];
  8275. pring->ringno = i;
  8276. pring->sli.sli3.next_cmdidx = 0;
  8277. pring->sli.sli3.local_getidx = 0;
  8278. pring->sli.sli3.cmdidx = 0;
  8279. INIT_LIST_HEAD(&pring->txq);
  8280. INIT_LIST_HEAD(&pring->txcmplq);
  8281. INIT_LIST_HEAD(&pring->iocb_continueq);
  8282. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8283. INIT_LIST_HEAD(&pring->postbufq);
  8284. spin_lock_init(&pring->ring_lock);
  8285. }
  8286. spin_unlock_irq(&phba->hbalock);
  8287. return 1;
  8288. }
  8289. /**
  8290. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8291. * @phba: Pointer to HBA context object.
  8292. *
  8293. * This routine flushes the mailbox command subsystem. It will unconditionally
  8294. * flush all the mailbox commands in the three possible stages in the mailbox
  8295. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8296. * command; and completed mailbox command queue. It is caller's responsibility
  8297. * to make sure that the driver is in the proper state to flush the mailbox
  8298. * command sub-system. Namely, the posting of mailbox commands into the
  8299. * pending mailbox command queue from the various clients must be stopped;
  8300. * either the HBA is in a state that it will never works on the outstanding
  8301. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8302. * mailbox command has been completed.
  8303. **/
  8304. static void
  8305. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8306. {
  8307. LIST_HEAD(completions);
  8308. struct lpfc_sli *psli = &phba->sli;
  8309. LPFC_MBOXQ_t *pmb;
  8310. unsigned long iflag;
  8311. /* Flush all the mailbox commands in the mbox system */
  8312. spin_lock_irqsave(&phba->hbalock, iflag);
  8313. /* The pending mailbox command queue */
  8314. list_splice_init(&phba->sli.mboxq, &completions);
  8315. /* The outstanding active mailbox command */
  8316. if (psli->mbox_active) {
  8317. list_add_tail(&psli->mbox_active->list, &completions);
  8318. psli->mbox_active = NULL;
  8319. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8320. }
  8321. /* The completed mailbox command queue */
  8322. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8323. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8324. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8325. while (!list_empty(&completions)) {
  8326. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8327. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8328. if (pmb->mbox_cmpl)
  8329. pmb->mbox_cmpl(phba, pmb);
  8330. }
  8331. }
  8332. /**
  8333. * lpfc_sli_host_down - Vport cleanup function
  8334. * @vport: Pointer to virtual port object.
  8335. *
  8336. * lpfc_sli_host_down is called to clean up the resources
  8337. * associated with a vport before destroying virtual
  8338. * port data structures.
  8339. * This function does following operations:
  8340. * - Free discovery resources associated with this virtual
  8341. * port.
  8342. * - Free iocbs associated with this virtual port in
  8343. * the txq.
  8344. * - Send abort for all iocb commands associated with this
  8345. * vport in txcmplq.
  8346. *
  8347. * This function is called with no lock held and always returns 1.
  8348. **/
  8349. int
  8350. lpfc_sli_host_down(struct lpfc_vport *vport)
  8351. {
  8352. LIST_HEAD(completions);
  8353. struct lpfc_hba *phba = vport->phba;
  8354. struct lpfc_sli *psli = &phba->sli;
  8355. struct lpfc_sli_ring *pring;
  8356. struct lpfc_iocbq *iocb, *next_iocb;
  8357. int i;
  8358. unsigned long flags = 0;
  8359. uint16_t prev_pring_flag;
  8360. lpfc_cleanup_discovery_resources(vport);
  8361. spin_lock_irqsave(&phba->hbalock, flags);
  8362. for (i = 0; i < psli->num_rings; i++) {
  8363. pring = &psli->ring[i];
  8364. prev_pring_flag = pring->flag;
  8365. /* Only slow rings */
  8366. if (pring->ringno == LPFC_ELS_RING) {
  8367. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8368. /* Set the lpfc data pending flag */
  8369. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8370. }
  8371. /*
  8372. * Error everything on the txq since these iocbs have not been
  8373. * given to the FW yet.
  8374. */
  8375. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8376. if (iocb->vport != vport)
  8377. continue;
  8378. list_move_tail(&iocb->list, &completions);
  8379. }
  8380. /* Next issue ABTS for everything on the txcmplq */
  8381. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8382. list) {
  8383. if (iocb->vport != vport)
  8384. continue;
  8385. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8386. }
  8387. pring->flag = prev_pring_flag;
  8388. }
  8389. spin_unlock_irqrestore(&phba->hbalock, flags);
  8390. /* Cancel all the IOCBs from the completions list */
  8391. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8392. IOERR_SLI_DOWN);
  8393. return 1;
  8394. }
  8395. /**
  8396. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8397. * @phba: Pointer to HBA context object.
  8398. *
  8399. * This function cleans up all iocb, buffers, mailbox commands
  8400. * while shutting down the HBA. This function is called with no
  8401. * lock held and always returns 1.
  8402. * This function does the following to cleanup driver resources:
  8403. * - Free discovery resources for each virtual port
  8404. * - Cleanup any pending fabric iocbs
  8405. * - Iterate through the iocb txq and free each entry
  8406. * in the list.
  8407. * - Free up any buffer posted to the HBA
  8408. * - Free mailbox commands in the mailbox queue.
  8409. **/
  8410. int
  8411. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8412. {
  8413. LIST_HEAD(completions);
  8414. struct lpfc_sli *psli = &phba->sli;
  8415. struct lpfc_sli_ring *pring;
  8416. struct lpfc_dmabuf *buf_ptr;
  8417. unsigned long flags = 0;
  8418. int i;
  8419. /* Shutdown the mailbox command sub-system */
  8420. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8421. lpfc_hba_down_prep(phba);
  8422. lpfc_fabric_abort_hba(phba);
  8423. spin_lock_irqsave(&phba->hbalock, flags);
  8424. for (i = 0; i < psli->num_rings; i++) {
  8425. pring = &psli->ring[i];
  8426. /* Only slow rings */
  8427. if (pring->ringno == LPFC_ELS_RING) {
  8428. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8429. /* Set the lpfc data pending flag */
  8430. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8431. }
  8432. /*
  8433. * Error everything on the txq since these iocbs have not been
  8434. * given to the FW yet.
  8435. */
  8436. list_splice_init(&pring->txq, &completions);
  8437. }
  8438. spin_unlock_irqrestore(&phba->hbalock, flags);
  8439. /* Cancel all the IOCBs from the completions list */
  8440. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8441. IOERR_SLI_DOWN);
  8442. spin_lock_irqsave(&phba->hbalock, flags);
  8443. list_splice_init(&phba->elsbuf, &completions);
  8444. phba->elsbuf_cnt = 0;
  8445. phba->elsbuf_prev_cnt = 0;
  8446. spin_unlock_irqrestore(&phba->hbalock, flags);
  8447. while (!list_empty(&completions)) {
  8448. list_remove_head(&completions, buf_ptr,
  8449. struct lpfc_dmabuf, list);
  8450. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8451. kfree(buf_ptr);
  8452. }
  8453. /* Return any active mbox cmds */
  8454. del_timer_sync(&psli->mbox_tmo);
  8455. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8456. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8457. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8458. return 1;
  8459. }
  8460. /**
  8461. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8462. * @srcp: Source memory pointer.
  8463. * @destp: Destination memory pointer.
  8464. * @cnt: Number of words required to be copied.
  8465. *
  8466. * This function is used for copying data between driver memory
  8467. * and the SLI memory. This function also changes the endianness
  8468. * of each word if native endianness is different from SLI
  8469. * endianness. This function can be called with or without
  8470. * lock.
  8471. **/
  8472. void
  8473. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8474. {
  8475. uint32_t *src = srcp;
  8476. uint32_t *dest = destp;
  8477. uint32_t ldata;
  8478. int i;
  8479. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8480. ldata = *src;
  8481. ldata = le32_to_cpu(ldata);
  8482. *dest = ldata;
  8483. src++;
  8484. dest++;
  8485. }
  8486. }
  8487. /**
  8488. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8489. * @srcp: Source memory pointer.
  8490. * @destp: Destination memory pointer.
  8491. * @cnt: Number of words required to be copied.
  8492. *
  8493. * This function is used for copying data between a data structure
  8494. * with big endian representation to local endianness.
  8495. * This function can be called with or without lock.
  8496. **/
  8497. void
  8498. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8499. {
  8500. uint32_t *src = srcp;
  8501. uint32_t *dest = destp;
  8502. uint32_t ldata;
  8503. int i;
  8504. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8505. ldata = *src;
  8506. ldata = be32_to_cpu(ldata);
  8507. *dest = ldata;
  8508. src++;
  8509. dest++;
  8510. }
  8511. }
  8512. /**
  8513. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8514. * @phba: Pointer to HBA context object.
  8515. * @pring: Pointer to driver SLI ring object.
  8516. * @mp: Pointer to driver buffer object.
  8517. *
  8518. * This function is called with no lock held.
  8519. * It always return zero after adding the buffer to the postbufq
  8520. * buffer list.
  8521. **/
  8522. int
  8523. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8524. struct lpfc_dmabuf *mp)
  8525. {
  8526. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8527. later */
  8528. spin_lock_irq(&phba->hbalock);
  8529. list_add_tail(&mp->list, &pring->postbufq);
  8530. pring->postbufq_cnt++;
  8531. spin_unlock_irq(&phba->hbalock);
  8532. return 0;
  8533. }
  8534. /**
  8535. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8536. * @phba: Pointer to HBA context object.
  8537. *
  8538. * When HBQ is enabled, buffers are searched based on tags. This function
  8539. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8540. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8541. * does not conflict with tags of buffer posted for unsolicited events.
  8542. * The function returns the allocated tag. The function is called with
  8543. * no locks held.
  8544. **/
  8545. uint32_t
  8546. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8547. {
  8548. spin_lock_irq(&phba->hbalock);
  8549. phba->buffer_tag_count++;
  8550. /*
  8551. * Always set the QUE_BUFTAG_BIT to distiguish between
  8552. * a tag assigned by HBQ.
  8553. */
  8554. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8555. spin_unlock_irq(&phba->hbalock);
  8556. return phba->buffer_tag_count;
  8557. }
  8558. /**
  8559. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8560. * @phba: Pointer to HBA context object.
  8561. * @pring: Pointer to driver SLI ring object.
  8562. * @tag: Buffer tag.
  8563. *
  8564. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8565. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8566. * iocb is posted to the response ring with the tag of the buffer.
  8567. * This function searches the pring->postbufq list using the tag
  8568. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8569. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8570. * buffer is returned to the caller else NULL is returned.
  8571. * This function is called with no lock held.
  8572. **/
  8573. struct lpfc_dmabuf *
  8574. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8575. uint32_t tag)
  8576. {
  8577. struct lpfc_dmabuf *mp, *next_mp;
  8578. struct list_head *slp = &pring->postbufq;
  8579. /* Search postbufq, from the beginning, looking for a match on tag */
  8580. spin_lock_irq(&phba->hbalock);
  8581. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8582. if (mp->buffer_tag == tag) {
  8583. list_del_init(&mp->list);
  8584. pring->postbufq_cnt--;
  8585. spin_unlock_irq(&phba->hbalock);
  8586. return mp;
  8587. }
  8588. }
  8589. spin_unlock_irq(&phba->hbalock);
  8590. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8591. "0402 Cannot find virtual addr for buffer tag on "
  8592. "ring %d Data x%lx x%p x%p x%x\n",
  8593. pring->ringno, (unsigned long) tag,
  8594. slp->next, slp->prev, pring->postbufq_cnt);
  8595. return NULL;
  8596. }
  8597. /**
  8598. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8599. * @phba: Pointer to HBA context object.
  8600. * @pring: Pointer to driver SLI ring object.
  8601. * @phys: DMA address of the buffer.
  8602. *
  8603. * This function searches the buffer list using the dma_address
  8604. * of unsolicited event to find the driver's lpfc_dmabuf object
  8605. * corresponding to the dma_address. The function returns the
  8606. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8607. * This function is called by the ct and els unsolicited event
  8608. * handlers to get the buffer associated with the unsolicited
  8609. * event.
  8610. *
  8611. * This function is called with no lock held.
  8612. **/
  8613. struct lpfc_dmabuf *
  8614. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8615. dma_addr_t phys)
  8616. {
  8617. struct lpfc_dmabuf *mp, *next_mp;
  8618. struct list_head *slp = &pring->postbufq;
  8619. /* Search postbufq, from the beginning, looking for a match on phys */
  8620. spin_lock_irq(&phba->hbalock);
  8621. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8622. if (mp->phys == phys) {
  8623. list_del_init(&mp->list);
  8624. pring->postbufq_cnt--;
  8625. spin_unlock_irq(&phba->hbalock);
  8626. return mp;
  8627. }
  8628. }
  8629. spin_unlock_irq(&phba->hbalock);
  8630. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8631. "0410 Cannot find virtual addr for mapped buf on "
  8632. "ring %d Data x%llx x%p x%p x%x\n",
  8633. pring->ringno, (unsigned long long)phys,
  8634. slp->next, slp->prev, pring->postbufq_cnt);
  8635. return NULL;
  8636. }
  8637. /**
  8638. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8639. * @phba: Pointer to HBA context object.
  8640. * @cmdiocb: Pointer to driver command iocb object.
  8641. * @rspiocb: Pointer to driver response iocb object.
  8642. *
  8643. * This function is the completion handler for the abort iocbs for
  8644. * ELS commands. This function is called from the ELS ring event
  8645. * handler with no lock held. This function frees memory resources
  8646. * associated with the abort iocb.
  8647. **/
  8648. static void
  8649. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8650. struct lpfc_iocbq *rspiocb)
  8651. {
  8652. IOCB_t *irsp = &rspiocb->iocb;
  8653. uint16_t abort_iotag, abort_context;
  8654. struct lpfc_iocbq *abort_iocb = NULL;
  8655. if (irsp->ulpStatus) {
  8656. /*
  8657. * Assume that the port already completed and returned, or
  8658. * will return the iocb. Just Log the message.
  8659. */
  8660. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8661. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8662. spin_lock_irq(&phba->hbalock);
  8663. if (phba->sli_rev < LPFC_SLI_REV4) {
  8664. if (abort_iotag != 0 &&
  8665. abort_iotag <= phba->sli.last_iotag)
  8666. abort_iocb =
  8667. phba->sli.iocbq_lookup[abort_iotag];
  8668. } else
  8669. /* For sli4 the abort_tag is the XRI,
  8670. * so the abort routine puts the iotag of the iocb
  8671. * being aborted in the context field of the abort
  8672. * IOCB.
  8673. */
  8674. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8675. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8676. "0327 Cannot abort els iocb %p "
  8677. "with tag %x context %x, abort status %x, "
  8678. "abort code %x\n",
  8679. abort_iocb, abort_iotag, abort_context,
  8680. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8681. spin_unlock_irq(&phba->hbalock);
  8682. }
  8683. lpfc_sli_release_iocbq(phba, cmdiocb);
  8684. return;
  8685. }
  8686. /**
  8687. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8688. * @phba: Pointer to HBA context object.
  8689. * @cmdiocb: Pointer to driver command iocb object.
  8690. * @rspiocb: Pointer to driver response iocb object.
  8691. *
  8692. * The function is called from SLI ring event handler with no
  8693. * lock held. This function is the completion handler for ELS commands
  8694. * which are aborted. The function frees memory resources used for
  8695. * the aborted ELS commands.
  8696. **/
  8697. static void
  8698. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8699. struct lpfc_iocbq *rspiocb)
  8700. {
  8701. IOCB_t *irsp = &rspiocb->iocb;
  8702. /* ELS cmd tag <ulpIoTag> completes */
  8703. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8704. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8705. "x%x x%x x%x\n",
  8706. irsp->ulpIoTag, irsp->ulpStatus,
  8707. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8708. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8709. lpfc_ct_free_iocb(phba, cmdiocb);
  8710. else
  8711. lpfc_els_free_iocb(phba, cmdiocb);
  8712. return;
  8713. }
  8714. /**
  8715. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8716. * @phba: Pointer to HBA context object.
  8717. * @pring: Pointer to driver SLI ring object.
  8718. * @cmdiocb: Pointer to driver command iocb object.
  8719. *
  8720. * This function issues an abort iocb for the provided command iocb down to
  8721. * the port. Other than the case the outstanding command iocb is an abort
  8722. * request, this function issues abort out unconditionally. This function is
  8723. * called with hbalock held. The function returns 0 when it fails due to
  8724. * memory allocation failure or when the command iocb is an abort request.
  8725. **/
  8726. static int
  8727. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8728. struct lpfc_iocbq *cmdiocb)
  8729. {
  8730. struct lpfc_vport *vport = cmdiocb->vport;
  8731. struct lpfc_iocbq *abtsiocbp;
  8732. IOCB_t *icmd = NULL;
  8733. IOCB_t *iabt = NULL;
  8734. int retval;
  8735. unsigned long iflags;
  8736. /*
  8737. * There are certain command types we don't want to abort. And we
  8738. * don't want to abort commands that are already in the process of
  8739. * being aborted.
  8740. */
  8741. icmd = &cmdiocb->iocb;
  8742. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8743. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8744. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8745. return 0;
  8746. /* issue ABTS for this IOCB based on iotag */
  8747. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8748. if (abtsiocbp == NULL)
  8749. return 0;
  8750. /* This signals the response to set the correct status
  8751. * before calling the completion handler
  8752. */
  8753. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8754. iabt = &abtsiocbp->iocb;
  8755. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8756. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8757. if (phba->sli_rev == LPFC_SLI_REV4) {
  8758. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8759. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8760. }
  8761. else
  8762. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8763. iabt->ulpLe = 1;
  8764. iabt->ulpClass = icmd->ulpClass;
  8765. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8766. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8767. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8768. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8769. if (phba->link_state >= LPFC_LINK_UP)
  8770. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8771. else
  8772. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8773. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8774. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8775. "0339 Abort xri x%x, original iotag x%x, "
  8776. "abort cmd iotag x%x\n",
  8777. iabt->un.acxri.abortIoTag,
  8778. iabt->un.acxri.abortContextTag,
  8779. abtsiocbp->iotag);
  8780. if (phba->sli_rev == LPFC_SLI_REV4) {
  8781. /* Note: both hbalock and ring_lock need to be set here */
  8782. spin_lock_irqsave(&pring->ring_lock, iflags);
  8783. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8784. abtsiocbp, 0);
  8785. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8786. } else {
  8787. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8788. abtsiocbp, 0);
  8789. }
  8790. if (retval)
  8791. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8792. /*
  8793. * Caller to this routine should check for IOCB_ERROR
  8794. * and handle it properly. This routine no longer removes
  8795. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8796. */
  8797. return retval;
  8798. }
  8799. /**
  8800. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8801. * @phba: Pointer to HBA context object.
  8802. * @pring: Pointer to driver SLI ring object.
  8803. * @cmdiocb: Pointer to driver command iocb object.
  8804. *
  8805. * This function issues an abort iocb for the provided command iocb. In case
  8806. * of unloading, the abort iocb will not be issued to commands on the ELS
  8807. * ring. Instead, the callback function shall be changed to those commands
  8808. * so that nothing happens when them finishes. This function is called with
  8809. * hbalock held. The function returns 0 when the command iocb is an abort
  8810. * request.
  8811. **/
  8812. int
  8813. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8814. struct lpfc_iocbq *cmdiocb)
  8815. {
  8816. struct lpfc_vport *vport = cmdiocb->vport;
  8817. int retval = IOCB_ERROR;
  8818. IOCB_t *icmd = NULL;
  8819. /*
  8820. * There are certain command types we don't want to abort. And we
  8821. * don't want to abort commands that are already in the process of
  8822. * being aborted.
  8823. */
  8824. icmd = &cmdiocb->iocb;
  8825. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8826. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8827. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8828. return 0;
  8829. /*
  8830. * If we're unloading, don't abort iocb on the ELS ring, but change
  8831. * the callback so that nothing happens when it finishes.
  8832. */
  8833. if ((vport->load_flag & FC_UNLOADING) &&
  8834. (pring->ringno == LPFC_ELS_RING)) {
  8835. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8836. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8837. else
  8838. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8839. goto abort_iotag_exit;
  8840. }
  8841. /* Now, we try to issue the abort to the cmdiocb out */
  8842. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8843. abort_iotag_exit:
  8844. /*
  8845. * Caller to this routine should check for IOCB_ERROR
  8846. * and handle it properly. This routine no longer removes
  8847. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8848. */
  8849. return retval;
  8850. }
  8851. /**
  8852. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8853. * @phba: Pointer to HBA context object.
  8854. * @pring: Pointer to driver SLI ring object.
  8855. *
  8856. * This function aborts all iocbs in the given ring and frees all the iocb
  8857. * objects in txq. This function issues abort iocbs unconditionally for all
  8858. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8859. * to complete before the return of this function. The caller is not required
  8860. * to hold any locks.
  8861. **/
  8862. static void
  8863. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8864. {
  8865. LIST_HEAD(completions);
  8866. struct lpfc_iocbq *iocb, *next_iocb;
  8867. if (pring->ringno == LPFC_ELS_RING)
  8868. lpfc_fabric_abort_hba(phba);
  8869. spin_lock_irq(&phba->hbalock);
  8870. /* Take off all the iocbs on txq for cancelling */
  8871. list_splice_init(&pring->txq, &completions);
  8872. pring->txq_cnt = 0;
  8873. /* Next issue ABTS for everything on the txcmplq */
  8874. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8875. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8876. spin_unlock_irq(&phba->hbalock);
  8877. /* Cancel all the IOCBs from the completions list */
  8878. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8879. IOERR_SLI_ABORTED);
  8880. }
  8881. /**
  8882. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8883. * @phba: pointer to lpfc HBA data structure.
  8884. *
  8885. * This routine will abort all pending and outstanding iocbs to an HBA.
  8886. **/
  8887. void
  8888. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8889. {
  8890. struct lpfc_sli *psli = &phba->sli;
  8891. struct lpfc_sli_ring *pring;
  8892. int i;
  8893. for (i = 0; i < psli->num_rings; i++) {
  8894. pring = &psli->ring[i];
  8895. lpfc_sli_iocb_ring_abort(phba, pring);
  8896. }
  8897. }
  8898. /**
  8899. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8900. * @iocbq: Pointer to driver iocb object.
  8901. * @vport: Pointer to driver virtual port object.
  8902. * @tgt_id: SCSI ID of the target.
  8903. * @lun_id: LUN ID of the scsi device.
  8904. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8905. *
  8906. * This function acts as an iocb filter for functions which abort or count
  8907. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8908. * 0 if the filtering criteria is met for the given iocb and will return
  8909. * 1 if the filtering criteria is not met.
  8910. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8911. * given iocb is for the SCSI device specified by vport, tgt_id and
  8912. * lun_id parameter.
  8913. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8914. * given iocb is for the SCSI target specified by vport and tgt_id
  8915. * parameters.
  8916. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8917. * given iocb is for the SCSI host associated with the given vport.
  8918. * This function is called with no locks held.
  8919. **/
  8920. static int
  8921. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8922. uint16_t tgt_id, uint64_t lun_id,
  8923. lpfc_ctx_cmd ctx_cmd)
  8924. {
  8925. struct lpfc_scsi_buf *lpfc_cmd;
  8926. int rc = 1;
  8927. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8928. return rc;
  8929. if (iocbq->vport != vport)
  8930. return rc;
  8931. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8932. if (lpfc_cmd->pCmd == NULL)
  8933. return rc;
  8934. switch (ctx_cmd) {
  8935. case LPFC_CTX_LUN:
  8936. if ((lpfc_cmd->rdata->pnode) &&
  8937. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8938. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8939. rc = 0;
  8940. break;
  8941. case LPFC_CTX_TGT:
  8942. if ((lpfc_cmd->rdata->pnode) &&
  8943. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8944. rc = 0;
  8945. break;
  8946. case LPFC_CTX_HOST:
  8947. rc = 0;
  8948. break;
  8949. default:
  8950. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8951. __func__, ctx_cmd);
  8952. break;
  8953. }
  8954. return rc;
  8955. }
  8956. /**
  8957. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8958. * @vport: Pointer to virtual port.
  8959. * @tgt_id: SCSI ID of the target.
  8960. * @lun_id: LUN ID of the scsi device.
  8961. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8962. *
  8963. * This function returns number of FCP commands pending for the vport.
  8964. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8965. * commands pending on the vport associated with SCSI device specified
  8966. * by tgt_id and lun_id parameters.
  8967. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8968. * commands pending on the vport associated with SCSI target specified
  8969. * by tgt_id parameter.
  8970. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8971. * commands pending on the vport.
  8972. * This function returns the number of iocbs which satisfy the filter.
  8973. * This function is called without any lock held.
  8974. **/
  8975. int
  8976. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8977. lpfc_ctx_cmd ctx_cmd)
  8978. {
  8979. struct lpfc_hba *phba = vport->phba;
  8980. struct lpfc_iocbq *iocbq;
  8981. int sum, i;
  8982. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8983. iocbq = phba->sli.iocbq_lookup[i];
  8984. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8985. ctx_cmd) == 0)
  8986. sum++;
  8987. }
  8988. return sum;
  8989. }
  8990. /**
  8991. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8992. * @phba: Pointer to HBA context object
  8993. * @cmdiocb: Pointer to command iocb object.
  8994. * @rspiocb: Pointer to response iocb object.
  8995. *
  8996. * This function is called when an aborted FCP iocb completes. This
  8997. * function is called by the ring event handler with no lock held.
  8998. * This function frees the iocb.
  8999. **/
  9000. void
  9001. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  9002. struct lpfc_iocbq *rspiocb)
  9003. {
  9004. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9005. "3096 ABORT_XRI_CN completing on rpi x%x "
  9006. "original iotag x%x, abort cmd iotag x%x "
  9007. "status 0x%x, reason 0x%x\n",
  9008. cmdiocb->iocb.un.acxri.abortContextTag,
  9009. cmdiocb->iocb.un.acxri.abortIoTag,
  9010. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  9011. rspiocb->iocb.un.ulpWord[4]);
  9012. lpfc_sli_release_iocbq(phba, cmdiocb);
  9013. return;
  9014. }
  9015. /**
  9016. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  9017. * @vport: Pointer to virtual port.
  9018. * @pring: Pointer to driver SLI ring object.
  9019. * @tgt_id: SCSI ID of the target.
  9020. * @lun_id: LUN ID of the scsi device.
  9021. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  9022. *
  9023. * This function sends an abort command for every SCSI command
  9024. * associated with the given virtual port pending on the ring
  9025. * filtered by lpfc_sli_validate_fcp_iocb function.
  9026. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  9027. * FCP iocbs associated with lun specified by tgt_id and lun_id
  9028. * parameters
  9029. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  9030. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  9031. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  9032. * FCP iocbs associated with virtual port.
  9033. * This function returns number of iocbs it failed to abort.
  9034. * This function is called with no locks held.
  9035. **/
  9036. int
  9037. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  9038. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  9039. {
  9040. struct lpfc_hba *phba = vport->phba;
  9041. struct lpfc_iocbq *iocbq;
  9042. struct lpfc_iocbq *abtsiocb;
  9043. IOCB_t *cmd = NULL;
  9044. int errcnt = 0, ret_val = 0;
  9045. int i;
  9046. for (i = 1; i <= phba->sli.last_iotag; i++) {
  9047. iocbq = phba->sli.iocbq_lookup[i];
  9048. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  9049. abort_cmd) != 0)
  9050. continue;
  9051. /*
  9052. * If the iocbq is already being aborted, don't take a second
  9053. * action, but do count it.
  9054. */
  9055. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  9056. continue;
  9057. /* issue ABTS for this IOCB based on iotag */
  9058. abtsiocb = lpfc_sli_get_iocbq(phba);
  9059. if (abtsiocb == NULL) {
  9060. errcnt++;
  9061. continue;
  9062. }
  9063. /* indicate the IO is being aborted by the driver. */
  9064. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  9065. cmd = &iocbq->iocb;
  9066. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  9067. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  9068. if (phba->sli_rev == LPFC_SLI_REV4)
  9069. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  9070. else
  9071. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  9072. abtsiocb->iocb.ulpLe = 1;
  9073. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  9074. abtsiocb->vport = vport;
  9075. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9076. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  9077. if (iocbq->iocb_flag & LPFC_IO_FCP)
  9078. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  9079. if (lpfc_is_link_up(phba))
  9080. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  9081. else
  9082. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  9083. /* Setup callback routine and issue the command. */
  9084. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  9085. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  9086. abtsiocb, 0);
  9087. if (ret_val == IOCB_ERROR) {
  9088. lpfc_sli_release_iocbq(phba, abtsiocb);
  9089. errcnt++;
  9090. continue;
  9091. }
  9092. }
  9093. return errcnt;
  9094. }
  9095. /**
  9096. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  9097. * @phba: Pointer to HBA context object.
  9098. * @cmdiocbq: Pointer to command iocb.
  9099. * @rspiocbq: Pointer to response iocb.
  9100. *
  9101. * This function is the completion handler for iocbs issued using
  9102. * lpfc_sli_issue_iocb_wait function. This function is called by the
  9103. * ring event handler function without any lock held. This function
  9104. * can be called from both worker thread context and interrupt
  9105. * context. This function also can be called from other thread which
  9106. * cleans up the SLI layer objects.
  9107. * This function copy the contents of the response iocb to the
  9108. * response iocb memory object provided by the caller of
  9109. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  9110. * sleeps for the iocb completion.
  9111. **/
  9112. static void
  9113. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  9114. struct lpfc_iocbq *cmdiocbq,
  9115. struct lpfc_iocbq *rspiocbq)
  9116. {
  9117. wait_queue_head_t *pdone_q;
  9118. unsigned long iflags;
  9119. struct lpfc_scsi_buf *lpfc_cmd;
  9120. spin_lock_irqsave(&phba->hbalock, iflags);
  9121. if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
  9122. /*
  9123. * A time out has occurred for the iocb. If a time out
  9124. * completion handler has been supplied, call it. Otherwise,
  9125. * just free the iocbq.
  9126. */
  9127. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9128. cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
  9129. cmdiocbq->wait_iocb_cmpl = NULL;
  9130. if (cmdiocbq->iocb_cmpl)
  9131. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
  9132. else
  9133. lpfc_sli_release_iocbq(phba, cmdiocbq);
  9134. return;
  9135. }
  9136. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  9137. if (cmdiocbq->context2 && rspiocbq)
  9138. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  9139. &rspiocbq->iocb, sizeof(IOCB_t));
  9140. /* Set the exchange busy flag for task management commands */
  9141. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  9142. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  9143. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  9144. cur_iocbq);
  9145. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  9146. }
  9147. pdone_q = cmdiocbq->context_un.wait_queue;
  9148. if (pdone_q)
  9149. wake_up(pdone_q);
  9150. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9151. return;
  9152. }
  9153. /**
  9154. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  9155. * @phba: Pointer to HBA context object..
  9156. * @piocbq: Pointer to command iocb.
  9157. * @flag: Flag to test.
  9158. *
  9159. * This routine grabs the hbalock and then test the iocb_flag to
  9160. * see if the passed in flag is set.
  9161. * Returns:
  9162. * 1 if flag is set.
  9163. * 0 if flag is not set.
  9164. **/
  9165. static int
  9166. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  9167. struct lpfc_iocbq *piocbq, uint32_t flag)
  9168. {
  9169. unsigned long iflags;
  9170. int ret;
  9171. spin_lock_irqsave(&phba->hbalock, iflags);
  9172. ret = piocbq->iocb_flag & flag;
  9173. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9174. return ret;
  9175. }
  9176. /**
  9177. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  9178. * @phba: Pointer to HBA context object..
  9179. * @pring: Pointer to sli ring.
  9180. * @piocb: Pointer to command iocb.
  9181. * @prspiocbq: Pointer to response iocb.
  9182. * @timeout: Timeout in number of seconds.
  9183. *
  9184. * This function issues the iocb to firmware and waits for the
  9185. * iocb to complete. The iocb_cmpl field of the shall be used
  9186. * to handle iocbs which time out. If the field is NULL, the
  9187. * function shall free the iocbq structure. If more clean up is
  9188. * needed, the caller is expected to provide a completion function
  9189. * that will provide the needed clean up. If the iocb command is
  9190. * not completed within timeout seconds, the function will either
  9191. * free the iocbq structure (if iocb_cmpl == NULL) or execute the
  9192. * completion function set in the iocb_cmpl field and then return
  9193. * a status of IOCB_TIMEDOUT. The caller should not free the iocb
  9194. * resources if this function returns IOCB_TIMEDOUT.
  9195. * The function waits for the iocb completion using an
  9196. * non-interruptible wait.
  9197. * This function will sleep while waiting for iocb completion.
  9198. * So, this function should not be called from any context which
  9199. * does not allow sleeping. Due to the same reason, this function
  9200. * cannot be called with interrupt disabled.
  9201. * This function assumes that the iocb completions occur while
  9202. * this function sleep. So, this function cannot be called from
  9203. * the thread which process iocb completion for this ring.
  9204. * This function clears the iocb_flag of the iocb object before
  9205. * issuing the iocb and the iocb completion handler sets this
  9206. * flag and wakes this thread when the iocb completes.
  9207. * The contents of the response iocb will be copied to prspiocbq
  9208. * by the completion handler when the command completes.
  9209. * This function returns IOCB_SUCCESS when success.
  9210. * This function is called with no lock held.
  9211. **/
  9212. int
  9213. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9214. uint32_t ring_number,
  9215. struct lpfc_iocbq *piocb,
  9216. struct lpfc_iocbq *prspiocbq,
  9217. uint32_t timeout)
  9218. {
  9219. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9220. long timeleft, timeout_req = 0;
  9221. int retval = IOCB_SUCCESS;
  9222. uint32_t creg_val;
  9223. struct lpfc_iocbq *iocb;
  9224. int txq_cnt = 0;
  9225. int txcmplq_cnt = 0;
  9226. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9227. unsigned long iflags;
  9228. bool iocb_completed = true;
  9229. /*
  9230. * If the caller has provided a response iocbq buffer, then context2
  9231. * is NULL or its an error.
  9232. */
  9233. if (prspiocbq) {
  9234. if (piocb->context2)
  9235. return IOCB_ERROR;
  9236. piocb->context2 = prspiocbq;
  9237. }
  9238. piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
  9239. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9240. piocb->context_un.wait_queue = &done_q;
  9241. piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
  9242. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9243. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9244. return IOCB_ERROR;
  9245. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9246. writel(creg_val, phba->HCregaddr);
  9247. readl(phba->HCregaddr); /* flush */
  9248. }
  9249. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9250. SLI_IOCB_RET_IOCB);
  9251. if (retval == IOCB_SUCCESS) {
  9252. timeout_req = msecs_to_jiffies(timeout * 1000);
  9253. timeleft = wait_event_timeout(done_q,
  9254. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9255. timeout_req);
  9256. spin_lock_irqsave(&phba->hbalock, iflags);
  9257. if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
  9258. /*
  9259. * IOCB timed out. Inform the wake iocb wait
  9260. * completion function and set local status
  9261. */
  9262. iocb_completed = false;
  9263. piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
  9264. }
  9265. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9266. if (iocb_completed) {
  9267. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9268. "0331 IOCB wake signaled\n");
  9269. /* Note: we are not indicating if the IOCB has a success
  9270. * status or not - that's for the caller to check.
  9271. * IOCB_SUCCESS means just that the command was sent and
  9272. * completed. Not that it completed successfully.
  9273. * */
  9274. } else if (timeleft == 0) {
  9275. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9276. "0338 IOCB wait timeout error - no "
  9277. "wake response Data x%x\n", timeout);
  9278. retval = IOCB_TIMEDOUT;
  9279. } else {
  9280. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9281. "0330 IOCB wake NOT set, "
  9282. "Data x%x x%lx\n",
  9283. timeout, (timeleft / jiffies));
  9284. retval = IOCB_TIMEDOUT;
  9285. }
  9286. } else if (retval == IOCB_BUSY) {
  9287. if (phba->cfg_log_verbose & LOG_SLI) {
  9288. list_for_each_entry(iocb, &pring->txq, list) {
  9289. txq_cnt++;
  9290. }
  9291. list_for_each_entry(iocb, &pring->txcmplq, list) {
  9292. txcmplq_cnt++;
  9293. }
  9294. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9295. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9296. phba->iocb_cnt, txq_cnt, txcmplq_cnt);
  9297. }
  9298. return retval;
  9299. } else {
  9300. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9301. "0332 IOCB wait issue failed, Data x%x\n",
  9302. retval);
  9303. retval = IOCB_ERROR;
  9304. }
  9305. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9306. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9307. return IOCB_ERROR;
  9308. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9309. writel(creg_val, phba->HCregaddr);
  9310. readl(phba->HCregaddr); /* flush */
  9311. }
  9312. if (prspiocbq)
  9313. piocb->context2 = NULL;
  9314. piocb->context_un.wait_queue = NULL;
  9315. piocb->iocb_cmpl = NULL;
  9316. return retval;
  9317. }
  9318. /**
  9319. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9320. * @phba: Pointer to HBA context object.
  9321. * @pmboxq: Pointer to driver mailbox object.
  9322. * @timeout: Timeout in number of seconds.
  9323. *
  9324. * This function issues the mailbox to firmware and waits for the
  9325. * mailbox command to complete. If the mailbox command is not
  9326. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9327. * The function waits for the mailbox completion using an
  9328. * interruptible wait. If the thread is woken up due to a
  9329. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9330. * should not free the mailbox resources, if this function returns
  9331. * MBX_TIMEOUT.
  9332. * This function will sleep while waiting for mailbox completion.
  9333. * So, this function should not be called from any context which
  9334. * does not allow sleeping. Due to the same reason, this function
  9335. * cannot be called with interrupt disabled.
  9336. * This function assumes that the mailbox completion occurs while
  9337. * this function sleep. So, this function cannot be called from
  9338. * the worker thread which processes mailbox completion.
  9339. * This function is called in the context of HBA management
  9340. * applications.
  9341. * This function returns MBX_SUCCESS when successful.
  9342. * This function is called with no lock held.
  9343. **/
  9344. int
  9345. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9346. uint32_t timeout)
  9347. {
  9348. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9349. MAILBOX_t *mb = NULL;
  9350. int retval;
  9351. unsigned long flag;
  9352. /* The caller might set context1 for extended buffer */
  9353. if (pmboxq->context1)
  9354. mb = (MAILBOX_t *)pmboxq->context1;
  9355. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9356. /* setup wake call as IOCB callback */
  9357. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9358. /* setup context field to pass wait_queue pointer to wake function */
  9359. pmboxq->context1 = &done_q;
  9360. /* now issue the command */
  9361. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9362. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9363. wait_event_interruptible_timeout(done_q,
  9364. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9365. msecs_to_jiffies(timeout * 1000));
  9366. spin_lock_irqsave(&phba->hbalock, flag);
  9367. /* restore the possible extended buffer for free resource */
  9368. pmboxq->context1 = (uint8_t *)mb;
  9369. /*
  9370. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9371. * else do not free the resources.
  9372. */
  9373. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9374. retval = MBX_SUCCESS;
  9375. } else {
  9376. retval = MBX_TIMEOUT;
  9377. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9378. }
  9379. spin_unlock_irqrestore(&phba->hbalock, flag);
  9380. } else {
  9381. /* restore the possible extended buffer for free resource */
  9382. pmboxq->context1 = (uint8_t *)mb;
  9383. }
  9384. return retval;
  9385. }
  9386. /**
  9387. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9388. * @phba: Pointer to HBA context.
  9389. *
  9390. * This function is called to shutdown the driver's mailbox sub-system.
  9391. * It first marks the mailbox sub-system is in a block state to prevent
  9392. * the asynchronous mailbox command from issued off the pending mailbox
  9393. * command queue. If the mailbox command sub-system shutdown is due to
  9394. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9395. * the mailbox sub-system flush routine to forcefully bring down the
  9396. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9397. * as with offline or HBA function reset), this routine will wait for the
  9398. * outstanding mailbox command to complete before invoking the mailbox
  9399. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9400. **/
  9401. void
  9402. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9403. {
  9404. struct lpfc_sli *psli = &phba->sli;
  9405. unsigned long timeout;
  9406. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9407. /* delay 100ms for port state */
  9408. msleep(100);
  9409. lpfc_sli_mbox_sys_flush(phba);
  9410. return;
  9411. }
  9412. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9413. spin_lock_irq(&phba->hbalock);
  9414. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9415. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9416. /* Determine how long we might wait for the active mailbox
  9417. * command to be gracefully completed by firmware.
  9418. */
  9419. if (phba->sli.mbox_active)
  9420. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9421. phba->sli.mbox_active) *
  9422. 1000) + jiffies;
  9423. spin_unlock_irq(&phba->hbalock);
  9424. while (phba->sli.mbox_active) {
  9425. /* Check active mailbox complete status every 2ms */
  9426. msleep(2);
  9427. if (time_after(jiffies, timeout))
  9428. /* Timeout, let the mailbox flush routine to
  9429. * forcefully release active mailbox command
  9430. */
  9431. break;
  9432. }
  9433. } else
  9434. spin_unlock_irq(&phba->hbalock);
  9435. lpfc_sli_mbox_sys_flush(phba);
  9436. }
  9437. /**
  9438. * lpfc_sli_eratt_read - read sli-3 error attention events
  9439. * @phba: Pointer to HBA context.
  9440. *
  9441. * This function is called to read the SLI3 device error attention registers
  9442. * for possible error attention events. The caller must hold the hostlock
  9443. * with spin_lock_irq().
  9444. *
  9445. * This function returns 1 when there is Error Attention in the Host Attention
  9446. * Register and returns 0 otherwise.
  9447. **/
  9448. static int
  9449. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9450. {
  9451. uint32_t ha_copy;
  9452. /* Read chip Host Attention (HA) register */
  9453. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9454. goto unplug_err;
  9455. if (ha_copy & HA_ERATT) {
  9456. /* Read host status register to retrieve error event */
  9457. if (lpfc_sli_read_hs(phba))
  9458. goto unplug_err;
  9459. /* Check if there is a deferred error condition is active */
  9460. if ((HS_FFER1 & phba->work_hs) &&
  9461. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9462. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9463. phba->hba_flag |= DEFER_ERATT;
  9464. /* Clear all interrupt enable conditions */
  9465. writel(0, phba->HCregaddr);
  9466. readl(phba->HCregaddr);
  9467. }
  9468. /* Set the driver HA work bitmap */
  9469. phba->work_ha |= HA_ERATT;
  9470. /* Indicate polling handles this ERATT */
  9471. phba->hba_flag |= HBA_ERATT_HANDLED;
  9472. return 1;
  9473. }
  9474. return 0;
  9475. unplug_err:
  9476. /* Set the driver HS work bitmap */
  9477. phba->work_hs |= UNPLUG_ERR;
  9478. /* Set the driver HA work bitmap */
  9479. phba->work_ha |= HA_ERATT;
  9480. /* Indicate polling handles this ERATT */
  9481. phba->hba_flag |= HBA_ERATT_HANDLED;
  9482. return 1;
  9483. }
  9484. /**
  9485. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9486. * @phba: Pointer to HBA context.
  9487. *
  9488. * This function is called to read the SLI4 device error attention registers
  9489. * for possible error attention events. The caller must hold the hostlock
  9490. * with spin_lock_irq().
  9491. *
  9492. * This function returns 1 when there is Error Attention in the Host Attention
  9493. * Register and returns 0 otherwise.
  9494. **/
  9495. static int
  9496. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9497. {
  9498. uint32_t uerr_sta_hi, uerr_sta_lo;
  9499. uint32_t if_type, portsmphr;
  9500. struct lpfc_register portstat_reg;
  9501. /*
  9502. * For now, use the SLI4 device internal unrecoverable error
  9503. * registers for error attention. This can be changed later.
  9504. */
  9505. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9506. switch (if_type) {
  9507. case LPFC_SLI_INTF_IF_TYPE_0:
  9508. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9509. &uerr_sta_lo) ||
  9510. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9511. &uerr_sta_hi)) {
  9512. phba->work_hs |= UNPLUG_ERR;
  9513. phba->work_ha |= HA_ERATT;
  9514. phba->hba_flag |= HBA_ERATT_HANDLED;
  9515. return 1;
  9516. }
  9517. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9518. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9519. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9520. "1423 HBA Unrecoverable error: "
  9521. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9522. "ue_mask_lo_reg=0x%x, "
  9523. "ue_mask_hi_reg=0x%x\n",
  9524. uerr_sta_lo, uerr_sta_hi,
  9525. phba->sli4_hba.ue_mask_lo,
  9526. phba->sli4_hba.ue_mask_hi);
  9527. phba->work_status[0] = uerr_sta_lo;
  9528. phba->work_status[1] = uerr_sta_hi;
  9529. phba->work_ha |= HA_ERATT;
  9530. phba->hba_flag |= HBA_ERATT_HANDLED;
  9531. return 1;
  9532. }
  9533. break;
  9534. case LPFC_SLI_INTF_IF_TYPE_2:
  9535. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9536. &portstat_reg.word0) ||
  9537. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9538. &portsmphr)){
  9539. phba->work_hs |= UNPLUG_ERR;
  9540. phba->work_ha |= HA_ERATT;
  9541. phba->hba_flag |= HBA_ERATT_HANDLED;
  9542. return 1;
  9543. }
  9544. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9545. phba->work_status[0] =
  9546. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9547. phba->work_status[1] =
  9548. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9549. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9550. "2885 Port Status Event: "
  9551. "port status reg 0x%x, "
  9552. "port smphr reg 0x%x, "
  9553. "error 1=0x%x, error 2=0x%x\n",
  9554. portstat_reg.word0,
  9555. portsmphr,
  9556. phba->work_status[0],
  9557. phba->work_status[1]);
  9558. phba->work_ha |= HA_ERATT;
  9559. phba->hba_flag |= HBA_ERATT_HANDLED;
  9560. return 1;
  9561. }
  9562. break;
  9563. case LPFC_SLI_INTF_IF_TYPE_1:
  9564. default:
  9565. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9566. "2886 HBA Error Attention on unsupported "
  9567. "if type %d.", if_type);
  9568. return 1;
  9569. }
  9570. return 0;
  9571. }
  9572. /**
  9573. * lpfc_sli_check_eratt - check error attention events
  9574. * @phba: Pointer to HBA context.
  9575. *
  9576. * This function is called from timer soft interrupt context to check HBA's
  9577. * error attention register bit for error attention events.
  9578. *
  9579. * This function returns 1 when there is Error Attention in the Host Attention
  9580. * Register and returns 0 otherwise.
  9581. **/
  9582. int
  9583. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9584. {
  9585. uint32_t ha_copy;
  9586. /* If somebody is waiting to handle an eratt, don't process it
  9587. * here. The brdkill function will do this.
  9588. */
  9589. if (phba->link_flag & LS_IGNORE_ERATT)
  9590. return 0;
  9591. /* Check if interrupt handler handles this ERATT */
  9592. spin_lock_irq(&phba->hbalock);
  9593. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9594. /* Interrupt handler has handled ERATT */
  9595. spin_unlock_irq(&phba->hbalock);
  9596. return 0;
  9597. }
  9598. /*
  9599. * If there is deferred error attention, do not check for error
  9600. * attention
  9601. */
  9602. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9603. spin_unlock_irq(&phba->hbalock);
  9604. return 0;
  9605. }
  9606. /* If PCI channel is offline, don't process it */
  9607. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9608. spin_unlock_irq(&phba->hbalock);
  9609. return 0;
  9610. }
  9611. switch (phba->sli_rev) {
  9612. case LPFC_SLI_REV2:
  9613. case LPFC_SLI_REV3:
  9614. /* Read chip Host Attention (HA) register */
  9615. ha_copy = lpfc_sli_eratt_read(phba);
  9616. break;
  9617. case LPFC_SLI_REV4:
  9618. /* Read device Uncoverable Error (UERR) registers */
  9619. ha_copy = lpfc_sli4_eratt_read(phba);
  9620. break;
  9621. default:
  9622. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9623. "0299 Invalid SLI revision (%d)\n",
  9624. phba->sli_rev);
  9625. ha_copy = 0;
  9626. break;
  9627. }
  9628. spin_unlock_irq(&phba->hbalock);
  9629. return ha_copy;
  9630. }
  9631. /**
  9632. * lpfc_intr_state_check - Check device state for interrupt handling
  9633. * @phba: Pointer to HBA context.
  9634. *
  9635. * This inline routine checks whether a device or its PCI slot is in a state
  9636. * that the interrupt should be handled.
  9637. *
  9638. * This function returns 0 if the device or the PCI slot is in a state that
  9639. * interrupt should be handled, otherwise -EIO.
  9640. */
  9641. static inline int
  9642. lpfc_intr_state_check(struct lpfc_hba *phba)
  9643. {
  9644. /* If the pci channel is offline, ignore all the interrupts */
  9645. if (unlikely(pci_channel_offline(phba->pcidev)))
  9646. return -EIO;
  9647. /* Update device level interrupt statistics */
  9648. phba->sli.slistat.sli_intr++;
  9649. /* Ignore all interrupts during initialization. */
  9650. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9651. return -EIO;
  9652. return 0;
  9653. }
  9654. /**
  9655. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9656. * @irq: Interrupt number.
  9657. * @dev_id: The device context pointer.
  9658. *
  9659. * This function is directly called from the PCI layer as an interrupt
  9660. * service routine when device with SLI-3 interface spec is enabled with
  9661. * MSI-X multi-message interrupt mode and there are slow-path events in
  9662. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9663. * interrupt mode, this function is called as part of the device-level
  9664. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9665. * is undergoing initialization, the interrupt handler will not process
  9666. * the interrupt. The link attention and ELS ring attention events are
  9667. * handled by the worker thread. The interrupt handler signals the worker
  9668. * thread and returns for these events. This function is called without
  9669. * any lock held. It gets the hbalock to access and update SLI data
  9670. * structures.
  9671. *
  9672. * This function returns IRQ_HANDLED when interrupt is handled else it
  9673. * returns IRQ_NONE.
  9674. **/
  9675. irqreturn_t
  9676. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9677. {
  9678. struct lpfc_hba *phba;
  9679. uint32_t ha_copy, hc_copy;
  9680. uint32_t work_ha_copy;
  9681. unsigned long status;
  9682. unsigned long iflag;
  9683. uint32_t control;
  9684. MAILBOX_t *mbox, *pmbox;
  9685. struct lpfc_vport *vport;
  9686. struct lpfc_nodelist *ndlp;
  9687. struct lpfc_dmabuf *mp;
  9688. LPFC_MBOXQ_t *pmb;
  9689. int rc;
  9690. /*
  9691. * Get the driver's phba structure from the dev_id and
  9692. * assume the HBA is not interrupting.
  9693. */
  9694. phba = (struct lpfc_hba *)dev_id;
  9695. if (unlikely(!phba))
  9696. return IRQ_NONE;
  9697. /*
  9698. * Stuff needs to be attented to when this function is invoked as an
  9699. * individual interrupt handler in MSI-X multi-message interrupt mode
  9700. */
  9701. if (phba->intr_type == MSIX) {
  9702. /* Check device state for handling interrupt */
  9703. if (lpfc_intr_state_check(phba))
  9704. return IRQ_NONE;
  9705. /* Need to read HA REG for slow-path events */
  9706. spin_lock_irqsave(&phba->hbalock, iflag);
  9707. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9708. goto unplug_error;
  9709. /* If somebody is waiting to handle an eratt don't process it
  9710. * here. The brdkill function will do this.
  9711. */
  9712. if (phba->link_flag & LS_IGNORE_ERATT)
  9713. ha_copy &= ~HA_ERATT;
  9714. /* Check the need for handling ERATT in interrupt handler */
  9715. if (ha_copy & HA_ERATT) {
  9716. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9717. /* ERATT polling has handled ERATT */
  9718. ha_copy &= ~HA_ERATT;
  9719. else
  9720. /* Indicate interrupt handler handles ERATT */
  9721. phba->hba_flag |= HBA_ERATT_HANDLED;
  9722. }
  9723. /*
  9724. * If there is deferred error attention, do not check for any
  9725. * interrupt.
  9726. */
  9727. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9728. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9729. return IRQ_NONE;
  9730. }
  9731. /* Clear up only attention source related to slow-path */
  9732. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9733. goto unplug_error;
  9734. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9735. HC_LAINT_ENA | HC_ERINT_ENA),
  9736. phba->HCregaddr);
  9737. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9738. phba->HAregaddr);
  9739. writel(hc_copy, phba->HCregaddr);
  9740. readl(phba->HAregaddr); /* flush */
  9741. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9742. } else
  9743. ha_copy = phba->ha_copy;
  9744. work_ha_copy = ha_copy & phba->work_ha_mask;
  9745. if (work_ha_copy) {
  9746. if (work_ha_copy & HA_LATT) {
  9747. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9748. /*
  9749. * Turn off Link Attention interrupts
  9750. * until CLEAR_LA done
  9751. */
  9752. spin_lock_irqsave(&phba->hbalock, iflag);
  9753. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9754. if (lpfc_readl(phba->HCregaddr, &control))
  9755. goto unplug_error;
  9756. control &= ~HC_LAINT_ENA;
  9757. writel(control, phba->HCregaddr);
  9758. readl(phba->HCregaddr); /* flush */
  9759. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9760. }
  9761. else
  9762. work_ha_copy &= ~HA_LATT;
  9763. }
  9764. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9765. /*
  9766. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9767. * the only slow ring.
  9768. */
  9769. status = (work_ha_copy &
  9770. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9771. status >>= (4*LPFC_ELS_RING);
  9772. if (status & HA_RXMASK) {
  9773. spin_lock_irqsave(&phba->hbalock, iflag);
  9774. if (lpfc_readl(phba->HCregaddr, &control))
  9775. goto unplug_error;
  9776. lpfc_debugfs_slow_ring_trc(phba,
  9777. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9778. control, status,
  9779. (uint32_t)phba->sli.slistat.sli_intr);
  9780. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9781. lpfc_debugfs_slow_ring_trc(phba,
  9782. "ISR Disable ring:"
  9783. "pwork:x%x hawork:x%x wait:x%x",
  9784. phba->work_ha, work_ha_copy,
  9785. (uint32_t)((unsigned long)
  9786. &phba->work_waitq));
  9787. control &=
  9788. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9789. writel(control, phba->HCregaddr);
  9790. readl(phba->HCregaddr); /* flush */
  9791. }
  9792. else {
  9793. lpfc_debugfs_slow_ring_trc(phba,
  9794. "ISR slow ring: pwork:"
  9795. "x%x hawork:x%x wait:x%x",
  9796. phba->work_ha, work_ha_copy,
  9797. (uint32_t)((unsigned long)
  9798. &phba->work_waitq));
  9799. }
  9800. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9801. }
  9802. }
  9803. spin_lock_irqsave(&phba->hbalock, iflag);
  9804. if (work_ha_copy & HA_ERATT) {
  9805. if (lpfc_sli_read_hs(phba))
  9806. goto unplug_error;
  9807. /*
  9808. * Check if there is a deferred error condition
  9809. * is active
  9810. */
  9811. if ((HS_FFER1 & phba->work_hs) &&
  9812. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9813. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9814. phba->work_hs)) {
  9815. phba->hba_flag |= DEFER_ERATT;
  9816. /* Clear all interrupt enable conditions */
  9817. writel(0, phba->HCregaddr);
  9818. readl(phba->HCregaddr);
  9819. }
  9820. }
  9821. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9822. pmb = phba->sli.mbox_active;
  9823. pmbox = &pmb->u.mb;
  9824. mbox = phba->mbox;
  9825. vport = pmb->vport;
  9826. /* First check out the status word */
  9827. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9828. if (pmbox->mbxOwner != OWN_HOST) {
  9829. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9830. /*
  9831. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9832. * mbxStatus <status>
  9833. */
  9834. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9835. LOG_SLI,
  9836. "(%d):0304 Stray Mailbox "
  9837. "Interrupt mbxCommand x%x "
  9838. "mbxStatus x%x\n",
  9839. (vport ? vport->vpi : 0),
  9840. pmbox->mbxCommand,
  9841. pmbox->mbxStatus);
  9842. /* clear mailbox attention bit */
  9843. work_ha_copy &= ~HA_MBATT;
  9844. } else {
  9845. phba->sli.mbox_active = NULL;
  9846. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9847. phba->last_completion_time = jiffies;
  9848. del_timer(&phba->sli.mbox_tmo);
  9849. if (pmb->mbox_cmpl) {
  9850. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9851. MAILBOX_CMD_SIZE);
  9852. if (pmb->out_ext_byte_len &&
  9853. pmb->context2)
  9854. lpfc_sli_pcimem_bcopy(
  9855. phba->mbox_ext,
  9856. pmb->context2,
  9857. pmb->out_ext_byte_len);
  9858. }
  9859. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9860. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9861. lpfc_debugfs_disc_trc(vport,
  9862. LPFC_DISC_TRC_MBOX_VPORT,
  9863. "MBOX dflt rpi: : "
  9864. "status:x%x rpi:x%x",
  9865. (uint32_t)pmbox->mbxStatus,
  9866. pmbox->un.varWords[0], 0);
  9867. if (!pmbox->mbxStatus) {
  9868. mp = (struct lpfc_dmabuf *)
  9869. (pmb->context1);
  9870. ndlp = (struct lpfc_nodelist *)
  9871. pmb->context2;
  9872. /* Reg_LOGIN of dflt RPI was
  9873. * successful. new lets get
  9874. * rid of the RPI using the
  9875. * same mbox buffer.
  9876. */
  9877. lpfc_unreg_login(phba,
  9878. vport->vpi,
  9879. pmbox->un.varWords[0],
  9880. pmb);
  9881. pmb->mbox_cmpl =
  9882. lpfc_mbx_cmpl_dflt_rpi;
  9883. pmb->context1 = mp;
  9884. pmb->context2 = ndlp;
  9885. pmb->vport = vport;
  9886. rc = lpfc_sli_issue_mbox(phba,
  9887. pmb,
  9888. MBX_NOWAIT);
  9889. if (rc != MBX_BUSY)
  9890. lpfc_printf_log(phba,
  9891. KERN_ERR,
  9892. LOG_MBOX | LOG_SLI,
  9893. "0350 rc should have"
  9894. "been MBX_BUSY\n");
  9895. if (rc != MBX_NOT_FINISHED)
  9896. goto send_current_mbox;
  9897. }
  9898. }
  9899. spin_lock_irqsave(
  9900. &phba->pport->work_port_lock,
  9901. iflag);
  9902. phba->pport->work_port_events &=
  9903. ~WORKER_MBOX_TMO;
  9904. spin_unlock_irqrestore(
  9905. &phba->pport->work_port_lock,
  9906. iflag);
  9907. lpfc_mbox_cmpl_put(phba, pmb);
  9908. }
  9909. } else
  9910. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9911. if ((work_ha_copy & HA_MBATT) &&
  9912. (phba->sli.mbox_active == NULL)) {
  9913. send_current_mbox:
  9914. /* Process next mailbox command if there is one */
  9915. do {
  9916. rc = lpfc_sli_issue_mbox(phba, NULL,
  9917. MBX_NOWAIT);
  9918. } while (rc == MBX_NOT_FINISHED);
  9919. if (rc != MBX_SUCCESS)
  9920. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9921. LOG_SLI, "0349 rc should be "
  9922. "MBX_SUCCESS\n");
  9923. }
  9924. spin_lock_irqsave(&phba->hbalock, iflag);
  9925. phba->work_ha |= work_ha_copy;
  9926. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9927. lpfc_worker_wake_up(phba);
  9928. }
  9929. return IRQ_HANDLED;
  9930. unplug_error:
  9931. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9932. return IRQ_HANDLED;
  9933. } /* lpfc_sli_sp_intr_handler */
  9934. /**
  9935. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9936. * @irq: Interrupt number.
  9937. * @dev_id: The device context pointer.
  9938. *
  9939. * This function is directly called from the PCI layer as an interrupt
  9940. * service routine when device with SLI-3 interface spec is enabled with
  9941. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9942. * ring event in the HBA. However, when the device is enabled with either
  9943. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9944. * device-level interrupt handler. When the PCI slot is in error recovery
  9945. * or the HBA is undergoing initialization, the interrupt handler will not
  9946. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9947. * the intrrupt context. This function is called without any lock held.
  9948. * It gets the hbalock to access and update SLI data structures.
  9949. *
  9950. * This function returns IRQ_HANDLED when interrupt is handled else it
  9951. * returns IRQ_NONE.
  9952. **/
  9953. irqreturn_t
  9954. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9955. {
  9956. struct lpfc_hba *phba;
  9957. uint32_t ha_copy;
  9958. unsigned long status;
  9959. unsigned long iflag;
  9960. /* Get the driver's phba structure from the dev_id and
  9961. * assume the HBA is not interrupting.
  9962. */
  9963. phba = (struct lpfc_hba *) dev_id;
  9964. if (unlikely(!phba))
  9965. return IRQ_NONE;
  9966. /*
  9967. * Stuff needs to be attented to when this function is invoked as an
  9968. * individual interrupt handler in MSI-X multi-message interrupt mode
  9969. */
  9970. if (phba->intr_type == MSIX) {
  9971. /* Check device state for handling interrupt */
  9972. if (lpfc_intr_state_check(phba))
  9973. return IRQ_NONE;
  9974. /* Need to read HA REG for FCP ring and other ring events */
  9975. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9976. return IRQ_HANDLED;
  9977. /* Clear up only attention source related to fast-path */
  9978. spin_lock_irqsave(&phba->hbalock, iflag);
  9979. /*
  9980. * If there is deferred error attention, do not check for
  9981. * any interrupt.
  9982. */
  9983. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9984. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9985. return IRQ_NONE;
  9986. }
  9987. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9988. phba->HAregaddr);
  9989. readl(phba->HAregaddr); /* flush */
  9990. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9991. } else
  9992. ha_copy = phba->ha_copy;
  9993. /*
  9994. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9995. */
  9996. ha_copy &= ~(phba->work_ha_mask);
  9997. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9998. status >>= (4*LPFC_FCP_RING);
  9999. if (status & HA_RXMASK)
  10000. lpfc_sli_handle_fast_ring_event(phba,
  10001. &phba->sli.ring[LPFC_FCP_RING],
  10002. status);
  10003. if (phba->cfg_multi_ring_support == 2) {
  10004. /*
  10005. * Process all events on extra ring. Take the optimized path
  10006. * for extra ring IO.
  10007. */
  10008. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10009. status >>= (4*LPFC_EXTRA_RING);
  10010. if (status & HA_RXMASK) {
  10011. lpfc_sli_handle_fast_ring_event(phba,
  10012. &phba->sli.ring[LPFC_EXTRA_RING],
  10013. status);
  10014. }
  10015. }
  10016. return IRQ_HANDLED;
  10017. } /* lpfc_sli_fp_intr_handler */
  10018. /**
  10019. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  10020. * @irq: Interrupt number.
  10021. * @dev_id: The device context pointer.
  10022. *
  10023. * This function is the HBA device-level interrupt handler to device with
  10024. * SLI-3 interface spec, called from the PCI layer when either MSI or
  10025. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  10026. * requires driver attention. This function invokes the slow-path interrupt
  10027. * attention handling function and fast-path interrupt attention handling
  10028. * function in turn to process the relevant HBA attention events. This
  10029. * function is called without any lock held. It gets the hbalock to access
  10030. * and update SLI data structures.
  10031. *
  10032. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10033. * returns IRQ_NONE.
  10034. **/
  10035. irqreturn_t
  10036. lpfc_sli_intr_handler(int irq, void *dev_id)
  10037. {
  10038. struct lpfc_hba *phba;
  10039. irqreturn_t sp_irq_rc, fp_irq_rc;
  10040. unsigned long status1, status2;
  10041. uint32_t hc_copy;
  10042. /*
  10043. * Get the driver's phba structure from the dev_id and
  10044. * assume the HBA is not interrupting.
  10045. */
  10046. phba = (struct lpfc_hba *) dev_id;
  10047. if (unlikely(!phba))
  10048. return IRQ_NONE;
  10049. /* Check device state for handling interrupt */
  10050. if (lpfc_intr_state_check(phba))
  10051. return IRQ_NONE;
  10052. spin_lock(&phba->hbalock);
  10053. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  10054. spin_unlock(&phba->hbalock);
  10055. return IRQ_HANDLED;
  10056. }
  10057. if (unlikely(!phba->ha_copy)) {
  10058. spin_unlock(&phba->hbalock);
  10059. return IRQ_NONE;
  10060. } else if (phba->ha_copy & HA_ERATT) {
  10061. if (phba->hba_flag & HBA_ERATT_HANDLED)
  10062. /* ERATT polling has handled ERATT */
  10063. phba->ha_copy &= ~HA_ERATT;
  10064. else
  10065. /* Indicate interrupt handler handles ERATT */
  10066. phba->hba_flag |= HBA_ERATT_HANDLED;
  10067. }
  10068. /*
  10069. * If there is deferred error attention, do not check for any interrupt.
  10070. */
  10071. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10072. spin_unlock(&phba->hbalock);
  10073. return IRQ_NONE;
  10074. }
  10075. /* Clear attention sources except link and error attentions */
  10076. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  10077. spin_unlock(&phba->hbalock);
  10078. return IRQ_HANDLED;
  10079. }
  10080. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  10081. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  10082. phba->HCregaddr);
  10083. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  10084. writel(hc_copy, phba->HCregaddr);
  10085. readl(phba->HAregaddr); /* flush */
  10086. spin_unlock(&phba->hbalock);
  10087. /*
  10088. * Invokes slow-path host attention interrupt handling as appropriate.
  10089. */
  10090. /* status of events with mailbox and link attention */
  10091. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  10092. /* status of events with ELS ring */
  10093. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  10094. status2 >>= (4*LPFC_ELS_RING);
  10095. if (status1 || (status2 & HA_RXMASK))
  10096. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  10097. else
  10098. sp_irq_rc = IRQ_NONE;
  10099. /*
  10100. * Invoke fast-path host attention interrupt handling as appropriate.
  10101. */
  10102. /* status of events with FCP ring */
  10103. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  10104. status1 >>= (4*LPFC_FCP_RING);
  10105. /* status of events with extra ring */
  10106. if (phba->cfg_multi_ring_support == 2) {
  10107. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  10108. status2 >>= (4*LPFC_EXTRA_RING);
  10109. } else
  10110. status2 = 0;
  10111. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  10112. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  10113. else
  10114. fp_irq_rc = IRQ_NONE;
  10115. /* Return device-level interrupt handling status */
  10116. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  10117. } /* lpfc_sli_intr_handler */
  10118. /**
  10119. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  10120. * @phba: pointer to lpfc hba data structure.
  10121. *
  10122. * This routine is invoked by the worker thread to process all the pending
  10123. * SLI4 FCP abort XRI events.
  10124. **/
  10125. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  10126. {
  10127. struct lpfc_cq_event *cq_event;
  10128. /* First, declare the fcp xri abort event has been handled */
  10129. spin_lock_irq(&phba->hbalock);
  10130. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  10131. spin_unlock_irq(&phba->hbalock);
  10132. /* Now, handle all the fcp xri abort events */
  10133. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  10134. /* Get the first event from the head of the event queue */
  10135. spin_lock_irq(&phba->hbalock);
  10136. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  10137. cq_event, struct lpfc_cq_event, list);
  10138. spin_unlock_irq(&phba->hbalock);
  10139. /* Notify aborted XRI for FCP work queue */
  10140. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10141. /* Free the event processed back to the free pool */
  10142. lpfc_sli4_cq_event_release(phba, cq_event);
  10143. }
  10144. }
  10145. /**
  10146. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  10147. * @phba: pointer to lpfc hba data structure.
  10148. *
  10149. * This routine is invoked by the worker thread to process all the pending
  10150. * SLI4 els abort xri events.
  10151. **/
  10152. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  10153. {
  10154. struct lpfc_cq_event *cq_event;
  10155. /* First, declare the els xri abort event has been handled */
  10156. spin_lock_irq(&phba->hbalock);
  10157. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  10158. spin_unlock_irq(&phba->hbalock);
  10159. /* Now, handle all the els xri abort events */
  10160. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  10161. /* Get the first event from the head of the event queue */
  10162. spin_lock_irq(&phba->hbalock);
  10163. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  10164. cq_event, struct lpfc_cq_event, list);
  10165. spin_unlock_irq(&phba->hbalock);
  10166. /* Notify aborted XRI for ELS work queue */
  10167. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  10168. /* Free the event processed back to the free pool */
  10169. lpfc_sli4_cq_event_release(phba, cq_event);
  10170. }
  10171. }
  10172. /**
  10173. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  10174. * @phba: pointer to lpfc hba data structure
  10175. * @pIocbIn: pointer to the rspiocbq
  10176. * @pIocbOut: pointer to the cmdiocbq
  10177. * @wcqe: pointer to the complete wcqe
  10178. *
  10179. * This routine transfers the fields of a command iocbq to a response iocbq
  10180. * by copying all the IOCB fields from command iocbq and transferring the
  10181. * completion status information from the complete wcqe.
  10182. **/
  10183. static void
  10184. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  10185. struct lpfc_iocbq *pIocbIn,
  10186. struct lpfc_iocbq *pIocbOut,
  10187. struct lpfc_wcqe_complete *wcqe)
  10188. {
  10189. int numBdes, i;
  10190. unsigned long iflags;
  10191. uint32_t status, max_response;
  10192. struct lpfc_dmabuf *dmabuf;
  10193. struct ulp_bde64 *bpl, bde;
  10194. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  10195. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  10196. sizeof(struct lpfc_iocbq) - offset);
  10197. /* Map WCQE parameters into irspiocb parameters */
  10198. status = bf_get(lpfc_wcqe_c_status, wcqe);
  10199. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  10200. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  10201. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  10202. pIocbIn->iocb.un.fcpi.fcpi_parm =
  10203. pIocbOut->iocb.un.fcpi.fcpi_parm -
  10204. wcqe->total_data_placed;
  10205. else
  10206. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10207. else {
  10208. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10209. switch (pIocbOut->iocb.ulpCommand) {
  10210. case CMD_ELS_REQUEST64_CR:
  10211. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10212. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10213. bde.tus.w = le32_to_cpu(bpl[1].tus.w);
  10214. max_response = bde.tus.f.bdeSize;
  10215. break;
  10216. case CMD_GEN_REQUEST64_CR:
  10217. max_response = 0;
  10218. if (!pIocbOut->context3)
  10219. break;
  10220. numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
  10221. sizeof(struct ulp_bde64);
  10222. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  10223. bpl = (struct ulp_bde64 *)dmabuf->virt;
  10224. for (i = 0; i < numBdes; i++) {
  10225. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  10226. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  10227. max_response += bde.tus.f.bdeSize;
  10228. }
  10229. break;
  10230. default:
  10231. max_response = wcqe->total_data_placed;
  10232. break;
  10233. }
  10234. if (max_response < wcqe->total_data_placed)
  10235. pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
  10236. else
  10237. pIocbIn->iocb.un.genreq64.bdl.bdeSize =
  10238. wcqe->total_data_placed;
  10239. }
  10240. /* Convert BG errors for completion status */
  10241. if (status == CQE_STATUS_DI_ERROR) {
  10242. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  10243. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  10244. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  10245. else
  10246. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  10247. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  10248. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  10249. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10250. BGS_GUARD_ERR_MASK;
  10251. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  10252. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10253. BGS_APPTAG_ERR_MASK;
  10254. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  10255. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10256. BGS_REFTAG_ERR_MASK;
  10257. /* Check to see if there was any good data before the error */
  10258. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  10259. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10260. BGS_HI_WATER_MARK_PRESENT_MASK;
  10261. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  10262. wcqe->total_data_placed;
  10263. }
  10264. /*
  10265. * Set ALL the error bits to indicate we don't know what
  10266. * type of error it is.
  10267. */
  10268. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  10269. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10270. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  10271. BGS_GUARD_ERR_MASK);
  10272. }
  10273. /* Pick up HBA exchange busy condition */
  10274. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  10275. spin_lock_irqsave(&phba->hbalock, iflags);
  10276. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  10277. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10278. }
  10279. }
  10280. /**
  10281. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10282. * @phba: Pointer to HBA context object.
  10283. * @wcqe: Pointer to work-queue completion queue entry.
  10284. *
  10285. * This routine handles an ELS work-queue completion event and construct
  10286. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10287. * discovery engine to handle.
  10288. *
  10289. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10290. **/
  10291. static struct lpfc_iocbq *
  10292. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10293. struct lpfc_iocbq *irspiocbq)
  10294. {
  10295. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10296. struct lpfc_iocbq *cmdiocbq;
  10297. struct lpfc_wcqe_complete *wcqe;
  10298. unsigned long iflags;
  10299. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10300. spin_lock_irqsave(&pring->ring_lock, iflags);
  10301. pring->stats.iocb_event++;
  10302. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10303. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10304. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10305. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10306. if (unlikely(!cmdiocbq)) {
  10307. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10308. "0386 ELS complete with no corresponding "
  10309. "cmdiocb: iotag (%d)\n",
  10310. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10311. lpfc_sli_release_iocbq(phba, irspiocbq);
  10312. return NULL;
  10313. }
  10314. /* Fake the irspiocbq and copy necessary response information */
  10315. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10316. return irspiocbq;
  10317. }
  10318. /**
  10319. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10320. * @phba: Pointer to HBA context object.
  10321. * @cqe: Pointer to mailbox completion queue entry.
  10322. *
  10323. * This routine process a mailbox completion queue entry with asynchrous
  10324. * event.
  10325. *
  10326. * Return: true if work posted to worker thread, otherwise false.
  10327. **/
  10328. static bool
  10329. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10330. {
  10331. struct lpfc_cq_event *cq_event;
  10332. unsigned long iflags;
  10333. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10334. "0392 Async Event: word0:x%x, word1:x%x, "
  10335. "word2:x%x, word3:x%x\n", mcqe->word0,
  10336. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10337. /* Allocate a new internal CQ_EVENT entry */
  10338. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10339. if (!cq_event) {
  10340. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10341. "0394 Failed to allocate CQ_EVENT entry\n");
  10342. return false;
  10343. }
  10344. /* Move the CQE into an asynchronous event entry */
  10345. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10346. spin_lock_irqsave(&phba->hbalock, iflags);
  10347. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10348. /* Set the async event flag */
  10349. phba->hba_flag |= ASYNC_EVENT;
  10350. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10351. return true;
  10352. }
  10353. /**
  10354. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10355. * @phba: Pointer to HBA context object.
  10356. * @cqe: Pointer to mailbox completion queue entry.
  10357. *
  10358. * This routine process a mailbox completion queue entry with mailbox
  10359. * completion event.
  10360. *
  10361. * Return: true if work posted to worker thread, otherwise false.
  10362. **/
  10363. static bool
  10364. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10365. {
  10366. uint32_t mcqe_status;
  10367. MAILBOX_t *mbox, *pmbox;
  10368. struct lpfc_mqe *mqe;
  10369. struct lpfc_vport *vport;
  10370. struct lpfc_nodelist *ndlp;
  10371. struct lpfc_dmabuf *mp;
  10372. unsigned long iflags;
  10373. LPFC_MBOXQ_t *pmb;
  10374. bool workposted = false;
  10375. int rc;
  10376. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10377. if (!bf_get(lpfc_trailer_completed, mcqe))
  10378. goto out_no_mqe_complete;
  10379. /* Get the reference to the active mbox command */
  10380. spin_lock_irqsave(&phba->hbalock, iflags);
  10381. pmb = phba->sli.mbox_active;
  10382. if (unlikely(!pmb)) {
  10383. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10384. "1832 No pending MBOX command to handle\n");
  10385. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10386. goto out_no_mqe_complete;
  10387. }
  10388. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10389. mqe = &pmb->u.mqe;
  10390. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10391. mbox = phba->mbox;
  10392. vport = pmb->vport;
  10393. /* Reset heartbeat timer */
  10394. phba->last_completion_time = jiffies;
  10395. del_timer(&phba->sli.mbox_tmo);
  10396. /* Move mbox data to caller's mailbox region, do endian swapping */
  10397. if (pmb->mbox_cmpl && mbox)
  10398. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10399. /*
  10400. * For mcqe errors, conditionally move a modified error code to
  10401. * the mbox so that the error will not be missed.
  10402. */
  10403. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10404. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10405. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10406. bf_set(lpfc_mqe_status, mqe,
  10407. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10408. }
  10409. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10410. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10411. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10412. "MBOX dflt rpi: status:x%x rpi:x%x",
  10413. mcqe_status,
  10414. pmbox->un.varWords[0], 0);
  10415. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10416. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10417. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10418. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10419. * RID of the PPI using the same mbox buffer.
  10420. */
  10421. lpfc_unreg_login(phba, vport->vpi,
  10422. pmbox->un.varWords[0], pmb);
  10423. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10424. pmb->context1 = mp;
  10425. pmb->context2 = ndlp;
  10426. pmb->vport = vport;
  10427. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10428. if (rc != MBX_BUSY)
  10429. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10430. LOG_SLI, "0385 rc should "
  10431. "have been MBX_BUSY\n");
  10432. if (rc != MBX_NOT_FINISHED)
  10433. goto send_current_mbox;
  10434. }
  10435. }
  10436. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10437. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10438. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10439. /* There is mailbox completion work to do */
  10440. spin_lock_irqsave(&phba->hbalock, iflags);
  10441. __lpfc_mbox_cmpl_put(phba, pmb);
  10442. phba->work_ha |= HA_MBATT;
  10443. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10444. workposted = true;
  10445. send_current_mbox:
  10446. spin_lock_irqsave(&phba->hbalock, iflags);
  10447. /* Release the mailbox command posting token */
  10448. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10449. /* Setting active mailbox pointer need to be in sync to flag clear */
  10450. phba->sli.mbox_active = NULL;
  10451. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10452. /* Wake up worker thread to post the next pending mailbox command */
  10453. lpfc_worker_wake_up(phba);
  10454. out_no_mqe_complete:
  10455. if (bf_get(lpfc_trailer_consumed, mcqe))
  10456. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10457. return workposted;
  10458. }
  10459. /**
  10460. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10461. * @phba: Pointer to HBA context object.
  10462. * @cqe: Pointer to mailbox completion queue entry.
  10463. *
  10464. * This routine process a mailbox completion queue entry, it invokes the
  10465. * proper mailbox complete handling or asynchrous event handling routine
  10466. * according to the MCQE's async bit.
  10467. *
  10468. * Return: true if work posted to worker thread, otherwise false.
  10469. **/
  10470. static bool
  10471. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10472. {
  10473. struct lpfc_mcqe mcqe;
  10474. bool workposted;
  10475. /* Copy the mailbox MCQE and convert endian order as needed */
  10476. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10477. /* Invoke the proper event handling routine */
  10478. if (!bf_get(lpfc_trailer_async, &mcqe))
  10479. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10480. else
  10481. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10482. return workposted;
  10483. }
  10484. /**
  10485. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10486. * @phba: Pointer to HBA context object.
  10487. * @cq: Pointer to associated CQ
  10488. * @wcqe: Pointer to work-queue completion queue entry.
  10489. *
  10490. * This routine handles an ELS work-queue completion event.
  10491. *
  10492. * Return: true if work posted to worker thread, otherwise false.
  10493. **/
  10494. static bool
  10495. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10496. struct lpfc_wcqe_complete *wcqe)
  10497. {
  10498. struct lpfc_iocbq *irspiocbq;
  10499. unsigned long iflags;
  10500. struct lpfc_sli_ring *pring = cq->pring;
  10501. int txq_cnt = 0;
  10502. int txcmplq_cnt = 0;
  10503. int fcp_txcmplq_cnt = 0;
  10504. /* Get an irspiocbq for later ELS response processing use */
  10505. irspiocbq = lpfc_sli_get_iocbq(phba);
  10506. if (!irspiocbq) {
  10507. if (!list_empty(&pring->txq))
  10508. txq_cnt++;
  10509. if (!list_empty(&pring->txcmplq))
  10510. txcmplq_cnt++;
  10511. if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
  10512. fcp_txcmplq_cnt++;
  10513. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10514. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10515. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10516. txq_cnt, phba->iocb_cnt,
  10517. fcp_txcmplq_cnt,
  10518. txcmplq_cnt);
  10519. return false;
  10520. }
  10521. /* Save off the slow-path queue event for work thread to process */
  10522. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10523. spin_lock_irqsave(&phba->hbalock, iflags);
  10524. list_add_tail(&irspiocbq->cq_event.list,
  10525. &phba->sli4_hba.sp_queue_event);
  10526. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10527. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10528. return true;
  10529. }
  10530. /**
  10531. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10532. * @phba: Pointer to HBA context object.
  10533. * @wcqe: Pointer to work-queue completion queue entry.
  10534. *
  10535. * This routine handles slow-path WQ entry comsumed event by invoking the
  10536. * proper WQ release routine to the slow-path WQ.
  10537. **/
  10538. static void
  10539. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10540. struct lpfc_wcqe_release *wcqe)
  10541. {
  10542. /* sanity check on queue memory */
  10543. if (unlikely(!phba->sli4_hba.els_wq))
  10544. return;
  10545. /* Check for the slow-path ELS work queue */
  10546. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10547. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10548. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10549. else
  10550. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10551. "2579 Slow-path wqe consume event carries "
  10552. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10553. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10554. phba->sli4_hba.els_wq->queue_id);
  10555. }
  10556. /**
  10557. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10558. * @phba: Pointer to HBA context object.
  10559. * @cq: Pointer to a WQ completion queue.
  10560. * @wcqe: Pointer to work-queue completion queue entry.
  10561. *
  10562. * This routine handles an XRI abort event.
  10563. *
  10564. * Return: true if work posted to worker thread, otherwise false.
  10565. **/
  10566. static bool
  10567. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10568. struct lpfc_queue *cq,
  10569. struct sli4_wcqe_xri_aborted *wcqe)
  10570. {
  10571. bool workposted = false;
  10572. struct lpfc_cq_event *cq_event;
  10573. unsigned long iflags;
  10574. /* Allocate a new internal CQ_EVENT entry */
  10575. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10576. if (!cq_event) {
  10577. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10578. "0602 Failed to allocate CQ_EVENT entry\n");
  10579. return false;
  10580. }
  10581. /* Move the CQE into the proper xri abort event list */
  10582. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10583. switch (cq->subtype) {
  10584. case LPFC_FCP:
  10585. spin_lock_irqsave(&phba->hbalock, iflags);
  10586. list_add_tail(&cq_event->list,
  10587. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10588. /* Set the fcp xri abort event flag */
  10589. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10590. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10591. workposted = true;
  10592. break;
  10593. case LPFC_ELS:
  10594. spin_lock_irqsave(&phba->hbalock, iflags);
  10595. list_add_tail(&cq_event->list,
  10596. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10597. /* Set the els xri abort event flag */
  10598. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10599. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10600. workposted = true;
  10601. break;
  10602. default:
  10603. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10604. "0603 Invalid work queue CQE subtype (x%x)\n",
  10605. cq->subtype);
  10606. workposted = false;
  10607. break;
  10608. }
  10609. return workposted;
  10610. }
  10611. /**
  10612. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10613. * @phba: Pointer to HBA context object.
  10614. * @rcqe: Pointer to receive-queue completion queue entry.
  10615. *
  10616. * This routine process a receive-queue completion queue entry.
  10617. *
  10618. * Return: true if work posted to worker thread, otherwise false.
  10619. **/
  10620. static bool
  10621. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10622. {
  10623. bool workposted = false;
  10624. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10625. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10626. struct hbq_dmabuf *dma_buf;
  10627. uint32_t status, rq_id;
  10628. unsigned long iflags;
  10629. /* sanity check on queue memory */
  10630. if (unlikely(!hrq) || unlikely(!drq))
  10631. return workposted;
  10632. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10633. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10634. else
  10635. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10636. if (rq_id != hrq->queue_id)
  10637. goto out;
  10638. status = bf_get(lpfc_rcqe_status, rcqe);
  10639. switch (status) {
  10640. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10641. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10642. "2537 Receive Frame Truncated!!\n");
  10643. hrq->RQ_buf_trunc++;
  10644. case FC_STATUS_RQ_SUCCESS:
  10645. lpfc_sli4_rq_release(hrq, drq);
  10646. spin_lock_irqsave(&phba->hbalock, iflags);
  10647. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10648. if (!dma_buf) {
  10649. hrq->RQ_no_buf_found++;
  10650. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10651. goto out;
  10652. }
  10653. hrq->RQ_rcv_buf++;
  10654. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10655. /* save off the frame for the word thread to process */
  10656. list_add_tail(&dma_buf->cq_event.list,
  10657. &phba->sli4_hba.sp_queue_event);
  10658. /* Frame received */
  10659. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10660. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10661. workposted = true;
  10662. break;
  10663. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10664. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10665. hrq->RQ_no_posted_buf++;
  10666. /* Post more buffers if possible */
  10667. spin_lock_irqsave(&phba->hbalock, iflags);
  10668. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10669. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10670. workposted = true;
  10671. break;
  10672. }
  10673. out:
  10674. return workposted;
  10675. }
  10676. /**
  10677. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10678. * @phba: Pointer to HBA context object.
  10679. * @cq: Pointer to the completion queue.
  10680. * @wcqe: Pointer to a completion queue entry.
  10681. *
  10682. * This routine process a slow-path work-queue or receive queue completion queue
  10683. * entry.
  10684. *
  10685. * Return: true if work posted to worker thread, otherwise false.
  10686. **/
  10687. static bool
  10688. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10689. struct lpfc_cqe *cqe)
  10690. {
  10691. struct lpfc_cqe cqevt;
  10692. bool workposted = false;
  10693. /* Copy the work queue CQE and convert endian order if needed */
  10694. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10695. /* Check and process for different type of WCQE and dispatch */
  10696. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10697. case CQE_CODE_COMPL_WQE:
  10698. /* Process the WQ/RQ complete event */
  10699. phba->last_completion_time = jiffies;
  10700. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  10701. (struct lpfc_wcqe_complete *)&cqevt);
  10702. break;
  10703. case CQE_CODE_RELEASE_WQE:
  10704. /* Process the WQ release event */
  10705. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10706. (struct lpfc_wcqe_release *)&cqevt);
  10707. break;
  10708. case CQE_CODE_XRI_ABORTED:
  10709. /* Process the WQ XRI abort event */
  10710. phba->last_completion_time = jiffies;
  10711. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10712. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10713. break;
  10714. case CQE_CODE_RECEIVE:
  10715. case CQE_CODE_RECEIVE_V1:
  10716. /* Process the RQ event */
  10717. phba->last_completion_time = jiffies;
  10718. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10719. (struct lpfc_rcqe *)&cqevt);
  10720. break;
  10721. default:
  10722. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10723. "0388 Not a valid WCQE code: x%x\n",
  10724. bf_get(lpfc_cqe_code, &cqevt));
  10725. break;
  10726. }
  10727. return workposted;
  10728. }
  10729. /**
  10730. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10731. * @phba: Pointer to HBA context object.
  10732. * @eqe: Pointer to fast-path event queue entry.
  10733. *
  10734. * This routine process a event queue entry from the slow-path event queue.
  10735. * It will check the MajorCode and MinorCode to determine this is for a
  10736. * completion event on a completion queue, if not, an error shall be logged
  10737. * and just return. Otherwise, it will get to the corresponding completion
  10738. * queue and process all the entries on that completion queue, rearm the
  10739. * completion queue, and then return.
  10740. *
  10741. **/
  10742. static void
  10743. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10744. struct lpfc_queue *speq)
  10745. {
  10746. struct lpfc_queue *cq = NULL, *childq;
  10747. struct lpfc_cqe *cqe;
  10748. bool workposted = false;
  10749. int ecount = 0;
  10750. uint16_t cqid;
  10751. /* Get the reference to the corresponding CQ */
  10752. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10753. list_for_each_entry(childq, &speq->child_list, list) {
  10754. if (childq->queue_id == cqid) {
  10755. cq = childq;
  10756. break;
  10757. }
  10758. }
  10759. if (unlikely(!cq)) {
  10760. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10761. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10762. "0365 Slow-path CQ identifier "
  10763. "(%d) does not exist\n", cqid);
  10764. return;
  10765. }
  10766. /* Process all the entries to the CQ */
  10767. switch (cq->type) {
  10768. case LPFC_MCQ:
  10769. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10770. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10771. if (!(++ecount % cq->entry_repost))
  10772. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10773. cq->CQ_mbox++;
  10774. }
  10775. break;
  10776. case LPFC_WCQ:
  10777. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10778. if (cq->subtype == LPFC_FCP)
  10779. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10780. cqe);
  10781. else
  10782. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10783. cqe);
  10784. if (!(++ecount % cq->entry_repost))
  10785. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10786. }
  10787. /* Track the max number of CQEs processed in 1 EQ */
  10788. if (ecount > cq->CQ_max_cqe)
  10789. cq->CQ_max_cqe = ecount;
  10790. break;
  10791. default:
  10792. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10793. "0370 Invalid completion queue type (%d)\n",
  10794. cq->type);
  10795. return;
  10796. }
  10797. /* Catch the no cq entry condition, log an error */
  10798. if (unlikely(ecount == 0))
  10799. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10800. "0371 No entry from the CQ: identifier "
  10801. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10802. /* In any case, flash and re-arm the RCQ */
  10803. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10804. /* wake up worker thread if there are works to be done */
  10805. if (workposted)
  10806. lpfc_worker_wake_up(phba);
  10807. }
  10808. /**
  10809. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10810. * @phba: Pointer to HBA context object.
  10811. * @cq: Pointer to associated CQ
  10812. * @wcqe: Pointer to work-queue completion queue entry.
  10813. *
  10814. * This routine process a fast-path work queue completion entry from fast-path
  10815. * event queue for FCP command response completion.
  10816. **/
  10817. static void
  10818. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10819. struct lpfc_wcqe_complete *wcqe)
  10820. {
  10821. struct lpfc_sli_ring *pring = cq->pring;
  10822. struct lpfc_iocbq *cmdiocbq;
  10823. struct lpfc_iocbq irspiocbq;
  10824. unsigned long iflags;
  10825. /* Check for response status */
  10826. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10827. /* If resource errors reported from HBA, reduce queue
  10828. * depth of the SCSI device.
  10829. */
  10830. if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10831. IOSTAT_LOCAL_REJECT)) &&
  10832. ((wcqe->parameter & IOERR_PARAM_MASK) ==
  10833. IOERR_NO_RESOURCES))
  10834. phba->lpfc_rampdown_queue_depth(phba);
  10835. /* Log the error status */
  10836. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10837. "0373 FCP complete error: status=x%x, "
  10838. "hw_status=x%x, total_data_specified=%d, "
  10839. "parameter=x%x, word3=x%x\n",
  10840. bf_get(lpfc_wcqe_c_status, wcqe),
  10841. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10842. wcqe->total_data_placed, wcqe->parameter,
  10843. wcqe->word3);
  10844. }
  10845. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10846. spin_lock_irqsave(&pring->ring_lock, iflags);
  10847. pring->stats.iocb_event++;
  10848. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10849. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10850. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10851. if (unlikely(!cmdiocbq)) {
  10852. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10853. "0374 FCP complete with no corresponding "
  10854. "cmdiocb: iotag (%d)\n",
  10855. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10856. return;
  10857. }
  10858. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10859. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10860. "0375 FCP cmdiocb not callback function "
  10861. "iotag: (%d)\n",
  10862. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10863. return;
  10864. }
  10865. /* Fake the irspiocb and copy necessary response information */
  10866. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10867. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10868. spin_lock_irqsave(&phba->hbalock, iflags);
  10869. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10870. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10871. }
  10872. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10873. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10874. }
  10875. /**
  10876. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10877. * @phba: Pointer to HBA context object.
  10878. * @cq: Pointer to completion queue.
  10879. * @wcqe: Pointer to work-queue completion queue entry.
  10880. *
  10881. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10882. * proper WQ release routine to the slow-path WQ.
  10883. **/
  10884. static void
  10885. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10886. struct lpfc_wcqe_release *wcqe)
  10887. {
  10888. struct lpfc_queue *childwq;
  10889. bool wqid_matched = false;
  10890. uint16_t fcp_wqid;
  10891. /* Check for fast-path FCP work queue release */
  10892. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10893. list_for_each_entry(childwq, &cq->child_list, list) {
  10894. if (childwq->queue_id == fcp_wqid) {
  10895. lpfc_sli4_wq_release(childwq,
  10896. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10897. wqid_matched = true;
  10898. break;
  10899. }
  10900. }
  10901. /* Report warning log message if no match found */
  10902. if (wqid_matched != true)
  10903. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10904. "2580 Fast-path wqe consume event carries "
  10905. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10906. }
  10907. /**
  10908. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10909. * @cq: Pointer to the completion queue.
  10910. * @eqe: Pointer to fast-path completion queue entry.
  10911. *
  10912. * This routine process a fast-path work queue completion entry from fast-path
  10913. * event queue for FCP command response completion.
  10914. **/
  10915. static int
  10916. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10917. struct lpfc_cqe *cqe)
  10918. {
  10919. struct lpfc_wcqe_release wcqe;
  10920. bool workposted = false;
  10921. /* Copy the work queue CQE and convert endian order if needed */
  10922. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10923. /* Check and process for different type of WCQE and dispatch */
  10924. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10925. case CQE_CODE_COMPL_WQE:
  10926. cq->CQ_wq++;
  10927. /* Process the WQ complete event */
  10928. phba->last_completion_time = jiffies;
  10929. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  10930. (struct lpfc_wcqe_complete *)&wcqe);
  10931. break;
  10932. case CQE_CODE_RELEASE_WQE:
  10933. cq->CQ_release_wqe++;
  10934. /* Process the WQ release event */
  10935. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10936. (struct lpfc_wcqe_release *)&wcqe);
  10937. break;
  10938. case CQE_CODE_XRI_ABORTED:
  10939. cq->CQ_xri_aborted++;
  10940. /* Process the WQ XRI abort event */
  10941. phba->last_completion_time = jiffies;
  10942. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10943. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10944. break;
  10945. default:
  10946. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10947. "0144 Not a valid WCQE code: x%x\n",
  10948. bf_get(lpfc_wcqe_c_code, &wcqe));
  10949. break;
  10950. }
  10951. return workposted;
  10952. }
  10953. /**
  10954. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  10955. * @phba: Pointer to HBA context object.
  10956. * @eqe: Pointer to fast-path event queue entry.
  10957. *
  10958. * This routine process a event queue entry from the fast-path event queue.
  10959. * It will check the MajorCode and MinorCode to determine this is for a
  10960. * completion event on a completion queue, if not, an error shall be logged
  10961. * and just return. Otherwise, it will get to the corresponding completion
  10962. * queue and process all the entries on the completion queue, rearm the
  10963. * completion queue, and then return.
  10964. **/
  10965. static void
  10966. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10967. uint32_t qidx)
  10968. {
  10969. struct lpfc_queue *cq;
  10970. struct lpfc_cqe *cqe;
  10971. bool workposted = false;
  10972. uint16_t cqid;
  10973. int ecount = 0;
  10974. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10975. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10976. "0366 Not a valid completion "
  10977. "event: majorcode=x%x, minorcode=x%x\n",
  10978. bf_get_le32(lpfc_eqe_major_code, eqe),
  10979. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10980. return;
  10981. }
  10982. /* Get the reference to the corresponding CQ */
  10983. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10984. /* Check if this is a Slow path event */
  10985. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  10986. lpfc_sli4_sp_handle_eqe(phba, eqe,
  10987. phba->sli4_hba.hba_eq[qidx]);
  10988. return;
  10989. }
  10990. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10991. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10992. "3146 Fast-path completion queues "
  10993. "does not exist\n");
  10994. return;
  10995. }
  10996. cq = phba->sli4_hba.fcp_cq[qidx];
  10997. if (unlikely(!cq)) {
  10998. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10999. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11000. "0367 Fast-path completion queue "
  11001. "(%d) does not exist\n", qidx);
  11002. return;
  11003. }
  11004. if (unlikely(cqid != cq->queue_id)) {
  11005. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11006. "0368 Miss-matched fast-path completion "
  11007. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  11008. cqid, cq->queue_id);
  11009. return;
  11010. }
  11011. /* Process all the entries to the CQ */
  11012. while ((cqe = lpfc_sli4_cq_get(cq))) {
  11013. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  11014. if (!(++ecount % cq->entry_repost))
  11015. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  11016. }
  11017. /* Track the max number of CQEs processed in 1 EQ */
  11018. if (ecount > cq->CQ_max_cqe)
  11019. cq->CQ_max_cqe = ecount;
  11020. /* Catch the no cq entry condition */
  11021. if (unlikely(ecount == 0))
  11022. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11023. "0369 No entry from fast-path completion "
  11024. "queue fcpcqid=%d\n", cq->queue_id);
  11025. /* In any case, flash and re-arm the CQ */
  11026. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  11027. /* wake up worker thread if there are works to be done */
  11028. if (workposted)
  11029. lpfc_worker_wake_up(phba);
  11030. }
  11031. static void
  11032. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11033. {
  11034. struct lpfc_eqe *eqe;
  11035. /* walk all the EQ entries and drop on the floor */
  11036. while ((eqe = lpfc_sli4_eq_get(eq)))
  11037. ;
  11038. /* Clear and re-arm the EQ */
  11039. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  11040. }
  11041. /**
  11042. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  11043. * @irq: Interrupt number.
  11044. * @dev_id: The device context pointer.
  11045. *
  11046. * This function is directly called from the PCI layer as an interrupt
  11047. * service routine when device with SLI-4 interface spec is enabled with
  11048. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  11049. * ring event in the HBA. However, when the device is enabled with either
  11050. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11051. * device-level interrupt handler. When the PCI slot is in error recovery
  11052. * or the HBA is undergoing initialization, the interrupt handler will not
  11053. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  11054. * the intrrupt context. This function is called without any lock held.
  11055. * It gets the hbalock to access and update SLI data structures. Note that,
  11056. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  11057. * equal to that of FCP CQ index.
  11058. *
  11059. * The link attention and ELS ring attention events are handled
  11060. * by the worker thread. The interrupt handler signals the worker thread
  11061. * and returns for these events. This function is called without any lock
  11062. * held. It gets the hbalock to access and update SLI data structures.
  11063. *
  11064. * This function returns IRQ_HANDLED when interrupt is handled else it
  11065. * returns IRQ_NONE.
  11066. **/
  11067. irqreturn_t
  11068. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  11069. {
  11070. struct lpfc_hba *phba;
  11071. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  11072. struct lpfc_queue *fpeq;
  11073. struct lpfc_eqe *eqe;
  11074. unsigned long iflag;
  11075. int ecount = 0;
  11076. int fcp_eqidx;
  11077. /* Get the driver's phba structure from the dev_id */
  11078. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  11079. phba = fcp_eq_hdl->phba;
  11080. fcp_eqidx = fcp_eq_hdl->idx;
  11081. if (unlikely(!phba))
  11082. return IRQ_NONE;
  11083. if (unlikely(!phba->sli4_hba.hba_eq))
  11084. return IRQ_NONE;
  11085. /* Get to the EQ struct associated with this vector */
  11086. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11087. if (unlikely(!fpeq))
  11088. return IRQ_NONE;
  11089. if (lpfc_fcp_look_ahead) {
  11090. if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
  11091. lpfc_sli4_eq_clr_intr(fpeq);
  11092. else {
  11093. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11094. return IRQ_NONE;
  11095. }
  11096. }
  11097. /* Check device state for handling interrupt */
  11098. if (unlikely(lpfc_intr_state_check(phba))) {
  11099. fpeq->EQ_badstate++;
  11100. /* Check again for link_state with lock held */
  11101. spin_lock_irqsave(&phba->hbalock, iflag);
  11102. if (phba->link_state < LPFC_LINK_DOWN)
  11103. /* Flush, clear interrupt, and rearm the EQ */
  11104. lpfc_sli4_eq_flush(phba, fpeq);
  11105. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11106. if (lpfc_fcp_look_ahead)
  11107. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11108. return IRQ_NONE;
  11109. }
  11110. /*
  11111. * Process all the event on FCP fast-path EQ
  11112. */
  11113. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  11114. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  11115. if (!(++ecount % fpeq->entry_repost))
  11116. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  11117. fpeq->EQ_processed++;
  11118. }
  11119. /* Track the max number of EQEs processed in 1 intr */
  11120. if (ecount > fpeq->EQ_max_eqe)
  11121. fpeq->EQ_max_eqe = ecount;
  11122. /* Always clear and re-arm the fast-path EQ */
  11123. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  11124. if (unlikely(ecount == 0)) {
  11125. fpeq->EQ_no_entry++;
  11126. if (lpfc_fcp_look_ahead) {
  11127. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11128. return IRQ_NONE;
  11129. }
  11130. if (phba->intr_type == MSIX)
  11131. /* MSI-X treated interrupt served as no EQ share INT */
  11132. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11133. "0358 MSI-X interrupt with no EQE\n");
  11134. else
  11135. /* Non MSI-X treated on interrupt as EQ share INT */
  11136. return IRQ_NONE;
  11137. }
  11138. if (lpfc_fcp_look_ahead)
  11139. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  11140. return IRQ_HANDLED;
  11141. } /* lpfc_sli4_fp_intr_handler */
  11142. /**
  11143. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  11144. * @irq: Interrupt number.
  11145. * @dev_id: The device context pointer.
  11146. *
  11147. * This function is the device-level interrupt handler to device with SLI-4
  11148. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  11149. * interrupt mode is enabled and there is an event in the HBA which requires
  11150. * driver attention. This function invokes the slow-path interrupt attention
  11151. * handling function and fast-path interrupt attention handling function in
  11152. * turn to process the relevant HBA attention events. This function is called
  11153. * without any lock held. It gets the hbalock to access and update SLI data
  11154. * structures.
  11155. *
  11156. * This function returns IRQ_HANDLED when interrupt is handled, else it
  11157. * returns IRQ_NONE.
  11158. **/
  11159. irqreturn_t
  11160. lpfc_sli4_intr_handler(int irq, void *dev_id)
  11161. {
  11162. struct lpfc_hba *phba;
  11163. irqreturn_t hba_irq_rc;
  11164. bool hba_handled = false;
  11165. int fcp_eqidx;
  11166. /* Get the driver's phba structure from the dev_id */
  11167. phba = (struct lpfc_hba *)dev_id;
  11168. if (unlikely(!phba))
  11169. return IRQ_NONE;
  11170. /*
  11171. * Invoke fast-path host attention interrupt handling as appropriate.
  11172. */
  11173. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  11174. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  11175. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  11176. if (hba_irq_rc == IRQ_HANDLED)
  11177. hba_handled |= true;
  11178. }
  11179. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  11180. } /* lpfc_sli4_intr_handler */
  11181. /**
  11182. * lpfc_sli4_queue_free - free a queue structure and associated memory
  11183. * @queue: The queue structure to free.
  11184. *
  11185. * This function frees a queue structure and the DMAable memory used for
  11186. * the host resident queue. This function must be called after destroying the
  11187. * queue on the HBA.
  11188. **/
  11189. void
  11190. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  11191. {
  11192. struct lpfc_dmabuf *dmabuf;
  11193. if (!queue)
  11194. return;
  11195. while (!list_empty(&queue->page_list)) {
  11196. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  11197. list);
  11198. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  11199. dmabuf->virt, dmabuf->phys);
  11200. kfree(dmabuf);
  11201. }
  11202. kfree(queue);
  11203. return;
  11204. }
  11205. /**
  11206. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  11207. * @phba: The HBA that this queue is being created on.
  11208. * @entry_size: The size of each queue entry for this queue.
  11209. * @entry count: The number of entries that this queue will handle.
  11210. *
  11211. * This function allocates a queue structure and the DMAable memory used for
  11212. * the host resident queue. This function must be called before creating the
  11213. * queue on the HBA.
  11214. **/
  11215. struct lpfc_queue *
  11216. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  11217. uint32_t entry_count)
  11218. {
  11219. struct lpfc_queue *queue;
  11220. struct lpfc_dmabuf *dmabuf;
  11221. int x, total_qe_count;
  11222. void *dma_pointer;
  11223. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11224. if (!phba->sli4_hba.pc_sli4_params.supported)
  11225. hw_page_size = SLI4_PAGE_SIZE;
  11226. queue = kzalloc(sizeof(struct lpfc_queue) +
  11227. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  11228. if (!queue)
  11229. return NULL;
  11230. queue->page_count = (ALIGN(entry_size * entry_count,
  11231. hw_page_size))/hw_page_size;
  11232. INIT_LIST_HEAD(&queue->list);
  11233. INIT_LIST_HEAD(&queue->page_list);
  11234. INIT_LIST_HEAD(&queue->child_list);
  11235. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  11236. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  11237. if (!dmabuf)
  11238. goto out_fail;
  11239. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  11240. hw_page_size, &dmabuf->phys,
  11241. GFP_KERNEL);
  11242. if (!dmabuf->virt) {
  11243. kfree(dmabuf);
  11244. goto out_fail;
  11245. }
  11246. memset(dmabuf->virt, 0, hw_page_size);
  11247. dmabuf->buffer_tag = x;
  11248. list_add_tail(&dmabuf->list, &queue->page_list);
  11249. /* initialize queue's entry array */
  11250. dma_pointer = dmabuf->virt;
  11251. for (; total_qe_count < entry_count &&
  11252. dma_pointer < (hw_page_size + dmabuf->virt);
  11253. total_qe_count++, dma_pointer += entry_size) {
  11254. queue->qe[total_qe_count].address = dma_pointer;
  11255. }
  11256. }
  11257. queue->entry_size = entry_size;
  11258. queue->entry_count = entry_count;
  11259. /*
  11260. * entry_repost is calculated based on the number of entries in the
  11261. * queue. This works out except for RQs. If buffers are NOT initially
  11262. * posted for every RQE, entry_repost should be adjusted accordingly.
  11263. */
  11264. queue->entry_repost = (entry_count >> 3);
  11265. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11266. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11267. queue->phba = phba;
  11268. return queue;
  11269. out_fail:
  11270. lpfc_sli4_queue_free(queue);
  11271. return NULL;
  11272. }
  11273. /**
  11274. * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
  11275. * @phba: HBA structure that indicates port to create a queue on.
  11276. * @pci_barset: PCI BAR set flag.
  11277. *
  11278. * This function shall perform iomap of the specified PCI BAR address to host
  11279. * memory address if not already done so and return it. The returned host
  11280. * memory address can be NULL.
  11281. */
  11282. static void __iomem *
  11283. lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
  11284. {
  11285. struct pci_dev *pdev;
  11286. if (!phba->pcidev)
  11287. return NULL;
  11288. else
  11289. pdev = phba->pcidev;
  11290. switch (pci_barset) {
  11291. case WQ_PCI_BAR_0_AND_1:
  11292. return phba->pci_bar0_memmap_p;
  11293. case WQ_PCI_BAR_2_AND_3:
  11294. return phba->pci_bar2_memmap_p;
  11295. case WQ_PCI_BAR_4_AND_5:
  11296. return phba->pci_bar4_memmap_p;
  11297. default:
  11298. break;
  11299. }
  11300. return NULL;
  11301. }
  11302. /**
  11303. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11304. * @phba: HBA structure that indicates port to create a queue on.
  11305. * @startq: The starting FCP EQ to modify
  11306. *
  11307. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11308. *
  11309. * The @phba struct is used to send mailbox command to HBA. The @startq
  11310. * is used to get the starting FCP EQ to change.
  11311. * This function is asynchronous and will wait for the mailbox
  11312. * command to finish before continuing.
  11313. *
  11314. * On success this function will return a zero. If unable to allocate enough
  11315. * memory this function will return -ENOMEM. If the queue create mailbox command
  11316. * fails this function will return -ENXIO.
  11317. **/
  11318. uint32_t
  11319. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  11320. {
  11321. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11322. LPFC_MBOXQ_t *mbox;
  11323. struct lpfc_queue *eq;
  11324. int cnt, rc, length, status = 0;
  11325. uint32_t shdr_status, shdr_add_status;
  11326. uint32_t result;
  11327. int fcp_eqidx;
  11328. union lpfc_sli4_cfg_shdr *shdr;
  11329. uint16_t dmult;
  11330. if (startq >= phba->cfg_fcp_io_channel)
  11331. return 0;
  11332. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11333. if (!mbox)
  11334. return -ENOMEM;
  11335. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11336. sizeof(struct lpfc_sli4_cfg_mhdr));
  11337. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11338. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11339. length, LPFC_SLI4_MBX_EMBED);
  11340. eq_delay = &mbox->u.mqe.un.eq_delay;
  11341. /* Calculate delay multiper from maximum interrupt per second */
  11342. result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11343. if (result > LPFC_DMULT_CONST)
  11344. dmult = 0;
  11345. else
  11346. dmult = LPFC_DMULT_CONST/result - 1;
  11347. cnt = 0;
  11348. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11349. fcp_eqidx++) {
  11350. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11351. if (!eq)
  11352. continue;
  11353. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11354. eq_delay->u.request.eq[cnt].phase = 0;
  11355. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11356. cnt++;
  11357. if (cnt >= LPFC_MAX_EQ_DELAY)
  11358. break;
  11359. }
  11360. eq_delay->u.request.num_eq = cnt;
  11361. mbox->vport = phba->pport;
  11362. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11363. mbox->context1 = NULL;
  11364. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11365. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11366. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11367. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11368. if (shdr_status || shdr_add_status || rc) {
  11369. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11370. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11371. "status x%x add_status x%x, mbx status x%x\n",
  11372. shdr_status, shdr_add_status, rc);
  11373. status = -ENXIO;
  11374. }
  11375. mempool_free(mbox, phba->mbox_mem_pool);
  11376. return status;
  11377. }
  11378. /**
  11379. * lpfc_eq_create - Create an Event Queue on the HBA
  11380. * @phba: HBA structure that indicates port to create a queue on.
  11381. * @eq: The queue structure to use to create the event queue.
  11382. * @imax: The maximum interrupt per second limit.
  11383. *
  11384. * This function creates an event queue, as detailed in @eq, on a port,
  11385. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11386. *
  11387. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11388. * is used to get the entry count and entry size that are necessary to
  11389. * determine the number of pages to allocate and use for this queue. This
  11390. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11391. * event queue. This function is asynchronous and will wait for the mailbox
  11392. * command to finish before continuing.
  11393. *
  11394. * On success this function will return a zero. If unable to allocate enough
  11395. * memory this function will return -ENOMEM. If the queue create mailbox command
  11396. * fails this function will return -ENXIO.
  11397. **/
  11398. uint32_t
  11399. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
  11400. {
  11401. struct lpfc_mbx_eq_create *eq_create;
  11402. LPFC_MBOXQ_t *mbox;
  11403. int rc, length, status = 0;
  11404. struct lpfc_dmabuf *dmabuf;
  11405. uint32_t shdr_status, shdr_add_status;
  11406. union lpfc_sli4_cfg_shdr *shdr;
  11407. uint16_t dmult;
  11408. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11409. /* sanity check on queue memory */
  11410. if (!eq)
  11411. return -ENODEV;
  11412. if (!phba->sli4_hba.pc_sli4_params.supported)
  11413. hw_page_size = SLI4_PAGE_SIZE;
  11414. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11415. if (!mbox)
  11416. return -ENOMEM;
  11417. length = (sizeof(struct lpfc_mbx_eq_create) -
  11418. sizeof(struct lpfc_sli4_cfg_mhdr));
  11419. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11420. LPFC_MBOX_OPCODE_EQ_CREATE,
  11421. length, LPFC_SLI4_MBX_EMBED);
  11422. eq_create = &mbox->u.mqe.un.eq_create;
  11423. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11424. eq->page_count);
  11425. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11426. LPFC_EQE_SIZE);
  11427. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11428. /* Calculate delay multiper from maximum interrupt per second */
  11429. if (imax > LPFC_DMULT_CONST)
  11430. dmult = 0;
  11431. else
  11432. dmult = LPFC_DMULT_CONST/imax - 1;
  11433. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11434. dmult);
  11435. switch (eq->entry_count) {
  11436. default:
  11437. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11438. "0360 Unsupported EQ count. (%d)\n",
  11439. eq->entry_count);
  11440. if (eq->entry_count < 256)
  11441. return -EINVAL;
  11442. /* otherwise default to smallest count (drop through) */
  11443. case 256:
  11444. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11445. LPFC_EQ_CNT_256);
  11446. break;
  11447. case 512:
  11448. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11449. LPFC_EQ_CNT_512);
  11450. break;
  11451. case 1024:
  11452. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11453. LPFC_EQ_CNT_1024);
  11454. break;
  11455. case 2048:
  11456. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11457. LPFC_EQ_CNT_2048);
  11458. break;
  11459. case 4096:
  11460. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11461. LPFC_EQ_CNT_4096);
  11462. break;
  11463. }
  11464. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11465. memset(dmabuf->virt, 0, hw_page_size);
  11466. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11467. putPaddrLow(dmabuf->phys);
  11468. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11469. putPaddrHigh(dmabuf->phys);
  11470. }
  11471. mbox->vport = phba->pport;
  11472. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11473. mbox->context1 = NULL;
  11474. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11475. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11476. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11477. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11478. if (shdr_status || shdr_add_status || rc) {
  11479. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11480. "2500 EQ_CREATE mailbox failed with "
  11481. "status x%x add_status x%x, mbx status x%x\n",
  11482. shdr_status, shdr_add_status, rc);
  11483. status = -ENXIO;
  11484. }
  11485. eq->type = LPFC_EQ;
  11486. eq->subtype = LPFC_NONE;
  11487. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11488. if (eq->queue_id == 0xFFFF)
  11489. status = -ENXIO;
  11490. eq->host_index = 0;
  11491. eq->hba_index = 0;
  11492. mempool_free(mbox, phba->mbox_mem_pool);
  11493. return status;
  11494. }
  11495. /**
  11496. * lpfc_cq_create - Create a Completion Queue on the HBA
  11497. * @phba: HBA structure that indicates port to create a queue on.
  11498. * @cq: The queue structure to use to create the completion queue.
  11499. * @eq: The event queue to bind this completion queue to.
  11500. *
  11501. * This function creates a completion queue, as detailed in @wq, on a port,
  11502. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11503. *
  11504. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11505. * is used to get the entry count and entry size that are necessary to
  11506. * determine the number of pages to allocate and use for this queue. The @eq
  11507. * is used to indicate which event queue to bind this completion queue to. This
  11508. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11509. * completion queue. This function is asynchronous and will wait for the mailbox
  11510. * command to finish before continuing.
  11511. *
  11512. * On success this function will return a zero. If unable to allocate enough
  11513. * memory this function will return -ENOMEM. If the queue create mailbox command
  11514. * fails this function will return -ENXIO.
  11515. **/
  11516. uint32_t
  11517. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11518. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11519. {
  11520. struct lpfc_mbx_cq_create *cq_create;
  11521. struct lpfc_dmabuf *dmabuf;
  11522. LPFC_MBOXQ_t *mbox;
  11523. int rc, length, status = 0;
  11524. uint32_t shdr_status, shdr_add_status;
  11525. union lpfc_sli4_cfg_shdr *shdr;
  11526. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11527. /* sanity check on queue memory */
  11528. if (!cq || !eq)
  11529. return -ENODEV;
  11530. if (!phba->sli4_hba.pc_sli4_params.supported)
  11531. hw_page_size = SLI4_PAGE_SIZE;
  11532. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11533. if (!mbox)
  11534. return -ENOMEM;
  11535. length = (sizeof(struct lpfc_mbx_cq_create) -
  11536. sizeof(struct lpfc_sli4_cfg_mhdr));
  11537. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11538. LPFC_MBOX_OPCODE_CQ_CREATE,
  11539. length, LPFC_SLI4_MBX_EMBED);
  11540. cq_create = &mbox->u.mqe.un.cq_create;
  11541. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11542. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11543. cq->page_count);
  11544. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11545. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11546. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11547. phba->sli4_hba.pc_sli4_params.cqv);
  11548. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11549. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11550. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11551. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11552. eq->queue_id);
  11553. } else {
  11554. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11555. eq->queue_id);
  11556. }
  11557. switch (cq->entry_count) {
  11558. default:
  11559. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11560. "0361 Unsupported CQ count. (%d)\n",
  11561. cq->entry_count);
  11562. if (cq->entry_count < 256) {
  11563. status = -EINVAL;
  11564. goto out;
  11565. }
  11566. /* otherwise default to smallest count (drop through) */
  11567. case 256:
  11568. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11569. LPFC_CQ_CNT_256);
  11570. break;
  11571. case 512:
  11572. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11573. LPFC_CQ_CNT_512);
  11574. break;
  11575. case 1024:
  11576. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11577. LPFC_CQ_CNT_1024);
  11578. break;
  11579. }
  11580. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11581. memset(dmabuf->virt, 0, hw_page_size);
  11582. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11583. putPaddrLow(dmabuf->phys);
  11584. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11585. putPaddrHigh(dmabuf->phys);
  11586. }
  11587. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11588. /* The IOCTL status is embedded in the mailbox subheader. */
  11589. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11590. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11591. if (shdr_status || shdr_add_status || rc) {
  11592. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11593. "2501 CQ_CREATE mailbox failed with "
  11594. "status x%x add_status x%x, mbx status x%x\n",
  11595. shdr_status, shdr_add_status, rc);
  11596. status = -ENXIO;
  11597. goto out;
  11598. }
  11599. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11600. if (cq->queue_id == 0xFFFF) {
  11601. status = -ENXIO;
  11602. goto out;
  11603. }
  11604. /* link the cq onto the parent eq child list */
  11605. list_add_tail(&cq->list, &eq->child_list);
  11606. /* Set up completion queue's type and subtype */
  11607. cq->type = type;
  11608. cq->subtype = subtype;
  11609. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11610. cq->assoc_qid = eq->queue_id;
  11611. cq->host_index = 0;
  11612. cq->hba_index = 0;
  11613. out:
  11614. mempool_free(mbox, phba->mbox_mem_pool);
  11615. return status;
  11616. }
  11617. /**
  11618. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11619. * @phba: HBA structure that indicates port to create a queue on.
  11620. * @mq: The queue structure to use to create the mailbox queue.
  11621. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11622. * @cq: The completion queue to associate with this cq.
  11623. *
  11624. * This function provides failback (fb) functionality when the
  11625. * mq_create_ext fails on older FW generations. It's purpose is identical
  11626. * to mq_create_ext otherwise.
  11627. *
  11628. * This routine cannot fail as all attributes were previously accessed and
  11629. * initialized in mq_create_ext.
  11630. **/
  11631. static void
  11632. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11633. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11634. {
  11635. struct lpfc_mbx_mq_create *mq_create;
  11636. struct lpfc_dmabuf *dmabuf;
  11637. int length;
  11638. length = (sizeof(struct lpfc_mbx_mq_create) -
  11639. sizeof(struct lpfc_sli4_cfg_mhdr));
  11640. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11641. LPFC_MBOX_OPCODE_MQ_CREATE,
  11642. length, LPFC_SLI4_MBX_EMBED);
  11643. mq_create = &mbox->u.mqe.un.mq_create;
  11644. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11645. mq->page_count);
  11646. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11647. cq->queue_id);
  11648. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11649. switch (mq->entry_count) {
  11650. case 16:
  11651. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11652. LPFC_MQ_RING_SIZE_16);
  11653. break;
  11654. case 32:
  11655. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11656. LPFC_MQ_RING_SIZE_32);
  11657. break;
  11658. case 64:
  11659. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11660. LPFC_MQ_RING_SIZE_64);
  11661. break;
  11662. case 128:
  11663. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11664. LPFC_MQ_RING_SIZE_128);
  11665. break;
  11666. }
  11667. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11668. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11669. putPaddrLow(dmabuf->phys);
  11670. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11671. putPaddrHigh(dmabuf->phys);
  11672. }
  11673. }
  11674. /**
  11675. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11676. * @phba: HBA structure that indicates port to create a queue on.
  11677. * @mq: The queue structure to use to create the mailbox queue.
  11678. * @cq: The completion queue to associate with this cq.
  11679. * @subtype: The queue's subtype.
  11680. *
  11681. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11682. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11683. *
  11684. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11685. * is used to get the entry count and entry size that are necessary to
  11686. * determine the number of pages to allocate and use for this queue. This
  11687. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11688. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11689. * command to finish before continuing.
  11690. *
  11691. * On success this function will return a zero. If unable to allocate enough
  11692. * memory this function will return -ENOMEM. If the queue create mailbox command
  11693. * fails this function will return -ENXIO.
  11694. **/
  11695. int32_t
  11696. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11697. struct lpfc_queue *cq, uint32_t subtype)
  11698. {
  11699. struct lpfc_mbx_mq_create *mq_create;
  11700. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11701. struct lpfc_dmabuf *dmabuf;
  11702. LPFC_MBOXQ_t *mbox;
  11703. int rc, length, status = 0;
  11704. uint32_t shdr_status, shdr_add_status;
  11705. union lpfc_sli4_cfg_shdr *shdr;
  11706. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11707. /* sanity check on queue memory */
  11708. if (!mq || !cq)
  11709. return -ENODEV;
  11710. if (!phba->sli4_hba.pc_sli4_params.supported)
  11711. hw_page_size = SLI4_PAGE_SIZE;
  11712. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11713. if (!mbox)
  11714. return -ENOMEM;
  11715. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11716. sizeof(struct lpfc_sli4_cfg_mhdr));
  11717. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11718. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11719. length, LPFC_SLI4_MBX_EMBED);
  11720. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11721. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11722. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11723. &mq_create_ext->u.request, mq->page_count);
  11724. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11725. &mq_create_ext->u.request, 1);
  11726. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11727. &mq_create_ext->u.request, 1);
  11728. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11729. &mq_create_ext->u.request, 1);
  11730. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11731. &mq_create_ext->u.request, 1);
  11732. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11733. &mq_create_ext->u.request, 1);
  11734. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11735. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11736. phba->sli4_hba.pc_sli4_params.mqv);
  11737. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11738. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11739. cq->queue_id);
  11740. else
  11741. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11742. cq->queue_id);
  11743. switch (mq->entry_count) {
  11744. default:
  11745. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11746. "0362 Unsupported MQ count. (%d)\n",
  11747. mq->entry_count);
  11748. if (mq->entry_count < 16) {
  11749. status = -EINVAL;
  11750. goto out;
  11751. }
  11752. /* otherwise default to smallest count (drop through) */
  11753. case 16:
  11754. bf_set(lpfc_mq_context_ring_size,
  11755. &mq_create_ext->u.request.context,
  11756. LPFC_MQ_RING_SIZE_16);
  11757. break;
  11758. case 32:
  11759. bf_set(lpfc_mq_context_ring_size,
  11760. &mq_create_ext->u.request.context,
  11761. LPFC_MQ_RING_SIZE_32);
  11762. break;
  11763. case 64:
  11764. bf_set(lpfc_mq_context_ring_size,
  11765. &mq_create_ext->u.request.context,
  11766. LPFC_MQ_RING_SIZE_64);
  11767. break;
  11768. case 128:
  11769. bf_set(lpfc_mq_context_ring_size,
  11770. &mq_create_ext->u.request.context,
  11771. LPFC_MQ_RING_SIZE_128);
  11772. break;
  11773. }
  11774. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11775. memset(dmabuf->virt, 0, hw_page_size);
  11776. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11777. putPaddrLow(dmabuf->phys);
  11778. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11779. putPaddrHigh(dmabuf->phys);
  11780. }
  11781. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11782. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11783. &mq_create_ext->u.response);
  11784. if (rc != MBX_SUCCESS) {
  11785. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11786. "2795 MQ_CREATE_EXT failed with "
  11787. "status x%x. Failback to MQ_CREATE.\n",
  11788. rc);
  11789. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11790. mq_create = &mbox->u.mqe.un.mq_create;
  11791. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11792. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11793. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11794. &mq_create->u.response);
  11795. }
  11796. /* The IOCTL status is embedded in the mailbox subheader. */
  11797. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11798. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11799. if (shdr_status || shdr_add_status || rc) {
  11800. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11801. "2502 MQ_CREATE mailbox failed with "
  11802. "status x%x add_status x%x, mbx status x%x\n",
  11803. shdr_status, shdr_add_status, rc);
  11804. status = -ENXIO;
  11805. goto out;
  11806. }
  11807. if (mq->queue_id == 0xFFFF) {
  11808. status = -ENXIO;
  11809. goto out;
  11810. }
  11811. mq->type = LPFC_MQ;
  11812. mq->assoc_qid = cq->queue_id;
  11813. mq->subtype = subtype;
  11814. mq->host_index = 0;
  11815. mq->hba_index = 0;
  11816. /* link the mq onto the parent cq child list */
  11817. list_add_tail(&mq->list, &cq->child_list);
  11818. out:
  11819. mempool_free(mbox, phba->mbox_mem_pool);
  11820. return status;
  11821. }
  11822. /**
  11823. * lpfc_wq_create - Create a Work Queue on the HBA
  11824. * @phba: HBA structure that indicates port to create a queue on.
  11825. * @wq: The queue structure to use to create the work queue.
  11826. * @cq: The completion queue to bind this work queue to.
  11827. * @subtype: The subtype of the work queue indicating its functionality.
  11828. *
  11829. * This function creates a work queue, as detailed in @wq, on a port, described
  11830. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11831. *
  11832. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11833. * is used to get the entry count and entry size that are necessary to
  11834. * determine the number of pages to allocate and use for this queue. The @cq
  11835. * is used to indicate which completion queue to bind this work queue to. This
  11836. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11837. * work queue. This function is asynchronous and will wait for the mailbox
  11838. * command to finish before continuing.
  11839. *
  11840. * On success this function will return a zero. If unable to allocate enough
  11841. * memory this function will return -ENOMEM. If the queue create mailbox command
  11842. * fails this function will return -ENXIO.
  11843. **/
  11844. uint32_t
  11845. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11846. struct lpfc_queue *cq, uint32_t subtype)
  11847. {
  11848. struct lpfc_mbx_wq_create *wq_create;
  11849. struct lpfc_dmabuf *dmabuf;
  11850. LPFC_MBOXQ_t *mbox;
  11851. int rc, length, status = 0;
  11852. uint32_t shdr_status, shdr_add_status;
  11853. union lpfc_sli4_cfg_shdr *shdr;
  11854. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11855. struct dma_address *page;
  11856. void __iomem *bar_memmap_p;
  11857. uint32_t db_offset;
  11858. uint16_t pci_barset;
  11859. /* sanity check on queue memory */
  11860. if (!wq || !cq)
  11861. return -ENODEV;
  11862. if (!phba->sli4_hba.pc_sli4_params.supported)
  11863. hw_page_size = SLI4_PAGE_SIZE;
  11864. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11865. if (!mbox)
  11866. return -ENOMEM;
  11867. length = (sizeof(struct lpfc_mbx_wq_create) -
  11868. sizeof(struct lpfc_sli4_cfg_mhdr));
  11869. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11870. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11871. length, LPFC_SLI4_MBX_EMBED);
  11872. wq_create = &mbox->u.mqe.un.wq_create;
  11873. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11874. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11875. wq->page_count);
  11876. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11877. cq->queue_id);
  11878. /* wqv is the earliest version supported, NOT the latest */
  11879. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11880. phba->sli4_hba.pc_sli4_params.wqv);
  11881. switch (phba->sli4_hba.pc_sli4_params.wqv) {
  11882. case LPFC_Q_CREATE_VERSION_0:
  11883. switch (wq->entry_size) {
  11884. default:
  11885. case 64:
  11886. /* Nothing to do, version 0 ONLY supports 64 byte */
  11887. page = wq_create->u.request.page;
  11888. break;
  11889. case 128:
  11890. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  11891. LPFC_WQ_SZ128_SUPPORT)) {
  11892. status = -ERANGE;
  11893. goto out;
  11894. }
  11895. /* If we get here the HBA MUST also support V1 and
  11896. * we MUST use it
  11897. */
  11898. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11899. LPFC_Q_CREATE_VERSION_1);
  11900. bf_set(lpfc_mbx_wq_create_wqe_count,
  11901. &wq_create->u.request_1, wq->entry_count);
  11902. bf_set(lpfc_mbx_wq_create_wqe_size,
  11903. &wq_create->u.request_1,
  11904. LPFC_WQ_WQE_SIZE_128);
  11905. bf_set(lpfc_mbx_wq_create_page_size,
  11906. &wq_create->u.request_1,
  11907. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11908. page = wq_create->u.request_1.page;
  11909. break;
  11910. }
  11911. break;
  11912. case LPFC_Q_CREATE_VERSION_1:
  11913. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11914. wq->entry_count);
  11915. switch (wq->entry_size) {
  11916. default:
  11917. case 64:
  11918. bf_set(lpfc_mbx_wq_create_wqe_size,
  11919. &wq_create->u.request_1,
  11920. LPFC_WQ_WQE_SIZE_64);
  11921. break;
  11922. case 128:
  11923. if (!(phba->sli4_hba.pc_sli4_params.wqsize &
  11924. LPFC_WQ_SZ128_SUPPORT)) {
  11925. status = -ERANGE;
  11926. goto out;
  11927. }
  11928. bf_set(lpfc_mbx_wq_create_wqe_size,
  11929. &wq_create->u.request_1,
  11930. LPFC_WQ_WQE_SIZE_128);
  11931. break;
  11932. }
  11933. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11934. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11935. page = wq_create->u.request_1.page;
  11936. break;
  11937. default:
  11938. status = -ERANGE;
  11939. goto out;
  11940. }
  11941. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11942. memset(dmabuf->virt, 0, hw_page_size);
  11943. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11944. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11945. }
  11946. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  11947. bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
  11948. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11949. /* The IOCTL status is embedded in the mailbox subheader. */
  11950. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11951. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11952. if (shdr_status || shdr_add_status || rc) {
  11953. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11954. "2503 WQ_CREATE mailbox failed with "
  11955. "status x%x add_status x%x, mbx status x%x\n",
  11956. shdr_status, shdr_add_status, rc);
  11957. status = -ENXIO;
  11958. goto out;
  11959. }
  11960. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11961. if (wq->queue_id == 0xFFFF) {
  11962. status = -ENXIO;
  11963. goto out;
  11964. }
  11965. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  11966. wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
  11967. &wq_create->u.response);
  11968. if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
  11969. (wq->db_format != LPFC_DB_RING_FORMAT)) {
  11970. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11971. "3265 WQ[%d] doorbell format not "
  11972. "supported: x%x\n", wq->queue_id,
  11973. wq->db_format);
  11974. status = -EINVAL;
  11975. goto out;
  11976. }
  11977. pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
  11978. &wq_create->u.response);
  11979. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  11980. if (!bar_memmap_p) {
  11981. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11982. "3263 WQ[%d] failed to memmap pci "
  11983. "barset:x%x\n", wq->queue_id,
  11984. pci_barset);
  11985. status = -ENOMEM;
  11986. goto out;
  11987. }
  11988. db_offset = wq_create->u.response.doorbell_offset;
  11989. if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
  11990. (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
  11991. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11992. "3252 WQ[%d] doorbell offset not "
  11993. "supported: x%x\n", wq->queue_id,
  11994. db_offset);
  11995. status = -EINVAL;
  11996. goto out;
  11997. }
  11998. wq->db_regaddr = bar_memmap_p + db_offset;
  11999. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12000. "3264 WQ[%d]: barset:x%x, offset:x%x, "
  12001. "format:x%x\n", wq->queue_id, pci_barset,
  12002. db_offset, wq->db_format);
  12003. } else {
  12004. wq->db_format = LPFC_DB_LIST_FORMAT;
  12005. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  12006. }
  12007. wq->type = LPFC_WQ;
  12008. wq->assoc_qid = cq->queue_id;
  12009. wq->subtype = subtype;
  12010. wq->host_index = 0;
  12011. wq->hba_index = 0;
  12012. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  12013. /* link the wq onto the parent cq child list */
  12014. list_add_tail(&wq->list, &cq->child_list);
  12015. out:
  12016. mempool_free(mbox, phba->mbox_mem_pool);
  12017. return status;
  12018. }
  12019. /**
  12020. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  12021. * @phba: HBA structure that indicates port to create a queue on.
  12022. * @rq: The queue structure to use for the receive queue.
  12023. * @qno: The associated HBQ number
  12024. *
  12025. *
  12026. * For SLI4 we need to adjust the RQ repost value based on
  12027. * the number of buffers that are initially posted to the RQ.
  12028. */
  12029. void
  12030. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  12031. {
  12032. uint32_t cnt;
  12033. /* sanity check on queue memory */
  12034. if (!rq)
  12035. return;
  12036. cnt = lpfc_hbq_defs[qno]->entry_count;
  12037. /* Recalc repost for RQs based on buffers initially posted */
  12038. cnt = (cnt >> 3);
  12039. if (cnt < LPFC_QUEUE_MIN_REPOST)
  12040. cnt = LPFC_QUEUE_MIN_REPOST;
  12041. rq->entry_repost = cnt;
  12042. }
  12043. /**
  12044. * lpfc_rq_create - Create a Receive Queue on the HBA
  12045. * @phba: HBA structure that indicates port to create a queue on.
  12046. * @hrq: The queue structure to use to create the header receive queue.
  12047. * @drq: The queue structure to use to create the data receive queue.
  12048. * @cq: The completion queue to bind this work queue to.
  12049. *
  12050. * This function creates a receive buffer queue pair , as detailed in @hrq and
  12051. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  12052. * to the HBA.
  12053. *
  12054. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  12055. * struct is used to get the entry count that is necessary to determine the
  12056. * number of pages to use for this queue. The @cq is used to indicate which
  12057. * completion queue to bind received buffers that are posted to these queues to.
  12058. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  12059. * receive queue pair. This function is asynchronous and will wait for the
  12060. * mailbox command to finish before continuing.
  12061. *
  12062. * On success this function will return a zero. If unable to allocate enough
  12063. * memory this function will return -ENOMEM. If the queue create mailbox command
  12064. * fails this function will return -ENXIO.
  12065. **/
  12066. uint32_t
  12067. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12068. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  12069. {
  12070. struct lpfc_mbx_rq_create *rq_create;
  12071. struct lpfc_dmabuf *dmabuf;
  12072. LPFC_MBOXQ_t *mbox;
  12073. int rc, length, status = 0;
  12074. uint32_t shdr_status, shdr_add_status;
  12075. union lpfc_sli4_cfg_shdr *shdr;
  12076. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12077. void __iomem *bar_memmap_p;
  12078. uint32_t db_offset;
  12079. uint16_t pci_barset;
  12080. /* sanity check on queue memory */
  12081. if (!hrq || !drq || !cq)
  12082. return -ENODEV;
  12083. if (!phba->sli4_hba.pc_sli4_params.supported)
  12084. hw_page_size = SLI4_PAGE_SIZE;
  12085. if (hrq->entry_count != drq->entry_count)
  12086. return -EINVAL;
  12087. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12088. if (!mbox)
  12089. return -ENOMEM;
  12090. length = (sizeof(struct lpfc_mbx_rq_create) -
  12091. sizeof(struct lpfc_sli4_cfg_mhdr));
  12092. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12093. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12094. length, LPFC_SLI4_MBX_EMBED);
  12095. rq_create = &mbox->u.mqe.un.rq_create;
  12096. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12097. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12098. phba->sli4_hba.pc_sli4_params.rqv);
  12099. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12100. bf_set(lpfc_rq_context_rqe_count_1,
  12101. &rq_create->u.request.context,
  12102. hrq->entry_count);
  12103. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  12104. bf_set(lpfc_rq_context_rqe_size,
  12105. &rq_create->u.request.context,
  12106. LPFC_RQE_SIZE_8);
  12107. bf_set(lpfc_rq_context_page_size,
  12108. &rq_create->u.request.context,
  12109. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12110. } else {
  12111. switch (hrq->entry_count) {
  12112. default:
  12113. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12114. "2535 Unsupported RQ count. (%d)\n",
  12115. hrq->entry_count);
  12116. if (hrq->entry_count < 512) {
  12117. status = -EINVAL;
  12118. goto out;
  12119. }
  12120. /* otherwise default to smallest count (drop through) */
  12121. case 512:
  12122. bf_set(lpfc_rq_context_rqe_count,
  12123. &rq_create->u.request.context,
  12124. LPFC_RQ_RING_SIZE_512);
  12125. break;
  12126. case 1024:
  12127. bf_set(lpfc_rq_context_rqe_count,
  12128. &rq_create->u.request.context,
  12129. LPFC_RQ_RING_SIZE_1024);
  12130. break;
  12131. case 2048:
  12132. bf_set(lpfc_rq_context_rqe_count,
  12133. &rq_create->u.request.context,
  12134. LPFC_RQ_RING_SIZE_2048);
  12135. break;
  12136. case 4096:
  12137. bf_set(lpfc_rq_context_rqe_count,
  12138. &rq_create->u.request.context,
  12139. LPFC_RQ_RING_SIZE_4096);
  12140. break;
  12141. }
  12142. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12143. LPFC_HDR_BUF_SIZE);
  12144. }
  12145. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12146. cq->queue_id);
  12147. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12148. hrq->page_count);
  12149. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  12150. memset(dmabuf->virt, 0, hw_page_size);
  12151. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12152. putPaddrLow(dmabuf->phys);
  12153. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12154. putPaddrHigh(dmabuf->phys);
  12155. }
  12156. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12157. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12158. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12159. /* The IOCTL status is embedded in the mailbox subheader. */
  12160. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12161. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12162. if (shdr_status || shdr_add_status || rc) {
  12163. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12164. "2504 RQ_CREATE mailbox failed with "
  12165. "status x%x add_status x%x, mbx status x%x\n",
  12166. shdr_status, shdr_add_status, rc);
  12167. status = -ENXIO;
  12168. goto out;
  12169. }
  12170. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12171. if (hrq->queue_id == 0xFFFF) {
  12172. status = -ENXIO;
  12173. goto out;
  12174. }
  12175. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  12176. hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
  12177. &rq_create->u.response);
  12178. if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
  12179. (hrq->db_format != LPFC_DB_RING_FORMAT)) {
  12180. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12181. "3262 RQ [%d] doorbell format not "
  12182. "supported: x%x\n", hrq->queue_id,
  12183. hrq->db_format);
  12184. status = -EINVAL;
  12185. goto out;
  12186. }
  12187. pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
  12188. &rq_create->u.response);
  12189. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  12190. if (!bar_memmap_p) {
  12191. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12192. "3269 RQ[%d] failed to memmap pci "
  12193. "barset:x%x\n", hrq->queue_id,
  12194. pci_barset);
  12195. status = -ENOMEM;
  12196. goto out;
  12197. }
  12198. db_offset = rq_create->u.response.doorbell_offset;
  12199. if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
  12200. (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
  12201. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12202. "3270 RQ[%d] doorbell offset not "
  12203. "supported: x%x\n", hrq->queue_id,
  12204. db_offset);
  12205. status = -EINVAL;
  12206. goto out;
  12207. }
  12208. hrq->db_regaddr = bar_memmap_p + db_offset;
  12209. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12210. "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
  12211. "format:x%x\n", hrq->queue_id, pci_barset,
  12212. db_offset, hrq->db_format);
  12213. } else {
  12214. hrq->db_format = LPFC_DB_RING_FORMAT;
  12215. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  12216. }
  12217. hrq->type = LPFC_HRQ;
  12218. hrq->assoc_qid = cq->queue_id;
  12219. hrq->subtype = subtype;
  12220. hrq->host_index = 0;
  12221. hrq->hba_index = 0;
  12222. /* now create the data queue */
  12223. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12224. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  12225. length, LPFC_SLI4_MBX_EMBED);
  12226. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  12227. phba->sli4_hba.pc_sli4_params.rqv);
  12228. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  12229. bf_set(lpfc_rq_context_rqe_count_1,
  12230. &rq_create->u.request.context, hrq->entry_count);
  12231. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  12232. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  12233. LPFC_RQE_SIZE_8);
  12234. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  12235. (PAGE_SIZE/SLI4_PAGE_SIZE));
  12236. } else {
  12237. switch (drq->entry_count) {
  12238. default:
  12239. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12240. "2536 Unsupported RQ count. (%d)\n",
  12241. drq->entry_count);
  12242. if (drq->entry_count < 512) {
  12243. status = -EINVAL;
  12244. goto out;
  12245. }
  12246. /* otherwise default to smallest count (drop through) */
  12247. case 512:
  12248. bf_set(lpfc_rq_context_rqe_count,
  12249. &rq_create->u.request.context,
  12250. LPFC_RQ_RING_SIZE_512);
  12251. break;
  12252. case 1024:
  12253. bf_set(lpfc_rq_context_rqe_count,
  12254. &rq_create->u.request.context,
  12255. LPFC_RQ_RING_SIZE_1024);
  12256. break;
  12257. case 2048:
  12258. bf_set(lpfc_rq_context_rqe_count,
  12259. &rq_create->u.request.context,
  12260. LPFC_RQ_RING_SIZE_2048);
  12261. break;
  12262. case 4096:
  12263. bf_set(lpfc_rq_context_rqe_count,
  12264. &rq_create->u.request.context,
  12265. LPFC_RQ_RING_SIZE_4096);
  12266. break;
  12267. }
  12268. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12269. LPFC_DATA_BUF_SIZE);
  12270. }
  12271. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12272. cq->queue_id);
  12273. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12274. drq->page_count);
  12275. list_for_each_entry(dmabuf, &drq->page_list, list) {
  12276. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12277. putPaddrLow(dmabuf->phys);
  12278. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12279. putPaddrHigh(dmabuf->phys);
  12280. }
  12281. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12282. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12283. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12284. /* The IOCTL status is embedded in the mailbox subheader. */
  12285. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12286. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12287. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12288. if (shdr_status || shdr_add_status || rc) {
  12289. status = -ENXIO;
  12290. goto out;
  12291. }
  12292. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12293. if (drq->queue_id == 0xFFFF) {
  12294. status = -ENXIO;
  12295. goto out;
  12296. }
  12297. drq->type = LPFC_DRQ;
  12298. drq->assoc_qid = cq->queue_id;
  12299. drq->subtype = subtype;
  12300. drq->host_index = 0;
  12301. drq->hba_index = 0;
  12302. /* link the header and data RQs onto the parent cq child list */
  12303. list_add_tail(&hrq->list, &cq->child_list);
  12304. list_add_tail(&drq->list, &cq->child_list);
  12305. out:
  12306. mempool_free(mbox, phba->mbox_mem_pool);
  12307. return status;
  12308. }
  12309. /**
  12310. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  12311. * @eq: The queue structure associated with the queue to destroy.
  12312. *
  12313. * This function destroys a queue, as detailed in @eq by sending an mailbox
  12314. * command, specific to the type of queue, to the HBA.
  12315. *
  12316. * The @eq struct is used to get the queue ID of the queue to destroy.
  12317. *
  12318. * On success this function will return a zero. If the queue destroy mailbox
  12319. * command fails this function will return -ENXIO.
  12320. **/
  12321. uint32_t
  12322. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  12323. {
  12324. LPFC_MBOXQ_t *mbox;
  12325. int rc, length, status = 0;
  12326. uint32_t shdr_status, shdr_add_status;
  12327. union lpfc_sli4_cfg_shdr *shdr;
  12328. /* sanity check on queue memory */
  12329. if (!eq)
  12330. return -ENODEV;
  12331. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  12332. if (!mbox)
  12333. return -ENOMEM;
  12334. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  12335. sizeof(struct lpfc_sli4_cfg_mhdr));
  12336. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12337. LPFC_MBOX_OPCODE_EQ_DESTROY,
  12338. length, LPFC_SLI4_MBX_EMBED);
  12339. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  12340. eq->queue_id);
  12341. mbox->vport = eq->phba->pport;
  12342. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12343. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  12344. /* The IOCTL status is embedded in the mailbox subheader. */
  12345. shdr = (union lpfc_sli4_cfg_shdr *)
  12346. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  12347. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12348. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12349. if (shdr_status || shdr_add_status || rc) {
  12350. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12351. "2505 EQ_DESTROY mailbox failed with "
  12352. "status x%x add_status x%x, mbx status x%x\n",
  12353. shdr_status, shdr_add_status, rc);
  12354. status = -ENXIO;
  12355. }
  12356. /* Remove eq from any list */
  12357. list_del_init(&eq->list);
  12358. mempool_free(mbox, eq->phba->mbox_mem_pool);
  12359. return status;
  12360. }
  12361. /**
  12362. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  12363. * @cq: The queue structure associated with the queue to destroy.
  12364. *
  12365. * This function destroys a queue, as detailed in @cq by sending an mailbox
  12366. * command, specific to the type of queue, to the HBA.
  12367. *
  12368. * The @cq struct is used to get the queue ID of the queue to destroy.
  12369. *
  12370. * On success this function will return a zero. If the queue destroy mailbox
  12371. * command fails this function will return -ENXIO.
  12372. **/
  12373. uint32_t
  12374. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  12375. {
  12376. LPFC_MBOXQ_t *mbox;
  12377. int rc, length, status = 0;
  12378. uint32_t shdr_status, shdr_add_status;
  12379. union lpfc_sli4_cfg_shdr *shdr;
  12380. /* sanity check on queue memory */
  12381. if (!cq)
  12382. return -ENODEV;
  12383. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  12384. if (!mbox)
  12385. return -ENOMEM;
  12386. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  12387. sizeof(struct lpfc_sli4_cfg_mhdr));
  12388. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12389. LPFC_MBOX_OPCODE_CQ_DESTROY,
  12390. length, LPFC_SLI4_MBX_EMBED);
  12391. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  12392. cq->queue_id);
  12393. mbox->vport = cq->phba->pport;
  12394. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12395. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  12396. /* The IOCTL status is embedded in the mailbox subheader. */
  12397. shdr = (union lpfc_sli4_cfg_shdr *)
  12398. &mbox->u.mqe.un.wq_create.header.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 (shdr_status || shdr_add_status || rc) {
  12402. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12403. "2506 CQ_DESTROY mailbox failed with "
  12404. "status x%x add_status x%x, mbx status x%x\n",
  12405. shdr_status, shdr_add_status, rc);
  12406. status = -ENXIO;
  12407. }
  12408. /* Remove cq from any list */
  12409. list_del_init(&cq->list);
  12410. mempool_free(mbox, cq->phba->mbox_mem_pool);
  12411. return status;
  12412. }
  12413. /**
  12414. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  12415. * @qm: The queue structure associated with the queue to destroy.
  12416. *
  12417. * This function destroys a queue, as detailed in @mq by sending an mailbox
  12418. * command, specific to the type of queue, to the HBA.
  12419. *
  12420. * The @mq struct is used to get the queue ID of the queue to destroy.
  12421. *
  12422. * On success this function will return a zero. If the queue destroy mailbox
  12423. * command fails this function will return -ENXIO.
  12424. **/
  12425. uint32_t
  12426. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  12427. {
  12428. LPFC_MBOXQ_t *mbox;
  12429. int rc, length, status = 0;
  12430. uint32_t shdr_status, shdr_add_status;
  12431. union lpfc_sli4_cfg_shdr *shdr;
  12432. /* sanity check on queue memory */
  12433. if (!mq)
  12434. return -ENODEV;
  12435. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  12436. if (!mbox)
  12437. return -ENOMEM;
  12438. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12439. sizeof(struct lpfc_sli4_cfg_mhdr));
  12440. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12441. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12442. length, LPFC_SLI4_MBX_EMBED);
  12443. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12444. mq->queue_id);
  12445. mbox->vport = mq->phba->pport;
  12446. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12447. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12448. /* The IOCTL status is embedded in the mailbox subheader. */
  12449. shdr = (union lpfc_sli4_cfg_shdr *)
  12450. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12451. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12452. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12453. if (shdr_status || shdr_add_status || rc) {
  12454. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12455. "2507 MQ_DESTROY mailbox failed with "
  12456. "status x%x add_status x%x, mbx status x%x\n",
  12457. shdr_status, shdr_add_status, rc);
  12458. status = -ENXIO;
  12459. }
  12460. /* Remove mq from any list */
  12461. list_del_init(&mq->list);
  12462. mempool_free(mbox, mq->phba->mbox_mem_pool);
  12463. return status;
  12464. }
  12465. /**
  12466. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12467. * @wq: The queue structure associated with the queue to destroy.
  12468. *
  12469. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12470. * command, specific to the type of queue, to the HBA.
  12471. *
  12472. * The @wq struct is used to get the queue ID of the queue to destroy.
  12473. *
  12474. * On success this function will return a zero. If the queue destroy mailbox
  12475. * command fails this function will return -ENXIO.
  12476. **/
  12477. uint32_t
  12478. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12479. {
  12480. LPFC_MBOXQ_t *mbox;
  12481. int rc, length, status = 0;
  12482. uint32_t shdr_status, shdr_add_status;
  12483. union lpfc_sli4_cfg_shdr *shdr;
  12484. /* sanity check on queue memory */
  12485. if (!wq)
  12486. return -ENODEV;
  12487. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12488. if (!mbox)
  12489. return -ENOMEM;
  12490. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12491. sizeof(struct lpfc_sli4_cfg_mhdr));
  12492. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12493. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12494. length, LPFC_SLI4_MBX_EMBED);
  12495. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12496. wq->queue_id);
  12497. mbox->vport = wq->phba->pport;
  12498. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12499. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12500. shdr = (union lpfc_sli4_cfg_shdr *)
  12501. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12502. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12503. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12504. if (shdr_status || shdr_add_status || rc) {
  12505. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12506. "2508 WQ_DESTROY mailbox failed with "
  12507. "status x%x add_status x%x, mbx status x%x\n",
  12508. shdr_status, shdr_add_status, rc);
  12509. status = -ENXIO;
  12510. }
  12511. /* Remove wq from any list */
  12512. list_del_init(&wq->list);
  12513. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12514. return status;
  12515. }
  12516. /**
  12517. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12518. * @rq: The queue structure associated with the queue to destroy.
  12519. *
  12520. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12521. * command, specific to the type of queue, to the HBA.
  12522. *
  12523. * The @rq struct is used to get the queue ID of the queue to destroy.
  12524. *
  12525. * On success this function will return a zero. If the queue destroy mailbox
  12526. * command fails this function will return -ENXIO.
  12527. **/
  12528. uint32_t
  12529. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12530. struct lpfc_queue *drq)
  12531. {
  12532. LPFC_MBOXQ_t *mbox;
  12533. int rc, length, status = 0;
  12534. uint32_t shdr_status, shdr_add_status;
  12535. union lpfc_sli4_cfg_shdr *shdr;
  12536. /* sanity check on queue memory */
  12537. if (!hrq || !drq)
  12538. return -ENODEV;
  12539. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12540. if (!mbox)
  12541. return -ENOMEM;
  12542. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12543. sizeof(struct lpfc_sli4_cfg_mhdr));
  12544. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12545. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12546. length, LPFC_SLI4_MBX_EMBED);
  12547. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12548. hrq->queue_id);
  12549. mbox->vport = hrq->phba->pport;
  12550. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12551. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12552. /* The IOCTL status is embedded in the mailbox subheader. */
  12553. shdr = (union lpfc_sli4_cfg_shdr *)
  12554. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12555. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12556. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12557. if (shdr_status || shdr_add_status || rc) {
  12558. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12559. "2509 RQ_DESTROY mailbox failed with "
  12560. "status x%x add_status x%x, mbx status x%x\n",
  12561. shdr_status, shdr_add_status, rc);
  12562. if (rc != MBX_TIMEOUT)
  12563. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12564. return -ENXIO;
  12565. }
  12566. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12567. drq->queue_id);
  12568. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12569. shdr = (union lpfc_sli4_cfg_shdr *)
  12570. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12571. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12572. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12573. if (shdr_status || shdr_add_status || rc) {
  12574. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12575. "2510 RQ_DESTROY mailbox failed with "
  12576. "status x%x add_status x%x, mbx status x%x\n",
  12577. shdr_status, shdr_add_status, rc);
  12578. status = -ENXIO;
  12579. }
  12580. list_del_init(&hrq->list);
  12581. list_del_init(&drq->list);
  12582. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12583. return status;
  12584. }
  12585. /**
  12586. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12587. * @phba: The virtual port for which this call being executed.
  12588. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12589. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12590. * @xritag: the xritag that ties this io to the SGL pages.
  12591. *
  12592. * This routine will post the sgl pages for the IO that has the xritag
  12593. * that is in the iocbq structure. The xritag is assigned during iocbq
  12594. * creation and persists for as long as the driver is loaded.
  12595. * if the caller has fewer than 256 scatter gather segments to map then
  12596. * pdma_phys_addr1 should be 0.
  12597. * If the caller needs to map more than 256 scatter gather segment then
  12598. * pdma_phys_addr1 should be a valid physical address.
  12599. * physical address for SGLs must be 64 byte aligned.
  12600. * If you are going to map 2 SGL's then the first one must have 256 entries
  12601. * the second sgl can have between 1 and 256 entries.
  12602. *
  12603. * Return codes:
  12604. * 0 - Success
  12605. * -ENXIO, -ENOMEM - Failure
  12606. **/
  12607. int
  12608. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12609. dma_addr_t pdma_phys_addr0,
  12610. dma_addr_t pdma_phys_addr1,
  12611. uint16_t xritag)
  12612. {
  12613. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12614. LPFC_MBOXQ_t *mbox;
  12615. int rc;
  12616. uint32_t shdr_status, shdr_add_status;
  12617. uint32_t mbox_tmo;
  12618. union lpfc_sli4_cfg_shdr *shdr;
  12619. if (xritag == NO_XRI) {
  12620. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12621. "0364 Invalid param:\n");
  12622. return -EINVAL;
  12623. }
  12624. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12625. if (!mbox)
  12626. return -ENOMEM;
  12627. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12628. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12629. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12630. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12631. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12632. &mbox->u.mqe.un.post_sgl_pages;
  12633. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12634. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12635. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12636. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12637. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12638. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12639. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12640. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12641. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12642. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12643. if (!phba->sli4_hba.intr_enable)
  12644. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12645. else {
  12646. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12647. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12648. }
  12649. /* The IOCTL status is embedded in the mailbox subheader. */
  12650. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12651. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12652. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12653. if (rc != MBX_TIMEOUT)
  12654. mempool_free(mbox, phba->mbox_mem_pool);
  12655. if (shdr_status || shdr_add_status || rc) {
  12656. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12657. "2511 POST_SGL mailbox failed with "
  12658. "status x%x add_status x%x, mbx status x%x\n",
  12659. shdr_status, shdr_add_status, rc);
  12660. rc = -ENXIO;
  12661. }
  12662. return 0;
  12663. }
  12664. /**
  12665. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12666. * @phba: pointer to lpfc hba data structure.
  12667. *
  12668. * This routine is invoked to post rpi header templates to the
  12669. * HBA consistent with the SLI-4 interface spec. This routine
  12670. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12671. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12672. *
  12673. * Returns
  12674. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12675. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12676. **/
  12677. uint16_t
  12678. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12679. {
  12680. unsigned long xri;
  12681. /*
  12682. * Fetch the next logical xri. Because this index is logical,
  12683. * the driver starts at 0 each time.
  12684. */
  12685. spin_lock_irq(&phba->hbalock);
  12686. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12687. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12688. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12689. spin_unlock_irq(&phba->hbalock);
  12690. return NO_XRI;
  12691. } else {
  12692. set_bit(xri, phba->sli4_hba.xri_bmask);
  12693. phba->sli4_hba.max_cfg_param.xri_used++;
  12694. }
  12695. spin_unlock_irq(&phba->hbalock);
  12696. return xri;
  12697. }
  12698. /**
  12699. * lpfc_sli4_free_xri - Release an xri for reuse.
  12700. * @phba: pointer to lpfc hba data structure.
  12701. *
  12702. * This routine is invoked to release an xri to the pool of
  12703. * available rpis maintained by the driver.
  12704. **/
  12705. void
  12706. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12707. {
  12708. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12709. phba->sli4_hba.max_cfg_param.xri_used--;
  12710. }
  12711. }
  12712. /**
  12713. * lpfc_sli4_free_xri - Release an xri for reuse.
  12714. * @phba: pointer to lpfc hba data structure.
  12715. *
  12716. * This routine is invoked to release an xri to the pool of
  12717. * available rpis maintained by the driver.
  12718. **/
  12719. void
  12720. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12721. {
  12722. spin_lock_irq(&phba->hbalock);
  12723. __lpfc_sli4_free_xri(phba, xri);
  12724. spin_unlock_irq(&phba->hbalock);
  12725. }
  12726. /**
  12727. * lpfc_sli4_next_xritag - Get an xritag for the io
  12728. * @phba: Pointer to HBA context object.
  12729. *
  12730. * This function gets an xritag for the iocb. If there is no unused xritag
  12731. * it will return 0xffff.
  12732. * The function returns the allocated xritag if successful, else returns zero.
  12733. * Zero is not a valid xritag.
  12734. * The caller is not required to hold any lock.
  12735. **/
  12736. uint16_t
  12737. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12738. {
  12739. uint16_t xri_index;
  12740. xri_index = lpfc_sli4_alloc_xri(phba);
  12741. if (xri_index == NO_XRI)
  12742. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12743. "2004 Failed to allocate XRI.last XRITAG is %d"
  12744. " Max XRI is %d, Used XRI is %d\n",
  12745. xri_index,
  12746. phba->sli4_hba.max_cfg_param.max_xri,
  12747. phba->sli4_hba.max_cfg_param.xri_used);
  12748. return xri_index;
  12749. }
  12750. /**
  12751. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12752. * @phba: pointer to lpfc hba data structure.
  12753. * @post_sgl_list: pointer to els sgl entry list.
  12754. * @count: number of els sgl entries on the list.
  12755. *
  12756. * This routine is invoked to post a block of driver's sgl pages to the
  12757. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12758. * is only called when the driver is loading and after all IO has been
  12759. * stopped.
  12760. **/
  12761. static int
  12762. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12763. struct list_head *post_sgl_list,
  12764. int post_cnt)
  12765. {
  12766. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12767. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12768. struct sgl_page_pairs *sgl_pg_pairs;
  12769. void *viraddr;
  12770. LPFC_MBOXQ_t *mbox;
  12771. uint32_t reqlen, alloclen, pg_pairs;
  12772. uint32_t mbox_tmo;
  12773. uint16_t xritag_start = 0;
  12774. int rc = 0;
  12775. uint32_t shdr_status, shdr_add_status;
  12776. union lpfc_sli4_cfg_shdr *shdr;
  12777. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12778. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12779. if (reqlen > SLI4_PAGE_SIZE) {
  12780. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12781. "2559 Block sgl registration required DMA "
  12782. "size (%d) great than a page\n", reqlen);
  12783. return -ENOMEM;
  12784. }
  12785. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12786. if (!mbox)
  12787. return -ENOMEM;
  12788. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12789. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12790. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12791. LPFC_SLI4_MBX_NEMBED);
  12792. if (alloclen < reqlen) {
  12793. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12794. "0285 Allocated DMA memory size (%d) is "
  12795. "less than the requested DMA memory "
  12796. "size (%d)\n", alloclen, reqlen);
  12797. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12798. return -ENOMEM;
  12799. }
  12800. /* Set up the SGL pages in the non-embedded DMA pages */
  12801. viraddr = mbox->sge_array->addr[0];
  12802. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12803. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12804. pg_pairs = 0;
  12805. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12806. /* Set up the sge entry */
  12807. sgl_pg_pairs->sgl_pg0_addr_lo =
  12808. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12809. sgl_pg_pairs->sgl_pg0_addr_hi =
  12810. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12811. sgl_pg_pairs->sgl_pg1_addr_lo =
  12812. cpu_to_le32(putPaddrLow(0));
  12813. sgl_pg_pairs->sgl_pg1_addr_hi =
  12814. cpu_to_le32(putPaddrHigh(0));
  12815. /* Keep the first xritag on the list */
  12816. if (pg_pairs == 0)
  12817. xritag_start = sglq_entry->sli4_xritag;
  12818. sgl_pg_pairs++;
  12819. pg_pairs++;
  12820. }
  12821. /* Complete initialization and perform endian conversion. */
  12822. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12823. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12824. sgl->word0 = cpu_to_le32(sgl->word0);
  12825. if (!phba->sli4_hba.intr_enable)
  12826. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12827. else {
  12828. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12829. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12830. }
  12831. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12832. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12833. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12834. if (rc != MBX_TIMEOUT)
  12835. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12836. if (shdr_status || shdr_add_status || rc) {
  12837. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12838. "2513 POST_SGL_BLOCK mailbox command failed "
  12839. "status x%x add_status x%x mbx status x%x\n",
  12840. shdr_status, shdr_add_status, rc);
  12841. rc = -ENXIO;
  12842. }
  12843. return rc;
  12844. }
  12845. /**
  12846. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12847. * @phba: pointer to lpfc hba data structure.
  12848. * @sblist: pointer to scsi buffer list.
  12849. * @count: number of scsi buffers on the list.
  12850. *
  12851. * This routine is invoked to post a block of @count scsi sgl pages from a
  12852. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12853. * No Lock is held.
  12854. *
  12855. **/
  12856. int
  12857. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12858. struct list_head *sblist,
  12859. int count)
  12860. {
  12861. struct lpfc_scsi_buf *psb;
  12862. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12863. struct sgl_page_pairs *sgl_pg_pairs;
  12864. void *viraddr;
  12865. LPFC_MBOXQ_t *mbox;
  12866. uint32_t reqlen, alloclen, pg_pairs;
  12867. uint32_t mbox_tmo;
  12868. uint16_t xritag_start = 0;
  12869. int rc = 0;
  12870. uint32_t shdr_status, shdr_add_status;
  12871. dma_addr_t pdma_phys_bpl1;
  12872. union lpfc_sli4_cfg_shdr *shdr;
  12873. /* Calculate the requested length of the dma memory */
  12874. reqlen = count * sizeof(struct sgl_page_pairs) +
  12875. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12876. if (reqlen > SLI4_PAGE_SIZE) {
  12877. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12878. "0217 Block sgl registration required DMA "
  12879. "size (%d) great than a page\n", reqlen);
  12880. return -ENOMEM;
  12881. }
  12882. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12883. if (!mbox) {
  12884. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12885. "0283 Failed to allocate mbox cmd memory\n");
  12886. return -ENOMEM;
  12887. }
  12888. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12889. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12890. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12891. LPFC_SLI4_MBX_NEMBED);
  12892. if (alloclen < reqlen) {
  12893. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12894. "2561 Allocated DMA memory size (%d) is "
  12895. "less than the requested DMA memory "
  12896. "size (%d)\n", alloclen, reqlen);
  12897. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12898. return -ENOMEM;
  12899. }
  12900. /* Get the first SGE entry from the non-embedded DMA memory */
  12901. viraddr = mbox->sge_array->addr[0];
  12902. /* Set up the SGL pages in the non-embedded DMA pages */
  12903. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12904. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12905. pg_pairs = 0;
  12906. list_for_each_entry(psb, sblist, list) {
  12907. /* Set up the sge entry */
  12908. sgl_pg_pairs->sgl_pg0_addr_lo =
  12909. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12910. sgl_pg_pairs->sgl_pg0_addr_hi =
  12911. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12912. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12913. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12914. else
  12915. pdma_phys_bpl1 = 0;
  12916. sgl_pg_pairs->sgl_pg1_addr_lo =
  12917. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12918. sgl_pg_pairs->sgl_pg1_addr_hi =
  12919. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12920. /* Keep the first xritag on the list */
  12921. if (pg_pairs == 0)
  12922. xritag_start = psb->cur_iocbq.sli4_xritag;
  12923. sgl_pg_pairs++;
  12924. pg_pairs++;
  12925. }
  12926. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12927. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12928. /* Perform endian conversion if necessary */
  12929. sgl->word0 = cpu_to_le32(sgl->word0);
  12930. if (!phba->sli4_hba.intr_enable)
  12931. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12932. else {
  12933. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12934. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12935. }
  12936. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12937. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12938. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12939. if (rc != MBX_TIMEOUT)
  12940. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12941. if (shdr_status || shdr_add_status || rc) {
  12942. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12943. "2564 POST_SGL_BLOCK mailbox command failed "
  12944. "status x%x add_status x%x mbx status x%x\n",
  12945. shdr_status, shdr_add_status, rc);
  12946. rc = -ENXIO;
  12947. }
  12948. return rc;
  12949. }
  12950. /**
  12951. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12952. * @phba: pointer to lpfc_hba struct that the frame was received on
  12953. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12954. *
  12955. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12956. * valid type of frame that the LPFC driver will handle. This function will
  12957. * return a zero if the frame is a valid frame or a non zero value when the
  12958. * frame does not pass the check.
  12959. **/
  12960. static int
  12961. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12962. {
  12963. /* make rctl_names static to save stack space */
  12964. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12965. char *type_names[] = FC_TYPE_NAMES_INIT;
  12966. struct fc_vft_header *fc_vft_hdr;
  12967. uint32_t *header = (uint32_t *) fc_hdr;
  12968. switch (fc_hdr->fh_r_ctl) {
  12969. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12970. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12971. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12972. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12973. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12974. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12975. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12976. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12977. case FC_RCTL_ELS_REQ: /* extended link services request */
  12978. case FC_RCTL_ELS_REP: /* extended link services reply */
  12979. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12980. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12981. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12982. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12983. case FC_RCTL_BA_RMC: /* remove connection */
  12984. case FC_RCTL_BA_ACC: /* basic accept */
  12985. case FC_RCTL_BA_RJT: /* basic reject */
  12986. case FC_RCTL_BA_PRMT:
  12987. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12988. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12989. case FC_RCTL_P_RJT: /* port reject */
  12990. case FC_RCTL_F_RJT: /* fabric reject */
  12991. case FC_RCTL_P_BSY: /* port busy */
  12992. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12993. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12994. case FC_RCTL_LCR: /* link credit reset */
  12995. case FC_RCTL_END: /* end */
  12996. break;
  12997. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12998. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12999. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  13000. return lpfc_fc_frame_check(phba, fc_hdr);
  13001. default:
  13002. goto drop;
  13003. }
  13004. switch (fc_hdr->fh_type) {
  13005. case FC_TYPE_BLS:
  13006. case FC_TYPE_ELS:
  13007. case FC_TYPE_FCP:
  13008. case FC_TYPE_CT:
  13009. break;
  13010. case FC_TYPE_IP:
  13011. case FC_TYPE_ILS:
  13012. default:
  13013. goto drop;
  13014. }
  13015. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  13016. "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
  13017. "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
  13018. rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
  13019. type_names[fc_hdr->fh_type], fc_hdr->fh_type,
  13020. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  13021. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  13022. be32_to_cpu(header[4]), be32_to_cpu(header[5]),
  13023. be32_to_cpu(header[6]));
  13024. return 0;
  13025. drop:
  13026. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  13027. "2539 Dropped frame rctl:%s type:%s\n",
  13028. rctl_names[fc_hdr->fh_r_ctl],
  13029. type_names[fc_hdr->fh_type]);
  13030. return 1;
  13031. }
  13032. /**
  13033. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  13034. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13035. *
  13036. * This function processes the FC header to retrieve the VFI from the VF
  13037. * header, if one exists. This function will return the VFI if one exists
  13038. * or 0 if no VSAN Header exists.
  13039. **/
  13040. static uint32_t
  13041. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  13042. {
  13043. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  13044. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  13045. return 0;
  13046. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  13047. }
  13048. /**
  13049. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  13050. * @phba: Pointer to the HBA structure to search for the vport on
  13051. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  13052. * @fcfi: The FC Fabric ID that the frame came from
  13053. *
  13054. * This function searches the @phba for a vport that matches the content of the
  13055. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  13056. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  13057. * returns the matching vport pointer or NULL if unable to match frame to a
  13058. * vport.
  13059. **/
  13060. static struct lpfc_vport *
  13061. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  13062. uint16_t fcfi)
  13063. {
  13064. struct lpfc_vport **vports;
  13065. struct lpfc_vport *vport = NULL;
  13066. int i;
  13067. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  13068. fc_hdr->fh_d_id[1] << 8 |
  13069. fc_hdr->fh_d_id[2]);
  13070. if (did == Fabric_DID)
  13071. return phba->pport;
  13072. if ((phba->pport->fc_flag & FC_PT2PT) &&
  13073. !(phba->link_state == LPFC_HBA_READY))
  13074. return phba->pport;
  13075. vports = lpfc_create_vport_work_array(phba);
  13076. if (vports != NULL)
  13077. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  13078. if (phba->fcf.fcfi == fcfi &&
  13079. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  13080. vports[i]->fc_myDID == did) {
  13081. vport = vports[i];
  13082. break;
  13083. }
  13084. }
  13085. lpfc_destroy_vport_work_array(phba, vports);
  13086. return vport;
  13087. }
  13088. /**
  13089. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  13090. * @vport: The vport to work on.
  13091. *
  13092. * This function updates the receive sequence time stamp for this vport. The
  13093. * receive sequence time stamp indicates the time that the last frame of the
  13094. * the sequence that has been idle for the longest amount of time was received.
  13095. * the driver uses this time stamp to indicate if any received sequences have
  13096. * timed out.
  13097. **/
  13098. void
  13099. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  13100. {
  13101. struct lpfc_dmabuf *h_buf;
  13102. struct hbq_dmabuf *dmabuf = NULL;
  13103. /* get the oldest sequence on the rcv list */
  13104. h_buf = list_get_first(&vport->rcv_buffer_list,
  13105. struct lpfc_dmabuf, list);
  13106. if (!h_buf)
  13107. return;
  13108. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13109. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  13110. }
  13111. /**
  13112. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  13113. * @vport: The vport that the received sequences were sent to.
  13114. *
  13115. * This function cleans up all outstanding received sequences. This is called
  13116. * by the driver when a link event or user action invalidates all the received
  13117. * sequences.
  13118. **/
  13119. void
  13120. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  13121. {
  13122. struct lpfc_dmabuf *h_buf, *hnext;
  13123. struct lpfc_dmabuf *d_buf, *dnext;
  13124. struct hbq_dmabuf *dmabuf = NULL;
  13125. /* start with the oldest sequence on the rcv list */
  13126. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13127. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13128. list_del_init(&dmabuf->hbuf.list);
  13129. list_for_each_entry_safe(d_buf, dnext,
  13130. &dmabuf->dbuf.list, list) {
  13131. list_del_init(&d_buf->list);
  13132. lpfc_in_buf_free(vport->phba, d_buf);
  13133. }
  13134. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13135. }
  13136. }
  13137. /**
  13138. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  13139. * @vport: The vport that the received sequences were sent to.
  13140. *
  13141. * This function determines whether any received sequences have timed out by
  13142. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  13143. * indicates that there is at least one timed out sequence this routine will
  13144. * go through the received sequences one at a time from most inactive to most
  13145. * active to determine which ones need to be cleaned up. Once it has determined
  13146. * that a sequence needs to be cleaned up it will simply free up the resources
  13147. * without sending an abort.
  13148. **/
  13149. void
  13150. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  13151. {
  13152. struct lpfc_dmabuf *h_buf, *hnext;
  13153. struct lpfc_dmabuf *d_buf, *dnext;
  13154. struct hbq_dmabuf *dmabuf = NULL;
  13155. unsigned long timeout;
  13156. int abort_count = 0;
  13157. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13158. vport->rcv_buffer_time_stamp);
  13159. if (list_empty(&vport->rcv_buffer_list) ||
  13160. time_before(jiffies, timeout))
  13161. return;
  13162. /* start with the oldest sequence on the rcv list */
  13163. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  13164. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13165. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  13166. dmabuf->time_stamp);
  13167. if (time_before(jiffies, timeout))
  13168. break;
  13169. abort_count++;
  13170. list_del_init(&dmabuf->hbuf.list);
  13171. list_for_each_entry_safe(d_buf, dnext,
  13172. &dmabuf->dbuf.list, list) {
  13173. list_del_init(&d_buf->list);
  13174. lpfc_in_buf_free(vport->phba, d_buf);
  13175. }
  13176. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  13177. }
  13178. if (abort_count)
  13179. lpfc_update_rcv_time_stamp(vport);
  13180. }
  13181. /**
  13182. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  13183. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  13184. *
  13185. * This function searches through the existing incomplete sequences that have
  13186. * been sent to this @vport. If the frame matches one of the incomplete
  13187. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  13188. * make up that sequence. If no sequence is found that matches this frame then
  13189. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  13190. * This function returns a pointer to the first dmabuf in the sequence list that
  13191. * the frame was linked to.
  13192. **/
  13193. static struct hbq_dmabuf *
  13194. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13195. {
  13196. struct fc_frame_header *new_hdr;
  13197. struct fc_frame_header *temp_hdr;
  13198. struct lpfc_dmabuf *d_buf;
  13199. struct lpfc_dmabuf *h_buf;
  13200. struct hbq_dmabuf *seq_dmabuf = NULL;
  13201. struct hbq_dmabuf *temp_dmabuf = NULL;
  13202. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13203. dmabuf->time_stamp = jiffies;
  13204. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13205. /* Use the hdr_buf to find the sequence that this frame belongs to */
  13206. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13207. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13208. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13209. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13210. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13211. continue;
  13212. /* found a pending sequence that matches this frame */
  13213. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13214. break;
  13215. }
  13216. if (!seq_dmabuf) {
  13217. /*
  13218. * This indicates first frame received for this sequence.
  13219. * Queue the buffer on the vport's rcv_buffer_list.
  13220. */
  13221. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13222. lpfc_update_rcv_time_stamp(vport);
  13223. return dmabuf;
  13224. }
  13225. temp_hdr = seq_dmabuf->hbuf.virt;
  13226. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  13227. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13228. list_del_init(&seq_dmabuf->hbuf.list);
  13229. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13230. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13231. lpfc_update_rcv_time_stamp(vport);
  13232. return dmabuf;
  13233. }
  13234. /* move this sequence to the tail to indicate a young sequence */
  13235. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  13236. seq_dmabuf->time_stamp = jiffies;
  13237. lpfc_update_rcv_time_stamp(vport);
  13238. if (list_empty(&seq_dmabuf->dbuf.list)) {
  13239. temp_hdr = dmabuf->hbuf.virt;
  13240. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  13241. return seq_dmabuf;
  13242. }
  13243. /* find the correct place in the sequence to insert this frame */
  13244. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  13245. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13246. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  13247. /*
  13248. * If the frame's sequence count is greater than the frame on
  13249. * the list then insert the frame right after this frame
  13250. */
  13251. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  13252. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13253. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  13254. return seq_dmabuf;
  13255. }
  13256. }
  13257. return NULL;
  13258. }
  13259. /**
  13260. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  13261. * @vport: pointer to a vitural port
  13262. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13263. *
  13264. * This function tries to abort from the partially assembed sequence, described
  13265. * by the information from basic abbort @dmabuf. It checks to see whether such
  13266. * partially assembled sequence held by the driver. If so, it shall free up all
  13267. * the frames from the partially assembled sequence.
  13268. *
  13269. * Return
  13270. * true -- if there is matching partially assembled sequence present and all
  13271. * the frames freed with the sequence;
  13272. * false -- if there is no matching partially assembled sequence present so
  13273. * nothing got aborted in the lower layer driver
  13274. **/
  13275. static bool
  13276. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  13277. struct hbq_dmabuf *dmabuf)
  13278. {
  13279. struct fc_frame_header *new_hdr;
  13280. struct fc_frame_header *temp_hdr;
  13281. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  13282. struct hbq_dmabuf *seq_dmabuf = NULL;
  13283. /* Use the hdr_buf to find the sequence that matches this frame */
  13284. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13285. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  13286. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13287. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13288. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13289. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13290. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13291. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13292. continue;
  13293. /* found a pending sequence that matches this frame */
  13294. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13295. break;
  13296. }
  13297. /* Free up all the frames from the partially assembled sequence */
  13298. if (seq_dmabuf) {
  13299. list_for_each_entry_safe(d_buf, n_buf,
  13300. &seq_dmabuf->dbuf.list, list) {
  13301. list_del_init(&d_buf->list);
  13302. lpfc_in_buf_free(vport->phba, d_buf);
  13303. }
  13304. return true;
  13305. }
  13306. return false;
  13307. }
  13308. /**
  13309. * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
  13310. * @vport: pointer to a vitural port
  13311. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13312. *
  13313. * This function tries to abort from the assembed sequence from upper level
  13314. * protocol, described by the information from basic abbort @dmabuf. It
  13315. * checks to see whether such pending context exists at upper level protocol.
  13316. * If so, it shall clean up the pending context.
  13317. *
  13318. * Return
  13319. * true -- if there is matching pending context of the sequence cleaned
  13320. * at ulp;
  13321. * false -- if there is no matching pending context of the sequence present
  13322. * at ulp.
  13323. **/
  13324. static bool
  13325. lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13326. {
  13327. struct lpfc_hba *phba = vport->phba;
  13328. int handled;
  13329. /* Accepting abort at ulp with SLI4 only */
  13330. if (phba->sli_rev < LPFC_SLI_REV4)
  13331. return false;
  13332. /* Register all caring upper level protocols to attend abort */
  13333. handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
  13334. if (handled)
  13335. return true;
  13336. return false;
  13337. }
  13338. /**
  13339. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  13340. * @phba: Pointer to HBA context object.
  13341. * @cmd_iocbq: pointer to the command iocbq structure.
  13342. * @rsp_iocbq: pointer to the response iocbq structure.
  13343. *
  13344. * This function handles the sequence abort response iocb command complete
  13345. * event. It properly releases the memory allocated to the sequence abort
  13346. * accept iocb.
  13347. **/
  13348. static void
  13349. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  13350. struct lpfc_iocbq *cmd_iocbq,
  13351. struct lpfc_iocbq *rsp_iocbq)
  13352. {
  13353. struct lpfc_nodelist *ndlp;
  13354. if (cmd_iocbq) {
  13355. ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
  13356. lpfc_nlp_put(ndlp);
  13357. lpfc_nlp_not_used(ndlp);
  13358. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  13359. }
  13360. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  13361. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  13362. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13363. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  13364. rsp_iocbq->iocb.ulpStatus,
  13365. rsp_iocbq->iocb.un.ulpWord[4]);
  13366. }
  13367. /**
  13368. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  13369. * @phba: Pointer to HBA context object.
  13370. * @xri: xri id in transaction.
  13371. *
  13372. * This function validates the xri maps to the known range of XRIs allocated an
  13373. * used by the driver.
  13374. **/
  13375. uint16_t
  13376. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  13377. uint16_t xri)
  13378. {
  13379. int i;
  13380. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  13381. if (xri == phba->sli4_hba.xri_ids[i])
  13382. return i;
  13383. }
  13384. return NO_XRI;
  13385. }
  13386. /**
  13387. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  13388. * @phba: Pointer to HBA context object.
  13389. * @fc_hdr: pointer to a FC frame header.
  13390. *
  13391. * This function sends a basic response to a previous unsol sequence abort
  13392. * event after aborting the sequence handling.
  13393. **/
  13394. static void
  13395. lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
  13396. struct fc_frame_header *fc_hdr, bool aborted)
  13397. {
  13398. struct lpfc_hba *phba = vport->phba;
  13399. struct lpfc_iocbq *ctiocb = NULL;
  13400. struct lpfc_nodelist *ndlp;
  13401. uint16_t oxid, rxid, xri, lxri;
  13402. uint32_t sid, fctl;
  13403. IOCB_t *icmd;
  13404. int rc;
  13405. if (!lpfc_is_link_up(phba))
  13406. return;
  13407. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13408. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  13409. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  13410. ndlp = lpfc_findnode_did(vport, sid);
  13411. if (!ndlp) {
  13412. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  13413. if (!ndlp) {
  13414. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13415. "1268 Failed to allocate ndlp for "
  13416. "oxid:x%x SID:x%x\n", oxid, sid);
  13417. return;
  13418. }
  13419. lpfc_nlp_init(vport, ndlp, sid);
  13420. /* Put ndlp onto pport node list */
  13421. lpfc_enqueue_node(vport, ndlp);
  13422. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  13423. /* re-setup ndlp without removing from node list */
  13424. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  13425. if (!ndlp) {
  13426. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13427. "3275 Failed to active ndlp found "
  13428. "for oxid:x%x SID:x%x\n", oxid, sid);
  13429. return;
  13430. }
  13431. }
  13432. /* Allocate buffer for rsp iocb */
  13433. ctiocb = lpfc_sli_get_iocbq(phba);
  13434. if (!ctiocb)
  13435. return;
  13436. /* Extract the F_CTL field from FC_HDR */
  13437. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  13438. icmd = &ctiocb->iocb;
  13439. icmd->un.xseq64.bdl.bdeSize = 0;
  13440. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  13441. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13442. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13443. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13444. /* Fill in the rest of iocb fields */
  13445. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13446. icmd->ulpBdeCount = 0;
  13447. icmd->ulpLe = 1;
  13448. icmd->ulpClass = CLASS3;
  13449. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13450. ctiocb->context1 = lpfc_nlp_get(ndlp);
  13451. ctiocb->iocb_cmpl = NULL;
  13452. ctiocb->vport = phba->pport;
  13453. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  13454. ctiocb->sli4_lxritag = NO_XRI;
  13455. ctiocb->sli4_xritag = NO_XRI;
  13456. if (fctl & FC_FC_EX_CTX)
  13457. /* Exchange responder sent the abort so we
  13458. * own the oxid.
  13459. */
  13460. xri = oxid;
  13461. else
  13462. xri = rxid;
  13463. lxri = lpfc_sli4_xri_inrange(phba, xri);
  13464. if (lxri != NO_XRI)
  13465. lpfc_set_rrq_active(phba, ndlp, lxri,
  13466. (xri == oxid) ? rxid : oxid, 0);
  13467. /* For BA_ABTS from exchange responder, if the logical xri with
  13468. * the oxid maps to the FCP XRI range, the port no longer has
  13469. * that exchange context, send a BLS_RJT. Override the IOCB for
  13470. * a BA_RJT.
  13471. */
  13472. if ((fctl & FC_FC_EX_CTX) &&
  13473. (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
  13474. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13475. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13476. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13477. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13478. }
  13479. /* If BA_ABTS failed to abort a partially assembled receive sequence,
  13480. * the driver no longer has that exchange, send a BLS_RJT. Override
  13481. * the IOCB for a BA_RJT.
  13482. */
  13483. if (aborted == false) {
  13484. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13485. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13486. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13487. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13488. }
  13489. if (fctl & FC_FC_EX_CTX) {
  13490. /* ABTS sent by responder to CT exchange, construction
  13491. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13492. * field and RX_ID from ABTS for RX_ID field.
  13493. */
  13494. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13495. } else {
  13496. /* ABTS sent by initiator to CT exchange, construction
  13497. * of BA_ACC will need to allocate a new XRI as for the
  13498. * XRI_TAG field.
  13499. */
  13500. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13501. }
  13502. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13503. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13504. /* Xmit CT abts response on exchange <xid> */
  13505. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  13506. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13507. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13508. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13509. if (rc == IOCB_ERROR) {
  13510. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  13511. "2925 Failed to issue CT ABTS RSP x%x on "
  13512. "xri x%x, Data x%x\n",
  13513. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13514. phba->link_state);
  13515. lpfc_nlp_put(ndlp);
  13516. ctiocb->context1 = NULL;
  13517. lpfc_sli_release_iocbq(phba, ctiocb);
  13518. }
  13519. }
  13520. /**
  13521. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13522. * @vport: Pointer to the vport on which this sequence was received
  13523. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13524. *
  13525. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13526. * receive sequence is only partially assembed by the driver, it shall abort
  13527. * the partially assembled frames for the sequence. Otherwise, if the
  13528. * unsolicited receive sequence has been completely assembled and passed to
  13529. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13530. * unsolicited sequence has been aborted. After that, it will issue a basic
  13531. * accept to accept the abort.
  13532. **/
  13533. void
  13534. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13535. struct hbq_dmabuf *dmabuf)
  13536. {
  13537. struct lpfc_hba *phba = vport->phba;
  13538. struct fc_frame_header fc_hdr;
  13539. uint32_t fctl;
  13540. bool aborted;
  13541. /* Make a copy of fc_hdr before the dmabuf being released */
  13542. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13543. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13544. if (fctl & FC_FC_EX_CTX) {
  13545. /* ABTS by responder to exchange, no cleanup needed */
  13546. aborted = true;
  13547. } else {
  13548. /* ABTS by initiator to exchange, need to do cleanup */
  13549. aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13550. if (aborted == false)
  13551. aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
  13552. }
  13553. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13554. /* Respond with BA_ACC or BA_RJT accordingly */
  13555. lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
  13556. }
  13557. /**
  13558. * lpfc_seq_complete - Indicates if a sequence is complete
  13559. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13560. *
  13561. * This function checks the sequence, starting with the frame described by
  13562. * @dmabuf, to see if all the frames associated with this sequence are present.
  13563. * the frames associated with this sequence are linked to the @dmabuf using the
  13564. * dbuf list. This function looks for two major things. 1) That the first frame
  13565. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13566. * set. 3) That there are no holes in the sequence count. The function will
  13567. * return 1 when the sequence is complete, otherwise it will return 0.
  13568. **/
  13569. static int
  13570. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13571. {
  13572. struct fc_frame_header *hdr;
  13573. struct lpfc_dmabuf *d_buf;
  13574. struct hbq_dmabuf *seq_dmabuf;
  13575. uint32_t fctl;
  13576. int seq_count = 0;
  13577. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13578. /* make sure first fame of sequence has a sequence count of zero */
  13579. if (hdr->fh_seq_cnt != seq_count)
  13580. return 0;
  13581. fctl = (hdr->fh_f_ctl[0] << 16 |
  13582. hdr->fh_f_ctl[1] << 8 |
  13583. hdr->fh_f_ctl[2]);
  13584. /* If last frame of sequence we can return success. */
  13585. if (fctl & FC_FC_END_SEQ)
  13586. return 1;
  13587. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13588. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13589. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13590. /* If there is a hole in the sequence count then fail. */
  13591. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13592. return 0;
  13593. fctl = (hdr->fh_f_ctl[0] << 16 |
  13594. hdr->fh_f_ctl[1] << 8 |
  13595. hdr->fh_f_ctl[2]);
  13596. /* If last frame of sequence we can return success. */
  13597. if (fctl & FC_FC_END_SEQ)
  13598. return 1;
  13599. }
  13600. return 0;
  13601. }
  13602. /**
  13603. * lpfc_prep_seq - Prep sequence for ULP processing
  13604. * @vport: Pointer to the vport on which this sequence was received
  13605. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13606. *
  13607. * This function takes a sequence, described by a list of frames, and creates
  13608. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13609. * used to issue to the generic unsolicited sequence handler. This routine
  13610. * returns a pointer to the first iocbq in the list. If the function is unable
  13611. * to allocate an iocbq then it throw out the received frames that were not
  13612. * able to be described and return a pointer to the first iocbq. If unable to
  13613. * allocate any iocbqs (including the first) this function will return NULL.
  13614. **/
  13615. static struct lpfc_iocbq *
  13616. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13617. {
  13618. struct hbq_dmabuf *hbq_buf;
  13619. struct lpfc_dmabuf *d_buf, *n_buf;
  13620. struct lpfc_iocbq *first_iocbq, *iocbq;
  13621. struct fc_frame_header *fc_hdr;
  13622. uint32_t sid;
  13623. uint32_t len, tot_len;
  13624. struct ulp_bde64 *pbde;
  13625. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13626. /* remove from receive buffer list */
  13627. list_del_init(&seq_dmabuf->hbuf.list);
  13628. lpfc_update_rcv_time_stamp(vport);
  13629. /* get the Remote Port's SID */
  13630. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13631. tot_len = 0;
  13632. /* Get an iocbq struct to fill in. */
  13633. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13634. if (first_iocbq) {
  13635. /* Initialize the first IOCB. */
  13636. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13637. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13638. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13639. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13640. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13641. first_iocbq->iocb.un.rcvels.parmRo =
  13642. sli4_did_from_fc_hdr(fc_hdr);
  13643. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13644. } else
  13645. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13646. first_iocbq->iocb.ulpContext = NO_XRI;
  13647. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13648. be16_to_cpu(fc_hdr->fh_ox_id);
  13649. /* iocbq is prepped for internal consumption. Physical vpi. */
  13650. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13651. vport->phba->vpi_ids[vport->vpi];
  13652. /* put the first buffer into the first IOCBq */
  13653. tot_len = bf_get(lpfc_rcqe_length,
  13654. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13655. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13656. first_iocbq->context3 = NULL;
  13657. first_iocbq->iocb.ulpBdeCount = 1;
  13658. if (tot_len > LPFC_DATA_BUF_SIZE)
  13659. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13660. LPFC_DATA_BUF_SIZE;
  13661. else
  13662. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
  13663. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13664. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13665. }
  13666. iocbq = first_iocbq;
  13667. /*
  13668. * Each IOCBq can have two Buffers assigned, so go through the list
  13669. * of buffers for this sequence and save two buffers in each IOCBq
  13670. */
  13671. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13672. if (!iocbq) {
  13673. lpfc_in_buf_free(vport->phba, d_buf);
  13674. continue;
  13675. }
  13676. if (!iocbq->context3) {
  13677. iocbq->context3 = d_buf;
  13678. iocbq->iocb.ulpBdeCount++;
  13679. /* We need to get the size out of the right CQE */
  13680. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13681. len = bf_get(lpfc_rcqe_length,
  13682. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13683. pbde = (struct ulp_bde64 *)
  13684. &iocbq->iocb.unsli3.sli3Words[4];
  13685. if (len > LPFC_DATA_BUF_SIZE)
  13686. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13687. else
  13688. pbde->tus.f.bdeSize = len;
  13689. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13690. tot_len += len;
  13691. } else {
  13692. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13693. if (!iocbq) {
  13694. if (first_iocbq) {
  13695. first_iocbq->iocb.ulpStatus =
  13696. IOSTAT_FCP_RSP_ERROR;
  13697. first_iocbq->iocb.un.ulpWord[4] =
  13698. IOERR_NO_RESOURCES;
  13699. }
  13700. lpfc_in_buf_free(vport->phba, d_buf);
  13701. continue;
  13702. }
  13703. /* We need to get the size out of the right CQE */
  13704. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13705. len = bf_get(lpfc_rcqe_length,
  13706. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13707. iocbq->context2 = d_buf;
  13708. iocbq->context3 = NULL;
  13709. iocbq->iocb.ulpBdeCount = 1;
  13710. if (len > LPFC_DATA_BUF_SIZE)
  13711. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13712. LPFC_DATA_BUF_SIZE;
  13713. else
  13714. iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
  13715. tot_len += len;
  13716. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13717. iocbq->iocb.un.rcvels.remoteID = sid;
  13718. list_add_tail(&iocbq->list, &first_iocbq->list);
  13719. }
  13720. }
  13721. return first_iocbq;
  13722. }
  13723. static void
  13724. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13725. struct hbq_dmabuf *seq_dmabuf)
  13726. {
  13727. struct fc_frame_header *fc_hdr;
  13728. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13729. struct lpfc_hba *phba = vport->phba;
  13730. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13731. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13732. if (!iocbq) {
  13733. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13734. "2707 Ring %d handler: Failed to allocate "
  13735. "iocb Rctl x%x Type x%x received\n",
  13736. LPFC_ELS_RING,
  13737. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13738. return;
  13739. }
  13740. if (!lpfc_complete_unsol_iocb(phba,
  13741. &phba->sli.ring[LPFC_ELS_RING],
  13742. iocbq, fc_hdr->fh_r_ctl,
  13743. fc_hdr->fh_type))
  13744. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13745. "2540 Ring %d handler: unexpected Rctl "
  13746. "x%x Type x%x received\n",
  13747. LPFC_ELS_RING,
  13748. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13749. /* Free iocb created in lpfc_prep_seq */
  13750. list_for_each_entry_safe(curr_iocb, next_iocb,
  13751. &iocbq->list, list) {
  13752. list_del_init(&curr_iocb->list);
  13753. lpfc_sli_release_iocbq(phba, curr_iocb);
  13754. }
  13755. lpfc_sli_release_iocbq(phba, iocbq);
  13756. }
  13757. /**
  13758. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13759. * @phba: Pointer to HBA context object.
  13760. *
  13761. * This function is called with no lock held. This function processes all
  13762. * the received buffers and gives it to upper layers when a received buffer
  13763. * indicates that it is the final frame in the sequence. The interrupt
  13764. * service routine processes received buffers at interrupt contexts and adds
  13765. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13766. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13767. * appropriate receive function when the final frame in a sequence is received.
  13768. **/
  13769. void
  13770. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13771. struct hbq_dmabuf *dmabuf)
  13772. {
  13773. struct hbq_dmabuf *seq_dmabuf;
  13774. struct fc_frame_header *fc_hdr;
  13775. struct lpfc_vport *vport;
  13776. uint32_t fcfi;
  13777. uint32_t did;
  13778. /* Process each received buffer */
  13779. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13780. /* check to see if this a valid type of frame */
  13781. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13782. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13783. return;
  13784. }
  13785. if ((bf_get(lpfc_cqe_code,
  13786. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13787. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13788. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13789. else
  13790. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13791. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13792. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13793. if (!vport) {
  13794. /* throw out the frame */
  13795. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13796. return;
  13797. }
  13798. /* d_id this frame is directed to */
  13799. did = sli4_did_from_fc_hdr(fc_hdr);
  13800. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13801. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13802. (did != Fabric_DID)) {
  13803. /*
  13804. * Throw out the frame if we are not pt2pt.
  13805. * The pt2pt protocol allows for discovery frames
  13806. * to be received without a registered VPI.
  13807. */
  13808. if (!(vport->fc_flag & FC_PT2PT) ||
  13809. (phba->link_state == LPFC_HBA_READY)) {
  13810. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13811. return;
  13812. }
  13813. }
  13814. /* Handle the basic abort sequence (BA_ABTS) event */
  13815. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13816. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13817. return;
  13818. }
  13819. /* Link this frame */
  13820. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13821. if (!seq_dmabuf) {
  13822. /* unable to add frame to vport - throw it out */
  13823. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13824. return;
  13825. }
  13826. /* If not last frame in sequence continue processing frames. */
  13827. if (!lpfc_seq_complete(seq_dmabuf))
  13828. return;
  13829. /* Send the complete sequence to the upper layer protocol */
  13830. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13831. }
  13832. /**
  13833. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13834. * @phba: pointer to lpfc hba data structure.
  13835. *
  13836. * This routine is invoked to post rpi header templates to the
  13837. * HBA consistent with the SLI-4 interface spec. This routine
  13838. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13839. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13840. *
  13841. * This routine does not require any locks. It's usage is expected
  13842. * to be driver load or reset recovery when the driver is
  13843. * sequential.
  13844. *
  13845. * Return codes
  13846. * 0 - successful
  13847. * -EIO - The mailbox failed to complete successfully.
  13848. * When this error occurs, the driver is not guaranteed
  13849. * to have any rpi regions posted to the device and
  13850. * must either attempt to repost the regions or take a
  13851. * fatal error.
  13852. **/
  13853. int
  13854. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13855. {
  13856. struct lpfc_rpi_hdr *rpi_page;
  13857. uint32_t rc = 0;
  13858. uint16_t lrpi = 0;
  13859. /* SLI4 ports that support extents do not require RPI headers. */
  13860. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13861. goto exit;
  13862. if (phba->sli4_hba.extents_in_use)
  13863. return -EIO;
  13864. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13865. /*
  13866. * Assign the rpi headers a physical rpi only if the driver
  13867. * has not initialized those resources. A port reset only
  13868. * needs the headers posted.
  13869. */
  13870. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13871. LPFC_RPI_RSRC_RDY)
  13872. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13873. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13874. if (rc != MBX_SUCCESS) {
  13875. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13876. "2008 Error %d posting all rpi "
  13877. "headers\n", rc);
  13878. rc = -EIO;
  13879. break;
  13880. }
  13881. }
  13882. exit:
  13883. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13884. LPFC_RPI_RSRC_RDY);
  13885. return rc;
  13886. }
  13887. /**
  13888. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13889. * @phba: pointer to lpfc hba data structure.
  13890. * @rpi_page: pointer to the rpi memory region.
  13891. *
  13892. * This routine is invoked to post a single rpi header to the
  13893. * HBA consistent with the SLI-4 interface spec. This memory region
  13894. * maps up to 64 rpi context regions.
  13895. *
  13896. * Return codes
  13897. * 0 - successful
  13898. * -ENOMEM - No available memory
  13899. * -EIO - The mailbox failed to complete successfully.
  13900. **/
  13901. int
  13902. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13903. {
  13904. LPFC_MBOXQ_t *mboxq;
  13905. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13906. uint32_t rc = 0;
  13907. uint32_t shdr_status, shdr_add_status;
  13908. union lpfc_sli4_cfg_shdr *shdr;
  13909. /* SLI4 ports that support extents do not require RPI headers. */
  13910. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13911. return rc;
  13912. if (phba->sli4_hba.extents_in_use)
  13913. return -EIO;
  13914. /* The port is notified of the header region via a mailbox command. */
  13915. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13916. if (!mboxq) {
  13917. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13918. "2001 Unable to allocate memory for issuing "
  13919. "SLI_CONFIG_SPECIAL mailbox command\n");
  13920. return -ENOMEM;
  13921. }
  13922. /* Post all rpi memory regions to the port. */
  13923. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13924. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13925. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13926. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13927. sizeof(struct lpfc_sli4_cfg_mhdr),
  13928. LPFC_SLI4_MBX_EMBED);
  13929. /* Post the physical rpi to the port for this rpi header. */
  13930. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13931. rpi_page->start_rpi);
  13932. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13933. hdr_tmpl, rpi_page->page_count);
  13934. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13935. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13936. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13937. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13938. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13939. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13940. if (rc != MBX_TIMEOUT)
  13941. mempool_free(mboxq, phba->mbox_mem_pool);
  13942. if (shdr_status || shdr_add_status || rc) {
  13943. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13944. "2514 POST_RPI_HDR mailbox failed with "
  13945. "status x%x add_status x%x, mbx status x%x\n",
  13946. shdr_status, shdr_add_status, rc);
  13947. rc = -ENXIO;
  13948. }
  13949. return rc;
  13950. }
  13951. /**
  13952. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13953. * @phba: pointer to lpfc hba data structure.
  13954. *
  13955. * This routine is invoked to post rpi header templates to the
  13956. * HBA consistent with the SLI-4 interface spec. This routine
  13957. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13958. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13959. *
  13960. * Returns
  13961. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13962. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13963. **/
  13964. int
  13965. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13966. {
  13967. unsigned long rpi;
  13968. uint16_t max_rpi, rpi_limit;
  13969. uint16_t rpi_remaining, lrpi = 0;
  13970. struct lpfc_rpi_hdr *rpi_hdr;
  13971. unsigned long iflag;
  13972. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13973. rpi_limit = phba->sli4_hba.next_rpi;
  13974. /*
  13975. * Fetch the next logical rpi. Because this index is logical,
  13976. * the driver starts at 0 each time.
  13977. */
  13978. spin_lock_irqsave(&phba->hbalock, iflag);
  13979. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13980. if (rpi >= rpi_limit)
  13981. rpi = LPFC_RPI_ALLOC_ERROR;
  13982. else {
  13983. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13984. phba->sli4_hba.max_cfg_param.rpi_used++;
  13985. phba->sli4_hba.rpi_count++;
  13986. }
  13987. /*
  13988. * Don't try to allocate more rpi header regions if the device limit
  13989. * has been exhausted.
  13990. */
  13991. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13992. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13993. spin_unlock_irqrestore(&phba->hbalock, iflag);
  13994. return rpi;
  13995. }
  13996. /*
  13997. * RPI header postings are not required for SLI4 ports capable of
  13998. * extents.
  13999. */
  14000. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  14001. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14002. return rpi;
  14003. }
  14004. /*
  14005. * If the driver is running low on rpi resources, allocate another
  14006. * page now. Note that the next_rpi value is used because
  14007. * it represents how many are actually in use whereas max_rpi notes
  14008. * how many are supported max by the device.
  14009. */
  14010. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  14011. spin_unlock_irqrestore(&phba->hbalock, iflag);
  14012. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  14013. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  14014. if (!rpi_hdr) {
  14015. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14016. "2002 Error Could not grow rpi "
  14017. "count\n");
  14018. } else {
  14019. lrpi = rpi_hdr->start_rpi;
  14020. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  14021. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  14022. }
  14023. }
  14024. return rpi;
  14025. }
  14026. /**
  14027. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14028. * @phba: pointer to lpfc hba data structure.
  14029. *
  14030. * This routine is invoked to release an rpi to the pool of
  14031. * available rpis maintained by the driver.
  14032. **/
  14033. void
  14034. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14035. {
  14036. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  14037. phba->sli4_hba.rpi_count--;
  14038. phba->sli4_hba.max_cfg_param.rpi_used--;
  14039. }
  14040. }
  14041. /**
  14042. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  14043. * @phba: pointer to lpfc hba data structure.
  14044. *
  14045. * This routine is invoked to release an rpi to the pool of
  14046. * available rpis maintained by the driver.
  14047. **/
  14048. void
  14049. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  14050. {
  14051. spin_lock_irq(&phba->hbalock);
  14052. __lpfc_sli4_free_rpi(phba, rpi);
  14053. spin_unlock_irq(&phba->hbalock);
  14054. }
  14055. /**
  14056. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  14057. * @phba: pointer to lpfc hba data structure.
  14058. *
  14059. * This routine is invoked to remove the memory region that
  14060. * provided rpi via a bitmask.
  14061. **/
  14062. void
  14063. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  14064. {
  14065. kfree(phba->sli4_hba.rpi_bmask);
  14066. kfree(phba->sli4_hba.rpi_ids);
  14067. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  14068. }
  14069. /**
  14070. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  14071. * @phba: pointer to lpfc hba data structure.
  14072. *
  14073. * This routine is invoked to remove the memory region that
  14074. * provided rpi via a bitmask.
  14075. **/
  14076. int
  14077. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  14078. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  14079. {
  14080. LPFC_MBOXQ_t *mboxq;
  14081. struct lpfc_hba *phba = ndlp->phba;
  14082. int rc;
  14083. /* The port is notified of the header region via a mailbox command. */
  14084. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14085. if (!mboxq)
  14086. return -ENOMEM;
  14087. /* Post all rpi memory regions to the port. */
  14088. lpfc_resume_rpi(mboxq, ndlp);
  14089. if (cmpl) {
  14090. mboxq->mbox_cmpl = cmpl;
  14091. mboxq->context1 = arg;
  14092. mboxq->context2 = ndlp;
  14093. } else
  14094. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14095. mboxq->vport = ndlp->vport;
  14096. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14097. if (rc == MBX_NOT_FINISHED) {
  14098. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14099. "2010 Resume RPI Mailbox failed "
  14100. "status %d, mbxStatus x%x\n", rc,
  14101. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14102. mempool_free(mboxq, phba->mbox_mem_pool);
  14103. return -EIO;
  14104. }
  14105. return 0;
  14106. }
  14107. /**
  14108. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  14109. * @vport: Pointer to the vport for which the vpi is being initialized
  14110. *
  14111. * This routine is invoked to activate a vpi with the port.
  14112. *
  14113. * Returns:
  14114. * 0 success
  14115. * -Evalue otherwise
  14116. **/
  14117. int
  14118. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  14119. {
  14120. LPFC_MBOXQ_t *mboxq;
  14121. int rc = 0;
  14122. int retval = MBX_SUCCESS;
  14123. uint32_t mbox_tmo;
  14124. struct lpfc_hba *phba = vport->phba;
  14125. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14126. if (!mboxq)
  14127. return -ENOMEM;
  14128. lpfc_init_vpi(phba, mboxq, vport->vpi);
  14129. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  14130. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  14131. if (rc != MBX_SUCCESS) {
  14132. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  14133. "2022 INIT VPI Mailbox failed "
  14134. "status %d, mbxStatus x%x\n", rc,
  14135. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  14136. retval = -EIO;
  14137. }
  14138. if (rc != MBX_TIMEOUT)
  14139. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  14140. return retval;
  14141. }
  14142. /**
  14143. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  14144. * @phba: pointer to lpfc hba data structure.
  14145. * @mboxq: Pointer to mailbox object.
  14146. *
  14147. * This routine is invoked to manually add a single FCF record. The caller
  14148. * must pass a completely initialized FCF_Record. This routine takes
  14149. * care of the nonembedded mailbox operations.
  14150. **/
  14151. static void
  14152. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  14153. {
  14154. void *virt_addr;
  14155. union lpfc_sli4_cfg_shdr *shdr;
  14156. uint32_t shdr_status, shdr_add_status;
  14157. virt_addr = mboxq->sge_array->addr[0];
  14158. /* The IOCTL status is embedded in the mailbox subheader. */
  14159. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  14160. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14161. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14162. if ((shdr_status || shdr_add_status) &&
  14163. (shdr_status != STATUS_FCF_IN_USE))
  14164. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14165. "2558 ADD_FCF_RECORD mailbox failed with "
  14166. "status x%x add_status x%x\n",
  14167. shdr_status, shdr_add_status);
  14168. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14169. }
  14170. /**
  14171. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  14172. * @phba: pointer to lpfc hba data structure.
  14173. * @fcf_record: pointer to the initialized fcf record to add.
  14174. *
  14175. * This routine is invoked to manually add a single FCF record. The caller
  14176. * must pass a completely initialized FCF_Record. This routine takes
  14177. * care of the nonembedded mailbox operations.
  14178. **/
  14179. int
  14180. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  14181. {
  14182. int rc = 0;
  14183. LPFC_MBOXQ_t *mboxq;
  14184. uint8_t *bytep;
  14185. void *virt_addr;
  14186. dma_addr_t phys_addr;
  14187. struct lpfc_mbx_sge sge;
  14188. uint32_t alloc_len, req_len;
  14189. uint32_t fcfindex;
  14190. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14191. if (!mboxq) {
  14192. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14193. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  14194. return -ENOMEM;
  14195. }
  14196. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  14197. sizeof(uint32_t);
  14198. /* Allocate DMA memory and set up the non-embedded mailbox command */
  14199. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  14200. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  14201. req_len, LPFC_SLI4_MBX_NEMBED);
  14202. if (alloc_len < req_len) {
  14203. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14204. "2523 Allocated DMA memory size (x%x) is "
  14205. "less than the requested DMA memory "
  14206. "size (x%x)\n", alloc_len, req_len);
  14207. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14208. return -ENOMEM;
  14209. }
  14210. /*
  14211. * Get the first SGE entry from the non-embedded DMA memory. This
  14212. * routine only uses a single SGE.
  14213. */
  14214. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  14215. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  14216. virt_addr = mboxq->sge_array->addr[0];
  14217. /*
  14218. * Configure the FCF record for FCFI 0. This is the driver's
  14219. * hardcoded default and gets used in nonFIP mode.
  14220. */
  14221. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  14222. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  14223. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  14224. /*
  14225. * Copy the fcf_index and the FCF Record Data. The data starts after
  14226. * the FCoE header plus word10. The data copy needs to be endian
  14227. * correct.
  14228. */
  14229. bytep += sizeof(uint32_t);
  14230. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  14231. mboxq->vport = phba->pport;
  14232. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  14233. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14234. if (rc == MBX_NOT_FINISHED) {
  14235. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14236. "2515 ADD_FCF_RECORD mailbox failed with "
  14237. "status 0x%x\n", rc);
  14238. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14239. rc = -EIO;
  14240. } else
  14241. rc = 0;
  14242. return rc;
  14243. }
  14244. /**
  14245. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  14246. * @phba: pointer to lpfc hba data structure.
  14247. * @fcf_record: pointer to the fcf record to write the default data.
  14248. * @fcf_index: FCF table entry index.
  14249. *
  14250. * This routine is invoked to build the driver's default FCF record. The
  14251. * values used are hardcoded. This routine handles memory initialization.
  14252. *
  14253. **/
  14254. void
  14255. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  14256. struct fcf_record *fcf_record,
  14257. uint16_t fcf_index)
  14258. {
  14259. memset(fcf_record, 0, sizeof(struct fcf_record));
  14260. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  14261. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  14262. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  14263. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  14264. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  14265. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  14266. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  14267. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  14268. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  14269. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  14270. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  14271. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  14272. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  14273. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  14274. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  14275. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  14276. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  14277. /* Set the VLAN bit map */
  14278. if (phba->valid_vlan) {
  14279. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  14280. = 1 << (phba->vlan_id % 8);
  14281. }
  14282. }
  14283. /**
  14284. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  14285. * @phba: pointer to lpfc hba data structure.
  14286. * @fcf_index: FCF table entry offset.
  14287. *
  14288. * This routine is invoked to scan the entire FCF table by reading FCF
  14289. * record and processing it one at a time starting from the @fcf_index
  14290. * for initial FCF discovery or fast FCF failover rediscovery.
  14291. *
  14292. * Return 0 if the mailbox command is submitted successfully, none 0
  14293. * otherwise.
  14294. **/
  14295. int
  14296. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14297. {
  14298. int rc = 0, error;
  14299. LPFC_MBOXQ_t *mboxq;
  14300. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  14301. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  14302. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14303. if (!mboxq) {
  14304. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14305. "2000 Failed to allocate mbox for "
  14306. "READ_FCF cmd\n");
  14307. error = -ENOMEM;
  14308. goto fail_fcf_scan;
  14309. }
  14310. /* Construct the read FCF record mailbox command */
  14311. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14312. if (rc) {
  14313. error = -EINVAL;
  14314. goto fail_fcf_scan;
  14315. }
  14316. /* Issue the mailbox command asynchronously */
  14317. mboxq->vport = phba->pport;
  14318. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  14319. spin_lock_irq(&phba->hbalock);
  14320. phba->hba_flag |= FCF_TS_INPROG;
  14321. spin_unlock_irq(&phba->hbalock);
  14322. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14323. if (rc == MBX_NOT_FINISHED)
  14324. error = -EIO;
  14325. else {
  14326. /* Reset eligible FCF count for new scan */
  14327. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  14328. phba->fcf.eligible_fcf_cnt = 0;
  14329. error = 0;
  14330. }
  14331. fail_fcf_scan:
  14332. if (error) {
  14333. if (mboxq)
  14334. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14335. /* FCF scan failed, clear FCF_TS_INPROG flag */
  14336. spin_lock_irq(&phba->hbalock);
  14337. phba->hba_flag &= ~FCF_TS_INPROG;
  14338. spin_unlock_irq(&phba->hbalock);
  14339. }
  14340. return error;
  14341. }
  14342. /**
  14343. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  14344. * @phba: pointer to lpfc hba data structure.
  14345. * @fcf_index: FCF table entry offset.
  14346. *
  14347. * This routine is invoked to read an FCF record indicated by @fcf_index
  14348. * and to use it for FLOGI roundrobin FCF failover.
  14349. *
  14350. * Return 0 if the mailbox command is submitted successfully, none 0
  14351. * otherwise.
  14352. **/
  14353. int
  14354. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14355. {
  14356. int rc = 0, error;
  14357. LPFC_MBOXQ_t *mboxq;
  14358. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14359. if (!mboxq) {
  14360. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14361. "2763 Failed to allocate mbox for "
  14362. "READ_FCF cmd\n");
  14363. error = -ENOMEM;
  14364. goto fail_fcf_read;
  14365. }
  14366. /* Construct the read FCF record mailbox command */
  14367. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14368. if (rc) {
  14369. error = -EINVAL;
  14370. goto fail_fcf_read;
  14371. }
  14372. /* Issue the mailbox command asynchronously */
  14373. mboxq->vport = phba->pport;
  14374. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  14375. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14376. if (rc == MBX_NOT_FINISHED)
  14377. error = -EIO;
  14378. else
  14379. error = 0;
  14380. fail_fcf_read:
  14381. if (error && mboxq)
  14382. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14383. return error;
  14384. }
  14385. /**
  14386. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  14387. * @phba: pointer to lpfc hba data structure.
  14388. * @fcf_index: FCF table entry offset.
  14389. *
  14390. * This routine is invoked to read an FCF record indicated by @fcf_index to
  14391. * determine whether it's eligible for FLOGI roundrobin failover list.
  14392. *
  14393. * Return 0 if the mailbox command is submitted successfully, none 0
  14394. * otherwise.
  14395. **/
  14396. int
  14397. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14398. {
  14399. int rc = 0, error;
  14400. LPFC_MBOXQ_t *mboxq;
  14401. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14402. if (!mboxq) {
  14403. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14404. "2758 Failed to allocate mbox for "
  14405. "READ_FCF cmd\n");
  14406. error = -ENOMEM;
  14407. goto fail_fcf_read;
  14408. }
  14409. /* Construct the read FCF record mailbox command */
  14410. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14411. if (rc) {
  14412. error = -EINVAL;
  14413. goto fail_fcf_read;
  14414. }
  14415. /* Issue the mailbox command asynchronously */
  14416. mboxq->vport = phba->pport;
  14417. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  14418. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14419. if (rc == MBX_NOT_FINISHED)
  14420. error = -EIO;
  14421. else
  14422. error = 0;
  14423. fail_fcf_read:
  14424. if (error && mboxq)
  14425. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14426. return error;
  14427. }
  14428. /**
  14429. * lpfc_check_next_fcf_pri
  14430. * phba pointer to the lpfc_hba struct for this port.
  14431. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  14432. * routine when the rr_bmask is empty. The FCF indecies are put into the
  14433. * rr_bmask based on their priority level. Starting from the highest priority
  14434. * to the lowest. The most likely FCF candidate will be in the highest
  14435. * priority group. When this routine is called it searches the fcf_pri list for
  14436. * next lowest priority group and repopulates the rr_bmask with only those
  14437. * fcf_indexes.
  14438. * returns:
  14439. * 1=success 0=failure
  14440. **/
  14441. int
  14442. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  14443. {
  14444. uint16_t next_fcf_pri;
  14445. uint16_t last_index;
  14446. struct lpfc_fcf_pri *fcf_pri;
  14447. int rc;
  14448. int ret = 0;
  14449. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  14450. LPFC_SLI4_FCF_TBL_INDX_MAX);
  14451. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14452. "3060 Last IDX %d\n", last_index);
  14453. /* Verify the priority list has 2 or more entries */
  14454. spin_lock_irq(&phba->hbalock);
  14455. if (list_empty(&phba->fcf.fcf_pri_list) ||
  14456. list_is_singular(&phba->fcf.fcf_pri_list)) {
  14457. spin_unlock_irq(&phba->hbalock);
  14458. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14459. "3061 Last IDX %d\n", last_index);
  14460. return 0; /* Empty rr list */
  14461. }
  14462. spin_unlock_irq(&phba->hbalock);
  14463. next_fcf_pri = 0;
  14464. /*
  14465. * Clear the rr_bmask and set all of the bits that are at this
  14466. * priority.
  14467. */
  14468. memset(phba->fcf.fcf_rr_bmask, 0,
  14469. sizeof(*phba->fcf.fcf_rr_bmask));
  14470. spin_lock_irq(&phba->hbalock);
  14471. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14472. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  14473. continue;
  14474. /*
  14475. * the 1st priority that has not FLOGI failed
  14476. * will be the highest.
  14477. */
  14478. if (!next_fcf_pri)
  14479. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14480. spin_unlock_irq(&phba->hbalock);
  14481. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14482. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14483. fcf_pri->fcf_rec.fcf_index);
  14484. if (rc)
  14485. return 0;
  14486. }
  14487. spin_lock_irq(&phba->hbalock);
  14488. }
  14489. /*
  14490. * if next_fcf_pri was not set above and the list is not empty then
  14491. * we have failed flogis on all of them. So reset flogi failed
  14492. * and start at the beginning.
  14493. */
  14494. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  14495. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14496. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14497. /*
  14498. * the 1st priority that has not FLOGI failed
  14499. * will be the highest.
  14500. */
  14501. if (!next_fcf_pri)
  14502. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14503. spin_unlock_irq(&phba->hbalock);
  14504. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14505. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14506. fcf_pri->fcf_rec.fcf_index);
  14507. if (rc)
  14508. return 0;
  14509. }
  14510. spin_lock_irq(&phba->hbalock);
  14511. }
  14512. } else
  14513. ret = 1;
  14514. spin_unlock_irq(&phba->hbalock);
  14515. return ret;
  14516. }
  14517. /**
  14518. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14519. * @phba: pointer to lpfc hba data structure.
  14520. *
  14521. * This routine is to get the next eligible FCF record index in a round
  14522. * robin fashion. If the next eligible FCF record index equals to the
  14523. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14524. * shall be returned, otherwise, the next eligible FCF record's index
  14525. * shall be returned.
  14526. **/
  14527. uint16_t
  14528. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14529. {
  14530. uint16_t next_fcf_index;
  14531. initial_priority:
  14532. /* Search start from next bit of currently registered FCF index */
  14533. next_fcf_index = phba->fcf.current_rec.fcf_indx;
  14534. next_priority:
  14535. /* Determine the next fcf index to check */
  14536. next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
  14537. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14538. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14539. next_fcf_index);
  14540. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14541. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14542. /*
  14543. * If we have wrapped then we need to clear the bits that
  14544. * have been tested so that we can detect when we should
  14545. * change the priority level.
  14546. */
  14547. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14548. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14549. }
  14550. /* Check roundrobin failover list empty condition */
  14551. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14552. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14553. /*
  14554. * If next fcf index is not found check if there are lower
  14555. * Priority level fcf's in the fcf_priority list.
  14556. * Set up the rr_bmask with all of the avaiable fcf bits
  14557. * at that level and continue the selection process.
  14558. */
  14559. if (lpfc_check_next_fcf_pri_level(phba))
  14560. goto initial_priority;
  14561. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14562. "2844 No roundrobin failover FCF available\n");
  14563. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14564. return LPFC_FCOE_FCF_NEXT_NONE;
  14565. else {
  14566. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14567. "3063 Only FCF available idx %d, flag %x\n",
  14568. next_fcf_index,
  14569. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14570. return next_fcf_index;
  14571. }
  14572. }
  14573. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14574. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14575. LPFC_FCF_FLOGI_FAILED)
  14576. goto next_priority;
  14577. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14578. "2845 Get next roundrobin failover FCF (x%x)\n",
  14579. next_fcf_index);
  14580. return next_fcf_index;
  14581. }
  14582. /**
  14583. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14584. * @phba: pointer to lpfc hba data structure.
  14585. *
  14586. * This routine sets the FCF record index in to the eligible bmask for
  14587. * roundrobin failover search. It checks to make sure that the index
  14588. * does not go beyond the range of the driver allocated bmask dimension
  14589. * before setting the bit.
  14590. *
  14591. * Returns 0 if the index bit successfully set, otherwise, it returns
  14592. * -EINVAL.
  14593. **/
  14594. int
  14595. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14596. {
  14597. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14598. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14599. "2610 FCF (x%x) reached driver's book "
  14600. "keeping dimension:x%x\n",
  14601. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14602. return -EINVAL;
  14603. }
  14604. /* Set the eligible FCF record index bmask */
  14605. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14606. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14607. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14608. "bmask\n", fcf_index);
  14609. return 0;
  14610. }
  14611. /**
  14612. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14613. * @phba: pointer to lpfc hba data structure.
  14614. *
  14615. * This routine clears the FCF record index from the eligible bmask for
  14616. * roundrobin failover search. It checks to make sure that the index
  14617. * does not go beyond the range of the driver allocated bmask dimension
  14618. * before clearing the bit.
  14619. **/
  14620. void
  14621. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14622. {
  14623. struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
  14624. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14625. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14626. "2762 FCF (x%x) reached driver's book "
  14627. "keeping dimension:x%x\n",
  14628. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14629. return;
  14630. }
  14631. /* Clear the eligible FCF record index bmask */
  14632. spin_lock_irq(&phba->hbalock);
  14633. list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
  14634. list) {
  14635. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14636. list_del_init(&fcf_pri->list);
  14637. break;
  14638. }
  14639. }
  14640. spin_unlock_irq(&phba->hbalock);
  14641. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14642. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14643. "2791 Clear FCF (x%x) from roundrobin failover "
  14644. "bmask\n", fcf_index);
  14645. }
  14646. /**
  14647. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14648. * @phba: pointer to lpfc hba data structure.
  14649. *
  14650. * This routine is the completion routine for the rediscover FCF table mailbox
  14651. * command. If the mailbox command returned failure, it will try to stop the
  14652. * FCF rediscover wait timer.
  14653. **/
  14654. void
  14655. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14656. {
  14657. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14658. uint32_t shdr_status, shdr_add_status;
  14659. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14660. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14661. &redisc_fcf->header.cfg_shdr.response);
  14662. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14663. &redisc_fcf->header.cfg_shdr.response);
  14664. if (shdr_status || shdr_add_status) {
  14665. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14666. "2746 Requesting for FCF rediscovery failed "
  14667. "status x%x add_status x%x\n",
  14668. shdr_status, shdr_add_status);
  14669. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14670. spin_lock_irq(&phba->hbalock);
  14671. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14672. spin_unlock_irq(&phba->hbalock);
  14673. /*
  14674. * CVL event triggered FCF rediscover request failed,
  14675. * last resort to re-try current registered FCF entry.
  14676. */
  14677. lpfc_retry_pport_discovery(phba);
  14678. } else {
  14679. spin_lock_irq(&phba->hbalock);
  14680. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14681. spin_unlock_irq(&phba->hbalock);
  14682. /*
  14683. * DEAD FCF event triggered FCF rediscover request
  14684. * failed, last resort to fail over as a link down
  14685. * to FCF registration.
  14686. */
  14687. lpfc_sli4_fcf_dead_failthrough(phba);
  14688. }
  14689. } else {
  14690. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14691. "2775 Start FCF rediscover quiescent timer\n");
  14692. /*
  14693. * Start FCF rediscovery wait timer for pending FCF
  14694. * before rescan FCF record table.
  14695. */
  14696. lpfc_fcf_redisc_wait_start_timer(phba);
  14697. }
  14698. mempool_free(mbox, phba->mbox_mem_pool);
  14699. }
  14700. /**
  14701. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14702. * @phba: pointer to lpfc hba data structure.
  14703. *
  14704. * This routine is invoked to request for rediscovery of the entire FCF table
  14705. * by the port.
  14706. **/
  14707. int
  14708. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14709. {
  14710. LPFC_MBOXQ_t *mbox;
  14711. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14712. int rc, length;
  14713. /* Cancel retry delay timers to all vports before FCF rediscover */
  14714. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14715. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14716. if (!mbox) {
  14717. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14718. "2745 Failed to allocate mbox for "
  14719. "requesting FCF rediscover.\n");
  14720. return -ENOMEM;
  14721. }
  14722. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14723. sizeof(struct lpfc_sli4_cfg_mhdr));
  14724. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14725. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14726. length, LPFC_SLI4_MBX_EMBED);
  14727. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14728. /* Set count to 0 for invalidating the entire FCF database */
  14729. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14730. /* Issue the mailbox command asynchronously */
  14731. mbox->vport = phba->pport;
  14732. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14733. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14734. if (rc == MBX_NOT_FINISHED) {
  14735. mempool_free(mbox, phba->mbox_mem_pool);
  14736. return -EIO;
  14737. }
  14738. return 0;
  14739. }
  14740. /**
  14741. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14742. * @phba: pointer to lpfc hba data structure.
  14743. *
  14744. * This function is the failover routine as a last resort to the FCF DEAD
  14745. * event when driver failed to perform fast FCF failover.
  14746. **/
  14747. void
  14748. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14749. {
  14750. uint32_t link_state;
  14751. /*
  14752. * Last resort as FCF DEAD event failover will treat this as
  14753. * a link down, but save the link state because we don't want
  14754. * it to be changed to Link Down unless it is already down.
  14755. */
  14756. link_state = phba->link_state;
  14757. lpfc_linkdown(phba);
  14758. phba->link_state = link_state;
  14759. /* Unregister FCF if no devices connected to it */
  14760. lpfc_unregister_unused_fcf(phba);
  14761. }
  14762. /**
  14763. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14764. * @phba: pointer to lpfc hba data structure.
  14765. * @rgn23_data: pointer to configure region 23 data.
  14766. *
  14767. * This function gets SLI3 port configure region 23 data through memory dump
  14768. * mailbox command. When it successfully retrieves data, the size of the data
  14769. * will be returned, otherwise, 0 will be returned.
  14770. **/
  14771. static uint32_t
  14772. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14773. {
  14774. LPFC_MBOXQ_t *pmb = NULL;
  14775. MAILBOX_t *mb;
  14776. uint32_t offset = 0;
  14777. int rc;
  14778. if (!rgn23_data)
  14779. return 0;
  14780. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14781. if (!pmb) {
  14782. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14783. "2600 failed to allocate mailbox memory\n");
  14784. return 0;
  14785. }
  14786. mb = &pmb->u.mb;
  14787. do {
  14788. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14789. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14790. if (rc != MBX_SUCCESS) {
  14791. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14792. "2601 failed to read config "
  14793. "region 23, rc 0x%x Status 0x%x\n",
  14794. rc, mb->mbxStatus);
  14795. mb->un.varDmp.word_cnt = 0;
  14796. }
  14797. /*
  14798. * dump mem may return a zero when finished or we got a
  14799. * mailbox error, either way we are done.
  14800. */
  14801. if (mb->un.varDmp.word_cnt == 0)
  14802. break;
  14803. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14804. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14805. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14806. rgn23_data + offset,
  14807. mb->un.varDmp.word_cnt);
  14808. offset += mb->un.varDmp.word_cnt;
  14809. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14810. mempool_free(pmb, phba->mbox_mem_pool);
  14811. return offset;
  14812. }
  14813. /**
  14814. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14815. * @phba: pointer to lpfc hba data structure.
  14816. * @rgn23_data: pointer to configure region 23 data.
  14817. *
  14818. * This function gets SLI4 port configure region 23 data through memory dump
  14819. * mailbox command. When it successfully retrieves data, the size of the data
  14820. * will be returned, otherwise, 0 will be returned.
  14821. **/
  14822. static uint32_t
  14823. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14824. {
  14825. LPFC_MBOXQ_t *mboxq = NULL;
  14826. struct lpfc_dmabuf *mp = NULL;
  14827. struct lpfc_mqe *mqe;
  14828. uint32_t data_length = 0;
  14829. int rc;
  14830. if (!rgn23_data)
  14831. return 0;
  14832. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14833. if (!mboxq) {
  14834. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14835. "3105 failed to allocate mailbox memory\n");
  14836. return 0;
  14837. }
  14838. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14839. goto out;
  14840. mqe = &mboxq->u.mqe;
  14841. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14842. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14843. if (rc)
  14844. goto out;
  14845. data_length = mqe->un.mb_words[5];
  14846. if (data_length == 0)
  14847. goto out;
  14848. if (data_length > DMP_RGN23_SIZE) {
  14849. data_length = 0;
  14850. goto out;
  14851. }
  14852. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14853. out:
  14854. mempool_free(mboxq, phba->mbox_mem_pool);
  14855. if (mp) {
  14856. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14857. kfree(mp);
  14858. }
  14859. return data_length;
  14860. }
  14861. /**
  14862. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14863. * @phba: pointer to lpfc hba data structure.
  14864. *
  14865. * This function read region 23 and parse TLV for port status to
  14866. * decide if the user disaled the port. If the TLV indicates the
  14867. * port is disabled, the hba_flag is set accordingly.
  14868. **/
  14869. void
  14870. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14871. {
  14872. uint8_t *rgn23_data = NULL;
  14873. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14874. uint32_t offset = 0;
  14875. /* Get adapter Region 23 data */
  14876. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14877. if (!rgn23_data)
  14878. goto out;
  14879. if (phba->sli_rev < LPFC_SLI_REV4)
  14880. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14881. else {
  14882. if_type = bf_get(lpfc_sli_intf_if_type,
  14883. &phba->sli4_hba.sli_intf);
  14884. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14885. goto out;
  14886. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14887. }
  14888. if (!data_size)
  14889. goto out;
  14890. /* Check the region signature first */
  14891. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14892. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14893. "2619 Config region 23 has bad signature\n");
  14894. goto out;
  14895. }
  14896. offset += 4;
  14897. /* Check the data structure version */
  14898. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14899. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14900. "2620 Config region 23 has bad version\n");
  14901. goto out;
  14902. }
  14903. offset += 4;
  14904. /* Parse TLV entries in the region */
  14905. while (offset < data_size) {
  14906. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14907. break;
  14908. /*
  14909. * If the TLV is not driver specific TLV or driver id is
  14910. * not linux driver id, skip the record.
  14911. */
  14912. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14913. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14914. (rgn23_data[offset + 3] != 0)) {
  14915. offset += rgn23_data[offset + 1] * 4 + 4;
  14916. continue;
  14917. }
  14918. /* Driver found a driver specific TLV in the config region */
  14919. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14920. offset += 4;
  14921. tlv_offset = 0;
  14922. /*
  14923. * Search for configured port state sub-TLV.
  14924. */
  14925. while ((offset < data_size) &&
  14926. (tlv_offset < sub_tlv_len)) {
  14927. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14928. offset += 4;
  14929. tlv_offset += 4;
  14930. break;
  14931. }
  14932. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14933. offset += rgn23_data[offset + 1] * 4 + 4;
  14934. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14935. continue;
  14936. }
  14937. /* This HBA contains PORT_STE configured */
  14938. if (!rgn23_data[offset + 2])
  14939. phba->hba_flag |= LINK_DISABLED;
  14940. goto out;
  14941. }
  14942. }
  14943. out:
  14944. kfree(rgn23_data);
  14945. return;
  14946. }
  14947. /**
  14948. * lpfc_wr_object - write an object to the firmware
  14949. * @phba: HBA structure that indicates port to create a queue on.
  14950. * @dmabuf_list: list of dmabufs to write to the port.
  14951. * @size: the total byte value of the objects to write to the port.
  14952. * @offset: the current offset to be used to start the transfer.
  14953. *
  14954. * This routine will create a wr_object mailbox command to send to the port.
  14955. * the mailbox command will be constructed using the dma buffers described in
  14956. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14957. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14958. * used to indicate the starting offset of the transfer and will also return
  14959. * the offset after the write object mailbox has completed. @size is used to
  14960. * determine the end of the object and whether the eof bit should be set.
  14961. *
  14962. * Return 0 is successful and offset will contain the the new offset to use
  14963. * for the next write.
  14964. * Return negative value for error cases.
  14965. **/
  14966. int
  14967. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14968. uint32_t size, uint32_t *offset)
  14969. {
  14970. struct lpfc_mbx_wr_object *wr_object;
  14971. LPFC_MBOXQ_t *mbox;
  14972. int rc = 0, i = 0;
  14973. uint32_t shdr_status, shdr_add_status;
  14974. uint32_t mbox_tmo;
  14975. union lpfc_sli4_cfg_shdr *shdr;
  14976. struct lpfc_dmabuf *dmabuf;
  14977. uint32_t written = 0;
  14978. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14979. if (!mbox)
  14980. return -ENOMEM;
  14981. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14982. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14983. sizeof(struct lpfc_mbx_wr_object) -
  14984. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14985. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14986. wr_object->u.request.write_offset = *offset;
  14987. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14988. wr_object->u.request.object_name[0] =
  14989. cpu_to_le32(wr_object->u.request.object_name[0]);
  14990. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14991. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14992. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14993. break;
  14994. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14995. wr_object->u.request.bde[i].addrHigh =
  14996. putPaddrHigh(dmabuf->phys);
  14997. if (written + SLI4_PAGE_SIZE >= size) {
  14998. wr_object->u.request.bde[i].tus.f.bdeSize =
  14999. (size - written);
  15000. written += (size - written);
  15001. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  15002. } else {
  15003. wr_object->u.request.bde[i].tus.f.bdeSize =
  15004. SLI4_PAGE_SIZE;
  15005. written += SLI4_PAGE_SIZE;
  15006. }
  15007. i++;
  15008. }
  15009. wr_object->u.request.bde_count = i;
  15010. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  15011. if (!phba->sli4_hba.intr_enable)
  15012. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  15013. else {
  15014. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  15015. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  15016. }
  15017. /* The IOCTL status is embedded in the mailbox subheader. */
  15018. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  15019. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  15020. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  15021. if (rc != MBX_TIMEOUT)
  15022. mempool_free(mbox, phba->mbox_mem_pool);
  15023. if (shdr_status || shdr_add_status || rc) {
  15024. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15025. "3025 Write Object mailbox failed with "
  15026. "status x%x add_status x%x, mbx status x%x\n",
  15027. shdr_status, shdr_add_status, rc);
  15028. rc = -ENXIO;
  15029. } else
  15030. *offset += wr_object->u.response.actual_write_length;
  15031. return rc;
  15032. }
  15033. /**
  15034. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  15035. * @vport: pointer to vport data structure.
  15036. *
  15037. * This function iterate through the mailboxq and clean up all REG_LOGIN
  15038. * and REG_VPI mailbox commands associated with the vport. This function
  15039. * is called when driver want to restart discovery of the vport due to
  15040. * a Clear Virtual Link event.
  15041. **/
  15042. void
  15043. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  15044. {
  15045. struct lpfc_hba *phba = vport->phba;
  15046. LPFC_MBOXQ_t *mb, *nextmb;
  15047. struct lpfc_dmabuf *mp;
  15048. struct lpfc_nodelist *ndlp;
  15049. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  15050. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  15051. LIST_HEAD(mbox_cmd_list);
  15052. uint8_t restart_loop;
  15053. /* Clean up internally queued mailbox commands with the vport */
  15054. spin_lock_irq(&phba->hbalock);
  15055. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  15056. if (mb->vport != vport)
  15057. continue;
  15058. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15059. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15060. continue;
  15061. list_del(&mb->list);
  15062. list_add_tail(&mb->list, &mbox_cmd_list);
  15063. }
  15064. /* Clean up active mailbox command with the vport */
  15065. mb = phba->sli.mbox_active;
  15066. if (mb && (mb->vport == vport)) {
  15067. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  15068. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  15069. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15070. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15071. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  15072. /* Put reference count for delayed processing */
  15073. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  15074. /* Unregister the RPI when mailbox complete */
  15075. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15076. }
  15077. }
  15078. /* Cleanup any mailbox completions which are not yet processed */
  15079. do {
  15080. restart_loop = 0;
  15081. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  15082. /*
  15083. * If this mailox is already processed or it is
  15084. * for another vport ignore it.
  15085. */
  15086. if ((mb->vport != vport) ||
  15087. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  15088. continue;
  15089. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  15090. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  15091. continue;
  15092. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  15093. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15094. ndlp = (struct lpfc_nodelist *)mb->context2;
  15095. /* Unregister the RPI when mailbox complete */
  15096. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  15097. restart_loop = 1;
  15098. spin_unlock_irq(&phba->hbalock);
  15099. spin_lock(shost->host_lock);
  15100. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15101. spin_unlock(shost->host_lock);
  15102. spin_lock_irq(&phba->hbalock);
  15103. break;
  15104. }
  15105. }
  15106. } while (restart_loop);
  15107. spin_unlock_irq(&phba->hbalock);
  15108. /* Release the cleaned-up mailbox commands */
  15109. while (!list_empty(&mbox_cmd_list)) {
  15110. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  15111. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  15112. mp = (struct lpfc_dmabuf *) (mb->context1);
  15113. if (mp) {
  15114. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  15115. kfree(mp);
  15116. }
  15117. ndlp = (struct lpfc_nodelist *) mb->context2;
  15118. mb->context2 = NULL;
  15119. if (ndlp) {
  15120. spin_lock(shost->host_lock);
  15121. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15122. spin_unlock(shost->host_lock);
  15123. lpfc_nlp_put(ndlp);
  15124. }
  15125. }
  15126. mempool_free(mb, phba->mbox_mem_pool);
  15127. }
  15128. /* Release the ndlp with the cleaned-up active mailbox command */
  15129. if (act_mbx_ndlp) {
  15130. spin_lock(shost->host_lock);
  15131. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  15132. spin_unlock(shost->host_lock);
  15133. lpfc_nlp_put(act_mbx_ndlp);
  15134. }
  15135. }
  15136. /**
  15137. * lpfc_drain_txq - Drain the txq
  15138. * @phba: Pointer to HBA context object.
  15139. *
  15140. * This function attempt to submit IOCBs on the txq
  15141. * to the adapter. For SLI4 adapters, the txq contains
  15142. * ELS IOCBs that have been deferred because the there
  15143. * are no SGLs. This congestion can occur with large
  15144. * vport counts during node discovery.
  15145. **/
  15146. uint32_t
  15147. lpfc_drain_txq(struct lpfc_hba *phba)
  15148. {
  15149. LIST_HEAD(completions);
  15150. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  15151. struct lpfc_iocbq *piocbq = 0;
  15152. unsigned long iflags = 0;
  15153. char *fail_msg = NULL;
  15154. struct lpfc_sglq *sglq;
  15155. union lpfc_wqe wqe;
  15156. int txq_cnt = 0;
  15157. spin_lock_irqsave(&pring->ring_lock, iflags);
  15158. list_for_each_entry(piocbq, &pring->txq, list) {
  15159. txq_cnt++;
  15160. }
  15161. if (txq_cnt > pring->txq_max)
  15162. pring->txq_max = txq_cnt;
  15163. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15164. while (!list_empty(&pring->txq)) {
  15165. spin_lock_irqsave(&pring->ring_lock, iflags);
  15166. piocbq = lpfc_sli_ringtx_get(phba, pring);
  15167. if (!piocbq) {
  15168. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15169. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15170. "2823 txq empty and txq_cnt is %d\n ",
  15171. txq_cnt);
  15172. break;
  15173. }
  15174. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  15175. if (!sglq) {
  15176. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  15177. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15178. break;
  15179. }
  15180. txq_cnt--;
  15181. /* The xri and iocb resources secured,
  15182. * attempt to issue request
  15183. */
  15184. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  15185. piocbq->sli4_xritag = sglq->sli4_xritag;
  15186. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  15187. fail_msg = "to convert bpl to sgl";
  15188. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  15189. fail_msg = "to convert iocb to wqe";
  15190. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  15191. fail_msg = " - Wq is full";
  15192. else
  15193. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  15194. if (fail_msg) {
  15195. /* Failed means we can't issue and need to cancel */
  15196. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15197. "2822 IOCB failed %s iotag 0x%x "
  15198. "xri 0x%x\n",
  15199. fail_msg,
  15200. piocbq->iotag, piocbq->sli4_xritag);
  15201. list_add_tail(&piocbq->list, &completions);
  15202. }
  15203. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  15204. }
  15205. /* Cancel all the IOCBs that cannot be issued */
  15206. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  15207. IOERR_SLI_ABORTED);
  15208. return txq_cnt;
  15209. }