lpfc_sli.c 498 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
  67. uint32_t);
  68. static IOCB_t *
  69. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  70. {
  71. return &iocbq->iocb;
  72. }
  73. /**
  74. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  75. * @q: The Work Queue to operate on.
  76. * @wqe: The work Queue Entry to put on the Work queue.
  77. *
  78. * This routine will copy the contents of @wqe to the next available entry on
  79. * the @q. This function will then ring the Work Queue Doorbell to signal the
  80. * HBA to start processing the Work Queue Entry. This function returns 0 if
  81. * successful. If no entries are available on @q then this function will return
  82. * -ENOMEM.
  83. * The caller is expected to hold the hbalock when calling this routine.
  84. **/
  85. static uint32_t
  86. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  87. {
  88. union lpfc_wqe *temp_wqe;
  89. struct lpfc_register doorbell;
  90. uint32_t host_index;
  91. uint32_t idx;
  92. /* sanity check on queue memory */
  93. if (unlikely(!q))
  94. return -ENOMEM;
  95. temp_wqe = q->qe[q->host_index].wqe;
  96. /* If the host has not yet processed the next entry then we are done */
  97. idx = ((q->host_index + 1) % q->entry_count);
  98. if (idx == q->hba_index) {
  99. q->WQ_overflow++;
  100. return -ENOMEM;
  101. }
  102. q->WQ_posted++;
  103. /* set consumption flag every once in a while */
  104. if (!((q->host_index + 1) % q->entry_repost))
  105. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  106. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  107. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  108. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  109. /* Update the host index before invoking device */
  110. host_index = q->host_index;
  111. q->host_index = idx;
  112. /* Ring Doorbell */
  113. doorbell.word0 = 0;
  114. if (q->db_format == LPFC_DB_LIST_FORMAT) {
  115. bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
  116. bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
  117. bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
  118. } else if (q->db_format == LPFC_DB_RING_FORMAT) {
  119. bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
  120. bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
  121. } else {
  122. return -EINVAL;
  123. }
  124. writel(doorbell.word0, q->db_regaddr);
  125. return 0;
  126. }
  127. /**
  128. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  129. * @q: The Work Queue to operate on.
  130. * @index: The index to advance the hba index to.
  131. *
  132. * This routine will update the HBA index of a queue to reflect consumption of
  133. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  134. * an entry the host calls this function to update the queue's internal
  135. * pointers. This routine returns the number of entries that were consumed by
  136. * the HBA.
  137. **/
  138. static uint32_t
  139. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  140. {
  141. uint32_t released = 0;
  142. /* sanity check on queue memory */
  143. if (unlikely(!q))
  144. return 0;
  145. if (q->hba_index == index)
  146. return 0;
  147. do {
  148. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  149. released++;
  150. } while (q->hba_index != index);
  151. return released;
  152. }
  153. /**
  154. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  155. * @q: The Mailbox Queue to operate on.
  156. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  157. *
  158. * This routine will copy the contents of @mqe to the next available entry on
  159. * the @q. This function will then ring the Work Queue Doorbell to signal the
  160. * HBA to start processing the Work Queue Entry. This function returns 0 if
  161. * successful. If no entries are available on @q then this function will return
  162. * -ENOMEM.
  163. * The caller is expected to hold the hbalock when calling this routine.
  164. **/
  165. static uint32_t
  166. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  167. {
  168. struct lpfc_mqe *temp_mqe;
  169. struct lpfc_register doorbell;
  170. uint32_t host_index;
  171. /* sanity check on queue memory */
  172. if (unlikely(!q))
  173. return -ENOMEM;
  174. temp_mqe = q->qe[q->host_index].mqe;
  175. /* If the host has not yet processed the next entry then we are done */
  176. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  177. return -ENOMEM;
  178. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  179. /* Save off the mailbox pointer for completion */
  180. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  181. /* Update the host index before invoking device */
  182. host_index = q->host_index;
  183. q->host_index = ((q->host_index + 1) % q->entry_count);
  184. /* Ring Doorbell */
  185. doorbell.word0 = 0;
  186. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  187. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  188. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  189. return 0;
  190. }
  191. /**
  192. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  193. * @q: The Mailbox Queue to operate on.
  194. *
  195. * This routine will update the HBA index of a queue to reflect consumption of
  196. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  197. * an entry the host calls this function to update the queue's internal
  198. * pointers. This routine returns the number of entries that were consumed by
  199. * the HBA.
  200. **/
  201. static uint32_t
  202. lpfc_sli4_mq_release(struct lpfc_queue *q)
  203. {
  204. /* sanity check on queue memory */
  205. if (unlikely(!q))
  206. return 0;
  207. /* Clear the mailbox pointer for completion */
  208. q->phba->mbox = NULL;
  209. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  210. return 1;
  211. }
  212. /**
  213. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  214. * @q: The Event Queue to get the first valid EQE from
  215. *
  216. * This routine will get the first valid Event Queue Entry from @q, update
  217. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  218. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  219. * processed, but not popped back to the HBA then this routine will return NULL.
  220. **/
  221. static struct lpfc_eqe *
  222. lpfc_sli4_eq_get(struct lpfc_queue *q)
  223. {
  224. struct lpfc_eqe *eqe;
  225. uint32_t idx;
  226. /* sanity check on queue memory */
  227. if (unlikely(!q))
  228. return NULL;
  229. eqe = q->qe[q->hba_index].eqe;
  230. /* If the next EQE is not valid then we are done */
  231. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  232. return NULL;
  233. /* If the host has not yet processed the next entry then we are done */
  234. idx = ((q->hba_index + 1) % q->entry_count);
  235. if (idx == q->host_index)
  236. return NULL;
  237. q->hba_index = idx;
  238. return eqe;
  239. }
  240. /**
  241. * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
  242. * @q: The Event Queue to disable interrupts
  243. *
  244. **/
  245. static inline void
  246. lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
  247. {
  248. struct lpfc_register doorbell;
  249. doorbell.word0 = 0;
  250. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  251. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  252. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  253. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  254. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  255. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  256. }
  257. /**
  258. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  259. * @q: The Event Queue that the host has completed processing for.
  260. * @arm: Indicates whether the host wants to arms this CQ.
  261. *
  262. * This routine will mark all Event Queue Entries on @q, from the last
  263. * known completed entry to the last entry that was processed, as completed
  264. * by clearing the valid bit for each completion queue entry. Then it will
  265. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  266. * The internal host index in the @q will be updated by this routine to indicate
  267. * that the host has finished processing the entries. The @arm parameter
  268. * indicates that the queue should be rearmed when ringing the doorbell.
  269. *
  270. * This function will return the number of EQEs that were popped.
  271. **/
  272. uint32_t
  273. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  274. {
  275. uint32_t released = 0;
  276. struct lpfc_eqe *temp_eqe;
  277. struct lpfc_register doorbell;
  278. /* sanity check on queue memory */
  279. if (unlikely(!q))
  280. return 0;
  281. /* while there are valid entries */
  282. while (q->hba_index != q->host_index) {
  283. temp_eqe = q->qe[q->host_index].eqe;
  284. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  285. released++;
  286. q->host_index = ((q->host_index + 1) % q->entry_count);
  287. }
  288. if (unlikely(released == 0 && !arm))
  289. return 0;
  290. /* ring doorbell for number popped */
  291. doorbell.word0 = 0;
  292. if (arm) {
  293. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  294. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  295. }
  296. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  297. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  298. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  299. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  300. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  301. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  302. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  303. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  304. readl(q->phba->sli4_hba.EQCQDBregaddr);
  305. return released;
  306. }
  307. /**
  308. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  309. * @q: The Completion Queue to get the first valid CQE from
  310. *
  311. * This routine will get the first valid Completion Queue Entry from @q, update
  312. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  313. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  314. * processed, but not popped back to the HBA then this routine will return NULL.
  315. **/
  316. static struct lpfc_cqe *
  317. lpfc_sli4_cq_get(struct lpfc_queue *q)
  318. {
  319. struct lpfc_cqe *cqe;
  320. uint32_t idx;
  321. /* sanity check on queue memory */
  322. if (unlikely(!q))
  323. return NULL;
  324. /* If the next CQE is not valid then we are done */
  325. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  326. return NULL;
  327. /* If the host has not yet processed the next entry then we are done */
  328. idx = ((q->hba_index + 1) % q->entry_count);
  329. if (idx == q->host_index)
  330. return NULL;
  331. cqe = q->qe[q->hba_index].cqe;
  332. q->hba_index = idx;
  333. return cqe;
  334. }
  335. /**
  336. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  337. * @q: The Completion Queue that the host has completed processing for.
  338. * @arm: Indicates whether the host wants to arms this CQ.
  339. *
  340. * This routine will mark all Completion queue entries on @q, from the last
  341. * known completed entry to the last entry that was processed, as completed
  342. * by clearing the valid bit for each completion queue entry. Then it will
  343. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  344. * The internal host index in the @q will be updated by this routine to indicate
  345. * that the host has finished processing the entries. The @arm parameter
  346. * indicates that the queue should be rearmed when ringing the doorbell.
  347. *
  348. * This function will return the number of CQEs that were released.
  349. **/
  350. uint32_t
  351. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  352. {
  353. uint32_t released = 0;
  354. struct lpfc_cqe *temp_qe;
  355. struct lpfc_register doorbell;
  356. /* sanity check on queue memory */
  357. if (unlikely(!q))
  358. return 0;
  359. /* while there are valid entries */
  360. while (q->hba_index != q->host_index) {
  361. temp_qe = q->qe[q->host_index].cqe;
  362. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  363. released++;
  364. q->host_index = ((q->host_index + 1) % q->entry_count);
  365. }
  366. if (unlikely(released == 0 && !arm))
  367. return 0;
  368. /* ring doorbell for number popped */
  369. doorbell.word0 = 0;
  370. if (arm)
  371. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  372. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  373. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  374. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  375. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  376. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  377. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  378. return released;
  379. }
  380. /**
  381. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  382. * @q: The Header Receive Queue to operate on.
  383. * @wqe: The Receive Queue Entry to put on the Receive queue.
  384. *
  385. * This routine will copy the contents of @wqe to the next available entry on
  386. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  387. * HBA to start processing the Receive Queue Entry. This function returns the
  388. * index that the rqe was copied to if successful. If no entries are available
  389. * on @q then this function will return -ENOMEM.
  390. * The caller is expected to hold the hbalock when calling this routine.
  391. **/
  392. static int
  393. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  394. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  395. {
  396. struct lpfc_rqe *temp_hrqe;
  397. struct lpfc_rqe *temp_drqe;
  398. struct lpfc_register doorbell;
  399. int put_index;
  400. /* sanity check on queue memory */
  401. if (unlikely(!hq) || unlikely(!dq))
  402. return -ENOMEM;
  403. put_index = hq->host_index;
  404. temp_hrqe = hq->qe[hq->host_index].rqe;
  405. temp_drqe = dq->qe[dq->host_index].rqe;
  406. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  407. return -EINVAL;
  408. if (hq->host_index != dq->host_index)
  409. return -EINVAL;
  410. /* If the host has not yet processed the next entry then we are done */
  411. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  412. return -EBUSY;
  413. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  414. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  415. /* Update the host index to point to the next slot */
  416. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  417. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  418. /* Ring The Header Receive Queue Doorbell */
  419. if (!(hq->host_index % hq->entry_repost)) {
  420. doorbell.word0 = 0;
  421. if (hq->db_format == LPFC_DB_RING_FORMAT) {
  422. bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  423. hq->entry_repost);
  424. bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  425. } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  426. bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  427. hq->entry_repost);
  428. bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  429. hq->host_index);
  430. bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  431. } else {
  432. return -EINVAL;
  433. }
  434. writel(doorbell.word0, hq->db_regaddr);
  435. }
  436. return put_index;
  437. }
  438. /**
  439. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  440. * @q: The Header Receive Queue to operate on.
  441. *
  442. * This routine will update the HBA index of a queue to reflect consumption of
  443. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  444. * consumed an entry the host calls this function to update the queue's
  445. * internal pointers. This routine returns the number of entries that were
  446. * consumed by the HBA.
  447. **/
  448. static uint32_t
  449. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  450. {
  451. /* sanity check on queue memory */
  452. if (unlikely(!hq) || unlikely(!dq))
  453. return 0;
  454. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  455. return 0;
  456. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  457. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  458. return 1;
  459. }
  460. /**
  461. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  462. * @phba: Pointer to HBA context object.
  463. * @pring: Pointer to driver SLI ring object.
  464. *
  465. * This function returns pointer to next command iocb entry
  466. * in the command ring. The caller must hold hbalock to prevent
  467. * other threads consume the next command iocb.
  468. * SLI-2/SLI-3 provide different sized iocbs.
  469. **/
  470. static inline IOCB_t *
  471. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  472. {
  473. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  474. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  475. }
  476. /**
  477. * lpfc_resp_iocb - Get next response iocb entry in the ring
  478. * @phba: Pointer to HBA context object.
  479. * @pring: Pointer to driver SLI ring object.
  480. *
  481. * This function returns pointer to next response iocb entry
  482. * in the response ring. The caller must hold hbalock to make sure
  483. * that no other thread consume the next response iocb.
  484. * SLI-2/SLI-3 provide different sized iocbs.
  485. **/
  486. static inline IOCB_t *
  487. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  488. {
  489. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  490. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  491. }
  492. /**
  493. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  494. * @phba: Pointer to HBA context object.
  495. *
  496. * This function is called with hbalock held. This function
  497. * allocates a new driver iocb object from the iocb pool. If the
  498. * allocation is successful, it returns pointer to the newly
  499. * allocated iocb object else it returns NULL.
  500. **/
  501. struct lpfc_iocbq *
  502. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  503. {
  504. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  505. struct lpfc_iocbq * iocbq = NULL;
  506. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  507. if (iocbq)
  508. phba->iocb_cnt++;
  509. if (phba->iocb_cnt > phba->iocb_max)
  510. phba->iocb_max = phba->iocb_cnt;
  511. return iocbq;
  512. }
  513. /**
  514. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  515. * @phba: Pointer to HBA context object.
  516. * @xritag: XRI value.
  517. *
  518. * This function clears the sglq pointer from the array of acive
  519. * sglq's. The xritag that is passed in is used to index into the
  520. * array. Before the xritag can be used it needs to be adjusted
  521. * by subtracting the xribase.
  522. *
  523. * Returns sglq ponter = success, NULL = Failure.
  524. **/
  525. static struct lpfc_sglq *
  526. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  527. {
  528. struct lpfc_sglq *sglq;
  529. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  530. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  531. return sglq;
  532. }
  533. /**
  534. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  535. * @phba: Pointer to HBA context object.
  536. * @xritag: XRI value.
  537. *
  538. * This function returns the sglq pointer from the array of acive
  539. * sglq's. The xritag that is passed in is used to index into the
  540. * array. Before the xritag can be used it needs to be adjusted
  541. * by subtracting the xribase.
  542. *
  543. * Returns sglq ponter = success, NULL = Failure.
  544. **/
  545. struct lpfc_sglq *
  546. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  547. {
  548. struct lpfc_sglq *sglq;
  549. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  550. return sglq;
  551. }
  552. /**
  553. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  554. * @phba: Pointer to HBA context object.
  555. * @xritag: xri used in this exchange.
  556. * @rrq: The RRQ to be cleared.
  557. *
  558. **/
  559. void
  560. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  561. uint16_t xritag,
  562. struct lpfc_node_rrq *rrq)
  563. {
  564. struct lpfc_nodelist *ndlp = NULL;
  565. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  566. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  567. /* The target DID could have been swapped (cable swap)
  568. * we should use the ndlp from the findnode if it is
  569. * available.
  570. */
  571. if ((!ndlp) && rrq->ndlp)
  572. ndlp = rrq->ndlp;
  573. if (!ndlp)
  574. goto out;
  575. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  576. rrq->send_rrq = 0;
  577. rrq->xritag = 0;
  578. rrq->rrq_stop_time = 0;
  579. }
  580. out:
  581. mempool_free(rrq, phba->rrq_pool);
  582. }
  583. /**
  584. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  585. * @phba: Pointer to HBA context object.
  586. *
  587. * This function is called with hbalock held. This function
  588. * Checks if stop_time (ratov from setting rrq active) has
  589. * been reached, if it has and the send_rrq flag is set then
  590. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  591. * then it will just call the routine to clear the rrq and
  592. * free the rrq resource.
  593. * The timer is set to the next rrq that is going to expire before
  594. * leaving the routine.
  595. *
  596. **/
  597. void
  598. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  599. {
  600. struct lpfc_node_rrq *rrq;
  601. struct lpfc_node_rrq *nextrrq;
  602. unsigned long next_time;
  603. unsigned long iflags;
  604. LIST_HEAD(send_rrq);
  605. spin_lock_irqsave(&phba->hbalock, iflags);
  606. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  607. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  608. list_for_each_entry_safe(rrq, nextrrq,
  609. &phba->active_rrq_list, list) {
  610. if (time_after(jiffies, rrq->rrq_stop_time))
  611. list_move(&rrq->list, &send_rrq);
  612. else if (time_before(rrq->rrq_stop_time, next_time))
  613. next_time = rrq->rrq_stop_time;
  614. }
  615. spin_unlock_irqrestore(&phba->hbalock, iflags);
  616. if (!list_empty(&phba->active_rrq_list))
  617. mod_timer(&phba->rrq_tmr, next_time);
  618. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  619. list_del(&rrq->list);
  620. if (!rrq->send_rrq)
  621. /* this call will free the rrq */
  622. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  623. else if (lpfc_send_rrq(phba, rrq)) {
  624. /* if we send the rrq then the completion handler
  625. * will clear the bit in the xribitmap.
  626. */
  627. lpfc_clr_rrq_active(phba, rrq->xritag,
  628. rrq);
  629. }
  630. }
  631. }
  632. /**
  633. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  634. * @vport: Pointer to vport context object.
  635. * @xri: The xri used in the exchange.
  636. * @did: The targets DID for this exchange.
  637. *
  638. * returns NULL = rrq not found in the phba->active_rrq_list.
  639. * rrq = rrq for this xri and target.
  640. **/
  641. struct lpfc_node_rrq *
  642. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  643. {
  644. struct lpfc_hba *phba = vport->phba;
  645. struct lpfc_node_rrq *rrq;
  646. struct lpfc_node_rrq *nextrrq;
  647. unsigned long iflags;
  648. if (phba->sli_rev != LPFC_SLI_REV4)
  649. return NULL;
  650. spin_lock_irqsave(&phba->hbalock, iflags);
  651. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  652. if (rrq->vport == vport && rrq->xritag == xri &&
  653. rrq->nlp_DID == did){
  654. list_del(&rrq->list);
  655. spin_unlock_irqrestore(&phba->hbalock, iflags);
  656. return rrq;
  657. }
  658. }
  659. spin_unlock_irqrestore(&phba->hbalock, iflags);
  660. return NULL;
  661. }
  662. /**
  663. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  664. * @vport: Pointer to vport context object.
  665. * @ndlp: Pointer to the lpfc_node_list structure.
  666. * If ndlp is NULL Remove all active RRQs for this vport from the
  667. * phba->active_rrq_list and clear the rrq.
  668. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  669. **/
  670. void
  671. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  672. {
  673. struct lpfc_hba *phba = vport->phba;
  674. struct lpfc_node_rrq *rrq;
  675. struct lpfc_node_rrq *nextrrq;
  676. unsigned long iflags;
  677. LIST_HEAD(rrq_list);
  678. if (phba->sli_rev != LPFC_SLI_REV4)
  679. return;
  680. if (!ndlp) {
  681. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  682. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  683. }
  684. spin_lock_irqsave(&phba->hbalock, iflags);
  685. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  686. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  687. list_move(&rrq->list, &rrq_list);
  688. spin_unlock_irqrestore(&phba->hbalock, iflags);
  689. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  690. list_del(&rrq->list);
  691. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  692. }
  693. }
  694. /**
  695. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  696. * @phba: Pointer to HBA context object.
  697. *
  698. * Remove all rrqs from the phba->active_rrq_list and free them by
  699. * calling __lpfc_clr_active_rrq
  700. *
  701. **/
  702. void
  703. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  704. {
  705. struct lpfc_node_rrq *rrq;
  706. struct lpfc_node_rrq *nextrrq;
  707. unsigned long next_time;
  708. unsigned long iflags;
  709. LIST_HEAD(rrq_list);
  710. if (phba->sli_rev != LPFC_SLI_REV4)
  711. return;
  712. spin_lock_irqsave(&phba->hbalock, iflags);
  713. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  714. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  715. list_splice_init(&phba->active_rrq_list, &rrq_list);
  716. spin_unlock_irqrestore(&phba->hbalock, iflags);
  717. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  718. list_del(&rrq->list);
  719. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  720. }
  721. if (!list_empty(&phba->active_rrq_list))
  722. mod_timer(&phba->rrq_tmr, next_time);
  723. }
  724. /**
  725. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  726. * @phba: Pointer to HBA context object.
  727. * @ndlp: Targets nodelist pointer for this exchange.
  728. * @xritag the xri in the bitmap to test.
  729. *
  730. * This function is called with hbalock held. This function
  731. * returns 0 = rrq not active for this xri
  732. * 1 = rrq is valid for this xri.
  733. **/
  734. int
  735. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  736. uint16_t xritag)
  737. {
  738. if (!ndlp)
  739. return 0;
  740. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  741. return 1;
  742. else
  743. return 0;
  744. }
  745. /**
  746. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  747. * @phba: Pointer to HBA context object.
  748. * @ndlp: nodelist pointer for this target.
  749. * @xritag: xri used in this exchange.
  750. * @rxid: Remote Exchange ID.
  751. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  752. *
  753. * This function takes the hbalock.
  754. * The active bit is always set in the active rrq xri_bitmap even
  755. * if there is no slot avaiable for the other rrq information.
  756. *
  757. * returns 0 rrq actived for this xri
  758. * < 0 No memory or invalid ndlp.
  759. **/
  760. int
  761. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  762. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  763. {
  764. unsigned long iflags;
  765. struct lpfc_node_rrq *rrq;
  766. int empty;
  767. if (!ndlp)
  768. return -EINVAL;
  769. if (!phba->cfg_enable_rrq)
  770. return -EINVAL;
  771. spin_lock_irqsave(&phba->hbalock, iflags);
  772. if (phba->pport->load_flag & FC_UNLOADING) {
  773. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  774. goto out;
  775. }
  776. /*
  777. * set the active bit even if there is no mem available.
  778. */
  779. if (NLP_CHK_FREE_REQ(ndlp))
  780. goto out;
  781. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  782. goto out;
  783. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  784. goto out;
  785. spin_unlock_irqrestore(&phba->hbalock, iflags);
  786. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  787. if (!rrq) {
  788. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  789. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  790. " DID:0x%x Send:%d\n",
  791. xritag, rxid, ndlp->nlp_DID, send_rrq);
  792. return -EINVAL;
  793. }
  794. rrq->send_rrq = send_rrq;
  795. rrq->xritag = xritag;
  796. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  797. rrq->ndlp = ndlp;
  798. rrq->nlp_DID = ndlp->nlp_DID;
  799. rrq->vport = ndlp->vport;
  800. rrq->rxid = rxid;
  801. rrq->send_rrq = send_rrq;
  802. spin_lock_irqsave(&phba->hbalock, iflags);
  803. empty = list_empty(&phba->active_rrq_list);
  804. list_add_tail(&rrq->list, &phba->active_rrq_list);
  805. phba->hba_flag |= HBA_RRQ_ACTIVE;
  806. if (empty)
  807. lpfc_worker_wake_up(phba);
  808. spin_unlock_irqrestore(&phba->hbalock, iflags);
  809. return 0;
  810. out:
  811. spin_unlock_irqrestore(&phba->hbalock, iflags);
  812. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  813. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  814. " DID:0x%x Send:%d\n",
  815. xritag, rxid, ndlp->nlp_DID, send_rrq);
  816. return -EINVAL;
  817. }
  818. /**
  819. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  820. * @phba: Pointer to HBA context object.
  821. * @piocb: Pointer to the iocbq.
  822. *
  823. * This function is called with hbalock held. This function
  824. * gets a new driver sglq object from the sglq list. If the
  825. * list is not empty then it is successful, it returns pointer to the newly
  826. * allocated sglq object else it returns NULL.
  827. **/
  828. static struct lpfc_sglq *
  829. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  830. {
  831. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  832. struct lpfc_sglq *sglq = NULL;
  833. struct lpfc_sglq *start_sglq = NULL;
  834. struct lpfc_scsi_buf *lpfc_cmd;
  835. struct lpfc_nodelist *ndlp;
  836. int found = 0;
  837. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  838. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  839. ndlp = lpfc_cmd->rdata->pnode;
  840. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  841. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  842. ndlp = piocbq->context_un.ndlp;
  843. else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
  844. (piocbq->iocb_flag & LPFC_IO_LIBDFC))
  845. ndlp = piocbq->context_un.ndlp;
  846. else
  847. ndlp = piocbq->context1;
  848. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  849. start_sglq = sglq;
  850. while (!found) {
  851. if (!sglq)
  852. return NULL;
  853. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  854. /* This xri has an rrq outstanding for this DID.
  855. * put it back in the list and get another xri.
  856. */
  857. list_add_tail(&sglq->list, lpfc_sgl_list);
  858. sglq = NULL;
  859. list_remove_head(lpfc_sgl_list, sglq,
  860. struct lpfc_sglq, list);
  861. if (sglq == start_sglq) {
  862. sglq = NULL;
  863. break;
  864. } else
  865. continue;
  866. }
  867. sglq->ndlp = ndlp;
  868. found = 1;
  869. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  870. sglq->state = SGL_ALLOCATED;
  871. }
  872. return sglq;
  873. }
  874. /**
  875. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  876. * @phba: Pointer to HBA context object.
  877. *
  878. * This function is called with no lock held. This function
  879. * allocates a new driver iocb object from the iocb pool. If the
  880. * allocation is successful, it returns pointer to the newly
  881. * allocated iocb object else it returns NULL.
  882. **/
  883. struct lpfc_iocbq *
  884. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  885. {
  886. struct lpfc_iocbq * iocbq = NULL;
  887. unsigned long iflags;
  888. spin_lock_irqsave(&phba->hbalock, iflags);
  889. iocbq = __lpfc_sli_get_iocbq(phba);
  890. spin_unlock_irqrestore(&phba->hbalock, iflags);
  891. return iocbq;
  892. }
  893. /**
  894. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  895. * @phba: Pointer to HBA context object.
  896. * @iocbq: Pointer to driver iocb object.
  897. *
  898. * This function is called with hbalock held to release driver
  899. * iocb object to the iocb pool. The iotag in the iocb object
  900. * does not change for each use of the iocb object. This function
  901. * clears all other fields of the iocb object when it is freed.
  902. * The sqlq structure that holds the xritag and phys and virtual
  903. * mappings for the scatter gather list is retrieved from the
  904. * active array of sglq. The get of the sglq pointer also clears
  905. * the entry in the array. If the status of the IO indiactes that
  906. * this IO was aborted then the sglq entry it put on the
  907. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  908. * IO has good status or fails for any other reason then the sglq
  909. * entry is added to the free list (lpfc_sgl_list).
  910. **/
  911. static void
  912. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  913. {
  914. struct lpfc_sglq *sglq;
  915. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  916. unsigned long iflag = 0;
  917. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  918. if (iocbq->sli4_xritag == NO_XRI)
  919. sglq = NULL;
  920. else
  921. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  922. if (sglq) {
  923. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  924. (sglq->state != SGL_XRI_ABORTED)) {
  925. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  926. iflag);
  927. list_add(&sglq->list,
  928. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  929. spin_unlock_irqrestore(
  930. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  931. } else {
  932. sglq->state = SGL_FREED;
  933. sglq->ndlp = NULL;
  934. list_add_tail(&sglq->list,
  935. &phba->sli4_hba.lpfc_sgl_list);
  936. /* Check if TXQ queue needs to be serviced */
  937. if (pring->txq_cnt)
  938. lpfc_worker_wake_up(phba);
  939. }
  940. }
  941. /*
  942. * Clean all volatile data fields, preserve iotag and node struct.
  943. */
  944. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  945. iocbq->sli4_lxritag = NO_XRI;
  946. iocbq->sli4_xritag = NO_XRI;
  947. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  948. }
  949. /**
  950. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  951. * @phba: Pointer to HBA context object.
  952. * @iocbq: Pointer to driver iocb object.
  953. *
  954. * This function is called with hbalock held to release driver
  955. * iocb object to the iocb pool. The iotag in the iocb object
  956. * does not change for each use of the iocb object. This function
  957. * clears all other fields of the iocb object when it is freed.
  958. **/
  959. static void
  960. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  961. {
  962. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  963. /*
  964. * Clean all volatile data fields, preserve iotag and node struct.
  965. */
  966. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  967. iocbq->sli4_xritag = NO_XRI;
  968. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  969. }
  970. /**
  971. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  972. * @phba: Pointer to HBA context object.
  973. * @iocbq: Pointer to driver iocb object.
  974. *
  975. * This function is called with hbalock held to release driver
  976. * iocb object to the iocb pool. The iotag in the iocb object
  977. * does not change for each use of the iocb object. This function
  978. * clears all other fields of the iocb object when it is freed.
  979. **/
  980. static void
  981. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  982. {
  983. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  984. phba->iocb_cnt--;
  985. }
  986. /**
  987. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  988. * @phba: Pointer to HBA context object.
  989. * @iocbq: Pointer to driver iocb object.
  990. *
  991. * This function is called with no lock held to release the iocb to
  992. * iocb pool.
  993. **/
  994. void
  995. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  996. {
  997. unsigned long iflags;
  998. /*
  999. * Clean all volatile data fields, preserve iotag and node struct.
  1000. */
  1001. spin_lock_irqsave(&phba->hbalock, iflags);
  1002. __lpfc_sli_release_iocbq(phba, iocbq);
  1003. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1004. }
  1005. /**
  1006. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  1007. * @phba: Pointer to HBA context object.
  1008. * @iocblist: List of IOCBs.
  1009. * @ulpstatus: ULP status in IOCB command field.
  1010. * @ulpWord4: ULP word-4 in IOCB command field.
  1011. *
  1012. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  1013. * on the list by invoking the complete callback function associated with the
  1014. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  1015. * fields.
  1016. **/
  1017. void
  1018. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  1019. uint32_t ulpstatus, uint32_t ulpWord4)
  1020. {
  1021. struct lpfc_iocbq *piocb;
  1022. while (!list_empty(iocblist)) {
  1023. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  1024. if (!piocb->iocb_cmpl)
  1025. lpfc_sli_release_iocbq(phba, piocb);
  1026. else {
  1027. piocb->iocb.ulpStatus = ulpstatus;
  1028. piocb->iocb.un.ulpWord[4] = ulpWord4;
  1029. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1030. }
  1031. }
  1032. return;
  1033. }
  1034. /**
  1035. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1036. * @iocb_cmnd: iocb command code.
  1037. *
  1038. * This function is called by ring event handler function to get the iocb type.
  1039. * This function translates the iocb command to an iocb command type used to
  1040. * decide the final disposition of each completed IOCB.
  1041. * The function returns
  1042. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1043. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1044. * LPFC_ABORT_IOCB if it is an abort iocb
  1045. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1046. *
  1047. * The caller is not required to hold any lock.
  1048. **/
  1049. static lpfc_iocb_type
  1050. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1051. {
  1052. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1053. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1054. return 0;
  1055. switch (iocb_cmnd) {
  1056. case CMD_XMIT_SEQUENCE_CR:
  1057. case CMD_XMIT_SEQUENCE_CX:
  1058. case CMD_XMIT_BCAST_CN:
  1059. case CMD_XMIT_BCAST_CX:
  1060. case CMD_ELS_REQUEST_CR:
  1061. case CMD_ELS_REQUEST_CX:
  1062. case CMD_CREATE_XRI_CR:
  1063. case CMD_CREATE_XRI_CX:
  1064. case CMD_GET_RPI_CN:
  1065. case CMD_XMIT_ELS_RSP_CX:
  1066. case CMD_GET_RPI_CR:
  1067. case CMD_FCP_IWRITE_CR:
  1068. case CMD_FCP_IWRITE_CX:
  1069. case CMD_FCP_IREAD_CR:
  1070. case CMD_FCP_IREAD_CX:
  1071. case CMD_FCP_ICMND_CR:
  1072. case CMD_FCP_ICMND_CX:
  1073. case CMD_FCP_TSEND_CX:
  1074. case CMD_FCP_TRSP_CX:
  1075. case CMD_FCP_TRECEIVE_CX:
  1076. case CMD_FCP_AUTO_TRSP_CX:
  1077. case CMD_ADAPTER_MSG:
  1078. case CMD_ADAPTER_DUMP:
  1079. case CMD_XMIT_SEQUENCE64_CR:
  1080. case CMD_XMIT_SEQUENCE64_CX:
  1081. case CMD_XMIT_BCAST64_CN:
  1082. case CMD_XMIT_BCAST64_CX:
  1083. case CMD_ELS_REQUEST64_CR:
  1084. case CMD_ELS_REQUEST64_CX:
  1085. case CMD_FCP_IWRITE64_CR:
  1086. case CMD_FCP_IWRITE64_CX:
  1087. case CMD_FCP_IREAD64_CR:
  1088. case CMD_FCP_IREAD64_CX:
  1089. case CMD_FCP_ICMND64_CR:
  1090. case CMD_FCP_ICMND64_CX:
  1091. case CMD_FCP_TSEND64_CX:
  1092. case CMD_FCP_TRSP64_CX:
  1093. case CMD_FCP_TRECEIVE64_CX:
  1094. case CMD_GEN_REQUEST64_CR:
  1095. case CMD_GEN_REQUEST64_CX:
  1096. case CMD_XMIT_ELS_RSP64_CX:
  1097. case DSSCMD_IWRITE64_CR:
  1098. case DSSCMD_IWRITE64_CX:
  1099. case DSSCMD_IREAD64_CR:
  1100. case DSSCMD_IREAD64_CX:
  1101. type = LPFC_SOL_IOCB;
  1102. break;
  1103. case CMD_ABORT_XRI_CN:
  1104. case CMD_ABORT_XRI_CX:
  1105. case CMD_CLOSE_XRI_CN:
  1106. case CMD_CLOSE_XRI_CX:
  1107. case CMD_XRI_ABORTED_CX:
  1108. case CMD_ABORT_MXRI64_CN:
  1109. case CMD_XMIT_BLS_RSP64_CX:
  1110. type = LPFC_ABORT_IOCB;
  1111. break;
  1112. case CMD_RCV_SEQUENCE_CX:
  1113. case CMD_RCV_ELS_REQ_CX:
  1114. case CMD_RCV_SEQUENCE64_CX:
  1115. case CMD_RCV_ELS_REQ64_CX:
  1116. case CMD_ASYNC_STATUS:
  1117. case CMD_IOCB_RCV_SEQ64_CX:
  1118. case CMD_IOCB_RCV_ELS64_CX:
  1119. case CMD_IOCB_RCV_CONT64_CX:
  1120. case CMD_IOCB_RET_XRI64_CX:
  1121. type = LPFC_UNSOL_IOCB;
  1122. break;
  1123. case CMD_IOCB_XMIT_MSEQ64_CR:
  1124. case CMD_IOCB_XMIT_MSEQ64_CX:
  1125. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1126. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1127. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1128. case CMD_IOCB_ABORT_EXTENDED_CN:
  1129. case CMD_IOCB_RET_HBQE64_CN:
  1130. case CMD_IOCB_FCP_IBIDIR64_CR:
  1131. case CMD_IOCB_FCP_IBIDIR64_CX:
  1132. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1133. case CMD_IOCB_LOGENTRY_CN:
  1134. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1135. printk("%s - Unhandled SLI-3 Command x%x\n",
  1136. __func__, iocb_cmnd);
  1137. type = LPFC_UNKNOWN_IOCB;
  1138. break;
  1139. default:
  1140. type = LPFC_UNKNOWN_IOCB;
  1141. break;
  1142. }
  1143. return type;
  1144. }
  1145. /**
  1146. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1147. * @phba: Pointer to HBA context object.
  1148. *
  1149. * This function is called from SLI initialization code
  1150. * to configure every ring of the HBA's SLI interface. The
  1151. * caller is not required to hold any lock. This function issues
  1152. * a config_ring mailbox command for each ring.
  1153. * This function returns zero if successful else returns a negative
  1154. * error code.
  1155. **/
  1156. static int
  1157. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1158. {
  1159. struct lpfc_sli *psli = &phba->sli;
  1160. LPFC_MBOXQ_t *pmb;
  1161. MAILBOX_t *pmbox;
  1162. int i, rc, ret = 0;
  1163. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1164. if (!pmb)
  1165. return -ENOMEM;
  1166. pmbox = &pmb->u.mb;
  1167. phba->link_state = LPFC_INIT_MBX_CMDS;
  1168. for (i = 0; i < psli->num_rings; i++) {
  1169. lpfc_config_ring(phba, i, pmb);
  1170. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1171. if (rc != MBX_SUCCESS) {
  1172. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1173. "0446 Adapter failed to init (%d), "
  1174. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1175. "ring %d\n",
  1176. rc, pmbox->mbxCommand,
  1177. pmbox->mbxStatus, i);
  1178. phba->link_state = LPFC_HBA_ERROR;
  1179. ret = -ENXIO;
  1180. break;
  1181. }
  1182. }
  1183. mempool_free(pmb, phba->mbox_mem_pool);
  1184. return ret;
  1185. }
  1186. /**
  1187. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1188. * @phba: Pointer to HBA context object.
  1189. * @pring: Pointer to driver SLI ring object.
  1190. * @piocb: Pointer to the driver iocb object.
  1191. *
  1192. * This function is called with hbalock held. The function adds the
  1193. * new iocb to txcmplq of the given ring. This function always returns
  1194. * 0. If this function is called for ELS ring, this function checks if
  1195. * there is a vport associated with the ELS command. This function also
  1196. * starts els_tmofunc timer if this is an ELS command.
  1197. **/
  1198. static int
  1199. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1200. struct lpfc_iocbq *piocb)
  1201. {
  1202. list_add_tail(&piocb->list, &pring->txcmplq);
  1203. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1204. pring->txcmplq_cnt++;
  1205. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1206. pring->txcmplq_max = pring->txcmplq_cnt;
  1207. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1208. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1209. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1210. if (!piocb->vport)
  1211. BUG();
  1212. else
  1213. mod_timer(&piocb->vport->els_tmofunc,
  1214. jiffies + HZ * (phba->fc_ratov << 1));
  1215. }
  1216. return 0;
  1217. }
  1218. /**
  1219. * lpfc_sli_ringtx_get - Get first element of the txq
  1220. * @phba: Pointer to HBA context object.
  1221. * @pring: Pointer to driver SLI ring object.
  1222. *
  1223. * This function is called with hbalock held to get next
  1224. * iocb in txq of the given ring. If there is any iocb in
  1225. * the txq, the function returns first iocb in the list after
  1226. * removing the iocb from the list, else it returns NULL.
  1227. **/
  1228. struct lpfc_iocbq *
  1229. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1230. {
  1231. struct lpfc_iocbq *cmd_iocb;
  1232. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1233. if (cmd_iocb != NULL)
  1234. pring->txq_cnt--;
  1235. return cmd_iocb;
  1236. }
  1237. /**
  1238. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1239. * @phba: Pointer to HBA context object.
  1240. * @pring: Pointer to driver SLI ring object.
  1241. *
  1242. * This function is called with hbalock held and the caller must post the
  1243. * iocb without releasing the lock. If the caller releases the lock,
  1244. * iocb slot returned by the function is not guaranteed to be available.
  1245. * The function returns pointer to the next available iocb slot if there
  1246. * is available slot in the ring, else it returns NULL.
  1247. * If the get index of the ring is ahead of the put index, the function
  1248. * will post an error attention event to the worker thread to take the
  1249. * HBA to offline state.
  1250. **/
  1251. static IOCB_t *
  1252. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1253. {
  1254. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1255. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1256. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1257. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1258. pring->sli.sli3.next_cmdidx = 0;
  1259. if (unlikely(pring->sli.sli3.local_getidx ==
  1260. pring->sli.sli3.next_cmdidx)) {
  1261. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1262. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1263. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1264. "0315 Ring %d issue: portCmdGet %d "
  1265. "is bigger than cmd ring %d\n",
  1266. pring->ringno,
  1267. pring->sli.sli3.local_getidx,
  1268. max_cmd_idx);
  1269. phba->link_state = LPFC_HBA_ERROR;
  1270. /*
  1271. * All error attention handlers are posted to
  1272. * worker thread
  1273. */
  1274. phba->work_ha |= HA_ERATT;
  1275. phba->work_hs = HS_FFER3;
  1276. lpfc_worker_wake_up(phba);
  1277. return NULL;
  1278. }
  1279. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1280. return NULL;
  1281. }
  1282. return lpfc_cmd_iocb(phba, pring);
  1283. }
  1284. /**
  1285. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1286. * @phba: Pointer to HBA context object.
  1287. * @iocbq: Pointer to driver iocb object.
  1288. *
  1289. * This function gets an iotag for the iocb. If there is no unused iotag and
  1290. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1291. * array and assigns a new iotag.
  1292. * The function returns the allocated iotag if successful, else returns zero.
  1293. * Zero is not a valid iotag.
  1294. * The caller is not required to hold any lock.
  1295. **/
  1296. uint16_t
  1297. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1298. {
  1299. struct lpfc_iocbq **new_arr;
  1300. struct lpfc_iocbq **old_arr;
  1301. size_t new_len;
  1302. struct lpfc_sli *psli = &phba->sli;
  1303. uint16_t iotag;
  1304. spin_lock_irq(&phba->hbalock);
  1305. iotag = psli->last_iotag;
  1306. if(++iotag < psli->iocbq_lookup_len) {
  1307. psli->last_iotag = iotag;
  1308. psli->iocbq_lookup[iotag] = iocbq;
  1309. spin_unlock_irq(&phba->hbalock);
  1310. iocbq->iotag = iotag;
  1311. return iotag;
  1312. } else if (psli->iocbq_lookup_len < (0xffff
  1313. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1314. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1315. spin_unlock_irq(&phba->hbalock);
  1316. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1317. GFP_KERNEL);
  1318. if (new_arr) {
  1319. spin_lock_irq(&phba->hbalock);
  1320. old_arr = psli->iocbq_lookup;
  1321. if (new_len <= psli->iocbq_lookup_len) {
  1322. /* highly unprobable case */
  1323. kfree(new_arr);
  1324. iotag = psli->last_iotag;
  1325. if(++iotag < psli->iocbq_lookup_len) {
  1326. psli->last_iotag = iotag;
  1327. psli->iocbq_lookup[iotag] = iocbq;
  1328. spin_unlock_irq(&phba->hbalock);
  1329. iocbq->iotag = iotag;
  1330. return iotag;
  1331. }
  1332. spin_unlock_irq(&phba->hbalock);
  1333. return 0;
  1334. }
  1335. if (psli->iocbq_lookup)
  1336. memcpy(new_arr, old_arr,
  1337. ((psli->last_iotag + 1) *
  1338. sizeof (struct lpfc_iocbq *)));
  1339. psli->iocbq_lookup = new_arr;
  1340. psli->iocbq_lookup_len = new_len;
  1341. psli->last_iotag = iotag;
  1342. psli->iocbq_lookup[iotag] = iocbq;
  1343. spin_unlock_irq(&phba->hbalock);
  1344. iocbq->iotag = iotag;
  1345. kfree(old_arr);
  1346. return iotag;
  1347. }
  1348. } else
  1349. spin_unlock_irq(&phba->hbalock);
  1350. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1351. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1352. psli->last_iotag);
  1353. return 0;
  1354. }
  1355. /**
  1356. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1357. * @phba: Pointer to HBA context object.
  1358. * @pring: Pointer to driver SLI ring object.
  1359. * @iocb: Pointer to iocb slot in the ring.
  1360. * @nextiocb: Pointer to driver iocb object which need to be
  1361. * posted to firmware.
  1362. *
  1363. * This function is called with hbalock held to post a new iocb to
  1364. * the firmware. This function copies the new iocb to ring iocb slot and
  1365. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1366. * a completion call back for this iocb else the function will free the
  1367. * iocb object.
  1368. **/
  1369. static void
  1370. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1371. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1372. {
  1373. /*
  1374. * Set up an iotag
  1375. */
  1376. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1377. if (pring->ringno == LPFC_ELS_RING) {
  1378. lpfc_debugfs_slow_ring_trc(phba,
  1379. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1380. *(((uint32_t *) &nextiocb->iocb) + 4),
  1381. *(((uint32_t *) &nextiocb->iocb) + 6),
  1382. *(((uint32_t *) &nextiocb->iocb) + 7));
  1383. }
  1384. /*
  1385. * Issue iocb command to adapter
  1386. */
  1387. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1388. wmb();
  1389. pring->stats.iocb_cmd++;
  1390. /*
  1391. * If there is no completion routine to call, we can release the
  1392. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1393. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1394. */
  1395. if (nextiocb->iocb_cmpl)
  1396. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1397. else
  1398. __lpfc_sli_release_iocbq(phba, nextiocb);
  1399. /*
  1400. * Let the HBA know what IOCB slot will be the next one the
  1401. * driver will put a command into.
  1402. */
  1403. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1404. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1405. }
  1406. /**
  1407. * lpfc_sli_update_full_ring - Update the chip attention register
  1408. * @phba: Pointer to HBA context object.
  1409. * @pring: Pointer to driver SLI ring object.
  1410. *
  1411. * The caller is not required to hold any lock for calling this function.
  1412. * This function updates the chip attention bits for the ring to inform firmware
  1413. * that there are pending work to be done for this ring and requests an
  1414. * interrupt when there is space available in the ring. This function is
  1415. * called when the driver is unable to post more iocbs to the ring due
  1416. * to unavailability of space in the ring.
  1417. **/
  1418. static void
  1419. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1420. {
  1421. int ringno = pring->ringno;
  1422. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1423. wmb();
  1424. /*
  1425. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1426. * The HBA will tell us when an IOCB entry is available.
  1427. */
  1428. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1429. readl(phba->CAregaddr); /* flush */
  1430. pring->stats.iocb_cmd_full++;
  1431. }
  1432. /**
  1433. * lpfc_sli_update_ring - Update chip attention register
  1434. * @phba: Pointer to HBA context object.
  1435. * @pring: Pointer to driver SLI ring object.
  1436. *
  1437. * This function updates the chip attention register bit for the
  1438. * given ring to inform HBA that there is more work to be done
  1439. * in this ring. The caller is not required to hold any lock.
  1440. **/
  1441. static void
  1442. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1443. {
  1444. int ringno = pring->ringno;
  1445. /*
  1446. * Tell the HBA that there is work to do in this ring.
  1447. */
  1448. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1449. wmb();
  1450. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1451. readl(phba->CAregaddr); /* flush */
  1452. }
  1453. }
  1454. /**
  1455. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1456. * @phba: Pointer to HBA context object.
  1457. * @pring: Pointer to driver SLI ring object.
  1458. *
  1459. * This function is called with hbalock held to post pending iocbs
  1460. * in the txq to the firmware. This function is called when driver
  1461. * detects space available in the ring.
  1462. **/
  1463. static void
  1464. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1465. {
  1466. IOCB_t *iocb;
  1467. struct lpfc_iocbq *nextiocb;
  1468. /*
  1469. * Check to see if:
  1470. * (a) there is anything on the txq to send
  1471. * (b) link is up
  1472. * (c) link attention events can be processed (fcp ring only)
  1473. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1474. */
  1475. if (pring->txq_cnt &&
  1476. lpfc_is_link_up(phba) &&
  1477. (pring->ringno != phba->sli.fcp_ring ||
  1478. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1479. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1480. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1481. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1482. if (iocb)
  1483. lpfc_sli_update_ring(phba, pring);
  1484. else
  1485. lpfc_sli_update_full_ring(phba, pring);
  1486. }
  1487. return;
  1488. }
  1489. /**
  1490. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1491. * @phba: Pointer to HBA context object.
  1492. * @hbqno: HBQ number.
  1493. *
  1494. * This function is called with hbalock held to get the next
  1495. * available slot for the given HBQ. If there is free slot
  1496. * available for the HBQ it will return pointer to the next available
  1497. * HBQ entry else it will return NULL.
  1498. **/
  1499. static struct lpfc_hbq_entry *
  1500. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1501. {
  1502. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1503. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1504. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1505. hbqp->next_hbqPutIdx = 0;
  1506. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1507. uint32_t raw_index = phba->hbq_get[hbqno];
  1508. uint32_t getidx = le32_to_cpu(raw_index);
  1509. hbqp->local_hbqGetIdx = getidx;
  1510. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1511. lpfc_printf_log(phba, KERN_ERR,
  1512. LOG_SLI | LOG_VPORT,
  1513. "1802 HBQ %d: local_hbqGetIdx "
  1514. "%u is > than hbqp->entry_count %u\n",
  1515. hbqno, hbqp->local_hbqGetIdx,
  1516. hbqp->entry_count);
  1517. phba->link_state = LPFC_HBA_ERROR;
  1518. return NULL;
  1519. }
  1520. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1521. return NULL;
  1522. }
  1523. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1524. hbqp->hbqPutIdx;
  1525. }
  1526. /**
  1527. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1528. * @phba: Pointer to HBA context object.
  1529. *
  1530. * This function is called with no lock held to free all the
  1531. * hbq buffers while uninitializing the SLI interface. It also
  1532. * frees the HBQ buffers returned by the firmware but not yet
  1533. * processed by the upper layers.
  1534. **/
  1535. void
  1536. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1537. {
  1538. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1539. struct hbq_dmabuf *hbq_buf;
  1540. unsigned long flags;
  1541. int i, hbq_count;
  1542. uint32_t hbqno;
  1543. hbq_count = lpfc_sli_hbq_count();
  1544. /* Return all memory used by all HBQs */
  1545. spin_lock_irqsave(&phba->hbalock, flags);
  1546. for (i = 0; i < hbq_count; ++i) {
  1547. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1548. &phba->hbqs[i].hbq_buffer_list, list) {
  1549. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1550. list_del(&hbq_buf->dbuf.list);
  1551. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1552. }
  1553. phba->hbqs[i].buffer_count = 0;
  1554. }
  1555. /* Return all HBQ buffer that are in-fly */
  1556. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1557. list) {
  1558. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1559. list_del(&hbq_buf->dbuf.list);
  1560. if (hbq_buf->tag == -1) {
  1561. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1562. (phba, hbq_buf);
  1563. } else {
  1564. hbqno = hbq_buf->tag >> 16;
  1565. if (hbqno >= LPFC_MAX_HBQS)
  1566. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1567. (phba, hbq_buf);
  1568. else
  1569. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1570. hbq_buf);
  1571. }
  1572. }
  1573. /* Mark the HBQs not in use */
  1574. phba->hbq_in_use = 0;
  1575. spin_unlock_irqrestore(&phba->hbalock, flags);
  1576. }
  1577. /**
  1578. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1579. * @phba: Pointer to HBA context object.
  1580. * @hbqno: HBQ number.
  1581. * @hbq_buf: Pointer to HBQ buffer.
  1582. *
  1583. * This function is called with the hbalock held to post a
  1584. * hbq buffer to the firmware. If the function finds an empty
  1585. * slot in the HBQ, it will post the buffer. The function will return
  1586. * pointer to the hbq entry if it successfully post the buffer
  1587. * else it will return NULL.
  1588. **/
  1589. static int
  1590. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1591. struct hbq_dmabuf *hbq_buf)
  1592. {
  1593. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1594. }
  1595. /**
  1596. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1597. * @phba: Pointer to HBA context object.
  1598. * @hbqno: HBQ number.
  1599. * @hbq_buf: Pointer to HBQ buffer.
  1600. *
  1601. * This function is called with the hbalock held to post a hbq buffer to the
  1602. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1603. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1604. * it successfully post the buffer else it will return an error.
  1605. **/
  1606. static int
  1607. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1608. struct hbq_dmabuf *hbq_buf)
  1609. {
  1610. struct lpfc_hbq_entry *hbqe;
  1611. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1612. /* Get next HBQ entry slot to use */
  1613. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1614. if (hbqe) {
  1615. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1616. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1617. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1618. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1619. hbqe->bde.tus.f.bdeFlags = 0;
  1620. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1621. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1622. /* Sync SLIM */
  1623. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1624. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1625. /* flush */
  1626. readl(phba->hbq_put + hbqno);
  1627. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1628. return 0;
  1629. } else
  1630. return -ENOMEM;
  1631. }
  1632. /**
  1633. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1634. * @phba: Pointer to HBA context object.
  1635. * @hbqno: HBQ number.
  1636. * @hbq_buf: Pointer to HBQ buffer.
  1637. *
  1638. * This function is called with the hbalock held to post an RQE to the SLI4
  1639. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1640. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1641. **/
  1642. static int
  1643. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1644. struct hbq_dmabuf *hbq_buf)
  1645. {
  1646. int rc;
  1647. struct lpfc_rqe hrqe;
  1648. struct lpfc_rqe drqe;
  1649. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1650. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1651. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1652. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1653. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1654. &hrqe, &drqe);
  1655. if (rc < 0)
  1656. return rc;
  1657. hbq_buf->tag = rc;
  1658. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1659. return 0;
  1660. }
  1661. /* HBQ for ELS and CT traffic. */
  1662. static struct lpfc_hbq_init lpfc_els_hbq = {
  1663. .rn = 1,
  1664. .entry_count = 256,
  1665. .mask_count = 0,
  1666. .profile = 0,
  1667. .ring_mask = (1 << LPFC_ELS_RING),
  1668. .buffer_count = 0,
  1669. .init_count = 40,
  1670. .add_count = 40,
  1671. };
  1672. /* HBQ for the extra ring if needed */
  1673. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1674. .rn = 1,
  1675. .entry_count = 200,
  1676. .mask_count = 0,
  1677. .profile = 0,
  1678. .ring_mask = (1 << LPFC_EXTRA_RING),
  1679. .buffer_count = 0,
  1680. .init_count = 0,
  1681. .add_count = 5,
  1682. };
  1683. /* Array of HBQs */
  1684. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1685. &lpfc_els_hbq,
  1686. &lpfc_extra_hbq,
  1687. };
  1688. /**
  1689. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1690. * @phba: Pointer to HBA context object.
  1691. * @hbqno: HBQ number.
  1692. * @count: Number of HBQ buffers to be posted.
  1693. *
  1694. * This function is called with no lock held to post more hbq buffers to the
  1695. * given HBQ. The function returns the number of HBQ buffers successfully
  1696. * posted.
  1697. **/
  1698. static int
  1699. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1700. {
  1701. uint32_t i, posted = 0;
  1702. unsigned long flags;
  1703. struct hbq_dmabuf *hbq_buffer;
  1704. LIST_HEAD(hbq_buf_list);
  1705. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1706. return 0;
  1707. if ((phba->hbqs[hbqno].buffer_count + count) >
  1708. lpfc_hbq_defs[hbqno]->entry_count)
  1709. count = lpfc_hbq_defs[hbqno]->entry_count -
  1710. phba->hbqs[hbqno].buffer_count;
  1711. if (!count)
  1712. return 0;
  1713. /* Allocate HBQ entries */
  1714. for (i = 0; i < count; i++) {
  1715. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1716. if (!hbq_buffer)
  1717. break;
  1718. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1719. }
  1720. /* Check whether HBQ is still in use */
  1721. spin_lock_irqsave(&phba->hbalock, flags);
  1722. if (!phba->hbq_in_use)
  1723. goto err;
  1724. while (!list_empty(&hbq_buf_list)) {
  1725. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1726. dbuf.list);
  1727. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1728. (hbqno << 16));
  1729. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1730. phba->hbqs[hbqno].buffer_count++;
  1731. posted++;
  1732. } else
  1733. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1734. }
  1735. spin_unlock_irqrestore(&phba->hbalock, flags);
  1736. return posted;
  1737. err:
  1738. spin_unlock_irqrestore(&phba->hbalock, flags);
  1739. while (!list_empty(&hbq_buf_list)) {
  1740. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1741. dbuf.list);
  1742. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1743. }
  1744. return 0;
  1745. }
  1746. /**
  1747. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1748. * @phba: Pointer to HBA context object.
  1749. * @qno: HBQ number.
  1750. *
  1751. * This function posts more buffers to the HBQ. This function
  1752. * is called with no lock held. The function returns the number of HBQ entries
  1753. * successfully allocated.
  1754. **/
  1755. int
  1756. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1757. {
  1758. if (phba->sli_rev == LPFC_SLI_REV4)
  1759. return 0;
  1760. else
  1761. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1762. lpfc_hbq_defs[qno]->add_count);
  1763. }
  1764. /**
  1765. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1766. * @phba: Pointer to HBA context object.
  1767. * @qno: HBQ queue number.
  1768. *
  1769. * This function is called from SLI initialization code path with
  1770. * no lock held to post initial HBQ buffers to firmware. The
  1771. * function returns the number of HBQ entries successfully allocated.
  1772. **/
  1773. static int
  1774. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1775. {
  1776. if (phba->sli_rev == LPFC_SLI_REV4)
  1777. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1778. lpfc_hbq_defs[qno]->entry_count);
  1779. else
  1780. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1781. lpfc_hbq_defs[qno]->init_count);
  1782. }
  1783. /**
  1784. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1785. * @phba: Pointer to HBA context object.
  1786. * @hbqno: HBQ number.
  1787. *
  1788. * This function removes the first hbq buffer on an hbq list and returns a
  1789. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1790. **/
  1791. static struct hbq_dmabuf *
  1792. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1793. {
  1794. struct lpfc_dmabuf *d_buf;
  1795. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1796. if (!d_buf)
  1797. return NULL;
  1798. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1799. }
  1800. /**
  1801. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1802. * @phba: Pointer to HBA context object.
  1803. * @tag: Tag of the hbq buffer.
  1804. *
  1805. * This function is called with hbalock held. This function searches
  1806. * for the hbq buffer associated with the given tag in the hbq buffer
  1807. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1808. * it returns NULL.
  1809. **/
  1810. static struct hbq_dmabuf *
  1811. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1812. {
  1813. struct lpfc_dmabuf *d_buf;
  1814. struct hbq_dmabuf *hbq_buf;
  1815. uint32_t hbqno;
  1816. hbqno = tag >> 16;
  1817. if (hbqno >= LPFC_MAX_HBQS)
  1818. return NULL;
  1819. spin_lock_irq(&phba->hbalock);
  1820. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1821. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1822. if (hbq_buf->tag == tag) {
  1823. spin_unlock_irq(&phba->hbalock);
  1824. return hbq_buf;
  1825. }
  1826. }
  1827. spin_unlock_irq(&phba->hbalock);
  1828. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1829. "1803 Bad hbq tag. Data: x%x x%x\n",
  1830. tag, phba->hbqs[tag >> 16].buffer_count);
  1831. return NULL;
  1832. }
  1833. /**
  1834. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1835. * @phba: Pointer to HBA context object.
  1836. * @hbq_buffer: Pointer to HBQ buffer.
  1837. *
  1838. * This function is called with hbalock. This function gives back
  1839. * the hbq buffer to firmware. If the HBQ does not have space to
  1840. * post the buffer, it will free the buffer.
  1841. **/
  1842. void
  1843. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1844. {
  1845. uint32_t hbqno;
  1846. if (hbq_buffer) {
  1847. hbqno = hbq_buffer->tag >> 16;
  1848. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1849. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1850. }
  1851. }
  1852. /**
  1853. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1854. * @mbxCommand: mailbox command code.
  1855. *
  1856. * This function is called by the mailbox event handler function to verify
  1857. * that the completed mailbox command is a legitimate mailbox command. If the
  1858. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1859. * and the mailbox event handler will take the HBA offline.
  1860. **/
  1861. static int
  1862. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1863. {
  1864. uint8_t ret;
  1865. switch (mbxCommand) {
  1866. case MBX_LOAD_SM:
  1867. case MBX_READ_NV:
  1868. case MBX_WRITE_NV:
  1869. case MBX_WRITE_VPARMS:
  1870. case MBX_RUN_BIU_DIAG:
  1871. case MBX_INIT_LINK:
  1872. case MBX_DOWN_LINK:
  1873. case MBX_CONFIG_LINK:
  1874. case MBX_CONFIG_RING:
  1875. case MBX_RESET_RING:
  1876. case MBX_READ_CONFIG:
  1877. case MBX_READ_RCONFIG:
  1878. case MBX_READ_SPARM:
  1879. case MBX_READ_STATUS:
  1880. case MBX_READ_RPI:
  1881. case MBX_READ_XRI:
  1882. case MBX_READ_REV:
  1883. case MBX_READ_LNK_STAT:
  1884. case MBX_REG_LOGIN:
  1885. case MBX_UNREG_LOGIN:
  1886. case MBX_CLEAR_LA:
  1887. case MBX_DUMP_MEMORY:
  1888. case MBX_DUMP_CONTEXT:
  1889. case MBX_RUN_DIAGS:
  1890. case MBX_RESTART:
  1891. case MBX_UPDATE_CFG:
  1892. case MBX_DOWN_LOAD:
  1893. case MBX_DEL_LD_ENTRY:
  1894. case MBX_RUN_PROGRAM:
  1895. case MBX_SET_MASK:
  1896. case MBX_SET_VARIABLE:
  1897. case MBX_UNREG_D_ID:
  1898. case MBX_KILL_BOARD:
  1899. case MBX_CONFIG_FARP:
  1900. case MBX_BEACON:
  1901. case MBX_LOAD_AREA:
  1902. case MBX_RUN_BIU_DIAG64:
  1903. case MBX_CONFIG_PORT:
  1904. case MBX_READ_SPARM64:
  1905. case MBX_READ_RPI64:
  1906. case MBX_REG_LOGIN64:
  1907. case MBX_READ_TOPOLOGY:
  1908. case MBX_WRITE_WWN:
  1909. case MBX_SET_DEBUG:
  1910. case MBX_LOAD_EXP_ROM:
  1911. case MBX_ASYNCEVT_ENABLE:
  1912. case MBX_REG_VPI:
  1913. case MBX_UNREG_VPI:
  1914. case MBX_HEARTBEAT:
  1915. case MBX_PORT_CAPABILITIES:
  1916. case MBX_PORT_IOV_CONTROL:
  1917. case MBX_SLI4_CONFIG:
  1918. case MBX_SLI4_REQ_FTRS:
  1919. case MBX_REG_FCFI:
  1920. case MBX_UNREG_FCFI:
  1921. case MBX_REG_VFI:
  1922. case MBX_UNREG_VFI:
  1923. case MBX_INIT_VPI:
  1924. case MBX_INIT_VFI:
  1925. case MBX_RESUME_RPI:
  1926. case MBX_READ_EVENT_LOG_STATUS:
  1927. case MBX_READ_EVENT_LOG:
  1928. case MBX_SECURITY_MGMT:
  1929. case MBX_AUTH_PORT:
  1930. case MBX_ACCESS_VDATA:
  1931. ret = mbxCommand;
  1932. break;
  1933. default:
  1934. ret = MBX_SHUTDOWN;
  1935. break;
  1936. }
  1937. return ret;
  1938. }
  1939. /**
  1940. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1941. * @phba: Pointer to HBA context object.
  1942. * @pmboxq: Pointer to mailbox command.
  1943. *
  1944. * This is completion handler function for mailbox commands issued from
  1945. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1946. * mailbox event handler function with no lock held. This function
  1947. * will wake up thread waiting on the wait queue pointed by context1
  1948. * of the mailbox.
  1949. **/
  1950. void
  1951. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1952. {
  1953. wait_queue_head_t *pdone_q;
  1954. unsigned long drvr_flag;
  1955. /*
  1956. * If pdone_q is empty, the driver thread gave up waiting and
  1957. * continued running.
  1958. */
  1959. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1960. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1961. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1962. if (pdone_q)
  1963. wake_up_interruptible(pdone_q);
  1964. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1965. return;
  1966. }
  1967. /**
  1968. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1969. * @phba: Pointer to HBA context object.
  1970. * @pmb: Pointer to mailbox object.
  1971. *
  1972. * This function is the default mailbox completion handler. It
  1973. * frees the memory resources associated with the completed mailbox
  1974. * command. If the completed command is a REG_LOGIN mailbox command,
  1975. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1976. **/
  1977. void
  1978. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1979. {
  1980. struct lpfc_vport *vport = pmb->vport;
  1981. struct lpfc_dmabuf *mp;
  1982. struct lpfc_nodelist *ndlp;
  1983. struct Scsi_Host *shost;
  1984. uint16_t rpi, vpi;
  1985. int rc;
  1986. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1987. if (mp) {
  1988. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1989. kfree(mp);
  1990. }
  1991. /*
  1992. * If a REG_LOGIN succeeded after node is destroyed or node
  1993. * is in re-discovery driver need to cleanup the RPI.
  1994. */
  1995. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1996. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1997. !pmb->u.mb.mbxStatus) {
  1998. rpi = pmb->u.mb.un.varWords[0];
  1999. vpi = pmb->u.mb.un.varRegLogin.vpi;
  2000. lpfc_unreg_login(phba, vpi, rpi, pmb);
  2001. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2002. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2003. if (rc != MBX_NOT_FINISHED)
  2004. return;
  2005. }
  2006. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  2007. !(phba->pport->load_flag & FC_UNLOADING) &&
  2008. !pmb->u.mb.mbxStatus) {
  2009. shost = lpfc_shost_from_vport(vport);
  2010. spin_lock_irq(shost->host_lock);
  2011. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2012. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2013. spin_unlock_irq(shost->host_lock);
  2014. }
  2015. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  2016. ndlp = (struct lpfc_nodelist *)pmb->context2;
  2017. lpfc_nlp_put(ndlp);
  2018. pmb->context2 = NULL;
  2019. }
  2020. /* Check security permission status on INIT_LINK mailbox command */
  2021. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  2022. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  2023. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2024. "2860 SLI authentication is required "
  2025. "for INIT_LINK but has not done yet\n");
  2026. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  2027. lpfc_sli4_mbox_cmd_free(phba, pmb);
  2028. else
  2029. mempool_free(pmb, phba->mbox_mem_pool);
  2030. }
  2031. /**
  2032. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2033. * @phba: Pointer to HBA context object.
  2034. *
  2035. * This function is called with no lock held. This function processes all
  2036. * the completed mailbox commands and gives it to upper layers. The interrupt
  2037. * service routine processes mailbox completion interrupt and adds completed
  2038. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2039. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2040. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2041. * function returns the mailbox commands to the upper layer by calling the
  2042. * completion handler function of each mailbox.
  2043. **/
  2044. int
  2045. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2046. {
  2047. MAILBOX_t *pmbox;
  2048. LPFC_MBOXQ_t *pmb;
  2049. int rc;
  2050. LIST_HEAD(cmplq);
  2051. phba->sli.slistat.mbox_event++;
  2052. /* Get all completed mailboxe buffers into the cmplq */
  2053. spin_lock_irq(&phba->hbalock);
  2054. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2055. spin_unlock_irq(&phba->hbalock);
  2056. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2057. do {
  2058. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2059. if (pmb == NULL)
  2060. break;
  2061. pmbox = &pmb->u.mb;
  2062. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2063. if (pmb->vport) {
  2064. lpfc_debugfs_disc_trc(pmb->vport,
  2065. LPFC_DISC_TRC_MBOX_VPORT,
  2066. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2067. (uint32_t)pmbox->mbxCommand,
  2068. pmbox->un.varWords[0],
  2069. pmbox->un.varWords[1]);
  2070. }
  2071. else {
  2072. lpfc_debugfs_disc_trc(phba->pport,
  2073. LPFC_DISC_TRC_MBOX,
  2074. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2075. (uint32_t)pmbox->mbxCommand,
  2076. pmbox->un.varWords[0],
  2077. pmbox->un.varWords[1]);
  2078. }
  2079. }
  2080. /*
  2081. * It is a fatal error if unknown mbox command completion.
  2082. */
  2083. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2084. MBX_SHUTDOWN) {
  2085. /* Unknown mailbox command compl */
  2086. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2087. "(%d):0323 Unknown Mailbox command "
  2088. "x%x (x%x/x%x) Cmpl\n",
  2089. pmb->vport ? pmb->vport->vpi : 0,
  2090. pmbox->mbxCommand,
  2091. lpfc_sli_config_mbox_subsys_get(phba,
  2092. pmb),
  2093. lpfc_sli_config_mbox_opcode_get(phba,
  2094. pmb));
  2095. phba->link_state = LPFC_HBA_ERROR;
  2096. phba->work_hs = HS_FFER3;
  2097. lpfc_handle_eratt(phba);
  2098. continue;
  2099. }
  2100. if (pmbox->mbxStatus) {
  2101. phba->sli.slistat.mbox_stat_err++;
  2102. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2103. /* Mbox cmd cmpl error - RETRYing */
  2104. lpfc_printf_log(phba, KERN_INFO,
  2105. LOG_MBOX | LOG_SLI,
  2106. "(%d):0305 Mbox cmd cmpl "
  2107. "error - RETRYing Data: x%x "
  2108. "(x%x/x%x) x%x x%x x%x\n",
  2109. pmb->vport ? pmb->vport->vpi : 0,
  2110. pmbox->mbxCommand,
  2111. lpfc_sli_config_mbox_subsys_get(phba,
  2112. pmb),
  2113. lpfc_sli_config_mbox_opcode_get(phba,
  2114. pmb),
  2115. pmbox->mbxStatus,
  2116. pmbox->un.varWords[0],
  2117. pmb->vport->port_state);
  2118. pmbox->mbxStatus = 0;
  2119. pmbox->mbxOwner = OWN_HOST;
  2120. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2121. if (rc != MBX_NOT_FINISHED)
  2122. continue;
  2123. }
  2124. }
  2125. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2126. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2127. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2128. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2129. pmb->vport ? pmb->vport->vpi : 0,
  2130. pmbox->mbxCommand,
  2131. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2132. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2133. pmb->mbox_cmpl,
  2134. *((uint32_t *) pmbox),
  2135. pmbox->un.varWords[0],
  2136. pmbox->un.varWords[1],
  2137. pmbox->un.varWords[2],
  2138. pmbox->un.varWords[3],
  2139. pmbox->un.varWords[4],
  2140. pmbox->un.varWords[5],
  2141. pmbox->un.varWords[6],
  2142. pmbox->un.varWords[7]);
  2143. if (pmb->mbox_cmpl)
  2144. pmb->mbox_cmpl(phba,pmb);
  2145. } while (1);
  2146. return 0;
  2147. }
  2148. /**
  2149. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2150. * @phba: Pointer to HBA context object.
  2151. * @pring: Pointer to driver SLI ring object.
  2152. * @tag: buffer tag.
  2153. *
  2154. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2155. * is set in the tag the buffer is posted for a particular exchange,
  2156. * the function will return the buffer without replacing the buffer.
  2157. * If the buffer is for unsolicited ELS or CT traffic, this function
  2158. * returns the buffer and also posts another buffer to the firmware.
  2159. **/
  2160. static struct lpfc_dmabuf *
  2161. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2162. struct lpfc_sli_ring *pring,
  2163. uint32_t tag)
  2164. {
  2165. struct hbq_dmabuf *hbq_entry;
  2166. if (tag & QUE_BUFTAG_BIT)
  2167. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2168. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2169. if (!hbq_entry)
  2170. return NULL;
  2171. return &hbq_entry->dbuf;
  2172. }
  2173. /**
  2174. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2175. * @phba: Pointer to HBA context object.
  2176. * @pring: Pointer to driver SLI ring object.
  2177. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2178. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2179. * @fch_type: the type for the first frame of the sequence.
  2180. *
  2181. * This function is called with no lock held. This function uses the r_ctl and
  2182. * type of the received sequence to find the correct callback function to call
  2183. * to process the sequence.
  2184. **/
  2185. static int
  2186. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2187. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2188. uint32_t fch_type)
  2189. {
  2190. int i;
  2191. /* unSolicited Responses */
  2192. if (pring->prt[0].profile) {
  2193. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2194. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2195. saveq);
  2196. return 1;
  2197. }
  2198. /* We must search, based on rctl / type
  2199. for the right routine */
  2200. for (i = 0; i < pring->num_mask; i++) {
  2201. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2202. (pring->prt[i].type == fch_type)) {
  2203. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2204. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2205. (phba, pring, saveq);
  2206. return 1;
  2207. }
  2208. }
  2209. return 0;
  2210. }
  2211. /**
  2212. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2213. * @phba: Pointer to HBA context object.
  2214. * @pring: Pointer to driver SLI ring object.
  2215. * @saveq: Pointer to the unsolicited iocb.
  2216. *
  2217. * This function is called with no lock held by the ring event handler
  2218. * when there is an unsolicited iocb posted to the response ring by the
  2219. * firmware. This function gets the buffer associated with the iocbs
  2220. * and calls the event handler for the ring. This function handles both
  2221. * qring buffers and hbq buffers.
  2222. * When the function returns 1 the caller can free the iocb object otherwise
  2223. * upper layer functions will free the iocb objects.
  2224. **/
  2225. static int
  2226. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2227. struct lpfc_iocbq *saveq)
  2228. {
  2229. IOCB_t * irsp;
  2230. WORD5 * w5p;
  2231. uint32_t Rctl, Type;
  2232. uint32_t match;
  2233. struct lpfc_iocbq *iocbq;
  2234. struct lpfc_dmabuf *dmzbuf;
  2235. match = 0;
  2236. irsp = &(saveq->iocb);
  2237. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2238. if (pring->lpfc_sli_rcv_async_status)
  2239. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2240. else
  2241. lpfc_printf_log(phba,
  2242. KERN_WARNING,
  2243. LOG_SLI,
  2244. "0316 Ring %d handler: unexpected "
  2245. "ASYNC_STATUS iocb received evt_code "
  2246. "0x%x\n",
  2247. pring->ringno,
  2248. irsp->un.asyncstat.evt_code);
  2249. return 1;
  2250. }
  2251. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2252. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2253. if (irsp->ulpBdeCount > 0) {
  2254. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2255. irsp->un.ulpWord[3]);
  2256. lpfc_in_buf_free(phba, dmzbuf);
  2257. }
  2258. if (irsp->ulpBdeCount > 1) {
  2259. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2260. irsp->unsli3.sli3Words[3]);
  2261. lpfc_in_buf_free(phba, dmzbuf);
  2262. }
  2263. if (irsp->ulpBdeCount > 2) {
  2264. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2265. irsp->unsli3.sli3Words[7]);
  2266. lpfc_in_buf_free(phba, dmzbuf);
  2267. }
  2268. return 1;
  2269. }
  2270. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2271. if (irsp->ulpBdeCount != 0) {
  2272. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2273. irsp->un.ulpWord[3]);
  2274. if (!saveq->context2)
  2275. lpfc_printf_log(phba,
  2276. KERN_ERR,
  2277. LOG_SLI,
  2278. "0341 Ring %d Cannot find buffer for "
  2279. "an unsolicited iocb. tag 0x%x\n",
  2280. pring->ringno,
  2281. irsp->un.ulpWord[3]);
  2282. }
  2283. if (irsp->ulpBdeCount == 2) {
  2284. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2285. irsp->unsli3.sli3Words[7]);
  2286. if (!saveq->context3)
  2287. lpfc_printf_log(phba,
  2288. KERN_ERR,
  2289. LOG_SLI,
  2290. "0342 Ring %d Cannot find buffer for an"
  2291. " unsolicited iocb. tag 0x%x\n",
  2292. pring->ringno,
  2293. irsp->unsli3.sli3Words[7]);
  2294. }
  2295. list_for_each_entry(iocbq, &saveq->list, list) {
  2296. irsp = &(iocbq->iocb);
  2297. if (irsp->ulpBdeCount != 0) {
  2298. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2299. irsp->un.ulpWord[3]);
  2300. if (!iocbq->context2)
  2301. lpfc_printf_log(phba,
  2302. KERN_ERR,
  2303. LOG_SLI,
  2304. "0343 Ring %d Cannot find "
  2305. "buffer for an unsolicited iocb"
  2306. ". tag 0x%x\n", pring->ringno,
  2307. irsp->un.ulpWord[3]);
  2308. }
  2309. if (irsp->ulpBdeCount == 2) {
  2310. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2311. irsp->unsli3.sli3Words[7]);
  2312. if (!iocbq->context3)
  2313. lpfc_printf_log(phba,
  2314. KERN_ERR,
  2315. LOG_SLI,
  2316. "0344 Ring %d Cannot find "
  2317. "buffer for an unsolicited "
  2318. "iocb. tag 0x%x\n",
  2319. pring->ringno,
  2320. irsp->unsli3.sli3Words[7]);
  2321. }
  2322. }
  2323. }
  2324. if (irsp->ulpBdeCount != 0 &&
  2325. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2326. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2327. int found = 0;
  2328. /* search continue save q for same XRI */
  2329. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2330. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2331. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2332. list_add_tail(&saveq->list, &iocbq->list);
  2333. found = 1;
  2334. break;
  2335. }
  2336. }
  2337. if (!found)
  2338. list_add_tail(&saveq->clist,
  2339. &pring->iocb_continue_saveq);
  2340. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2341. list_del_init(&iocbq->clist);
  2342. saveq = iocbq;
  2343. irsp = &(saveq->iocb);
  2344. } else
  2345. return 0;
  2346. }
  2347. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2348. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2349. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2350. Rctl = FC_RCTL_ELS_REQ;
  2351. Type = FC_TYPE_ELS;
  2352. } else {
  2353. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2354. Rctl = w5p->hcsw.Rctl;
  2355. Type = w5p->hcsw.Type;
  2356. /* Firmware Workaround */
  2357. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2358. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2359. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2360. Rctl = FC_RCTL_ELS_REQ;
  2361. Type = FC_TYPE_ELS;
  2362. w5p->hcsw.Rctl = Rctl;
  2363. w5p->hcsw.Type = Type;
  2364. }
  2365. }
  2366. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2367. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2368. "0313 Ring %d handler: unexpected Rctl x%x "
  2369. "Type x%x received\n",
  2370. pring->ringno, Rctl, Type);
  2371. return 1;
  2372. }
  2373. /**
  2374. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2375. * @phba: Pointer to HBA context object.
  2376. * @pring: Pointer to driver SLI ring object.
  2377. * @prspiocb: Pointer to response iocb object.
  2378. *
  2379. * This function looks up the iocb_lookup table to get the command iocb
  2380. * corresponding to the given response iocb using the iotag of the
  2381. * response iocb. This function is called with the hbalock held.
  2382. * This function returns the command iocb object if it finds the command
  2383. * iocb else returns NULL.
  2384. **/
  2385. static struct lpfc_iocbq *
  2386. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2387. struct lpfc_sli_ring *pring,
  2388. struct lpfc_iocbq *prspiocb)
  2389. {
  2390. struct lpfc_iocbq *cmd_iocb = NULL;
  2391. uint16_t iotag;
  2392. iotag = prspiocb->iocb.ulpIoTag;
  2393. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2394. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2395. list_del_init(&cmd_iocb->list);
  2396. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2397. pring->txcmplq_cnt--;
  2398. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2399. }
  2400. return cmd_iocb;
  2401. }
  2402. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2403. "0317 iotag x%x is out off "
  2404. "range: max iotag x%x wd0 x%x\n",
  2405. iotag, phba->sli.last_iotag,
  2406. *(((uint32_t *) &prspiocb->iocb) + 7));
  2407. return NULL;
  2408. }
  2409. /**
  2410. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2411. * @phba: Pointer to HBA context object.
  2412. * @pring: Pointer to driver SLI ring object.
  2413. * @iotag: IOCB tag.
  2414. *
  2415. * This function looks up the iocb_lookup table to get the command iocb
  2416. * corresponding to the given iotag. This function is called with the
  2417. * hbalock held.
  2418. * This function returns the command iocb object if it finds the command
  2419. * iocb else returns NULL.
  2420. **/
  2421. static struct lpfc_iocbq *
  2422. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2423. struct lpfc_sli_ring *pring, uint16_t iotag)
  2424. {
  2425. struct lpfc_iocbq *cmd_iocb;
  2426. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2427. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2428. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2429. /* remove from txcmpl queue list */
  2430. list_del_init(&cmd_iocb->list);
  2431. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2432. pring->txcmplq_cnt--;
  2433. return cmd_iocb;
  2434. }
  2435. }
  2436. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2437. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2438. iotag, phba->sli.last_iotag);
  2439. return NULL;
  2440. }
  2441. /**
  2442. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2443. * @phba: Pointer to HBA context object.
  2444. * @pring: Pointer to driver SLI ring object.
  2445. * @saveq: Pointer to the response iocb to be processed.
  2446. *
  2447. * This function is called by the ring event handler for non-fcp
  2448. * rings when there is a new response iocb in the response ring.
  2449. * The caller is not required to hold any locks. This function
  2450. * gets the command iocb associated with the response iocb and
  2451. * calls the completion handler for the command iocb. If there
  2452. * is no completion handler, the function will free the resources
  2453. * associated with command iocb. If the response iocb is for
  2454. * an already aborted command iocb, the status of the completion
  2455. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2456. * This function always returns 1.
  2457. **/
  2458. static int
  2459. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2460. struct lpfc_iocbq *saveq)
  2461. {
  2462. struct lpfc_iocbq *cmdiocbp;
  2463. int rc = 1;
  2464. unsigned long iflag;
  2465. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2466. spin_lock_irqsave(&phba->hbalock, iflag);
  2467. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2468. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2469. if (cmdiocbp) {
  2470. if (cmdiocbp->iocb_cmpl) {
  2471. /*
  2472. * If an ELS command failed send an event to mgmt
  2473. * application.
  2474. */
  2475. if (saveq->iocb.ulpStatus &&
  2476. (pring->ringno == LPFC_ELS_RING) &&
  2477. (cmdiocbp->iocb.ulpCommand ==
  2478. CMD_ELS_REQUEST64_CR))
  2479. lpfc_send_els_failure_event(phba,
  2480. cmdiocbp, saveq);
  2481. /*
  2482. * Post all ELS completions to the worker thread.
  2483. * All other are passed to the completion callback.
  2484. */
  2485. if (pring->ringno == LPFC_ELS_RING) {
  2486. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2487. (cmdiocbp->iocb_flag &
  2488. LPFC_DRIVER_ABORTED)) {
  2489. spin_lock_irqsave(&phba->hbalock,
  2490. iflag);
  2491. cmdiocbp->iocb_flag &=
  2492. ~LPFC_DRIVER_ABORTED;
  2493. spin_unlock_irqrestore(&phba->hbalock,
  2494. iflag);
  2495. saveq->iocb.ulpStatus =
  2496. IOSTAT_LOCAL_REJECT;
  2497. saveq->iocb.un.ulpWord[4] =
  2498. IOERR_SLI_ABORTED;
  2499. /* Firmware could still be in progress
  2500. * of DMAing payload, so don't free data
  2501. * buffer till after a hbeat.
  2502. */
  2503. spin_lock_irqsave(&phba->hbalock,
  2504. iflag);
  2505. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2506. spin_unlock_irqrestore(&phba->hbalock,
  2507. iflag);
  2508. }
  2509. if (phba->sli_rev == LPFC_SLI_REV4) {
  2510. if (saveq->iocb_flag &
  2511. LPFC_EXCHANGE_BUSY) {
  2512. /* Set cmdiocb flag for the
  2513. * exchange busy so sgl (xri)
  2514. * will not be released until
  2515. * the abort xri is received
  2516. * from hba.
  2517. */
  2518. spin_lock_irqsave(
  2519. &phba->hbalock, iflag);
  2520. cmdiocbp->iocb_flag |=
  2521. LPFC_EXCHANGE_BUSY;
  2522. spin_unlock_irqrestore(
  2523. &phba->hbalock, iflag);
  2524. }
  2525. if (cmdiocbp->iocb_flag &
  2526. LPFC_DRIVER_ABORTED) {
  2527. /*
  2528. * Clear LPFC_DRIVER_ABORTED
  2529. * bit in case it was driver
  2530. * initiated abort.
  2531. */
  2532. spin_lock_irqsave(
  2533. &phba->hbalock, iflag);
  2534. cmdiocbp->iocb_flag &=
  2535. ~LPFC_DRIVER_ABORTED;
  2536. spin_unlock_irqrestore(
  2537. &phba->hbalock, iflag);
  2538. cmdiocbp->iocb.ulpStatus =
  2539. IOSTAT_LOCAL_REJECT;
  2540. cmdiocbp->iocb.un.ulpWord[4] =
  2541. IOERR_ABORT_REQUESTED;
  2542. /*
  2543. * For SLI4, irsiocb contains
  2544. * NO_XRI in sli_xritag, it
  2545. * shall not affect releasing
  2546. * sgl (xri) process.
  2547. */
  2548. saveq->iocb.ulpStatus =
  2549. IOSTAT_LOCAL_REJECT;
  2550. saveq->iocb.un.ulpWord[4] =
  2551. IOERR_SLI_ABORTED;
  2552. spin_lock_irqsave(
  2553. &phba->hbalock, iflag);
  2554. saveq->iocb_flag |=
  2555. LPFC_DELAY_MEM_FREE;
  2556. spin_unlock_irqrestore(
  2557. &phba->hbalock, iflag);
  2558. }
  2559. }
  2560. }
  2561. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2562. } else
  2563. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2564. } else {
  2565. /*
  2566. * Unknown initiating command based on the response iotag.
  2567. * This could be the case on the ELS ring because of
  2568. * lpfc_els_abort().
  2569. */
  2570. if (pring->ringno != LPFC_ELS_RING) {
  2571. /*
  2572. * Ring <ringno> handler: unexpected completion IoTag
  2573. * <IoTag>
  2574. */
  2575. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2576. "0322 Ring %d handler: "
  2577. "unexpected completion IoTag x%x "
  2578. "Data: x%x x%x x%x x%x\n",
  2579. pring->ringno,
  2580. saveq->iocb.ulpIoTag,
  2581. saveq->iocb.ulpStatus,
  2582. saveq->iocb.un.ulpWord[4],
  2583. saveq->iocb.ulpCommand,
  2584. saveq->iocb.ulpContext);
  2585. }
  2586. }
  2587. return rc;
  2588. }
  2589. /**
  2590. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2591. * @phba: Pointer to HBA context object.
  2592. * @pring: Pointer to driver SLI ring object.
  2593. *
  2594. * This function is called from the iocb ring event handlers when
  2595. * put pointer is ahead of the get pointer for a ring. This function signal
  2596. * an error attention condition to the worker thread and the worker
  2597. * thread will transition the HBA to offline state.
  2598. **/
  2599. static void
  2600. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2601. {
  2602. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2603. /*
  2604. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2605. * rsp ring <portRspMax>
  2606. */
  2607. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2608. "0312 Ring %d handler: portRspPut %d "
  2609. "is bigger than rsp ring %d\n",
  2610. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2611. pring->sli.sli3.numRiocb);
  2612. phba->link_state = LPFC_HBA_ERROR;
  2613. /*
  2614. * All error attention handlers are posted to
  2615. * worker thread
  2616. */
  2617. phba->work_ha |= HA_ERATT;
  2618. phba->work_hs = HS_FFER3;
  2619. lpfc_worker_wake_up(phba);
  2620. return;
  2621. }
  2622. /**
  2623. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2624. * @ptr: Pointer to address of HBA context object.
  2625. *
  2626. * This function is invoked by the Error Attention polling timer when the
  2627. * timer times out. It will check the SLI Error Attention register for
  2628. * possible attention events. If so, it will post an Error Attention event
  2629. * and wake up worker thread to process it. Otherwise, it will set up the
  2630. * Error Attention polling timer for the next poll.
  2631. **/
  2632. void lpfc_poll_eratt(unsigned long ptr)
  2633. {
  2634. struct lpfc_hba *phba;
  2635. uint32_t eratt = 0, rem;
  2636. uint64_t sli_intr, cnt;
  2637. phba = (struct lpfc_hba *)ptr;
  2638. /* Here we will also keep track of interrupts per sec of the hba */
  2639. sli_intr = phba->sli.slistat.sli_intr;
  2640. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2641. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2642. sli_intr);
  2643. else
  2644. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2645. /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
  2646. rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
  2647. phba->sli.slistat.sli_ips = cnt;
  2648. phba->sli.slistat.sli_prev_intr = sli_intr;
  2649. /* Check chip HA register for error event */
  2650. eratt = lpfc_sli_check_eratt(phba);
  2651. if (eratt)
  2652. /* Tell the worker thread there is work to do */
  2653. lpfc_worker_wake_up(phba);
  2654. else
  2655. /* Restart the timer for next eratt poll */
  2656. mod_timer(&phba->eratt_poll, jiffies +
  2657. HZ * LPFC_ERATT_POLL_INTERVAL);
  2658. return;
  2659. }
  2660. /**
  2661. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2662. * @phba: Pointer to HBA context object.
  2663. * @pring: Pointer to driver SLI ring object.
  2664. * @mask: Host attention register mask for this ring.
  2665. *
  2666. * This function is called from the interrupt context when there is a ring
  2667. * event for the fcp ring. The caller does not hold any lock.
  2668. * The function processes each response iocb in the response ring until it
  2669. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2670. * LE bit set. The function will call the completion handler of the command iocb
  2671. * if the response iocb indicates a completion for a command iocb or it is
  2672. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2673. * function if this is an unsolicited iocb.
  2674. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2675. * to check it explicitly.
  2676. */
  2677. int
  2678. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2679. struct lpfc_sli_ring *pring, uint32_t mask)
  2680. {
  2681. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2682. IOCB_t *irsp = NULL;
  2683. IOCB_t *entry = NULL;
  2684. struct lpfc_iocbq *cmdiocbq = NULL;
  2685. struct lpfc_iocbq rspiocbq;
  2686. uint32_t status;
  2687. uint32_t portRspPut, portRspMax;
  2688. int rc = 1;
  2689. lpfc_iocb_type type;
  2690. unsigned long iflag;
  2691. uint32_t rsp_cmpl = 0;
  2692. spin_lock_irqsave(&phba->hbalock, iflag);
  2693. pring->stats.iocb_event++;
  2694. /*
  2695. * The next available response entry should never exceed the maximum
  2696. * entries. If it does, treat it as an adapter hardware error.
  2697. */
  2698. portRspMax = pring->sli.sli3.numRiocb;
  2699. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2700. if (unlikely(portRspPut >= portRspMax)) {
  2701. lpfc_sli_rsp_pointers_error(phba, pring);
  2702. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2703. return 1;
  2704. }
  2705. if (phba->fcp_ring_in_use) {
  2706. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2707. return 1;
  2708. } else
  2709. phba->fcp_ring_in_use = 1;
  2710. rmb();
  2711. while (pring->sli.sli3.rspidx != portRspPut) {
  2712. /*
  2713. * Fetch an entry off the ring and copy it into a local data
  2714. * structure. The copy involves a byte-swap since the
  2715. * network byte order and pci byte orders are different.
  2716. */
  2717. entry = lpfc_resp_iocb(phba, pring);
  2718. phba->last_completion_time = jiffies;
  2719. if (++pring->sli.sli3.rspidx >= portRspMax)
  2720. pring->sli.sli3.rspidx = 0;
  2721. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2722. (uint32_t *) &rspiocbq.iocb,
  2723. phba->iocb_rsp_size);
  2724. INIT_LIST_HEAD(&(rspiocbq.list));
  2725. irsp = &rspiocbq.iocb;
  2726. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2727. pring->stats.iocb_rsp++;
  2728. rsp_cmpl++;
  2729. if (unlikely(irsp->ulpStatus)) {
  2730. /*
  2731. * If resource errors reported from HBA, reduce
  2732. * queuedepths of the SCSI device.
  2733. */
  2734. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2735. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2736. IOERR_NO_RESOURCES)) {
  2737. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2738. phba->lpfc_rampdown_queue_depth(phba);
  2739. spin_lock_irqsave(&phba->hbalock, iflag);
  2740. }
  2741. /* Rsp ring <ringno> error: IOCB */
  2742. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2743. "0336 Rsp Ring %d error: IOCB Data: "
  2744. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2745. pring->ringno,
  2746. irsp->un.ulpWord[0],
  2747. irsp->un.ulpWord[1],
  2748. irsp->un.ulpWord[2],
  2749. irsp->un.ulpWord[3],
  2750. irsp->un.ulpWord[4],
  2751. irsp->un.ulpWord[5],
  2752. *(uint32_t *)&irsp->un1,
  2753. *((uint32_t *)&irsp->un1 + 1));
  2754. }
  2755. switch (type) {
  2756. case LPFC_ABORT_IOCB:
  2757. case LPFC_SOL_IOCB:
  2758. /*
  2759. * Idle exchange closed via ABTS from port. No iocb
  2760. * resources need to be recovered.
  2761. */
  2762. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2763. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2764. "0333 IOCB cmd 0x%x"
  2765. " processed. Skipping"
  2766. " completion\n",
  2767. irsp->ulpCommand);
  2768. break;
  2769. }
  2770. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2771. &rspiocbq);
  2772. if (unlikely(!cmdiocbq))
  2773. break;
  2774. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2775. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2776. if (cmdiocbq->iocb_cmpl) {
  2777. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2778. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2779. &rspiocbq);
  2780. spin_lock_irqsave(&phba->hbalock, iflag);
  2781. }
  2782. break;
  2783. case LPFC_UNSOL_IOCB:
  2784. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2785. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2786. spin_lock_irqsave(&phba->hbalock, iflag);
  2787. break;
  2788. default:
  2789. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2790. char adaptermsg[LPFC_MAX_ADPTMSG];
  2791. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2792. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2793. MAX_MSG_DATA);
  2794. dev_warn(&((phba->pcidev)->dev),
  2795. "lpfc%d: %s\n",
  2796. phba->brd_no, adaptermsg);
  2797. } else {
  2798. /* Unknown IOCB command */
  2799. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2800. "0334 Unknown IOCB command "
  2801. "Data: x%x, x%x x%x x%x x%x\n",
  2802. type, irsp->ulpCommand,
  2803. irsp->ulpStatus,
  2804. irsp->ulpIoTag,
  2805. irsp->ulpContext);
  2806. }
  2807. break;
  2808. }
  2809. /*
  2810. * The response IOCB has been processed. Update the ring
  2811. * pointer in SLIM. If the port response put pointer has not
  2812. * been updated, sync the pgp->rspPutInx and fetch the new port
  2813. * response put pointer.
  2814. */
  2815. writel(pring->sli.sli3.rspidx,
  2816. &phba->host_gp[pring->ringno].rspGetInx);
  2817. if (pring->sli.sli3.rspidx == portRspPut)
  2818. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2819. }
  2820. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2821. pring->stats.iocb_rsp_full++;
  2822. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2823. writel(status, phba->CAregaddr);
  2824. readl(phba->CAregaddr);
  2825. }
  2826. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2827. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2828. pring->stats.iocb_cmd_empty++;
  2829. /* Force update of the local copy of cmdGetInx */
  2830. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2831. lpfc_sli_resume_iocb(phba, pring);
  2832. if ((pring->lpfc_sli_cmd_available))
  2833. (pring->lpfc_sli_cmd_available) (phba, pring);
  2834. }
  2835. phba->fcp_ring_in_use = 0;
  2836. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2837. return rc;
  2838. }
  2839. /**
  2840. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2841. * @phba: Pointer to HBA context object.
  2842. * @pring: Pointer to driver SLI ring object.
  2843. * @rspiocbp: Pointer to driver response IOCB object.
  2844. *
  2845. * This function is called from the worker thread when there is a slow-path
  2846. * response IOCB to process. This function chains all the response iocbs until
  2847. * seeing the iocb with the LE bit set. The function will call
  2848. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2849. * completion of a command iocb. The function will call the
  2850. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2851. * The function frees the resources or calls the completion handler if this
  2852. * iocb is an abort completion. The function returns NULL when the response
  2853. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2854. * this function shall chain the iocb on to the iocb_continueq and return the
  2855. * response iocb passed in.
  2856. **/
  2857. static struct lpfc_iocbq *
  2858. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2859. struct lpfc_iocbq *rspiocbp)
  2860. {
  2861. struct lpfc_iocbq *saveq;
  2862. struct lpfc_iocbq *cmdiocbp;
  2863. struct lpfc_iocbq *next_iocb;
  2864. IOCB_t *irsp = NULL;
  2865. uint32_t free_saveq;
  2866. uint8_t iocb_cmd_type;
  2867. lpfc_iocb_type type;
  2868. unsigned long iflag;
  2869. int rc;
  2870. spin_lock_irqsave(&phba->hbalock, iflag);
  2871. /* First add the response iocb to the countinueq list */
  2872. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2873. pring->iocb_continueq_cnt++;
  2874. /* Now, determine whether the list is completed for processing */
  2875. irsp = &rspiocbp->iocb;
  2876. if (irsp->ulpLe) {
  2877. /*
  2878. * By default, the driver expects to free all resources
  2879. * associated with this iocb completion.
  2880. */
  2881. free_saveq = 1;
  2882. saveq = list_get_first(&pring->iocb_continueq,
  2883. struct lpfc_iocbq, list);
  2884. irsp = &(saveq->iocb);
  2885. list_del_init(&pring->iocb_continueq);
  2886. pring->iocb_continueq_cnt = 0;
  2887. pring->stats.iocb_rsp++;
  2888. /*
  2889. * If resource errors reported from HBA, reduce
  2890. * queuedepths of the SCSI device.
  2891. */
  2892. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2893. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2894. IOERR_NO_RESOURCES)) {
  2895. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2896. phba->lpfc_rampdown_queue_depth(phba);
  2897. spin_lock_irqsave(&phba->hbalock, iflag);
  2898. }
  2899. if (irsp->ulpStatus) {
  2900. /* Rsp ring <ringno> error: IOCB */
  2901. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2902. "0328 Rsp Ring %d error: "
  2903. "IOCB Data: "
  2904. "x%x x%x x%x x%x "
  2905. "x%x x%x x%x x%x "
  2906. "x%x x%x x%x x%x "
  2907. "x%x x%x x%x x%x\n",
  2908. pring->ringno,
  2909. irsp->un.ulpWord[0],
  2910. irsp->un.ulpWord[1],
  2911. irsp->un.ulpWord[2],
  2912. irsp->un.ulpWord[3],
  2913. irsp->un.ulpWord[4],
  2914. irsp->un.ulpWord[5],
  2915. *(((uint32_t *) irsp) + 6),
  2916. *(((uint32_t *) irsp) + 7),
  2917. *(((uint32_t *) irsp) + 8),
  2918. *(((uint32_t *) irsp) + 9),
  2919. *(((uint32_t *) irsp) + 10),
  2920. *(((uint32_t *) irsp) + 11),
  2921. *(((uint32_t *) irsp) + 12),
  2922. *(((uint32_t *) irsp) + 13),
  2923. *(((uint32_t *) irsp) + 14),
  2924. *(((uint32_t *) irsp) + 15));
  2925. }
  2926. /*
  2927. * Fetch the IOCB command type and call the correct completion
  2928. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2929. * get freed back to the lpfc_iocb_list by the discovery
  2930. * kernel thread.
  2931. */
  2932. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2933. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2934. switch (type) {
  2935. case LPFC_SOL_IOCB:
  2936. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2937. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2938. spin_lock_irqsave(&phba->hbalock, iflag);
  2939. break;
  2940. case LPFC_UNSOL_IOCB:
  2941. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2942. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2943. spin_lock_irqsave(&phba->hbalock, iflag);
  2944. if (!rc)
  2945. free_saveq = 0;
  2946. break;
  2947. case LPFC_ABORT_IOCB:
  2948. cmdiocbp = NULL;
  2949. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2950. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2951. saveq);
  2952. if (cmdiocbp) {
  2953. /* Call the specified completion routine */
  2954. if (cmdiocbp->iocb_cmpl) {
  2955. spin_unlock_irqrestore(&phba->hbalock,
  2956. iflag);
  2957. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2958. saveq);
  2959. spin_lock_irqsave(&phba->hbalock,
  2960. iflag);
  2961. } else
  2962. __lpfc_sli_release_iocbq(phba,
  2963. cmdiocbp);
  2964. }
  2965. break;
  2966. case LPFC_UNKNOWN_IOCB:
  2967. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2968. char adaptermsg[LPFC_MAX_ADPTMSG];
  2969. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2970. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2971. MAX_MSG_DATA);
  2972. dev_warn(&((phba->pcidev)->dev),
  2973. "lpfc%d: %s\n",
  2974. phba->brd_no, adaptermsg);
  2975. } else {
  2976. /* Unknown IOCB command */
  2977. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2978. "0335 Unknown IOCB "
  2979. "command Data: x%x "
  2980. "x%x x%x x%x\n",
  2981. irsp->ulpCommand,
  2982. irsp->ulpStatus,
  2983. irsp->ulpIoTag,
  2984. irsp->ulpContext);
  2985. }
  2986. break;
  2987. }
  2988. if (free_saveq) {
  2989. list_for_each_entry_safe(rspiocbp, next_iocb,
  2990. &saveq->list, list) {
  2991. list_del(&rspiocbp->list);
  2992. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2993. }
  2994. __lpfc_sli_release_iocbq(phba, saveq);
  2995. }
  2996. rspiocbp = NULL;
  2997. }
  2998. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2999. return rspiocbp;
  3000. }
  3001. /**
  3002. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  3003. * @phba: Pointer to HBA context object.
  3004. * @pring: Pointer to driver SLI ring object.
  3005. * @mask: Host attention register mask for this ring.
  3006. *
  3007. * This routine wraps the actual slow_ring event process routine from the
  3008. * API jump table function pointer from the lpfc_hba struct.
  3009. **/
  3010. void
  3011. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  3012. struct lpfc_sli_ring *pring, uint32_t mask)
  3013. {
  3014. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  3015. }
  3016. /**
  3017. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  3018. * @phba: Pointer to HBA context object.
  3019. * @pring: Pointer to driver SLI ring object.
  3020. * @mask: Host attention register mask for this ring.
  3021. *
  3022. * This function is called from the worker thread when there is a ring event
  3023. * for non-fcp rings. The caller does not hold any lock. The function will
  3024. * remove each response iocb in the response ring and calls the handle
  3025. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3026. **/
  3027. static void
  3028. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  3029. struct lpfc_sli_ring *pring, uint32_t mask)
  3030. {
  3031. struct lpfc_pgp *pgp;
  3032. IOCB_t *entry;
  3033. IOCB_t *irsp = NULL;
  3034. struct lpfc_iocbq *rspiocbp = NULL;
  3035. uint32_t portRspPut, portRspMax;
  3036. unsigned long iflag;
  3037. uint32_t status;
  3038. pgp = &phba->port_gp[pring->ringno];
  3039. spin_lock_irqsave(&phba->hbalock, iflag);
  3040. pring->stats.iocb_event++;
  3041. /*
  3042. * The next available response entry should never exceed the maximum
  3043. * entries. If it does, treat it as an adapter hardware error.
  3044. */
  3045. portRspMax = pring->sli.sli3.numRiocb;
  3046. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3047. if (portRspPut >= portRspMax) {
  3048. /*
  3049. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3050. * rsp ring <portRspMax>
  3051. */
  3052. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3053. "0303 Ring %d handler: portRspPut %d "
  3054. "is bigger than rsp ring %d\n",
  3055. pring->ringno, portRspPut, portRspMax);
  3056. phba->link_state = LPFC_HBA_ERROR;
  3057. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3058. phba->work_hs = HS_FFER3;
  3059. lpfc_handle_eratt(phba);
  3060. return;
  3061. }
  3062. rmb();
  3063. while (pring->sli.sli3.rspidx != portRspPut) {
  3064. /*
  3065. * Build a completion list and call the appropriate handler.
  3066. * The process is to get the next available response iocb, get
  3067. * a free iocb from the list, copy the response data into the
  3068. * free iocb, insert to the continuation list, and update the
  3069. * next response index to slim. This process makes response
  3070. * iocb's in the ring available to DMA as fast as possible but
  3071. * pays a penalty for a copy operation. Since the iocb is
  3072. * only 32 bytes, this penalty is considered small relative to
  3073. * the PCI reads for register values and a slim write. When
  3074. * the ulpLe field is set, the entire Command has been
  3075. * received.
  3076. */
  3077. entry = lpfc_resp_iocb(phba, pring);
  3078. phba->last_completion_time = jiffies;
  3079. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3080. if (rspiocbp == NULL) {
  3081. printk(KERN_ERR "%s: out of buffers! Failing "
  3082. "completion.\n", __func__);
  3083. break;
  3084. }
  3085. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3086. phba->iocb_rsp_size);
  3087. irsp = &rspiocbp->iocb;
  3088. if (++pring->sli.sli3.rspidx >= portRspMax)
  3089. pring->sli.sli3.rspidx = 0;
  3090. if (pring->ringno == LPFC_ELS_RING) {
  3091. lpfc_debugfs_slow_ring_trc(phba,
  3092. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3093. *(((uint32_t *) irsp) + 4),
  3094. *(((uint32_t *) irsp) + 6),
  3095. *(((uint32_t *) irsp) + 7));
  3096. }
  3097. writel(pring->sli.sli3.rspidx,
  3098. &phba->host_gp[pring->ringno].rspGetInx);
  3099. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3100. /* Handle the response IOCB */
  3101. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3102. spin_lock_irqsave(&phba->hbalock, iflag);
  3103. /*
  3104. * If the port response put pointer has not been updated, sync
  3105. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3106. * response put pointer.
  3107. */
  3108. if (pring->sli.sli3.rspidx == portRspPut) {
  3109. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3110. }
  3111. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3112. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3113. /* At least one response entry has been freed */
  3114. pring->stats.iocb_rsp_full++;
  3115. /* SET RxRE_RSP in Chip Att register */
  3116. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3117. writel(status, phba->CAregaddr);
  3118. readl(phba->CAregaddr); /* flush */
  3119. }
  3120. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3121. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3122. pring->stats.iocb_cmd_empty++;
  3123. /* Force update of the local copy of cmdGetInx */
  3124. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3125. lpfc_sli_resume_iocb(phba, pring);
  3126. if ((pring->lpfc_sli_cmd_available))
  3127. (pring->lpfc_sli_cmd_available) (phba, pring);
  3128. }
  3129. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3130. return;
  3131. }
  3132. /**
  3133. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3134. * @phba: Pointer to HBA context object.
  3135. * @pring: Pointer to driver SLI ring object.
  3136. * @mask: Host attention register mask for this ring.
  3137. *
  3138. * This function is called from the worker thread when there is a pending
  3139. * ELS response iocb on the driver internal slow-path response iocb worker
  3140. * queue. The caller does not hold any lock. The function will remove each
  3141. * response iocb from the response worker queue and calls the handle
  3142. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3143. **/
  3144. static void
  3145. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3146. struct lpfc_sli_ring *pring, uint32_t mask)
  3147. {
  3148. struct lpfc_iocbq *irspiocbq;
  3149. struct hbq_dmabuf *dmabuf;
  3150. struct lpfc_cq_event *cq_event;
  3151. unsigned long iflag;
  3152. spin_lock_irqsave(&phba->hbalock, iflag);
  3153. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3154. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3155. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3156. /* Get the response iocb from the head of work queue */
  3157. spin_lock_irqsave(&phba->hbalock, iflag);
  3158. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3159. cq_event, struct lpfc_cq_event, list);
  3160. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3161. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3162. case CQE_CODE_COMPL_WQE:
  3163. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3164. cq_event);
  3165. /* Translate ELS WCQE to response IOCBQ */
  3166. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3167. irspiocbq);
  3168. if (irspiocbq)
  3169. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3170. irspiocbq);
  3171. break;
  3172. case CQE_CODE_RECEIVE:
  3173. case CQE_CODE_RECEIVE_V1:
  3174. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3175. cq_event);
  3176. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3177. break;
  3178. default:
  3179. break;
  3180. }
  3181. }
  3182. }
  3183. /**
  3184. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3185. * @phba: Pointer to HBA context object.
  3186. * @pring: Pointer to driver SLI ring object.
  3187. *
  3188. * This function aborts all iocbs in the given ring and frees all the iocb
  3189. * objects in txq. This function issues an abort iocb for all the iocb commands
  3190. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3191. * the return of this function. The caller is not required to hold any locks.
  3192. **/
  3193. void
  3194. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3195. {
  3196. LIST_HEAD(completions);
  3197. struct lpfc_iocbq *iocb, *next_iocb;
  3198. if (pring->ringno == LPFC_ELS_RING) {
  3199. lpfc_fabric_abort_hba(phba);
  3200. }
  3201. /* Error everything on txq and txcmplq
  3202. * First do the txq.
  3203. */
  3204. spin_lock_irq(&phba->hbalock);
  3205. list_splice_init(&pring->txq, &completions);
  3206. pring->txq_cnt = 0;
  3207. /* Next issue ABTS for everything on the txcmplq */
  3208. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3209. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3210. spin_unlock_irq(&phba->hbalock);
  3211. /* Cancel all the IOCBs from the completions list */
  3212. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3213. IOERR_SLI_ABORTED);
  3214. }
  3215. /**
  3216. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3217. * @phba: Pointer to HBA context object.
  3218. *
  3219. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3220. * objects in txq and txcmplq. This function will not issue abort iocbs
  3221. * for all the iocb commands in txcmplq, they will just be returned with
  3222. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3223. * slot has been permanently disabled.
  3224. **/
  3225. void
  3226. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3227. {
  3228. LIST_HEAD(txq);
  3229. LIST_HEAD(txcmplq);
  3230. struct lpfc_sli *psli = &phba->sli;
  3231. struct lpfc_sli_ring *pring;
  3232. /* Currently, only one fcp ring */
  3233. pring = &psli->ring[psli->fcp_ring];
  3234. spin_lock_irq(&phba->hbalock);
  3235. /* Retrieve everything on txq */
  3236. list_splice_init(&pring->txq, &txq);
  3237. pring->txq_cnt = 0;
  3238. /* Retrieve everything on the txcmplq */
  3239. list_splice_init(&pring->txcmplq, &txcmplq);
  3240. pring->txcmplq_cnt = 0;
  3241. /* Indicate the I/O queues are flushed */
  3242. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3243. spin_unlock_irq(&phba->hbalock);
  3244. /* Flush the txq */
  3245. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3246. IOERR_SLI_DOWN);
  3247. /* Flush the txcmpq */
  3248. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3249. IOERR_SLI_DOWN);
  3250. }
  3251. /**
  3252. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3253. * @phba: Pointer to HBA context object.
  3254. * @mask: Bit mask to be checked.
  3255. *
  3256. * This function reads the host status register and compares
  3257. * with the provided bit mask to check if HBA completed
  3258. * the restart. This function will wait in a loop for the
  3259. * HBA to complete restart. If the HBA does not restart within
  3260. * 15 iterations, the function will reset the HBA again. The
  3261. * function returns 1 when HBA fail to restart otherwise returns
  3262. * zero.
  3263. **/
  3264. static int
  3265. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3266. {
  3267. uint32_t status;
  3268. int i = 0;
  3269. int retval = 0;
  3270. /* Read the HBA Host Status Register */
  3271. if (lpfc_readl(phba->HSregaddr, &status))
  3272. return 1;
  3273. /*
  3274. * Check status register every 100ms for 5 retries, then every
  3275. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3276. * every 2.5 sec for 4.
  3277. * Break our of the loop if errors occurred during init.
  3278. */
  3279. while (((status & mask) != mask) &&
  3280. !(status & HS_FFERM) &&
  3281. i++ < 20) {
  3282. if (i <= 5)
  3283. msleep(10);
  3284. else if (i <= 10)
  3285. msleep(500);
  3286. else
  3287. msleep(2500);
  3288. if (i == 15) {
  3289. /* Do post */
  3290. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3291. lpfc_sli_brdrestart(phba);
  3292. }
  3293. /* Read the HBA Host Status Register */
  3294. if (lpfc_readl(phba->HSregaddr, &status)) {
  3295. retval = 1;
  3296. break;
  3297. }
  3298. }
  3299. /* Check to see if any errors occurred during init */
  3300. if ((status & HS_FFERM) || (i >= 20)) {
  3301. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3302. "2751 Adapter failed to restart, "
  3303. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3304. status,
  3305. readl(phba->MBslimaddr + 0xa8),
  3306. readl(phba->MBslimaddr + 0xac));
  3307. phba->link_state = LPFC_HBA_ERROR;
  3308. retval = 1;
  3309. }
  3310. return retval;
  3311. }
  3312. /**
  3313. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3314. * @phba: Pointer to HBA context object.
  3315. * @mask: Bit mask to be checked.
  3316. *
  3317. * This function checks the host status register to check if HBA is
  3318. * ready. This function will wait in a loop for the HBA to be ready
  3319. * If the HBA is not ready , the function will will reset the HBA PCI
  3320. * function again. The function returns 1 when HBA fail to be ready
  3321. * otherwise returns zero.
  3322. **/
  3323. static int
  3324. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3325. {
  3326. uint32_t status;
  3327. int retval = 0;
  3328. /* Read the HBA Host Status Register */
  3329. status = lpfc_sli4_post_status_check(phba);
  3330. if (status) {
  3331. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3332. lpfc_sli_brdrestart(phba);
  3333. status = lpfc_sli4_post_status_check(phba);
  3334. }
  3335. /* Check to see if any errors occurred during init */
  3336. if (status) {
  3337. phba->link_state = LPFC_HBA_ERROR;
  3338. retval = 1;
  3339. } else
  3340. phba->sli4_hba.intr_enable = 0;
  3341. return retval;
  3342. }
  3343. /**
  3344. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3345. * @phba: Pointer to HBA context object.
  3346. * @mask: Bit mask to be checked.
  3347. *
  3348. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3349. * from the API jump table function pointer from the lpfc_hba struct.
  3350. **/
  3351. int
  3352. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3353. {
  3354. return phba->lpfc_sli_brdready(phba, mask);
  3355. }
  3356. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3357. /**
  3358. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3359. * @phba: Pointer to HBA context object.
  3360. *
  3361. * This function is called before resetting an HBA. This function is called
  3362. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3363. **/
  3364. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3365. {
  3366. uint32_t __iomem *resp_buf;
  3367. uint32_t __iomem *mbox_buf;
  3368. volatile uint32_t mbox;
  3369. uint32_t hc_copy, ha_copy, resp_data;
  3370. int i;
  3371. uint8_t hdrtype;
  3372. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3373. if (hdrtype != 0x80 ||
  3374. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3375. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3376. return;
  3377. /*
  3378. * Tell the other part of the chip to suspend temporarily all
  3379. * its DMA activity.
  3380. */
  3381. resp_buf = phba->MBslimaddr;
  3382. /* Disable the error attention */
  3383. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3384. return;
  3385. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3386. readl(phba->HCregaddr); /* flush */
  3387. phba->link_flag |= LS_IGNORE_ERATT;
  3388. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3389. return;
  3390. if (ha_copy & HA_ERATT) {
  3391. /* Clear Chip error bit */
  3392. writel(HA_ERATT, phba->HAregaddr);
  3393. phba->pport->stopped = 1;
  3394. }
  3395. mbox = 0;
  3396. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3397. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3398. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3399. mbox_buf = phba->MBslimaddr;
  3400. writel(mbox, mbox_buf);
  3401. for (i = 0; i < 50; i++) {
  3402. if (lpfc_readl((resp_buf + 1), &resp_data))
  3403. return;
  3404. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3405. mdelay(1);
  3406. else
  3407. break;
  3408. }
  3409. resp_data = 0;
  3410. if (lpfc_readl((resp_buf + 1), &resp_data))
  3411. return;
  3412. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3413. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3414. phba->pport->stopped)
  3415. goto restore_hc;
  3416. else
  3417. goto clear_errat;
  3418. }
  3419. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3420. resp_data = 0;
  3421. for (i = 0; i < 500; i++) {
  3422. if (lpfc_readl(resp_buf, &resp_data))
  3423. return;
  3424. if (resp_data != mbox)
  3425. mdelay(1);
  3426. else
  3427. break;
  3428. }
  3429. clear_errat:
  3430. while (++i < 500) {
  3431. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3432. return;
  3433. if (!(ha_copy & HA_ERATT))
  3434. mdelay(1);
  3435. else
  3436. break;
  3437. }
  3438. if (readl(phba->HAregaddr) & HA_ERATT) {
  3439. writel(HA_ERATT, phba->HAregaddr);
  3440. phba->pport->stopped = 1;
  3441. }
  3442. restore_hc:
  3443. phba->link_flag &= ~LS_IGNORE_ERATT;
  3444. writel(hc_copy, phba->HCregaddr);
  3445. readl(phba->HCregaddr); /* flush */
  3446. }
  3447. /**
  3448. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3449. * @phba: Pointer to HBA context object.
  3450. *
  3451. * This function issues a kill_board mailbox command and waits for
  3452. * the error attention interrupt. This function is called for stopping
  3453. * the firmware processing. The caller is not required to hold any
  3454. * locks. This function calls lpfc_hba_down_post function to free
  3455. * any pending commands after the kill. The function will return 1 when it
  3456. * fails to kill the board else will return 0.
  3457. **/
  3458. int
  3459. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3460. {
  3461. struct lpfc_sli *psli;
  3462. LPFC_MBOXQ_t *pmb;
  3463. uint32_t status;
  3464. uint32_t ha_copy;
  3465. int retval;
  3466. int i = 0;
  3467. psli = &phba->sli;
  3468. /* Kill HBA */
  3469. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3470. "0329 Kill HBA Data: x%x x%x\n",
  3471. phba->pport->port_state, psli->sli_flag);
  3472. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3473. if (!pmb)
  3474. return 1;
  3475. /* Disable the error attention */
  3476. spin_lock_irq(&phba->hbalock);
  3477. if (lpfc_readl(phba->HCregaddr, &status)) {
  3478. spin_unlock_irq(&phba->hbalock);
  3479. mempool_free(pmb, phba->mbox_mem_pool);
  3480. return 1;
  3481. }
  3482. status &= ~HC_ERINT_ENA;
  3483. writel(status, phba->HCregaddr);
  3484. readl(phba->HCregaddr); /* flush */
  3485. phba->link_flag |= LS_IGNORE_ERATT;
  3486. spin_unlock_irq(&phba->hbalock);
  3487. lpfc_kill_board(phba, pmb);
  3488. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3489. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3490. if (retval != MBX_SUCCESS) {
  3491. if (retval != MBX_BUSY)
  3492. mempool_free(pmb, phba->mbox_mem_pool);
  3493. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3494. "2752 KILL_BOARD command failed retval %d\n",
  3495. retval);
  3496. spin_lock_irq(&phba->hbalock);
  3497. phba->link_flag &= ~LS_IGNORE_ERATT;
  3498. spin_unlock_irq(&phba->hbalock);
  3499. return 1;
  3500. }
  3501. spin_lock_irq(&phba->hbalock);
  3502. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3503. spin_unlock_irq(&phba->hbalock);
  3504. mempool_free(pmb, phba->mbox_mem_pool);
  3505. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3506. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3507. * 3 seconds we still set HBA_ERROR state because the status of the
  3508. * board is now undefined.
  3509. */
  3510. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3511. return 1;
  3512. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3513. mdelay(100);
  3514. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3515. return 1;
  3516. }
  3517. del_timer_sync(&psli->mbox_tmo);
  3518. if (ha_copy & HA_ERATT) {
  3519. writel(HA_ERATT, phba->HAregaddr);
  3520. phba->pport->stopped = 1;
  3521. }
  3522. spin_lock_irq(&phba->hbalock);
  3523. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3524. psli->mbox_active = NULL;
  3525. phba->link_flag &= ~LS_IGNORE_ERATT;
  3526. spin_unlock_irq(&phba->hbalock);
  3527. lpfc_hba_down_post(phba);
  3528. phba->link_state = LPFC_HBA_ERROR;
  3529. return ha_copy & HA_ERATT ? 0 : 1;
  3530. }
  3531. /**
  3532. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3533. * @phba: Pointer to HBA context object.
  3534. *
  3535. * This function resets the HBA by writing HC_INITFF to the control
  3536. * register. After the HBA resets, this function resets all the iocb ring
  3537. * indices. This function disables PCI layer parity checking during
  3538. * the reset.
  3539. * This function returns 0 always.
  3540. * The caller is not required to hold any locks.
  3541. **/
  3542. int
  3543. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3544. {
  3545. struct lpfc_sli *psli;
  3546. struct lpfc_sli_ring *pring;
  3547. uint16_t cfg_value;
  3548. int i;
  3549. psli = &phba->sli;
  3550. /* Reset HBA */
  3551. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3552. "0325 Reset HBA Data: x%x x%x\n",
  3553. phba->pport->port_state, psli->sli_flag);
  3554. /* perform board reset */
  3555. phba->fc_eventTag = 0;
  3556. phba->link_events = 0;
  3557. phba->pport->fc_myDID = 0;
  3558. phba->pport->fc_prevDID = 0;
  3559. /* Turn off parity checking and serr during the physical reset */
  3560. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3561. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3562. (cfg_value &
  3563. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3564. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3565. /* Now toggle INITFF bit in the Host Control Register */
  3566. writel(HC_INITFF, phba->HCregaddr);
  3567. mdelay(1);
  3568. readl(phba->HCregaddr); /* flush */
  3569. writel(0, phba->HCregaddr);
  3570. readl(phba->HCregaddr); /* flush */
  3571. /* Restore PCI cmd register */
  3572. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3573. /* Initialize relevant SLI info */
  3574. for (i = 0; i < psli->num_rings; i++) {
  3575. pring = &psli->ring[i];
  3576. pring->flag = 0;
  3577. pring->sli.sli3.rspidx = 0;
  3578. pring->sli.sli3.next_cmdidx = 0;
  3579. pring->sli.sli3.local_getidx = 0;
  3580. pring->sli.sli3.cmdidx = 0;
  3581. pring->missbufcnt = 0;
  3582. }
  3583. phba->link_state = LPFC_WARM_START;
  3584. return 0;
  3585. }
  3586. /**
  3587. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3588. * @phba: Pointer to HBA context object.
  3589. *
  3590. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3591. * checking during resets the device. The caller is not required to hold
  3592. * any locks.
  3593. *
  3594. * This function returns 0 always.
  3595. **/
  3596. int
  3597. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3598. {
  3599. struct lpfc_sli *psli = &phba->sli;
  3600. uint16_t cfg_value;
  3601. int rc;
  3602. /* Reset HBA */
  3603. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3604. "0295 Reset HBA Data: x%x x%x\n",
  3605. phba->pport->port_state, psli->sli_flag);
  3606. /* perform board reset */
  3607. phba->fc_eventTag = 0;
  3608. phba->link_events = 0;
  3609. phba->pport->fc_myDID = 0;
  3610. phba->pport->fc_prevDID = 0;
  3611. spin_lock_irq(&phba->hbalock);
  3612. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3613. phba->fcf.fcf_flag = 0;
  3614. spin_unlock_irq(&phba->hbalock);
  3615. /* Now physically reset the device */
  3616. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3617. "0389 Performing PCI function reset!\n");
  3618. /* Turn off parity checking and serr during the physical reset */
  3619. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3620. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3621. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3622. /* Perform FCoE PCI function reset before freeing queue memory */
  3623. rc = lpfc_pci_function_reset(phba);
  3624. lpfc_sli4_queue_destroy(phba);
  3625. /* Restore PCI cmd register */
  3626. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3627. return rc;
  3628. }
  3629. /**
  3630. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3631. * @phba: Pointer to HBA context object.
  3632. *
  3633. * This function is called in the SLI initialization code path to
  3634. * restart the HBA. The caller is not required to hold any lock.
  3635. * This function writes MBX_RESTART mailbox command to the SLIM and
  3636. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3637. * function to free any pending commands. The function enables
  3638. * POST only during the first initialization. The function returns zero.
  3639. * The function does not guarantee completion of MBX_RESTART mailbox
  3640. * command before the return of this function.
  3641. **/
  3642. static int
  3643. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3644. {
  3645. MAILBOX_t *mb;
  3646. struct lpfc_sli *psli;
  3647. volatile uint32_t word0;
  3648. void __iomem *to_slim;
  3649. uint32_t hba_aer_enabled;
  3650. spin_lock_irq(&phba->hbalock);
  3651. /* Take PCIe device Advanced Error Reporting (AER) state */
  3652. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3653. psli = &phba->sli;
  3654. /* Restart HBA */
  3655. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3656. "0337 Restart HBA Data: x%x x%x\n",
  3657. phba->pport->port_state, psli->sli_flag);
  3658. word0 = 0;
  3659. mb = (MAILBOX_t *) &word0;
  3660. mb->mbxCommand = MBX_RESTART;
  3661. mb->mbxHc = 1;
  3662. lpfc_reset_barrier(phba);
  3663. to_slim = phba->MBslimaddr;
  3664. writel(*(uint32_t *) mb, to_slim);
  3665. readl(to_slim); /* flush */
  3666. /* Only skip post after fc_ffinit is completed */
  3667. if (phba->pport->port_state)
  3668. word0 = 1; /* This is really setting up word1 */
  3669. else
  3670. word0 = 0; /* This is really setting up word1 */
  3671. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3672. writel(*(uint32_t *) mb, to_slim);
  3673. readl(to_slim); /* flush */
  3674. lpfc_sli_brdreset(phba);
  3675. phba->pport->stopped = 0;
  3676. phba->link_state = LPFC_INIT_START;
  3677. phba->hba_flag = 0;
  3678. spin_unlock_irq(&phba->hbalock);
  3679. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3680. psli->stats_start = get_seconds();
  3681. /* Give the INITFF and Post time to settle. */
  3682. mdelay(100);
  3683. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3684. if (hba_aer_enabled)
  3685. pci_disable_pcie_error_reporting(phba->pcidev);
  3686. lpfc_hba_down_post(phba);
  3687. return 0;
  3688. }
  3689. /**
  3690. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3691. * @phba: Pointer to HBA context object.
  3692. *
  3693. * This function is called in the SLI initialization code path to restart
  3694. * a SLI4 HBA. The caller is not required to hold any lock.
  3695. * At the end of the function, it calls lpfc_hba_down_post function to
  3696. * free any pending commands.
  3697. **/
  3698. static int
  3699. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3700. {
  3701. struct lpfc_sli *psli = &phba->sli;
  3702. uint32_t hba_aer_enabled;
  3703. int rc;
  3704. /* Restart HBA */
  3705. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3706. "0296 Restart HBA Data: x%x x%x\n",
  3707. phba->pport->port_state, psli->sli_flag);
  3708. /* Take PCIe device Advanced Error Reporting (AER) state */
  3709. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3710. rc = lpfc_sli4_brdreset(phba);
  3711. spin_lock_irq(&phba->hbalock);
  3712. phba->pport->stopped = 0;
  3713. phba->link_state = LPFC_INIT_START;
  3714. phba->hba_flag = 0;
  3715. spin_unlock_irq(&phba->hbalock);
  3716. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3717. psli->stats_start = get_seconds();
  3718. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3719. if (hba_aer_enabled)
  3720. pci_disable_pcie_error_reporting(phba->pcidev);
  3721. lpfc_hba_down_post(phba);
  3722. return rc;
  3723. }
  3724. /**
  3725. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3726. * @phba: Pointer to HBA context object.
  3727. *
  3728. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3729. * API jump table function pointer from the lpfc_hba struct.
  3730. **/
  3731. int
  3732. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3733. {
  3734. return phba->lpfc_sli_brdrestart(phba);
  3735. }
  3736. /**
  3737. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3738. * @phba: Pointer to HBA context object.
  3739. *
  3740. * This function is called after a HBA restart to wait for successful
  3741. * restart of the HBA. Successful restart of the HBA is indicated by
  3742. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3743. * iteration, the function will restart the HBA again. The function returns
  3744. * zero if HBA successfully restarted else returns negative error code.
  3745. **/
  3746. static int
  3747. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3748. {
  3749. uint32_t status, i = 0;
  3750. /* Read the HBA Host Status Register */
  3751. if (lpfc_readl(phba->HSregaddr, &status))
  3752. return -EIO;
  3753. /* Check status register to see what current state is */
  3754. i = 0;
  3755. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3756. /* Check every 10ms for 10 retries, then every 100ms for 90
  3757. * retries, then every 1 sec for 50 retires for a total of
  3758. * ~60 seconds before reset the board again and check every
  3759. * 1 sec for 50 retries. The up to 60 seconds before the
  3760. * board ready is required by the Falcon FIPS zeroization
  3761. * complete, and any reset the board in between shall cause
  3762. * restart of zeroization, further delay the board ready.
  3763. */
  3764. if (i++ >= 200) {
  3765. /* Adapter failed to init, timeout, status reg
  3766. <status> */
  3767. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3768. "0436 Adapter failed to init, "
  3769. "timeout, status reg x%x, "
  3770. "FW Data: A8 x%x AC x%x\n", status,
  3771. readl(phba->MBslimaddr + 0xa8),
  3772. readl(phba->MBslimaddr + 0xac));
  3773. phba->link_state = LPFC_HBA_ERROR;
  3774. return -ETIMEDOUT;
  3775. }
  3776. /* Check to see if any errors occurred during init */
  3777. if (status & HS_FFERM) {
  3778. /* ERROR: During chipset initialization */
  3779. /* Adapter failed to init, chipset, status reg
  3780. <status> */
  3781. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3782. "0437 Adapter failed to init, "
  3783. "chipset, status reg x%x, "
  3784. "FW Data: A8 x%x AC x%x\n", status,
  3785. readl(phba->MBslimaddr + 0xa8),
  3786. readl(phba->MBslimaddr + 0xac));
  3787. phba->link_state = LPFC_HBA_ERROR;
  3788. return -EIO;
  3789. }
  3790. if (i <= 10)
  3791. msleep(10);
  3792. else if (i <= 100)
  3793. msleep(100);
  3794. else
  3795. msleep(1000);
  3796. if (i == 150) {
  3797. /* Do post */
  3798. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3799. lpfc_sli_brdrestart(phba);
  3800. }
  3801. /* Read the HBA Host Status Register */
  3802. if (lpfc_readl(phba->HSregaddr, &status))
  3803. return -EIO;
  3804. }
  3805. /* Check to see if any errors occurred during init */
  3806. if (status & HS_FFERM) {
  3807. /* ERROR: During chipset initialization */
  3808. /* Adapter failed to init, chipset, status reg <status> */
  3809. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3810. "0438 Adapter failed to init, chipset, "
  3811. "status reg x%x, "
  3812. "FW Data: A8 x%x AC x%x\n", status,
  3813. readl(phba->MBslimaddr + 0xa8),
  3814. readl(phba->MBslimaddr + 0xac));
  3815. phba->link_state = LPFC_HBA_ERROR;
  3816. return -EIO;
  3817. }
  3818. /* Clear all interrupt enable conditions */
  3819. writel(0, phba->HCregaddr);
  3820. readl(phba->HCregaddr); /* flush */
  3821. /* setup host attn register */
  3822. writel(0xffffffff, phba->HAregaddr);
  3823. readl(phba->HAregaddr); /* flush */
  3824. return 0;
  3825. }
  3826. /**
  3827. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3828. *
  3829. * This function calculates and returns the number of HBQs required to be
  3830. * configured.
  3831. **/
  3832. int
  3833. lpfc_sli_hbq_count(void)
  3834. {
  3835. return ARRAY_SIZE(lpfc_hbq_defs);
  3836. }
  3837. /**
  3838. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3839. *
  3840. * This function adds the number of hbq entries in every HBQ to get
  3841. * the total number of hbq entries required for the HBA and returns
  3842. * the total count.
  3843. **/
  3844. static int
  3845. lpfc_sli_hbq_entry_count(void)
  3846. {
  3847. int hbq_count = lpfc_sli_hbq_count();
  3848. int count = 0;
  3849. int i;
  3850. for (i = 0; i < hbq_count; ++i)
  3851. count += lpfc_hbq_defs[i]->entry_count;
  3852. return count;
  3853. }
  3854. /**
  3855. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3856. *
  3857. * This function calculates amount of memory required for all hbq entries
  3858. * to be configured and returns the total memory required.
  3859. **/
  3860. int
  3861. lpfc_sli_hbq_size(void)
  3862. {
  3863. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3864. }
  3865. /**
  3866. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3867. * @phba: Pointer to HBA context object.
  3868. *
  3869. * This function is called during the SLI initialization to configure
  3870. * all the HBQs and post buffers to the HBQ. The caller is not
  3871. * required to hold any locks. This function will return zero if successful
  3872. * else it will return negative error code.
  3873. **/
  3874. static int
  3875. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3876. {
  3877. int hbq_count = lpfc_sli_hbq_count();
  3878. LPFC_MBOXQ_t *pmb;
  3879. MAILBOX_t *pmbox;
  3880. uint32_t hbqno;
  3881. uint32_t hbq_entry_index;
  3882. /* Get a Mailbox buffer to setup mailbox
  3883. * commands for HBA initialization
  3884. */
  3885. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3886. if (!pmb)
  3887. return -ENOMEM;
  3888. pmbox = &pmb->u.mb;
  3889. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3890. phba->link_state = LPFC_INIT_MBX_CMDS;
  3891. phba->hbq_in_use = 1;
  3892. hbq_entry_index = 0;
  3893. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3894. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3895. phba->hbqs[hbqno].hbqPutIdx = 0;
  3896. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3897. phba->hbqs[hbqno].entry_count =
  3898. lpfc_hbq_defs[hbqno]->entry_count;
  3899. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3900. hbq_entry_index, pmb);
  3901. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3902. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3903. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3904. mbxStatus <status>, ring <num> */
  3905. lpfc_printf_log(phba, KERN_ERR,
  3906. LOG_SLI | LOG_VPORT,
  3907. "1805 Adapter failed to init. "
  3908. "Data: x%x x%x x%x\n",
  3909. pmbox->mbxCommand,
  3910. pmbox->mbxStatus, hbqno);
  3911. phba->link_state = LPFC_HBA_ERROR;
  3912. mempool_free(pmb, phba->mbox_mem_pool);
  3913. return -ENXIO;
  3914. }
  3915. }
  3916. phba->hbq_count = hbq_count;
  3917. mempool_free(pmb, phba->mbox_mem_pool);
  3918. /* Initially populate or replenish the HBQs */
  3919. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3920. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3921. return 0;
  3922. }
  3923. /**
  3924. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3925. * @phba: Pointer to HBA context object.
  3926. *
  3927. * This function is called during the SLI initialization to configure
  3928. * all the HBQs and post buffers to the HBQ. The caller is not
  3929. * required to hold any locks. This function will return zero if successful
  3930. * else it will return negative error code.
  3931. **/
  3932. static int
  3933. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3934. {
  3935. phba->hbq_in_use = 1;
  3936. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3937. phba->hbq_count = 1;
  3938. /* Initially populate or replenish the HBQs */
  3939. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3940. return 0;
  3941. }
  3942. /**
  3943. * lpfc_sli_config_port - Issue config port mailbox command
  3944. * @phba: Pointer to HBA context object.
  3945. * @sli_mode: sli mode - 2/3
  3946. *
  3947. * This function is called by the sli intialization code path
  3948. * to issue config_port mailbox command. This function restarts the
  3949. * HBA firmware and issues a config_port mailbox command to configure
  3950. * the SLI interface in the sli mode specified by sli_mode
  3951. * variable. The caller is not required to hold any locks.
  3952. * The function returns 0 if successful, else returns negative error
  3953. * code.
  3954. **/
  3955. int
  3956. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3957. {
  3958. LPFC_MBOXQ_t *pmb;
  3959. uint32_t resetcount = 0, rc = 0, done = 0;
  3960. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3961. if (!pmb) {
  3962. phba->link_state = LPFC_HBA_ERROR;
  3963. return -ENOMEM;
  3964. }
  3965. phba->sli_rev = sli_mode;
  3966. while (resetcount < 2 && !done) {
  3967. spin_lock_irq(&phba->hbalock);
  3968. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3969. spin_unlock_irq(&phba->hbalock);
  3970. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3971. lpfc_sli_brdrestart(phba);
  3972. rc = lpfc_sli_chipset_init(phba);
  3973. if (rc)
  3974. break;
  3975. spin_lock_irq(&phba->hbalock);
  3976. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3977. spin_unlock_irq(&phba->hbalock);
  3978. resetcount++;
  3979. /* Call pre CONFIG_PORT mailbox command initialization. A
  3980. * value of 0 means the call was successful. Any other
  3981. * nonzero value is a failure, but if ERESTART is returned,
  3982. * the driver may reset the HBA and try again.
  3983. */
  3984. rc = lpfc_config_port_prep(phba);
  3985. if (rc == -ERESTART) {
  3986. phba->link_state = LPFC_LINK_UNKNOWN;
  3987. continue;
  3988. } else if (rc)
  3989. break;
  3990. phba->link_state = LPFC_INIT_MBX_CMDS;
  3991. lpfc_config_port(phba, pmb);
  3992. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3993. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3994. LPFC_SLI3_HBQ_ENABLED |
  3995. LPFC_SLI3_CRP_ENABLED |
  3996. LPFC_SLI3_BG_ENABLED |
  3997. LPFC_SLI3_DSS_ENABLED);
  3998. if (rc != MBX_SUCCESS) {
  3999. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4000. "0442 Adapter failed to init, mbxCmd x%x "
  4001. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  4002. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  4003. spin_lock_irq(&phba->hbalock);
  4004. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  4005. spin_unlock_irq(&phba->hbalock);
  4006. rc = -ENXIO;
  4007. } else {
  4008. /* Allow asynchronous mailbox command to go through */
  4009. spin_lock_irq(&phba->hbalock);
  4010. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4011. spin_unlock_irq(&phba->hbalock);
  4012. done = 1;
  4013. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  4014. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  4015. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  4016. "3110 Port did not grant ASABT\n");
  4017. }
  4018. }
  4019. if (!done) {
  4020. rc = -EINVAL;
  4021. goto do_prep_failed;
  4022. }
  4023. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  4024. if (!pmb->u.mb.un.varCfgPort.cMA) {
  4025. rc = -ENXIO;
  4026. goto do_prep_failed;
  4027. }
  4028. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  4029. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  4030. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  4031. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  4032. phba->max_vpi : phba->max_vports;
  4033. } else
  4034. phba->max_vpi = 0;
  4035. phba->fips_level = 0;
  4036. phba->fips_spec_rev = 0;
  4037. if (pmb->u.mb.un.varCfgPort.gdss) {
  4038. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  4039. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4040. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4041. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4042. "2850 Security Crypto Active. FIPS x%d "
  4043. "(Spec Rev: x%d)",
  4044. phba->fips_level, phba->fips_spec_rev);
  4045. }
  4046. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4047. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4048. "2856 Config Port Security Crypto "
  4049. "Error: x%x ",
  4050. pmb->u.mb.un.varCfgPort.sec_err);
  4051. }
  4052. if (pmb->u.mb.un.varCfgPort.gerbm)
  4053. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4054. if (pmb->u.mb.un.varCfgPort.gcrp)
  4055. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4056. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4057. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4058. if (phba->cfg_enable_bg) {
  4059. if (pmb->u.mb.un.varCfgPort.gbg)
  4060. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4061. else
  4062. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4063. "0443 Adapter did not grant "
  4064. "BlockGuard\n");
  4065. }
  4066. } else {
  4067. phba->hbq_get = NULL;
  4068. phba->port_gp = phba->mbox->us.s2.port;
  4069. phba->max_vpi = 0;
  4070. }
  4071. do_prep_failed:
  4072. mempool_free(pmb, phba->mbox_mem_pool);
  4073. return rc;
  4074. }
  4075. /**
  4076. * lpfc_sli_hba_setup - SLI intialization function
  4077. * @phba: Pointer to HBA context object.
  4078. *
  4079. * This function is the main SLI intialization function. This function
  4080. * is called by the HBA intialization code, HBA reset code and HBA
  4081. * error attention handler code. Caller is not required to hold any
  4082. * locks. This function issues config_port mailbox command to configure
  4083. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4084. * calls the config_port_post function to issue init_link mailbox
  4085. * command and to start the discovery. The function will return zero
  4086. * if successful, else it will return negative error code.
  4087. **/
  4088. int
  4089. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4090. {
  4091. uint32_t rc;
  4092. int mode = 3, i;
  4093. int longs;
  4094. switch (lpfc_sli_mode) {
  4095. case 2:
  4096. if (phba->cfg_enable_npiv) {
  4097. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4098. "1824 NPIV enabled: Override lpfc_sli_mode "
  4099. "parameter (%d) to auto (0).\n",
  4100. lpfc_sli_mode);
  4101. break;
  4102. }
  4103. mode = 2;
  4104. break;
  4105. case 0:
  4106. case 3:
  4107. break;
  4108. default:
  4109. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4110. "1819 Unrecognized lpfc_sli_mode "
  4111. "parameter: %d.\n", lpfc_sli_mode);
  4112. break;
  4113. }
  4114. rc = lpfc_sli_config_port(phba, mode);
  4115. if (rc && lpfc_sli_mode == 3)
  4116. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4117. "1820 Unable to select SLI-3. "
  4118. "Not supported by adapter.\n");
  4119. if (rc && mode != 2)
  4120. rc = lpfc_sli_config_port(phba, 2);
  4121. if (rc)
  4122. goto lpfc_sli_hba_setup_error;
  4123. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4124. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4125. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4126. if (!rc) {
  4127. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4128. "2709 This device supports "
  4129. "Advanced Error Reporting (AER)\n");
  4130. spin_lock_irq(&phba->hbalock);
  4131. phba->hba_flag |= HBA_AER_ENABLED;
  4132. spin_unlock_irq(&phba->hbalock);
  4133. } else {
  4134. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4135. "2708 This device does not support "
  4136. "Advanced Error Reporting (AER)\n");
  4137. phba->cfg_aer_support = 0;
  4138. }
  4139. }
  4140. if (phba->sli_rev == 3) {
  4141. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4142. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4143. } else {
  4144. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4145. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4146. phba->sli3_options = 0;
  4147. }
  4148. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4149. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4150. phba->sli_rev, phba->max_vpi);
  4151. rc = lpfc_sli_ring_map(phba);
  4152. if (rc)
  4153. goto lpfc_sli_hba_setup_error;
  4154. /* Initialize VPIs. */
  4155. if (phba->sli_rev == LPFC_SLI_REV3) {
  4156. /*
  4157. * The VPI bitmask and physical ID array are allocated
  4158. * and initialized once only - at driver load. A port
  4159. * reset doesn't need to reinitialize this memory.
  4160. */
  4161. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4162. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4163. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4164. GFP_KERNEL);
  4165. if (!phba->vpi_bmask) {
  4166. rc = -ENOMEM;
  4167. goto lpfc_sli_hba_setup_error;
  4168. }
  4169. phba->vpi_ids = kzalloc(
  4170. (phba->max_vpi+1) * sizeof(uint16_t),
  4171. GFP_KERNEL);
  4172. if (!phba->vpi_ids) {
  4173. kfree(phba->vpi_bmask);
  4174. rc = -ENOMEM;
  4175. goto lpfc_sli_hba_setup_error;
  4176. }
  4177. for (i = 0; i < phba->max_vpi; i++)
  4178. phba->vpi_ids[i] = i;
  4179. }
  4180. }
  4181. /* Init HBQs */
  4182. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4183. rc = lpfc_sli_hbq_setup(phba);
  4184. if (rc)
  4185. goto lpfc_sli_hba_setup_error;
  4186. }
  4187. spin_lock_irq(&phba->hbalock);
  4188. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4189. spin_unlock_irq(&phba->hbalock);
  4190. rc = lpfc_config_port_post(phba);
  4191. if (rc)
  4192. goto lpfc_sli_hba_setup_error;
  4193. return rc;
  4194. lpfc_sli_hba_setup_error:
  4195. phba->link_state = LPFC_HBA_ERROR;
  4196. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4197. "0445 Firmware initialization failed\n");
  4198. return rc;
  4199. }
  4200. /**
  4201. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4202. * @phba: Pointer to HBA context object.
  4203. * @mboxq: mailbox pointer.
  4204. * This function issue a dump mailbox command to read config region
  4205. * 23 and parse the records in the region and populate driver
  4206. * data structure.
  4207. **/
  4208. static int
  4209. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4210. {
  4211. LPFC_MBOXQ_t *mboxq;
  4212. struct lpfc_dmabuf *mp;
  4213. struct lpfc_mqe *mqe;
  4214. uint32_t data_length;
  4215. int rc;
  4216. /* Program the default value of vlan_id and fc_map */
  4217. phba->valid_vlan = 0;
  4218. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4219. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4220. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4221. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4222. if (!mboxq)
  4223. return -ENOMEM;
  4224. mqe = &mboxq->u.mqe;
  4225. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4226. rc = -ENOMEM;
  4227. goto out_free_mboxq;
  4228. }
  4229. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4230. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4231. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4232. "(%d):2571 Mailbox cmd x%x Status x%x "
  4233. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4234. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4235. "CQ: x%x x%x x%x x%x\n",
  4236. mboxq->vport ? mboxq->vport->vpi : 0,
  4237. bf_get(lpfc_mqe_command, mqe),
  4238. bf_get(lpfc_mqe_status, mqe),
  4239. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4240. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4241. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4242. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4243. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4244. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4245. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4246. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4247. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4248. mboxq->mcqe.word0,
  4249. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4250. mboxq->mcqe.trailer);
  4251. if (rc) {
  4252. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4253. kfree(mp);
  4254. rc = -EIO;
  4255. goto out_free_mboxq;
  4256. }
  4257. data_length = mqe->un.mb_words[5];
  4258. if (data_length > DMP_RGN23_SIZE) {
  4259. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4260. kfree(mp);
  4261. rc = -EIO;
  4262. goto out_free_mboxq;
  4263. }
  4264. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4265. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4266. kfree(mp);
  4267. rc = 0;
  4268. out_free_mboxq:
  4269. mempool_free(mboxq, phba->mbox_mem_pool);
  4270. return rc;
  4271. }
  4272. /**
  4273. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4274. * @phba: pointer to lpfc hba data structure.
  4275. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4276. * @vpd: pointer to the memory to hold resulting port vpd data.
  4277. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4278. * On output, the number of data bytes in @vpd.
  4279. *
  4280. * This routine executes a READ_REV SLI4 mailbox command. In
  4281. * addition, this routine gets the port vpd data.
  4282. *
  4283. * Return codes
  4284. * 0 - successful
  4285. * -ENOMEM - could not allocated memory.
  4286. **/
  4287. static int
  4288. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4289. uint8_t *vpd, uint32_t *vpd_size)
  4290. {
  4291. int rc = 0;
  4292. uint32_t dma_size;
  4293. struct lpfc_dmabuf *dmabuf;
  4294. struct lpfc_mqe *mqe;
  4295. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4296. if (!dmabuf)
  4297. return -ENOMEM;
  4298. /*
  4299. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4300. * mailbox command.
  4301. */
  4302. dma_size = *vpd_size;
  4303. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4304. dma_size,
  4305. &dmabuf->phys,
  4306. GFP_KERNEL);
  4307. if (!dmabuf->virt) {
  4308. kfree(dmabuf);
  4309. return -ENOMEM;
  4310. }
  4311. memset(dmabuf->virt, 0, dma_size);
  4312. /*
  4313. * The SLI4 implementation of READ_REV conflicts at word1,
  4314. * bits 31:16 and SLI4 adds vpd functionality not present
  4315. * in SLI3. This code corrects the conflicts.
  4316. */
  4317. lpfc_read_rev(phba, mboxq);
  4318. mqe = &mboxq->u.mqe;
  4319. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4320. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4321. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4322. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4323. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4324. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4325. if (rc) {
  4326. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4327. dmabuf->virt, dmabuf->phys);
  4328. kfree(dmabuf);
  4329. return -EIO;
  4330. }
  4331. /*
  4332. * The available vpd length cannot be bigger than the
  4333. * DMA buffer passed to the port. Catch the less than
  4334. * case and update the caller's size.
  4335. */
  4336. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4337. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4338. memcpy(vpd, dmabuf->virt, *vpd_size);
  4339. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4340. dmabuf->virt, dmabuf->phys);
  4341. kfree(dmabuf);
  4342. return 0;
  4343. }
  4344. /**
  4345. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4346. * @phba: pointer to lpfc hba data structure.
  4347. *
  4348. * This routine retrieves SLI4 device physical port name this PCI function
  4349. * is attached to.
  4350. *
  4351. * Return codes
  4352. * 0 - successful
  4353. * otherwise - failed to retrieve physical port name
  4354. **/
  4355. static int
  4356. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4357. {
  4358. LPFC_MBOXQ_t *mboxq;
  4359. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4360. struct lpfc_controller_attribute *cntl_attr;
  4361. struct lpfc_mbx_get_port_name *get_port_name;
  4362. void *virtaddr = NULL;
  4363. uint32_t alloclen, reqlen;
  4364. uint32_t shdr_status, shdr_add_status;
  4365. union lpfc_sli4_cfg_shdr *shdr;
  4366. char cport_name = 0;
  4367. int rc;
  4368. /* We assume nothing at this point */
  4369. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4370. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4371. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4372. if (!mboxq)
  4373. return -ENOMEM;
  4374. /* obtain link type and link number via READ_CONFIG */
  4375. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4376. lpfc_sli4_read_config(phba);
  4377. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4378. goto retrieve_ppname;
  4379. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4380. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4381. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4382. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4383. LPFC_SLI4_MBX_NEMBED);
  4384. if (alloclen < reqlen) {
  4385. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4386. "3084 Allocated DMA memory size (%d) is "
  4387. "less than the requested DMA memory size "
  4388. "(%d)\n", alloclen, reqlen);
  4389. rc = -ENOMEM;
  4390. goto out_free_mboxq;
  4391. }
  4392. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4393. virtaddr = mboxq->sge_array->addr[0];
  4394. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4395. shdr = &mbx_cntl_attr->cfg_shdr;
  4396. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4397. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4398. if (shdr_status || shdr_add_status || rc) {
  4399. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4400. "3085 Mailbox x%x (x%x/x%x) failed, "
  4401. "rc:x%x, status:x%x, add_status:x%x\n",
  4402. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4403. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4404. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4405. rc, shdr_status, shdr_add_status);
  4406. rc = -ENXIO;
  4407. goto out_free_mboxq;
  4408. }
  4409. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4410. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4411. phba->sli4_hba.lnk_info.lnk_tp =
  4412. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4413. phba->sli4_hba.lnk_info.lnk_no =
  4414. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4415. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4416. "3086 lnk_type:%d, lnk_numb:%d\n",
  4417. phba->sli4_hba.lnk_info.lnk_tp,
  4418. phba->sli4_hba.lnk_info.lnk_no);
  4419. retrieve_ppname:
  4420. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4421. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4422. sizeof(struct lpfc_mbx_get_port_name) -
  4423. sizeof(struct lpfc_sli4_cfg_mhdr),
  4424. LPFC_SLI4_MBX_EMBED);
  4425. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4426. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4427. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4428. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4429. phba->sli4_hba.lnk_info.lnk_tp);
  4430. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4431. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4432. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4433. if (shdr_status || shdr_add_status || rc) {
  4434. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4435. "3087 Mailbox x%x (x%x/x%x) failed: "
  4436. "rc:x%x, status:x%x, add_status:x%x\n",
  4437. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4438. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4439. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4440. rc, shdr_status, shdr_add_status);
  4441. rc = -ENXIO;
  4442. goto out_free_mboxq;
  4443. }
  4444. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4445. case LPFC_LINK_NUMBER_0:
  4446. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4447. &get_port_name->u.response);
  4448. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4449. break;
  4450. case LPFC_LINK_NUMBER_1:
  4451. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4452. &get_port_name->u.response);
  4453. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4454. break;
  4455. case LPFC_LINK_NUMBER_2:
  4456. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4457. &get_port_name->u.response);
  4458. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4459. break;
  4460. case LPFC_LINK_NUMBER_3:
  4461. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4462. &get_port_name->u.response);
  4463. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4464. break;
  4465. default:
  4466. break;
  4467. }
  4468. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4469. phba->Port[0] = cport_name;
  4470. phba->Port[1] = '\0';
  4471. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4472. "3091 SLI get port name: %s\n", phba->Port);
  4473. }
  4474. out_free_mboxq:
  4475. if (rc != MBX_TIMEOUT) {
  4476. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4477. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4478. else
  4479. mempool_free(mboxq, phba->mbox_mem_pool);
  4480. }
  4481. return rc;
  4482. }
  4483. /**
  4484. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4485. * @phba: pointer to lpfc hba data structure.
  4486. *
  4487. * This routine is called to explicitly arm the SLI4 device's completion and
  4488. * event queues
  4489. **/
  4490. static void
  4491. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4492. {
  4493. int fcp_eqidx;
  4494. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4495. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4496. fcp_eqidx = 0;
  4497. if (phba->sli4_hba.fcp_cq) {
  4498. do {
  4499. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4500. LPFC_QUEUE_REARM);
  4501. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4502. }
  4503. if (phba->sli4_hba.hba_eq) {
  4504. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4505. fcp_eqidx++)
  4506. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4507. LPFC_QUEUE_REARM);
  4508. }
  4509. }
  4510. /**
  4511. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4512. * @phba: Pointer to HBA context object.
  4513. * @type: The resource extent type.
  4514. * @extnt_count: buffer to hold port available extent count.
  4515. * @extnt_size: buffer to hold element count per extent.
  4516. *
  4517. * This function calls the port and retrievs the number of available
  4518. * extents and their size for a particular extent type.
  4519. *
  4520. * Returns: 0 if successful. Nonzero otherwise.
  4521. **/
  4522. int
  4523. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4524. uint16_t *extnt_count, uint16_t *extnt_size)
  4525. {
  4526. int rc = 0;
  4527. uint32_t length;
  4528. uint32_t mbox_tmo;
  4529. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4530. LPFC_MBOXQ_t *mbox;
  4531. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4532. if (!mbox)
  4533. return -ENOMEM;
  4534. /* Find out how many extents are available for this resource type */
  4535. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4536. sizeof(struct lpfc_sli4_cfg_mhdr));
  4537. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4538. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4539. length, LPFC_SLI4_MBX_EMBED);
  4540. /* Send an extents count of 0 - the GET doesn't use it. */
  4541. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4542. LPFC_SLI4_MBX_EMBED);
  4543. if (unlikely(rc)) {
  4544. rc = -EIO;
  4545. goto err_exit;
  4546. }
  4547. if (!phba->sli4_hba.intr_enable)
  4548. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4549. else {
  4550. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4551. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4552. }
  4553. if (unlikely(rc)) {
  4554. rc = -EIO;
  4555. goto err_exit;
  4556. }
  4557. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4558. if (bf_get(lpfc_mbox_hdr_status,
  4559. &rsrc_info->header.cfg_shdr.response)) {
  4560. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4561. "2930 Failed to get resource extents "
  4562. "Status 0x%x Add'l Status 0x%x\n",
  4563. bf_get(lpfc_mbox_hdr_status,
  4564. &rsrc_info->header.cfg_shdr.response),
  4565. bf_get(lpfc_mbox_hdr_add_status,
  4566. &rsrc_info->header.cfg_shdr.response));
  4567. rc = -EIO;
  4568. goto err_exit;
  4569. }
  4570. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4571. &rsrc_info->u.rsp);
  4572. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4573. &rsrc_info->u.rsp);
  4574. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4575. "3162 Retrieved extents type-%d from port: count:%d, "
  4576. "size:%d\n", type, *extnt_count, *extnt_size);
  4577. err_exit:
  4578. mempool_free(mbox, phba->mbox_mem_pool);
  4579. return rc;
  4580. }
  4581. /**
  4582. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4583. * @phba: Pointer to HBA context object.
  4584. * @type: The extent type to check.
  4585. *
  4586. * This function reads the current available extents from the port and checks
  4587. * if the extent count or extent size has changed since the last access.
  4588. * Callers use this routine post port reset to understand if there is a
  4589. * extent reprovisioning requirement.
  4590. *
  4591. * Returns:
  4592. * -Error: error indicates problem.
  4593. * 1: Extent count or size has changed.
  4594. * 0: No changes.
  4595. **/
  4596. static int
  4597. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4598. {
  4599. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4600. uint16_t size_diff, rsrc_ext_size;
  4601. int rc = 0;
  4602. struct lpfc_rsrc_blks *rsrc_entry;
  4603. struct list_head *rsrc_blk_list = NULL;
  4604. size_diff = 0;
  4605. curr_ext_cnt = 0;
  4606. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4607. &rsrc_ext_cnt,
  4608. &rsrc_ext_size);
  4609. if (unlikely(rc))
  4610. return -EIO;
  4611. switch (type) {
  4612. case LPFC_RSC_TYPE_FCOE_RPI:
  4613. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4614. break;
  4615. case LPFC_RSC_TYPE_FCOE_VPI:
  4616. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4617. break;
  4618. case LPFC_RSC_TYPE_FCOE_XRI:
  4619. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4620. break;
  4621. case LPFC_RSC_TYPE_FCOE_VFI:
  4622. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4623. break;
  4624. default:
  4625. break;
  4626. }
  4627. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4628. curr_ext_cnt++;
  4629. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4630. size_diff++;
  4631. }
  4632. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4633. rc = 1;
  4634. return rc;
  4635. }
  4636. /**
  4637. * lpfc_sli4_cfg_post_extnts -
  4638. * @phba: Pointer to HBA context object.
  4639. * @extnt_cnt - number of available extents.
  4640. * @type - the extent type (rpi, xri, vfi, vpi).
  4641. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4642. * @mbox - pointer to the caller's allocated mailbox structure.
  4643. *
  4644. * This function executes the extents allocation request. It also
  4645. * takes care of the amount of memory needed to allocate or get the
  4646. * allocated extents. It is the caller's responsibility to evaluate
  4647. * the response.
  4648. *
  4649. * Returns:
  4650. * -Error: Error value describes the condition found.
  4651. * 0: if successful
  4652. **/
  4653. static int
  4654. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4655. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4656. {
  4657. int rc = 0;
  4658. uint32_t req_len;
  4659. uint32_t emb_len;
  4660. uint32_t alloc_len, mbox_tmo;
  4661. /* Calculate the total requested length of the dma memory */
  4662. req_len = extnt_cnt * sizeof(uint16_t);
  4663. /*
  4664. * Calculate the size of an embedded mailbox. The uint32_t
  4665. * accounts for extents-specific word.
  4666. */
  4667. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4668. sizeof(uint32_t);
  4669. /*
  4670. * Presume the allocation and response will fit into an embedded
  4671. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4672. */
  4673. *emb = LPFC_SLI4_MBX_EMBED;
  4674. if (req_len > emb_len) {
  4675. req_len = extnt_cnt * sizeof(uint16_t) +
  4676. sizeof(union lpfc_sli4_cfg_shdr) +
  4677. sizeof(uint32_t);
  4678. *emb = LPFC_SLI4_MBX_NEMBED;
  4679. }
  4680. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4681. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4682. req_len, *emb);
  4683. if (alloc_len < req_len) {
  4684. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4685. "2982 Allocated DMA memory size (x%x) is "
  4686. "less than the requested DMA memory "
  4687. "size (x%x)\n", alloc_len, req_len);
  4688. return -ENOMEM;
  4689. }
  4690. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4691. if (unlikely(rc))
  4692. return -EIO;
  4693. if (!phba->sli4_hba.intr_enable)
  4694. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4695. else {
  4696. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4697. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4698. }
  4699. if (unlikely(rc))
  4700. rc = -EIO;
  4701. return rc;
  4702. }
  4703. /**
  4704. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4705. * @phba: Pointer to HBA context object.
  4706. * @type: The resource extent type to allocate.
  4707. *
  4708. * This function allocates the number of elements for the specified
  4709. * resource type.
  4710. **/
  4711. static int
  4712. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4713. {
  4714. bool emb = false;
  4715. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4716. uint16_t rsrc_id, rsrc_start, j, k;
  4717. uint16_t *ids;
  4718. int i, rc;
  4719. unsigned long longs;
  4720. unsigned long *bmask;
  4721. struct lpfc_rsrc_blks *rsrc_blks;
  4722. LPFC_MBOXQ_t *mbox;
  4723. uint32_t length;
  4724. struct lpfc_id_range *id_array = NULL;
  4725. void *virtaddr = NULL;
  4726. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4727. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4728. struct list_head *ext_blk_list;
  4729. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4730. &rsrc_cnt,
  4731. &rsrc_size);
  4732. if (unlikely(rc))
  4733. return -EIO;
  4734. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4735. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4736. "3009 No available Resource Extents "
  4737. "for resource type 0x%x: Count: 0x%x, "
  4738. "Size 0x%x\n", type, rsrc_cnt,
  4739. rsrc_size);
  4740. return -ENOMEM;
  4741. }
  4742. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4743. "2903 Post resource extents type-0x%x: "
  4744. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4745. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4746. if (!mbox)
  4747. return -ENOMEM;
  4748. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4749. if (unlikely(rc)) {
  4750. rc = -EIO;
  4751. goto err_exit;
  4752. }
  4753. /*
  4754. * Figure out where the response is located. Then get local pointers
  4755. * to the response data. The port does not guarantee to respond to
  4756. * all extents counts request so update the local variable with the
  4757. * allocated count from the port.
  4758. */
  4759. if (emb == LPFC_SLI4_MBX_EMBED) {
  4760. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4761. id_array = &rsrc_ext->u.rsp.id[0];
  4762. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4763. } else {
  4764. virtaddr = mbox->sge_array->addr[0];
  4765. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4766. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4767. id_array = &n_rsrc->id;
  4768. }
  4769. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4770. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4771. /*
  4772. * Based on the resource size and count, correct the base and max
  4773. * resource values.
  4774. */
  4775. length = sizeof(struct lpfc_rsrc_blks);
  4776. switch (type) {
  4777. case LPFC_RSC_TYPE_FCOE_RPI:
  4778. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4779. sizeof(unsigned long),
  4780. GFP_KERNEL);
  4781. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4782. rc = -ENOMEM;
  4783. goto err_exit;
  4784. }
  4785. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4786. sizeof(uint16_t),
  4787. GFP_KERNEL);
  4788. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4789. kfree(phba->sli4_hba.rpi_bmask);
  4790. rc = -ENOMEM;
  4791. goto err_exit;
  4792. }
  4793. /*
  4794. * The next_rpi was initialized with the maximum available
  4795. * count but the port may allocate a smaller number. Catch
  4796. * that case and update the next_rpi.
  4797. */
  4798. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4799. /* Initialize local ptrs for common extent processing later. */
  4800. bmask = phba->sli4_hba.rpi_bmask;
  4801. ids = phba->sli4_hba.rpi_ids;
  4802. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4803. break;
  4804. case LPFC_RSC_TYPE_FCOE_VPI:
  4805. phba->vpi_bmask = kzalloc(longs *
  4806. sizeof(unsigned long),
  4807. GFP_KERNEL);
  4808. if (unlikely(!phba->vpi_bmask)) {
  4809. rc = -ENOMEM;
  4810. goto err_exit;
  4811. }
  4812. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4813. sizeof(uint16_t),
  4814. GFP_KERNEL);
  4815. if (unlikely(!phba->vpi_ids)) {
  4816. kfree(phba->vpi_bmask);
  4817. rc = -ENOMEM;
  4818. goto err_exit;
  4819. }
  4820. /* Initialize local ptrs for common extent processing later. */
  4821. bmask = phba->vpi_bmask;
  4822. ids = phba->vpi_ids;
  4823. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4824. break;
  4825. case LPFC_RSC_TYPE_FCOE_XRI:
  4826. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4827. sizeof(unsigned long),
  4828. GFP_KERNEL);
  4829. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4830. rc = -ENOMEM;
  4831. goto err_exit;
  4832. }
  4833. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4834. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4835. sizeof(uint16_t),
  4836. GFP_KERNEL);
  4837. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4838. kfree(phba->sli4_hba.xri_bmask);
  4839. rc = -ENOMEM;
  4840. goto err_exit;
  4841. }
  4842. /* Initialize local ptrs for common extent processing later. */
  4843. bmask = phba->sli4_hba.xri_bmask;
  4844. ids = phba->sli4_hba.xri_ids;
  4845. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4846. break;
  4847. case LPFC_RSC_TYPE_FCOE_VFI:
  4848. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4849. sizeof(unsigned long),
  4850. GFP_KERNEL);
  4851. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4852. rc = -ENOMEM;
  4853. goto err_exit;
  4854. }
  4855. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4856. sizeof(uint16_t),
  4857. GFP_KERNEL);
  4858. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4859. kfree(phba->sli4_hba.vfi_bmask);
  4860. rc = -ENOMEM;
  4861. goto err_exit;
  4862. }
  4863. /* Initialize local ptrs for common extent processing later. */
  4864. bmask = phba->sli4_hba.vfi_bmask;
  4865. ids = phba->sli4_hba.vfi_ids;
  4866. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4867. break;
  4868. default:
  4869. /* Unsupported Opcode. Fail call. */
  4870. id_array = NULL;
  4871. bmask = NULL;
  4872. ids = NULL;
  4873. ext_blk_list = NULL;
  4874. goto err_exit;
  4875. }
  4876. /*
  4877. * Complete initializing the extent configuration with the
  4878. * allocated ids assigned to this function. The bitmask serves
  4879. * as an index into the array and manages the available ids. The
  4880. * array just stores the ids communicated to the port via the wqes.
  4881. */
  4882. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4883. if ((i % 2) == 0)
  4884. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4885. &id_array[k]);
  4886. else
  4887. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4888. &id_array[k]);
  4889. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4890. if (unlikely(!rsrc_blks)) {
  4891. rc = -ENOMEM;
  4892. kfree(bmask);
  4893. kfree(ids);
  4894. goto err_exit;
  4895. }
  4896. rsrc_blks->rsrc_start = rsrc_id;
  4897. rsrc_blks->rsrc_size = rsrc_size;
  4898. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4899. rsrc_start = rsrc_id;
  4900. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4901. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4902. lpfc_sli4_get_els_iocb_cnt(phba);
  4903. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4904. ids[j] = rsrc_id;
  4905. rsrc_id++;
  4906. j++;
  4907. }
  4908. /* Entire word processed. Get next word.*/
  4909. if ((i % 2) == 1)
  4910. k++;
  4911. }
  4912. err_exit:
  4913. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4914. return rc;
  4915. }
  4916. /**
  4917. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4918. * @phba: Pointer to HBA context object.
  4919. * @type: the extent's type.
  4920. *
  4921. * This function deallocates all extents of a particular resource type.
  4922. * SLI4 does not allow for deallocating a particular extent range. It
  4923. * is the caller's responsibility to release all kernel memory resources.
  4924. **/
  4925. static int
  4926. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4927. {
  4928. int rc;
  4929. uint32_t length, mbox_tmo = 0;
  4930. LPFC_MBOXQ_t *mbox;
  4931. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4932. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4933. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4934. if (!mbox)
  4935. return -ENOMEM;
  4936. /*
  4937. * This function sends an embedded mailbox because it only sends the
  4938. * the resource type. All extents of this type are released by the
  4939. * port.
  4940. */
  4941. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4942. sizeof(struct lpfc_sli4_cfg_mhdr));
  4943. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4944. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4945. length, LPFC_SLI4_MBX_EMBED);
  4946. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4947. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4948. LPFC_SLI4_MBX_EMBED);
  4949. if (unlikely(rc)) {
  4950. rc = -EIO;
  4951. goto out_free_mbox;
  4952. }
  4953. if (!phba->sli4_hba.intr_enable)
  4954. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4955. else {
  4956. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4957. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4958. }
  4959. if (unlikely(rc)) {
  4960. rc = -EIO;
  4961. goto out_free_mbox;
  4962. }
  4963. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4964. if (bf_get(lpfc_mbox_hdr_status,
  4965. &dealloc_rsrc->header.cfg_shdr.response)) {
  4966. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4967. "2919 Failed to release resource extents "
  4968. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4969. "Resource memory not released.\n",
  4970. type,
  4971. bf_get(lpfc_mbox_hdr_status,
  4972. &dealloc_rsrc->header.cfg_shdr.response),
  4973. bf_get(lpfc_mbox_hdr_add_status,
  4974. &dealloc_rsrc->header.cfg_shdr.response));
  4975. rc = -EIO;
  4976. goto out_free_mbox;
  4977. }
  4978. /* Release kernel memory resources for the specific type. */
  4979. switch (type) {
  4980. case LPFC_RSC_TYPE_FCOE_VPI:
  4981. kfree(phba->vpi_bmask);
  4982. kfree(phba->vpi_ids);
  4983. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4984. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4985. &phba->lpfc_vpi_blk_list, list) {
  4986. list_del_init(&rsrc_blk->list);
  4987. kfree(rsrc_blk);
  4988. }
  4989. break;
  4990. case LPFC_RSC_TYPE_FCOE_XRI:
  4991. kfree(phba->sli4_hba.xri_bmask);
  4992. kfree(phba->sli4_hba.xri_ids);
  4993. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4994. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4995. list_del_init(&rsrc_blk->list);
  4996. kfree(rsrc_blk);
  4997. }
  4998. break;
  4999. case LPFC_RSC_TYPE_FCOE_VFI:
  5000. kfree(phba->sli4_hba.vfi_bmask);
  5001. kfree(phba->sli4_hba.vfi_ids);
  5002. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5003. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5004. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  5005. list_del_init(&rsrc_blk->list);
  5006. kfree(rsrc_blk);
  5007. }
  5008. break;
  5009. case LPFC_RSC_TYPE_FCOE_RPI:
  5010. /* RPI bitmask and physical id array are cleaned up earlier. */
  5011. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5012. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  5013. list_del_init(&rsrc_blk->list);
  5014. kfree(rsrc_blk);
  5015. }
  5016. break;
  5017. default:
  5018. break;
  5019. }
  5020. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5021. out_free_mbox:
  5022. mempool_free(mbox, phba->mbox_mem_pool);
  5023. return rc;
  5024. }
  5025. /**
  5026. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  5027. * @phba: Pointer to HBA context object.
  5028. *
  5029. * This function allocates all SLI4 resource identifiers.
  5030. **/
  5031. int
  5032. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  5033. {
  5034. int i, rc, error = 0;
  5035. uint16_t count, base;
  5036. unsigned long longs;
  5037. if (!phba->sli4_hba.rpi_hdrs_in_use)
  5038. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  5039. if (phba->sli4_hba.extents_in_use) {
  5040. /*
  5041. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5042. * resource extent count must be read and allocated before
  5043. * provisioning the resource id arrays.
  5044. */
  5045. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5046. LPFC_IDX_RSRC_RDY) {
  5047. /*
  5048. * Extent-based resources are set - the driver could
  5049. * be in a port reset. Figure out if any corrective
  5050. * actions need to be taken.
  5051. */
  5052. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5053. LPFC_RSC_TYPE_FCOE_VFI);
  5054. if (rc != 0)
  5055. error++;
  5056. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5057. LPFC_RSC_TYPE_FCOE_VPI);
  5058. if (rc != 0)
  5059. error++;
  5060. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5061. LPFC_RSC_TYPE_FCOE_XRI);
  5062. if (rc != 0)
  5063. error++;
  5064. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5065. LPFC_RSC_TYPE_FCOE_RPI);
  5066. if (rc != 0)
  5067. error++;
  5068. /*
  5069. * It's possible that the number of resources
  5070. * provided to this port instance changed between
  5071. * resets. Detect this condition and reallocate
  5072. * resources. Otherwise, there is no action.
  5073. */
  5074. if (error) {
  5075. lpfc_printf_log(phba, KERN_INFO,
  5076. LOG_MBOX | LOG_INIT,
  5077. "2931 Detected extent resource "
  5078. "change. Reallocating all "
  5079. "extents.\n");
  5080. rc = lpfc_sli4_dealloc_extent(phba,
  5081. LPFC_RSC_TYPE_FCOE_VFI);
  5082. rc = lpfc_sli4_dealloc_extent(phba,
  5083. LPFC_RSC_TYPE_FCOE_VPI);
  5084. rc = lpfc_sli4_dealloc_extent(phba,
  5085. LPFC_RSC_TYPE_FCOE_XRI);
  5086. rc = lpfc_sli4_dealloc_extent(phba,
  5087. LPFC_RSC_TYPE_FCOE_RPI);
  5088. } else
  5089. return 0;
  5090. }
  5091. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5092. if (unlikely(rc))
  5093. goto err_exit;
  5094. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5095. if (unlikely(rc))
  5096. goto err_exit;
  5097. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5098. if (unlikely(rc))
  5099. goto err_exit;
  5100. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5101. if (unlikely(rc))
  5102. goto err_exit;
  5103. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5104. LPFC_IDX_RSRC_RDY);
  5105. return rc;
  5106. } else {
  5107. /*
  5108. * The port does not support resource extents. The XRI, VPI,
  5109. * VFI, RPI resource ids were determined from READ_CONFIG.
  5110. * Just allocate the bitmasks and provision the resource id
  5111. * arrays. If a port reset is active, the resources don't
  5112. * need any action - just exit.
  5113. */
  5114. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5115. LPFC_IDX_RSRC_RDY) {
  5116. lpfc_sli4_dealloc_resource_identifiers(phba);
  5117. lpfc_sli4_remove_rpis(phba);
  5118. }
  5119. /* RPIs. */
  5120. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5121. if (count <= 0) {
  5122. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5123. "3279 Invalid provisioning of "
  5124. "rpi:%d\n", count);
  5125. rc = -EINVAL;
  5126. goto err_exit;
  5127. }
  5128. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5129. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5130. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5131. sizeof(unsigned long),
  5132. GFP_KERNEL);
  5133. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5134. rc = -ENOMEM;
  5135. goto err_exit;
  5136. }
  5137. phba->sli4_hba.rpi_ids = kzalloc(count *
  5138. sizeof(uint16_t),
  5139. GFP_KERNEL);
  5140. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5141. rc = -ENOMEM;
  5142. goto free_rpi_bmask;
  5143. }
  5144. for (i = 0; i < count; i++)
  5145. phba->sli4_hba.rpi_ids[i] = base + i;
  5146. /* VPIs. */
  5147. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5148. if (count <= 0) {
  5149. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5150. "3280 Invalid provisioning of "
  5151. "vpi:%d\n", count);
  5152. rc = -EINVAL;
  5153. goto free_rpi_ids;
  5154. }
  5155. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5156. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5157. phba->vpi_bmask = kzalloc(longs *
  5158. sizeof(unsigned long),
  5159. GFP_KERNEL);
  5160. if (unlikely(!phba->vpi_bmask)) {
  5161. rc = -ENOMEM;
  5162. goto free_rpi_ids;
  5163. }
  5164. phba->vpi_ids = kzalloc(count *
  5165. sizeof(uint16_t),
  5166. GFP_KERNEL);
  5167. if (unlikely(!phba->vpi_ids)) {
  5168. rc = -ENOMEM;
  5169. goto free_vpi_bmask;
  5170. }
  5171. for (i = 0; i < count; i++)
  5172. phba->vpi_ids[i] = base + i;
  5173. /* XRIs. */
  5174. count = phba->sli4_hba.max_cfg_param.max_xri;
  5175. if (count <= 0) {
  5176. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5177. "3281 Invalid provisioning of "
  5178. "xri:%d\n", count);
  5179. rc = -EINVAL;
  5180. goto free_vpi_ids;
  5181. }
  5182. base = phba->sli4_hba.max_cfg_param.xri_base;
  5183. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5184. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5185. sizeof(unsigned long),
  5186. GFP_KERNEL);
  5187. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5188. rc = -ENOMEM;
  5189. goto free_vpi_ids;
  5190. }
  5191. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5192. phba->sli4_hba.xri_ids = kzalloc(count *
  5193. sizeof(uint16_t),
  5194. GFP_KERNEL);
  5195. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5196. rc = -ENOMEM;
  5197. goto free_xri_bmask;
  5198. }
  5199. for (i = 0; i < count; i++)
  5200. phba->sli4_hba.xri_ids[i] = base + i;
  5201. /* VFIs. */
  5202. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5203. if (count <= 0) {
  5204. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5205. "3282 Invalid provisioning of "
  5206. "vfi:%d\n", count);
  5207. rc = -EINVAL;
  5208. goto free_xri_ids;
  5209. }
  5210. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5211. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5212. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5213. sizeof(unsigned long),
  5214. GFP_KERNEL);
  5215. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5216. rc = -ENOMEM;
  5217. goto free_xri_ids;
  5218. }
  5219. phba->sli4_hba.vfi_ids = kzalloc(count *
  5220. sizeof(uint16_t),
  5221. GFP_KERNEL);
  5222. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5223. rc = -ENOMEM;
  5224. goto free_vfi_bmask;
  5225. }
  5226. for (i = 0; i < count; i++)
  5227. phba->sli4_hba.vfi_ids[i] = base + i;
  5228. /*
  5229. * Mark all resources ready. An HBA reset doesn't need
  5230. * to reset the initialization.
  5231. */
  5232. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5233. LPFC_IDX_RSRC_RDY);
  5234. return 0;
  5235. }
  5236. free_vfi_bmask:
  5237. kfree(phba->sli4_hba.vfi_bmask);
  5238. free_xri_ids:
  5239. kfree(phba->sli4_hba.xri_ids);
  5240. free_xri_bmask:
  5241. kfree(phba->sli4_hba.xri_bmask);
  5242. free_vpi_ids:
  5243. kfree(phba->vpi_ids);
  5244. free_vpi_bmask:
  5245. kfree(phba->vpi_bmask);
  5246. free_rpi_ids:
  5247. kfree(phba->sli4_hba.rpi_ids);
  5248. free_rpi_bmask:
  5249. kfree(phba->sli4_hba.rpi_bmask);
  5250. err_exit:
  5251. return rc;
  5252. }
  5253. /**
  5254. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5255. * @phba: Pointer to HBA context object.
  5256. *
  5257. * This function allocates the number of elements for the specified
  5258. * resource type.
  5259. **/
  5260. int
  5261. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5262. {
  5263. if (phba->sli4_hba.extents_in_use) {
  5264. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5265. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5266. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5267. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5268. } else {
  5269. kfree(phba->vpi_bmask);
  5270. kfree(phba->vpi_ids);
  5271. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5272. kfree(phba->sli4_hba.xri_bmask);
  5273. kfree(phba->sli4_hba.xri_ids);
  5274. kfree(phba->sli4_hba.vfi_bmask);
  5275. kfree(phba->sli4_hba.vfi_ids);
  5276. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5277. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5278. }
  5279. return 0;
  5280. }
  5281. /**
  5282. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5283. * @phba: Pointer to HBA context object.
  5284. * @type: The resource extent type.
  5285. * @extnt_count: buffer to hold port extent count response
  5286. * @extnt_size: buffer to hold port extent size response.
  5287. *
  5288. * This function calls the port to read the host allocated extents
  5289. * for a particular type.
  5290. **/
  5291. int
  5292. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5293. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5294. {
  5295. bool emb;
  5296. int rc = 0;
  5297. uint16_t curr_blks = 0;
  5298. uint32_t req_len, emb_len;
  5299. uint32_t alloc_len, mbox_tmo;
  5300. struct list_head *blk_list_head;
  5301. struct lpfc_rsrc_blks *rsrc_blk;
  5302. LPFC_MBOXQ_t *mbox;
  5303. void *virtaddr = NULL;
  5304. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5305. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5306. union lpfc_sli4_cfg_shdr *shdr;
  5307. switch (type) {
  5308. case LPFC_RSC_TYPE_FCOE_VPI:
  5309. blk_list_head = &phba->lpfc_vpi_blk_list;
  5310. break;
  5311. case LPFC_RSC_TYPE_FCOE_XRI:
  5312. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5313. break;
  5314. case LPFC_RSC_TYPE_FCOE_VFI:
  5315. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5316. break;
  5317. case LPFC_RSC_TYPE_FCOE_RPI:
  5318. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5319. break;
  5320. default:
  5321. return -EIO;
  5322. }
  5323. /* Count the number of extents currently allocatd for this type. */
  5324. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5325. if (curr_blks == 0) {
  5326. /*
  5327. * The GET_ALLOCATED mailbox does not return the size,
  5328. * just the count. The size should be just the size
  5329. * stored in the current allocated block and all sizes
  5330. * for an extent type are the same so set the return
  5331. * value now.
  5332. */
  5333. *extnt_size = rsrc_blk->rsrc_size;
  5334. }
  5335. curr_blks++;
  5336. }
  5337. /* Calculate the total requested length of the dma memory. */
  5338. req_len = curr_blks * sizeof(uint16_t);
  5339. /*
  5340. * Calculate the size of an embedded mailbox. The uint32_t
  5341. * accounts for extents-specific word.
  5342. */
  5343. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5344. sizeof(uint32_t);
  5345. /*
  5346. * Presume the allocation and response will fit into an embedded
  5347. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5348. */
  5349. emb = LPFC_SLI4_MBX_EMBED;
  5350. req_len = emb_len;
  5351. if (req_len > emb_len) {
  5352. req_len = curr_blks * sizeof(uint16_t) +
  5353. sizeof(union lpfc_sli4_cfg_shdr) +
  5354. sizeof(uint32_t);
  5355. emb = LPFC_SLI4_MBX_NEMBED;
  5356. }
  5357. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5358. if (!mbox)
  5359. return -ENOMEM;
  5360. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5361. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5362. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5363. req_len, emb);
  5364. if (alloc_len < req_len) {
  5365. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5366. "2983 Allocated DMA memory size (x%x) is "
  5367. "less than the requested DMA memory "
  5368. "size (x%x)\n", alloc_len, req_len);
  5369. rc = -ENOMEM;
  5370. goto err_exit;
  5371. }
  5372. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5373. if (unlikely(rc)) {
  5374. rc = -EIO;
  5375. goto err_exit;
  5376. }
  5377. if (!phba->sli4_hba.intr_enable)
  5378. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5379. else {
  5380. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5381. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5382. }
  5383. if (unlikely(rc)) {
  5384. rc = -EIO;
  5385. goto err_exit;
  5386. }
  5387. /*
  5388. * Figure out where the response is located. Then get local pointers
  5389. * to the response data. The port does not guarantee to respond to
  5390. * all extents counts request so update the local variable with the
  5391. * allocated count from the port.
  5392. */
  5393. if (emb == LPFC_SLI4_MBX_EMBED) {
  5394. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5395. shdr = &rsrc_ext->header.cfg_shdr;
  5396. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5397. } else {
  5398. virtaddr = mbox->sge_array->addr[0];
  5399. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5400. shdr = &n_rsrc->cfg_shdr;
  5401. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5402. }
  5403. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5404. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5405. "2984 Failed to read allocated resources "
  5406. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5407. type,
  5408. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5409. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5410. rc = -EIO;
  5411. goto err_exit;
  5412. }
  5413. err_exit:
  5414. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5415. return rc;
  5416. }
  5417. /**
  5418. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5419. * @phba: pointer to lpfc hba data structure.
  5420. *
  5421. * This routine walks the list of els buffers that have been allocated and
  5422. * repost them to the port by using SGL block post. This is needed after a
  5423. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5424. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5425. * SGL block post mailbox commands to post them to the port. For single els
  5426. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5427. * mailbox command for posting.
  5428. *
  5429. * Returns: 0 = success, non-zero failure.
  5430. **/
  5431. static int
  5432. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5433. {
  5434. struct lpfc_sglq *sglq_entry = NULL;
  5435. struct lpfc_sglq *sglq_entry_next = NULL;
  5436. struct lpfc_sglq *sglq_entry_first = NULL;
  5437. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5438. int last_xritag = NO_XRI;
  5439. LIST_HEAD(prep_sgl_list);
  5440. LIST_HEAD(blck_sgl_list);
  5441. LIST_HEAD(allc_sgl_list);
  5442. LIST_HEAD(post_sgl_list);
  5443. LIST_HEAD(free_sgl_list);
  5444. spin_lock(&phba->hbalock);
  5445. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5446. spin_unlock(&phba->hbalock);
  5447. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5448. &allc_sgl_list, list) {
  5449. list_del_init(&sglq_entry->list);
  5450. block_cnt++;
  5451. if ((last_xritag != NO_XRI) &&
  5452. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5453. /* a hole in xri block, form a sgl posting block */
  5454. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5455. post_cnt = block_cnt - 1;
  5456. /* prepare list for next posting block */
  5457. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5458. block_cnt = 1;
  5459. } else {
  5460. /* prepare list for next posting block */
  5461. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5462. /* enough sgls for non-embed sgl mbox command */
  5463. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5464. list_splice_init(&prep_sgl_list,
  5465. &blck_sgl_list);
  5466. post_cnt = block_cnt;
  5467. block_cnt = 0;
  5468. }
  5469. }
  5470. num_posted++;
  5471. /* keep track of last sgl's xritag */
  5472. last_xritag = sglq_entry->sli4_xritag;
  5473. /* end of repost sgl list condition for els buffers */
  5474. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5475. if (post_cnt == 0) {
  5476. list_splice_init(&prep_sgl_list,
  5477. &blck_sgl_list);
  5478. post_cnt = block_cnt;
  5479. } else if (block_cnt == 1) {
  5480. status = lpfc_sli4_post_sgl(phba,
  5481. sglq_entry->phys, 0,
  5482. sglq_entry->sli4_xritag);
  5483. if (!status) {
  5484. /* successful, put sgl to posted list */
  5485. list_add_tail(&sglq_entry->list,
  5486. &post_sgl_list);
  5487. } else {
  5488. /* Failure, put sgl to free list */
  5489. lpfc_printf_log(phba, KERN_WARNING,
  5490. LOG_SLI,
  5491. "3159 Failed to post els "
  5492. "sgl, xritag:x%x\n",
  5493. sglq_entry->sli4_xritag);
  5494. list_add_tail(&sglq_entry->list,
  5495. &free_sgl_list);
  5496. spin_lock_irq(&phba->hbalock);
  5497. phba->sli4_hba.els_xri_cnt--;
  5498. spin_unlock_irq(&phba->hbalock);
  5499. }
  5500. }
  5501. }
  5502. /* continue until a nembed page worth of sgls */
  5503. if (post_cnt == 0)
  5504. continue;
  5505. /* post the els buffer list sgls as a block */
  5506. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5507. post_cnt);
  5508. if (!status) {
  5509. /* success, put sgl list to posted sgl list */
  5510. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5511. } else {
  5512. /* Failure, put sgl list to free sgl list */
  5513. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5514. struct lpfc_sglq,
  5515. list);
  5516. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5517. "3160 Failed to post els sgl-list, "
  5518. "xritag:x%x-x%x\n",
  5519. sglq_entry_first->sli4_xritag,
  5520. (sglq_entry_first->sli4_xritag +
  5521. post_cnt - 1));
  5522. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5523. spin_lock_irq(&phba->hbalock);
  5524. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5525. spin_unlock_irq(&phba->hbalock);
  5526. }
  5527. /* don't reset xirtag due to hole in xri block */
  5528. if (block_cnt == 0)
  5529. last_xritag = NO_XRI;
  5530. /* reset els sgl post count for next round of posting */
  5531. post_cnt = 0;
  5532. }
  5533. /* free the els sgls failed to post */
  5534. lpfc_free_sgl_list(phba, &free_sgl_list);
  5535. /* push els sgls posted to the availble list */
  5536. if (!list_empty(&post_sgl_list)) {
  5537. spin_lock(&phba->hbalock);
  5538. list_splice_init(&post_sgl_list,
  5539. &phba->sli4_hba.lpfc_sgl_list);
  5540. spin_unlock(&phba->hbalock);
  5541. } else {
  5542. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5543. "3161 Failure to post els sgl to port.\n");
  5544. return -EIO;
  5545. }
  5546. return 0;
  5547. }
  5548. /**
  5549. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5550. * @phba: Pointer to HBA context object.
  5551. *
  5552. * This function is the main SLI4 device intialization PCI function. This
  5553. * function is called by the HBA intialization code, HBA reset code and
  5554. * HBA error attention handler code. Caller is not required to hold any
  5555. * locks.
  5556. **/
  5557. int
  5558. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5559. {
  5560. int rc;
  5561. LPFC_MBOXQ_t *mboxq;
  5562. struct lpfc_mqe *mqe;
  5563. uint8_t *vpd;
  5564. uint32_t vpd_size;
  5565. uint32_t ftr_rsp = 0;
  5566. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5567. struct lpfc_vport *vport = phba->pport;
  5568. struct lpfc_dmabuf *mp;
  5569. /* Perform a PCI function reset to start from clean */
  5570. rc = lpfc_pci_function_reset(phba);
  5571. if (unlikely(rc))
  5572. return -ENODEV;
  5573. /* Check the HBA Host Status Register for readyness */
  5574. rc = lpfc_sli4_post_status_check(phba);
  5575. if (unlikely(rc))
  5576. return -ENODEV;
  5577. else {
  5578. spin_lock_irq(&phba->hbalock);
  5579. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5580. spin_unlock_irq(&phba->hbalock);
  5581. }
  5582. /*
  5583. * Allocate a single mailbox container for initializing the
  5584. * port.
  5585. */
  5586. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5587. if (!mboxq)
  5588. return -ENOMEM;
  5589. /* Issue READ_REV to collect vpd and FW information. */
  5590. vpd_size = SLI4_PAGE_SIZE;
  5591. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5592. if (!vpd) {
  5593. rc = -ENOMEM;
  5594. goto out_free_mbox;
  5595. }
  5596. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5597. if (unlikely(rc)) {
  5598. kfree(vpd);
  5599. goto out_free_mbox;
  5600. }
  5601. mqe = &mboxq->u.mqe;
  5602. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5603. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5604. phba->hba_flag |= HBA_FCOE_MODE;
  5605. else
  5606. phba->hba_flag &= ~HBA_FCOE_MODE;
  5607. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5608. LPFC_DCBX_CEE_MODE)
  5609. phba->hba_flag |= HBA_FIP_SUPPORT;
  5610. else
  5611. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5612. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5613. if (phba->sli_rev != LPFC_SLI_REV4) {
  5614. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5615. "0376 READ_REV Error. SLI Level %d "
  5616. "FCoE enabled %d\n",
  5617. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5618. rc = -EIO;
  5619. kfree(vpd);
  5620. goto out_free_mbox;
  5621. }
  5622. /*
  5623. * Continue initialization with default values even if driver failed
  5624. * to read FCoE param config regions, only read parameters if the
  5625. * board is FCoE
  5626. */
  5627. if (phba->hba_flag & HBA_FCOE_MODE &&
  5628. lpfc_sli4_read_fcoe_params(phba))
  5629. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5630. "2570 Failed to read FCoE parameters\n");
  5631. /*
  5632. * Retrieve sli4 device physical port name, failure of doing it
  5633. * is considered as non-fatal.
  5634. */
  5635. rc = lpfc_sli4_retrieve_pport_name(phba);
  5636. if (!rc)
  5637. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5638. "3080 Successful retrieving SLI4 device "
  5639. "physical port name: %s.\n", phba->Port);
  5640. /*
  5641. * Evaluate the read rev and vpd data. Populate the driver
  5642. * state with the results. If this routine fails, the failure
  5643. * is not fatal as the driver will use generic values.
  5644. */
  5645. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5646. if (unlikely(!rc)) {
  5647. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5648. "0377 Error %d parsing vpd. "
  5649. "Using defaults.\n", rc);
  5650. rc = 0;
  5651. }
  5652. kfree(vpd);
  5653. /* Save information as VPD data */
  5654. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5655. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5656. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5657. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5658. &mqe->un.read_rev);
  5659. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5660. &mqe->un.read_rev);
  5661. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5662. &mqe->un.read_rev);
  5663. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5664. &mqe->un.read_rev);
  5665. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5666. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5667. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5668. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5669. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5670. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5671. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5672. "(%d):0380 READ_REV Status x%x "
  5673. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5674. mboxq->vport ? mboxq->vport->vpi : 0,
  5675. bf_get(lpfc_mqe_status, mqe),
  5676. phba->vpd.rev.opFwName,
  5677. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5678. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5679. /*
  5680. * Discover the port's supported feature set and match it against the
  5681. * hosts requests.
  5682. */
  5683. lpfc_request_features(phba, mboxq);
  5684. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5685. if (unlikely(rc)) {
  5686. rc = -EIO;
  5687. goto out_free_mbox;
  5688. }
  5689. /*
  5690. * The port must support FCP initiator mode as this is the
  5691. * only mode running in the host.
  5692. */
  5693. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5694. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5695. "0378 No support for fcpi mode.\n");
  5696. ftr_rsp++;
  5697. }
  5698. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5699. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5700. else
  5701. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5702. /*
  5703. * If the port cannot support the host's requested features
  5704. * then turn off the global config parameters to disable the
  5705. * feature in the driver. This is not a fatal error.
  5706. */
  5707. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5708. if (phba->cfg_enable_bg) {
  5709. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5710. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5711. else
  5712. ftr_rsp++;
  5713. }
  5714. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5715. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5716. ftr_rsp++;
  5717. if (ftr_rsp) {
  5718. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5719. "0379 Feature Mismatch Data: x%08x %08x "
  5720. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5721. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5722. phba->cfg_enable_npiv, phba->max_vpi);
  5723. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5724. phba->cfg_enable_bg = 0;
  5725. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5726. phba->cfg_enable_npiv = 0;
  5727. }
  5728. /* These SLI3 features are assumed in SLI4 */
  5729. spin_lock_irq(&phba->hbalock);
  5730. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5731. spin_unlock_irq(&phba->hbalock);
  5732. /*
  5733. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5734. * calls depends on these resources to complete port setup.
  5735. */
  5736. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5737. if (rc) {
  5738. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5739. "2920 Failed to alloc Resource IDs "
  5740. "rc = x%x\n", rc);
  5741. goto out_free_mbox;
  5742. }
  5743. /* Read the port's service parameters. */
  5744. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5745. if (rc) {
  5746. phba->link_state = LPFC_HBA_ERROR;
  5747. rc = -ENOMEM;
  5748. goto out_free_mbox;
  5749. }
  5750. mboxq->vport = vport;
  5751. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5752. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5753. if (rc == MBX_SUCCESS) {
  5754. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5755. rc = 0;
  5756. }
  5757. /*
  5758. * This memory was allocated by the lpfc_read_sparam routine. Release
  5759. * it to the mbuf pool.
  5760. */
  5761. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5762. kfree(mp);
  5763. mboxq->context1 = NULL;
  5764. if (unlikely(rc)) {
  5765. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5766. "0382 READ_SPARAM command failed "
  5767. "status %d, mbxStatus x%x\n",
  5768. rc, bf_get(lpfc_mqe_status, mqe));
  5769. phba->link_state = LPFC_HBA_ERROR;
  5770. rc = -EIO;
  5771. goto out_free_mbox;
  5772. }
  5773. lpfc_update_vport_wwn(vport);
  5774. /* Update the fc_host data structures with new wwn. */
  5775. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5776. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5777. /* update host els and scsi xri-sgl sizes and mappings */
  5778. rc = lpfc_sli4_xri_sgl_update(phba);
  5779. if (unlikely(rc)) {
  5780. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5781. "1400 Failed to update xri-sgl size and "
  5782. "mapping: %d\n", rc);
  5783. goto out_free_mbox;
  5784. }
  5785. /* register the els sgl pool to the port */
  5786. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5787. if (unlikely(rc)) {
  5788. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5789. "0582 Error %d during els sgl post "
  5790. "operation\n", rc);
  5791. rc = -ENODEV;
  5792. goto out_free_mbox;
  5793. }
  5794. /* register the allocated scsi sgl pool to the port */
  5795. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5796. if (unlikely(rc)) {
  5797. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5798. "0383 Error %d during scsi sgl post "
  5799. "operation\n", rc);
  5800. /* Some Scsi buffers were moved to the abort scsi list */
  5801. /* A pci function reset will repost them */
  5802. rc = -ENODEV;
  5803. goto out_free_mbox;
  5804. }
  5805. /* Post the rpi header region to the device. */
  5806. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5807. if (unlikely(rc)) {
  5808. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5809. "0393 Error %d during rpi post operation\n",
  5810. rc);
  5811. rc = -ENODEV;
  5812. goto out_free_mbox;
  5813. }
  5814. lpfc_sli4_node_prep(phba);
  5815. /* Create all the SLI4 queues */
  5816. rc = lpfc_sli4_queue_create(phba);
  5817. if (rc) {
  5818. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5819. "3089 Failed to allocate queues\n");
  5820. rc = -ENODEV;
  5821. goto out_stop_timers;
  5822. }
  5823. /* Set up all the queues to the device */
  5824. rc = lpfc_sli4_queue_setup(phba);
  5825. if (unlikely(rc)) {
  5826. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5827. "0381 Error %d during queue setup.\n ", rc);
  5828. goto out_destroy_queue;
  5829. }
  5830. /* Arm the CQs and then EQs on device */
  5831. lpfc_sli4_arm_cqeq_intr(phba);
  5832. /* Indicate device interrupt mode */
  5833. phba->sli4_hba.intr_enable = 1;
  5834. /* Allow asynchronous mailbox command to go through */
  5835. spin_lock_irq(&phba->hbalock);
  5836. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5837. spin_unlock_irq(&phba->hbalock);
  5838. /* Post receive buffers to the device */
  5839. lpfc_sli4_rb_setup(phba);
  5840. /* Reset HBA FCF states after HBA reset */
  5841. phba->fcf.fcf_flag = 0;
  5842. phba->fcf.current_rec.flag = 0;
  5843. /* Start the ELS watchdog timer */
  5844. mod_timer(&vport->els_tmofunc,
  5845. jiffies + HZ * (phba->fc_ratov * 2));
  5846. /* Start heart beat timer */
  5847. mod_timer(&phba->hb_tmofunc,
  5848. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5849. phba->hb_outstanding = 0;
  5850. phba->last_completion_time = jiffies;
  5851. /* Start error attention (ERATT) polling timer */
  5852. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5853. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5854. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5855. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5856. if (!rc) {
  5857. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5858. "2829 This device supports "
  5859. "Advanced Error Reporting (AER)\n");
  5860. spin_lock_irq(&phba->hbalock);
  5861. phba->hba_flag |= HBA_AER_ENABLED;
  5862. spin_unlock_irq(&phba->hbalock);
  5863. } else {
  5864. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5865. "2830 This device does not support "
  5866. "Advanced Error Reporting (AER)\n");
  5867. phba->cfg_aer_support = 0;
  5868. }
  5869. rc = 0;
  5870. }
  5871. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5872. /*
  5873. * The FC Port needs to register FCFI (index 0)
  5874. */
  5875. lpfc_reg_fcfi(phba, mboxq);
  5876. mboxq->vport = phba->pport;
  5877. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5878. if (rc != MBX_SUCCESS)
  5879. goto out_unset_queue;
  5880. rc = 0;
  5881. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5882. &mboxq->u.mqe.un.reg_fcfi);
  5883. /* Check if the port is configured to be disabled */
  5884. lpfc_sli_read_link_ste(phba);
  5885. }
  5886. /*
  5887. * The port is ready, set the host's link state to LINK_DOWN
  5888. * in preparation for link interrupts.
  5889. */
  5890. spin_lock_irq(&phba->hbalock);
  5891. phba->link_state = LPFC_LINK_DOWN;
  5892. spin_unlock_irq(&phba->hbalock);
  5893. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5894. (phba->hba_flag & LINK_DISABLED)) {
  5895. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5896. "3103 Adapter Link is disabled.\n");
  5897. lpfc_down_link(phba, mboxq);
  5898. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5899. if (rc != MBX_SUCCESS) {
  5900. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5901. "3104 Adapter failed to issue "
  5902. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5903. goto out_unset_queue;
  5904. }
  5905. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5906. /* don't perform init_link on SLI4 FC port loopback test */
  5907. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5908. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5909. if (rc)
  5910. goto out_unset_queue;
  5911. }
  5912. }
  5913. mempool_free(mboxq, phba->mbox_mem_pool);
  5914. return rc;
  5915. out_unset_queue:
  5916. /* Unset all the queues set up in this routine when error out */
  5917. lpfc_sli4_queue_unset(phba);
  5918. out_destroy_queue:
  5919. lpfc_sli4_queue_destroy(phba);
  5920. out_stop_timers:
  5921. lpfc_stop_hba_timers(phba);
  5922. out_free_mbox:
  5923. mempool_free(mboxq, phba->mbox_mem_pool);
  5924. return rc;
  5925. }
  5926. /**
  5927. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5928. * @ptr: context object - pointer to hba structure.
  5929. *
  5930. * This is the callback function for mailbox timer. The mailbox
  5931. * timer is armed when a new mailbox command is issued and the timer
  5932. * is deleted when the mailbox complete. The function is called by
  5933. * the kernel timer code when a mailbox does not complete within
  5934. * expected time. This function wakes up the worker thread to
  5935. * process the mailbox timeout and returns. All the processing is
  5936. * done by the worker thread function lpfc_mbox_timeout_handler.
  5937. **/
  5938. void
  5939. lpfc_mbox_timeout(unsigned long ptr)
  5940. {
  5941. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5942. unsigned long iflag;
  5943. uint32_t tmo_posted;
  5944. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5945. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5946. if (!tmo_posted)
  5947. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5948. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5949. if (!tmo_posted)
  5950. lpfc_worker_wake_up(phba);
  5951. return;
  5952. }
  5953. /**
  5954. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5955. * @phba: Pointer to HBA context object.
  5956. *
  5957. * This function is called from worker thread when a mailbox command times out.
  5958. * The caller is not required to hold any locks. This function will reset the
  5959. * HBA and recover all the pending commands.
  5960. **/
  5961. void
  5962. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5963. {
  5964. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5965. MAILBOX_t *mb = &pmbox->u.mb;
  5966. struct lpfc_sli *psli = &phba->sli;
  5967. struct lpfc_sli_ring *pring;
  5968. /* Check the pmbox pointer first. There is a race condition
  5969. * between the mbox timeout handler getting executed in the
  5970. * worklist and the mailbox actually completing. When this
  5971. * race condition occurs, the mbox_active will be NULL.
  5972. */
  5973. spin_lock_irq(&phba->hbalock);
  5974. if (pmbox == NULL) {
  5975. lpfc_printf_log(phba, KERN_WARNING,
  5976. LOG_MBOX | LOG_SLI,
  5977. "0353 Active Mailbox cleared - mailbox timeout "
  5978. "exiting\n");
  5979. spin_unlock_irq(&phba->hbalock);
  5980. return;
  5981. }
  5982. /* Mbox cmd <mbxCommand> timeout */
  5983. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5984. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5985. mb->mbxCommand,
  5986. phba->pport->port_state,
  5987. phba->sli.sli_flag,
  5988. phba->sli.mbox_active);
  5989. spin_unlock_irq(&phba->hbalock);
  5990. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5991. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5992. * it to fail all outstanding SCSI IO.
  5993. */
  5994. spin_lock_irq(&phba->pport->work_port_lock);
  5995. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5996. spin_unlock_irq(&phba->pport->work_port_lock);
  5997. spin_lock_irq(&phba->hbalock);
  5998. phba->link_state = LPFC_LINK_UNKNOWN;
  5999. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  6000. spin_unlock_irq(&phba->hbalock);
  6001. pring = &psli->ring[psli->fcp_ring];
  6002. lpfc_sli_abort_iocb_ring(phba, pring);
  6003. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6004. "0345 Resetting board due to mailbox timeout\n");
  6005. /* Reset the HBA device */
  6006. lpfc_reset_hba(phba);
  6007. }
  6008. /**
  6009. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  6010. * @phba: Pointer to HBA context object.
  6011. * @pmbox: Pointer to mailbox object.
  6012. * @flag: Flag indicating how the mailbox need to be processed.
  6013. *
  6014. * This function is called by discovery code and HBA management code
  6015. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  6016. * function gets the hbalock to protect the data structures.
  6017. * The mailbox command can be submitted in polling mode, in which case
  6018. * this function will wait in a polling loop for the completion of the
  6019. * mailbox.
  6020. * If the mailbox is submitted in no_wait mode (not polling) the
  6021. * function will submit the command and returns immediately without waiting
  6022. * for the mailbox completion. The no_wait is supported only when HBA
  6023. * is in SLI2/SLI3 mode - interrupts are enabled.
  6024. * The SLI interface allows only one mailbox pending at a time. If the
  6025. * mailbox is issued in polling mode and there is already a mailbox
  6026. * pending, then the function will return an error. If the mailbox is issued
  6027. * in NO_WAIT mode and there is a mailbox pending already, the function
  6028. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  6029. * The sli layer owns the mailbox object until the completion of mailbox
  6030. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  6031. * return codes the caller owns the mailbox command after the return of
  6032. * the function.
  6033. **/
  6034. static int
  6035. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  6036. uint32_t flag)
  6037. {
  6038. MAILBOX_t *mbx;
  6039. struct lpfc_sli *psli = &phba->sli;
  6040. uint32_t status, evtctr;
  6041. uint32_t ha_copy, hc_copy;
  6042. int i;
  6043. unsigned long timeout;
  6044. unsigned long drvr_flag = 0;
  6045. uint32_t word0, ldata;
  6046. void __iomem *to_slim;
  6047. int processing_queue = 0;
  6048. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6049. if (!pmbox) {
  6050. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6051. /* processing mbox queue from intr_handler */
  6052. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6053. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6054. return MBX_SUCCESS;
  6055. }
  6056. processing_queue = 1;
  6057. pmbox = lpfc_mbox_get(phba);
  6058. if (!pmbox) {
  6059. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6060. return MBX_SUCCESS;
  6061. }
  6062. }
  6063. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  6064. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  6065. if(!pmbox->vport) {
  6066. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6067. lpfc_printf_log(phba, KERN_ERR,
  6068. LOG_MBOX | LOG_VPORT,
  6069. "1806 Mbox x%x failed. No vport\n",
  6070. pmbox->u.mb.mbxCommand);
  6071. dump_stack();
  6072. goto out_not_finished;
  6073. }
  6074. }
  6075. /* If the PCI channel is in offline state, do not post mbox. */
  6076. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6077. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6078. goto out_not_finished;
  6079. }
  6080. /* If HBA has a deferred error attention, fail the iocb. */
  6081. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6082. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6083. goto out_not_finished;
  6084. }
  6085. psli = &phba->sli;
  6086. mbx = &pmbox->u.mb;
  6087. status = MBX_SUCCESS;
  6088. if (phba->link_state == LPFC_HBA_ERROR) {
  6089. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6090. /* Mbox command <mbxCommand> cannot issue */
  6091. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6092. "(%d):0311 Mailbox command x%x cannot "
  6093. "issue Data: x%x x%x\n",
  6094. pmbox->vport ? pmbox->vport->vpi : 0,
  6095. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6096. goto out_not_finished;
  6097. }
  6098. if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6099. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6100. !(hc_copy & HC_MBINT_ENA)) {
  6101. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6102. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6103. "(%d):2528 Mailbox command x%x cannot "
  6104. "issue Data: x%x x%x\n",
  6105. pmbox->vport ? pmbox->vport->vpi : 0,
  6106. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6107. goto out_not_finished;
  6108. }
  6109. }
  6110. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6111. /* Polling for a mbox command when another one is already active
  6112. * is not allowed in SLI. Also, the driver must have established
  6113. * SLI2 mode to queue and process multiple mbox commands.
  6114. */
  6115. if (flag & MBX_POLL) {
  6116. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6117. /* Mbox command <mbxCommand> cannot issue */
  6118. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6119. "(%d):2529 Mailbox command x%x "
  6120. "cannot issue Data: x%x x%x\n",
  6121. pmbox->vport ? pmbox->vport->vpi : 0,
  6122. pmbox->u.mb.mbxCommand,
  6123. psli->sli_flag, flag);
  6124. goto out_not_finished;
  6125. }
  6126. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6127. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6128. /* Mbox command <mbxCommand> cannot issue */
  6129. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6130. "(%d):2530 Mailbox command x%x "
  6131. "cannot issue Data: x%x x%x\n",
  6132. pmbox->vport ? pmbox->vport->vpi : 0,
  6133. pmbox->u.mb.mbxCommand,
  6134. psli->sli_flag, flag);
  6135. goto out_not_finished;
  6136. }
  6137. /* Another mailbox command is still being processed, queue this
  6138. * command to be processed later.
  6139. */
  6140. lpfc_mbox_put(phba, pmbox);
  6141. /* Mbox cmd issue - BUSY */
  6142. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6143. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6144. "x%x x%x x%x x%x\n",
  6145. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6146. mbx->mbxCommand, phba->pport->port_state,
  6147. psli->sli_flag, flag);
  6148. psli->slistat.mbox_busy++;
  6149. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6150. if (pmbox->vport) {
  6151. lpfc_debugfs_disc_trc(pmbox->vport,
  6152. LPFC_DISC_TRC_MBOX_VPORT,
  6153. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6154. (uint32_t)mbx->mbxCommand,
  6155. mbx->un.varWords[0], mbx->un.varWords[1]);
  6156. }
  6157. else {
  6158. lpfc_debugfs_disc_trc(phba->pport,
  6159. LPFC_DISC_TRC_MBOX,
  6160. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6161. (uint32_t)mbx->mbxCommand,
  6162. mbx->un.varWords[0], mbx->un.varWords[1]);
  6163. }
  6164. return MBX_BUSY;
  6165. }
  6166. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6167. /* If we are not polling, we MUST be in SLI2 mode */
  6168. if (flag != MBX_POLL) {
  6169. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6170. (mbx->mbxCommand != MBX_KILL_BOARD)) {
  6171. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6172. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6173. /* Mbox command <mbxCommand> cannot issue */
  6174. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6175. "(%d):2531 Mailbox command x%x "
  6176. "cannot issue Data: x%x x%x\n",
  6177. pmbox->vport ? pmbox->vport->vpi : 0,
  6178. pmbox->u.mb.mbxCommand,
  6179. psli->sli_flag, flag);
  6180. goto out_not_finished;
  6181. }
  6182. /* timeout active mbox command */
  6183. mod_timer(&psli->mbox_tmo, (jiffies +
  6184. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6185. }
  6186. /* Mailbox cmd <cmd> issue */
  6187. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6188. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6189. "x%x\n",
  6190. pmbox->vport ? pmbox->vport->vpi : 0,
  6191. mbx->mbxCommand, phba->pport->port_state,
  6192. psli->sli_flag, flag);
  6193. if (mbx->mbxCommand != MBX_HEARTBEAT) {
  6194. if (pmbox->vport) {
  6195. lpfc_debugfs_disc_trc(pmbox->vport,
  6196. LPFC_DISC_TRC_MBOX_VPORT,
  6197. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6198. (uint32_t)mbx->mbxCommand,
  6199. mbx->un.varWords[0], mbx->un.varWords[1]);
  6200. }
  6201. else {
  6202. lpfc_debugfs_disc_trc(phba->pport,
  6203. LPFC_DISC_TRC_MBOX,
  6204. "MBOX Send: cmd:x%x mb:x%x x%x",
  6205. (uint32_t)mbx->mbxCommand,
  6206. mbx->un.varWords[0], mbx->un.varWords[1]);
  6207. }
  6208. }
  6209. psli->slistat.mbox_cmd++;
  6210. evtctr = psli->slistat.mbox_event;
  6211. /* next set own bit for the adapter and copy over command word */
  6212. mbx->mbxOwner = OWN_CHIP;
  6213. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6214. /* Populate mbox extension offset word. */
  6215. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6216. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6217. = (uint8_t *)phba->mbox_ext
  6218. - (uint8_t *)phba->mbox;
  6219. }
  6220. /* Copy the mailbox extension data */
  6221. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6222. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6223. (uint8_t *)phba->mbox_ext,
  6224. pmbox->in_ext_byte_len);
  6225. }
  6226. /* Copy command data to host SLIM area */
  6227. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6228. } else {
  6229. /* Populate mbox extension offset word. */
  6230. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6231. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6232. = MAILBOX_HBA_EXT_OFFSET;
  6233. /* Copy the mailbox extension data */
  6234. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6235. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6236. MAILBOX_HBA_EXT_OFFSET,
  6237. pmbox->context2, pmbox->in_ext_byte_len);
  6238. }
  6239. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6240. /* copy command data into host mbox for cmpl */
  6241. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6242. }
  6243. /* First copy mbox command data to HBA SLIM, skip past first
  6244. word */
  6245. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6246. lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
  6247. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6248. /* Next copy over first word, with mbxOwner set */
  6249. ldata = *((uint32_t *)mbx);
  6250. to_slim = phba->MBslimaddr;
  6251. writel(ldata, to_slim);
  6252. readl(to_slim); /* flush */
  6253. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6254. /* switch over to host mailbox */
  6255. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6256. }
  6257. }
  6258. wmb();
  6259. switch (flag) {
  6260. case MBX_NOWAIT:
  6261. /* Set up reference to mailbox command */
  6262. psli->mbox_active = pmbox;
  6263. /* Interrupt board to do it */
  6264. writel(CA_MBATT, phba->CAregaddr);
  6265. readl(phba->CAregaddr); /* flush */
  6266. /* Don't wait for it to finish, just return */
  6267. break;
  6268. case MBX_POLL:
  6269. /* Set up null reference to mailbox command */
  6270. psli->mbox_active = NULL;
  6271. /* Interrupt board to do it */
  6272. writel(CA_MBATT, phba->CAregaddr);
  6273. readl(phba->CAregaddr); /* flush */
  6274. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6275. /* First read mbox status word */
  6276. word0 = *((uint32_t *)phba->mbox);
  6277. word0 = le32_to_cpu(word0);
  6278. } else {
  6279. /* First read mbox status word */
  6280. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6281. spin_unlock_irqrestore(&phba->hbalock,
  6282. drvr_flag);
  6283. goto out_not_finished;
  6284. }
  6285. }
  6286. /* Read the HBA Host Attention Register */
  6287. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6288. spin_unlock_irqrestore(&phba->hbalock,
  6289. drvr_flag);
  6290. goto out_not_finished;
  6291. }
  6292. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6293. 1000) + jiffies;
  6294. i = 0;
  6295. /* Wait for command to complete */
  6296. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6297. (!(ha_copy & HA_MBATT) &&
  6298. (phba->link_state > LPFC_WARM_START))) {
  6299. if (time_after(jiffies, timeout)) {
  6300. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6301. spin_unlock_irqrestore(&phba->hbalock,
  6302. drvr_flag);
  6303. goto out_not_finished;
  6304. }
  6305. /* Check if we took a mbox interrupt while we were
  6306. polling */
  6307. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6308. && (evtctr != psli->slistat.mbox_event))
  6309. break;
  6310. if (i++ > 10) {
  6311. spin_unlock_irqrestore(&phba->hbalock,
  6312. drvr_flag);
  6313. msleep(1);
  6314. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6315. }
  6316. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6317. /* First copy command data */
  6318. word0 = *((uint32_t *)phba->mbox);
  6319. word0 = le32_to_cpu(word0);
  6320. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6321. MAILBOX_t *slimmb;
  6322. uint32_t slimword0;
  6323. /* Check real SLIM for any errors */
  6324. slimword0 = readl(phba->MBslimaddr);
  6325. slimmb = (MAILBOX_t *) & slimword0;
  6326. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6327. && slimmb->mbxStatus) {
  6328. psli->sli_flag &=
  6329. ~LPFC_SLI_ACTIVE;
  6330. word0 = slimword0;
  6331. }
  6332. }
  6333. } else {
  6334. /* First copy command data */
  6335. word0 = readl(phba->MBslimaddr);
  6336. }
  6337. /* Read the HBA Host Attention Register */
  6338. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6339. spin_unlock_irqrestore(&phba->hbalock,
  6340. drvr_flag);
  6341. goto out_not_finished;
  6342. }
  6343. }
  6344. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6345. /* copy results back to user */
  6346. lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
  6347. /* Copy the mailbox extension data */
  6348. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6349. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6350. pmbox->context2,
  6351. pmbox->out_ext_byte_len);
  6352. }
  6353. } else {
  6354. /* First copy command data */
  6355. lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
  6356. MAILBOX_CMD_SIZE);
  6357. /* Copy the mailbox extension data */
  6358. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6359. lpfc_memcpy_from_slim(pmbox->context2,
  6360. phba->MBslimaddr +
  6361. MAILBOX_HBA_EXT_OFFSET,
  6362. pmbox->out_ext_byte_len);
  6363. }
  6364. }
  6365. writel(HA_MBATT, phba->HAregaddr);
  6366. readl(phba->HAregaddr); /* flush */
  6367. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6368. status = mbx->mbxStatus;
  6369. }
  6370. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6371. return status;
  6372. out_not_finished:
  6373. if (processing_queue) {
  6374. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6375. lpfc_mbox_cmpl_put(phba, pmbox);
  6376. }
  6377. return MBX_NOT_FINISHED;
  6378. }
  6379. /**
  6380. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6381. * @phba: Pointer to HBA context object.
  6382. *
  6383. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6384. * the driver internal pending mailbox queue. It will then try to wait out the
  6385. * possible outstanding mailbox command before return.
  6386. *
  6387. * Returns:
  6388. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6389. * the outstanding mailbox command timed out.
  6390. **/
  6391. static int
  6392. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6393. {
  6394. struct lpfc_sli *psli = &phba->sli;
  6395. int rc = 0;
  6396. unsigned long timeout = 0;
  6397. /* Mark the asynchronous mailbox command posting as blocked */
  6398. spin_lock_irq(&phba->hbalock);
  6399. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6400. /* Determine how long we might wait for the active mailbox
  6401. * command to be gracefully completed by firmware.
  6402. */
  6403. if (phba->sli.mbox_active)
  6404. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6405. phba->sli.mbox_active) *
  6406. 1000) + jiffies;
  6407. spin_unlock_irq(&phba->hbalock);
  6408. /* Wait for the outstnading mailbox command to complete */
  6409. while (phba->sli.mbox_active) {
  6410. /* Check active mailbox complete status every 2ms */
  6411. msleep(2);
  6412. if (time_after(jiffies, timeout)) {
  6413. /* Timeout, marked the outstanding cmd not complete */
  6414. rc = 1;
  6415. break;
  6416. }
  6417. }
  6418. /* Can not cleanly block async mailbox command, fails it */
  6419. if (rc) {
  6420. spin_lock_irq(&phba->hbalock);
  6421. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6422. spin_unlock_irq(&phba->hbalock);
  6423. }
  6424. return rc;
  6425. }
  6426. /**
  6427. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6428. * @phba: Pointer to HBA context object.
  6429. *
  6430. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6431. * commands from the driver internal pending mailbox queue. It makes sure
  6432. * that there is no outstanding mailbox command before resuming posting
  6433. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6434. * mailbox command, it will try to wait it out before resuming asynchronous
  6435. * mailbox command posting.
  6436. **/
  6437. static void
  6438. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6439. {
  6440. struct lpfc_sli *psli = &phba->sli;
  6441. spin_lock_irq(&phba->hbalock);
  6442. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6443. /* Asynchronous mailbox posting is not blocked, do nothing */
  6444. spin_unlock_irq(&phba->hbalock);
  6445. return;
  6446. }
  6447. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6448. * successful or timeout, after timing-out the outstanding mailbox
  6449. * command shall always be removed, so just unblock posting async
  6450. * mailbox command and resume
  6451. */
  6452. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6453. spin_unlock_irq(&phba->hbalock);
  6454. /* wake up worker thread to post asynchronlous mailbox command */
  6455. lpfc_worker_wake_up(phba);
  6456. }
  6457. /**
  6458. * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
  6459. * @phba: Pointer to HBA context object.
  6460. * @mboxq: Pointer to mailbox object.
  6461. *
  6462. * The function waits for the bootstrap mailbox register ready bit from
  6463. * port for twice the regular mailbox command timeout value.
  6464. *
  6465. * 0 - no timeout on waiting for bootstrap mailbox register ready.
  6466. * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
  6467. **/
  6468. static int
  6469. lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6470. {
  6471. uint32_t db_ready;
  6472. unsigned long timeout;
  6473. struct lpfc_register bmbx_reg;
  6474. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6475. * 1000) + jiffies;
  6476. do {
  6477. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6478. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6479. if (!db_ready)
  6480. msleep(2);
  6481. if (time_after(jiffies, timeout))
  6482. return MBXERR_ERROR;
  6483. } while (!db_ready);
  6484. return 0;
  6485. }
  6486. /**
  6487. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6488. * @phba: Pointer to HBA context object.
  6489. * @mboxq: Pointer to mailbox object.
  6490. *
  6491. * The function posts a mailbox to the port. The mailbox is expected
  6492. * to be comletely filled in and ready for the port to operate on it.
  6493. * This routine executes a synchronous completion operation on the
  6494. * mailbox by polling for its completion.
  6495. *
  6496. * The caller must not be holding any locks when calling this routine.
  6497. *
  6498. * Returns:
  6499. * MBX_SUCCESS - mailbox posted successfully
  6500. * Any of the MBX error values.
  6501. **/
  6502. static int
  6503. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6504. {
  6505. int rc = MBX_SUCCESS;
  6506. unsigned long iflag;
  6507. uint32_t mcqe_status;
  6508. uint32_t mbx_cmnd;
  6509. struct lpfc_sli *psli = &phba->sli;
  6510. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6511. struct lpfc_bmbx_create *mbox_rgn;
  6512. struct dma_address *dma_address;
  6513. /*
  6514. * Only one mailbox can be active to the bootstrap mailbox region
  6515. * at a time and there is no queueing provided.
  6516. */
  6517. spin_lock_irqsave(&phba->hbalock, iflag);
  6518. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6519. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6520. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6521. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6522. "cannot issue Data: x%x x%x\n",
  6523. mboxq->vport ? mboxq->vport->vpi : 0,
  6524. mboxq->u.mb.mbxCommand,
  6525. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6526. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6527. psli->sli_flag, MBX_POLL);
  6528. return MBXERR_ERROR;
  6529. }
  6530. /* The server grabs the token and owns it until release */
  6531. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6532. phba->sli.mbox_active = mboxq;
  6533. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6534. /* wait for bootstrap mbox register for readyness */
  6535. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6536. if (rc)
  6537. goto exit;
  6538. /*
  6539. * Initialize the bootstrap memory region to avoid stale data areas
  6540. * in the mailbox post. Then copy the caller's mailbox contents to
  6541. * the bmbx mailbox region.
  6542. */
  6543. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6544. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6545. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6546. sizeof(struct lpfc_mqe));
  6547. /* Post the high mailbox dma address to the port and wait for ready. */
  6548. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6549. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6550. /* wait for bootstrap mbox register for hi-address write done */
  6551. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6552. if (rc)
  6553. goto exit;
  6554. /* Post the low mailbox dma address to the port. */
  6555. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6556. /* wait for bootstrap mbox register for low address write done */
  6557. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6558. if (rc)
  6559. goto exit;
  6560. /*
  6561. * Read the CQ to ensure the mailbox has completed.
  6562. * If so, update the mailbox status so that the upper layers
  6563. * can complete the request normally.
  6564. */
  6565. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6566. sizeof(struct lpfc_mqe));
  6567. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6568. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6569. sizeof(struct lpfc_mcqe));
  6570. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6571. /*
  6572. * When the CQE status indicates a failure and the mailbox status
  6573. * indicates success then copy the CQE status into the mailbox status
  6574. * (and prefix it with x4000).
  6575. */
  6576. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6577. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6578. bf_set(lpfc_mqe_status, mb,
  6579. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6580. rc = MBXERR_ERROR;
  6581. } else
  6582. lpfc_sli4_swap_str(phba, mboxq);
  6583. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6584. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6585. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6586. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6587. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6588. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6589. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6590. bf_get(lpfc_mqe_status, mb),
  6591. mb->un.mb_words[0], mb->un.mb_words[1],
  6592. mb->un.mb_words[2], mb->un.mb_words[3],
  6593. mb->un.mb_words[4], mb->un.mb_words[5],
  6594. mb->un.mb_words[6], mb->un.mb_words[7],
  6595. mb->un.mb_words[8], mb->un.mb_words[9],
  6596. mb->un.mb_words[10], mb->un.mb_words[11],
  6597. mb->un.mb_words[12], mboxq->mcqe.word0,
  6598. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6599. mboxq->mcqe.trailer);
  6600. exit:
  6601. /* We are holding the token, no needed for lock when release */
  6602. spin_lock_irqsave(&phba->hbalock, iflag);
  6603. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6604. phba->sli.mbox_active = NULL;
  6605. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6606. return rc;
  6607. }
  6608. /**
  6609. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6610. * @phba: Pointer to HBA context object.
  6611. * @pmbox: Pointer to mailbox object.
  6612. * @flag: Flag indicating how the mailbox need to be processed.
  6613. *
  6614. * This function is called by discovery code and HBA management code to submit
  6615. * a mailbox command to firmware with SLI-4 interface spec.
  6616. *
  6617. * Return codes the caller owns the mailbox command after the return of the
  6618. * function.
  6619. **/
  6620. static int
  6621. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6622. uint32_t flag)
  6623. {
  6624. struct lpfc_sli *psli = &phba->sli;
  6625. unsigned long iflags;
  6626. int rc;
  6627. /* dump from issue mailbox command if setup */
  6628. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6629. rc = lpfc_mbox_dev_check(phba);
  6630. if (unlikely(rc)) {
  6631. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6632. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6633. "cannot issue Data: x%x x%x\n",
  6634. mboxq->vport ? mboxq->vport->vpi : 0,
  6635. mboxq->u.mb.mbxCommand,
  6636. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6637. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6638. psli->sli_flag, flag);
  6639. goto out_not_finished;
  6640. }
  6641. /* Detect polling mode and jump to a handler */
  6642. if (!phba->sli4_hba.intr_enable) {
  6643. if (flag == MBX_POLL)
  6644. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6645. else
  6646. rc = -EIO;
  6647. if (rc != MBX_SUCCESS)
  6648. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6649. "(%d):2541 Mailbox command x%x "
  6650. "(x%x/x%x) failure: "
  6651. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6652. "Data: x%x x%x\n,",
  6653. mboxq->vport ? mboxq->vport->vpi : 0,
  6654. mboxq->u.mb.mbxCommand,
  6655. lpfc_sli_config_mbox_subsys_get(phba,
  6656. mboxq),
  6657. lpfc_sli_config_mbox_opcode_get(phba,
  6658. mboxq),
  6659. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6660. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6661. bf_get(lpfc_mcqe_ext_status,
  6662. &mboxq->mcqe),
  6663. psli->sli_flag, flag);
  6664. return rc;
  6665. } else if (flag == MBX_POLL) {
  6666. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6667. "(%d):2542 Try to issue mailbox command "
  6668. "x%x (x%x/x%x) synchronously ahead of async"
  6669. "mailbox command queue: x%x x%x\n",
  6670. mboxq->vport ? mboxq->vport->vpi : 0,
  6671. mboxq->u.mb.mbxCommand,
  6672. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6673. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6674. psli->sli_flag, flag);
  6675. /* Try to block the asynchronous mailbox posting */
  6676. rc = lpfc_sli4_async_mbox_block(phba);
  6677. if (!rc) {
  6678. /* Successfully blocked, now issue sync mbox cmd */
  6679. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6680. if (rc != MBX_SUCCESS)
  6681. lpfc_printf_log(phba, KERN_WARNING,
  6682. LOG_MBOX | LOG_SLI,
  6683. "(%d):2597 Sync Mailbox command "
  6684. "x%x (x%x/x%x) failure: "
  6685. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6686. "Data: x%x x%x\n,",
  6687. mboxq->vport ? mboxq->vport->vpi : 0,
  6688. mboxq->u.mb.mbxCommand,
  6689. lpfc_sli_config_mbox_subsys_get(phba,
  6690. mboxq),
  6691. lpfc_sli_config_mbox_opcode_get(phba,
  6692. mboxq),
  6693. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6694. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6695. bf_get(lpfc_mcqe_ext_status,
  6696. &mboxq->mcqe),
  6697. psli->sli_flag, flag);
  6698. /* Unblock the async mailbox posting afterward */
  6699. lpfc_sli4_async_mbox_unblock(phba);
  6700. }
  6701. return rc;
  6702. }
  6703. /* Now, interrupt mode asynchrous mailbox command */
  6704. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6705. if (rc) {
  6706. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6707. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6708. "cannot issue Data: x%x x%x\n",
  6709. mboxq->vport ? mboxq->vport->vpi : 0,
  6710. mboxq->u.mb.mbxCommand,
  6711. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6712. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6713. psli->sli_flag, flag);
  6714. goto out_not_finished;
  6715. }
  6716. /* Put the mailbox command to the driver internal FIFO */
  6717. psli->slistat.mbox_busy++;
  6718. spin_lock_irqsave(&phba->hbalock, iflags);
  6719. lpfc_mbox_put(phba, mboxq);
  6720. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6721. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6722. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6723. "x%x (x%x/x%x) x%x x%x x%x\n",
  6724. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6725. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6726. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6727. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6728. phba->pport->port_state,
  6729. psli->sli_flag, MBX_NOWAIT);
  6730. /* Wake up worker thread to transport mailbox command from head */
  6731. lpfc_worker_wake_up(phba);
  6732. return MBX_BUSY;
  6733. out_not_finished:
  6734. return MBX_NOT_FINISHED;
  6735. }
  6736. /**
  6737. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6738. * @phba: Pointer to HBA context object.
  6739. *
  6740. * This function is called by worker thread to send a mailbox command to
  6741. * SLI4 HBA firmware.
  6742. *
  6743. **/
  6744. int
  6745. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6746. {
  6747. struct lpfc_sli *psli = &phba->sli;
  6748. LPFC_MBOXQ_t *mboxq;
  6749. int rc = MBX_SUCCESS;
  6750. unsigned long iflags;
  6751. struct lpfc_mqe *mqe;
  6752. uint32_t mbx_cmnd;
  6753. /* Check interrupt mode before post async mailbox command */
  6754. if (unlikely(!phba->sli4_hba.intr_enable))
  6755. return MBX_NOT_FINISHED;
  6756. /* Check for mailbox command service token */
  6757. spin_lock_irqsave(&phba->hbalock, iflags);
  6758. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6759. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6760. return MBX_NOT_FINISHED;
  6761. }
  6762. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6763. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6764. return MBX_NOT_FINISHED;
  6765. }
  6766. if (unlikely(phba->sli.mbox_active)) {
  6767. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6768. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6769. "0384 There is pending active mailbox cmd\n");
  6770. return MBX_NOT_FINISHED;
  6771. }
  6772. /* Take the mailbox command service token */
  6773. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6774. /* Get the next mailbox command from head of queue */
  6775. mboxq = lpfc_mbox_get(phba);
  6776. /* If no more mailbox command waiting for post, we're done */
  6777. if (!mboxq) {
  6778. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6779. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6780. return MBX_SUCCESS;
  6781. }
  6782. phba->sli.mbox_active = mboxq;
  6783. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6784. /* Check device readiness for posting mailbox command */
  6785. rc = lpfc_mbox_dev_check(phba);
  6786. if (unlikely(rc))
  6787. /* Driver clean routine will clean up pending mailbox */
  6788. goto out_not_finished;
  6789. /* Prepare the mbox command to be posted */
  6790. mqe = &mboxq->u.mqe;
  6791. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6792. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6793. mod_timer(&psli->mbox_tmo, (jiffies +
  6794. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6795. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6796. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6797. "x%x x%x\n",
  6798. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6799. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6800. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6801. phba->pport->port_state, psli->sli_flag);
  6802. if (mbx_cmnd != MBX_HEARTBEAT) {
  6803. if (mboxq->vport) {
  6804. lpfc_debugfs_disc_trc(mboxq->vport,
  6805. LPFC_DISC_TRC_MBOX_VPORT,
  6806. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6807. mbx_cmnd, mqe->un.mb_words[0],
  6808. mqe->un.mb_words[1]);
  6809. } else {
  6810. lpfc_debugfs_disc_trc(phba->pport,
  6811. LPFC_DISC_TRC_MBOX,
  6812. "MBOX Send: cmd:x%x mb:x%x x%x",
  6813. mbx_cmnd, mqe->un.mb_words[0],
  6814. mqe->un.mb_words[1]);
  6815. }
  6816. }
  6817. psli->slistat.mbox_cmd++;
  6818. /* Post the mailbox command to the port */
  6819. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6820. if (rc != MBX_SUCCESS) {
  6821. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6822. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6823. "cannot issue Data: x%x x%x\n",
  6824. mboxq->vport ? mboxq->vport->vpi : 0,
  6825. mboxq->u.mb.mbxCommand,
  6826. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6827. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6828. psli->sli_flag, MBX_NOWAIT);
  6829. goto out_not_finished;
  6830. }
  6831. return rc;
  6832. out_not_finished:
  6833. spin_lock_irqsave(&phba->hbalock, iflags);
  6834. if (phba->sli.mbox_active) {
  6835. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6836. __lpfc_mbox_cmpl_put(phba, mboxq);
  6837. /* Release the token */
  6838. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6839. phba->sli.mbox_active = NULL;
  6840. }
  6841. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6842. return MBX_NOT_FINISHED;
  6843. }
  6844. /**
  6845. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6846. * @phba: Pointer to HBA context object.
  6847. * @pmbox: Pointer to mailbox object.
  6848. * @flag: Flag indicating how the mailbox need to be processed.
  6849. *
  6850. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6851. * the API jump table function pointer from the lpfc_hba struct.
  6852. *
  6853. * Return codes the caller owns the mailbox command after the return of the
  6854. * function.
  6855. **/
  6856. int
  6857. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6858. {
  6859. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6860. }
  6861. /**
  6862. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6863. * @phba: The hba struct for which this call is being executed.
  6864. * @dev_grp: The HBA PCI-Device group number.
  6865. *
  6866. * This routine sets up the mbox interface API function jump table in @phba
  6867. * struct.
  6868. * Returns: 0 - success, -ENODEV - failure.
  6869. **/
  6870. int
  6871. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6872. {
  6873. switch (dev_grp) {
  6874. case LPFC_PCI_DEV_LP:
  6875. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6876. phba->lpfc_sli_handle_slow_ring_event =
  6877. lpfc_sli_handle_slow_ring_event_s3;
  6878. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6879. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6880. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6881. break;
  6882. case LPFC_PCI_DEV_OC:
  6883. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6884. phba->lpfc_sli_handle_slow_ring_event =
  6885. lpfc_sli_handle_slow_ring_event_s4;
  6886. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6887. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6888. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6889. break;
  6890. default:
  6891. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6892. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6893. dev_grp);
  6894. return -ENODEV;
  6895. break;
  6896. }
  6897. return 0;
  6898. }
  6899. /**
  6900. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6901. * @phba: Pointer to HBA context object.
  6902. * @pring: Pointer to driver SLI ring object.
  6903. * @piocb: Pointer to address of newly added command iocb.
  6904. *
  6905. * This function is called with hbalock held to add a command
  6906. * iocb to the txq when SLI layer cannot submit the command iocb
  6907. * to the ring.
  6908. **/
  6909. void
  6910. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6911. struct lpfc_iocbq *piocb)
  6912. {
  6913. /* Insert the caller's iocb in the txq tail for later processing. */
  6914. list_add_tail(&piocb->list, &pring->txq);
  6915. pring->txq_cnt++;
  6916. }
  6917. /**
  6918. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6919. * @phba: Pointer to HBA context object.
  6920. * @pring: Pointer to driver SLI ring object.
  6921. * @piocb: Pointer to address of newly added command iocb.
  6922. *
  6923. * This function is called with hbalock held before a new
  6924. * iocb is submitted to the firmware. This function checks
  6925. * txq to flush the iocbs in txq to Firmware before
  6926. * submitting new iocbs to the Firmware.
  6927. * If there are iocbs in the txq which need to be submitted
  6928. * to firmware, lpfc_sli_next_iocb returns the first element
  6929. * of the txq after dequeuing it from txq.
  6930. * If there is no iocb in the txq then the function will return
  6931. * *piocb and *piocb is set to NULL. Caller needs to check
  6932. * *piocb to find if there are more commands in the txq.
  6933. **/
  6934. static struct lpfc_iocbq *
  6935. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6936. struct lpfc_iocbq **piocb)
  6937. {
  6938. struct lpfc_iocbq * nextiocb;
  6939. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6940. if (!nextiocb) {
  6941. nextiocb = *piocb;
  6942. *piocb = NULL;
  6943. }
  6944. return nextiocb;
  6945. }
  6946. /**
  6947. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6948. * @phba: Pointer to HBA context object.
  6949. * @ring_number: SLI ring number to issue iocb on.
  6950. * @piocb: Pointer to command iocb.
  6951. * @flag: Flag indicating if this command can be put into txq.
  6952. *
  6953. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6954. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6955. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6956. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6957. * this function allows only iocbs for posting buffers. This function finds
  6958. * next available slot in the command ring and posts the command to the
  6959. * available slot and writes the port attention register to request HBA start
  6960. * processing new iocb. If there is no slot available in the ring and
  6961. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6962. * the function returns IOCB_BUSY.
  6963. *
  6964. * This function is called with hbalock held. The function will return success
  6965. * after it successfully submit the iocb to firmware or after adding to the
  6966. * txq.
  6967. **/
  6968. static int
  6969. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6970. struct lpfc_iocbq *piocb, uint32_t flag)
  6971. {
  6972. struct lpfc_iocbq *nextiocb;
  6973. IOCB_t *iocb;
  6974. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6975. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6976. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6977. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6978. lpfc_printf_log(phba, KERN_ERR,
  6979. LOG_SLI | LOG_VPORT,
  6980. "1807 IOCB x%x failed. No vport\n",
  6981. piocb->iocb.ulpCommand);
  6982. dump_stack();
  6983. return IOCB_ERROR;
  6984. }
  6985. /* If the PCI channel is in offline state, do not post iocbs. */
  6986. if (unlikely(pci_channel_offline(phba->pcidev)))
  6987. return IOCB_ERROR;
  6988. /* If HBA has a deferred error attention, fail the iocb. */
  6989. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6990. return IOCB_ERROR;
  6991. /*
  6992. * We should never get an IOCB if we are in a < LINK_DOWN state
  6993. */
  6994. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6995. return IOCB_ERROR;
  6996. /*
  6997. * Check to see if we are blocking IOCB processing because of a
  6998. * outstanding event.
  6999. */
  7000. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  7001. goto iocb_busy;
  7002. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  7003. /*
  7004. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  7005. * can be issued if the link is not up.
  7006. */
  7007. switch (piocb->iocb.ulpCommand) {
  7008. case CMD_GEN_REQUEST64_CR:
  7009. case CMD_GEN_REQUEST64_CX:
  7010. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  7011. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  7012. FC_RCTL_DD_UNSOL_CMD) ||
  7013. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  7014. MENLO_TRANSPORT_TYPE))
  7015. goto iocb_busy;
  7016. break;
  7017. case CMD_QUE_RING_BUF_CN:
  7018. case CMD_QUE_RING_BUF64_CN:
  7019. /*
  7020. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  7021. * completion, iocb_cmpl MUST be 0.
  7022. */
  7023. if (piocb->iocb_cmpl)
  7024. piocb->iocb_cmpl = NULL;
  7025. /*FALLTHROUGH*/
  7026. case CMD_CREATE_XRI_CR:
  7027. case CMD_CLOSE_XRI_CN:
  7028. case CMD_CLOSE_XRI_CX:
  7029. break;
  7030. default:
  7031. goto iocb_busy;
  7032. }
  7033. /*
  7034. * For FCP commands, we must be in a state where we can process link
  7035. * attention events.
  7036. */
  7037. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  7038. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  7039. goto iocb_busy;
  7040. }
  7041. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  7042. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  7043. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  7044. if (iocb)
  7045. lpfc_sli_update_ring(phba, pring);
  7046. else
  7047. lpfc_sli_update_full_ring(phba, pring);
  7048. if (!piocb)
  7049. return IOCB_SUCCESS;
  7050. goto out_busy;
  7051. iocb_busy:
  7052. pring->stats.iocb_cmd_delay++;
  7053. out_busy:
  7054. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7055. __lpfc_sli_ringtx_put(phba, pring, piocb);
  7056. return IOCB_SUCCESS;
  7057. }
  7058. return IOCB_BUSY;
  7059. }
  7060. /**
  7061. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  7062. * @phba: Pointer to HBA context object.
  7063. * @piocb: Pointer to command iocb.
  7064. * @sglq: Pointer to the scatter gather queue object.
  7065. *
  7066. * This routine converts the bpl or bde that is in the IOCB
  7067. * to a sgl list for the sli4 hardware. The physical address
  7068. * of the bpl/bde is converted back to a virtual address.
  7069. * If the IOCB contains a BPL then the list of BDE's is
  7070. * converted to sli4_sge's. If the IOCB contains a single
  7071. * BDE then it is converted to a single sli_sge.
  7072. * The IOCB is still in cpu endianess so the contents of
  7073. * the bpl can be used without byte swapping.
  7074. *
  7075. * Returns valid XRI = Success, NO_XRI = Failure.
  7076. **/
  7077. static uint16_t
  7078. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  7079. struct lpfc_sglq *sglq)
  7080. {
  7081. uint16_t xritag = NO_XRI;
  7082. struct ulp_bde64 *bpl = NULL;
  7083. struct ulp_bde64 bde;
  7084. struct sli4_sge *sgl = NULL;
  7085. struct lpfc_dmabuf *dmabuf;
  7086. IOCB_t *icmd;
  7087. int numBdes = 0;
  7088. int i = 0;
  7089. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7090. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7091. if (!piocbq || !sglq)
  7092. return xritag;
  7093. sgl = (struct sli4_sge *)sglq->sgl;
  7094. icmd = &piocbq->iocb;
  7095. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7096. return sglq->sli4_xritag;
  7097. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7098. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7099. sizeof(struct ulp_bde64);
  7100. /* The addrHigh and addrLow fields within the IOCB
  7101. * have not been byteswapped yet so there is no
  7102. * need to swap them back.
  7103. */
  7104. if (piocbq->context3)
  7105. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7106. else
  7107. return xritag;
  7108. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7109. if (!bpl)
  7110. return xritag;
  7111. for (i = 0; i < numBdes; i++) {
  7112. /* Should already be byte swapped. */
  7113. sgl->addr_hi = bpl->addrHigh;
  7114. sgl->addr_lo = bpl->addrLow;
  7115. sgl->word2 = le32_to_cpu(sgl->word2);
  7116. if ((i+1) == numBdes)
  7117. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7118. else
  7119. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7120. /* swap the size field back to the cpu so we
  7121. * can assign it to the sgl.
  7122. */
  7123. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7124. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7125. /* The offsets in the sgl need to be accumulated
  7126. * separately for the request and reply lists.
  7127. * The request is always first, the reply follows.
  7128. */
  7129. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7130. /* add up the reply sg entries */
  7131. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7132. inbound++;
  7133. /* first inbound? reset the offset */
  7134. if (inbound == 1)
  7135. offset = 0;
  7136. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7137. bf_set(lpfc_sli4_sge_type, sgl,
  7138. LPFC_SGE_TYPE_DATA);
  7139. offset += bde.tus.f.bdeSize;
  7140. }
  7141. sgl->word2 = cpu_to_le32(sgl->word2);
  7142. bpl++;
  7143. sgl++;
  7144. }
  7145. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7146. /* The addrHigh and addrLow fields of the BDE have not
  7147. * been byteswapped yet so they need to be swapped
  7148. * before putting them in the sgl.
  7149. */
  7150. sgl->addr_hi =
  7151. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7152. sgl->addr_lo =
  7153. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7154. sgl->word2 = le32_to_cpu(sgl->word2);
  7155. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7156. sgl->word2 = cpu_to_le32(sgl->word2);
  7157. sgl->sge_len =
  7158. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7159. }
  7160. return sglq->sli4_xritag;
  7161. }
  7162. /**
  7163. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7164. * @phba: Pointer to HBA context object.
  7165. *
  7166. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7167. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7168. * held.
  7169. *
  7170. * Return: index into SLI4 fast-path FCP queue index.
  7171. **/
  7172. static inline uint32_t
  7173. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7174. {
  7175. int i;
  7176. if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
  7177. i = smp_processor_id();
  7178. else
  7179. i = atomic_add_return(1, &phba->fcp_qidx);
  7180. i = (i % phba->cfg_fcp_io_channel);
  7181. return i;
  7182. }
  7183. /**
  7184. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7185. * @phba: Pointer to HBA context object.
  7186. * @piocb: Pointer to command iocb.
  7187. * @wqe: Pointer to the work queue entry.
  7188. *
  7189. * This routine converts the iocb command to its Work Queue Entry
  7190. * equivalent. The wqe pointer should not have any fields set when
  7191. * this routine is called because it will memcpy over them.
  7192. * This routine does not set the CQ_ID or the WQEC bits in the
  7193. * wqe.
  7194. *
  7195. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7196. **/
  7197. static int
  7198. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7199. union lpfc_wqe *wqe)
  7200. {
  7201. uint32_t xmit_len = 0, total_len = 0;
  7202. uint8_t ct = 0;
  7203. uint32_t fip;
  7204. uint32_t abort_tag;
  7205. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7206. uint8_t cmnd;
  7207. uint16_t xritag;
  7208. uint16_t abrt_iotag;
  7209. struct lpfc_iocbq *abrtiocbq;
  7210. struct ulp_bde64 *bpl = NULL;
  7211. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7212. int numBdes, i;
  7213. struct ulp_bde64 bde;
  7214. struct lpfc_nodelist *ndlp;
  7215. uint32_t *pcmd;
  7216. uint32_t if_type;
  7217. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7218. /* The fcp commands will set command type */
  7219. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7220. command_type = FCP_COMMAND;
  7221. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7222. command_type = ELS_COMMAND_FIP;
  7223. else
  7224. command_type = ELS_COMMAND_NON_FIP;
  7225. /* Some of the fields are in the right position already */
  7226. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7227. abort_tag = (uint32_t) iocbq->iotag;
  7228. xritag = iocbq->sli4_xritag;
  7229. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7230. /* words0-2 bpl convert bde */
  7231. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7232. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7233. sizeof(struct ulp_bde64);
  7234. bpl = (struct ulp_bde64 *)
  7235. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7236. if (!bpl)
  7237. return IOCB_ERROR;
  7238. /* Should already be byte swapped. */
  7239. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7240. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7241. /* swap the size field back to the cpu so we
  7242. * can assign it to the sgl.
  7243. */
  7244. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7245. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7246. total_len = 0;
  7247. for (i = 0; i < numBdes; i++) {
  7248. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7249. total_len += bde.tus.f.bdeSize;
  7250. }
  7251. } else
  7252. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7253. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7254. cmnd = iocbq->iocb.ulpCommand;
  7255. switch (iocbq->iocb.ulpCommand) {
  7256. case CMD_ELS_REQUEST64_CR:
  7257. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7258. ndlp = iocbq->context_un.ndlp;
  7259. else
  7260. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7261. if (!iocbq->iocb.ulpLe) {
  7262. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7263. "2007 Only Limited Edition cmd Format"
  7264. " supported 0x%x\n",
  7265. iocbq->iocb.ulpCommand);
  7266. return IOCB_ERROR;
  7267. }
  7268. wqe->els_req.payload_len = xmit_len;
  7269. /* Els_reguest64 has a TMO */
  7270. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7271. iocbq->iocb.ulpTimeout);
  7272. /* Need a VF for word 4 set the vf bit*/
  7273. bf_set(els_req64_vf, &wqe->els_req, 0);
  7274. /* And a VFID for word 12 */
  7275. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7276. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7277. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7278. iocbq->iocb.ulpContext);
  7279. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7280. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7281. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7282. if (command_type == ELS_COMMAND_FIP)
  7283. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7284. >> LPFC_FIP_ELS_ID_SHIFT);
  7285. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7286. iocbq->context2)->virt);
  7287. if_type = bf_get(lpfc_sli_intf_if_type,
  7288. &phba->sli4_hba.sli_intf);
  7289. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7290. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7291. *pcmd == ELS_CMD_SCR ||
  7292. *pcmd == ELS_CMD_FDISC ||
  7293. *pcmd == ELS_CMD_LOGO ||
  7294. *pcmd == ELS_CMD_PLOGI)) {
  7295. bf_set(els_req64_sp, &wqe->els_req, 1);
  7296. bf_set(els_req64_sid, &wqe->els_req,
  7297. iocbq->vport->fc_myDID);
  7298. if ((*pcmd == ELS_CMD_FLOGI) &&
  7299. !(phba->fc_topology ==
  7300. LPFC_TOPOLOGY_LOOP))
  7301. bf_set(els_req64_sid, &wqe->els_req, 0);
  7302. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7303. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7304. phba->vpi_ids[iocbq->vport->vpi]);
  7305. } else if (pcmd && iocbq->context1) {
  7306. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7307. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7308. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7309. }
  7310. }
  7311. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7312. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7313. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7314. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7315. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7316. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7317. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7318. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7319. break;
  7320. case CMD_XMIT_SEQUENCE64_CX:
  7321. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7322. iocbq->iocb.un.ulpWord[3]);
  7323. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7324. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7325. /* The entire sequence is transmitted for this IOCB */
  7326. xmit_len = total_len;
  7327. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7328. if (phba->link_flag & LS_LOOPBACK_MODE)
  7329. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7330. case CMD_XMIT_SEQUENCE64_CR:
  7331. /* word3 iocb=io_tag32 wqe=reserved */
  7332. wqe->xmit_sequence.rsvd3 = 0;
  7333. /* word4 relative_offset memcpy */
  7334. /* word5 r_ctl/df_ctl memcpy */
  7335. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7336. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7337. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7338. LPFC_WQE_IOD_WRITE);
  7339. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7340. LPFC_WQE_LENLOC_WORD12);
  7341. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7342. wqe->xmit_sequence.xmit_len = xmit_len;
  7343. command_type = OTHER_COMMAND;
  7344. break;
  7345. case CMD_XMIT_BCAST64_CN:
  7346. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7347. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7348. /* word4 iocb=rsvd wqe=rsvd */
  7349. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7350. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7351. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7352. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7353. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7354. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7355. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7356. LPFC_WQE_LENLOC_WORD3);
  7357. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7358. break;
  7359. case CMD_FCP_IWRITE64_CR:
  7360. command_type = FCP_COMMAND_DATA_OUT;
  7361. /* word3 iocb=iotag wqe=payload_offset_len */
  7362. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7363. wqe->fcp_iwrite.payload_offset_len =
  7364. xmit_len + sizeof(struct fcp_rsp);
  7365. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7366. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7367. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7368. iocbq->iocb.ulpFCP2Rcvy);
  7369. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7370. /* Always open the exchange */
  7371. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7372. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7373. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7374. LPFC_WQE_LENLOC_WORD4);
  7375. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7376. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7377. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7378. break;
  7379. case CMD_FCP_IREAD64_CR:
  7380. /* word3 iocb=iotag wqe=payload_offset_len */
  7381. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7382. wqe->fcp_iread.payload_offset_len =
  7383. xmit_len + sizeof(struct fcp_rsp);
  7384. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7385. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7386. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7387. iocbq->iocb.ulpFCP2Rcvy);
  7388. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7389. /* Always open the exchange */
  7390. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7391. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7392. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7393. LPFC_WQE_LENLOC_WORD4);
  7394. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7395. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7396. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7397. break;
  7398. case CMD_FCP_ICMND64_CR:
  7399. /* word3 iocb=IO_TAG wqe=reserved */
  7400. wqe->fcp_icmd.rsrvd3 = 0;
  7401. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7402. /* Always open the exchange */
  7403. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7404. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7405. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7406. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7407. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7408. LPFC_WQE_LENLOC_NONE);
  7409. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7410. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  7411. iocbq->iocb.ulpFCP2Rcvy);
  7412. break;
  7413. case CMD_GEN_REQUEST64_CR:
  7414. /* For this command calculate the xmit length of the
  7415. * request bde.
  7416. */
  7417. xmit_len = 0;
  7418. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7419. sizeof(struct ulp_bde64);
  7420. for (i = 0; i < numBdes; i++) {
  7421. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7422. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7423. break;
  7424. xmit_len += bde.tus.f.bdeSize;
  7425. }
  7426. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7427. wqe->gen_req.request_payload_len = xmit_len;
  7428. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7429. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7430. /* word6 context tag copied in memcpy */
  7431. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7432. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7433. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7434. "2015 Invalid CT %x command 0x%x\n",
  7435. ct, iocbq->iocb.ulpCommand);
  7436. return IOCB_ERROR;
  7437. }
  7438. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7439. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7440. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7441. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7442. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7443. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7444. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7445. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7446. command_type = OTHER_COMMAND;
  7447. break;
  7448. case CMD_XMIT_ELS_RSP64_CX:
  7449. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7450. /* words0-2 BDE memcpy */
  7451. /* word3 iocb=iotag32 wqe=response_payload_len */
  7452. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7453. /* word4 */
  7454. wqe->xmit_els_rsp.word4 = 0;
  7455. /* word5 iocb=rsvd wge=did */
  7456. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7457. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7458. if_type = bf_get(lpfc_sli_intf_if_type,
  7459. &phba->sli4_hba.sli_intf);
  7460. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7461. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7462. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7463. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7464. iocbq->vport->fc_myDID);
  7465. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7466. bf_set(wqe_els_did,
  7467. &wqe->xmit_els_rsp.wqe_dest, 0);
  7468. }
  7469. }
  7470. }
  7471. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7472. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7473. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7474. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7475. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7476. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7477. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7478. phba->vpi_ids[iocbq->vport->vpi]);
  7479. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7480. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7481. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7482. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7483. LPFC_WQE_LENLOC_WORD3);
  7484. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7485. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7486. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7487. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7488. iocbq->context2)->virt);
  7489. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7490. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7491. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7492. iocbq->vport->fc_myDID);
  7493. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7494. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7495. phba->vpi_ids[phba->pport->vpi]);
  7496. }
  7497. command_type = OTHER_COMMAND;
  7498. break;
  7499. case CMD_CLOSE_XRI_CN:
  7500. case CMD_ABORT_XRI_CN:
  7501. case CMD_ABORT_XRI_CX:
  7502. /* words 0-2 memcpy should be 0 rserved */
  7503. /* port will send abts */
  7504. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7505. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7506. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7507. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7508. } else
  7509. fip = 0;
  7510. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7511. /*
  7512. * The link is down, or the command was ELS_FIP
  7513. * so the fw does not need to send abts
  7514. * on the wire.
  7515. */
  7516. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7517. else
  7518. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7519. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7520. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7521. wqe->abort_cmd.rsrvd5 = 0;
  7522. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7523. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7524. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7525. /*
  7526. * The abort handler will send us CMD_ABORT_XRI_CN or
  7527. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7528. */
  7529. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7530. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7531. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7532. LPFC_WQE_LENLOC_NONE);
  7533. cmnd = CMD_ABORT_XRI_CX;
  7534. command_type = OTHER_COMMAND;
  7535. xritag = 0;
  7536. break;
  7537. case CMD_XMIT_BLS_RSP64_CX:
  7538. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7539. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7540. * we re-construct this WQE here based on information in
  7541. * iocbq from scratch.
  7542. */
  7543. memset(wqe, 0, sizeof(union lpfc_wqe));
  7544. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7545. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7546. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7547. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7548. LPFC_ABTS_UNSOL_INT) {
  7549. /* ABTS sent by initiator to CT exchange, the
  7550. * RX_ID field will be filled with the newly
  7551. * allocated responder XRI.
  7552. */
  7553. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7554. iocbq->sli4_xritag);
  7555. } else {
  7556. /* ABTS sent by responder to CT exchange, the
  7557. * RX_ID field will be filled with the responder
  7558. * RX_ID from ABTS.
  7559. */
  7560. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7561. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7562. }
  7563. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7564. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7565. /* Use CT=VPI */
  7566. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7567. ndlp->nlp_DID);
  7568. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7569. iocbq->iocb.ulpContext);
  7570. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7571. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7572. phba->vpi_ids[phba->pport->vpi]);
  7573. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7574. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7575. LPFC_WQE_LENLOC_NONE);
  7576. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7577. command_type = OTHER_COMMAND;
  7578. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7579. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7580. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7581. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7582. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7583. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7584. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7585. }
  7586. break;
  7587. case CMD_XRI_ABORTED_CX:
  7588. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7589. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7590. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7591. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7592. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7593. default:
  7594. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7595. "2014 Invalid command 0x%x\n",
  7596. iocbq->iocb.ulpCommand);
  7597. return IOCB_ERROR;
  7598. break;
  7599. }
  7600. if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
  7601. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  7602. else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
  7603. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  7604. else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
  7605. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  7606. iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
  7607. LPFC_IO_DIF_INSERT);
  7608. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7609. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7610. wqe->generic.wqe_com.abort_tag = abort_tag;
  7611. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7612. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7613. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7614. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7615. return 0;
  7616. }
  7617. /**
  7618. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7619. * @phba: Pointer to HBA context object.
  7620. * @ring_number: SLI ring number to issue iocb on.
  7621. * @piocb: Pointer to command iocb.
  7622. * @flag: Flag indicating if this command can be put into txq.
  7623. *
  7624. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7625. * an iocb command to an HBA with SLI-4 interface spec.
  7626. *
  7627. * This function is called with hbalock held. The function will return success
  7628. * after it successfully submit the iocb to firmware or after adding to the
  7629. * txq.
  7630. **/
  7631. static int
  7632. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7633. struct lpfc_iocbq *piocb, uint32_t flag)
  7634. {
  7635. struct lpfc_sglq *sglq;
  7636. union lpfc_wqe wqe;
  7637. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7638. if (piocb->sli4_xritag == NO_XRI) {
  7639. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7640. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7641. sglq = NULL;
  7642. else {
  7643. if (pring->txq_cnt) {
  7644. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7645. __lpfc_sli_ringtx_put(phba,
  7646. pring, piocb);
  7647. return IOCB_SUCCESS;
  7648. } else {
  7649. return IOCB_BUSY;
  7650. }
  7651. } else {
  7652. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7653. if (!sglq) {
  7654. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7655. __lpfc_sli_ringtx_put(phba,
  7656. pring,
  7657. piocb);
  7658. return IOCB_SUCCESS;
  7659. } else
  7660. return IOCB_BUSY;
  7661. }
  7662. }
  7663. }
  7664. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7665. /* These IO's already have an XRI and a mapped sgl. */
  7666. sglq = NULL;
  7667. } else {
  7668. /*
  7669. * This is a continuation of a commandi,(CX) so this
  7670. * sglq is on the active list
  7671. */
  7672. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
  7673. if (!sglq)
  7674. return IOCB_ERROR;
  7675. }
  7676. if (sglq) {
  7677. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7678. piocb->sli4_xritag = sglq->sli4_xritag;
  7679. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7680. return IOCB_ERROR;
  7681. }
  7682. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7683. return IOCB_ERROR;
  7684. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7685. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7686. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7687. &wqe))
  7688. return IOCB_ERROR;
  7689. } else {
  7690. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7691. return IOCB_ERROR;
  7692. }
  7693. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7694. return 0;
  7695. }
  7696. /**
  7697. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7698. *
  7699. * This routine wraps the actual lockless version for issusing IOCB function
  7700. * pointer from the lpfc_hba struct.
  7701. *
  7702. * Return codes:
  7703. * IOCB_ERROR - Error
  7704. * IOCB_SUCCESS - Success
  7705. * IOCB_BUSY - Busy
  7706. **/
  7707. int
  7708. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7709. struct lpfc_iocbq *piocb, uint32_t flag)
  7710. {
  7711. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7712. }
  7713. /**
  7714. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7715. * @phba: The hba struct for which this call is being executed.
  7716. * @dev_grp: The HBA PCI-Device group number.
  7717. *
  7718. * This routine sets up the SLI interface API function jump table in @phba
  7719. * struct.
  7720. * Returns: 0 - success, -ENODEV - failure.
  7721. **/
  7722. int
  7723. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7724. {
  7725. switch (dev_grp) {
  7726. case LPFC_PCI_DEV_LP:
  7727. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7728. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7729. break;
  7730. case LPFC_PCI_DEV_OC:
  7731. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7732. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7733. break;
  7734. default:
  7735. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7736. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7737. dev_grp);
  7738. return -ENODEV;
  7739. break;
  7740. }
  7741. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7742. return 0;
  7743. }
  7744. /**
  7745. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7746. * @phba: Pointer to HBA context object.
  7747. * @pring: Pointer to driver SLI ring object.
  7748. * @piocb: Pointer to command iocb.
  7749. * @flag: Flag indicating if this command can be put into txq.
  7750. *
  7751. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7752. * function. This function gets the hbalock and calls
  7753. * __lpfc_sli_issue_iocb function and will return the error returned
  7754. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7755. * functions which do not hold hbalock.
  7756. **/
  7757. int
  7758. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7759. struct lpfc_iocbq *piocb, uint32_t flag)
  7760. {
  7761. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  7762. struct lpfc_sli_ring *pring;
  7763. struct lpfc_queue *fpeq;
  7764. struct lpfc_eqe *eqe;
  7765. unsigned long iflags;
  7766. int rc, idx;
  7767. if (phba->sli_rev == LPFC_SLI_REV4) {
  7768. if (piocb->iocb_flag & LPFC_IO_FCP) {
  7769. if (unlikely(!phba->sli4_hba.fcp_wq))
  7770. return IOCB_ERROR;
  7771. idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7772. piocb->fcp_wqidx = idx;
  7773. ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
  7774. pring = &phba->sli.ring[ring_number];
  7775. spin_lock_irqsave(&pring->ring_lock, iflags);
  7776. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
  7777. flag);
  7778. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7779. if (lpfc_fcp_look_ahead) {
  7780. fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
  7781. if (atomic_dec_and_test(&fcp_eq_hdl->
  7782. fcp_eq_in_use)) {
  7783. /* Get associated EQ with this index */
  7784. fpeq = phba->sli4_hba.hba_eq[idx];
  7785. /* Turn off interrupts from this EQ */
  7786. lpfc_sli4_eq_clr_intr(fpeq);
  7787. /*
  7788. * Process all the events on FCP EQ
  7789. */
  7790. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  7791. lpfc_sli4_hba_handle_eqe(phba,
  7792. eqe, idx);
  7793. fpeq->EQ_processed++;
  7794. }
  7795. /* Always clear and re-arm the EQ */
  7796. lpfc_sli4_eq_release(fpeq,
  7797. LPFC_QUEUE_REARM);
  7798. }
  7799. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  7800. }
  7801. } else {
  7802. pring = &phba->sli.ring[ring_number];
  7803. spin_lock_irqsave(&pring->ring_lock, iflags);
  7804. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
  7805. flag);
  7806. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7807. }
  7808. } else {
  7809. /* For now, SLI2/3 will still use hbalock */
  7810. spin_lock_irqsave(&phba->hbalock, iflags);
  7811. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7812. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7813. }
  7814. return rc;
  7815. }
  7816. /**
  7817. * lpfc_extra_ring_setup - Extra ring setup function
  7818. * @phba: Pointer to HBA context object.
  7819. *
  7820. * This function is called while driver attaches with the
  7821. * HBA to setup the extra ring. The extra ring is used
  7822. * only when driver needs to support target mode functionality
  7823. * or IP over FC functionalities.
  7824. *
  7825. * This function is called with no lock held.
  7826. **/
  7827. static int
  7828. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7829. {
  7830. struct lpfc_sli *psli;
  7831. struct lpfc_sli_ring *pring;
  7832. psli = &phba->sli;
  7833. /* Adjust cmd/rsp ring iocb entries more evenly */
  7834. /* Take some away from the FCP ring */
  7835. pring = &psli->ring[psli->fcp_ring];
  7836. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7837. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7838. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7839. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7840. /* and give them to the extra ring */
  7841. pring = &psli->ring[psli->extra_ring];
  7842. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7843. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7844. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7845. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7846. /* Setup default profile for this ring */
  7847. pring->iotag_max = 4096;
  7848. pring->num_mask = 1;
  7849. pring->prt[0].profile = 0; /* Mask 0 */
  7850. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7851. pring->prt[0].type = phba->cfg_multi_ring_type;
  7852. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7853. return 0;
  7854. }
  7855. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7856. * @phba: Pointer to HBA context object.
  7857. * @iocbq: Pointer to iocb object.
  7858. *
  7859. * The async_event handler calls this routine when it receives
  7860. * an ASYNC_STATUS_CN event from the port. The port generates
  7861. * this event when an Abort Sequence request to an rport fails
  7862. * twice in succession. The abort could be originated by the
  7863. * driver or by the port. The ABTS could have been for an ELS
  7864. * or FCP IO. The port only generates this event when an ABTS
  7865. * fails to complete after one retry.
  7866. */
  7867. static void
  7868. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7869. struct lpfc_iocbq *iocbq)
  7870. {
  7871. struct lpfc_nodelist *ndlp = NULL;
  7872. uint16_t rpi = 0, vpi = 0;
  7873. struct lpfc_vport *vport = NULL;
  7874. /* The rpi in the ulpContext is vport-sensitive. */
  7875. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7876. rpi = iocbq->iocb.ulpContext;
  7877. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7878. "3092 Port generated ABTS async event "
  7879. "on vpi %d rpi %d status 0x%x\n",
  7880. vpi, rpi, iocbq->iocb.ulpStatus);
  7881. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7882. if (!vport)
  7883. goto err_exit;
  7884. ndlp = lpfc_findnode_rpi(vport, rpi);
  7885. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7886. goto err_exit;
  7887. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7888. lpfc_sli_abts_recover_port(vport, ndlp);
  7889. return;
  7890. err_exit:
  7891. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7892. "3095 Event Context not found, no "
  7893. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7894. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7895. vpi, rpi);
  7896. }
  7897. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7898. * @phba: pointer to HBA context object.
  7899. * @ndlp: nodelist pointer for the impacted rport.
  7900. * @axri: pointer to the wcqe containing the failed exchange.
  7901. *
  7902. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7903. * port. The port generates this event when an abort exchange request to an
  7904. * rport fails twice in succession with no reply. The abort could be originated
  7905. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7906. */
  7907. void
  7908. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7909. struct lpfc_nodelist *ndlp,
  7910. struct sli4_wcqe_xri_aborted *axri)
  7911. {
  7912. struct lpfc_vport *vport;
  7913. uint32_t ext_status = 0;
  7914. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7915. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7916. "3115 Node Context not found, driver "
  7917. "ignoring abts err event\n");
  7918. return;
  7919. }
  7920. vport = ndlp->vport;
  7921. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7922. "3116 Port generated FCP XRI ABORT event on "
  7923. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7924. ndlp->vport->vpi, ndlp->nlp_rpi,
  7925. bf_get(lpfc_wcqe_xa_xri, axri),
  7926. bf_get(lpfc_wcqe_xa_status, axri),
  7927. axri->parameter);
  7928. /*
  7929. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7930. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7931. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7932. */
  7933. ext_status = axri->parameter & IOERR_PARAM_MASK;
  7934. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7935. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7936. lpfc_sli_abts_recover_port(vport, ndlp);
  7937. }
  7938. /**
  7939. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7940. * @phba: Pointer to HBA context object.
  7941. * @pring: Pointer to driver SLI ring object.
  7942. * @iocbq: Pointer to iocb object.
  7943. *
  7944. * This function is called by the slow ring event handler
  7945. * function when there is an ASYNC event iocb in the ring.
  7946. * This function is called with no lock held.
  7947. * Currently this function handles only temperature related
  7948. * ASYNC events. The function decodes the temperature sensor
  7949. * event message and posts events for the management applications.
  7950. **/
  7951. static void
  7952. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7953. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7954. {
  7955. IOCB_t *icmd;
  7956. uint16_t evt_code;
  7957. struct temp_event temp_event_data;
  7958. struct Scsi_Host *shost;
  7959. uint32_t *iocb_w;
  7960. icmd = &iocbq->iocb;
  7961. evt_code = icmd->un.asyncstat.evt_code;
  7962. switch (evt_code) {
  7963. case ASYNC_TEMP_WARN:
  7964. case ASYNC_TEMP_SAFE:
  7965. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7966. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7967. if (evt_code == ASYNC_TEMP_WARN) {
  7968. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7969. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7970. "0347 Adapter is very hot, please take "
  7971. "corrective action. temperature : %d Celsius\n",
  7972. (uint32_t) icmd->ulpContext);
  7973. } else {
  7974. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7975. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7976. "0340 Adapter temperature is OK now. "
  7977. "temperature : %d Celsius\n",
  7978. (uint32_t) icmd->ulpContext);
  7979. }
  7980. /* Send temperature change event to applications */
  7981. shost = lpfc_shost_from_vport(phba->pport);
  7982. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7983. sizeof(temp_event_data), (char *) &temp_event_data,
  7984. LPFC_NL_VENDOR_ID);
  7985. break;
  7986. case ASYNC_STATUS_CN:
  7987. lpfc_sli_abts_err_handler(phba, iocbq);
  7988. break;
  7989. default:
  7990. iocb_w = (uint32_t *) icmd;
  7991. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7992. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7993. " evt_code 0x%x\n"
  7994. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7995. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7996. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7997. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7998. pring->ringno, icmd->un.asyncstat.evt_code,
  7999. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  8000. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  8001. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  8002. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  8003. break;
  8004. }
  8005. }
  8006. /**
  8007. * lpfc_sli_setup - SLI ring setup function
  8008. * @phba: Pointer to HBA context object.
  8009. *
  8010. * lpfc_sli_setup sets up rings of the SLI interface with
  8011. * number of iocbs per ring and iotags. This function is
  8012. * called while driver attach to the HBA and before the
  8013. * interrupts are enabled. So there is no need for locking.
  8014. *
  8015. * This function always returns 0.
  8016. **/
  8017. int
  8018. lpfc_sli_setup(struct lpfc_hba *phba)
  8019. {
  8020. int i, totiocbsize = 0;
  8021. struct lpfc_sli *psli = &phba->sli;
  8022. struct lpfc_sli_ring *pring;
  8023. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  8024. if (phba->sli_rev == LPFC_SLI_REV4)
  8025. psli->num_rings += phba->cfg_fcp_io_channel;
  8026. psli->sli_flag = 0;
  8027. psli->fcp_ring = LPFC_FCP_RING;
  8028. psli->next_ring = LPFC_FCP_NEXT_RING;
  8029. psli->extra_ring = LPFC_EXTRA_RING;
  8030. psli->iocbq_lookup = NULL;
  8031. psli->iocbq_lookup_len = 0;
  8032. psli->last_iotag = 0;
  8033. for (i = 0; i < psli->num_rings; i++) {
  8034. pring = &psli->ring[i];
  8035. switch (i) {
  8036. case LPFC_FCP_RING: /* ring 0 - FCP */
  8037. /* numCiocb and numRiocb are used in config_port */
  8038. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  8039. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  8040. pring->sli.sli3.numCiocb +=
  8041. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8042. pring->sli.sli3.numRiocb +=
  8043. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8044. pring->sli.sli3.numCiocb +=
  8045. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8046. pring->sli.sli3.numRiocb +=
  8047. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8048. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8049. SLI3_IOCB_CMD_SIZE :
  8050. SLI2_IOCB_CMD_SIZE;
  8051. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8052. SLI3_IOCB_RSP_SIZE :
  8053. SLI2_IOCB_RSP_SIZE;
  8054. pring->iotag_ctr = 0;
  8055. pring->iotag_max =
  8056. (phba->cfg_hba_queue_depth * 2);
  8057. pring->fast_iotag = pring->iotag_max;
  8058. pring->num_mask = 0;
  8059. break;
  8060. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  8061. /* numCiocb and numRiocb are used in config_port */
  8062. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  8063. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  8064. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8065. SLI3_IOCB_CMD_SIZE :
  8066. SLI2_IOCB_CMD_SIZE;
  8067. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8068. SLI3_IOCB_RSP_SIZE :
  8069. SLI2_IOCB_RSP_SIZE;
  8070. pring->iotag_max = phba->cfg_hba_queue_depth;
  8071. pring->num_mask = 0;
  8072. break;
  8073. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8074. /* numCiocb and numRiocb are used in config_port */
  8075. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8076. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8077. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8078. SLI3_IOCB_CMD_SIZE :
  8079. SLI2_IOCB_CMD_SIZE;
  8080. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8081. SLI3_IOCB_RSP_SIZE :
  8082. SLI2_IOCB_RSP_SIZE;
  8083. pring->fast_iotag = 0;
  8084. pring->iotag_ctr = 0;
  8085. pring->iotag_max = 4096;
  8086. pring->lpfc_sli_rcv_async_status =
  8087. lpfc_sli_async_event_handler;
  8088. pring->num_mask = LPFC_MAX_RING_MASK;
  8089. pring->prt[0].profile = 0; /* Mask 0 */
  8090. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8091. pring->prt[0].type = FC_TYPE_ELS;
  8092. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8093. lpfc_els_unsol_event;
  8094. pring->prt[1].profile = 0; /* Mask 1 */
  8095. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8096. pring->prt[1].type = FC_TYPE_ELS;
  8097. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8098. lpfc_els_unsol_event;
  8099. pring->prt[2].profile = 0; /* Mask 2 */
  8100. /* NameServer Inquiry */
  8101. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8102. /* NameServer */
  8103. pring->prt[2].type = FC_TYPE_CT;
  8104. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8105. lpfc_ct_unsol_event;
  8106. pring->prt[3].profile = 0; /* Mask 3 */
  8107. /* NameServer response */
  8108. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8109. /* NameServer */
  8110. pring->prt[3].type = FC_TYPE_CT;
  8111. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8112. lpfc_ct_unsol_event;
  8113. break;
  8114. }
  8115. totiocbsize += (pring->sli.sli3.numCiocb *
  8116. pring->sli.sli3.sizeCiocb) +
  8117. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8118. }
  8119. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8120. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8121. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8122. "SLI2 SLIM Data: x%x x%lx\n",
  8123. phba->brd_no, totiocbsize,
  8124. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8125. }
  8126. if (phba->cfg_multi_ring_support == 2)
  8127. lpfc_extra_ring_setup(phba);
  8128. return 0;
  8129. }
  8130. /**
  8131. * lpfc_sli_queue_setup - Queue initialization function
  8132. * @phba: Pointer to HBA context object.
  8133. *
  8134. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8135. * ring. This function also initializes ring indices of each ring.
  8136. * This function is called during the initialization of the SLI
  8137. * interface of an HBA.
  8138. * This function is called with no lock held and always returns
  8139. * 1.
  8140. **/
  8141. int
  8142. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8143. {
  8144. struct lpfc_sli *psli;
  8145. struct lpfc_sli_ring *pring;
  8146. int i;
  8147. psli = &phba->sli;
  8148. spin_lock_irq(&phba->hbalock);
  8149. INIT_LIST_HEAD(&psli->mboxq);
  8150. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8151. /* Initialize list headers for txq and txcmplq as double linked lists */
  8152. for (i = 0; i < psli->num_rings; i++) {
  8153. pring = &psli->ring[i];
  8154. pring->ringno = i;
  8155. pring->sli.sli3.next_cmdidx = 0;
  8156. pring->sli.sli3.local_getidx = 0;
  8157. pring->sli.sli3.cmdidx = 0;
  8158. INIT_LIST_HEAD(&pring->txq);
  8159. INIT_LIST_HEAD(&pring->txcmplq);
  8160. INIT_LIST_HEAD(&pring->iocb_continueq);
  8161. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8162. INIT_LIST_HEAD(&pring->postbufq);
  8163. spin_lock_init(&pring->ring_lock);
  8164. }
  8165. spin_unlock_irq(&phba->hbalock);
  8166. return 1;
  8167. }
  8168. /**
  8169. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8170. * @phba: Pointer to HBA context object.
  8171. *
  8172. * This routine flushes the mailbox command subsystem. It will unconditionally
  8173. * flush all the mailbox commands in the three possible stages in the mailbox
  8174. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8175. * command; and completed mailbox command queue. It is caller's responsibility
  8176. * to make sure that the driver is in the proper state to flush the mailbox
  8177. * command sub-system. Namely, the posting of mailbox commands into the
  8178. * pending mailbox command queue from the various clients must be stopped;
  8179. * either the HBA is in a state that it will never works on the outstanding
  8180. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8181. * mailbox command has been completed.
  8182. **/
  8183. static void
  8184. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8185. {
  8186. LIST_HEAD(completions);
  8187. struct lpfc_sli *psli = &phba->sli;
  8188. LPFC_MBOXQ_t *pmb;
  8189. unsigned long iflag;
  8190. /* Flush all the mailbox commands in the mbox system */
  8191. spin_lock_irqsave(&phba->hbalock, iflag);
  8192. /* The pending mailbox command queue */
  8193. list_splice_init(&phba->sli.mboxq, &completions);
  8194. /* The outstanding active mailbox command */
  8195. if (psli->mbox_active) {
  8196. list_add_tail(&psli->mbox_active->list, &completions);
  8197. psli->mbox_active = NULL;
  8198. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8199. }
  8200. /* The completed mailbox command queue */
  8201. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8202. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8203. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8204. while (!list_empty(&completions)) {
  8205. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8206. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8207. if (pmb->mbox_cmpl)
  8208. pmb->mbox_cmpl(phba, pmb);
  8209. }
  8210. }
  8211. /**
  8212. * lpfc_sli_host_down - Vport cleanup function
  8213. * @vport: Pointer to virtual port object.
  8214. *
  8215. * lpfc_sli_host_down is called to clean up the resources
  8216. * associated with a vport before destroying virtual
  8217. * port data structures.
  8218. * This function does following operations:
  8219. * - Free discovery resources associated with this virtual
  8220. * port.
  8221. * - Free iocbs associated with this virtual port in
  8222. * the txq.
  8223. * - Send abort for all iocb commands associated with this
  8224. * vport in txcmplq.
  8225. *
  8226. * This function is called with no lock held and always returns 1.
  8227. **/
  8228. int
  8229. lpfc_sli_host_down(struct lpfc_vport *vport)
  8230. {
  8231. LIST_HEAD(completions);
  8232. struct lpfc_hba *phba = vport->phba;
  8233. struct lpfc_sli *psli = &phba->sli;
  8234. struct lpfc_sli_ring *pring;
  8235. struct lpfc_iocbq *iocb, *next_iocb;
  8236. int i;
  8237. unsigned long flags = 0;
  8238. uint16_t prev_pring_flag;
  8239. lpfc_cleanup_discovery_resources(vport);
  8240. spin_lock_irqsave(&phba->hbalock, flags);
  8241. for (i = 0; i < psli->num_rings; i++) {
  8242. pring = &psli->ring[i];
  8243. prev_pring_flag = pring->flag;
  8244. /* Only slow rings */
  8245. if (pring->ringno == LPFC_ELS_RING) {
  8246. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8247. /* Set the lpfc data pending flag */
  8248. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8249. }
  8250. /*
  8251. * Error everything on the txq since these iocbs have not been
  8252. * given to the FW yet.
  8253. */
  8254. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8255. if (iocb->vport != vport)
  8256. continue;
  8257. list_move_tail(&iocb->list, &completions);
  8258. pring->txq_cnt--;
  8259. }
  8260. /* Next issue ABTS for everything on the txcmplq */
  8261. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8262. list) {
  8263. if (iocb->vport != vport)
  8264. continue;
  8265. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8266. }
  8267. pring->flag = prev_pring_flag;
  8268. }
  8269. spin_unlock_irqrestore(&phba->hbalock, flags);
  8270. /* Cancel all the IOCBs from the completions list */
  8271. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8272. IOERR_SLI_DOWN);
  8273. return 1;
  8274. }
  8275. /**
  8276. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8277. * @phba: Pointer to HBA context object.
  8278. *
  8279. * This function cleans up all iocb, buffers, mailbox commands
  8280. * while shutting down the HBA. This function is called with no
  8281. * lock held and always returns 1.
  8282. * This function does the following to cleanup driver resources:
  8283. * - Free discovery resources for each virtual port
  8284. * - Cleanup any pending fabric iocbs
  8285. * - Iterate through the iocb txq and free each entry
  8286. * in the list.
  8287. * - Free up any buffer posted to the HBA
  8288. * - Free mailbox commands in the mailbox queue.
  8289. **/
  8290. int
  8291. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8292. {
  8293. LIST_HEAD(completions);
  8294. struct lpfc_sli *psli = &phba->sli;
  8295. struct lpfc_sli_ring *pring;
  8296. struct lpfc_dmabuf *buf_ptr;
  8297. unsigned long flags = 0;
  8298. int i;
  8299. /* Shutdown the mailbox command sub-system */
  8300. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8301. lpfc_hba_down_prep(phba);
  8302. lpfc_fabric_abort_hba(phba);
  8303. spin_lock_irqsave(&phba->hbalock, flags);
  8304. for (i = 0; i < psli->num_rings; i++) {
  8305. pring = &psli->ring[i];
  8306. /* Only slow rings */
  8307. if (pring->ringno == LPFC_ELS_RING) {
  8308. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8309. /* Set the lpfc data pending flag */
  8310. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8311. }
  8312. /*
  8313. * Error everything on the txq since these iocbs have not been
  8314. * given to the FW yet.
  8315. */
  8316. list_splice_init(&pring->txq, &completions);
  8317. pring->txq_cnt = 0;
  8318. }
  8319. spin_unlock_irqrestore(&phba->hbalock, flags);
  8320. /* Cancel all the IOCBs from the completions list */
  8321. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8322. IOERR_SLI_DOWN);
  8323. spin_lock_irqsave(&phba->hbalock, flags);
  8324. list_splice_init(&phba->elsbuf, &completions);
  8325. phba->elsbuf_cnt = 0;
  8326. phba->elsbuf_prev_cnt = 0;
  8327. spin_unlock_irqrestore(&phba->hbalock, flags);
  8328. while (!list_empty(&completions)) {
  8329. list_remove_head(&completions, buf_ptr,
  8330. struct lpfc_dmabuf, list);
  8331. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8332. kfree(buf_ptr);
  8333. }
  8334. /* Return any active mbox cmds */
  8335. del_timer_sync(&psli->mbox_tmo);
  8336. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8337. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8338. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8339. return 1;
  8340. }
  8341. /**
  8342. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8343. * @srcp: Source memory pointer.
  8344. * @destp: Destination memory pointer.
  8345. * @cnt: Number of words required to be copied.
  8346. *
  8347. * This function is used for copying data between driver memory
  8348. * and the SLI memory. This function also changes the endianness
  8349. * of each word if native endianness is different from SLI
  8350. * endianness. This function can be called with or without
  8351. * lock.
  8352. **/
  8353. void
  8354. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8355. {
  8356. uint32_t *src = srcp;
  8357. uint32_t *dest = destp;
  8358. uint32_t ldata;
  8359. int i;
  8360. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8361. ldata = *src;
  8362. ldata = le32_to_cpu(ldata);
  8363. *dest = ldata;
  8364. src++;
  8365. dest++;
  8366. }
  8367. }
  8368. /**
  8369. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8370. * @srcp: Source memory pointer.
  8371. * @destp: Destination memory pointer.
  8372. * @cnt: Number of words required to be copied.
  8373. *
  8374. * This function is used for copying data between a data structure
  8375. * with big endian representation to local endianness.
  8376. * This function can be called with or without lock.
  8377. **/
  8378. void
  8379. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8380. {
  8381. uint32_t *src = srcp;
  8382. uint32_t *dest = destp;
  8383. uint32_t ldata;
  8384. int i;
  8385. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8386. ldata = *src;
  8387. ldata = be32_to_cpu(ldata);
  8388. *dest = ldata;
  8389. src++;
  8390. dest++;
  8391. }
  8392. }
  8393. /**
  8394. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8395. * @phba: Pointer to HBA context object.
  8396. * @pring: Pointer to driver SLI ring object.
  8397. * @mp: Pointer to driver buffer object.
  8398. *
  8399. * This function is called with no lock held.
  8400. * It always return zero after adding the buffer to the postbufq
  8401. * buffer list.
  8402. **/
  8403. int
  8404. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8405. struct lpfc_dmabuf *mp)
  8406. {
  8407. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8408. later */
  8409. spin_lock_irq(&phba->hbalock);
  8410. list_add_tail(&mp->list, &pring->postbufq);
  8411. pring->postbufq_cnt++;
  8412. spin_unlock_irq(&phba->hbalock);
  8413. return 0;
  8414. }
  8415. /**
  8416. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8417. * @phba: Pointer to HBA context object.
  8418. *
  8419. * When HBQ is enabled, buffers are searched based on tags. This function
  8420. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8421. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8422. * does not conflict with tags of buffer posted for unsolicited events.
  8423. * The function returns the allocated tag. The function is called with
  8424. * no locks held.
  8425. **/
  8426. uint32_t
  8427. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8428. {
  8429. spin_lock_irq(&phba->hbalock);
  8430. phba->buffer_tag_count++;
  8431. /*
  8432. * Always set the QUE_BUFTAG_BIT to distiguish between
  8433. * a tag assigned by HBQ.
  8434. */
  8435. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8436. spin_unlock_irq(&phba->hbalock);
  8437. return phba->buffer_tag_count;
  8438. }
  8439. /**
  8440. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8441. * @phba: Pointer to HBA context object.
  8442. * @pring: Pointer to driver SLI ring object.
  8443. * @tag: Buffer tag.
  8444. *
  8445. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8446. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8447. * iocb is posted to the response ring with the tag of the buffer.
  8448. * This function searches the pring->postbufq list using the tag
  8449. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8450. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8451. * buffer is returned to the caller else NULL is returned.
  8452. * This function is called with no lock held.
  8453. **/
  8454. struct lpfc_dmabuf *
  8455. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8456. uint32_t tag)
  8457. {
  8458. struct lpfc_dmabuf *mp, *next_mp;
  8459. struct list_head *slp = &pring->postbufq;
  8460. /* Search postbufq, from the beginning, looking for a match on tag */
  8461. spin_lock_irq(&phba->hbalock);
  8462. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8463. if (mp->buffer_tag == tag) {
  8464. list_del_init(&mp->list);
  8465. pring->postbufq_cnt--;
  8466. spin_unlock_irq(&phba->hbalock);
  8467. return mp;
  8468. }
  8469. }
  8470. spin_unlock_irq(&phba->hbalock);
  8471. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8472. "0402 Cannot find virtual addr for buffer tag on "
  8473. "ring %d Data x%lx x%p x%p x%x\n",
  8474. pring->ringno, (unsigned long) tag,
  8475. slp->next, slp->prev, pring->postbufq_cnt);
  8476. return NULL;
  8477. }
  8478. /**
  8479. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8480. * @phba: Pointer to HBA context object.
  8481. * @pring: Pointer to driver SLI ring object.
  8482. * @phys: DMA address of the buffer.
  8483. *
  8484. * This function searches the buffer list using the dma_address
  8485. * of unsolicited event to find the driver's lpfc_dmabuf object
  8486. * corresponding to the dma_address. The function returns the
  8487. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8488. * This function is called by the ct and els unsolicited event
  8489. * handlers to get the buffer associated with the unsolicited
  8490. * event.
  8491. *
  8492. * This function is called with no lock held.
  8493. **/
  8494. struct lpfc_dmabuf *
  8495. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8496. dma_addr_t phys)
  8497. {
  8498. struct lpfc_dmabuf *mp, *next_mp;
  8499. struct list_head *slp = &pring->postbufq;
  8500. /* Search postbufq, from the beginning, looking for a match on phys */
  8501. spin_lock_irq(&phba->hbalock);
  8502. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8503. if (mp->phys == phys) {
  8504. list_del_init(&mp->list);
  8505. pring->postbufq_cnt--;
  8506. spin_unlock_irq(&phba->hbalock);
  8507. return mp;
  8508. }
  8509. }
  8510. spin_unlock_irq(&phba->hbalock);
  8511. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8512. "0410 Cannot find virtual addr for mapped buf on "
  8513. "ring %d Data x%llx x%p x%p x%x\n",
  8514. pring->ringno, (unsigned long long)phys,
  8515. slp->next, slp->prev, pring->postbufq_cnt);
  8516. return NULL;
  8517. }
  8518. /**
  8519. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8520. * @phba: Pointer to HBA context object.
  8521. * @cmdiocb: Pointer to driver command iocb object.
  8522. * @rspiocb: Pointer to driver response iocb object.
  8523. *
  8524. * This function is the completion handler for the abort iocbs for
  8525. * ELS commands. This function is called from the ELS ring event
  8526. * handler with no lock held. This function frees memory resources
  8527. * associated with the abort iocb.
  8528. **/
  8529. static void
  8530. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8531. struct lpfc_iocbq *rspiocb)
  8532. {
  8533. IOCB_t *irsp = &rspiocb->iocb;
  8534. uint16_t abort_iotag, abort_context;
  8535. struct lpfc_iocbq *abort_iocb = NULL;
  8536. if (irsp->ulpStatus) {
  8537. /*
  8538. * Assume that the port already completed and returned, or
  8539. * will return the iocb. Just Log the message.
  8540. */
  8541. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8542. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8543. spin_lock_irq(&phba->hbalock);
  8544. if (phba->sli_rev < LPFC_SLI_REV4) {
  8545. if (abort_iotag != 0 &&
  8546. abort_iotag <= phba->sli.last_iotag)
  8547. abort_iocb =
  8548. phba->sli.iocbq_lookup[abort_iotag];
  8549. } else
  8550. /* For sli4 the abort_tag is the XRI,
  8551. * so the abort routine puts the iotag of the iocb
  8552. * being aborted in the context field of the abort
  8553. * IOCB.
  8554. */
  8555. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8556. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8557. "0327 Cannot abort els iocb %p "
  8558. "with tag %x context %x, abort status %x, "
  8559. "abort code %x\n",
  8560. abort_iocb, abort_iotag, abort_context,
  8561. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8562. spin_unlock_irq(&phba->hbalock);
  8563. }
  8564. lpfc_sli_release_iocbq(phba, cmdiocb);
  8565. return;
  8566. }
  8567. /**
  8568. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8569. * @phba: Pointer to HBA context object.
  8570. * @cmdiocb: Pointer to driver command iocb object.
  8571. * @rspiocb: Pointer to driver response iocb object.
  8572. *
  8573. * The function is called from SLI ring event handler with no
  8574. * lock held. This function is the completion handler for ELS commands
  8575. * which are aborted. The function frees memory resources used for
  8576. * the aborted ELS commands.
  8577. **/
  8578. static void
  8579. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8580. struct lpfc_iocbq *rspiocb)
  8581. {
  8582. IOCB_t *irsp = &rspiocb->iocb;
  8583. /* ELS cmd tag <ulpIoTag> completes */
  8584. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8585. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8586. "x%x x%x x%x\n",
  8587. irsp->ulpIoTag, irsp->ulpStatus,
  8588. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8589. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8590. lpfc_ct_free_iocb(phba, cmdiocb);
  8591. else
  8592. lpfc_els_free_iocb(phba, cmdiocb);
  8593. return;
  8594. }
  8595. /**
  8596. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8597. * @phba: Pointer to HBA context object.
  8598. * @pring: Pointer to driver SLI ring object.
  8599. * @cmdiocb: Pointer to driver command iocb object.
  8600. *
  8601. * This function issues an abort iocb for the provided command iocb down to
  8602. * the port. Other than the case the outstanding command iocb is an abort
  8603. * request, this function issues abort out unconditionally. This function is
  8604. * called with hbalock held. The function returns 0 when it fails due to
  8605. * memory allocation failure or when the command iocb is an abort request.
  8606. **/
  8607. static int
  8608. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8609. struct lpfc_iocbq *cmdiocb)
  8610. {
  8611. struct lpfc_vport *vport = cmdiocb->vport;
  8612. struct lpfc_iocbq *abtsiocbp;
  8613. IOCB_t *icmd = NULL;
  8614. IOCB_t *iabt = NULL;
  8615. int retval;
  8616. unsigned long iflags;
  8617. /*
  8618. * There are certain command types we don't want to abort. And we
  8619. * don't want to abort commands that are already in the process of
  8620. * being aborted.
  8621. */
  8622. icmd = &cmdiocb->iocb;
  8623. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8624. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8625. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8626. return 0;
  8627. /* issue ABTS for this IOCB based on iotag */
  8628. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8629. if (abtsiocbp == NULL)
  8630. return 0;
  8631. /* This signals the response to set the correct status
  8632. * before calling the completion handler
  8633. */
  8634. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8635. iabt = &abtsiocbp->iocb;
  8636. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8637. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8638. if (phba->sli_rev == LPFC_SLI_REV4) {
  8639. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8640. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8641. }
  8642. else
  8643. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8644. iabt->ulpLe = 1;
  8645. iabt->ulpClass = icmd->ulpClass;
  8646. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8647. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8648. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8649. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8650. if (phba->link_state >= LPFC_LINK_UP)
  8651. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8652. else
  8653. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8654. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8655. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8656. "0339 Abort xri x%x, original iotag x%x, "
  8657. "abort cmd iotag x%x\n",
  8658. iabt->un.acxri.abortIoTag,
  8659. iabt->un.acxri.abortContextTag,
  8660. abtsiocbp->iotag);
  8661. if (phba->sli_rev == LPFC_SLI_REV4) {
  8662. /* Note: both hbalock and ring_lock need to be set here */
  8663. spin_lock_irqsave(&pring->ring_lock, iflags);
  8664. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8665. abtsiocbp, 0);
  8666. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8667. } else {
  8668. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8669. abtsiocbp, 0);
  8670. }
  8671. if (retval)
  8672. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8673. /*
  8674. * Caller to this routine should check for IOCB_ERROR
  8675. * and handle it properly. This routine no longer removes
  8676. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8677. */
  8678. return retval;
  8679. }
  8680. /**
  8681. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8682. * @phba: Pointer to HBA context object.
  8683. * @pring: Pointer to driver SLI ring object.
  8684. * @cmdiocb: Pointer to driver command iocb object.
  8685. *
  8686. * This function issues an abort iocb for the provided command iocb. In case
  8687. * of unloading, the abort iocb will not be issued to commands on the ELS
  8688. * ring. Instead, the callback function shall be changed to those commands
  8689. * so that nothing happens when them finishes. This function is called with
  8690. * hbalock held. The function returns 0 when the command iocb is an abort
  8691. * request.
  8692. **/
  8693. int
  8694. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8695. struct lpfc_iocbq *cmdiocb)
  8696. {
  8697. struct lpfc_vport *vport = cmdiocb->vport;
  8698. int retval = IOCB_ERROR;
  8699. IOCB_t *icmd = NULL;
  8700. /*
  8701. * There are certain command types we don't want to abort. And we
  8702. * don't want to abort commands that are already in the process of
  8703. * being aborted.
  8704. */
  8705. icmd = &cmdiocb->iocb;
  8706. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8707. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8708. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8709. return 0;
  8710. /*
  8711. * If we're unloading, don't abort iocb on the ELS ring, but change
  8712. * the callback so that nothing happens when it finishes.
  8713. */
  8714. if ((vport->load_flag & FC_UNLOADING) &&
  8715. (pring->ringno == LPFC_ELS_RING)) {
  8716. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8717. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8718. else
  8719. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8720. goto abort_iotag_exit;
  8721. }
  8722. /* Now, we try to issue the abort to the cmdiocb out */
  8723. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8724. abort_iotag_exit:
  8725. /*
  8726. * Caller to this routine should check for IOCB_ERROR
  8727. * and handle it properly. This routine no longer removes
  8728. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8729. */
  8730. return retval;
  8731. }
  8732. /**
  8733. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8734. * @phba: Pointer to HBA context object.
  8735. * @pring: Pointer to driver SLI ring object.
  8736. *
  8737. * This function aborts all iocbs in the given ring and frees all the iocb
  8738. * objects in txq. This function issues abort iocbs unconditionally for all
  8739. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8740. * to complete before the return of this function. The caller is not required
  8741. * to hold any locks.
  8742. **/
  8743. static void
  8744. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8745. {
  8746. LIST_HEAD(completions);
  8747. struct lpfc_iocbq *iocb, *next_iocb;
  8748. if (pring->ringno == LPFC_ELS_RING)
  8749. lpfc_fabric_abort_hba(phba);
  8750. spin_lock_irq(&phba->hbalock);
  8751. /* Take off all the iocbs on txq for cancelling */
  8752. list_splice_init(&pring->txq, &completions);
  8753. pring->txq_cnt = 0;
  8754. /* Next issue ABTS for everything on the txcmplq */
  8755. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8756. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8757. spin_unlock_irq(&phba->hbalock);
  8758. /* Cancel all the IOCBs from the completions list */
  8759. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8760. IOERR_SLI_ABORTED);
  8761. }
  8762. /**
  8763. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8764. * @phba: pointer to lpfc HBA data structure.
  8765. *
  8766. * This routine will abort all pending and outstanding iocbs to an HBA.
  8767. **/
  8768. void
  8769. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8770. {
  8771. struct lpfc_sli *psli = &phba->sli;
  8772. struct lpfc_sli_ring *pring;
  8773. int i;
  8774. for (i = 0; i < psli->num_rings; i++) {
  8775. pring = &psli->ring[i];
  8776. lpfc_sli_iocb_ring_abort(phba, pring);
  8777. }
  8778. }
  8779. /**
  8780. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8781. * @iocbq: Pointer to driver iocb object.
  8782. * @vport: Pointer to driver virtual port object.
  8783. * @tgt_id: SCSI ID of the target.
  8784. * @lun_id: LUN ID of the scsi device.
  8785. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8786. *
  8787. * This function acts as an iocb filter for functions which abort or count
  8788. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8789. * 0 if the filtering criteria is met for the given iocb and will return
  8790. * 1 if the filtering criteria is not met.
  8791. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8792. * given iocb is for the SCSI device specified by vport, tgt_id and
  8793. * lun_id parameter.
  8794. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8795. * given iocb is for the SCSI target specified by vport and tgt_id
  8796. * parameters.
  8797. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8798. * given iocb is for the SCSI host associated with the given vport.
  8799. * This function is called with no locks held.
  8800. **/
  8801. static int
  8802. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8803. uint16_t tgt_id, uint64_t lun_id,
  8804. lpfc_ctx_cmd ctx_cmd)
  8805. {
  8806. struct lpfc_scsi_buf *lpfc_cmd;
  8807. int rc = 1;
  8808. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8809. return rc;
  8810. if (iocbq->vport != vport)
  8811. return rc;
  8812. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8813. if (lpfc_cmd->pCmd == NULL)
  8814. return rc;
  8815. switch (ctx_cmd) {
  8816. case LPFC_CTX_LUN:
  8817. if ((lpfc_cmd->rdata->pnode) &&
  8818. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8819. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8820. rc = 0;
  8821. break;
  8822. case LPFC_CTX_TGT:
  8823. if ((lpfc_cmd->rdata->pnode) &&
  8824. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8825. rc = 0;
  8826. break;
  8827. case LPFC_CTX_HOST:
  8828. rc = 0;
  8829. break;
  8830. default:
  8831. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8832. __func__, ctx_cmd);
  8833. break;
  8834. }
  8835. return rc;
  8836. }
  8837. /**
  8838. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8839. * @vport: Pointer to virtual port.
  8840. * @tgt_id: SCSI ID of the target.
  8841. * @lun_id: LUN ID of the scsi device.
  8842. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8843. *
  8844. * This function returns number of FCP commands pending for the vport.
  8845. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8846. * commands pending on the vport associated with SCSI device specified
  8847. * by tgt_id and lun_id parameters.
  8848. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8849. * commands pending on the vport associated with SCSI target specified
  8850. * by tgt_id parameter.
  8851. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8852. * commands pending on the vport.
  8853. * This function returns the number of iocbs which satisfy the filter.
  8854. * This function is called without any lock held.
  8855. **/
  8856. int
  8857. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8858. lpfc_ctx_cmd ctx_cmd)
  8859. {
  8860. struct lpfc_hba *phba = vport->phba;
  8861. struct lpfc_iocbq *iocbq;
  8862. int sum, i;
  8863. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8864. iocbq = phba->sli.iocbq_lookup[i];
  8865. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8866. ctx_cmd) == 0)
  8867. sum++;
  8868. }
  8869. return sum;
  8870. }
  8871. /**
  8872. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8873. * @phba: Pointer to HBA context object
  8874. * @cmdiocb: Pointer to command iocb object.
  8875. * @rspiocb: Pointer to response iocb object.
  8876. *
  8877. * This function is called when an aborted FCP iocb completes. This
  8878. * function is called by the ring event handler with no lock held.
  8879. * This function frees the iocb.
  8880. **/
  8881. void
  8882. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8883. struct lpfc_iocbq *rspiocb)
  8884. {
  8885. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8886. "3096 ABORT_XRI_CN completing on xri x%x "
  8887. "original iotag x%x, abort cmd iotag x%x "
  8888. "status 0x%x, reason 0x%x\n",
  8889. cmdiocb->iocb.un.acxri.abortContextTag,
  8890. cmdiocb->iocb.un.acxri.abortIoTag,
  8891. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8892. rspiocb->iocb.un.ulpWord[4]);
  8893. lpfc_sli_release_iocbq(phba, cmdiocb);
  8894. return;
  8895. }
  8896. /**
  8897. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8898. * @vport: Pointer to virtual port.
  8899. * @pring: Pointer to driver SLI ring object.
  8900. * @tgt_id: SCSI ID of the target.
  8901. * @lun_id: LUN ID of the scsi device.
  8902. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8903. *
  8904. * This function sends an abort command for every SCSI command
  8905. * associated with the given virtual port pending on the ring
  8906. * filtered by lpfc_sli_validate_fcp_iocb function.
  8907. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8908. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8909. * parameters
  8910. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8911. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8912. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8913. * FCP iocbs associated with virtual port.
  8914. * This function returns number of iocbs it failed to abort.
  8915. * This function is called with no locks held.
  8916. **/
  8917. int
  8918. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8919. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8920. {
  8921. struct lpfc_hba *phba = vport->phba;
  8922. struct lpfc_iocbq *iocbq;
  8923. struct lpfc_iocbq *abtsiocb;
  8924. IOCB_t *cmd = NULL;
  8925. int errcnt = 0, ret_val = 0;
  8926. int i;
  8927. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8928. iocbq = phba->sli.iocbq_lookup[i];
  8929. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8930. abort_cmd) != 0)
  8931. continue;
  8932. /* issue ABTS for this IOCB based on iotag */
  8933. abtsiocb = lpfc_sli_get_iocbq(phba);
  8934. if (abtsiocb == NULL) {
  8935. errcnt++;
  8936. continue;
  8937. }
  8938. cmd = &iocbq->iocb;
  8939. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8940. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8941. if (phba->sli_rev == LPFC_SLI_REV4)
  8942. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8943. else
  8944. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8945. abtsiocb->iocb.ulpLe = 1;
  8946. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8947. abtsiocb->vport = phba->pport;
  8948. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8949. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8950. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8951. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8952. if (lpfc_is_link_up(phba))
  8953. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8954. else
  8955. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8956. /* Setup callback routine and issue the command. */
  8957. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8958. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8959. abtsiocb, 0);
  8960. if (ret_val == IOCB_ERROR) {
  8961. lpfc_sli_release_iocbq(phba, abtsiocb);
  8962. errcnt++;
  8963. continue;
  8964. }
  8965. }
  8966. return errcnt;
  8967. }
  8968. /**
  8969. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8970. * @phba: Pointer to HBA context object.
  8971. * @cmdiocbq: Pointer to command iocb.
  8972. * @rspiocbq: Pointer to response iocb.
  8973. *
  8974. * This function is the completion handler for iocbs issued using
  8975. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8976. * ring event handler function without any lock held. This function
  8977. * can be called from both worker thread context and interrupt
  8978. * context. This function also can be called from other thread which
  8979. * cleans up the SLI layer objects.
  8980. * This function copy the contents of the response iocb to the
  8981. * response iocb memory object provided by the caller of
  8982. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8983. * sleeps for the iocb completion.
  8984. **/
  8985. static void
  8986. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8987. struct lpfc_iocbq *cmdiocbq,
  8988. struct lpfc_iocbq *rspiocbq)
  8989. {
  8990. wait_queue_head_t *pdone_q;
  8991. unsigned long iflags;
  8992. struct lpfc_scsi_buf *lpfc_cmd;
  8993. spin_lock_irqsave(&phba->hbalock, iflags);
  8994. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8995. if (cmdiocbq->context2 && rspiocbq)
  8996. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8997. &rspiocbq->iocb, sizeof(IOCB_t));
  8998. /* Set the exchange busy flag for task management commands */
  8999. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  9000. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  9001. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  9002. cur_iocbq);
  9003. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  9004. }
  9005. pdone_q = cmdiocbq->context_un.wait_queue;
  9006. if (pdone_q)
  9007. wake_up(pdone_q);
  9008. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9009. return;
  9010. }
  9011. /**
  9012. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  9013. * @phba: Pointer to HBA context object..
  9014. * @piocbq: Pointer to command iocb.
  9015. * @flag: Flag to test.
  9016. *
  9017. * This routine grabs the hbalock and then test the iocb_flag to
  9018. * see if the passed in flag is set.
  9019. * Returns:
  9020. * 1 if flag is set.
  9021. * 0 if flag is not set.
  9022. **/
  9023. static int
  9024. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  9025. struct lpfc_iocbq *piocbq, uint32_t flag)
  9026. {
  9027. unsigned long iflags;
  9028. int ret;
  9029. spin_lock_irqsave(&phba->hbalock, iflags);
  9030. ret = piocbq->iocb_flag & flag;
  9031. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9032. return ret;
  9033. }
  9034. /**
  9035. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  9036. * @phba: Pointer to HBA context object..
  9037. * @pring: Pointer to sli ring.
  9038. * @piocb: Pointer to command iocb.
  9039. * @prspiocbq: Pointer to response iocb.
  9040. * @timeout: Timeout in number of seconds.
  9041. *
  9042. * This function issues the iocb to firmware and waits for the
  9043. * iocb to complete. If the iocb command is not
  9044. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  9045. * Caller should not free the iocb resources if this function
  9046. * returns IOCB_TIMEDOUT.
  9047. * The function waits for the iocb completion using an
  9048. * non-interruptible wait.
  9049. * This function will sleep while waiting for iocb completion.
  9050. * So, this function should not be called from any context which
  9051. * does not allow sleeping. Due to the same reason, this function
  9052. * cannot be called with interrupt disabled.
  9053. * This function assumes that the iocb completions occur while
  9054. * this function sleep. So, this function cannot be called from
  9055. * the thread which process iocb completion for this ring.
  9056. * This function clears the iocb_flag of the iocb object before
  9057. * issuing the iocb and the iocb completion handler sets this
  9058. * flag and wakes this thread when the iocb completes.
  9059. * The contents of the response iocb will be copied to prspiocbq
  9060. * by the completion handler when the command completes.
  9061. * This function returns IOCB_SUCCESS when success.
  9062. * This function is called with no lock held.
  9063. **/
  9064. int
  9065. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9066. uint32_t ring_number,
  9067. struct lpfc_iocbq *piocb,
  9068. struct lpfc_iocbq *prspiocbq,
  9069. uint32_t timeout)
  9070. {
  9071. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9072. long timeleft, timeout_req = 0;
  9073. int retval = IOCB_SUCCESS;
  9074. uint32_t creg_val;
  9075. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9076. /*
  9077. * If the caller has provided a response iocbq buffer, then context2
  9078. * is NULL or its an error.
  9079. */
  9080. if (prspiocbq) {
  9081. if (piocb->context2)
  9082. return IOCB_ERROR;
  9083. piocb->context2 = prspiocbq;
  9084. }
  9085. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9086. piocb->context_un.wait_queue = &done_q;
  9087. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  9088. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9089. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9090. return IOCB_ERROR;
  9091. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9092. writel(creg_val, phba->HCregaddr);
  9093. readl(phba->HCregaddr); /* flush */
  9094. }
  9095. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9096. SLI_IOCB_RET_IOCB);
  9097. if (retval == IOCB_SUCCESS) {
  9098. timeout_req = timeout * HZ;
  9099. timeleft = wait_event_timeout(done_q,
  9100. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9101. timeout_req);
  9102. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  9103. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9104. "0331 IOCB wake signaled\n");
  9105. } else if (timeleft == 0) {
  9106. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9107. "0338 IOCB wait timeout error - no "
  9108. "wake response Data x%x\n", timeout);
  9109. retval = IOCB_TIMEDOUT;
  9110. } else {
  9111. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9112. "0330 IOCB wake NOT set, "
  9113. "Data x%x x%lx\n",
  9114. timeout, (timeleft / jiffies));
  9115. retval = IOCB_TIMEDOUT;
  9116. }
  9117. } else if (retval == IOCB_BUSY) {
  9118. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9119. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9120. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  9121. return retval;
  9122. } else {
  9123. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9124. "0332 IOCB wait issue failed, Data x%x\n",
  9125. retval);
  9126. retval = IOCB_ERROR;
  9127. }
  9128. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9129. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9130. return IOCB_ERROR;
  9131. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9132. writel(creg_val, phba->HCregaddr);
  9133. readl(phba->HCregaddr); /* flush */
  9134. }
  9135. if (prspiocbq)
  9136. piocb->context2 = NULL;
  9137. piocb->context_un.wait_queue = NULL;
  9138. piocb->iocb_cmpl = NULL;
  9139. return retval;
  9140. }
  9141. /**
  9142. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9143. * @phba: Pointer to HBA context object.
  9144. * @pmboxq: Pointer to driver mailbox object.
  9145. * @timeout: Timeout in number of seconds.
  9146. *
  9147. * This function issues the mailbox to firmware and waits for the
  9148. * mailbox command to complete. If the mailbox command is not
  9149. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9150. * The function waits for the mailbox completion using an
  9151. * interruptible wait. If the thread is woken up due to a
  9152. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9153. * should not free the mailbox resources, if this function returns
  9154. * MBX_TIMEOUT.
  9155. * This function will sleep while waiting for mailbox completion.
  9156. * So, this function should not be called from any context which
  9157. * does not allow sleeping. Due to the same reason, this function
  9158. * cannot be called with interrupt disabled.
  9159. * This function assumes that the mailbox completion occurs while
  9160. * this function sleep. So, this function cannot be called from
  9161. * the worker thread which processes mailbox completion.
  9162. * This function is called in the context of HBA management
  9163. * applications.
  9164. * This function returns MBX_SUCCESS when successful.
  9165. * This function is called with no lock held.
  9166. **/
  9167. int
  9168. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9169. uint32_t timeout)
  9170. {
  9171. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9172. int retval;
  9173. unsigned long flag;
  9174. /* The caller must leave context1 empty. */
  9175. if (pmboxq->context1)
  9176. return MBX_NOT_FINISHED;
  9177. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9178. /* setup wake call as IOCB callback */
  9179. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9180. /* setup context field to pass wait_queue pointer to wake function */
  9181. pmboxq->context1 = &done_q;
  9182. /* now issue the command */
  9183. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9184. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9185. wait_event_interruptible_timeout(done_q,
  9186. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9187. timeout * HZ);
  9188. spin_lock_irqsave(&phba->hbalock, flag);
  9189. pmboxq->context1 = NULL;
  9190. /*
  9191. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9192. * else do not free the resources.
  9193. */
  9194. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9195. retval = MBX_SUCCESS;
  9196. lpfc_sli4_swap_str(phba, pmboxq);
  9197. } else {
  9198. retval = MBX_TIMEOUT;
  9199. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9200. }
  9201. spin_unlock_irqrestore(&phba->hbalock, flag);
  9202. }
  9203. return retval;
  9204. }
  9205. /**
  9206. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9207. * @phba: Pointer to HBA context.
  9208. *
  9209. * This function is called to shutdown the driver's mailbox sub-system.
  9210. * It first marks the mailbox sub-system is in a block state to prevent
  9211. * the asynchronous mailbox command from issued off the pending mailbox
  9212. * command queue. If the mailbox command sub-system shutdown is due to
  9213. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9214. * the mailbox sub-system flush routine to forcefully bring down the
  9215. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9216. * as with offline or HBA function reset), this routine will wait for the
  9217. * outstanding mailbox command to complete before invoking the mailbox
  9218. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9219. **/
  9220. void
  9221. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9222. {
  9223. struct lpfc_sli *psli = &phba->sli;
  9224. unsigned long timeout;
  9225. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9226. /* delay 100ms for port state */
  9227. msleep(100);
  9228. lpfc_sli_mbox_sys_flush(phba);
  9229. return;
  9230. }
  9231. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9232. spin_lock_irq(&phba->hbalock);
  9233. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9234. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9235. /* Determine how long we might wait for the active mailbox
  9236. * command to be gracefully completed by firmware.
  9237. */
  9238. if (phba->sli.mbox_active)
  9239. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9240. phba->sli.mbox_active) *
  9241. 1000) + jiffies;
  9242. spin_unlock_irq(&phba->hbalock);
  9243. while (phba->sli.mbox_active) {
  9244. /* Check active mailbox complete status every 2ms */
  9245. msleep(2);
  9246. if (time_after(jiffies, timeout))
  9247. /* Timeout, let the mailbox flush routine to
  9248. * forcefully release active mailbox command
  9249. */
  9250. break;
  9251. }
  9252. } else
  9253. spin_unlock_irq(&phba->hbalock);
  9254. lpfc_sli_mbox_sys_flush(phba);
  9255. }
  9256. /**
  9257. * lpfc_sli_eratt_read - read sli-3 error attention events
  9258. * @phba: Pointer to HBA context.
  9259. *
  9260. * This function is called to read the SLI3 device error attention registers
  9261. * for possible error attention events. The caller must hold the hostlock
  9262. * with spin_lock_irq().
  9263. *
  9264. * This function returns 1 when there is Error Attention in the Host Attention
  9265. * Register and returns 0 otherwise.
  9266. **/
  9267. static int
  9268. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9269. {
  9270. uint32_t ha_copy;
  9271. /* Read chip Host Attention (HA) register */
  9272. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9273. goto unplug_err;
  9274. if (ha_copy & HA_ERATT) {
  9275. /* Read host status register to retrieve error event */
  9276. if (lpfc_sli_read_hs(phba))
  9277. goto unplug_err;
  9278. /* Check if there is a deferred error condition is active */
  9279. if ((HS_FFER1 & phba->work_hs) &&
  9280. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9281. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9282. phba->hba_flag |= DEFER_ERATT;
  9283. /* Clear all interrupt enable conditions */
  9284. writel(0, phba->HCregaddr);
  9285. readl(phba->HCregaddr);
  9286. }
  9287. /* Set the driver HA work bitmap */
  9288. phba->work_ha |= HA_ERATT;
  9289. /* Indicate polling handles this ERATT */
  9290. phba->hba_flag |= HBA_ERATT_HANDLED;
  9291. return 1;
  9292. }
  9293. return 0;
  9294. unplug_err:
  9295. /* Set the driver HS work bitmap */
  9296. phba->work_hs |= UNPLUG_ERR;
  9297. /* Set the driver HA work bitmap */
  9298. phba->work_ha |= HA_ERATT;
  9299. /* Indicate polling handles this ERATT */
  9300. phba->hba_flag |= HBA_ERATT_HANDLED;
  9301. return 1;
  9302. }
  9303. /**
  9304. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9305. * @phba: Pointer to HBA context.
  9306. *
  9307. * This function is called to read the SLI4 device error attention registers
  9308. * for possible error attention events. The caller must hold the hostlock
  9309. * with spin_lock_irq().
  9310. *
  9311. * This function returns 1 when there is Error Attention in the Host Attention
  9312. * Register and returns 0 otherwise.
  9313. **/
  9314. static int
  9315. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9316. {
  9317. uint32_t uerr_sta_hi, uerr_sta_lo;
  9318. uint32_t if_type, portsmphr;
  9319. struct lpfc_register portstat_reg;
  9320. /*
  9321. * For now, use the SLI4 device internal unrecoverable error
  9322. * registers for error attention. This can be changed later.
  9323. */
  9324. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9325. switch (if_type) {
  9326. case LPFC_SLI_INTF_IF_TYPE_0:
  9327. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9328. &uerr_sta_lo) ||
  9329. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9330. &uerr_sta_hi)) {
  9331. phba->work_hs |= UNPLUG_ERR;
  9332. phba->work_ha |= HA_ERATT;
  9333. phba->hba_flag |= HBA_ERATT_HANDLED;
  9334. return 1;
  9335. }
  9336. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9337. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9338. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9339. "1423 HBA Unrecoverable error: "
  9340. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9341. "ue_mask_lo_reg=0x%x, "
  9342. "ue_mask_hi_reg=0x%x\n",
  9343. uerr_sta_lo, uerr_sta_hi,
  9344. phba->sli4_hba.ue_mask_lo,
  9345. phba->sli4_hba.ue_mask_hi);
  9346. phba->work_status[0] = uerr_sta_lo;
  9347. phba->work_status[1] = uerr_sta_hi;
  9348. phba->work_ha |= HA_ERATT;
  9349. phba->hba_flag |= HBA_ERATT_HANDLED;
  9350. return 1;
  9351. }
  9352. break;
  9353. case LPFC_SLI_INTF_IF_TYPE_2:
  9354. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9355. &portstat_reg.word0) ||
  9356. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9357. &portsmphr)){
  9358. phba->work_hs |= UNPLUG_ERR;
  9359. phba->work_ha |= HA_ERATT;
  9360. phba->hba_flag |= HBA_ERATT_HANDLED;
  9361. return 1;
  9362. }
  9363. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9364. phba->work_status[0] =
  9365. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9366. phba->work_status[1] =
  9367. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9368. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9369. "2885 Port Status Event: "
  9370. "port status reg 0x%x, "
  9371. "port smphr reg 0x%x, "
  9372. "error 1=0x%x, error 2=0x%x\n",
  9373. portstat_reg.word0,
  9374. portsmphr,
  9375. phba->work_status[0],
  9376. phba->work_status[1]);
  9377. phba->work_ha |= HA_ERATT;
  9378. phba->hba_flag |= HBA_ERATT_HANDLED;
  9379. return 1;
  9380. }
  9381. break;
  9382. case LPFC_SLI_INTF_IF_TYPE_1:
  9383. default:
  9384. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9385. "2886 HBA Error Attention on unsupported "
  9386. "if type %d.", if_type);
  9387. return 1;
  9388. }
  9389. return 0;
  9390. }
  9391. /**
  9392. * lpfc_sli_check_eratt - check error attention events
  9393. * @phba: Pointer to HBA context.
  9394. *
  9395. * This function is called from timer soft interrupt context to check HBA's
  9396. * error attention register bit for error attention events.
  9397. *
  9398. * This function returns 1 when there is Error Attention in the Host Attention
  9399. * Register and returns 0 otherwise.
  9400. **/
  9401. int
  9402. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9403. {
  9404. uint32_t ha_copy;
  9405. /* If somebody is waiting to handle an eratt, don't process it
  9406. * here. The brdkill function will do this.
  9407. */
  9408. if (phba->link_flag & LS_IGNORE_ERATT)
  9409. return 0;
  9410. /* Check if interrupt handler handles this ERATT */
  9411. spin_lock_irq(&phba->hbalock);
  9412. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9413. /* Interrupt handler has handled ERATT */
  9414. spin_unlock_irq(&phba->hbalock);
  9415. return 0;
  9416. }
  9417. /*
  9418. * If there is deferred error attention, do not check for error
  9419. * attention
  9420. */
  9421. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9422. spin_unlock_irq(&phba->hbalock);
  9423. return 0;
  9424. }
  9425. /* If PCI channel is offline, don't process it */
  9426. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9427. spin_unlock_irq(&phba->hbalock);
  9428. return 0;
  9429. }
  9430. switch (phba->sli_rev) {
  9431. case LPFC_SLI_REV2:
  9432. case LPFC_SLI_REV3:
  9433. /* Read chip Host Attention (HA) register */
  9434. ha_copy = lpfc_sli_eratt_read(phba);
  9435. break;
  9436. case LPFC_SLI_REV4:
  9437. /* Read device Uncoverable Error (UERR) registers */
  9438. ha_copy = lpfc_sli4_eratt_read(phba);
  9439. break;
  9440. default:
  9441. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9442. "0299 Invalid SLI revision (%d)\n",
  9443. phba->sli_rev);
  9444. ha_copy = 0;
  9445. break;
  9446. }
  9447. spin_unlock_irq(&phba->hbalock);
  9448. return ha_copy;
  9449. }
  9450. /**
  9451. * lpfc_intr_state_check - Check device state for interrupt handling
  9452. * @phba: Pointer to HBA context.
  9453. *
  9454. * This inline routine checks whether a device or its PCI slot is in a state
  9455. * that the interrupt should be handled.
  9456. *
  9457. * This function returns 0 if the device or the PCI slot is in a state that
  9458. * interrupt should be handled, otherwise -EIO.
  9459. */
  9460. static inline int
  9461. lpfc_intr_state_check(struct lpfc_hba *phba)
  9462. {
  9463. /* If the pci channel is offline, ignore all the interrupts */
  9464. if (unlikely(pci_channel_offline(phba->pcidev)))
  9465. return -EIO;
  9466. /* Update device level interrupt statistics */
  9467. phba->sli.slistat.sli_intr++;
  9468. /* Ignore all interrupts during initialization. */
  9469. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9470. return -EIO;
  9471. return 0;
  9472. }
  9473. /**
  9474. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9475. * @irq: Interrupt number.
  9476. * @dev_id: The device context pointer.
  9477. *
  9478. * This function is directly called from the PCI layer as an interrupt
  9479. * service routine when device with SLI-3 interface spec is enabled with
  9480. * MSI-X multi-message interrupt mode and there are slow-path events in
  9481. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9482. * interrupt mode, this function is called as part of the device-level
  9483. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9484. * is undergoing initialization, the interrupt handler will not process
  9485. * the interrupt. The link attention and ELS ring attention events are
  9486. * handled by the worker thread. The interrupt handler signals the worker
  9487. * thread and returns for these events. This function is called without
  9488. * any lock held. It gets the hbalock to access and update SLI data
  9489. * structures.
  9490. *
  9491. * This function returns IRQ_HANDLED when interrupt is handled else it
  9492. * returns IRQ_NONE.
  9493. **/
  9494. irqreturn_t
  9495. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9496. {
  9497. struct lpfc_hba *phba;
  9498. uint32_t ha_copy, hc_copy;
  9499. uint32_t work_ha_copy;
  9500. unsigned long status;
  9501. unsigned long iflag;
  9502. uint32_t control;
  9503. MAILBOX_t *mbox, *pmbox;
  9504. struct lpfc_vport *vport;
  9505. struct lpfc_nodelist *ndlp;
  9506. struct lpfc_dmabuf *mp;
  9507. LPFC_MBOXQ_t *pmb;
  9508. int rc;
  9509. /*
  9510. * Get the driver's phba structure from the dev_id and
  9511. * assume the HBA is not interrupting.
  9512. */
  9513. phba = (struct lpfc_hba *)dev_id;
  9514. if (unlikely(!phba))
  9515. return IRQ_NONE;
  9516. /*
  9517. * Stuff needs to be attented to when this function is invoked as an
  9518. * individual interrupt handler in MSI-X multi-message interrupt mode
  9519. */
  9520. if (phba->intr_type == MSIX) {
  9521. /* Check device state for handling interrupt */
  9522. if (lpfc_intr_state_check(phba))
  9523. return IRQ_NONE;
  9524. /* Need to read HA REG for slow-path events */
  9525. spin_lock_irqsave(&phba->hbalock, iflag);
  9526. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9527. goto unplug_error;
  9528. /* If somebody is waiting to handle an eratt don't process it
  9529. * here. The brdkill function will do this.
  9530. */
  9531. if (phba->link_flag & LS_IGNORE_ERATT)
  9532. ha_copy &= ~HA_ERATT;
  9533. /* Check the need for handling ERATT in interrupt handler */
  9534. if (ha_copy & HA_ERATT) {
  9535. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9536. /* ERATT polling has handled ERATT */
  9537. ha_copy &= ~HA_ERATT;
  9538. else
  9539. /* Indicate interrupt handler handles ERATT */
  9540. phba->hba_flag |= HBA_ERATT_HANDLED;
  9541. }
  9542. /*
  9543. * If there is deferred error attention, do not check for any
  9544. * interrupt.
  9545. */
  9546. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9547. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9548. return IRQ_NONE;
  9549. }
  9550. /* Clear up only attention source related to slow-path */
  9551. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9552. goto unplug_error;
  9553. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9554. HC_LAINT_ENA | HC_ERINT_ENA),
  9555. phba->HCregaddr);
  9556. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9557. phba->HAregaddr);
  9558. writel(hc_copy, phba->HCregaddr);
  9559. readl(phba->HAregaddr); /* flush */
  9560. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9561. } else
  9562. ha_copy = phba->ha_copy;
  9563. work_ha_copy = ha_copy & phba->work_ha_mask;
  9564. if (work_ha_copy) {
  9565. if (work_ha_copy & HA_LATT) {
  9566. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9567. /*
  9568. * Turn off Link Attention interrupts
  9569. * until CLEAR_LA done
  9570. */
  9571. spin_lock_irqsave(&phba->hbalock, iflag);
  9572. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9573. if (lpfc_readl(phba->HCregaddr, &control))
  9574. goto unplug_error;
  9575. control &= ~HC_LAINT_ENA;
  9576. writel(control, phba->HCregaddr);
  9577. readl(phba->HCregaddr); /* flush */
  9578. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9579. }
  9580. else
  9581. work_ha_copy &= ~HA_LATT;
  9582. }
  9583. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9584. /*
  9585. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9586. * the only slow ring.
  9587. */
  9588. status = (work_ha_copy &
  9589. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9590. status >>= (4*LPFC_ELS_RING);
  9591. if (status & HA_RXMASK) {
  9592. spin_lock_irqsave(&phba->hbalock, iflag);
  9593. if (lpfc_readl(phba->HCregaddr, &control))
  9594. goto unplug_error;
  9595. lpfc_debugfs_slow_ring_trc(phba,
  9596. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9597. control, status,
  9598. (uint32_t)phba->sli.slistat.sli_intr);
  9599. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9600. lpfc_debugfs_slow_ring_trc(phba,
  9601. "ISR Disable ring:"
  9602. "pwork:x%x hawork:x%x wait:x%x",
  9603. phba->work_ha, work_ha_copy,
  9604. (uint32_t)((unsigned long)
  9605. &phba->work_waitq));
  9606. control &=
  9607. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9608. writel(control, phba->HCregaddr);
  9609. readl(phba->HCregaddr); /* flush */
  9610. }
  9611. else {
  9612. lpfc_debugfs_slow_ring_trc(phba,
  9613. "ISR slow ring: pwork:"
  9614. "x%x hawork:x%x wait:x%x",
  9615. phba->work_ha, work_ha_copy,
  9616. (uint32_t)((unsigned long)
  9617. &phba->work_waitq));
  9618. }
  9619. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9620. }
  9621. }
  9622. spin_lock_irqsave(&phba->hbalock, iflag);
  9623. if (work_ha_copy & HA_ERATT) {
  9624. if (lpfc_sli_read_hs(phba))
  9625. goto unplug_error;
  9626. /*
  9627. * Check if there is a deferred error condition
  9628. * is active
  9629. */
  9630. if ((HS_FFER1 & phba->work_hs) &&
  9631. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9632. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9633. phba->work_hs)) {
  9634. phba->hba_flag |= DEFER_ERATT;
  9635. /* Clear all interrupt enable conditions */
  9636. writel(0, phba->HCregaddr);
  9637. readl(phba->HCregaddr);
  9638. }
  9639. }
  9640. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9641. pmb = phba->sli.mbox_active;
  9642. pmbox = &pmb->u.mb;
  9643. mbox = phba->mbox;
  9644. vport = pmb->vport;
  9645. /* First check out the status word */
  9646. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9647. if (pmbox->mbxOwner != OWN_HOST) {
  9648. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9649. /*
  9650. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9651. * mbxStatus <status>
  9652. */
  9653. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9654. LOG_SLI,
  9655. "(%d):0304 Stray Mailbox "
  9656. "Interrupt mbxCommand x%x "
  9657. "mbxStatus x%x\n",
  9658. (vport ? vport->vpi : 0),
  9659. pmbox->mbxCommand,
  9660. pmbox->mbxStatus);
  9661. /* clear mailbox attention bit */
  9662. work_ha_copy &= ~HA_MBATT;
  9663. } else {
  9664. phba->sli.mbox_active = NULL;
  9665. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9666. phba->last_completion_time = jiffies;
  9667. del_timer(&phba->sli.mbox_tmo);
  9668. if (pmb->mbox_cmpl) {
  9669. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9670. MAILBOX_CMD_SIZE);
  9671. if (pmb->out_ext_byte_len &&
  9672. pmb->context2)
  9673. lpfc_sli_pcimem_bcopy(
  9674. phba->mbox_ext,
  9675. pmb->context2,
  9676. pmb->out_ext_byte_len);
  9677. }
  9678. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9679. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9680. lpfc_debugfs_disc_trc(vport,
  9681. LPFC_DISC_TRC_MBOX_VPORT,
  9682. "MBOX dflt rpi: : "
  9683. "status:x%x rpi:x%x",
  9684. (uint32_t)pmbox->mbxStatus,
  9685. pmbox->un.varWords[0], 0);
  9686. if (!pmbox->mbxStatus) {
  9687. mp = (struct lpfc_dmabuf *)
  9688. (pmb->context1);
  9689. ndlp = (struct lpfc_nodelist *)
  9690. pmb->context2;
  9691. /* Reg_LOGIN of dflt RPI was
  9692. * successful. new lets get
  9693. * rid of the RPI using the
  9694. * same mbox buffer.
  9695. */
  9696. lpfc_unreg_login(phba,
  9697. vport->vpi,
  9698. pmbox->un.varWords[0],
  9699. pmb);
  9700. pmb->mbox_cmpl =
  9701. lpfc_mbx_cmpl_dflt_rpi;
  9702. pmb->context1 = mp;
  9703. pmb->context2 = ndlp;
  9704. pmb->vport = vport;
  9705. rc = lpfc_sli_issue_mbox(phba,
  9706. pmb,
  9707. MBX_NOWAIT);
  9708. if (rc != MBX_BUSY)
  9709. lpfc_printf_log(phba,
  9710. KERN_ERR,
  9711. LOG_MBOX | LOG_SLI,
  9712. "0350 rc should have"
  9713. "been MBX_BUSY\n");
  9714. if (rc != MBX_NOT_FINISHED)
  9715. goto send_current_mbox;
  9716. }
  9717. }
  9718. spin_lock_irqsave(
  9719. &phba->pport->work_port_lock,
  9720. iflag);
  9721. phba->pport->work_port_events &=
  9722. ~WORKER_MBOX_TMO;
  9723. spin_unlock_irqrestore(
  9724. &phba->pport->work_port_lock,
  9725. iflag);
  9726. lpfc_mbox_cmpl_put(phba, pmb);
  9727. }
  9728. } else
  9729. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9730. if ((work_ha_copy & HA_MBATT) &&
  9731. (phba->sli.mbox_active == NULL)) {
  9732. send_current_mbox:
  9733. /* Process next mailbox command if there is one */
  9734. do {
  9735. rc = lpfc_sli_issue_mbox(phba, NULL,
  9736. MBX_NOWAIT);
  9737. } while (rc == MBX_NOT_FINISHED);
  9738. if (rc != MBX_SUCCESS)
  9739. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9740. LOG_SLI, "0349 rc should be "
  9741. "MBX_SUCCESS\n");
  9742. }
  9743. spin_lock_irqsave(&phba->hbalock, iflag);
  9744. phba->work_ha |= work_ha_copy;
  9745. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9746. lpfc_worker_wake_up(phba);
  9747. }
  9748. return IRQ_HANDLED;
  9749. unplug_error:
  9750. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9751. return IRQ_HANDLED;
  9752. } /* lpfc_sli_sp_intr_handler */
  9753. /**
  9754. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9755. * @irq: Interrupt number.
  9756. * @dev_id: The device context pointer.
  9757. *
  9758. * This function is directly called from the PCI layer as an interrupt
  9759. * service routine when device with SLI-3 interface spec is enabled with
  9760. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9761. * ring event in the HBA. However, when the device is enabled with either
  9762. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9763. * device-level interrupt handler. When the PCI slot is in error recovery
  9764. * or the HBA is undergoing initialization, the interrupt handler will not
  9765. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9766. * the intrrupt context. This function is called without any lock held.
  9767. * It gets the hbalock to access and update SLI data structures.
  9768. *
  9769. * This function returns IRQ_HANDLED when interrupt is handled else it
  9770. * returns IRQ_NONE.
  9771. **/
  9772. irqreturn_t
  9773. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9774. {
  9775. struct lpfc_hba *phba;
  9776. uint32_t ha_copy;
  9777. unsigned long status;
  9778. unsigned long iflag;
  9779. /* Get the driver's phba structure from the dev_id and
  9780. * assume the HBA is not interrupting.
  9781. */
  9782. phba = (struct lpfc_hba *) dev_id;
  9783. if (unlikely(!phba))
  9784. return IRQ_NONE;
  9785. /*
  9786. * Stuff needs to be attented to when this function is invoked as an
  9787. * individual interrupt handler in MSI-X multi-message interrupt mode
  9788. */
  9789. if (phba->intr_type == MSIX) {
  9790. /* Check device state for handling interrupt */
  9791. if (lpfc_intr_state_check(phba))
  9792. return IRQ_NONE;
  9793. /* Need to read HA REG for FCP ring and other ring events */
  9794. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9795. return IRQ_HANDLED;
  9796. /* Clear up only attention source related to fast-path */
  9797. spin_lock_irqsave(&phba->hbalock, iflag);
  9798. /*
  9799. * If there is deferred error attention, do not check for
  9800. * any interrupt.
  9801. */
  9802. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9803. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9804. return IRQ_NONE;
  9805. }
  9806. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9807. phba->HAregaddr);
  9808. readl(phba->HAregaddr); /* flush */
  9809. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9810. } else
  9811. ha_copy = phba->ha_copy;
  9812. /*
  9813. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9814. */
  9815. ha_copy &= ~(phba->work_ha_mask);
  9816. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9817. status >>= (4*LPFC_FCP_RING);
  9818. if (status & HA_RXMASK)
  9819. lpfc_sli_handle_fast_ring_event(phba,
  9820. &phba->sli.ring[LPFC_FCP_RING],
  9821. status);
  9822. if (phba->cfg_multi_ring_support == 2) {
  9823. /*
  9824. * Process all events on extra ring. Take the optimized path
  9825. * for extra ring IO.
  9826. */
  9827. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9828. status >>= (4*LPFC_EXTRA_RING);
  9829. if (status & HA_RXMASK) {
  9830. lpfc_sli_handle_fast_ring_event(phba,
  9831. &phba->sli.ring[LPFC_EXTRA_RING],
  9832. status);
  9833. }
  9834. }
  9835. return IRQ_HANDLED;
  9836. } /* lpfc_sli_fp_intr_handler */
  9837. /**
  9838. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9839. * @irq: Interrupt number.
  9840. * @dev_id: The device context pointer.
  9841. *
  9842. * This function is the HBA device-level interrupt handler to device with
  9843. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9844. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9845. * requires driver attention. This function invokes the slow-path interrupt
  9846. * attention handling function and fast-path interrupt attention handling
  9847. * function in turn to process the relevant HBA attention events. This
  9848. * function is called without any lock held. It gets the hbalock to access
  9849. * and update SLI data structures.
  9850. *
  9851. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9852. * returns IRQ_NONE.
  9853. **/
  9854. irqreturn_t
  9855. lpfc_sli_intr_handler(int irq, void *dev_id)
  9856. {
  9857. struct lpfc_hba *phba;
  9858. irqreturn_t sp_irq_rc, fp_irq_rc;
  9859. unsigned long status1, status2;
  9860. uint32_t hc_copy;
  9861. /*
  9862. * Get the driver's phba structure from the dev_id and
  9863. * assume the HBA is not interrupting.
  9864. */
  9865. phba = (struct lpfc_hba *) dev_id;
  9866. if (unlikely(!phba))
  9867. return IRQ_NONE;
  9868. /* Check device state for handling interrupt */
  9869. if (lpfc_intr_state_check(phba))
  9870. return IRQ_NONE;
  9871. spin_lock(&phba->hbalock);
  9872. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9873. spin_unlock(&phba->hbalock);
  9874. return IRQ_HANDLED;
  9875. }
  9876. if (unlikely(!phba->ha_copy)) {
  9877. spin_unlock(&phba->hbalock);
  9878. return IRQ_NONE;
  9879. } else if (phba->ha_copy & HA_ERATT) {
  9880. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9881. /* ERATT polling has handled ERATT */
  9882. phba->ha_copy &= ~HA_ERATT;
  9883. else
  9884. /* Indicate interrupt handler handles ERATT */
  9885. phba->hba_flag |= HBA_ERATT_HANDLED;
  9886. }
  9887. /*
  9888. * If there is deferred error attention, do not check for any interrupt.
  9889. */
  9890. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9891. spin_unlock(&phba->hbalock);
  9892. return IRQ_NONE;
  9893. }
  9894. /* Clear attention sources except link and error attentions */
  9895. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9896. spin_unlock(&phba->hbalock);
  9897. return IRQ_HANDLED;
  9898. }
  9899. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9900. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9901. phba->HCregaddr);
  9902. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9903. writel(hc_copy, phba->HCregaddr);
  9904. readl(phba->HAregaddr); /* flush */
  9905. spin_unlock(&phba->hbalock);
  9906. /*
  9907. * Invokes slow-path host attention interrupt handling as appropriate.
  9908. */
  9909. /* status of events with mailbox and link attention */
  9910. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9911. /* status of events with ELS ring */
  9912. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9913. status2 >>= (4*LPFC_ELS_RING);
  9914. if (status1 || (status2 & HA_RXMASK))
  9915. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9916. else
  9917. sp_irq_rc = IRQ_NONE;
  9918. /*
  9919. * Invoke fast-path host attention interrupt handling as appropriate.
  9920. */
  9921. /* status of events with FCP ring */
  9922. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9923. status1 >>= (4*LPFC_FCP_RING);
  9924. /* status of events with extra ring */
  9925. if (phba->cfg_multi_ring_support == 2) {
  9926. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9927. status2 >>= (4*LPFC_EXTRA_RING);
  9928. } else
  9929. status2 = 0;
  9930. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9931. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9932. else
  9933. fp_irq_rc = IRQ_NONE;
  9934. /* Return device-level interrupt handling status */
  9935. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9936. } /* lpfc_sli_intr_handler */
  9937. /**
  9938. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9939. * @phba: pointer to lpfc hba data structure.
  9940. *
  9941. * This routine is invoked by the worker thread to process all the pending
  9942. * SLI4 FCP abort XRI events.
  9943. **/
  9944. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9945. {
  9946. struct lpfc_cq_event *cq_event;
  9947. /* First, declare the fcp xri abort event has been handled */
  9948. spin_lock_irq(&phba->hbalock);
  9949. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9950. spin_unlock_irq(&phba->hbalock);
  9951. /* Now, handle all the fcp xri abort events */
  9952. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9953. /* Get the first event from the head of the event queue */
  9954. spin_lock_irq(&phba->hbalock);
  9955. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9956. cq_event, struct lpfc_cq_event, list);
  9957. spin_unlock_irq(&phba->hbalock);
  9958. /* Notify aborted XRI for FCP work queue */
  9959. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9960. /* Free the event processed back to the free pool */
  9961. lpfc_sli4_cq_event_release(phba, cq_event);
  9962. }
  9963. }
  9964. /**
  9965. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9966. * @phba: pointer to lpfc hba data structure.
  9967. *
  9968. * This routine is invoked by the worker thread to process all the pending
  9969. * SLI4 els abort xri events.
  9970. **/
  9971. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9972. {
  9973. struct lpfc_cq_event *cq_event;
  9974. /* First, declare the els xri abort event has been handled */
  9975. spin_lock_irq(&phba->hbalock);
  9976. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9977. spin_unlock_irq(&phba->hbalock);
  9978. /* Now, handle all the els xri abort events */
  9979. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9980. /* Get the first event from the head of the event queue */
  9981. spin_lock_irq(&phba->hbalock);
  9982. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9983. cq_event, struct lpfc_cq_event, list);
  9984. spin_unlock_irq(&phba->hbalock);
  9985. /* Notify aborted XRI for ELS work queue */
  9986. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9987. /* Free the event processed back to the free pool */
  9988. lpfc_sli4_cq_event_release(phba, cq_event);
  9989. }
  9990. }
  9991. /**
  9992. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9993. * @phba: pointer to lpfc hba data structure
  9994. * @pIocbIn: pointer to the rspiocbq
  9995. * @pIocbOut: pointer to the cmdiocbq
  9996. * @wcqe: pointer to the complete wcqe
  9997. *
  9998. * This routine transfers the fields of a command iocbq to a response iocbq
  9999. * by copying all the IOCB fields from command iocbq and transferring the
  10000. * completion status information from the complete wcqe.
  10001. **/
  10002. static void
  10003. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  10004. struct lpfc_iocbq *pIocbIn,
  10005. struct lpfc_iocbq *pIocbOut,
  10006. struct lpfc_wcqe_complete *wcqe)
  10007. {
  10008. unsigned long iflags;
  10009. uint32_t status;
  10010. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  10011. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  10012. sizeof(struct lpfc_iocbq) - offset);
  10013. /* Map WCQE parameters into irspiocb parameters */
  10014. status = bf_get(lpfc_wcqe_c_status, wcqe);
  10015. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  10016. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  10017. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  10018. pIocbIn->iocb.un.fcpi.fcpi_parm =
  10019. pIocbOut->iocb.un.fcpi.fcpi_parm -
  10020. wcqe->total_data_placed;
  10021. else
  10022. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10023. else {
  10024. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10025. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  10026. }
  10027. /* Convert BG errors for completion status */
  10028. if (status == CQE_STATUS_DI_ERROR) {
  10029. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  10030. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  10031. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  10032. else
  10033. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  10034. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  10035. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  10036. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10037. BGS_GUARD_ERR_MASK;
  10038. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  10039. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10040. BGS_APPTAG_ERR_MASK;
  10041. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  10042. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10043. BGS_REFTAG_ERR_MASK;
  10044. /* Check to see if there was any good data before the error */
  10045. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  10046. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10047. BGS_HI_WATER_MARK_PRESENT_MASK;
  10048. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  10049. wcqe->total_data_placed;
  10050. }
  10051. /*
  10052. * Set ALL the error bits to indicate we don't know what
  10053. * type of error it is.
  10054. */
  10055. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  10056. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10057. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  10058. BGS_GUARD_ERR_MASK);
  10059. }
  10060. /* Pick up HBA exchange busy condition */
  10061. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  10062. spin_lock_irqsave(&phba->hbalock, iflags);
  10063. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  10064. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10065. }
  10066. }
  10067. /**
  10068. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10069. * @phba: Pointer to HBA context object.
  10070. * @wcqe: Pointer to work-queue completion queue entry.
  10071. *
  10072. * This routine handles an ELS work-queue completion event and construct
  10073. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10074. * discovery engine to handle.
  10075. *
  10076. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10077. **/
  10078. static struct lpfc_iocbq *
  10079. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10080. struct lpfc_iocbq *irspiocbq)
  10081. {
  10082. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10083. struct lpfc_iocbq *cmdiocbq;
  10084. struct lpfc_wcqe_complete *wcqe;
  10085. unsigned long iflags;
  10086. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10087. spin_lock_irqsave(&pring->ring_lock, iflags);
  10088. pring->stats.iocb_event++;
  10089. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10090. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10091. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10092. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10093. if (unlikely(!cmdiocbq)) {
  10094. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10095. "0386 ELS complete with no corresponding "
  10096. "cmdiocb: iotag (%d)\n",
  10097. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10098. lpfc_sli_release_iocbq(phba, irspiocbq);
  10099. return NULL;
  10100. }
  10101. /* Fake the irspiocbq and copy necessary response information */
  10102. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10103. return irspiocbq;
  10104. }
  10105. /**
  10106. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10107. * @phba: Pointer to HBA context object.
  10108. * @cqe: Pointer to mailbox completion queue entry.
  10109. *
  10110. * This routine process a mailbox completion queue entry with asynchrous
  10111. * event.
  10112. *
  10113. * Return: true if work posted to worker thread, otherwise false.
  10114. **/
  10115. static bool
  10116. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10117. {
  10118. struct lpfc_cq_event *cq_event;
  10119. unsigned long iflags;
  10120. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10121. "0392 Async Event: word0:x%x, word1:x%x, "
  10122. "word2:x%x, word3:x%x\n", mcqe->word0,
  10123. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10124. /* Allocate a new internal CQ_EVENT entry */
  10125. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10126. if (!cq_event) {
  10127. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10128. "0394 Failed to allocate CQ_EVENT entry\n");
  10129. return false;
  10130. }
  10131. /* Move the CQE into an asynchronous event entry */
  10132. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10133. spin_lock_irqsave(&phba->hbalock, iflags);
  10134. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10135. /* Set the async event flag */
  10136. phba->hba_flag |= ASYNC_EVENT;
  10137. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10138. return true;
  10139. }
  10140. /**
  10141. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10142. * @phba: Pointer to HBA context object.
  10143. * @cqe: Pointer to mailbox completion queue entry.
  10144. *
  10145. * This routine process a mailbox completion queue entry with mailbox
  10146. * completion event.
  10147. *
  10148. * Return: true if work posted to worker thread, otherwise false.
  10149. **/
  10150. static bool
  10151. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10152. {
  10153. uint32_t mcqe_status;
  10154. MAILBOX_t *mbox, *pmbox;
  10155. struct lpfc_mqe *mqe;
  10156. struct lpfc_vport *vport;
  10157. struct lpfc_nodelist *ndlp;
  10158. struct lpfc_dmabuf *mp;
  10159. unsigned long iflags;
  10160. LPFC_MBOXQ_t *pmb;
  10161. bool workposted = false;
  10162. int rc;
  10163. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10164. if (!bf_get(lpfc_trailer_completed, mcqe))
  10165. goto out_no_mqe_complete;
  10166. /* Get the reference to the active mbox command */
  10167. spin_lock_irqsave(&phba->hbalock, iflags);
  10168. pmb = phba->sli.mbox_active;
  10169. if (unlikely(!pmb)) {
  10170. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10171. "1832 No pending MBOX command to handle\n");
  10172. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10173. goto out_no_mqe_complete;
  10174. }
  10175. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10176. mqe = &pmb->u.mqe;
  10177. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10178. mbox = phba->mbox;
  10179. vport = pmb->vport;
  10180. /* Reset heartbeat timer */
  10181. phba->last_completion_time = jiffies;
  10182. del_timer(&phba->sli.mbox_tmo);
  10183. /* Move mbox data to caller's mailbox region, do endian swapping */
  10184. if (pmb->mbox_cmpl && mbox)
  10185. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10186. /*
  10187. * For mcqe errors, conditionally move a modified error code to
  10188. * the mbox so that the error will not be missed.
  10189. */
  10190. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10191. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10192. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10193. bf_set(lpfc_mqe_status, mqe,
  10194. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10195. }
  10196. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10197. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10198. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10199. "MBOX dflt rpi: status:x%x rpi:x%x",
  10200. mcqe_status,
  10201. pmbox->un.varWords[0], 0);
  10202. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10203. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10204. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10205. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10206. * RID of the PPI using the same mbox buffer.
  10207. */
  10208. lpfc_unreg_login(phba, vport->vpi,
  10209. pmbox->un.varWords[0], pmb);
  10210. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10211. pmb->context1 = mp;
  10212. pmb->context2 = ndlp;
  10213. pmb->vport = vport;
  10214. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10215. if (rc != MBX_BUSY)
  10216. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10217. LOG_SLI, "0385 rc should "
  10218. "have been MBX_BUSY\n");
  10219. if (rc != MBX_NOT_FINISHED)
  10220. goto send_current_mbox;
  10221. }
  10222. }
  10223. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10224. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10225. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10226. /* There is mailbox completion work to do */
  10227. spin_lock_irqsave(&phba->hbalock, iflags);
  10228. __lpfc_mbox_cmpl_put(phba, pmb);
  10229. phba->work_ha |= HA_MBATT;
  10230. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10231. workposted = true;
  10232. send_current_mbox:
  10233. spin_lock_irqsave(&phba->hbalock, iflags);
  10234. /* Release the mailbox command posting token */
  10235. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10236. /* Setting active mailbox pointer need to be in sync to flag clear */
  10237. phba->sli.mbox_active = NULL;
  10238. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10239. /* Wake up worker thread to post the next pending mailbox command */
  10240. lpfc_worker_wake_up(phba);
  10241. out_no_mqe_complete:
  10242. if (bf_get(lpfc_trailer_consumed, mcqe))
  10243. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10244. return workposted;
  10245. }
  10246. /**
  10247. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10248. * @phba: Pointer to HBA context object.
  10249. * @cqe: Pointer to mailbox completion queue entry.
  10250. *
  10251. * This routine process a mailbox completion queue entry, it invokes the
  10252. * proper mailbox complete handling or asynchrous event handling routine
  10253. * according to the MCQE's async bit.
  10254. *
  10255. * Return: true if work posted to worker thread, otherwise false.
  10256. **/
  10257. static bool
  10258. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10259. {
  10260. struct lpfc_mcqe mcqe;
  10261. bool workposted;
  10262. /* Copy the mailbox MCQE and convert endian order as needed */
  10263. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10264. /* Invoke the proper event handling routine */
  10265. if (!bf_get(lpfc_trailer_async, &mcqe))
  10266. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10267. else
  10268. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10269. return workposted;
  10270. }
  10271. /**
  10272. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10273. * @phba: Pointer to HBA context object.
  10274. * @cq: Pointer to associated CQ
  10275. * @wcqe: Pointer to work-queue completion queue entry.
  10276. *
  10277. * This routine handles an ELS work-queue completion event.
  10278. *
  10279. * Return: true if work posted to worker thread, otherwise false.
  10280. **/
  10281. static bool
  10282. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10283. struct lpfc_wcqe_complete *wcqe)
  10284. {
  10285. struct lpfc_iocbq *irspiocbq;
  10286. unsigned long iflags;
  10287. struct lpfc_sli_ring *pring = cq->pring;
  10288. /* Get an irspiocbq for later ELS response processing use */
  10289. irspiocbq = lpfc_sli_get_iocbq(phba);
  10290. if (!irspiocbq) {
  10291. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10292. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10293. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10294. pring->txq_cnt, phba->iocb_cnt,
  10295. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10296. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10297. return false;
  10298. }
  10299. /* Save off the slow-path queue event for work thread to process */
  10300. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10301. spin_lock_irqsave(&phba->hbalock, iflags);
  10302. list_add_tail(&irspiocbq->cq_event.list,
  10303. &phba->sli4_hba.sp_queue_event);
  10304. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10305. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10306. return true;
  10307. }
  10308. /**
  10309. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10310. * @phba: Pointer to HBA context object.
  10311. * @wcqe: Pointer to work-queue completion queue entry.
  10312. *
  10313. * This routine handles slow-path WQ entry comsumed event by invoking the
  10314. * proper WQ release routine to the slow-path WQ.
  10315. **/
  10316. static void
  10317. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10318. struct lpfc_wcqe_release *wcqe)
  10319. {
  10320. /* sanity check on queue memory */
  10321. if (unlikely(!phba->sli4_hba.els_wq))
  10322. return;
  10323. /* Check for the slow-path ELS work queue */
  10324. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10325. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10326. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10327. else
  10328. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10329. "2579 Slow-path wqe consume event carries "
  10330. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10331. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10332. phba->sli4_hba.els_wq->queue_id);
  10333. }
  10334. /**
  10335. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10336. * @phba: Pointer to HBA context object.
  10337. * @cq: Pointer to a WQ completion queue.
  10338. * @wcqe: Pointer to work-queue completion queue entry.
  10339. *
  10340. * This routine handles an XRI abort event.
  10341. *
  10342. * Return: true if work posted to worker thread, otherwise false.
  10343. **/
  10344. static bool
  10345. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10346. struct lpfc_queue *cq,
  10347. struct sli4_wcqe_xri_aborted *wcqe)
  10348. {
  10349. bool workposted = false;
  10350. struct lpfc_cq_event *cq_event;
  10351. unsigned long iflags;
  10352. /* Allocate a new internal CQ_EVENT entry */
  10353. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10354. if (!cq_event) {
  10355. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10356. "0602 Failed to allocate CQ_EVENT entry\n");
  10357. return false;
  10358. }
  10359. /* Move the CQE into the proper xri abort event list */
  10360. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10361. switch (cq->subtype) {
  10362. case LPFC_FCP:
  10363. spin_lock_irqsave(&phba->hbalock, iflags);
  10364. list_add_tail(&cq_event->list,
  10365. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10366. /* Set the fcp xri abort event flag */
  10367. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10368. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10369. workposted = true;
  10370. break;
  10371. case LPFC_ELS:
  10372. spin_lock_irqsave(&phba->hbalock, iflags);
  10373. list_add_tail(&cq_event->list,
  10374. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10375. /* Set the els xri abort event flag */
  10376. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10377. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10378. workposted = true;
  10379. break;
  10380. default:
  10381. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10382. "0603 Invalid work queue CQE subtype (x%x)\n",
  10383. cq->subtype);
  10384. workposted = false;
  10385. break;
  10386. }
  10387. return workposted;
  10388. }
  10389. /**
  10390. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10391. * @phba: Pointer to HBA context object.
  10392. * @rcqe: Pointer to receive-queue completion queue entry.
  10393. *
  10394. * This routine process a receive-queue completion queue entry.
  10395. *
  10396. * Return: true if work posted to worker thread, otherwise false.
  10397. **/
  10398. static bool
  10399. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10400. {
  10401. bool workposted = false;
  10402. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10403. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10404. struct hbq_dmabuf *dma_buf;
  10405. uint32_t status, rq_id;
  10406. unsigned long iflags;
  10407. /* sanity check on queue memory */
  10408. if (unlikely(!hrq) || unlikely(!drq))
  10409. return workposted;
  10410. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10411. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10412. else
  10413. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10414. if (rq_id != hrq->queue_id)
  10415. goto out;
  10416. status = bf_get(lpfc_rcqe_status, rcqe);
  10417. switch (status) {
  10418. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10419. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10420. "2537 Receive Frame Truncated!!\n");
  10421. hrq->RQ_buf_trunc++;
  10422. case FC_STATUS_RQ_SUCCESS:
  10423. lpfc_sli4_rq_release(hrq, drq);
  10424. spin_lock_irqsave(&phba->hbalock, iflags);
  10425. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10426. if (!dma_buf) {
  10427. hrq->RQ_no_buf_found++;
  10428. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10429. goto out;
  10430. }
  10431. hrq->RQ_rcv_buf++;
  10432. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10433. /* save off the frame for the word thread to process */
  10434. list_add_tail(&dma_buf->cq_event.list,
  10435. &phba->sli4_hba.sp_queue_event);
  10436. /* Frame received */
  10437. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10438. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10439. workposted = true;
  10440. break;
  10441. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10442. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10443. hrq->RQ_no_posted_buf++;
  10444. /* Post more buffers if possible */
  10445. spin_lock_irqsave(&phba->hbalock, iflags);
  10446. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10447. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10448. workposted = true;
  10449. break;
  10450. }
  10451. out:
  10452. return workposted;
  10453. }
  10454. /**
  10455. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10456. * @phba: Pointer to HBA context object.
  10457. * @cq: Pointer to the completion queue.
  10458. * @wcqe: Pointer to a completion queue entry.
  10459. *
  10460. * This routine process a slow-path work-queue or receive queue completion queue
  10461. * entry.
  10462. *
  10463. * Return: true if work posted to worker thread, otherwise false.
  10464. **/
  10465. static bool
  10466. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10467. struct lpfc_cqe *cqe)
  10468. {
  10469. struct lpfc_cqe cqevt;
  10470. bool workposted = false;
  10471. /* Copy the work queue CQE and convert endian order if needed */
  10472. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10473. /* Check and process for different type of WCQE and dispatch */
  10474. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10475. case CQE_CODE_COMPL_WQE:
  10476. /* Process the WQ/RQ complete event */
  10477. phba->last_completion_time = jiffies;
  10478. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  10479. (struct lpfc_wcqe_complete *)&cqevt);
  10480. break;
  10481. case CQE_CODE_RELEASE_WQE:
  10482. /* Process the WQ release event */
  10483. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10484. (struct lpfc_wcqe_release *)&cqevt);
  10485. break;
  10486. case CQE_CODE_XRI_ABORTED:
  10487. /* Process the WQ XRI abort event */
  10488. phba->last_completion_time = jiffies;
  10489. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10490. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10491. break;
  10492. case CQE_CODE_RECEIVE:
  10493. case CQE_CODE_RECEIVE_V1:
  10494. /* Process the RQ event */
  10495. phba->last_completion_time = jiffies;
  10496. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10497. (struct lpfc_rcqe *)&cqevt);
  10498. break;
  10499. default:
  10500. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10501. "0388 Not a valid WCQE code: x%x\n",
  10502. bf_get(lpfc_cqe_code, &cqevt));
  10503. break;
  10504. }
  10505. return workposted;
  10506. }
  10507. /**
  10508. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10509. * @phba: Pointer to HBA context object.
  10510. * @eqe: Pointer to fast-path event queue entry.
  10511. *
  10512. * This routine process a event queue entry from the slow-path event queue.
  10513. * It will check the MajorCode and MinorCode to determine this is for a
  10514. * completion event on a completion queue, if not, an error shall be logged
  10515. * and just return. Otherwise, it will get to the corresponding completion
  10516. * queue and process all the entries on that completion queue, rearm the
  10517. * completion queue, and then return.
  10518. *
  10519. **/
  10520. static void
  10521. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10522. struct lpfc_queue *speq)
  10523. {
  10524. struct lpfc_queue *cq = NULL, *childq;
  10525. struct lpfc_cqe *cqe;
  10526. bool workposted = false;
  10527. int ecount = 0;
  10528. uint16_t cqid;
  10529. /* Get the reference to the corresponding CQ */
  10530. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10531. list_for_each_entry(childq, &speq->child_list, list) {
  10532. if (childq->queue_id == cqid) {
  10533. cq = childq;
  10534. break;
  10535. }
  10536. }
  10537. if (unlikely(!cq)) {
  10538. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10539. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10540. "0365 Slow-path CQ identifier "
  10541. "(%d) does not exist\n", cqid);
  10542. return;
  10543. }
  10544. /* Process all the entries to the CQ */
  10545. switch (cq->type) {
  10546. case LPFC_MCQ:
  10547. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10548. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10549. if (!(++ecount % cq->entry_repost))
  10550. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10551. cq->CQ_mbox++;
  10552. }
  10553. break;
  10554. case LPFC_WCQ:
  10555. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10556. if (cq->subtype == LPFC_FCP)
  10557. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10558. cqe);
  10559. else
  10560. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10561. cqe);
  10562. if (!(++ecount % cq->entry_repost))
  10563. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10564. }
  10565. /* Track the max number of CQEs processed in 1 EQ */
  10566. if (ecount > cq->CQ_max_cqe)
  10567. cq->CQ_max_cqe = ecount;
  10568. break;
  10569. default:
  10570. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10571. "0370 Invalid completion queue type (%d)\n",
  10572. cq->type);
  10573. return;
  10574. }
  10575. /* Catch the no cq entry condition, log an error */
  10576. if (unlikely(ecount == 0))
  10577. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10578. "0371 No entry from the CQ: identifier "
  10579. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10580. /* In any case, flash and re-arm the RCQ */
  10581. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10582. /* wake up worker thread if there are works to be done */
  10583. if (workposted)
  10584. lpfc_worker_wake_up(phba);
  10585. }
  10586. /**
  10587. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10588. * @phba: Pointer to HBA context object.
  10589. * @cq: Pointer to associated CQ
  10590. * @wcqe: Pointer to work-queue completion queue entry.
  10591. *
  10592. * This routine process a fast-path work queue completion entry from fast-path
  10593. * event queue for FCP command response completion.
  10594. **/
  10595. static void
  10596. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10597. struct lpfc_wcqe_complete *wcqe)
  10598. {
  10599. struct lpfc_sli_ring *pring = cq->pring;
  10600. struct lpfc_iocbq *cmdiocbq;
  10601. struct lpfc_iocbq irspiocbq;
  10602. unsigned long iflags;
  10603. /* Check for response status */
  10604. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10605. /* If resource errors reported from HBA, reduce queue
  10606. * depth of the SCSI device.
  10607. */
  10608. if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10609. IOSTAT_LOCAL_REJECT)) &&
  10610. ((wcqe->parameter & IOERR_PARAM_MASK) ==
  10611. IOERR_NO_RESOURCES))
  10612. phba->lpfc_rampdown_queue_depth(phba);
  10613. /* Log the error status */
  10614. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10615. "0373 FCP complete error: status=x%x, "
  10616. "hw_status=x%x, total_data_specified=%d, "
  10617. "parameter=x%x, word3=x%x\n",
  10618. bf_get(lpfc_wcqe_c_status, wcqe),
  10619. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10620. wcqe->total_data_placed, wcqe->parameter,
  10621. wcqe->word3);
  10622. }
  10623. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10624. spin_lock_irqsave(&pring->ring_lock, iflags);
  10625. pring->stats.iocb_event++;
  10626. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10627. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10628. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10629. if (unlikely(!cmdiocbq)) {
  10630. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10631. "0374 FCP complete with no corresponding "
  10632. "cmdiocb: iotag (%d)\n",
  10633. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10634. return;
  10635. }
  10636. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10637. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10638. "0375 FCP cmdiocb not callback function "
  10639. "iotag: (%d)\n",
  10640. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10641. return;
  10642. }
  10643. /* Fake the irspiocb and copy necessary response information */
  10644. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10645. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10646. spin_lock_irqsave(&phba->hbalock, iflags);
  10647. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10648. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10649. }
  10650. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10651. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10652. }
  10653. /**
  10654. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10655. * @phba: Pointer to HBA context object.
  10656. * @cq: Pointer to completion queue.
  10657. * @wcqe: Pointer to work-queue completion queue entry.
  10658. *
  10659. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10660. * proper WQ release routine to the slow-path WQ.
  10661. **/
  10662. static void
  10663. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10664. struct lpfc_wcqe_release *wcqe)
  10665. {
  10666. struct lpfc_queue *childwq;
  10667. bool wqid_matched = false;
  10668. uint16_t fcp_wqid;
  10669. /* Check for fast-path FCP work queue release */
  10670. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10671. list_for_each_entry(childwq, &cq->child_list, list) {
  10672. if (childwq->queue_id == fcp_wqid) {
  10673. lpfc_sli4_wq_release(childwq,
  10674. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10675. wqid_matched = true;
  10676. break;
  10677. }
  10678. }
  10679. /* Report warning log message if no match found */
  10680. if (wqid_matched != true)
  10681. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10682. "2580 Fast-path wqe consume event carries "
  10683. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10684. }
  10685. /**
  10686. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10687. * @cq: Pointer to the completion queue.
  10688. * @eqe: Pointer to fast-path completion queue entry.
  10689. *
  10690. * This routine process a fast-path work queue completion entry from fast-path
  10691. * event queue for FCP command response completion.
  10692. **/
  10693. static int
  10694. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10695. struct lpfc_cqe *cqe)
  10696. {
  10697. struct lpfc_wcqe_release wcqe;
  10698. bool workposted = false;
  10699. /* Copy the work queue CQE and convert endian order if needed */
  10700. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10701. /* Check and process for different type of WCQE and dispatch */
  10702. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10703. case CQE_CODE_COMPL_WQE:
  10704. cq->CQ_wq++;
  10705. /* Process the WQ complete event */
  10706. phba->last_completion_time = jiffies;
  10707. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  10708. (struct lpfc_wcqe_complete *)&wcqe);
  10709. break;
  10710. case CQE_CODE_RELEASE_WQE:
  10711. cq->CQ_release_wqe++;
  10712. /* Process the WQ release event */
  10713. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10714. (struct lpfc_wcqe_release *)&wcqe);
  10715. break;
  10716. case CQE_CODE_XRI_ABORTED:
  10717. cq->CQ_xri_aborted++;
  10718. /* Process the WQ XRI abort event */
  10719. phba->last_completion_time = jiffies;
  10720. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10721. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10722. break;
  10723. default:
  10724. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10725. "0144 Not a valid WCQE code: x%x\n",
  10726. bf_get(lpfc_wcqe_c_code, &wcqe));
  10727. break;
  10728. }
  10729. return workposted;
  10730. }
  10731. /**
  10732. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  10733. * @phba: Pointer to HBA context object.
  10734. * @eqe: Pointer to fast-path event queue entry.
  10735. *
  10736. * This routine process a event queue entry from the fast-path event queue.
  10737. * It will check the MajorCode and MinorCode to determine this is for a
  10738. * completion event on a completion queue, if not, an error shall be logged
  10739. * and just return. Otherwise, it will get to the corresponding completion
  10740. * queue and process all the entries on the completion queue, rearm the
  10741. * completion queue, and then return.
  10742. **/
  10743. static void
  10744. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10745. uint32_t qidx)
  10746. {
  10747. struct lpfc_queue *cq;
  10748. struct lpfc_cqe *cqe;
  10749. bool workposted = false;
  10750. uint16_t cqid;
  10751. int ecount = 0;
  10752. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10753. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10754. "0366 Not a valid completion "
  10755. "event: majorcode=x%x, minorcode=x%x\n",
  10756. bf_get_le32(lpfc_eqe_major_code, eqe),
  10757. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10758. return;
  10759. }
  10760. /* Get the reference to the corresponding CQ */
  10761. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10762. /* Check if this is a Slow path event */
  10763. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  10764. lpfc_sli4_sp_handle_eqe(phba, eqe,
  10765. phba->sli4_hba.hba_eq[qidx]);
  10766. return;
  10767. }
  10768. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10769. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10770. "3146 Fast-path completion queues "
  10771. "does not exist\n");
  10772. return;
  10773. }
  10774. cq = phba->sli4_hba.fcp_cq[qidx];
  10775. if (unlikely(!cq)) {
  10776. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10777. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10778. "0367 Fast-path completion queue "
  10779. "(%d) does not exist\n", qidx);
  10780. return;
  10781. }
  10782. if (unlikely(cqid != cq->queue_id)) {
  10783. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10784. "0368 Miss-matched fast-path completion "
  10785. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10786. cqid, cq->queue_id);
  10787. return;
  10788. }
  10789. /* Process all the entries to the CQ */
  10790. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10791. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10792. if (!(++ecount % cq->entry_repost))
  10793. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10794. }
  10795. /* Track the max number of CQEs processed in 1 EQ */
  10796. if (ecount > cq->CQ_max_cqe)
  10797. cq->CQ_max_cqe = ecount;
  10798. /* Catch the no cq entry condition */
  10799. if (unlikely(ecount == 0))
  10800. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10801. "0369 No entry from fast-path completion "
  10802. "queue fcpcqid=%d\n", cq->queue_id);
  10803. /* In any case, flash and re-arm the CQ */
  10804. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10805. /* wake up worker thread if there are works to be done */
  10806. if (workposted)
  10807. lpfc_worker_wake_up(phba);
  10808. }
  10809. static void
  10810. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10811. {
  10812. struct lpfc_eqe *eqe;
  10813. /* walk all the EQ entries and drop on the floor */
  10814. while ((eqe = lpfc_sli4_eq_get(eq)))
  10815. ;
  10816. /* Clear and re-arm the EQ */
  10817. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10818. }
  10819. /**
  10820. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  10821. * @irq: Interrupt number.
  10822. * @dev_id: The device context pointer.
  10823. *
  10824. * This function is directly called from the PCI layer as an interrupt
  10825. * service routine when device with SLI-4 interface spec is enabled with
  10826. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10827. * ring event in the HBA. However, when the device is enabled with either
  10828. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10829. * device-level interrupt handler. When the PCI slot is in error recovery
  10830. * or the HBA is undergoing initialization, the interrupt handler will not
  10831. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10832. * the intrrupt context. This function is called without any lock held.
  10833. * It gets the hbalock to access and update SLI data structures. Note that,
  10834. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10835. * equal to that of FCP CQ index.
  10836. *
  10837. * The link attention and ELS ring attention events are handled
  10838. * by the worker thread. The interrupt handler signals the worker thread
  10839. * and returns for these events. This function is called without any lock
  10840. * held. It gets the hbalock to access and update SLI data structures.
  10841. *
  10842. * This function returns IRQ_HANDLED when interrupt is handled else it
  10843. * returns IRQ_NONE.
  10844. **/
  10845. irqreturn_t
  10846. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  10847. {
  10848. struct lpfc_hba *phba;
  10849. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10850. struct lpfc_queue *fpeq;
  10851. struct lpfc_eqe *eqe;
  10852. unsigned long iflag;
  10853. int ecount = 0;
  10854. int fcp_eqidx;
  10855. /* Get the driver's phba structure from the dev_id */
  10856. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10857. phba = fcp_eq_hdl->phba;
  10858. fcp_eqidx = fcp_eq_hdl->idx;
  10859. if (unlikely(!phba))
  10860. return IRQ_NONE;
  10861. if (unlikely(!phba->sli4_hba.hba_eq))
  10862. return IRQ_NONE;
  10863. /* Get to the EQ struct associated with this vector */
  10864. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  10865. if (unlikely(!fpeq))
  10866. return IRQ_NONE;
  10867. if (lpfc_fcp_look_ahead) {
  10868. if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
  10869. lpfc_sli4_eq_clr_intr(fpeq);
  10870. else {
  10871. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10872. return IRQ_NONE;
  10873. }
  10874. }
  10875. /* Check device state for handling interrupt */
  10876. if (unlikely(lpfc_intr_state_check(phba))) {
  10877. fpeq->EQ_badstate++;
  10878. /* Check again for link_state with lock held */
  10879. spin_lock_irqsave(&phba->hbalock, iflag);
  10880. if (phba->link_state < LPFC_LINK_DOWN)
  10881. /* Flush, clear interrupt, and rearm the EQ */
  10882. lpfc_sli4_eq_flush(phba, fpeq);
  10883. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10884. if (lpfc_fcp_look_ahead)
  10885. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10886. return IRQ_NONE;
  10887. }
  10888. /*
  10889. * Process all the event on FCP fast-path EQ
  10890. */
  10891. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10892. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  10893. if (!(++ecount % fpeq->entry_repost))
  10894. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10895. fpeq->EQ_processed++;
  10896. }
  10897. /* Track the max number of EQEs processed in 1 intr */
  10898. if (ecount > fpeq->EQ_max_eqe)
  10899. fpeq->EQ_max_eqe = ecount;
  10900. /* Always clear and re-arm the fast-path EQ */
  10901. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10902. if (unlikely(ecount == 0)) {
  10903. fpeq->EQ_no_entry++;
  10904. if (lpfc_fcp_look_ahead) {
  10905. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10906. return IRQ_NONE;
  10907. }
  10908. if (phba->intr_type == MSIX)
  10909. /* MSI-X treated interrupt served as no EQ share INT */
  10910. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10911. "0358 MSI-X interrupt with no EQE\n");
  10912. else
  10913. /* Non MSI-X treated on interrupt as EQ share INT */
  10914. return IRQ_NONE;
  10915. }
  10916. if (lpfc_fcp_look_ahead)
  10917. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10918. return IRQ_HANDLED;
  10919. } /* lpfc_sli4_fp_intr_handler */
  10920. /**
  10921. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10922. * @irq: Interrupt number.
  10923. * @dev_id: The device context pointer.
  10924. *
  10925. * This function is the device-level interrupt handler to device with SLI-4
  10926. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10927. * interrupt mode is enabled and there is an event in the HBA which requires
  10928. * driver attention. This function invokes the slow-path interrupt attention
  10929. * handling function and fast-path interrupt attention handling function in
  10930. * turn to process the relevant HBA attention events. This function is called
  10931. * without any lock held. It gets the hbalock to access and update SLI data
  10932. * structures.
  10933. *
  10934. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10935. * returns IRQ_NONE.
  10936. **/
  10937. irqreturn_t
  10938. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10939. {
  10940. struct lpfc_hba *phba;
  10941. irqreturn_t hba_irq_rc;
  10942. bool hba_handled = false;
  10943. int fcp_eqidx;
  10944. /* Get the driver's phba structure from the dev_id */
  10945. phba = (struct lpfc_hba *)dev_id;
  10946. if (unlikely(!phba))
  10947. return IRQ_NONE;
  10948. /*
  10949. * Invoke fast-path host attention interrupt handling as appropriate.
  10950. */
  10951. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  10952. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  10953. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10954. if (hba_irq_rc == IRQ_HANDLED)
  10955. hba_handled |= true;
  10956. }
  10957. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  10958. } /* lpfc_sli4_intr_handler */
  10959. /**
  10960. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10961. * @queue: The queue structure to free.
  10962. *
  10963. * This function frees a queue structure and the DMAable memory used for
  10964. * the host resident queue. This function must be called after destroying the
  10965. * queue on the HBA.
  10966. **/
  10967. void
  10968. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10969. {
  10970. struct lpfc_dmabuf *dmabuf;
  10971. if (!queue)
  10972. return;
  10973. while (!list_empty(&queue->page_list)) {
  10974. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10975. list);
  10976. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10977. dmabuf->virt, dmabuf->phys);
  10978. kfree(dmabuf);
  10979. }
  10980. kfree(queue);
  10981. return;
  10982. }
  10983. /**
  10984. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10985. * @phba: The HBA that this queue is being created on.
  10986. * @entry_size: The size of each queue entry for this queue.
  10987. * @entry count: The number of entries that this queue will handle.
  10988. *
  10989. * This function allocates a queue structure and the DMAable memory used for
  10990. * the host resident queue. This function must be called before creating the
  10991. * queue on the HBA.
  10992. **/
  10993. struct lpfc_queue *
  10994. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10995. uint32_t entry_count)
  10996. {
  10997. struct lpfc_queue *queue;
  10998. struct lpfc_dmabuf *dmabuf;
  10999. int x, total_qe_count;
  11000. void *dma_pointer;
  11001. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11002. if (!phba->sli4_hba.pc_sli4_params.supported)
  11003. hw_page_size = SLI4_PAGE_SIZE;
  11004. queue = kzalloc(sizeof(struct lpfc_queue) +
  11005. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  11006. if (!queue)
  11007. return NULL;
  11008. queue->page_count = (ALIGN(entry_size * entry_count,
  11009. hw_page_size))/hw_page_size;
  11010. INIT_LIST_HEAD(&queue->list);
  11011. INIT_LIST_HEAD(&queue->page_list);
  11012. INIT_LIST_HEAD(&queue->child_list);
  11013. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  11014. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  11015. if (!dmabuf)
  11016. goto out_fail;
  11017. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  11018. hw_page_size, &dmabuf->phys,
  11019. GFP_KERNEL);
  11020. if (!dmabuf->virt) {
  11021. kfree(dmabuf);
  11022. goto out_fail;
  11023. }
  11024. memset(dmabuf->virt, 0, hw_page_size);
  11025. dmabuf->buffer_tag = x;
  11026. list_add_tail(&dmabuf->list, &queue->page_list);
  11027. /* initialize queue's entry array */
  11028. dma_pointer = dmabuf->virt;
  11029. for (; total_qe_count < entry_count &&
  11030. dma_pointer < (hw_page_size + dmabuf->virt);
  11031. total_qe_count++, dma_pointer += entry_size) {
  11032. queue->qe[total_qe_count].address = dma_pointer;
  11033. }
  11034. }
  11035. queue->entry_size = entry_size;
  11036. queue->entry_count = entry_count;
  11037. /*
  11038. * entry_repost is calculated based on the number of entries in the
  11039. * queue. This works out except for RQs. If buffers are NOT initially
  11040. * posted for every RQE, entry_repost should be adjusted accordingly.
  11041. */
  11042. queue->entry_repost = (entry_count >> 3);
  11043. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11044. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11045. queue->phba = phba;
  11046. return queue;
  11047. out_fail:
  11048. lpfc_sli4_queue_free(queue);
  11049. return NULL;
  11050. }
  11051. /**
  11052. * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
  11053. * @phba: HBA structure that indicates port to create a queue on.
  11054. * @pci_barset: PCI BAR set flag.
  11055. *
  11056. * This function shall perform iomap of the specified PCI BAR address to host
  11057. * memory address if not already done so and return it. The returned host
  11058. * memory address can be NULL.
  11059. */
  11060. static void __iomem *
  11061. lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
  11062. {
  11063. struct pci_dev *pdev;
  11064. unsigned long bar_map, bar_map_len;
  11065. if (!phba->pcidev)
  11066. return NULL;
  11067. else
  11068. pdev = phba->pcidev;
  11069. switch (pci_barset) {
  11070. case WQ_PCI_BAR_0_AND_1:
  11071. if (!phba->pci_bar0_memmap_p) {
  11072. bar_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
  11073. bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
  11074. phba->pci_bar0_memmap_p = ioremap(bar_map, bar_map_len);
  11075. }
  11076. return phba->pci_bar0_memmap_p;
  11077. case WQ_PCI_BAR_2_AND_3:
  11078. if (!phba->pci_bar2_memmap_p) {
  11079. bar_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
  11080. bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
  11081. phba->pci_bar2_memmap_p = ioremap(bar_map, bar_map_len);
  11082. }
  11083. return phba->pci_bar2_memmap_p;
  11084. case WQ_PCI_BAR_4_AND_5:
  11085. if (!phba->pci_bar4_memmap_p) {
  11086. bar_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
  11087. bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
  11088. phba->pci_bar4_memmap_p = ioremap(bar_map, bar_map_len);
  11089. }
  11090. return phba->pci_bar4_memmap_p;
  11091. default:
  11092. break;
  11093. }
  11094. return NULL;
  11095. }
  11096. /**
  11097. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11098. * @phba: HBA structure that indicates port to create a queue on.
  11099. * @startq: The starting FCP EQ to modify
  11100. *
  11101. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11102. *
  11103. * The @phba struct is used to send mailbox command to HBA. The @startq
  11104. * is used to get the starting FCP EQ to change.
  11105. * This function is asynchronous and will wait for the mailbox
  11106. * command to finish before continuing.
  11107. *
  11108. * On success this function will return a zero. If unable to allocate enough
  11109. * memory this function will return -ENOMEM. If the queue create mailbox command
  11110. * fails this function will return -ENXIO.
  11111. **/
  11112. uint32_t
  11113. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  11114. {
  11115. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11116. LPFC_MBOXQ_t *mbox;
  11117. struct lpfc_queue *eq;
  11118. int cnt, rc, length, status = 0;
  11119. uint32_t shdr_status, shdr_add_status;
  11120. uint32_t result;
  11121. int fcp_eqidx;
  11122. union lpfc_sli4_cfg_shdr *shdr;
  11123. uint16_t dmult;
  11124. if (startq >= phba->cfg_fcp_io_channel)
  11125. return 0;
  11126. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11127. if (!mbox)
  11128. return -ENOMEM;
  11129. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11130. sizeof(struct lpfc_sli4_cfg_mhdr));
  11131. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11132. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11133. length, LPFC_SLI4_MBX_EMBED);
  11134. eq_delay = &mbox->u.mqe.un.eq_delay;
  11135. /* Calculate delay multiper from maximum interrupt per second */
  11136. result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11137. if (result > LPFC_DMULT_CONST)
  11138. dmult = 0;
  11139. else
  11140. dmult = LPFC_DMULT_CONST/result - 1;
  11141. cnt = 0;
  11142. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11143. fcp_eqidx++) {
  11144. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11145. if (!eq)
  11146. continue;
  11147. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11148. eq_delay->u.request.eq[cnt].phase = 0;
  11149. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11150. cnt++;
  11151. if (cnt >= LPFC_MAX_EQ_DELAY)
  11152. break;
  11153. }
  11154. eq_delay->u.request.num_eq = cnt;
  11155. mbox->vport = phba->pport;
  11156. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11157. mbox->context1 = NULL;
  11158. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11159. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11160. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11161. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11162. if (shdr_status || shdr_add_status || rc) {
  11163. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11164. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11165. "status x%x add_status x%x, mbx status x%x\n",
  11166. shdr_status, shdr_add_status, rc);
  11167. status = -ENXIO;
  11168. }
  11169. mempool_free(mbox, phba->mbox_mem_pool);
  11170. return status;
  11171. }
  11172. /**
  11173. * lpfc_eq_create - Create an Event Queue on the HBA
  11174. * @phba: HBA structure that indicates port to create a queue on.
  11175. * @eq: The queue structure to use to create the event queue.
  11176. * @imax: The maximum interrupt per second limit.
  11177. *
  11178. * This function creates an event queue, as detailed in @eq, on a port,
  11179. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11180. *
  11181. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11182. * is used to get the entry count and entry size that are necessary to
  11183. * determine the number of pages to allocate and use for this queue. This
  11184. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11185. * event queue. This function is asynchronous and will wait for the mailbox
  11186. * command to finish before continuing.
  11187. *
  11188. * On success this function will return a zero. If unable to allocate enough
  11189. * memory this function will return -ENOMEM. If the queue create mailbox command
  11190. * fails this function will return -ENXIO.
  11191. **/
  11192. uint32_t
  11193. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
  11194. {
  11195. struct lpfc_mbx_eq_create *eq_create;
  11196. LPFC_MBOXQ_t *mbox;
  11197. int rc, length, status = 0;
  11198. struct lpfc_dmabuf *dmabuf;
  11199. uint32_t shdr_status, shdr_add_status;
  11200. union lpfc_sli4_cfg_shdr *shdr;
  11201. uint16_t dmult;
  11202. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11203. /* sanity check on queue memory */
  11204. if (!eq)
  11205. return -ENODEV;
  11206. if (!phba->sli4_hba.pc_sli4_params.supported)
  11207. hw_page_size = SLI4_PAGE_SIZE;
  11208. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11209. if (!mbox)
  11210. return -ENOMEM;
  11211. length = (sizeof(struct lpfc_mbx_eq_create) -
  11212. sizeof(struct lpfc_sli4_cfg_mhdr));
  11213. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11214. LPFC_MBOX_OPCODE_EQ_CREATE,
  11215. length, LPFC_SLI4_MBX_EMBED);
  11216. eq_create = &mbox->u.mqe.un.eq_create;
  11217. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11218. eq->page_count);
  11219. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11220. LPFC_EQE_SIZE);
  11221. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11222. /* Calculate delay multiper from maximum interrupt per second */
  11223. if (imax > LPFC_DMULT_CONST)
  11224. dmult = 0;
  11225. else
  11226. dmult = LPFC_DMULT_CONST/imax - 1;
  11227. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11228. dmult);
  11229. switch (eq->entry_count) {
  11230. default:
  11231. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11232. "0360 Unsupported EQ count. (%d)\n",
  11233. eq->entry_count);
  11234. if (eq->entry_count < 256)
  11235. return -EINVAL;
  11236. /* otherwise default to smallest count (drop through) */
  11237. case 256:
  11238. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11239. LPFC_EQ_CNT_256);
  11240. break;
  11241. case 512:
  11242. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11243. LPFC_EQ_CNT_512);
  11244. break;
  11245. case 1024:
  11246. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11247. LPFC_EQ_CNT_1024);
  11248. break;
  11249. case 2048:
  11250. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11251. LPFC_EQ_CNT_2048);
  11252. break;
  11253. case 4096:
  11254. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11255. LPFC_EQ_CNT_4096);
  11256. break;
  11257. }
  11258. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11259. memset(dmabuf->virt, 0, hw_page_size);
  11260. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11261. putPaddrLow(dmabuf->phys);
  11262. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11263. putPaddrHigh(dmabuf->phys);
  11264. }
  11265. mbox->vport = phba->pport;
  11266. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11267. mbox->context1 = NULL;
  11268. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11269. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11270. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11271. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11272. if (shdr_status || shdr_add_status || rc) {
  11273. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11274. "2500 EQ_CREATE mailbox failed with "
  11275. "status x%x add_status x%x, mbx status x%x\n",
  11276. shdr_status, shdr_add_status, rc);
  11277. status = -ENXIO;
  11278. }
  11279. eq->type = LPFC_EQ;
  11280. eq->subtype = LPFC_NONE;
  11281. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11282. if (eq->queue_id == 0xFFFF)
  11283. status = -ENXIO;
  11284. eq->host_index = 0;
  11285. eq->hba_index = 0;
  11286. mempool_free(mbox, phba->mbox_mem_pool);
  11287. return status;
  11288. }
  11289. /**
  11290. * lpfc_cq_create - Create a Completion Queue on the HBA
  11291. * @phba: HBA structure that indicates port to create a queue on.
  11292. * @cq: The queue structure to use to create the completion queue.
  11293. * @eq: The event queue to bind this completion queue to.
  11294. *
  11295. * This function creates a completion queue, as detailed in @wq, on a port,
  11296. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11297. *
  11298. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11299. * is used to get the entry count and entry size that are necessary to
  11300. * determine the number of pages to allocate and use for this queue. The @eq
  11301. * is used to indicate which event queue to bind this completion queue to. This
  11302. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11303. * completion queue. This function is asynchronous and will wait for the mailbox
  11304. * command to finish before continuing.
  11305. *
  11306. * On success this function will return a zero. If unable to allocate enough
  11307. * memory this function will return -ENOMEM. If the queue create mailbox command
  11308. * fails this function will return -ENXIO.
  11309. **/
  11310. uint32_t
  11311. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11312. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11313. {
  11314. struct lpfc_mbx_cq_create *cq_create;
  11315. struct lpfc_dmabuf *dmabuf;
  11316. LPFC_MBOXQ_t *mbox;
  11317. int rc, length, status = 0;
  11318. uint32_t shdr_status, shdr_add_status;
  11319. union lpfc_sli4_cfg_shdr *shdr;
  11320. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11321. /* sanity check on queue memory */
  11322. if (!cq || !eq)
  11323. return -ENODEV;
  11324. if (!phba->sli4_hba.pc_sli4_params.supported)
  11325. hw_page_size = SLI4_PAGE_SIZE;
  11326. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11327. if (!mbox)
  11328. return -ENOMEM;
  11329. length = (sizeof(struct lpfc_mbx_cq_create) -
  11330. sizeof(struct lpfc_sli4_cfg_mhdr));
  11331. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11332. LPFC_MBOX_OPCODE_CQ_CREATE,
  11333. length, LPFC_SLI4_MBX_EMBED);
  11334. cq_create = &mbox->u.mqe.un.cq_create;
  11335. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11336. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11337. cq->page_count);
  11338. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11339. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11340. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11341. phba->sli4_hba.pc_sli4_params.cqv);
  11342. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11343. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11344. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11345. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11346. eq->queue_id);
  11347. } else {
  11348. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11349. eq->queue_id);
  11350. }
  11351. switch (cq->entry_count) {
  11352. default:
  11353. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11354. "0361 Unsupported CQ count. (%d)\n",
  11355. cq->entry_count);
  11356. if (cq->entry_count < 256) {
  11357. status = -EINVAL;
  11358. goto out;
  11359. }
  11360. /* otherwise default to smallest count (drop through) */
  11361. case 256:
  11362. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11363. LPFC_CQ_CNT_256);
  11364. break;
  11365. case 512:
  11366. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11367. LPFC_CQ_CNT_512);
  11368. break;
  11369. case 1024:
  11370. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11371. LPFC_CQ_CNT_1024);
  11372. break;
  11373. }
  11374. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11375. memset(dmabuf->virt, 0, hw_page_size);
  11376. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11377. putPaddrLow(dmabuf->phys);
  11378. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11379. putPaddrHigh(dmabuf->phys);
  11380. }
  11381. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11382. /* The IOCTL status is embedded in the mailbox subheader. */
  11383. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11384. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11385. if (shdr_status || shdr_add_status || rc) {
  11386. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11387. "2501 CQ_CREATE mailbox failed with "
  11388. "status x%x add_status x%x, mbx status x%x\n",
  11389. shdr_status, shdr_add_status, rc);
  11390. status = -ENXIO;
  11391. goto out;
  11392. }
  11393. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11394. if (cq->queue_id == 0xFFFF) {
  11395. status = -ENXIO;
  11396. goto out;
  11397. }
  11398. /* link the cq onto the parent eq child list */
  11399. list_add_tail(&cq->list, &eq->child_list);
  11400. /* Set up completion queue's type and subtype */
  11401. cq->type = type;
  11402. cq->subtype = subtype;
  11403. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11404. cq->assoc_qid = eq->queue_id;
  11405. cq->host_index = 0;
  11406. cq->hba_index = 0;
  11407. out:
  11408. mempool_free(mbox, phba->mbox_mem_pool);
  11409. return status;
  11410. }
  11411. /**
  11412. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11413. * @phba: HBA structure that indicates port to create a queue on.
  11414. * @mq: The queue structure to use to create the mailbox queue.
  11415. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11416. * @cq: The completion queue to associate with this cq.
  11417. *
  11418. * This function provides failback (fb) functionality when the
  11419. * mq_create_ext fails on older FW generations. It's purpose is identical
  11420. * to mq_create_ext otherwise.
  11421. *
  11422. * This routine cannot fail as all attributes were previously accessed and
  11423. * initialized in mq_create_ext.
  11424. **/
  11425. static void
  11426. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11427. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11428. {
  11429. struct lpfc_mbx_mq_create *mq_create;
  11430. struct lpfc_dmabuf *dmabuf;
  11431. int length;
  11432. length = (sizeof(struct lpfc_mbx_mq_create) -
  11433. sizeof(struct lpfc_sli4_cfg_mhdr));
  11434. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11435. LPFC_MBOX_OPCODE_MQ_CREATE,
  11436. length, LPFC_SLI4_MBX_EMBED);
  11437. mq_create = &mbox->u.mqe.un.mq_create;
  11438. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11439. mq->page_count);
  11440. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11441. cq->queue_id);
  11442. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11443. switch (mq->entry_count) {
  11444. case 16:
  11445. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11446. LPFC_MQ_RING_SIZE_16);
  11447. break;
  11448. case 32:
  11449. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11450. LPFC_MQ_RING_SIZE_32);
  11451. break;
  11452. case 64:
  11453. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11454. LPFC_MQ_RING_SIZE_64);
  11455. break;
  11456. case 128:
  11457. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11458. LPFC_MQ_RING_SIZE_128);
  11459. break;
  11460. }
  11461. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11462. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11463. putPaddrLow(dmabuf->phys);
  11464. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11465. putPaddrHigh(dmabuf->phys);
  11466. }
  11467. }
  11468. /**
  11469. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11470. * @phba: HBA structure that indicates port to create a queue on.
  11471. * @mq: The queue structure to use to create the mailbox queue.
  11472. * @cq: The completion queue to associate with this cq.
  11473. * @subtype: The queue's subtype.
  11474. *
  11475. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11476. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11477. *
  11478. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11479. * is used to get the entry count and entry size that are necessary to
  11480. * determine the number of pages to allocate and use for this queue. This
  11481. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11482. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11483. * command to finish before continuing.
  11484. *
  11485. * On success this function will return a zero. If unable to allocate enough
  11486. * memory this function will return -ENOMEM. If the queue create mailbox command
  11487. * fails this function will return -ENXIO.
  11488. **/
  11489. int32_t
  11490. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11491. struct lpfc_queue *cq, uint32_t subtype)
  11492. {
  11493. struct lpfc_mbx_mq_create *mq_create;
  11494. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11495. struct lpfc_dmabuf *dmabuf;
  11496. LPFC_MBOXQ_t *mbox;
  11497. int rc, length, status = 0;
  11498. uint32_t shdr_status, shdr_add_status;
  11499. union lpfc_sli4_cfg_shdr *shdr;
  11500. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11501. /* sanity check on queue memory */
  11502. if (!mq || !cq)
  11503. return -ENODEV;
  11504. if (!phba->sli4_hba.pc_sli4_params.supported)
  11505. hw_page_size = SLI4_PAGE_SIZE;
  11506. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11507. if (!mbox)
  11508. return -ENOMEM;
  11509. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11510. sizeof(struct lpfc_sli4_cfg_mhdr));
  11511. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11512. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11513. length, LPFC_SLI4_MBX_EMBED);
  11514. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11515. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11516. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11517. &mq_create_ext->u.request, mq->page_count);
  11518. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11519. &mq_create_ext->u.request, 1);
  11520. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11521. &mq_create_ext->u.request, 1);
  11522. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11523. &mq_create_ext->u.request, 1);
  11524. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11525. &mq_create_ext->u.request, 1);
  11526. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11527. &mq_create_ext->u.request, 1);
  11528. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11529. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11530. phba->sli4_hba.pc_sli4_params.mqv);
  11531. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11532. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11533. cq->queue_id);
  11534. else
  11535. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11536. cq->queue_id);
  11537. switch (mq->entry_count) {
  11538. default:
  11539. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11540. "0362 Unsupported MQ count. (%d)\n",
  11541. mq->entry_count);
  11542. if (mq->entry_count < 16) {
  11543. status = -EINVAL;
  11544. goto out;
  11545. }
  11546. /* otherwise default to smallest count (drop through) */
  11547. case 16:
  11548. bf_set(lpfc_mq_context_ring_size,
  11549. &mq_create_ext->u.request.context,
  11550. LPFC_MQ_RING_SIZE_16);
  11551. break;
  11552. case 32:
  11553. bf_set(lpfc_mq_context_ring_size,
  11554. &mq_create_ext->u.request.context,
  11555. LPFC_MQ_RING_SIZE_32);
  11556. break;
  11557. case 64:
  11558. bf_set(lpfc_mq_context_ring_size,
  11559. &mq_create_ext->u.request.context,
  11560. LPFC_MQ_RING_SIZE_64);
  11561. break;
  11562. case 128:
  11563. bf_set(lpfc_mq_context_ring_size,
  11564. &mq_create_ext->u.request.context,
  11565. LPFC_MQ_RING_SIZE_128);
  11566. break;
  11567. }
  11568. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11569. memset(dmabuf->virt, 0, hw_page_size);
  11570. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11571. putPaddrLow(dmabuf->phys);
  11572. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11573. putPaddrHigh(dmabuf->phys);
  11574. }
  11575. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11576. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11577. &mq_create_ext->u.response);
  11578. if (rc != MBX_SUCCESS) {
  11579. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11580. "2795 MQ_CREATE_EXT failed with "
  11581. "status x%x. Failback to MQ_CREATE.\n",
  11582. rc);
  11583. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11584. mq_create = &mbox->u.mqe.un.mq_create;
  11585. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11586. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11587. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11588. &mq_create->u.response);
  11589. }
  11590. /* The IOCTL status is embedded in the mailbox subheader. */
  11591. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11592. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11593. if (shdr_status || shdr_add_status || rc) {
  11594. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11595. "2502 MQ_CREATE mailbox failed with "
  11596. "status x%x add_status x%x, mbx status x%x\n",
  11597. shdr_status, shdr_add_status, rc);
  11598. status = -ENXIO;
  11599. goto out;
  11600. }
  11601. if (mq->queue_id == 0xFFFF) {
  11602. status = -ENXIO;
  11603. goto out;
  11604. }
  11605. mq->type = LPFC_MQ;
  11606. mq->assoc_qid = cq->queue_id;
  11607. mq->subtype = subtype;
  11608. mq->host_index = 0;
  11609. mq->hba_index = 0;
  11610. /* link the mq onto the parent cq child list */
  11611. list_add_tail(&mq->list, &cq->child_list);
  11612. out:
  11613. mempool_free(mbox, phba->mbox_mem_pool);
  11614. return status;
  11615. }
  11616. /**
  11617. * lpfc_wq_create - Create a Work Queue on the HBA
  11618. * @phba: HBA structure that indicates port to create a queue on.
  11619. * @wq: The queue structure to use to create the work queue.
  11620. * @cq: The completion queue to bind this work queue to.
  11621. * @subtype: The subtype of the work queue indicating its functionality.
  11622. *
  11623. * This function creates a work queue, as detailed in @wq, on a port, described
  11624. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11625. *
  11626. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11627. * is used to get the entry count and entry size that are necessary to
  11628. * determine the number of pages to allocate and use for this queue. The @cq
  11629. * is used to indicate which completion queue to bind this work queue to. This
  11630. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11631. * work queue. This function is asynchronous and will wait for the mailbox
  11632. * command to finish before continuing.
  11633. *
  11634. * On success this function will return a zero. If unable to allocate enough
  11635. * memory this function will return -ENOMEM. If the queue create mailbox command
  11636. * fails this function will return -ENXIO.
  11637. **/
  11638. uint32_t
  11639. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11640. struct lpfc_queue *cq, uint32_t subtype)
  11641. {
  11642. struct lpfc_mbx_wq_create *wq_create;
  11643. struct lpfc_dmabuf *dmabuf;
  11644. LPFC_MBOXQ_t *mbox;
  11645. int rc, length, status = 0;
  11646. uint32_t shdr_status, shdr_add_status;
  11647. union lpfc_sli4_cfg_shdr *shdr;
  11648. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11649. struct dma_address *page;
  11650. void __iomem *bar_memmap_p;
  11651. uint32_t db_offset;
  11652. uint16_t pci_barset;
  11653. /* sanity check on queue memory */
  11654. if (!wq || !cq)
  11655. return -ENODEV;
  11656. if (!phba->sli4_hba.pc_sli4_params.supported)
  11657. hw_page_size = SLI4_PAGE_SIZE;
  11658. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11659. if (!mbox)
  11660. return -ENOMEM;
  11661. length = (sizeof(struct lpfc_mbx_wq_create) -
  11662. sizeof(struct lpfc_sli4_cfg_mhdr));
  11663. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11664. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11665. length, LPFC_SLI4_MBX_EMBED);
  11666. wq_create = &mbox->u.mqe.un.wq_create;
  11667. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11668. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11669. wq->page_count);
  11670. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11671. cq->queue_id);
  11672. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11673. phba->sli4_hba.pc_sli4_params.wqv);
  11674. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11675. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11676. wq->entry_count);
  11677. switch (wq->entry_size) {
  11678. default:
  11679. case 64:
  11680. bf_set(lpfc_mbx_wq_create_wqe_size,
  11681. &wq_create->u.request_1,
  11682. LPFC_WQ_WQE_SIZE_64);
  11683. break;
  11684. case 128:
  11685. bf_set(lpfc_mbx_wq_create_wqe_size,
  11686. &wq_create->u.request_1,
  11687. LPFC_WQ_WQE_SIZE_128);
  11688. break;
  11689. }
  11690. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11691. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11692. page = wq_create->u.request_1.page;
  11693. } else {
  11694. page = wq_create->u.request.page;
  11695. }
  11696. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11697. memset(dmabuf->virt, 0, hw_page_size);
  11698. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11699. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11700. }
  11701. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  11702. bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
  11703. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11704. /* The IOCTL status is embedded in the mailbox subheader. */
  11705. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11706. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11707. if (shdr_status || shdr_add_status || rc) {
  11708. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11709. "2503 WQ_CREATE mailbox failed with "
  11710. "status x%x add_status x%x, mbx status x%x\n",
  11711. shdr_status, shdr_add_status, rc);
  11712. status = -ENXIO;
  11713. goto out;
  11714. }
  11715. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11716. if (wq->queue_id == 0xFFFF) {
  11717. status = -ENXIO;
  11718. goto out;
  11719. }
  11720. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  11721. wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
  11722. &wq_create->u.response);
  11723. if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
  11724. (wq->db_format != LPFC_DB_RING_FORMAT)) {
  11725. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11726. "3265 WQ[%d] doorbell format not "
  11727. "supported: x%x\n", wq->queue_id,
  11728. wq->db_format);
  11729. status = -EINVAL;
  11730. goto out;
  11731. }
  11732. pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
  11733. &wq_create->u.response);
  11734. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  11735. if (!bar_memmap_p) {
  11736. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11737. "3263 WQ[%d] failed to memmap pci "
  11738. "barset:x%x\n", wq->queue_id,
  11739. pci_barset);
  11740. status = -ENOMEM;
  11741. goto out;
  11742. }
  11743. db_offset = wq_create->u.response.doorbell_offset;
  11744. if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
  11745. (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
  11746. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11747. "3252 WQ[%d] doorbell offset not "
  11748. "supported: x%x\n", wq->queue_id,
  11749. db_offset);
  11750. status = -EINVAL;
  11751. goto out;
  11752. }
  11753. wq->db_regaddr = bar_memmap_p + db_offset;
  11754. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11755. "3264 WQ[%d]: barset:x%x, offset:x%x\n",
  11756. wq->queue_id, pci_barset, db_offset);
  11757. } else {
  11758. wq->db_format = LPFC_DB_LIST_FORMAT;
  11759. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  11760. }
  11761. wq->type = LPFC_WQ;
  11762. wq->assoc_qid = cq->queue_id;
  11763. wq->subtype = subtype;
  11764. wq->host_index = 0;
  11765. wq->hba_index = 0;
  11766. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11767. /* link the wq onto the parent cq child list */
  11768. list_add_tail(&wq->list, &cq->child_list);
  11769. out:
  11770. mempool_free(mbox, phba->mbox_mem_pool);
  11771. return status;
  11772. }
  11773. /**
  11774. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11775. * @phba: HBA structure that indicates port to create a queue on.
  11776. * @rq: The queue structure to use for the receive queue.
  11777. * @qno: The associated HBQ number
  11778. *
  11779. *
  11780. * For SLI4 we need to adjust the RQ repost value based on
  11781. * the number of buffers that are initially posted to the RQ.
  11782. */
  11783. void
  11784. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11785. {
  11786. uint32_t cnt;
  11787. /* sanity check on queue memory */
  11788. if (!rq)
  11789. return;
  11790. cnt = lpfc_hbq_defs[qno]->entry_count;
  11791. /* Recalc repost for RQs based on buffers initially posted */
  11792. cnt = (cnt >> 3);
  11793. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11794. cnt = LPFC_QUEUE_MIN_REPOST;
  11795. rq->entry_repost = cnt;
  11796. }
  11797. /**
  11798. * lpfc_rq_create - Create a Receive Queue on the HBA
  11799. * @phba: HBA structure that indicates port to create a queue on.
  11800. * @hrq: The queue structure to use to create the header receive queue.
  11801. * @drq: The queue structure to use to create the data receive queue.
  11802. * @cq: The completion queue to bind this work queue to.
  11803. *
  11804. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11805. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11806. * to the HBA.
  11807. *
  11808. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11809. * struct is used to get the entry count that is necessary to determine the
  11810. * number of pages to use for this queue. The @cq is used to indicate which
  11811. * completion queue to bind received buffers that are posted to these queues to.
  11812. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11813. * receive queue pair. This function is asynchronous and will wait for the
  11814. * mailbox command to finish before continuing.
  11815. *
  11816. * On success this function will return a zero. If unable to allocate enough
  11817. * memory this function will return -ENOMEM. If the queue create mailbox command
  11818. * fails this function will return -ENXIO.
  11819. **/
  11820. uint32_t
  11821. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11822. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11823. {
  11824. struct lpfc_mbx_rq_create *rq_create;
  11825. struct lpfc_dmabuf *dmabuf;
  11826. LPFC_MBOXQ_t *mbox;
  11827. int rc, length, status = 0;
  11828. uint32_t shdr_status, shdr_add_status;
  11829. union lpfc_sli4_cfg_shdr *shdr;
  11830. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11831. void __iomem *bar_memmap_p;
  11832. uint32_t db_offset;
  11833. uint16_t pci_barset;
  11834. /* sanity check on queue memory */
  11835. if (!hrq || !drq || !cq)
  11836. return -ENODEV;
  11837. if (!phba->sli4_hba.pc_sli4_params.supported)
  11838. hw_page_size = SLI4_PAGE_SIZE;
  11839. if (hrq->entry_count != drq->entry_count)
  11840. return -EINVAL;
  11841. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11842. if (!mbox)
  11843. return -ENOMEM;
  11844. length = (sizeof(struct lpfc_mbx_rq_create) -
  11845. sizeof(struct lpfc_sli4_cfg_mhdr));
  11846. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11847. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11848. length, LPFC_SLI4_MBX_EMBED);
  11849. rq_create = &mbox->u.mqe.un.rq_create;
  11850. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11851. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11852. phba->sli4_hba.pc_sli4_params.rqv);
  11853. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11854. bf_set(lpfc_rq_context_rqe_count_1,
  11855. &rq_create->u.request.context,
  11856. hrq->entry_count);
  11857. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11858. bf_set(lpfc_rq_context_rqe_size,
  11859. &rq_create->u.request.context,
  11860. LPFC_RQE_SIZE_8);
  11861. bf_set(lpfc_rq_context_page_size,
  11862. &rq_create->u.request.context,
  11863. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11864. } else {
  11865. switch (hrq->entry_count) {
  11866. default:
  11867. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11868. "2535 Unsupported RQ count. (%d)\n",
  11869. hrq->entry_count);
  11870. if (hrq->entry_count < 512) {
  11871. status = -EINVAL;
  11872. goto out;
  11873. }
  11874. /* otherwise default to smallest count (drop through) */
  11875. case 512:
  11876. bf_set(lpfc_rq_context_rqe_count,
  11877. &rq_create->u.request.context,
  11878. LPFC_RQ_RING_SIZE_512);
  11879. break;
  11880. case 1024:
  11881. bf_set(lpfc_rq_context_rqe_count,
  11882. &rq_create->u.request.context,
  11883. LPFC_RQ_RING_SIZE_1024);
  11884. break;
  11885. case 2048:
  11886. bf_set(lpfc_rq_context_rqe_count,
  11887. &rq_create->u.request.context,
  11888. LPFC_RQ_RING_SIZE_2048);
  11889. break;
  11890. case 4096:
  11891. bf_set(lpfc_rq_context_rqe_count,
  11892. &rq_create->u.request.context,
  11893. LPFC_RQ_RING_SIZE_4096);
  11894. break;
  11895. }
  11896. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11897. LPFC_HDR_BUF_SIZE);
  11898. }
  11899. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11900. cq->queue_id);
  11901. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11902. hrq->page_count);
  11903. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11904. memset(dmabuf->virt, 0, hw_page_size);
  11905. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11906. putPaddrLow(dmabuf->phys);
  11907. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11908. putPaddrHigh(dmabuf->phys);
  11909. }
  11910. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  11911. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  11912. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11913. /* The IOCTL status is embedded in the mailbox subheader. */
  11914. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11915. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11916. if (shdr_status || shdr_add_status || rc) {
  11917. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11918. "2504 RQ_CREATE mailbox failed with "
  11919. "status x%x add_status x%x, mbx status x%x\n",
  11920. shdr_status, shdr_add_status, rc);
  11921. status = -ENXIO;
  11922. goto out;
  11923. }
  11924. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11925. if (hrq->queue_id == 0xFFFF) {
  11926. status = -ENXIO;
  11927. goto out;
  11928. }
  11929. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  11930. hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
  11931. &rq_create->u.response);
  11932. if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
  11933. (hrq->db_format != LPFC_DB_RING_FORMAT)) {
  11934. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11935. "3262 RQ [%d] doorbell format not "
  11936. "supported: x%x\n", hrq->queue_id,
  11937. hrq->db_format);
  11938. status = -EINVAL;
  11939. goto out;
  11940. }
  11941. pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
  11942. &rq_create->u.response);
  11943. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  11944. if (!bar_memmap_p) {
  11945. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11946. "3269 RQ[%d] failed to memmap pci "
  11947. "barset:x%x\n", hrq->queue_id,
  11948. pci_barset);
  11949. status = -ENOMEM;
  11950. goto out;
  11951. }
  11952. db_offset = rq_create->u.response.doorbell_offset;
  11953. if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
  11954. (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
  11955. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11956. "3270 RQ[%d] doorbell offset not "
  11957. "supported: x%x\n", hrq->queue_id,
  11958. db_offset);
  11959. status = -EINVAL;
  11960. goto out;
  11961. }
  11962. hrq->db_regaddr = bar_memmap_p + db_offset;
  11963. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11964. "3266 RQ[qid:%d]: barset:x%x, offset:x%x\n",
  11965. hrq->queue_id, pci_barset, db_offset);
  11966. } else {
  11967. hrq->db_format = LPFC_DB_RING_FORMAT;
  11968. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  11969. }
  11970. hrq->type = LPFC_HRQ;
  11971. hrq->assoc_qid = cq->queue_id;
  11972. hrq->subtype = subtype;
  11973. hrq->host_index = 0;
  11974. hrq->hba_index = 0;
  11975. /* now create the data queue */
  11976. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11977. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11978. length, LPFC_SLI4_MBX_EMBED);
  11979. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11980. phba->sli4_hba.pc_sli4_params.rqv);
  11981. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11982. bf_set(lpfc_rq_context_rqe_count_1,
  11983. &rq_create->u.request.context, hrq->entry_count);
  11984. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11985. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11986. LPFC_RQE_SIZE_8);
  11987. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11988. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11989. } else {
  11990. switch (drq->entry_count) {
  11991. default:
  11992. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11993. "2536 Unsupported RQ count. (%d)\n",
  11994. drq->entry_count);
  11995. if (drq->entry_count < 512) {
  11996. status = -EINVAL;
  11997. goto out;
  11998. }
  11999. /* otherwise default to smallest count (drop through) */
  12000. case 512:
  12001. bf_set(lpfc_rq_context_rqe_count,
  12002. &rq_create->u.request.context,
  12003. LPFC_RQ_RING_SIZE_512);
  12004. break;
  12005. case 1024:
  12006. bf_set(lpfc_rq_context_rqe_count,
  12007. &rq_create->u.request.context,
  12008. LPFC_RQ_RING_SIZE_1024);
  12009. break;
  12010. case 2048:
  12011. bf_set(lpfc_rq_context_rqe_count,
  12012. &rq_create->u.request.context,
  12013. LPFC_RQ_RING_SIZE_2048);
  12014. break;
  12015. case 4096:
  12016. bf_set(lpfc_rq_context_rqe_count,
  12017. &rq_create->u.request.context,
  12018. LPFC_RQ_RING_SIZE_4096);
  12019. break;
  12020. }
  12021. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12022. LPFC_DATA_BUF_SIZE);
  12023. }
  12024. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12025. cq->queue_id);
  12026. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12027. drq->page_count);
  12028. list_for_each_entry(dmabuf, &drq->page_list, list) {
  12029. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12030. putPaddrLow(dmabuf->phys);
  12031. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12032. putPaddrHigh(dmabuf->phys);
  12033. }
  12034. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12035. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12036. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12037. /* The IOCTL status is embedded in the mailbox subheader. */
  12038. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12039. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12040. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12041. if (shdr_status || shdr_add_status || rc) {
  12042. status = -ENXIO;
  12043. goto out;
  12044. }
  12045. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12046. if (drq->queue_id == 0xFFFF) {
  12047. status = -ENXIO;
  12048. goto out;
  12049. }
  12050. drq->type = LPFC_DRQ;
  12051. drq->assoc_qid = cq->queue_id;
  12052. drq->subtype = subtype;
  12053. drq->host_index = 0;
  12054. drq->hba_index = 0;
  12055. /* link the header and data RQs onto the parent cq child list */
  12056. list_add_tail(&hrq->list, &cq->child_list);
  12057. list_add_tail(&drq->list, &cq->child_list);
  12058. out:
  12059. mempool_free(mbox, phba->mbox_mem_pool);
  12060. return status;
  12061. }
  12062. /**
  12063. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  12064. * @eq: The queue structure associated with the queue to destroy.
  12065. *
  12066. * This function destroys a queue, as detailed in @eq by sending an mailbox
  12067. * command, specific to the type of queue, to the HBA.
  12068. *
  12069. * The @eq struct is used to get the queue ID of the queue to destroy.
  12070. *
  12071. * On success this function will return a zero. If the queue destroy mailbox
  12072. * command fails this function will return -ENXIO.
  12073. **/
  12074. uint32_t
  12075. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  12076. {
  12077. LPFC_MBOXQ_t *mbox;
  12078. int rc, length, status = 0;
  12079. uint32_t shdr_status, shdr_add_status;
  12080. union lpfc_sli4_cfg_shdr *shdr;
  12081. /* sanity check on queue memory */
  12082. if (!eq)
  12083. return -ENODEV;
  12084. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  12085. if (!mbox)
  12086. return -ENOMEM;
  12087. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  12088. sizeof(struct lpfc_sli4_cfg_mhdr));
  12089. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12090. LPFC_MBOX_OPCODE_EQ_DESTROY,
  12091. length, LPFC_SLI4_MBX_EMBED);
  12092. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  12093. eq->queue_id);
  12094. mbox->vport = eq->phba->pport;
  12095. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12096. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  12097. /* The IOCTL status is embedded in the mailbox subheader. */
  12098. shdr = (union lpfc_sli4_cfg_shdr *)
  12099. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  12100. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12101. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12102. if (shdr_status || shdr_add_status || rc) {
  12103. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12104. "2505 EQ_DESTROY mailbox failed with "
  12105. "status x%x add_status x%x, mbx status x%x\n",
  12106. shdr_status, shdr_add_status, rc);
  12107. status = -ENXIO;
  12108. }
  12109. /* Remove eq from any list */
  12110. list_del_init(&eq->list);
  12111. mempool_free(mbox, eq->phba->mbox_mem_pool);
  12112. return status;
  12113. }
  12114. /**
  12115. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  12116. * @cq: The queue structure associated with the queue to destroy.
  12117. *
  12118. * This function destroys a queue, as detailed in @cq by sending an mailbox
  12119. * command, specific to the type of queue, to the HBA.
  12120. *
  12121. * The @cq struct is used to get the queue ID of the queue to destroy.
  12122. *
  12123. * On success this function will return a zero. If the queue destroy mailbox
  12124. * command fails this function will return -ENXIO.
  12125. **/
  12126. uint32_t
  12127. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  12128. {
  12129. LPFC_MBOXQ_t *mbox;
  12130. int rc, length, status = 0;
  12131. uint32_t shdr_status, shdr_add_status;
  12132. union lpfc_sli4_cfg_shdr *shdr;
  12133. /* sanity check on queue memory */
  12134. if (!cq)
  12135. return -ENODEV;
  12136. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  12137. if (!mbox)
  12138. return -ENOMEM;
  12139. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  12140. sizeof(struct lpfc_sli4_cfg_mhdr));
  12141. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12142. LPFC_MBOX_OPCODE_CQ_DESTROY,
  12143. length, LPFC_SLI4_MBX_EMBED);
  12144. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  12145. cq->queue_id);
  12146. mbox->vport = cq->phba->pport;
  12147. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12148. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  12149. /* The IOCTL status is embedded in the mailbox subheader. */
  12150. shdr = (union lpfc_sli4_cfg_shdr *)
  12151. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  12152. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12153. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12154. if (shdr_status || shdr_add_status || rc) {
  12155. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12156. "2506 CQ_DESTROY mailbox failed with "
  12157. "status x%x add_status x%x, mbx status x%x\n",
  12158. shdr_status, shdr_add_status, rc);
  12159. status = -ENXIO;
  12160. }
  12161. /* Remove cq from any list */
  12162. list_del_init(&cq->list);
  12163. mempool_free(mbox, cq->phba->mbox_mem_pool);
  12164. return status;
  12165. }
  12166. /**
  12167. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  12168. * @qm: The queue structure associated with the queue to destroy.
  12169. *
  12170. * This function destroys a queue, as detailed in @mq by sending an mailbox
  12171. * command, specific to the type of queue, to the HBA.
  12172. *
  12173. * The @mq struct is used to get the queue ID of the queue to destroy.
  12174. *
  12175. * On success this function will return a zero. If the queue destroy mailbox
  12176. * command fails this function will return -ENXIO.
  12177. **/
  12178. uint32_t
  12179. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  12180. {
  12181. LPFC_MBOXQ_t *mbox;
  12182. int rc, length, status = 0;
  12183. uint32_t shdr_status, shdr_add_status;
  12184. union lpfc_sli4_cfg_shdr *shdr;
  12185. /* sanity check on queue memory */
  12186. if (!mq)
  12187. return -ENODEV;
  12188. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  12189. if (!mbox)
  12190. return -ENOMEM;
  12191. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12192. sizeof(struct lpfc_sli4_cfg_mhdr));
  12193. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12194. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12195. length, LPFC_SLI4_MBX_EMBED);
  12196. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12197. mq->queue_id);
  12198. mbox->vport = mq->phba->pport;
  12199. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12200. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12201. /* The IOCTL status is embedded in the mailbox subheader. */
  12202. shdr = (union lpfc_sli4_cfg_shdr *)
  12203. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12204. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12205. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12206. if (shdr_status || shdr_add_status || rc) {
  12207. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12208. "2507 MQ_DESTROY mailbox failed with "
  12209. "status x%x add_status x%x, mbx status x%x\n",
  12210. shdr_status, shdr_add_status, rc);
  12211. status = -ENXIO;
  12212. }
  12213. /* Remove mq from any list */
  12214. list_del_init(&mq->list);
  12215. mempool_free(mbox, mq->phba->mbox_mem_pool);
  12216. return status;
  12217. }
  12218. /**
  12219. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12220. * @wq: The queue structure associated with the queue to destroy.
  12221. *
  12222. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12223. * command, specific to the type of queue, to the HBA.
  12224. *
  12225. * The @wq struct is used to get the queue ID of the queue to destroy.
  12226. *
  12227. * On success this function will return a zero. If the queue destroy mailbox
  12228. * command fails this function will return -ENXIO.
  12229. **/
  12230. uint32_t
  12231. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12232. {
  12233. LPFC_MBOXQ_t *mbox;
  12234. int rc, length, status = 0;
  12235. uint32_t shdr_status, shdr_add_status;
  12236. union lpfc_sli4_cfg_shdr *shdr;
  12237. /* sanity check on queue memory */
  12238. if (!wq)
  12239. return -ENODEV;
  12240. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12241. if (!mbox)
  12242. return -ENOMEM;
  12243. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12244. sizeof(struct lpfc_sli4_cfg_mhdr));
  12245. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12246. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12247. length, LPFC_SLI4_MBX_EMBED);
  12248. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12249. wq->queue_id);
  12250. mbox->vport = wq->phba->pport;
  12251. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12252. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12253. shdr = (union lpfc_sli4_cfg_shdr *)
  12254. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12255. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12256. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12257. if (shdr_status || shdr_add_status || rc) {
  12258. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12259. "2508 WQ_DESTROY mailbox failed with "
  12260. "status x%x add_status x%x, mbx status x%x\n",
  12261. shdr_status, shdr_add_status, rc);
  12262. status = -ENXIO;
  12263. }
  12264. /* Remove wq from any list */
  12265. list_del_init(&wq->list);
  12266. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12267. return status;
  12268. }
  12269. /**
  12270. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12271. * @rq: The queue structure associated with the queue to destroy.
  12272. *
  12273. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12274. * command, specific to the type of queue, to the HBA.
  12275. *
  12276. * The @rq struct is used to get the queue ID of the queue to destroy.
  12277. *
  12278. * On success this function will return a zero. If the queue destroy mailbox
  12279. * command fails this function will return -ENXIO.
  12280. **/
  12281. uint32_t
  12282. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12283. struct lpfc_queue *drq)
  12284. {
  12285. LPFC_MBOXQ_t *mbox;
  12286. int rc, length, status = 0;
  12287. uint32_t shdr_status, shdr_add_status;
  12288. union lpfc_sli4_cfg_shdr *shdr;
  12289. /* sanity check on queue memory */
  12290. if (!hrq || !drq)
  12291. return -ENODEV;
  12292. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12293. if (!mbox)
  12294. return -ENOMEM;
  12295. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12296. sizeof(struct lpfc_sli4_cfg_mhdr));
  12297. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12298. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12299. length, LPFC_SLI4_MBX_EMBED);
  12300. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12301. hrq->queue_id);
  12302. mbox->vport = hrq->phba->pport;
  12303. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12304. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12305. /* The IOCTL status is embedded in the mailbox subheader. */
  12306. shdr = (union lpfc_sli4_cfg_shdr *)
  12307. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12308. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12309. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12310. if (shdr_status || shdr_add_status || rc) {
  12311. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12312. "2509 RQ_DESTROY mailbox failed with "
  12313. "status x%x add_status x%x, mbx status x%x\n",
  12314. shdr_status, shdr_add_status, rc);
  12315. if (rc != MBX_TIMEOUT)
  12316. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12317. return -ENXIO;
  12318. }
  12319. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12320. drq->queue_id);
  12321. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12322. shdr = (union lpfc_sli4_cfg_shdr *)
  12323. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12324. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12325. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12326. if (shdr_status || shdr_add_status || rc) {
  12327. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12328. "2510 RQ_DESTROY mailbox failed with "
  12329. "status x%x add_status x%x, mbx status x%x\n",
  12330. shdr_status, shdr_add_status, rc);
  12331. status = -ENXIO;
  12332. }
  12333. list_del_init(&hrq->list);
  12334. list_del_init(&drq->list);
  12335. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12336. return status;
  12337. }
  12338. /**
  12339. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12340. * @phba: The virtual port for which this call being executed.
  12341. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12342. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12343. * @xritag: the xritag that ties this io to the SGL pages.
  12344. *
  12345. * This routine will post the sgl pages for the IO that has the xritag
  12346. * that is in the iocbq structure. The xritag is assigned during iocbq
  12347. * creation and persists for as long as the driver is loaded.
  12348. * if the caller has fewer than 256 scatter gather segments to map then
  12349. * pdma_phys_addr1 should be 0.
  12350. * If the caller needs to map more than 256 scatter gather segment then
  12351. * pdma_phys_addr1 should be a valid physical address.
  12352. * physical address for SGLs must be 64 byte aligned.
  12353. * If you are going to map 2 SGL's then the first one must have 256 entries
  12354. * the second sgl can have between 1 and 256 entries.
  12355. *
  12356. * Return codes:
  12357. * 0 - Success
  12358. * -ENXIO, -ENOMEM - Failure
  12359. **/
  12360. int
  12361. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12362. dma_addr_t pdma_phys_addr0,
  12363. dma_addr_t pdma_phys_addr1,
  12364. uint16_t xritag)
  12365. {
  12366. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12367. LPFC_MBOXQ_t *mbox;
  12368. int rc;
  12369. uint32_t shdr_status, shdr_add_status;
  12370. uint32_t mbox_tmo;
  12371. union lpfc_sli4_cfg_shdr *shdr;
  12372. if (xritag == NO_XRI) {
  12373. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12374. "0364 Invalid param:\n");
  12375. return -EINVAL;
  12376. }
  12377. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12378. if (!mbox)
  12379. return -ENOMEM;
  12380. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12381. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12382. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12383. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12384. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12385. &mbox->u.mqe.un.post_sgl_pages;
  12386. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12387. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12388. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12389. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12390. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12391. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12392. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12393. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12394. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12395. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12396. if (!phba->sli4_hba.intr_enable)
  12397. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12398. else {
  12399. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12400. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12401. }
  12402. /* The IOCTL status is embedded in the mailbox subheader. */
  12403. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12404. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12405. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12406. if (rc != MBX_TIMEOUT)
  12407. mempool_free(mbox, phba->mbox_mem_pool);
  12408. if (shdr_status || shdr_add_status || rc) {
  12409. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12410. "2511 POST_SGL mailbox failed with "
  12411. "status x%x add_status x%x, mbx status x%x\n",
  12412. shdr_status, shdr_add_status, rc);
  12413. rc = -ENXIO;
  12414. }
  12415. return 0;
  12416. }
  12417. /**
  12418. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12419. * @phba: pointer to lpfc hba data structure.
  12420. *
  12421. * This routine is invoked to post rpi header templates to the
  12422. * HBA consistent with the SLI-4 interface spec. This routine
  12423. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12424. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12425. *
  12426. * Returns
  12427. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12428. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12429. **/
  12430. uint16_t
  12431. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12432. {
  12433. unsigned long xri;
  12434. /*
  12435. * Fetch the next logical xri. Because this index is logical,
  12436. * the driver starts at 0 each time.
  12437. */
  12438. spin_lock_irq(&phba->hbalock);
  12439. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12440. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12441. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12442. spin_unlock_irq(&phba->hbalock);
  12443. return NO_XRI;
  12444. } else {
  12445. set_bit(xri, phba->sli4_hba.xri_bmask);
  12446. phba->sli4_hba.max_cfg_param.xri_used++;
  12447. }
  12448. spin_unlock_irq(&phba->hbalock);
  12449. return xri;
  12450. }
  12451. /**
  12452. * lpfc_sli4_free_xri - Release an xri for reuse.
  12453. * @phba: pointer to lpfc hba data structure.
  12454. *
  12455. * This routine is invoked to release an xri to the pool of
  12456. * available rpis maintained by the driver.
  12457. **/
  12458. void
  12459. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12460. {
  12461. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12462. phba->sli4_hba.max_cfg_param.xri_used--;
  12463. }
  12464. }
  12465. /**
  12466. * lpfc_sli4_free_xri - Release an xri for reuse.
  12467. * @phba: pointer to lpfc hba data structure.
  12468. *
  12469. * This routine is invoked to release an xri to the pool of
  12470. * available rpis maintained by the driver.
  12471. **/
  12472. void
  12473. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12474. {
  12475. spin_lock_irq(&phba->hbalock);
  12476. __lpfc_sli4_free_xri(phba, xri);
  12477. spin_unlock_irq(&phba->hbalock);
  12478. }
  12479. /**
  12480. * lpfc_sli4_next_xritag - Get an xritag for the io
  12481. * @phba: Pointer to HBA context object.
  12482. *
  12483. * This function gets an xritag for the iocb. If there is no unused xritag
  12484. * it will return 0xffff.
  12485. * The function returns the allocated xritag if successful, else returns zero.
  12486. * Zero is not a valid xritag.
  12487. * The caller is not required to hold any lock.
  12488. **/
  12489. uint16_t
  12490. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12491. {
  12492. uint16_t xri_index;
  12493. xri_index = lpfc_sli4_alloc_xri(phba);
  12494. if (xri_index == NO_XRI)
  12495. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12496. "2004 Failed to allocate XRI.last XRITAG is %d"
  12497. " Max XRI is %d, Used XRI is %d\n",
  12498. xri_index,
  12499. phba->sli4_hba.max_cfg_param.max_xri,
  12500. phba->sli4_hba.max_cfg_param.xri_used);
  12501. return xri_index;
  12502. }
  12503. /**
  12504. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12505. * @phba: pointer to lpfc hba data structure.
  12506. * @post_sgl_list: pointer to els sgl entry list.
  12507. * @count: number of els sgl entries on the list.
  12508. *
  12509. * This routine is invoked to post a block of driver's sgl pages to the
  12510. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12511. * is only called when the driver is loading and after all IO has been
  12512. * stopped.
  12513. **/
  12514. static int
  12515. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12516. struct list_head *post_sgl_list,
  12517. int post_cnt)
  12518. {
  12519. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12520. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12521. struct sgl_page_pairs *sgl_pg_pairs;
  12522. void *viraddr;
  12523. LPFC_MBOXQ_t *mbox;
  12524. uint32_t reqlen, alloclen, pg_pairs;
  12525. uint32_t mbox_tmo;
  12526. uint16_t xritag_start = 0;
  12527. int rc = 0;
  12528. uint32_t shdr_status, shdr_add_status;
  12529. union lpfc_sli4_cfg_shdr *shdr;
  12530. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12531. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12532. if (reqlen > SLI4_PAGE_SIZE) {
  12533. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12534. "2559 Block sgl registration required DMA "
  12535. "size (%d) great than a page\n", reqlen);
  12536. return -ENOMEM;
  12537. }
  12538. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12539. if (!mbox)
  12540. return -ENOMEM;
  12541. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12542. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12543. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12544. LPFC_SLI4_MBX_NEMBED);
  12545. if (alloclen < reqlen) {
  12546. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12547. "0285 Allocated DMA memory size (%d) is "
  12548. "less than the requested DMA memory "
  12549. "size (%d)\n", alloclen, reqlen);
  12550. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12551. return -ENOMEM;
  12552. }
  12553. /* Set up the SGL pages in the non-embedded DMA pages */
  12554. viraddr = mbox->sge_array->addr[0];
  12555. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12556. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12557. pg_pairs = 0;
  12558. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12559. /* Set up the sge entry */
  12560. sgl_pg_pairs->sgl_pg0_addr_lo =
  12561. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12562. sgl_pg_pairs->sgl_pg0_addr_hi =
  12563. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12564. sgl_pg_pairs->sgl_pg1_addr_lo =
  12565. cpu_to_le32(putPaddrLow(0));
  12566. sgl_pg_pairs->sgl_pg1_addr_hi =
  12567. cpu_to_le32(putPaddrHigh(0));
  12568. /* Keep the first xritag on the list */
  12569. if (pg_pairs == 0)
  12570. xritag_start = sglq_entry->sli4_xritag;
  12571. sgl_pg_pairs++;
  12572. pg_pairs++;
  12573. }
  12574. /* Complete initialization and perform endian conversion. */
  12575. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12576. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12577. sgl->word0 = cpu_to_le32(sgl->word0);
  12578. if (!phba->sli4_hba.intr_enable)
  12579. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12580. else {
  12581. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12582. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12583. }
  12584. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12585. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12586. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12587. if (rc != MBX_TIMEOUT)
  12588. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12589. if (shdr_status || shdr_add_status || rc) {
  12590. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12591. "2513 POST_SGL_BLOCK mailbox command failed "
  12592. "status x%x add_status x%x mbx status x%x\n",
  12593. shdr_status, shdr_add_status, rc);
  12594. rc = -ENXIO;
  12595. }
  12596. return rc;
  12597. }
  12598. /**
  12599. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12600. * @phba: pointer to lpfc hba data structure.
  12601. * @sblist: pointer to scsi buffer list.
  12602. * @count: number of scsi buffers on the list.
  12603. *
  12604. * This routine is invoked to post a block of @count scsi sgl pages from a
  12605. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12606. * No Lock is held.
  12607. *
  12608. **/
  12609. int
  12610. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12611. struct list_head *sblist,
  12612. int count)
  12613. {
  12614. struct lpfc_scsi_buf *psb;
  12615. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12616. struct sgl_page_pairs *sgl_pg_pairs;
  12617. void *viraddr;
  12618. LPFC_MBOXQ_t *mbox;
  12619. uint32_t reqlen, alloclen, pg_pairs;
  12620. uint32_t mbox_tmo;
  12621. uint16_t xritag_start = 0;
  12622. int rc = 0;
  12623. uint32_t shdr_status, shdr_add_status;
  12624. dma_addr_t pdma_phys_bpl1;
  12625. union lpfc_sli4_cfg_shdr *shdr;
  12626. /* Calculate the requested length of the dma memory */
  12627. reqlen = count * sizeof(struct sgl_page_pairs) +
  12628. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12629. if (reqlen > SLI4_PAGE_SIZE) {
  12630. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12631. "0217 Block sgl registration required DMA "
  12632. "size (%d) great than a page\n", reqlen);
  12633. return -ENOMEM;
  12634. }
  12635. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12636. if (!mbox) {
  12637. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12638. "0283 Failed to allocate mbox cmd memory\n");
  12639. return -ENOMEM;
  12640. }
  12641. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12642. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12643. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12644. LPFC_SLI4_MBX_NEMBED);
  12645. if (alloclen < reqlen) {
  12646. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12647. "2561 Allocated DMA memory size (%d) is "
  12648. "less than the requested DMA memory "
  12649. "size (%d)\n", alloclen, reqlen);
  12650. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12651. return -ENOMEM;
  12652. }
  12653. /* Get the first SGE entry from the non-embedded DMA memory */
  12654. viraddr = mbox->sge_array->addr[0];
  12655. /* Set up the SGL pages in the non-embedded DMA pages */
  12656. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12657. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12658. pg_pairs = 0;
  12659. list_for_each_entry(psb, sblist, list) {
  12660. /* Set up the sge entry */
  12661. sgl_pg_pairs->sgl_pg0_addr_lo =
  12662. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12663. sgl_pg_pairs->sgl_pg0_addr_hi =
  12664. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12665. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12666. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12667. else
  12668. pdma_phys_bpl1 = 0;
  12669. sgl_pg_pairs->sgl_pg1_addr_lo =
  12670. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12671. sgl_pg_pairs->sgl_pg1_addr_hi =
  12672. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12673. /* Keep the first xritag on the list */
  12674. if (pg_pairs == 0)
  12675. xritag_start = psb->cur_iocbq.sli4_xritag;
  12676. sgl_pg_pairs++;
  12677. pg_pairs++;
  12678. }
  12679. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12680. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12681. /* Perform endian conversion if necessary */
  12682. sgl->word0 = cpu_to_le32(sgl->word0);
  12683. if (!phba->sli4_hba.intr_enable)
  12684. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12685. else {
  12686. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12687. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12688. }
  12689. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12690. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12691. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12692. if (rc != MBX_TIMEOUT)
  12693. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12694. if (shdr_status || shdr_add_status || rc) {
  12695. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12696. "2564 POST_SGL_BLOCK mailbox command failed "
  12697. "status x%x add_status x%x mbx status x%x\n",
  12698. shdr_status, shdr_add_status, rc);
  12699. rc = -ENXIO;
  12700. }
  12701. return rc;
  12702. }
  12703. /**
  12704. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12705. * @phba: pointer to lpfc_hba struct that the frame was received on
  12706. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12707. *
  12708. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12709. * valid type of frame that the LPFC driver will handle. This function will
  12710. * return a zero if the frame is a valid frame or a non zero value when the
  12711. * frame does not pass the check.
  12712. **/
  12713. static int
  12714. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12715. {
  12716. /* make rctl_names static to save stack space */
  12717. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12718. char *type_names[] = FC_TYPE_NAMES_INIT;
  12719. struct fc_vft_header *fc_vft_hdr;
  12720. uint32_t *header = (uint32_t *) fc_hdr;
  12721. switch (fc_hdr->fh_r_ctl) {
  12722. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12723. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12724. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12725. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12726. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12727. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12728. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12729. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12730. case FC_RCTL_ELS_REQ: /* extended link services request */
  12731. case FC_RCTL_ELS_REP: /* extended link services reply */
  12732. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12733. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12734. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12735. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12736. case FC_RCTL_BA_RMC: /* remove connection */
  12737. case FC_RCTL_BA_ACC: /* basic accept */
  12738. case FC_RCTL_BA_RJT: /* basic reject */
  12739. case FC_RCTL_BA_PRMT:
  12740. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12741. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12742. case FC_RCTL_P_RJT: /* port reject */
  12743. case FC_RCTL_F_RJT: /* fabric reject */
  12744. case FC_RCTL_P_BSY: /* port busy */
  12745. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12746. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12747. case FC_RCTL_LCR: /* link credit reset */
  12748. case FC_RCTL_END: /* end */
  12749. break;
  12750. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12751. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12752. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12753. return lpfc_fc_frame_check(phba, fc_hdr);
  12754. default:
  12755. goto drop;
  12756. }
  12757. switch (fc_hdr->fh_type) {
  12758. case FC_TYPE_BLS:
  12759. case FC_TYPE_ELS:
  12760. case FC_TYPE_FCP:
  12761. case FC_TYPE_CT:
  12762. break;
  12763. case FC_TYPE_IP:
  12764. case FC_TYPE_ILS:
  12765. default:
  12766. goto drop;
  12767. }
  12768. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12769. "2538 Received frame rctl:%s type:%s "
  12770. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12771. rctl_names[fc_hdr->fh_r_ctl],
  12772. type_names[fc_hdr->fh_type],
  12773. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12774. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12775. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12776. return 0;
  12777. drop:
  12778. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12779. "2539 Dropped frame rctl:%s type:%s\n",
  12780. rctl_names[fc_hdr->fh_r_ctl],
  12781. type_names[fc_hdr->fh_type]);
  12782. return 1;
  12783. }
  12784. /**
  12785. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12786. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12787. *
  12788. * This function processes the FC header to retrieve the VFI from the VF
  12789. * header, if one exists. This function will return the VFI if one exists
  12790. * or 0 if no VSAN Header exists.
  12791. **/
  12792. static uint32_t
  12793. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12794. {
  12795. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12796. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12797. return 0;
  12798. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12799. }
  12800. /**
  12801. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12802. * @phba: Pointer to the HBA structure to search for the vport on
  12803. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12804. * @fcfi: The FC Fabric ID that the frame came from
  12805. *
  12806. * This function searches the @phba for a vport that matches the content of the
  12807. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12808. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12809. * returns the matching vport pointer or NULL if unable to match frame to a
  12810. * vport.
  12811. **/
  12812. static struct lpfc_vport *
  12813. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12814. uint16_t fcfi)
  12815. {
  12816. struct lpfc_vport **vports;
  12817. struct lpfc_vport *vport = NULL;
  12818. int i;
  12819. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12820. fc_hdr->fh_d_id[1] << 8 |
  12821. fc_hdr->fh_d_id[2]);
  12822. if (did == Fabric_DID)
  12823. return phba->pport;
  12824. if ((phba->pport->fc_flag & FC_PT2PT) &&
  12825. !(phba->link_state == LPFC_HBA_READY))
  12826. return phba->pport;
  12827. vports = lpfc_create_vport_work_array(phba);
  12828. if (vports != NULL)
  12829. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12830. if (phba->fcf.fcfi == fcfi &&
  12831. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12832. vports[i]->fc_myDID == did) {
  12833. vport = vports[i];
  12834. break;
  12835. }
  12836. }
  12837. lpfc_destroy_vport_work_array(phba, vports);
  12838. return vport;
  12839. }
  12840. /**
  12841. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12842. * @vport: The vport to work on.
  12843. *
  12844. * This function updates the receive sequence time stamp for this vport. The
  12845. * receive sequence time stamp indicates the time that the last frame of the
  12846. * the sequence that has been idle for the longest amount of time was received.
  12847. * the driver uses this time stamp to indicate if any received sequences have
  12848. * timed out.
  12849. **/
  12850. void
  12851. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12852. {
  12853. struct lpfc_dmabuf *h_buf;
  12854. struct hbq_dmabuf *dmabuf = NULL;
  12855. /* get the oldest sequence on the rcv list */
  12856. h_buf = list_get_first(&vport->rcv_buffer_list,
  12857. struct lpfc_dmabuf, list);
  12858. if (!h_buf)
  12859. return;
  12860. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12861. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12862. }
  12863. /**
  12864. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12865. * @vport: The vport that the received sequences were sent to.
  12866. *
  12867. * This function cleans up all outstanding received sequences. This is called
  12868. * by the driver when a link event or user action invalidates all the received
  12869. * sequences.
  12870. **/
  12871. void
  12872. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12873. {
  12874. struct lpfc_dmabuf *h_buf, *hnext;
  12875. struct lpfc_dmabuf *d_buf, *dnext;
  12876. struct hbq_dmabuf *dmabuf = NULL;
  12877. /* start with the oldest sequence on the rcv list */
  12878. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12879. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12880. list_del_init(&dmabuf->hbuf.list);
  12881. list_for_each_entry_safe(d_buf, dnext,
  12882. &dmabuf->dbuf.list, list) {
  12883. list_del_init(&d_buf->list);
  12884. lpfc_in_buf_free(vport->phba, d_buf);
  12885. }
  12886. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12887. }
  12888. }
  12889. /**
  12890. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12891. * @vport: The vport that the received sequences were sent to.
  12892. *
  12893. * This function determines whether any received sequences have timed out by
  12894. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12895. * indicates that there is at least one timed out sequence this routine will
  12896. * go through the received sequences one at a time from most inactive to most
  12897. * active to determine which ones need to be cleaned up. Once it has determined
  12898. * that a sequence needs to be cleaned up it will simply free up the resources
  12899. * without sending an abort.
  12900. **/
  12901. void
  12902. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12903. {
  12904. struct lpfc_dmabuf *h_buf, *hnext;
  12905. struct lpfc_dmabuf *d_buf, *dnext;
  12906. struct hbq_dmabuf *dmabuf = NULL;
  12907. unsigned long timeout;
  12908. int abort_count = 0;
  12909. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12910. vport->rcv_buffer_time_stamp);
  12911. if (list_empty(&vport->rcv_buffer_list) ||
  12912. time_before(jiffies, timeout))
  12913. return;
  12914. /* start with the oldest sequence on the rcv list */
  12915. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12916. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12917. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12918. dmabuf->time_stamp);
  12919. if (time_before(jiffies, timeout))
  12920. break;
  12921. abort_count++;
  12922. list_del_init(&dmabuf->hbuf.list);
  12923. list_for_each_entry_safe(d_buf, dnext,
  12924. &dmabuf->dbuf.list, list) {
  12925. list_del_init(&d_buf->list);
  12926. lpfc_in_buf_free(vport->phba, d_buf);
  12927. }
  12928. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12929. }
  12930. if (abort_count)
  12931. lpfc_update_rcv_time_stamp(vport);
  12932. }
  12933. /**
  12934. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12935. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12936. *
  12937. * This function searches through the existing incomplete sequences that have
  12938. * been sent to this @vport. If the frame matches one of the incomplete
  12939. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12940. * make up that sequence. If no sequence is found that matches this frame then
  12941. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12942. * This function returns a pointer to the first dmabuf in the sequence list that
  12943. * the frame was linked to.
  12944. **/
  12945. static struct hbq_dmabuf *
  12946. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12947. {
  12948. struct fc_frame_header *new_hdr;
  12949. struct fc_frame_header *temp_hdr;
  12950. struct lpfc_dmabuf *d_buf;
  12951. struct lpfc_dmabuf *h_buf;
  12952. struct hbq_dmabuf *seq_dmabuf = NULL;
  12953. struct hbq_dmabuf *temp_dmabuf = NULL;
  12954. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12955. dmabuf->time_stamp = jiffies;
  12956. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12957. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12958. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12959. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12960. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12961. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12962. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12963. continue;
  12964. /* found a pending sequence that matches this frame */
  12965. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12966. break;
  12967. }
  12968. if (!seq_dmabuf) {
  12969. /*
  12970. * This indicates first frame received for this sequence.
  12971. * Queue the buffer on the vport's rcv_buffer_list.
  12972. */
  12973. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12974. lpfc_update_rcv_time_stamp(vport);
  12975. return dmabuf;
  12976. }
  12977. temp_hdr = seq_dmabuf->hbuf.virt;
  12978. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12979. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12980. list_del_init(&seq_dmabuf->hbuf.list);
  12981. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12982. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12983. lpfc_update_rcv_time_stamp(vport);
  12984. return dmabuf;
  12985. }
  12986. /* move this sequence to the tail to indicate a young sequence */
  12987. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12988. seq_dmabuf->time_stamp = jiffies;
  12989. lpfc_update_rcv_time_stamp(vport);
  12990. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12991. temp_hdr = dmabuf->hbuf.virt;
  12992. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12993. return seq_dmabuf;
  12994. }
  12995. /* find the correct place in the sequence to insert this frame */
  12996. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12997. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12998. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12999. /*
  13000. * If the frame's sequence count is greater than the frame on
  13001. * the list then insert the frame right after this frame
  13002. */
  13003. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  13004. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13005. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  13006. return seq_dmabuf;
  13007. }
  13008. }
  13009. return NULL;
  13010. }
  13011. /**
  13012. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  13013. * @vport: pointer to a vitural port
  13014. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13015. *
  13016. * This function tries to abort from the partially assembed sequence, described
  13017. * by the information from basic abbort @dmabuf. It checks to see whether such
  13018. * partially assembled sequence held by the driver. If so, it shall free up all
  13019. * the frames from the partially assembled sequence.
  13020. *
  13021. * Return
  13022. * true -- if there is matching partially assembled sequence present and all
  13023. * the frames freed with the sequence;
  13024. * false -- if there is no matching partially assembled sequence present so
  13025. * nothing got aborted in the lower layer driver
  13026. **/
  13027. static bool
  13028. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  13029. struct hbq_dmabuf *dmabuf)
  13030. {
  13031. struct fc_frame_header *new_hdr;
  13032. struct fc_frame_header *temp_hdr;
  13033. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  13034. struct hbq_dmabuf *seq_dmabuf = NULL;
  13035. /* Use the hdr_buf to find the sequence that matches this frame */
  13036. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13037. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  13038. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13039. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13040. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13041. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13042. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13043. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13044. continue;
  13045. /* found a pending sequence that matches this frame */
  13046. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13047. break;
  13048. }
  13049. /* Free up all the frames from the partially assembled sequence */
  13050. if (seq_dmabuf) {
  13051. list_for_each_entry_safe(d_buf, n_buf,
  13052. &seq_dmabuf->dbuf.list, list) {
  13053. list_del_init(&d_buf->list);
  13054. lpfc_in_buf_free(vport->phba, d_buf);
  13055. }
  13056. return true;
  13057. }
  13058. return false;
  13059. }
  13060. /**
  13061. * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
  13062. * @vport: pointer to a vitural port
  13063. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13064. *
  13065. * This function tries to abort from the assembed sequence from upper level
  13066. * protocol, described by the information from basic abbort @dmabuf. It
  13067. * checks to see whether such pending context exists at upper level protocol.
  13068. * If so, it shall clean up the pending context.
  13069. *
  13070. * Return
  13071. * true -- if there is matching pending context of the sequence cleaned
  13072. * at ulp;
  13073. * false -- if there is no matching pending context of the sequence present
  13074. * at ulp.
  13075. **/
  13076. static bool
  13077. lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13078. {
  13079. struct lpfc_hba *phba = vport->phba;
  13080. int handled;
  13081. /* Accepting abort at ulp with SLI4 only */
  13082. if (phba->sli_rev < LPFC_SLI_REV4)
  13083. return false;
  13084. /* Register all caring upper level protocols to attend abort */
  13085. handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
  13086. if (handled)
  13087. return true;
  13088. return false;
  13089. }
  13090. /**
  13091. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  13092. * @phba: Pointer to HBA context object.
  13093. * @cmd_iocbq: pointer to the command iocbq structure.
  13094. * @rsp_iocbq: pointer to the response iocbq structure.
  13095. *
  13096. * This function handles the sequence abort response iocb command complete
  13097. * event. It properly releases the memory allocated to the sequence abort
  13098. * accept iocb.
  13099. **/
  13100. static void
  13101. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  13102. struct lpfc_iocbq *cmd_iocbq,
  13103. struct lpfc_iocbq *rsp_iocbq)
  13104. {
  13105. struct lpfc_nodelist *ndlp;
  13106. if (cmd_iocbq) {
  13107. ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
  13108. lpfc_nlp_put(ndlp);
  13109. lpfc_nlp_not_used(ndlp);
  13110. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  13111. }
  13112. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  13113. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  13114. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13115. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  13116. rsp_iocbq->iocb.ulpStatus,
  13117. rsp_iocbq->iocb.un.ulpWord[4]);
  13118. }
  13119. /**
  13120. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  13121. * @phba: Pointer to HBA context object.
  13122. * @xri: xri id in transaction.
  13123. *
  13124. * This function validates the xri maps to the known range of XRIs allocated an
  13125. * used by the driver.
  13126. **/
  13127. uint16_t
  13128. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  13129. uint16_t xri)
  13130. {
  13131. int i;
  13132. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  13133. if (xri == phba->sli4_hba.xri_ids[i])
  13134. return i;
  13135. }
  13136. return NO_XRI;
  13137. }
  13138. /**
  13139. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  13140. * @phba: Pointer to HBA context object.
  13141. * @fc_hdr: pointer to a FC frame header.
  13142. *
  13143. * This function sends a basic response to a previous unsol sequence abort
  13144. * event after aborting the sequence handling.
  13145. **/
  13146. static void
  13147. lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
  13148. struct fc_frame_header *fc_hdr, bool aborted)
  13149. {
  13150. struct lpfc_hba *phba = vport->phba;
  13151. struct lpfc_iocbq *ctiocb = NULL;
  13152. struct lpfc_nodelist *ndlp;
  13153. uint16_t oxid, rxid, xri, lxri;
  13154. uint32_t sid, fctl;
  13155. IOCB_t *icmd;
  13156. int rc;
  13157. if (!lpfc_is_link_up(phba))
  13158. return;
  13159. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13160. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  13161. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  13162. ndlp = lpfc_findnode_did(vport, sid);
  13163. if (!ndlp) {
  13164. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  13165. if (!ndlp) {
  13166. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13167. "1268 Failed to allocate ndlp for "
  13168. "oxid:x%x SID:x%x\n", oxid, sid);
  13169. return;
  13170. }
  13171. lpfc_nlp_init(vport, ndlp, sid);
  13172. /* Put ndlp onto pport node list */
  13173. lpfc_enqueue_node(vport, ndlp);
  13174. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  13175. /* re-setup ndlp without removing from node list */
  13176. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  13177. if (!ndlp) {
  13178. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13179. "3275 Failed to active ndlp found "
  13180. "for oxid:x%x SID:x%x\n", oxid, sid);
  13181. return;
  13182. }
  13183. }
  13184. /* Allocate buffer for rsp iocb */
  13185. ctiocb = lpfc_sli_get_iocbq(phba);
  13186. if (!ctiocb)
  13187. return;
  13188. /* Extract the F_CTL field from FC_HDR */
  13189. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  13190. icmd = &ctiocb->iocb;
  13191. icmd->un.xseq64.bdl.bdeSize = 0;
  13192. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  13193. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13194. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13195. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13196. /* Fill in the rest of iocb fields */
  13197. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13198. icmd->ulpBdeCount = 0;
  13199. icmd->ulpLe = 1;
  13200. icmd->ulpClass = CLASS3;
  13201. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13202. ctiocb->context1 = lpfc_nlp_get(ndlp);
  13203. ctiocb->iocb_cmpl = NULL;
  13204. ctiocb->vport = phba->pport;
  13205. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  13206. ctiocb->sli4_lxritag = NO_XRI;
  13207. ctiocb->sli4_xritag = NO_XRI;
  13208. if (fctl & FC_FC_EX_CTX)
  13209. /* Exchange responder sent the abort so we
  13210. * own the oxid.
  13211. */
  13212. xri = oxid;
  13213. else
  13214. xri = rxid;
  13215. lxri = lpfc_sli4_xri_inrange(phba, xri);
  13216. if (lxri != NO_XRI)
  13217. lpfc_set_rrq_active(phba, ndlp, lxri,
  13218. (xri == oxid) ? rxid : oxid, 0);
  13219. /* For BA_ABTS from exchange responder, if the logical xri with
  13220. * the oxid maps to the FCP XRI range, the port no longer has
  13221. * that exchange context, send a BLS_RJT. Override the IOCB for
  13222. * a BA_RJT.
  13223. */
  13224. if ((fctl & FC_FC_EX_CTX) &&
  13225. (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
  13226. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13227. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13228. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13229. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13230. }
  13231. /* If BA_ABTS failed to abort a partially assembled receive sequence,
  13232. * the driver no longer has that exchange, send a BLS_RJT. Override
  13233. * the IOCB for a BA_RJT.
  13234. */
  13235. if (aborted == false) {
  13236. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13237. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13238. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13239. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13240. }
  13241. if (fctl & FC_FC_EX_CTX) {
  13242. /* ABTS sent by responder to CT exchange, construction
  13243. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13244. * field and RX_ID from ABTS for RX_ID field.
  13245. */
  13246. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13247. } else {
  13248. /* ABTS sent by initiator to CT exchange, construction
  13249. * of BA_ACC will need to allocate a new XRI as for the
  13250. * XRI_TAG field.
  13251. */
  13252. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13253. }
  13254. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13255. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13256. /* Xmit CT abts response on exchange <xid> */
  13257. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  13258. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13259. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13260. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13261. if (rc == IOCB_ERROR) {
  13262. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  13263. "2925 Failed to issue CT ABTS RSP x%x on "
  13264. "xri x%x, Data x%x\n",
  13265. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13266. phba->link_state);
  13267. lpfc_nlp_put(ndlp);
  13268. ctiocb->context1 = NULL;
  13269. lpfc_sli_release_iocbq(phba, ctiocb);
  13270. }
  13271. }
  13272. /**
  13273. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13274. * @vport: Pointer to the vport on which this sequence was received
  13275. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13276. *
  13277. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13278. * receive sequence is only partially assembed by the driver, it shall abort
  13279. * the partially assembled frames for the sequence. Otherwise, if the
  13280. * unsolicited receive sequence has been completely assembled and passed to
  13281. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13282. * unsolicited sequence has been aborted. After that, it will issue a basic
  13283. * accept to accept the abort.
  13284. **/
  13285. void
  13286. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13287. struct hbq_dmabuf *dmabuf)
  13288. {
  13289. struct lpfc_hba *phba = vport->phba;
  13290. struct fc_frame_header fc_hdr;
  13291. uint32_t fctl;
  13292. bool aborted;
  13293. /* Make a copy of fc_hdr before the dmabuf being released */
  13294. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13295. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13296. if (fctl & FC_FC_EX_CTX) {
  13297. /* ABTS by responder to exchange, no cleanup needed */
  13298. aborted = true;
  13299. } else {
  13300. /* ABTS by initiator to exchange, need to do cleanup */
  13301. aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13302. if (aborted == false)
  13303. aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
  13304. }
  13305. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13306. /* Respond with BA_ACC or BA_RJT accordingly */
  13307. lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
  13308. }
  13309. /**
  13310. * lpfc_seq_complete - Indicates if a sequence is complete
  13311. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13312. *
  13313. * This function checks the sequence, starting with the frame described by
  13314. * @dmabuf, to see if all the frames associated with this sequence are present.
  13315. * the frames associated with this sequence are linked to the @dmabuf using the
  13316. * dbuf list. This function looks for two major things. 1) That the first frame
  13317. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13318. * set. 3) That there are no holes in the sequence count. The function will
  13319. * return 1 when the sequence is complete, otherwise it will return 0.
  13320. **/
  13321. static int
  13322. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13323. {
  13324. struct fc_frame_header *hdr;
  13325. struct lpfc_dmabuf *d_buf;
  13326. struct hbq_dmabuf *seq_dmabuf;
  13327. uint32_t fctl;
  13328. int seq_count = 0;
  13329. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13330. /* make sure first fame of sequence has a sequence count of zero */
  13331. if (hdr->fh_seq_cnt != seq_count)
  13332. return 0;
  13333. fctl = (hdr->fh_f_ctl[0] << 16 |
  13334. hdr->fh_f_ctl[1] << 8 |
  13335. hdr->fh_f_ctl[2]);
  13336. /* If last frame of sequence we can return success. */
  13337. if (fctl & FC_FC_END_SEQ)
  13338. return 1;
  13339. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13340. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13341. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13342. /* If there is a hole in the sequence count then fail. */
  13343. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13344. return 0;
  13345. fctl = (hdr->fh_f_ctl[0] << 16 |
  13346. hdr->fh_f_ctl[1] << 8 |
  13347. hdr->fh_f_ctl[2]);
  13348. /* If last frame of sequence we can return success. */
  13349. if (fctl & FC_FC_END_SEQ)
  13350. return 1;
  13351. }
  13352. return 0;
  13353. }
  13354. /**
  13355. * lpfc_prep_seq - Prep sequence for ULP processing
  13356. * @vport: Pointer to the vport on which this sequence was received
  13357. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13358. *
  13359. * This function takes a sequence, described by a list of frames, and creates
  13360. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13361. * used to issue to the generic unsolicited sequence handler. This routine
  13362. * returns a pointer to the first iocbq in the list. If the function is unable
  13363. * to allocate an iocbq then it throw out the received frames that were not
  13364. * able to be described and return a pointer to the first iocbq. If unable to
  13365. * allocate any iocbqs (including the first) this function will return NULL.
  13366. **/
  13367. static struct lpfc_iocbq *
  13368. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13369. {
  13370. struct hbq_dmabuf *hbq_buf;
  13371. struct lpfc_dmabuf *d_buf, *n_buf;
  13372. struct lpfc_iocbq *first_iocbq, *iocbq;
  13373. struct fc_frame_header *fc_hdr;
  13374. uint32_t sid;
  13375. uint32_t len, tot_len;
  13376. struct ulp_bde64 *pbde;
  13377. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13378. /* remove from receive buffer list */
  13379. list_del_init(&seq_dmabuf->hbuf.list);
  13380. lpfc_update_rcv_time_stamp(vport);
  13381. /* get the Remote Port's SID */
  13382. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13383. tot_len = 0;
  13384. /* Get an iocbq struct to fill in. */
  13385. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13386. if (first_iocbq) {
  13387. /* Initialize the first IOCB. */
  13388. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13389. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13390. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13391. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13392. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13393. first_iocbq->iocb.un.rcvels.parmRo =
  13394. sli4_did_from_fc_hdr(fc_hdr);
  13395. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13396. } else
  13397. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13398. first_iocbq->iocb.ulpContext = NO_XRI;
  13399. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13400. be16_to_cpu(fc_hdr->fh_ox_id);
  13401. /* iocbq is prepped for internal consumption. Physical vpi. */
  13402. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13403. vport->phba->vpi_ids[vport->vpi];
  13404. /* put the first buffer into the first IOCBq */
  13405. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13406. first_iocbq->context3 = NULL;
  13407. first_iocbq->iocb.ulpBdeCount = 1;
  13408. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13409. LPFC_DATA_BUF_SIZE;
  13410. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13411. tot_len = bf_get(lpfc_rcqe_length,
  13412. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13413. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13414. }
  13415. iocbq = first_iocbq;
  13416. /*
  13417. * Each IOCBq can have two Buffers assigned, so go through the list
  13418. * of buffers for this sequence and save two buffers in each IOCBq
  13419. */
  13420. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13421. if (!iocbq) {
  13422. lpfc_in_buf_free(vport->phba, d_buf);
  13423. continue;
  13424. }
  13425. if (!iocbq->context3) {
  13426. iocbq->context3 = d_buf;
  13427. iocbq->iocb.ulpBdeCount++;
  13428. pbde = (struct ulp_bde64 *)
  13429. &iocbq->iocb.unsli3.sli3Words[4];
  13430. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13431. /* We need to get the size out of the right CQE */
  13432. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13433. len = bf_get(lpfc_rcqe_length,
  13434. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13435. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13436. tot_len += len;
  13437. } else {
  13438. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13439. if (!iocbq) {
  13440. if (first_iocbq) {
  13441. first_iocbq->iocb.ulpStatus =
  13442. IOSTAT_FCP_RSP_ERROR;
  13443. first_iocbq->iocb.un.ulpWord[4] =
  13444. IOERR_NO_RESOURCES;
  13445. }
  13446. lpfc_in_buf_free(vport->phba, d_buf);
  13447. continue;
  13448. }
  13449. iocbq->context2 = d_buf;
  13450. iocbq->context3 = NULL;
  13451. iocbq->iocb.ulpBdeCount = 1;
  13452. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13453. LPFC_DATA_BUF_SIZE;
  13454. /* We need to get the size out of the right CQE */
  13455. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13456. len = bf_get(lpfc_rcqe_length,
  13457. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13458. tot_len += len;
  13459. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13460. iocbq->iocb.un.rcvels.remoteID = sid;
  13461. list_add_tail(&iocbq->list, &first_iocbq->list);
  13462. }
  13463. }
  13464. return first_iocbq;
  13465. }
  13466. static void
  13467. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13468. struct hbq_dmabuf *seq_dmabuf)
  13469. {
  13470. struct fc_frame_header *fc_hdr;
  13471. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13472. struct lpfc_hba *phba = vport->phba;
  13473. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13474. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13475. if (!iocbq) {
  13476. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13477. "2707 Ring %d handler: Failed to allocate "
  13478. "iocb Rctl x%x Type x%x received\n",
  13479. LPFC_ELS_RING,
  13480. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13481. return;
  13482. }
  13483. if (!lpfc_complete_unsol_iocb(phba,
  13484. &phba->sli.ring[LPFC_ELS_RING],
  13485. iocbq, fc_hdr->fh_r_ctl,
  13486. fc_hdr->fh_type))
  13487. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13488. "2540 Ring %d handler: unexpected Rctl "
  13489. "x%x Type x%x received\n",
  13490. LPFC_ELS_RING,
  13491. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13492. /* Free iocb created in lpfc_prep_seq */
  13493. list_for_each_entry_safe(curr_iocb, next_iocb,
  13494. &iocbq->list, list) {
  13495. list_del_init(&curr_iocb->list);
  13496. lpfc_sli_release_iocbq(phba, curr_iocb);
  13497. }
  13498. lpfc_sli_release_iocbq(phba, iocbq);
  13499. }
  13500. /**
  13501. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13502. * @phba: Pointer to HBA context object.
  13503. *
  13504. * This function is called with no lock held. This function processes all
  13505. * the received buffers and gives it to upper layers when a received buffer
  13506. * indicates that it is the final frame in the sequence. The interrupt
  13507. * service routine processes received buffers at interrupt contexts and adds
  13508. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13509. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13510. * appropriate receive function when the final frame in a sequence is received.
  13511. **/
  13512. void
  13513. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13514. struct hbq_dmabuf *dmabuf)
  13515. {
  13516. struct hbq_dmabuf *seq_dmabuf;
  13517. struct fc_frame_header *fc_hdr;
  13518. struct lpfc_vport *vport;
  13519. uint32_t fcfi;
  13520. uint32_t did;
  13521. /* Process each received buffer */
  13522. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13523. /* check to see if this a valid type of frame */
  13524. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13525. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13526. return;
  13527. }
  13528. if ((bf_get(lpfc_cqe_code,
  13529. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13530. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13531. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13532. else
  13533. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13534. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13535. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13536. if (!vport) {
  13537. /* throw out the frame */
  13538. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13539. return;
  13540. }
  13541. /* d_id this frame is directed to */
  13542. did = sli4_did_from_fc_hdr(fc_hdr);
  13543. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13544. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13545. (did != Fabric_DID)) {
  13546. /*
  13547. * Throw out the frame if we are not pt2pt.
  13548. * The pt2pt protocol allows for discovery frames
  13549. * to be received without a registered VPI.
  13550. */
  13551. if (!(vport->fc_flag & FC_PT2PT) ||
  13552. (phba->link_state == LPFC_HBA_READY)) {
  13553. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13554. return;
  13555. }
  13556. }
  13557. /* Handle the basic abort sequence (BA_ABTS) event */
  13558. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13559. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13560. return;
  13561. }
  13562. /* Link this frame */
  13563. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13564. if (!seq_dmabuf) {
  13565. /* unable to add frame to vport - throw it out */
  13566. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13567. return;
  13568. }
  13569. /* If not last frame in sequence continue processing frames. */
  13570. if (!lpfc_seq_complete(seq_dmabuf))
  13571. return;
  13572. /* Send the complete sequence to the upper layer protocol */
  13573. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13574. }
  13575. /**
  13576. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13577. * @phba: pointer to lpfc hba data structure.
  13578. *
  13579. * This routine is invoked to post rpi header templates to the
  13580. * HBA consistent with the SLI-4 interface spec. This routine
  13581. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13582. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13583. *
  13584. * This routine does not require any locks. It's usage is expected
  13585. * to be driver load or reset recovery when the driver is
  13586. * sequential.
  13587. *
  13588. * Return codes
  13589. * 0 - successful
  13590. * -EIO - The mailbox failed to complete successfully.
  13591. * When this error occurs, the driver is not guaranteed
  13592. * to have any rpi regions posted to the device and
  13593. * must either attempt to repost the regions or take a
  13594. * fatal error.
  13595. **/
  13596. int
  13597. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13598. {
  13599. struct lpfc_rpi_hdr *rpi_page;
  13600. uint32_t rc = 0;
  13601. uint16_t lrpi = 0;
  13602. /* SLI4 ports that support extents do not require RPI headers. */
  13603. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13604. goto exit;
  13605. if (phba->sli4_hba.extents_in_use)
  13606. return -EIO;
  13607. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13608. /*
  13609. * Assign the rpi headers a physical rpi only if the driver
  13610. * has not initialized those resources. A port reset only
  13611. * needs the headers posted.
  13612. */
  13613. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13614. LPFC_RPI_RSRC_RDY)
  13615. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13616. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13617. if (rc != MBX_SUCCESS) {
  13618. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13619. "2008 Error %d posting all rpi "
  13620. "headers\n", rc);
  13621. rc = -EIO;
  13622. break;
  13623. }
  13624. }
  13625. exit:
  13626. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13627. LPFC_RPI_RSRC_RDY);
  13628. return rc;
  13629. }
  13630. /**
  13631. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13632. * @phba: pointer to lpfc hba data structure.
  13633. * @rpi_page: pointer to the rpi memory region.
  13634. *
  13635. * This routine is invoked to post a single rpi header to the
  13636. * HBA consistent with the SLI-4 interface spec. This memory region
  13637. * maps up to 64 rpi context regions.
  13638. *
  13639. * Return codes
  13640. * 0 - successful
  13641. * -ENOMEM - No available memory
  13642. * -EIO - The mailbox failed to complete successfully.
  13643. **/
  13644. int
  13645. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13646. {
  13647. LPFC_MBOXQ_t *mboxq;
  13648. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13649. uint32_t rc = 0;
  13650. uint32_t shdr_status, shdr_add_status;
  13651. union lpfc_sli4_cfg_shdr *shdr;
  13652. /* SLI4 ports that support extents do not require RPI headers. */
  13653. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13654. return rc;
  13655. if (phba->sli4_hba.extents_in_use)
  13656. return -EIO;
  13657. /* The port is notified of the header region via a mailbox command. */
  13658. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13659. if (!mboxq) {
  13660. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13661. "2001 Unable to allocate memory for issuing "
  13662. "SLI_CONFIG_SPECIAL mailbox command\n");
  13663. return -ENOMEM;
  13664. }
  13665. /* Post all rpi memory regions to the port. */
  13666. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13667. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13668. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13669. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13670. sizeof(struct lpfc_sli4_cfg_mhdr),
  13671. LPFC_SLI4_MBX_EMBED);
  13672. /* Post the physical rpi to the port for this rpi header. */
  13673. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13674. rpi_page->start_rpi);
  13675. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13676. hdr_tmpl, rpi_page->page_count);
  13677. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13678. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13679. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13680. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13681. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13682. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13683. if (rc != MBX_TIMEOUT)
  13684. mempool_free(mboxq, phba->mbox_mem_pool);
  13685. if (shdr_status || shdr_add_status || rc) {
  13686. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13687. "2514 POST_RPI_HDR mailbox failed with "
  13688. "status x%x add_status x%x, mbx status x%x\n",
  13689. shdr_status, shdr_add_status, rc);
  13690. rc = -ENXIO;
  13691. }
  13692. return rc;
  13693. }
  13694. /**
  13695. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13696. * @phba: pointer to lpfc hba data structure.
  13697. *
  13698. * This routine is invoked to post rpi header templates to the
  13699. * HBA consistent with the SLI-4 interface spec. This routine
  13700. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13701. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13702. *
  13703. * Returns
  13704. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13705. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13706. **/
  13707. int
  13708. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13709. {
  13710. unsigned long rpi;
  13711. uint16_t max_rpi, rpi_limit;
  13712. uint16_t rpi_remaining, lrpi = 0;
  13713. struct lpfc_rpi_hdr *rpi_hdr;
  13714. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13715. rpi_limit = phba->sli4_hba.next_rpi;
  13716. /*
  13717. * Fetch the next logical rpi. Because this index is logical,
  13718. * the driver starts at 0 each time.
  13719. */
  13720. spin_lock_irq(&phba->hbalock);
  13721. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13722. if (rpi >= rpi_limit)
  13723. rpi = LPFC_RPI_ALLOC_ERROR;
  13724. else {
  13725. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13726. phba->sli4_hba.max_cfg_param.rpi_used++;
  13727. phba->sli4_hba.rpi_count++;
  13728. }
  13729. /*
  13730. * Don't try to allocate more rpi header regions if the device limit
  13731. * has been exhausted.
  13732. */
  13733. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13734. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13735. spin_unlock_irq(&phba->hbalock);
  13736. return rpi;
  13737. }
  13738. /*
  13739. * RPI header postings are not required for SLI4 ports capable of
  13740. * extents.
  13741. */
  13742. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13743. spin_unlock_irq(&phba->hbalock);
  13744. return rpi;
  13745. }
  13746. /*
  13747. * If the driver is running low on rpi resources, allocate another
  13748. * page now. Note that the next_rpi value is used because
  13749. * it represents how many are actually in use whereas max_rpi notes
  13750. * how many are supported max by the device.
  13751. */
  13752. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13753. spin_unlock_irq(&phba->hbalock);
  13754. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13755. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13756. if (!rpi_hdr) {
  13757. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13758. "2002 Error Could not grow rpi "
  13759. "count\n");
  13760. } else {
  13761. lrpi = rpi_hdr->start_rpi;
  13762. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13763. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13764. }
  13765. }
  13766. return rpi;
  13767. }
  13768. /**
  13769. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13770. * @phba: pointer to lpfc hba data structure.
  13771. *
  13772. * This routine is invoked to release an rpi to the pool of
  13773. * available rpis maintained by the driver.
  13774. **/
  13775. void
  13776. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13777. {
  13778. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13779. phba->sli4_hba.rpi_count--;
  13780. phba->sli4_hba.max_cfg_param.rpi_used--;
  13781. }
  13782. }
  13783. /**
  13784. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13785. * @phba: pointer to lpfc hba data structure.
  13786. *
  13787. * This routine is invoked to release an rpi to the pool of
  13788. * available rpis maintained by the driver.
  13789. **/
  13790. void
  13791. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13792. {
  13793. spin_lock_irq(&phba->hbalock);
  13794. __lpfc_sli4_free_rpi(phba, rpi);
  13795. spin_unlock_irq(&phba->hbalock);
  13796. }
  13797. /**
  13798. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13799. * @phba: pointer to lpfc hba data structure.
  13800. *
  13801. * This routine is invoked to remove the memory region that
  13802. * provided rpi via a bitmask.
  13803. **/
  13804. void
  13805. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13806. {
  13807. kfree(phba->sli4_hba.rpi_bmask);
  13808. kfree(phba->sli4_hba.rpi_ids);
  13809. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13810. }
  13811. /**
  13812. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13813. * @phba: pointer to lpfc hba data structure.
  13814. *
  13815. * This routine is invoked to remove the memory region that
  13816. * provided rpi via a bitmask.
  13817. **/
  13818. int
  13819. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13820. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13821. {
  13822. LPFC_MBOXQ_t *mboxq;
  13823. struct lpfc_hba *phba = ndlp->phba;
  13824. int rc;
  13825. /* The port is notified of the header region via a mailbox command. */
  13826. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13827. if (!mboxq)
  13828. return -ENOMEM;
  13829. /* Post all rpi memory regions to the port. */
  13830. lpfc_resume_rpi(mboxq, ndlp);
  13831. if (cmpl) {
  13832. mboxq->mbox_cmpl = cmpl;
  13833. mboxq->context1 = arg;
  13834. mboxq->context2 = ndlp;
  13835. } else
  13836. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13837. mboxq->vport = ndlp->vport;
  13838. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13839. if (rc == MBX_NOT_FINISHED) {
  13840. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13841. "2010 Resume RPI Mailbox failed "
  13842. "status %d, mbxStatus x%x\n", rc,
  13843. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13844. mempool_free(mboxq, phba->mbox_mem_pool);
  13845. return -EIO;
  13846. }
  13847. return 0;
  13848. }
  13849. /**
  13850. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13851. * @vport: Pointer to the vport for which the vpi is being initialized
  13852. *
  13853. * This routine is invoked to activate a vpi with the port.
  13854. *
  13855. * Returns:
  13856. * 0 success
  13857. * -Evalue otherwise
  13858. **/
  13859. int
  13860. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13861. {
  13862. LPFC_MBOXQ_t *mboxq;
  13863. int rc = 0;
  13864. int retval = MBX_SUCCESS;
  13865. uint32_t mbox_tmo;
  13866. struct lpfc_hba *phba = vport->phba;
  13867. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13868. if (!mboxq)
  13869. return -ENOMEM;
  13870. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13871. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13872. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13873. if (rc != MBX_SUCCESS) {
  13874. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13875. "2022 INIT VPI Mailbox failed "
  13876. "status %d, mbxStatus x%x\n", rc,
  13877. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13878. retval = -EIO;
  13879. }
  13880. if (rc != MBX_TIMEOUT)
  13881. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13882. return retval;
  13883. }
  13884. /**
  13885. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13886. * @phba: pointer to lpfc hba data structure.
  13887. * @mboxq: Pointer to mailbox object.
  13888. *
  13889. * This routine is invoked to manually add a single FCF record. The caller
  13890. * must pass a completely initialized FCF_Record. This routine takes
  13891. * care of the nonembedded mailbox operations.
  13892. **/
  13893. static void
  13894. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13895. {
  13896. void *virt_addr;
  13897. union lpfc_sli4_cfg_shdr *shdr;
  13898. uint32_t shdr_status, shdr_add_status;
  13899. virt_addr = mboxq->sge_array->addr[0];
  13900. /* The IOCTL status is embedded in the mailbox subheader. */
  13901. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13902. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13903. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13904. if ((shdr_status || shdr_add_status) &&
  13905. (shdr_status != STATUS_FCF_IN_USE))
  13906. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13907. "2558 ADD_FCF_RECORD mailbox failed with "
  13908. "status x%x add_status x%x\n",
  13909. shdr_status, shdr_add_status);
  13910. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13911. }
  13912. /**
  13913. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13914. * @phba: pointer to lpfc hba data structure.
  13915. * @fcf_record: pointer to the initialized fcf record to add.
  13916. *
  13917. * This routine is invoked to manually add a single FCF record. The caller
  13918. * must pass a completely initialized FCF_Record. This routine takes
  13919. * care of the nonembedded mailbox operations.
  13920. **/
  13921. int
  13922. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13923. {
  13924. int rc = 0;
  13925. LPFC_MBOXQ_t *mboxq;
  13926. uint8_t *bytep;
  13927. void *virt_addr;
  13928. dma_addr_t phys_addr;
  13929. struct lpfc_mbx_sge sge;
  13930. uint32_t alloc_len, req_len;
  13931. uint32_t fcfindex;
  13932. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13933. if (!mboxq) {
  13934. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13935. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13936. return -ENOMEM;
  13937. }
  13938. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13939. sizeof(uint32_t);
  13940. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13941. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13942. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13943. req_len, LPFC_SLI4_MBX_NEMBED);
  13944. if (alloc_len < req_len) {
  13945. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13946. "2523 Allocated DMA memory size (x%x) is "
  13947. "less than the requested DMA memory "
  13948. "size (x%x)\n", alloc_len, req_len);
  13949. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13950. return -ENOMEM;
  13951. }
  13952. /*
  13953. * Get the first SGE entry from the non-embedded DMA memory. This
  13954. * routine only uses a single SGE.
  13955. */
  13956. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13957. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13958. virt_addr = mboxq->sge_array->addr[0];
  13959. /*
  13960. * Configure the FCF record for FCFI 0. This is the driver's
  13961. * hardcoded default and gets used in nonFIP mode.
  13962. */
  13963. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13964. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13965. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13966. /*
  13967. * Copy the fcf_index and the FCF Record Data. The data starts after
  13968. * the FCoE header plus word10. The data copy needs to be endian
  13969. * correct.
  13970. */
  13971. bytep += sizeof(uint32_t);
  13972. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13973. mboxq->vport = phba->pport;
  13974. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13975. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13976. if (rc == MBX_NOT_FINISHED) {
  13977. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13978. "2515 ADD_FCF_RECORD mailbox failed with "
  13979. "status 0x%x\n", rc);
  13980. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13981. rc = -EIO;
  13982. } else
  13983. rc = 0;
  13984. return rc;
  13985. }
  13986. /**
  13987. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13988. * @phba: pointer to lpfc hba data structure.
  13989. * @fcf_record: pointer to the fcf record to write the default data.
  13990. * @fcf_index: FCF table entry index.
  13991. *
  13992. * This routine is invoked to build the driver's default FCF record. The
  13993. * values used are hardcoded. This routine handles memory initialization.
  13994. *
  13995. **/
  13996. void
  13997. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13998. struct fcf_record *fcf_record,
  13999. uint16_t fcf_index)
  14000. {
  14001. memset(fcf_record, 0, sizeof(struct fcf_record));
  14002. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  14003. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  14004. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  14005. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  14006. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  14007. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  14008. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  14009. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  14010. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  14011. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  14012. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  14013. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  14014. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  14015. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  14016. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  14017. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  14018. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  14019. /* Set the VLAN bit map */
  14020. if (phba->valid_vlan) {
  14021. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  14022. = 1 << (phba->vlan_id % 8);
  14023. }
  14024. }
  14025. /**
  14026. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  14027. * @phba: pointer to lpfc hba data structure.
  14028. * @fcf_index: FCF table entry offset.
  14029. *
  14030. * This routine is invoked to scan the entire FCF table by reading FCF
  14031. * record and processing it one at a time starting from the @fcf_index
  14032. * for initial FCF discovery or fast FCF failover rediscovery.
  14033. *
  14034. * Return 0 if the mailbox command is submitted successfully, none 0
  14035. * otherwise.
  14036. **/
  14037. int
  14038. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14039. {
  14040. int rc = 0, error;
  14041. LPFC_MBOXQ_t *mboxq;
  14042. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  14043. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  14044. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14045. if (!mboxq) {
  14046. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14047. "2000 Failed to allocate mbox for "
  14048. "READ_FCF cmd\n");
  14049. error = -ENOMEM;
  14050. goto fail_fcf_scan;
  14051. }
  14052. /* Construct the read FCF record mailbox command */
  14053. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14054. if (rc) {
  14055. error = -EINVAL;
  14056. goto fail_fcf_scan;
  14057. }
  14058. /* Issue the mailbox command asynchronously */
  14059. mboxq->vport = phba->pport;
  14060. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  14061. spin_lock_irq(&phba->hbalock);
  14062. phba->hba_flag |= FCF_TS_INPROG;
  14063. spin_unlock_irq(&phba->hbalock);
  14064. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14065. if (rc == MBX_NOT_FINISHED)
  14066. error = -EIO;
  14067. else {
  14068. /* Reset eligible FCF count for new scan */
  14069. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  14070. phba->fcf.eligible_fcf_cnt = 0;
  14071. error = 0;
  14072. }
  14073. fail_fcf_scan:
  14074. if (error) {
  14075. if (mboxq)
  14076. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14077. /* FCF scan failed, clear FCF_TS_INPROG flag */
  14078. spin_lock_irq(&phba->hbalock);
  14079. phba->hba_flag &= ~FCF_TS_INPROG;
  14080. spin_unlock_irq(&phba->hbalock);
  14081. }
  14082. return error;
  14083. }
  14084. /**
  14085. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  14086. * @phba: pointer to lpfc hba data structure.
  14087. * @fcf_index: FCF table entry offset.
  14088. *
  14089. * This routine is invoked to read an FCF record indicated by @fcf_index
  14090. * and to use it for FLOGI roundrobin FCF failover.
  14091. *
  14092. * Return 0 if the mailbox command is submitted successfully, none 0
  14093. * otherwise.
  14094. **/
  14095. int
  14096. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14097. {
  14098. int rc = 0, error;
  14099. LPFC_MBOXQ_t *mboxq;
  14100. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14101. if (!mboxq) {
  14102. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14103. "2763 Failed to allocate mbox for "
  14104. "READ_FCF cmd\n");
  14105. error = -ENOMEM;
  14106. goto fail_fcf_read;
  14107. }
  14108. /* Construct the read FCF record mailbox command */
  14109. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14110. if (rc) {
  14111. error = -EINVAL;
  14112. goto fail_fcf_read;
  14113. }
  14114. /* Issue the mailbox command asynchronously */
  14115. mboxq->vport = phba->pport;
  14116. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  14117. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14118. if (rc == MBX_NOT_FINISHED)
  14119. error = -EIO;
  14120. else
  14121. error = 0;
  14122. fail_fcf_read:
  14123. if (error && mboxq)
  14124. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14125. return error;
  14126. }
  14127. /**
  14128. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  14129. * @phba: pointer to lpfc hba data structure.
  14130. * @fcf_index: FCF table entry offset.
  14131. *
  14132. * This routine is invoked to read an FCF record indicated by @fcf_index to
  14133. * determine whether it's eligible for FLOGI roundrobin failover list.
  14134. *
  14135. * Return 0 if the mailbox command is submitted successfully, none 0
  14136. * otherwise.
  14137. **/
  14138. int
  14139. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14140. {
  14141. int rc = 0, error;
  14142. LPFC_MBOXQ_t *mboxq;
  14143. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14144. if (!mboxq) {
  14145. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14146. "2758 Failed to allocate mbox for "
  14147. "READ_FCF cmd\n");
  14148. error = -ENOMEM;
  14149. goto fail_fcf_read;
  14150. }
  14151. /* Construct the read FCF record mailbox command */
  14152. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14153. if (rc) {
  14154. error = -EINVAL;
  14155. goto fail_fcf_read;
  14156. }
  14157. /* Issue the mailbox command asynchronously */
  14158. mboxq->vport = phba->pport;
  14159. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  14160. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14161. if (rc == MBX_NOT_FINISHED)
  14162. error = -EIO;
  14163. else
  14164. error = 0;
  14165. fail_fcf_read:
  14166. if (error && mboxq)
  14167. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14168. return error;
  14169. }
  14170. /**
  14171. * lpfc_check_next_fcf_pri
  14172. * phba pointer to the lpfc_hba struct for this port.
  14173. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  14174. * routine when the rr_bmask is empty. The FCF indecies are put into the
  14175. * rr_bmask based on their priority level. Starting from the highest priority
  14176. * to the lowest. The most likely FCF candidate will be in the highest
  14177. * priority group. When this routine is called it searches the fcf_pri list for
  14178. * next lowest priority group and repopulates the rr_bmask with only those
  14179. * fcf_indexes.
  14180. * returns:
  14181. * 1=success 0=failure
  14182. **/
  14183. int
  14184. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  14185. {
  14186. uint16_t next_fcf_pri;
  14187. uint16_t last_index;
  14188. struct lpfc_fcf_pri *fcf_pri;
  14189. int rc;
  14190. int ret = 0;
  14191. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  14192. LPFC_SLI4_FCF_TBL_INDX_MAX);
  14193. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14194. "3060 Last IDX %d\n", last_index);
  14195. if (list_empty(&phba->fcf.fcf_pri_list)) {
  14196. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14197. "3061 Last IDX %d\n", last_index);
  14198. return 0; /* Empty rr list */
  14199. }
  14200. next_fcf_pri = 0;
  14201. /*
  14202. * Clear the rr_bmask and set all of the bits that are at this
  14203. * priority.
  14204. */
  14205. memset(phba->fcf.fcf_rr_bmask, 0,
  14206. sizeof(*phba->fcf.fcf_rr_bmask));
  14207. spin_lock_irq(&phba->hbalock);
  14208. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14209. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  14210. continue;
  14211. /*
  14212. * the 1st priority that has not FLOGI failed
  14213. * will be the highest.
  14214. */
  14215. if (!next_fcf_pri)
  14216. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14217. spin_unlock_irq(&phba->hbalock);
  14218. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14219. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14220. fcf_pri->fcf_rec.fcf_index);
  14221. if (rc)
  14222. return 0;
  14223. }
  14224. spin_lock_irq(&phba->hbalock);
  14225. }
  14226. /*
  14227. * if next_fcf_pri was not set above and the list is not empty then
  14228. * we have failed flogis on all of them. So reset flogi failed
  14229. * and start at the beginning.
  14230. */
  14231. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  14232. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14233. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14234. /*
  14235. * the 1st priority that has not FLOGI failed
  14236. * will be the highest.
  14237. */
  14238. if (!next_fcf_pri)
  14239. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14240. spin_unlock_irq(&phba->hbalock);
  14241. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14242. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14243. fcf_pri->fcf_rec.fcf_index);
  14244. if (rc)
  14245. return 0;
  14246. }
  14247. spin_lock_irq(&phba->hbalock);
  14248. }
  14249. } else
  14250. ret = 1;
  14251. spin_unlock_irq(&phba->hbalock);
  14252. return ret;
  14253. }
  14254. /**
  14255. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14256. * @phba: pointer to lpfc hba data structure.
  14257. *
  14258. * This routine is to get the next eligible FCF record index in a round
  14259. * robin fashion. If the next eligible FCF record index equals to the
  14260. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14261. * shall be returned, otherwise, the next eligible FCF record's index
  14262. * shall be returned.
  14263. **/
  14264. uint16_t
  14265. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14266. {
  14267. uint16_t next_fcf_index;
  14268. initial_priority:
  14269. /* Search start from next bit of currently registered FCF index */
  14270. next_fcf_index = phba->fcf.current_rec.fcf_indx;
  14271. next_priority:
  14272. /* Determine the next fcf index to check */
  14273. next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
  14274. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14275. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14276. next_fcf_index);
  14277. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14278. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14279. /*
  14280. * If we have wrapped then we need to clear the bits that
  14281. * have been tested so that we can detect when we should
  14282. * change the priority level.
  14283. */
  14284. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14285. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14286. }
  14287. /* Check roundrobin failover list empty condition */
  14288. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14289. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14290. /*
  14291. * If next fcf index is not found check if there are lower
  14292. * Priority level fcf's in the fcf_priority list.
  14293. * Set up the rr_bmask with all of the avaiable fcf bits
  14294. * at that level and continue the selection process.
  14295. */
  14296. if (lpfc_check_next_fcf_pri_level(phba))
  14297. goto initial_priority;
  14298. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14299. "2844 No roundrobin failover FCF available\n");
  14300. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14301. return LPFC_FCOE_FCF_NEXT_NONE;
  14302. else {
  14303. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14304. "3063 Only FCF available idx %d, flag %x\n",
  14305. next_fcf_index,
  14306. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14307. return next_fcf_index;
  14308. }
  14309. }
  14310. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14311. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14312. LPFC_FCF_FLOGI_FAILED)
  14313. goto next_priority;
  14314. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14315. "2845 Get next roundrobin failover FCF (x%x)\n",
  14316. next_fcf_index);
  14317. return next_fcf_index;
  14318. }
  14319. /**
  14320. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14321. * @phba: pointer to lpfc hba data structure.
  14322. *
  14323. * This routine sets the FCF record index in to the eligible bmask for
  14324. * roundrobin failover search. It checks to make sure that the index
  14325. * does not go beyond the range of the driver allocated bmask dimension
  14326. * before setting the bit.
  14327. *
  14328. * Returns 0 if the index bit successfully set, otherwise, it returns
  14329. * -EINVAL.
  14330. **/
  14331. int
  14332. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14333. {
  14334. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14335. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14336. "2610 FCF (x%x) reached driver's book "
  14337. "keeping dimension:x%x\n",
  14338. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14339. return -EINVAL;
  14340. }
  14341. /* Set the eligible FCF record index bmask */
  14342. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14343. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14344. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14345. "bmask\n", fcf_index);
  14346. return 0;
  14347. }
  14348. /**
  14349. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14350. * @phba: pointer to lpfc hba data structure.
  14351. *
  14352. * This routine clears the FCF record index from the eligible bmask for
  14353. * roundrobin failover search. It checks to make sure that the index
  14354. * does not go beyond the range of the driver allocated bmask dimension
  14355. * before clearing the bit.
  14356. **/
  14357. void
  14358. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14359. {
  14360. struct lpfc_fcf_pri *fcf_pri;
  14361. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14362. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14363. "2762 FCF (x%x) reached driver's book "
  14364. "keeping dimension:x%x\n",
  14365. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14366. return;
  14367. }
  14368. /* Clear the eligible FCF record index bmask */
  14369. spin_lock_irq(&phba->hbalock);
  14370. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14371. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14372. list_del_init(&fcf_pri->list);
  14373. break;
  14374. }
  14375. }
  14376. spin_unlock_irq(&phba->hbalock);
  14377. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14378. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14379. "2791 Clear FCF (x%x) from roundrobin failover "
  14380. "bmask\n", fcf_index);
  14381. }
  14382. /**
  14383. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14384. * @phba: pointer to lpfc hba data structure.
  14385. *
  14386. * This routine is the completion routine for the rediscover FCF table mailbox
  14387. * command. If the mailbox command returned failure, it will try to stop the
  14388. * FCF rediscover wait timer.
  14389. **/
  14390. void
  14391. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14392. {
  14393. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14394. uint32_t shdr_status, shdr_add_status;
  14395. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14396. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14397. &redisc_fcf->header.cfg_shdr.response);
  14398. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14399. &redisc_fcf->header.cfg_shdr.response);
  14400. if (shdr_status || shdr_add_status) {
  14401. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14402. "2746 Requesting for FCF rediscovery failed "
  14403. "status x%x add_status x%x\n",
  14404. shdr_status, shdr_add_status);
  14405. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14406. spin_lock_irq(&phba->hbalock);
  14407. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14408. spin_unlock_irq(&phba->hbalock);
  14409. /*
  14410. * CVL event triggered FCF rediscover request failed,
  14411. * last resort to re-try current registered FCF entry.
  14412. */
  14413. lpfc_retry_pport_discovery(phba);
  14414. } else {
  14415. spin_lock_irq(&phba->hbalock);
  14416. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14417. spin_unlock_irq(&phba->hbalock);
  14418. /*
  14419. * DEAD FCF event triggered FCF rediscover request
  14420. * failed, last resort to fail over as a link down
  14421. * to FCF registration.
  14422. */
  14423. lpfc_sli4_fcf_dead_failthrough(phba);
  14424. }
  14425. } else {
  14426. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14427. "2775 Start FCF rediscover quiescent timer\n");
  14428. /*
  14429. * Start FCF rediscovery wait timer for pending FCF
  14430. * before rescan FCF record table.
  14431. */
  14432. lpfc_fcf_redisc_wait_start_timer(phba);
  14433. }
  14434. mempool_free(mbox, phba->mbox_mem_pool);
  14435. }
  14436. /**
  14437. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14438. * @phba: pointer to lpfc hba data structure.
  14439. *
  14440. * This routine is invoked to request for rediscovery of the entire FCF table
  14441. * by the port.
  14442. **/
  14443. int
  14444. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14445. {
  14446. LPFC_MBOXQ_t *mbox;
  14447. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14448. int rc, length;
  14449. /* Cancel retry delay timers to all vports before FCF rediscover */
  14450. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14451. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14452. if (!mbox) {
  14453. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14454. "2745 Failed to allocate mbox for "
  14455. "requesting FCF rediscover.\n");
  14456. return -ENOMEM;
  14457. }
  14458. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14459. sizeof(struct lpfc_sli4_cfg_mhdr));
  14460. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14461. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14462. length, LPFC_SLI4_MBX_EMBED);
  14463. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14464. /* Set count to 0 for invalidating the entire FCF database */
  14465. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14466. /* Issue the mailbox command asynchronously */
  14467. mbox->vport = phba->pport;
  14468. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14469. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14470. if (rc == MBX_NOT_FINISHED) {
  14471. mempool_free(mbox, phba->mbox_mem_pool);
  14472. return -EIO;
  14473. }
  14474. return 0;
  14475. }
  14476. /**
  14477. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14478. * @phba: pointer to lpfc hba data structure.
  14479. *
  14480. * This function is the failover routine as a last resort to the FCF DEAD
  14481. * event when driver failed to perform fast FCF failover.
  14482. **/
  14483. void
  14484. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14485. {
  14486. uint32_t link_state;
  14487. /*
  14488. * Last resort as FCF DEAD event failover will treat this as
  14489. * a link down, but save the link state because we don't want
  14490. * it to be changed to Link Down unless it is already down.
  14491. */
  14492. link_state = phba->link_state;
  14493. lpfc_linkdown(phba);
  14494. phba->link_state = link_state;
  14495. /* Unregister FCF if no devices connected to it */
  14496. lpfc_unregister_unused_fcf(phba);
  14497. }
  14498. /**
  14499. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14500. * @phba: pointer to lpfc hba data structure.
  14501. * @rgn23_data: pointer to configure region 23 data.
  14502. *
  14503. * This function gets SLI3 port configure region 23 data through memory dump
  14504. * mailbox command. When it successfully retrieves data, the size of the data
  14505. * will be returned, otherwise, 0 will be returned.
  14506. **/
  14507. static uint32_t
  14508. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14509. {
  14510. LPFC_MBOXQ_t *pmb = NULL;
  14511. MAILBOX_t *mb;
  14512. uint32_t offset = 0;
  14513. int rc;
  14514. if (!rgn23_data)
  14515. return 0;
  14516. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14517. if (!pmb) {
  14518. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14519. "2600 failed to allocate mailbox memory\n");
  14520. return 0;
  14521. }
  14522. mb = &pmb->u.mb;
  14523. do {
  14524. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14525. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14526. if (rc != MBX_SUCCESS) {
  14527. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14528. "2601 failed to read config "
  14529. "region 23, rc 0x%x Status 0x%x\n",
  14530. rc, mb->mbxStatus);
  14531. mb->un.varDmp.word_cnt = 0;
  14532. }
  14533. /*
  14534. * dump mem may return a zero when finished or we got a
  14535. * mailbox error, either way we are done.
  14536. */
  14537. if (mb->un.varDmp.word_cnt == 0)
  14538. break;
  14539. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14540. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14541. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14542. rgn23_data + offset,
  14543. mb->un.varDmp.word_cnt);
  14544. offset += mb->un.varDmp.word_cnt;
  14545. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14546. mempool_free(pmb, phba->mbox_mem_pool);
  14547. return offset;
  14548. }
  14549. /**
  14550. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14551. * @phba: pointer to lpfc hba data structure.
  14552. * @rgn23_data: pointer to configure region 23 data.
  14553. *
  14554. * This function gets SLI4 port configure region 23 data through memory dump
  14555. * mailbox command. When it successfully retrieves data, the size of the data
  14556. * will be returned, otherwise, 0 will be returned.
  14557. **/
  14558. static uint32_t
  14559. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14560. {
  14561. LPFC_MBOXQ_t *mboxq = NULL;
  14562. struct lpfc_dmabuf *mp = NULL;
  14563. struct lpfc_mqe *mqe;
  14564. uint32_t data_length = 0;
  14565. int rc;
  14566. if (!rgn23_data)
  14567. return 0;
  14568. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14569. if (!mboxq) {
  14570. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14571. "3105 failed to allocate mailbox memory\n");
  14572. return 0;
  14573. }
  14574. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14575. goto out;
  14576. mqe = &mboxq->u.mqe;
  14577. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14578. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14579. if (rc)
  14580. goto out;
  14581. data_length = mqe->un.mb_words[5];
  14582. if (data_length == 0)
  14583. goto out;
  14584. if (data_length > DMP_RGN23_SIZE) {
  14585. data_length = 0;
  14586. goto out;
  14587. }
  14588. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14589. out:
  14590. mempool_free(mboxq, phba->mbox_mem_pool);
  14591. if (mp) {
  14592. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14593. kfree(mp);
  14594. }
  14595. return data_length;
  14596. }
  14597. /**
  14598. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14599. * @phba: pointer to lpfc hba data structure.
  14600. *
  14601. * This function read region 23 and parse TLV for port status to
  14602. * decide if the user disaled the port. If the TLV indicates the
  14603. * port is disabled, the hba_flag is set accordingly.
  14604. **/
  14605. void
  14606. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14607. {
  14608. uint8_t *rgn23_data = NULL;
  14609. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14610. uint32_t offset = 0;
  14611. /* Get adapter Region 23 data */
  14612. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14613. if (!rgn23_data)
  14614. goto out;
  14615. if (phba->sli_rev < LPFC_SLI_REV4)
  14616. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14617. else {
  14618. if_type = bf_get(lpfc_sli_intf_if_type,
  14619. &phba->sli4_hba.sli_intf);
  14620. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14621. goto out;
  14622. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14623. }
  14624. if (!data_size)
  14625. goto out;
  14626. /* Check the region signature first */
  14627. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14628. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14629. "2619 Config region 23 has bad signature\n");
  14630. goto out;
  14631. }
  14632. offset += 4;
  14633. /* Check the data structure version */
  14634. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14635. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14636. "2620 Config region 23 has bad version\n");
  14637. goto out;
  14638. }
  14639. offset += 4;
  14640. /* Parse TLV entries in the region */
  14641. while (offset < data_size) {
  14642. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14643. break;
  14644. /*
  14645. * If the TLV is not driver specific TLV or driver id is
  14646. * not linux driver id, skip the record.
  14647. */
  14648. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14649. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14650. (rgn23_data[offset + 3] != 0)) {
  14651. offset += rgn23_data[offset + 1] * 4 + 4;
  14652. continue;
  14653. }
  14654. /* Driver found a driver specific TLV in the config region */
  14655. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14656. offset += 4;
  14657. tlv_offset = 0;
  14658. /*
  14659. * Search for configured port state sub-TLV.
  14660. */
  14661. while ((offset < data_size) &&
  14662. (tlv_offset < sub_tlv_len)) {
  14663. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14664. offset += 4;
  14665. tlv_offset += 4;
  14666. break;
  14667. }
  14668. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14669. offset += rgn23_data[offset + 1] * 4 + 4;
  14670. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14671. continue;
  14672. }
  14673. /* This HBA contains PORT_STE configured */
  14674. if (!rgn23_data[offset + 2])
  14675. phba->hba_flag |= LINK_DISABLED;
  14676. goto out;
  14677. }
  14678. }
  14679. out:
  14680. kfree(rgn23_data);
  14681. return;
  14682. }
  14683. /**
  14684. * lpfc_wr_object - write an object to the firmware
  14685. * @phba: HBA structure that indicates port to create a queue on.
  14686. * @dmabuf_list: list of dmabufs to write to the port.
  14687. * @size: the total byte value of the objects to write to the port.
  14688. * @offset: the current offset to be used to start the transfer.
  14689. *
  14690. * This routine will create a wr_object mailbox command to send to the port.
  14691. * the mailbox command will be constructed using the dma buffers described in
  14692. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14693. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14694. * used to indicate the starting offset of the transfer and will also return
  14695. * the offset after the write object mailbox has completed. @size is used to
  14696. * determine the end of the object and whether the eof bit should be set.
  14697. *
  14698. * Return 0 is successful and offset will contain the the new offset to use
  14699. * for the next write.
  14700. * Return negative value for error cases.
  14701. **/
  14702. int
  14703. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14704. uint32_t size, uint32_t *offset)
  14705. {
  14706. struct lpfc_mbx_wr_object *wr_object;
  14707. LPFC_MBOXQ_t *mbox;
  14708. int rc = 0, i = 0;
  14709. uint32_t shdr_status, shdr_add_status;
  14710. uint32_t mbox_tmo;
  14711. union lpfc_sli4_cfg_shdr *shdr;
  14712. struct lpfc_dmabuf *dmabuf;
  14713. uint32_t written = 0;
  14714. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14715. if (!mbox)
  14716. return -ENOMEM;
  14717. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14718. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14719. sizeof(struct lpfc_mbx_wr_object) -
  14720. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14721. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14722. wr_object->u.request.write_offset = *offset;
  14723. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14724. wr_object->u.request.object_name[0] =
  14725. cpu_to_le32(wr_object->u.request.object_name[0]);
  14726. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14727. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14728. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14729. break;
  14730. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14731. wr_object->u.request.bde[i].addrHigh =
  14732. putPaddrHigh(dmabuf->phys);
  14733. if (written + SLI4_PAGE_SIZE >= size) {
  14734. wr_object->u.request.bde[i].tus.f.bdeSize =
  14735. (size - written);
  14736. written += (size - written);
  14737. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14738. } else {
  14739. wr_object->u.request.bde[i].tus.f.bdeSize =
  14740. SLI4_PAGE_SIZE;
  14741. written += SLI4_PAGE_SIZE;
  14742. }
  14743. i++;
  14744. }
  14745. wr_object->u.request.bde_count = i;
  14746. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14747. if (!phba->sli4_hba.intr_enable)
  14748. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14749. else {
  14750. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14751. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14752. }
  14753. /* The IOCTL status is embedded in the mailbox subheader. */
  14754. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14755. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14756. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14757. if (rc != MBX_TIMEOUT)
  14758. mempool_free(mbox, phba->mbox_mem_pool);
  14759. if (shdr_status || shdr_add_status || rc) {
  14760. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14761. "3025 Write Object mailbox failed with "
  14762. "status x%x add_status x%x, mbx status x%x\n",
  14763. shdr_status, shdr_add_status, rc);
  14764. rc = -ENXIO;
  14765. } else
  14766. *offset += wr_object->u.response.actual_write_length;
  14767. return rc;
  14768. }
  14769. /**
  14770. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14771. * @vport: pointer to vport data structure.
  14772. *
  14773. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14774. * and REG_VPI mailbox commands associated with the vport. This function
  14775. * is called when driver want to restart discovery of the vport due to
  14776. * a Clear Virtual Link event.
  14777. **/
  14778. void
  14779. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14780. {
  14781. struct lpfc_hba *phba = vport->phba;
  14782. LPFC_MBOXQ_t *mb, *nextmb;
  14783. struct lpfc_dmabuf *mp;
  14784. struct lpfc_nodelist *ndlp;
  14785. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14786. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14787. LIST_HEAD(mbox_cmd_list);
  14788. uint8_t restart_loop;
  14789. /* Clean up internally queued mailbox commands with the vport */
  14790. spin_lock_irq(&phba->hbalock);
  14791. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14792. if (mb->vport != vport)
  14793. continue;
  14794. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14795. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14796. continue;
  14797. list_del(&mb->list);
  14798. list_add_tail(&mb->list, &mbox_cmd_list);
  14799. }
  14800. /* Clean up active mailbox command with the vport */
  14801. mb = phba->sli.mbox_active;
  14802. if (mb && (mb->vport == vport)) {
  14803. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14804. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14805. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14806. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14807. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14808. /* Put reference count for delayed processing */
  14809. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14810. /* Unregister the RPI when mailbox complete */
  14811. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14812. }
  14813. }
  14814. /* Cleanup any mailbox completions which are not yet processed */
  14815. do {
  14816. restart_loop = 0;
  14817. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14818. /*
  14819. * If this mailox is already processed or it is
  14820. * for another vport ignore it.
  14821. */
  14822. if ((mb->vport != vport) ||
  14823. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14824. continue;
  14825. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14826. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14827. continue;
  14828. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14829. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14830. ndlp = (struct lpfc_nodelist *)mb->context2;
  14831. /* Unregister the RPI when mailbox complete */
  14832. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14833. restart_loop = 1;
  14834. spin_unlock_irq(&phba->hbalock);
  14835. spin_lock(shost->host_lock);
  14836. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14837. spin_unlock(shost->host_lock);
  14838. spin_lock_irq(&phba->hbalock);
  14839. break;
  14840. }
  14841. }
  14842. } while (restart_loop);
  14843. spin_unlock_irq(&phba->hbalock);
  14844. /* Release the cleaned-up mailbox commands */
  14845. while (!list_empty(&mbox_cmd_list)) {
  14846. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14847. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14848. mp = (struct lpfc_dmabuf *) (mb->context1);
  14849. if (mp) {
  14850. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14851. kfree(mp);
  14852. }
  14853. ndlp = (struct lpfc_nodelist *) mb->context2;
  14854. mb->context2 = NULL;
  14855. if (ndlp) {
  14856. spin_lock(shost->host_lock);
  14857. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14858. spin_unlock(shost->host_lock);
  14859. lpfc_nlp_put(ndlp);
  14860. }
  14861. }
  14862. mempool_free(mb, phba->mbox_mem_pool);
  14863. }
  14864. /* Release the ndlp with the cleaned-up active mailbox command */
  14865. if (act_mbx_ndlp) {
  14866. spin_lock(shost->host_lock);
  14867. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14868. spin_unlock(shost->host_lock);
  14869. lpfc_nlp_put(act_mbx_ndlp);
  14870. }
  14871. }
  14872. /**
  14873. * lpfc_drain_txq - Drain the txq
  14874. * @phba: Pointer to HBA context object.
  14875. *
  14876. * This function attempt to submit IOCBs on the txq
  14877. * to the adapter. For SLI4 adapters, the txq contains
  14878. * ELS IOCBs that have been deferred because the there
  14879. * are no SGLs. This congestion can occur with large
  14880. * vport counts during node discovery.
  14881. **/
  14882. uint32_t
  14883. lpfc_drain_txq(struct lpfc_hba *phba)
  14884. {
  14885. LIST_HEAD(completions);
  14886. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14887. struct lpfc_iocbq *piocbq = 0;
  14888. unsigned long iflags = 0;
  14889. char *fail_msg = NULL;
  14890. struct lpfc_sglq *sglq;
  14891. union lpfc_wqe wqe;
  14892. spin_lock_irqsave(&phba->hbalock, iflags);
  14893. if (pring->txq_cnt > pring->txq_max)
  14894. pring->txq_max = pring->txq_cnt;
  14895. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14896. while (pring->txq_cnt) {
  14897. spin_lock_irqsave(&phba->hbalock, iflags);
  14898. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14899. if (!piocbq) {
  14900. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14901. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14902. "2823 txq empty and txq_cnt is %d\n ",
  14903. pring->txq_cnt);
  14904. break;
  14905. }
  14906. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14907. if (!sglq) {
  14908. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14909. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14910. break;
  14911. }
  14912. /* The xri and iocb resources secured,
  14913. * attempt to issue request
  14914. */
  14915. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14916. piocbq->sli4_xritag = sglq->sli4_xritag;
  14917. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14918. fail_msg = "to convert bpl to sgl";
  14919. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14920. fail_msg = "to convert iocb to wqe";
  14921. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14922. fail_msg = " - Wq is full";
  14923. else
  14924. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14925. if (fail_msg) {
  14926. /* Failed means we can't issue and need to cancel */
  14927. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14928. "2822 IOCB failed %s iotag 0x%x "
  14929. "xri 0x%x\n",
  14930. fail_msg,
  14931. piocbq->iotag, piocbq->sli4_xritag);
  14932. list_add_tail(&piocbq->list, &completions);
  14933. }
  14934. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14935. }
  14936. /* Cancel all the IOCBs that cannot be issued */
  14937. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14938. IOERR_SLI_ABORTED);
  14939. return pring->txq_cnt;
  14940. }