lpfc_sli.c 590 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
  6. * Copyright (C) 2004-2016 Emulex. All rights reserved. *
  7. * EMULEX and SLI are trademarks of Emulex. *
  8. * www.broadcom.com *
  9. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  10. * *
  11. * This program is free software; you can redistribute it and/or *
  12. * modify it under the terms of version 2 of the GNU General *
  13. * Public License as published by the Free Software Foundation. *
  14. * This program is distributed in the hope that it will be useful. *
  15. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  16. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  17. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  18. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  20. * more details, a copy of which can be found in the file COPYING *
  21. * included with this package. *
  22. *******************************************************************/
  23. #include <linux/blkdev.h>
  24. #include <linux/pci.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/lockdep.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_host.h>
  33. #include <scsi/scsi_transport_fc.h>
  34. #include <scsi/fc/fc_fs.h>
  35. #include <linux/aer.h>
  36. #ifdef CONFIG_X86
  37. #include <asm/set_memory.h>
  38. #endif
  39. #include <linux/nvme-fc-driver.h>
  40. #include "lpfc_hw4.h"
  41. #include "lpfc_hw.h"
  42. #include "lpfc_sli.h"
  43. #include "lpfc_sli4.h"
  44. #include "lpfc_nl.h"
  45. #include "lpfc_disc.h"
  46. #include "lpfc.h"
  47. #include "lpfc_scsi.h"
  48. #include "lpfc_nvme.h"
  49. #include "lpfc_nvmet.h"
  50. #include "lpfc_crtn.h"
  51. #include "lpfc_logmsg.h"
  52. #include "lpfc_compat.h"
  53. #include "lpfc_debugfs.h"
  54. #include "lpfc_vport.h"
  55. #include "lpfc_version.h"
  56. /* There are only four IOCB completion types. */
  57. typedef enum _lpfc_iocb_type {
  58. LPFC_UNKNOWN_IOCB,
  59. LPFC_UNSOL_IOCB,
  60. LPFC_SOL_IOCB,
  61. LPFC_ABORT_IOCB
  62. } lpfc_iocb_type;
  63. /* Provide function prototypes local to this module. */
  64. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  65. uint32_t);
  66. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  67. uint8_t *, uint32_t *);
  68. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  69. struct lpfc_iocbq *);
  70. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  71. struct hbq_dmabuf *);
  72. static void lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
  73. struct hbq_dmabuf *dmabuf);
  74. static int lpfc_sli4_fp_handle_cqe(struct lpfc_hba *, struct lpfc_queue *,
  75. struct lpfc_cqe *);
  76. static int lpfc_sli4_post_sgl_list(struct lpfc_hba *, struct list_head *,
  77. int);
  78. static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba,
  79. struct lpfc_eqe *eqe, uint32_t qidx);
  80. static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
  81. static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
  82. static int lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba,
  83. struct lpfc_sli_ring *pring,
  84. struct lpfc_iocbq *cmdiocb);
  85. static IOCB_t *
  86. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  87. {
  88. return &iocbq->iocb;
  89. }
  90. #if defined(CONFIG_64BIT) && defined(__LITTLE_ENDIAN)
  91. /**
  92. * lpfc_sli4_pcimem_bcopy - SLI4 memory copy function
  93. * @srcp: Source memory pointer.
  94. * @destp: Destination memory pointer.
  95. * @cnt: Number of words required to be copied.
  96. * Must be a multiple of sizeof(uint64_t)
  97. *
  98. * This function is used for copying data between driver memory
  99. * and the SLI WQ. This function also changes the endianness
  100. * of each word if native endianness is different from SLI
  101. * endianness. This function can be called with or without
  102. * lock.
  103. **/
  104. void
  105. lpfc_sli4_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  106. {
  107. uint64_t *src = srcp;
  108. uint64_t *dest = destp;
  109. int i;
  110. for (i = 0; i < (int)cnt; i += sizeof(uint64_t))
  111. *dest++ = *src++;
  112. }
  113. #else
  114. #define lpfc_sli4_pcimem_bcopy(a, b, c) lpfc_sli_pcimem_bcopy(a, b, c)
  115. #endif
  116. /**
  117. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  118. * @q: The Work Queue to operate on.
  119. * @wqe: The work Queue Entry to put on the Work queue.
  120. *
  121. * This routine will copy the contents of @wqe to the next available entry on
  122. * the @q. This function will then ring the Work Queue Doorbell to signal the
  123. * HBA to start processing the Work Queue Entry. This function returns 0 if
  124. * successful. If no entries are available on @q then this function will return
  125. * -ENOMEM.
  126. * The caller is expected to hold the hbalock when calling this routine.
  127. **/
  128. static int
  129. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe128 *wqe)
  130. {
  131. union lpfc_wqe *temp_wqe;
  132. struct lpfc_register doorbell;
  133. uint32_t host_index;
  134. uint32_t idx;
  135. uint32_t i = 0;
  136. uint8_t *tmp;
  137. u32 if_type;
  138. /* sanity check on queue memory */
  139. if (unlikely(!q))
  140. return -ENOMEM;
  141. temp_wqe = q->qe[q->host_index].wqe;
  142. /* If the host has not yet processed the next entry then we are done */
  143. idx = ((q->host_index + 1) % q->entry_count);
  144. if (idx == q->hba_index) {
  145. q->WQ_overflow++;
  146. return -EBUSY;
  147. }
  148. q->WQ_posted++;
  149. /* set consumption flag every once in a while */
  150. if (!((q->host_index + 1) % q->entry_repost))
  151. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  152. else
  153. bf_set(wqe_wqec, &wqe->generic.wqe_com, 0);
  154. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  155. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  156. lpfc_sli4_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  157. if (q->dpp_enable && q->phba->cfg_enable_dpp) {
  158. /* write to DPP aperture taking advatage of Combined Writes */
  159. tmp = (uint8_t *)temp_wqe;
  160. #ifdef __raw_writeq
  161. for (i = 0; i < q->entry_size; i += sizeof(uint64_t))
  162. __raw_writeq(*((uint64_t *)(tmp + i)),
  163. q->dpp_regaddr + i);
  164. #else
  165. for (i = 0; i < q->entry_size; i += sizeof(uint32_t))
  166. __raw_writel(*((uint32_t *)(tmp + i)),
  167. q->dpp_regaddr + i);
  168. #endif
  169. }
  170. /* ensure WQE bcopy and DPP flushed before doorbell write */
  171. wmb();
  172. /* Update the host index before invoking device */
  173. host_index = q->host_index;
  174. q->host_index = idx;
  175. /* Ring Doorbell */
  176. doorbell.word0 = 0;
  177. if (q->db_format == LPFC_DB_LIST_FORMAT) {
  178. if (q->dpp_enable && q->phba->cfg_enable_dpp) {
  179. bf_set(lpfc_if6_wq_db_list_fm_num_posted, &doorbell, 1);
  180. bf_set(lpfc_if6_wq_db_list_fm_dpp, &doorbell, 1);
  181. bf_set(lpfc_if6_wq_db_list_fm_dpp_id, &doorbell,
  182. q->dpp_id);
  183. bf_set(lpfc_if6_wq_db_list_fm_id, &doorbell,
  184. q->queue_id);
  185. } else {
  186. bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
  187. bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
  188. /* Leave bits <23:16> clear for if_type 6 dpp */
  189. if_type = bf_get(lpfc_sli_intf_if_type,
  190. &q->phba->sli4_hba.sli_intf);
  191. if (if_type != LPFC_SLI_INTF_IF_TYPE_6)
  192. bf_set(lpfc_wq_db_list_fm_index, &doorbell,
  193. host_index);
  194. }
  195. } else if (q->db_format == LPFC_DB_RING_FORMAT) {
  196. bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
  197. bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
  198. } else {
  199. return -EINVAL;
  200. }
  201. writel(doorbell.word0, q->db_regaddr);
  202. return 0;
  203. }
  204. /**
  205. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  206. * @q: The Work Queue to operate on.
  207. * @index: The index to advance the hba index to.
  208. *
  209. * This routine will update the HBA index of a queue to reflect consumption of
  210. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  211. * an entry the host calls this function to update the queue's internal
  212. * pointers. This routine returns the number of entries that were consumed by
  213. * the HBA.
  214. **/
  215. static uint32_t
  216. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  217. {
  218. uint32_t released = 0;
  219. /* sanity check on queue memory */
  220. if (unlikely(!q))
  221. return 0;
  222. if (q->hba_index == index)
  223. return 0;
  224. do {
  225. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  226. released++;
  227. } while (q->hba_index != index);
  228. return released;
  229. }
  230. /**
  231. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  232. * @q: The Mailbox Queue to operate on.
  233. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  234. *
  235. * This routine will copy the contents of @mqe to the next available entry on
  236. * the @q. This function will then ring the Work Queue Doorbell to signal the
  237. * HBA to start processing the Work Queue Entry. This function returns 0 if
  238. * successful. If no entries are available on @q then this function will return
  239. * -ENOMEM.
  240. * The caller is expected to hold the hbalock when calling this routine.
  241. **/
  242. static uint32_t
  243. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  244. {
  245. struct lpfc_mqe *temp_mqe;
  246. struct lpfc_register doorbell;
  247. /* sanity check on queue memory */
  248. if (unlikely(!q))
  249. return -ENOMEM;
  250. temp_mqe = q->qe[q->host_index].mqe;
  251. /* If the host has not yet processed the next entry then we are done */
  252. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  253. return -ENOMEM;
  254. lpfc_sli4_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  255. /* Save off the mailbox pointer for completion */
  256. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  257. /* Update the host index before invoking device */
  258. q->host_index = ((q->host_index + 1) % q->entry_count);
  259. /* Ring Doorbell */
  260. doorbell.word0 = 0;
  261. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  262. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  263. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  264. return 0;
  265. }
  266. /**
  267. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  268. * @q: The Mailbox Queue to operate on.
  269. *
  270. * This routine will update the HBA index of a queue to reflect consumption of
  271. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  272. * an entry the host calls this function to update the queue's internal
  273. * pointers. This routine returns the number of entries that were consumed by
  274. * the HBA.
  275. **/
  276. static uint32_t
  277. lpfc_sli4_mq_release(struct lpfc_queue *q)
  278. {
  279. /* sanity check on queue memory */
  280. if (unlikely(!q))
  281. return 0;
  282. /* Clear the mailbox pointer for completion */
  283. q->phba->mbox = NULL;
  284. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  285. return 1;
  286. }
  287. /**
  288. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  289. * @q: The Event Queue to get the first valid EQE from
  290. *
  291. * This routine will get the first valid Event Queue Entry from @q, update
  292. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  293. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  294. * processed, but not popped back to the HBA then this routine will return NULL.
  295. **/
  296. static struct lpfc_eqe *
  297. lpfc_sli4_eq_get(struct lpfc_queue *q)
  298. {
  299. struct lpfc_hba *phba;
  300. struct lpfc_eqe *eqe;
  301. uint32_t idx;
  302. /* sanity check on queue memory */
  303. if (unlikely(!q))
  304. return NULL;
  305. phba = q->phba;
  306. eqe = q->qe[q->hba_index].eqe;
  307. /* If the next EQE is not valid then we are done */
  308. if (bf_get_le32(lpfc_eqe_valid, eqe) != q->qe_valid)
  309. return NULL;
  310. /* If the host has not yet processed the next entry then we are done */
  311. idx = ((q->hba_index + 1) % q->entry_count);
  312. if (idx == q->host_index)
  313. return NULL;
  314. q->hba_index = idx;
  315. /* if the index wrapped around, toggle the valid bit */
  316. if (phba->sli4_hba.pc_sli4_params.eqav && !q->hba_index)
  317. q->qe_valid = (q->qe_valid) ? 0 : 1;
  318. /*
  319. * insert barrier for instruction interlock : data from the hardware
  320. * must have the valid bit checked before it can be copied and acted
  321. * upon. Speculative instructions were allowing a bcopy at the start
  322. * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
  323. * after our return, to copy data before the valid bit check above
  324. * was done. As such, some of the copied data was stale. The barrier
  325. * ensures the check is before any data is copied.
  326. */
  327. mb();
  328. return eqe;
  329. }
  330. /**
  331. * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
  332. * @q: The Event Queue to disable interrupts
  333. *
  334. **/
  335. inline void
  336. lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
  337. {
  338. struct lpfc_register doorbell;
  339. doorbell.word0 = 0;
  340. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  341. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  342. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  343. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  344. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  345. writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  346. }
  347. /**
  348. * lpfc_sli4_if6_eq_clr_intr - Turn off interrupts from this EQ
  349. * @q: The Event Queue to disable interrupts
  350. *
  351. **/
  352. inline void
  353. lpfc_sli4_if6_eq_clr_intr(struct lpfc_queue *q)
  354. {
  355. struct lpfc_register doorbell;
  356. doorbell.word0 = 0;
  357. bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
  358. writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  359. }
  360. /**
  361. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  362. * @q: The Event Queue that the host has completed processing for.
  363. * @arm: Indicates whether the host wants to arms this CQ.
  364. *
  365. * This routine will mark all Event Queue Entries on @q, from the last
  366. * known completed entry to the last entry that was processed, as completed
  367. * by clearing the valid bit for each completion queue entry. Then it will
  368. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  369. * The internal host index in the @q will be updated by this routine to indicate
  370. * that the host has finished processing the entries. The @arm parameter
  371. * indicates that the queue should be rearmed when ringing the doorbell.
  372. *
  373. * This function will return the number of EQEs that were popped.
  374. **/
  375. uint32_t
  376. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  377. {
  378. uint32_t released = 0;
  379. struct lpfc_hba *phba;
  380. struct lpfc_eqe *temp_eqe;
  381. struct lpfc_register doorbell;
  382. /* sanity check on queue memory */
  383. if (unlikely(!q))
  384. return 0;
  385. phba = q->phba;
  386. /* while there are valid entries */
  387. while (q->hba_index != q->host_index) {
  388. if (!phba->sli4_hba.pc_sli4_params.eqav) {
  389. temp_eqe = q->qe[q->host_index].eqe;
  390. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  391. }
  392. released++;
  393. q->host_index = ((q->host_index + 1) % q->entry_count);
  394. }
  395. if (unlikely(released == 0 && !arm))
  396. return 0;
  397. /* ring doorbell for number popped */
  398. doorbell.word0 = 0;
  399. if (arm) {
  400. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  401. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  402. }
  403. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  404. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  405. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  406. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  407. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  408. writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  409. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  410. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  411. readl(q->phba->sli4_hba.EQDBregaddr);
  412. return released;
  413. }
  414. /**
  415. * lpfc_sli4_if6_eq_release - Indicates the host has finished processing an EQ
  416. * @q: The Event Queue that the host has completed processing for.
  417. * @arm: Indicates whether the host wants to arms this CQ.
  418. *
  419. * This routine will mark all Event Queue Entries on @q, from the last
  420. * known completed entry to the last entry that was processed, as completed
  421. * by clearing the valid bit for each completion queue entry. Then it will
  422. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  423. * The internal host index in the @q will be updated by this routine to indicate
  424. * that the host has finished processing the entries. The @arm parameter
  425. * indicates that the queue should be rearmed when ringing the doorbell.
  426. *
  427. * This function will return the number of EQEs that were popped.
  428. **/
  429. uint32_t
  430. lpfc_sli4_if6_eq_release(struct lpfc_queue *q, bool arm)
  431. {
  432. uint32_t released = 0;
  433. struct lpfc_hba *phba;
  434. struct lpfc_eqe *temp_eqe;
  435. struct lpfc_register doorbell;
  436. /* sanity check on queue memory */
  437. if (unlikely(!q))
  438. return 0;
  439. phba = q->phba;
  440. /* while there are valid entries */
  441. while (q->hba_index != q->host_index) {
  442. if (!phba->sli4_hba.pc_sli4_params.eqav) {
  443. temp_eqe = q->qe[q->host_index].eqe;
  444. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  445. }
  446. released++;
  447. q->host_index = ((q->host_index + 1) % q->entry_count);
  448. }
  449. if (unlikely(released == 0 && !arm))
  450. return 0;
  451. /* ring doorbell for number popped */
  452. doorbell.word0 = 0;
  453. if (arm)
  454. bf_set(lpfc_if6_eq_doorbell_arm, &doorbell, 1);
  455. bf_set(lpfc_if6_eq_doorbell_num_released, &doorbell, released);
  456. bf_set(lpfc_if6_eq_doorbell_eqid, &doorbell, q->queue_id);
  457. writel(doorbell.word0, q->phba->sli4_hba.EQDBregaddr);
  458. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  459. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  460. readl(q->phba->sli4_hba.EQDBregaddr);
  461. return released;
  462. }
  463. /**
  464. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  465. * @q: The Completion Queue to get the first valid CQE from
  466. *
  467. * This routine will get the first valid Completion Queue Entry from @q, update
  468. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  469. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  470. * processed, but not popped back to the HBA then this routine will return NULL.
  471. **/
  472. static struct lpfc_cqe *
  473. lpfc_sli4_cq_get(struct lpfc_queue *q)
  474. {
  475. struct lpfc_hba *phba;
  476. struct lpfc_cqe *cqe;
  477. uint32_t idx;
  478. /* sanity check on queue memory */
  479. if (unlikely(!q))
  480. return NULL;
  481. phba = q->phba;
  482. cqe = q->qe[q->hba_index].cqe;
  483. /* If the next CQE is not valid then we are done */
  484. if (bf_get_le32(lpfc_cqe_valid, cqe) != q->qe_valid)
  485. return NULL;
  486. /* If the host has not yet processed the next entry then we are done */
  487. idx = ((q->hba_index + 1) % q->entry_count);
  488. if (idx == q->host_index)
  489. return NULL;
  490. q->hba_index = idx;
  491. /* if the index wrapped around, toggle the valid bit */
  492. if (phba->sli4_hba.pc_sli4_params.cqav && !q->hba_index)
  493. q->qe_valid = (q->qe_valid) ? 0 : 1;
  494. /*
  495. * insert barrier for instruction interlock : data from the hardware
  496. * must have the valid bit checked before it can be copied and acted
  497. * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
  498. * instructions allowing action on content before valid bit checked,
  499. * add barrier here as well. May not be needed as "content" is a
  500. * single 32-bit entity here (vs multi word structure for cq's).
  501. */
  502. mb();
  503. return cqe;
  504. }
  505. /**
  506. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  507. * @q: The Completion Queue that the host has completed processing for.
  508. * @arm: Indicates whether the host wants to arms this CQ.
  509. *
  510. * This routine will mark all Completion queue entries on @q, from the last
  511. * known completed entry to the last entry that was processed, as completed
  512. * by clearing the valid bit for each completion queue entry. Then it will
  513. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  514. * The internal host index in the @q will be updated by this routine to indicate
  515. * that the host has finished processing the entries. The @arm parameter
  516. * indicates that the queue should be rearmed when ringing the doorbell.
  517. *
  518. * This function will return the number of CQEs that were released.
  519. **/
  520. uint32_t
  521. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  522. {
  523. uint32_t released = 0;
  524. struct lpfc_hba *phba;
  525. struct lpfc_cqe *temp_qe;
  526. struct lpfc_register doorbell;
  527. /* sanity check on queue memory */
  528. if (unlikely(!q))
  529. return 0;
  530. phba = q->phba;
  531. /* while there are valid entries */
  532. while (q->hba_index != q->host_index) {
  533. if (!phba->sli4_hba.pc_sli4_params.cqav) {
  534. temp_qe = q->qe[q->host_index].cqe;
  535. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  536. }
  537. released++;
  538. q->host_index = ((q->host_index + 1) % q->entry_count);
  539. }
  540. if (unlikely(released == 0 && !arm))
  541. return 0;
  542. /* ring doorbell for number popped */
  543. doorbell.word0 = 0;
  544. if (arm)
  545. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  546. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  547. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  548. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  549. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  550. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  551. writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
  552. return released;
  553. }
  554. /**
  555. * lpfc_sli4_if6_cq_release - Indicates the host has finished processing a CQ
  556. * @q: The Completion Queue that the host has completed processing for.
  557. * @arm: Indicates whether the host wants to arms this CQ.
  558. *
  559. * This routine will mark all Completion queue entries on @q, from the last
  560. * known completed entry to the last entry that was processed, as completed
  561. * by clearing the valid bit for each completion queue entry. Then it will
  562. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  563. * The internal host index in the @q will be updated by this routine to indicate
  564. * that the host has finished processing the entries. The @arm parameter
  565. * indicates that the queue should be rearmed when ringing the doorbell.
  566. *
  567. * This function will return the number of CQEs that were released.
  568. **/
  569. uint32_t
  570. lpfc_sli4_if6_cq_release(struct lpfc_queue *q, bool arm)
  571. {
  572. uint32_t released = 0;
  573. struct lpfc_hba *phba;
  574. struct lpfc_cqe *temp_qe;
  575. struct lpfc_register doorbell;
  576. /* sanity check on queue memory */
  577. if (unlikely(!q))
  578. return 0;
  579. phba = q->phba;
  580. /* while there are valid entries */
  581. while (q->hba_index != q->host_index) {
  582. if (!phba->sli4_hba.pc_sli4_params.cqav) {
  583. temp_qe = q->qe[q->host_index].cqe;
  584. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  585. }
  586. released++;
  587. q->host_index = ((q->host_index + 1) % q->entry_count);
  588. }
  589. if (unlikely(released == 0 && !arm))
  590. return 0;
  591. /* ring doorbell for number popped */
  592. doorbell.word0 = 0;
  593. if (arm)
  594. bf_set(lpfc_if6_cq_doorbell_arm, &doorbell, 1);
  595. bf_set(lpfc_if6_cq_doorbell_num_released, &doorbell, released);
  596. bf_set(lpfc_if6_cq_doorbell_cqid, &doorbell, q->queue_id);
  597. writel(doorbell.word0, q->phba->sli4_hba.CQDBregaddr);
  598. return released;
  599. }
  600. /**
  601. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  602. * @q: The Header Receive Queue to operate on.
  603. * @wqe: The Receive Queue Entry to put on the Receive queue.
  604. *
  605. * This routine will copy the contents of @wqe to the next available entry on
  606. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  607. * HBA to start processing the Receive Queue Entry. This function returns the
  608. * index that the rqe was copied to if successful. If no entries are available
  609. * on @q then this function will return -ENOMEM.
  610. * The caller is expected to hold the hbalock when calling this routine.
  611. **/
  612. int
  613. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  614. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  615. {
  616. struct lpfc_rqe *temp_hrqe;
  617. struct lpfc_rqe *temp_drqe;
  618. struct lpfc_register doorbell;
  619. int hq_put_index;
  620. int dq_put_index;
  621. /* sanity check on queue memory */
  622. if (unlikely(!hq) || unlikely(!dq))
  623. return -ENOMEM;
  624. hq_put_index = hq->host_index;
  625. dq_put_index = dq->host_index;
  626. temp_hrqe = hq->qe[hq_put_index].rqe;
  627. temp_drqe = dq->qe[dq_put_index].rqe;
  628. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  629. return -EINVAL;
  630. if (hq_put_index != dq_put_index)
  631. return -EINVAL;
  632. /* If the host has not yet processed the next entry then we are done */
  633. if (((hq_put_index + 1) % hq->entry_count) == hq->hba_index)
  634. return -EBUSY;
  635. lpfc_sli4_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  636. lpfc_sli4_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  637. /* Update the host index to point to the next slot */
  638. hq->host_index = ((hq_put_index + 1) % hq->entry_count);
  639. dq->host_index = ((dq_put_index + 1) % dq->entry_count);
  640. hq->RQ_buf_posted++;
  641. /* Ring The Header Receive Queue Doorbell */
  642. if (!(hq->host_index % hq->entry_repost)) {
  643. doorbell.word0 = 0;
  644. if (hq->db_format == LPFC_DB_RING_FORMAT) {
  645. bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  646. hq->entry_repost);
  647. bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  648. } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  649. bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  650. hq->entry_repost);
  651. bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  652. hq->host_index);
  653. bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  654. } else {
  655. return -EINVAL;
  656. }
  657. writel(doorbell.word0, hq->db_regaddr);
  658. }
  659. return hq_put_index;
  660. }
  661. /**
  662. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  663. * @q: The Header Receive Queue to operate on.
  664. *
  665. * This routine will update the HBA index of a queue to reflect consumption of
  666. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  667. * consumed an entry the host calls this function to update the queue's
  668. * internal pointers. This routine returns the number of entries that were
  669. * consumed by the HBA.
  670. **/
  671. static uint32_t
  672. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  673. {
  674. /* sanity check on queue memory */
  675. if (unlikely(!hq) || unlikely(!dq))
  676. return 0;
  677. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  678. return 0;
  679. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  680. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  681. return 1;
  682. }
  683. /**
  684. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  685. * @phba: Pointer to HBA context object.
  686. * @pring: Pointer to driver SLI ring object.
  687. *
  688. * This function returns pointer to next command iocb entry
  689. * in the command ring. The caller must hold hbalock to prevent
  690. * other threads consume the next command iocb.
  691. * SLI-2/SLI-3 provide different sized iocbs.
  692. **/
  693. static inline IOCB_t *
  694. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  695. {
  696. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  697. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  698. }
  699. /**
  700. * lpfc_resp_iocb - Get next response iocb entry in the ring
  701. * @phba: Pointer to HBA context object.
  702. * @pring: Pointer to driver SLI ring object.
  703. *
  704. * This function returns pointer to next response iocb entry
  705. * in the response ring. The caller must hold hbalock to make sure
  706. * that no other thread consume the next response iocb.
  707. * SLI-2/SLI-3 provide different sized iocbs.
  708. **/
  709. static inline IOCB_t *
  710. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  711. {
  712. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  713. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  714. }
  715. /**
  716. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  717. * @phba: Pointer to HBA context object.
  718. *
  719. * This function is called with hbalock held. This function
  720. * allocates a new driver iocb object from the iocb pool. If the
  721. * allocation is successful, it returns pointer to the newly
  722. * allocated iocb object else it returns NULL.
  723. **/
  724. struct lpfc_iocbq *
  725. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  726. {
  727. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  728. struct lpfc_iocbq * iocbq = NULL;
  729. lockdep_assert_held(&phba->hbalock);
  730. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  731. if (iocbq)
  732. phba->iocb_cnt++;
  733. if (phba->iocb_cnt > phba->iocb_max)
  734. phba->iocb_max = phba->iocb_cnt;
  735. return iocbq;
  736. }
  737. /**
  738. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  739. * @phba: Pointer to HBA context object.
  740. * @xritag: XRI value.
  741. *
  742. * This function clears the sglq pointer from the array of acive
  743. * sglq's. The xritag that is passed in is used to index into the
  744. * array. Before the xritag can be used it needs to be adjusted
  745. * by subtracting the xribase.
  746. *
  747. * Returns sglq ponter = success, NULL = Failure.
  748. **/
  749. struct lpfc_sglq *
  750. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  751. {
  752. struct lpfc_sglq *sglq;
  753. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  754. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  755. return sglq;
  756. }
  757. /**
  758. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  759. * @phba: Pointer to HBA context object.
  760. * @xritag: XRI value.
  761. *
  762. * This function returns the sglq pointer from the array of acive
  763. * sglq's. The xritag that is passed in is used to index into the
  764. * array. Before the xritag can be used it needs to be adjusted
  765. * by subtracting the xribase.
  766. *
  767. * Returns sglq ponter = success, NULL = Failure.
  768. **/
  769. struct lpfc_sglq *
  770. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  771. {
  772. struct lpfc_sglq *sglq;
  773. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  774. return sglq;
  775. }
  776. /**
  777. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  778. * @phba: Pointer to HBA context object.
  779. * @xritag: xri used in this exchange.
  780. * @rrq: The RRQ to be cleared.
  781. *
  782. **/
  783. void
  784. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  785. uint16_t xritag,
  786. struct lpfc_node_rrq *rrq)
  787. {
  788. struct lpfc_nodelist *ndlp = NULL;
  789. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  790. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  791. /* The target DID could have been swapped (cable swap)
  792. * we should use the ndlp from the findnode if it is
  793. * available.
  794. */
  795. if ((!ndlp) && rrq->ndlp)
  796. ndlp = rrq->ndlp;
  797. if (!ndlp)
  798. goto out;
  799. if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
  800. rrq->send_rrq = 0;
  801. rrq->xritag = 0;
  802. rrq->rrq_stop_time = 0;
  803. }
  804. out:
  805. mempool_free(rrq, phba->rrq_pool);
  806. }
  807. /**
  808. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  809. * @phba: Pointer to HBA context object.
  810. *
  811. * This function is called with hbalock held. This function
  812. * Checks if stop_time (ratov from setting rrq active) has
  813. * been reached, if it has and the send_rrq flag is set then
  814. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  815. * then it will just call the routine to clear the rrq and
  816. * free the rrq resource.
  817. * The timer is set to the next rrq that is going to expire before
  818. * leaving the routine.
  819. *
  820. **/
  821. void
  822. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  823. {
  824. struct lpfc_node_rrq *rrq;
  825. struct lpfc_node_rrq *nextrrq;
  826. unsigned long next_time;
  827. unsigned long iflags;
  828. LIST_HEAD(send_rrq);
  829. spin_lock_irqsave(&phba->hbalock, iflags);
  830. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  831. next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  832. list_for_each_entry_safe(rrq, nextrrq,
  833. &phba->active_rrq_list, list) {
  834. if (time_after(jiffies, rrq->rrq_stop_time))
  835. list_move(&rrq->list, &send_rrq);
  836. else if (time_before(rrq->rrq_stop_time, next_time))
  837. next_time = rrq->rrq_stop_time;
  838. }
  839. spin_unlock_irqrestore(&phba->hbalock, iflags);
  840. if ((!list_empty(&phba->active_rrq_list)) &&
  841. (!(phba->pport->load_flag & FC_UNLOADING)))
  842. mod_timer(&phba->rrq_tmr, next_time);
  843. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  844. list_del(&rrq->list);
  845. if (!rrq->send_rrq)
  846. /* this call will free the rrq */
  847. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  848. else if (lpfc_send_rrq(phba, rrq)) {
  849. /* if we send the rrq then the completion handler
  850. * will clear the bit in the xribitmap.
  851. */
  852. lpfc_clr_rrq_active(phba, rrq->xritag,
  853. rrq);
  854. }
  855. }
  856. }
  857. /**
  858. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  859. * @vport: Pointer to vport context object.
  860. * @xri: The xri used in the exchange.
  861. * @did: The targets DID for this exchange.
  862. *
  863. * returns NULL = rrq not found in the phba->active_rrq_list.
  864. * rrq = rrq for this xri and target.
  865. **/
  866. struct lpfc_node_rrq *
  867. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  868. {
  869. struct lpfc_hba *phba = vport->phba;
  870. struct lpfc_node_rrq *rrq;
  871. struct lpfc_node_rrq *nextrrq;
  872. unsigned long iflags;
  873. if (phba->sli_rev != LPFC_SLI_REV4)
  874. return NULL;
  875. spin_lock_irqsave(&phba->hbalock, iflags);
  876. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  877. if (rrq->vport == vport && rrq->xritag == xri &&
  878. rrq->nlp_DID == did){
  879. list_del(&rrq->list);
  880. spin_unlock_irqrestore(&phba->hbalock, iflags);
  881. return rrq;
  882. }
  883. }
  884. spin_unlock_irqrestore(&phba->hbalock, iflags);
  885. return NULL;
  886. }
  887. /**
  888. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  889. * @vport: Pointer to vport context object.
  890. * @ndlp: Pointer to the lpfc_node_list structure.
  891. * If ndlp is NULL Remove all active RRQs for this vport from the
  892. * phba->active_rrq_list and clear the rrq.
  893. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  894. **/
  895. void
  896. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  897. {
  898. struct lpfc_hba *phba = vport->phba;
  899. struct lpfc_node_rrq *rrq;
  900. struct lpfc_node_rrq *nextrrq;
  901. unsigned long iflags;
  902. LIST_HEAD(rrq_list);
  903. if (phba->sli_rev != LPFC_SLI_REV4)
  904. return;
  905. if (!ndlp) {
  906. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  907. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  908. }
  909. spin_lock_irqsave(&phba->hbalock, iflags);
  910. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  911. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  912. list_move(&rrq->list, &rrq_list);
  913. spin_unlock_irqrestore(&phba->hbalock, iflags);
  914. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  915. list_del(&rrq->list);
  916. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  917. }
  918. }
  919. /**
  920. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  921. * @phba: Pointer to HBA context object.
  922. * @ndlp: Targets nodelist pointer for this exchange.
  923. * @xritag the xri in the bitmap to test.
  924. *
  925. * This function is called with hbalock held. This function
  926. * returns 0 = rrq not active for this xri
  927. * 1 = rrq is valid for this xri.
  928. **/
  929. int
  930. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  931. uint16_t xritag)
  932. {
  933. lockdep_assert_held(&phba->hbalock);
  934. if (!ndlp)
  935. return 0;
  936. if (!ndlp->active_rrqs_xri_bitmap)
  937. return 0;
  938. if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  939. return 1;
  940. else
  941. return 0;
  942. }
  943. /**
  944. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  945. * @phba: Pointer to HBA context object.
  946. * @ndlp: nodelist pointer for this target.
  947. * @xritag: xri used in this exchange.
  948. * @rxid: Remote Exchange ID.
  949. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  950. *
  951. * This function takes the hbalock.
  952. * The active bit is always set in the active rrq xri_bitmap even
  953. * if there is no slot avaiable for the other rrq information.
  954. *
  955. * returns 0 rrq actived for this xri
  956. * < 0 No memory or invalid ndlp.
  957. **/
  958. int
  959. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  960. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  961. {
  962. unsigned long iflags;
  963. struct lpfc_node_rrq *rrq;
  964. int empty;
  965. if (!ndlp)
  966. return -EINVAL;
  967. if (!phba->cfg_enable_rrq)
  968. return -EINVAL;
  969. spin_lock_irqsave(&phba->hbalock, iflags);
  970. if (phba->pport->load_flag & FC_UNLOADING) {
  971. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  972. goto out;
  973. }
  974. /*
  975. * set the active bit even if there is no mem available.
  976. */
  977. if (NLP_CHK_FREE_REQ(ndlp))
  978. goto out;
  979. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  980. goto out;
  981. if (!ndlp->active_rrqs_xri_bitmap)
  982. goto out;
  983. if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
  984. goto out;
  985. spin_unlock_irqrestore(&phba->hbalock, iflags);
  986. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  987. if (!rrq) {
  988. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  989. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  990. " DID:0x%x Send:%d\n",
  991. xritag, rxid, ndlp->nlp_DID, send_rrq);
  992. return -EINVAL;
  993. }
  994. if (phba->cfg_enable_rrq == 1)
  995. rrq->send_rrq = send_rrq;
  996. else
  997. rrq->send_rrq = 0;
  998. rrq->xritag = xritag;
  999. rrq->rrq_stop_time = jiffies +
  1000. msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
  1001. rrq->ndlp = ndlp;
  1002. rrq->nlp_DID = ndlp->nlp_DID;
  1003. rrq->vport = ndlp->vport;
  1004. rrq->rxid = rxid;
  1005. spin_lock_irqsave(&phba->hbalock, iflags);
  1006. empty = list_empty(&phba->active_rrq_list);
  1007. list_add_tail(&rrq->list, &phba->active_rrq_list);
  1008. phba->hba_flag |= HBA_RRQ_ACTIVE;
  1009. if (empty)
  1010. lpfc_worker_wake_up(phba);
  1011. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1012. return 0;
  1013. out:
  1014. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1015. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  1016. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  1017. " DID:0x%x Send:%d\n",
  1018. xritag, rxid, ndlp->nlp_DID, send_rrq);
  1019. return -EINVAL;
  1020. }
  1021. /**
  1022. * __lpfc_sli_get_els_sglq - Allocates an iocb object from sgl pool
  1023. * @phba: Pointer to HBA context object.
  1024. * @piocb: Pointer to the iocbq.
  1025. *
  1026. * This function is called with the ring lock held. This function
  1027. * gets a new driver sglq object from the sglq list. If the
  1028. * list is not empty then it is successful, it returns pointer to the newly
  1029. * allocated sglq object else it returns NULL.
  1030. **/
  1031. static struct lpfc_sglq *
  1032. __lpfc_sli_get_els_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  1033. {
  1034. struct list_head *lpfc_els_sgl_list = &phba->sli4_hba.lpfc_els_sgl_list;
  1035. struct lpfc_sglq *sglq = NULL;
  1036. struct lpfc_sglq *start_sglq = NULL;
  1037. struct lpfc_scsi_buf *lpfc_cmd;
  1038. struct lpfc_nodelist *ndlp;
  1039. int found = 0;
  1040. lockdep_assert_held(&phba->hbalock);
  1041. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  1042. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  1043. ndlp = lpfc_cmd->rdata->pnode;
  1044. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  1045. !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  1046. ndlp = piocbq->context_un.ndlp;
  1047. } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
  1048. if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
  1049. ndlp = NULL;
  1050. else
  1051. ndlp = piocbq->context_un.ndlp;
  1052. } else {
  1053. ndlp = piocbq->context1;
  1054. }
  1055. spin_lock(&phba->sli4_hba.sgl_list_lock);
  1056. list_remove_head(lpfc_els_sgl_list, sglq, struct lpfc_sglq, list);
  1057. start_sglq = sglq;
  1058. while (!found) {
  1059. if (!sglq)
  1060. break;
  1061. if (ndlp && ndlp->active_rrqs_xri_bitmap &&
  1062. test_bit(sglq->sli4_lxritag,
  1063. ndlp->active_rrqs_xri_bitmap)) {
  1064. /* This xri has an rrq outstanding for this DID.
  1065. * put it back in the list and get another xri.
  1066. */
  1067. list_add_tail(&sglq->list, lpfc_els_sgl_list);
  1068. sglq = NULL;
  1069. list_remove_head(lpfc_els_sgl_list, sglq,
  1070. struct lpfc_sglq, list);
  1071. if (sglq == start_sglq) {
  1072. list_add_tail(&sglq->list, lpfc_els_sgl_list);
  1073. sglq = NULL;
  1074. break;
  1075. } else
  1076. continue;
  1077. }
  1078. sglq->ndlp = ndlp;
  1079. found = 1;
  1080. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  1081. sglq->state = SGL_ALLOCATED;
  1082. }
  1083. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  1084. return sglq;
  1085. }
  1086. /**
  1087. * __lpfc_sli_get_nvmet_sglq - Allocates an iocb object from sgl pool
  1088. * @phba: Pointer to HBA context object.
  1089. * @piocb: Pointer to the iocbq.
  1090. *
  1091. * This function is called with the sgl_list lock held. This function
  1092. * gets a new driver sglq object from the sglq list. If the
  1093. * list is not empty then it is successful, it returns pointer to the newly
  1094. * allocated sglq object else it returns NULL.
  1095. **/
  1096. struct lpfc_sglq *
  1097. __lpfc_sli_get_nvmet_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  1098. {
  1099. struct list_head *lpfc_nvmet_sgl_list;
  1100. struct lpfc_sglq *sglq = NULL;
  1101. lpfc_nvmet_sgl_list = &phba->sli4_hba.lpfc_nvmet_sgl_list;
  1102. lockdep_assert_held(&phba->sli4_hba.sgl_list_lock);
  1103. list_remove_head(lpfc_nvmet_sgl_list, sglq, struct lpfc_sglq, list);
  1104. if (!sglq)
  1105. return NULL;
  1106. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  1107. sglq->state = SGL_ALLOCATED;
  1108. return sglq;
  1109. }
  1110. /**
  1111. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  1112. * @phba: Pointer to HBA context object.
  1113. *
  1114. * This function is called with no lock held. This function
  1115. * allocates a new driver iocb object from the iocb pool. If the
  1116. * allocation is successful, it returns pointer to the newly
  1117. * allocated iocb object else it returns NULL.
  1118. **/
  1119. struct lpfc_iocbq *
  1120. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  1121. {
  1122. struct lpfc_iocbq * iocbq = NULL;
  1123. unsigned long iflags;
  1124. spin_lock_irqsave(&phba->hbalock, iflags);
  1125. iocbq = __lpfc_sli_get_iocbq(phba);
  1126. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1127. return iocbq;
  1128. }
  1129. /**
  1130. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  1131. * @phba: Pointer to HBA context object.
  1132. * @iocbq: Pointer to driver iocb object.
  1133. *
  1134. * This function is called with hbalock held to release driver
  1135. * iocb object to the iocb pool. The iotag in the iocb object
  1136. * does not change for each use of the iocb object. This function
  1137. * clears all other fields of the iocb object when it is freed.
  1138. * The sqlq structure that holds the xritag and phys and virtual
  1139. * mappings for the scatter gather list is retrieved from the
  1140. * active array of sglq. The get of the sglq pointer also clears
  1141. * the entry in the array. If the status of the IO indiactes that
  1142. * this IO was aborted then the sglq entry it put on the
  1143. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  1144. * IO has good status or fails for any other reason then the sglq
  1145. * entry is added to the free list (lpfc_els_sgl_list).
  1146. **/
  1147. static void
  1148. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1149. {
  1150. struct lpfc_sglq *sglq;
  1151. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  1152. unsigned long iflag = 0;
  1153. struct lpfc_sli_ring *pring;
  1154. lockdep_assert_held(&phba->hbalock);
  1155. if (iocbq->sli4_xritag == NO_XRI)
  1156. sglq = NULL;
  1157. else
  1158. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  1159. if (sglq) {
  1160. if (iocbq->iocb_flag & LPFC_IO_NVMET) {
  1161. spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
  1162. iflag);
  1163. sglq->state = SGL_FREED;
  1164. sglq->ndlp = NULL;
  1165. list_add_tail(&sglq->list,
  1166. &phba->sli4_hba.lpfc_nvmet_sgl_list);
  1167. spin_unlock_irqrestore(
  1168. &phba->sli4_hba.sgl_list_lock, iflag);
  1169. goto out;
  1170. }
  1171. pring = phba->sli4_hba.els_wq->pring;
  1172. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  1173. (sglq->state != SGL_XRI_ABORTED)) {
  1174. spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
  1175. iflag);
  1176. list_add(&sglq->list,
  1177. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  1178. spin_unlock_irqrestore(
  1179. &phba->sli4_hba.sgl_list_lock, iflag);
  1180. } else {
  1181. spin_lock_irqsave(&phba->sli4_hba.sgl_list_lock,
  1182. iflag);
  1183. sglq->state = SGL_FREED;
  1184. sglq->ndlp = NULL;
  1185. list_add_tail(&sglq->list,
  1186. &phba->sli4_hba.lpfc_els_sgl_list);
  1187. spin_unlock_irqrestore(
  1188. &phba->sli4_hba.sgl_list_lock, iflag);
  1189. /* Check if TXQ queue needs to be serviced */
  1190. if (!list_empty(&pring->txq))
  1191. lpfc_worker_wake_up(phba);
  1192. }
  1193. }
  1194. out:
  1195. /*
  1196. * Clean all volatile data fields, preserve iotag and node struct.
  1197. */
  1198. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  1199. iocbq->sli4_lxritag = NO_XRI;
  1200. iocbq->sli4_xritag = NO_XRI;
  1201. iocbq->iocb_flag &= ~(LPFC_IO_NVME | LPFC_IO_NVMET |
  1202. LPFC_IO_NVME_LS);
  1203. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  1204. }
  1205. /**
  1206. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  1207. * @phba: Pointer to HBA context object.
  1208. * @iocbq: Pointer to driver iocb object.
  1209. *
  1210. * This function is called with hbalock held to release driver
  1211. * iocb object to the iocb pool. The iotag in the iocb object
  1212. * does not change for each use of the iocb object. This function
  1213. * clears all other fields of the iocb object when it is freed.
  1214. **/
  1215. static void
  1216. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1217. {
  1218. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  1219. lockdep_assert_held(&phba->hbalock);
  1220. /*
  1221. * Clean all volatile data fields, preserve iotag and node struct.
  1222. */
  1223. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  1224. iocbq->sli4_xritag = NO_XRI;
  1225. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  1226. }
  1227. /**
  1228. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  1229. * @phba: Pointer to HBA context object.
  1230. * @iocbq: Pointer to driver iocb object.
  1231. *
  1232. * This function is called with hbalock held to release driver
  1233. * iocb object to the iocb pool. The iotag in the iocb object
  1234. * does not change for each use of the iocb object. This function
  1235. * clears all other fields of the iocb object when it is freed.
  1236. **/
  1237. static void
  1238. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1239. {
  1240. lockdep_assert_held(&phba->hbalock);
  1241. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  1242. phba->iocb_cnt--;
  1243. }
  1244. /**
  1245. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  1246. * @phba: Pointer to HBA context object.
  1247. * @iocbq: Pointer to driver iocb object.
  1248. *
  1249. * This function is called with no lock held to release the iocb to
  1250. * iocb pool.
  1251. **/
  1252. void
  1253. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1254. {
  1255. unsigned long iflags;
  1256. /*
  1257. * Clean all volatile data fields, preserve iotag and node struct.
  1258. */
  1259. spin_lock_irqsave(&phba->hbalock, iflags);
  1260. __lpfc_sli_release_iocbq(phba, iocbq);
  1261. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1262. }
  1263. /**
  1264. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  1265. * @phba: Pointer to HBA context object.
  1266. * @iocblist: List of IOCBs.
  1267. * @ulpstatus: ULP status in IOCB command field.
  1268. * @ulpWord4: ULP word-4 in IOCB command field.
  1269. *
  1270. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  1271. * on the list by invoking the complete callback function associated with the
  1272. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  1273. * fields.
  1274. **/
  1275. void
  1276. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  1277. uint32_t ulpstatus, uint32_t ulpWord4)
  1278. {
  1279. struct lpfc_iocbq *piocb;
  1280. while (!list_empty(iocblist)) {
  1281. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  1282. if (!piocb->iocb_cmpl)
  1283. lpfc_sli_release_iocbq(phba, piocb);
  1284. else {
  1285. piocb->iocb.ulpStatus = ulpstatus;
  1286. piocb->iocb.un.ulpWord[4] = ulpWord4;
  1287. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1288. }
  1289. }
  1290. return;
  1291. }
  1292. /**
  1293. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1294. * @iocb_cmnd: iocb command code.
  1295. *
  1296. * This function is called by ring event handler function to get the iocb type.
  1297. * This function translates the iocb command to an iocb command type used to
  1298. * decide the final disposition of each completed IOCB.
  1299. * The function returns
  1300. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1301. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1302. * LPFC_ABORT_IOCB if it is an abort iocb
  1303. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1304. *
  1305. * The caller is not required to hold any lock.
  1306. **/
  1307. static lpfc_iocb_type
  1308. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1309. {
  1310. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1311. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1312. return 0;
  1313. switch (iocb_cmnd) {
  1314. case CMD_XMIT_SEQUENCE_CR:
  1315. case CMD_XMIT_SEQUENCE_CX:
  1316. case CMD_XMIT_BCAST_CN:
  1317. case CMD_XMIT_BCAST_CX:
  1318. case CMD_ELS_REQUEST_CR:
  1319. case CMD_ELS_REQUEST_CX:
  1320. case CMD_CREATE_XRI_CR:
  1321. case CMD_CREATE_XRI_CX:
  1322. case CMD_GET_RPI_CN:
  1323. case CMD_XMIT_ELS_RSP_CX:
  1324. case CMD_GET_RPI_CR:
  1325. case CMD_FCP_IWRITE_CR:
  1326. case CMD_FCP_IWRITE_CX:
  1327. case CMD_FCP_IREAD_CR:
  1328. case CMD_FCP_IREAD_CX:
  1329. case CMD_FCP_ICMND_CR:
  1330. case CMD_FCP_ICMND_CX:
  1331. case CMD_FCP_TSEND_CX:
  1332. case CMD_FCP_TRSP_CX:
  1333. case CMD_FCP_TRECEIVE_CX:
  1334. case CMD_FCP_AUTO_TRSP_CX:
  1335. case CMD_ADAPTER_MSG:
  1336. case CMD_ADAPTER_DUMP:
  1337. case CMD_XMIT_SEQUENCE64_CR:
  1338. case CMD_XMIT_SEQUENCE64_CX:
  1339. case CMD_XMIT_BCAST64_CN:
  1340. case CMD_XMIT_BCAST64_CX:
  1341. case CMD_ELS_REQUEST64_CR:
  1342. case CMD_ELS_REQUEST64_CX:
  1343. case CMD_FCP_IWRITE64_CR:
  1344. case CMD_FCP_IWRITE64_CX:
  1345. case CMD_FCP_IREAD64_CR:
  1346. case CMD_FCP_IREAD64_CX:
  1347. case CMD_FCP_ICMND64_CR:
  1348. case CMD_FCP_ICMND64_CX:
  1349. case CMD_FCP_TSEND64_CX:
  1350. case CMD_FCP_TRSP64_CX:
  1351. case CMD_FCP_TRECEIVE64_CX:
  1352. case CMD_GEN_REQUEST64_CR:
  1353. case CMD_GEN_REQUEST64_CX:
  1354. case CMD_XMIT_ELS_RSP64_CX:
  1355. case DSSCMD_IWRITE64_CR:
  1356. case DSSCMD_IWRITE64_CX:
  1357. case DSSCMD_IREAD64_CR:
  1358. case DSSCMD_IREAD64_CX:
  1359. type = LPFC_SOL_IOCB;
  1360. break;
  1361. case CMD_ABORT_XRI_CN:
  1362. case CMD_ABORT_XRI_CX:
  1363. case CMD_CLOSE_XRI_CN:
  1364. case CMD_CLOSE_XRI_CX:
  1365. case CMD_XRI_ABORTED_CX:
  1366. case CMD_ABORT_MXRI64_CN:
  1367. case CMD_XMIT_BLS_RSP64_CX:
  1368. type = LPFC_ABORT_IOCB;
  1369. break;
  1370. case CMD_RCV_SEQUENCE_CX:
  1371. case CMD_RCV_ELS_REQ_CX:
  1372. case CMD_RCV_SEQUENCE64_CX:
  1373. case CMD_RCV_ELS_REQ64_CX:
  1374. case CMD_ASYNC_STATUS:
  1375. case CMD_IOCB_RCV_SEQ64_CX:
  1376. case CMD_IOCB_RCV_ELS64_CX:
  1377. case CMD_IOCB_RCV_CONT64_CX:
  1378. case CMD_IOCB_RET_XRI64_CX:
  1379. type = LPFC_UNSOL_IOCB;
  1380. break;
  1381. case CMD_IOCB_XMIT_MSEQ64_CR:
  1382. case CMD_IOCB_XMIT_MSEQ64_CX:
  1383. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1384. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1385. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1386. case CMD_IOCB_ABORT_EXTENDED_CN:
  1387. case CMD_IOCB_RET_HBQE64_CN:
  1388. case CMD_IOCB_FCP_IBIDIR64_CR:
  1389. case CMD_IOCB_FCP_IBIDIR64_CX:
  1390. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1391. case CMD_IOCB_LOGENTRY_CN:
  1392. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1393. printk("%s - Unhandled SLI-3 Command x%x\n",
  1394. __func__, iocb_cmnd);
  1395. type = LPFC_UNKNOWN_IOCB;
  1396. break;
  1397. default:
  1398. type = LPFC_UNKNOWN_IOCB;
  1399. break;
  1400. }
  1401. return type;
  1402. }
  1403. /**
  1404. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1405. * @phba: Pointer to HBA context object.
  1406. *
  1407. * This function is called from SLI initialization code
  1408. * to configure every ring of the HBA's SLI interface. The
  1409. * caller is not required to hold any lock. This function issues
  1410. * a config_ring mailbox command for each ring.
  1411. * This function returns zero if successful else returns a negative
  1412. * error code.
  1413. **/
  1414. static int
  1415. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1416. {
  1417. struct lpfc_sli *psli = &phba->sli;
  1418. LPFC_MBOXQ_t *pmb;
  1419. MAILBOX_t *pmbox;
  1420. int i, rc, ret = 0;
  1421. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1422. if (!pmb)
  1423. return -ENOMEM;
  1424. pmbox = &pmb->u.mb;
  1425. phba->link_state = LPFC_INIT_MBX_CMDS;
  1426. for (i = 0; i < psli->num_rings; i++) {
  1427. lpfc_config_ring(phba, i, pmb);
  1428. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1429. if (rc != MBX_SUCCESS) {
  1430. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1431. "0446 Adapter failed to init (%d), "
  1432. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1433. "ring %d\n",
  1434. rc, pmbox->mbxCommand,
  1435. pmbox->mbxStatus, i);
  1436. phba->link_state = LPFC_HBA_ERROR;
  1437. ret = -ENXIO;
  1438. break;
  1439. }
  1440. }
  1441. mempool_free(pmb, phba->mbox_mem_pool);
  1442. return ret;
  1443. }
  1444. /**
  1445. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1446. * @phba: Pointer to HBA context object.
  1447. * @pring: Pointer to driver SLI ring object.
  1448. * @piocb: Pointer to the driver iocb object.
  1449. *
  1450. * This function is called with hbalock held. The function adds the
  1451. * new iocb to txcmplq of the given ring. This function always returns
  1452. * 0. If this function is called for ELS ring, this function checks if
  1453. * there is a vport associated with the ELS command. This function also
  1454. * starts els_tmofunc timer if this is an ELS command.
  1455. **/
  1456. static int
  1457. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1458. struct lpfc_iocbq *piocb)
  1459. {
  1460. lockdep_assert_held(&phba->hbalock);
  1461. BUG_ON(!piocb);
  1462. list_add_tail(&piocb->list, &pring->txcmplq);
  1463. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1464. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1465. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1466. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1467. BUG_ON(!piocb->vport);
  1468. if (!(piocb->vport->load_flag & FC_UNLOADING))
  1469. mod_timer(&piocb->vport->els_tmofunc,
  1470. jiffies +
  1471. msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
  1472. }
  1473. return 0;
  1474. }
  1475. /**
  1476. * lpfc_sli_ringtx_get - Get first element of the txq
  1477. * @phba: Pointer to HBA context object.
  1478. * @pring: Pointer to driver SLI ring object.
  1479. *
  1480. * This function is called with hbalock held to get next
  1481. * iocb in txq of the given ring. If there is any iocb in
  1482. * the txq, the function returns first iocb in the list after
  1483. * removing the iocb from the list, else it returns NULL.
  1484. **/
  1485. struct lpfc_iocbq *
  1486. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1487. {
  1488. struct lpfc_iocbq *cmd_iocb;
  1489. lockdep_assert_held(&phba->hbalock);
  1490. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1491. return cmd_iocb;
  1492. }
  1493. /**
  1494. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1495. * @phba: Pointer to HBA context object.
  1496. * @pring: Pointer to driver SLI ring object.
  1497. *
  1498. * This function is called with hbalock held and the caller must post the
  1499. * iocb without releasing the lock. If the caller releases the lock,
  1500. * iocb slot returned by the function is not guaranteed to be available.
  1501. * The function returns pointer to the next available iocb slot if there
  1502. * is available slot in the ring, else it returns NULL.
  1503. * If the get index of the ring is ahead of the put index, the function
  1504. * will post an error attention event to the worker thread to take the
  1505. * HBA to offline state.
  1506. **/
  1507. static IOCB_t *
  1508. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1509. {
  1510. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1511. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1512. lockdep_assert_held(&phba->hbalock);
  1513. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1514. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1515. pring->sli.sli3.next_cmdidx = 0;
  1516. if (unlikely(pring->sli.sli3.local_getidx ==
  1517. pring->sli.sli3.next_cmdidx)) {
  1518. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1519. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1520. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1521. "0315 Ring %d issue: portCmdGet %d "
  1522. "is bigger than cmd ring %d\n",
  1523. pring->ringno,
  1524. pring->sli.sli3.local_getidx,
  1525. max_cmd_idx);
  1526. phba->link_state = LPFC_HBA_ERROR;
  1527. /*
  1528. * All error attention handlers are posted to
  1529. * worker thread
  1530. */
  1531. phba->work_ha |= HA_ERATT;
  1532. phba->work_hs = HS_FFER3;
  1533. lpfc_worker_wake_up(phba);
  1534. return NULL;
  1535. }
  1536. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1537. return NULL;
  1538. }
  1539. return lpfc_cmd_iocb(phba, pring);
  1540. }
  1541. /**
  1542. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1543. * @phba: Pointer to HBA context object.
  1544. * @iocbq: Pointer to driver iocb object.
  1545. *
  1546. * This function gets an iotag for the iocb. If there is no unused iotag and
  1547. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1548. * array and assigns a new iotag.
  1549. * The function returns the allocated iotag if successful, else returns zero.
  1550. * Zero is not a valid iotag.
  1551. * The caller is not required to hold any lock.
  1552. **/
  1553. uint16_t
  1554. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1555. {
  1556. struct lpfc_iocbq **new_arr;
  1557. struct lpfc_iocbq **old_arr;
  1558. size_t new_len;
  1559. struct lpfc_sli *psli = &phba->sli;
  1560. uint16_t iotag;
  1561. spin_lock_irq(&phba->hbalock);
  1562. iotag = psli->last_iotag;
  1563. if(++iotag < psli->iocbq_lookup_len) {
  1564. psli->last_iotag = iotag;
  1565. psli->iocbq_lookup[iotag] = iocbq;
  1566. spin_unlock_irq(&phba->hbalock);
  1567. iocbq->iotag = iotag;
  1568. return iotag;
  1569. } else if (psli->iocbq_lookup_len < (0xffff
  1570. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1571. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1572. spin_unlock_irq(&phba->hbalock);
  1573. new_arr = kcalloc(new_len, sizeof(struct lpfc_iocbq *),
  1574. GFP_KERNEL);
  1575. if (new_arr) {
  1576. spin_lock_irq(&phba->hbalock);
  1577. old_arr = psli->iocbq_lookup;
  1578. if (new_len <= psli->iocbq_lookup_len) {
  1579. /* highly unprobable case */
  1580. kfree(new_arr);
  1581. iotag = psli->last_iotag;
  1582. if(++iotag < psli->iocbq_lookup_len) {
  1583. psli->last_iotag = iotag;
  1584. psli->iocbq_lookup[iotag] = iocbq;
  1585. spin_unlock_irq(&phba->hbalock);
  1586. iocbq->iotag = iotag;
  1587. return iotag;
  1588. }
  1589. spin_unlock_irq(&phba->hbalock);
  1590. return 0;
  1591. }
  1592. if (psli->iocbq_lookup)
  1593. memcpy(new_arr, old_arr,
  1594. ((psli->last_iotag + 1) *
  1595. sizeof (struct lpfc_iocbq *)));
  1596. psli->iocbq_lookup = new_arr;
  1597. psli->iocbq_lookup_len = new_len;
  1598. psli->last_iotag = iotag;
  1599. psli->iocbq_lookup[iotag] = iocbq;
  1600. spin_unlock_irq(&phba->hbalock);
  1601. iocbq->iotag = iotag;
  1602. kfree(old_arr);
  1603. return iotag;
  1604. }
  1605. } else
  1606. spin_unlock_irq(&phba->hbalock);
  1607. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1608. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1609. psli->last_iotag);
  1610. return 0;
  1611. }
  1612. /**
  1613. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1614. * @phba: Pointer to HBA context object.
  1615. * @pring: Pointer to driver SLI ring object.
  1616. * @iocb: Pointer to iocb slot in the ring.
  1617. * @nextiocb: Pointer to driver iocb object which need to be
  1618. * posted to firmware.
  1619. *
  1620. * This function is called with hbalock held to post a new iocb to
  1621. * the firmware. This function copies the new iocb to ring iocb slot and
  1622. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1623. * a completion call back for this iocb else the function will free the
  1624. * iocb object.
  1625. **/
  1626. static void
  1627. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1628. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1629. {
  1630. lockdep_assert_held(&phba->hbalock);
  1631. /*
  1632. * Set up an iotag
  1633. */
  1634. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1635. if (pring->ringno == LPFC_ELS_RING) {
  1636. lpfc_debugfs_slow_ring_trc(phba,
  1637. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1638. *(((uint32_t *) &nextiocb->iocb) + 4),
  1639. *(((uint32_t *) &nextiocb->iocb) + 6),
  1640. *(((uint32_t *) &nextiocb->iocb) + 7));
  1641. }
  1642. /*
  1643. * Issue iocb command to adapter
  1644. */
  1645. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1646. wmb();
  1647. pring->stats.iocb_cmd++;
  1648. /*
  1649. * If there is no completion routine to call, we can release the
  1650. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1651. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1652. */
  1653. if (nextiocb->iocb_cmpl)
  1654. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1655. else
  1656. __lpfc_sli_release_iocbq(phba, nextiocb);
  1657. /*
  1658. * Let the HBA know what IOCB slot will be the next one the
  1659. * driver will put a command into.
  1660. */
  1661. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1662. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1663. }
  1664. /**
  1665. * lpfc_sli_update_full_ring - Update the chip attention register
  1666. * @phba: Pointer to HBA context object.
  1667. * @pring: Pointer to driver SLI ring object.
  1668. *
  1669. * The caller is not required to hold any lock for calling this function.
  1670. * This function updates the chip attention bits for the ring to inform firmware
  1671. * that there are pending work to be done for this ring and requests an
  1672. * interrupt when there is space available in the ring. This function is
  1673. * called when the driver is unable to post more iocbs to the ring due
  1674. * to unavailability of space in the ring.
  1675. **/
  1676. static void
  1677. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1678. {
  1679. int ringno = pring->ringno;
  1680. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1681. wmb();
  1682. /*
  1683. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1684. * The HBA will tell us when an IOCB entry is available.
  1685. */
  1686. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1687. readl(phba->CAregaddr); /* flush */
  1688. pring->stats.iocb_cmd_full++;
  1689. }
  1690. /**
  1691. * lpfc_sli_update_ring - Update chip attention register
  1692. * @phba: Pointer to HBA context object.
  1693. * @pring: Pointer to driver SLI ring object.
  1694. *
  1695. * This function updates the chip attention register bit for the
  1696. * given ring to inform HBA that there is more work to be done
  1697. * in this ring. The caller is not required to hold any lock.
  1698. **/
  1699. static void
  1700. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1701. {
  1702. int ringno = pring->ringno;
  1703. /*
  1704. * Tell the HBA that there is work to do in this ring.
  1705. */
  1706. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1707. wmb();
  1708. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1709. readl(phba->CAregaddr); /* flush */
  1710. }
  1711. }
  1712. /**
  1713. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1714. * @phba: Pointer to HBA context object.
  1715. * @pring: Pointer to driver SLI ring object.
  1716. *
  1717. * This function is called with hbalock held to post pending iocbs
  1718. * in the txq to the firmware. This function is called when driver
  1719. * detects space available in the ring.
  1720. **/
  1721. static void
  1722. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1723. {
  1724. IOCB_t *iocb;
  1725. struct lpfc_iocbq *nextiocb;
  1726. lockdep_assert_held(&phba->hbalock);
  1727. /*
  1728. * Check to see if:
  1729. * (a) there is anything on the txq to send
  1730. * (b) link is up
  1731. * (c) link attention events can be processed (fcp ring only)
  1732. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1733. */
  1734. if (lpfc_is_link_up(phba) &&
  1735. (!list_empty(&pring->txq)) &&
  1736. (pring->ringno != LPFC_FCP_RING ||
  1737. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1738. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1739. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1740. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1741. if (iocb)
  1742. lpfc_sli_update_ring(phba, pring);
  1743. else
  1744. lpfc_sli_update_full_ring(phba, pring);
  1745. }
  1746. return;
  1747. }
  1748. /**
  1749. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1750. * @phba: Pointer to HBA context object.
  1751. * @hbqno: HBQ number.
  1752. *
  1753. * This function is called with hbalock held to get the next
  1754. * available slot for the given HBQ. If there is free slot
  1755. * available for the HBQ it will return pointer to the next available
  1756. * HBQ entry else it will return NULL.
  1757. **/
  1758. static struct lpfc_hbq_entry *
  1759. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1760. {
  1761. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1762. lockdep_assert_held(&phba->hbalock);
  1763. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1764. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1765. hbqp->next_hbqPutIdx = 0;
  1766. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1767. uint32_t raw_index = phba->hbq_get[hbqno];
  1768. uint32_t getidx = le32_to_cpu(raw_index);
  1769. hbqp->local_hbqGetIdx = getidx;
  1770. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1771. lpfc_printf_log(phba, KERN_ERR,
  1772. LOG_SLI | LOG_VPORT,
  1773. "1802 HBQ %d: local_hbqGetIdx "
  1774. "%u is > than hbqp->entry_count %u\n",
  1775. hbqno, hbqp->local_hbqGetIdx,
  1776. hbqp->entry_count);
  1777. phba->link_state = LPFC_HBA_ERROR;
  1778. return NULL;
  1779. }
  1780. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1781. return NULL;
  1782. }
  1783. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1784. hbqp->hbqPutIdx;
  1785. }
  1786. /**
  1787. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1788. * @phba: Pointer to HBA context object.
  1789. *
  1790. * This function is called with no lock held to free all the
  1791. * hbq buffers while uninitializing the SLI interface. It also
  1792. * frees the HBQ buffers returned by the firmware but not yet
  1793. * processed by the upper layers.
  1794. **/
  1795. void
  1796. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1797. {
  1798. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1799. struct hbq_dmabuf *hbq_buf;
  1800. unsigned long flags;
  1801. int i, hbq_count;
  1802. hbq_count = lpfc_sli_hbq_count();
  1803. /* Return all memory used by all HBQs */
  1804. spin_lock_irqsave(&phba->hbalock, flags);
  1805. for (i = 0; i < hbq_count; ++i) {
  1806. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1807. &phba->hbqs[i].hbq_buffer_list, list) {
  1808. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1809. list_del(&hbq_buf->dbuf.list);
  1810. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1811. }
  1812. phba->hbqs[i].buffer_count = 0;
  1813. }
  1814. /* Mark the HBQs not in use */
  1815. phba->hbq_in_use = 0;
  1816. spin_unlock_irqrestore(&phba->hbalock, flags);
  1817. }
  1818. /**
  1819. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1820. * @phba: Pointer to HBA context object.
  1821. * @hbqno: HBQ number.
  1822. * @hbq_buf: Pointer to HBQ buffer.
  1823. *
  1824. * This function is called with the hbalock held to post a
  1825. * hbq buffer to the firmware. If the function finds an empty
  1826. * slot in the HBQ, it will post the buffer. The function will return
  1827. * pointer to the hbq entry if it successfully post the buffer
  1828. * else it will return NULL.
  1829. **/
  1830. static int
  1831. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1832. struct hbq_dmabuf *hbq_buf)
  1833. {
  1834. lockdep_assert_held(&phba->hbalock);
  1835. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1836. }
  1837. /**
  1838. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1839. * @phba: Pointer to HBA context object.
  1840. * @hbqno: HBQ number.
  1841. * @hbq_buf: Pointer to HBQ buffer.
  1842. *
  1843. * This function is called with the hbalock held to post a hbq buffer to the
  1844. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1845. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1846. * it successfully post the buffer else it will return an error.
  1847. **/
  1848. static int
  1849. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1850. struct hbq_dmabuf *hbq_buf)
  1851. {
  1852. struct lpfc_hbq_entry *hbqe;
  1853. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1854. lockdep_assert_held(&phba->hbalock);
  1855. /* Get next HBQ entry slot to use */
  1856. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1857. if (hbqe) {
  1858. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1859. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1860. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1861. hbqe->bde.tus.f.bdeSize = hbq_buf->total_size;
  1862. hbqe->bde.tus.f.bdeFlags = 0;
  1863. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1864. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1865. /* Sync SLIM */
  1866. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1867. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1868. /* flush */
  1869. readl(phba->hbq_put + hbqno);
  1870. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1871. return 0;
  1872. } else
  1873. return -ENOMEM;
  1874. }
  1875. /**
  1876. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1877. * @phba: Pointer to HBA context object.
  1878. * @hbqno: HBQ number.
  1879. * @hbq_buf: Pointer to HBQ buffer.
  1880. *
  1881. * This function is called with the hbalock held to post an RQE to the SLI4
  1882. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1883. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1884. **/
  1885. static int
  1886. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1887. struct hbq_dmabuf *hbq_buf)
  1888. {
  1889. int rc;
  1890. struct lpfc_rqe hrqe;
  1891. struct lpfc_rqe drqe;
  1892. struct lpfc_queue *hrq;
  1893. struct lpfc_queue *drq;
  1894. if (hbqno != LPFC_ELS_HBQ)
  1895. return 1;
  1896. hrq = phba->sli4_hba.hdr_rq;
  1897. drq = phba->sli4_hba.dat_rq;
  1898. lockdep_assert_held(&phba->hbalock);
  1899. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1900. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1901. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1902. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1903. rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
  1904. if (rc < 0)
  1905. return rc;
  1906. hbq_buf->tag = (rc | (hbqno << 16));
  1907. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1908. return 0;
  1909. }
  1910. /* HBQ for ELS and CT traffic. */
  1911. static struct lpfc_hbq_init lpfc_els_hbq = {
  1912. .rn = 1,
  1913. .entry_count = 256,
  1914. .mask_count = 0,
  1915. .profile = 0,
  1916. .ring_mask = (1 << LPFC_ELS_RING),
  1917. .buffer_count = 0,
  1918. .init_count = 40,
  1919. .add_count = 40,
  1920. };
  1921. /* Array of HBQs */
  1922. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1923. &lpfc_els_hbq,
  1924. };
  1925. /**
  1926. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1927. * @phba: Pointer to HBA context object.
  1928. * @hbqno: HBQ number.
  1929. * @count: Number of HBQ buffers to be posted.
  1930. *
  1931. * This function is called with no lock held to post more hbq buffers to the
  1932. * given HBQ. The function returns the number of HBQ buffers successfully
  1933. * posted.
  1934. **/
  1935. static int
  1936. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1937. {
  1938. uint32_t i, posted = 0;
  1939. unsigned long flags;
  1940. struct hbq_dmabuf *hbq_buffer;
  1941. LIST_HEAD(hbq_buf_list);
  1942. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1943. return 0;
  1944. if ((phba->hbqs[hbqno].buffer_count + count) >
  1945. lpfc_hbq_defs[hbqno]->entry_count)
  1946. count = lpfc_hbq_defs[hbqno]->entry_count -
  1947. phba->hbqs[hbqno].buffer_count;
  1948. if (!count)
  1949. return 0;
  1950. /* Allocate HBQ entries */
  1951. for (i = 0; i < count; i++) {
  1952. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1953. if (!hbq_buffer)
  1954. break;
  1955. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1956. }
  1957. /* Check whether HBQ is still in use */
  1958. spin_lock_irqsave(&phba->hbalock, flags);
  1959. if (!phba->hbq_in_use)
  1960. goto err;
  1961. while (!list_empty(&hbq_buf_list)) {
  1962. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1963. dbuf.list);
  1964. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1965. (hbqno << 16));
  1966. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1967. phba->hbqs[hbqno].buffer_count++;
  1968. posted++;
  1969. } else
  1970. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1971. }
  1972. spin_unlock_irqrestore(&phba->hbalock, flags);
  1973. return posted;
  1974. err:
  1975. spin_unlock_irqrestore(&phba->hbalock, flags);
  1976. while (!list_empty(&hbq_buf_list)) {
  1977. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1978. dbuf.list);
  1979. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1980. }
  1981. return 0;
  1982. }
  1983. /**
  1984. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1985. * @phba: Pointer to HBA context object.
  1986. * @qno: HBQ number.
  1987. *
  1988. * This function posts more buffers to the HBQ. This function
  1989. * is called with no lock held. The function returns the number of HBQ entries
  1990. * successfully allocated.
  1991. **/
  1992. int
  1993. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1994. {
  1995. if (phba->sli_rev == LPFC_SLI_REV4)
  1996. return 0;
  1997. else
  1998. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1999. lpfc_hbq_defs[qno]->add_count);
  2000. }
  2001. /**
  2002. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  2003. * @phba: Pointer to HBA context object.
  2004. * @qno: HBQ queue number.
  2005. *
  2006. * This function is called from SLI initialization code path with
  2007. * no lock held to post initial HBQ buffers to firmware. The
  2008. * function returns the number of HBQ entries successfully allocated.
  2009. **/
  2010. static int
  2011. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  2012. {
  2013. if (phba->sli_rev == LPFC_SLI_REV4)
  2014. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  2015. lpfc_hbq_defs[qno]->entry_count);
  2016. else
  2017. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  2018. lpfc_hbq_defs[qno]->init_count);
  2019. }
  2020. /**
  2021. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  2022. * @phba: Pointer to HBA context object.
  2023. * @hbqno: HBQ number.
  2024. *
  2025. * This function removes the first hbq buffer on an hbq list and returns a
  2026. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  2027. **/
  2028. static struct hbq_dmabuf *
  2029. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  2030. {
  2031. struct lpfc_dmabuf *d_buf;
  2032. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  2033. if (!d_buf)
  2034. return NULL;
  2035. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  2036. }
  2037. /**
  2038. * lpfc_sli_rqbuf_get - Remove the first dma buffer off of an RQ list
  2039. * @phba: Pointer to HBA context object.
  2040. * @hbqno: HBQ number.
  2041. *
  2042. * This function removes the first RQ buffer on an RQ buffer list and returns a
  2043. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  2044. **/
  2045. static struct rqb_dmabuf *
  2046. lpfc_sli_rqbuf_get(struct lpfc_hba *phba, struct lpfc_queue *hrq)
  2047. {
  2048. struct lpfc_dmabuf *h_buf;
  2049. struct lpfc_rqb *rqbp;
  2050. rqbp = hrq->rqbp;
  2051. list_remove_head(&rqbp->rqb_buffer_list, h_buf,
  2052. struct lpfc_dmabuf, list);
  2053. if (!h_buf)
  2054. return NULL;
  2055. rqbp->buffer_count--;
  2056. return container_of(h_buf, struct rqb_dmabuf, hbuf);
  2057. }
  2058. /**
  2059. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  2060. * @phba: Pointer to HBA context object.
  2061. * @tag: Tag of the hbq buffer.
  2062. *
  2063. * This function searches for the hbq buffer associated with the given tag in
  2064. * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
  2065. * otherwise it returns NULL.
  2066. **/
  2067. static struct hbq_dmabuf *
  2068. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  2069. {
  2070. struct lpfc_dmabuf *d_buf;
  2071. struct hbq_dmabuf *hbq_buf;
  2072. uint32_t hbqno;
  2073. hbqno = tag >> 16;
  2074. if (hbqno >= LPFC_MAX_HBQS)
  2075. return NULL;
  2076. spin_lock_irq(&phba->hbalock);
  2077. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  2078. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  2079. if (hbq_buf->tag == tag) {
  2080. spin_unlock_irq(&phba->hbalock);
  2081. return hbq_buf;
  2082. }
  2083. }
  2084. spin_unlock_irq(&phba->hbalock);
  2085. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  2086. "1803 Bad hbq tag. Data: x%x x%x\n",
  2087. tag, phba->hbqs[tag >> 16].buffer_count);
  2088. return NULL;
  2089. }
  2090. /**
  2091. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  2092. * @phba: Pointer to HBA context object.
  2093. * @hbq_buffer: Pointer to HBQ buffer.
  2094. *
  2095. * This function is called with hbalock. This function gives back
  2096. * the hbq buffer to firmware. If the HBQ does not have space to
  2097. * post the buffer, it will free the buffer.
  2098. **/
  2099. void
  2100. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  2101. {
  2102. uint32_t hbqno;
  2103. if (hbq_buffer) {
  2104. hbqno = hbq_buffer->tag >> 16;
  2105. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  2106. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  2107. }
  2108. }
  2109. /**
  2110. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  2111. * @mbxCommand: mailbox command code.
  2112. *
  2113. * This function is called by the mailbox event handler function to verify
  2114. * that the completed mailbox command is a legitimate mailbox command. If the
  2115. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  2116. * and the mailbox event handler will take the HBA offline.
  2117. **/
  2118. static int
  2119. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  2120. {
  2121. uint8_t ret;
  2122. switch (mbxCommand) {
  2123. case MBX_LOAD_SM:
  2124. case MBX_READ_NV:
  2125. case MBX_WRITE_NV:
  2126. case MBX_WRITE_VPARMS:
  2127. case MBX_RUN_BIU_DIAG:
  2128. case MBX_INIT_LINK:
  2129. case MBX_DOWN_LINK:
  2130. case MBX_CONFIG_LINK:
  2131. case MBX_CONFIG_RING:
  2132. case MBX_RESET_RING:
  2133. case MBX_READ_CONFIG:
  2134. case MBX_READ_RCONFIG:
  2135. case MBX_READ_SPARM:
  2136. case MBX_READ_STATUS:
  2137. case MBX_READ_RPI:
  2138. case MBX_READ_XRI:
  2139. case MBX_READ_REV:
  2140. case MBX_READ_LNK_STAT:
  2141. case MBX_REG_LOGIN:
  2142. case MBX_UNREG_LOGIN:
  2143. case MBX_CLEAR_LA:
  2144. case MBX_DUMP_MEMORY:
  2145. case MBX_DUMP_CONTEXT:
  2146. case MBX_RUN_DIAGS:
  2147. case MBX_RESTART:
  2148. case MBX_UPDATE_CFG:
  2149. case MBX_DOWN_LOAD:
  2150. case MBX_DEL_LD_ENTRY:
  2151. case MBX_RUN_PROGRAM:
  2152. case MBX_SET_MASK:
  2153. case MBX_SET_VARIABLE:
  2154. case MBX_UNREG_D_ID:
  2155. case MBX_KILL_BOARD:
  2156. case MBX_CONFIG_FARP:
  2157. case MBX_BEACON:
  2158. case MBX_LOAD_AREA:
  2159. case MBX_RUN_BIU_DIAG64:
  2160. case MBX_CONFIG_PORT:
  2161. case MBX_READ_SPARM64:
  2162. case MBX_READ_RPI64:
  2163. case MBX_REG_LOGIN64:
  2164. case MBX_READ_TOPOLOGY:
  2165. case MBX_WRITE_WWN:
  2166. case MBX_SET_DEBUG:
  2167. case MBX_LOAD_EXP_ROM:
  2168. case MBX_ASYNCEVT_ENABLE:
  2169. case MBX_REG_VPI:
  2170. case MBX_UNREG_VPI:
  2171. case MBX_HEARTBEAT:
  2172. case MBX_PORT_CAPABILITIES:
  2173. case MBX_PORT_IOV_CONTROL:
  2174. case MBX_SLI4_CONFIG:
  2175. case MBX_SLI4_REQ_FTRS:
  2176. case MBX_REG_FCFI:
  2177. case MBX_UNREG_FCFI:
  2178. case MBX_REG_VFI:
  2179. case MBX_UNREG_VFI:
  2180. case MBX_INIT_VPI:
  2181. case MBX_INIT_VFI:
  2182. case MBX_RESUME_RPI:
  2183. case MBX_READ_EVENT_LOG_STATUS:
  2184. case MBX_READ_EVENT_LOG:
  2185. case MBX_SECURITY_MGMT:
  2186. case MBX_AUTH_PORT:
  2187. case MBX_ACCESS_VDATA:
  2188. ret = mbxCommand;
  2189. break;
  2190. default:
  2191. ret = MBX_SHUTDOWN;
  2192. break;
  2193. }
  2194. return ret;
  2195. }
  2196. /**
  2197. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  2198. * @phba: Pointer to HBA context object.
  2199. * @pmboxq: Pointer to mailbox command.
  2200. *
  2201. * This is completion handler function for mailbox commands issued from
  2202. * lpfc_sli_issue_mbox_wait function. This function is called by the
  2203. * mailbox event handler function with no lock held. This function
  2204. * will wake up thread waiting on the wait queue pointed by context1
  2205. * of the mailbox.
  2206. **/
  2207. void
  2208. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  2209. {
  2210. unsigned long drvr_flag;
  2211. struct completion *pmbox_done;
  2212. /*
  2213. * If pmbox_done is empty, the driver thread gave up waiting and
  2214. * continued running.
  2215. */
  2216. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  2217. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  2218. pmbox_done = (struct completion *)pmboxq->context3;
  2219. if (pmbox_done)
  2220. complete(pmbox_done);
  2221. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  2222. return;
  2223. }
  2224. /**
  2225. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  2226. * @phba: Pointer to HBA context object.
  2227. * @pmb: Pointer to mailbox object.
  2228. *
  2229. * This function is the default mailbox completion handler. It
  2230. * frees the memory resources associated with the completed mailbox
  2231. * command. If the completed command is a REG_LOGIN mailbox command,
  2232. * this function will issue a UREG_LOGIN to re-claim the RPI.
  2233. **/
  2234. void
  2235. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2236. {
  2237. struct lpfc_vport *vport = pmb->vport;
  2238. struct lpfc_dmabuf *mp;
  2239. struct lpfc_nodelist *ndlp;
  2240. struct Scsi_Host *shost;
  2241. uint16_t rpi, vpi;
  2242. int rc;
  2243. mp = (struct lpfc_dmabuf *) (pmb->context1);
  2244. if (mp) {
  2245. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  2246. kfree(mp);
  2247. }
  2248. /*
  2249. * If a REG_LOGIN succeeded after node is destroyed or node
  2250. * is in re-discovery driver need to cleanup the RPI.
  2251. */
  2252. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  2253. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  2254. !pmb->u.mb.mbxStatus) {
  2255. rpi = pmb->u.mb.un.varWords[0];
  2256. vpi = pmb->u.mb.un.varRegLogin.vpi;
  2257. if (phba->sli_rev == LPFC_SLI_REV4)
  2258. vpi -= phba->sli4_hba.max_cfg_param.vpi_base;
  2259. lpfc_unreg_login(phba, vpi, rpi, pmb);
  2260. pmb->vport = vport;
  2261. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2262. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2263. if (rc != MBX_NOT_FINISHED)
  2264. return;
  2265. }
  2266. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  2267. !(phba->pport->load_flag & FC_UNLOADING) &&
  2268. !pmb->u.mb.mbxStatus) {
  2269. shost = lpfc_shost_from_vport(vport);
  2270. spin_lock_irq(shost->host_lock);
  2271. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2272. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2273. spin_unlock_irq(shost->host_lock);
  2274. }
  2275. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  2276. ndlp = (struct lpfc_nodelist *)pmb->context2;
  2277. lpfc_nlp_put(ndlp);
  2278. pmb->context2 = NULL;
  2279. }
  2280. /* Check security permission status on INIT_LINK mailbox command */
  2281. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  2282. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  2283. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2284. "2860 SLI authentication is required "
  2285. "for INIT_LINK but has not done yet\n");
  2286. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  2287. lpfc_sli4_mbox_cmd_free(phba, pmb);
  2288. else
  2289. mempool_free(pmb, phba->mbox_mem_pool);
  2290. }
  2291. /**
  2292. * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
  2293. * @phba: Pointer to HBA context object.
  2294. * @pmb: Pointer to mailbox object.
  2295. *
  2296. * This function is the unreg rpi mailbox completion handler. It
  2297. * frees the memory resources associated with the completed mailbox
  2298. * command. An additional refrenece is put on the ndlp to prevent
  2299. * lpfc_nlp_release from freeing the rpi bit in the bitmask before
  2300. * the unreg mailbox command completes, this routine puts the
  2301. * reference back.
  2302. *
  2303. **/
  2304. void
  2305. lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  2306. {
  2307. struct lpfc_vport *vport = pmb->vport;
  2308. struct lpfc_nodelist *ndlp;
  2309. ndlp = pmb->context1;
  2310. if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
  2311. if (phba->sli_rev == LPFC_SLI_REV4 &&
  2312. (bf_get(lpfc_sli_intf_if_type,
  2313. &phba->sli4_hba.sli_intf) >=
  2314. LPFC_SLI_INTF_IF_TYPE_2)) {
  2315. if (ndlp) {
  2316. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  2317. "0010 UNREG_LOGIN vpi:%x "
  2318. "rpi:%x DID:%x map:%x %p\n",
  2319. vport->vpi, ndlp->nlp_rpi,
  2320. ndlp->nlp_DID,
  2321. ndlp->nlp_usg_map, ndlp);
  2322. ndlp->nlp_flag &= ~NLP_LOGO_ACC;
  2323. lpfc_nlp_put(ndlp);
  2324. }
  2325. }
  2326. }
  2327. mempool_free(pmb, phba->mbox_mem_pool);
  2328. }
  2329. /**
  2330. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2331. * @phba: Pointer to HBA context object.
  2332. *
  2333. * This function is called with no lock held. This function processes all
  2334. * the completed mailbox commands and gives it to upper layers. The interrupt
  2335. * service routine processes mailbox completion interrupt and adds completed
  2336. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2337. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2338. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2339. * function returns the mailbox commands to the upper layer by calling the
  2340. * completion handler function of each mailbox.
  2341. **/
  2342. int
  2343. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2344. {
  2345. MAILBOX_t *pmbox;
  2346. LPFC_MBOXQ_t *pmb;
  2347. int rc;
  2348. LIST_HEAD(cmplq);
  2349. phba->sli.slistat.mbox_event++;
  2350. /* Get all completed mailboxe buffers into the cmplq */
  2351. spin_lock_irq(&phba->hbalock);
  2352. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2353. spin_unlock_irq(&phba->hbalock);
  2354. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2355. do {
  2356. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2357. if (pmb == NULL)
  2358. break;
  2359. pmbox = &pmb->u.mb;
  2360. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2361. if (pmb->vport) {
  2362. lpfc_debugfs_disc_trc(pmb->vport,
  2363. LPFC_DISC_TRC_MBOX_VPORT,
  2364. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2365. (uint32_t)pmbox->mbxCommand,
  2366. pmbox->un.varWords[0],
  2367. pmbox->un.varWords[1]);
  2368. }
  2369. else {
  2370. lpfc_debugfs_disc_trc(phba->pport,
  2371. LPFC_DISC_TRC_MBOX,
  2372. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2373. (uint32_t)pmbox->mbxCommand,
  2374. pmbox->un.varWords[0],
  2375. pmbox->un.varWords[1]);
  2376. }
  2377. }
  2378. /*
  2379. * It is a fatal error if unknown mbox command completion.
  2380. */
  2381. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2382. MBX_SHUTDOWN) {
  2383. /* Unknown mailbox command compl */
  2384. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2385. "(%d):0323 Unknown Mailbox command "
  2386. "x%x (x%x/x%x) Cmpl\n",
  2387. pmb->vport ? pmb->vport->vpi : 0,
  2388. pmbox->mbxCommand,
  2389. lpfc_sli_config_mbox_subsys_get(phba,
  2390. pmb),
  2391. lpfc_sli_config_mbox_opcode_get(phba,
  2392. pmb));
  2393. phba->link_state = LPFC_HBA_ERROR;
  2394. phba->work_hs = HS_FFER3;
  2395. lpfc_handle_eratt(phba);
  2396. continue;
  2397. }
  2398. if (pmbox->mbxStatus) {
  2399. phba->sli.slistat.mbox_stat_err++;
  2400. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2401. /* Mbox cmd cmpl error - RETRYing */
  2402. lpfc_printf_log(phba, KERN_INFO,
  2403. LOG_MBOX | LOG_SLI,
  2404. "(%d):0305 Mbox cmd cmpl "
  2405. "error - RETRYing Data: x%x "
  2406. "(x%x/x%x) x%x x%x x%x\n",
  2407. pmb->vport ? pmb->vport->vpi : 0,
  2408. pmbox->mbxCommand,
  2409. lpfc_sli_config_mbox_subsys_get(phba,
  2410. pmb),
  2411. lpfc_sli_config_mbox_opcode_get(phba,
  2412. pmb),
  2413. pmbox->mbxStatus,
  2414. pmbox->un.varWords[0],
  2415. pmb->vport->port_state);
  2416. pmbox->mbxStatus = 0;
  2417. pmbox->mbxOwner = OWN_HOST;
  2418. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2419. if (rc != MBX_NOT_FINISHED)
  2420. continue;
  2421. }
  2422. }
  2423. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2424. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2425. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2426. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  2427. "x%x x%x x%x\n",
  2428. pmb->vport ? pmb->vport->vpi : 0,
  2429. pmbox->mbxCommand,
  2430. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2431. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2432. pmb->mbox_cmpl,
  2433. *((uint32_t *) pmbox),
  2434. pmbox->un.varWords[0],
  2435. pmbox->un.varWords[1],
  2436. pmbox->un.varWords[2],
  2437. pmbox->un.varWords[3],
  2438. pmbox->un.varWords[4],
  2439. pmbox->un.varWords[5],
  2440. pmbox->un.varWords[6],
  2441. pmbox->un.varWords[7],
  2442. pmbox->un.varWords[8],
  2443. pmbox->un.varWords[9],
  2444. pmbox->un.varWords[10]);
  2445. if (pmb->mbox_cmpl)
  2446. pmb->mbox_cmpl(phba,pmb);
  2447. } while (1);
  2448. return 0;
  2449. }
  2450. /**
  2451. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2452. * @phba: Pointer to HBA context object.
  2453. * @pring: Pointer to driver SLI ring object.
  2454. * @tag: buffer tag.
  2455. *
  2456. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2457. * is set in the tag the buffer is posted for a particular exchange,
  2458. * the function will return the buffer without replacing the buffer.
  2459. * If the buffer is for unsolicited ELS or CT traffic, this function
  2460. * returns the buffer and also posts another buffer to the firmware.
  2461. **/
  2462. static struct lpfc_dmabuf *
  2463. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2464. struct lpfc_sli_ring *pring,
  2465. uint32_t tag)
  2466. {
  2467. struct hbq_dmabuf *hbq_entry;
  2468. if (tag & QUE_BUFTAG_BIT)
  2469. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2470. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2471. if (!hbq_entry)
  2472. return NULL;
  2473. return &hbq_entry->dbuf;
  2474. }
  2475. /**
  2476. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2477. * @phba: Pointer to HBA context object.
  2478. * @pring: Pointer to driver SLI ring object.
  2479. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2480. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2481. * @fch_type: the type for the first frame of the sequence.
  2482. *
  2483. * This function is called with no lock held. This function uses the r_ctl and
  2484. * type of the received sequence to find the correct callback function to call
  2485. * to process the sequence.
  2486. **/
  2487. static int
  2488. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2489. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2490. uint32_t fch_type)
  2491. {
  2492. int i;
  2493. switch (fch_type) {
  2494. case FC_TYPE_NVME:
  2495. lpfc_nvmet_unsol_ls_event(phba, pring, saveq);
  2496. return 1;
  2497. default:
  2498. break;
  2499. }
  2500. /* unSolicited Responses */
  2501. if (pring->prt[0].profile) {
  2502. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2503. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2504. saveq);
  2505. return 1;
  2506. }
  2507. /* We must search, based on rctl / type
  2508. for the right routine */
  2509. for (i = 0; i < pring->num_mask; i++) {
  2510. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2511. (pring->prt[i].type == fch_type)) {
  2512. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2513. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2514. (phba, pring, saveq);
  2515. return 1;
  2516. }
  2517. }
  2518. return 0;
  2519. }
  2520. /**
  2521. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2522. * @phba: Pointer to HBA context object.
  2523. * @pring: Pointer to driver SLI ring object.
  2524. * @saveq: Pointer to the unsolicited iocb.
  2525. *
  2526. * This function is called with no lock held by the ring event handler
  2527. * when there is an unsolicited iocb posted to the response ring by the
  2528. * firmware. This function gets the buffer associated with the iocbs
  2529. * and calls the event handler for the ring. This function handles both
  2530. * qring buffers and hbq buffers.
  2531. * When the function returns 1 the caller can free the iocb object otherwise
  2532. * upper layer functions will free the iocb objects.
  2533. **/
  2534. static int
  2535. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2536. struct lpfc_iocbq *saveq)
  2537. {
  2538. IOCB_t * irsp;
  2539. WORD5 * w5p;
  2540. uint32_t Rctl, Type;
  2541. struct lpfc_iocbq *iocbq;
  2542. struct lpfc_dmabuf *dmzbuf;
  2543. irsp = &(saveq->iocb);
  2544. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2545. if (pring->lpfc_sli_rcv_async_status)
  2546. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2547. else
  2548. lpfc_printf_log(phba,
  2549. KERN_WARNING,
  2550. LOG_SLI,
  2551. "0316 Ring %d handler: unexpected "
  2552. "ASYNC_STATUS iocb received evt_code "
  2553. "0x%x\n",
  2554. pring->ringno,
  2555. irsp->un.asyncstat.evt_code);
  2556. return 1;
  2557. }
  2558. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2559. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2560. if (irsp->ulpBdeCount > 0) {
  2561. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2562. irsp->un.ulpWord[3]);
  2563. lpfc_in_buf_free(phba, dmzbuf);
  2564. }
  2565. if (irsp->ulpBdeCount > 1) {
  2566. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2567. irsp->unsli3.sli3Words[3]);
  2568. lpfc_in_buf_free(phba, dmzbuf);
  2569. }
  2570. if (irsp->ulpBdeCount > 2) {
  2571. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2572. irsp->unsli3.sli3Words[7]);
  2573. lpfc_in_buf_free(phba, dmzbuf);
  2574. }
  2575. return 1;
  2576. }
  2577. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2578. if (irsp->ulpBdeCount != 0) {
  2579. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2580. irsp->un.ulpWord[3]);
  2581. if (!saveq->context2)
  2582. lpfc_printf_log(phba,
  2583. KERN_ERR,
  2584. LOG_SLI,
  2585. "0341 Ring %d Cannot find buffer for "
  2586. "an unsolicited iocb. tag 0x%x\n",
  2587. pring->ringno,
  2588. irsp->un.ulpWord[3]);
  2589. }
  2590. if (irsp->ulpBdeCount == 2) {
  2591. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2592. irsp->unsli3.sli3Words[7]);
  2593. if (!saveq->context3)
  2594. lpfc_printf_log(phba,
  2595. KERN_ERR,
  2596. LOG_SLI,
  2597. "0342 Ring %d Cannot find buffer for an"
  2598. " unsolicited iocb. tag 0x%x\n",
  2599. pring->ringno,
  2600. irsp->unsli3.sli3Words[7]);
  2601. }
  2602. list_for_each_entry(iocbq, &saveq->list, list) {
  2603. irsp = &(iocbq->iocb);
  2604. if (irsp->ulpBdeCount != 0) {
  2605. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2606. irsp->un.ulpWord[3]);
  2607. if (!iocbq->context2)
  2608. lpfc_printf_log(phba,
  2609. KERN_ERR,
  2610. LOG_SLI,
  2611. "0343 Ring %d Cannot find "
  2612. "buffer for an unsolicited iocb"
  2613. ". tag 0x%x\n", pring->ringno,
  2614. irsp->un.ulpWord[3]);
  2615. }
  2616. if (irsp->ulpBdeCount == 2) {
  2617. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2618. irsp->unsli3.sli3Words[7]);
  2619. if (!iocbq->context3)
  2620. lpfc_printf_log(phba,
  2621. KERN_ERR,
  2622. LOG_SLI,
  2623. "0344 Ring %d Cannot find "
  2624. "buffer for an unsolicited "
  2625. "iocb. tag 0x%x\n",
  2626. pring->ringno,
  2627. irsp->unsli3.sli3Words[7]);
  2628. }
  2629. }
  2630. }
  2631. if (irsp->ulpBdeCount != 0 &&
  2632. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2633. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2634. int found = 0;
  2635. /* search continue save q for same XRI */
  2636. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2637. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2638. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2639. list_add_tail(&saveq->list, &iocbq->list);
  2640. found = 1;
  2641. break;
  2642. }
  2643. }
  2644. if (!found)
  2645. list_add_tail(&saveq->clist,
  2646. &pring->iocb_continue_saveq);
  2647. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2648. list_del_init(&iocbq->clist);
  2649. saveq = iocbq;
  2650. irsp = &(saveq->iocb);
  2651. } else
  2652. return 0;
  2653. }
  2654. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2655. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2656. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2657. Rctl = FC_RCTL_ELS_REQ;
  2658. Type = FC_TYPE_ELS;
  2659. } else {
  2660. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2661. Rctl = w5p->hcsw.Rctl;
  2662. Type = w5p->hcsw.Type;
  2663. /* Firmware Workaround */
  2664. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2665. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2666. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2667. Rctl = FC_RCTL_ELS_REQ;
  2668. Type = FC_TYPE_ELS;
  2669. w5p->hcsw.Rctl = Rctl;
  2670. w5p->hcsw.Type = Type;
  2671. }
  2672. }
  2673. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2674. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2675. "0313 Ring %d handler: unexpected Rctl x%x "
  2676. "Type x%x received\n",
  2677. pring->ringno, Rctl, Type);
  2678. return 1;
  2679. }
  2680. /**
  2681. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2682. * @phba: Pointer to HBA context object.
  2683. * @pring: Pointer to driver SLI ring object.
  2684. * @prspiocb: Pointer to response iocb object.
  2685. *
  2686. * This function looks up the iocb_lookup table to get the command iocb
  2687. * corresponding to the given response iocb using the iotag of the
  2688. * response iocb. This function is called with the hbalock held
  2689. * for sli3 devices or the ring_lock for sli4 devices.
  2690. * This function returns the command iocb object if it finds the command
  2691. * iocb else returns NULL.
  2692. **/
  2693. static struct lpfc_iocbq *
  2694. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2695. struct lpfc_sli_ring *pring,
  2696. struct lpfc_iocbq *prspiocb)
  2697. {
  2698. struct lpfc_iocbq *cmd_iocb = NULL;
  2699. uint16_t iotag;
  2700. lockdep_assert_held(&phba->hbalock);
  2701. iotag = prspiocb->iocb.ulpIoTag;
  2702. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2703. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2704. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2705. /* remove from txcmpl queue list */
  2706. list_del_init(&cmd_iocb->list);
  2707. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2708. return cmd_iocb;
  2709. }
  2710. }
  2711. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2712. "0317 iotag x%x is out of "
  2713. "range: max iotag x%x wd0 x%x\n",
  2714. iotag, phba->sli.last_iotag,
  2715. *(((uint32_t *) &prspiocb->iocb) + 7));
  2716. return NULL;
  2717. }
  2718. /**
  2719. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2720. * @phba: Pointer to HBA context object.
  2721. * @pring: Pointer to driver SLI ring object.
  2722. * @iotag: IOCB tag.
  2723. *
  2724. * This function looks up the iocb_lookup table to get the command iocb
  2725. * corresponding to the given iotag. This function is called with the
  2726. * hbalock held.
  2727. * This function returns the command iocb object if it finds the command
  2728. * iocb else returns NULL.
  2729. **/
  2730. static struct lpfc_iocbq *
  2731. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2732. struct lpfc_sli_ring *pring, uint16_t iotag)
  2733. {
  2734. struct lpfc_iocbq *cmd_iocb = NULL;
  2735. lockdep_assert_held(&phba->hbalock);
  2736. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2737. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2738. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2739. /* remove from txcmpl queue list */
  2740. list_del_init(&cmd_iocb->list);
  2741. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2742. return cmd_iocb;
  2743. }
  2744. }
  2745. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2746. "0372 iotag x%x lookup error: max iotag (x%x) "
  2747. "iocb_flag x%x\n",
  2748. iotag, phba->sli.last_iotag,
  2749. cmd_iocb ? cmd_iocb->iocb_flag : 0xffff);
  2750. return NULL;
  2751. }
  2752. /**
  2753. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2754. * @phba: Pointer to HBA context object.
  2755. * @pring: Pointer to driver SLI ring object.
  2756. * @saveq: Pointer to the response iocb to be processed.
  2757. *
  2758. * This function is called by the ring event handler for non-fcp
  2759. * rings when there is a new response iocb in the response ring.
  2760. * The caller is not required to hold any locks. This function
  2761. * gets the command iocb associated with the response iocb and
  2762. * calls the completion handler for the command iocb. If there
  2763. * is no completion handler, the function will free the resources
  2764. * associated with command iocb. If the response iocb is for
  2765. * an already aborted command iocb, the status of the completion
  2766. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2767. * This function always returns 1.
  2768. **/
  2769. static int
  2770. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2771. struct lpfc_iocbq *saveq)
  2772. {
  2773. struct lpfc_iocbq *cmdiocbp;
  2774. int rc = 1;
  2775. unsigned long iflag;
  2776. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2777. if (phba->sli_rev == LPFC_SLI_REV4)
  2778. spin_lock_irqsave(&pring->ring_lock, iflag);
  2779. else
  2780. spin_lock_irqsave(&phba->hbalock, iflag);
  2781. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2782. if (phba->sli_rev == LPFC_SLI_REV4)
  2783. spin_unlock_irqrestore(&pring->ring_lock, iflag);
  2784. else
  2785. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2786. if (cmdiocbp) {
  2787. if (cmdiocbp->iocb_cmpl) {
  2788. /*
  2789. * If an ELS command failed send an event to mgmt
  2790. * application.
  2791. */
  2792. if (saveq->iocb.ulpStatus &&
  2793. (pring->ringno == LPFC_ELS_RING) &&
  2794. (cmdiocbp->iocb.ulpCommand ==
  2795. CMD_ELS_REQUEST64_CR))
  2796. lpfc_send_els_failure_event(phba,
  2797. cmdiocbp, saveq);
  2798. /*
  2799. * Post all ELS completions to the worker thread.
  2800. * All other are passed to the completion callback.
  2801. */
  2802. if (pring->ringno == LPFC_ELS_RING) {
  2803. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2804. (cmdiocbp->iocb_flag &
  2805. LPFC_DRIVER_ABORTED)) {
  2806. spin_lock_irqsave(&phba->hbalock,
  2807. iflag);
  2808. cmdiocbp->iocb_flag &=
  2809. ~LPFC_DRIVER_ABORTED;
  2810. spin_unlock_irqrestore(&phba->hbalock,
  2811. iflag);
  2812. saveq->iocb.ulpStatus =
  2813. IOSTAT_LOCAL_REJECT;
  2814. saveq->iocb.un.ulpWord[4] =
  2815. IOERR_SLI_ABORTED;
  2816. /* Firmware could still be in progress
  2817. * of DMAing payload, so don't free data
  2818. * buffer till after a hbeat.
  2819. */
  2820. spin_lock_irqsave(&phba->hbalock,
  2821. iflag);
  2822. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2823. spin_unlock_irqrestore(&phba->hbalock,
  2824. iflag);
  2825. }
  2826. if (phba->sli_rev == LPFC_SLI_REV4) {
  2827. if (saveq->iocb_flag &
  2828. LPFC_EXCHANGE_BUSY) {
  2829. /* Set cmdiocb flag for the
  2830. * exchange busy so sgl (xri)
  2831. * will not be released until
  2832. * the abort xri is received
  2833. * from hba.
  2834. */
  2835. spin_lock_irqsave(
  2836. &phba->hbalock, iflag);
  2837. cmdiocbp->iocb_flag |=
  2838. LPFC_EXCHANGE_BUSY;
  2839. spin_unlock_irqrestore(
  2840. &phba->hbalock, iflag);
  2841. }
  2842. if (cmdiocbp->iocb_flag &
  2843. LPFC_DRIVER_ABORTED) {
  2844. /*
  2845. * Clear LPFC_DRIVER_ABORTED
  2846. * bit in case it was driver
  2847. * initiated abort.
  2848. */
  2849. spin_lock_irqsave(
  2850. &phba->hbalock, iflag);
  2851. cmdiocbp->iocb_flag &=
  2852. ~LPFC_DRIVER_ABORTED;
  2853. spin_unlock_irqrestore(
  2854. &phba->hbalock, iflag);
  2855. cmdiocbp->iocb.ulpStatus =
  2856. IOSTAT_LOCAL_REJECT;
  2857. cmdiocbp->iocb.un.ulpWord[4] =
  2858. IOERR_ABORT_REQUESTED;
  2859. /*
  2860. * For SLI4, irsiocb contains
  2861. * NO_XRI in sli_xritag, it
  2862. * shall not affect releasing
  2863. * sgl (xri) process.
  2864. */
  2865. saveq->iocb.ulpStatus =
  2866. IOSTAT_LOCAL_REJECT;
  2867. saveq->iocb.un.ulpWord[4] =
  2868. IOERR_SLI_ABORTED;
  2869. spin_lock_irqsave(
  2870. &phba->hbalock, iflag);
  2871. saveq->iocb_flag |=
  2872. LPFC_DELAY_MEM_FREE;
  2873. spin_unlock_irqrestore(
  2874. &phba->hbalock, iflag);
  2875. }
  2876. }
  2877. }
  2878. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2879. } else
  2880. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2881. } else {
  2882. /*
  2883. * Unknown initiating command based on the response iotag.
  2884. * This could be the case on the ELS ring because of
  2885. * lpfc_els_abort().
  2886. */
  2887. if (pring->ringno != LPFC_ELS_RING) {
  2888. /*
  2889. * Ring <ringno> handler: unexpected completion IoTag
  2890. * <IoTag>
  2891. */
  2892. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2893. "0322 Ring %d handler: "
  2894. "unexpected completion IoTag x%x "
  2895. "Data: x%x x%x x%x x%x\n",
  2896. pring->ringno,
  2897. saveq->iocb.ulpIoTag,
  2898. saveq->iocb.ulpStatus,
  2899. saveq->iocb.un.ulpWord[4],
  2900. saveq->iocb.ulpCommand,
  2901. saveq->iocb.ulpContext);
  2902. }
  2903. }
  2904. return rc;
  2905. }
  2906. /**
  2907. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2908. * @phba: Pointer to HBA context object.
  2909. * @pring: Pointer to driver SLI ring object.
  2910. *
  2911. * This function is called from the iocb ring event handlers when
  2912. * put pointer is ahead of the get pointer for a ring. This function signal
  2913. * an error attention condition to the worker thread and the worker
  2914. * thread will transition the HBA to offline state.
  2915. **/
  2916. static void
  2917. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2918. {
  2919. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2920. /*
  2921. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2922. * rsp ring <portRspMax>
  2923. */
  2924. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2925. "0312 Ring %d handler: portRspPut %d "
  2926. "is bigger than rsp ring %d\n",
  2927. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2928. pring->sli.sli3.numRiocb);
  2929. phba->link_state = LPFC_HBA_ERROR;
  2930. /*
  2931. * All error attention handlers are posted to
  2932. * worker thread
  2933. */
  2934. phba->work_ha |= HA_ERATT;
  2935. phba->work_hs = HS_FFER3;
  2936. lpfc_worker_wake_up(phba);
  2937. return;
  2938. }
  2939. /**
  2940. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2941. * @ptr: Pointer to address of HBA context object.
  2942. *
  2943. * This function is invoked by the Error Attention polling timer when the
  2944. * timer times out. It will check the SLI Error Attention register for
  2945. * possible attention events. If so, it will post an Error Attention event
  2946. * and wake up worker thread to process it. Otherwise, it will set up the
  2947. * Error Attention polling timer for the next poll.
  2948. **/
  2949. void lpfc_poll_eratt(struct timer_list *t)
  2950. {
  2951. struct lpfc_hba *phba;
  2952. uint32_t eratt = 0;
  2953. uint64_t sli_intr, cnt;
  2954. phba = from_timer(phba, t, eratt_poll);
  2955. /* Here we will also keep track of interrupts per sec of the hba */
  2956. sli_intr = phba->sli.slistat.sli_intr;
  2957. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2958. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2959. sli_intr);
  2960. else
  2961. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2962. /* 64-bit integer division not supported on 32-bit x86 - use do_div */
  2963. do_div(cnt, phba->eratt_poll_interval);
  2964. phba->sli.slistat.sli_ips = cnt;
  2965. phba->sli.slistat.sli_prev_intr = sli_intr;
  2966. /* Check chip HA register for error event */
  2967. eratt = lpfc_sli_check_eratt(phba);
  2968. if (eratt)
  2969. /* Tell the worker thread there is work to do */
  2970. lpfc_worker_wake_up(phba);
  2971. else
  2972. /* Restart the timer for next eratt poll */
  2973. mod_timer(&phba->eratt_poll,
  2974. jiffies +
  2975. msecs_to_jiffies(1000 * phba->eratt_poll_interval));
  2976. return;
  2977. }
  2978. /**
  2979. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2980. * @phba: Pointer to HBA context object.
  2981. * @pring: Pointer to driver SLI ring object.
  2982. * @mask: Host attention register mask for this ring.
  2983. *
  2984. * This function is called from the interrupt context when there is a ring
  2985. * event for the fcp ring. The caller does not hold any lock.
  2986. * The function processes each response iocb in the response ring until it
  2987. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2988. * LE bit set. The function will call the completion handler of the command iocb
  2989. * if the response iocb indicates a completion for a command iocb or it is
  2990. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2991. * function if this is an unsolicited iocb.
  2992. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2993. * to check it explicitly.
  2994. */
  2995. int
  2996. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2997. struct lpfc_sli_ring *pring, uint32_t mask)
  2998. {
  2999. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  3000. IOCB_t *irsp = NULL;
  3001. IOCB_t *entry = NULL;
  3002. struct lpfc_iocbq *cmdiocbq = NULL;
  3003. struct lpfc_iocbq rspiocbq;
  3004. uint32_t status;
  3005. uint32_t portRspPut, portRspMax;
  3006. int rc = 1;
  3007. lpfc_iocb_type type;
  3008. unsigned long iflag;
  3009. uint32_t rsp_cmpl = 0;
  3010. spin_lock_irqsave(&phba->hbalock, iflag);
  3011. pring->stats.iocb_event++;
  3012. /*
  3013. * The next available response entry should never exceed the maximum
  3014. * entries. If it does, treat it as an adapter hardware error.
  3015. */
  3016. portRspMax = pring->sli.sli3.numRiocb;
  3017. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3018. if (unlikely(portRspPut >= portRspMax)) {
  3019. lpfc_sli_rsp_pointers_error(phba, pring);
  3020. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3021. return 1;
  3022. }
  3023. if (phba->fcp_ring_in_use) {
  3024. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3025. return 1;
  3026. } else
  3027. phba->fcp_ring_in_use = 1;
  3028. rmb();
  3029. while (pring->sli.sli3.rspidx != portRspPut) {
  3030. /*
  3031. * Fetch an entry off the ring and copy it into a local data
  3032. * structure. The copy involves a byte-swap since the
  3033. * network byte order and pci byte orders are different.
  3034. */
  3035. entry = lpfc_resp_iocb(phba, pring);
  3036. phba->last_completion_time = jiffies;
  3037. if (++pring->sli.sli3.rspidx >= portRspMax)
  3038. pring->sli.sli3.rspidx = 0;
  3039. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  3040. (uint32_t *) &rspiocbq.iocb,
  3041. phba->iocb_rsp_size);
  3042. INIT_LIST_HEAD(&(rspiocbq.list));
  3043. irsp = &rspiocbq.iocb;
  3044. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  3045. pring->stats.iocb_rsp++;
  3046. rsp_cmpl++;
  3047. if (unlikely(irsp->ulpStatus)) {
  3048. /*
  3049. * If resource errors reported from HBA, reduce
  3050. * queuedepths of the SCSI device.
  3051. */
  3052. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  3053. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  3054. IOERR_NO_RESOURCES)) {
  3055. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3056. phba->lpfc_rampdown_queue_depth(phba);
  3057. spin_lock_irqsave(&phba->hbalock, iflag);
  3058. }
  3059. /* Rsp ring <ringno> error: IOCB */
  3060. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  3061. "0336 Rsp Ring %d error: IOCB Data: "
  3062. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  3063. pring->ringno,
  3064. irsp->un.ulpWord[0],
  3065. irsp->un.ulpWord[1],
  3066. irsp->un.ulpWord[2],
  3067. irsp->un.ulpWord[3],
  3068. irsp->un.ulpWord[4],
  3069. irsp->un.ulpWord[5],
  3070. *(uint32_t *)&irsp->un1,
  3071. *((uint32_t *)&irsp->un1 + 1));
  3072. }
  3073. switch (type) {
  3074. case LPFC_ABORT_IOCB:
  3075. case LPFC_SOL_IOCB:
  3076. /*
  3077. * Idle exchange closed via ABTS from port. No iocb
  3078. * resources need to be recovered.
  3079. */
  3080. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  3081. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3082. "0333 IOCB cmd 0x%x"
  3083. " processed. Skipping"
  3084. " completion\n",
  3085. irsp->ulpCommand);
  3086. break;
  3087. }
  3088. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  3089. &rspiocbq);
  3090. if (unlikely(!cmdiocbq))
  3091. break;
  3092. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  3093. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  3094. if (cmdiocbq->iocb_cmpl) {
  3095. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3096. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  3097. &rspiocbq);
  3098. spin_lock_irqsave(&phba->hbalock, iflag);
  3099. }
  3100. break;
  3101. case LPFC_UNSOL_IOCB:
  3102. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3103. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  3104. spin_lock_irqsave(&phba->hbalock, iflag);
  3105. break;
  3106. default:
  3107. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  3108. char adaptermsg[LPFC_MAX_ADPTMSG];
  3109. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  3110. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  3111. MAX_MSG_DATA);
  3112. dev_warn(&((phba->pcidev)->dev),
  3113. "lpfc%d: %s\n",
  3114. phba->brd_no, adaptermsg);
  3115. } else {
  3116. /* Unknown IOCB command */
  3117. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3118. "0334 Unknown IOCB command "
  3119. "Data: x%x, x%x x%x x%x x%x\n",
  3120. type, irsp->ulpCommand,
  3121. irsp->ulpStatus,
  3122. irsp->ulpIoTag,
  3123. irsp->ulpContext);
  3124. }
  3125. break;
  3126. }
  3127. /*
  3128. * The response IOCB has been processed. Update the ring
  3129. * pointer in SLIM. If the port response put pointer has not
  3130. * been updated, sync the pgp->rspPutInx and fetch the new port
  3131. * response put pointer.
  3132. */
  3133. writel(pring->sli.sli3.rspidx,
  3134. &phba->host_gp[pring->ringno].rspGetInx);
  3135. if (pring->sli.sli3.rspidx == portRspPut)
  3136. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3137. }
  3138. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  3139. pring->stats.iocb_rsp_full++;
  3140. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3141. writel(status, phba->CAregaddr);
  3142. readl(phba->CAregaddr);
  3143. }
  3144. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3145. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3146. pring->stats.iocb_cmd_empty++;
  3147. /* Force update of the local copy of cmdGetInx */
  3148. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3149. lpfc_sli_resume_iocb(phba, pring);
  3150. if ((pring->lpfc_sli_cmd_available))
  3151. (pring->lpfc_sli_cmd_available) (phba, pring);
  3152. }
  3153. phba->fcp_ring_in_use = 0;
  3154. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3155. return rc;
  3156. }
  3157. /**
  3158. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  3159. * @phba: Pointer to HBA context object.
  3160. * @pring: Pointer to driver SLI ring object.
  3161. * @rspiocbp: Pointer to driver response IOCB object.
  3162. *
  3163. * This function is called from the worker thread when there is a slow-path
  3164. * response IOCB to process. This function chains all the response iocbs until
  3165. * seeing the iocb with the LE bit set. The function will call
  3166. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  3167. * completion of a command iocb. The function will call the
  3168. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  3169. * The function frees the resources or calls the completion handler if this
  3170. * iocb is an abort completion. The function returns NULL when the response
  3171. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  3172. * this function shall chain the iocb on to the iocb_continueq and return the
  3173. * response iocb passed in.
  3174. **/
  3175. static struct lpfc_iocbq *
  3176. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  3177. struct lpfc_iocbq *rspiocbp)
  3178. {
  3179. struct lpfc_iocbq *saveq;
  3180. struct lpfc_iocbq *cmdiocbp;
  3181. struct lpfc_iocbq *next_iocb;
  3182. IOCB_t *irsp = NULL;
  3183. uint32_t free_saveq;
  3184. uint8_t iocb_cmd_type;
  3185. lpfc_iocb_type type;
  3186. unsigned long iflag;
  3187. int rc;
  3188. spin_lock_irqsave(&phba->hbalock, iflag);
  3189. /* First add the response iocb to the countinueq list */
  3190. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  3191. pring->iocb_continueq_cnt++;
  3192. /* Now, determine whether the list is completed for processing */
  3193. irsp = &rspiocbp->iocb;
  3194. if (irsp->ulpLe) {
  3195. /*
  3196. * By default, the driver expects to free all resources
  3197. * associated with this iocb completion.
  3198. */
  3199. free_saveq = 1;
  3200. saveq = list_get_first(&pring->iocb_continueq,
  3201. struct lpfc_iocbq, list);
  3202. irsp = &(saveq->iocb);
  3203. list_del_init(&pring->iocb_continueq);
  3204. pring->iocb_continueq_cnt = 0;
  3205. pring->stats.iocb_rsp++;
  3206. /*
  3207. * If resource errors reported from HBA, reduce
  3208. * queuedepths of the SCSI device.
  3209. */
  3210. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  3211. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  3212. IOERR_NO_RESOURCES)) {
  3213. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3214. phba->lpfc_rampdown_queue_depth(phba);
  3215. spin_lock_irqsave(&phba->hbalock, iflag);
  3216. }
  3217. if (irsp->ulpStatus) {
  3218. /* Rsp ring <ringno> error: IOCB */
  3219. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  3220. "0328 Rsp Ring %d error: "
  3221. "IOCB Data: "
  3222. "x%x x%x x%x x%x "
  3223. "x%x x%x x%x x%x "
  3224. "x%x x%x x%x x%x "
  3225. "x%x x%x x%x x%x\n",
  3226. pring->ringno,
  3227. irsp->un.ulpWord[0],
  3228. irsp->un.ulpWord[1],
  3229. irsp->un.ulpWord[2],
  3230. irsp->un.ulpWord[3],
  3231. irsp->un.ulpWord[4],
  3232. irsp->un.ulpWord[5],
  3233. *(((uint32_t *) irsp) + 6),
  3234. *(((uint32_t *) irsp) + 7),
  3235. *(((uint32_t *) irsp) + 8),
  3236. *(((uint32_t *) irsp) + 9),
  3237. *(((uint32_t *) irsp) + 10),
  3238. *(((uint32_t *) irsp) + 11),
  3239. *(((uint32_t *) irsp) + 12),
  3240. *(((uint32_t *) irsp) + 13),
  3241. *(((uint32_t *) irsp) + 14),
  3242. *(((uint32_t *) irsp) + 15));
  3243. }
  3244. /*
  3245. * Fetch the IOCB command type and call the correct completion
  3246. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  3247. * get freed back to the lpfc_iocb_list by the discovery
  3248. * kernel thread.
  3249. */
  3250. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  3251. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  3252. switch (type) {
  3253. case LPFC_SOL_IOCB:
  3254. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3255. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  3256. spin_lock_irqsave(&phba->hbalock, iflag);
  3257. break;
  3258. case LPFC_UNSOL_IOCB:
  3259. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3260. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  3261. spin_lock_irqsave(&phba->hbalock, iflag);
  3262. if (!rc)
  3263. free_saveq = 0;
  3264. break;
  3265. case LPFC_ABORT_IOCB:
  3266. cmdiocbp = NULL;
  3267. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  3268. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  3269. saveq);
  3270. if (cmdiocbp) {
  3271. /* Call the specified completion routine */
  3272. if (cmdiocbp->iocb_cmpl) {
  3273. spin_unlock_irqrestore(&phba->hbalock,
  3274. iflag);
  3275. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  3276. saveq);
  3277. spin_lock_irqsave(&phba->hbalock,
  3278. iflag);
  3279. } else
  3280. __lpfc_sli_release_iocbq(phba,
  3281. cmdiocbp);
  3282. }
  3283. break;
  3284. case LPFC_UNKNOWN_IOCB:
  3285. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  3286. char adaptermsg[LPFC_MAX_ADPTMSG];
  3287. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  3288. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  3289. MAX_MSG_DATA);
  3290. dev_warn(&((phba->pcidev)->dev),
  3291. "lpfc%d: %s\n",
  3292. phba->brd_no, adaptermsg);
  3293. } else {
  3294. /* Unknown IOCB command */
  3295. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3296. "0335 Unknown IOCB "
  3297. "command Data: x%x "
  3298. "x%x x%x x%x\n",
  3299. irsp->ulpCommand,
  3300. irsp->ulpStatus,
  3301. irsp->ulpIoTag,
  3302. irsp->ulpContext);
  3303. }
  3304. break;
  3305. }
  3306. if (free_saveq) {
  3307. list_for_each_entry_safe(rspiocbp, next_iocb,
  3308. &saveq->list, list) {
  3309. list_del_init(&rspiocbp->list);
  3310. __lpfc_sli_release_iocbq(phba, rspiocbp);
  3311. }
  3312. __lpfc_sli_release_iocbq(phba, saveq);
  3313. }
  3314. rspiocbp = NULL;
  3315. }
  3316. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3317. return rspiocbp;
  3318. }
  3319. /**
  3320. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  3321. * @phba: Pointer to HBA context object.
  3322. * @pring: Pointer to driver SLI ring object.
  3323. * @mask: Host attention register mask for this ring.
  3324. *
  3325. * This routine wraps the actual slow_ring event process routine from the
  3326. * API jump table function pointer from the lpfc_hba struct.
  3327. **/
  3328. void
  3329. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  3330. struct lpfc_sli_ring *pring, uint32_t mask)
  3331. {
  3332. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  3333. }
  3334. /**
  3335. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  3336. * @phba: Pointer to HBA context object.
  3337. * @pring: Pointer to driver SLI ring object.
  3338. * @mask: Host attention register mask for this ring.
  3339. *
  3340. * This function is called from the worker thread when there is a ring event
  3341. * for non-fcp rings. The caller does not hold any lock. The function will
  3342. * remove each response iocb in the response ring and calls the handle
  3343. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3344. **/
  3345. static void
  3346. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  3347. struct lpfc_sli_ring *pring, uint32_t mask)
  3348. {
  3349. struct lpfc_pgp *pgp;
  3350. IOCB_t *entry;
  3351. IOCB_t *irsp = NULL;
  3352. struct lpfc_iocbq *rspiocbp = NULL;
  3353. uint32_t portRspPut, portRspMax;
  3354. unsigned long iflag;
  3355. uint32_t status;
  3356. pgp = &phba->port_gp[pring->ringno];
  3357. spin_lock_irqsave(&phba->hbalock, iflag);
  3358. pring->stats.iocb_event++;
  3359. /*
  3360. * The next available response entry should never exceed the maximum
  3361. * entries. If it does, treat it as an adapter hardware error.
  3362. */
  3363. portRspMax = pring->sli.sli3.numRiocb;
  3364. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3365. if (portRspPut >= portRspMax) {
  3366. /*
  3367. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3368. * rsp ring <portRspMax>
  3369. */
  3370. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3371. "0303 Ring %d handler: portRspPut %d "
  3372. "is bigger than rsp ring %d\n",
  3373. pring->ringno, portRspPut, portRspMax);
  3374. phba->link_state = LPFC_HBA_ERROR;
  3375. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3376. phba->work_hs = HS_FFER3;
  3377. lpfc_handle_eratt(phba);
  3378. return;
  3379. }
  3380. rmb();
  3381. while (pring->sli.sli3.rspidx != portRspPut) {
  3382. /*
  3383. * Build a completion list and call the appropriate handler.
  3384. * The process is to get the next available response iocb, get
  3385. * a free iocb from the list, copy the response data into the
  3386. * free iocb, insert to the continuation list, and update the
  3387. * next response index to slim. This process makes response
  3388. * iocb's in the ring available to DMA as fast as possible but
  3389. * pays a penalty for a copy operation. Since the iocb is
  3390. * only 32 bytes, this penalty is considered small relative to
  3391. * the PCI reads for register values and a slim write. When
  3392. * the ulpLe field is set, the entire Command has been
  3393. * received.
  3394. */
  3395. entry = lpfc_resp_iocb(phba, pring);
  3396. phba->last_completion_time = jiffies;
  3397. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3398. if (rspiocbp == NULL) {
  3399. printk(KERN_ERR "%s: out of buffers! Failing "
  3400. "completion.\n", __func__);
  3401. break;
  3402. }
  3403. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3404. phba->iocb_rsp_size);
  3405. irsp = &rspiocbp->iocb;
  3406. if (++pring->sli.sli3.rspidx >= portRspMax)
  3407. pring->sli.sli3.rspidx = 0;
  3408. if (pring->ringno == LPFC_ELS_RING) {
  3409. lpfc_debugfs_slow_ring_trc(phba,
  3410. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3411. *(((uint32_t *) irsp) + 4),
  3412. *(((uint32_t *) irsp) + 6),
  3413. *(((uint32_t *) irsp) + 7));
  3414. }
  3415. writel(pring->sli.sli3.rspidx,
  3416. &phba->host_gp[pring->ringno].rspGetInx);
  3417. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3418. /* Handle the response IOCB */
  3419. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3420. spin_lock_irqsave(&phba->hbalock, iflag);
  3421. /*
  3422. * If the port response put pointer has not been updated, sync
  3423. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3424. * response put pointer.
  3425. */
  3426. if (pring->sli.sli3.rspidx == portRspPut) {
  3427. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3428. }
  3429. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3430. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3431. /* At least one response entry has been freed */
  3432. pring->stats.iocb_rsp_full++;
  3433. /* SET RxRE_RSP in Chip Att register */
  3434. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3435. writel(status, phba->CAregaddr);
  3436. readl(phba->CAregaddr); /* flush */
  3437. }
  3438. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3439. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3440. pring->stats.iocb_cmd_empty++;
  3441. /* Force update of the local copy of cmdGetInx */
  3442. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3443. lpfc_sli_resume_iocb(phba, pring);
  3444. if ((pring->lpfc_sli_cmd_available))
  3445. (pring->lpfc_sli_cmd_available) (phba, pring);
  3446. }
  3447. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3448. return;
  3449. }
  3450. /**
  3451. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3452. * @phba: Pointer to HBA context object.
  3453. * @pring: Pointer to driver SLI ring object.
  3454. * @mask: Host attention register mask for this ring.
  3455. *
  3456. * This function is called from the worker thread when there is a pending
  3457. * ELS response iocb on the driver internal slow-path response iocb worker
  3458. * queue. The caller does not hold any lock. The function will remove each
  3459. * response iocb from the response worker queue and calls the handle
  3460. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3461. **/
  3462. static void
  3463. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3464. struct lpfc_sli_ring *pring, uint32_t mask)
  3465. {
  3466. struct lpfc_iocbq *irspiocbq;
  3467. struct hbq_dmabuf *dmabuf;
  3468. struct lpfc_cq_event *cq_event;
  3469. unsigned long iflag;
  3470. int count = 0;
  3471. spin_lock_irqsave(&phba->hbalock, iflag);
  3472. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3473. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3474. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3475. /* Get the response iocb from the head of work queue */
  3476. spin_lock_irqsave(&phba->hbalock, iflag);
  3477. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3478. cq_event, struct lpfc_cq_event, list);
  3479. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3480. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3481. case CQE_CODE_COMPL_WQE:
  3482. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3483. cq_event);
  3484. /* Translate ELS WCQE to response IOCBQ */
  3485. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3486. irspiocbq);
  3487. if (irspiocbq)
  3488. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3489. irspiocbq);
  3490. count++;
  3491. break;
  3492. case CQE_CODE_RECEIVE:
  3493. case CQE_CODE_RECEIVE_V1:
  3494. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3495. cq_event);
  3496. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3497. count++;
  3498. break;
  3499. default:
  3500. break;
  3501. }
  3502. /* Limit the number of events to 64 to avoid soft lockups */
  3503. if (count == 64)
  3504. break;
  3505. }
  3506. }
  3507. /**
  3508. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3509. * @phba: Pointer to HBA context object.
  3510. * @pring: Pointer to driver SLI ring object.
  3511. *
  3512. * This function aborts all iocbs in the given ring and frees all the iocb
  3513. * objects in txq. This function issues an abort iocb for all the iocb commands
  3514. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3515. * the return of this function. The caller is not required to hold any locks.
  3516. **/
  3517. void
  3518. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3519. {
  3520. LIST_HEAD(completions);
  3521. struct lpfc_iocbq *iocb, *next_iocb;
  3522. if (pring->ringno == LPFC_ELS_RING) {
  3523. lpfc_fabric_abort_hba(phba);
  3524. }
  3525. /* Error everything on txq and txcmplq
  3526. * First do the txq.
  3527. */
  3528. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3529. spin_lock_irq(&pring->ring_lock);
  3530. list_splice_init(&pring->txq, &completions);
  3531. pring->txq_cnt = 0;
  3532. spin_unlock_irq(&pring->ring_lock);
  3533. spin_lock_irq(&phba->hbalock);
  3534. /* Next issue ABTS for everything on the txcmplq */
  3535. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3536. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3537. spin_unlock_irq(&phba->hbalock);
  3538. } else {
  3539. spin_lock_irq(&phba->hbalock);
  3540. list_splice_init(&pring->txq, &completions);
  3541. pring->txq_cnt = 0;
  3542. /* Next issue ABTS for everything on the txcmplq */
  3543. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3544. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3545. spin_unlock_irq(&phba->hbalock);
  3546. }
  3547. /* Cancel all the IOCBs from the completions list */
  3548. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3549. IOERR_SLI_ABORTED);
  3550. }
  3551. /**
  3552. * lpfc_sli_abort_wqe_ring - Abort all iocbs in the ring
  3553. * @phba: Pointer to HBA context object.
  3554. * @pring: Pointer to driver SLI ring object.
  3555. *
  3556. * This function aborts all iocbs in the given ring and frees all the iocb
  3557. * objects in txq. This function issues an abort iocb for all the iocb commands
  3558. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3559. * the return of this function. The caller is not required to hold any locks.
  3560. **/
  3561. void
  3562. lpfc_sli_abort_wqe_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3563. {
  3564. LIST_HEAD(completions);
  3565. struct lpfc_iocbq *iocb, *next_iocb;
  3566. if (pring->ringno == LPFC_ELS_RING)
  3567. lpfc_fabric_abort_hba(phba);
  3568. spin_lock_irq(&phba->hbalock);
  3569. /* Next issue ABTS for everything on the txcmplq */
  3570. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3571. lpfc_sli4_abort_nvme_io(phba, pring, iocb);
  3572. spin_unlock_irq(&phba->hbalock);
  3573. }
  3574. /**
  3575. * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
  3576. * @phba: Pointer to HBA context object.
  3577. * @pring: Pointer to driver SLI ring object.
  3578. *
  3579. * This function aborts all iocbs in FCP rings and frees all the iocb
  3580. * objects in txq. This function issues an abort iocb for all the iocb commands
  3581. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3582. * the return of this function. The caller is not required to hold any locks.
  3583. **/
  3584. void
  3585. lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
  3586. {
  3587. struct lpfc_sli *psli = &phba->sli;
  3588. struct lpfc_sli_ring *pring;
  3589. uint32_t i;
  3590. /* Look on all the FCP Rings for the iotag */
  3591. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3592. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3593. pring = phba->sli4_hba.fcp_wq[i]->pring;
  3594. lpfc_sli_abort_iocb_ring(phba, pring);
  3595. }
  3596. } else {
  3597. pring = &psli->sli3_ring[LPFC_FCP_RING];
  3598. lpfc_sli_abort_iocb_ring(phba, pring);
  3599. }
  3600. }
  3601. /**
  3602. * lpfc_sli_abort_nvme_rings - Abort all wqes in all NVME rings
  3603. * @phba: Pointer to HBA context object.
  3604. *
  3605. * This function aborts all wqes in NVME rings. This function issues an
  3606. * abort wqe for all the outstanding IO commands in txcmplq. The iocbs in
  3607. * the txcmplq is not guaranteed to complete before the return of this
  3608. * function. The caller is not required to hold any locks.
  3609. **/
  3610. void
  3611. lpfc_sli_abort_nvme_rings(struct lpfc_hba *phba)
  3612. {
  3613. struct lpfc_sli_ring *pring;
  3614. uint32_t i;
  3615. if (phba->sli_rev < LPFC_SLI_REV4)
  3616. return;
  3617. /* Abort all IO on each NVME ring. */
  3618. for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
  3619. pring = phba->sli4_hba.nvme_wq[i]->pring;
  3620. lpfc_sli_abort_wqe_ring(phba, pring);
  3621. }
  3622. }
  3623. /**
  3624. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3625. * @phba: Pointer to HBA context object.
  3626. *
  3627. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3628. * objects in txq and txcmplq. This function will not issue abort iocbs
  3629. * for all the iocb commands in txcmplq, they will just be returned with
  3630. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3631. * slot has been permanently disabled.
  3632. **/
  3633. void
  3634. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3635. {
  3636. LIST_HEAD(txq);
  3637. LIST_HEAD(txcmplq);
  3638. struct lpfc_sli *psli = &phba->sli;
  3639. struct lpfc_sli_ring *pring;
  3640. uint32_t i;
  3641. struct lpfc_iocbq *piocb, *next_iocb;
  3642. spin_lock_irq(&phba->hbalock);
  3643. /* Indicate the I/O queues are flushed */
  3644. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3645. spin_unlock_irq(&phba->hbalock);
  3646. /* Look on all the FCP Rings for the iotag */
  3647. if (phba->sli_rev >= LPFC_SLI_REV4) {
  3648. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  3649. pring = phba->sli4_hba.fcp_wq[i]->pring;
  3650. spin_lock_irq(&pring->ring_lock);
  3651. /* Retrieve everything on txq */
  3652. list_splice_init(&pring->txq, &txq);
  3653. list_for_each_entry_safe(piocb, next_iocb,
  3654. &pring->txcmplq, list)
  3655. piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  3656. /* Retrieve everything on the txcmplq */
  3657. list_splice_init(&pring->txcmplq, &txcmplq);
  3658. pring->txq_cnt = 0;
  3659. pring->txcmplq_cnt = 0;
  3660. spin_unlock_irq(&pring->ring_lock);
  3661. /* Flush the txq */
  3662. lpfc_sli_cancel_iocbs(phba, &txq,
  3663. IOSTAT_LOCAL_REJECT,
  3664. IOERR_SLI_DOWN);
  3665. /* Flush the txcmpq */
  3666. lpfc_sli_cancel_iocbs(phba, &txcmplq,
  3667. IOSTAT_LOCAL_REJECT,
  3668. IOERR_SLI_DOWN);
  3669. }
  3670. } else {
  3671. pring = &psli->sli3_ring[LPFC_FCP_RING];
  3672. spin_lock_irq(&phba->hbalock);
  3673. /* Retrieve everything on txq */
  3674. list_splice_init(&pring->txq, &txq);
  3675. list_for_each_entry_safe(piocb, next_iocb,
  3676. &pring->txcmplq, list)
  3677. piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  3678. /* Retrieve everything on the txcmplq */
  3679. list_splice_init(&pring->txcmplq, &txcmplq);
  3680. pring->txq_cnt = 0;
  3681. pring->txcmplq_cnt = 0;
  3682. spin_unlock_irq(&phba->hbalock);
  3683. /* Flush the txq */
  3684. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3685. IOERR_SLI_DOWN);
  3686. /* Flush the txcmpq */
  3687. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3688. IOERR_SLI_DOWN);
  3689. }
  3690. }
  3691. /**
  3692. * lpfc_sli_flush_nvme_rings - flush all wqes in the nvme rings
  3693. * @phba: Pointer to HBA context object.
  3694. *
  3695. * This function flushes all wqes in the nvme rings and frees all resources
  3696. * in the txcmplq. This function does not issue abort wqes for the IO
  3697. * commands in txcmplq, they will just be returned with
  3698. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3699. * slot has been permanently disabled.
  3700. **/
  3701. void
  3702. lpfc_sli_flush_nvme_rings(struct lpfc_hba *phba)
  3703. {
  3704. LIST_HEAD(txcmplq);
  3705. struct lpfc_sli_ring *pring;
  3706. uint32_t i;
  3707. struct lpfc_iocbq *piocb, *next_iocb;
  3708. if (phba->sli_rev < LPFC_SLI_REV4)
  3709. return;
  3710. /* Hint to other driver operations that a flush is in progress. */
  3711. spin_lock_irq(&phba->hbalock);
  3712. phba->hba_flag |= HBA_NVME_IOQ_FLUSH;
  3713. spin_unlock_irq(&phba->hbalock);
  3714. /* Cycle through all NVME rings and complete each IO with
  3715. * a local driver reason code. This is a flush so no
  3716. * abort exchange to FW.
  3717. */
  3718. for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
  3719. pring = phba->sli4_hba.nvme_wq[i]->pring;
  3720. spin_lock_irq(&pring->ring_lock);
  3721. list_for_each_entry_safe(piocb, next_iocb,
  3722. &pring->txcmplq, list)
  3723. piocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  3724. /* Retrieve everything on the txcmplq */
  3725. list_splice_init(&pring->txcmplq, &txcmplq);
  3726. pring->txcmplq_cnt = 0;
  3727. spin_unlock_irq(&pring->ring_lock);
  3728. /* Flush the txcmpq &&&PAE */
  3729. lpfc_sli_cancel_iocbs(phba, &txcmplq,
  3730. IOSTAT_LOCAL_REJECT,
  3731. IOERR_SLI_DOWN);
  3732. }
  3733. }
  3734. /**
  3735. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3736. * @phba: Pointer to HBA context object.
  3737. * @mask: Bit mask to be checked.
  3738. *
  3739. * This function reads the host status register and compares
  3740. * with the provided bit mask to check if HBA completed
  3741. * the restart. This function will wait in a loop for the
  3742. * HBA to complete restart. If the HBA does not restart within
  3743. * 15 iterations, the function will reset the HBA again. The
  3744. * function returns 1 when HBA fail to restart otherwise returns
  3745. * zero.
  3746. **/
  3747. static int
  3748. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3749. {
  3750. uint32_t status;
  3751. int i = 0;
  3752. int retval = 0;
  3753. /* Read the HBA Host Status Register */
  3754. if (lpfc_readl(phba->HSregaddr, &status))
  3755. return 1;
  3756. /*
  3757. * Check status register every 100ms for 5 retries, then every
  3758. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3759. * every 2.5 sec for 4.
  3760. * Break our of the loop if errors occurred during init.
  3761. */
  3762. while (((status & mask) != mask) &&
  3763. !(status & HS_FFERM) &&
  3764. i++ < 20) {
  3765. if (i <= 5)
  3766. msleep(10);
  3767. else if (i <= 10)
  3768. msleep(500);
  3769. else
  3770. msleep(2500);
  3771. if (i == 15) {
  3772. /* Do post */
  3773. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3774. lpfc_sli_brdrestart(phba);
  3775. }
  3776. /* Read the HBA Host Status Register */
  3777. if (lpfc_readl(phba->HSregaddr, &status)) {
  3778. retval = 1;
  3779. break;
  3780. }
  3781. }
  3782. /* Check to see if any errors occurred during init */
  3783. if ((status & HS_FFERM) || (i >= 20)) {
  3784. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3785. "2751 Adapter failed to restart, "
  3786. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3787. status,
  3788. readl(phba->MBslimaddr + 0xa8),
  3789. readl(phba->MBslimaddr + 0xac));
  3790. phba->link_state = LPFC_HBA_ERROR;
  3791. retval = 1;
  3792. }
  3793. return retval;
  3794. }
  3795. /**
  3796. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3797. * @phba: Pointer to HBA context object.
  3798. * @mask: Bit mask to be checked.
  3799. *
  3800. * This function checks the host status register to check if HBA is
  3801. * ready. This function will wait in a loop for the HBA to be ready
  3802. * If the HBA is not ready , the function will will reset the HBA PCI
  3803. * function again. The function returns 1 when HBA fail to be ready
  3804. * otherwise returns zero.
  3805. **/
  3806. static int
  3807. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3808. {
  3809. uint32_t status;
  3810. int retval = 0;
  3811. /* Read the HBA Host Status Register */
  3812. status = lpfc_sli4_post_status_check(phba);
  3813. if (status) {
  3814. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3815. lpfc_sli_brdrestart(phba);
  3816. status = lpfc_sli4_post_status_check(phba);
  3817. }
  3818. /* Check to see if any errors occurred during init */
  3819. if (status) {
  3820. phba->link_state = LPFC_HBA_ERROR;
  3821. retval = 1;
  3822. } else
  3823. phba->sli4_hba.intr_enable = 0;
  3824. return retval;
  3825. }
  3826. /**
  3827. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3828. * @phba: Pointer to HBA context object.
  3829. * @mask: Bit mask to be checked.
  3830. *
  3831. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3832. * from the API jump table function pointer from the lpfc_hba struct.
  3833. **/
  3834. int
  3835. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3836. {
  3837. return phba->lpfc_sli_brdready(phba, mask);
  3838. }
  3839. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3840. /**
  3841. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3842. * @phba: Pointer to HBA context object.
  3843. *
  3844. * This function is called before resetting an HBA. This function is called
  3845. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3846. **/
  3847. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3848. {
  3849. uint32_t __iomem *resp_buf;
  3850. uint32_t __iomem *mbox_buf;
  3851. volatile uint32_t mbox;
  3852. uint32_t hc_copy, ha_copy, resp_data;
  3853. int i;
  3854. uint8_t hdrtype;
  3855. lockdep_assert_held(&phba->hbalock);
  3856. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3857. if (hdrtype != 0x80 ||
  3858. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3859. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3860. return;
  3861. /*
  3862. * Tell the other part of the chip to suspend temporarily all
  3863. * its DMA activity.
  3864. */
  3865. resp_buf = phba->MBslimaddr;
  3866. /* Disable the error attention */
  3867. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3868. return;
  3869. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3870. readl(phba->HCregaddr); /* flush */
  3871. phba->link_flag |= LS_IGNORE_ERATT;
  3872. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3873. return;
  3874. if (ha_copy & HA_ERATT) {
  3875. /* Clear Chip error bit */
  3876. writel(HA_ERATT, phba->HAregaddr);
  3877. phba->pport->stopped = 1;
  3878. }
  3879. mbox = 0;
  3880. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3881. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3882. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3883. mbox_buf = phba->MBslimaddr;
  3884. writel(mbox, mbox_buf);
  3885. for (i = 0; i < 50; i++) {
  3886. if (lpfc_readl((resp_buf + 1), &resp_data))
  3887. return;
  3888. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3889. mdelay(1);
  3890. else
  3891. break;
  3892. }
  3893. resp_data = 0;
  3894. if (lpfc_readl((resp_buf + 1), &resp_data))
  3895. return;
  3896. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3897. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3898. phba->pport->stopped)
  3899. goto restore_hc;
  3900. else
  3901. goto clear_errat;
  3902. }
  3903. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3904. resp_data = 0;
  3905. for (i = 0; i < 500; i++) {
  3906. if (lpfc_readl(resp_buf, &resp_data))
  3907. return;
  3908. if (resp_data != mbox)
  3909. mdelay(1);
  3910. else
  3911. break;
  3912. }
  3913. clear_errat:
  3914. while (++i < 500) {
  3915. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3916. return;
  3917. if (!(ha_copy & HA_ERATT))
  3918. mdelay(1);
  3919. else
  3920. break;
  3921. }
  3922. if (readl(phba->HAregaddr) & HA_ERATT) {
  3923. writel(HA_ERATT, phba->HAregaddr);
  3924. phba->pport->stopped = 1;
  3925. }
  3926. restore_hc:
  3927. phba->link_flag &= ~LS_IGNORE_ERATT;
  3928. writel(hc_copy, phba->HCregaddr);
  3929. readl(phba->HCregaddr); /* flush */
  3930. }
  3931. /**
  3932. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3933. * @phba: Pointer to HBA context object.
  3934. *
  3935. * This function issues a kill_board mailbox command and waits for
  3936. * the error attention interrupt. This function is called for stopping
  3937. * the firmware processing. The caller is not required to hold any
  3938. * locks. This function calls lpfc_hba_down_post function to free
  3939. * any pending commands after the kill. The function will return 1 when it
  3940. * fails to kill the board else will return 0.
  3941. **/
  3942. int
  3943. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3944. {
  3945. struct lpfc_sli *psli;
  3946. LPFC_MBOXQ_t *pmb;
  3947. uint32_t status;
  3948. uint32_t ha_copy;
  3949. int retval;
  3950. int i = 0;
  3951. psli = &phba->sli;
  3952. /* Kill HBA */
  3953. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3954. "0329 Kill HBA Data: x%x x%x\n",
  3955. phba->pport->port_state, psli->sli_flag);
  3956. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3957. if (!pmb)
  3958. return 1;
  3959. /* Disable the error attention */
  3960. spin_lock_irq(&phba->hbalock);
  3961. if (lpfc_readl(phba->HCregaddr, &status)) {
  3962. spin_unlock_irq(&phba->hbalock);
  3963. mempool_free(pmb, phba->mbox_mem_pool);
  3964. return 1;
  3965. }
  3966. status &= ~HC_ERINT_ENA;
  3967. writel(status, phba->HCregaddr);
  3968. readl(phba->HCregaddr); /* flush */
  3969. phba->link_flag |= LS_IGNORE_ERATT;
  3970. spin_unlock_irq(&phba->hbalock);
  3971. lpfc_kill_board(phba, pmb);
  3972. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3973. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3974. if (retval != MBX_SUCCESS) {
  3975. if (retval != MBX_BUSY)
  3976. mempool_free(pmb, phba->mbox_mem_pool);
  3977. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3978. "2752 KILL_BOARD command failed retval %d\n",
  3979. retval);
  3980. spin_lock_irq(&phba->hbalock);
  3981. phba->link_flag &= ~LS_IGNORE_ERATT;
  3982. spin_unlock_irq(&phba->hbalock);
  3983. return 1;
  3984. }
  3985. spin_lock_irq(&phba->hbalock);
  3986. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3987. spin_unlock_irq(&phba->hbalock);
  3988. mempool_free(pmb, phba->mbox_mem_pool);
  3989. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3990. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3991. * 3 seconds we still set HBA_ERROR state because the status of the
  3992. * board is now undefined.
  3993. */
  3994. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3995. return 1;
  3996. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3997. mdelay(100);
  3998. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3999. return 1;
  4000. }
  4001. del_timer_sync(&psli->mbox_tmo);
  4002. if (ha_copy & HA_ERATT) {
  4003. writel(HA_ERATT, phba->HAregaddr);
  4004. phba->pport->stopped = 1;
  4005. }
  4006. spin_lock_irq(&phba->hbalock);
  4007. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4008. psli->mbox_active = NULL;
  4009. phba->link_flag &= ~LS_IGNORE_ERATT;
  4010. spin_unlock_irq(&phba->hbalock);
  4011. lpfc_hba_down_post(phba);
  4012. phba->link_state = LPFC_HBA_ERROR;
  4013. return ha_copy & HA_ERATT ? 0 : 1;
  4014. }
  4015. /**
  4016. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  4017. * @phba: Pointer to HBA context object.
  4018. *
  4019. * This function resets the HBA by writing HC_INITFF to the control
  4020. * register. After the HBA resets, this function resets all the iocb ring
  4021. * indices. This function disables PCI layer parity checking during
  4022. * the reset.
  4023. * This function returns 0 always.
  4024. * The caller is not required to hold any locks.
  4025. **/
  4026. int
  4027. lpfc_sli_brdreset(struct lpfc_hba *phba)
  4028. {
  4029. struct lpfc_sli *psli;
  4030. struct lpfc_sli_ring *pring;
  4031. uint16_t cfg_value;
  4032. int i;
  4033. psli = &phba->sli;
  4034. /* Reset HBA */
  4035. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4036. "0325 Reset HBA Data: x%x x%x\n",
  4037. (phba->pport) ? phba->pport->port_state : 0,
  4038. psli->sli_flag);
  4039. /* perform board reset */
  4040. phba->fc_eventTag = 0;
  4041. phba->link_events = 0;
  4042. if (phba->pport) {
  4043. phba->pport->fc_myDID = 0;
  4044. phba->pport->fc_prevDID = 0;
  4045. }
  4046. /* Turn off parity checking and serr during the physical reset */
  4047. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  4048. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  4049. (cfg_value &
  4050. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  4051. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  4052. /* Now toggle INITFF bit in the Host Control Register */
  4053. writel(HC_INITFF, phba->HCregaddr);
  4054. mdelay(1);
  4055. readl(phba->HCregaddr); /* flush */
  4056. writel(0, phba->HCregaddr);
  4057. readl(phba->HCregaddr); /* flush */
  4058. /* Restore PCI cmd register */
  4059. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  4060. /* Initialize relevant SLI info */
  4061. for (i = 0; i < psli->num_rings; i++) {
  4062. pring = &psli->sli3_ring[i];
  4063. pring->flag = 0;
  4064. pring->sli.sli3.rspidx = 0;
  4065. pring->sli.sli3.next_cmdidx = 0;
  4066. pring->sli.sli3.local_getidx = 0;
  4067. pring->sli.sli3.cmdidx = 0;
  4068. pring->missbufcnt = 0;
  4069. }
  4070. phba->link_state = LPFC_WARM_START;
  4071. return 0;
  4072. }
  4073. /**
  4074. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  4075. * @phba: Pointer to HBA context object.
  4076. *
  4077. * This function resets a SLI4 HBA. This function disables PCI layer parity
  4078. * checking during resets the device. The caller is not required to hold
  4079. * any locks.
  4080. *
  4081. * This function returns 0 always.
  4082. **/
  4083. int
  4084. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  4085. {
  4086. struct lpfc_sli *psli = &phba->sli;
  4087. uint16_t cfg_value;
  4088. int rc = 0;
  4089. /* Reset HBA */
  4090. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4091. "0295 Reset HBA Data: x%x x%x x%x\n",
  4092. phba->pport->port_state, psli->sli_flag,
  4093. phba->hba_flag);
  4094. /* perform board reset */
  4095. phba->fc_eventTag = 0;
  4096. phba->link_events = 0;
  4097. phba->pport->fc_myDID = 0;
  4098. phba->pport->fc_prevDID = 0;
  4099. spin_lock_irq(&phba->hbalock);
  4100. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  4101. phba->fcf.fcf_flag = 0;
  4102. spin_unlock_irq(&phba->hbalock);
  4103. /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
  4104. if (phba->hba_flag & HBA_FW_DUMP_OP) {
  4105. phba->hba_flag &= ~HBA_FW_DUMP_OP;
  4106. return rc;
  4107. }
  4108. /* Now physically reset the device */
  4109. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4110. "0389 Performing PCI function reset!\n");
  4111. /* Turn off parity checking and serr during the physical reset */
  4112. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  4113. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  4114. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  4115. /* Perform FCoE PCI function reset before freeing queue memory */
  4116. rc = lpfc_pci_function_reset(phba);
  4117. /* Restore PCI cmd register */
  4118. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  4119. return rc;
  4120. }
  4121. /**
  4122. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  4123. * @phba: Pointer to HBA context object.
  4124. *
  4125. * This function is called in the SLI initialization code path to
  4126. * restart the HBA. The caller is not required to hold any lock.
  4127. * This function writes MBX_RESTART mailbox command to the SLIM and
  4128. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  4129. * function to free any pending commands. The function enables
  4130. * POST only during the first initialization. The function returns zero.
  4131. * The function does not guarantee completion of MBX_RESTART mailbox
  4132. * command before the return of this function.
  4133. **/
  4134. static int
  4135. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  4136. {
  4137. MAILBOX_t *mb;
  4138. struct lpfc_sli *psli;
  4139. volatile uint32_t word0;
  4140. void __iomem *to_slim;
  4141. uint32_t hba_aer_enabled;
  4142. spin_lock_irq(&phba->hbalock);
  4143. /* Take PCIe device Advanced Error Reporting (AER) state */
  4144. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  4145. psli = &phba->sli;
  4146. /* Restart HBA */
  4147. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4148. "0337 Restart HBA Data: x%x x%x\n",
  4149. (phba->pport) ? phba->pport->port_state : 0,
  4150. psli->sli_flag);
  4151. word0 = 0;
  4152. mb = (MAILBOX_t *) &word0;
  4153. mb->mbxCommand = MBX_RESTART;
  4154. mb->mbxHc = 1;
  4155. lpfc_reset_barrier(phba);
  4156. to_slim = phba->MBslimaddr;
  4157. writel(*(uint32_t *) mb, to_slim);
  4158. readl(to_slim); /* flush */
  4159. /* Only skip post after fc_ffinit is completed */
  4160. if (phba->pport && phba->pport->port_state)
  4161. word0 = 1; /* This is really setting up word1 */
  4162. else
  4163. word0 = 0; /* This is really setting up word1 */
  4164. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  4165. writel(*(uint32_t *) mb, to_slim);
  4166. readl(to_slim); /* flush */
  4167. lpfc_sli_brdreset(phba);
  4168. if (phba->pport)
  4169. phba->pport->stopped = 0;
  4170. phba->link_state = LPFC_INIT_START;
  4171. phba->hba_flag = 0;
  4172. spin_unlock_irq(&phba->hbalock);
  4173. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  4174. psli->stats_start = ktime_get_seconds();
  4175. /* Give the INITFF and Post time to settle. */
  4176. mdelay(100);
  4177. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  4178. if (hba_aer_enabled)
  4179. pci_disable_pcie_error_reporting(phba->pcidev);
  4180. lpfc_hba_down_post(phba);
  4181. return 0;
  4182. }
  4183. /**
  4184. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  4185. * @phba: Pointer to HBA context object.
  4186. *
  4187. * This function is called in the SLI initialization code path to restart
  4188. * a SLI4 HBA. The caller is not required to hold any lock.
  4189. * At the end of the function, it calls lpfc_hba_down_post function to
  4190. * free any pending commands.
  4191. **/
  4192. static int
  4193. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  4194. {
  4195. struct lpfc_sli *psli = &phba->sli;
  4196. uint32_t hba_aer_enabled;
  4197. int rc;
  4198. /* Restart HBA */
  4199. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4200. "0296 Restart HBA Data: x%x x%x\n",
  4201. phba->pport->port_state, psli->sli_flag);
  4202. /* Take PCIe device Advanced Error Reporting (AER) state */
  4203. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  4204. rc = lpfc_sli4_brdreset(phba);
  4205. if (rc)
  4206. return rc;
  4207. spin_lock_irq(&phba->hbalock);
  4208. phba->pport->stopped = 0;
  4209. phba->link_state = LPFC_INIT_START;
  4210. phba->hba_flag = 0;
  4211. spin_unlock_irq(&phba->hbalock);
  4212. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  4213. psli->stats_start = ktime_get_seconds();
  4214. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  4215. if (hba_aer_enabled)
  4216. pci_disable_pcie_error_reporting(phba->pcidev);
  4217. lpfc_hba_down_post(phba);
  4218. lpfc_sli4_queue_destroy(phba);
  4219. return rc;
  4220. }
  4221. /**
  4222. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  4223. * @phba: Pointer to HBA context object.
  4224. *
  4225. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  4226. * API jump table function pointer from the lpfc_hba struct.
  4227. **/
  4228. int
  4229. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  4230. {
  4231. return phba->lpfc_sli_brdrestart(phba);
  4232. }
  4233. /**
  4234. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  4235. * @phba: Pointer to HBA context object.
  4236. *
  4237. * This function is called after a HBA restart to wait for successful
  4238. * restart of the HBA. Successful restart of the HBA is indicated by
  4239. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  4240. * iteration, the function will restart the HBA again. The function returns
  4241. * zero if HBA successfully restarted else returns negative error code.
  4242. **/
  4243. int
  4244. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  4245. {
  4246. uint32_t status, i = 0;
  4247. /* Read the HBA Host Status Register */
  4248. if (lpfc_readl(phba->HSregaddr, &status))
  4249. return -EIO;
  4250. /* Check status register to see what current state is */
  4251. i = 0;
  4252. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  4253. /* Check every 10ms for 10 retries, then every 100ms for 90
  4254. * retries, then every 1 sec for 50 retires for a total of
  4255. * ~60 seconds before reset the board again and check every
  4256. * 1 sec for 50 retries. The up to 60 seconds before the
  4257. * board ready is required by the Falcon FIPS zeroization
  4258. * complete, and any reset the board in between shall cause
  4259. * restart of zeroization, further delay the board ready.
  4260. */
  4261. if (i++ >= 200) {
  4262. /* Adapter failed to init, timeout, status reg
  4263. <status> */
  4264. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4265. "0436 Adapter failed to init, "
  4266. "timeout, status reg x%x, "
  4267. "FW Data: A8 x%x AC x%x\n", status,
  4268. readl(phba->MBslimaddr + 0xa8),
  4269. readl(phba->MBslimaddr + 0xac));
  4270. phba->link_state = LPFC_HBA_ERROR;
  4271. return -ETIMEDOUT;
  4272. }
  4273. /* Check to see if any errors occurred during init */
  4274. if (status & HS_FFERM) {
  4275. /* ERROR: During chipset initialization */
  4276. /* Adapter failed to init, chipset, status reg
  4277. <status> */
  4278. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4279. "0437 Adapter failed to init, "
  4280. "chipset, status reg x%x, "
  4281. "FW Data: A8 x%x AC x%x\n", status,
  4282. readl(phba->MBslimaddr + 0xa8),
  4283. readl(phba->MBslimaddr + 0xac));
  4284. phba->link_state = LPFC_HBA_ERROR;
  4285. return -EIO;
  4286. }
  4287. if (i <= 10)
  4288. msleep(10);
  4289. else if (i <= 100)
  4290. msleep(100);
  4291. else
  4292. msleep(1000);
  4293. if (i == 150) {
  4294. /* Do post */
  4295. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  4296. lpfc_sli_brdrestart(phba);
  4297. }
  4298. /* Read the HBA Host Status Register */
  4299. if (lpfc_readl(phba->HSregaddr, &status))
  4300. return -EIO;
  4301. }
  4302. /* Check to see if any errors occurred during init */
  4303. if (status & HS_FFERM) {
  4304. /* ERROR: During chipset initialization */
  4305. /* Adapter failed to init, chipset, status reg <status> */
  4306. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4307. "0438 Adapter failed to init, chipset, "
  4308. "status reg x%x, "
  4309. "FW Data: A8 x%x AC x%x\n", status,
  4310. readl(phba->MBslimaddr + 0xa8),
  4311. readl(phba->MBslimaddr + 0xac));
  4312. phba->link_state = LPFC_HBA_ERROR;
  4313. return -EIO;
  4314. }
  4315. /* Clear all interrupt enable conditions */
  4316. writel(0, phba->HCregaddr);
  4317. readl(phba->HCregaddr); /* flush */
  4318. /* setup host attn register */
  4319. writel(0xffffffff, phba->HAregaddr);
  4320. readl(phba->HAregaddr); /* flush */
  4321. return 0;
  4322. }
  4323. /**
  4324. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  4325. *
  4326. * This function calculates and returns the number of HBQs required to be
  4327. * configured.
  4328. **/
  4329. int
  4330. lpfc_sli_hbq_count(void)
  4331. {
  4332. return ARRAY_SIZE(lpfc_hbq_defs);
  4333. }
  4334. /**
  4335. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  4336. *
  4337. * This function adds the number of hbq entries in every HBQ to get
  4338. * the total number of hbq entries required for the HBA and returns
  4339. * the total count.
  4340. **/
  4341. static int
  4342. lpfc_sli_hbq_entry_count(void)
  4343. {
  4344. int hbq_count = lpfc_sli_hbq_count();
  4345. int count = 0;
  4346. int i;
  4347. for (i = 0; i < hbq_count; ++i)
  4348. count += lpfc_hbq_defs[i]->entry_count;
  4349. return count;
  4350. }
  4351. /**
  4352. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  4353. *
  4354. * This function calculates amount of memory required for all hbq entries
  4355. * to be configured and returns the total memory required.
  4356. **/
  4357. int
  4358. lpfc_sli_hbq_size(void)
  4359. {
  4360. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  4361. }
  4362. /**
  4363. * lpfc_sli_hbq_setup - configure and initialize HBQs
  4364. * @phba: Pointer to HBA context object.
  4365. *
  4366. * This function is called during the SLI initialization to configure
  4367. * all the HBQs and post buffers to the HBQ. The caller is not
  4368. * required to hold any locks. This function will return zero if successful
  4369. * else it will return negative error code.
  4370. **/
  4371. static int
  4372. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  4373. {
  4374. int hbq_count = lpfc_sli_hbq_count();
  4375. LPFC_MBOXQ_t *pmb;
  4376. MAILBOX_t *pmbox;
  4377. uint32_t hbqno;
  4378. uint32_t hbq_entry_index;
  4379. /* Get a Mailbox buffer to setup mailbox
  4380. * commands for HBA initialization
  4381. */
  4382. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4383. if (!pmb)
  4384. return -ENOMEM;
  4385. pmbox = &pmb->u.mb;
  4386. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  4387. phba->link_state = LPFC_INIT_MBX_CMDS;
  4388. phba->hbq_in_use = 1;
  4389. hbq_entry_index = 0;
  4390. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  4391. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  4392. phba->hbqs[hbqno].hbqPutIdx = 0;
  4393. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  4394. phba->hbqs[hbqno].entry_count =
  4395. lpfc_hbq_defs[hbqno]->entry_count;
  4396. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  4397. hbq_entry_index, pmb);
  4398. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  4399. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  4400. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  4401. mbxStatus <status>, ring <num> */
  4402. lpfc_printf_log(phba, KERN_ERR,
  4403. LOG_SLI | LOG_VPORT,
  4404. "1805 Adapter failed to init. "
  4405. "Data: x%x x%x x%x\n",
  4406. pmbox->mbxCommand,
  4407. pmbox->mbxStatus, hbqno);
  4408. phba->link_state = LPFC_HBA_ERROR;
  4409. mempool_free(pmb, phba->mbox_mem_pool);
  4410. return -ENXIO;
  4411. }
  4412. }
  4413. phba->hbq_count = hbq_count;
  4414. mempool_free(pmb, phba->mbox_mem_pool);
  4415. /* Initially populate or replenish the HBQs */
  4416. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  4417. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  4418. return 0;
  4419. }
  4420. /**
  4421. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  4422. * @phba: Pointer to HBA context object.
  4423. *
  4424. * This function is called during the SLI initialization to configure
  4425. * all the HBQs and post buffers to the HBQ. The caller is not
  4426. * required to hold any locks. This function will return zero if successful
  4427. * else it will return negative error code.
  4428. **/
  4429. static int
  4430. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  4431. {
  4432. phba->hbq_in_use = 1;
  4433. phba->hbqs[LPFC_ELS_HBQ].entry_count =
  4434. lpfc_hbq_defs[LPFC_ELS_HBQ]->entry_count;
  4435. phba->hbq_count = 1;
  4436. lpfc_sli_hbqbuf_init_hbqs(phba, LPFC_ELS_HBQ);
  4437. /* Initially populate or replenish the HBQs */
  4438. return 0;
  4439. }
  4440. /**
  4441. * lpfc_sli_config_port - Issue config port mailbox command
  4442. * @phba: Pointer to HBA context object.
  4443. * @sli_mode: sli mode - 2/3
  4444. *
  4445. * This function is called by the sli initialization code path
  4446. * to issue config_port mailbox command. This function restarts the
  4447. * HBA firmware and issues a config_port mailbox command to configure
  4448. * the SLI interface in the sli mode specified by sli_mode
  4449. * variable. The caller is not required to hold any locks.
  4450. * The function returns 0 if successful, else returns negative error
  4451. * code.
  4452. **/
  4453. int
  4454. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  4455. {
  4456. LPFC_MBOXQ_t *pmb;
  4457. uint32_t resetcount = 0, rc = 0, done = 0;
  4458. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4459. if (!pmb) {
  4460. phba->link_state = LPFC_HBA_ERROR;
  4461. return -ENOMEM;
  4462. }
  4463. phba->sli_rev = sli_mode;
  4464. while (resetcount < 2 && !done) {
  4465. spin_lock_irq(&phba->hbalock);
  4466. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  4467. spin_unlock_irq(&phba->hbalock);
  4468. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  4469. lpfc_sli_brdrestart(phba);
  4470. rc = lpfc_sli_chipset_init(phba);
  4471. if (rc)
  4472. break;
  4473. spin_lock_irq(&phba->hbalock);
  4474. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  4475. spin_unlock_irq(&phba->hbalock);
  4476. resetcount++;
  4477. /* Call pre CONFIG_PORT mailbox command initialization. A
  4478. * value of 0 means the call was successful. Any other
  4479. * nonzero value is a failure, but if ERESTART is returned,
  4480. * the driver may reset the HBA and try again.
  4481. */
  4482. rc = lpfc_config_port_prep(phba);
  4483. if (rc == -ERESTART) {
  4484. phba->link_state = LPFC_LINK_UNKNOWN;
  4485. continue;
  4486. } else if (rc)
  4487. break;
  4488. phba->link_state = LPFC_INIT_MBX_CMDS;
  4489. lpfc_config_port(phba, pmb);
  4490. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  4491. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  4492. LPFC_SLI3_HBQ_ENABLED |
  4493. LPFC_SLI3_CRP_ENABLED |
  4494. LPFC_SLI3_DSS_ENABLED);
  4495. if (rc != MBX_SUCCESS) {
  4496. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4497. "0442 Adapter failed to init, mbxCmd x%x "
  4498. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  4499. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  4500. spin_lock_irq(&phba->hbalock);
  4501. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  4502. spin_unlock_irq(&phba->hbalock);
  4503. rc = -ENXIO;
  4504. } else {
  4505. /* Allow asynchronous mailbox command to go through */
  4506. spin_lock_irq(&phba->hbalock);
  4507. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4508. spin_unlock_irq(&phba->hbalock);
  4509. done = 1;
  4510. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  4511. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  4512. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  4513. "3110 Port did not grant ASABT\n");
  4514. }
  4515. }
  4516. if (!done) {
  4517. rc = -EINVAL;
  4518. goto do_prep_failed;
  4519. }
  4520. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  4521. if (!pmb->u.mb.un.varCfgPort.cMA) {
  4522. rc = -ENXIO;
  4523. goto do_prep_failed;
  4524. }
  4525. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  4526. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  4527. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  4528. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  4529. phba->max_vpi : phba->max_vports;
  4530. } else
  4531. phba->max_vpi = 0;
  4532. phba->fips_level = 0;
  4533. phba->fips_spec_rev = 0;
  4534. if (pmb->u.mb.un.varCfgPort.gdss) {
  4535. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  4536. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4537. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4538. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4539. "2850 Security Crypto Active. FIPS x%d "
  4540. "(Spec Rev: x%d)",
  4541. phba->fips_level, phba->fips_spec_rev);
  4542. }
  4543. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4544. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4545. "2856 Config Port Security Crypto "
  4546. "Error: x%x ",
  4547. pmb->u.mb.un.varCfgPort.sec_err);
  4548. }
  4549. if (pmb->u.mb.un.varCfgPort.gerbm)
  4550. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4551. if (pmb->u.mb.un.varCfgPort.gcrp)
  4552. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4553. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4554. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4555. if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
  4556. if (pmb->u.mb.un.varCfgPort.gbg == 0) {
  4557. phba->cfg_enable_bg = 0;
  4558. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  4559. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4560. "0443 Adapter did not grant "
  4561. "BlockGuard\n");
  4562. }
  4563. }
  4564. } else {
  4565. phba->hbq_get = NULL;
  4566. phba->port_gp = phba->mbox->us.s2.port;
  4567. phba->max_vpi = 0;
  4568. }
  4569. do_prep_failed:
  4570. mempool_free(pmb, phba->mbox_mem_pool);
  4571. return rc;
  4572. }
  4573. /**
  4574. * lpfc_sli_hba_setup - SLI initialization function
  4575. * @phba: Pointer to HBA context object.
  4576. *
  4577. * This function is the main SLI initialization function. This function
  4578. * is called by the HBA initialization code, HBA reset code and HBA
  4579. * error attention handler code. Caller is not required to hold any
  4580. * locks. This function issues config_port mailbox command to configure
  4581. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4582. * calls the config_port_post function to issue init_link mailbox
  4583. * command and to start the discovery. The function will return zero
  4584. * if successful, else it will return negative error code.
  4585. **/
  4586. int
  4587. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4588. {
  4589. uint32_t rc;
  4590. int mode = 3, i;
  4591. int longs;
  4592. switch (phba->cfg_sli_mode) {
  4593. case 2:
  4594. if (phba->cfg_enable_npiv) {
  4595. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4596. "1824 NPIV enabled: Override sli_mode "
  4597. "parameter (%d) to auto (0).\n",
  4598. phba->cfg_sli_mode);
  4599. break;
  4600. }
  4601. mode = 2;
  4602. break;
  4603. case 0:
  4604. case 3:
  4605. break;
  4606. default:
  4607. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4608. "1819 Unrecognized sli_mode parameter: %d.\n",
  4609. phba->cfg_sli_mode);
  4610. break;
  4611. }
  4612. phba->fcp_embed_io = 0; /* SLI4 FC support only */
  4613. rc = lpfc_sli_config_port(phba, mode);
  4614. if (rc && phba->cfg_sli_mode == 3)
  4615. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4616. "1820 Unable to select SLI-3. "
  4617. "Not supported by adapter.\n");
  4618. if (rc && mode != 2)
  4619. rc = lpfc_sli_config_port(phba, 2);
  4620. else if (rc && mode == 2)
  4621. rc = lpfc_sli_config_port(phba, 3);
  4622. if (rc)
  4623. goto lpfc_sli_hba_setup_error;
  4624. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4625. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4626. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4627. if (!rc) {
  4628. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4629. "2709 This device supports "
  4630. "Advanced Error Reporting (AER)\n");
  4631. spin_lock_irq(&phba->hbalock);
  4632. phba->hba_flag |= HBA_AER_ENABLED;
  4633. spin_unlock_irq(&phba->hbalock);
  4634. } else {
  4635. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4636. "2708 This device does not support "
  4637. "Advanced Error Reporting (AER): %d\n",
  4638. rc);
  4639. phba->cfg_aer_support = 0;
  4640. }
  4641. }
  4642. if (phba->sli_rev == 3) {
  4643. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4644. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4645. } else {
  4646. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4647. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4648. phba->sli3_options = 0;
  4649. }
  4650. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4651. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4652. phba->sli_rev, phba->max_vpi);
  4653. rc = lpfc_sli_ring_map(phba);
  4654. if (rc)
  4655. goto lpfc_sli_hba_setup_error;
  4656. /* Initialize VPIs. */
  4657. if (phba->sli_rev == LPFC_SLI_REV3) {
  4658. /*
  4659. * The VPI bitmask and physical ID array are allocated
  4660. * and initialized once only - at driver load. A port
  4661. * reset doesn't need to reinitialize this memory.
  4662. */
  4663. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4664. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4665. phba->vpi_bmask = kcalloc(longs,
  4666. sizeof(unsigned long),
  4667. GFP_KERNEL);
  4668. if (!phba->vpi_bmask) {
  4669. rc = -ENOMEM;
  4670. goto lpfc_sli_hba_setup_error;
  4671. }
  4672. phba->vpi_ids = kcalloc(phba->max_vpi + 1,
  4673. sizeof(uint16_t),
  4674. GFP_KERNEL);
  4675. if (!phba->vpi_ids) {
  4676. kfree(phba->vpi_bmask);
  4677. rc = -ENOMEM;
  4678. goto lpfc_sli_hba_setup_error;
  4679. }
  4680. for (i = 0; i < phba->max_vpi; i++)
  4681. phba->vpi_ids[i] = i;
  4682. }
  4683. }
  4684. /* Init HBQs */
  4685. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4686. rc = lpfc_sli_hbq_setup(phba);
  4687. if (rc)
  4688. goto lpfc_sli_hba_setup_error;
  4689. }
  4690. spin_lock_irq(&phba->hbalock);
  4691. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4692. spin_unlock_irq(&phba->hbalock);
  4693. rc = lpfc_config_port_post(phba);
  4694. if (rc)
  4695. goto lpfc_sli_hba_setup_error;
  4696. return rc;
  4697. lpfc_sli_hba_setup_error:
  4698. phba->link_state = LPFC_HBA_ERROR;
  4699. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4700. "0445 Firmware initialization failed\n");
  4701. return rc;
  4702. }
  4703. /**
  4704. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4705. * @phba: Pointer to HBA context object.
  4706. * @mboxq: mailbox pointer.
  4707. * This function issue a dump mailbox command to read config region
  4708. * 23 and parse the records in the region and populate driver
  4709. * data structure.
  4710. **/
  4711. static int
  4712. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4713. {
  4714. LPFC_MBOXQ_t *mboxq;
  4715. struct lpfc_dmabuf *mp;
  4716. struct lpfc_mqe *mqe;
  4717. uint32_t data_length;
  4718. int rc;
  4719. /* Program the default value of vlan_id and fc_map */
  4720. phba->valid_vlan = 0;
  4721. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4722. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4723. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4724. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4725. if (!mboxq)
  4726. return -ENOMEM;
  4727. mqe = &mboxq->u.mqe;
  4728. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4729. rc = -ENOMEM;
  4730. goto out_free_mboxq;
  4731. }
  4732. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4733. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4734. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4735. "(%d):2571 Mailbox cmd x%x Status x%x "
  4736. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4737. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4738. "CQ: x%x x%x x%x x%x\n",
  4739. mboxq->vport ? mboxq->vport->vpi : 0,
  4740. bf_get(lpfc_mqe_command, mqe),
  4741. bf_get(lpfc_mqe_status, mqe),
  4742. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4743. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4744. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4745. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4746. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4747. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4748. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4749. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4750. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4751. mboxq->mcqe.word0,
  4752. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4753. mboxq->mcqe.trailer);
  4754. if (rc) {
  4755. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4756. kfree(mp);
  4757. rc = -EIO;
  4758. goto out_free_mboxq;
  4759. }
  4760. data_length = mqe->un.mb_words[5];
  4761. if (data_length > DMP_RGN23_SIZE) {
  4762. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4763. kfree(mp);
  4764. rc = -EIO;
  4765. goto out_free_mboxq;
  4766. }
  4767. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4768. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4769. kfree(mp);
  4770. rc = 0;
  4771. out_free_mboxq:
  4772. mempool_free(mboxq, phba->mbox_mem_pool);
  4773. return rc;
  4774. }
  4775. /**
  4776. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4777. * @phba: pointer to lpfc hba data structure.
  4778. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4779. * @vpd: pointer to the memory to hold resulting port vpd data.
  4780. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4781. * On output, the number of data bytes in @vpd.
  4782. *
  4783. * This routine executes a READ_REV SLI4 mailbox command. In
  4784. * addition, this routine gets the port vpd data.
  4785. *
  4786. * Return codes
  4787. * 0 - successful
  4788. * -ENOMEM - could not allocated memory.
  4789. **/
  4790. static int
  4791. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4792. uint8_t *vpd, uint32_t *vpd_size)
  4793. {
  4794. int rc = 0;
  4795. uint32_t dma_size;
  4796. struct lpfc_dmabuf *dmabuf;
  4797. struct lpfc_mqe *mqe;
  4798. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4799. if (!dmabuf)
  4800. return -ENOMEM;
  4801. /*
  4802. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4803. * mailbox command.
  4804. */
  4805. dma_size = *vpd_size;
  4806. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
  4807. &dmabuf->phys, GFP_KERNEL);
  4808. if (!dmabuf->virt) {
  4809. kfree(dmabuf);
  4810. return -ENOMEM;
  4811. }
  4812. /*
  4813. * The SLI4 implementation of READ_REV conflicts at word1,
  4814. * bits 31:16 and SLI4 adds vpd functionality not present
  4815. * in SLI3. This code corrects the conflicts.
  4816. */
  4817. lpfc_read_rev(phba, mboxq);
  4818. mqe = &mboxq->u.mqe;
  4819. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4820. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4821. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4822. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4823. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4824. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4825. if (rc) {
  4826. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4827. dmabuf->virt, dmabuf->phys);
  4828. kfree(dmabuf);
  4829. return -EIO;
  4830. }
  4831. /*
  4832. * The available vpd length cannot be bigger than the
  4833. * DMA buffer passed to the port. Catch the less than
  4834. * case and update the caller's size.
  4835. */
  4836. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4837. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4838. memcpy(vpd, dmabuf->virt, *vpd_size);
  4839. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4840. dmabuf->virt, dmabuf->phys);
  4841. kfree(dmabuf);
  4842. return 0;
  4843. }
  4844. /**
  4845. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4846. * @phba: pointer to lpfc hba data structure.
  4847. *
  4848. * This routine retrieves SLI4 device physical port name this PCI function
  4849. * is attached to.
  4850. *
  4851. * Return codes
  4852. * 0 - successful
  4853. * otherwise - failed to retrieve physical port name
  4854. **/
  4855. static int
  4856. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4857. {
  4858. LPFC_MBOXQ_t *mboxq;
  4859. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4860. struct lpfc_controller_attribute *cntl_attr;
  4861. struct lpfc_mbx_get_port_name *get_port_name;
  4862. void *virtaddr = NULL;
  4863. uint32_t alloclen, reqlen;
  4864. uint32_t shdr_status, shdr_add_status;
  4865. union lpfc_sli4_cfg_shdr *shdr;
  4866. char cport_name = 0;
  4867. int rc;
  4868. /* We assume nothing at this point */
  4869. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4870. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4871. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4872. if (!mboxq)
  4873. return -ENOMEM;
  4874. /* obtain link type and link number via READ_CONFIG */
  4875. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4876. lpfc_sli4_read_config(phba);
  4877. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4878. goto retrieve_ppname;
  4879. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4880. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4881. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4882. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4883. LPFC_SLI4_MBX_NEMBED);
  4884. if (alloclen < reqlen) {
  4885. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4886. "3084 Allocated DMA memory size (%d) is "
  4887. "less than the requested DMA memory size "
  4888. "(%d)\n", alloclen, reqlen);
  4889. rc = -ENOMEM;
  4890. goto out_free_mboxq;
  4891. }
  4892. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4893. virtaddr = mboxq->sge_array->addr[0];
  4894. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4895. shdr = &mbx_cntl_attr->cfg_shdr;
  4896. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4897. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4898. if (shdr_status || shdr_add_status || rc) {
  4899. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4900. "3085 Mailbox x%x (x%x/x%x) failed, "
  4901. "rc:x%x, status:x%x, add_status:x%x\n",
  4902. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4903. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4904. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4905. rc, shdr_status, shdr_add_status);
  4906. rc = -ENXIO;
  4907. goto out_free_mboxq;
  4908. }
  4909. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4910. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4911. phba->sli4_hba.lnk_info.lnk_tp =
  4912. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4913. phba->sli4_hba.lnk_info.lnk_no =
  4914. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4915. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4916. "3086 lnk_type:%d, lnk_numb:%d\n",
  4917. phba->sli4_hba.lnk_info.lnk_tp,
  4918. phba->sli4_hba.lnk_info.lnk_no);
  4919. retrieve_ppname:
  4920. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4921. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4922. sizeof(struct lpfc_mbx_get_port_name) -
  4923. sizeof(struct lpfc_sli4_cfg_mhdr),
  4924. LPFC_SLI4_MBX_EMBED);
  4925. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4926. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4927. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4928. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4929. phba->sli4_hba.lnk_info.lnk_tp);
  4930. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4931. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4932. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4933. if (shdr_status || shdr_add_status || rc) {
  4934. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4935. "3087 Mailbox x%x (x%x/x%x) failed: "
  4936. "rc:x%x, status:x%x, add_status:x%x\n",
  4937. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4938. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4939. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4940. rc, shdr_status, shdr_add_status);
  4941. rc = -ENXIO;
  4942. goto out_free_mboxq;
  4943. }
  4944. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4945. case LPFC_LINK_NUMBER_0:
  4946. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4947. &get_port_name->u.response);
  4948. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4949. break;
  4950. case LPFC_LINK_NUMBER_1:
  4951. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4952. &get_port_name->u.response);
  4953. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4954. break;
  4955. case LPFC_LINK_NUMBER_2:
  4956. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4957. &get_port_name->u.response);
  4958. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4959. break;
  4960. case LPFC_LINK_NUMBER_3:
  4961. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4962. &get_port_name->u.response);
  4963. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4964. break;
  4965. default:
  4966. break;
  4967. }
  4968. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4969. phba->Port[0] = cport_name;
  4970. phba->Port[1] = '\0';
  4971. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4972. "3091 SLI get port name: %s\n", phba->Port);
  4973. }
  4974. out_free_mboxq:
  4975. if (rc != MBX_TIMEOUT) {
  4976. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4977. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4978. else
  4979. mempool_free(mboxq, phba->mbox_mem_pool);
  4980. }
  4981. return rc;
  4982. }
  4983. /**
  4984. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4985. * @phba: pointer to lpfc hba data structure.
  4986. *
  4987. * This routine is called to explicitly arm the SLI4 device's completion and
  4988. * event queues
  4989. **/
  4990. static void
  4991. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4992. {
  4993. int qidx;
  4994. struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
  4995. sli4_hba->sli4_cq_release(sli4_hba->mbx_cq, LPFC_QUEUE_REARM);
  4996. sli4_hba->sli4_cq_release(sli4_hba->els_cq, LPFC_QUEUE_REARM);
  4997. if (sli4_hba->nvmels_cq)
  4998. sli4_hba->sli4_cq_release(sli4_hba->nvmels_cq,
  4999. LPFC_QUEUE_REARM);
  5000. if (sli4_hba->fcp_cq)
  5001. for (qidx = 0; qidx < phba->cfg_fcp_io_channel; qidx++)
  5002. sli4_hba->sli4_cq_release(sli4_hba->fcp_cq[qidx],
  5003. LPFC_QUEUE_REARM);
  5004. if (sli4_hba->nvme_cq)
  5005. for (qidx = 0; qidx < phba->cfg_nvme_io_channel; qidx++)
  5006. sli4_hba->sli4_cq_release(sli4_hba->nvme_cq[qidx],
  5007. LPFC_QUEUE_REARM);
  5008. if (phba->cfg_fof)
  5009. sli4_hba->sli4_cq_release(sli4_hba->oas_cq, LPFC_QUEUE_REARM);
  5010. if (sli4_hba->hba_eq)
  5011. for (qidx = 0; qidx < phba->io_channel_irqs; qidx++)
  5012. sli4_hba->sli4_eq_release(sli4_hba->hba_eq[qidx],
  5013. LPFC_QUEUE_REARM);
  5014. if (phba->nvmet_support) {
  5015. for (qidx = 0; qidx < phba->cfg_nvmet_mrq; qidx++) {
  5016. sli4_hba->sli4_cq_release(
  5017. sli4_hba->nvmet_cqset[qidx],
  5018. LPFC_QUEUE_REARM);
  5019. }
  5020. }
  5021. if (phba->cfg_fof)
  5022. sli4_hba->sli4_eq_release(sli4_hba->fof_eq, LPFC_QUEUE_REARM);
  5023. }
  5024. /**
  5025. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  5026. * @phba: Pointer to HBA context object.
  5027. * @type: The resource extent type.
  5028. * @extnt_count: buffer to hold port available extent count.
  5029. * @extnt_size: buffer to hold element count per extent.
  5030. *
  5031. * This function calls the port and retrievs the number of available
  5032. * extents and their size for a particular extent type.
  5033. *
  5034. * Returns: 0 if successful. Nonzero otherwise.
  5035. **/
  5036. int
  5037. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  5038. uint16_t *extnt_count, uint16_t *extnt_size)
  5039. {
  5040. int rc = 0;
  5041. uint32_t length;
  5042. uint32_t mbox_tmo;
  5043. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  5044. LPFC_MBOXQ_t *mbox;
  5045. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5046. if (!mbox)
  5047. return -ENOMEM;
  5048. /* Find out how many extents are available for this resource type */
  5049. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  5050. sizeof(struct lpfc_sli4_cfg_mhdr));
  5051. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5052. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  5053. length, LPFC_SLI4_MBX_EMBED);
  5054. /* Send an extents count of 0 - the GET doesn't use it. */
  5055. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  5056. LPFC_SLI4_MBX_EMBED);
  5057. if (unlikely(rc)) {
  5058. rc = -EIO;
  5059. goto err_exit;
  5060. }
  5061. if (!phba->sli4_hba.intr_enable)
  5062. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5063. else {
  5064. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5065. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5066. }
  5067. if (unlikely(rc)) {
  5068. rc = -EIO;
  5069. goto err_exit;
  5070. }
  5071. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  5072. if (bf_get(lpfc_mbox_hdr_status,
  5073. &rsrc_info->header.cfg_shdr.response)) {
  5074. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5075. "2930 Failed to get resource extents "
  5076. "Status 0x%x Add'l Status 0x%x\n",
  5077. bf_get(lpfc_mbox_hdr_status,
  5078. &rsrc_info->header.cfg_shdr.response),
  5079. bf_get(lpfc_mbox_hdr_add_status,
  5080. &rsrc_info->header.cfg_shdr.response));
  5081. rc = -EIO;
  5082. goto err_exit;
  5083. }
  5084. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  5085. &rsrc_info->u.rsp);
  5086. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  5087. &rsrc_info->u.rsp);
  5088. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  5089. "3162 Retrieved extents type-%d from port: count:%d, "
  5090. "size:%d\n", type, *extnt_count, *extnt_size);
  5091. err_exit:
  5092. mempool_free(mbox, phba->mbox_mem_pool);
  5093. return rc;
  5094. }
  5095. /**
  5096. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  5097. * @phba: Pointer to HBA context object.
  5098. * @type: The extent type to check.
  5099. *
  5100. * This function reads the current available extents from the port and checks
  5101. * if the extent count or extent size has changed since the last access.
  5102. * Callers use this routine post port reset to understand if there is a
  5103. * extent reprovisioning requirement.
  5104. *
  5105. * Returns:
  5106. * -Error: error indicates problem.
  5107. * 1: Extent count or size has changed.
  5108. * 0: No changes.
  5109. **/
  5110. static int
  5111. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  5112. {
  5113. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  5114. uint16_t size_diff, rsrc_ext_size;
  5115. int rc = 0;
  5116. struct lpfc_rsrc_blks *rsrc_entry;
  5117. struct list_head *rsrc_blk_list = NULL;
  5118. size_diff = 0;
  5119. curr_ext_cnt = 0;
  5120. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  5121. &rsrc_ext_cnt,
  5122. &rsrc_ext_size);
  5123. if (unlikely(rc))
  5124. return -EIO;
  5125. switch (type) {
  5126. case LPFC_RSC_TYPE_FCOE_RPI:
  5127. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  5128. break;
  5129. case LPFC_RSC_TYPE_FCOE_VPI:
  5130. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  5131. break;
  5132. case LPFC_RSC_TYPE_FCOE_XRI:
  5133. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  5134. break;
  5135. case LPFC_RSC_TYPE_FCOE_VFI:
  5136. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  5137. break;
  5138. default:
  5139. break;
  5140. }
  5141. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  5142. curr_ext_cnt++;
  5143. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  5144. size_diff++;
  5145. }
  5146. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  5147. rc = 1;
  5148. return rc;
  5149. }
  5150. /**
  5151. * lpfc_sli4_cfg_post_extnts -
  5152. * @phba: Pointer to HBA context object.
  5153. * @extnt_cnt - number of available extents.
  5154. * @type - the extent type (rpi, xri, vfi, vpi).
  5155. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  5156. * @mbox - pointer to the caller's allocated mailbox structure.
  5157. *
  5158. * This function executes the extents allocation request. It also
  5159. * takes care of the amount of memory needed to allocate or get the
  5160. * allocated extents. It is the caller's responsibility to evaluate
  5161. * the response.
  5162. *
  5163. * Returns:
  5164. * -Error: Error value describes the condition found.
  5165. * 0: if successful
  5166. **/
  5167. static int
  5168. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  5169. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  5170. {
  5171. int rc = 0;
  5172. uint32_t req_len;
  5173. uint32_t emb_len;
  5174. uint32_t alloc_len, mbox_tmo;
  5175. /* Calculate the total requested length of the dma memory */
  5176. req_len = extnt_cnt * sizeof(uint16_t);
  5177. /*
  5178. * Calculate the size of an embedded mailbox. The uint32_t
  5179. * accounts for extents-specific word.
  5180. */
  5181. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5182. sizeof(uint32_t);
  5183. /*
  5184. * Presume the allocation and response will fit into an embedded
  5185. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5186. */
  5187. *emb = LPFC_SLI4_MBX_EMBED;
  5188. if (req_len > emb_len) {
  5189. req_len = extnt_cnt * sizeof(uint16_t) +
  5190. sizeof(union lpfc_sli4_cfg_shdr) +
  5191. sizeof(uint32_t);
  5192. *emb = LPFC_SLI4_MBX_NEMBED;
  5193. }
  5194. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5195. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  5196. req_len, *emb);
  5197. if (alloc_len < req_len) {
  5198. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5199. "2982 Allocated DMA memory size (x%x) is "
  5200. "less than the requested DMA memory "
  5201. "size (x%x)\n", alloc_len, req_len);
  5202. return -ENOMEM;
  5203. }
  5204. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  5205. if (unlikely(rc))
  5206. return -EIO;
  5207. if (!phba->sli4_hba.intr_enable)
  5208. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5209. else {
  5210. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5211. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5212. }
  5213. if (unlikely(rc))
  5214. rc = -EIO;
  5215. return rc;
  5216. }
  5217. /**
  5218. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  5219. * @phba: Pointer to HBA context object.
  5220. * @type: The resource extent type to allocate.
  5221. *
  5222. * This function allocates the number of elements for the specified
  5223. * resource type.
  5224. **/
  5225. static int
  5226. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  5227. {
  5228. bool emb = false;
  5229. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  5230. uint16_t rsrc_id, rsrc_start, j, k;
  5231. uint16_t *ids;
  5232. int i, rc;
  5233. unsigned long longs;
  5234. unsigned long *bmask;
  5235. struct lpfc_rsrc_blks *rsrc_blks;
  5236. LPFC_MBOXQ_t *mbox;
  5237. uint32_t length;
  5238. struct lpfc_id_range *id_array = NULL;
  5239. void *virtaddr = NULL;
  5240. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5241. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5242. struct list_head *ext_blk_list;
  5243. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  5244. &rsrc_cnt,
  5245. &rsrc_size);
  5246. if (unlikely(rc))
  5247. return -EIO;
  5248. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  5249. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5250. "3009 No available Resource Extents "
  5251. "for resource type 0x%x: Count: 0x%x, "
  5252. "Size 0x%x\n", type, rsrc_cnt,
  5253. rsrc_size);
  5254. return -ENOMEM;
  5255. }
  5256. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  5257. "2903 Post resource extents type-0x%x: "
  5258. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  5259. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5260. if (!mbox)
  5261. return -ENOMEM;
  5262. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  5263. if (unlikely(rc)) {
  5264. rc = -EIO;
  5265. goto err_exit;
  5266. }
  5267. /*
  5268. * Figure out where the response is located. Then get local pointers
  5269. * to the response data. The port does not guarantee to respond to
  5270. * all extents counts request so update the local variable with the
  5271. * allocated count from the port.
  5272. */
  5273. if (emb == LPFC_SLI4_MBX_EMBED) {
  5274. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5275. id_array = &rsrc_ext->u.rsp.id[0];
  5276. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5277. } else {
  5278. virtaddr = mbox->sge_array->addr[0];
  5279. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5280. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5281. id_array = &n_rsrc->id;
  5282. }
  5283. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5284. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  5285. /*
  5286. * Based on the resource size and count, correct the base and max
  5287. * resource values.
  5288. */
  5289. length = sizeof(struct lpfc_rsrc_blks);
  5290. switch (type) {
  5291. case LPFC_RSC_TYPE_FCOE_RPI:
  5292. phba->sli4_hba.rpi_bmask = kcalloc(longs,
  5293. sizeof(unsigned long),
  5294. GFP_KERNEL);
  5295. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5296. rc = -ENOMEM;
  5297. goto err_exit;
  5298. }
  5299. phba->sli4_hba.rpi_ids = kcalloc(rsrc_id_cnt,
  5300. sizeof(uint16_t),
  5301. GFP_KERNEL);
  5302. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5303. kfree(phba->sli4_hba.rpi_bmask);
  5304. rc = -ENOMEM;
  5305. goto err_exit;
  5306. }
  5307. /*
  5308. * The next_rpi was initialized with the maximum available
  5309. * count but the port may allocate a smaller number. Catch
  5310. * that case and update the next_rpi.
  5311. */
  5312. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  5313. /* Initialize local ptrs for common extent processing later. */
  5314. bmask = phba->sli4_hba.rpi_bmask;
  5315. ids = phba->sli4_hba.rpi_ids;
  5316. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  5317. break;
  5318. case LPFC_RSC_TYPE_FCOE_VPI:
  5319. phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
  5320. GFP_KERNEL);
  5321. if (unlikely(!phba->vpi_bmask)) {
  5322. rc = -ENOMEM;
  5323. goto err_exit;
  5324. }
  5325. phba->vpi_ids = kcalloc(rsrc_id_cnt, sizeof(uint16_t),
  5326. GFP_KERNEL);
  5327. if (unlikely(!phba->vpi_ids)) {
  5328. kfree(phba->vpi_bmask);
  5329. rc = -ENOMEM;
  5330. goto err_exit;
  5331. }
  5332. /* Initialize local ptrs for common extent processing later. */
  5333. bmask = phba->vpi_bmask;
  5334. ids = phba->vpi_ids;
  5335. ext_blk_list = &phba->lpfc_vpi_blk_list;
  5336. break;
  5337. case LPFC_RSC_TYPE_FCOE_XRI:
  5338. phba->sli4_hba.xri_bmask = kcalloc(longs,
  5339. sizeof(unsigned long),
  5340. GFP_KERNEL);
  5341. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5342. rc = -ENOMEM;
  5343. goto err_exit;
  5344. }
  5345. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5346. phba->sli4_hba.xri_ids = kcalloc(rsrc_id_cnt,
  5347. sizeof(uint16_t),
  5348. GFP_KERNEL);
  5349. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5350. kfree(phba->sli4_hba.xri_bmask);
  5351. rc = -ENOMEM;
  5352. goto err_exit;
  5353. }
  5354. /* Initialize local ptrs for common extent processing later. */
  5355. bmask = phba->sli4_hba.xri_bmask;
  5356. ids = phba->sli4_hba.xri_ids;
  5357. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  5358. break;
  5359. case LPFC_RSC_TYPE_FCOE_VFI:
  5360. phba->sli4_hba.vfi_bmask = kcalloc(longs,
  5361. sizeof(unsigned long),
  5362. GFP_KERNEL);
  5363. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5364. rc = -ENOMEM;
  5365. goto err_exit;
  5366. }
  5367. phba->sli4_hba.vfi_ids = kcalloc(rsrc_id_cnt,
  5368. sizeof(uint16_t),
  5369. GFP_KERNEL);
  5370. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5371. kfree(phba->sli4_hba.vfi_bmask);
  5372. rc = -ENOMEM;
  5373. goto err_exit;
  5374. }
  5375. /* Initialize local ptrs for common extent processing later. */
  5376. bmask = phba->sli4_hba.vfi_bmask;
  5377. ids = phba->sli4_hba.vfi_ids;
  5378. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  5379. break;
  5380. default:
  5381. /* Unsupported Opcode. Fail call. */
  5382. id_array = NULL;
  5383. bmask = NULL;
  5384. ids = NULL;
  5385. ext_blk_list = NULL;
  5386. goto err_exit;
  5387. }
  5388. /*
  5389. * Complete initializing the extent configuration with the
  5390. * allocated ids assigned to this function. The bitmask serves
  5391. * as an index into the array and manages the available ids. The
  5392. * array just stores the ids communicated to the port via the wqes.
  5393. */
  5394. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  5395. if ((i % 2) == 0)
  5396. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  5397. &id_array[k]);
  5398. else
  5399. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  5400. &id_array[k]);
  5401. rsrc_blks = kzalloc(length, GFP_KERNEL);
  5402. if (unlikely(!rsrc_blks)) {
  5403. rc = -ENOMEM;
  5404. kfree(bmask);
  5405. kfree(ids);
  5406. goto err_exit;
  5407. }
  5408. rsrc_blks->rsrc_start = rsrc_id;
  5409. rsrc_blks->rsrc_size = rsrc_size;
  5410. list_add_tail(&rsrc_blks->list, ext_blk_list);
  5411. rsrc_start = rsrc_id;
  5412. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0)) {
  5413. phba->sli4_hba.scsi_xri_start = rsrc_start +
  5414. lpfc_sli4_get_iocb_cnt(phba);
  5415. phba->sli4_hba.nvme_xri_start =
  5416. phba->sli4_hba.scsi_xri_start +
  5417. phba->sli4_hba.scsi_xri_max;
  5418. }
  5419. while (rsrc_id < (rsrc_start + rsrc_size)) {
  5420. ids[j] = rsrc_id;
  5421. rsrc_id++;
  5422. j++;
  5423. }
  5424. /* Entire word processed. Get next word.*/
  5425. if ((i % 2) == 1)
  5426. k++;
  5427. }
  5428. err_exit:
  5429. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5430. return rc;
  5431. }
  5432. /**
  5433. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  5434. * @phba: Pointer to HBA context object.
  5435. * @type: the extent's type.
  5436. *
  5437. * This function deallocates all extents of a particular resource type.
  5438. * SLI4 does not allow for deallocating a particular extent range. It
  5439. * is the caller's responsibility to release all kernel memory resources.
  5440. **/
  5441. static int
  5442. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  5443. {
  5444. int rc;
  5445. uint32_t length, mbox_tmo = 0;
  5446. LPFC_MBOXQ_t *mbox;
  5447. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  5448. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  5449. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5450. if (!mbox)
  5451. return -ENOMEM;
  5452. /*
  5453. * This function sends an embedded mailbox because it only sends the
  5454. * the resource type. All extents of this type are released by the
  5455. * port.
  5456. */
  5457. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  5458. sizeof(struct lpfc_sli4_cfg_mhdr));
  5459. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5460. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  5461. length, LPFC_SLI4_MBX_EMBED);
  5462. /* Send an extents count of 0 - the dealloc doesn't use it. */
  5463. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  5464. LPFC_SLI4_MBX_EMBED);
  5465. if (unlikely(rc)) {
  5466. rc = -EIO;
  5467. goto out_free_mbox;
  5468. }
  5469. if (!phba->sli4_hba.intr_enable)
  5470. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5471. else {
  5472. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5473. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5474. }
  5475. if (unlikely(rc)) {
  5476. rc = -EIO;
  5477. goto out_free_mbox;
  5478. }
  5479. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  5480. if (bf_get(lpfc_mbox_hdr_status,
  5481. &dealloc_rsrc->header.cfg_shdr.response)) {
  5482. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5483. "2919 Failed to release resource extents "
  5484. "for type %d - Status 0x%x Add'l Status 0x%x. "
  5485. "Resource memory not released.\n",
  5486. type,
  5487. bf_get(lpfc_mbox_hdr_status,
  5488. &dealloc_rsrc->header.cfg_shdr.response),
  5489. bf_get(lpfc_mbox_hdr_add_status,
  5490. &dealloc_rsrc->header.cfg_shdr.response));
  5491. rc = -EIO;
  5492. goto out_free_mbox;
  5493. }
  5494. /* Release kernel memory resources for the specific type. */
  5495. switch (type) {
  5496. case LPFC_RSC_TYPE_FCOE_VPI:
  5497. kfree(phba->vpi_bmask);
  5498. kfree(phba->vpi_ids);
  5499. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5500. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5501. &phba->lpfc_vpi_blk_list, list) {
  5502. list_del_init(&rsrc_blk->list);
  5503. kfree(rsrc_blk);
  5504. }
  5505. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5506. break;
  5507. case LPFC_RSC_TYPE_FCOE_XRI:
  5508. kfree(phba->sli4_hba.xri_bmask);
  5509. kfree(phba->sli4_hba.xri_ids);
  5510. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5511. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  5512. list_del_init(&rsrc_blk->list);
  5513. kfree(rsrc_blk);
  5514. }
  5515. break;
  5516. case LPFC_RSC_TYPE_FCOE_VFI:
  5517. kfree(phba->sli4_hba.vfi_bmask);
  5518. kfree(phba->sli4_hba.vfi_ids);
  5519. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5520. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5521. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  5522. list_del_init(&rsrc_blk->list);
  5523. kfree(rsrc_blk);
  5524. }
  5525. break;
  5526. case LPFC_RSC_TYPE_FCOE_RPI:
  5527. /* RPI bitmask and physical id array are cleaned up earlier. */
  5528. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5529. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  5530. list_del_init(&rsrc_blk->list);
  5531. kfree(rsrc_blk);
  5532. }
  5533. break;
  5534. default:
  5535. break;
  5536. }
  5537. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5538. out_free_mbox:
  5539. mempool_free(mbox, phba->mbox_mem_pool);
  5540. return rc;
  5541. }
  5542. static void
  5543. lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
  5544. uint32_t feature)
  5545. {
  5546. uint32_t len;
  5547. len = sizeof(struct lpfc_mbx_set_feature) -
  5548. sizeof(struct lpfc_sli4_cfg_mhdr);
  5549. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5550. LPFC_MBOX_OPCODE_SET_FEATURES, len,
  5551. LPFC_SLI4_MBX_EMBED);
  5552. switch (feature) {
  5553. case LPFC_SET_UE_RECOVERY:
  5554. bf_set(lpfc_mbx_set_feature_UER,
  5555. &mbox->u.mqe.un.set_feature, 1);
  5556. mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
  5557. mbox->u.mqe.un.set_feature.param_len = 8;
  5558. break;
  5559. case LPFC_SET_MDS_DIAGS:
  5560. bf_set(lpfc_mbx_set_feature_mds,
  5561. &mbox->u.mqe.un.set_feature, 1);
  5562. bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
  5563. &mbox->u.mqe.un.set_feature, 1);
  5564. mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
  5565. mbox->u.mqe.un.set_feature.param_len = 8;
  5566. break;
  5567. }
  5568. return;
  5569. }
  5570. /**
  5571. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  5572. * @phba: Pointer to HBA context object.
  5573. *
  5574. * This function allocates all SLI4 resource identifiers.
  5575. **/
  5576. int
  5577. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  5578. {
  5579. int i, rc, error = 0;
  5580. uint16_t count, base;
  5581. unsigned long longs;
  5582. if (!phba->sli4_hba.rpi_hdrs_in_use)
  5583. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  5584. if (phba->sli4_hba.extents_in_use) {
  5585. /*
  5586. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5587. * resource extent count must be read and allocated before
  5588. * provisioning the resource id arrays.
  5589. */
  5590. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5591. LPFC_IDX_RSRC_RDY) {
  5592. /*
  5593. * Extent-based resources are set - the driver could
  5594. * be in a port reset. Figure out if any corrective
  5595. * actions need to be taken.
  5596. */
  5597. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5598. LPFC_RSC_TYPE_FCOE_VFI);
  5599. if (rc != 0)
  5600. error++;
  5601. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5602. LPFC_RSC_TYPE_FCOE_VPI);
  5603. if (rc != 0)
  5604. error++;
  5605. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5606. LPFC_RSC_TYPE_FCOE_XRI);
  5607. if (rc != 0)
  5608. error++;
  5609. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5610. LPFC_RSC_TYPE_FCOE_RPI);
  5611. if (rc != 0)
  5612. error++;
  5613. /*
  5614. * It's possible that the number of resources
  5615. * provided to this port instance changed between
  5616. * resets. Detect this condition and reallocate
  5617. * resources. Otherwise, there is no action.
  5618. */
  5619. if (error) {
  5620. lpfc_printf_log(phba, KERN_INFO,
  5621. LOG_MBOX | LOG_INIT,
  5622. "2931 Detected extent resource "
  5623. "change. Reallocating all "
  5624. "extents.\n");
  5625. rc = lpfc_sli4_dealloc_extent(phba,
  5626. LPFC_RSC_TYPE_FCOE_VFI);
  5627. rc = lpfc_sli4_dealloc_extent(phba,
  5628. LPFC_RSC_TYPE_FCOE_VPI);
  5629. rc = lpfc_sli4_dealloc_extent(phba,
  5630. LPFC_RSC_TYPE_FCOE_XRI);
  5631. rc = lpfc_sli4_dealloc_extent(phba,
  5632. LPFC_RSC_TYPE_FCOE_RPI);
  5633. } else
  5634. return 0;
  5635. }
  5636. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5637. if (unlikely(rc))
  5638. goto err_exit;
  5639. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5640. if (unlikely(rc))
  5641. goto err_exit;
  5642. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5643. if (unlikely(rc))
  5644. goto err_exit;
  5645. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5646. if (unlikely(rc))
  5647. goto err_exit;
  5648. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5649. LPFC_IDX_RSRC_RDY);
  5650. return rc;
  5651. } else {
  5652. /*
  5653. * The port does not support resource extents. The XRI, VPI,
  5654. * VFI, RPI resource ids were determined from READ_CONFIG.
  5655. * Just allocate the bitmasks and provision the resource id
  5656. * arrays. If a port reset is active, the resources don't
  5657. * need any action - just exit.
  5658. */
  5659. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5660. LPFC_IDX_RSRC_RDY) {
  5661. lpfc_sli4_dealloc_resource_identifiers(phba);
  5662. lpfc_sli4_remove_rpis(phba);
  5663. }
  5664. /* RPIs. */
  5665. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5666. if (count <= 0) {
  5667. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5668. "3279 Invalid provisioning of "
  5669. "rpi:%d\n", count);
  5670. rc = -EINVAL;
  5671. goto err_exit;
  5672. }
  5673. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5674. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5675. phba->sli4_hba.rpi_bmask = kcalloc(longs,
  5676. sizeof(unsigned long),
  5677. GFP_KERNEL);
  5678. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5679. rc = -ENOMEM;
  5680. goto err_exit;
  5681. }
  5682. phba->sli4_hba.rpi_ids = kcalloc(count, sizeof(uint16_t),
  5683. GFP_KERNEL);
  5684. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5685. rc = -ENOMEM;
  5686. goto free_rpi_bmask;
  5687. }
  5688. for (i = 0; i < count; i++)
  5689. phba->sli4_hba.rpi_ids[i] = base + i;
  5690. /* VPIs. */
  5691. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5692. if (count <= 0) {
  5693. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5694. "3280 Invalid provisioning of "
  5695. "vpi:%d\n", count);
  5696. rc = -EINVAL;
  5697. goto free_rpi_ids;
  5698. }
  5699. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5700. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5701. phba->vpi_bmask = kcalloc(longs, sizeof(unsigned long),
  5702. GFP_KERNEL);
  5703. if (unlikely(!phba->vpi_bmask)) {
  5704. rc = -ENOMEM;
  5705. goto free_rpi_ids;
  5706. }
  5707. phba->vpi_ids = kcalloc(count, sizeof(uint16_t),
  5708. GFP_KERNEL);
  5709. if (unlikely(!phba->vpi_ids)) {
  5710. rc = -ENOMEM;
  5711. goto free_vpi_bmask;
  5712. }
  5713. for (i = 0; i < count; i++)
  5714. phba->vpi_ids[i] = base + i;
  5715. /* XRIs. */
  5716. count = phba->sli4_hba.max_cfg_param.max_xri;
  5717. if (count <= 0) {
  5718. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5719. "3281 Invalid provisioning of "
  5720. "xri:%d\n", count);
  5721. rc = -EINVAL;
  5722. goto free_vpi_ids;
  5723. }
  5724. base = phba->sli4_hba.max_cfg_param.xri_base;
  5725. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5726. phba->sli4_hba.xri_bmask = kcalloc(longs,
  5727. sizeof(unsigned long),
  5728. GFP_KERNEL);
  5729. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5730. rc = -ENOMEM;
  5731. goto free_vpi_ids;
  5732. }
  5733. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5734. phba->sli4_hba.xri_ids = kcalloc(count, sizeof(uint16_t),
  5735. GFP_KERNEL);
  5736. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5737. rc = -ENOMEM;
  5738. goto free_xri_bmask;
  5739. }
  5740. for (i = 0; i < count; i++)
  5741. phba->sli4_hba.xri_ids[i] = base + i;
  5742. /* VFIs. */
  5743. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5744. if (count <= 0) {
  5745. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5746. "3282 Invalid provisioning of "
  5747. "vfi:%d\n", count);
  5748. rc = -EINVAL;
  5749. goto free_xri_ids;
  5750. }
  5751. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5752. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5753. phba->sli4_hba.vfi_bmask = kcalloc(longs,
  5754. sizeof(unsigned long),
  5755. GFP_KERNEL);
  5756. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5757. rc = -ENOMEM;
  5758. goto free_xri_ids;
  5759. }
  5760. phba->sli4_hba.vfi_ids = kcalloc(count, sizeof(uint16_t),
  5761. GFP_KERNEL);
  5762. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5763. rc = -ENOMEM;
  5764. goto free_vfi_bmask;
  5765. }
  5766. for (i = 0; i < count; i++)
  5767. phba->sli4_hba.vfi_ids[i] = base + i;
  5768. /*
  5769. * Mark all resources ready. An HBA reset doesn't need
  5770. * to reset the initialization.
  5771. */
  5772. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5773. LPFC_IDX_RSRC_RDY);
  5774. return 0;
  5775. }
  5776. free_vfi_bmask:
  5777. kfree(phba->sli4_hba.vfi_bmask);
  5778. phba->sli4_hba.vfi_bmask = NULL;
  5779. free_xri_ids:
  5780. kfree(phba->sli4_hba.xri_ids);
  5781. phba->sli4_hba.xri_ids = NULL;
  5782. free_xri_bmask:
  5783. kfree(phba->sli4_hba.xri_bmask);
  5784. phba->sli4_hba.xri_bmask = NULL;
  5785. free_vpi_ids:
  5786. kfree(phba->vpi_ids);
  5787. phba->vpi_ids = NULL;
  5788. free_vpi_bmask:
  5789. kfree(phba->vpi_bmask);
  5790. phba->vpi_bmask = NULL;
  5791. free_rpi_ids:
  5792. kfree(phba->sli4_hba.rpi_ids);
  5793. phba->sli4_hba.rpi_ids = NULL;
  5794. free_rpi_bmask:
  5795. kfree(phba->sli4_hba.rpi_bmask);
  5796. phba->sli4_hba.rpi_bmask = NULL;
  5797. err_exit:
  5798. return rc;
  5799. }
  5800. /**
  5801. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5802. * @phba: Pointer to HBA context object.
  5803. *
  5804. * This function allocates the number of elements for the specified
  5805. * resource type.
  5806. **/
  5807. int
  5808. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5809. {
  5810. if (phba->sli4_hba.extents_in_use) {
  5811. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5812. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5813. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5814. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5815. } else {
  5816. kfree(phba->vpi_bmask);
  5817. phba->sli4_hba.max_cfg_param.vpi_used = 0;
  5818. kfree(phba->vpi_ids);
  5819. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5820. kfree(phba->sli4_hba.xri_bmask);
  5821. kfree(phba->sli4_hba.xri_ids);
  5822. kfree(phba->sli4_hba.vfi_bmask);
  5823. kfree(phba->sli4_hba.vfi_ids);
  5824. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5825. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5826. }
  5827. return 0;
  5828. }
  5829. /**
  5830. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5831. * @phba: Pointer to HBA context object.
  5832. * @type: The resource extent type.
  5833. * @extnt_count: buffer to hold port extent count response
  5834. * @extnt_size: buffer to hold port extent size response.
  5835. *
  5836. * This function calls the port to read the host allocated extents
  5837. * for a particular type.
  5838. **/
  5839. int
  5840. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5841. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5842. {
  5843. bool emb;
  5844. int rc = 0;
  5845. uint16_t curr_blks = 0;
  5846. uint32_t req_len, emb_len;
  5847. uint32_t alloc_len, mbox_tmo;
  5848. struct list_head *blk_list_head;
  5849. struct lpfc_rsrc_blks *rsrc_blk;
  5850. LPFC_MBOXQ_t *mbox;
  5851. void *virtaddr = NULL;
  5852. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5853. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5854. union lpfc_sli4_cfg_shdr *shdr;
  5855. switch (type) {
  5856. case LPFC_RSC_TYPE_FCOE_VPI:
  5857. blk_list_head = &phba->lpfc_vpi_blk_list;
  5858. break;
  5859. case LPFC_RSC_TYPE_FCOE_XRI:
  5860. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5861. break;
  5862. case LPFC_RSC_TYPE_FCOE_VFI:
  5863. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5864. break;
  5865. case LPFC_RSC_TYPE_FCOE_RPI:
  5866. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5867. break;
  5868. default:
  5869. return -EIO;
  5870. }
  5871. /* Count the number of extents currently allocatd for this type. */
  5872. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5873. if (curr_blks == 0) {
  5874. /*
  5875. * The GET_ALLOCATED mailbox does not return the size,
  5876. * just the count. The size should be just the size
  5877. * stored in the current allocated block and all sizes
  5878. * for an extent type are the same so set the return
  5879. * value now.
  5880. */
  5881. *extnt_size = rsrc_blk->rsrc_size;
  5882. }
  5883. curr_blks++;
  5884. }
  5885. /*
  5886. * Calculate the size of an embedded mailbox. The uint32_t
  5887. * accounts for extents-specific word.
  5888. */
  5889. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5890. sizeof(uint32_t);
  5891. /*
  5892. * Presume the allocation and response will fit into an embedded
  5893. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5894. */
  5895. emb = LPFC_SLI4_MBX_EMBED;
  5896. req_len = emb_len;
  5897. if (req_len > emb_len) {
  5898. req_len = curr_blks * sizeof(uint16_t) +
  5899. sizeof(union lpfc_sli4_cfg_shdr) +
  5900. sizeof(uint32_t);
  5901. emb = LPFC_SLI4_MBX_NEMBED;
  5902. }
  5903. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5904. if (!mbox)
  5905. return -ENOMEM;
  5906. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5907. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5908. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5909. req_len, emb);
  5910. if (alloc_len < req_len) {
  5911. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5912. "2983 Allocated DMA memory size (x%x) is "
  5913. "less than the requested DMA memory "
  5914. "size (x%x)\n", alloc_len, req_len);
  5915. rc = -ENOMEM;
  5916. goto err_exit;
  5917. }
  5918. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5919. if (unlikely(rc)) {
  5920. rc = -EIO;
  5921. goto err_exit;
  5922. }
  5923. if (!phba->sli4_hba.intr_enable)
  5924. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5925. else {
  5926. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5927. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5928. }
  5929. if (unlikely(rc)) {
  5930. rc = -EIO;
  5931. goto err_exit;
  5932. }
  5933. /*
  5934. * Figure out where the response is located. Then get local pointers
  5935. * to the response data. The port does not guarantee to respond to
  5936. * all extents counts request so update the local variable with the
  5937. * allocated count from the port.
  5938. */
  5939. if (emb == LPFC_SLI4_MBX_EMBED) {
  5940. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5941. shdr = &rsrc_ext->header.cfg_shdr;
  5942. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5943. } else {
  5944. virtaddr = mbox->sge_array->addr[0];
  5945. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5946. shdr = &n_rsrc->cfg_shdr;
  5947. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5948. }
  5949. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5950. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5951. "2984 Failed to read allocated resources "
  5952. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5953. type,
  5954. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5955. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5956. rc = -EIO;
  5957. goto err_exit;
  5958. }
  5959. err_exit:
  5960. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5961. return rc;
  5962. }
  5963. /**
  5964. * lpfc_sli4_repost_sgl_list - Repost the buffers sgl pages as block
  5965. * @phba: pointer to lpfc hba data structure.
  5966. * @pring: Pointer to driver SLI ring object.
  5967. * @sgl_list: linked link of sgl buffers to post
  5968. * @cnt: number of linked list buffers
  5969. *
  5970. * This routine walks the list of buffers that have been allocated and
  5971. * repost them to the port by using SGL block post. This is needed after a
  5972. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5973. * of buffer sgls which contains contiguous xris and uses the non-embedded
  5974. * SGL block post mailbox commands to post them to the port. For single
  5975. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5976. * mailbox command for posting.
  5977. *
  5978. * Returns: 0 = success, non-zero failure.
  5979. **/
  5980. static int
  5981. lpfc_sli4_repost_sgl_list(struct lpfc_hba *phba,
  5982. struct list_head *sgl_list, int cnt)
  5983. {
  5984. struct lpfc_sglq *sglq_entry = NULL;
  5985. struct lpfc_sglq *sglq_entry_next = NULL;
  5986. struct lpfc_sglq *sglq_entry_first = NULL;
  5987. int status, total_cnt;
  5988. int post_cnt = 0, num_posted = 0, block_cnt = 0;
  5989. int last_xritag = NO_XRI;
  5990. LIST_HEAD(prep_sgl_list);
  5991. LIST_HEAD(blck_sgl_list);
  5992. LIST_HEAD(allc_sgl_list);
  5993. LIST_HEAD(post_sgl_list);
  5994. LIST_HEAD(free_sgl_list);
  5995. spin_lock_irq(&phba->hbalock);
  5996. spin_lock(&phba->sli4_hba.sgl_list_lock);
  5997. list_splice_init(sgl_list, &allc_sgl_list);
  5998. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  5999. spin_unlock_irq(&phba->hbalock);
  6000. total_cnt = cnt;
  6001. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  6002. &allc_sgl_list, list) {
  6003. list_del_init(&sglq_entry->list);
  6004. block_cnt++;
  6005. if ((last_xritag != NO_XRI) &&
  6006. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  6007. /* a hole in xri block, form a sgl posting block */
  6008. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  6009. post_cnt = block_cnt - 1;
  6010. /* prepare list for next posting block */
  6011. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  6012. block_cnt = 1;
  6013. } else {
  6014. /* prepare list for next posting block */
  6015. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  6016. /* enough sgls for non-embed sgl mbox command */
  6017. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  6018. list_splice_init(&prep_sgl_list,
  6019. &blck_sgl_list);
  6020. post_cnt = block_cnt;
  6021. block_cnt = 0;
  6022. }
  6023. }
  6024. num_posted++;
  6025. /* keep track of last sgl's xritag */
  6026. last_xritag = sglq_entry->sli4_xritag;
  6027. /* end of repost sgl list condition for buffers */
  6028. if (num_posted == total_cnt) {
  6029. if (post_cnt == 0) {
  6030. list_splice_init(&prep_sgl_list,
  6031. &blck_sgl_list);
  6032. post_cnt = block_cnt;
  6033. } else if (block_cnt == 1) {
  6034. status = lpfc_sli4_post_sgl(phba,
  6035. sglq_entry->phys, 0,
  6036. sglq_entry->sli4_xritag);
  6037. if (!status) {
  6038. /* successful, put sgl to posted list */
  6039. list_add_tail(&sglq_entry->list,
  6040. &post_sgl_list);
  6041. } else {
  6042. /* Failure, put sgl to free list */
  6043. lpfc_printf_log(phba, KERN_WARNING,
  6044. LOG_SLI,
  6045. "3159 Failed to post "
  6046. "sgl, xritag:x%x\n",
  6047. sglq_entry->sli4_xritag);
  6048. list_add_tail(&sglq_entry->list,
  6049. &free_sgl_list);
  6050. total_cnt--;
  6051. }
  6052. }
  6053. }
  6054. /* continue until a nembed page worth of sgls */
  6055. if (post_cnt == 0)
  6056. continue;
  6057. /* post the buffer list sgls as a block */
  6058. status = lpfc_sli4_post_sgl_list(phba, &blck_sgl_list,
  6059. post_cnt);
  6060. if (!status) {
  6061. /* success, put sgl list to posted sgl list */
  6062. list_splice_init(&blck_sgl_list, &post_sgl_list);
  6063. } else {
  6064. /* Failure, put sgl list to free sgl list */
  6065. sglq_entry_first = list_first_entry(&blck_sgl_list,
  6066. struct lpfc_sglq,
  6067. list);
  6068. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  6069. "3160 Failed to post sgl-list, "
  6070. "xritag:x%x-x%x\n",
  6071. sglq_entry_first->sli4_xritag,
  6072. (sglq_entry_first->sli4_xritag +
  6073. post_cnt - 1));
  6074. list_splice_init(&blck_sgl_list, &free_sgl_list);
  6075. total_cnt -= post_cnt;
  6076. }
  6077. /* don't reset xirtag due to hole in xri block */
  6078. if (block_cnt == 0)
  6079. last_xritag = NO_XRI;
  6080. /* reset sgl post count for next round of posting */
  6081. post_cnt = 0;
  6082. }
  6083. /* free the sgls failed to post */
  6084. lpfc_free_sgl_list(phba, &free_sgl_list);
  6085. /* push sgls posted to the available list */
  6086. if (!list_empty(&post_sgl_list)) {
  6087. spin_lock_irq(&phba->hbalock);
  6088. spin_lock(&phba->sli4_hba.sgl_list_lock);
  6089. list_splice_init(&post_sgl_list, sgl_list);
  6090. spin_unlock(&phba->sli4_hba.sgl_list_lock);
  6091. spin_unlock_irq(&phba->hbalock);
  6092. } else {
  6093. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6094. "3161 Failure to post sgl to port.\n");
  6095. return -EIO;
  6096. }
  6097. /* return the number of XRIs actually posted */
  6098. return total_cnt;
  6099. }
  6100. void
  6101. lpfc_set_host_data(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  6102. {
  6103. uint32_t len;
  6104. len = sizeof(struct lpfc_mbx_set_host_data) -
  6105. sizeof(struct lpfc_sli4_cfg_mhdr);
  6106. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  6107. LPFC_MBOX_OPCODE_SET_HOST_DATA, len,
  6108. LPFC_SLI4_MBX_EMBED);
  6109. mbox->u.mqe.un.set_host_data.param_id = LPFC_SET_HOST_OS_DRIVER_VERSION;
  6110. mbox->u.mqe.un.set_host_data.param_len =
  6111. LPFC_HOST_OS_DRIVER_VERSION_SIZE;
  6112. snprintf(mbox->u.mqe.un.set_host_data.data,
  6113. LPFC_HOST_OS_DRIVER_VERSION_SIZE,
  6114. "Linux %s v"LPFC_DRIVER_VERSION,
  6115. (phba->hba_flag & HBA_FCOE_MODE) ? "FCoE" : "FC");
  6116. }
  6117. int
  6118. lpfc_post_rq_buffer(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  6119. struct lpfc_queue *drq, int count, int idx)
  6120. {
  6121. int rc, i;
  6122. struct lpfc_rqe hrqe;
  6123. struct lpfc_rqe drqe;
  6124. struct lpfc_rqb *rqbp;
  6125. unsigned long flags;
  6126. struct rqb_dmabuf *rqb_buffer;
  6127. LIST_HEAD(rqb_buf_list);
  6128. rqbp = hrq->rqbp;
  6129. for (i = 0; i < count; i++) {
  6130. spin_lock_irqsave(&phba->hbalock, flags);
  6131. /* IF RQ is already full, don't bother */
  6132. if (rqbp->buffer_count + i >= rqbp->entry_count - 1) {
  6133. spin_unlock_irqrestore(&phba->hbalock, flags);
  6134. break;
  6135. }
  6136. spin_unlock_irqrestore(&phba->hbalock, flags);
  6137. rqb_buffer = rqbp->rqb_alloc_buffer(phba);
  6138. if (!rqb_buffer)
  6139. break;
  6140. rqb_buffer->hrq = hrq;
  6141. rqb_buffer->drq = drq;
  6142. rqb_buffer->idx = idx;
  6143. list_add_tail(&rqb_buffer->hbuf.list, &rqb_buf_list);
  6144. }
  6145. spin_lock_irqsave(&phba->hbalock, flags);
  6146. while (!list_empty(&rqb_buf_list)) {
  6147. list_remove_head(&rqb_buf_list, rqb_buffer, struct rqb_dmabuf,
  6148. hbuf.list);
  6149. hrqe.address_lo = putPaddrLow(rqb_buffer->hbuf.phys);
  6150. hrqe.address_hi = putPaddrHigh(rqb_buffer->hbuf.phys);
  6151. drqe.address_lo = putPaddrLow(rqb_buffer->dbuf.phys);
  6152. drqe.address_hi = putPaddrHigh(rqb_buffer->dbuf.phys);
  6153. rc = lpfc_sli4_rq_put(hrq, drq, &hrqe, &drqe);
  6154. if (rc < 0) {
  6155. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6156. "6421 Cannot post to HRQ %d: %x %x %x "
  6157. "DRQ %x %x\n",
  6158. hrq->queue_id,
  6159. hrq->host_index,
  6160. hrq->hba_index,
  6161. hrq->entry_count,
  6162. drq->host_index,
  6163. drq->hba_index);
  6164. rqbp->rqb_free_buffer(phba, rqb_buffer);
  6165. } else {
  6166. list_add_tail(&rqb_buffer->hbuf.list,
  6167. &rqbp->rqb_buffer_list);
  6168. rqbp->buffer_count++;
  6169. }
  6170. }
  6171. spin_unlock_irqrestore(&phba->hbalock, flags);
  6172. return 1;
  6173. }
  6174. /**
  6175. * lpfc_sli4_hba_setup - SLI4 device initialization PCI function
  6176. * @phba: Pointer to HBA context object.
  6177. *
  6178. * This function is the main SLI4 device initialization PCI function. This
  6179. * function is called by the HBA initialization code, HBA reset code and
  6180. * HBA error attention handler code. Caller is not required to hold any
  6181. * locks.
  6182. **/
  6183. int
  6184. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  6185. {
  6186. int rc, i, cnt;
  6187. LPFC_MBOXQ_t *mboxq;
  6188. struct lpfc_mqe *mqe;
  6189. uint8_t *vpd;
  6190. uint32_t vpd_size;
  6191. uint32_t ftr_rsp = 0;
  6192. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  6193. struct lpfc_vport *vport = phba->pport;
  6194. struct lpfc_dmabuf *mp;
  6195. struct lpfc_rqb *rqbp;
  6196. /* Perform a PCI function reset to start from clean */
  6197. rc = lpfc_pci_function_reset(phba);
  6198. if (unlikely(rc))
  6199. return -ENODEV;
  6200. /* Check the HBA Host Status Register for readyness */
  6201. rc = lpfc_sli4_post_status_check(phba);
  6202. if (unlikely(rc))
  6203. return -ENODEV;
  6204. else {
  6205. spin_lock_irq(&phba->hbalock);
  6206. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  6207. spin_unlock_irq(&phba->hbalock);
  6208. }
  6209. /*
  6210. * Allocate a single mailbox container for initializing the
  6211. * port.
  6212. */
  6213. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  6214. if (!mboxq)
  6215. return -ENOMEM;
  6216. /* Issue READ_REV to collect vpd and FW information. */
  6217. vpd_size = SLI4_PAGE_SIZE;
  6218. vpd = kzalloc(vpd_size, GFP_KERNEL);
  6219. if (!vpd) {
  6220. rc = -ENOMEM;
  6221. goto out_free_mbox;
  6222. }
  6223. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  6224. if (unlikely(rc)) {
  6225. kfree(vpd);
  6226. goto out_free_mbox;
  6227. }
  6228. mqe = &mboxq->u.mqe;
  6229. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  6230. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
  6231. phba->hba_flag |= HBA_FCOE_MODE;
  6232. phba->fcp_embed_io = 0; /* SLI4 FC support only */
  6233. } else {
  6234. phba->hba_flag &= ~HBA_FCOE_MODE;
  6235. }
  6236. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  6237. LPFC_DCBX_CEE_MODE)
  6238. phba->hba_flag |= HBA_FIP_SUPPORT;
  6239. else
  6240. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  6241. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  6242. if (phba->sli_rev != LPFC_SLI_REV4) {
  6243. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6244. "0376 READ_REV Error. SLI Level %d "
  6245. "FCoE enabled %d\n",
  6246. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  6247. rc = -EIO;
  6248. kfree(vpd);
  6249. goto out_free_mbox;
  6250. }
  6251. /*
  6252. * Continue initialization with default values even if driver failed
  6253. * to read FCoE param config regions, only read parameters if the
  6254. * board is FCoE
  6255. */
  6256. if (phba->hba_flag & HBA_FCOE_MODE &&
  6257. lpfc_sli4_read_fcoe_params(phba))
  6258. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  6259. "2570 Failed to read FCoE parameters\n");
  6260. /*
  6261. * Retrieve sli4 device physical port name, failure of doing it
  6262. * is considered as non-fatal.
  6263. */
  6264. rc = lpfc_sli4_retrieve_pport_name(phba);
  6265. if (!rc)
  6266. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6267. "3080 Successful retrieving SLI4 device "
  6268. "physical port name: %s.\n", phba->Port);
  6269. /*
  6270. * Evaluate the read rev and vpd data. Populate the driver
  6271. * state with the results. If this routine fails, the failure
  6272. * is not fatal as the driver will use generic values.
  6273. */
  6274. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  6275. if (unlikely(!rc)) {
  6276. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6277. "0377 Error %d parsing vpd. "
  6278. "Using defaults.\n", rc);
  6279. rc = 0;
  6280. }
  6281. kfree(vpd);
  6282. /* Save information as VPD data */
  6283. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  6284. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  6285. /*
  6286. * This is because first G7 ASIC doesn't support the standard
  6287. * 0x5a NVME cmd descriptor type/subtype
  6288. */
  6289. if ((bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  6290. LPFC_SLI_INTF_IF_TYPE_6) &&
  6291. (phba->vpd.rev.biuRev == LPFC_G7_ASIC_1) &&
  6292. (phba->vpd.rev.smRev == 0) &&
  6293. (phba->cfg_nvme_embed_cmd == 1))
  6294. phba->cfg_nvme_embed_cmd = 0;
  6295. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  6296. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  6297. &mqe->un.read_rev);
  6298. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  6299. &mqe->un.read_rev);
  6300. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  6301. &mqe->un.read_rev);
  6302. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  6303. &mqe->un.read_rev);
  6304. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  6305. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  6306. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  6307. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  6308. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  6309. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  6310. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6311. "(%d):0380 READ_REV Status x%x "
  6312. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  6313. mboxq->vport ? mboxq->vport->vpi : 0,
  6314. bf_get(lpfc_mqe_status, mqe),
  6315. phba->vpd.rev.opFwName,
  6316. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  6317. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  6318. /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
  6319. rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
  6320. if (phba->pport->cfg_lun_queue_depth > rc) {
  6321. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  6322. "3362 LUN queue depth changed from %d to %d\n",
  6323. phba->pport->cfg_lun_queue_depth, rc);
  6324. phba->pport->cfg_lun_queue_depth = rc;
  6325. }
  6326. if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
  6327. LPFC_SLI_INTF_IF_TYPE_0) {
  6328. lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
  6329. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6330. if (rc == MBX_SUCCESS) {
  6331. phba->hba_flag |= HBA_RECOVERABLE_UE;
  6332. /* Set 1Sec interval to detect UE */
  6333. phba->eratt_poll_interval = 1;
  6334. phba->sli4_hba.ue_to_sr = bf_get(
  6335. lpfc_mbx_set_feature_UESR,
  6336. &mboxq->u.mqe.un.set_feature);
  6337. phba->sli4_hba.ue_to_rp = bf_get(
  6338. lpfc_mbx_set_feature_UERP,
  6339. &mboxq->u.mqe.un.set_feature);
  6340. }
  6341. }
  6342. if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
  6343. /* Enable MDS Diagnostics only if the SLI Port supports it */
  6344. lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
  6345. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6346. if (rc != MBX_SUCCESS)
  6347. phba->mds_diags_support = 0;
  6348. }
  6349. /*
  6350. * Discover the port's supported feature set and match it against the
  6351. * hosts requests.
  6352. */
  6353. lpfc_request_features(phba, mboxq);
  6354. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6355. if (unlikely(rc)) {
  6356. rc = -EIO;
  6357. goto out_free_mbox;
  6358. }
  6359. /*
  6360. * The port must support FCP initiator mode as this is the
  6361. * only mode running in the host.
  6362. */
  6363. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  6364. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6365. "0378 No support for fcpi mode.\n");
  6366. ftr_rsp++;
  6367. }
  6368. /* Performance Hints are ONLY for FCoE */
  6369. if (phba->hba_flag & HBA_FCOE_MODE) {
  6370. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  6371. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  6372. else
  6373. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  6374. }
  6375. /*
  6376. * If the port cannot support the host's requested features
  6377. * then turn off the global config parameters to disable the
  6378. * feature in the driver. This is not a fatal error.
  6379. */
  6380. if (phba->sli3_options & LPFC_SLI3_BG_ENABLED) {
  6381. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))) {
  6382. phba->cfg_enable_bg = 0;
  6383. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  6384. ftr_rsp++;
  6385. }
  6386. }
  6387. if (phba->max_vpi && phba->cfg_enable_npiv &&
  6388. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  6389. ftr_rsp++;
  6390. if (ftr_rsp) {
  6391. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6392. "0379 Feature Mismatch Data: x%08x %08x "
  6393. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  6394. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  6395. phba->cfg_enable_npiv, phba->max_vpi);
  6396. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  6397. phba->cfg_enable_bg = 0;
  6398. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  6399. phba->cfg_enable_npiv = 0;
  6400. }
  6401. /* These SLI3 features are assumed in SLI4 */
  6402. spin_lock_irq(&phba->hbalock);
  6403. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  6404. spin_unlock_irq(&phba->hbalock);
  6405. /*
  6406. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  6407. * calls depends on these resources to complete port setup.
  6408. */
  6409. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  6410. if (rc) {
  6411. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6412. "2920 Failed to alloc Resource IDs "
  6413. "rc = x%x\n", rc);
  6414. goto out_free_mbox;
  6415. }
  6416. lpfc_set_host_data(phba, mboxq);
  6417. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6418. if (rc) {
  6419. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6420. "2134 Failed to set host os driver version %x",
  6421. rc);
  6422. }
  6423. /* Read the port's service parameters. */
  6424. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  6425. if (rc) {
  6426. phba->link_state = LPFC_HBA_ERROR;
  6427. rc = -ENOMEM;
  6428. goto out_free_mbox;
  6429. }
  6430. mboxq->vport = vport;
  6431. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6432. mp = (struct lpfc_dmabuf *) mboxq->context1;
  6433. if (rc == MBX_SUCCESS) {
  6434. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  6435. rc = 0;
  6436. }
  6437. /*
  6438. * This memory was allocated by the lpfc_read_sparam routine. Release
  6439. * it to the mbuf pool.
  6440. */
  6441. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  6442. kfree(mp);
  6443. mboxq->context1 = NULL;
  6444. if (unlikely(rc)) {
  6445. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6446. "0382 READ_SPARAM command failed "
  6447. "status %d, mbxStatus x%x\n",
  6448. rc, bf_get(lpfc_mqe_status, mqe));
  6449. phba->link_state = LPFC_HBA_ERROR;
  6450. rc = -EIO;
  6451. goto out_free_mbox;
  6452. }
  6453. lpfc_update_vport_wwn(vport);
  6454. /* Update the fc_host data structures with new wwn. */
  6455. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  6456. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  6457. /* Create all the SLI4 queues */
  6458. rc = lpfc_sli4_queue_create(phba);
  6459. if (rc) {
  6460. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6461. "3089 Failed to allocate queues\n");
  6462. rc = -ENODEV;
  6463. goto out_free_mbox;
  6464. }
  6465. /* Set up all the queues to the device */
  6466. rc = lpfc_sli4_queue_setup(phba);
  6467. if (unlikely(rc)) {
  6468. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6469. "0381 Error %d during queue setup.\n ", rc);
  6470. goto out_stop_timers;
  6471. }
  6472. /* Initialize the driver internal SLI layer lists. */
  6473. lpfc_sli4_setup(phba);
  6474. lpfc_sli4_queue_init(phba);
  6475. /* update host els xri-sgl sizes and mappings */
  6476. rc = lpfc_sli4_els_sgl_update(phba);
  6477. if (unlikely(rc)) {
  6478. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6479. "1400 Failed to update xri-sgl size and "
  6480. "mapping: %d\n", rc);
  6481. goto out_destroy_queue;
  6482. }
  6483. /* register the els sgl pool to the port */
  6484. rc = lpfc_sli4_repost_sgl_list(phba, &phba->sli4_hba.lpfc_els_sgl_list,
  6485. phba->sli4_hba.els_xri_cnt);
  6486. if (unlikely(rc < 0)) {
  6487. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6488. "0582 Error %d during els sgl post "
  6489. "operation\n", rc);
  6490. rc = -ENODEV;
  6491. goto out_destroy_queue;
  6492. }
  6493. phba->sli4_hba.els_xri_cnt = rc;
  6494. if (phba->nvmet_support) {
  6495. /* update host nvmet xri-sgl sizes and mappings */
  6496. rc = lpfc_sli4_nvmet_sgl_update(phba);
  6497. if (unlikely(rc)) {
  6498. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6499. "6308 Failed to update nvmet-sgl size "
  6500. "and mapping: %d\n", rc);
  6501. goto out_destroy_queue;
  6502. }
  6503. /* register the nvmet sgl pool to the port */
  6504. rc = lpfc_sli4_repost_sgl_list(
  6505. phba,
  6506. &phba->sli4_hba.lpfc_nvmet_sgl_list,
  6507. phba->sli4_hba.nvmet_xri_cnt);
  6508. if (unlikely(rc < 0)) {
  6509. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6510. "3117 Error %d during nvmet "
  6511. "sgl post\n", rc);
  6512. rc = -ENODEV;
  6513. goto out_destroy_queue;
  6514. }
  6515. phba->sli4_hba.nvmet_xri_cnt = rc;
  6516. cnt = phba->cfg_iocb_cnt * 1024;
  6517. /* We need 1 iocbq for every SGL, for IO processing */
  6518. cnt += phba->sli4_hba.nvmet_xri_cnt;
  6519. } else {
  6520. /* update host scsi xri-sgl sizes and mappings */
  6521. rc = lpfc_sli4_scsi_sgl_update(phba);
  6522. if (unlikely(rc)) {
  6523. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6524. "6309 Failed to update scsi-sgl size "
  6525. "and mapping: %d\n", rc);
  6526. goto out_destroy_queue;
  6527. }
  6528. /* update host nvme xri-sgl sizes and mappings */
  6529. rc = lpfc_sli4_nvme_sgl_update(phba);
  6530. if (unlikely(rc)) {
  6531. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6532. "6082 Failed to update nvme-sgl size "
  6533. "and mapping: %d\n", rc);
  6534. goto out_destroy_queue;
  6535. }
  6536. cnt = phba->cfg_iocb_cnt * 1024;
  6537. }
  6538. if (!phba->sli.iocbq_lookup) {
  6539. /* Initialize and populate the iocb list per host */
  6540. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6541. "2821 initialize iocb list %d total %d\n",
  6542. phba->cfg_iocb_cnt, cnt);
  6543. rc = lpfc_init_iocb_list(phba, cnt);
  6544. if (rc) {
  6545. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6546. "1413 Failed to init iocb list.\n");
  6547. goto out_destroy_queue;
  6548. }
  6549. }
  6550. if (phba->nvmet_support)
  6551. lpfc_nvmet_create_targetport(phba);
  6552. if (phba->nvmet_support && phba->cfg_nvmet_mrq) {
  6553. /* Post initial buffers to all RQs created */
  6554. for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
  6555. rqbp = phba->sli4_hba.nvmet_mrq_hdr[i]->rqbp;
  6556. INIT_LIST_HEAD(&rqbp->rqb_buffer_list);
  6557. rqbp->rqb_alloc_buffer = lpfc_sli4_nvmet_alloc;
  6558. rqbp->rqb_free_buffer = lpfc_sli4_nvmet_free;
  6559. rqbp->entry_count = LPFC_NVMET_RQE_DEF_COUNT;
  6560. rqbp->buffer_count = 0;
  6561. lpfc_post_rq_buffer(
  6562. phba, phba->sli4_hba.nvmet_mrq_hdr[i],
  6563. phba->sli4_hba.nvmet_mrq_data[i],
  6564. phba->cfg_nvmet_mrq_post, i);
  6565. }
  6566. }
  6567. if (phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP) {
  6568. /* register the allocated scsi sgl pool to the port */
  6569. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  6570. if (unlikely(rc)) {
  6571. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6572. "0383 Error %d during scsi sgl post "
  6573. "operation\n", rc);
  6574. /* Some Scsi buffers were moved to abort scsi list */
  6575. /* A pci function reset will repost them */
  6576. rc = -ENODEV;
  6577. goto out_destroy_queue;
  6578. }
  6579. }
  6580. if ((phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME) &&
  6581. (phba->nvmet_support == 0)) {
  6582. /* register the allocated nvme sgl pool to the port */
  6583. rc = lpfc_repost_nvme_sgl_list(phba);
  6584. if (unlikely(rc)) {
  6585. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6586. "6116 Error %d during nvme sgl post "
  6587. "operation\n", rc);
  6588. /* Some NVME buffers were moved to abort nvme list */
  6589. /* A pci function reset will repost them */
  6590. rc = -ENODEV;
  6591. goto out_destroy_queue;
  6592. }
  6593. }
  6594. /* Post the rpi header region to the device. */
  6595. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  6596. if (unlikely(rc)) {
  6597. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6598. "0393 Error %d during rpi post operation\n",
  6599. rc);
  6600. rc = -ENODEV;
  6601. goto out_destroy_queue;
  6602. }
  6603. lpfc_sli4_node_prep(phba);
  6604. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  6605. if ((phba->nvmet_support == 0) || (phba->cfg_nvmet_mrq == 1)) {
  6606. /*
  6607. * The FC Port needs to register FCFI (index 0)
  6608. */
  6609. lpfc_reg_fcfi(phba, mboxq);
  6610. mboxq->vport = phba->pport;
  6611. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6612. if (rc != MBX_SUCCESS)
  6613. goto out_unset_queue;
  6614. rc = 0;
  6615. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  6616. &mboxq->u.mqe.un.reg_fcfi);
  6617. } else {
  6618. /* We are a NVME Target mode with MRQ > 1 */
  6619. /* First register the FCFI */
  6620. lpfc_reg_fcfi_mrq(phba, mboxq, 0);
  6621. mboxq->vport = phba->pport;
  6622. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6623. if (rc != MBX_SUCCESS)
  6624. goto out_unset_queue;
  6625. rc = 0;
  6626. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_mrq_fcfi,
  6627. &mboxq->u.mqe.un.reg_fcfi_mrq);
  6628. /* Next register the MRQs */
  6629. lpfc_reg_fcfi_mrq(phba, mboxq, 1);
  6630. mboxq->vport = phba->pport;
  6631. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6632. if (rc != MBX_SUCCESS)
  6633. goto out_unset_queue;
  6634. rc = 0;
  6635. }
  6636. /* Check if the port is configured to be disabled */
  6637. lpfc_sli_read_link_ste(phba);
  6638. }
  6639. /* Arm the CQs and then EQs on device */
  6640. lpfc_sli4_arm_cqeq_intr(phba);
  6641. /* Indicate device interrupt mode */
  6642. phba->sli4_hba.intr_enable = 1;
  6643. /* Allow asynchronous mailbox command to go through */
  6644. spin_lock_irq(&phba->hbalock);
  6645. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6646. spin_unlock_irq(&phba->hbalock);
  6647. /* Post receive buffers to the device */
  6648. lpfc_sli4_rb_setup(phba);
  6649. /* Reset HBA FCF states after HBA reset */
  6650. phba->fcf.fcf_flag = 0;
  6651. phba->fcf.current_rec.flag = 0;
  6652. /* Start the ELS watchdog timer */
  6653. mod_timer(&vport->els_tmofunc,
  6654. jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
  6655. /* Start heart beat timer */
  6656. mod_timer(&phba->hb_tmofunc,
  6657. jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
  6658. phba->hb_outstanding = 0;
  6659. phba->last_completion_time = jiffies;
  6660. /* Start error attention (ERATT) polling timer */
  6661. mod_timer(&phba->eratt_poll,
  6662. jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
  6663. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  6664. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  6665. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  6666. if (!rc) {
  6667. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6668. "2829 This device supports "
  6669. "Advanced Error Reporting (AER)\n");
  6670. spin_lock_irq(&phba->hbalock);
  6671. phba->hba_flag |= HBA_AER_ENABLED;
  6672. spin_unlock_irq(&phba->hbalock);
  6673. } else {
  6674. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  6675. "2830 This device does not support "
  6676. "Advanced Error Reporting (AER)\n");
  6677. phba->cfg_aer_support = 0;
  6678. }
  6679. rc = 0;
  6680. }
  6681. /*
  6682. * The port is ready, set the host's link state to LINK_DOWN
  6683. * in preparation for link interrupts.
  6684. */
  6685. spin_lock_irq(&phba->hbalock);
  6686. phba->link_state = LPFC_LINK_DOWN;
  6687. spin_unlock_irq(&phba->hbalock);
  6688. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  6689. (phba->hba_flag & LINK_DISABLED)) {
  6690. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  6691. "3103 Adapter Link is disabled.\n");
  6692. lpfc_down_link(phba, mboxq);
  6693. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  6694. if (rc != MBX_SUCCESS) {
  6695. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  6696. "3104 Adapter failed to issue "
  6697. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  6698. goto out_unset_queue;
  6699. }
  6700. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  6701. /* don't perform init_link on SLI4 FC port loopback test */
  6702. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  6703. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  6704. if (rc)
  6705. goto out_unset_queue;
  6706. }
  6707. }
  6708. mempool_free(mboxq, phba->mbox_mem_pool);
  6709. return rc;
  6710. out_unset_queue:
  6711. /* Unset all the queues set up in this routine when error out */
  6712. lpfc_sli4_queue_unset(phba);
  6713. out_destroy_queue:
  6714. lpfc_free_iocb_list(phba);
  6715. lpfc_sli4_queue_destroy(phba);
  6716. out_stop_timers:
  6717. lpfc_stop_hba_timers(phba);
  6718. out_free_mbox:
  6719. mempool_free(mboxq, phba->mbox_mem_pool);
  6720. return rc;
  6721. }
  6722. /**
  6723. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  6724. * @ptr: context object - pointer to hba structure.
  6725. *
  6726. * This is the callback function for mailbox timer. The mailbox
  6727. * timer is armed when a new mailbox command is issued and the timer
  6728. * is deleted when the mailbox complete. The function is called by
  6729. * the kernel timer code when a mailbox does not complete within
  6730. * expected time. This function wakes up the worker thread to
  6731. * process the mailbox timeout and returns. All the processing is
  6732. * done by the worker thread function lpfc_mbox_timeout_handler.
  6733. **/
  6734. void
  6735. lpfc_mbox_timeout(struct timer_list *t)
  6736. {
  6737. struct lpfc_hba *phba = from_timer(phba, t, sli.mbox_tmo);
  6738. unsigned long iflag;
  6739. uint32_t tmo_posted;
  6740. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  6741. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  6742. if (!tmo_posted)
  6743. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  6744. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  6745. if (!tmo_posted)
  6746. lpfc_worker_wake_up(phba);
  6747. return;
  6748. }
  6749. /**
  6750. * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
  6751. * are pending
  6752. * @phba: Pointer to HBA context object.
  6753. *
  6754. * This function checks if any mailbox completions are present on the mailbox
  6755. * completion queue.
  6756. **/
  6757. static bool
  6758. lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
  6759. {
  6760. uint32_t idx;
  6761. struct lpfc_queue *mcq;
  6762. struct lpfc_mcqe *mcqe;
  6763. bool pending_completions = false;
  6764. uint8_t qe_valid;
  6765. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6766. return false;
  6767. /* Check for completions on mailbox completion queue */
  6768. mcq = phba->sli4_hba.mbx_cq;
  6769. idx = mcq->hba_index;
  6770. qe_valid = mcq->qe_valid;
  6771. while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe) == qe_valid) {
  6772. mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
  6773. if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
  6774. (!bf_get_le32(lpfc_trailer_async, mcqe))) {
  6775. pending_completions = true;
  6776. break;
  6777. }
  6778. idx = (idx + 1) % mcq->entry_count;
  6779. if (mcq->hba_index == idx)
  6780. break;
  6781. /* if the index wrapped around, toggle the valid bit */
  6782. if (phba->sli4_hba.pc_sli4_params.cqav && !idx)
  6783. qe_valid = (qe_valid) ? 0 : 1;
  6784. }
  6785. return pending_completions;
  6786. }
  6787. /**
  6788. * lpfc_sli4_process_missed_mbox_completions - process mbox completions
  6789. * that were missed.
  6790. * @phba: Pointer to HBA context object.
  6791. *
  6792. * For sli4, it is possible to miss an interrupt. As such mbox completions
  6793. * maybe missed causing erroneous mailbox timeouts to occur. This function
  6794. * checks to see if mbox completions are on the mailbox completion queue
  6795. * and will process all the completions associated with the eq for the
  6796. * mailbox completion queue.
  6797. **/
  6798. bool
  6799. lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
  6800. {
  6801. struct lpfc_sli4_hba *sli4_hba = &phba->sli4_hba;
  6802. uint32_t eqidx;
  6803. struct lpfc_queue *fpeq = NULL;
  6804. struct lpfc_eqe *eqe;
  6805. bool mbox_pending;
  6806. if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
  6807. return false;
  6808. /* Find the eq associated with the mcq */
  6809. if (sli4_hba->hba_eq)
  6810. for (eqidx = 0; eqidx < phba->io_channel_irqs; eqidx++)
  6811. if (sli4_hba->hba_eq[eqidx]->queue_id ==
  6812. sli4_hba->mbx_cq->assoc_qid) {
  6813. fpeq = sli4_hba->hba_eq[eqidx];
  6814. break;
  6815. }
  6816. if (!fpeq)
  6817. return false;
  6818. /* Turn off interrupts from this EQ */
  6819. sli4_hba->sli4_eq_clr_intr(fpeq);
  6820. /* Check to see if a mbox completion is pending */
  6821. mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
  6822. /*
  6823. * If a mbox completion is pending, process all the events on EQ
  6824. * associated with the mbox completion queue (this could include
  6825. * mailbox commands, async events, els commands, receive queue data
  6826. * and fcp commands)
  6827. */
  6828. if (mbox_pending)
  6829. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  6830. lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
  6831. fpeq->EQ_processed++;
  6832. }
  6833. /* Always clear and re-arm the EQ */
  6834. sli4_hba->sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  6835. return mbox_pending;
  6836. }
  6837. /**
  6838. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  6839. * @phba: Pointer to HBA context object.
  6840. *
  6841. * This function is called from worker thread when a mailbox command times out.
  6842. * The caller is not required to hold any locks. This function will reset the
  6843. * HBA and recover all the pending commands.
  6844. **/
  6845. void
  6846. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  6847. {
  6848. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  6849. MAILBOX_t *mb = NULL;
  6850. struct lpfc_sli *psli = &phba->sli;
  6851. /* If the mailbox completed, process the completion and return */
  6852. if (lpfc_sli4_process_missed_mbox_completions(phba))
  6853. return;
  6854. if (pmbox != NULL)
  6855. mb = &pmbox->u.mb;
  6856. /* Check the pmbox pointer first. There is a race condition
  6857. * between the mbox timeout handler getting executed in the
  6858. * worklist and the mailbox actually completing. When this
  6859. * race condition occurs, the mbox_active will be NULL.
  6860. */
  6861. spin_lock_irq(&phba->hbalock);
  6862. if (pmbox == NULL) {
  6863. lpfc_printf_log(phba, KERN_WARNING,
  6864. LOG_MBOX | LOG_SLI,
  6865. "0353 Active Mailbox cleared - mailbox timeout "
  6866. "exiting\n");
  6867. spin_unlock_irq(&phba->hbalock);
  6868. return;
  6869. }
  6870. /* Mbox cmd <mbxCommand> timeout */
  6871. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6872. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  6873. mb->mbxCommand,
  6874. phba->pport->port_state,
  6875. phba->sli.sli_flag,
  6876. phba->sli.mbox_active);
  6877. spin_unlock_irq(&phba->hbalock);
  6878. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  6879. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  6880. * it to fail all outstanding SCSI IO.
  6881. */
  6882. spin_lock_irq(&phba->pport->work_port_lock);
  6883. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  6884. spin_unlock_irq(&phba->pport->work_port_lock);
  6885. spin_lock_irq(&phba->hbalock);
  6886. phba->link_state = LPFC_LINK_UNKNOWN;
  6887. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  6888. spin_unlock_irq(&phba->hbalock);
  6889. lpfc_sli_abort_fcp_rings(phba);
  6890. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6891. "0345 Resetting board due to mailbox timeout\n");
  6892. /* Reset the HBA device */
  6893. lpfc_reset_hba(phba);
  6894. }
  6895. /**
  6896. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  6897. * @phba: Pointer to HBA context object.
  6898. * @pmbox: Pointer to mailbox object.
  6899. * @flag: Flag indicating how the mailbox need to be processed.
  6900. *
  6901. * This function is called by discovery code and HBA management code
  6902. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  6903. * function gets the hbalock to protect the data structures.
  6904. * The mailbox command can be submitted in polling mode, in which case
  6905. * this function will wait in a polling loop for the completion of the
  6906. * mailbox.
  6907. * If the mailbox is submitted in no_wait mode (not polling) the
  6908. * function will submit the command and returns immediately without waiting
  6909. * for the mailbox completion. The no_wait is supported only when HBA
  6910. * is in SLI2/SLI3 mode - interrupts are enabled.
  6911. * The SLI interface allows only one mailbox pending at a time. If the
  6912. * mailbox is issued in polling mode and there is already a mailbox
  6913. * pending, then the function will return an error. If the mailbox is issued
  6914. * in NO_WAIT mode and there is a mailbox pending already, the function
  6915. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  6916. * The sli layer owns the mailbox object until the completion of mailbox
  6917. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  6918. * return codes the caller owns the mailbox command after the return of
  6919. * the function.
  6920. **/
  6921. static int
  6922. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  6923. uint32_t flag)
  6924. {
  6925. MAILBOX_t *mbx;
  6926. struct lpfc_sli *psli = &phba->sli;
  6927. uint32_t status, evtctr;
  6928. uint32_t ha_copy, hc_copy;
  6929. int i;
  6930. unsigned long timeout;
  6931. unsigned long drvr_flag = 0;
  6932. uint32_t word0, ldata;
  6933. void __iomem *to_slim;
  6934. int processing_queue = 0;
  6935. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6936. if (!pmbox) {
  6937. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6938. /* processing mbox queue from intr_handler */
  6939. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6940. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6941. return MBX_SUCCESS;
  6942. }
  6943. processing_queue = 1;
  6944. pmbox = lpfc_mbox_get(phba);
  6945. if (!pmbox) {
  6946. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6947. return MBX_SUCCESS;
  6948. }
  6949. }
  6950. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  6951. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  6952. if(!pmbox->vport) {
  6953. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6954. lpfc_printf_log(phba, KERN_ERR,
  6955. LOG_MBOX | LOG_VPORT,
  6956. "1806 Mbox x%x failed. No vport\n",
  6957. pmbox->u.mb.mbxCommand);
  6958. dump_stack();
  6959. goto out_not_finished;
  6960. }
  6961. }
  6962. /* If the PCI channel is in offline state, do not post mbox. */
  6963. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6964. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6965. goto out_not_finished;
  6966. }
  6967. /* If HBA has a deferred error attention, fail the iocb. */
  6968. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6969. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6970. goto out_not_finished;
  6971. }
  6972. psli = &phba->sli;
  6973. mbx = &pmbox->u.mb;
  6974. status = MBX_SUCCESS;
  6975. if (phba->link_state == LPFC_HBA_ERROR) {
  6976. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6977. /* Mbox command <mbxCommand> cannot issue */
  6978. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6979. "(%d):0311 Mailbox command x%x cannot "
  6980. "issue Data: x%x x%x\n",
  6981. pmbox->vport ? pmbox->vport->vpi : 0,
  6982. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6983. goto out_not_finished;
  6984. }
  6985. if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6986. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6987. !(hc_copy & HC_MBINT_ENA)) {
  6988. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6989. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6990. "(%d):2528 Mailbox command x%x cannot "
  6991. "issue Data: x%x x%x\n",
  6992. pmbox->vport ? pmbox->vport->vpi : 0,
  6993. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6994. goto out_not_finished;
  6995. }
  6996. }
  6997. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6998. /* Polling for a mbox command when another one is already active
  6999. * is not allowed in SLI. Also, the driver must have established
  7000. * SLI2 mode to queue and process multiple mbox commands.
  7001. */
  7002. if (flag & MBX_POLL) {
  7003. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  7004. /* Mbox command <mbxCommand> cannot issue */
  7005. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7006. "(%d):2529 Mailbox command x%x "
  7007. "cannot issue Data: x%x x%x\n",
  7008. pmbox->vport ? pmbox->vport->vpi : 0,
  7009. pmbox->u.mb.mbxCommand,
  7010. psli->sli_flag, flag);
  7011. goto out_not_finished;
  7012. }
  7013. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  7014. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  7015. /* Mbox command <mbxCommand> cannot issue */
  7016. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7017. "(%d):2530 Mailbox command x%x "
  7018. "cannot issue Data: x%x x%x\n",
  7019. pmbox->vport ? pmbox->vport->vpi : 0,
  7020. pmbox->u.mb.mbxCommand,
  7021. psli->sli_flag, flag);
  7022. goto out_not_finished;
  7023. }
  7024. /* Another mailbox command is still being processed, queue this
  7025. * command to be processed later.
  7026. */
  7027. lpfc_mbox_put(phba, pmbox);
  7028. /* Mbox cmd issue - BUSY */
  7029. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7030. "(%d):0308 Mbox cmd issue - BUSY Data: "
  7031. "x%x x%x x%x x%x\n",
  7032. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  7033. mbx->mbxCommand,
  7034. phba->pport ? phba->pport->port_state : 0xff,
  7035. psli->sli_flag, flag);
  7036. psli->slistat.mbox_busy++;
  7037. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  7038. if (pmbox->vport) {
  7039. lpfc_debugfs_disc_trc(pmbox->vport,
  7040. LPFC_DISC_TRC_MBOX_VPORT,
  7041. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  7042. (uint32_t)mbx->mbxCommand,
  7043. mbx->un.varWords[0], mbx->un.varWords[1]);
  7044. }
  7045. else {
  7046. lpfc_debugfs_disc_trc(phba->pport,
  7047. LPFC_DISC_TRC_MBOX,
  7048. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  7049. (uint32_t)mbx->mbxCommand,
  7050. mbx->un.varWords[0], mbx->un.varWords[1]);
  7051. }
  7052. return MBX_BUSY;
  7053. }
  7054. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  7055. /* If we are not polling, we MUST be in SLI2 mode */
  7056. if (flag != MBX_POLL) {
  7057. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  7058. (mbx->mbxCommand != MBX_KILL_BOARD)) {
  7059. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7060. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  7061. /* Mbox command <mbxCommand> cannot issue */
  7062. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7063. "(%d):2531 Mailbox command x%x "
  7064. "cannot issue Data: x%x x%x\n",
  7065. pmbox->vport ? pmbox->vport->vpi : 0,
  7066. pmbox->u.mb.mbxCommand,
  7067. psli->sli_flag, flag);
  7068. goto out_not_finished;
  7069. }
  7070. /* timeout active mbox command */
  7071. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  7072. 1000);
  7073. mod_timer(&psli->mbox_tmo, jiffies + timeout);
  7074. }
  7075. /* Mailbox cmd <cmd> issue */
  7076. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7077. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  7078. "x%x\n",
  7079. pmbox->vport ? pmbox->vport->vpi : 0,
  7080. mbx->mbxCommand,
  7081. phba->pport ? phba->pport->port_state : 0xff,
  7082. psli->sli_flag, flag);
  7083. if (mbx->mbxCommand != MBX_HEARTBEAT) {
  7084. if (pmbox->vport) {
  7085. lpfc_debugfs_disc_trc(pmbox->vport,
  7086. LPFC_DISC_TRC_MBOX_VPORT,
  7087. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  7088. (uint32_t)mbx->mbxCommand,
  7089. mbx->un.varWords[0], mbx->un.varWords[1]);
  7090. }
  7091. else {
  7092. lpfc_debugfs_disc_trc(phba->pport,
  7093. LPFC_DISC_TRC_MBOX,
  7094. "MBOX Send: cmd:x%x mb:x%x x%x",
  7095. (uint32_t)mbx->mbxCommand,
  7096. mbx->un.varWords[0], mbx->un.varWords[1]);
  7097. }
  7098. }
  7099. psli->slistat.mbox_cmd++;
  7100. evtctr = psli->slistat.mbox_event;
  7101. /* next set own bit for the adapter and copy over command word */
  7102. mbx->mbxOwner = OWN_CHIP;
  7103. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  7104. /* Populate mbox extension offset word. */
  7105. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  7106. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  7107. = (uint8_t *)phba->mbox_ext
  7108. - (uint8_t *)phba->mbox;
  7109. }
  7110. /* Copy the mailbox extension data */
  7111. if (pmbox->in_ext_byte_len && pmbox->context2) {
  7112. lpfc_sli_pcimem_bcopy(pmbox->context2,
  7113. (uint8_t *)phba->mbox_ext,
  7114. pmbox->in_ext_byte_len);
  7115. }
  7116. /* Copy command data to host SLIM area */
  7117. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  7118. } else {
  7119. /* Populate mbox extension offset word. */
  7120. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  7121. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  7122. = MAILBOX_HBA_EXT_OFFSET;
  7123. /* Copy the mailbox extension data */
  7124. if (pmbox->in_ext_byte_len && pmbox->context2)
  7125. lpfc_memcpy_to_slim(phba->MBslimaddr +
  7126. MAILBOX_HBA_EXT_OFFSET,
  7127. pmbox->context2, pmbox->in_ext_byte_len);
  7128. if (mbx->mbxCommand == MBX_CONFIG_PORT)
  7129. /* copy command data into host mbox for cmpl */
  7130. lpfc_sli_pcimem_bcopy(mbx, phba->mbox,
  7131. MAILBOX_CMD_SIZE);
  7132. /* First copy mbox command data to HBA SLIM, skip past first
  7133. word */
  7134. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  7135. lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
  7136. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  7137. /* Next copy over first word, with mbxOwner set */
  7138. ldata = *((uint32_t *)mbx);
  7139. to_slim = phba->MBslimaddr;
  7140. writel(ldata, to_slim);
  7141. readl(to_slim); /* flush */
  7142. if (mbx->mbxCommand == MBX_CONFIG_PORT)
  7143. /* switch over to host mailbox */
  7144. psli->sli_flag |= LPFC_SLI_ACTIVE;
  7145. }
  7146. wmb();
  7147. switch (flag) {
  7148. case MBX_NOWAIT:
  7149. /* Set up reference to mailbox command */
  7150. psli->mbox_active = pmbox;
  7151. /* Interrupt board to do it */
  7152. writel(CA_MBATT, phba->CAregaddr);
  7153. readl(phba->CAregaddr); /* flush */
  7154. /* Don't wait for it to finish, just return */
  7155. break;
  7156. case MBX_POLL:
  7157. /* Set up null reference to mailbox command */
  7158. psli->mbox_active = NULL;
  7159. /* Interrupt board to do it */
  7160. writel(CA_MBATT, phba->CAregaddr);
  7161. readl(phba->CAregaddr); /* flush */
  7162. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  7163. /* First read mbox status word */
  7164. word0 = *((uint32_t *)phba->mbox);
  7165. word0 = le32_to_cpu(word0);
  7166. } else {
  7167. /* First read mbox status word */
  7168. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  7169. spin_unlock_irqrestore(&phba->hbalock,
  7170. drvr_flag);
  7171. goto out_not_finished;
  7172. }
  7173. }
  7174. /* Read the HBA Host Attention Register */
  7175. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  7176. spin_unlock_irqrestore(&phba->hbalock,
  7177. drvr_flag);
  7178. goto out_not_finished;
  7179. }
  7180. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  7181. 1000) + jiffies;
  7182. i = 0;
  7183. /* Wait for command to complete */
  7184. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  7185. (!(ha_copy & HA_MBATT) &&
  7186. (phba->link_state > LPFC_WARM_START))) {
  7187. if (time_after(jiffies, timeout)) {
  7188. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7189. spin_unlock_irqrestore(&phba->hbalock,
  7190. drvr_flag);
  7191. goto out_not_finished;
  7192. }
  7193. /* Check if we took a mbox interrupt while we were
  7194. polling */
  7195. if (((word0 & OWN_CHIP) != OWN_CHIP)
  7196. && (evtctr != psli->slistat.mbox_event))
  7197. break;
  7198. if (i++ > 10) {
  7199. spin_unlock_irqrestore(&phba->hbalock,
  7200. drvr_flag);
  7201. msleep(1);
  7202. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  7203. }
  7204. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  7205. /* First copy command data */
  7206. word0 = *((uint32_t *)phba->mbox);
  7207. word0 = le32_to_cpu(word0);
  7208. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  7209. MAILBOX_t *slimmb;
  7210. uint32_t slimword0;
  7211. /* Check real SLIM for any errors */
  7212. slimword0 = readl(phba->MBslimaddr);
  7213. slimmb = (MAILBOX_t *) & slimword0;
  7214. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  7215. && slimmb->mbxStatus) {
  7216. psli->sli_flag &=
  7217. ~LPFC_SLI_ACTIVE;
  7218. word0 = slimword0;
  7219. }
  7220. }
  7221. } else {
  7222. /* First copy command data */
  7223. word0 = readl(phba->MBslimaddr);
  7224. }
  7225. /* Read the HBA Host Attention Register */
  7226. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  7227. spin_unlock_irqrestore(&phba->hbalock,
  7228. drvr_flag);
  7229. goto out_not_finished;
  7230. }
  7231. }
  7232. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  7233. /* copy results back to user */
  7234. lpfc_sli_pcimem_bcopy(phba->mbox, mbx,
  7235. MAILBOX_CMD_SIZE);
  7236. /* Copy the mailbox extension data */
  7237. if (pmbox->out_ext_byte_len && pmbox->context2) {
  7238. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  7239. pmbox->context2,
  7240. pmbox->out_ext_byte_len);
  7241. }
  7242. } else {
  7243. /* First copy command data */
  7244. lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
  7245. MAILBOX_CMD_SIZE);
  7246. /* Copy the mailbox extension data */
  7247. if (pmbox->out_ext_byte_len && pmbox->context2) {
  7248. lpfc_memcpy_from_slim(pmbox->context2,
  7249. phba->MBslimaddr +
  7250. MAILBOX_HBA_EXT_OFFSET,
  7251. pmbox->out_ext_byte_len);
  7252. }
  7253. }
  7254. writel(HA_MBATT, phba->HAregaddr);
  7255. readl(phba->HAregaddr); /* flush */
  7256. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7257. status = mbx->mbxStatus;
  7258. }
  7259. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  7260. return status;
  7261. out_not_finished:
  7262. if (processing_queue) {
  7263. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7264. lpfc_mbox_cmpl_put(phba, pmbox);
  7265. }
  7266. return MBX_NOT_FINISHED;
  7267. }
  7268. /**
  7269. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  7270. * @phba: Pointer to HBA context object.
  7271. *
  7272. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  7273. * the driver internal pending mailbox queue. It will then try to wait out the
  7274. * possible outstanding mailbox command before return.
  7275. *
  7276. * Returns:
  7277. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  7278. * the outstanding mailbox command timed out.
  7279. **/
  7280. static int
  7281. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  7282. {
  7283. struct lpfc_sli *psli = &phba->sli;
  7284. int rc = 0;
  7285. unsigned long timeout = 0;
  7286. /* Mark the asynchronous mailbox command posting as blocked */
  7287. spin_lock_irq(&phba->hbalock);
  7288. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  7289. /* Determine how long we might wait for the active mailbox
  7290. * command to be gracefully completed by firmware.
  7291. */
  7292. if (phba->sli.mbox_active)
  7293. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  7294. phba->sli.mbox_active) *
  7295. 1000) + jiffies;
  7296. spin_unlock_irq(&phba->hbalock);
  7297. /* Make sure the mailbox is really active */
  7298. if (timeout)
  7299. lpfc_sli4_process_missed_mbox_completions(phba);
  7300. /* Wait for the outstnading mailbox command to complete */
  7301. while (phba->sli.mbox_active) {
  7302. /* Check active mailbox complete status every 2ms */
  7303. msleep(2);
  7304. if (time_after(jiffies, timeout)) {
  7305. /* Timeout, marked the outstanding cmd not complete */
  7306. rc = 1;
  7307. break;
  7308. }
  7309. }
  7310. /* Can not cleanly block async mailbox command, fails it */
  7311. if (rc) {
  7312. spin_lock_irq(&phba->hbalock);
  7313. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  7314. spin_unlock_irq(&phba->hbalock);
  7315. }
  7316. return rc;
  7317. }
  7318. /**
  7319. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  7320. * @phba: Pointer to HBA context object.
  7321. *
  7322. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  7323. * commands from the driver internal pending mailbox queue. It makes sure
  7324. * that there is no outstanding mailbox command before resuming posting
  7325. * asynchronous mailbox commands. If, for any reason, there is outstanding
  7326. * mailbox command, it will try to wait it out before resuming asynchronous
  7327. * mailbox command posting.
  7328. **/
  7329. static void
  7330. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  7331. {
  7332. struct lpfc_sli *psli = &phba->sli;
  7333. spin_lock_irq(&phba->hbalock);
  7334. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  7335. /* Asynchronous mailbox posting is not blocked, do nothing */
  7336. spin_unlock_irq(&phba->hbalock);
  7337. return;
  7338. }
  7339. /* Outstanding synchronous mailbox command is guaranteed to be done,
  7340. * successful or timeout, after timing-out the outstanding mailbox
  7341. * command shall always be removed, so just unblock posting async
  7342. * mailbox command and resume
  7343. */
  7344. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  7345. spin_unlock_irq(&phba->hbalock);
  7346. /* wake up worker thread to post asynchronlous mailbox command */
  7347. lpfc_worker_wake_up(phba);
  7348. }
  7349. /**
  7350. * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
  7351. * @phba: Pointer to HBA context object.
  7352. * @mboxq: Pointer to mailbox object.
  7353. *
  7354. * The function waits for the bootstrap mailbox register ready bit from
  7355. * port for twice the regular mailbox command timeout value.
  7356. *
  7357. * 0 - no timeout on waiting for bootstrap mailbox register ready.
  7358. * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
  7359. **/
  7360. static int
  7361. lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  7362. {
  7363. uint32_t db_ready;
  7364. unsigned long timeout;
  7365. struct lpfc_register bmbx_reg;
  7366. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  7367. * 1000) + jiffies;
  7368. do {
  7369. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  7370. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  7371. if (!db_ready)
  7372. msleep(2);
  7373. if (time_after(jiffies, timeout))
  7374. return MBXERR_ERROR;
  7375. } while (!db_ready);
  7376. return 0;
  7377. }
  7378. /**
  7379. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  7380. * @phba: Pointer to HBA context object.
  7381. * @mboxq: Pointer to mailbox object.
  7382. *
  7383. * The function posts a mailbox to the port. The mailbox is expected
  7384. * to be comletely filled in and ready for the port to operate on it.
  7385. * This routine executes a synchronous completion operation on the
  7386. * mailbox by polling for its completion.
  7387. *
  7388. * The caller must not be holding any locks when calling this routine.
  7389. *
  7390. * Returns:
  7391. * MBX_SUCCESS - mailbox posted successfully
  7392. * Any of the MBX error values.
  7393. **/
  7394. static int
  7395. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  7396. {
  7397. int rc = MBX_SUCCESS;
  7398. unsigned long iflag;
  7399. uint32_t mcqe_status;
  7400. uint32_t mbx_cmnd;
  7401. struct lpfc_sli *psli = &phba->sli;
  7402. struct lpfc_mqe *mb = &mboxq->u.mqe;
  7403. struct lpfc_bmbx_create *mbox_rgn;
  7404. struct dma_address *dma_address;
  7405. /*
  7406. * Only one mailbox can be active to the bootstrap mailbox region
  7407. * at a time and there is no queueing provided.
  7408. */
  7409. spin_lock_irqsave(&phba->hbalock, iflag);
  7410. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  7411. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7412. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7413. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  7414. "cannot issue Data: x%x x%x\n",
  7415. mboxq->vport ? mboxq->vport->vpi : 0,
  7416. mboxq->u.mb.mbxCommand,
  7417. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7418. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7419. psli->sli_flag, MBX_POLL);
  7420. return MBXERR_ERROR;
  7421. }
  7422. /* The server grabs the token and owns it until release */
  7423. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  7424. phba->sli.mbox_active = mboxq;
  7425. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7426. /* wait for bootstrap mbox register for readyness */
  7427. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  7428. if (rc)
  7429. goto exit;
  7430. /*
  7431. * Initialize the bootstrap memory region to avoid stale data areas
  7432. * in the mailbox post. Then copy the caller's mailbox contents to
  7433. * the bmbx mailbox region.
  7434. */
  7435. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  7436. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  7437. lpfc_sli4_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  7438. sizeof(struct lpfc_mqe));
  7439. /* Post the high mailbox dma address to the port and wait for ready. */
  7440. dma_address = &phba->sli4_hba.bmbx.dma_address;
  7441. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  7442. /* wait for bootstrap mbox register for hi-address write done */
  7443. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  7444. if (rc)
  7445. goto exit;
  7446. /* Post the low mailbox dma address to the port. */
  7447. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  7448. /* wait for bootstrap mbox register for low address write done */
  7449. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  7450. if (rc)
  7451. goto exit;
  7452. /*
  7453. * Read the CQ to ensure the mailbox has completed.
  7454. * If so, update the mailbox status so that the upper layers
  7455. * can complete the request normally.
  7456. */
  7457. lpfc_sli4_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  7458. sizeof(struct lpfc_mqe));
  7459. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  7460. lpfc_sli4_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  7461. sizeof(struct lpfc_mcqe));
  7462. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  7463. /*
  7464. * When the CQE status indicates a failure and the mailbox status
  7465. * indicates success then copy the CQE status into the mailbox status
  7466. * (and prefix it with x4000).
  7467. */
  7468. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  7469. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  7470. bf_set(lpfc_mqe_status, mb,
  7471. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  7472. rc = MBXERR_ERROR;
  7473. } else
  7474. lpfc_sli4_swap_str(phba, mboxq);
  7475. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7476. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  7477. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  7478. " x%x x%x CQ: x%x x%x x%x x%x\n",
  7479. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  7480. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7481. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7482. bf_get(lpfc_mqe_status, mb),
  7483. mb->un.mb_words[0], mb->un.mb_words[1],
  7484. mb->un.mb_words[2], mb->un.mb_words[3],
  7485. mb->un.mb_words[4], mb->un.mb_words[5],
  7486. mb->un.mb_words[6], mb->un.mb_words[7],
  7487. mb->un.mb_words[8], mb->un.mb_words[9],
  7488. mb->un.mb_words[10], mb->un.mb_words[11],
  7489. mb->un.mb_words[12], mboxq->mcqe.word0,
  7490. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  7491. mboxq->mcqe.trailer);
  7492. exit:
  7493. /* We are holding the token, no needed for lock when release */
  7494. spin_lock_irqsave(&phba->hbalock, iflag);
  7495. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7496. phba->sli.mbox_active = NULL;
  7497. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7498. return rc;
  7499. }
  7500. /**
  7501. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  7502. * @phba: Pointer to HBA context object.
  7503. * @pmbox: Pointer to mailbox object.
  7504. * @flag: Flag indicating how the mailbox need to be processed.
  7505. *
  7506. * This function is called by discovery code and HBA management code to submit
  7507. * a mailbox command to firmware with SLI-4 interface spec.
  7508. *
  7509. * Return codes the caller owns the mailbox command after the return of the
  7510. * function.
  7511. **/
  7512. static int
  7513. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  7514. uint32_t flag)
  7515. {
  7516. struct lpfc_sli *psli = &phba->sli;
  7517. unsigned long iflags;
  7518. int rc;
  7519. /* dump from issue mailbox command if setup */
  7520. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  7521. rc = lpfc_mbox_dev_check(phba);
  7522. if (unlikely(rc)) {
  7523. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7524. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  7525. "cannot issue Data: x%x x%x\n",
  7526. mboxq->vport ? mboxq->vport->vpi : 0,
  7527. mboxq->u.mb.mbxCommand,
  7528. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7529. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7530. psli->sli_flag, flag);
  7531. goto out_not_finished;
  7532. }
  7533. /* Detect polling mode and jump to a handler */
  7534. if (!phba->sli4_hba.intr_enable) {
  7535. if (flag == MBX_POLL)
  7536. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  7537. else
  7538. rc = -EIO;
  7539. if (rc != MBX_SUCCESS)
  7540. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  7541. "(%d):2541 Mailbox command x%x "
  7542. "(x%x/x%x) failure: "
  7543. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  7544. "Data: x%x x%x\n,",
  7545. mboxq->vport ? mboxq->vport->vpi : 0,
  7546. mboxq->u.mb.mbxCommand,
  7547. lpfc_sli_config_mbox_subsys_get(phba,
  7548. mboxq),
  7549. lpfc_sli_config_mbox_opcode_get(phba,
  7550. mboxq),
  7551. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  7552. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  7553. bf_get(lpfc_mcqe_ext_status,
  7554. &mboxq->mcqe),
  7555. psli->sli_flag, flag);
  7556. return rc;
  7557. } else if (flag == MBX_POLL) {
  7558. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  7559. "(%d):2542 Try to issue mailbox command "
  7560. "x%x (x%x/x%x) synchronously ahead of async "
  7561. "mailbox command queue: x%x x%x\n",
  7562. mboxq->vport ? mboxq->vport->vpi : 0,
  7563. mboxq->u.mb.mbxCommand,
  7564. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7565. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7566. psli->sli_flag, flag);
  7567. /* Try to block the asynchronous mailbox posting */
  7568. rc = lpfc_sli4_async_mbox_block(phba);
  7569. if (!rc) {
  7570. /* Successfully blocked, now issue sync mbox cmd */
  7571. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  7572. if (rc != MBX_SUCCESS)
  7573. lpfc_printf_log(phba, KERN_WARNING,
  7574. LOG_MBOX | LOG_SLI,
  7575. "(%d):2597 Sync Mailbox command "
  7576. "x%x (x%x/x%x) failure: "
  7577. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  7578. "Data: x%x x%x\n,",
  7579. mboxq->vport ? mboxq->vport->vpi : 0,
  7580. mboxq->u.mb.mbxCommand,
  7581. lpfc_sli_config_mbox_subsys_get(phba,
  7582. mboxq),
  7583. lpfc_sli_config_mbox_opcode_get(phba,
  7584. mboxq),
  7585. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  7586. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  7587. bf_get(lpfc_mcqe_ext_status,
  7588. &mboxq->mcqe),
  7589. psli->sli_flag, flag);
  7590. /* Unblock the async mailbox posting afterward */
  7591. lpfc_sli4_async_mbox_unblock(phba);
  7592. }
  7593. return rc;
  7594. }
  7595. /* Now, interrupt mode asynchrous mailbox command */
  7596. rc = lpfc_mbox_cmd_check(phba, mboxq);
  7597. if (rc) {
  7598. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7599. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  7600. "cannot issue Data: x%x x%x\n",
  7601. mboxq->vport ? mboxq->vport->vpi : 0,
  7602. mboxq->u.mb.mbxCommand,
  7603. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7604. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7605. psli->sli_flag, flag);
  7606. goto out_not_finished;
  7607. }
  7608. /* Put the mailbox command to the driver internal FIFO */
  7609. psli->slistat.mbox_busy++;
  7610. spin_lock_irqsave(&phba->hbalock, iflags);
  7611. lpfc_mbox_put(phba, mboxq);
  7612. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7613. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7614. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  7615. "x%x (x%x/x%x) x%x x%x x%x\n",
  7616. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  7617. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  7618. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7619. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7620. phba->pport->port_state,
  7621. psli->sli_flag, MBX_NOWAIT);
  7622. /* Wake up worker thread to transport mailbox command from head */
  7623. lpfc_worker_wake_up(phba);
  7624. return MBX_BUSY;
  7625. out_not_finished:
  7626. return MBX_NOT_FINISHED;
  7627. }
  7628. /**
  7629. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  7630. * @phba: Pointer to HBA context object.
  7631. *
  7632. * This function is called by worker thread to send a mailbox command to
  7633. * SLI4 HBA firmware.
  7634. *
  7635. **/
  7636. int
  7637. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  7638. {
  7639. struct lpfc_sli *psli = &phba->sli;
  7640. LPFC_MBOXQ_t *mboxq;
  7641. int rc = MBX_SUCCESS;
  7642. unsigned long iflags;
  7643. struct lpfc_mqe *mqe;
  7644. uint32_t mbx_cmnd;
  7645. /* Check interrupt mode before post async mailbox command */
  7646. if (unlikely(!phba->sli4_hba.intr_enable))
  7647. return MBX_NOT_FINISHED;
  7648. /* Check for mailbox command service token */
  7649. spin_lock_irqsave(&phba->hbalock, iflags);
  7650. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  7651. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7652. return MBX_NOT_FINISHED;
  7653. }
  7654. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  7655. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7656. return MBX_NOT_FINISHED;
  7657. }
  7658. if (unlikely(phba->sli.mbox_active)) {
  7659. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7660. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7661. "0384 There is pending active mailbox cmd\n");
  7662. return MBX_NOT_FINISHED;
  7663. }
  7664. /* Take the mailbox command service token */
  7665. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  7666. /* Get the next mailbox command from head of queue */
  7667. mboxq = lpfc_mbox_get(phba);
  7668. /* If no more mailbox command waiting for post, we're done */
  7669. if (!mboxq) {
  7670. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7671. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7672. return MBX_SUCCESS;
  7673. }
  7674. phba->sli.mbox_active = mboxq;
  7675. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7676. /* Check device readiness for posting mailbox command */
  7677. rc = lpfc_mbox_dev_check(phba);
  7678. if (unlikely(rc))
  7679. /* Driver clean routine will clean up pending mailbox */
  7680. goto out_not_finished;
  7681. /* Prepare the mbox command to be posted */
  7682. mqe = &mboxq->u.mqe;
  7683. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  7684. /* Start timer for the mbox_tmo and log some mailbox post messages */
  7685. mod_timer(&psli->mbox_tmo, (jiffies +
  7686. msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
  7687. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  7688. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  7689. "x%x x%x\n",
  7690. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  7691. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7692. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7693. phba->pport->port_state, psli->sli_flag);
  7694. if (mbx_cmnd != MBX_HEARTBEAT) {
  7695. if (mboxq->vport) {
  7696. lpfc_debugfs_disc_trc(mboxq->vport,
  7697. LPFC_DISC_TRC_MBOX_VPORT,
  7698. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  7699. mbx_cmnd, mqe->un.mb_words[0],
  7700. mqe->un.mb_words[1]);
  7701. } else {
  7702. lpfc_debugfs_disc_trc(phba->pport,
  7703. LPFC_DISC_TRC_MBOX,
  7704. "MBOX Send: cmd:x%x mb:x%x x%x",
  7705. mbx_cmnd, mqe->un.mb_words[0],
  7706. mqe->un.mb_words[1]);
  7707. }
  7708. }
  7709. psli->slistat.mbox_cmd++;
  7710. /* Post the mailbox command to the port */
  7711. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  7712. if (rc != MBX_SUCCESS) {
  7713. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  7714. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  7715. "cannot issue Data: x%x x%x\n",
  7716. mboxq->vport ? mboxq->vport->vpi : 0,
  7717. mboxq->u.mb.mbxCommand,
  7718. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  7719. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  7720. psli->sli_flag, MBX_NOWAIT);
  7721. goto out_not_finished;
  7722. }
  7723. return rc;
  7724. out_not_finished:
  7725. spin_lock_irqsave(&phba->hbalock, iflags);
  7726. if (phba->sli.mbox_active) {
  7727. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7728. __lpfc_mbox_cmpl_put(phba, mboxq);
  7729. /* Release the token */
  7730. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7731. phba->sli.mbox_active = NULL;
  7732. }
  7733. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7734. return MBX_NOT_FINISHED;
  7735. }
  7736. /**
  7737. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  7738. * @phba: Pointer to HBA context object.
  7739. * @pmbox: Pointer to mailbox object.
  7740. * @flag: Flag indicating how the mailbox need to be processed.
  7741. *
  7742. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  7743. * the API jump table function pointer from the lpfc_hba struct.
  7744. *
  7745. * Return codes the caller owns the mailbox command after the return of the
  7746. * function.
  7747. **/
  7748. int
  7749. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  7750. {
  7751. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  7752. }
  7753. /**
  7754. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  7755. * @phba: The hba struct for which this call is being executed.
  7756. * @dev_grp: The HBA PCI-Device group number.
  7757. *
  7758. * This routine sets up the mbox interface API function jump table in @phba
  7759. * struct.
  7760. * Returns: 0 - success, -ENODEV - failure.
  7761. **/
  7762. int
  7763. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7764. {
  7765. switch (dev_grp) {
  7766. case LPFC_PCI_DEV_LP:
  7767. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  7768. phba->lpfc_sli_handle_slow_ring_event =
  7769. lpfc_sli_handle_slow_ring_event_s3;
  7770. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  7771. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  7772. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  7773. break;
  7774. case LPFC_PCI_DEV_OC:
  7775. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  7776. phba->lpfc_sli_handle_slow_ring_event =
  7777. lpfc_sli_handle_slow_ring_event_s4;
  7778. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  7779. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  7780. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  7781. break;
  7782. default:
  7783. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7784. "1420 Invalid HBA PCI-device group: 0x%x\n",
  7785. dev_grp);
  7786. return -ENODEV;
  7787. break;
  7788. }
  7789. return 0;
  7790. }
  7791. /**
  7792. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  7793. * @phba: Pointer to HBA context object.
  7794. * @pring: Pointer to driver SLI ring object.
  7795. * @piocb: Pointer to address of newly added command iocb.
  7796. *
  7797. * This function is called with hbalock held to add a command
  7798. * iocb to the txq when SLI layer cannot submit the command iocb
  7799. * to the ring.
  7800. **/
  7801. void
  7802. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7803. struct lpfc_iocbq *piocb)
  7804. {
  7805. lockdep_assert_held(&phba->hbalock);
  7806. /* Insert the caller's iocb in the txq tail for later processing. */
  7807. list_add_tail(&piocb->list, &pring->txq);
  7808. }
  7809. /**
  7810. * lpfc_sli_next_iocb - Get the next iocb in the txq
  7811. * @phba: Pointer to HBA context object.
  7812. * @pring: Pointer to driver SLI ring object.
  7813. * @piocb: Pointer to address of newly added command iocb.
  7814. *
  7815. * This function is called with hbalock held before a new
  7816. * iocb is submitted to the firmware. This function checks
  7817. * txq to flush the iocbs in txq to Firmware before
  7818. * submitting new iocbs to the Firmware.
  7819. * If there are iocbs in the txq which need to be submitted
  7820. * to firmware, lpfc_sli_next_iocb returns the first element
  7821. * of the txq after dequeuing it from txq.
  7822. * If there is no iocb in the txq then the function will return
  7823. * *piocb and *piocb is set to NULL. Caller needs to check
  7824. * *piocb to find if there are more commands in the txq.
  7825. **/
  7826. static struct lpfc_iocbq *
  7827. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7828. struct lpfc_iocbq **piocb)
  7829. {
  7830. struct lpfc_iocbq * nextiocb;
  7831. lockdep_assert_held(&phba->hbalock);
  7832. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  7833. if (!nextiocb) {
  7834. nextiocb = *piocb;
  7835. *piocb = NULL;
  7836. }
  7837. return nextiocb;
  7838. }
  7839. /**
  7840. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  7841. * @phba: Pointer to HBA context object.
  7842. * @ring_number: SLI ring number to issue iocb on.
  7843. * @piocb: Pointer to command iocb.
  7844. * @flag: Flag indicating if this command can be put into txq.
  7845. *
  7846. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  7847. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  7848. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  7849. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  7850. * this function allows only iocbs for posting buffers. This function finds
  7851. * next available slot in the command ring and posts the command to the
  7852. * available slot and writes the port attention register to request HBA start
  7853. * processing new iocb. If there is no slot available in the ring and
  7854. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  7855. * the function returns IOCB_BUSY.
  7856. *
  7857. * This function is called with hbalock held. The function will return success
  7858. * after it successfully submit the iocb to firmware or after adding to the
  7859. * txq.
  7860. **/
  7861. static int
  7862. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  7863. struct lpfc_iocbq *piocb, uint32_t flag)
  7864. {
  7865. struct lpfc_iocbq *nextiocb;
  7866. IOCB_t *iocb;
  7867. struct lpfc_sli_ring *pring = &phba->sli.sli3_ring[ring_number];
  7868. lockdep_assert_held(&phba->hbalock);
  7869. if (piocb->iocb_cmpl && (!piocb->vport) &&
  7870. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  7871. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  7872. lpfc_printf_log(phba, KERN_ERR,
  7873. LOG_SLI | LOG_VPORT,
  7874. "1807 IOCB x%x failed. No vport\n",
  7875. piocb->iocb.ulpCommand);
  7876. dump_stack();
  7877. return IOCB_ERROR;
  7878. }
  7879. /* If the PCI channel is in offline state, do not post iocbs. */
  7880. if (unlikely(pci_channel_offline(phba->pcidev)))
  7881. return IOCB_ERROR;
  7882. /* If HBA has a deferred error attention, fail the iocb. */
  7883. if (unlikely(phba->hba_flag & DEFER_ERATT))
  7884. return IOCB_ERROR;
  7885. /*
  7886. * We should never get an IOCB if we are in a < LINK_DOWN state
  7887. */
  7888. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  7889. return IOCB_ERROR;
  7890. /*
  7891. * Check to see if we are blocking IOCB processing because of a
  7892. * outstanding event.
  7893. */
  7894. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  7895. goto iocb_busy;
  7896. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  7897. /*
  7898. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  7899. * can be issued if the link is not up.
  7900. */
  7901. switch (piocb->iocb.ulpCommand) {
  7902. case CMD_GEN_REQUEST64_CR:
  7903. case CMD_GEN_REQUEST64_CX:
  7904. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  7905. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  7906. FC_RCTL_DD_UNSOL_CMD) ||
  7907. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  7908. MENLO_TRANSPORT_TYPE))
  7909. goto iocb_busy;
  7910. break;
  7911. case CMD_QUE_RING_BUF_CN:
  7912. case CMD_QUE_RING_BUF64_CN:
  7913. /*
  7914. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  7915. * completion, iocb_cmpl MUST be 0.
  7916. */
  7917. if (piocb->iocb_cmpl)
  7918. piocb->iocb_cmpl = NULL;
  7919. /*FALLTHROUGH*/
  7920. case CMD_CREATE_XRI_CR:
  7921. case CMD_CLOSE_XRI_CN:
  7922. case CMD_CLOSE_XRI_CX:
  7923. break;
  7924. default:
  7925. goto iocb_busy;
  7926. }
  7927. /*
  7928. * For FCP commands, we must be in a state where we can process link
  7929. * attention events.
  7930. */
  7931. } else if (unlikely(pring->ringno == LPFC_FCP_RING &&
  7932. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  7933. goto iocb_busy;
  7934. }
  7935. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  7936. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  7937. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  7938. if (iocb)
  7939. lpfc_sli_update_ring(phba, pring);
  7940. else
  7941. lpfc_sli_update_full_ring(phba, pring);
  7942. if (!piocb)
  7943. return IOCB_SUCCESS;
  7944. goto out_busy;
  7945. iocb_busy:
  7946. pring->stats.iocb_cmd_delay++;
  7947. out_busy:
  7948. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7949. __lpfc_sli_ringtx_put(phba, pring, piocb);
  7950. return IOCB_SUCCESS;
  7951. }
  7952. return IOCB_BUSY;
  7953. }
  7954. /**
  7955. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  7956. * @phba: Pointer to HBA context object.
  7957. * @piocb: Pointer to command iocb.
  7958. * @sglq: Pointer to the scatter gather queue object.
  7959. *
  7960. * This routine converts the bpl or bde that is in the IOCB
  7961. * to a sgl list for the sli4 hardware. The physical address
  7962. * of the bpl/bde is converted back to a virtual address.
  7963. * If the IOCB contains a BPL then the list of BDE's is
  7964. * converted to sli4_sge's. If the IOCB contains a single
  7965. * BDE then it is converted to a single sli_sge.
  7966. * The IOCB is still in cpu endianess so the contents of
  7967. * the bpl can be used without byte swapping.
  7968. *
  7969. * Returns valid XRI = Success, NO_XRI = Failure.
  7970. **/
  7971. static uint16_t
  7972. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  7973. struct lpfc_sglq *sglq)
  7974. {
  7975. uint16_t xritag = NO_XRI;
  7976. struct ulp_bde64 *bpl = NULL;
  7977. struct ulp_bde64 bde;
  7978. struct sli4_sge *sgl = NULL;
  7979. struct lpfc_dmabuf *dmabuf;
  7980. IOCB_t *icmd;
  7981. int numBdes = 0;
  7982. int i = 0;
  7983. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7984. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7985. if (!piocbq || !sglq)
  7986. return xritag;
  7987. sgl = (struct sli4_sge *)sglq->sgl;
  7988. icmd = &piocbq->iocb;
  7989. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7990. return sglq->sli4_xritag;
  7991. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7992. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7993. sizeof(struct ulp_bde64);
  7994. /* The addrHigh and addrLow fields within the IOCB
  7995. * have not been byteswapped yet so there is no
  7996. * need to swap them back.
  7997. */
  7998. if (piocbq->context3)
  7999. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  8000. else
  8001. return xritag;
  8002. bpl = (struct ulp_bde64 *)dmabuf->virt;
  8003. if (!bpl)
  8004. return xritag;
  8005. for (i = 0; i < numBdes; i++) {
  8006. /* Should already be byte swapped. */
  8007. sgl->addr_hi = bpl->addrHigh;
  8008. sgl->addr_lo = bpl->addrLow;
  8009. sgl->word2 = le32_to_cpu(sgl->word2);
  8010. if ((i+1) == numBdes)
  8011. bf_set(lpfc_sli4_sge_last, sgl, 1);
  8012. else
  8013. bf_set(lpfc_sli4_sge_last, sgl, 0);
  8014. /* swap the size field back to the cpu so we
  8015. * can assign it to the sgl.
  8016. */
  8017. bde.tus.w = le32_to_cpu(bpl->tus.w);
  8018. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  8019. /* The offsets in the sgl need to be accumulated
  8020. * separately for the request and reply lists.
  8021. * The request is always first, the reply follows.
  8022. */
  8023. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  8024. /* add up the reply sg entries */
  8025. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  8026. inbound++;
  8027. /* first inbound? reset the offset */
  8028. if (inbound == 1)
  8029. offset = 0;
  8030. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  8031. bf_set(lpfc_sli4_sge_type, sgl,
  8032. LPFC_SGE_TYPE_DATA);
  8033. offset += bde.tus.f.bdeSize;
  8034. }
  8035. sgl->word2 = cpu_to_le32(sgl->word2);
  8036. bpl++;
  8037. sgl++;
  8038. }
  8039. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  8040. /* The addrHigh and addrLow fields of the BDE have not
  8041. * been byteswapped yet so they need to be swapped
  8042. * before putting them in the sgl.
  8043. */
  8044. sgl->addr_hi =
  8045. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  8046. sgl->addr_lo =
  8047. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  8048. sgl->word2 = le32_to_cpu(sgl->word2);
  8049. bf_set(lpfc_sli4_sge_last, sgl, 1);
  8050. sgl->word2 = cpu_to_le32(sgl->word2);
  8051. sgl->sge_len =
  8052. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  8053. }
  8054. return sglq->sli4_xritag;
  8055. }
  8056. /**
  8057. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  8058. * @phba: Pointer to HBA context object.
  8059. * @piocb: Pointer to command iocb.
  8060. * @wqe: Pointer to the work queue entry.
  8061. *
  8062. * This routine converts the iocb command to its Work Queue Entry
  8063. * equivalent. The wqe pointer should not have any fields set when
  8064. * this routine is called because it will memcpy over them.
  8065. * This routine does not set the CQ_ID or the WQEC bits in the
  8066. * wqe.
  8067. *
  8068. * Returns: 0 = Success, IOCB_ERROR = Failure.
  8069. **/
  8070. static int
  8071. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  8072. union lpfc_wqe128 *wqe)
  8073. {
  8074. uint32_t xmit_len = 0, total_len = 0;
  8075. uint8_t ct = 0;
  8076. uint32_t fip;
  8077. uint32_t abort_tag;
  8078. uint8_t command_type = ELS_COMMAND_NON_FIP;
  8079. uint8_t cmnd;
  8080. uint16_t xritag;
  8081. uint16_t abrt_iotag;
  8082. struct lpfc_iocbq *abrtiocbq;
  8083. struct ulp_bde64 *bpl = NULL;
  8084. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  8085. int numBdes, i;
  8086. struct ulp_bde64 bde;
  8087. struct lpfc_nodelist *ndlp;
  8088. uint32_t *pcmd;
  8089. uint32_t if_type;
  8090. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  8091. /* The fcp commands will set command type */
  8092. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8093. command_type = FCP_COMMAND;
  8094. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  8095. command_type = ELS_COMMAND_FIP;
  8096. else
  8097. command_type = ELS_COMMAND_NON_FIP;
  8098. if (phba->fcp_embed_io)
  8099. memset(wqe, 0, sizeof(union lpfc_wqe128));
  8100. /* Some of the fields are in the right position already */
  8101. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  8102. if (iocbq->iocb.ulpCommand != CMD_SEND_FRAME) {
  8103. /* The ct field has moved so reset */
  8104. wqe->generic.wqe_com.word7 = 0;
  8105. wqe->generic.wqe_com.word10 = 0;
  8106. }
  8107. abort_tag = (uint32_t) iocbq->iotag;
  8108. xritag = iocbq->sli4_xritag;
  8109. /* words0-2 bpl convert bde */
  8110. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  8111. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  8112. sizeof(struct ulp_bde64);
  8113. bpl = (struct ulp_bde64 *)
  8114. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  8115. if (!bpl)
  8116. return IOCB_ERROR;
  8117. /* Should already be byte swapped. */
  8118. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  8119. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  8120. /* swap the size field back to the cpu so we
  8121. * can assign it to the sgl.
  8122. */
  8123. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  8124. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  8125. total_len = 0;
  8126. for (i = 0; i < numBdes; i++) {
  8127. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  8128. total_len += bde.tus.f.bdeSize;
  8129. }
  8130. } else
  8131. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  8132. iocbq->iocb.ulpIoTag = iocbq->iotag;
  8133. cmnd = iocbq->iocb.ulpCommand;
  8134. switch (iocbq->iocb.ulpCommand) {
  8135. case CMD_ELS_REQUEST64_CR:
  8136. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  8137. ndlp = iocbq->context_un.ndlp;
  8138. else
  8139. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  8140. if (!iocbq->iocb.ulpLe) {
  8141. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8142. "2007 Only Limited Edition cmd Format"
  8143. " supported 0x%x\n",
  8144. iocbq->iocb.ulpCommand);
  8145. return IOCB_ERROR;
  8146. }
  8147. wqe->els_req.payload_len = xmit_len;
  8148. /* Els_reguest64 has a TMO */
  8149. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  8150. iocbq->iocb.ulpTimeout);
  8151. /* Need a VF for word 4 set the vf bit*/
  8152. bf_set(els_req64_vf, &wqe->els_req, 0);
  8153. /* And a VFID for word 12 */
  8154. bf_set(els_req64_vfid, &wqe->els_req, 0);
  8155. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  8156. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  8157. iocbq->iocb.ulpContext);
  8158. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  8159. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  8160. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  8161. if (command_type == ELS_COMMAND_FIP)
  8162. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  8163. >> LPFC_FIP_ELS_ID_SHIFT);
  8164. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  8165. iocbq->context2)->virt);
  8166. if_type = bf_get(lpfc_sli_intf_if_type,
  8167. &phba->sli4_hba.sli_intf);
  8168. if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
  8169. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  8170. *pcmd == ELS_CMD_SCR ||
  8171. *pcmd == ELS_CMD_FDISC ||
  8172. *pcmd == ELS_CMD_LOGO ||
  8173. *pcmd == ELS_CMD_PLOGI)) {
  8174. bf_set(els_req64_sp, &wqe->els_req, 1);
  8175. bf_set(els_req64_sid, &wqe->els_req,
  8176. iocbq->vport->fc_myDID);
  8177. if ((*pcmd == ELS_CMD_FLOGI) &&
  8178. !(phba->fc_topology ==
  8179. LPFC_TOPOLOGY_LOOP))
  8180. bf_set(els_req64_sid, &wqe->els_req, 0);
  8181. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  8182. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  8183. phba->vpi_ids[iocbq->vport->vpi]);
  8184. } else if (pcmd && iocbq->context1) {
  8185. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  8186. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  8187. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  8188. }
  8189. }
  8190. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  8191. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  8192. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  8193. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  8194. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  8195. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  8196. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  8197. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  8198. wqe->els_req.max_response_payload_len = total_len - xmit_len;
  8199. break;
  8200. case CMD_XMIT_SEQUENCE64_CX:
  8201. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  8202. iocbq->iocb.un.ulpWord[3]);
  8203. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  8204. iocbq->iocb.unsli3.rcvsli3.ox_id);
  8205. /* The entire sequence is transmitted for this IOCB */
  8206. xmit_len = total_len;
  8207. cmnd = CMD_XMIT_SEQUENCE64_CR;
  8208. if (phba->link_flag & LS_LOOPBACK_MODE)
  8209. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  8210. case CMD_XMIT_SEQUENCE64_CR:
  8211. /* word3 iocb=io_tag32 wqe=reserved */
  8212. wqe->xmit_sequence.rsvd3 = 0;
  8213. /* word4 relative_offset memcpy */
  8214. /* word5 r_ctl/df_ctl memcpy */
  8215. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  8216. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  8217. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  8218. LPFC_WQE_IOD_WRITE);
  8219. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  8220. LPFC_WQE_LENLOC_WORD12);
  8221. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  8222. wqe->xmit_sequence.xmit_len = xmit_len;
  8223. command_type = OTHER_COMMAND;
  8224. break;
  8225. case CMD_XMIT_BCAST64_CN:
  8226. /* word3 iocb=iotag32 wqe=seq_payload_len */
  8227. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  8228. /* word4 iocb=rsvd wqe=rsvd */
  8229. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  8230. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  8231. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  8232. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  8233. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  8234. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  8235. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  8236. LPFC_WQE_LENLOC_WORD3);
  8237. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  8238. break;
  8239. case CMD_FCP_IWRITE64_CR:
  8240. command_type = FCP_COMMAND_DATA_OUT;
  8241. /* word3 iocb=iotag wqe=payload_offset_len */
  8242. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  8243. bf_set(payload_offset_len, &wqe->fcp_iwrite,
  8244. xmit_len + sizeof(struct fcp_rsp));
  8245. bf_set(cmd_buff_len, &wqe->fcp_iwrite,
  8246. 0);
  8247. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  8248. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  8249. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  8250. iocbq->iocb.ulpFCP2Rcvy);
  8251. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  8252. /* Always open the exchange */
  8253. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  8254. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  8255. LPFC_WQE_LENLOC_WORD4);
  8256. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  8257. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  8258. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  8259. bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
  8260. bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
  8261. if (iocbq->priority) {
  8262. bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
  8263. (iocbq->priority << 1));
  8264. } else {
  8265. bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
  8266. (phba->cfg_XLanePriority << 1));
  8267. }
  8268. }
  8269. /* Note, word 10 is already initialized to 0 */
  8270. /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
  8271. if (phba->cfg_enable_pbde)
  8272. bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
  8273. else
  8274. bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 0);
  8275. if (phba->fcp_embed_io) {
  8276. struct lpfc_scsi_buf *lpfc_cmd;
  8277. struct sli4_sge *sgl;
  8278. struct fcp_cmnd *fcp_cmnd;
  8279. uint32_t *ptr;
  8280. /* 128 byte wqe support here */
  8281. lpfc_cmd = iocbq->context1;
  8282. sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  8283. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  8284. /* Word 0-2 - FCP_CMND */
  8285. wqe->generic.bde.tus.f.bdeFlags =
  8286. BUFF_TYPE_BDE_IMMED;
  8287. wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
  8288. wqe->generic.bde.addrHigh = 0;
  8289. wqe->generic.bde.addrLow = 88; /* Word 22 */
  8290. bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
  8291. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
  8292. /* Word 22-29 FCP CMND Payload */
  8293. ptr = &wqe->words[22];
  8294. memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
  8295. }
  8296. break;
  8297. case CMD_FCP_IREAD64_CR:
  8298. /* word3 iocb=iotag wqe=payload_offset_len */
  8299. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  8300. bf_set(payload_offset_len, &wqe->fcp_iread,
  8301. xmit_len + sizeof(struct fcp_rsp));
  8302. bf_set(cmd_buff_len, &wqe->fcp_iread,
  8303. 0);
  8304. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  8305. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  8306. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  8307. iocbq->iocb.ulpFCP2Rcvy);
  8308. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  8309. /* Always open the exchange */
  8310. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  8311. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  8312. LPFC_WQE_LENLOC_WORD4);
  8313. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  8314. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  8315. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  8316. bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
  8317. bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
  8318. if (iocbq->priority) {
  8319. bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
  8320. (iocbq->priority << 1));
  8321. } else {
  8322. bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
  8323. (phba->cfg_XLanePriority << 1));
  8324. }
  8325. }
  8326. /* Note, word 10 is already initialized to 0 */
  8327. /* Don't set PBDE for Perf hints, just lpfc_enable_pbde */
  8328. if (phba->cfg_enable_pbde)
  8329. bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
  8330. else
  8331. bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 0);
  8332. if (phba->fcp_embed_io) {
  8333. struct lpfc_scsi_buf *lpfc_cmd;
  8334. struct sli4_sge *sgl;
  8335. struct fcp_cmnd *fcp_cmnd;
  8336. uint32_t *ptr;
  8337. /* 128 byte wqe support here */
  8338. lpfc_cmd = iocbq->context1;
  8339. sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  8340. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  8341. /* Word 0-2 - FCP_CMND */
  8342. wqe->generic.bde.tus.f.bdeFlags =
  8343. BUFF_TYPE_BDE_IMMED;
  8344. wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
  8345. wqe->generic.bde.addrHigh = 0;
  8346. wqe->generic.bde.addrLow = 88; /* Word 22 */
  8347. bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
  8348. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
  8349. /* Word 22-29 FCP CMND Payload */
  8350. ptr = &wqe->words[22];
  8351. memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
  8352. }
  8353. break;
  8354. case CMD_FCP_ICMND64_CR:
  8355. /* word3 iocb=iotag wqe=payload_offset_len */
  8356. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  8357. bf_set(payload_offset_len, &wqe->fcp_icmd,
  8358. xmit_len + sizeof(struct fcp_rsp));
  8359. bf_set(cmd_buff_len, &wqe->fcp_icmd,
  8360. 0);
  8361. /* word3 iocb=IO_TAG wqe=reserved */
  8362. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  8363. /* Always open the exchange */
  8364. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  8365. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  8366. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  8367. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  8368. LPFC_WQE_LENLOC_NONE);
  8369. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  8370. iocbq->iocb.ulpFCP2Rcvy);
  8371. if (iocbq->iocb_flag & LPFC_IO_OAS) {
  8372. bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
  8373. bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
  8374. if (iocbq->priority) {
  8375. bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
  8376. (iocbq->priority << 1));
  8377. } else {
  8378. bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
  8379. (phba->cfg_XLanePriority << 1));
  8380. }
  8381. }
  8382. /* Note, word 10 is already initialized to 0 */
  8383. if (phba->fcp_embed_io) {
  8384. struct lpfc_scsi_buf *lpfc_cmd;
  8385. struct sli4_sge *sgl;
  8386. struct fcp_cmnd *fcp_cmnd;
  8387. uint32_t *ptr;
  8388. /* 128 byte wqe support here */
  8389. lpfc_cmd = iocbq->context1;
  8390. sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  8391. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  8392. /* Word 0-2 - FCP_CMND */
  8393. wqe->generic.bde.tus.f.bdeFlags =
  8394. BUFF_TYPE_BDE_IMMED;
  8395. wqe->generic.bde.tus.f.bdeSize = sgl->sge_len;
  8396. wqe->generic.bde.addrHigh = 0;
  8397. wqe->generic.bde.addrLow = 88; /* Word 22 */
  8398. bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
  8399. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
  8400. /* Word 22-29 FCP CMND Payload */
  8401. ptr = &wqe->words[22];
  8402. memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
  8403. }
  8404. break;
  8405. case CMD_GEN_REQUEST64_CR:
  8406. /* For this command calculate the xmit length of the
  8407. * request bde.
  8408. */
  8409. xmit_len = 0;
  8410. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  8411. sizeof(struct ulp_bde64);
  8412. for (i = 0; i < numBdes; i++) {
  8413. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  8414. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  8415. break;
  8416. xmit_len += bde.tus.f.bdeSize;
  8417. }
  8418. /* word3 iocb=IO_TAG wqe=request_payload_len */
  8419. wqe->gen_req.request_payload_len = xmit_len;
  8420. /* word4 iocb=parameter wqe=relative_offset memcpy */
  8421. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  8422. /* word6 context tag copied in memcpy */
  8423. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  8424. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  8425. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8426. "2015 Invalid CT %x command 0x%x\n",
  8427. ct, iocbq->iocb.ulpCommand);
  8428. return IOCB_ERROR;
  8429. }
  8430. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  8431. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  8432. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  8433. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  8434. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  8435. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  8436. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  8437. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  8438. wqe->gen_req.max_response_payload_len = total_len - xmit_len;
  8439. command_type = OTHER_COMMAND;
  8440. break;
  8441. case CMD_XMIT_ELS_RSP64_CX:
  8442. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  8443. /* words0-2 BDE memcpy */
  8444. /* word3 iocb=iotag32 wqe=response_payload_len */
  8445. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  8446. /* word4 */
  8447. wqe->xmit_els_rsp.word4 = 0;
  8448. /* word5 iocb=rsvd wge=did */
  8449. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  8450. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  8451. if_type = bf_get(lpfc_sli_intf_if_type,
  8452. &phba->sli4_hba.sli_intf);
  8453. if (if_type >= LPFC_SLI_INTF_IF_TYPE_2) {
  8454. if (iocbq->vport->fc_flag & FC_PT2PT) {
  8455. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  8456. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  8457. iocbq->vport->fc_myDID);
  8458. if (iocbq->vport->fc_myDID == Fabric_DID) {
  8459. bf_set(wqe_els_did,
  8460. &wqe->xmit_els_rsp.wqe_dest, 0);
  8461. }
  8462. }
  8463. }
  8464. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  8465. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  8466. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  8467. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  8468. iocbq->iocb.unsli3.rcvsli3.ox_id);
  8469. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  8470. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  8471. phba->vpi_ids[iocbq->vport->vpi]);
  8472. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  8473. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  8474. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  8475. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  8476. LPFC_WQE_LENLOC_WORD3);
  8477. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  8478. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  8479. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  8480. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  8481. iocbq->context2)->virt);
  8482. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  8483. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  8484. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  8485. iocbq->vport->fc_myDID);
  8486. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  8487. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  8488. phba->vpi_ids[phba->pport->vpi]);
  8489. }
  8490. command_type = OTHER_COMMAND;
  8491. break;
  8492. case CMD_CLOSE_XRI_CN:
  8493. case CMD_ABORT_XRI_CN:
  8494. case CMD_ABORT_XRI_CX:
  8495. /* words 0-2 memcpy should be 0 rserved */
  8496. /* port will send abts */
  8497. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  8498. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  8499. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  8500. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  8501. } else
  8502. fip = 0;
  8503. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  8504. /*
  8505. * The link is down, or the command was ELS_FIP
  8506. * so the fw does not need to send abts
  8507. * on the wire.
  8508. */
  8509. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  8510. else
  8511. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  8512. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  8513. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  8514. wqe->abort_cmd.rsrvd5 = 0;
  8515. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  8516. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  8517. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  8518. /*
  8519. * The abort handler will send us CMD_ABORT_XRI_CN or
  8520. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  8521. */
  8522. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  8523. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  8524. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  8525. LPFC_WQE_LENLOC_NONE);
  8526. cmnd = CMD_ABORT_XRI_CX;
  8527. command_type = OTHER_COMMAND;
  8528. xritag = 0;
  8529. break;
  8530. case CMD_XMIT_BLS_RSP64_CX:
  8531. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  8532. /* As BLS ABTS RSP WQE is very different from other WQEs,
  8533. * we re-construct this WQE here based on information in
  8534. * iocbq from scratch.
  8535. */
  8536. memset(wqe, 0, sizeof(union lpfc_wqe));
  8537. /* OX_ID is invariable to who sent ABTS to CT exchange */
  8538. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  8539. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  8540. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  8541. LPFC_ABTS_UNSOL_INT) {
  8542. /* ABTS sent by initiator to CT exchange, the
  8543. * RX_ID field will be filled with the newly
  8544. * allocated responder XRI.
  8545. */
  8546. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  8547. iocbq->sli4_xritag);
  8548. } else {
  8549. /* ABTS sent by responder to CT exchange, the
  8550. * RX_ID field will be filled with the responder
  8551. * RX_ID from ABTS.
  8552. */
  8553. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  8554. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  8555. }
  8556. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  8557. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  8558. /* Use CT=VPI */
  8559. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  8560. ndlp->nlp_DID);
  8561. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  8562. iocbq->iocb.ulpContext);
  8563. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  8564. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  8565. phba->vpi_ids[phba->pport->vpi]);
  8566. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  8567. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  8568. LPFC_WQE_LENLOC_NONE);
  8569. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  8570. command_type = OTHER_COMMAND;
  8571. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  8572. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  8573. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  8574. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  8575. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  8576. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  8577. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  8578. }
  8579. break;
  8580. case CMD_SEND_FRAME:
  8581. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  8582. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  8583. return 0;
  8584. case CMD_XRI_ABORTED_CX:
  8585. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  8586. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  8587. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  8588. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  8589. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  8590. default:
  8591. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8592. "2014 Invalid command 0x%x\n",
  8593. iocbq->iocb.ulpCommand);
  8594. return IOCB_ERROR;
  8595. break;
  8596. }
  8597. if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
  8598. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  8599. else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
  8600. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  8601. else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
  8602. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  8603. iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
  8604. LPFC_IO_DIF_INSERT);
  8605. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  8606. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  8607. wqe->generic.wqe_com.abort_tag = abort_tag;
  8608. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  8609. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  8610. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  8611. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  8612. return 0;
  8613. }
  8614. /**
  8615. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  8616. * @phba: Pointer to HBA context object.
  8617. * @ring_number: SLI ring number to issue iocb on.
  8618. * @piocb: Pointer to command iocb.
  8619. * @flag: Flag indicating if this command can be put into txq.
  8620. *
  8621. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  8622. * an iocb command to an HBA with SLI-4 interface spec.
  8623. *
  8624. * This function is called with hbalock held. The function will return success
  8625. * after it successfully submit the iocb to firmware or after adding to the
  8626. * txq.
  8627. **/
  8628. static int
  8629. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  8630. struct lpfc_iocbq *piocb, uint32_t flag)
  8631. {
  8632. struct lpfc_sglq *sglq;
  8633. union lpfc_wqe128 wqe;
  8634. struct lpfc_queue *wq;
  8635. struct lpfc_sli_ring *pring;
  8636. /* Get the WQ */
  8637. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  8638. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  8639. if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS)))
  8640. wq = phba->sli4_hba.fcp_wq[piocb->hba_wqidx];
  8641. else
  8642. wq = phba->sli4_hba.oas_wq;
  8643. } else {
  8644. wq = phba->sli4_hba.els_wq;
  8645. }
  8646. /* Get corresponding ring */
  8647. pring = wq->pring;
  8648. /*
  8649. * The WQE can be either 64 or 128 bytes,
  8650. */
  8651. lockdep_assert_held(&phba->hbalock);
  8652. if (piocb->sli4_xritag == NO_XRI) {
  8653. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  8654. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  8655. sglq = NULL;
  8656. else {
  8657. if (!list_empty(&pring->txq)) {
  8658. if (!(flag & SLI_IOCB_RET_IOCB)) {
  8659. __lpfc_sli_ringtx_put(phba,
  8660. pring, piocb);
  8661. return IOCB_SUCCESS;
  8662. } else {
  8663. return IOCB_BUSY;
  8664. }
  8665. } else {
  8666. sglq = __lpfc_sli_get_els_sglq(phba, piocb);
  8667. if (!sglq) {
  8668. if (!(flag & SLI_IOCB_RET_IOCB)) {
  8669. __lpfc_sli_ringtx_put(phba,
  8670. pring,
  8671. piocb);
  8672. return IOCB_SUCCESS;
  8673. } else
  8674. return IOCB_BUSY;
  8675. }
  8676. }
  8677. }
  8678. } else if (piocb->iocb_flag & LPFC_IO_FCP)
  8679. /* These IO's already have an XRI and a mapped sgl. */
  8680. sglq = NULL;
  8681. else {
  8682. /*
  8683. * This is a continuation of a commandi,(CX) so this
  8684. * sglq is on the active list
  8685. */
  8686. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
  8687. if (!sglq)
  8688. return IOCB_ERROR;
  8689. }
  8690. if (sglq) {
  8691. piocb->sli4_lxritag = sglq->sli4_lxritag;
  8692. piocb->sli4_xritag = sglq->sli4_xritag;
  8693. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  8694. return IOCB_ERROR;
  8695. }
  8696. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  8697. return IOCB_ERROR;
  8698. if (lpfc_sli4_wq_put(wq, &wqe))
  8699. return IOCB_ERROR;
  8700. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  8701. return 0;
  8702. }
  8703. /**
  8704. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  8705. *
  8706. * This routine wraps the actual lockless version for issusing IOCB function
  8707. * pointer from the lpfc_hba struct.
  8708. *
  8709. * Return codes:
  8710. * IOCB_ERROR - Error
  8711. * IOCB_SUCCESS - Success
  8712. * IOCB_BUSY - Busy
  8713. **/
  8714. int
  8715. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  8716. struct lpfc_iocbq *piocb, uint32_t flag)
  8717. {
  8718. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8719. }
  8720. /**
  8721. * lpfc_sli_api_table_setup - Set up sli api function jump table
  8722. * @phba: The hba struct for which this call is being executed.
  8723. * @dev_grp: The HBA PCI-Device group number.
  8724. *
  8725. * This routine sets up the SLI interface API function jump table in @phba
  8726. * struct.
  8727. * Returns: 0 - success, -ENODEV - failure.
  8728. **/
  8729. int
  8730. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  8731. {
  8732. switch (dev_grp) {
  8733. case LPFC_PCI_DEV_LP:
  8734. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  8735. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  8736. break;
  8737. case LPFC_PCI_DEV_OC:
  8738. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  8739. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  8740. break;
  8741. default:
  8742. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8743. "1419 Invalid HBA PCI-device group: 0x%x\n",
  8744. dev_grp);
  8745. return -ENODEV;
  8746. break;
  8747. }
  8748. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  8749. return 0;
  8750. }
  8751. /**
  8752. * lpfc_sli4_calc_ring - Calculates which ring to use
  8753. * @phba: Pointer to HBA context object.
  8754. * @piocb: Pointer to command iocb.
  8755. *
  8756. * For SLI4 only, FCP IO can deferred to one fo many WQs, based on
  8757. * hba_wqidx, thus we need to calculate the corresponding ring.
  8758. * Since ABORTS must go on the same WQ of the command they are
  8759. * aborting, we use command's hba_wqidx.
  8760. */
  8761. struct lpfc_sli_ring *
  8762. lpfc_sli4_calc_ring(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
  8763. {
  8764. if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
  8765. if (!(phba->cfg_fof) ||
  8766. (!(piocb->iocb_flag & LPFC_IO_FOF))) {
  8767. if (unlikely(!phba->sli4_hba.fcp_wq))
  8768. return NULL;
  8769. /*
  8770. * for abort iocb hba_wqidx should already
  8771. * be setup based on what work queue we used.
  8772. */
  8773. if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  8774. piocb->hba_wqidx =
  8775. lpfc_sli4_scmd_to_wqidx_distr(phba,
  8776. piocb->context1);
  8777. piocb->hba_wqidx = piocb->hba_wqidx %
  8778. phba->cfg_fcp_io_channel;
  8779. }
  8780. return phba->sli4_hba.fcp_wq[piocb->hba_wqidx]->pring;
  8781. } else {
  8782. if (unlikely(!phba->sli4_hba.oas_wq))
  8783. return NULL;
  8784. piocb->hba_wqidx = 0;
  8785. return phba->sli4_hba.oas_wq->pring;
  8786. }
  8787. } else {
  8788. if (unlikely(!phba->sli4_hba.els_wq))
  8789. return NULL;
  8790. piocb->hba_wqidx = 0;
  8791. return phba->sli4_hba.els_wq->pring;
  8792. }
  8793. }
  8794. /**
  8795. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  8796. * @phba: Pointer to HBA context object.
  8797. * @pring: Pointer to driver SLI ring object.
  8798. * @piocb: Pointer to command iocb.
  8799. * @flag: Flag indicating if this command can be put into txq.
  8800. *
  8801. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  8802. * function. This function gets the hbalock and calls
  8803. * __lpfc_sli_issue_iocb function and will return the error returned
  8804. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  8805. * functions which do not hold hbalock.
  8806. **/
  8807. int
  8808. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  8809. struct lpfc_iocbq *piocb, uint32_t flag)
  8810. {
  8811. struct lpfc_hba_eq_hdl *hba_eq_hdl;
  8812. struct lpfc_sli_ring *pring;
  8813. struct lpfc_queue *fpeq;
  8814. struct lpfc_eqe *eqe;
  8815. unsigned long iflags;
  8816. int rc, idx;
  8817. if (phba->sli_rev == LPFC_SLI_REV4) {
  8818. pring = lpfc_sli4_calc_ring(phba, piocb);
  8819. if (unlikely(pring == NULL))
  8820. return IOCB_ERROR;
  8821. spin_lock_irqsave(&pring->ring_lock, iflags);
  8822. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8823. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8824. if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
  8825. idx = piocb->hba_wqidx;
  8826. hba_eq_hdl = &phba->sli4_hba.hba_eq_hdl[idx];
  8827. if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use)) {
  8828. /* Get associated EQ with this index */
  8829. fpeq = phba->sli4_hba.hba_eq[idx];
  8830. /* Turn off interrupts from this EQ */
  8831. phba->sli4_hba.sli4_eq_clr_intr(fpeq);
  8832. /*
  8833. * Process all the events on FCP EQ
  8834. */
  8835. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  8836. lpfc_sli4_hba_handle_eqe(phba,
  8837. eqe, idx);
  8838. fpeq->EQ_processed++;
  8839. }
  8840. /* Always clear and re-arm the EQ */
  8841. phba->sli4_hba.sli4_eq_release(fpeq,
  8842. LPFC_QUEUE_REARM);
  8843. }
  8844. atomic_inc(&hba_eq_hdl->hba_eq_in_use);
  8845. }
  8846. } else {
  8847. /* For now, SLI2/3 will still use hbalock */
  8848. spin_lock_irqsave(&phba->hbalock, iflags);
  8849. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  8850. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8851. }
  8852. return rc;
  8853. }
  8854. /**
  8855. * lpfc_extra_ring_setup - Extra ring setup function
  8856. * @phba: Pointer to HBA context object.
  8857. *
  8858. * This function is called while driver attaches with the
  8859. * HBA to setup the extra ring. The extra ring is used
  8860. * only when driver needs to support target mode functionality
  8861. * or IP over FC functionalities.
  8862. *
  8863. * This function is called with no lock held. SLI3 only.
  8864. **/
  8865. static int
  8866. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  8867. {
  8868. struct lpfc_sli *psli;
  8869. struct lpfc_sli_ring *pring;
  8870. psli = &phba->sli;
  8871. /* Adjust cmd/rsp ring iocb entries more evenly */
  8872. /* Take some away from the FCP ring */
  8873. pring = &psli->sli3_ring[LPFC_FCP_RING];
  8874. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8875. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8876. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8877. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8878. /* and give them to the extra ring */
  8879. pring = &psli->sli3_ring[LPFC_EXTRA_RING];
  8880. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8881. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8882. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8883. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8884. /* Setup default profile for this ring */
  8885. pring->iotag_max = 4096;
  8886. pring->num_mask = 1;
  8887. pring->prt[0].profile = 0; /* Mask 0 */
  8888. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  8889. pring->prt[0].type = phba->cfg_multi_ring_type;
  8890. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  8891. return 0;
  8892. }
  8893. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  8894. * @phba: Pointer to HBA context object.
  8895. * @iocbq: Pointer to iocb object.
  8896. *
  8897. * The async_event handler calls this routine when it receives
  8898. * an ASYNC_STATUS_CN event from the port. The port generates
  8899. * this event when an Abort Sequence request to an rport fails
  8900. * twice in succession. The abort could be originated by the
  8901. * driver or by the port. The ABTS could have been for an ELS
  8902. * or FCP IO. The port only generates this event when an ABTS
  8903. * fails to complete after one retry.
  8904. */
  8905. static void
  8906. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  8907. struct lpfc_iocbq *iocbq)
  8908. {
  8909. struct lpfc_nodelist *ndlp = NULL;
  8910. uint16_t rpi = 0, vpi = 0;
  8911. struct lpfc_vport *vport = NULL;
  8912. /* The rpi in the ulpContext is vport-sensitive. */
  8913. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  8914. rpi = iocbq->iocb.ulpContext;
  8915. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8916. "3092 Port generated ABTS async event "
  8917. "on vpi %d rpi %d status 0x%x\n",
  8918. vpi, rpi, iocbq->iocb.ulpStatus);
  8919. vport = lpfc_find_vport_by_vpid(phba, vpi);
  8920. if (!vport)
  8921. goto err_exit;
  8922. ndlp = lpfc_findnode_rpi(vport, rpi);
  8923. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  8924. goto err_exit;
  8925. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  8926. lpfc_sli_abts_recover_port(vport, ndlp);
  8927. return;
  8928. err_exit:
  8929. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8930. "3095 Event Context not found, no "
  8931. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  8932. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  8933. vpi, rpi);
  8934. }
  8935. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  8936. * @phba: pointer to HBA context object.
  8937. * @ndlp: nodelist pointer for the impacted rport.
  8938. * @axri: pointer to the wcqe containing the failed exchange.
  8939. *
  8940. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  8941. * port. The port generates this event when an abort exchange request to an
  8942. * rport fails twice in succession with no reply. The abort could be originated
  8943. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  8944. */
  8945. void
  8946. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  8947. struct lpfc_nodelist *ndlp,
  8948. struct sli4_wcqe_xri_aborted *axri)
  8949. {
  8950. struct lpfc_vport *vport;
  8951. uint32_t ext_status = 0;
  8952. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  8953. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8954. "3115 Node Context not found, driver "
  8955. "ignoring abts err event\n");
  8956. return;
  8957. }
  8958. vport = ndlp->vport;
  8959. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  8960. "3116 Port generated FCP XRI ABORT event on "
  8961. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  8962. ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
  8963. bf_get(lpfc_wcqe_xa_xri, axri),
  8964. bf_get(lpfc_wcqe_xa_status, axri),
  8965. axri->parameter);
  8966. /*
  8967. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  8968. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  8969. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  8970. */
  8971. ext_status = axri->parameter & IOERR_PARAM_MASK;
  8972. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  8973. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  8974. lpfc_sli_abts_recover_port(vport, ndlp);
  8975. }
  8976. /**
  8977. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  8978. * @phba: Pointer to HBA context object.
  8979. * @pring: Pointer to driver SLI ring object.
  8980. * @iocbq: Pointer to iocb object.
  8981. *
  8982. * This function is called by the slow ring event handler
  8983. * function when there is an ASYNC event iocb in the ring.
  8984. * This function is called with no lock held.
  8985. * Currently this function handles only temperature related
  8986. * ASYNC events. The function decodes the temperature sensor
  8987. * event message and posts events for the management applications.
  8988. **/
  8989. static void
  8990. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  8991. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  8992. {
  8993. IOCB_t *icmd;
  8994. uint16_t evt_code;
  8995. struct temp_event temp_event_data;
  8996. struct Scsi_Host *shost;
  8997. uint32_t *iocb_w;
  8998. icmd = &iocbq->iocb;
  8999. evt_code = icmd->un.asyncstat.evt_code;
  9000. switch (evt_code) {
  9001. case ASYNC_TEMP_WARN:
  9002. case ASYNC_TEMP_SAFE:
  9003. temp_event_data.data = (uint32_t) icmd->ulpContext;
  9004. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  9005. if (evt_code == ASYNC_TEMP_WARN) {
  9006. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  9007. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  9008. "0347 Adapter is very hot, please take "
  9009. "corrective action. temperature : %d Celsius\n",
  9010. (uint32_t) icmd->ulpContext);
  9011. } else {
  9012. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  9013. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  9014. "0340 Adapter temperature is OK now. "
  9015. "temperature : %d Celsius\n",
  9016. (uint32_t) icmd->ulpContext);
  9017. }
  9018. /* Send temperature change event to applications */
  9019. shost = lpfc_shost_from_vport(phba->pport);
  9020. fc_host_post_vendor_event(shost, fc_get_event_number(),
  9021. sizeof(temp_event_data), (char *) &temp_event_data,
  9022. LPFC_NL_VENDOR_ID);
  9023. break;
  9024. case ASYNC_STATUS_CN:
  9025. lpfc_sli_abts_err_handler(phba, iocbq);
  9026. break;
  9027. default:
  9028. iocb_w = (uint32_t *) icmd;
  9029. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9030. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  9031. " evt_code 0x%x\n"
  9032. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  9033. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  9034. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  9035. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  9036. pring->ringno, icmd->un.asyncstat.evt_code,
  9037. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  9038. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  9039. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  9040. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  9041. break;
  9042. }
  9043. }
  9044. /**
  9045. * lpfc_sli4_setup - SLI ring setup function
  9046. * @phba: Pointer to HBA context object.
  9047. *
  9048. * lpfc_sli_setup sets up rings of the SLI interface with
  9049. * number of iocbs per ring and iotags. This function is
  9050. * called while driver attach to the HBA and before the
  9051. * interrupts are enabled. So there is no need for locking.
  9052. *
  9053. * This function always returns 0.
  9054. **/
  9055. int
  9056. lpfc_sli4_setup(struct lpfc_hba *phba)
  9057. {
  9058. struct lpfc_sli_ring *pring;
  9059. pring = phba->sli4_hba.els_wq->pring;
  9060. pring->num_mask = LPFC_MAX_RING_MASK;
  9061. pring->prt[0].profile = 0; /* Mask 0 */
  9062. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  9063. pring->prt[0].type = FC_TYPE_ELS;
  9064. pring->prt[0].lpfc_sli_rcv_unsol_event =
  9065. lpfc_els_unsol_event;
  9066. pring->prt[1].profile = 0; /* Mask 1 */
  9067. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  9068. pring->prt[1].type = FC_TYPE_ELS;
  9069. pring->prt[1].lpfc_sli_rcv_unsol_event =
  9070. lpfc_els_unsol_event;
  9071. pring->prt[2].profile = 0; /* Mask 2 */
  9072. /* NameServer Inquiry */
  9073. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  9074. /* NameServer */
  9075. pring->prt[2].type = FC_TYPE_CT;
  9076. pring->prt[2].lpfc_sli_rcv_unsol_event =
  9077. lpfc_ct_unsol_event;
  9078. pring->prt[3].profile = 0; /* Mask 3 */
  9079. /* NameServer response */
  9080. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  9081. /* NameServer */
  9082. pring->prt[3].type = FC_TYPE_CT;
  9083. pring->prt[3].lpfc_sli_rcv_unsol_event =
  9084. lpfc_ct_unsol_event;
  9085. return 0;
  9086. }
  9087. /**
  9088. * lpfc_sli_setup - SLI ring setup function
  9089. * @phba: Pointer to HBA context object.
  9090. *
  9091. * lpfc_sli_setup sets up rings of the SLI interface with
  9092. * number of iocbs per ring and iotags. This function is
  9093. * called while driver attach to the HBA and before the
  9094. * interrupts are enabled. So there is no need for locking.
  9095. *
  9096. * This function always returns 0. SLI3 only.
  9097. **/
  9098. int
  9099. lpfc_sli_setup(struct lpfc_hba *phba)
  9100. {
  9101. int i, totiocbsize = 0;
  9102. struct lpfc_sli *psli = &phba->sli;
  9103. struct lpfc_sli_ring *pring;
  9104. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  9105. psli->sli_flag = 0;
  9106. psli->iocbq_lookup = NULL;
  9107. psli->iocbq_lookup_len = 0;
  9108. psli->last_iotag = 0;
  9109. for (i = 0; i < psli->num_rings; i++) {
  9110. pring = &psli->sli3_ring[i];
  9111. switch (i) {
  9112. case LPFC_FCP_RING: /* ring 0 - FCP */
  9113. /* numCiocb and numRiocb are used in config_port */
  9114. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  9115. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  9116. pring->sli.sli3.numCiocb +=
  9117. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  9118. pring->sli.sli3.numRiocb +=
  9119. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  9120. pring->sli.sli3.numCiocb +=
  9121. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  9122. pring->sli.sli3.numRiocb +=
  9123. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  9124. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  9125. SLI3_IOCB_CMD_SIZE :
  9126. SLI2_IOCB_CMD_SIZE;
  9127. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  9128. SLI3_IOCB_RSP_SIZE :
  9129. SLI2_IOCB_RSP_SIZE;
  9130. pring->iotag_ctr = 0;
  9131. pring->iotag_max =
  9132. (phba->cfg_hba_queue_depth * 2);
  9133. pring->fast_iotag = pring->iotag_max;
  9134. pring->num_mask = 0;
  9135. break;
  9136. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  9137. /* numCiocb and numRiocb are used in config_port */
  9138. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  9139. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  9140. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  9141. SLI3_IOCB_CMD_SIZE :
  9142. SLI2_IOCB_CMD_SIZE;
  9143. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  9144. SLI3_IOCB_RSP_SIZE :
  9145. SLI2_IOCB_RSP_SIZE;
  9146. pring->iotag_max = phba->cfg_hba_queue_depth;
  9147. pring->num_mask = 0;
  9148. break;
  9149. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  9150. /* numCiocb and numRiocb are used in config_port */
  9151. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  9152. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  9153. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  9154. SLI3_IOCB_CMD_SIZE :
  9155. SLI2_IOCB_CMD_SIZE;
  9156. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  9157. SLI3_IOCB_RSP_SIZE :
  9158. SLI2_IOCB_RSP_SIZE;
  9159. pring->fast_iotag = 0;
  9160. pring->iotag_ctr = 0;
  9161. pring->iotag_max = 4096;
  9162. pring->lpfc_sli_rcv_async_status =
  9163. lpfc_sli_async_event_handler;
  9164. pring->num_mask = LPFC_MAX_RING_MASK;
  9165. pring->prt[0].profile = 0; /* Mask 0 */
  9166. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  9167. pring->prt[0].type = FC_TYPE_ELS;
  9168. pring->prt[0].lpfc_sli_rcv_unsol_event =
  9169. lpfc_els_unsol_event;
  9170. pring->prt[1].profile = 0; /* Mask 1 */
  9171. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  9172. pring->prt[1].type = FC_TYPE_ELS;
  9173. pring->prt[1].lpfc_sli_rcv_unsol_event =
  9174. lpfc_els_unsol_event;
  9175. pring->prt[2].profile = 0; /* Mask 2 */
  9176. /* NameServer Inquiry */
  9177. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  9178. /* NameServer */
  9179. pring->prt[2].type = FC_TYPE_CT;
  9180. pring->prt[2].lpfc_sli_rcv_unsol_event =
  9181. lpfc_ct_unsol_event;
  9182. pring->prt[3].profile = 0; /* Mask 3 */
  9183. /* NameServer response */
  9184. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  9185. /* NameServer */
  9186. pring->prt[3].type = FC_TYPE_CT;
  9187. pring->prt[3].lpfc_sli_rcv_unsol_event =
  9188. lpfc_ct_unsol_event;
  9189. break;
  9190. }
  9191. totiocbsize += (pring->sli.sli3.numCiocb *
  9192. pring->sli.sli3.sizeCiocb) +
  9193. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  9194. }
  9195. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  9196. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  9197. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  9198. "SLI2 SLIM Data: x%x x%lx\n",
  9199. phba->brd_no, totiocbsize,
  9200. (unsigned long) MAX_SLIM_IOCB_SIZE);
  9201. }
  9202. if (phba->cfg_multi_ring_support == 2)
  9203. lpfc_extra_ring_setup(phba);
  9204. return 0;
  9205. }
  9206. /**
  9207. * lpfc_sli4_queue_init - Queue initialization function
  9208. * @phba: Pointer to HBA context object.
  9209. *
  9210. * lpfc_sli4_queue_init sets up mailbox queues and iocb queues for each
  9211. * ring. This function also initializes ring indices of each ring.
  9212. * This function is called during the initialization of the SLI
  9213. * interface of an HBA.
  9214. * This function is called with no lock held and always returns
  9215. * 1.
  9216. **/
  9217. void
  9218. lpfc_sli4_queue_init(struct lpfc_hba *phba)
  9219. {
  9220. struct lpfc_sli *psli;
  9221. struct lpfc_sli_ring *pring;
  9222. int i;
  9223. psli = &phba->sli;
  9224. spin_lock_irq(&phba->hbalock);
  9225. INIT_LIST_HEAD(&psli->mboxq);
  9226. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  9227. /* Initialize list headers for txq and txcmplq as double linked lists */
  9228. for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
  9229. pring = phba->sli4_hba.fcp_wq[i]->pring;
  9230. pring->flag = 0;
  9231. pring->ringno = LPFC_FCP_RING;
  9232. INIT_LIST_HEAD(&pring->txq);
  9233. INIT_LIST_HEAD(&pring->txcmplq);
  9234. INIT_LIST_HEAD(&pring->iocb_continueq);
  9235. spin_lock_init(&pring->ring_lock);
  9236. }
  9237. for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
  9238. pring = phba->sli4_hba.nvme_wq[i]->pring;
  9239. pring->flag = 0;
  9240. pring->ringno = LPFC_FCP_RING;
  9241. INIT_LIST_HEAD(&pring->txq);
  9242. INIT_LIST_HEAD(&pring->txcmplq);
  9243. INIT_LIST_HEAD(&pring->iocb_continueq);
  9244. spin_lock_init(&pring->ring_lock);
  9245. }
  9246. pring = phba->sli4_hba.els_wq->pring;
  9247. pring->flag = 0;
  9248. pring->ringno = LPFC_ELS_RING;
  9249. INIT_LIST_HEAD(&pring->txq);
  9250. INIT_LIST_HEAD(&pring->txcmplq);
  9251. INIT_LIST_HEAD(&pring->iocb_continueq);
  9252. spin_lock_init(&pring->ring_lock);
  9253. if (phba->cfg_nvme_io_channel) {
  9254. pring = phba->sli4_hba.nvmels_wq->pring;
  9255. pring->flag = 0;
  9256. pring->ringno = LPFC_ELS_RING;
  9257. INIT_LIST_HEAD(&pring->txq);
  9258. INIT_LIST_HEAD(&pring->txcmplq);
  9259. INIT_LIST_HEAD(&pring->iocb_continueq);
  9260. spin_lock_init(&pring->ring_lock);
  9261. }
  9262. if (phba->cfg_fof) {
  9263. pring = phba->sli4_hba.oas_wq->pring;
  9264. pring->flag = 0;
  9265. pring->ringno = LPFC_FCP_RING;
  9266. INIT_LIST_HEAD(&pring->txq);
  9267. INIT_LIST_HEAD(&pring->txcmplq);
  9268. INIT_LIST_HEAD(&pring->iocb_continueq);
  9269. spin_lock_init(&pring->ring_lock);
  9270. }
  9271. spin_unlock_irq(&phba->hbalock);
  9272. }
  9273. /**
  9274. * lpfc_sli_queue_init - Queue initialization function
  9275. * @phba: Pointer to HBA context object.
  9276. *
  9277. * lpfc_sli_queue_init sets up mailbox queues and iocb queues for each
  9278. * ring. This function also initializes ring indices of each ring.
  9279. * This function is called during the initialization of the SLI
  9280. * interface of an HBA.
  9281. * This function is called with no lock held and always returns
  9282. * 1.
  9283. **/
  9284. void
  9285. lpfc_sli_queue_init(struct lpfc_hba *phba)
  9286. {
  9287. struct lpfc_sli *psli;
  9288. struct lpfc_sli_ring *pring;
  9289. int i;
  9290. psli = &phba->sli;
  9291. spin_lock_irq(&phba->hbalock);
  9292. INIT_LIST_HEAD(&psli->mboxq);
  9293. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  9294. /* Initialize list headers for txq and txcmplq as double linked lists */
  9295. for (i = 0; i < psli->num_rings; i++) {
  9296. pring = &psli->sli3_ring[i];
  9297. pring->ringno = i;
  9298. pring->sli.sli3.next_cmdidx = 0;
  9299. pring->sli.sli3.local_getidx = 0;
  9300. pring->sli.sli3.cmdidx = 0;
  9301. INIT_LIST_HEAD(&pring->iocb_continueq);
  9302. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  9303. INIT_LIST_HEAD(&pring->postbufq);
  9304. pring->flag = 0;
  9305. INIT_LIST_HEAD(&pring->txq);
  9306. INIT_LIST_HEAD(&pring->txcmplq);
  9307. spin_lock_init(&pring->ring_lock);
  9308. }
  9309. spin_unlock_irq(&phba->hbalock);
  9310. }
  9311. /**
  9312. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  9313. * @phba: Pointer to HBA context object.
  9314. *
  9315. * This routine flushes the mailbox command subsystem. It will unconditionally
  9316. * flush all the mailbox commands in the three possible stages in the mailbox
  9317. * command sub-system: pending mailbox command queue; the outstanding mailbox
  9318. * command; and completed mailbox command queue. It is caller's responsibility
  9319. * to make sure that the driver is in the proper state to flush the mailbox
  9320. * command sub-system. Namely, the posting of mailbox commands into the
  9321. * pending mailbox command queue from the various clients must be stopped;
  9322. * either the HBA is in a state that it will never works on the outstanding
  9323. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  9324. * mailbox command has been completed.
  9325. **/
  9326. static void
  9327. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  9328. {
  9329. LIST_HEAD(completions);
  9330. struct lpfc_sli *psli = &phba->sli;
  9331. LPFC_MBOXQ_t *pmb;
  9332. unsigned long iflag;
  9333. /* Flush all the mailbox commands in the mbox system */
  9334. spin_lock_irqsave(&phba->hbalock, iflag);
  9335. /* The pending mailbox command queue */
  9336. list_splice_init(&phba->sli.mboxq, &completions);
  9337. /* The outstanding active mailbox command */
  9338. if (psli->mbox_active) {
  9339. list_add_tail(&psli->mbox_active->list, &completions);
  9340. psli->mbox_active = NULL;
  9341. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  9342. }
  9343. /* The completed mailbox command queue */
  9344. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  9345. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9346. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  9347. while (!list_empty(&completions)) {
  9348. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  9349. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  9350. if (pmb->mbox_cmpl)
  9351. pmb->mbox_cmpl(phba, pmb);
  9352. }
  9353. }
  9354. /**
  9355. * lpfc_sli_host_down - Vport cleanup function
  9356. * @vport: Pointer to virtual port object.
  9357. *
  9358. * lpfc_sli_host_down is called to clean up the resources
  9359. * associated with a vport before destroying virtual
  9360. * port data structures.
  9361. * This function does following operations:
  9362. * - Free discovery resources associated with this virtual
  9363. * port.
  9364. * - Free iocbs associated with this virtual port in
  9365. * the txq.
  9366. * - Send abort for all iocb commands associated with this
  9367. * vport in txcmplq.
  9368. *
  9369. * This function is called with no lock held and always returns 1.
  9370. **/
  9371. int
  9372. lpfc_sli_host_down(struct lpfc_vport *vport)
  9373. {
  9374. LIST_HEAD(completions);
  9375. struct lpfc_hba *phba = vport->phba;
  9376. struct lpfc_sli *psli = &phba->sli;
  9377. struct lpfc_queue *qp = NULL;
  9378. struct lpfc_sli_ring *pring;
  9379. struct lpfc_iocbq *iocb, *next_iocb;
  9380. int i;
  9381. unsigned long flags = 0;
  9382. uint16_t prev_pring_flag;
  9383. lpfc_cleanup_discovery_resources(vport);
  9384. spin_lock_irqsave(&phba->hbalock, flags);
  9385. /*
  9386. * Error everything on the txq since these iocbs
  9387. * have not been given to the FW yet.
  9388. * Also issue ABTS for everything on the txcmplq
  9389. */
  9390. if (phba->sli_rev != LPFC_SLI_REV4) {
  9391. for (i = 0; i < psli->num_rings; i++) {
  9392. pring = &psli->sli3_ring[i];
  9393. prev_pring_flag = pring->flag;
  9394. /* Only slow rings */
  9395. if (pring->ringno == LPFC_ELS_RING) {
  9396. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  9397. /* Set the lpfc data pending flag */
  9398. set_bit(LPFC_DATA_READY, &phba->data_flags);
  9399. }
  9400. list_for_each_entry_safe(iocb, next_iocb,
  9401. &pring->txq, list) {
  9402. if (iocb->vport != vport)
  9403. continue;
  9404. list_move_tail(&iocb->list, &completions);
  9405. }
  9406. list_for_each_entry_safe(iocb, next_iocb,
  9407. &pring->txcmplq, list) {
  9408. if (iocb->vport != vport)
  9409. continue;
  9410. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  9411. }
  9412. pring->flag = prev_pring_flag;
  9413. }
  9414. } else {
  9415. list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
  9416. pring = qp->pring;
  9417. if (!pring)
  9418. continue;
  9419. if (pring == phba->sli4_hba.els_wq->pring) {
  9420. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  9421. /* Set the lpfc data pending flag */
  9422. set_bit(LPFC_DATA_READY, &phba->data_flags);
  9423. }
  9424. prev_pring_flag = pring->flag;
  9425. spin_lock_irq(&pring->ring_lock);
  9426. list_for_each_entry_safe(iocb, next_iocb,
  9427. &pring->txq, list) {
  9428. if (iocb->vport != vport)
  9429. continue;
  9430. list_move_tail(&iocb->list, &completions);
  9431. }
  9432. spin_unlock_irq(&pring->ring_lock);
  9433. list_for_each_entry_safe(iocb, next_iocb,
  9434. &pring->txcmplq, list) {
  9435. if (iocb->vport != vport)
  9436. continue;
  9437. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  9438. }
  9439. pring->flag = prev_pring_flag;
  9440. }
  9441. }
  9442. spin_unlock_irqrestore(&phba->hbalock, flags);
  9443. /* Cancel all the IOCBs from the completions list */
  9444. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  9445. IOERR_SLI_DOWN);
  9446. return 1;
  9447. }
  9448. /**
  9449. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  9450. * @phba: Pointer to HBA context object.
  9451. *
  9452. * This function cleans up all iocb, buffers, mailbox commands
  9453. * while shutting down the HBA. This function is called with no
  9454. * lock held and always returns 1.
  9455. * This function does the following to cleanup driver resources:
  9456. * - Free discovery resources for each virtual port
  9457. * - Cleanup any pending fabric iocbs
  9458. * - Iterate through the iocb txq and free each entry
  9459. * in the list.
  9460. * - Free up any buffer posted to the HBA
  9461. * - Free mailbox commands in the mailbox queue.
  9462. **/
  9463. int
  9464. lpfc_sli_hba_down(struct lpfc_hba *phba)
  9465. {
  9466. LIST_HEAD(completions);
  9467. struct lpfc_sli *psli = &phba->sli;
  9468. struct lpfc_queue *qp = NULL;
  9469. struct lpfc_sli_ring *pring;
  9470. struct lpfc_dmabuf *buf_ptr;
  9471. unsigned long flags = 0;
  9472. int i;
  9473. /* Shutdown the mailbox command sub-system */
  9474. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  9475. lpfc_hba_down_prep(phba);
  9476. lpfc_fabric_abort_hba(phba);
  9477. spin_lock_irqsave(&phba->hbalock, flags);
  9478. /*
  9479. * Error everything on the txq since these iocbs
  9480. * have not been given to the FW yet.
  9481. */
  9482. if (phba->sli_rev != LPFC_SLI_REV4) {
  9483. for (i = 0; i < psli->num_rings; i++) {
  9484. pring = &psli->sli3_ring[i];
  9485. /* Only slow rings */
  9486. if (pring->ringno == LPFC_ELS_RING) {
  9487. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  9488. /* Set the lpfc data pending flag */
  9489. set_bit(LPFC_DATA_READY, &phba->data_flags);
  9490. }
  9491. list_splice_init(&pring->txq, &completions);
  9492. }
  9493. } else {
  9494. list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
  9495. pring = qp->pring;
  9496. if (!pring)
  9497. continue;
  9498. spin_lock_irq(&pring->ring_lock);
  9499. list_splice_init(&pring->txq, &completions);
  9500. spin_unlock_irq(&pring->ring_lock);
  9501. if (pring == phba->sli4_hba.els_wq->pring) {
  9502. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  9503. /* Set the lpfc data pending flag */
  9504. set_bit(LPFC_DATA_READY, &phba->data_flags);
  9505. }
  9506. }
  9507. }
  9508. spin_unlock_irqrestore(&phba->hbalock, flags);
  9509. /* Cancel all the IOCBs from the completions list */
  9510. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  9511. IOERR_SLI_DOWN);
  9512. spin_lock_irqsave(&phba->hbalock, flags);
  9513. list_splice_init(&phba->elsbuf, &completions);
  9514. phba->elsbuf_cnt = 0;
  9515. phba->elsbuf_prev_cnt = 0;
  9516. spin_unlock_irqrestore(&phba->hbalock, flags);
  9517. while (!list_empty(&completions)) {
  9518. list_remove_head(&completions, buf_ptr,
  9519. struct lpfc_dmabuf, list);
  9520. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  9521. kfree(buf_ptr);
  9522. }
  9523. /* Return any active mbox cmds */
  9524. del_timer_sync(&psli->mbox_tmo);
  9525. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  9526. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  9527. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  9528. return 1;
  9529. }
  9530. /**
  9531. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  9532. * @srcp: Source memory pointer.
  9533. * @destp: Destination memory pointer.
  9534. * @cnt: Number of words required to be copied.
  9535. *
  9536. * This function is used for copying data between driver memory
  9537. * and the SLI memory. This function also changes the endianness
  9538. * of each word if native endianness is different from SLI
  9539. * endianness. This function can be called with or without
  9540. * lock.
  9541. **/
  9542. void
  9543. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  9544. {
  9545. uint32_t *src = srcp;
  9546. uint32_t *dest = destp;
  9547. uint32_t ldata;
  9548. int i;
  9549. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  9550. ldata = *src;
  9551. ldata = le32_to_cpu(ldata);
  9552. *dest = ldata;
  9553. src++;
  9554. dest++;
  9555. }
  9556. }
  9557. /**
  9558. * lpfc_sli_bemem_bcopy - SLI memory copy function
  9559. * @srcp: Source memory pointer.
  9560. * @destp: Destination memory pointer.
  9561. * @cnt: Number of words required to be copied.
  9562. *
  9563. * This function is used for copying data between a data structure
  9564. * with big endian representation to local endianness.
  9565. * This function can be called with or without lock.
  9566. **/
  9567. void
  9568. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  9569. {
  9570. uint32_t *src = srcp;
  9571. uint32_t *dest = destp;
  9572. uint32_t ldata;
  9573. int i;
  9574. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  9575. ldata = *src;
  9576. ldata = be32_to_cpu(ldata);
  9577. *dest = ldata;
  9578. src++;
  9579. dest++;
  9580. }
  9581. }
  9582. /**
  9583. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  9584. * @phba: Pointer to HBA context object.
  9585. * @pring: Pointer to driver SLI ring object.
  9586. * @mp: Pointer to driver buffer object.
  9587. *
  9588. * This function is called with no lock held.
  9589. * It always return zero after adding the buffer to the postbufq
  9590. * buffer list.
  9591. **/
  9592. int
  9593. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9594. struct lpfc_dmabuf *mp)
  9595. {
  9596. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  9597. later */
  9598. spin_lock_irq(&phba->hbalock);
  9599. list_add_tail(&mp->list, &pring->postbufq);
  9600. pring->postbufq_cnt++;
  9601. spin_unlock_irq(&phba->hbalock);
  9602. return 0;
  9603. }
  9604. /**
  9605. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  9606. * @phba: Pointer to HBA context object.
  9607. *
  9608. * When HBQ is enabled, buffers are searched based on tags. This function
  9609. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  9610. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  9611. * does not conflict with tags of buffer posted for unsolicited events.
  9612. * The function returns the allocated tag. The function is called with
  9613. * no locks held.
  9614. **/
  9615. uint32_t
  9616. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  9617. {
  9618. spin_lock_irq(&phba->hbalock);
  9619. phba->buffer_tag_count++;
  9620. /*
  9621. * Always set the QUE_BUFTAG_BIT to distiguish between
  9622. * a tag assigned by HBQ.
  9623. */
  9624. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  9625. spin_unlock_irq(&phba->hbalock);
  9626. return phba->buffer_tag_count;
  9627. }
  9628. /**
  9629. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  9630. * @phba: Pointer to HBA context object.
  9631. * @pring: Pointer to driver SLI ring object.
  9632. * @tag: Buffer tag.
  9633. *
  9634. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  9635. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  9636. * iocb is posted to the response ring with the tag of the buffer.
  9637. * This function searches the pring->postbufq list using the tag
  9638. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  9639. * iocb. If the buffer is found then lpfc_dmabuf object of the
  9640. * buffer is returned to the caller else NULL is returned.
  9641. * This function is called with no lock held.
  9642. **/
  9643. struct lpfc_dmabuf *
  9644. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9645. uint32_t tag)
  9646. {
  9647. struct lpfc_dmabuf *mp, *next_mp;
  9648. struct list_head *slp = &pring->postbufq;
  9649. /* Search postbufq, from the beginning, looking for a match on tag */
  9650. spin_lock_irq(&phba->hbalock);
  9651. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  9652. if (mp->buffer_tag == tag) {
  9653. list_del_init(&mp->list);
  9654. pring->postbufq_cnt--;
  9655. spin_unlock_irq(&phba->hbalock);
  9656. return mp;
  9657. }
  9658. }
  9659. spin_unlock_irq(&phba->hbalock);
  9660. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9661. "0402 Cannot find virtual addr for buffer tag on "
  9662. "ring %d Data x%lx x%p x%p x%x\n",
  9663. pring->ringno, (unsigned long) tag,
  9664. slp->next, slp->prev, pring->postbufq_cnt);
  9665. return NULL;
  9666. }
  9667. /**
  9668. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  9669. * @phba: Pointer to HBA context object.
  9670. * @pring: Pointer to driver SLI ring object.
  9671. * @phys: DMA address of the buffer.
  9672. *
  9673. * This function searches the buffer list using the dma_address
  9674. * of unsolicited event to find the driver's lpfc_dmabuf object
  9675. * corresponding to the dma_address. The function returns the
  9676. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  9677. * This function is called by the ct and els unsolicited event
  9678. * handlers to get the buffer associated with the unsolicited
  9679. * event.
  9680. *
  9681. * This function is called with no lock held.
  9682. **/
  9683. struct lpfc_dmabuf *
  9684. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9685. dma_addr_t phys)
  9686. {
  9687. struct lpfc_dmabuf *mp, *next_mp;
  9688. struct list_head *slp = &pring->postbufq;
  9689. /* Search postbufq, from the beginning, looking for a match on phys */
  9690. spin_lock_irq(&phba->hbalock);
  9691. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  9692. if (mp->phys == phys) {
  9693. list_del_init(&mp->list);
  9694. pring->postbufq_cnt--;
  9695. spin_unlock_irq(&phba->hbalock);
  9696. return mp;
  9697. }
  9698. }
  9699. spin_unlock_irq(&phba->hbalock);
  9700. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9701. "0410 Cannot find virtual addr for mapped buf on "
  9702. "ring %d Data x%llx x%p x%p x%x\n",
  9703. pring->ringno, (unsigned long long)phys,
  9704. slp->next, slp->prev, pring->postbufq_cnt);
  9705. return NULL;
  9706. }
  9707. /**
  9708. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  9709. * @phba: Pointer to HBA context object.
  9710. * @cmdiocb: Pointer to driver command iocb object.
  9711. * @rspiocb: Pointer to driver response iocb object.
  9712. *
  9713. * This function is the completion handler for the abort iocbs for
  9714. * ELS commands. This function is called from the ELS ring event
  9715. * handler with no lock held. This function frees memory resources
  9716. * associated with the abort iocb.
  9717. **/
  9718. static void
  9719. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  9720. struct lpfc_iocbq *rspiocb)
  9721. {
  9722. IOCB_t *irsp = &rspiocb->iocb;
  9723. uint16_t abort_iotag, abort_context;
  9724. struct lpfc_iocbq *abort_iocb = NULL;
  9725. if (irsp->ulpStatus) {
  9726. /*
  9727. * Assume that the port already completed and returned, or
  9728. * will return the iocb. Just Log the message.
  9729. */
  9730. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  9731. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  9732. spin_lock_irq(&phba->hbalock);
  9733. if (phba->sli_rev < LPFC_SLI_REV4) {
  9734. if (irsp->ulpCommand == CMD_ABORT_XRI_CX &&
  9735. irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
  9736. irsp->un.ulpWord[4] == IOERR_ABORT_REQUESTED) {
  9737. spin_unlock_irq(&phba->hbalock);
  9738. goto release_iocb;
  9739. }
  9740. if (abort_iotag != 0 &&
  9741. abort_iotag <= phba->sli.last_iotag)
  9742. abort_iocb =
  9743. phba->sli.iocbq_lookup[abort_iotag];
  9744. } else
  9745. /* For sli4 the abort_tag is the XRI,
  9746. * so the abort routine puts the iotag of the iocb
  9747. * being aborted in the context field of the abort
  9748. * IOCB.
  9749. */
  9750. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  9751. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  9752. "0327 Cannot abort els iocb %p "
  9753. "with tag %x context %x, abort status %x, "
  9754. "abort code %x\n",
  9755. abort_iocb, abort_iotag, abort_context,
  9756. irsp->ulpStatus, irsp->un.ulpWord[4]);
  9757. spin_unlock_irq(&phba->hbalock);
  9758. }
  9759. release_iocb:
  9760. lpfc_sli_release_iocbq(phba, cmdiocb);
  9761. return;
  9762. }
  9763. /**
  9764. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  9765. * @phba: Pointer to HBA context object.
  9766. * @cmdiocb: Pointer to driver command iocb object.
  9767. * @rspiocb: Pointer to driver response iocb object.
  9768. *
  9769. * The function is called from SLI ring event handler with no
  9770. * lock held. This function is the completion handler for ELS commands
  9771. * which are aborted. The function frees memory resources used for
  9772. * the aborted ELS commands.
  9773. **/
  9774. static void
  9775. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  9776. struct lpfc_iocbq *rspiocb)
  9777. {
  9778. IOCB_t *irsp = &rspiocb->iocb;
  9779. /* ELS cmd tag <ulpIoTag> completes */
  9780. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  9781. "0139 Ignoring ELS cmd tag x%x completion Data: "
  9782. "x%x x%x x%x\n",
  9783. irsp->ulpIoTag, irsp->ulpStatus,
  9784. irsp->un.ulpWord[4], irsp->ulpTimeout);
  9785. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  9786. lpfc_ct_free_iocb(phba, cmdiocb);
  9787. else
  9788. lpfc_els_free_iocb(phba, cmdiocb);
  9789. return;
  9790. }
  9791. /**
  9792. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  9793. * @phba: Pointer to HBA context object.
  9794. * @pring: Pointer to driver SLI ring object.
  9795. * @cmdiocb: Pointer to driver command iocb object.
  9796. *
  9797. * This function issues an abort iocb for the provided command iocb down to
  9798. * the port. Other than the case the outstanding command iocb is an abort
  9799. * request, this function issues abort out unconditionally. This function is
  9800. * called with hbalock held. The function returns 0 when it fails due to
  9801. * memory allocation failure or when the command iocb is an abort request.
  9802. **/
  9803. static int
  9804. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9805. struct lpfc_iocbq *cmdiocb)
  9806. {
  9807. struct lpfc_vport *vport = cmdiocb->vport;
  9808. struct lpfc_iocbq *abtsiocbp;
  9809. IOCB_t *icmd = NULL;
  9810. IOCB_t *iabt = NULL;
  9811. int retval;
  9812. unsigned long iflags;
  9813. struct lpfc_nodelist *ndlp;
  9814. lockdep_assert_held(&phba->hbalock);
  9815. /*
  9816. * There are certain command types we don't want to abort. And we
  9817. * don't want to abort commands that are already in the process of
  9818. * being aborted.
  9819. */
  9820. icmd = &cmdiocb->iocb;
  9821. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  9822. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  9823. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  9824. return 0;
  9825. /* issue ABTS for this IOCB based on iotag */
  9826. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  9827. if (abtsiocbp == NULL)
  9828. return 0;
  9829. /* This signals the response to set the correct status
  9830. * before calling the completion handler
  9831. */
  9832. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  9833. iabt = &abtsiocbp->iocb;
  9834. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  9835. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  9836. if (phba->sli_rev == LPFC_SLI_REV4) {
  9837. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  9838. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  9839. } else {
  9840. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  9841. if (pring->ringno == LPFC_ELS_RING) {
  9842. ndlp = (struct lpfc_nodelist *)(cmdiocb->context1);
  9843. iabt->un.acxri.abortContextTag = ndlp->nlp_rpi;
  9844. }
  9845. }
  9846. iabt->ulpLe = 1;
  9847. iabt->ulpClass = icmd->ulpClass;
  9848. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  9849. abtsiocbp->hba_wqidx = cmdiocb->hba_wqidx;
  9850. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  9851. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  9852. if (cmdiocb->iocb_flag & LPFC_IO_FOF)
  9853. abtsiocbp->iocb_flag |= LPFC_IO_FOF;
  9854. if (phba->link_state >= LPFC_LINK_UP)
  9855. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  9856. else
  9857. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  9858. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  9859. abtsiocbp->vport = vport;
  9860. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  9861. "0339 Abort xri x%x, original iotag x%x, "
  9862. "abort cmd iotag x%x\n",
  9863. iabt->un.acxri.abortIoTag,
  9864. iabt->un.acxri.abortContextTag,
  9865. abtsiocbp->iotag);
  9866. if (phba->sli_rev == LPFC_SLI_REV4) {
  9867. pring = lpfc_sli4_calc_ring(phba, abtsiocbp);
  9868. if (unlikely(pring == NULL))
  9869. return 0;
  9870. /* Note: both hbalock and ring_lock need to be set here */
  9871. spin_lock_irqsave(&pring->ring_lock, iflags);
  9872. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  9873. abtsiocbp, 0);
  9874. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  9875. } else {
  9876. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  9877. abtsiocbp, 0);
  9878. }
  9879. if (retval)
  9880. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  9881. /*
  9882. * Caller to this routine should check for IOCB_ERROR
  9883. * and handle it properly. This routine no longer removes
  9884. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  9885. */
  9886. return retval;
  9887. }
  9888. /**
  9889. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  9890. * @phba: Pointer to HBA context object.
  9891. * @pring: Pointer to driver SLI ring object.
  9892. * @cmdiocb: Pointer to driver command iocb object.
  9893. *
  9894. * This function issues an abort iocb for the provided command iocb. In case
  9895. * of unloading, the abort iocb will not be issued to commands on the ELS
  9896. * ring. Instead, the callback function shall be changed to those commands
  9897. * so that nothing happens when them finishes. This function is called with
  9898. * hbalock held. The function returns 0 when the command iocb is an abort
  9899. * request.
  9900. **/
  9901. int
  9902. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9903. struct lpfc_iocbq *cmdiocb)
  9904. {
  9905. struct lpfc_vport *vport = cmdiocb->vport;
  9906. int retval = IOCB_ERROR;
  9907. IOCB_t *icmd = NULL;
  9908. lockdep_assert_held(&phba->hbalock);
  9909. /*
  9910. * There are certain command types we don't want to abort. And we
  9911. * don't want to abort commands that are already in the process of
  9912. * being aborted.
  9913. */
  9914. icmd = &cmdiocb->iocb;
  9915. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  9916. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  9917. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  9918. return 0;
  9919. if (!pring) {
  9920. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  9921. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  9922. else
  9923. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  9924. goto abort_iotag_exit;
  9925. }
  9926. /*
  9927. * If we're unloading, don't abort iocb on the ELS ring, but change
  9928. * the callback so that nothing happens when it finishes.
  9929. */
  9930. if ((vport->load_flag & FC_UNLOADING) &&
  9931. (pring->ringno == LPFC_ELS_RING)) {
  9932. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  9933. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  9934. else
  9935. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  9936. goto abort_iotag_exit;
  9937. }
  9938. /* Now, we try to issue the abort to the cmdiocb out */
  9939. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  9940. abort_iotag_exit:
  9941. /*
  9942. * Caller to this routine should check for IOCB_ERROR
  9943. * and handle it properly. This routine no longer removes
  9944. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  9945. */
  9946. return retval;
  9947. }
  9948. /**
  9949. * lpfc_sli4_abort_nvme_io - Issue abort for a command iocb
  9950. * @phba: Pointer to HBA context object.
  9951. * @pring: Pointer to driver SLI ring object.
  9952. * @cmdiocb: Pointer to driver command iocb object.
  9953. *
  9954. * This function issues an abort iocb for the provided command iocb down to
  9955. * the port. Other than the case the outstanding command iocb is an abort
  9956. * request, this function issues abort out unconditionally. This function is
  9957. * called with hbalock held. The function returns 0 when it fails due to
  9958. * memory allocation failure or when the command iocb is an abort request.
  9959. **/
  9960. static int
  9961. lpfc_sli4_abort_nvme_io(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  9962. struct lpfc_iocbq *cmdiocb)
  9963. {
  9964. struct lpfc_vport *vport = cmdiocb->vport;
  9965. struct lpfc_iocbq *abtsiocbp;
  9966. union lpfc_wqe128 *abts_wqe;
  9967. int retval;
  9968. /*
  9969. * There are certain command types we don't want to abort. And we
  9970. * don't want to abort commands that are already in the process of
  9971. * being aborted.
  9972. */
  9973. if (cmdiocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  9974. cmdiocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
  9975. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  9976. return 0;
  9977. /* issue ABTS for this io based on iotag */
  9978. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  9979. if (abtsiocbp == NULL)
  9980. return 0;
  9981. /* This signals the response to set the correct status
  9982. * before calling the completion handler
  9983. */
  9984. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  9985. /* Complete prepping the abort wqe and issue to the FW. */
  9986. abts_wqe = &abtsiocbp->wqe;
  9987. /* Clear any stale WQE contents */
  9988. memset(abts_wqe, 0, sizeof(union lpfc_wqe));
  9989. bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
  9990. /* word 7 */
  9991. bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  9992. bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
  9993. cmdiocb->iocb.ulpClass);
  9994. /* word 8 - tell the FW to abort the IO associated with this
  9995. * outstanding exchange ID.
  9996. */
  9997. abts_wqe->abort_cmd.wqe_com.abort_tag = cmdiocb->sli4_xritag;
  9998. /* word 9 - this is the iotag for the abts_wqe completion. */
  9999. bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
  10000. abtsiocbp->iotag);
  10001. /* word 10 */
  10002. bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
  10003. bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
  10004. /* word 11 */
  10005. bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
  10006. bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
  10007. bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  10008. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  10009. abtsiocbp->iocb_flag |= LPFC_IO_NVME;
  10010. abtsiocbp->vport = vport;
  10011. abtsiocbp->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
  10012. retval = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abtsiocbp);
  10013. if (retval) {
  10014. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
  10015. "6147 Failed abts issue_wqe with status x%x "
  10016. "for oxid x%x\n",
  10017. retval, cmdiocb->sli4_xritag);
  10018. lpfc_sli_release_iocbq(phba, abtsiocbp);
  10019. return retval;
  10020. }
  10021. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
  10022. "6148 Drv Abort NVME Request Issued for "
  10023. "ox_id x%x on reqtag x%x\n",
  10024. cmdiocb->sli4_xritag,
  10025. abtsiocbp->iotag);
  10026. return retval;
  10027. }
  10028. /**
  10029. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  10030. * @phba: pointer to lpfc HBA data structure.
  10031. *
  10032. * This routine will abort all pending and outstanding iocbs to an HBA.
  10033. **/
  10034. void
  10035. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  10036. {
  10037. struct lpfc_sli *psli = &phba->sli;
  10038. struct lpfc_sli_ring *pring;
  10039. struct lpfc_queue *qp = NULL;
  10040. int i;
  10041. if (phba->sli_rev != LPFC_SLI_REV4) {
  10042. for (i = 0; i < psli->num_rings; i++) {
  10043. pring = &psli->sli3_ring[i];
  10044. lpfc_sli_abort_iocb_ring(phba, pring);
  10045. }
  10046. return;
  10047. }
  10048. list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
  10049. pring = qp->pring;
  10050. if (!pring)
  10051. continue;
  10052. lpfc_sli_abort_iocb_ring(phba, pring);
  10053. }
  10054. }
  10055. /**
  10056. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  10057. * @iocbq: Pointer to driver iocb object.
  10058. * @vport: Pointer to driver virtual port object.
  10059. * @tgt_id: SCSI ID of the target.
  10060. * @lun_id: LUN ID of the scsi device.
  10061. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  10062. *
  10063. * This function acts as an iocb filter for functions which abort or count
  10064. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  10065. * 0 if the filtering criteria is met for the given iocb and will return
  10066. * 1 if the filtering criteria is not met.
  10067. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  10068. * given iocb is for the SCSI device specified by vport, tgt_id and
  10069. * lun_id parameter.
  10070. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  10071. * given iocb is for the SCSI target specified by vport and tgt_id
  10072. * parameters.
  10073. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  10074. * given iocb is for the SCSI host associated with the given vport.
  10075. * This function is called with no locks held.
  10076. **/
  10077. static int
  10078. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  10079. uint16_t tgt_id, uint64_t lun_id,
  10080. lpfc_ctx_cmd ctx_cmd)
  10081. {
  10082. struct lpfc_scsi_buf *lpfc_cmd;
  10083. int rc = 1;
  10084. if (iocbq->vport != vport)
  10085. return rc;
  10086. if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
  10087. !(iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ))
  10088. return rc;
  10089. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  10090. if (lpfc_cmd->pCmd == NULL)
  10091. return rc;
  10092. switch (ctx_cmd) {
  10093. case LPFC_CTX_LUN:
  10094. if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
  10095. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  10096. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  10097. rc = 0;
  10098. break;
  10099. case LPFC_CTX_TGT:
  10100. if ((lpfc_cmd->rdata) && (lpfc_cmd->rdata->pnode) &&
  10101. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  10102. rc = 0;
  10103. break;
  10104. case LPFC_CTX_HOST:
  10105. rc = 0;
  10106. break;
  10107. default:
  10108. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  10109. __func__, ctx_cmd);
  10110. break;
  10111. }
  10112. return rc;
  10113. }
  10114. /**
  10115. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  10116. * @vport: Pointer to virtual port.
  10117. * @tgt_id: SCSI ID of the target.
  10118. * @lun_id: LUN ID of the scsi device.
  10119. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  10120. *
  10121. * This function returns number of FCP commands pending for the vport.
  10122. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  10123. * commands pending on the vport associated with SCSI device specified
  10124. * by tgt_id and lun_id parameters.
  10125. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  10126. * commands pending on the vport associated with SCSI target specified
  10127. * by tgt_id parameter.
  10128. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  10129. * commands pending on the vport.
  10130. * This function returns the number of iocbs which satisfy the filter.
  10131. * This function is called without any lock held.
  10132. **/
  10133. int
  10134. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  10135. lpfc_ctx_cmd ctx_cmd)
  10136. {
  10137. struct lpfc_hba *phba = vport->phba;
  10138. struct lpfc_iocbq *iocbq;
  10139. int sum, i;
  10140. spin_lock_irq(&phba->hbalock);
  10141. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  10142. iocbq = phba->sli.iocbq_lookup[i];
  10143. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  10144. ctx_cmd) == 0)
  10145. sum++;
  10146. }
  10147. spin_unlock_irq(&phba->hbalock);
  10148. return sum;
  10149. }
  10150. /**
  10151. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  10152. * @phba: Pointer to HBA context object
  10153. * @cmdiocb: Pointer to command iocb object.
  10154. * @rspiocb: Pointer to response iocb object.
  10155. *
  10156. * This function is called when an aborted FCP iocb completes. This
  10157. * function is called by the ring event handler with no lock held.
  10158. * This function frees the iocb.
  10159. **/
  10160. void
  10161. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  10162. struct lpfc_iocbq *rspiocb)
  10163. {
  10164. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10165. "3096 ABORT_XRI_CN completing on rpi x%x "
  10166. "original iotag x%x, abort cmd iotag x%x "
  10167. "status 0x%x, reason 0x%x\n",
  10168. cmdiocb->iocb.un.acxri.abortContextTag,
  10169. cmdiocb->iocb.un.acxri.abortIoTag,
  10170. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  10171. rspiocb->iocb.un.ulpWord[4]);
  10172. lpfc_sli_release_iocbq(phba, cmdiocb);
  10173. return;
  10174. }
  10175. /**
  10176. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  10177. * @vport: Pointer to virtual port.
  10178. * @pring: Pointer to driver SLI ring object.
  10179. * @tgt_id: SCSI ID of the target.
  10180. * @lun_id: LUN ID of the scsi device.
  10181. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  10182. *
  10183. * This function sends an abort command for every SCSI command
  10184. * associated with the given virtual port pending on the ring
  10185. * filtered by lpfc_sli_validate_fcp_iocb function.
  10186. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  10187. * FCP iocbs associated with lun specified by tgt_id and lun_id
  10188. * parameters
  10189. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  10190. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  10191. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  10192. * FCP iocbs associated with virtual port.
  10193. * This function returns number of iocbs it failed to abort.
  10194. * This function is called with no locks held.
  10195. **/
  10196. int
  10197. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  10198. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  10199. {
  10200. struct lpfc_hba *phba = vport->phba;
  10201. struct lpfc_iocbq *iocbq;
  10202. struct lpfc_iocbq *abtsiocb;
  10203. struct lpfc_sli_ring *pring_s4;
  10204. IOCB_t *cmd = NULL;
  10205. int errcnt = 0, ret_val = 0;
  10206. int i;
  10207. /* all I/Os are in process of being flushed */
  10208. if (phba->hba_flag & HBA_FCP_IOQ_FLUSH)
  10209. return errcnt;
  10210. for (i = 1; i <= phba->sli.last_iotag; i++) {
  10211. iocbq = phba->sli.iocbq_lookup[i];
  10212. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  10213. abort_cmd) != 0)
  10214. continue;
  10215. /*
  10216. * If the iocbq is already being aborted, don't take a second
  10217. * action, but do count it.
  10218. */
  10219. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  10220. continue;
  10221. /* issue ABTS for this IOCB based on iotag */
  10222. abtsiocb = lpfc_sli_get_iocbq(phba);
  10223. if (abtsiocb == NULL) {
  10224. errcnt++;
  10225. continue;
  10226. }
  10227. /* indicate the IO is being aborted by the driver. */
  10228. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  10229. cmd = &iocbq->iocb;
  10230. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  10231. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  10232. if (phba->sli_rev == LPFC_SLI_REV4)
  10233. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  10234. else
  10235. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  10236. abtsiocb->iocb.ulpLe = 1;
  10237. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  10238. abtsiocb->vport = vport;
  10239. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  10240. abtsiocb->hba_wqidx = iocbq->hba_wqidx;
  10241. if (iocbq->iocb_flag & LPFC_IO_FCP)
  10242. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  10243. if (iocbq->iocb_flag & LPFC_IO_FOF)
  10244. abtsiocb->iocb_flag |= LPFC_IO_FOF;
  10245. if (lpfc_is_link_up(phba))
  10246. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  10247. else
  10248. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  10249. /* Setup callback routine and issue the command. */
  10250. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  10251. if (phba->sli_rev == LPFC_SLI_REV4) {
  10252. pring_s4 = lpfc_sli4_calc_ring(phba, iocbq);
  10253. if (!pring_s4)
  10254. continue;
  10255. ret_val = lpfc_sli_issue_iocb(phba, pring_s4->ringno,
  10256. abtsiocb, 0);
  10257. } else
  10258. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  10259. abtsiocb, 0);
  10260. if (ret_val == IOCB_ERROR) {
  10261. lpfc_sli_release_iocbq(phba, abtsiocb);
  10262. errcnt++;
  10263. continue;
  10264. }
  10265. }
  10266. return errcnt;
  10267. }
  10268. /**
  10269. * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
  10270. * @vport: Pointer to virtual port.
  10271. * @pring: Pointer to driver SLI ring object.
  10272. * @tgt_id: SCSI ID of the target.
  10273. * @lun_id: LUN ID of the scsi device.
  10274. * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  10275. *
  10276. * This function sends an abort command for every SCSI command
  10277. * associated with the given virtual port pending on the ring
  10278. * filtered by lpfc_sli_validate_fcp_iocb function.
  10279. * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
  10280. * FCP iocbs associated with lun specified by tgt_id and lun_id
  10281. * parameters
  10282. * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
  10283. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  10284. * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
  10285. * FCP iocbs associated with virtual port.
  10286. * This function returns number of iocbs it aborted .
  10287. * This function is called with no locks held right after a taskmgmt
  10288. * command is sent.
  10289. **/
  10290. int
  10291. lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  10292. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
  10293. {
  10294. struct lpfc_hba *phba = vport->phba;
  10295. struct lpfc_scsi_buf *lpfc_cmd;
  10296. struct lpfc_iocbq *abtsiocbq;
  10297. struct lpfc_nodelist *ndlp;
  10298. struct lpfc_iocbq *iocbq;
  10299. IOCB_t *icmd;
  10300. int sum, i, ret_val;
  10301. unsigned long iflags;
  10302. struct lpfc_sli_ring *pring_s4;
  10303. spin_lock_irqsave(&phba->hbalock, iflags);
  10304. /* all I/Os are in process of being flushed */
  10305. if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
  10306. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10307. return 0;
  10308. }
  10309. sum = 0;
  10310. for (i = 1; i <= phba->sli.last_iotag; i++) {
  10311. iocbq = phba->sli.iocbq_lookup[i];
  10312. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  10313. cmd) != 0)
  10314. continue;
  10315. /*
  10316. * If the iocbq is already being aborted, don't take a second
  10317. * action, but do count it.
  10318. */
  10319. if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  10320. continue;
  10321. /* issue ABTS for this IOCB based on iotag */
  10322. abtsiocbq = __lpfc_sli_get_iocbq(phba);
  10323. if (abtsiocbq == NULL)
  10324. continue;
  10325. icmd = &iocbq->iocb;
  10326. abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  10327. abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
  10328. if (phba->sli_rev == LPFC_SLI_REV4)
  10329. abtsiocbq->iocb.un.acxri.abortIoTag =
  10330. iocbq->sli4_xritag;
  10331. else
  10332. abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
  10333. abtsiocbq->iocb.ulpLe = 1;
  10334. abtsiocbq->iocb.ulpClass = icmd->ulpClass;
  10335. abtsiocbq->vport = vport;
  10336. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  10337. abtsiocbq->hba_wqidx = iocbq->hba_wqidx;
  10338. if (iocbq->iocb_flag & LPFC_IO_FCP)
  10339. abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
  10340. if (iocbq->iocb_flag & LPFC_IO_FOF)
  10341. abtsiocbq->iocb_flag |= LPFC_IO_FOF;
  10342. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  10343. ndlp = lpfc_cmd->rdata->pnode;
  10344. if (lpfc_is_link_up(phba) &&
  10345. (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
  10346. abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  10347. else
  10348. abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  10349. /* Setup callback routine and issue the command. */
  10350. abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  10351. /*
  10352. * Indicate the IO is being aborted by the driver and set
  10353. * the caller's flag into the aborted IO.
  10354. */
  10355. iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
  10356. if (phba->sli_rev == LPFC_SLI_REV4) {
  10357. pring_s4 = lpfc_sli4_calc_ring(phba, abtsiocbq);
  10358. if (!pring_s4)
  10359. continue;
  10360. /* Note: both hbalock and ring_lock must be set here */
  10361. spin_lock(&pring_s4->ring_lock);
  10362. ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
  10363. abtsiocbq, 0);
  10364. spin_unlock(&pring_s4->ring_lock);
  10365. } else {
  10366. ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
  10367. abtsiocbq, 0);
  10368. }
  10369. if (ret_val == IOCB_ERROR)
  10370. __lpfc_sli_release_iocbq(phba, abtsiocbq);
  10371. else
  10372. sum++;
  10373. }
  10374. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10375. return sum;
  10376. }
  10377. /**
  10378. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  10379. * @phba: Pointer to HBA context object.
  10380. * @cmdiocbq: Pointer to command iocb.
  10381. * @rspiocbq: Pointer to response iocb.
  10382. *
  10383. * This function is the completion handler for iocbs issued using
  10384. * lpfc_sli_issue_iocb_wait function. This function is called by the
  10385. * ring event handler function without any lock held. This function
  10386. * can be called from both worker thread context and interrupt
  10387. * context. This function also can be called from other thread which
  10388. * cleans up the SLI layer objects.
  10389. * This function copy the contents of the response iocb to the
  10390. * response iocb memory object provided by the caller of
  10391. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  10392. * sleeps for the iocb completion.
  10393. **/
  10394. static void
  10395. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  10396. struct lpfc_iocbq *cmdiocbq,
  10397. struct lpfc_iocbq *rspiocbq)
  10398. {
  10399. wait_queue_head_t *pdone_q;
  10400. unsigned long iflags;
  10401. struct lpfc_scsi_buf *lpfc_cmd;
  10402. spin_lock_irqsave(&phba->hbalock, iflags);
  10403. if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
  10404. /*
  10405. * A time out has occurred for the iocb. If a time out
  10406. * completion handler has been supplied, call it. Otherwise,
  10407. * just free the iocbq.
  10408. */
  10409. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10410. cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
  10411. cmdiocbq->wait_iocb_cmpl = NULL;
  10412. if (cmdiocbq->iocb_cmpl)
  10413. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
  10414. else
  10415. lpfc_sli_release_iocbq(phba, cmdiocbq);
  10416. return;
  10417. }
  10418. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  10419. if (cmdiocbq->context2 && rspiocbq)
  10420. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  10421. &rspiocbq->iocb, sizeof(IOCB_t));
  10422. /* Set the exchange busy flag for task management commands */
  10423. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  10424. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  10425. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  10426. cur_iocbq);
  10427. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  10428. }
  10429. pdone_q = cmdiocbq->context_un.wait_queue;
  10430. if (pdone_q)
  10431. wake_up(pdone_q);
  10432. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10433. return;
  10434. }
  10435. /**
  10436. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  10437. * @phba: Pointer to HBA context object..
  10438. * @piocbq: Pointer to command iocb.
  10439. * @flag: Flag to test.
  10440. *
  10441. * This routine grabs the hbalock and then test the iocb_flag to
  10442. * see if the passed in flag is set.
  10443. * Returns:
  10444. * 1 if flag is set.
  10445. * 0 if flag is not set.
  10446. **/
  10447. static int
  10448. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  10449. struct lpfc_iocbq *piocbq, uint32_t flag)
  10450. {
  10451. unsigned long iflags;
  10452. int ret;
  10453. spin_lock_irqsave(&phba->hbalock, iflags);
  10454. ret = piocbq->iocb_flag & flag;
  10455. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10456. return ret;
  10457. }
  10458. /**
  10459. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  10460. * @phba: Pointer to HBA context object..
  10461. * @pring: Pointer to sli ring.
  10462. * @piocb: Pointer to command iocb.
  10463. * @prspiocbq: Pointer to response iocb.
  10464. * @timeout: Timeout in number of seconds.
  10465. *
  10466. * This function issues the iocb to firmware and waits for the
  10467. * iocb to complete. The iocb_cmpl field of the shall be used
  10468. * to handle iocbs which time out. If the field is NULL, the
  10469. * function shall free the iocbq structure. If more clean up is
  10470. * needed, the caller is expected to provide a completion function
  10471. * that will provide the needed clean up. If the iocb command is
  10472. * not completed within timeout seconds, the function will either
  10473. * free the iocbq structure (if iocb_cmpl == NULL) or execute the
  10474. * completion function set in the iocb_cmpl field and then return
  10475. * a status of IOCB_TIMEDOUT. The caller should not free the iocb
  10476. * resources if this function returns IOCB_TIMEDOUT.
  10477. * The function waits for the iocb completion using an
  10478. * non-interruptible wait.
  10479. * This function will sleep while waiting for iocb completion.
  10480. * So, this function should not be called from any context which
  10481. * does not allow sleeping. Due to the same reason, this function
  10482. * cannot be called with interrupt disabled.
  10483. * This function assumes that the iocb completions occur while
  10484. * this function sleep. So, this function cannot be called from
  10485. * the thread which process iocb completion for this ring.
  10486. * This function clears the iocb_flag of the iocb object before
  10487. * issuing the iocb and the iocb completion handler sets this
  10488. * flag and wakes this thread when the iocb completes.
  10489. * The contents of the response iocb will be copied to prspiocbq
  10490. * by the completion handler when the command completes.
  10491. * This function returns IOCB_SUCCESS when success.
  10492. * This function is called with no lock held.
  10493. **/
  10494. int
  10495. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  10496. uint32_t ring_number,
  10497. struct lpfc_iocbq *piocb,
  10498. struct lpfc_iocbq *prspiocbq,
  10499. uint32_t timeout)
  10500. {
  10501. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  10502. long timeleft, timeout_req = 0;
  10503. int retval = IOCB_SUCCESS;
  10504. uint32_t creg_val;
  10505. struct lpfc_iocbq *iocb;
  10506. int txq_cnt = 0;
  10507. int txcmplq_cnt = 0;
  10508. struct lpfc_sli_ring *pring;
  10509. unsigned long iflags;
  10510. bool iocb_completed = true;
  10511. if (phba->sli_rev >= LPFC_SLI_REV4)
  10512. pring = lpfc_sli4_calc_ring(phba, piocb);
  10513. else
  10514. pring = &phba->sli.sli3_ring[ring_number];
  10515. /*
  10516. * If the caller has provided a response iocbq buffer, then context2
  10517. * is NULL or its an error.
  10518. */
  10519. if (prspiocbq) {
  10520. if (piocb->context2)
  10521. return IOCB_ERROR;
  10522. piocb->context2 = prspiocbq;
  10523. }
  10524. piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
  10525. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  10526. piocb->context_un.wait_queue = &done_q;
  10527. piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
  10528. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  10529. if (lpfc_readl(phba->HCregaddr, &creg_val))
  10530. return IOCB_ERROR;
  10531. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  10532. writel(creg_val, phba->HCregaddr);
  10533. readl(phba->HCregaddr); /* flush */
  10534. }
  10535. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  10536. SLI_IOCB_RET_IOCB);
  10537. if (retval == IOCB_SUCCESS) {
  10538. timeout_req = msecs_to_jiffies(timeout * 1000);
  10539. timeleft = wait_event_timeout(done_q,
  10540. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  10541. timeout_req);
  10542. spin_lock_irqsave(&phba->hbalock, iflags);
  10543. if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
  10544. /*
  10545. * IOCB timed out. Inform the wake iocb wait
  10546. * completion function and set local status
  10547. */
  10548. iocb_completed = false;
  10549. piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
  10550. }
  10551. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10552. if (iocb_completed) {
  10553. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10554. "0331 IOCB wake signaled\n");
  10555. /* Note: we are not indicating if the IOCB has a success
  10556. * status or not - that's for the caller to check.
  10557. * IOCB_SUCCESS means just that the command was sent and
  10558. * completed. Not that it completed successfully.
  10559. * */
  10560. } else if (timeleft == 0) {
  10561. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10562. "0338 IOCB wait timeout error - no "
  10563. "wake response Data x%x\n", timeout);
  10564. retval = IOCB_TIMEDOUT;
  10565. } else {
  10566. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10567. "0330 IOCB wake NOT set, "
  10568. "Data x%x x%lx\n",
  10569. timeout, (timeleft / jiffies));
  10570. retval = IOCB_TIMEDOUT;
  10571. }
  10572. } else if (retval == IOCB_BUSY) {
  10573. if (phba->cfg_log_verbose & LOG_SLI) {
  10574. list_for_each_entry(iocb, &pring->txq, list) {
  10575. txq_cnt++;
  10576. }
  10577. list_for_each_entry(iocb, &pring->txcmplq, list) {
  10578. txcmplq_cnt++;
  10579. }
  10580. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10581. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  10582. phba->iocb_cnt, txq_cnt, txcmplq_cnt);
  10583. }
  10584. return retval;
  10585. } else {
  10586. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10587. "0332 IOCB wait issue failed, Data x%x\n",
  10588. retval);
  10589. retval = IOCB_ERROR;
  10590. }
  10591. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  10592. if (lpfc_readl(phba->HCregaddr, &creg_val))
  10593. return IOCB_ERROR;
  10594. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  10595. writel(creg_val, phba->HCregaddr);
  10596. readl(phba->HCregaddr); /* flush */
  10597. }
  10598. if (prspiocbq)
  10599. piocb->context2 = NULL;
  10600. piocb->context_un.wait_queue = NULL;
  10601. piocb->iocb_cmpl = NULL;
  10602. return retval;
  10603. }
  10604. /**
  10605. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  10606. * @phba: Pointer to HBA context object.
  10607. * @pmboxq: Pointer to driver mailbox object.
  10608. * @timeout: Timeout in number of seconds.
  10609. *
  10610. * This function issues the mailbox to firmware and waits for the
  10611. * mailbox command to complete. If the mailbox command is not
  10612. * completed within timeout seconds, it returns MBX_TIMEOUT.
  10613. * The function waits for the mailbox completion using an
  10614. * interruptible wait. If the thread is woken up due to a
  10615. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  10616. * should not free the mailbox resources, if this function returns
  10617. * MBX_TIMEOUT.
  10618. * This function will sleep while waiting for mailbox completion.
  10619. * So, this function should not be called from any context which
  10620. * does not allow sleeping. Due to the same reason, this function
  10621. * cannot be called with interrupt disabled.
  10622. * This function assumes that the mailbox completion occurs while
  10623. * this function sleep. So, this function cannot be called from
  10624. * the worker thread which processes mailbox completion.
  10625. * This function is called in the context of HBA management
  10626. * applications.
  10627. * This function returns MBX_SUCCESS when successful.
  10628. * This function is called with no lock held.
  10629. **/
  10630. int
  10631. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  10632. uint32_t timeout)
  10633. {
  10634. struct completion mbox_done;
  10635. int retval;
  10636. unsigned long flag;
  10637. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  10638. /* setup wake call as IOCB callback */
  10639. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  10640. /* setup context3 field to pass wait_queue pointer to wake function */
  10641. init_completion(&mbox_done);
  10642. pmboxq->context3 = &mbox_done;
  10643. /* now issue the command */
  10644. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  10645. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  10646. wait_for_completion_timeout(&mbox_done,
  10647. msecs_to_jiffies(timeout * 1000));
  10648. spin_lock_irqsave(&phba->hbalock, flag);
  10649. pmboxq->context3 = NULL;
  10650. /*
  10651. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  10652. * else do not free the resources.
  10653. */
  10654. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  10655. retval = MBX_SUCCESS;
  10656. } else {
  10657. retval = MBX_TIMEOUT;
  10658. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  10659. }
  10660. spin_unlock_irqrestore(&phba->hbalock, flag);
  10661. }
  10662. return retval;
  10663. }
  10664. /**
  10665. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  10666. * @phba: Pointer to HBA context.
  10667. *
  10668. * This function is called to shutdown the driver's mailbox sub-system.
  10669. * It first marks the mailbox sub-system is in a block state to prevent
  10670. * the asynchronous mailbox command from issued off the pending mailbox
  10671. * command queue. If the mailbox command sub-system shutdown is due to
  10672. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  10673. * the mailbox sub-system flush routine to forcefully bring down the
  10674. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  10675. * as with offline or HBA function reset), this routine will wait for the
  10676. * outstanding mailbox command to complete before invoking the mailbox
  10677. * sub-system flush routine to gracefully bring down mailbox sub-system.
  10678. **/
  10679. void
  10680. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  10681. {
  10682. struct lpfc_sli *psli = &phba->sli;
  10683. unsigned long timeout;
  10684. if (mbx_action == LPFC_MBX_NO_WAIT) {
  10685. /* delay 100ms for port state */
  10686. msleep(100);
  10687. lpfc_sli_mbox_sys_flush(phba);
  10688. return;
  10689. }
  10690. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  10691. spin_lock_irq(&phba->hbalock);
  10692. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  10693. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  10694. /* Determine how long we might wait for the active mailbox
  10695. * command to be gracefully completed by firmware.
  10696. */
  10697. if (phba->sli.mbox_active)
  10698. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  10699. phba->sli.mbox_active) *
  10700. 1000) + jiffies;
  10701. spin_unlock_irq(&phba->hbalock);
  10702. while (phba->sli.mbox_active) {
  10703. /* Check active mailbox complete status every 2ms */
  10704. msleep(2);
  10705. if (time_after(jiffies, timeout))
  10706. /* Timeout, let the mailbox flush routine to
  10707. * forcefully release active mailbox command
  10708. */
  10709. break;
  10710. }
  10711. } else
  10712. spin_unlock_irq(&phba->hbalock);
  10713. lpfc_sli_mbox_sys_flush(phba);
  10714. }
  10715. /**
  10716. * lpfc_sli_eratt_read - read sli-3 error attention events
  10717. * @phba: Pointer to HBA context.
  10718. *
  10719. * This function is called to read the SLI3 device error attention registers
  10720. * for possible error attention events. The caller must hold the hostlock
  10721. * with spin_lock_irq().
  10722. *
  10723. * This function returns 1 when there is Error Attention in the Host Attention
  10724. * Register and returns 0 otherwise.
  10725. **/
  10726. static int
  10727. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  10728. {
  10729. uint32_t ha_copy;
  10730. /* Read chip Host Attention (HA) register */
  10731. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  10732. goto unplug_err;
  10733. if (ha_copy & HA_ERATT) {
  10734. /* Read host status register to retrieve error event */
  10735. if (lpfc_sli_read_hs(phba))
  10736. goto unplug_err;
  10737. /* Check if there is a deferred error condition is active */
  10738. if ((HS_FFER1 & phba->work_hs) &&
  10739. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  10740. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  10741. phba->hba_flag |= DEFER_ERATT;
  10742. /* Clear all interrupt enable conditions */
  10743. writel(0, phba->HCregaddr);
  10744. readl(phba->HCregaddr);
  10745. }
  10746. /* Set the driver HA work bitmap */
  10747. phba->work_ha |= HA_ERATT;
  10748. /* Indicate polling handles this ERATT */
  10749. phba->hba_flag |= HBA_ERATT_HANDLED;
  10750. return 1;
  10751. }
  10752. return 0;
  10753. unplug_err:
  10754. /* Set the driver HS work bitmap */
  10755. phba->work_hs |= UNPLUG_ERR;
  10756. /* Set the driver HA work bitmap */
  10757. phba->work_ha |= HA_ERATT;
  10758. /* Indicate polling handles this ERATT */
  10759. phba->hba_flag |= HBA_ERATT_HANDLED;
  10760. return 1;
  10761. }
  10762. /**
  10763. * lpfc_sli4_eratt_read - read sli-4 error attention events
  10764. * @phba: Pointer to HBA context.
  10765. *
  10766. * This function is called to read the SLI4 device error attention registers
  10767. * for possible error attention events. The caller must hold the hostlock
  10768. * with spin_lock_irq().
  10769. *
  10770. * This function returns 1 when there is Error Attention in the Host Attention
  10771. * Register and returns 0 otherwise.
  10772. **/
  10773. static int
  10774. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  10775. {
  10776. uint32_t uerr_sta_hi, uerr_sta_lo;
  10777. uint32_t if_type, portsmphr;
  10778. struct lpfc_register portstat_reg;
  10779. /*
  10780. * For now, use the SLI4 device internal unrecoverable error
  10781. * registers for error attention. This can be changed later.
  10782. */
  10783. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  10784. switch (if_type) {
  10785. case LPFC_SLI_INTF_IF_TYPE_0:
  10786. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  10787. &uerr_sta_lo) ||
  10788. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  10789. &uerr_sta_hi)) {
  10790. phba->work_hs |= UNPLUG_ERR;
  10791. phba->work_ha |= HA_ERATT;
  10792. phba->hba_flag |= HBA_ERATT_HANDLED;
  10793. return 1;
  10794. }
  10795. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  10796. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  10797. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10798. "1423 HBA Unrecoverable error: "
  10799. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  10800. "ue_mask_lo_reg=0x%x, "
  10801. "ue_mask_hi_reg=0x%x\n",
  10802. uerr_sta_lo, uerr_sta_hi,
  10803. phba->sli4_hba.ue_mask_lo,
  10804. phba->sli4_hba.ue_mask_hi);
  10805. phba->work_status[0] = uerr_sta_lo;
  10806. phba->work_status[1] = uerr_sta_hi;
  10807. phba->work_ha |= HA_ERATT;
  10808. phba->hba_flag |= HBA_ERATT_HANDLED;
  10809. return 1;
  10810. }
  10811. break;
  10812. case LPFC_SLI_INTF_IF_TYPE_2:
  10813. case LPFC_SLI_INTF_IF_TYPE_6:
  10814. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  10815. &portstat_reg.word0) ||
  10816. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  10817. &portsmphr)){
  10818. phba->work_hs |= UNPLUG_ERR;
  10819. phba->work_ha |= HA_ERATT;
  10820. phba->hba_flag |= HBA_ERATT_HANDLED;
  10821. return 1;
  10822. }
  10823. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  10824. phba->work_status[0] =
  10825. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  10826. phba->work_status[1] =
  10827. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  10828. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10829. "2885 Port Status Event: "
  10830. "port status reg 0x%x, "
  10831. "port smphr reg 0x%x, "
  10832. "error 1=0x%x, error 2=0x%x\n",
  10833. portstat_reg.word0,
  10834. portsmphr,
  10835. phba->work_status[0],
  10836. phba->work_status[1]);
  10837. phba->work_ha |= HA_ERATT;
  10838. phba->hba_flag |= HBA_ERATT_HANDLED;
  10839. return 1;
  10840. }
  10841. break;
  10842. case LPFC_SLI_INTF_IF_TYPE_1:
  10843. default:
  10844. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10845. "2886 HBA Error Attention on unsupported "
  10846. "if type %d.", if_type);
  10847. return 1;
  10848. }
  10849. return 0;
  10850. }
  10851. /**
  10852. * lpfc_sli_check_eratt - check error attention events
  10853. * @phba: Pointer to HBA context.
  10854. *
  10855. * This function is called from timer soft interrupt context to check HBA's
  10856. * error attention register bit for error attention events.
  10857. *
  10858. * This function returns 1 when there is Error Attention in the Host Attention
  10859. * Register and returns 0 otherwise.
  10860. **/
  10861. int
  10862. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  10863. {
  10864. uint32_t ha_copy;
  10865. /* If somebody is waiting to handle an eratt, don't process it
  10866. * here. The brdkill function will do this.
  10867. */
  10868. if (phba->link_flag & LS_IGNORE_ERATT)
  10869. return 0;
  10870. /* Check if interrupt handler handles this ERATT */
  10871. spin_lock_irq(&phba->hbalock);
  10872. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  10873. /* Interrupt handler has handled ERATT */
  10874. spin_unlock_irq(&phba->hbalock);
  10875. return 0;
  10876. }
  10877. /*
  10878. * If there is deferred error attention, do not check for error
  10879. * attention
  10880. */
  10881. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  10882. spin_unlock_irq(&phba->hbalock);
  10883. return 0;
  10884. }
  10885. /* If PCI channel is offline, don't process it */
  10886. if (unlikely(pci_channel_offline(phba->pcidev))) {
  10887. spin_unlock_irq(&phba->hbalock);
  10888. return 0;
  10889. }
  10890. switch (phba->sli_rev) {
  10891. case LPFC_SLI_REV2:
  10892. case LPFC_SLI_REV3:
  10893. /* Read chip Host Attention (HA) register */
  10894. ha_copy = lpfc_sli_eratt_read(phba);
  10895. break;
  10896. case LPFC_SLI_REV4:
  10897. /* Read device Uncoverable Error (UERR) registers */
  10898. ha_copy = lpfc_sli4_eratt_read(phba);
  10899. break;
  10900. default:
  10901. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10902. "0299 Invalid SLI revision (%d)\n",
  10903. phba->sli_rev);
  10904. ha_copy = 0;
  10905. break;
  10906. }
  10907. spin_unlock_irq(&phba->hbalock);
  10908. return ha_copy;
  10909. }
  10910. /**
  10911. * lpfc_intr_state_check - Check device state for interrupt handling
  10912. * @phba: Pointer to HBA context.
  10913. *
  10914. * This inline routine checks whether a device or its PCI slot is in a state
  10915. * that the interrupt should be handled.
  10916. *
  10917. * This function returns 0 if the device or the PCI slot is in a state that
  10918. * interrupt should be handled, otherwise -EIO.
  10919. */
  10920. static inline int
  10921. lpfc_intr_state_check(struct lpfc_hba *phba)
  10922. {
  10923. /* If the pci channel is offline, ignore all the interrupts */
  10924. if (unlikely(pci_channel_offline(phba->pcidev)))
  10925. return -EIO;
  10926. /* Update device level interrupt statistics */
  10927. phba->sli.slistat.sli_intr++;
  10928. /* Ignore all interrupts during initialization. */
  10929. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  10930. return -EIO;
  10931. return 0;
  10932. }
  10933. /**
  10934. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  10935. * @irq: Interrupt number.
  10936. * @dev_id: The device context pointer.
  10937. *
  10938. * This function is directly called from the PCI layer as an interrupt
  10939. * service routine when device with SLI-3 interface spec is enabled with
  10940. * MSI-X multi-message interrupt mode and there are slow-path events in
  10941. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10942. * interrupt mode, this function is called as part of the device-level
  10943. * interrupt handler. When the PCI slot is in error recovery or the HBA
  10944. * is undergoing initialization, the interrupt handler will not process
  10945. * the interrupt. The link attention and ELS ring attention events are
  10946. * handled by the worker thread. The interrupt handler signals the worker
  10947. * thread and returns for these events. This function is called without
  10948. * any lock held. It gets the hbalock to access and update SLI data
  10949. * structures.
  10950. *
  10951. * This function returns IRQ_HANDLED when interrupt is handled else it
  10952. * returns IRQ_NONE.
  10953. **/
  10954. irqreturn_t
  10955. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  10956. {
  10957. struct lpfc_hba *phba;
  10958. uint32_t ha_copy, hc_copy;
  10959. uint32_t work_ha_copy;
  10960. unsigned long status;
  10961. unsigned long iflag;
  10962. uint32_t control;
  10963. MAILBOX_t *mbox, *pmbox;
  10964. struct lpfc_vport *vport;
  10965. struct lpfc_nodelist *ndlp;
  10966. struct lpfc_dmabuf *mp;
  10967. LPFC_MBOXQ_t *pmb;
  10968. int rc;
  10969. /*
  10970. * Get the driver's phba structure from the dev_id and
  10971. * assume the HBA is not interrupting.
  10972. */
  10973. phba = (struct lpfc_hba *)dev_id;
  10974. if (unlikely(!phba))
  10975. return IRQ_NONE;
  10976. /*
  10977. * Stuff needs to be attented to when this function is invoked as an
  10978. * individual interrupt handler in MSI-X multi-message interrupt mode
  10979. */
  10980. if (phba->intr_type == MSIX) {
  10981. /* Check device state for handling interrupt */
  10982. if (lpfc_intr_state_check(phba))
  10983. return IRQ_NONE;
  10984. /* Need to read HA REG for slow-path events */
  10985. spin_lock_irqsave(&phba->hbalock, iflag);
  10986. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  10987. goto unplug_error;
  10988. /* If somebody is waiting to handle an eratt don't process it
  10989. * here. The brdkill function will do this.
  10990. */
  10991. if (phba->link_flag & LS_IGNORE_ERATT)
  10992. ha_copy &= ~HA_ERATT;
  10993. /* Check the need for handling ERATT in interrupt handler */
  10994. if (ha_copy & HA_ERATT) {
  10995. if (phba->hba_flag & HBA_ERATT_HANDLED)
  10996. /* ERATT polling has handled ERATT */
  10997. ha_copy &= ~HA_ERATT;
  10998. else
  10999. /* Indicate interrupt handler handles ERATT */
  11000. phba->hba_flag |= HBA_ERATT_HANDLED;
  11001. }
  11002. /*
  11003. * If there is deferred error attention, do not check for any
  11004. * interrupt.
  11005. */
  11006. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  11007. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11008. return IRQ_NONE;
  11009. }
  11010. /* Clear up only attention source related to slow-path */
  11011. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  11012. goto unplug_error;
  11013. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  11014. HC_LAINT_ENA | HC_ERINT_ENA),
  11015. phba->HCregaddr);
  11016. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  11017. phba->HAregaddr);
  11018. writel(hc_copy, phba->HCregaddr);
  11019. readl(phba->HAregaddr); /* flush */
  11020. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11021. } else
  11022. ha_copy = phba->ha_copy;
  11023. work_ha_copy = ha_copy & phba->work_ha_mask;
  11024. if (work_ha_copy) {
  11025. if (work_ha_copy & HA_LATT) {
  11026. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  11027. /*
  11028. * Turn off Link Attention interrupts
  11029. * until CLEAR_LA done
  11030. */
  11031. spin_lock_irqsave(&phba->hbalock, iflag);
  11032. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  11033. if (lpfc_readl(phba->HCregaddr, &control))
  11034. goto unplug_error;
  11035. control &= ~HC_LAINT_ENA;
  11036. writel(control, phba->HCregaddr);
  11037. readl(phba->HCregaddr); /* flush */
  11038. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11039. }
  11040. else
  11041. work_ha_copy &= ~HA_LATT;
  11042. }
  11043. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  11044. /*
  11045. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  11046. * the only slow ring.
  11047. */
  11048. status = (work_ha_copy &
  11049. (HA_RXMASK << (4*LPFC_ELS_RING)));
  11050. status >>= (4*LPFC_ELS_RING);
  11051. if (status & HA_RXMASK) {
  11052. spin_lock_irqsave(&phba->hbalock, iflag);
  11053. if (lpfc_readl(phba->HCregaddr, &control))
  11054. goto unplug_error;
  11055. lpfc_debugfs_slow_ring_trc(phba,
  11056. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  11057. control, status,
  11058. (uint32_t)phba->sli.slistat.sli_intr);
  11059. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  11060. lpfc_debugfs_slow_ring_trc(phba,
  11061. "ISR Disable ring:"
  11062. "pwork:x%x hawork:x%x wait:x%x",
  11063. phba->work_ha, work_ha_copy,
  11064. (uint32_t)((unsigned long)
  11065. &phba->work_waitq));
  11066. control &=
  11067. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  11068. writel(control, phba->HCregaddr);
  11069. readl(phba->HCregaddr); /* flush */
  11070. }
  11071. else {
  11072. lpfc_debugfs_slow_ring_trc(phba,
  11073. "ISR slow ring: pwork:"
  11074. "x%x hawork:x%x wait:x%x",
  11075. phba->work_ha, work_ha_copy,
  11076. (uint32_t)((unsigned long)
  11077. &phba->work_waitq));
  11078. }
  11079. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11080. }
  11081. }
  11082. spin_lock_irqsave(&phba->hbalock, iflag);
  11083. if (work_ha_copy & HA_ERATT) {
  11084. if (lpfc_sli_read_hs(phba))
  11085. goto unplug_error;
  11086. /*
  11087. * Check if there is a deferred error condition
  11088. * is active
  11089. */
  11090. if ((HS_FFER1 & phba->work_hs) &&
  11091. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  11092. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  11093. phba->work_hs)) {
  11094. phba->hba_flag |= DEFER_ERATT;
  11095. /* Clear all interrupt enable conditions */
  11096. writel(0, phba->HCregaddr);
  11097. readl(phba->HCregaddr);
  11098. }
  11099. }
  11100. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  11101. pmb = phba->sli.mbox_active;
  11102. pmbox = &pmb->u.mb;
  11103. mbox = phba->mbox;
  11104. vport = pmb->vport;
  11105. /* First check out the status word */
  11106. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  11107. if (pmbox->mbxOwner != OWN_HOST) {
  11108. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11109. /*
  11110. * Stray Mailbox Interrupt, mbxCommand <cmd>
  11111. * mbxStatus <status>
  11112. */
  11113. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  11114. LOG_SLI,
  11115. "(%d):0304 Stray Mailbox "
  11116. "Interrupt mbxCommand x%x "
  11117. "mbxStatus x%x\n",
  11118. (vport ? vport->vpi : 0),
  11119. pmbox->mbxCommand,
  11120. pmbox->mbxStatus);
  11121. /* clear mailbox attention bit */
  11122. work_ha_copy &= ~HA_MBATT;
  11123. } else {
  11124. phba->sli.mbox_active = NULL;
  11125. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11126. phba->last_completion_time = jiffies;
  11127. del_timer(&phba->sli.mbox_tmo);
  11128. if (pmb->mbox_cmpl) {
  11129. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  11130. MAILBOX_CMD_SIZE);
  11131. if (pmb->out_ext_byte_len &&
  11132. pmb->context2)
  11133. lpfc_sli_pcimem_bcopy(
  11134. phba->mbox_ext,
  11135. pmb->context2,
  11136. pmb->out_ext_byte_len);
  11137. }
  11138. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  11139. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  11140. lpfc_debugfs_disc_trc(vport,
  11141. LPFC_DISC_TRC_MBOX_VPORT,
  11142. "MBOX dflt rpi: : "
  11143. "status:x%x rpi:x%x",
  11144. (uint32_t)pmbox->mbxStatus,
  11145. pmbox->un.varWords[0], 0);
  11146. if (!pmbox->mbxStatus) {
  11147. mp = (struct lpfc_dmabuf *)
  11148. (pmb->context1);
  11149. ndlp = (struct lpfc_nodelist *)
  11150. pmb->context2;
  11151. /* Reg_LOGIN of dflt RPI was
  11152. * successful. new lets get
  11153. * rid of the RPI using the
  11154. * same mbox buffer.
  11155. */
  11156. lpfc_unreg_login(phba,
  11157. vport->vpi,
  11158. pmbox->un.varWords[0],
  11159. pmb);
  11160. pmb->mbox_cmpl =
  11161. lpfc_mbx_cmpl_dflt_rpi;
  11162. pmb->context1 = mp;
  11163. pmb->context2 = ndlp;
  11164. pmb->vport = vport;
  11165. rc = lpfc_sli_issue_mbox(phba,
  11166. pmb,
  11167. MBX_NOWAIT);
  11168. if (rc != MBX_BUSY)
  11169. lpfc_printf_log(phba,
  11170. KERN_ERR,
  11171. LOG_MBOX | LOG_SLI,
  11172. "0350 rc should have"
  11173. "been MBX_BUSY\n");
  11174. if (rc != MBX_NOT_FINISHED)
  11175. goto send_current_mbox;
  11176. }
  11177. }
  11178. spin_lock_irqsave(
  11179. &phba->pport->work_port_lock,
  11180. iflag);
  11181. phba->pport->work_port_events &=
  11182. ~WORKER_MBOX_TMO;
  11183. spin_unlock_irqrestore(
  11184. &phba->pport->work_port_lock,
  11185. iflag);
  11186. lpfc_mbox_cmpl_put(phba, pmb);
  11187. }
  11188. } else
  11189. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11190. if ((work_ha_copy & HA_MBATT) &&
  11191. (phba->sli.mbox_active == NULL)) {
  11192. send_current_mbox:
  11193. /* Process next mailbox command if there is one */
  11194. do {
  11195. rc = lpfc_sli_issue_mbox(phba, NULL,
  11196. MBX_NOWAIT);
  11197. } while (rc == MBX_NOT_FINISHED);
  11198. if (rc != MBX_SUCCESS)
  11199. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  11200. LOG_SLI, "0349 rc should be "
  11201. "MBX_SUCCESS\n");
  11202. }
  11203. spin_lock_irqsave(&phba->hbalock, iflag);
  11204. phba->work_ha |= work_ha_copy;
  11205. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11206. lpfc_worker_wake_up(phba);
  11207. }
  11208. return IRQ_HANDLED;
  11209. unplug_error:
  11210. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11211. return IRQ_HANDLED;
  11212. } /* lpfc_sli_sp_intr_handler */
  11213. /**
  11214. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  11215. * @irq: Interrupt number.
  11216. * @dev_id: The device context pointer.
  11217. *
  11218. * This function is directly called from the PCI layer as an interrupt
  11219. * service routine when device with SLI-3 interface spec is enabled with
  11220. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  11221. * ring event in the HBA. However, when the device is enabled with either
  11222. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  11223. * device-level interrupt handler. When the PCI slot is in error recovery
  11224. * or the HBA is undergoing initialization, the interrupt handler will not
  11225. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  11226. * the intrrupt context. This function is called without any lock held.
  11227. * It gets the hbalock to access and update SLI data structures.
  11228. *
  11229. * This function returns IRQ_HANDLED when interrupt is handled else it
  11230. * returns IRQ_NONE.
  11231. **/
  11232. irqreturn_t
  11233. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  11234. {
  11235. struct lpfc_hba *phba;
  11236. uint32_t ha_copy;
  11237. unsigned long status;
  11238. unsigned long iflag;
  11239. struct lpfc_sli_ring *pring;
  11240. /* Get the driver's phba structure from the dev_id and
  11241. * assume the HBA is not interrupting.
  11242. */
  11243. phba = (struct lpfc_hba *) dev_id;
  11244. if (unlikely(!phba))
  11245. return IRQ_NONE;
  11246. /*
  11247. * Stuff needs to be attented to when this function is invoked as an
  11248. * individual interrupt handler in MSI-X multi-message interrupt mode
  11249. */
  11250. if (phba->intr_type == MSIX) {
  11251. /* Check device state for handling interrupt */
  11252. if (lpfc_intr_state_check(phba))
  11253. return IRQ_NONE;
  11254. /* Need to read HA REG for FCP ring and other ring events */
  11255. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  11256. return IRQ_HANDLED;
  11257. /* Clear up only attention source related to fast-path */
  11258. spin_lock_irqsave(&phba->hbalock, iflag);
  11259. /*
  11260. * If there is deferred error attention, do not check for
  11261. * any interrupt.
  11262. */
  11263. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  11264. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11265. return IRQ_NONE;
  11266. }
  11267. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  11268. phba->HAregaddr);
  11269. readl(phba->HAregaddr); /* flush */
  11270. spin_unlock_irqrestore(&phba->hbalock, iflag);
  11271. } else
  11272. ha_copy = phba->ha_copy;
  11273. /*
  11274. * Process all events on FCP ring. Take the optimized path for FCP IO.
  11275. */
  11276. ha_copy &= ~(phba->work_ha_mask);
  11277. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  11278. status >>= (4*LPFC_FCP_RING);
  11279. pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
  11280. if (status & HA_RXMASK)
  11281. lpfc_sli_handle_fast_ring_event(phba, pring, status);
  11282. if (phba->cfg_multi_ring_support == 2) {
  11283. /*
  11284. * Process all events on extra ring. Take the optimized path
  11285. * for extra ring IO.
  11286. */
  11287. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  11288. status >>= (4*LPFC_EXTRA_RING);
  11289. if (status & HA_RXMASK) {
  11290. lpfc_sli_handle_fast_ring_event(phba,
  11291. &phba->sli.sli3_ring[LPFC_EXTRA_RING],
  11292. status);
  11293. }
  11294. }
  11295. return IRQ_HANDLED;
  11296. } /* lpfc_sli_fp_intr_handler */
  11297. /**
  11298. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  11299. * @irq: Interrupt number.
  11300. * @dev_id: The device context pointer.
  11301. *
  11302. * This function is the HBA device-level interrupt handler to device with
  11303. * SLI-3 interface spec, called from the PCI layer when either MSI or
  11304. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  11305. * requires driver attention. This function invokes the slow-path interrupt
  11306. * attention handling function and fast-path interrupt attention handling
  11307. * function in turn to process the relevant HBA attention events. This
  11308. * function is called without any lock held. It gets the hbalock to access
  11309. * and update SLI data structures.
  11310. *
  11311. * This function returns IRQ_HANDLED when interrupt is handled, else it
  11312. * returns IRQ_NONE.
  11313. **/
  11314. irqreturn_t
  11315. lpfc_sli_intr_handler(int irq, void *dev_id)
  11316. {
  11317. struct lpfc_hba *phba;
  11318. irqreturn_t sp_irq_rc, fp_irq_rc;
  11319. unsigned long status1, status2;
  11320. uint32_t hc_copy;
  11321. /*
  11322. * Get the driver's phba structure from the dev_id and
  11323. * assume the HBA is not interrupting.
  11324. */
  11325. phba = (struct lpfc_hba *) dev_id;
  11326. if (unlikely(!phba))
  11327. return IRQ_NONE;
  11328. /* Check device state for handling interrupt */
  11329. if (lpfc_intr_state_check(phba))
  11330. return IRQ_NONE;
  11331. spin_lock(&phba->hbalock);
  11332. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  11333. spin_unlock(&phba->hbalock);
  11334. return IRQ_HANDLED;
  11335. }
  11336. if (unlikely(!phba->ha_copy)) {
  11337. spin_unlock(&phba->hbalock);
  11338. return IRQ_NONE;
  11339. } else if (phba->ha_copy & HA_ERATT) {
  11340. if (phba->hba_flag & HBA_ERATT_HANDLED)
  11341. /* ERATT polling has handled ERATT */
  11342. phba->ha_copy &= ~HA_ERATT;
  11343. else
  11344. /* Indicate interrupt handler handles ERATT */
  11345. phba->hba_flag |= HBA_ERATT_HANDLED;
  11346. }
  11347. /*
  11348. * If there is deferred error attention, do not check for any interrupt.
  11349. */
  11350. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  11351. spin_unlock(&phba->hbalock);
  11352. return IRQ_NONE;
  11353. }
  11354. /* Clear attention sources except link and error attentions */
  11355. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  11356. spin_unlock(&phba->hbalock);
  11357. return IRQ_HANDLED;
  11358. }
  11359. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  11360. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  11361. phba->HCregaddr);
  11362. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  11363. writel(hc_copy, phba->HCregaddr);
  11364. readl(phba->HAregaddr); /* flush */
  11365. spin_unlock(&phba->hbalock);
  11366. /*
  11367. * Invokes slow-path host attention interrupt handling as appropriate.
  11368. */
  11369. /* status of events with mailbox and link attention */
  11370. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  11371. /* status of events with ELS ring */
  11372. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  11373. status2 >>= (4*LPFC_ELS_RING);
  11374. if (status1 || (status2 & HA_RXMASK))
  11375. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  11376. else
  11377. sp_irq_rc = IRQ_NONE;
  11378. /*
  11379. * Invoke fast-path host attention interrupt handling as appropriate.
  11380. */
  11381. /* status of events with FCP ring */
  11382. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  11383. status1 >>= (4*LPFC_FCP_RING);
  11384. /* status of events with extra ring */
  11385. if (phba->cfg_multi_ring_support == 2) {
  11386. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  11387. status2 >>= (4*LPFC_EXTRA_RING);
  11388. } else
  11389. status2 = 0;
  11390. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  11391. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  11392. else
  11393. fp_irq_rc = IRQ_NONE;
  11394. /* Return device-level interrupt handling status */
  11395. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  11396. } /* lpfc_sli_intr_handler */
  11397. /**
  11398. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  11399. * @phba: pointer to lpfc hba data structure.
  11400. *
  11401. * This routine is invoked by the worker thread to process all the pending
  11402. * SLI4 FCP abort XRI events.
  11403. **/
  11404. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  11405. {
  11406. struct lpfc_cq_event *cq_event;
  11407. /* First, declare the fcp xri abort event has been handled */
  11408. spin_lock_irq(&phba->hbalock);
  11409. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  11410. spin_unlock_irq(&phba->hbalock);
  11411. /* Now, handle all the fcp xri abort events */
  11412. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  11413. /* Get the first event from the head of the event queue */
  11414. spin_lock_irq(&phba->hbalock);
  11415. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  11416. cq_event, struct lpfc_cq_event, list);
  11417. spin_unlock_irq(&phba->hbalock);
  11418. /* Notify aborted XRI for FCP work queue */
  11419. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  11420. /* Free the event processed back to the free pool */
  11421. lpfc_sli4_cq_event_release(phba, cq_event);
  11422. }
  11423. }
  11424. /**
  11425. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  11426. * @phba: pointer to lpfc hba data structure.
  11427. *
  11428. * This routine is invoked by the worker thread to process all the pending
  11429. * SLI4 els abort xri events.
  11430. **/
  11431. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  11432. {
  11433. struct lpfc_cq_event *cq_event;
  11434. /* First, declare the els xri abort event has been handled */
  11435. spin_lock_irq(&phba->hbalock);
  11436. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  11437. spin_unlock_irq(&phba->hbalock);
  11438. /* Now, handle all the els xri abort events */
  11439. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  11440. /* Get the first event from the head of the event queue */
  11441. spin_lock_irq(&phba->hbalock);
  11442. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  11443. cq_event, struct lpfc_cq_event, list);
  11444. spin_unlock_irq(&phba->hbalock);
  11445. /* Notify aborted XRI for ELS work queue */
  11446. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  11447. /* Free the event processed back to the free pool */
  11448. lpfc_sli4_cq_event_release(phba, cq_event);
  11449. }
  11450. }
  11451. /**
  11452. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  11453. * @phba: pointer to lpfc hba data structure
  11454. * @pIocbIn: pointer to the rspiocbq
  11455. * @pIocbOut: pointer to the cmdiocbq
  11456. * @wcqe: pointer to the complete wcqe
  11457. *
  11458. * This routine transfers the fields of a command iocbq to a response iocbq
  11459. * by copying all the IOCB fields from command iocbq and transferring the
  11460. * completion status information from the complete wcqe.
  11461. **/
  11462. static void
  11463. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  11464. struct lpfc_iocbq *pIocbIn,
  11465. struct lpfc_iocbq *pIocbOut,
  11466. struct lpfc_wcqe_complete *wcqe)
  11467. {
  11468. int numBdes, i;
  11469. unsigned long iflags;
  11470. uint32_t status, max_response;
  11471. struct lpfc_dmabuf *dmabuf;
  11472. struct ulp_bde64 *bpl, bde;
  11473. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  11474. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  11475. sizeof(struct lpfc_iocbq) - offset);
  11476. /* Map WCQE parameters into irspiocb parameters */
  11477. status = bf_get(lpfc_wcqe_c_status, wcqe);
  11478. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  11479. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  11480. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  11481. pIocbIn->iocb.un.fcpi.fcpi_parm =
  11482. pIocbOut->iocb.un.fcpi.fcpi_parm -
  11483. wcqe->total_data_placed;
  11484. else
  11485. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  11486. else {
  11487. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  11488. switch (pIocbOut->iocb.ulpCommand) {
  11489. case CMD_ELS_REQUEST64_CR:
  11490. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  11491. bpl = (struct ulp_bde64 *)dmabuf->virt;
  11492. bde.tus.w = le32_to_cpu(bpl[1].tus.w);
  11493. max_response = bde.tus.f.bdeSize;
  11494. break;
  11495. case CMD_GEN_REQUEST64_CR:
  11496. max_response = 0;
  11497. if (!pIocbOut->context3)
  11498. break;
  11499. numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
  11500. sizeof(struct ulp_bde64);
  11501. dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
  11502. bpl = (struct ulp_bde64 *)dmabuf->virt;
  11503. for (i = 0; i < numBdes; i++) {
  11504. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  11505. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  11506. max_response += bde.tus.f.bdeSize;
  11507. }
  11508. break;
  11509. default:
  11510. max_response = wcqe->total_data_placed;
  11511. break;
  11512. }
  11513. if (max_response < wcqe->total_data_placed)
  11514. pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
  11515. else
  11516. pIocbIn->iocb.un.genreq64.bdl.bdeSize =
  11517. wcqe->total_data_placed;
  11518. }
  11519. /* Convert BG errors for completion status */
  11520. if (status == CQE_STATUS_DI_ERROR) {
  11521. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  11522. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  11523. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  11524. else
  11525. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  11526. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  11527. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  11528. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  11529. BGS_GUARD_ERR_MASK;
  11530. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  11531. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  11532. BGS_APPTAG_ERR_MASK;
  11533. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  11534. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  11535. BGS_REFTAG_ERR_MASK;
  11536. /* Check to see if there was any good data before the error */
  11537. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  11538. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  11539. BGS_HI_WATER_MARK_PRESENT_MASK;
  11540. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  11541. wcqe->total_data_placed;
  11542. }
  11543. /*
  11544. * Set ALL the error bits to indicate we don't know what
  11545. * type of error it is.
  11546. */
  11547. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  11548. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  11549. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  11550. BGS_GUARD_ERR_MASK);
  11551. }
  11552. /* Pick up HBA exchange busy condition */
  11553. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  11554. spin_lock_irqsave(&phba->hbalock, iflags);
  11555. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  11556. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11557. }
  11558. }
  11559. /**
  11560. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  11561. * @phba: Pointer to HBA context object.
  11562. * @wcqe: Pointer to work-queue completion queue entry.
  11563. *
  11564. * This routine handles an ELS work-queue completion event and construct
  11565. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  11566. * discovery engine to handle.
  11567. *
  11568. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  11569. **/
  11570. static struct lpfc_iocbq *
  11571. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  11572. struct lpfc_iocbq *irspiocbq)
  11573. {
  11574. struct lpfc_sli_ring *pring;
  11575. struct lpfc_iocbq *cmdiocbq;
  11576. struct lpfc_wcqe_complete *wcqe;
  11577. unsigned long iflags;
  11578. pring = lpfc_phba_elsring(phba);
  11579. if (unlikely(!pring))
  11580. return NULL;
  11581. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  11582. spin_lock_irqsave(&pring->ring_lock, iflags);
  11583. pring->stats.iocb_event++;
  11584. /* Look up the ELS command IOCB and create pseudo response IOCB */
  11585. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  11586. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  11587. if (unlikely(!cmdiocbq)) {
  11588. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  11589. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11590. "0386 ELS complete with no corresponding "
  11591. "cmdiocb: 0x%x 0x%x 0x%x 0x%x\n",
  11592. wcqe->word0, wcqe->total_data_placed,
  11593. wcqe->parameter, wcqe->word3);
  11594. lpfc_sli_release_iocbq(phba, irspiocbq);
  11595. return NULL;
  11596. }
  11597. /* Put the iocb back on the txcmplq */
  11598. lpfc_sli_ringtxcmpl_put(phba, pring, cmdiocbq);
  11599. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  11600. /* Fake the irspiocbq and copy necessary response information */
  11601. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  11602. return irspiocbq;
  11603. }
  11604. inline struct lpfc_cq_event *
  11605. lpfc_cq_event_setup(struct lpfc_hba *phba, void *entry, int size)
  11606. {
  11607. struct lpfc_cq_event *cq_event;
  11608. /* Allocate a new internal CQ_EVENT entry */
  11609. cq_event = lpfc_sli4_cq_event_alloc(phba);
  11610. if (!cq_event) {
  11611. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11612. "0602 Failed to alloc CQ_EVENT entry\n");
  11613. return NULL;
  11614. }
  11615. /* Move the CQE into the event */
  11616. memcpy(&cq_event->cqe, entry, size);
  11617. return cq_event;
  11618. }
  11619. /**
  11620. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  11621. * @phba: Pointer to HBA context object.
  11622. * @cqe: Pointer to mailbox completion queue entry.
  11623. *
  11624. * This routine process a mailbox completion queue entry with asynchrous
  11625. * event.
  11626. *
  11627. * Return: true if work posted to worker thread, otherwise false.
  11628. **/
  11629. static bool
  11630. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  11631. {
  11632. struct lpfc_cq_event *cq_event;
  11633. unsigned long iflags;
  11634. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  11635. "0392 Async Event: word0:x%x, word1:x%x, "
  11636. "word2:x%x, word3:x%x\n", mcqe->word0,
  11637. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  11638. cq_event = lpfc_cq_event_setup(phba, mcqe, sizeof(struct lpfc_mcqe));
  11639. if (!cq_event)
  11640. return false;
  11641. spin_lock_irqsave(&phba->hbalock, iflags);
  11642. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  11643. /* Set the async event flag */
  11644. phba->hba_flag |= ASYNC_EVENT;
  11645. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11646. return true;
  11647. }
  11648. /**
  11649. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  11650. * @phba: Pointer to HBA context object.
  11651. * @cqe: Pointer to mailbox completion queue entry.
  11652. *
  11653. * This routine process a mailbox completion queue entry with mailbox
  11654. * completion event.
  11655. *
  11656. * Return: true if work posted to worker thread, otherwise false.
  11657. **/
  11658. static bool
  11659. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  11660. {
  11661. uint32_t mcqe_status;
  11662. MAILBOX_t *mbox, *pmbox;
  11663. struct lpfc_mqe *mqe;
  11664. struct lpfc_vport *vport;
  11665. struct lpfc_nodelist *ndlp;
  11666. struct lpfc_dmabuf *mp;
  11667. unsigned long iflags;
  11668. LPFC_MBOXQ_t *pmb;
  11669. bool workposted = false;
  11670. int rc;
  11671. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  11672. if (!bf_get(lpfc_trailer_completed, mcqe))
  11673. goto out_no_mqe_complete;
  11674. /* Get the reference to the active mbox command */
  11675. spin_lock_irqsave(&phba->hbalock, iflags);
  11676. pmb = phba->sli.mbox_active;
  11677. if (unlikely(!pmb)) {
  11678. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  11679. "1832 No pending MBOX command to handle\n");
  11680. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11681. goto out_no_mqe_complete;
  11682. }
  11683. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11684. mqe = &pmb->u.mqe;
  11685. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  11686. mbox = phba->mbox;
  11687. vport = pmb->vport;
  11688. /* Reset heartbeat timer */
  11689. phba->last_completion_time = jiffies;
  11690. del_timer(&phba->sli.mbox_tmo);
  11691. /* Move mbox data to caller's mailbox region, do endian swapping */
  11692. if (pmb->mbox_cmpl && mbox)
  11693. lpfc_sli4_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  11694. /*
  11695. * For mcqe errors, conditionally move a modified error code to
  11696. * the mbox so that the error will not be missed.
  11697. */
  11698. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  11699. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  11700. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  11701. bf_set(lpfc_mqe_status, mqe,
  11702. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  11703. }
  11704. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  11705. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  11706. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  11707. "MBOX dflt rpi: status:x%x rpi:x%x",
  11708. mcqe_status,
  11709. pmbox->un.varWords[0], 0);
  11710. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  11711. mp = (struct lpfc_dmabuf *)(pmb->context1);
  11712. ndlp = (struct lpfc_nodelist *)pmb->context2;
  11713. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  11714. * RID of the PPI using the same mbox buffer.
  11715. */
  11716. lpfc_unreg_login(phba, vport->vpi,
  11717. pmbox->un.varWords[0], pmb);
  11718. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  11719. pmb->context1 = mp;
  11720. pmb->context2 = ndlp;
  11721. pmb->vport = vport;
  11722. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  11723. if (rc != MBX_BUSY)
  11724. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  11725. LOG_SLI, "0385 rc should "
  11726. "have been MBX_BUSY\n");
  11727. if (rc != MBX_NOT_FINISHED)
  11728. goto send_current_mbox;
  11729. }
  11730. }
  11731. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  11732. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  11733. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  11734. /* There is mailbox completion work to do */
  11735. spin_lock_irqsave(&phba->hbalock, iflags);
  11736. __lpfc_mbox_cmpl_put(phba, pmb);
  11737. phba->work_ha |= HA_MBATT;
  11738. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11739. workposted = true;
  11740. send_current_mbox:
  11741. spin_lock_irqsave(&phba->hbalock, iflags);
  11742. /* Release the mailbox command posting token */
  11743. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  11744. /* Setting active mailbox pointer need to be in sync to flag clear */
  11745. phba->sli.mbox_active = NULL;
  11746. if (bf_get(lpfc_trailer_consumed, mcqe))
  11747. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  11748. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11749. /* Wake up worker thread to post the next pending mailbox command */
  11750. lpfc_worker_wake_up(phba);
  11751. return workposted;
  11752. out_no_mqe_complete:
  11753. spin_lock_irqsave(&phba->hbalock, iflags);
  11754. if (bf_get(lpfc_trailer_consumed, mcqe))
  11755. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  11756. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11757. return false;
  11758. }
  11759. /**
  11760. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  11761. * @phba: Pointer to HBA context object.
  11762. * @cqe: Pointer to mailbox completion queue entry.
  11763. *
  11764. * This routine process a mailbox completion queue entry, it invokes the
  11765. * proper mailbox complete handling or asynchrous event handling routine
  11766. * according to the MCQE's async bit.
  11767. *
  11768. * Return: true if work posted to worker thread, otherwise false.
  11769. **/
  11770. static bool
  11771. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  11772. {
  11773. struct lpfc_mcqe mcqe;
  11774. bool workposted;
  11775. /* Copy the mailbox MCQE and convert endian order as needed */
  11776. lpfc_sli4_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  11777. /* Invoke the proper event handling routine */
  11778. if (!bf_get(lpfc_trailer_async, &mcqe))
  11779. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  11780. else
  11781. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  11782. return workposted;
  11783. }
  11784. /**
  11785. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  11786. * @phba: Pointer to HBA context object.
  11787. * @cq: Pointer to associated CQ
  11788. * @wcqe: Pointer to work-queue completion queue entry.
  11789. *
  11790. * This routine handles an ELS work-queue completion event.
  11791. *
  11792. * Return: true if work posted to worker thread, otherwise false.
  11793. **/
  11794. static bool
  11795. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11796. struct lpfc_wcqe_complete *wcqe)
  11797. {
  11798. struct lpfc_iocbq *irspiocbq;
  11799. unsigned long iflags;
  11800. struct lpfc_sli_ring *pring = cq->pring;
  11801. int txq_cnt = 0;
  11802. int txcmplq_cnt = 0;
  11803. int fcp_txcmplq_cnt = 0;
  11804. /* Check for response status */
  11805. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  11806. /* Log the error status */
  11807. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  11808. "0357 ELS CQE error: status=x%x: "
  11809. "CQE: %08x %08x %08x %08x\n",
  11810. bf_get(lpfc_wcqe_c_status, wcqe),
  11811. wcqe->word0, wcqe->total_data_placed,
  11812. wcqe->parameter, wcqe->word3);
  11813. }
  11814. /* Get an irspiocbq for later ELS response processing use */
  11815. irspiocbq = lpfc_sli_get_iocbq(phba);
  11816. if (!irspiocbq) {
  11817. if (!list_empty(&pring->txq))
  11818. txq_cnt++;
  11819. if (!list_empty(&pring->txcmplq))
  11820. txcmplq_cnt++;
  11821. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11822. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  11823. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  11824. txq_cnt, phba->iocb_cnt,
  11825. fcp_txcmplq_cnt,
  11826. txcmplq_cnt);
  11827. return false;
  11828. }
  11829. /* Save off the slow-path queue event for work thread to process */
  11830. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  11831. spin_lock_irqsave(&phba->hbalock, iflags);
  11832. list_add_tail(&irspiocbq->cq_event.list,
  11833. &phba->sli4_hba.sp_queue_event);
  11834. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  11835. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11836. return true;
  11837. }
  11838. /**
  11839. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  11840. * @phba: Pointer to HBA context object.
  11841. * @wcqe: Pointer to work-queue completion queue entry.
  11842. *
  11843. * This routine handles slow-path WQ entry consumed event by invoking the
  11844. * proper WQ release routine to the slow-path WQ.
  11845. **/
  11846. static void
  11847. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  11848. struct lpfc_wcqe_release *wcqe)
  11849. {
  11850. /* sanity check on queue memory */
  11851. if (unlikely(!phba->sli4_hba.els_wq))
  11852. return;
  11853. /* Check for the slow-path ELS work queue */
  11854. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  11855. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  11856. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  11857. else
  11858. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  11859. "2579 Slow-path wqe consume event carries "
  11860. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  11861. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  11862. phba->sli4_hba.els_wq->queue_id);
  11863. }
  11864. /**
  11865. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  11866. * @phba: Pointer to HBA context object.
  11867. * @cq: Pointer to a WQ completion queue.
  11868. * @wcqe: Pointer to work-queue completion queue entry.
  11869. *
  11870. * This routine handles an XRI abort event.
  11871. *
  11872. * Return: true if work posted to worker thread, otherwise false.
  11873. **/
  11874. static bool
  11875. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  11876. struct lpfc_queue *cq,
  11877. struct sli4_wcqe_xri_aborted *wcqe)
  11878. {
  11879. bool workposted = false;
  11880. struct lpfc_cq_event *cq_event;
  11881. unsigned long iflags;
  11882. switch (cq->subtype) {
  11883. case LPFC_FCP:
  11884. cq_event = lpfc_cq_event_setup(
  11885. phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  11886. if (!cq_event)
  11887. return false;
  11888. spin_lock_irqsave(&phba->hbalock, iflags);
  11889. list_add_tail(&cq_event->list,
  11890. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  11891. /* Set the fcp xri abort event flag */
  11892. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  11893. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11894. workposted = true;
  11895. break;
  11896. case LPFC_NVME_LS: /* NVME LS uses ELS resources */
  11897. case LPFC_ELS:
  11898. cq_event = lpfc_cq_event_setup(
  11899. phba, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  11900. if (!cq_event)
  11901. return false;
  11902. spin_lock_irqsave(&phba->hbalock, iflags);
  11903. list_add_tail(&cq_event->list,
  11904. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  11905. /* Set the els xri abort event flag */
  11906. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  11907. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11908. workposted = true;
  11909. break;
  11910. case LPFC_NVME:
  11911. /* Notify aborted XRI for NVME work queue */
  11912. if (phba->nvmet_support)
  11913. lpfc_sli4_nvmet_xri_aborted(phba, wcqe);
  11914. else
  11915. lpfc_sli4_nvme_xri_aborted(phba, wcqe);
  11916. workposted = false;
  11917. break;
  11918. default:
  11919. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11920. "0603 Invalid CQ subtype %d: "
  11921. "%08x %08x %08x %08x\n",
  11922. cq->subtype, wcqe->word0, wcqe->parameter,
  11923. wcqe->word2, wcqe->word3);
  11924. workposted = false;
  11925. break;
  11926. }
  11927. return workposted;
  11928. }
  11929. /**
  11930. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  11931. * @phba: Pointer to HBA context object.
  11932. * @rcqe: Pointer to receive-queue completion queue entry.
  11933. *
  11934. * This routine process a receive-queue completion queue entry.
  11935. *
  11936. * Return: true if work posted to worker thread, otherwise false.
  11937. **/
  11938. static bool
  11939. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  11940. {
  11941. bool workposted = false;
  11942. struct fc_frame_header *fc_hdr;
  11943. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  11944. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  11945. struct lpfc_nvmet_tgtport *tgtp;
  11946. struct hbq_dmabuf *dma_buf;
  11947. uint32_t status, rq_id;
  11948. unsigned long iflags;
  11949. /* sanity check on queue memory */
  11950. if (unlikely(!hrq) || unlikely(!drq))
  11951. return workposted;
  11952. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  11953. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  11954. else
  11955. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  11956. if (rq_id != hrq->queue_id)
  11957. goto out;
  11958. status = bf_get(lpfc_rcqe_status, rcqe);
  11959. switch (status) {
  11960. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  11961. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11962. "2537 Receive Frame Truncated!!\n");
  11963. case FC_STATUS_RQ_SUCCESS:
  11964. spin_lock_irqsave(&phba->hbalock, iflags);
  11965. lpfc_sli4_rq_release(hrq, drq);
  11966. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  11967. if (!dma_buf) {
  11968. hrq->RQ_no_buf_found++;
  11969. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11970. goto out;
  11971. }
  11972. hrq->RQ_rcv_buf++;
  11973. hrq->RQ_buf_posted--;
  11974. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  11975. /* If a NVME LS event (type 0x28), treat it as Fast path */
  11976. fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
  11977. /* save off the frame for the word thread to process */
  11978. list_add_tail(&dma_buf->cq_event.list,
  11979. &phba->sli4_hba.sp_queue_event);
  11980. /* Frame received */
  11981. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  11982. spin_unlock_irqrestore(&phba->hbalock, iflags);
  11983. workposted = true;
  11984. break;
  11985. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  11986. if (phba->nvmet_support) {
  11987. tgtp = phba->targetport->private;
  11988. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
  11989. "6402 RQE Error x%x, posted %d err_cnt "
  11990. "%d: %x %x %x\n",
  11991. status, hrq->RQ_buf_posted,
  11992. hrq->RQ_no_posted_buf,
  11993. atomic_read(&tgtp->rcv_fcp_cmd_in),
  11994. atomic_read(&tgtp->rcv_fcp_cmd_out),
  11995. atomic_read(&tgtp->xmt_fcp_release));
  11996. }
  11997. /* fallthrough */
  11998. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  11999. hrq->RQ_no_posted_buf++;
  12000. /* Post more buffers if possible */
  12001. spin_lock_irqsave(&phba->hbalock, iflags);
  12002. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  12003. spin_unlock_irqrestore(&phba->hbalock, iflags);
  12004. workposted = true;
  12005. break;
  12006. }
  12007. out:
  12008. return workposted;
  12009. }
  12010. /**
  12011. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  12012. * @phba: Pointer to HBA context object.
  12013. * @cq: Pointer to the completion queue.
  12014. * @wcqe: Pointer to a completion queue entry.
  12015. *
  12016. * This routine process a slow-path work-queue or receive queue completion queue
  12017. * entry.
  12018. *
  12019. * Return: true if work posted to worker thread, otherwise false.
  12020. **/
  12021. static bool
  12022. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  12023. struct lpfc_cqe *cqe)
  12024. {
  12025. struct lpfc_cqe cqevt;
  12026. bool workposted = false;
  12027. /* Copy the work queue CQE and convert endian order if needed */
  12028. lpfc_sli4_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  12029. /* Check and process for different type of WCQE and dispatch */
  12030. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  12031. case CQE_CODE_COMPL_WQE:
  12032. /* Process the WQ/RQ complete event */
  12033. phba->last_completion_time = jiffies;
  12034. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  12035. (struct lpfc_wcqe_complete *)&cqevt);
  12036. break;
  12037. case CQE_CODE_RELEASE_WQE:
  12038. /* Process the WQ release event */
  12039. lpfc_sli4_sp_handle_rel_wcqe(phba,
  12040. (struct lpfc_wcqe_release *)&cqevt);
  12041. break;
  12042. case CQE_CODE_XRI_ABORTED:
  12043. /* Process the WQ XRI abort event */
  12044. phba->last_completion_time = jiffies;
  12045. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  12046. (struct sli4_wcqe_xri_aborted *)&cqevt);
  12047. break;
  12048. case CQE_CODE_RECEIVE:
  12049. case CQE_CODE_RECEIVE_V1:
  12050. /* Process the RQ event */
  12051. phba->last_completion_time = jiffies;
  12052. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  12053. (struct lpfc_rcqe *)&cqevt);
  12054. break;
  12055. default:
  12056. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12057. "0388 Not a valid WCQE code: x%x\n",
  12058. bf_get(lpfc_cqe_code, &cqevt));
  12059. break;
  12060. }
  12061. return workposted;
  12062. }
  12063. /**
  12064. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  12065. * @phba: Pointer to HBA context object.
  12066. * @eqe: Pointer to fast-path event queue entry.
  12067. *
  12068. * This routine process a event queue entry from the slow-path event queue.
  12069. * It will check the MajorCode and MinorCode to determine this is for a
  12070. * completion event on a completion queue, if not, an error shall be logged
  12071. * and just return. Otherwise, it will get to the corresponding completion
  12072. * queue and process all the entries on that completion queue, rearm the
  12073. * completion queue, and then return.
  12074. *
  12075. **/
  12076. static void
  12077. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  12078. struct lpfc_queue *speq)
  12079. {
  12080. struct lpfc_queue *cq = NULL, *childq;
  12081. uint16_t cqid;
  12082. /* Get the reference to the corresponding CQ */
  12083. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  12084. list_for_each_entry(childq, &speq->child_list, list) {
  12085. if (childq->queue_id == cqid) {
  12086. cq = childq;
  12087. break;
  12088. }
  12089. }
  12090. if (unlikely(!cq)) {
  12091. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  12092. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12093. "0365 Slow-path CQ identifier "
  12094. "(%d) does not exist\n", cqid);
  12095. return;
  12096. }
  12097. /* Save EQ associated with this CQ */
  12098. cq->assoc_qp = speq;
  12099. if (!queue_work(phba->wq, &cq->spwork))
  12100. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12101. "0390 Cannot schedule soft IRQ "
  12102. "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
  12103. cqid, cq->queue_id, smp_processor_id());
  12104. }
  12105. /**
  12106. * lpfc_sli4_sp_process_cq - Process a slow-path event queue entry
  12107. * @phba: Pointer to HBA context object.
  12108. *
  12109. * This routine process a event queue entry from the slow-path event queue.
  12110. * It will check the MajorCode and MinorCode to determine this is for a
  12111. * completion event on a completion queue, if not, an error shall be logged
  12112. * and just return. Otherwise, it will get to the corresponding completion
  12113. * queue and process all the entries on that completion queue, rearm the
  12114. * completion queue, and then return.
  12115. *
  12116. **/
  12117. static void
  12118. lpfc_sli4_sp_process_cq(struct work_struct *work)
  12119. {
  12120. struct lpfc_queue *cq =
  12121. container_of(work, struct lpfc_queue, spwork);
  12122. struct lpfc_hba *phba = cq->phba;
  12123. struct lpfc_cqe *cqe;
  12124. bool workposted = false;
  12125. int ccount = 0;
  12126. /* Process all the entries to the CQ */
  12127. switch (cq->type) {
  12128. case LPFC_MCQ:
  12129. while ((cqe = lpfc_sli4_cq_get(cq))) {
  12130. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  12131. if (!(++ccount % cq->entry_repost))
  12132. break;
  12133. cq->CQ_mbox++;
  12134. }
  12135. break;
  12136. case LPFC_WCQ:
  12137. while ((cqe = lpfc_sli4_cq_get(cq))) {
  12138. if (cq->subtype == LPFC_FCP ||
  12139. cq->subtype == LPFC_NVME) {
  12140. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  12141. if (phba->ktime_on)
  12142. cq->isr_timestamp = ktime_get_ns();
  12143. else
  12144. cq->isr_timestamp = 0;
  12145. #endif
  12146. workposted |= lpfc_sli4_fp_handle_cqe(phba, cq,
  12147. cqe);
  12148. } else {
  12149. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  12150. cqe);
  12151. }
  12152. if (!(++ccount % cq->entry_repost))
  12153. break;
  12154. }
  12155. /* Track the max number of CQEs processed in 1 EQ */
  12156. if (ccount > cq->CQ_max_cqe)
  12157. cq->CQ_max_cqe = ccount;
  12158. break;
  12159. default:
  12160. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12161. "0370 Invalid completion queue type (%d)\n",
  12162. cq->type);
  12163. return;
  12164. }
  12165. /* Catch the no cq entry condition, log an error */
  12166. if (unlikely(ccount == 0))
  12167. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12168. "0371 No entry from the CQ: identifier "
  12169. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  12170. /* In any case, flash and re-arm the RCQ */
  12171. phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
  12172. /* wake up worker thread if there are works to be done */
  12173. if (workposted)
  12174. lpfc_worker_wake_up(phba);
  12175. }
  12176. /**
  12177. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  12178. * @phba: Pointer to HBA context object.
  12179. * @cq: Pointer to associated CQ
  12180. * @wcqe: Pointer to work-queue completion queue entry.
  12181. *
  12182. * This routine process a fast-path work queue completion entry from fast-path
  12183. * event queue for FCP command response completion.
  12184. **/
  12185. static void
  12186. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  12187. struct lpfc_wcqe_complete *wcqe)
  12188. {
  12189. struct lpfc_sli_ring *pring = cq->pring;
  12190. struct lpfc_iocbq *cmdiocbq;
  12191. struct lpfc_iocbq irspiocbq;
  12192. unsigned long iflags;
  12193. /* Check for response status */
  12194. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  12195. /* If resource errors reported from HBA, reduce queue
  12196. * depth of the SCSI device.
  12197. */
  12198. if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
  12199. IOSTAT_LOCAL_REJECT)) &&
  12200. ((wcqe->parameter & IOERR_PARAM_MASK) ==
  12201. IOERR_NO_RESOURCES))
  12202. phba->lpfc_rampdown_queue_depth(phba);
  12203. /* Log the error status */
  12204. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  12205. "0373 FCP CQE error: status=x%x: "
  12206. "CQE: %08x %08x %08x %08x\n",
  12207. bf_get(lpfc_wcqe_c_status, wcqe),
  12208. wcqe->word0, wcqe->total_data_placed,
  12209. wcqe->parameter, wcqe->word3);
  12210. }
  12211. /* Look up the FCP command IOCB and create pseudo response IOCB */
  12212. spin_lock_irqsave(&pring->ring_lock, iflags);
  12213. pring->stats.iocb_event++;
  12214. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  12215. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  12216. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  12217. if (unlikely(!cmdiocbq)) {
  12218. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12219. "0374 FCP complete with no corresponding "
  12220. "cmdiocb: iotag (%d)\n",
  12221. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  12222. return;
  12223. }
  12224. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  12225. cmdiocbq->isr_timestamp = cq->isr_timestamp;
  12226. #endif
  12227. if (cmdiocbq->iocb_cmpl == NULL) {
  12228. if (cmdiocbq->wqe_cmpl) {
  12229. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  12230. spin_lock_irqsave(&phba->hbalock, iflags);
  12231. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  12232. spin_unlock_irqrestore(&phba->hbalock, iflags);
  12233. }
  12234. /* Pass the cmd_iocb and the wcqe to the upper layer */
  12235. (cmdiocbq->wqe_cmpl)(phba, cmdiocbq, wcqe);
  12236. return;
  12237. }
  12238. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12239. "0375 FCP cmdiocb not callback function "
  12240. "iotag: (%d)\n",
  12241. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  12242. return;
  12243. }
  12244. /* Fake the irspiocb and copy necessary response information */
  12245. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  12246. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  12247. spin_lock_irqsave(&phba->hbalock, iflags);
  12248. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  12249. spin_unlock_irqrestore(&phba->hbalock, iflags);
  12250. }
  12251. /* Pass the cmd_iocb and the rsp state to the upper layer */
  12252. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  12253. }
  12254. /**
  12255. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  12256. * @phba: Pointer to HBA context object.
  12257. * @cq: Pointer to completion queue.
  12258. * @wcqe: Pointer to work-queue completion queue entry.
  12259. *
  12260. * This routine handles an fast-path WQ entry consumed event by invoking the
  12261. * proper WQ release routine to the slow-path WQ.
  12262. **/
  12263. static void
  12264. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  12265. struct lpfc_wcqe_release *wcqe)
  12266. {
  12267. struct lpfc_queue *childwq;
  12268. bool wqid_matched = false;
  12269. uint16_t hba_wqid;
  12270. /* Check for fast-path FCP work queue release */
  12271. hba_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  12272. list_for_each_entry(childwq, &cq->child_list, list) {
  12273. if (childwq->queue_id == hba_wqid) {
  12274. lpfc_sli4_wq_release(childwq,
  12275. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  12276. if (childwq->q_flag & HBA_NVMET_WQFULL)
  12277. lpfc_nvmet_wqfull_process(phba, childwq);
  12278. wqid_matched = true;
  12279. break;
  12280. }
  12281. }
  12282. /* Report warning log message if no match found */
  12283. if (wqid_matched != true)
  12284. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12285. "2580 Fast-path wqe consume event carries "
  12286. "miss-matched qid: wcqe-qid=x%x\n", hba_wqid);
  12287. }
  12288. /**
  12289. * lpfc_sli4_nvmet_handle_rcqe - Process a receive-queue completion queue entry
  12290. * @phba: Pointer to HBA context object.
  12291. * @rcqe: Pointer to receive-queue completion queue entry.
  12292. *
  12293. * This routine process a receive-queue completion queue entry.
  12294. *
  12295. * Return: true if work posted to worker thread, otherwise false.
  12296. **/
  12297. static bool
  12298. lpfc_sli4_nvmet_handle_rcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  12299. struct lpfc_rcqe *rcqe)
  12300. {
  12301. bool workposted = false;
  12302. struct lpfc_queue *hrq;
  12303. struct lpfc_queue *drq;
  12304. struct rqb_dmabuf *dma_buf;
  12305. struct fc_frame_header *fc_hdr;
  12306. struct lpfc_nvmet_tgtport *tgtp;
  12307. uint32_t status, rq_id;
  12308. unsigned long iflags;
  12309. uint32_t fctl, idx;
  12310. if ((phba->nvmet_support == 0) ||
  12311. (phba->sli4_hba.nvmet_cqset == NULL))
  12312. return workposted;
  12313. idx = cq->queue_id - phba->sli4_hba.nvmet_cqset[0]->queue_id;
  12314. hrq = phba->sli4_hba.nvmet_mrq_hdr[idx];
  12315. drq = phba->sli4_hba.nvmet_mrq_data[idx];
  12316. /* sanity check on queue memory */
  12317. if (unlikely(!hrq) || unlikely(!drq))
  12318. return workposted;
  12319. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  12320. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  12321. else
  12322. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  12323. if ((phba->nvmet_support == 0) ||
  12324. (rq_id != hrq->queue_id))
  12325. return workposted;
  12326. status = bf_get(lpfc_rcqe_status, rcqe);
  12327. switch (status) {
  12328. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  12329. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12330. "6126 Receive Frame Truncated!!\n");
  12331. /* Drop thru */
  12332. case FC_STATUS_RQ_SUCCESS:
  12333. spin_lock_irqsave(&phba->hbalock, iflags);
  12334. lpfc_sli4_rq_release(hrq, drq);
  12335. dma_buf = lpfc_sli_rqbuf_get(phba, hrq);
  12336. if (!dma_buf) {
  12337. hrq->RQ_no_buf_found++;
  12338. spin_unlock_irqrestore(&phba->hbalock, iflags);
  12339. goto out;
  12340. }
  12341. spin_unlock_irqrestore(&phba->hbalock, iflags);
  12342. hrq->RQ_rcv_buf++;
  12343. hrq->RQ_buf_posted--;
  12344. fc_hdr = (struct fc_frame_header *)dma_buf->hbuf.virt;
  12345. /* Just some basic sanity checks on FCP Command frame */
  12346. fctl = (fc_hdr->fh_f_ctl[0] << 16 |
  12347. fc_hdr->fh_f_ctl[1] << 8 |
  12348. fc_hdr->fh_f_ctl[2]);
  12349. if (((fctl &
  12350. (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) !=
  12351. (FC_FC_FIRST_SEQ | FC_FC_END_SEQ | FC_FC_SEQ_INIT)) ||
  12352. (fc_hdr->fh_seq_cnt != 0)) /* 0 byte swapped is still 0 */
  12353. goto drop;
  12354. if (fc_hdr->fh_type == FC_TYPE_FCP) {
  12355. dma_buf->bytes_recv = bf_get(lpfc_rcqe_length, rcqe);
  12356. lpfc_nvmet_unsol_fcp_event(
  12357. phba, idx, dma_buf,
  12358. cq->isr_timestamp);
  12359. return false;
  12360. }
  12361. drop:
  12362. lpfc_in_buf_free(phba, &dma_buf->dbuf);
  12363. break;
  12364. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  12365. if (phba->nvmet_support) {
  12366. tgtp = phba->targetport->private;
  12367. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_NVME,
  12368. "6401 RQE Error x%x, posted %d err_cnt "
  12369. "%d: %x %x %x\n",
  12370. status, hrq->RQ_buf_posted,
  12371. hrq->RQ_no_posted_buf,
  12372. atomic_read(&tgtp->rcv_fcp_cmd_in),
  12373. atomic_read(&tgtp->rcv_fcp_cmd_out),
  12374. atomic_read(&tgtp->xmt_fcp_release));
  12375. }
  12376. /* fallthrough */
  12377. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  12378. hrq->RQ_no_posted_buf++;
  12379. /* Post more buffers if possible */
  12380. break;
  12381. }
  12382. out:
  12383. return workposted;
  12384. }
  12385. /**
  12386. * lpfc_sli4_fp_handle_cqe - Process fast-path work queue completion entry
  12387. * @cq: Pointer to the completion queue.
  12388. * @eqe: Pointer to fast-path completion queue entry.
  12389. *
  12390. * This routine process a fast-path work queue completion entry from fast-path
  12391. * event queue for FCP command response completion.
  12392. **/
  12393. static int
  12394. lpfc_sli4_fp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  12395. struct lpfc_cqe *cqe)
  12396. {
  12397. struct lpfc_wcqe_release wcqe;
  12398. bool workposted = false;
  12399. /* Copy the work queue CQE and convert endian order if needed */
  12400. lpfc_sli4_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  12401. /* Check and process for different type of WCQE and dispatch */
  12402. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  12403. case CQE_CODE_COMPL_WQE:
  12404. case CQE_CODE_NVME_ERSP:
  12405. cq->CQ_wq++;
  12406. /* Process the WQ complete event */
  12407. phba->last_completion_time = jiffies;
  12408. if ((cq->subtype == LPFC_FCP) || (cq->subtype == LPFC_NVME))
  12409. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  12410. (struct lpfc_wcqe_complete *)&wcqe);
  12411. if (cq->subtype == LPFC_NVME_LS)
  12412. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  12413. (struct lpfc_wcqe_complete *)&wcqe);
  12414. break;
  12415. case CQE_CODE_RELEASE_WQE:
  12416. cq->CQ_release_wqe++;
  12417. /* Process the WQ release event */
  12418. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  12419. (struct lpfc_wcqe_release *)&wcqe);
  12420. break;
  12421. case CQE_CODE_XRI_ABORTED:
  12422. cq->CQ_xri_aborted++;
  12423. /* Process the WQ XRI abort event */
  12424. phba->last_completion_time = jiffies;
  12425. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  12426. (struct sli4_wcqe_xri_aborted *)&wcqe);
  12427. break;
  12428. case CQE_CODE_RECEIVE_V1:
  12429. case CQE_CODE_RECEIVE:
  12430. phba->last_completion_time = jiffies;
  12431. if (cq->subtype == LPFC_NVMET) {
  12432. workposted = lpfc_sli4_nvmet_handle_rcqe(
  12433. phba, cq, (struct lpfc_rcqe *)&wcqe);
  12434. }
  12435. break;
  12436. default:
  12437. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12438. "0144 Not a valid CQE code: x%x\n",
  12439. bf_get(lpfc_wcqe_c_code, &wcqe));
  12440. break;
  12441. }
  12442. return workposted;
  12443. }
  12444. /**
  12445. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  12446. * @phba: Pointer to HBA context object.
  12447. * @eqe: Pointer to fast-path event queue entry.
  12448. *
  12449. * This routine process a event queue entry from the fast-path event queue.
  12450. * It will check the MajorCode and MinorCode to determine this is for a
  12451. * completion event on a completion queue, if not, an error shall be logged
  12452. * and just return. Otherwise, it will get to the corresponding completion
  12453. * queue and process all the entries on the completion queue, rearm the
  12454. * completion queue, and then return.
  12455. **/
  12456. static void
  12457. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  12458. uint32_t qidx)
  12459. {
  12460. struct lpfc_queue *cq = NULL;
  12461. uint16_t cqid, id;
  12462. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  12463. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12464. "0366 Not a valid completion "
  12465. "event: majorcode=x%x, minorcode=x%x\n",
  12466. bf_get_le32(lpfc_eqe_major_code, eqe),
  12467. bf_get_le32(lpfc_eqe_minor_code, eqe));
  12468. return;
  12469. }
  12470. /* Get the reference to the corresponding CQ */
  12471. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  12472. if (phba->cfg_nvmet_mrq && phba->sli4_hba.nvmet_cqset) {
  12473. id = phba->sli4_hba.nvmet_cqset[0]->queue_id;
  12474. if ((cqid >= id) && (cqid < (id + phba->cfg_nvmet_mrq))) {
  12475. /* Process NVMET unsol rcv */
  12476. cq = phba->sli4_hba.nvmet_cqset[cqid - id];
  12477. goto process_cq;
  12478. }
  12479. }
  12480. if (phba->sli4_hba.nvme_cq_map &&
  12481. (cqid == phba->sli4_hba.nvme_cq_map[qidx])) {
  12482. /* Process NVME / NVMET command completion */
  12483. cq = phba->sli4_hba.nvme_cq[qidx];
  12484. goto process_cq;
  12485. }
  12486. if (phba->sli4_hba.fcp_cq_map &&
  12487. (cqid == phba->sli4_hba.fcp_cq_map[qidx])) {
  12488. /* Process FCP command completion */
  12489. cq = phba->sli4_hba.fcp_cq[qidx];
  12490. goto process_cq;
  12491. }
  12492. if (phba->sli4_hba.nvmels_cq &&
  12493. (cqid == phba->sli4_hba.nvmels_cq->queue_id)) {
  12494. /* Process NVME unsol rcv */
  12495. cq = phba->sli4_hba.nvmels_cq;
  12496. }
  12497. /* Otherwise this is a Slow path event */
  12498. if (cq == NULL) {
  12499. lpfc_sli4_sp_handle_eqe(phba, eqe, phba->sli4_hba.hba_eq[qidx]);
  12500. return;
  12501. }
  12502. process_cq:
  12503. if (unlikely(cqid != cq->queue_id)) {
  12504. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12505. "0368 Miss-matched fast-path completion "
  12506. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  12507. cqid, cq->queue_id);
  12508. return;
  12509. }
  12510. /* Save EQ associated with this CQ */
  12511. cq->assoc_qp = phba->sli4_hba.hba_eq[qidx];
  12512. if (!queue_work(phba->wq, &cq->irqwork))
  12513. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12514. "0363 Cannot schedule soft IRQ "
  12515. "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
  12516. cqid, cq->queue_id, smp_processor_id());
  12517. }
  12518. /**
  12519. * lpfc_sli4_hba_process_cq - Process a fast-path event queue entry
  12520. * @phba: Pointer to HBA context object.
  12521. * @eqe: Pointer to fast-path event queue entry.
  12522. *
  12523. * This routine process a event queue entry from the fast-path event queue.
  12524. * It will check the MajorCode and MinorCode to determine this is for a
  12525. * completion event on a completion queue, if not, an error shall be logged
  12526. * and just return. Otherwise, it will get to the corresponding completion
  12527. * queue and process all the entries on the completion queue, rearm the
  12528. * completion queue, and then return.
  12529. **/
  12530. static void
  12531. lpfc_sli4_hba_process_cq(struct work_struct *work)
  12532. {
  12533. struct lpfc_queue *cq =
  12534. container_of(work, struct lpfc_queue, irqwork);
  12535. struct lpfc_hba *phba = cq->phba;
  12536. struct lpfc_cqe *cqe;
  12537. bool workposted = false;
  12538. int ccount = 0;
  12539. /* Process all the entries to the CQ */
  12540. while ((cqe = lpfc_sli4_cq_get(cq))) {
  12541. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  12542. if (phba->ktime_on)
  12543. cq->isr_timestamp = ktime_get_ns();
  12544. else
  12545. cq->isr_timestamp = 0;
  12546. #endif
  12547. workposted |= lpfc_sli4_fp_handle_cqe(phba, cq, cqe);
  12548. if (!(++ccount % cq->entry_repost))
  12549. break;
  12550. }
  12551. /* Track the max number of CQEs processed in 1 EQ */
  12552. if (ccount > cq->CQ_max_cqe)
  12553. cq->CQ_max_cqe = ccount;
  12554. cq->assoc_qp->EQ_cqe_cnt += ccount;
  12555. /* Catch the no cq entry condition */
  12556. if (unlikely(ccount == 0))
  12557. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12558. "0369 No entry from fast-path completion "
  12559. "queue fcpcqid=%d\n", cq->queue_id);
  12560. /* In any case, flash and re-arm the CQ */
  12561. phba->sli4_hba.sli4_cq_release(cq, LPFC_QUEUE_REARM);
  12562. /* wake up worker thread if there are works to be done */
  12563. if (workposted)
  12564. lpfc_worker_wake_up(phba);
  12565. }
  12566. static void
  12567. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  12568. {
  12569. struct lpfc_eqe *eqe;
  12570. /* walk all the EQ entries and drop on the floor */
  12571. while ((eqe = lpfc_sli4_eq_get(eq)))
  12572. ;
  12573. /* Clear and re-arm the EQ */
  12574. phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
  12575. }
  12576. /**
  12577. * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
  12578. * entry
  12579. * @phba: Pointer to HBA context object.
  12580. * @eqe: Pointer to fast-path event queue entry.
  12581. *
  12582. * This routine process a event queue entry from the Flash Optimized Fabric
  12583. * event queue. It will check the MajorCode and MinorCode to determine this
  12584. * is for a completion event on a completion queue, if not, an error shall be
  12585. * logged and just return. Otherwise, it will get to the corresponding
  12586. * completion queue and process all the entries on the completion queue, rearm
  12587. * the completion queue, and then return.
  12588. **/
  12589. static void
  12590. lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  12591. {
  12592. struct lpfc_queue *cq;
  12593. uint16_t cqid;
  12594. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  12595. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12596. "9147 Not a valid completion "
  12597. "event: majorcode=x%x, minorcode=x%x\n",
  12598. bf_get_le32(lpfc_eqe_major_code, eqe),
  12599. bf_get_le32(lpfc_eqe_minor_code, eqe));
  12600. return;
  12601. }
  12602. /* Get the reference to the corresponding CQ */
  12603. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  12604. /* Next check for OAS */
  12605. cq = phba->sli4_hba.oas_cq;
  12606. if (unlikely(!cq)) {
  12607. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  12608. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12609. "9148 OAS completion queue "
  12610. "does not exist\n");
  12611. return;
  12612. }
  12613. if (unlikely(cqid != cq->queue_id)) {
  12614. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12615. "9149 Miss-matched fast-path compl "
  12616. "queue id: eqcqid=%d, fcpcqid=%d\n",
  12617. cqid, cq->queue_id);
  12618. return;
  12619. }
  12620. /* Save EQ associated with this CQ */
  12621. cq->assoc_qp = phba->sli4_hba.fof_eq;
  12622. /* CQ work will be processed on CPU affinitized to this IRQ */
  12623. if (!queue_work(phba->wq, &cq->irqwork))
  12624. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12625. "0367 Cannot schedule soft IRQ "
  12626. "for CQ eqcqid=%d, cqid=%d on CPU %d\n",
  12627. cqid, cq->queue_id, smp_processor_id());
  12628. }
  12629. /**
  12630. * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
  12631. * @irq: Interrupt number.
  12632. * @dev_id: The device context pointer.
  12633. *
  12634. * This function is directly called from the PCI layer as an interrupt
  12635. * service routine when device with SLI-4 interface spec is enabled with
  12636. * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
  12637. * IOCB ring event in the HBA. However, when the device is enabled with either
  12638. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  12639. * device-level interrupt handler. When the PCI slot is in error recovery
  12640. * or the HBA is undergoing initialization, the interrupt handler will not
  12641. * process the interrupt. The Flash Optimized Fabric ring event are handled in
  12642. * the intrrupt context. This function is called without any lock held.
  12643. * It gets the hbalock to access and update SLI data structures. Note that,
  12644. * the EQ to CQ are one-to-one map such that the EQ index is
  12645. * equal to that of CQ index.
  12646. *
  12647. * This function returns IRQ_HANDLED when interrupt is handled else it
  12648. * returns IRQ_NONE.
  12649. **/
  12650. irqreturn_t
  12651. lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
  12652. {
  12653. struct lpfc_hba *phba;
  12654. struct lpfc_hba_eq_hdl *hba_eq_hdl;
  12655. struct lpfc_queue *eq;
  12656. struct lpfc_eqe *eqe;
  12657. unsigned long iflag;
  12658. int ecount = 0;
  12659. /* Get the driver's phba structure from the dev_id */
  12660. hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
  12661. phba = hba_eq_hdl->phba;
  12662. if (unlikely(!phba))
  12663. return IRQ_NONE;
  12664. /* Get to the EQ struct associated with this vector */
  12665. eq = phba->sli4_hba.fof_eq;
  12666. if (unlikely(!eq))
  12667. return IRQ_NONE;
  12668. /* Check device state for handling interrupt */
  12669. if (unlikely(lpfc_intr_state_check(phba))) {
  12670. /* Check again for link_state with lock held */
  12671. spin_lock_irqsave(&phba->hbalock, iflag);
  12672. if (phba->link_state < LPFC_LINK_DOWN)
  12673. /* Flush, clear interrupt, and rearm the EQ */
  12674. lpfc_sli4_eq_flush(phba, eq);
  12675. spin_unlock_irqrestore(&phba->hbalock, iflag);
  12676. return IRQ_NONE;
  12677. }
  12678. /*
  12679. * Process all the event on FCP fast-path EQ
  12680. */
  12681. while ((eqe = lpfc_sli4_eq_get(eq))) {
  12682. lpfc_sli4_fof_handle_eqe(phba, eqe);
  12683. if (!(++ecount % eq->entry_repost))
  12684. break;
  12685. eq->EQ_processed++;
  12686. }
  12687. /* Track the max number of EQEs processed in 1 intr */
  12688. if (ecount > eq->EQ_max_eqe)
  12689. eq->EQ_max_eqe = ecount;
  12690. if (unlikely(ecount == 0)) {
  12691. eq->EQ_no_entry++;
  12692. if (phba->intr_type == MSIX)
  12693. /* MSI-X treated interrupt served as no EQ share INT */
  12694. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12695. "9145 MSI-X interrupt with no EQE\n");
  12696. else {
  12697. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12698. "9146 ISR interrupt with no EQE\n");
  12699. /* Non MSI-X treated on interrupt as EQ share INT */
  12700. return IRQ_NONE;
  12701. }
  12702. }
  12703. /* Always clear and re-arm the fast-path EQ */
  12704. phba->sli4_hba.sli4_eq_release(eq, LPFC_QUEUE_REARM);
  12705. return IRQ_HANDLED;
  12706. }
  12707. /**
  12708. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  12709. * @irq: Interrupt number.
  12710. * @dev_id: The device context pointer.
  12711. *
  12712. * This function is directly called from the PCI layer as an interrupt
  12713. * service routine when device with SLI-4 interface spec is enabled with
  12714. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  12715. * ring event in the HBA. However, when the device is enabled with either
  12716. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  12717. * device-level interrupt handler. When the PCI slot is in error recovery
  12718. * or the HBA is undergoing initialization, the interrupt handler will not
  12719. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  12720. * the intrrupt context. This function is called without any lock held.
  12721. * It gets the hbalock to access and update SLI data structures. Note that,
  12722. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  12723. * equal to that of FCP CQ index.
  12724. *
  12725. * The link attention and ELS ring attention events are handled
  12726. * by the worker thread. The interrupt handler signals the worker thread
  12727. * and returns for these events. This function is called without any lock
  12728. * held. It gets the hbalock to access and update SLI data structures.
  12729. *
  12730. * This function returns IRQ_HANDLED when interrupt is handled else it
  12731. * returns IRQ_NONE.
  12732. **/
  12733. irqreturn_t
  12734. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  12735. {
  12736. struct lpfc_hba *phba;
  12737. struct lpfc_hba_eq_hdl *hba_eq_hdl;
  12738. struct lpfc_queue *fpeq;
  12739. struct lpfc_eqe *eqe;
  12740. unsigned long iflag;
  12741. int ecount = 0;
  12742. int hba_eqidx;
  12743. /* Get the driver's phba structure from the dev_id */
  12744. hba_eq_hdl = (struct lpfc_hba_eq_hdl *)dev_id;
  12745. phba = hba_eq_hdl->phba;
  12746. hba_eqidx = hba_eq_hdl->idx;
  12747. if (unlikely(!phba))
  12748. return IRQ_NONE;
  12749. if (unlikely(!phba->sli4_hba.hba_eq))
  12750. return IRQ_NONE;
  12751. /* Get to the EQ struct associated with this vector */
  12752. fpeq = phba->sli4_hba.hba_eq[hba_eqidx];
  12753. if (unlikely(!fpeq))
  12754. return IRQ_NONE;
  12755. if (lpfc_fcp_look_ahead) {
  12756. if (atomic_dec_and_test(&hba_eq_hdl->hba_eq_in_use))
  12757. phba->sli4_hba.sli4_eq_clr_intr(fpeq);
  12758. else {
  12759. atomic_inc(&hba_eq_hdl->hba_eq_in_use);
  12760. return IRQ_NONE;
  12761. }
  12762. }
  12763. /* Check device state for handling interrupt */
  12764. if (unlikely(lpfc_intr_state_check(phba))) {
  12765. /* Check again for link_state with lock held */
  12766. spin_lock_irqsave(&phba->hbalock, iflag);
  12767. if (phba->link_state < LPFC_LINK_DOWN)
  12768. /* Flush, clear interrupt, and rearm the EQ */
  12769. lpfc_sli4_eq_flush(phba, fpeq);
  12770. spin_unlock_irqrestore(&phba->hbalock, iflag);
  12771. if (lpfc_fcp_look_ahead)
  12772. atomic_inc(&hba_eq_hdl->hba_eq_in_use);
  12773. return IRQ_NONE;
  12774. }
  12775. /*
  12776. * Process all the event on FCP fast-path EQ
  12777. */
  12778. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  12779. lpfc_sli4_hba_handle_eqe(phba, eqe, hba_eqidx);
  12780. if (!(++ecount % fpeq->entry_repost))
  12781. break;
  12782. fpeq->EQ_processed++;
  12783. }
  12784. /* Track the max number of EQEs processed in 1 intr */
  12785. if (ecount > fpeq->EQ_max_eqe)
  12786. fpeq->EQ_max_eqe = ecount;
  12787. /* Always clear and re-arm the fast-path EQ */
  12788. phba->sli4_hba.sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  12789. if (unlikely(ecount == 0)) {
  12790. fpeq->EQ_no_entry++;
  12791. if (lpfc_fcp_look_ahead) {
  12792. atomic_inc(&hba_eq_hdl->hba_eq_in_use);
  12793. return IRQ_NONE;
  12794. }
  12795. if (phba->intr_type == MSIX)
  12796. /* MSI-X treated interrupt served as no EQ share INT */
  12797. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12798. "0358 MSI-X interrupt with no EQE\n");
  12799. else
  12800. /* Non MSI-X treated on interrupt as EQ share INT */
  12801. return IRQ_NONE;
  12802. }
  12803. if (lpfc_fcp_look_ahead)
  12804. atomic_inc(&hba_eq_hdl->hba_eq_in_use);
  12805. return IRQ_HANDLED;
  12806. } /* lpfc_sli4_fp_intr_handler */
  12807. /**
  12808. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  12809. * @irq: Interrupt number.
  12810. * @dev_id: The device context pointer.
  12811. *
  12812. * This function is the device-level interrupt handler to device with SLI-4
  12813. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  12814. * interrupt mode is enabled and there is an event in the HBA which requires
  12815. * driver attention. This function invokes the slow-path interrupt attention
  12816. * handling function and fast-path interrupt attention handling function in
  12817. * turn to process the relevant HBA attention events. This function is called
  12818. * without any lock held. It gets the hbalock to access and update SLI data
  12819. * structures.
  12820. *
  12821. * This function returns IRQ_HANDLED when interrupt is handled, else it
  12822. * returns IRQ_NONE.
  12823. **/
  12824. irqreturn_t
  12825. lpfc_sli4_intr_handler(int irq, void *dev_id)
  12826. {
  12827. struct lpfc_hba *phba;
  12828. irqreturn_t hba_irq_rc;
  12829. bool hba_handled = false;
  12830. int qidx;
  12831. /* Get the driver's phba structure from the dev_id */
  12832. phba = (struct lpfc_hba *)dev_id;
  12833. if (unlikely(!phba))
  12834. return IRQ_NONE;
  12835. /*
  12836. * Invoke fast-path host attention interrupt handling as appropriate.
  12837. */
  12838. for (qidx = 0; qidx < phba->io_channel_irqs; qidx++) {
  12839. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  12840. &phba->sli4_hba.hba_eq_hdl[qidx]);
  12841. if (hba_irq_rc == IRQ_HANDLED)
  12842. hba_handled |= true;
  12843. }
  12844. if (phba->cfg_fof) {
  12845. hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
  12846. &phba->sli4_hba.hba_eq_hdl[qidx]);
  12847. if (hba_irq_rc == IRQ_HANDLED)
  12848. hba_handled |= true;
  12849. }
  12850. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  12851. } /* lpfc_sli4_intr_handler */
  12852. /**
  12853. * lpfc_sli4_queue_free - free a queue structure and associated memory
  12854. * @queue: The queue structure to free.
  12855. *
  12856. * This function frees a queue structure and the DMAable memory used for
  12857. * the host resident queue. This function must be called after destroying the
  12858. * queue on the HBA.
  12859. **/
  12860. void
  12861. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  12862. {
  12863. struct lpfc_dmabuf *dmabuf;
  12864. if (!queue)
  12865. return;
  12866. while (!list_empty(&queue->page_list)) {
  12867. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  12868. list);
  12869. dma_free_coherent(&queue->phba->pcidev->dev, queue->page_size,
  12870. dmabuf->virt, dmabuf->phys);
  12871. kfree(dmabuf);
  12872. }
  12873. if (queue->rqbp) {
  12874. lpfc_free_rq_buffer(queue->phba, queue);
  12875. kfree(queue->rqbp);
  12876. }
  12877. if (!list_empty(&queue->wq_list))
  12878. list_del(&queue->wq_list);
  12879. kfree(queue);
  12880. return;
  12881. }
  12882. /**
  12883. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  12884. * @phba: The HBA that this queue is being created on.
  12885. * @page_size: The size of a queue page
  12886. * @entry_size: The size of each queue entry for this queue.
  12887. * @entry count: The number of entries that this queue will handle.
  12888. *
  12889. * This function allocates a queue structure and the DMAable memory used for
  12890. * the host resident queue. This function must be called before creating the
  12891. * queue on the HBA.
  12892. **/
  12893. struct lpfc_queue *
  12894. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t page_size,
  12895. uint32_t entry_size, uint32_t entry_count)
  12896. {
  12897. struct lpfc_queue *queue;
  12898. struct lpfc_dmabuf *dmabuf;
  12899. int x, total_qe_count;
  12900. void *dma_pointer;
  12901. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  12902. if (!phba->sli4_hba.pc_sli4_params.supported)
  12903. hw_page_size = page_size;
  12904. queue = kzalloc(sizeof(struct lpfc_queue) +
  12905. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  12906. if (!queue)
  12907. return NULL;
  12908. queue->page_count = (ALIGN(entry_size * entry_count,
  12909. hw_page_size))/hw_page_size;
  12910. /* If needed, Adjust page count to match the max the adapter supports */
  12911. if (phba->sli4_hba.pc_sli4_params.wqpcnt &&
  12912. (queue->page_count > phba->sli4_hba.pc_sli4_params.wqpcnt))
  12913. queue->page_count = phba->sli4_hba.pc_sli4_params.wqpcnt;
  12914. INIT_LIST_HEAD(&queue->list);
  12915. INIT_LIST_HEAD(&queue->wq_list);
  12916. INIT_LIST_HEAD(&queue->wqfull_list);
  12917. INIT_LIST_HEAD(&queue->page_list);
  12918. INIT_LIST_HEAD(&queue->child_list);
  12919. /* Set queue parameters now. If the system cannot provide memory
  12920. * resources, the free routine needs to know what was allocated.
  12921. */
  12922. queue->entry_size = entry_size;
  12923. queue->entry_count = entry_count;
  12924. queue->page_size = hw_page_size;
  12925. queue->phba = phba;
  12926. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  12927. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  12928. if (!dmabuf)
  12929. goto out_fail;
  12930. dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
  12931. hw_page_size, &dmabuf->phys,
  12932. GFP_KERNEL);
  12933. if (!dmabuf->virt) {
  12934. kfree(dmabuf);
  12935. goto out_fail;
  12936. }
  12937. dmabuf->buffer_tag = x;
  12938. list_add_tail(&dmabuf->list, &queue->page_list);
  12939. /* initialize queue's entry array */
  12940. dma_pointer = dmabuf->virt;
  12941. for (; total_qe_count < entry_count &&
  12942. dma_pointer < (hw_page_size + dmabuf->virt);
  12943. total_qe_count++, dma_pointer += entry_size) {
  12944. queue->qe[total_qe_count].address = dma_pointer;
  12945. }
  12946. }
  12947. INIT_WORK(&queue->irqwork, lpfc_sli4_hba_process_cq);
  12948. INIT_WORK(&queue->spwork, lpfc_sli4_sp_process_cq);
  12949. /* entry_repost will be set during q creation */
  12950. return queue;
  12951. out_fail:
  12952. lpfc_sli4_queue_free(queue);
  12953. return NULL;
  12954. }
  12955. /**
  12956. * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
  12957. * @phba: HBA structure that indicates port to create a queue on.
  12958. * @pci_barset: PCI BAR set flag.
  12959. *
  12960. * This function shall perform iomap of the specified PCI BAR address to host
  12961. * memory address if not already done so and return it. The returned host
  12962. * memory address can be NULL.
  12963. */
  12964. static void __iomem *
  12965. lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
  12966. {
  12967. if (!phba->pcidev)
  12968. return NULL;
  12969. switch (pci_barset) {
  12970. case WQ_PCI_BAR_0_AND_1:
  12971. return phba->pci_bar0_memmap_p;
  12972. case WQ_PCI_BAR_2_AND_3:
  12973. return phba->pci_bar2_memmap_p;
  12974. case WQ_PCI_BAR_4_AND_5:
  12975. return phba->pci_bar4_memmap_p;
  12976. default:
  12977. break;
  12978. }
  12979. return NULL;
  12980. }
  12981. /**
  12982. * lpfc_modify_hba_eq_delay - Modify Delay Multiplier on FCP EQs
  12983. * @phba: HBA structure that indicates port to create a queue on.
  12984. * @startq: The starting FCP EQ to modify
  12985. *
  12986. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  12987. * The command allows up to LPFC_MAX_EQ_DELAY_EQID_CNT EQ ID's to be
  12988. * updated in one mailbox command.
  12989. *
  12990. * The @phba struct is used to send mailbox command to HBA. The @startq
  12991. * is used to get the starting FCP EQ to change.
  12992. * This function is asynchronous and will wait for the mailbox
  12993. * command to finish before continuing.
  12994. *
  12995. * On success this function will return a zero. If unable to allocate enough
  12996. * memory this function will return -ENOMEM. If the queue create mailbox command
  12997. * fails this function will return -ENXIO.
  12998. **/
  12999. int
  13000. lpfc_modify_hba_eq_delay(struct lpfc_hba *phba, uint32_t startq,
  13001. uint32_t numq, uint32_t imax)
  13002. {
  13003. struct lpfc_mbx_modify_eq_delay *eq_delay;
  13004. LPFC_MBOXQ_t *mbox;
  13005. struct lpfc_queue *eq;
  13006. int cnt, rc, length, status = 0;
  13007. uint32_t shdr_status, shdr_add_status;
  13008. uint32_t result, val;
  13009. int qidx;
  13010. union lpfc_sli4_cfg_shdr *shdr;
  13011. uint16_t dmult;
  13012. if (startq >= phba->io_channel_irqs)
  13013. return 0;
  13014. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13015. if (!mbox)
  13016. return -ENOMEM;
  13017. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  13018. sizeof(struct lpfc_sli4_cfg_mhdr));
  13019. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  13020. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  13021. length, LPFC_SLI4_MBX_EMBED);
  13022. eq_delay = &mbox->u.mqe.un.eq_delay;
  13023. /* Calculate delay multiper from maximum interrupt per second */
  13024. result = imax / phba->io_channel_irqs;
  13025. if (result > LPFC_DMULT_CONST || result == 0)
  13026. dmult = 0;
  13027. else
  13028. dmult = LPFC_DMULT_CONST/result - 1;
  13029. if (dmult > LPFC_DMULT_MAX)
  13030. dmult = LPFC_DMULT_MAX;
  13031. cnt = 0;
  13032. for (qidx = startq; qidx < phba->io_channel_irqs; qidx++) {
  13033. eq = phba->sli4_hba.hba_eq[qidx];
  13034. if (!eq)
  13035. continue;
  13036. eq->q_mode = imax;
  13037. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  13038. eq_delay->u.request.eq[cnt].phase = 0;
  13039. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  13040. cnt++;
  13041. /* q_mode is only used for auto_imax */
  13042. if (phba->sli.sli_flag & LPFC_SLI_USE_EQDR) {
  13043. /* Use EQ Delay Register method for q_mode */
  13044. /* Convert for EQ Delay register */
  13045. val = phba->cfg_fcp_imax;
  13046. if (val) {
  13047. /* First, interrupts per sec per EQ */
  13048. val = phba->cfg_fcp_imax /
  13049. phba->io_channel_irqs;
  13050. /* us delay between each interrupt */
  13051. val = LPFC_SEC_TO_USEC / val;
  13052. }
  13053. eq->q_mode = val;
  13054. } else {
  13055. eq->q_mode = imax;
  13056. }
  13057. if (cnt >= numq)
  13058. break;
  13059. }
  13060. eq_delay->u.request.num_eq = cnt;
  13061. mbox->vport = phba->pport;
  13062. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13063. mbox->context1 = NULL;
  13064. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13065. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  13066. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13067. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13068. if (shdr_status || shdr_add_status || rc) {
  13069. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13070. "2512 MODIFY_EQ_DELAY mailbox failed with "
  13071. "status x%x add_status x%x, mbx status x%x\n",
  13072. shdr_status, shdr_add_status, rc);
  13073. status = -ENXIO;
  13074. }
  13075. mempool_free(mbox, phba->mbox_mem_pool);
  13076. return status;
  13077. }
  13078. /**
  13079. * lpfc_eq_create - Create an Event Queue on the HBA
  13080. * @phba: HBA structure that indicates port to create a queue on.
  13081. * @eq: The queue structure to use to create the event queue.
  13082. * @imax: The maximum interrupt per second limit.
  13083. *
  13084. * This function creates an event queue, as detailed in @eq, on a port,
  13085. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  13086. *
  13087. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  13088. * is used to get the entry count and entry size that are necessary to
  13089. * determine the number of pages to allocate and use for this queue. This
  13090. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  13091. * event queue. This function is asynchronous and will wait for the mailbox
  13092. * command to finish before continuing.
  13093. *
  13094. * On success this function will return a zero. If unable to allocate enough
  13095. * memory this function will return -ENOMEM. If the queue create mailbox command
  13096. * fails this function will return -ENXIO.
  13097. **/
  13098. int
  13099. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
  13100. {
  13101. struct lpfc_mbx_eq_create *eq_create;
  13102. LPFC_MBOXQ_t *mbox;
  13103. int rc, length, status = 0;
  13104. struct lpfc_dmabuf *dmabuf;
  13105. uint32_t shdr_status, shdr_add_status;
  13106. union lpfc_sli4_cfg_shdr *shdr;
  13107. uint16_t dmult;
  13108. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  13109. /* sanity check on queue memory */
  13110. if (!eq)
  13111. return -ENODEV;
  13112. if (!phba->sli4_hba.pc_sli4_params.supported)
  13113. hw_page_size = SLI4_PAGE_SIZE;
  13114. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13115. if (!mbox)
  13116. return -ENOMEM;
  13117. length = (sizeof(struct lpfc_mbx_eq_create) -
  13118. sizeof(struct lpfc_sli4_cfg_mhdr));
  13119. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  13120. LPFC_MBOX_OPCODE_EQ_CREATE,
  13121. length, LPFC_SLI4_MBX_EMBED);
  13122. eq_create = &mbox->u.mqe.un.eq_create;
  13123. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  13124. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  13125. eq->page_count);
  13126. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  13127. LPFC_EQE_SIZE);
  13128. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  13129. /* Use version 2 of CREATE_EQ if eqav is set */
  13130. if (phba->sli4_hba.pc_sli4_params.eqav) {
  13131. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  13132. LPFC_Q_CREATE_VERSION_2);
  13133. bf_set(lpfc_eq_context_autovalid, &eq_create->u.request.context,
  13134. phba->sli4_hba.pc_sli4_params.eqav);
  13135. }
  13136. /* don't setup delay multiplier using EQ_CREATE */
  13137. dmult = 0;
  13138. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  13139. dmult);
  13140. switch (eq->entry_count) {
  13141. default:
  13142. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13143. "0360 Unsupported EQ count. (%d)\n",
  13144. eq->entry_count);
  13145. if (eq->entry_count < 256)
  13146. return -EINVAL;
  13147. /* otherwise default to smallest count (drop through) */
  13148. case 256:
  13149. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  13150. LPFC_EQ_CNT_256);
  13151. break;
  13152. case 512:
  13153. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  13154. LPFC_EQ_CNT_512);
  13155. break;
  13156. case 1024:
  13157. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  13158. LPFC_EQ_CNT_1024);
  13159. break;
  13160. case 2048:
  13161. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  13162. LPFC_EQ_CNT_2048);
  13163. break;
  13164. case 4096:
  13165. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  13166. LPFC_EQ_CNT_4096);
  13167. break;
  13168. }
  13169. list_for_each_entry(dmabuf, &eq->page_list, list) {
  13170. memset(dmabuf->virt, 0, hw_page_size);
  13171. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  13172. putPaddrLow(dmabuf->phys);
  13173. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  13174. putPaddrHigh(dmabuf->phys);
  13175. }
  13176. mbox->vport = phba->pport;
  13177. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13178. mbox->context1 = NULL;
  13179. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13180. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13181. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13182. if (shdr_status || shdr_add_status || rc) {
  13183. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13184. "2500 EQ_CREATE mailbox failed with "
  13185. "status x%x add_status x%x, mbx status x%x\n",
  13186. shdr_status, shdr_add_status, rc);
  13187. status = -ENXIO;
  13188. }
  13189. eq->type = LPFC_EQ;
  13190. eq->subtype = LPFC_NONE;
  13191. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  13192. if (eq->queue_id == 0xFFFF)
  13193. status = -ENXIO;
  13194. eq->host_index = 0;
  13195. eq->hba_index = 0;
  13196. eq->entry_repost = LPFC_EQ_REPOST;
  13197. mempool_free(mbox, phba->mbox_mem_pool);
  13198. return status;
  13199. }
  13200. /**
  13201. * lpfc_cq_create - Create a Completion Queue on the HBA
  13202. * @phba: HBA structure that indicates port to create a queue on.
  13203. * @cq: The queue structure to use to create the completion queue.
  13204. * @eq: The event queue to bind this completion queue to.
  13205. *
  13206. * This function creates a completion queue, as detailed in @wq, on a port,
  13207. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  13208. *
  13209. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  13210. * is used to get the entry count and entry size that are necessary to
  13211. * determine the number of pages to allocate and use for this queue. The @eq
  13212. * is used to indicate which event queue to bind this completion queue to. This
  13213. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  13214. * completion queue. This function is asynchronous and will wait for the mailbox
  13215. * command to finish before continuing.
  13216. *
  13217. * On success this function will return a zero. If unable to allocate enough
  13218. * memory this function will return -ENOMEM. If the queue create mailbox command
  13219. * fails this function will return -ENXIO.
  13220. **/
  13221. int
  13222. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  13223. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  13224. {
  13225. struct lpfc_mbx_cq_create *cq_create;
  13226. struct lpfc_dmabuf *dmabuf;
  13227. LPFC_MBOXQ_t *mbox;
  13228. int rc, length, status = 0;
  13229. uint32_t shdr_status, shdr_add_status;
  13230. union lpfc_sli4_cfg_shdr *shdr;
  13231. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  13232. /* sanity check on queue memory */
  13233. if (!cq || !eq)
  13234. return -ENODEV;
  13235. if (!phba->sli4_hba.pc_sli4_params.supported)
  13236. hw_page_size = cq->page_size;
  13237. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13238. if (!mbox)
  13239. return -ENOMEM;
  13240. length = (sizeof(struct lpfc_mbx_cq_create) -
  13241. sizeof(struct lpfc_sli4_cfg_mhdr));
  13242. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  13243. LPFC_MBOX_OPCODE_CQ_CREATE,
  13244. length, LPFC_SLI4_MBX_EMBED);
  13245. cq_create = &mbox->u.mqe.un.cq_create;
  13246. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  13247. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  13248. cq->page_count);
  13249. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  13250. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  13251. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  13252. phba->sli4_hba.pc_sli4_params.cqv);
  13253. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  13254. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request,
  13255. (cq->page_size / SLI4_PAGE_SIZE));
  13256. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  13257. eq->queue_id);
  13258. bf_set(lpfc_cq_context_autovalid, &cq_create->u.request.context,
  13259. phba->sli4_hba.pc_sli4_params.cqav);
  13260. } else {
  13261. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  13262. eq->queue_id);
  13263. }
  13264. switch (cq->entry_count) {
  13265. case 2048:
  13266. case 4096:
  13267. if (phba->sli4_hba.pc_sli4_params.cqv ==
  13268. LPFC_Q_CREATE_VERSION_2) {
  13269. cq_create->u.request.context.lpfc_cq_context_count =
  13270. cq->entry_count;
  13271. bf_set(lpfc_cq_context_count,
  13272. &cq_create->u.request.context,
  13273. LPFC_CQ_CNT_WORD7);
  13274. break;
  13275. }
  13276. /* Fall Thru */
  13277. default:
  13278. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13279. "0361 Unsupported CQ count: "
  13280. "entry cnt %d sz %d pg cnt %d\n",
  13281. cq->entry_count, cq->entry_size,
  13282. cq->page_count);
  13283. if (cq->entry_count < 256) {
  13284. status = -EINVAL;
  13285. goto out;
  13286. }
  13287. /* otherwise default to smallest count (drop through) */
  13288. case 256:
  13289. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  13290. LPFC_CQ_CNT_256);
  13291. break;
  13292. case 512:
  13293. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  13294. LPFC_CQ_CNT_512);
  13295. break;
  13296. case 1024:
  13297. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  13298. LPFC_CQ_CNT_1024);
  13299. break;
  13300. }
  13301. list_for_each_entry(dmabuf, &cq->page_list, list) {
  13302. memset(dmabuf->virt, 0, cq->page_size);
  13303. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  13304. putPaddrLow(dmabuf->phys);
  13305. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  13306. putPaddrHigh(dmabuf->phys);
  13307. }
  13308. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13309. /* The IOCTL status is embedded in the mailbox subheader. */
  13310. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13311. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13312. if (shdr_status || shdr_add_status || rc) {
  13313. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13314. "2501 CQ_CREATE mailbox failed with "
  13315. "status x%x add_status x%x, mbx status x%x\n",
  13316. shdr_status, shdr_add_status, rc);
  13317. status = -ENXIO;
  13318. goto out;
  13319. }
  13320. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  13321. if (cq->queue_id == 0xFFFF) {
  13322. status = -ENXIO;
  13323. goto out;
  13324. }
  13325. /* link the cq onto the parent eq child list */
  13326. list_add_tail(&cq->list, &eq->child_list);
  13327. /* Set up completion queue's type and subtype */
  13328. cq->type = type;
  13329. cq->subtype = subtype;
  13330. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  13331. cq->assoc_qid = eq->queue_id;
  13332. cq->host_index = 0;
  13333. cq->hba_index = 0;
  13334. cq->entry_repost = LPFC_CQ_REPOST;
  13335. out:
  13336. mempool_free(mbox, phba->mbox_mem_pool);
  13337. return status;
  13338. }
  13339. /**
  13340. * lpfc_cq_create_set - Create a set of Completion Queues on the HBA for MRQ
  13341. * @phba: HBA structure that indicates port to create a queue on.
  13342. * @cqp: The queue structure array to use to create the completion queues.
  13343. * @eqp: The event queue array to bind these completion queues to.
  13344. *
  13345. * This function creates a set of completion queue, s to support MRQ
  13346. * as detailed in @cqp, on a port,
  13347. * described by @phba by sending a CREATE_CQ_SET mailbox command to the HBA.
  13348. *
  13349. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  13350. * is used to get the entry count and entry size that are necessary to
  13351. * determine the number of pages to allocate and use for this queue. The @eq
  13352. * is used to indicate which event queue to bind this completion queue to. This
  13353. * function will send the CREATE_CQ_SET mailbox command to the HBA to setup the
  13354. * completion queue. This function is asynchronous and will wait for the mailbox
  13355. * command to finish before continuing.
  13356. *
  13357. * On success this function will return a zero. If unable to allocate enough
  13358. * memory this function will return -ENOMEM. If the queue create mailbox command
  13359. * fails this function will return -ENXIO.
  13360. **/
  13361. int
  13362. lpfc_cq_create_set(struct lpfc_hba *phba, struct lpfc_queue **cqp,
  13363. struct lpfc_queue **eqp, uint32_t type, uint32_t subtype)
  13364. {
  13365. struct lpfc_queue *cq;
  13366. struct lpfc_queue *eq;
  13367. struct lpfc_mbx_cq_create_set *cq_set;
  13368. struct lpfc_dmabuf *dmabuf;
  13369. LPFC_MBOXQ_t *mbox;
  13370. int rc, length, alloclen, status = 0;
  13371. int cnt, idx, numcq, page_idx = 0;
  13372. uint32_t shdr_status, shdr_add_status;
  13373. union lpfc_sli4_cfg_shdr *shdr;
  13374. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  13375. /* sanity check on queue memory */
  13376. numcq = phba->cfg_nvmet_mrq;
  13377. if (!cqp || !eqp || !numcq)
  13378. return -ENODEV;
  13379. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13380. if (!mbox)
  13381. return -ENOMEM;
  13382. length = sizeof(struct lpfc_mbx_cq_create_set);
  13383. length += ((numcq * cqp[0]->page_count) *
  13384. sizeof(struct dma_address));
  13385. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13386. LPFC_MBOX_OPCODE_FCOE_CQ_CREATE_SET, length,
  13387. LPFC_SLI4_MBX_NEMBED);
  13388. if (alloclen < length) {
  13389. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13390. "3098 Allocated DMA memory size (%d) is "
  13391. "less than the requested DMA memory size "
  13392. "(%d)\n", alloclen, length);
  13393. status = -ENOMEM;
  13394. goto out;
  13395. }
  13396. cq_set = mbox->sge_array->addr[0];
  13397. shdr = (union lpfc_sli4_cfg_shdr *)&cq_set->cfg_shdr;
  13398. bf_set(lpfc_mbox_hdr_version, &shdr->request, 0);
  13399. for (idx = 0; idx < numcq; idx++) {
  13400. cq = cqp[idx];
  13401. eq = eqp[idx];
  13402. if (!cq || !eq) {
  13403. status = -ENOMEM;
  13404. goto out;
  13405. }
  13406. if (!phba->sli4_hba.pc_sli4_params.supported)
  13407. hw_page_size = cq->page_size;
  13408. switch (idx) {
  13409. case 0:
  13410. bf_set(lpfc_mbx_cq_create_set_page_size,
  13411. &cq_set->u.request,
  13412. (hw_page_size / SLI4_PAGE_SIZE));
  13413. bf_set(lpfc_mbx_cq_create_set_num_pages,
  13414. &cq_set->u.request, cq->page_count);
  13415. bf_set(lpfc_mbx_cq_create_set_evt,
  13416. &cq_set->u.request, 1);
  13417. bf_set(lpfc_mbx_cq_create_set_valid,
  13418. &cq_set->u.request, 1);
  13419. bf_set(lpfc_mbx_cq_create_set_cqe_size,
  13420. &cq_set->u.request, 0);
  13421. bf_set(lpfc_mbx_cq_create_set_num_cq,
  13422. &cq_set->u.request, numcq);
  13423. bf_set(lpfc_mbx_cq_create_set_autovalid,
  13424. &cq_set->u.request,
  13425. phba->sli4_hba.pc_sli4_params.cqav);
  13426. switch (cq->entry_count) {
  13427. case 2048:
  13428. case 4096:
  13429. if (phba->sli4_hba.pc_sli4_params.cqv ==
  13430. LPFC_Q_CREATE_VERSION_2) {
  13431. bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
  13432. &cq_set->u.request,
  13433. cq->entry_count);
  13434. bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
  13435. &cq_set->u.request,
  13436. LPFC_CQ_CNT_WORD7);
  13437. break;
  13438. }
  13439. /* Fall Thru */
  13440. default:
  13441. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13442. "3118 Bad CQ count. (%d)\n",
  13443. cq->entry_count);
  13444. if (cq->entry_count < 256) {
  13445. status = -EINVAL;
  13446. goto out;
  13447. }
  13448. /* otherwise default to smallest (drop thru) */
  13449. case 256:
  13450. bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
  13451. &cq_set->u.request, LPFC_CQ_CNT_256);
  13452. break;
  13453. case 512:
  13454. bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
  13455. &cq_set->u.request, LPFC_CQ_CNT_512);
  13456. break;
  13457. case 1024:
  13458. bf_set(lpfc_mbx_cq_create_set_cqe_cnt,
  13459. &cq_set->u.request, LPFC_CQ_CNT_1024);
  13460. break;
  13461. }
  13462. bf_set(lpfc_mbx_cq_create_set_eq_id0,
  13463. &cq_set->u.request, eq->queue_id);
  13464. break;
  13465. case 1:
  13466. bf_set(lpfc_mbx_cq_create_set_eq_id1,
  13467. &cq_set->u.request, eq->queue_id);
  13468. break;
  13469. case 2:
  13470. bf_set(lpfc_mbx_cq_create_set_eq_id2,
  13471. &cq_set->u.request, eq->queue_id);
  13472. break;
  13473. case 3:
  13474. bf_set(lpfc_mbx_cq_create_set_eq_id3,
  13475. &cq_set->u.request, eq->queue_id);
  13476. break;
  13477. case 4:
  13478. bf_set(lpfc_mbx_cq_create_set_eq_id4,
  13479. &cq_set->u.request, eq->queue_id);
  13480. break;
  13481. case 5:
  13482. bf_set(lpfc_mbx_cq_create_set_eq_id5,
  13483. &cq_set->u.request, eq->queue_id);
  13484. break;
  13485. case 6:
  13486. bf_set(lpfc_mbx_cq_create_set_eq_id6,
  13487. &cq_set->u.request, eq->queue_id);
  13488. break;
  13489. case 7:
  13490. bf_set(lpfc_mbx_cq_create_set_eq_id7,
  13491. &cq_set->u.request, eq->queue_id);
  13492. break;
  13493. case 8:
  13494. bf_set(lpfc_mbx_cq_create_set_eq_id8,
  13495. &cq_set->u.request, eq->queue_id);
  13496. break;
  13497. case 9:
  13498. bf_set(lpfc_mbx_cq_create_set_eq_id9,
  13499. &cq_set->u.request, eq->queue_id);
  13500. break;
  13501. case 10:
  13502. bf_set(lpfc_mbx_cq_create_set_eq_id10,
  13503. &cq_set->u.request, eq->queue_id);
  13504. break;
  13505. case 11:
  13506. bf_set(lpfc_mbx_cq_create_set_eq_id11,
  13507. &cq_set->u.request, eq->queue_id);
  13508. break;
  13509. case 12:
  13510. bf_set(lpfc_mbx_cq_create_set_eq_id12,
  13511. &cq_set->u.request, eq->queue_id);
  13512. break;
  13513. case 13:
  13514. bf_set(lpfc_mbx_cq_create_set_eq_id13,
  13515. &cq_set->u.request, eq->queue_id);
  13516. break;
  13517. case 14:
  13518. bf_set(lpfc_mbx_cq_create_set_eq_id14,
  13519. &cq_set->u.request, eq->queue_id);
  13520. break;
  13521. case 15:
  13522. bf_set(lpfc_mbx_cq_create_set_eq_id15,
  13523. &cq_set->u.request, eq->queue_id);
  13524. break;
  13525. }
  13526. /* link the cq onto the parent eq child list */
  13527. list_add_tail(&cq->list, &eq->child_list);
  13528. /* Set up completion queue's type and subtype */
  13529. cq->type = type;
  13530. cq->subtype = subtype;
  13531. cq->assoc_qid = eq->queue_id;
  13532. cq->host_index = 0;
  13533. cq->hba_index = 0;
  13534. cq->entry_repost = LPFC_CQ_REPOST;
  13535. cq->chann = idx;
  13536. rc = 0;
  13537. list_for_each_entry(dmabuf, &cq->page_list, list) {
  13538. memset(dmabuf->virt, 0, hw_page_size);
  13539. cnt = page_idx + dmabuf->buffer_tag;
  13540. cq_set->u.request.page[cnt].addr_lo =
  13541. putPaddrLow(dmabuf->phys);
  13542. cq_set->u.request.page[cnt].addr_hi =
  13543. putPaddrHigh(dmabuf->phys);
  13544. rc++;
  13545. }
  13546. page_idx += rc;
  13547. }
  13548. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13549. /* The IOCTL status is embedded in the mailbox subheader. */
  13550. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13551. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13552. if (shdr_status || shdr_add_status || rc) {
  13553. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13554. "3119 CQ_CREATE_SET mailbox failed with "
  13555. "status x%x add_status x%x, mbx status x%x\n",
  13556. shdr_status, shdr_add_status, rc);
  13557. status = -ENXIO;
  13558. goto out;
  13559. }
  13560. rc = bf_get(lpfc_mbx_cq_create_set_base_id, &cq_set->u.response);
  13561. if (rc == 0xFFFF) {
  13562. status = -ENXIO;
  13563. goto out;
  13564. }
  13565. for (idx = 0; idx < numcq; idx++) {
  13566. cq = cqp[idx];
  13567. cq->queue_id = rc + idx;
  13568. }
  13569. out:
  13570. lpfc_sli4_mbox_cmd_free(phba, mbox);
  13571. return status;
  13572. }
  13573. /**
  13574. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  13575. * @phba: HBA structure that indicates port to create a queue on.
  13576. * @mq: The queue structure to use to create the mailbox queue.
  13577. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  13578. * @cq: The completion queue to associate with this cq.
  13579. *
  13580. * This function provides failback (fb) functionality when the
  13581. * mq_create_ext fails on older FW generations. It's purpose is identical
  13582. * to mq_create_ext otherwise.
  13583. *
  13584. * This routine cannot fail as all attributes were previously accessed and
  13585. * initialized in mq_create_ext.
  13586. **/
  13587. static void
  13588. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  13589. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  13590. {
  13591. struct lpfc_mbx_mq_create *mq_create;
  13592. struct lpfc_dmabuf *dmabuf;
  13593. int length;
  13594. length = (sizeof(struct lpfc_mbx_mq_create) -
  13595. sizeof(struct lpfc_sli4_cfg_mhdr));
  13596. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  13597. LPFC_MBOX_OPCODE_MQ_CREATE,
  13598. length, LPFC_SLI4_MBX_EMBED);
  13599. mq_create = &mbox->u.mqe.un.mq_create;
  13600. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  13601. mq->page_count);
  13602. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  13603. cq->queue_id);
  13604. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  13605. switch (mq->entry_count) {
  13606. case 16:
  13607. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  13608. LPFC_MQ_RING_SIZE_16);
  13609. break;
  13610. case 32:
  13611. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  13612. LPFC_MQ_RING_SIZE_32);
  13613. break;
  13614. case 64:
  13615. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  13616. LPFC_MQ_RING_SIZE_64);
  13617. break;
  13618. case 128:
  13619. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  13620. LPFC_MQ_RING_SIZE_128);
  13621. break;
  13622. }
  13623. list_for_each_entry(dmabuf, &mq->page_list, list) {
  13624. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  13625. putPaddrLow(dmabuf->phys);
  13626. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  13627. putPaddrHigh(dmabuf->phys);
  13628. }
  13629. }
  13630. /**
  13631. * lpfc_mq_create - Create a mailbox Queue on the HBA
  13632. * @phba: HBA structure that indicates port to create a queue on.
  13633. * @mq: The queue structure to use to create the mailbox queue.
  13634. * @cq: The completion queue to associate with this cq.
  13635. * @subtype: The queue's subtype.
  13636. *
  13637. * This function creates a mailbox queue, as detailed in @mq, on a port,
  13638. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  13639. *
  13640. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  13641. * is used to get the entry count and entry size that are necessary to
  13642. * determine the number of pages to allocate and use for this queue. This
  13643. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  13644. * mailbox queue. This function is asynchronous and will wait for the mailbox
  13645. * command to finish before continuing.
  13646. *
  13647. * On success this function will return a zero. If unable to allocate enough
  13648. * memory this function will return -ENOMEM. If the queue create mailbox command
  13649. * fails this function will return -ENXIO.
  13650. **/
  13651. int32_t
  13652. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  13653. struct lpfc_queue *cq, uint32_t subtype)
  13654. {
  13655. struct lpfc_mbx_mq_create *mq_create;
  13656. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  13657. struct lpfc_dmabuf *dmabuf;
  13658. LPFC_MBOXQ_t *mbox;
  13659. int rc, length, status = 0;
  13660. uint32_t shdr_status, shdr_add_status;
  13661. union lpfc_sli4_cfg_shdr *shdr;
  13662. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  13663. /* sanity check on queue memory */
  13664. if (!mq || !cq)
  13665. return -ENODEV;
  13666. if (!phba->sli4_hba.pc_sli4_params.supported)
  13667. hw_page_size = SLI4_PAGE_SIZE;
  13668. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13669. if (!mbox)
  13670. return -ENOMEM;
  13671. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  13672. sizeof(struct lpfc_sli4_cfg_mhdr));
  13673. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  13674. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  13675. length, LPFC_SLI4_MBX_EMBED);
  13676. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  13677. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  13678. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  13679. &mq_create_ext->u.request, mq->page_count);
  13680. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  13681. &mq_create_ext->u.request, 1);
  13682. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  13683. &mq_create_ext->u.request, 1);
  13684. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  13685. &mq_create_ext->u.request, 1);
  13686. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  13687. &mq_create_ext->u.request, 1);
  13688. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  13689. &mq_create_ext->u.request, 1);
  13690. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  13691. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  13692. phba->sli4_hba.pc_sli4_params.mqv);
  13693. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  13694. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  13695. cq->queue_id);
  13696. else
  13697. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  13698. cq->queue_id);
  13699. switch (mq->entry_count) {
  13700. default:
  13701. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13702. "0362 Unsupported MQ count. (%d)\n",
  13703. mq->entry_count);
  13704. if (mq->entry_count < 16) {
  13705. status = -EINVAL;
  13706. goto out;
  13707. }
  13708. /* otherwise default to smallest count (drop through) */
  13709. case 16:
  13710. bf_set(lpfc_mq_context_ring_size,
  13711. &mq_create_ext->u.request.context,
  13712. LPFC_MQ_RING_SIZE_16);
  13713. break;
  13714. case 32:
  13715. bf_set(lpfc_mq_context_ring_size,
  13716. &mq_create_ext->u.request.context,
  13717. LPFC_MQ_RING_SIZE_32);
  13718. break;
  13719. case 64:
  13720. bf_set(lpfc_mq_context_ring_size,
  13721. &mq_create_ext->u.request.context,
  13722. LPFC_MQ_RING_SIZE_64);
  13723. break;
  13724. case 128:
  13725. bf_set(lpfc_mq_context_ring_size,
  13726. &mq_create_ext->u.request.context,
  13727. LPFC_MQ_RING_SIZE_128);
  13728. break;
  13729. }
  13730. list_for_each_entry(dmabuf, &mq->page_list, list) {
  13731. memset(dmabuf->virt, 0, hw_page_size);
  13732. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  13733. putPaddrLow(dmabuf->phys);
  13734. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  13735. putPaddrHigh(dmabuf->phys);
  13736. }
  13737. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13738. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  13739. &mq_create_ext->u.response);
  13740. if (rc != MBX_SUCCESS) {
  13741. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  13742. "2795 MQ_CREATE_EXT failed with "
  13743. "status x%x. Failback to MQ_CREATE.\n",
  13744. rc);
  13745. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  13746. mq_create = &mbox->u.mqe.un.mq_create;
  13747. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13748. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  13749. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  13750. &mq_create->u.response);
  13751. }
  13752. /* The IOCTL status is embedded in the mailbox subheader. */
  13753. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13754. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13755. if (shdr_status || shdr_add_status || rc) {
  13756. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13757. "2502 MQ_CREATE mailbox failed with "
  13758. "status x%x add_status x%x, mbx status x%x\n",
  13759. shdr_status, shdr_add_status, rc);
  13760. status = -ENXIO;
  13761. goto out;
  13762. }
  13763. if (mq->queue_id == 0xFFFF) {
  13764. status = -ENXIO;
  13765. goto out;
  13766. }
  13767. mq->type = LPFC_MQ;
  13768. mq->assoc_qid = cq->queue_id;
  13769. mq->subtype = subtype;
  13770. mq->host_index = 0;
  13771. mq->hba_index = 0;
  13772. mq->entry_repost = LPFC_MQ_REPOST;
  13773. /* link the mq onto the parent cq child list */
  13774. list_add_tail(&mq->list, &cq->child_list);
  13775. out:
  13776. mempool_free(mbox, phba->mbox_mem_pool);
  13777. return status;
  13778. }
  13779. /**
  13780. * lpfc_wq_create - Create a Work Queue on the HBA
  13781. * @phba: HBA structure that indicates port to create a queue on.
  13782. * @wq: The queue structure to use to create the work queue.
  13783. * @cq: The completion queue to bind this work queue to.
  13784. * @subtype: The subtype of the work queue indicating its functionality.
  13785. *
  13786. * This function creates a work queue, as detailed in @wq, on a port, described
  13787. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  13788. *
  13789. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  13790. * is used to get the entry count and entry size that are necessary to
  13791. * determine the number of pages to allocate and use for this queue. The @cq
  13792. * is used to indicate which completion queue to bind this work queue to. This
  13793. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  13794. * work queue. This function is asynchronous and will wait for the mailbox
  13795. * command to finish before continuing.
  13796. *
  13797. * On success this function will return a zero. If unable to allocate enough
  13798. * memory this function will return -ENOMEM. If the queue create mailbox command
  13799. * fails this function will return -ENXIO.
  13800. **/
  13801. int
  13802. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  13803. struct lpfc_queue *cq, uint32_t subtype)
  13804. {
  13805. struct lpfc_mbx_wq_create *wq_create;
  13806. struct lpfc_dmabuf *dmabuf;
  13807. LPFC_MBOXQ_t *mbox;
  13808. int rc, length, status = 0;
  13809. uint32_t shdr_status, shdr_add_status;
  13810. union lpfc_sli4_cfg_shdr *shdr;
  13811. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  13812. struct dma_address *page;
  13813. void __iomem *bar_memmap_p;
  13814. uint32_t db_offset;
  13815. uint16_t pci_barset;
  13816. uint8_t dpp_barset;
  13817. uint32_t dpp_offset;
  13818. unsigned long pg_addr;
  13819. uint8_t wq_create_version;
  13820. /* sanity check on queue memory */
  13821. if (!wq || !cq)
  13822. return -ENODEV;
  13823. if (!phba->sli4_hba.pc_sli4_params.supported)
  13824. hw_page_size = wq->page_size;
  13825. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13826. if (!mbox)
  13827. return -ENOMEM;
  13828. length = (sizeof(struct lpfc_mbx_wq_create) -
  13829. sizeof(struct lpfc_sli4_cfg_mhdr));
  13830. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13831. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  13832. length, LPFC_SLI4_MBX_EMBED);
  13833. wq_create = &mbox->u.mqe.un.wq_create;
  13834. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  13835. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  13836. wq->page_count);
  13837. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  13838. cq->queue_id);
  13839. /* wqv is the earliest version supported, NOT the latest */
  13840. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  13841. phba->sli4_hba.pc_sli4_params.wqv);
  13842. if ((phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT) ||
  13843. (wq->page_size > SLI4_PAGE_SIZE))
  13844. wq_create_version = LPFC_Q_CREATE_VERSION_1;
  13845. else
  13846. wq_create_version = LPFC_Q_CREATE_VERSION_0;
  13847. if (phba->sli4_hba.pc_sli4_params.wqsize & LPFC_WQ_SZ128_SUPPORT)
  13848. wq_create_version = LPFC_Q_CREATE_VERSION_1;
  13849. else
  13850. wq_create_version = LPFC_Q_CREATE_VERSION_0;
  13851. switch (wq_create_version) {
  13852. case LPFC_Q_CREATE_VERSION_1:
  13853. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  13854. wq->entry_count);
  13855. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  13856. LPFC_Q_CREATE_VERSION_1);
  13857. switch (wq->entry_size) {
  13858. default:
  13859. case 64:
  13860. bf_set(lpfc_mbx_wq_create_wqe_size,
  13861. &wq_create->u.request_1,
  13862. LPFC_WQ_WQE_SIZE_64);
  13863. break;
  13864. case 128:
  13865. bf_set(lpfc_mbx_wq_create_wqe_size,
  13866. &wq_create->u.request_1,
  13867. LPFC_WQ_WQE_SIZE_128);
  13868. break;
  13869. }
  13870. /* Request DPP by default */
  13871. bf_set(lpfc_mbx_wq_create_dpp_req, &wq_create->u.request_1, 1);
  13872. bf_set(lpfc_mbx_wq_create_page_size,
  13873. &wq_create->u.request_1,
  13874. (wq->page_size / SLI4_PAGE_SIZE));
  13875. page = wq_create->u.request_1.page;
  13876. break;
  13877. default:
  13878. page = wq_create->u.request.page;
  13879. break;
  13880. }
  13881. list_for_each_entry(dmabuf, &wq->page_list, list) {
  13882. memset(dmabuf->virt, 0, hw_page_size);
  13883. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  13884. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  13885. }
  13886. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  13887. bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
  13888. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  13889. /* The IOCTL status is embedded in the mailbox subheader. */
  13890. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13891. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13892. if (shdr_status || shdr_add_status || rc) {
  13893. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13894. "2503 WQ_CREATE mailbox failed with "
  13895. "status x%x add_status x%x, mbx status x%x\n",
  13896. shdr_status, shdr_add_status, rc);
  13897. status = -ENXIO;
  13898. goto out;
  13899. }
  13900. if (wq_create_version == LPFC_Q_CREATE_VERSION_0)
  13901. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id,
  13902. &wq_create->u.response);
  13903. else
  13904. wq->queue_id = bf_get(lpfc_mbx_wq_create_v1_q_id,
  13905. &wq_create->u.response_1);
  13906. if (wq->queue_id == 0xFFFF) {
  13907. status = -ENXIO;
  13908. goto out;
  13909. }
  13910. wq->db_format = LPFC_DB_LIST_FORMAT;
  13911. if (wq_create_version == LPFC_Q_CREATE_VERSION_0) {
  13912. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  13913. wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
  13914. &wq_create->u.response);
  13915. if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
  13916. (wq->db_format != LPFC_DB_RING_FORMAT)) {
  13917. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13918. "3265 WQ[%d] doorbell format "
  13919. "not supported: x%x\n",
  13920. wq->queue_id, wq->db_format);
  13921. status = -EINVAL;
  13922. goto out;
  13923. }
  13924. pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
  13925. &wq_create->u.response);
  13926. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
  13927. pci_barset);
  13928. if (!bar_memmap_p) {
  13929. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13930. "3263 WQ[%d] failed to memmap "
  13931. "pci barset:x%x\n",
  13932. wq->queue_id, pci_barset);
  13933. status = -ENOMEM;
  13934. goto out;
  13935. }
  13936. db_offset = wq_create->u.response.doorbell_offset;
  13937. if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
  13938. (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
  13939. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13940. "3252 WQ[%d] doorbell offset "
  13941. "not supported: x%x\n",
  13942. wq->queue_id, db_offset);
  13943. status = -EINVAL;
  13944. goto out;
  13945. }
  13946. wq->db_regaddr = bar_memmap_p + db_offset;
  13947. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  13948. "3264 WQ[%d]: barset:x%x, offset:x%x, "
  13949. "format:x%x\n", wq->queue_id,
  13950. pci_barset, db_offset, wq->db_format);
  13951. } else
  13952. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  13953. } else {
  13954. /* Check if DPP was honored by the firmware */
  13955. wq->dpp_enable = bf_get(lpfc_mbx_wq_create_dpp_rsp,
  13956. &wq_create->u.response_1);
  13957. if (wq->dpp_enable) {
  13958. pci_barset = bf_get(lpfc_mbx_wq_create_v1_bar_set,
  13959. &wq_create->u.response_1);
  13960. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
  13961. pci_barset);
  13962. if (!bar_memmap_p) {
  13963. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13964. "3267 WQ[%d] failed to memmap "
  13965. "pci barset:x%x\n",
  13966. wq->queue_id, pci_barset);
  13967. status = -ENOMEM;
  13968. goto out;
  13969. }
  13970. db_offset = wq_create->u.response_1.doorbell_offset;
  13971. wq->db_regaddr = bar_memmap_p + db_offset;
  13972. wq->dpp_id = bf_get(lpfc_mbx_wq_create_dpp_id,
  13973. &wq_create->u.response_1);
  13974. dpp_barset = bf_get(lpfc_mbx_wq_create_dpp_bar,
  13975. &wq_create->u.response_1);
  13976. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba,
  13977. dpp_barset);
  13978. if (!bar_memmap_p) {
  13979. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13980. "3268 WQ[%d] failed to memmap "
  13981. "pci barset:x%x\n",
  13982. wq->queue_id, dpp_barset);
  13983. status = -ENOMEM;
  13984. goto out;
  13985. }
  13986. dpp_offset = wq_create->u.response_1.dpp_offset;
  13987. wq->dpp_regaddr = bar_memmap_p + dpp_offset;
  13988. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  13989. "3271 WQ[%d]: barset:x%x, offset:x%x, "
  13990. "dpp_id:x%x dpp_barset:x%x "
  13991. "dpp_offset:x%x\n",
  13992. wq->queue_id, pci_barset, db_offset,
  13993. wq->dpp_id, dpp_barset, dpp_offset);
  13994. /* Enable combined writes for DPP aperture */
  13995. pg_addr = (unsigned long)(wq->dpp_regaddr) & PAGE_MASK;
  13996. #ifdef CONFIG_X86
  13997. rc = set_memory_wc(pg_addr, 1);
  13998. if (rc) {
  13999. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14000. "3272 Cannot setup Combined "
  14001. "Write on WQ[%d] - disable DPP\n",
  14002. wq->queue_id);
  14003. phba->cfg_enable_dpp = 0;
  14004. }
  14005. #else
  14006. phba->cfg_enable_dpp = 0;
  14007. #endif
  14008. } else
  14009. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  14010. }
  14011. wq->pring = kzalloc(sizeof(struct lpfc_sli_ring), GFP_KERNEL);
  14012. if (wq->pring == NULL) {
  14013. status = -ENOMEM;
  14014. goto out;
  14015. }
  14016. wq->type = LPFC_WQ;
  14017. wq->assoc_qid = cq->queue_id;
  14018. wq->subtype = subtype;
  14019. wq->host_index = 0;
  14020. wq->hba_index = 0;
  14021. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  14022. /* link the wq onto the parent cq child list */
  14023. list_add_tail(&wq->list, &cq->child_list);
  14024. out:
  14025. mempool_free(mbox, phba->mbox_mem_pool);
  14026. return status;
  14027. }
  14028. /**
  14029. * lpfc_rq_create - Create a Receive Queue on the HBA
  14030. * @phba: HBA structure that indicates port to create a queue on.
  14031. * @hrq: The queue structure to use to create the header receive queue.
  14032. * @drq: The queue structure to use to create the data receive queue.
  14033. * @cq: The completion queue to bind this work queue to.
  14034. *
  14035. * This function creates a receive buffer queue pair , as detailed in @hrq and
  14036. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  14037. * to the HBA.
  14038. *
  14039. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  14040. * struct is used to get the entry count that is necessary to determine the
  14041. * number of pages to use for this queue. The @cq is used to indicate which
  14042. * completion queue to bind received buffers that are posted to these queues to.
  14043. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  14044. * receive queue pair. This function is asynchronous and will wait for the
  14045. * mailbox command to finish before continuing.
  14046. *
  14047. * On success this function will return a zero. If unable to allocate enough
  14048. * memory this function will return -ENOMEM. If the queue create mailbox command
  14049. * fails this function will return -ENXIO.
  14050. **/
  14051. int
  14052. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  14053. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  14054. {
  14055. struct lpfc_mbx_rq_create *rq_create;
  14056. struct lpfc_dmabuf *dmabuf;
  14057. LPFC_MBOXQ_t *mbox;
  14058. int rc, length, status = 0;
  14059. uint32_t shdr_status, shdr_add_status;
  14060. union lpfc_sli4_cfg_shdr *shdr;
  14061. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  14062. void __iomem *bar_memmap_p;
  14063. uint32_t db_offset;
  14064. uint16_t pci_barset;
  14065. /* sanity check on queue memory */
  14066. if (!hrq || !drq || !cq)
  14067. return -ENODEV;
  14068. if (!phba->sli4_hba.pc_sli4_params.supported)
  14069. hw_page_size = SLI4_PAGE_SIZE;
  14070. if (hrq->entry_count != drq->entry_count)
  14071. return -EINVAL;
  14072. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14073. if (!mbox)
  14074. return -ENOMEM;
  14075. length = (sizeof(struct lpfc_mbx_rq_create) -
  14076. sizeof(struct lpfc_sli4_cfg_mhdr));
  14077. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14078. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  14079. length, LPFC_SLI4_MBX_EMBED);
  14080. rq_create = &mbox->u.mqe.un.rq_create;
  14081. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  14082. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  14083. phba->sli4_hba.pc_sli4_params.rqv);
  14084. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  14085. bf_set(lpfc_rq_context_rqe_count_1,
  14086. &rq_create->u.request.context,
  14087. hrq->entry_count);
  14088. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  14089. bf_set(lpfc_rq_context_rqe_size,
  14090. &rq_create->u.request.context,
  14091. LPFC_RQE_SIZE_8);
  14092. bf_set(lpfc_rq_context_page_size,
  14093. &rq_create->u.request.context,
  14094. LPFC_RQ_PAGE_SIZE_4096);
  14095. } else {
  14096. switch (hrq->entry_count) {
  14097. default:
  14098. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14099. "2535 Unsupported RQ count. (%d)\n",
  14100. hrq->entry_count);
  14101. if (hrq->entry_count < 512) {
  14102. status = -EINVAL;
  14103. goto out;
  14104. }
  14105. /* otherwise default to smallest count (drop through) */
  14106. case 512:
  14107. bf_set(lpfc_rq_context_rqe_count,
  14108. &rq_create->u.request.context,
  14109. LPFC_RQ_RING_SIZE_512);
  14110. break;
  14111. case 1024:
  14112. bf_set(lpfc_rq_context_rqe_count,
  14113. &rq_create->u.request.context,
  14114. LPFC_RQ_RING_SIZE_1024);
  14115. break;
  14116. case 2048:
  14117. bf_set(lpfc_rq_context_rqe_count,
  14118. &rq_create->u.request.context,
  14119. LPFC_RQ_RING_SIZE_2048);
  14120. break;
  14121. case 4096:
  14122. bf_set(lpfc_rq_context_rqe_count,
  14123. &rq_create->u.request.context,
  14124. LPFC_RQ_RING_SIZE_4096);
  14125. break;
  14126. }
  14127. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  14128. LPFC_HDR_BUF_SIZE);
  14129. }
  14130. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  14131. cq->queue_id);
  14132. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  14133. hrq->page_count);
  14134. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  14135. memset(dmabuf->virt, 0, hw_page_size);
  14136. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  14137. putPaddrLow(dmabuf->phys);
  14138. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  14139. putPaddrHigh(dmabuf->phys);
  14140. }
  14141. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  14142. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  14143. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14144. /* The IOCTL status is embedded in the mailbox subheader. */
  14145. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14146. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14147. if (shdr_status || shdr_add_status || rc) {
  14148. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14149. "2504 RQ_CREATE mailbox failed with "
  14150. "status x%x add_status x%x, mbx status x%x\n",
  14151. shdr_status, shdr_add_status, rc);
  14152. status = -ENXIO;
  14153. goto out;
  14154. }
  14155. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  14156. if (hrq->queue_id == 0xFFFF) {
  14157. status = -ENXIO;
  14158. goto out;
  14159. }
  14160. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  14161. hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
  14162. &rq_create->u.response);
  14163. if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
  14164. (hrq->db_format != LPFC_DB_RING_FORMAT)) {
  14165. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14166. "3262 RQ [%d] doorbell format not "
  14167. "supported: x%x\n", hrq->queue_id,
  14168. hrq->db_format);
  14169. status = -EINVAL;
  14170. goto out;
  14171. }
  14172. pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
  14173. &rq_create->u.response);
  14174. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  14175. if (!bar_memmap_p) {
  14176. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14177. "3269 RQ[%d] failed to memmap pci "
  14178. "barset:x%x\n", hrq->queue_id,
  14179. pci_barset);
  14180. status = -ENOMEM;
  14181. goto out;
  14182. }
  14183. db_offset = rq_create->u.response.doorbell_offset;
  14184. if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
  14185. (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
  14186. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14187. "3270 RQ[%d] doorbell offset not "
  14188. "supported: x%x\n", hrq->queue_id,
  14189. db_offset);
  14190. status = -EINVAL;
  14191. goto out;
  14192. }
  14193. hrq->db_regaddr = bar_memmap_p + db_offset;
  14194. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14195. "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
  14196. "format:x%x\n", hrq->queue_id, pci_barset,
  14197. db_offset, hrq->db_format);
  14198. } else {
  14199. hrq->db_format = LPFC_DB_RING_FORMAT;
  14200. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  14201. }
  14202. hrq->type = LPFC_HRQ;
  14203. hrq->assoc_qid = cq->queue_id;
  14204. hrq->subtype = subtype;
  14205. hrq->host_index = 0;
  14206. hrq->hba_index = 0;
  14207. hrq->entry_repost = LPFC_RQ_REPOST;
  14208. /* now create the data queue */
  14209. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14210. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  14211. length, LPFC_SLI4_MBX_EMBED);
  14212. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  14213. phba->sli4_hba.pc_sli4_params.rqv);
  14214. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  14215. bf_set(lpfc_rq_context_rqe_count_1,
  14216. &rq_create->u.request.context, hrq->entry_count);
  14217. if (subtype == LPFC_NVMET)
  14218. rq_create->u.request.context.buffer_size =
  14219. LPFC_NVMET_DATA_BUF_SIZE;
  14220. else
  14221. rq_create->u.request.context.buffer_size =
  14222. LPFC_DATA_BUF_SIZE;
  14223. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  14224. LPFC_RQE_SIZE_8);
  14225. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  14226. (PAGE_SIZE/SLI4_PAGE_SIZE));
  14227. } else {
  14228. switch (drq->entry_count) {
  14229. default:
  14230. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14231. "2536 Unsupported RQ count. (%d)\n",
  14232. drq->entry_count);
  14233. if (drq->entry_count < 512) {
  14234. status = -EINVAL;
  14235. goto out;
  14236. }
  14237. /* otherwise default to smallest count (drop through) */
  14238. case 512:
  14239. bf_set(lpfc_rq_context_rqe_count,
  14240. &rq_create->u.request.context,
  14241. LPFC_RQ_RING_SIZE_512);
  14242. break;
  14243. case 1024:
  14244. bf_set(lpfc_rq_context_rqe_count,
  14245. &rq_create->u.request.context,
  14246. LPFC_RQ_RING_SIZE_1024);
  14247. break;
  14248. case 2048:
  14249. bf_set(lpfc_rq_context_rqe_count,
  14250. &rq_create->u.request.context,
  14251. LPFC_RQ_RING_SIZE_2048);
  14252. break;
  14253. case 4096:
  14254. bf_set(lpfc_rq_context_rqe_count,
  14255. &rq_create->u.request.context,
  14256. LPFC_RQ_RING_SIZE_4096);
  14257. break;
  14258. }
  14259. if (subtype == LPFC_NVMET)
  14260. bf_set(lpfc_rq_context_buf_size,
  14261. &rq_create->u.request.context,
  14262. LPFC_NVMET_DATA_BUF_SIZE);
  14263. else
  14264. bf_set(lpfc_rq_context_buf_size,
  14265. &rq_create->u.request.context,
  14266. LPFC_DATA_BUF_SIZE);
  14267. }
  14268. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  14269. cq->queue_id);
  14270. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  14271. drq->page_count);
  14272. list_for_each_entry(dmabuf, &drq->page_list, list) {
  14273. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  14274. putPaddrLow(dmabuf->phys);
  14275. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  14276. putPaddrHigh(dmabuf->phys);
  14277. }
  14278. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  14279. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  14280. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14281. /* The IOCTL status is embedded in the mailbox subheader. */
  14282. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  14283. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14284. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14285. if (shdr_status || shdr_add_status || rc) {
  14286. status = -ENXIO;
  14287. goto out;
  14288. }
  14289. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  14290. if (drq->queue_id == 0xFFFF) {
  14291. status = -ENXIO;
  14292. goto out;
  14293. }
  14294. drq->type = LPFC_DRQ;
  14295. drq->assoc_qid = cq->queue_id;
  14296. drq->subtype = subtype;
  14297. drq->host_index = 0;
  14298. drq->hba_index = 0;
  14299. drq->entry_repost = LPFC_RQ_REPOST;
  14300. /* link the header and data RQs onto the parent cq child list */
  14301. list_add_tail(&hrq->list, &cq->child_list);
  14302. list_add_tail(&drq->list, &cq->child_list);
  14303. out:
  14304. mempool_free(mbox, phba->mbox_mem_pool);
  14305. return status;
  14306. }
  14307. /**
  14308. * lpfc_mrq_create - Create MRQ Receive Queues on the HBA
  14309. * @phba: HBA structure that indicates port to create a queue on.
  14310. * @hrqp: The queue structure array to use to create the header receive queues.
  14311. * @drqp: The queue structure array to use to create the data receive queues.
  14312. * @cqp: The completion queue array to bind these receive queues to.
  14313. *
  14314. * This function creates a receive buffer queue pair , as detailed in @hrq and
  14315. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  14316. * to the HBA.
  14317. *
  14318. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  14319. * struct is used to get the entry count that is necessary to determine the
  14320. * number of pages to use for this queue. The @cq is used to indicate which
  14321. * completion queue to bind received buffers that are posted to these queues to.
  14322. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  14323. * receive queue pair. This function is asynchronous and will wait for the
  14324. * mailbox command to finish before continuing.
  14325. *
  14326. * On success this function will return a zero. If unable to allocate enough
  14327. * memory this function will return -ENOMEM. If the queue create mailbox command
  14328. * fails this function will return -ENXIO.
  14329. **/
  14330. int
  14331. lpfc_mrq_create(struct lpfc_hba *phba, struct lpfc_queue **hrqp,
  14332. struct lpfc_queue **drqp, struct lpfc_queue **cqp,
  14333. uint32_t subtype)
  14334. {
  14335. struct lpfc_queue *hrq, *drq, *cq;
  14336. struct lpfc_mbx_rq_create_v2 *rq_create;
  14337. struct lpfc_dmabuf *dmabuf;
  14338. LPFC_MBOXQ_t *mbox;
  14339. int rc, length, alloclen, status = 0;
  14340. int cnt, idx, numrq, page_idx = 0;
  14341. uint32_t shdr_status, shdr_add_status;
  14342. union lpfc_sli4_cfg_shdr *shdr;
  14343. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  14344. numrq = phba->cfg_nvmet_mrq;
  14345. /* sanity check on array memory */
  14346. if (!hrqp || !drqp || !cqp || !numrq)
  14347. return -ENODEV;
  14348. if (!phba->sli4_hba.pc_sli4_params.supported)
  14349. hw_page_size = SLI4_PAGE_SIZE;
  14350. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14351. if (!mbox)
  14352. return -ENOMEM;
  14353. length = sizeof(struct lpfc_mbx_rq_create_v2);
  14354. length += ((2 * numrq * hrqp[0]->page_count) *
  14355. sizeof(struct dma_address));
  14356. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14357. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE, length,
  14358. LPFC_SLI4_MBX_NEMBED);
  14359. if (alloclen < length) {
  14360. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14361. "3099 Allocated DMA memory size (%d) is "
  14362. "less than the requested DMA memory size "
  14363. "(%d)\n", alloclen, length);
  14364. status = -ENOMEM;
  14365. goto out;
  14366. }
  14367. rq_create = mbox->sge_array->addr[0];
  14368. shdr = (union lpfc_sli4_cfg_shdr *)&rq_create->cfg_shdr;
  14369. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_Q_CREATE_VERSION_2);
  14370. cnt = 0;
  14371. for (idx = 0; idx < numrq; idx++) {
  14372. hrq = hrqp[idx];
  14373. drq = drqp[idx];
  14374. cq = cqp[idx];
  14375. /* sanity check on queue memory */
  14376. if (!hrq || !drq || !cq) {
  14377. status = -ENODEV;
  14378. goto out;
  14379. }
  14380. if (hrq->entry_count != drq->entry_count) {
  14381. status = -EINVAL;
  14382. goto out;
  14383. }
  14384. if (idx == 0) {
  14385. bf_set(lpfc_mbx_rq_create_num_pages,
  14386. &rq_create->u.request,
  14387. hrq->page_count);
  14388. bf_set(lpfc_mbx_rq_create_rq_cnt,
  14389. &rq_create->u.request, (numrq * 2));
  14390. bf_set(lpfc_mbx_rq_create_dnb, &rq_create->u.request,
  14391. 1);
  14392. bf_set(lpfc_rq_context_base_cq,
  14393. &rq_create->u.request.context,
  14394. cq->queue_id);
  14395. bf_set(lpfc_rq_context_data_size,
  14396. &rq_create->u.request.context,
  14397. LPFC_NVMET_DATA_BUF_SIZE);
  14398. bf_set(lpfc_rq_context_hdr_size,
  14399. &rq_create->u.request.context,
  14400. LPFC_HDR_BUF_SIZE);
  14401. bf_set(lpfc_rq_context_rqe_count_1,
  14402. &rq_create->u.request.context,
  14403. hrq->entry_count);
  14404. bf_set(lpfc_rq_context_rqe_size,
  14405. &rq_create->u.request.context,
  14406. LPFC_RQE_SIZE_8);
  14407. bf_set(lpfc_rq_context_page_size,
  14408. &rq_create->u.request.context,
  14409. (PAGE_SIZE/SLI4_PAGE_SIZE));
  14410. }
  14411. rc = 0;
  14412. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  14413. memset(dmabuf->virt, 0, hw_page_size);
  14414. cnt = page_idx + dmabuf->buffer_tag;
  14415. rq_create->u.request.page[cnt].addr_lo =
  14416. putPaddrLow(dmabuf->phys);
  14417. rq_create->u.request.page[cnt].addr_hi =
  14418. putPaddrHigh(dmabuf->phys);
  14419. rc++;
  14420. }
  14421. page_idx += rc;
  14422. rc = 0;
  14423. list_for_each_entry(dmabuf, &drq->page_list, list) {
  14424. memset(dmabuf->virt, 0, hw_page_size);
  14425. cnt = page_idx + dmabuf->buffer_tag;
  14426. rq_create->u.request.page[cnt].addr_lo =
  14427. putPaddrLow(dmabuf->phys);
  14428. rq_create->u.request.page[cnt].addr_hi =
  14429. putPaddrHigh(dmabuf->phys);
  14430. rc++;
  14431. }
  14432. page_idx += rc;
  14433. hrq->db_format = LPFC_DB_RING_FORMAT;
  14434. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  14435. hrq->type = LPFC_HRQ;
  14436. hrq->assoc_qid = cq->queue_id;
  14437. hrq->subtype = subtype;
  14438. hrq->host_index = 0;
  14439. hrq->hba_index = 0;
  14440. hrq->entry_repost = LPFC_RQ_REPOST;
  14441. drq->db_format = LPFC_DB_RING_FORMAT;
  14442. drq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  14443. drq->type = LPFC_DRQ;
  14444. drq->assoc_qid = cq->queue_id;
  14445. drq->subtype = subtype;
  14446. drq->host_index = 0;
  14447. drq->hba_index = 0;
  14448. drq->entry_repost = LPFC_RQ_REPOST;
  14449. list_add_tail(&hrq->list, &cq->child_list);
  14450. list_add_tail(&drq->list, &cq->child_list);
  14451. }
  14452. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14453. /* The IOCTL status is embedded in the mailbox subheader. */
  14454. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14455. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14456. if (shdr_status || shdr_add_status || rc) {
  14457. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14458. "3120 RQ_CREATE mailbox failed with "
  14459. "status x%x add_status x%x, mbx status x%x\n",
  14460. shdr_status, shdr_add_status, rc);
  14461. status = -ENXIO;
  14462. goto out;
  14463. }
  14464. rc = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  14465. if (rc == 0xFFFF) {
  14466. status = -ENXIO;
  14467. goto out;
  14468. }
  14469. /* Initialize all RQs with associated queue id */
  14470. for (idx = 0; idx < numrq; idx++) {
  14471. hrq = hrqp[idx];
  14472. hrq->queue_id = rc + (2 * idx);
  14473. drq = drqp[idx];
  14474. drq->queue_id = rc + (2 * idx) + 1;
  14475. }
  14476. out:
  14477. lpfc_sli4_mbox_cmd_free(phba, mbox);
  14478. return status;
  14479. }
  14480. /**
  14481. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  14482. * @eq: The queue structure associated with the queue to destroy.
  14483. *
  14484. * This function destroys a queue, as detailed in @eq by sending an mailbox
  14485. * command, specific to the type of queue, to the HBA.
  14486. *
  14487. * The @eq struct is used to get the queue ID of the queue to destroy.
  14488. *
  14489. * On success this function will return a zero. If the queue destroy mailbox
  14490. * command fails this function will return -ENXIO.
  14491. **/
  14492. int
  14493. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  14494. {
  14495. LPFC_MBOXQ_t *mbox;
  14496. int rc, length, status = 0;
  14497. uint32_t shdr_status, shdr_add_status;
  14498. union lpfc_sli4_cfg_shdr *shdr;
  14499. /* sanity check on queue memory */
  14500. if (!eq)
  14501. return -ENODEV;
  14502. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  14503. if (!mbox)
  14504. return -ENOMEM;
  14505. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  14506. sizeof(struct lpfc_sli4_cfg_mhdr));
  14507. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14508. LPFC_MBOX_OPCODE_EQ_DESTROY,
  14509. length, LPFC_SLI4_MBX_EMBED);
  14510. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  14511. eq->queue_id);
  14512. mbox->vport = eq->phba->pport;
  14513. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14514. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  14515. /* The IOCTL status is embedded in the mailbox subheader. */
  14516. shdr = (union lpfc_sli4_cfg_shdr *)
  14517. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  14518. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14519. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14520. if (shdr_status || shdr_add_status || rc) {
  14521. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14522. "2505 EQ_DESTROY mailbox failed with "
  14523. "status x%x add_status x%x, mbx status x%x\n",
  14524. shdr_status, shdr_add_status, rc);
  14525. status = -ENXIO;
  14526. }
  14527. /* Remove eq from any list */
  14528. list_del_init(&eq->list);
  14529. mempool_free(mbox, eq->phba->mbox_mem_pool);
  14530. return status;
  14531. }
  14532. /**
  14533. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  14534. * @cq: The queue structure associated with the queue to destroy.
  14535. *
  14536. * This function destroys a queue, as detailed in @cq by sending an mailbox
  14537. * command, specific to the type of queue, to the HBA.
  14538. *
  14539. * The @cq struct is used to get the queue ID of the queue to destroy.
  14540. *
  14541. * On success this function will return a zero. If the queue destroy mailbox
  14542. * command fails this function will return -ENXIO.
  14543. **/
  14544. int
  14545. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  14546. {
  14547. LPFC_MBOXQ_t *mbox;
  14548. int rc, length, status = 0;
  14549. uint32_t shdr_status, shdr_add_status;
  14550. union lpfc_sli4_cfg_shdr *shdr;
  14551. /* sanity check on queue memory */
  14552. if (!cq)
  14553. return -ENODEV;
  14554. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  14555. if (!mbox)
  14556. return -ENOMEM;
  14557. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  14558. sizeof(struct lpfc_sli4_cfg_mhdr));
  14559. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14560. LPFC_MBOX_OPCODE_CQ_DESTROY,
  14561. length, LPFC_SLI4_MBX_EMBED);
  14562. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  14563. cq->queue_id);
  14564. mbox->vport = cq->phba->pport;
  14565. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14566. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  14567. /* The IOCTL status is embedded in the mailbox subheader. */
  14568. shdr = (union lpfc_sli4_cfg_shdr *)
  14569. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  14570. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14571. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14572. if (shdr_status || shdr_add_status || rc) {
  14573. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14574. "2506 CQ_DESTROY mailbox failed with "
  14575. "status x%x add_status x%x, mbx status x%x\n",
  14576. shdr_status, shdr_add_status, rc);
  14577. status = -ENXIO;
  14578. }
  14579. /* Remove cq from any list */
  14580. list_del_init(&cq->list);
  14581. mempool_free(mbox, cq->phba->mbox_mem_pool);
  14582. return status;
  14583. }
  14584. /**
  14585. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  14586. * @qm: The queue structure associated with the queue to destroy.
  14587. *
  14588. * This function destroys a queue, as detailed in @mq by sending an mailbox
  14589. * command, specific to the type of queue, to the HBA.
  14590. *
  14591. * The @mq struct is used to get the queue ID of the queue to destroy.
  14592. *
  14593. * On success this function will return a zero. If the queue destroy mailbox
  14594. * command fails this function will return -ENXIO.
  14595. **/
  14596. int
  14597. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  14598. {
  14599. LPFC_MBOXQ_t *mbox;
  14600. int rc, length, status = 0;
  14601. uint32_t shdr_status, shdr_add_status;
  14602. union lpfc_sli4_cfg_shdr *shdr;
  14603. /* sanity check on queue memory */
  14604. if (!mq)
  14605. return -ENODEV;
  14606. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  14607. if (!mbox)
  14608. return -ENOMEM;
  14609. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  14610. sizeof(struct lpfc_sli4_cfg_mhdr));
  14611. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14612. LPFC_MBOX_OPCODE_MQ_DESTROY,
  14613. length, LPFC_SLI4_MBX_EMBED);
  14614. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  14615. mq->queue_id);
  14616. mbox->vport = mq->phba->pport;
  14617. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14618. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  14619. /* The IOCTL status is embedded in the mailbox subheader. */
  14620. shdr = (union lpfc_sli4_cfg_shdr *)
  14621. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  14622. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14623. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14624. if (shdr_status || shdr_add_status || rc) {
  14625. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14626. "2507 MQ_DESTROY mailbox failed with "
  14627. "status x%x add_status x%x, mbx status x%x\n",
  14628. shdr_status, shdr_add_status, rc);
  14629. status = -ENXIO;
  14630. }
  14631. /* Remove mq from any list */
  14632. list_del_init(&mq->list);
  14633. mempool_free(mbox, mq->phba->mbox_mem_pool);
  14634. return status;
  14635. }
  14636. /**
  14637. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  14638. * @wq: The queue structure associated with the queue to destroy.
  14639. *
  14640. * This function destroys a queue, as detailed in @wq by sending an mailbox
  14641. * command, specific to the type of queue, to the HBA.
  14642. *
  14643. * The @wq struct is used to get the queue ID of the queue to destroy.
  14644. *
  14645. * On success this function will return a zero. If the queue destroy mailbox
  14646. * command fails this function will return -ENXIO.
  14647. **/
  14648. int
  14649. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  14650. {
  14651. LPFC_MBOXQ_t *mbox;
  14652. int rc, length, status = 0;
  14653. uint32_t shdr_status, shdr_add_status;
  14654. union lpfc_sli4_cfg_shdr *shdr;
  14655. /* sanity check on queue memory */
  14656. if (!wq)
  14657. return -ENODEV;
  14658. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  14659. if (!mbox)
  14660. return -ENOMEM;
  14661. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  14662. sizeof(struct lpfc_sli4_cfg_mhdr));
  14663. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14664. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  14665. length, LPFC_SLI4_MBX_EMBED);
  14666. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  14667. wq->queue_id);
  14668. mbox->vport = wq->phba->pport;
  14669. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14670. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  14671. shdr = (union lpfc_sli4_cfg_shdr *)
  14672. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  14673. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14674. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14675. if (shdr_status || shdr_add_status || rc) {
  14676. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14677. "2508 WQ_DESTROY mailbox failed with "
  14678. "status x%x add_status x%x, mbx status x%x\n",
  14679. shdr_status, shdr_add_status, rc);
  14680. status = -ENXIO;
  14681. }
  14682. /* Remove wq from any list */
  14683. list_del_init(&wq->list);
  14684. kfree(wq->pring);
  14685. wq->pring = NULL;
  14686. mempool_free(mbox, wq->phba->mbox_mem_pool);
  14687. return status;
  14688. }
  14689. /**
  14690. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  14691. * @rq: The queue structure associated with the queue to destroy.
  14692. *
  14693. * This function destroys a queue, as detailed in @rq by sending an mailbox
  14694. * command, specific to the type of queue, to the HBA.
  14695. *
  14696. * The @rq struct is used to get the queue ID of the queue to destroy.
  14697. *
  14698. * On success this function will return a zero. If the queue destroy mailbox
  14699. * command fails this function will return -ENXIO.
  14700. **/
  14701. int
  14702. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  14703. struct lpfc_queue *drq)
  14704. {
  14705. LPFC_MBOXQ_t *mbox;
  14706. int rc, length, status = 0;
  14707. uint32_t shdr_status, shdr_add_status;
  14708. union lpfc_sli4_cfg_shdr *shdr;
  14709. /* sanity check on queue memory */
  14710. if (!hrq || !drq)
  14711. return -ENODEV;
  14712. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  14713. if (!mbox)
  14714. return -ENOMEM;
  14715. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  14716. sizeof(struct lpfc_sli4_cfg_mhdr));
  14717. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14718. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  14719. length, LPFC_SLI4_MBX_EMBED);
  14720. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  14721. hrq->queue_id);
  14722. mbox->vport = hrq->phba->pport;
  14723. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14724. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  14725. /* The IOCTL status is embedded in the mailbox subheader. */
  14726. shdr = (union lpfc_sli4_cfg_shdr *)
  14727. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  14728. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14729. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14730. if (shdr_status || shdr_add_status || rc) {
  14731. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14732. "2509 RQ_DESTROY mailbox failed with "
  14733. "status x%x add_status x%x, mbx status x%x\n",
  14734. shdr_status, shdr_add_status, rc);
  14735. if (rc != MBX_TIMEOUT)
  14736. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  14737. return -ENXIO;
  14738. }
  14739. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  14740. drq->queue_id);
  14741. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  14742. shdr = (union lpfc_sli4_cfg_shdr *)
  14743. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  14744. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14745. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14746. if (shdr_status || shdr_add_status || rc) {
  14747. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14748. "2510 RQ_DESTROY mailbox failed with "
  14749. "status x%x add_status x%x, mbx status x%x\n",
  14750. shdr_status, shdr_add_status, rc);
  14751. status = -ENXIO;
  14752. }
  14753. list_del_init(&hrq->list);
  14754. list_del_init(&drq->list);
  14755. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  14756. return status;
  14757. }
  14758. /**
  14759. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  14760. * @phba: The virtual port for which this call being executed.
  14761. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  14762. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  14763. * @xritag: the xritag that ties this io to the SGL pages.
  14764. *
  14765. * This routine will post the sgl pages for the IO that has the xritag
  14766. * that is in the iocbq structure. The xritag is assigned during iocbq
  14767. * creation and persists for as long as the driver is loaded.
  14768. * if the caller has fewer than 256 scatter gather segments to map then
  14769. * pdma_phys_addr1 should be 0.
  14770. * If the caller needs to map more than 256 scatter gather segment then
  14771. * pdma_phys_addr1 should be a valid physical address.
  14772. * physical address for SGLs must be 64 byte aligned.
  14773. * If you are going to map 2 SGL's then the first one must have 256 entries
  14774. * the second sgl can have between 1 and 256 entries.
  14775. *
  14776. * Return codes:
  14777. * 0 - Success
  14778. * -ENXIO, -ENOMEM - Failure
  14779. **/
  14780. int
  14781. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  14782. dma_addr_t pdma_phys_addr0,
  14783. dma_addr_t pdma_phys_addr1,
  14784. uint16_t xritag)
  14785. {
  14786. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  14787. LPFC_MBOXQ_t *mbox;
  14788. int rc;
  14789. uint32_t shdr_status, shdr_add_status;
  14790. uint32_t mbox_tmo;
  14791. union lpfc_sli4_cfg_shdr *shdr;
  14792. if (xritag == NO_XRI) {
  14793. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14794. "0364 Invalid param:\n");
  14795. return -EINVAL;
  14796. }
  14797. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14798. if (!mbox)
  14799. return -ENOMEM;
  14800. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14801. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  14802. sizeof(struct lpfc_mbx_post_sgl_pages) -
  14803. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14804. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  14805. &mbox->u.mqe.un.post_sgl_pages;
  14806. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  14807. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  14808. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  14809. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  14810. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  14811. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  14812. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  14813. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  14814. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  14815. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  14816. if (!phba->sli4_hba.intr_enable)
  14817. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14818. else {
  14819. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14820. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14821. }
  14822. /* The IOCTL status is embedded in the mailbox subheader. */
  14823. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  14824. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14825. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14826. if (rc != MBX_TIMEOUT)
  14827. mempool_free(mbox, phba->mbox_mem_pool);
  14828. if (shdr_status || shdr_add_status || rc) {
  14829. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14830. "2511 POST_SGL mailbox failed with "
  14831. "status x%x add_status x%x, mbx status x%x\n",
  14832. shdr_status, shdr_add_status, rc);
  14833. }
  14834. return 0;
  14835. }
  14836. /**
  14837. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  14838. * @phba: pointer to lpfc hba data structure.
  14839. *
  14840. * This routine is invoked to post rpi header templates to the
  14841. * HBA consistent with the SLI-4 interface spec. This routine
  14842. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  14843. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  14844. *
  14845. * Returns
  14846. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  14847. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  14848. **/
  14849. static uint16_t
  14850. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  14851. {
  14852. unsigned long xri;
  14853. /*
  14854. * Fetch the next logical xri. Because this index is logical,
  14855. * the driver starts at 0 each time.
  14856. */
  14857. spin_lock_irq(&phba->hbalock);
  14858. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  14859. phba->sli4_hba.max_cfg_param.max_xri, 0);
  14860. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  14861. spin_unlock_irq(&phba->hbalock);
  14862. return NO_XRI;
  14863. } else {
  14864. set_bit(xri, phba->sli4_hba.xri_bmask);
  14865. phba->sli4_hba.max_cfg_param.xri_used++;
  14866. }
  14867. spin_unlock_irq(&phba->hbalock);
  14868. return xri;
  14869. }
  14870. /**
  14871. * lpfc_sli4_free_xri - Release an xri for reuse.
  14872. * @phba: pointer to lpfc hba data structure.
  14873. *
  14874. * This routine is invoked to release an xri to the pool of
  14875. * available rpis maintained by the driver.
  14876. **/
  14877. static void
  14878. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  14879. {
  14880. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  14881. phba->sli4_hba.max_cfg_param.xri_used--;
  14882. }
  14883. }
  14884. /**
  14885. * lpfc_sli4_free_xri - Release an xri for reuse.
  14886. * @phba: pointer to lpfc hba data structure.
  14887. *
  14888. * This routine is invoked to release an xri to the pool of
  14889. * available rpis maintained by the driver.
  14890. **/
  14891. void
  14892. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  14893. {
  14894. spin_lock_irq(&phba->hbalock);
  14895. __lpfc_sli4_free_xri(phba, xri);
  14896. spin_unlock_irq(&phba->hbalock);
  14897. }
  14898. /**
  14899. * lpfc_sli4_next_xritag - Get an xritag for the io
  14900. * @phba: Pointer to HBA context object.
  14901. *
  14902. * This function gets an xritag for the iocb. If there is no unused xritag
  14903. * it will return 0xffff.
  14904. * The function returns the allocated xritag if successful, else returns zero.
  14905. * Zero is not a valid xritag.
  14906. * The caller is not required to hold any lock.
  14907. **/
  14908. uint16_t
  14909. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  14910. {
  14911. uint16_t xri_index;
  14912. xri_index = lpfc_sli4_alloc_xri(phba);
  14913. if (xri_index == NO_XRI)
  14914. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  14915. "2004 Failed to allocate XRI.last XRITAG is %d"
  14916. " Max XRI is %d, Used XRI is %d\n",
  14917. xri_index,
  14918. phba->sli4_hba.max_cfg_param.max_xri,
  14919. phba->sli4_hba.max_cfg_param.xri_used);
  14920. return xri_index;
  14921. }
  14922. /**
  14923. * lpfc_sli4_post_sgl_list - post a block of ELS sgls to the port.
  14924. * @phba: pointer to lpfc hba data structure.
  14925. * @post_sgl_list: pointer to els sgl entry list.
  14926. * @count: number of els sgl entries on the list.
  14927. *
  14928. * This routine is invoked to post a block of driver's sgl pages to the
  14929. * HBA using non-embedded mailbox command. No Lock is held. This routine
  14930. * is only called when the driver is loading and after all IO has been
  14931. * stopped.
  14932. **/
  14933. static int
  14934. lpfc_sli4_post_sgl_list(struct lpfc_hba *phba,
  14935. struct list_head *post_sgl_list,
  14936. int post_cnt)
  14937. {
  14938. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  14939. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  14940. struct sgl_page_pairs *sgl_pg_pairs;
  14941. void *viraddr;
  14942. LPFC_MBOXQ_t *mbox;
  14943. uint32_t reqlen, alloclen, pg_pairs;
  14944. uint32_t mbox_tmo;
  14945. uint16_t xritag_start = 0;
  14946. int rc = 0;
  14947. uint32_t shdr_status, shdr_add_status;
  14948. union lpfc_sli4_cfg_shdr *shdr;
  14949. reqlen = post_cnt * sizeof(struct sgl_page_pairs) +
  14950. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  14951. if (reqlen > SLI4_PAGE_SIZE) {
  14952. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14953. "2559 Block sgl registration required DMA "
  14954. "size (%d) great than a page\n", reqlen);
  14955. return -ENOMEM;
  14956. }
  14957. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14958. if (!mbox)
  14959. return -ENOMEM;
  14960. /* Allocate DMA memory and set up the non-embedded mailbox command */
  14961. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14962. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  14963. LPFC_SLI4_MBX_NEMBED);
  14964. if (alloclen < reqlen) {
  14965. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14966. "0285 Allocated DMA memory size (%d) is "
  14967. "less than the requested DMA memory "
  14968. "size (%d)\n", alloclen, reqlen);
  14969. lpfc_sli4_mbox_cmd_free(phba, mbox);
  14970. return -ENOMEM;
  14971. }
  14972. /* Set up the SGL pages in the non-embedded DMA pages */
  14973. viraddr = mbox->sge_array->addr[0];
  14974. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  14975. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  14976. pg_pairs = 0;
  14977. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  14978. /* Set up the sge entry */
  14979. sgl_pg_pairs->sgl_pg0_addr_lo =
  14980. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  14981. sgl_pg_pairs->sgl_pg0_addr_hi =
  14982. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  14983. sgl_pg_pairs->sgl_pg1_addr_lo =
  14984. cpu_to_le32(putPaddrLow(0));
  14985. sgl_pg_pairs->sgl_pg1_addr_hi =
  14986. cpu_to_le32(putPaddrHigh(0));
  14987. /* Keep the first xritag on the list */
  14988. if (pg_pairs == 0)
  14989. xritag_start = sglq_entry->sli4_xritag;
  14990. sgl_pg_pairs++;
  14991. pg_pairs++;
  14992. }
  14993. /* Complete initialization and perform endian conversion. */
  14994. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  14995. bf_set(lpfc_post_sgl_pages_xricnt, sgl, post_cnt);
  14996. sgl->word0 = cpu_to_le32(sgl->word0);
  14997. if (!phba->sli4_hba.intr_enable)
  14998. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14999. else {
  15000. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  15001. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  15002. }
  15003. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  15004. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  15005. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  15006. if (rc != MBX_TIMEOUT)
  15007. lpfc_sli4_mbox_cmd_free(phba, mbox);
  15008. if (shdr_status || shdr_add_status || rc) {
  15009. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15010. "2513 POST_SGL_BLOCK mailbox command failed "
  15011. "status x%x add_status x%x mbx status x%x\n",
  15012. shdr_status, shdr_add_status, rc);
  15013. rc = -ENXIO;
  15014. }
  15015. return rc;
  15016. }
  15017. /**
  15018. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  15019. * @phba: pointer to lpfc hba data structure.
  15020. * @sblist: pointer to scsi buffer list.
  15021. * @count: number of scsi buffers on the list.
  15022. *
  15023. * This routine is invoked to post a block of @count scsi sgl pages from a
  15024. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  15025. * No Lock is held.
  15026. *
  15027. **/
  15028. int
  15029. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  15030. struct list_head *sblist,
  15031. int count)
  15032. {
  15033. struct lpfc_scsi_buf *psb;
  15034. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  15035. struct sgl_page_pairs *sgl_pg_pairs;
  15036. void *viraddr;
  15037. LPFC_MBOXQ_t *mbox;
  15038. uint32_t reqlen, alloclen, pg_pairs;
  15039. uint32_t mbox_tmo;
  15040. uint16_t xritag_start = 0;
  15041. int rc = 0;
  15042. uint32_t shdr_status, shdr_add_status;
  15043. dma_addr_t pdma_phys_bpl1;
  15044. union lpfc_sli4_cfg_shdr *shdr;
  15045. /* Calculate the requested length of the dma memory */
  15046. reqlen = count * sizeof(struct sgl_page_pairs) +
  15047. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  15048. if (reqlen > SLI4_PAGE_SIZE) {
  15049. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  15050. "0217 Block sgl registration required DMA "
  15051. "size (%d) great than a page\n", reqlen);
  15052. return -ENOMEM;
  15053. }
  15054. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  15055. if (!mbox) {
  15056. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15057. "0283 Failed to allocate mbox cmd memory\n");
  15058. return -ENOMEM;
  15059. }
  15060. /* Allocate DMA memory and set up the non-embedded mailbox command */
  15061. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  15062. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  15063. LPFC_SLI4_MBX_NEMBED);
  15064. if (alloclen < reqlen) {
  15065. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  15066. "2561 Allocated DMA memory size (%d) is "
  15067. "less than the requested DMA memory "
  15068. "size (%d)\n", alloclen, reqlen);
  15069. lpfc_sli4_mbox_cmd_free(phba, mbox);
  15070. return -ENOMEM;
  15071. }
  15072. /* Get the first SGE entry from the non-embedded DMA memory */
  15073. viraddr = mbox->sge_array->addr[0];
  15074. /* Set up the SGL pages in the non-embedded DMA pages */
  15075. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  15076. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  15077. pg_pairs = 0;
  15078. list_for_each_entry(psb, sblist, list) {
  15079. /* Set up the sge entry */
  15080. sgl_pg_pairs->sgl_pg0_addr_lo =
  15081. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  15082. sgl_pg_pairs->sgl_pg0_addr_hi =
  15083. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  15084. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  15085. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  15086. else
  15087. pdma_phys_bpl1 = 0;
  15088. sgl_pg_pairs->sgl_pg1_addr_lo =
  15089. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  15090. sgl_pg_pairs->sgl_pg1_addr_hi =
  15091. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  15092. /* Keep the first xritag on the list */
  15093. if (pg_pairs == 0)
  15094. xritag_start = psb->cur_iocbq.sli4_xritag;
  15095. sgl_pg_pairs++;
  15096. pg_pairs++;
  15097. }
  15098. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  15099. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  15100. /* Perform endian conversion if necessary */
  15101. sgl->word0 = cpu_to_le32(sgl->word0);
  15102. if (!phba->sli4_hba.intr_enable)
  15103. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  15104. else {
  15105. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  15106. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  15107. }
  15108. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  15109. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  15110. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  15111. if (rc != MBX_TIMEOUT)
  15112. lpfc_sli4_mbox_cmd_free(phba, mbox);
  15113. if (shdr_status || shdr_add_status || rc) {
  15114. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15115. "2564 POST_SGL_BLOCK mailbox command failed "
  15116. "status x%x add_status x%x mbx status x%x\n",
  15117. shdr_status, shdr_add_status, rc);
  15118. rc = -ENXIO;
  15119. }
  15120. return rc;
  15121. }
  15122. /**
  15123. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  15124. * @phba: pointer to lpfc_hba struct that the frame was received on
  15125. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  15126. *
  15127. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  15128. * valid type of frame that the LPFC driver will handle. This function will
  15129. * return a zero if the frame is a valid frame or a non zero value when the
  15130. * frame does not pass the check.
  15131. **/
  15132. static int
  15133. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  15134. {
  15135. /* make rctl_names static to save stack space */
  15136. struct fc_vft_header *fc_vft_hdr;
  15137. uint32_t *header = (uint32_t *) fc_hdr;
  15138. #define FC_RCTL_MDS_DIAGS 0xF4
  15139. switch (fc_hdr->fh_r_ctl) {
  15140. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  15141. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  15142. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  15143. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  15144. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  15145. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  15146. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  15147. case FC_RCTL_DD_CMD_STATUS: /* command status */
  15148. case FC_RCTL_ELS_REQ: /* extended link services request */
  15149. case FC_RCTL_ELS_REP: /* extended link services reply */
  15150. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  15151. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  15152. case FC_RCTL_BA_NOP: /* basic link service NOP */
  15153. case FC_RCTL_BA_ABTS: /* basic link service abort */
  15154. case FC_RCTL_BA_RMC: /* remove connection */
  15155. case FC_RCTL_BA_ACC: /* basic accept */
  15156. case FC_RCTL_BA_RJT: /* basic reject */
  15157. case FC_RCTL_BA_PRMT:
  15158. case FC_RCTL_ACK_1: /* acknowledge_1 */
  15159. case FC_RCTL_ACK_0: /* acknowledge_0 */
  15160. case FC_RCTL_P_RJT: /* port reject */
  15161. case FC_RCTL_F_RJT: /* fabric reject */
  15162. case FC_RCTL_P_BSY: /* port busy */
  15163. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  15164. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  15165. case FC_RCTL_LCR: /* link credit reset */
  15166. case FC_RCTL_MDS_DIAGS: /* MDS Diagnostics */
  15167. case FC_RCTL_END: /* end */
  15168. break;
  15169. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  15170. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  15171. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  15172. return lpfc_fc_frame_check(phba, fc_hdr);
  15173. default:
  15174. goto drop;
  15175. }
  15176. #define FC_TYPE_VENDOR_UNIQUE 0xFF
  15177. switch (fc_hdr->fh_type) {
  15178. case FC_TYPE_BLS:
  15179. case FC_TYPE_ELS:
  15180. case FC_TYPE_FCP:
  15181. case FC_TYPE_CT:
  15182. case FC_TYPE_NVME:
  15183. case FC_TYPE_VENDOR_UNIQUE:
  15184. break;
  15185. case FC_TYPE_IP:
  15186. case FC_TYPE_ILS:
  15187. default:
  15188. goto drop;
  15189. }
  15190. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  15191. "2538 Received frame rctl:x%x, type:x%x, "
  15192. "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
  15193. fc_hdr->fh_r_ctl, fc_hdr->fh_type,
  15194. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  15195. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  15196. be32_to_cpu(header[4]), be32_to_cpu(header[5]),
  15197. be32_to_cpu(header[6]));
  15198. return 0;
  15199. drop:
  15200. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  15201. "2539 Dropped frame rctl:x%x type:x%x\n",
  15202. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  15203. return 1;
  15204. }
  15205. /**
  15206. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  15207. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  15208. *
  15209. * This function processes the FC header to retrieve the VFI from the VF
  15210. * header, if one exists. This function will return the VFI if one exists
  15211. * or 0 if no VSAN Header exists.
  15212. **/
  15213. static uint32_t
  15214. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  15215. {
  15216. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  15217. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  15218. return 0;
  15219. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  15220. }
  15221. /**
  15222. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  15223. * @phba: Pointer to the HBA structure to search for the vport on
  15224. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  15225. * @fcfi: The FC Fabric ID that the frame came from
  15226. *
  15227. * This function searches the @phba for a vport that matches the content of the
  15228. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  15229. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  15230. * returns the matching vport pointer or NULL if unable to match frame to a
  15231. * vport.
  15232. **/
  15233. static struct lpfc_vport *
  15234. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  15235. uint16_t fcfi, uint32_t did)
  15236. {
  15237. struct lpfc_vport **vports;
  15238. struct lpfc_vport *vport = NULL;
  15239. int i;
  15240. if (did == Fabric_DID)
  15241. return phba->pport;
  15242. if ((phba->pport->fc_flag & FC_PT2PT) &&
  15243. !(phba->link_state == LPFC_HBA_READY))
  15244. return phba->pport;
  15245. vports = lpfc_create_vport_work_array(phba);
  15246. if (vports != NULL) {
  15247. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  15248. if (phba->fcf.fcfi == fcfi &&
  15249. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  15250. vports[i]->fc_myDID == did) {
  15251. vport = vports[i];
  15252. break;
  15253. }
  15254. }
  15255. }
  15256. lpfc_destroy_vport_work_array(phba, vports);
  15257. return vport;
  15258. }
  15259. /**
  15260. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  15261. * @vport: The vport to work on.
  15262. *
  15263. * This function updates the receive sequence time stamp for this vport. The
  15264. * receive sequence time stamp indicates the time that the last frame of the
  15265. * the sequence that has been idle for the longest amount of time was received.
  15266. * the driver uses this time stamp to indicate if any received sequences have
  15267. * timed out.
  15268. **/
  15269. static void
  15270. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  15271. {
  15272. struct lpfc_dmabuf *h_buf;
  15273. struct hbq_dmabuf *dmabuf = NULL;
  15274. /* get the oldest sequence on the rcv list */
  15275. h_buf = list_get_first(&vport->rcv_buffer_list,
  15276. struct lpfc_dmabuf, list);
  15277. if (!h_buf)
  15278. return;
  15279. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  15280. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  15281. }
  15282. /**
  15283. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  15284. * @vport: The vport that the received sequences were sent to.
  15285. *
  15286. * This function cleans up all outstanding received sequences. This is called
  15287. * by the driver when a link event or user action invalidates all the received
  15288. * sequences.
  15289. **/
  15290. void
  15291. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  15292. {
  15293. struct lpfc_dmabuf *h_buf, *hnext;
  15294. struct lpfc_dmabuf *d_buf, *dnext;
  15295. struct hbq_dmabuf *dmabuf = NULL;
  15296. /* start with the oldest sequence on the rcv list */
  15297. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  15298. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  15299. list_del_init(&dmabuf->hbuf.list);
  15300. list_for_each_entry_safe(d_buf, dnext,
  15301. &dmabuf->dbuf.list, list) {
  15302. list_del_init(&d_buf->list);
  15303. lpfc_in_buf_free(vport->phba, d_buf);
  15304. }
  15305. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  15306. }
  15307. }
  15308. /**
  15309. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  15310. * @vport: The vport that the received sequences were sent to.
  15311. *
  15312. * This function determines whether any received sequences have timed out by
  15313. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  15314. * indicates that there is at least one timed out sequence this routine will
  15315. * go through the received sequences one at a time from most inactive to most
  15316. * active to determine which ones need to be cleaned up. Once it has determined
  15317. * that a sequence needs to be cleaned up it will simply free up the resources
  15318. * without sending an abort.
  15319. **/
  15320. void
  15321. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  15322. {
  15323. struct lpfc_dmabuf *h_buf, *hnext;
  15324. struct lpfc_dmabuf *d_buf, *dnext;
  15325. struct hbq_dmabuf *dmabuf = NULL;
  15326. unsigned long timeout;
  15327. int abort_count = 0;
  15328. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  15329. vport->rcv_buffer_time_stamp);
  15330. if (list_empty(&vport->rcv_buffer_list) ||
  15331. time_before(jiffies, timeout))
  15332. return;
  15333. /* start with the oldest sequence on the rcv list */
  15334. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  15335. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  15336. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  15337. dmabuf->time_stamp);
  15338. if (time_before(jiffies, timeout))
  15339. break;
  15340. abort_count++;
  15341. list_del_init(&dmabuf->hbuf.list);
  15342. list_for_each_entry_safe(d_buf, dnext,
  15343. &dmabuf->dbuf.list, list) {
  15344. list_del_init(&d_buf->list);
  15345. lpfc_in_buf_free(vport->phba, d_buf);
  15346. }
  15347. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  15348. }
  15349. if (abort_count)
  15350. lpfc_update_rcv_time_stamp(vport);
  15351. }
  15352. /**
  15353. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  15354. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  15355. *
  15356. * This function searches through the existing incomplete sequences that have
  15357. * been sent to this @vport. If the frame matches one of the incomplete
  15358. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  15359. * make up that sequence. If no sequence is found that matches this frame then
  15360. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  15361. * This function returns a pointer to the first dmabuf in the sequence list that
  15362. * the frame was linked to.
  15363. **/
  15364. static struct hbq_dmabuf *
  15365. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  15366. {
  15367. struct fc_frame_header *new_hdr;
  15368. struct fc_frame_header *temp_hdr;
  15369. struct lpfc_dmabuf *d_buf;
  15370. struct lpfc_dmabuf *h_buf;
  15371. struct hbq_dmabuf *seq_dmabuf = NULL;
  15372. struct hbq_dmabuf *temp_dmabuf = NULL;
  15373. uint8_t found = 0;
  15374. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  15375. dmabuf->time_stamp = jiffies;
  15376. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  15377. /* Use the hdr_buf to find the sequence that this frame belongs to */
  15378. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  15379. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  15380. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  15381. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  15382. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  15383. continue;
  15384. /* found a pending sequence that matches this frame */
  15385. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  15386. break;
  15387. }
  15388. if (!seq_dmabuf) {
  15389. /*
  15390. * This indicates first frame received for this sequence.
  15391. * Queue the buffer on the vport's rcv_buffer_list.
  15392. */
  15393. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  15394. lpfc_update_rcv_time_stamp(vport);
  15395. return dmabuf;
  15396. }
  15397. temp_hdr = seq_dmabuf->hbuf.virt;
  15398. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  15399. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  15400. list_del_init(&seq_dmabuf->hbuf.list);
  15401. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  15402. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  15403. lpfc_update_rcv_time_stamp(vport);
  15404. return dmabuf;
  15405. }
  15406. /* move this sequence to the tail to indicate a young sequence */
  15407. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  15408. seq_dmabuf->time_stamp = jiffies;
  15409. lpfc_update_rcv_time_stamp(vport);
  15410. if (list_empty(&seq_dmabuf->dbuf.list)) {
  15411. temp_hdr = dmabuf->hbuf.virt;
  15412. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  15413. return seq_dmabuf;
  15414. }
  15415. /* find the correct place in the sequence to insert this frame */
  15416. d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
  15417. while (!found) {
  15418. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  15419. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  15420. /*
  15421. * If the frame's sequence count is greater than the frame on
  15422. * the list then insert the frame right after this frame
  15423. */
  15424. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  15425. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  15426. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  15427. found = 1;
  15428. break;
  15429. }
  15430. if (&d_buf->list == &seq_dmabuf->dbuf.list)
  15431. break;
  15432. d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
  15433. }
  15434. if (found)
  15435. return seq_dmabuf;
  15436. return NULL;
  15437. }
  15438. /**
  15439. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  15440. * @vport: pointer to a vitural port
  15441. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  15442. *
  15443. * This function tries to abort from the partially assembed sequence, described
  15444. * by the information from basic abbort @dmabuf. It checks to see whether such
  15445. * partially assembled sequence held by the driver. If so, it shall free up all
  15446. * the frames from the partially assembled sequence.
  15447. *
  15448. * Return
  15449. * true -- if there is matching partially assembled sequence present and all
  15450. * the frames freed with the sequence;
  15451. * false -- if there is no matching partially assembled sequence present so
  15452. * nothing got aborted in the lower layer driver
  15453. **/
  15454. static bool
  15455. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  15456. struct hbq_dmabuf *dmabuf)
  15457. {
  15458. struct fc_frame_header *new_hdr;
  15459. struct fc_frame_header *temp_hdr;
  15460. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  15461. struct hbq_dmabuf *seq_dmabuf = NULL;
  15462. /* Use the hdr_buf to find the sequence that matches this frame */
  15463. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  15464. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  15465. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  15466. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  15467. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  15468. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  15469. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  15470. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  15471. continue;
  15472. /* found a pending sequence that matches this frame */
  15473. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  15474. break;
  15475. }
  15476. /* Free up all the frames from the partially assembled sequence */
  15477. if (seq_dmabuf) {
  15478. list_for_each_entry_safe(d_buf, n_buf,
  15479. &seq_dmabuf->dbuf.list, list) {
  15480. list_del_init(&d_buf->list);
  15481. lpfc_in_buf_free(vport->phba, d_buf);
  15482. }
  15483. return true;
  15484. }
  15485. return false;
  15486. }
  15487. /**
  15488. * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
  15489. * @vport: pointer to a vitural port
  15490. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  15491. *
  15492. * This function tries to abort from the assembed sequence from upper level
  15493. * protocol, described by the information from basic abbort @dmabuf. It
  15494. * checks to see whether such pending context exists at upper level protocol.
  15495. * If so, it shall clean up the pending context.
  15496. *
  15497. * Return
  15498. * true -- if there is matching pending context of the sequence cleaned
  15499. * at ulp;
  15500. * false -- if there is no matching pending context of the sequence present
  15501. * at ulp.
  15502. **/
  15503. static bool
  15504. lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  15505. {
  15506. struct lpfc_hba *phba = vport->phba;
  15507. int handled;
  15508. /* Accepting abort at ulp with SLI4 only */
  15509. if (phba->sli_rev < LPFC_SLI_REV4)
  15510. return false;
  15511. /* Register all caring upper level protocols to attend abort */
  15512. handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
  15513. if (handled)
  15514. return true;
  15515. return false;
  15516. }
  15517. /**
  15518. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  15519. * @phba: Pointer to HBA context object.
  15520. * @cmd_iocbq: pointer to the command iocbq structure.
  15521. * @rsp_iocbq: pointer to the response iocbq structure.
  15522. *
  15523. * This function handles the sequence abort response iocb command complete
  15524. * event. It properly releases the memory allocated to the sequence abort
  15525. * accept iocb.
  15526. **/
  15527. static void
  15528. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  15529. struct lpfc_iocbq *cmd_iocbq,
  15530. struct lpfc_iocbq *rsp_iocbq)
  15531. {
  15532. struct lpfc_nodelist *ndlp;
  15533. if (cmd_iocbq) {
  15534. ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
  15535. lpfc_nlp_put(ndlp);
  15536. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  15537. }
  15538. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  15539. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  15540. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15541. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  15542. rsp_iocbq->iocb.ulpStatus,
  15543. rsp_iocbq->iocb.un.ulpWord[4]);
  15544. }
  15545. /**
  15546. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  15547. * @phba: Pointer to HBA context object.
  15548. * @xri: xri id in transaction.
  15549. *
  15550. * This function validates the xri maps to the known range of XRIs allocated an
  15551. * used by the driver.
  15552. **/
  15553. uint16_t
  15554. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  15555. uint16_t xri)
  15556. {
  15557. uint16_t i;
  15558. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  15559. if (xri == phba->sli4_hba.xri_ids[i])
  15560. return i;
  15561. }
  15562. return NO_XRI;
  15563. }
  15564. /**
  15565. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  15566. * @phba: Pointer to HBA context object.
  15567. * @fc_hdr: pointer to a FC frame header.
  15568. *
  15569. * This function sends a basic response to a previous unsol sequence abort
  15570. * event after aborting the sequence handling.
  15571. **/
  15572. void
  15573. lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
  15574. struct fc_frame_header *fc_hdr, bool aborted)
  15575. {
  15576. struct lpfc_hba *phba = vport->phba;
  15577. struct lpfc_iocbq *ctiocb = NULL;
  15578. struct lpfc_nodelist *ndlp;
  15579. uint16_t oxid, rxid, xri, lxri;
  15580. uint32_t sid, fctl;
  15581. IOCB_t *icmd;
  15582. int rc;
  15583. if (!lpfc_is_link_up(phba))
  15584. return;
  15585. sid = sli4_sid_from_fc_hdr(fc_hdr);
  15586. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  15587. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  15588. ndlp = lpfc_findnode_did(vport, sid);
  15589. if (!ndlp) {
  15590. ndlp = lpfc_nlp_init(vport, sid);
  15591. if (!ndlp) {
  15592. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  15593. "1268 Failed to allocate ndlp for "
  15594. "oxid:x%x SID:x%x\n", oxid, sid);
  15595. return;
  15596. }
  15597. /* Put ndlp onto pport node list */
  15598. lpfc_enqueue_node(vport, ndlp);
  15599. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  15600. /* re-setup ndlp without removing from node list */
  15601. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  15602. if (!ndlp) {
  15603. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  15604. "3275 Failed to active ndlp found "
  15605. "for oxid:x%x SID:x%x\n", oxid, sid);
  15606. return;
  15607. }
  15608. }
  15609. /* Allocate buffer for rsp iocb */
  15610. ctiocb = lpfc_sli_get_iocbq(phba);
  15611. if (!ctiocb)
  15612. return;
  15613. /* Extract the F_CTL field from FC_HDR */
  15614. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  15615. icmd = &ctiocb->iocb;
  15616. icmd->un.xseq64.bdl.bdeSize = 0;
  15617. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  15618. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  15619. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  15620. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  15621. /* Fill in the rest of iocb fields */
  15622. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  15623. icmd->ulpBdeCount = 0;
  15624. icmd->ulpLe = 1;
  15625. icmd->ulpClass = CLASS3;
  15626. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  15627. ctiocb->context1 = lpfc_nlp_get(ndlp);
  15628. ctiocb->iocb_cmpl = NULL;
  15629. ctiocb->vport = phba->pport;
  15630. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  15631. ctiocb->sli4_lxritag = NO_XRI;
  15632. ctiocb->sli4_xritag = NO_XRI;
  15633. if (fctl & FC_FC_EX_CTX)
  15634. /* Exchange responder sent the abort so we
  15635. * own the oxid.
  15636. */
  15637. xri = oxid;
  15638. else
  15639. xri = rxid;
  15640. lxri = lpfc_sli4_xri_inrange(phba, xri);
  15641. if (lxri != NO_XRI)
  15642. lpfc_set_rrq_active(phba, ndlp, lxri,
  15643. (xri == oxid) ? rxid : oxid, 0);
  15644. /* For BA_ABTS from exchange responder, if the logical xri with
  15645. * the oxid maps to the FCP XRI range, the port no longer has
  15646. * that exchange context, send a BLS_RJT. Override the IOCB for
  15647. * a BA_RJT.
  15648. */
  15649. if ((fctl & FC_FC_EX_CTX) &&
  15650. (lxri > lpfc_sli4_get_iocb_cnt(phba))) {
  15651. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  15652. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  15653. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  15654. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  15655. }
  15656. /* If BA_ABTS failed to abort a partially assembled receive sequence,
  15657. * the driver no longer has that exchange, send a BLS_RJT. Override
  15658. * the IOCB for a BA_RJT.
  15659. */
  15660. if (aborted == false) {
  15661. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  15662. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  15663. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  15664. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  15665. }
  15666. if (fctl & FC_FC_EX_CTX) {
  15667. /* ABTS sent by responder to CT exchange, construction
  15668. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  15669. * field and RX_ID from ABTS for RX_ID field.
  15670. */
  15671. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  15672. } else {
  15673. /* ABTS sent by initiator to CT exchange, construction
  15674. * of BA_ACC will need to allocate a new XRI as for the
  15675. * XRI_TAG field.
  15676. */
  15677. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  15678. }
  15679. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  15680. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  15681. /* Xmit CT abts response on exchange <xid> */
  15682. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  15683. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  15684. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  15685. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  15686. if (rc == IOCB_ERROR) {
  15687. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  15688. "2925 Failed to issue CT ABTS RSP x%x on "
  15689. "xri x%x, Data x%x\n",
  15690. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  15691. phba->link_state);
  15692. lpfc_nlp_put(ndlp);
  15693. ctiocb->context1 = NULL;
  15694. lpfc_sli_release_iocbq(phba, ctiocb);
  15695. }
  15696. }
  15697. /**
  15698. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  15699. * @vport: Pointer to the vport on which this sequence was received
  15700. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  15701. *
  15702. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  15703. * receive sequence is only partially assembed by the driver, it shall abort
  15704. * the partially assembled frames for the sequence. Otherwise, if the
  15705. * unsolicited receive sequence has been completely assembled and passed to
  15706. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  15707. * unsolicited sequence has been aborted. After that, it will issue a basic
  15708. * accept to accept the abort.
  15709. **/
  15710. static void
  15711. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  15712. struct hbq_dmabuf *dmabuf)
  15713. {
  15714. struct lpfc_hba *phba = vport->phba;
  15715. struct fc_frame_header fc_hdr;
  15716. uint32_t fctl;
  15717. bool aborted;
  15718. /* Make a copy of fc_hdr before the dmabuf being released */
  15719. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  15720. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  15721. if (fctl & FC_FC_EX_CTX) {
  15722. /* ABTS by responder to exchange, no cleanup needed */
  15723. aborted = true;
  15724. } else {
  15725. /* ABTS by initiator to exchange, need to do cleanup */
  15726. aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  15727. if (aborted == false)
  15728. aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
  15729. }
  15730. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  15731. if (phba->nvmet_support) {
  15732. lpfc_nvmet_rcv_unsol_abort(vport, &fc_hdr);
  15733. return;
  15734. }
  15735. /* Respond with BA_ACC or BA_RJT accordingly */
  15736. lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
  15737. }
  15738. /**
  15739. * lpfc_seq_complete - Indicates if a sequence is complete
  15740. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  15741. *
  15742. * This function checks the sequence, starting with the frame described by
  15743. * @dmabuf, to see if all the frames associated with this sequence are present.
  15744. * the frames associated with this sequence are linked to the @dmabuf using the
  15745. * dbuf list. This function looks for two major things. 1) That the first frame
  15746. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  15747. * set. 3) That there are no holes in the sequence count. The function will
  15748. * return 1 when the sequence is complete, otherwise it will return 0.
  15749. **/
  15750. static int
  15751. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  15752. {
  15753. struct fc_frame_header *hdr;
  15754. struct lpfc_dmabuf *d_buf;
  15755. struct hbq_dmabuf *seq_dmabuf;
  15756. uint32_t fctl;
  15757. int seq_count = 0;
  15758. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  15759. /* make sure first fame of sequence has a sequence count of zero */
  15760. if (hdr->fh_seq_cnt != seq_count)
  15761. return 0;
  15762. fctl = (hdr->fh_f_ctl[0] << 16 |
  15763. hdr->fh_f_ctl[1] << 8 |
  15764. hdr->fh_f_ctl[2]);
  15765. /* If last frame of sequence we can return success. */
  15766. if (fctl & FC_FC_END_SEQ)
  15767. return 1;
  15768. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  15769. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  15770. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  15771. /* If there is a hole in the sequence count then fail. */
  15772. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  15773. return 0;
  15774. fctl = (hdr->fh_f_ctl[0] << 16 |
  15775. hdr->fh_f_ctl[1] << 8 |
  15776. hdr->fh_f_ctl[2]);
  15777. /* If last frame of sequence we can return success. */
  15778. if (fctl & FC_FC_END_SEQ)
  15779. return 1;
  15780. }
  15781. return 0;
  15782. }
  15783. /**
  15784. * lpfc_prep_seq - Prep sequence for ULP processing
  15785. * @vport: Pointer to the vport on which this sequence was received
  15786. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  15787. *
  15788. * This function takes a sequence, described by a list of frames, and creates
  15789. * a list of iocbq structures to describe the sequence. This iocbq list will be
  15790. * used to issue to the generic unsolicited sequence handler. This routine
  15791. * returns a pointer to the first iocbq in the list. If the function is unable
  15792. * to allocate an iocbq then it throw out the received frames that were not
  15793. * able to be described and return a pointer to the first iocbq. If unable to
  15794. * allocate any iocbqs (including the first) this function will return NULL.
  15795. **/
  15796. static struct lpfc_iocbq *
  15797. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  15798. {
  15799. struct hbq_dmabuf *hbq_buf;
  15800. struct lpfc_dmabuf *d_buf, *n_buf;
  15801. struct lpfc_iocbq *first_iocbq, *iocbq;
  15802. struct fc_frame_header *fc_hdr;
  15803. uint32_t sid;
  15804. uint32_t len, tot_len;
  15805. struct ulp_bde64 *pbde;
  15806. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  15807. /* remove from receive buffer list */
  15808. list_del_init(&seq_dmabuf->hbuf.list);
  15809. lpfc_update_rcv_time_stamp(vport);
  15810. /* get the Remote Port's SID */
  15811. sid = sli4_sid_from_fc_hdr(fc_hdr);
  15812. tot_len = 0;
  15813. /* Get an iocbq struct to fill in. */
  15814. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  15815. if (first_iocbq) {
  15816. /* Initialize the first IOCB. */
  15817. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  15818. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  15819. first_iocbq->vport = vport;
  15820. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  15821. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  15822. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  15823. first_iocbq->iocb.un.rcvels.parmRo =
  15824. sli4_did_from_fc_hdr(fc_hdr);
  15825. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  15826. } else
  15827. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  15828. first_iocbq->iocb.ulpContext = NO_XRI;
  15829. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  15830. be16_to_cpu(fc_hdr->fh_ox_id);
  15831. /* iocbq is prepped for internal consumption. Physical vpi. */
  15832. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  15833. vport->phba->vpi_ids[vport->vpi];
  15834. /* put the first buffer into the first IOCBq */
  15835. tot_len = bf_get(lpfc_rcqe_length,
  15836. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  15837. first_iocbq->context2 = &seq_dmabuf->dbuf;
  15838. first_iocbq->context3 = NULL;
  15839. first_iocbq->iocb.ulpBdeCount = 1;
  15840. if (tot_len > LPFC_DATA_BUF_SIZE)
  15841. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  15842. LPFC_DATA_BUF_SIZE;
  15843. else
  15844. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
  15845. first_iocbq->iocb.un.rcvels.remoteID = sid;
  15846. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  15847. }
  15848. iocbq = first_iocbq;
  15849. /*
  15850. * Each IOCBq can have two Buffers assigned, so go through the list
  15851. * of buffers for this sequence and save two buffers in each IOCBq
  15852. */
  15853. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  15854. if (!iocbq) {
  15855. lpfc_in_buf_free(vport->phba, d_buf);
  15856. continue;
  15857. }
  15858. if (!iocbq->context3) {
  15859. iocbq->context3 = d_buf;
  15860. iocbq->iocb.ulpBdeCount++;
  15861. /* We need to get the size out of the right CQE */
  15862. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  15863. len = bf_get(lpfc_rcqe_length,
  15864. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  15865. pbde = (struct ulp_bde64 *)
  15866. &iocbq->iocb.unsli3.sli3Words[4];
  15867. if (len > LPFC_DATA_BUF_SIZE)
  15868. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  15869. else
  15870. pbde->tus.f.bdeSize = len;
  15871. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  15872. tot_len += len;
  15873. } else {
  15874. iocbq = lpfc_sli_get_iocbq(vport->phba);
  15875. if (!iocbq) {
  15876. if (first_iocbq) {
  15877. first_iocbq->iocb.ulpStatus =
  15878. IOSTAT_FCP_RSP_ERROR;
  15879. first_iocbq->iocb.un.ulpWord[4] =
  15880. IOERR_NO_RESOURCES;
  15881. }
  15882. lpfc_in_buf_free(vport->phba, d_buf);
  15883. continue;
  15884. }
  15885. /* We need to get the size out of the right CQE */
  15886. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  15887. len = bf_get(lpfc_rcqe_length,
  15888. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  15889. iocbq->context2 = d_buf;
  15890. iocbq->context3 = NULL;
  15891. iocbq->iocb.ulpBdeCount = 1;
  15892. if (len > LPFC_DATA_BUF_SIZE)
  15893. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  15894. LPFC_DATA_BUF_SIZE;
  15895. else
  15896. iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
  15897. tot_len += len;
  15898. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  15899. iocbq->iocb.un.rcvels.remoteID = sid;
  15900. list_add_tail(&iocbq->list, &first_iocbq->list);
  15901. }
  15902. }
  15903. /* Free the sequence's header buffer */
  15904. if (!first_iocbq)
  15905. lpfc_in_buf_free(vport->phba, &seq_dmabuf->dbuf);
  15906. return first_iocbq;
  15907. }
  15908. static void
  15909. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  15910. struct hbq_dmabuf *seq_dmabuf)
  15911. {
  15912. struct fc_frame_header *fc_hdr;
  15913. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  15914. struct lpfc_hba *phba = vport->phba;
  15915. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  15916. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  15917. if (!iocbq) {
  15918. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15919. "2707 Ring %d handler: Failed to allocate "
  15920. "iocb Rctl x%x Type x%x received\n",
  15921. LPFC_ELS_RING,
  15922. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  15923. return;
  15924. }
  15925. if (!lpfc_complete_unsol_iocb(phba,
  15926. phba->sli4_hba.els_wq->pring,
  15927. iocbq, fc_hdr->fh_r_ctl,
  15928. fc_hdr->fh_type))
  15929. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  15930. "2540 Ring %d handler: unexpected Rctl "
  15931. "x%x Type x%x received\n",
  15932. LPFC_ELS_RING,
  15933. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  15934. /* Free iocb created in lpfc_prep_seq */
  15935. list_for_each_entry_safe(curr_iocb, next_iocb,
  15936. &iocbq->list, list) {
  15937. list_del_init(&curr_iocb->list);
  15938. lpfc_sli_release_iocbq(phba, curr_iocb);
  15939. }
  15940. lpfc_sli_release_iocbq(phba, iocbq);
  15941. }
  15942. static void
  15943. lpfc_sli4_mds_loopback_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  15944. struct lpfc_iocbq *rspiocb)
  15945. {
  15946. struct lpfc_dmabuf *pcmd = cmdiocb->context2;
  15947. if (pcmd && pcmd->virt)
  15948. dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
  15949. kfree(pcmd);
  15950. lpfc_sli_release_iocbq(phba, cmdiocb);
  15951. }
  15952. static void
  15953. lpfc_sli4_handle_mds_loopback(struct lpfc_vport *vport,
  15954. struct hbq_dmabuf *dmabuf)
  15955. {
  15956. struct fc_frame_header *fc_hdr;
  15957. struct lpfc_hba *phba = vport->phba;
  15958. struct lpfc_iocbq *iocbq = NULL;
  15959. union lpfc_wqe *wqe;
  15960. struct lpfc_dmabuf *pcmd = NULL;
  15961. uint32_t frame_len;
  15962. int rc;
  15963. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  15964. frame_len = bf_get(lpfc_rcqe_length, &dmabuf->cq_event.cqe.rcqe_cmpl);
  15965. /* Send the received frame back */
  15966. iocbq = lpfc_sli_get_iocbq(phba);
  15967. if (!iocbq)
  15968. goto exit;
  15969. /* Allocate buffer for command payload */
  15970. pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  15971. if (pcmd)
  15972. pcmd->virt = dma_pool_alloc(phba->lpfc_drb_pool, GFP_KERNEL,
  15973. &pcmd->phys);
  15974. if (!pcmd || !pcmd->virt)
  15975. goto exit;
  15976. INIT_LIST_HEAD(&pcmd->list);
  15977. /* copyin the payload */
  15978. memcpy(pcmd->virt, dmabuf->dbuf.virt, frame_len);
  15979. /* fill in BDE's for command */
  15980. iocbq->iocb.un.xseq64.bdl.addrHigh = putPaddrHigh(pcmd->phys);
  15981. iocbq->iocb.un.xseq64.bdl.addrLow = putPaddrLow(pcmd->phys);
  15982. iocbq->iocb.un.xseq64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
  15983. iocbq->iocb.un.xseq64.bdl.bdeSize = frame_len;
  15984. iocbq->context2 = pcmd;
  15985. iocbq->vport = vport;
  15986. iocbq->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
  15987. iocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
  15988. /*
  15989. * Setup rest of the iocb as though it were a WQE
  15990. * Build the SEND_FRAME WQE
  15991. */
  15992. wqe = (union lpfc_wqe *)&iocbq->iocb;
  15993. wqe->send_frame.frame_len = frame_len;
  15994. wqe->send_frame.fc_hdr_wd0 = be32_to_cpu(*((uint32_t *)fc_hdr));
  15995. wqe->send_frame.fc_hdr_wd1 = be32_to_cpu(*((uint32_t *)fc_hdr + 1));
  15996. wqe->send_frame.fc_hdr_wd2 = be32_to_cpu(*((uint32_t *)fc_hdr + 2));
  15997. wqe->send_frame.fc_hdr_wd3 = be32_to_cpu(*((uint32_t *)fc_hdr + 3));
  15998. wqe->send_frame.fc_hdr_wd4 = be32_to_cpu(*((uint32_t *)fc_hdr + 4));
  15999. wqe->send_frame.fc_hdr_wd5 = be32_to_cpu(*((uint32_t *)fc_hdr + 5));
  16000. iocbq->iocb.ulpCommand = CMD_SEND_FRAME;
  16001. iocbq->iocb.ulpLe = 1;
  16002. iocbq->iocb_cmpl = lpfc_sli4_mds_loopback_cmpl;
  16003. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocbq, 0);
  16004. if (rc == IOCB_ERROR)
  16005. goto exit;
  16006. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  16007. return;
  16008. exit:
  16009. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  16010. "2023 Unable to process MDS loopback frame\n");
  16011. if (pcmd && pcmd->virt)
  16012. dma_pool_free(phba->lpfc_drb_pool, pcmd->virt, pcmd->phys);
  16013. kfree(pcmd);
  16014. if (iocbq)
  16015. lpfc_sli_release_iocbq(phba, iocbq);
  16016. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  16017. }
  16018. /**
  16019. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  16020. * @phba: Pointer to HBA context object.
  16021. *
  16022. * This function is called with no lock held. This function processes all
  16023. * the received buffers and gives it to upper layers when a received buffer
  16024. * indicates that it is the final frame in the sequence. The interrupt
  16025. * service routine processes received buffers at interrupt contexts.
  16026. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  16027. * appropriate receive function when the final frame in a sequence is received.
  16028. **/
  16029. void
  16030. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  16031. struct hbq_dmabuf *dmabuf)
  16032. {
  16033. struct hbq_dmabuf *seq_dmabuf;
  16034. struct fc_frame_header *fc_hdr;
  16035. struct lpfc_vport *vport;
  16036. uint32_t fcfi;
  16037. uint32_t did;
  16038. /* Process each received buffer */
  16039. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  16040. /* check to see if this a valid type of frame */
  16041. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  16042. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  16043. return;
  16044. }
  16045. if ((bf_get(lpfc_cqe_code,
  16046. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  16047. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  16048. &dmabuf->cq_event.cqe.rcqe_cmpl);
  16049. else
  16050. fcfi = bf_get(lpfc_rcqe_fcf_id,
  16051. &dmabuf->cq_event.cqe.rcqe_cmpl);
  16052. if (fc_hdr->fh_r_ctl == 0xF4 && fc_hdr->fh_type == 0xFF) {
  16053. vport = phba->pport;
  16054. /* Handle MDS Loopback frames */
  16055. lpfc_sli4_handle_mds_loopback(vport, dmabuf);
  16056. return;
  16057. }
  16058. /* d_id this frame is directed to */
  16059. did = sli4_did_from_fc_hdr(fc_hdr);
  16060. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi, did);
  16061. if (!vport) {
  16062. /* throw out the frame */
  16063. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  16064. return;
  16065. }
  16066. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  16067. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  16068. (did != Fabric_DID)) {
  16069. /*
  16070. * Throw out the frame if we are not pt2pt.
  16071. * The pt2pt protocol allows for discovery frames
  16072. * to be received without a registered VPI.
  16073. */
  16074. if (!(vport->fc_flag & FC_PT2PT) ||
  16075. (phba->link_state == LPFC_HBA_READY)) {
  16076. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  16077. return;
  16078. }
  16079. }
  16080. /* Handle the basic abort sequence (BA_ABTS) event */
  16081. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  16082. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  16083. return;
  16084. }
  16085. /* Link this frame */
  16086. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  16087. if (!seq_dmabuf) {
  16088. /* unable to add frame to vport - throw it out */
  16089. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  16090. return;
  16091. }
  16092. /* If not last frame in sequence continue processing frames. */
  16093. if (!lpfc_seq_complete(seq_dmabuf))
  16094. return;
  16095. /* Send the complete sequence to the upper layer protocol */
  16096. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  16097. }
  16098. /**
  16099. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  16100. * @phba: pointer to lpfc hba data structure.
  16101. *
  16102. * This routine is invoked to post rpi header templates to the
  16103. * HBA consistent with the SLI-4 interface spec. This routine
  16104. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  16105. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  16106. *
  16107. * This routine does not require any locks. It's usage is expected
  16108. * to be driver load or reset recovery when the driver is
  16109. * sequential.
  16110. *
  16111. * Return codes
  16112. * 0 - successful
  16113. * -EIO - The mailbox failed to complete successfully.
  16114. * When this error occurs, the driver is not guaranteed
  16115. * to have any rpi regions posted to the device and
  16116. * must either attempt to repost the regions or take a
  16117. * fatal error.
  16118. **/
  16119. int
  16120. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  16121. {
  16122. struct lpfc_rpi_hdr *rpi_page;
  16123. uint32_t rc = 0;
  16124. uint16_t lrpi = 0;
  16125. /* SLI4 ports that support extents do not require RPI headers. */
  16126. if (!phba->sli4_hba.rpi_hdrs_in_use)
  16127. goto exit;
  16128. if (phba->sli4_hba.extents_in_use)
  16129. return -EIO;
  16130. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  16131. /*
  16132. * Assign the rpi headers a physical rpi only if the driver
  16133. * has not initialized those resources. A port reset only
  16134. * needs the headers posted.
  16135. */
  16136. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  16137. LPFC_RPI_RSRC_RDY)
  16138. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  16139. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  16140. if (rc != MBX_SUCCESS) {
  16141. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  16142. "2008 Error %d posting all rpi "
  16143. "headers\n", rc);
  16144. rc = -EIO;
  16145. break;
  16146. }
  16147. }
  16148. exit:
  16149. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  16150. LPFC_RPI_RSRC_RDY);
  16151. return rc;
  16152. }
  16153. /**
  16154. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  16155. * @phba: pointer to lpfc hba data structure.
  16156. * @rpi_page: pointer to the rpi memory region.
  16157. *
  16158. * This routine is invoked to post a single rpi header to the
  16159. * HBA consistent with the SLI-4 interface spec. This memory region
  16160. * maps up to 64 rpi context regions.
  16161. *
  16162. * Return codes
  16163. * 0 - successful
  16164. * -ENOMEM - No available memory
  16165. * -EIO - The mailbox failed to complete successfully.
  16166. **/
  16167. int
  16168. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  16169. {
  16170. LPFC_MBOXQ_t *mboxq;
  16171. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  16172. uint32_t rc = 0;
  16173. uint32_t shdr_status, shdr_add_status;
  16174. union lpfc_sli4_cfg_shdr *shdr;
  16175. /* SLI4 ports that support extents do not require RPI headers. */
  16176. if (!phba->sli4_hba.rpi_hdrs_in_use)
  16177. return rc;
  16178. if (phba->sli4_hba.extents_in_use)
  16179. return -EIO;
  16180. /* The port is notified of the header region via a mailbox command. */
  16181. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16182. if (!mboxq) {
  16183. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  16184. "2001 Unable to allocate memory for issuing "
  16185. "SLI_CONFIG_SPECIAL mailbox command\n");
  16186. return -ENOMEM;
  16187. }
  16188. /* Post all rpi memory regions to the port. */
  16189. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  16190. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  16191. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  16192. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  16193. sizeof(struct lpfc_sli4_cfg_mhdr),
  16194. LPFC_SLI4_MBX_EMBED);
  16195. /* Post the physical rpi to the port for this rpi header. */
  16196. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  16197. rpi_page->start_rpi);
  16198. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  16199. hdr_tmpl, rpi_page->page_count);
  16200. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  16201. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  16202. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  16203. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  16204. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  16205. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  16206. if (rc != MBX_TIMEOUT)
  16207. mempool_free(mboxq, phba->mbox_mem_pool);
  16208. if (shdr_status || shdr_add_status || rc) {
  16209. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  16210. "2514 POST_RPI_HDR mailbox failed with "
  16211. "status x%x add_status x%x, mbx status x%x\n",
  16212. shdr_status, shdr_add_status, rc);
  16213. rc = -ENXIO;
  16214. } else {
  16215. /*
  16216. * The next_rpi stores the next logical module-64 rpi value used
  16217. * to post physical rpis in subsequent rpi postings.
  16218. */
  16219. spin_lock_irq(&phba->hbalock);
  16220. phba->sli4_hba.next_rpi = rpi_page->next_rpi;
  16221. spin_unlock_irq(&phba->hbalock);
  16222. }
  16223. return rc;
  16224. }
  16225. /**
  16226. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  16227. * @phba: pointer to lpfc hba data structure.
  16228. *
  16229. * This routine is invoked to post rpi header templates to the
  16230. * HBA consistent with the SLI-4 interface spec. This routine
  16231. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  16232. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  16233. *
  16234. * Returns
  16235. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  16236. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  16237. **/
  16238. int
  16239. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  16240. {
  16241. unsigned long rpi;
  16242. uint16_t max_rpi, rpi_limit;
  16243. uint16_t rpi_remaining, lrpi = 0;
  16244. struct lpfc_rpi_hdr *rpi_hdr;
  16245. unsigned long iflag;
  16246. /*
  16247. * Fetch the next logical rpi. Because this index is logical,
  16248. * the driver starts at 0 each time.
  16249. */
  16250. spin_lock_irqsave(&phba->hbalock, iflag);
  16251. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  16252. rpi_limit = phba->sli4_hba.next_rpi;
  16253. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  16254. if (rpi >= rpi_limit)
  16255. rpi = LPFC_RPI_ALLOC_ERROR;
  16256. else {
  16257. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  16258. phba->sli4_hba.max_cfg_param.rpi_used++;
  16259. phba->sli4_hba.rpi_count++;
  16260. }
  16261. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  16262. "0001 rpi:%x max:%x lim:%x\n",
  16263. (int) rpi, max_rpi, rpi_limit);
  16264. /*
  16265. * Don't try to allocate more rpi header regions if the device limit
  16266. * has been exhausted.
  16267. */
  16268. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  16269. (phba->sli4_hba.rpi_count >= max_rpi)) {
  16270. spin_unlock_irqrestore(&phba->hbalock, iflag);
  16271. return rpi;
  16272. }
  16273. /*
  16274. * RPI header postings are not required for SLI4 ports capable of
  16275. * extents.
  16276. */
  16277. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  16278. spin_unlock_irqrestore(&phba->hbalock, iflag);
  16279. return rpi;
  16280. }
  16281. /*
  16282. * If the driver is running low on rpi resources, allocate another
  16283. * page now. Note that the next_rpi value is used because
  16284. * it represents how many are actually in use whereas max_rpi notes
  16285. * how many are supported max by the device.
  16286. */
  16287. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  16288. spin_unlock_irqrestore(&phba->hbalock, iflag);
  16289. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  16290. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  16291. if (!rpi_hdr) {
  16292. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  16293. "2002 Error Could not grow rpi "
  16294. "count\n");
  16295. } else {
  16296. lrpi = rpi_hdr->start_rpi;
  16297. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  16298. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  16299. }
  16300. }
  16301. return rpi;
  16302. }
  16303. /**
  16304. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  16305. * @phba: pointer to lpfc hba data structure.
  16306. *
  16307. * This routine is invoked to release an rpi to the pool of
  16308. * available rpis maintained by the driver.
  16309. **/
  16310. static void
  16311. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  16312. {
  16313. /*
  16314. * if the rpi value indicates a prior unreg has already
  16315. * been done, skip the unreg.
  16316. */
  16317. if (rpi == LPFC_RPI_ALLOC_ERROR)
  16318. return;
  16319. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  16320. phba->sli4_hba.rpi_count--;
  16321. phba->sli4_hba.max_cfg_param.rpi_used--;
  16322. }
  16323. }
  16324. /**
  16325. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  16326. * @phba: pointer to lpfc hba data structure.
  16327. *
  16328. * This routine is invoked to release an rpi to the pool of
  16329. * available rpis maintained by the driver.
  16330. **/
  16331. void
  16332. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  16333. {
  16334. spin_lock_irq(&phba->hbalock);
  16335. __lpfc_sli4_free_rpi(phba, rpi);
  16336. spin_unlock_irq(&phba->hbalock);
  16337. }
  16338. /**
  16339. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  16340. * @phba: pointer to lpfc hba data structure.
  16341. *
  16342. * This routine is invoked to remove the memory region that
  16343. * provided rpi via a bitmask.
  16344. **/
  16345. void
  16346. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  16347. {
  16348. kfree(phba->sli4_hba.rpi_bmask);
  16349. kfree(phba->sli4_hba.rpi_ids);
  16350. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  16351. }
  16352. /**
  16353. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  16354. * @phba: pointer to lpfc hba data structure.
  16355. *
  16356. * This routine is invoked to remove the memory region that
  16357. * provided rpi via a bitmask.
  16358. **/
  16359. int
  16360. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  16361. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  16362. {
  16363. LPFC_MBOXQ_t *mboxq;
  16364. struct lpfc_hba *phba = ndlp->phba;
  16365. int rc;
  16366. /* The port is notified of the header region via a mailbox command. */
  16367. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16368. if (!mboxq)
  16369. return -ENOMEM;
  16370. /* Post all rpi memory regions to the port. */
  16371. lpfc_resume_rpi(mboxq, ndlp);
  16372. if (cmpl) {
  16373. mboxq->mbox_cmpl = cmpl;
  16374. mboxq->context1 = arg;
  16375. mboxq->context2 = ndlp;
  16376. } else
  16377. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  16378. mboxq->vport = ndlp->vport;
  16379. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  16380. if (rc == MBX_NOT_FINISHED) {
  16381. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  16382. "2010 Resume RPI Mailbox failed "
  16383. "status %d, mbxStatus x%x\n", rc,
  16384. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  16385. mempool_free(mboxq, phba->mbox_mem_pool);
  16386. return -EIO;
  16387. }
  16388. return 0;
  16389. }
  16390. /**
  16391. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  16392. * @vport: Pointer to the vport for which the vpi is being initialized
  16393. *
  16394. * This routine is invoked to activate a vpi with the port.
  16395. *
  16396. * Returns:
  16397. * 0 success
  16398. * -Evalue otherwise
  16399. **/
  16400. int
  16401. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  16402. {
  16403. LPFC_MBOXQ_t *mboxq;
  16404. int rc = 0;
  16405. int retval = MBX_SUCCESS;
  16406. uint32_t mbox_tmo;
  16407. struct lpfc_hba *phba = vport->phba;
  16408. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16409. if (!mboxq)
  16410. return -ENOMEM;
  16411. lpfc_init_vpi(phba, mboxq, vport->vpi);
  16412. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  16413. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  16414. if (rc != MBX_SUCCESS) {
  16415. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  16416. "2022 INIT VPI Mailbox failed "
  16417. "status %d, mbxStatus x%x\n", rc,
  16418. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  16419. retval = -EIO;
  16420. }
  16421. if (rc != MBX_TIMEOUT)
  16422. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  16423. return retval;
  16424. }
  16425. /**
  16426. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  16427. * @phba: pointer to lpfc hba data structure.
  16428. * @mboxq: Pointer to mailbox object.
  16429. *
  16430. * This routine is invoked to manually add a single FCF record. The caller
  16431. * must pass a completely initialized FCF_Record. This routine takes
  16432. * care of the nonembedded mailbox operations.
  16433. **/
  16434. static void
  16435. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  16436. {
  16437. void *virt_addr;
  16438. union lpfc_sli4_cfg_shdr *shdr;
  16439. uint32_t shdr_status, shdr_add_status;
  16440. virt_addr = mboxq->sge_array->addr[0];
  16441. /* The IOCTL status is embedded in the mailbox subheader. */
  16442. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  16443. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  16444. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  16445. if ((shdr_status || shdr_add_status) &&
  16446. (shdr_status != STATUS_FCF_IN_USE))
  16447. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  16448. "2558 ADD_FCF_RECORD mailbox failed with "
  16449. "status x%x add_status x%x\n",
  16450. shdr_status, shdr_add_status);
  16451. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  16452. }
  16453. /**
  16454. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  16455. * @phba: pointer to lpfc hba data structure.
  16456. * @fcf_record: pointer to the initialized fcf record to add.
  16457. *
  16458. * This routine is invoked to manually add a single FCF record. The caller
  16459. * must pass a completely initialized FCF_Record. This routine takes
  16460. * care of the nonembedded mailbox operations.
  16461. **/
  16462. int
  16463. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  16464. {
  16465. int rc = 0;
  16466. LPFC_MBOXQ_t *mboxq;
  16467. uint8_t *bytep;
  16468. void *virt_addr;
  16469. struct lpfc_mbx_sge sge;
  16470. uint32_t alloc_len, req_len;
  16471. uint32_t fcfindex;
  16472. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16473. if (!mboxq) {
  16474. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  16475. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  16476. return -ENOMEM;
  16477. }
  16478. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  16479. sizeof(uint32_t);
  16480. /* Allocate DMA memory and set up the non-embedded mailbox command */
  16481. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  16482. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  16483. req_len, LPFC_SLI4_MBX_NEMBED);
  16484. if (alloc_len < req_len) {
  16485. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  16486. "2523 Allocated DMA memory size (x%x) is "
  16487. "less than the requested DMA memory "
  16488. "size (x%x)\n", alloc_len, req_len);
  16489. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  16490. return -ENOMEM;
  16491. }
  16492. /*
  16493. * Get the first SGE entry from the non-embedded DMA memory. This
  16494. * routine only uses a single SGE.
  16495. */
  16496. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  16497. virt_addr = mboxq->sge_array->addr[0];
  16498. /*
  16499. * Configure the FCF record for FCFI 0. This is the driver's
  16500. * hardcoded default and gets used in nonFIP mode.
  16501. */
  16502. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  16503. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  16504. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  16505. /*
  16506. * Copy the fcf_index and the FCF Record Data. The data starts after
  16507. * the FCoE header plus word10. The data copy needs to be endian
  16508. * correct.
  16509. */
  16510. bytep += sizeof(uint32_t);
  16511. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  16512. mboxq->vport = phba->pport;
  16513. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  16514. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  16515. if (rc == MBX_NOT_FINISHED) {
  16516. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  16517. "2515 ADD_FCF_RECORD mailbox failed with "
  16518. "status 0x%x\n", rc);
  16519. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  16520. rc = -EIO;
  16521. } else
  16522. rc = 0;
  16523. return rc;
  16524. }
  16525. /**
  16526. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  16527. * @phba: pointer to lpfc hba data structure.
  16528. * @fcf_record: pointer to the fcf record to write the default data.
  16529. * @fcf_index: FCF table entry index.
  16530. *
  16531. * This routine is invoked to build the driver's default FCF record. The
  16532. * values used are hardcoded. This routine handles memory initialization.
  16533. *
  16534. **/
  16535. void
  16536. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  16537. struct fcf_record *fcf_record,
  16538. uint16_t fcf_index)
  16539. {
  16540. memset(fcf_record, 0, sizeof(struct fcf_record));
  16541. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  16542. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  16543. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  16544. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  16545. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  16546. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  16547. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  16548. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  16549. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  16550. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  16551. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  16552. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  16553. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  16554. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  16555. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  16556. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  16557. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  16558. /* Set the VLAN bit map */
  16559. if (phba->valid_vlan) {
  16560. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  16561. = 1 << (phba->vlan_id % 8);
  16562. }
  16563. }
  16564. /**
  16565. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  16566. * @phba: pointer to lpfc hba data structure.
  16567. * @fcf_index: FCF table entry offset.
  16568. *
  16569. * This routine is invoked to scan the entire FCF table by reading FCF
  16570. * record and processing it one at a time starting from the @fcf_index
  16571. * for initial FCF discovery or fast FCF failover rediscovery.
  16572. *
  16573. * Return 0 if the mailbox command is submitted successfully, none 0
  16574. * otherwise.
  16575. **/
  16576. int
  16577. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  16578. {
  16579. int rc = 0, error;
  16580. LPFC_MBOXQ_t *mboxq;
  16581. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  16582. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  16583. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16584. if (!mboxq) {
  16585. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  16586. "2000 Failed to allocate mbox for "
  16587. "READ_FCF cmd\n");
  16588. error = -ENOMEM;
  16589. goto fail_fcf_scan;
  16590. }
  16591. /* Construct the read FCF record mailbox command */
  16592. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  16593. if (rc) {
  16594. error = -EINVAL;
  16595. goto fail_fcf_scan;
  16596. }
  16597. /* Issue the mailbox command asynchronously */
  16598. mboxq->vport = phba->pport;
  16599. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  16600. spin_lock_irq(&phba->hbalock);
  16601. phba->hba_flag |= FCF_TS_INPROG;
  16602. spin_unlock_irq(&phba->hbalock);
  16603. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  16604. if (rc == MBX_NOT_FINISHED)
  16605. error = -EIO;
  16606. else {
  16607. /* Reset eligible FCF count for new scan */
  16608. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  16609. phba->fcf.eligible_fcf_cnt = 0;
  16610. error = 0;
  16611. }
  16612. fail_fcf_scan:
  16613. if (error) {
  16614. if (mboxq)
  16615. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  16616. /* FCF scan failed, clear FCF_TS_INPROG flag */
  16617. spin_lock_irq(&phba->hbalock);
  16618. phba->hba_flag &= ~FCF_TS_INPROG;
  16619. spin_unlock_irq(&phba->hbalock);
  16620. }
  16621. return error;
  16622. }
  16623. /**
  16624. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  16625. * @phba: pointer to lpfc hba data structure.
  16626. * @fcf_index: FCF table entry offset.
  16627. *
  16628. * This routine is invoked to read an FCF record indicated by @fcf_index
  16629. * and to use it for FLOGI roundrobin FCF failover.
  16630. *
  16631. * Return 0 if the mailbox command is submitted successfully, none 0
  16632. * otherwise.
  16633. **/
  16634. int
  16635. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  16636. {
  16637. int rc = 0, error;
  16638. LPFC_MBOXQ_t *mboxq;
  16639. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16640. if (!mboxq) {
  16641. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  16642. "2763 Failed to allocate mbox for "
  16643. "READ_FCF cmd\n");
  16644. error = -ENOMEM;
  16645. goto fail_fcf_read;
  16646. }
  16647. /* Construct the read FCF record mailbox command */
  16648. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  16649. if (rc) {
  16650. error = -EINVAL;
  16651. goto fail_fcf_read;
  16652. }
  16653. /* Issue the mailbox command asynchronously */
  16654. mboxq->vport = phba->pport;
  16655. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  16656. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  16657. if (rc == MBX_NOT_FINISHED)
  16658. error = -EIO;
  16659. else
  16660. error = 0;
  16661. fail_fcf_read:
  16662. if (error && mboxq)
  16663. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  16664. return error;
  16665. }
  16666. /**
  16667. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  16668. * @phba: pointer to lpfc hba data structure.
  16669. * @fcf_index: FCF table entry offset.
  16670. *
  16671. * This routine is invoked to read an FCF record indicated by @fcf_index to
  16672. * determine whether it's eligible for FLOGI roundrobin failover list.
  16673. *
  16674. * Return 0 if the mailbox command is submitted successfully, none 0
  16675. * otherwise.
  16676. **/
  16677. int
  16678. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  16679. {
  16680. int rc = 0, error;
  16681. LPFC_MBOXQ_t *mboxq;
  16682. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16683. if (!mboxq) {
  16684. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  16685. "2758 Failed to allocate mbox for "
  16686. "READ_FCF cmd\n");
  16687. error = -ENOMEM;
  16688. goto fail_fcf_read;
  16689. }
  16690. /* Construct the read FCF record mailbox command */
  16691. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  16692. if (rc) {
  16693. error = -EINVAL;
  16694. goto fail_fcf_read;
  16695. }
  16696. /* Issue the mailbox command asynchronously */
  16697. mboxq->vport = phba->pport;
  16698. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  16699. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  16700. if (rc == MBX_NOT_FINISHED)
  16701. error = -EIO;
  16702. else
  16703. error = 0;
  16704. fail_fcf_read:
  16705. if (error && mboxq)
  16706. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  16707. return error;
  16708. }
  16709. /**
  16710. * lpfc_check_next_fcf_pri_level
  16711. * phba pointer to the lpfc_hba struct for this port.
  16712. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  16713. * routine when the rr_bmask is empty. The FCF indecies are put into the
  16714. * rr_bmask based on their priority level. Starting from the highest priority
  16715. * to the lowest. The most likely FCF candidate will be in the highest
  16716. * priority group. When this routine is called it searches the fcf_pri list for
  16717. * next lowest priority group and repopulates the rr_bmask with only those
  16718. * fcf_indexes.
  16719. * returns:
  16720. * 1=success 0=failure
  16721. **/
  16722. static int
  16723. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  16724. {
  16725. uint16_t next_fcf_pri;
  16726. uint16_t last_index;
  16727. struct lpfc_fcf_pri *fcf_pri;
  16728. int rc;
  16729. int ret = 0;
  16730. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  16731. LPFC_SLI4_FCF_TBL_INDX_MAX);
  16732. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  16733. "3060 Last IDX %d\n", last_index);
  16734. /* Verify the priority list has 2 or more entries */
  16735. spin_lock_irq(&phba->hbalock);
  16736. if (list_empty(&phba->fcf.fcf_pri_list) ||
  16737. list_is_singular(&phba->fcf.fcf_pri_list)) {
  16738. spin_unlock_irq(&phba->hbalock);
  16739. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  16740. "3061 Last IDX %d\n", last_index);
  16741. return 0; /* Empty rr list */
  16742. }
  16743. spin_unlock_irq(&phba->hbalock);
  16744. next_fcf_pri = 0;
  16745. /*
  16746. * Clear the rr_bmask and set all of the bits that are at this
  16747. * priority.
  16748. */
  16749. memset(phba->fcf.fcf_rr_bmask, 0,
  16750. sizeof(*phba->fcf.fcf_rr_bmask));
  16751. spin_lock_irq(&phba->hbalock);
  16752. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  16753. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  16754. continue;
  16755. /*
  16756. * the 1st priority that has not FLOGI failed
  16757. * will be the highest.
  16758. */
  16759. if (!next_fcf_pri)
  16760. next_fcf_pri = fcf_pri->fcf_rec.priority;
  16761. spin_unlock_irq(&phba->hbalock);
  16762. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  16763. rc = lpfc_sli4_fcf_rr_index_set(phba,
  16764. fcf_pri->fcf_rec.fcf_index);
  16765. if (rc)
  16766. return 0;
  16767. }
  16768. spin_lock_irq(&phba->hbalock);
  16769. }
  16770. /*
  16771. * if next_fcf_pri was not set above and the list is not empty then
  16772. * we have failed flogis on all of them. So reset flogi failed
  16773. * and start at the beginning.
  16774. */
  16775. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  16776. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  16777. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  16778. /*
  16779. * the 1st priority that has not FLOGI failed
  16780. * will be the highest.
  16781. */
  16782. if (!next_fcf_pri)
  16783. next_fcf_pri = fcf_pri->fcf_rec.priority;
  16784. spin_unlock_irq(&phba->hbalock);
  16785. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  16786. rc = lpfc_sli4_fcf_rr_index_set(phba,
  16787. fcf_pri->fcf_rec.fcf_index);
  16788. if (rc)
  16789. return 0;
  16790. }
  16791. spin_lock_irq(&phba->hbalock);
  16792. }
  16793. } else
  16794. ret = 1;
  16795. spin_unlock_irq(&phba->hbalock);
  16796. return ret;
  16797. }
  16798. /**
  16799. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  16800. * @phba: pointer to lpfc hba data structure.
  16801. *
  16802. * This routine is to get the next eligible FCF record index in a round
  16803. * robin fashion. If the next eligible FCF record index equals to the
  16804. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  16805. * shall be returned, otherwise, the next eligible FCF record's index
  16806. * shall be returned.
  16807. **/
  16808. uint16_t
  16809. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  16810. {
  16811. uint16_t next_fcf_index;
  16812. initial_priority:
  16813. /* Search start from next bit of currently registered FCF index */
  16814. next_fcf_index = phba->fcf.current_rec.fcf_indx;
  16815. next_priority:
  16816. /* Determine the next fcf index to check */
  16817. next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
  16818. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  16819. LPFC_SLI4_FCF_TBL_INDX_MAX,
  16820. next_fcf_index);
  16821. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  16822. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  16823. /*
  16824. * If we have wrapped then we need to clear the bits that
  16825. * have been tested so that we can detect when we should
  16826. * change the priority level.
  16827. */
  16828. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  16829. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  16830. }
  16831. /* Check roundrobin failover list empty condition */
  16832. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  16833. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  16834. /*
  16835. * If next fcf index is not found check if there are lower
  16836. * Priority level fcf's in the fcf_priority list.
  16837. * Set up the rr_bmask with all of the avaiable fcf bits
  16838. * at that level and continue the selection process.
  16839. */
  16840. if (lpfc_check_next_fcf_pri_level(phba))
  16841. goto initial_priority;
  16842. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  16843. "2844 No roundrobin failover FCF available\n");
  16844. return LPFC_FCOE_FCF_NEXT_NONE;
  16845. }
  16846. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  16847. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  16848. LPFC_FCF_FLOGI_FAILED) {
  16849. if (list_is_singular(&phba->fcf.fcf_pri_list))
  16850. return LPFC_FCOE_FCF_NEXT_NONE;
  16851. goto next_priority;
  16852. }
  16853. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  16854. "2845 Get next roundrobin failover FCF (x%x)\n",
  16855. next_fcf_index);
  16856. return next_fcf_index;
  16857. }
  16858. /**
  16859. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  16860. * @phba: pointer to lpfc hba data structure.
  16861. *
  16862. * This routine sets the FCF record index in to the eligible bmask for
  16863. * roundrobin failover search. It checks to make sure that the index
  16864. * does not go beyond the range of the driver allocated bmask dimension
  16865. * before setting the bit.
  16866. *
  16867. * Returns 0 if the index bit successfully set, otherwise, it returns
  16868. * -EINVAL.
  16869. **/
  16870. int
  16871. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  16872. {
  16873. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  16874. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  16875. "2610 FCF (x%x) reached driver's book "
  16876. "keeping dimension:x%x\n",
  16877. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  16878. return -EINVAL;
  16879. }
  16880. /* Set the eligible FCF record index bmask */
  16881. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  16882. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  16883. "2790 Set FCF (x%x) to roundrobin FCF failover "
  16884. "bmask\n", fcf_index);
  16885. return 0;
  16886. }
  16887. /**
  16888. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  16889. * @phba: pointer to lpfc hba data structure.
  16890. *
  16891. * This routine clears the FCF record index from the eligible bmask for
  16892. * roundrobin failover search. It checks to make sure that the index
  16893. * does not go beyond the range of the driver allocated bmask dimension
  16894. * before clearing the bit.
  16895. **/
  16896. void
  16897. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  16898. {
  16899. struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
  16900. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  16901. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  16902. "2762 FCF (x%x) reached driver's book "
  16903. "keeping dimension:x%x\n",
  16904. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  16905. return;
  16906. }
  16907. /* Clear the eligible FCF record index bmask */
  16908. spin_lock_irq(&phba->hbalock);
  16909. list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
  16910. list) {
  16911. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  16912. list_del_init(&fcf_pri->list);
  16913. break;
  16914. }
  16915. }
  16916. spin_unlock_irq(&phba->hbalock);
  16917. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  16918. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  16919. "2791 Clear FCF (x%x) from roundrobin failover "
  16920. "bmask\n", fcf_index);
  16921. }
  16922. /**
  16923. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  16924. * @phba: pointer to lpfc hba data structure.
  16925. *
  16926. * This routine is the completion routine for the rediscover FCF table mailbox
  16927. * command. If the mailbox command returned failure, it will try to stop the
  16928. * FCF rediscover wait timer.
  16929. **/
  16930. static void
  16931. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  16932. {
  16933. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  16934. uint32_t shdr_status, shdr_add_status;
  16935. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  16936. shdr_status = bf_get(lpfc_mbox_hdr_status,
  16937. &redisc_fcf->header.cfg_shdr.response);
  16938. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  16939. &redisc_fcf->header.cfg_shdr.response);
  16940. if (shdr_status || shdr_add_status) {
  16941. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  16942. "2746 Requesting for FCF rediscovery failed "
  16943. "status x%x add_status x%x\n",
  16944. shdr_status, shdr_add_status);
  16945. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  16946. spin_lock_irq(&phba->hbalock);
  16947. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  16948. spin_unlock_irq(&phba->hbalock);
  16949. /*
  16950. * CVL event triggered FCF rediscover request failed,
  16951. * last resort to re-try current registered FCF entry.
  16952. */
  16953. lpfc_retry_pport_discovery(phba);
  16954. } else {
  16955. spin_lock_irq(&phba->hbalock);
  16956. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  16957. spin_unlock_irq(&phba->hbalock);
  16958. /*
  16959. * DEAD FCF event triggered FCF rediscover request
  16960. * failed, last resort to fail over as a link down
  16961. * to FCF registration.
  16962. */
  16963. lpfc_sli4_fcf_dead_failthrough(phba);
  16964. }
  16965. } else {
  16966. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  16967. "2775 Start FCF rediscover quiescent timer\n");
  16968. /*
  16969. * Start FCF rediscovery wait timer for pending FCF
  16970. * before rescan FCF record table.
  16971. */
  16972. lpfc_fcf_redisc_wait_start_timer(phba);
  16973. }
  16974. mempool_free(mbox, phba->mbox_mem_pool);
  16975. }
  16976. /**
  16977. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  16978. * @phba: pointer to lpfc hba data structure.
  16979. *
  16980. * This routine is invoked to request for rediscovery of the entire FCF table
  16981. * by the port.
  16982. **/
  16983. int
  16984. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  16985. {
  16986. LPFC_MBOXQ_t *mbox;
  16987. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  16988. int rc, length;
  16989. /* Cancel retry delay timers to all vports before FCF rediscover */
  16990. lpfc_cancel_all_vport_retry_delay_timer(phba);
  16991. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  16992. if (!mbox) {
  16993. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  16994. "2745 Failed to allocate mbox for "
  16995. "requesting FCF rediscover.\n");
  16996. return -ENOMEM;
  16997. }
  16998. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  16999. sizeof(struct lpfc_sli4_cfg_mhdr));
  17000. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  17001. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  17002. length, LPFC_SLI4_MBX_EMBED);
  17003. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  17004. /* Set count to 0 for invalidating the entire FCF database */
  17005. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  17006. /* Issue the mailbox command asynchronously */
  17007. mbox->vport = phba->pport;
  17008. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  17009. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  17010. if (rc == MBX_NOT_FINISHED) {
  17011. mempool_free(mbox, phba->mbox_mem_pool);
  17012. return -EIO;
  17013. }
  17014. return 0;
  17015. }
  17016. /**
  17017. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  17018. * @phba: pointer to lpfc hba data structure.
  17019. *
  17020. * This function is the failover routine as a last resort to the FCF DEAD
  17021. * event when driver failed to perform fast FCF failover.
  17022. **/
  17023. void
  17024. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  17025. {
  17026. uint32_t link_state;
  17027. /*
  17028. * Last resort as FCF DEAD event failover will treat this as
  17029. * a link down, but save the link state because we don't want
  17030. * it to be changed to Link Down unless it is already down.
  17031. */
  17032. link_state = phba->link_state;
  17033. lpfc_linkdown(phba);
  17034. phba->link_state = link_state;
  17035. /* Unregister FCF if no devices connected to it */
  17036. lpfc_unregister_unused_fcf(phba);
  17037. }
  17038. /**
  17039. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  17040. * @phba: pointer to lpfc hba data structure.
  17041. * @rgn23_data: pointer to configure region 23 data.
  17042. *
  17043. * This function gets SLI3 port configure region 23 data through memory dump
  17044. * mailbox command. When it successfully retrieves data, the size of the data
  17045. * will be returned, otherwise, 0 will be returned.
  17046. **/
  17047. static uint32_t
  17048. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  17049. {
  17050. LPFC_MBOXQ_t *pmb = NULL;
  17051. MAILBOX_t *mb;
  17052. uint32_t offset = 0;
  17053. int rc;
  17054. if (!rgn23_data)
  17055. return 0;
  17056. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  17057. if (!pmb) {
  17058. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  17059. "2600 failed to allocate mailbox memory\n");
  17060. return 0;
  17061. }
  17062. mb = &pmb->u.mb;
  17063. do {
  17064. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  17065. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  17066. if (rc != MBX_SUCCESS) {
  17067. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  17068. "2601 failed to read config "
  17069. "region 23, rc 0x%x Status 0x%x\n",
  17070. rc, mb->mbxStatus);
  17071. mb->un.varDmp.word_cnt = 0;
  17072. }
  17073. /*
  17074. * dump mem may return a zero when finished or we got a
  17075. * mailbox error, either way we are done.
  17076. */
  17077. if (mb->un.varDmp.word_cnt == 0)
  17078. break;
  17079. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  17080. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  17081. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  17082. rgn23_data + offset,
  17083. mb->un.varDmp.word_cnt);
  17084. offset += mb->un.varDmp.word_cnt;
  17085. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  17086. mempool_free(pmb, phba->mbox_mem_pool);
  17087. return offset;
  17088. }
  17089. /**
  17090. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  17091. * @phba: pointer to lpfc hba data structure.
  17092. * @rgn23_data: pointer to configure region 23 data.
  17093. *
  17094. * This function gets SLI4 port configure region 23 data through memory dump
  17095. * mailbox command. When it successfully retrieves data, the size of the data
  17096. * will be returned, otherwise, 0 will be returned.
  17097. **/
  17098. static uint32_t
  17099. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  17100. {
  17101. LPFC_MBOXQ_t *mboxq = NULL;
  17102. struct lpfc_dmabuf *mp = NULL;
  17103. struct lpfc_mqe *mqe;
  17104. uint32_t data_length = 0;
  17105. int rc;
  17106. if (!rgn23_data)
  17107. return 0;
  17108. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  17109. if (!mboxq) {
  17110. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  17111. "3105 failed to allocate mailbox memory\n");
  17112. return 0;
  17113. }
  17114. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  17115. goto out;
  17116. mqe = &mboxq->u.mqe;
  17117. mp = (struct lpfc_dmabuf *) mboxq->context1;
  17118. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  17119. if (rc)
  17120. goto out;
  17121. data_length = mqe->un.mb_words[5];
  17122. if (data_length == 0)
  17123. goto out;
  17124. if (data_length > DMP_RGN23_SIZE) {
  17125. data_length = 0;
  17126. goto out;
  17127. }
  17128. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  17129. out:
  17130. mempool_free(mboxq, phba->mbox_mem_pool);
  17131. if (mp) {
  17132. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  17133. kfree(mp);
  17134. }
  17135. return data_length;
  17136. }
  17137. /**
  17138. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  17139. * @phba: pointer to lpfc hba data structure.
  17140. *
  17141. * This function read region 23 and parse TLV for port status to
  17142. * decide if the user disaled the port. If the TLV indicates the
  17143. * port is disabled, the hba_flag is set accordingly.
  17144. **/
  17145. void
  17146. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  17147. {
  17148. uint8_t *rgn23_data = NULL;
  17149. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  17150. uint32_t offset = 0;
  17151. /* Get adapter Region 23 data */
  17152. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  17153. if (!rgn23_data)
  17154. goto out;
  17155. if (phba->sli_rev < LPFC_SLI_REV4)
  17156. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  17157. else {
  17158. if_type = bf_get(lpfc_sli_intf_if_type,
  17159. &phba->sli4_hba.sli_intf);
  17160. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  17161. goto out;
  17162. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  17163. }
  17164. if (!data_size)
  17165. goto out;
  17166. /* Check the region signature first */
  17167. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  17168. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  17169. "2619 Config region 23 has bad signature\n");
  17170. goto out;
  17171. }
  17172. offset += 4;
  17173. /* Check the data structure version */
  17174. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  17175. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  17176. "2620 Config region 23 has bad version\n");
  17177. goto out;
  17178. }
  17179. offset += 4;
  17180. /* Parse TLV entries in the region */
  17181. while (offset < data_size) {
  17182. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  17183. break;
  17184. /*
  17185. * If the TLV is not driver specific TLV or driver id is
  17186. * not linux driver id, skip the record.
  17187. */
  17188. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  17189. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  17190. (rgn23_data[offset + 3] != 0)) {
  17191. offset += rgn23_data[offset + 1] * 4 + 4;
  17192. continue;
  17193. }
  17194. /* Driver found a driver specific TLV in the config region */
  17195. sub_tlv_len = rgn23_data[offset + 1] * 4;
  17196. offset += 4;
  17197. tlv_offset = 0;
  17198. /*
  17199. * Search for configured port state sub-TLV.
  17200. */
  17201. while ((offset < data_size) &&
  17202. (tlv_offset < sub_tlv_len)) {
  17203. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  17204. offset += 4;
  17205. tlv_offset += 4;
  17206. break;
  17207. }
  17208. if (rgn23_data[offset] != PORT_STE_TYPE) {
  17209. offset += rgn23_data[offset + 1] * 4 + 4;
  17210. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  17211. continue;
  17212. }
  17213. /* This HBA contains PORT_STE configured */
  17214. if (!rgn23_data[offset + 2])
  17215. phba->hba_flag |= LINK_DISABLED;
  17216. goto out;
  17217. }
  17218. }
  17219. out:
  17220. kfree(rgn23_data);
  17221. return;
  17222. }
  17223. /**
  17224. * lpfc_wr_object - write an object to the firmware
  17225. * @phba: HBA structure that indicates port to create a queue on.
  17226. * @dmabuf_list: list of dmabufs to write to the port.
  17227. * @size: the total byte value of the objects to write to the port.
  17228. * @offset: the current offset to be used to start the transfer.
  17229. *
  17230. * This routine will create a wr_object mailbox command to send to the port.
  17231. * the mailbox command will be constructed using the dma buffers described in
  17232. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  17233. * BDEs that the imbedded mailbox can support. The @offset variable will be
  17234. * used to indicate the starting offset of the transfer and will also return
  17235. * the offset after the write object mailbox has completed. @size is used to
  17236. * determine the end of the object and whether the eof bit should be set.
  17237. *
  17238. * Return 0 is successful and offset will contain the the new offset to use
  17239. * for the next write.
  17240. * Return negative value for error cases.
  17241. **/
  17242. int
  17243. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  17244. uint32_t size, uint32_t *offset)
  17245. {
  17246. struct lpfc_mbx_wr_object *wr_object;
  17247. LPFC_MBOXQ_t *mbox;
  17248. int rc = 0, i = 0;
  17249. uint32_t shdr_status, shdr_add_status;
  17250. uint32_t mbox_tmo;
  17251. union lpfc_sli4_cfg_shdr *shdr;
  17252. struct lpfc_dmabuf *dmabuf;
  17253. uint32_t written = 0;
  17254. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  17255. if (!mbox)
  17256. return -ENOMEM;
  17257. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  17258. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  17259. sizeof(struct lpfc_mbx_wr_object) -
  17260. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  17261. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  17262. wr_object->u.request.write_offset = *offset;
  17263. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  17264. wr_object->u.request.object_name[0] =
  17265. cpu_to_le32(wr_object->u.request.object_name[0]);
  17266. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  17267. list_for_each_entry(dmabuf, dmabuf_list, list) {
  17268. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  17269. break;
  17270. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  17271. wr_object->u.request.bde[i].addrHigh =
  17272. putPaddrHigh(dmabuf->phys);
  17273. if (written + SLI4_PAGE_SIZE >= size) {
  17274. wr_object->u.request.bde[i].tus.f.bdeSize =
  17275. (size - written);
  17276. written += (size - written);
  17277. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  17278. } else {
  17279. wr_object->u.request.bde[i].tus.f.bdeSize =
  17280. SLI4_PAGE_SIZE;
  17281. written += SLI4_PAGE_SIZE;
  17282. }
  17283. i++;
  17284. }
  17285. wr_object->u.request.bde_count = i;
  17286. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  17287. if (!phba->sli4_hba.intr_enable)
  17288. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  17289. else {
  17290. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  17291. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  17292. }
  17293. /* The IOCTL status is embedded in the mailbox subheader. */
  17294. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  17295. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  17296. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  17297. if (rc != MBX_TIMEOUT)
  17298. mempool_free(mbox, phba->mbox_mem_pool);
  17299. if (shdr_status || shdr_add_status || rc) {
  17300. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  17301. "3025 Write Object mailbox failed with "
  17302. "status x%x add_status x%x, mbx status x%x\n",
  17303. shdr_status, shdr_add_status, rc);
  17304. rc = -ENXIO;
  17305. *offset = shdr_add_status;
  17306. } else
  17307. *offset += wr_object->u.response.actual_write_length;
  17308. return rc;
  17309. }
  17310. /**
  17311. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  17312. * @vport: pointer to vport data structure.
  17313. *
  17314. * This function iterate through the mailboxq and clean up all REG_LOGIN
  17315. * and REG_VPI mailbox commands associated with the vport. This function
  17316. * is called when driver want to restart discovery of the vport due to
  17317. * a Clear Virtual Link event.
  17318. **/
  17319. void
  17320. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  17321. {
  17322. struct lpfc_hba *phba = vport->phba;
  17323. LPFC_MBOXQ_t *mb, *nextmb;
  17324. struct lpfc_dmabuf *mp;
  17325. struct lpfc_nodelist *ndlp;
  17326. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  17327. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  17328. LIST_HEAD(mbox_cmd_list);
  17329. uint8_t restart_loop;
  17330. /* Clean up internally queued mailbox commands with the vport */
  17331. spin_lock_irq(&phba->hbalock);
  17332. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  17333. if (mb->vport != vport)
  17334. continue;
  17335. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  17336. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  17337. continue;
  17338. list_del(&mb->list);
  17339. list_add_tail(&mb->list, &mbox_cmd_list);
  17340. }
  17341. /* Clean up active mailbox command with the vport */
  17342. mb = phba->sli.mbox_active;
  17343. if (mb && (mb->vport == vport)) {
  17344. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  17345. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  17346. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  17347. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  17348. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  17349. /* Put reference count for delayed processing */
  17350. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  17351. /* Unregister the RPI when mailbox complete */
  17352. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  17353. }
  17354. }
  17355. /* Cleanup any mailbox completions which are not yet processed */
  17356. do {
  17357. restart_loop = 0;
  17358. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  17359. /*
  17360. * If this mailox is already processed or it is
  17361. * for another vport ignore it.
  17362. */
  17363. if ((mb->vport != vport) ||
  17364. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  17365. continue;
  17366. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  17367. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  17368. continue;
  17369. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  17370. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  17371. ndlp = (struct lpfc_nodelist *)mb->context2;
  17372. /* Unregister the RPI when mailbox complete */
  17373. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  17374. restart_loop = 1;
  17375. spin_unlock_irq(&phba->hbalock);
  17376. spin_lock(shost->host_lock);
  17377. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  17378. spin_unlock(shost->host_lock);
  17379. spin_lock_irq(&phba->hbalock);
  17380. break;
  17381. }
  17382. }
  17383. } while (restart_loop);
  17384. spin_unlock_irq(&phba->hbalock);
  17385. /* Release the cleaned-up mailbox commands */
  17386. while (!list_empty(&mbox_cmd_list)) {
  17387. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  17388. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  17389. mp = (struct lpfc_dmabuf *) (mb->context1);
  17390. if (mp) {
  17391. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  17392. kfree(mp);
  17393. }
  17394. ndlp = (struct lpfc_nodelist *) mb->context2;
  17395. mb->context2 = NULL;
  17396. if (ndlp) {
  17397. spin_lock(shost->host_lock);
  17398. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  17399. spin_unlock(shost->host_lock);
  17400. lpfc_nlp_put(ndlp);
  17401. }
  17402. }
  17403. mempool_free(mb, phba->mbox_mem_pool);
  17404. }
  17405. /* Release the ndlp with the cleaned-up active mailbox command */
  17406. if (act_mbx_ndlp) {
  17407. spin_lock(shost->host_lock);
  17408. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  17409. spin_unlock(shost->host_lock);
  17410. lpfc_nlp_put(act_mbx_ndlp);
  17411. }
  17412. }
  17413. /**
  17414. * lpfc_drain_txq - Drain the txq
  17415. * @phba: Pointer to HBA context object.
  17416. *
  17417. * This function attempt to submit IOCBs on the txq
  17418. * to the adapter. For SLI4 adapters, the txq contains
  17419. * ELS IOCBs that have been deferred because the there
  17420. * are no SGLs. This congestion can occur with large
  17421. * vport counts during node discovery.
  17422. **/
  17423. uint32_t
  17424. lpfc_drain_txq(struct lpfc_hba *phba)
  17425. {
  17426. LIST_HEAD(completions);
  17427. struct lpfc_sli_ring *pring;
  17428. struct lpfc_iocbq *piocbq = NULL;
  17429. unsigned long iflags = 0;
  17430. char *fail_msg = NULL;
  17431. struct lpfc_sglq *sglq;
  17432. union lpfc_wqe128 wqe;
  17433. uint32_t txq_cnt = 0;
  17434. struct lpfc_queue *wq;
  17435. if (phba->link_flag & LS_MDS_LOOPBACK) {
  17436. /* MDS WQE are posted only to first WQ*/
  17437. wq = phba->sli4_hba.fcp_wq[0];
  17438. if (unlikely(!wq))
  17439. return 0;
  17440. pring = wq->pring;
  17441. } else {
  17442. wq = phba->sli4_hba.els_wq;
  17443. if (unlikely(!wq))
  17444. return 0;
  17445. pring = lpfc_phba_elsring(phba);
  17446. }
  17447. if (unlikely(!pring) || list_empty(&pring->txq))
  17448. return 0;
  17449. spin_lock_irqsave(&pring->ring_lock, iflags);
  17450. list_for_each_entry(piocbq, &pring->txq, list) {
  17451. txq_cnt++;
  17452. }
  17453. if (txq_cnt > pring->txq_max)
  17454. pring->txq_max = txq_cnt;
  17455. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17456. while (!list_empty(&pring->txq)) {
  17457. spin_lock_irqsave(&pring->ring_lock, iflags);
  17458. piocbq = lpfc_sli_ringtx_get(phba, pring);
  17459. if (!piocbq) {
  17460. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17461. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  17462. "2823 txq empty and txq_cnt is %d\n ",
  17463. txq_cnt);
  17464. break;
  17465. }
  17466. sglq = __lpfc_sli_get_els_sglq(phba, piocbq);
  17467. if (!sglq) {
  17468. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  17469. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17470. break;
  17471. }
  17472. txq_cnt--;
  17473. /* The xri and iocb resources secured,
  17474. * attempt to issue request
  17475. */
  17476. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  17477. piocbq->sli4_xritag = sglq->sli4_xritag;
  17478. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  17479. fail_msg = "to convert bpl to sgl";
  17480. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  17481. fail_msg = "to convert iocb to wqe";
  17482. else if (lpfc_sli4_wq_put(wq, &wqe))
  17483. fail_msg = " - Wq is full";
  17484. else
  17485. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  17486. if (fail_msg) {
  17487. /* Failed means we can't issue and need to cancel */
  17488. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  17489. "2822 IOCB failed %s iotag 0x%x "
  17490. "xri 0x%x\n",
  17491. fail_msg,
  17492. piocbq->iotag, piocbq->sli4_xritag);
  17493. list_add_tail(&piocbq->list, &completions);
  17494. }
  17495. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17496. }
  17497. /* Cancel all the IOCBs that cannot be issued */
  17498. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  17499. IOERR_SLI_ABORTED);
  17500. return txq_cnt;
  17501. }
  17502. /**
  17503. * lpfc_wqe_bpl2sgl - Convert the bpl/bde to a sgl.
  17504. * @phba: Pointer to HBA context object.
  17505. * @pwqe: Pointer to command WQE.
  17506. * @sglq: Pointer to the scatter gather queue object.
  17507. *
  17508. * This routine converts the bpl or bde that is in the WQE
  17509. * to a sgl list for the sli4 hardware. The physical address
  17510. * of the bpl/bde is converted back to a virtual address.
  17511. * If the WQE contains a BPL then the list of BDE's is
  17512. * converted to sli4_sge's. If the WQE contains a single
  17513. * BDE then it is converted to a single sli_sge.
  17514. * The WQE is still in cpu endianness so the contents of
  17515. * the bpl can be used without byte swapping.
  17516. *
  17517. * Returns valid XRI = Success, NO_XRI = Failure.
  17518. */
  17519. static uint16_t
  17520. lpfc_wqe_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeq,
  17521. struct lpfc_sglq *sglq)
  17522. {
  17523. uint16_t xritag = NO_XRI;
  17524. struct ulp_bde64 *bpl = NULL;
  17525. struct ulp_bde64 bde;
  17526. struct sli4_sge *sgl = NULL;
  17527. struct lpfc_dmabuf *dmabuf;
  17528. union lpfc_wqe128 *wqe;
  17529. int numBdes = 0;
  17530. int i = 0;
  17531. uint32_t offset = 0; /* accumulated offset in the sg request list */
  17532. int inbound = 0; /* number of sg reply entries inbound from firmware */
  17533. uint32_t cmd;
  17534. if (!pwqeq || !sglq)
  17535. return xritag;
  17536. sgl = (struct sli4_sge *)sglq->sgl;
  17537. wqe = &pwqeq->wqe;
  17538. pwqeq->iocb.ulpIoTag = pwqeq->iotag;
  17539. cmd = bf_get(wqe_cmnd, &wqe->generic.wqe_com);
  17540. if (cmd == CMD_XMIT_BLS_RSP64_WQE)
  17541. return sglq->sli4_xritag;
  17542. numBdes = pwqeq->rsvd2;
  17543. if (numBdes) {
  17544. /* The addrHigh and addrLow fields within the WQE
  17545. * have not been byteswapped yet so there is no
  17546. * need to swap them back.
  17547. */
  17548. if (pwqeq->context3)
  17549. dmabuf = (struct lpfc_dmabuf *)pwqeq->context3;
  17550. else
  17551. return xritag;
  17552. bpl = (struct ulp_bde64 *)dmabuf->virt;
  17553. if (!bpl)
  17554. return xritag;
  17555. for (i = 0; i < numBdes; i++) {
  17556. /* Should already be byte swapped. */
  17557. sgl->addr_hi = bpl->addrHigh;
  17558. sgl->addr_lo = bpl->addrLow;
  17559. sgl->word2 = le32_to_cpu(sgl->word2);
  17560. if ((i+1) == numBdes)
  17561. bf_set(lpfc_sli4_sge_last, sgl, 1);
  17562. else
  17563. bf_set(lpfc_sli4_sge_last, sgl, 0);
  17564. /* swap the size field back to the cpu so we
  17565. * can assign it to the sgl.
  17566. */
  17567. bde.tus.w = le32_to_cpu(bpl->tus.w);
  17568. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  17569. /* The offsets in the sgl need to be accumulated
  17570. * separately for the request and reply lists.
  17571. * The request is always first, the reply follows.
  17572. */
  17573. switch (cmd) {
  17574. case CMD_GEN_REQUEST64_WQE:
  17575. /* add up the reply sg entries */
  17576. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  17577. inbound++;
  17578. /* first inbound? reset the offset */
  17579. if (inbound == 1)
  17580. offset = 0;
  17581. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  17582. bf_set(lpfc_sli4_sge_type, sgl,
  17583. LPFC_SGE_TYPE_DATA);
  17584. offset += bde.tus.f.bdeSize;
  17585. break;
  17586. case CMD_FCP_TRSP64_WQE:
  17587. bf_set(lpfc_sli4_sge_offset, sgl, 0);
  17588. bf_set(lpfc_sli4_sge_type, sgl,
  17589. LPFC_SGE_TYPE_DATA);
  17590. break;
  17591. case CMD_FCP_TSEND64_WQE:
  17592. case CMD_FCP_TRECEIVE64_WQE:
  17593. bf_set(lpfc_sli4_sge_type, sgl,
  17594. bpl->tus.f.bdeFlags);
  17595. if (i < 3)
  17596. offset = 0;
  17597. else
  17598. offset += bde.tus.f.bdeSize;
  17599. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  17600. break;
  17601. }
  17602. sgl->word2 = cpu_to_le32(sgl->word2);
  17603. bpl++;
  17604. sgl++;
  17605. }
  17606. } else if (wqe->gen_req.bde.tus.f.bdeFlags == BUFF_TYPE_BDE_64) {
  17607. /* The addrHigh and addrLow fields of the BDE have not
  17608. * been byteswapped yet so they need to be swapped
  17609. * before putting them in the sgl.
  17610. */
  17611. sgl->addr_hi = cpu_to_le32(wqe->gen_req.bde.addrHigh);
  17612. sgl->addr_lo = cpu_to_le32(wqe->gen_req.bde.addrLow);
  17613. sgl->word2 = le32_to_cpu(sgl->word2);
  17614. bf_set(lpfc_sli4_sge_last, sgl, 1);
  17615. sgl->word2 = cpu_to_le32(sgl->word2);
  17616. sgl->sge_len = cpu_to_le32(wqe->gen_req.bde.tus.f.bdeSize);
  17617. }
  17618. return sglq->sli4_xritag;
  17619. }
  17620. /**
  17621. * lpfc_sli4_issue_wqe - Issue an SLI4 Work Queue Entry (WQE)
  17622. * @phba: Pointer to HBA context object.
  17623. * @ring_number: Base sli ring number
  17624. * @pwqe: Pointer to command WQE.
  17625. **/
  17626. int
  17627. lpfc_sli4_issue_wqe(struct lpfc_hba *phba, uint32_t ring_number,
  17628. struct lpfc_iocbq *pwqe)
  17629. {
  17630. union lpfc_wqe128 *wqe = &pwqe->wqe;
  17631. struct lpfc_nvmet_rcv_ctx *ctxp;
  17632. struct lpfc_queue *wq;
  17633. struct lpfc_sglq *sglq;
  17634. struct lpfc_sli_ring *pring;
  17635. unsigned long iflags;
  17636. uint32_t ret = 0;
  17637. /* NVME_LS and NVME_LS ABTS requests. */
  17638. if (pwqe->iocb_flag & LPFC_IO_NVME_LS) {
  17639. pring = phba->sli4_hba.nvmels_wq->pring;
  17640. spin_lock_irqsave(&pring->ring_lock, iflags);
  17641. sglq = __lpfc_sli_get_els_sglq(phba, pwqe);
  17642. if (!sglq) {
  17643. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17644. return WQE_BUSY;
  17645. }
  17646. pwqe->sli4_lxritag = sglq->sli4_lxritag;
  17647. pwqe->sli4_xritag = sglq->sli4_xritag;
  17648. if (lpfc_wqe_bpl2sgl(phba, pwqe, sglq) == NO_XRI) {
  17649. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17650. return WQE_ERROR;
  17651. }
  17652. bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
  17653. pwqe->sli4_xritag);
  17654. ret = lpfc_sli4_wq_put(phba->sli4_hba.nvmels_wq, wqe);
  17655. if (ret) {
  17656. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17657. return ret;
  17658. }
  17659. lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
  17660. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17661. return 0;
  17662. }
  17663. /* NVME_FCREQ and NVME_ABTS requests */
  17664. if (pwqe->iocb_flag & LPFC_IO_NVME) {
  17665. /* Get the IO distribution (hba_wqidx) for WQ assignment. */
  17666. pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
  17667. spin_lock_irqsave(&pring->ring_lock, iflags);
  17668. wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
  17669. bf_set(wqe_cqid, &wqe->generic.wqe_com,
  17670. phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
  17671. ret = lpfc_sli4_wq_put(wq, wqe);
  17672. if (ret) {
  17673. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17674. return ret;
  17675. }
  17676. lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
  17677. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17678. return 0;
  17679. }
  17680. /* NVMET requests */
  17681. if (pwqe->iocb_flag & LPFC_IO_NVMET) {
  17682. /* Get the IO distribution (hba_wqidx) for WQ assignment. */
  17683. pring = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx]->pring;
  17684. spin_lock_irqsave(&pring->ring_lock, iflags);
  17685. ctxp = pwqe->context2;
  17686. sglq = ctxp->ctxbuf->sglq;
  17687. if (pwqe->sli4_xritag == NO_XRI) {
  17688. pwqe->sli4_lxritag = sglq->sli4_lxritag;
  17689. pwqe->sli4_xritag = sglq->sli4_xritag;
  17690. }
  17691. bf_set(wqe_xri_tag, &pwqe->wqe.xmit_bls_rsp.wqe_com,
  17692. pwqe->sli4_xritag);
  17693. wq = phba->sli4_hba.nvme_wq[pwqe->hba_wqidx];
  17694. bf_set(wqe_cqid, &wqe->generic.wqe_com,
  17695. phba->sli4_hba.nvme_cq[pwqe->hba_wqidx]->queue_id);
  17696. ret = lpfc_sli4_wq_put(wq, wqe);
  17697. if (ret) {
  17698. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17699. return ret;
  17700. }
  17701. lpfc_sli_ringtxcmpl_put(phba, pring, pwqe);
  17702. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  17703. return 0;
  17704. }
  17705. return WQE_ERROR;
  17706. }