lpfc_sli.c 498 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303
  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 = hq->host_index;
  400. /* sanity check on queue memory */
  401. if (unlikely(!hq) || unlikely(!dq))
  402. return -ENOMEM;
  403. temp_hrqe = hq->qe[hq->host_index].rqe;
  404. temp_drqe = dq->qe[dq->host_index].rqe;
  405. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  406. return -EINVAL;
  407. if (hq->host_index != dq->host_index)
  408. return -EINVAL;
  409. /* If the host has not yet processed the next entry then we are done */
  410. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  411. return -EBUSY;
  412. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  413. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  414. /* Update the host index to point to the next slot */
  415. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  416. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  417. /* Ring The Header Receive Queue Doorbell */
  418. if (!(hq->host_index % hq->entry_repost)) {
  419. doorbell.word0 = 0;
  420. if (hq->db_format == LPFC_DB_RING_FORMAT) {
  421. bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
  422. hq->entry_repost);
  423. bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
  424. } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
  425. bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
  426. hq->entry_repost);
  427. bf_set(lpfc_rq_db_list_fm_index, &doorbell,
  428. hq->host_index);
  429. bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
  430. } else {
  431. return -EINVAL;
  432. }
  433. writel(doorbell.word0, hq->db_regaddr);
  434. }
  435. return put_index;
  436. }
  437. /**
  438. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  439. * @q: The Header Receive Queue to operate on.
  440. *
  441. * This routine will update the HBA index of a queue to reflect consumption of
  442. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  443. * consumed an entry the host calls this function to update the queue's
  444. * internal pointers. This routine returns the number of entries that were
  445. * consumed by the HBA.
  446. **/
  447. static uint32_t
  448. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  449. {
  450. /* sanity check on queue memory */
  451. if (unlikely(!hq) || unlikely(!dq))
  452. return 0;
  453. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  454. return 0;
  455. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  456. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  457. return 1;
  458. }
  459. /**
  460. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  461. * @phba: Pointer to HBA context object.
  462. * @pring: Pointer to driver SLI ring object.
  463. *
  464. * This function returns pointer to next command iocb entry
  465. * in the command ring. The caller must hold hbalock to prevent
  466. * other threads consume the next command iocb.
  467. * SLI-2/SLI-3 provide different sized iocbs.
  468. **/
  469. static inline IOCB_t *
  470. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  471. {
  472. return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
  473. pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
  474. }
  475. /**
  476. * lpfc_resp_iocb - Get next response iocb entry in the ring
  477. * @phba: Pointer to HBA context object.
  478. * @pring: Pointer to driver SLI ring object.
  479. *
  480. * This function returns pointer to next response iocb entry
  481. * in the response ring. The caller must hold hbalock to make sure
  482. * that no other thread consume the next response iocb.
  483. * SLI-2/SLI-3 provide different sized iocbs.
  484. **/
  485. static inline IOCB_t *
  486. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  487. {
  488. return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
  489. pring->sli.sli3.rspidx * phba->iocb_rsp_size);
  490. }
  491. /**
  492. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  493. * @phba: Pointer to HBA context object.
  494. *
  495. * This function is called with hbalock held. This function
  496. * allocates a new driver iocb object from the iocb pool. If the
  497. * allocation is successful, it returns pointer to the newly
  498. * allocated iocb object else it returns NULL.
  499. **/
  500. struct lpfc_iocbq *
  501. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  502. {
  503. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  504. struct lpfc_iocbq * iocbq = NULL;
  505. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  506. if (iocbq)
  507. phba->iocb_cnt++;
  508. if (phba->iocb_cnt > phba->iocb_max)
  509. phba->iocb_max = phba->iocb_cnt;
  510. return iocbq;
  511. }
  512. /**
  513. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  514. * @phba: Pointer to HBA context object.
  515. * @xritag: XRI value.
  516. *
  517. * This function clears the sglq pointer from the array of acive
  518. * sglq's. The xritag that is passed in is used to index into the
  519. * array. Before the xritag can be used it needs to be adjusted
  520. * by subtracting the xribase.
  521. *
  522. * Returns sglq ponter = success, NULL = Failure.
  523. **/
  524. static struct lpfc_sglq *
  525. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  526. {
  527. struct lpfc_sglq *sglq;
  528. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  529. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  530. return sglq;
  531. }
  532. /**
  533. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  534. * @phba: Pointer to HBA context object.
  535. * @xritag: XRI value.
  536. *
  537. * This function returns the sglq pointer from the array of acive
  538. * sglq's. The xritag that is passed in is used to index into the
  539. * array. Before the xritag can be used it needs to be adjusted
  540. * by subtracting the xribase.
  541. *
  542. * Returns sglq ponter = success, NULL = Failure.
  543. **/
  544. struct lpfc_sglq *
  545. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  546. {
  547. struct lpfc_sglq *sglq;
  548. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  549. return sglq;
  550. }
  551. /**
  552. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  553. * @phba: Pointer to HBA context object.
  554. * @xritag: xri used in this exchange.
  555. * @rrq: The RRQ to be cleared.
  556. *
  557. **/
  558. void
  559. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  560. uint16_t xritag,
  561. struct lpfc_node_rrq *rrq)
  562. {
  563. struct lpfc_nodelist *ndlp = NULL;
  564. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  565. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  566. /* The target DID could have been swapped (cable swap)
  567. * we should use the ndlp from the findnode if it is
  568. * available.
  569. */
  570. if ((!ndlp) && rrq->ndlp)
  571. ndlp = rrq->ndlp;
  572. if (!ndlp)
  573. goto out;
  574. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  575. rrq->send_rrq = 0;
  576. rrq->xritag = 0;
  577. rrq->rrq_stop_time = 0;
  578. }
  579. out:
  580. mempool_free(rrq, phba->rrq_pool);
  581. }
  582. /**
  583. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  584. * @phba: Pointer to HBA context object.
  585. *
  586. * This function is called with hbalock held. This function
  587. * Checks if stop_time (ratov from setting rrq active) has
  588. * been reached, if it has and the send_rrq flag is set then
  589. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  590. * then it will just call the routine to clear the rrq and
  591. * free the rrq resource.
  592. * The timer is set to the next rrq that is going to expire before
  593. * leaving the routine.
  594. *
  595. **/
  596. void
  597. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  598. {
  599. struct lpfc_node_rrq *rrq;
  600. struct lpfc_node_rrq *nextrrq;
  601. unsigned long next_time;
  602. unsigned long iflags;
  603. LIST_HEAD(send_rrq);
  604. spin_lock_irqsave(&phba->hbalock, iflags);
  605. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  606. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  607. list_for_each_entry_safe(rrq, nextrrq,
  608. &phba->active_rrq_list, list) {
  609. if (time_after(jiffies, rrq->rrq_stop_time))
  610. list_move(&rrq->list, &send_rrq);
  611. else if (time_before(rrq->rrq_stop_time, next_time))
  612. next_time = rrq->rrq_stop_time;
  613. }
  614. spin_unlock_irqrestore(&phba->hbalock, iflags);
  615. if (!list_empty(&phba->active_rrq_list))
  616. mod_timer(&phba->rrq_tmr, next_time);
  617. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  618. list_del(&rrq->list);
  619. if (!rrq->send_rrq)
  620. /* this call will free the rrq */
  621. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  622. else if (lpfc_send_rrq(phba, rrq)) {
  623. /* if we send the rrq then the completion handler
  624. * will clear the bit in the xribitmap.
  625. */
  626. lpfc_clr_rrq_active(phba, rrq->xritag,
  627. rrq);
  628. }
  629. }
  630. }
  631. /**
  632. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  633. * @vport: Pointer to vport context object.
  634. * @xri: The xri used in the exchange.
  635. * @did: The targets DID for this exchange.
  636. *
  637. * returns NULL = rrq not found in the phba->active_rrq_list.
  638. * rrq = rrq for this xri and target.
  639. **/
  640. struct lpfc_node_rrq *
  641. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  642. {
  643. struct lpfc_hba *phba = vport->phba;
  644. struct lpfc_node_rrq *rrq;
  645. struct lpfc_node_rrq *nextrrq;
  646. unsigned long iflags;
  647. if (phba->sli_rev != LPFC_SLI_REV4)
  648. return NULL;
  649. spin_lock_irqsave(&phba->hbalock, iflags);
  650. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  651. if (rrq->vport == vport && rrq->xritag == xri &&
  652. rrq->nlp_DID == did){
  653. list_del(&rrq->list);
  654. spin_unlock_irqrestore(&phba->hbalock, iflags);
  655. return rrq;
  656. }
  657. }
  658. spin_unlock_irqrestore(&phba->hbalock, iflags);
  659. return NULL;
  660. }
  661. /**
  662. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  663. * @vport: Pointer to vport context object.
  664. * @ndlp: Pointer to the lpfc_node_list structure.
  665. * If ndlp is NULL Remove all active RRQs for this vport from the
  666. * phba->active_rrq_list and clear the rrq.
  667. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  668. **/
  669. void
  670. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  671. {
  672. struct lpfc_hba *phba = vport->phba;
  673. struct lpfc_node_rrq *rrq;
  674. struct lpfc_node_rrq *nextrrq;
  675. unsigned long iflags;
  676. LIST_HEAD(rrq_list);
  677. if (phba->sli_rev != LPFC_SLI_REV4)
  678. return;
  679. if (!ndlp) {
  680. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  681. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  682. }
  683. spin_lock_irqsave(&phba->hbalock, iflags);
  684. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  685. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  686. list_move(&rrq->list, &rrq_list);
  687. spin_unlock_irqrestore(&phba->hbalock, iflags);
  688. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  689. list_del(&rrq->list);
  690. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  691. }
  692. }
  693. /**
  694. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  695. * @phba: Pointer to HBA context object.
  696. *
  697. * Remove all rrqs from the phba->active_rrq_list and free them by
  698. * calling __lpfc_clr_active_rrq
  699. *
  700. **/
  701. void
  702. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  703. {
  704. struct lpfc_node_rrq *rrq;
  705. struct lpfc_node_rrq *nextrrq;
  706. unsigned long next_time;
  707. unsigned long iflags;
  708. LIST_HEAD(rrq_list);
  709. if (phba->sli_rev != LPFC_SLI_REV4)
  710. return;
  711. spin_lock_irqsave(&phba->hbalock, iflags);
  712. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  713. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  714. list_splice_init(&phba->active_rrq_list, &rrq_list);
  715. spin_unlock_irqrestore(&phba->hbalock, iflags);
  716. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  717. list_del(&rrq->list);
  718. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  719. }
  720. if (!list_empty(&phba->active_rrq_list))
  721. mod_timer(&phba->rrq_tmr, next_time);
  722. }
  723. /**
  724. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  725. * @phba: Pointer to HBA context object.
  726. * @ndlp: Targets nodelist pointer for this exchange.
  727. * @xritag the xri in the bitmap to test.
  728. *
  729. * This function is called with hbalock held. This function
  730. * returns 0 = rrq not active for this xri
  731. * 1 = rrq is valid for this xri.
  732. **/
  733. int
  734. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  735. uint16_t xritag)
  736. {
  737. if (!ndlp)
  738. return 0;
  739. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  740. return 1;
  741. else
  742. return 0;
  743. }
  744. /**
  745. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  746. * @phba: Pointer to HBA context object.
  747. * @ndlp: nodelist pointer for this target.
  748. * @xritag: xri used in this exchange.
  749. * @rxid: Remote Exchange ID.
  750. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  751. *
  752. * This function takes the hbalock.
  753. * The active bit is always set in the active rrq xri_bitmap even
  754. * if there is no slot avaiable for the other rrq information.
  755. *
  756. * returns 0 rrq actived for this xri
  757. * < 0 No memory or invalid ndlp.
  758. **/
  759. int
  760. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  761. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  762. {
  763. unsigned long iflags;
  764. struct lpfc_node_rrq *rrq;
  765. int empty;
  766. if (!ndlp)
  767. return -EINVAL;
  768. if (!phba->cfg_enable_rrq)
  769. return -EINVAL;
  770. spin_lock_irqsave(&phba->hbalock, iflags);
  771. if (phba->pport->load_flag & FC_UNLOADING) {
  772. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  773. goto out;
  774. }
  775. /*
  776. * set the active bit even if there is no mem available.
  777. */
  778. if (NLP_CHK_FREE_REQ(ndlp))
  779. goto out;
  780. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  781. goto out;
  782. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  783. goto out;
  784. spin_unlock_irqrestore(&phba->hbalock, iflags);
  785. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  786. if (!rrq) {
  787. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  788. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  789. " DID:0x%x Send:%d\n",
  790. xritag, rxid, ndlp->nlp_DID, send_rrq);
  791. return -EINVAL;
  792. }
  793. rrq->send_rrq = send_rrq;
  794. rrq->xritag = xritag;
  795. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  796. rrq->ndlp = ndlp;
  797. rrq->nlp_DID = ndlp->nlp_DID;
  798. rrq->vport = ndlp->vport;
  799. rrq->rxid = rxid;
  800. rrq->send_rrq = send_rrq;
  801. spin_lock_irqsave(&phba->hbalock, iflags);
  802. empty = list_empty(&phba->active_rrq_list);
  803. list_add_tail(&rrq->list, &phba->active_rrq_list);
  804. phba->hba_flag |= HBA_RRQ_ACTIVE;
  805. if (empty)
  806. lpfc_worker_wake_up(phba);
  807. spin_unlock_irqrestore(&phba->hbalock, iflags);
  808. return 0;
  809. out:
  810. spin_unlock_irqrestore(&phba->hbalock, iflags);
  811. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  812. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  813. " DID:0x%x Send:%d\n",
  814. xritag, rxid, ndlp->nlp_DID, send_rrq);
  815. return -EINVAL;
  816. }
  817. /**
  818. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  819. * @phba: Pointer to HBA context object.
  820. * @piocb: Pointer to the iocbq.
  821. *
  822. * This function is called with hbalock held. This function
  823. * gets a new driver sglq object from the sglq list. If the
  824. * list is not empty then it is successful, it returns pointer to the newly
  825. * allocated sglq object else it returns NULL.
  826. **/
  827. static struct lpfc_sglq *
  828. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  829. {
  830. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  831. struct lpfc_sglq *sglq = NULL;
  832. struct lpfc_sglq *start_sglq = NULL;
  833. struct lpfc_scsi_buf *lpfc_cmd;
  834. struct lpfc_nodelist *ndlp;
  835. int found = 0;
  836. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  837. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  838. ndlp = lpfc_cmd->rdata->pnode;
  839. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  840. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  841. ndlp = piocbq->context_un.ndlp;
  842. else if ((piocbq->iocb.ulpCommand == CMD_ELS_REQUEST64_CR) &&
  843. (piocbq->iocb_flag & LPFC_IO_LIBDFC))
  844. ndlp = piocbq->context_un.ndlp;
  845. else
  846. ndlp = piocbq->context1;
  847. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  848. start_sglq = sglq;
  849. while (!found) {
  850. if (!sglq)
  851. return NULL;
  852. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
  853. /* This xri has an rrq outstanding for this DID.
  854. * put it back in the list and get another xri.
  855. */
  856. list_add_tail(&sglq->list, lpfc_sgl_list);
  857. sglq = NULL;
  858. list_remove_head(lpfc_sgl_list, sglq,
  859. struct lpfc_sglq, list);
  860. if (sglq == start_sglq) {
  861. sglq = NULL;
  862. break;
  863. } else
  864. continue;
  865. }
  866. sglq->ndlp = ndlp;
  867. found = 1;
  868. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  869. sglq->state = SGL_ALLOCATED;
  870. }
  871. return sglq;
  872. }
  873. /**
  874. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  875. * @phba: Pointer to HBA context object.
  876. *
  877. * This function is called with no lock held. This function
  878. * allocates a new driver iocb object from the iocb pool. If the
  879. * allocation is successful, it returns pointer to the newly
  880. * allocated iocb object else it returns NULL.
  881. **/
  882. struct lpfc_iocbq *
  883. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  884. {
  885. struct lpfc_iocbq * iocbq = NULL;
  886. unsigned long iflags;
  887. spin_lock_irqsave(&phba->hbalock, iflags);
  888. iocbq = __lpfc_sli_get_iocbq(phba);
  889. spin_unlock_irqrestore(&phba->hbalock, iflags);
  890. return iocbq;
  891. }
  892. /**
  893. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  894. * @phba: Pointer to HBA context object.
  895. * @iocbq: Pointer to driver iocb object.
  896. *
  897. * This function is called with hbalock held to release driver
  898. * iocb object to the iocb pool. The iotag in the iocb object
  899. * does not change for each use of the iocb object. This function
  900. * clears all other fields of the iocb object when it is freed.
  901. * The sqlq structure that holds the xritag and phys and virtual
  902. * mappings for the scatter gather list is retrieved from the
  903. * active array of sglq. The get of the sglq pointer also clears
  904. * the entry in the array. If the status of the IO indiactes that
  905. * this IO was aborted then the sglq entry it put on the
  906. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  907. * IO has good status or fails for any other reason then the sglq
  908. * entry is added to the free list (lpfc_sgl_list).
  909. **/
  910. static void
  911. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  912. {
  913. struct lpfc_sglq *sglq;
  914. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  915. unsigned long iflag = 0;
  916. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  917. if (iocbq->sli4_xritag == NO_XRI)
  918. sglq = NULL;
  919. else
  920. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  921. if (sglq) {
  922. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  923. (sglq->state != SGL_XRI_ABORTED)) {
  924. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  925. iflag);
  926. list_add(&sglq->list,
  927. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  928. spin_unlock_irqrestore(
  929. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  930. } else {
  931. sglq->state = SGL_FREED;
  932. sglq->ndlp = NULL;
  933. list_add_tail(&sglq->list,
  934. &phba->sli4_hba.lpfc_sgl_list);
  935. /* Check if TXQ queue needs to be serviced */
  936. if (pring->txq_cnt)
  937. lpfc_worker_wake_up(phba);
  938. }
  939. }
  940. /*
  941. * Clean all volatile data fields, preserve iotag and node struct.
  942. */
  943. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  944. iocbq->sli4_lxritag = NO_XRI;
  945. iocbq->sli4_xritag = NO_XRI;
  946. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  947. }
  948. /**
  949. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  950. * @phba: Pointer to HBA context object.
  951. * @iocbq: Pointer to driver iocb object.
  952. *
  953. * This function is called with hbalock held to release driver
  954. * iocb object to the iocb pool. The iotag in the iocb object
  955. * does not change for each use of the iocb object. This function
  956. * clears all other fields of the iocb object when it is freed.
  957. **/
  958. static void
  959. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  960. {
  961. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  962. /*
  963. * Clean all volatile data fields, preserve iotag and node struct.
  964. */
  965. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  966. iocbq->sli4_xritag = NO_XRI;
  967. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  968. }
  969. /**
  970. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  971. * @phba: Pointer to HBA context object.
  972. * @iocbq: Pointer to driver iocb object.
  973. *
  974. * This function is called with hbalock held to release driver
  975. * iocb object to the iocb pool. The iotag in the iocb object
  976. * does not change for each use of the iocb object. This function
  977. * clears all other fields of the iocb object when it is freed.
  978. **/
  979. static void
  980. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  981. {
  982. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  983. phba->iocb_cnt--;
  984. }
  985. /**
  986. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  987. * @phba: Pointer to HBA context object.
  988. * @iocbq: Pointer to driver iocb object.
  989. *
  990. * This function is called with no lock held to release the iocb to
  991. * iocb pool.
  992. **/
  993. void
  994. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  995. {
  996. unsigned long iflags;
  997. /*
  998. * Clean all volatile data fields, preserve iotag and node struct.
  999. */
  1000. spin_lock_irqsave(&phba->hbalock, iflags);
  1001. __lpfc_sli_release_iocbq(phba, iocbq);
  1002. spin_unlock_irqrestore(&phba->hbalock, iflags);
  1003. }
  1004. /**
  1005. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  1006. * @phba: Pointer to HBA context object.
  1007. * @iocblist: List of IOCBs.
  1008. * @ulpstatus: ULP status in IOCB command field.
  1009. * @ulpWord4: ULP word-4 in IOCB command field.
  1010. *
  1011. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  1012. * on the list by invoking the complete callback function associated with the
  1013. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  1014. * fields.
  1015. **/
  1016. void
  1017. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  1018. uint32_t ulpstatus, uint32_t ulpWord4)
  1019. {
  1020. struct lpfc_iocbq *piocb;
  1021. while (!list_empty(iocblist)) {
  1022. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  1023. if (!piocb->iocb_cmpl)
  1024. lpfc_sli_release_iocbq(phba, piocb);
  1025. else {
  1026. piocb->iocb.ulpStatus = ulpstatus;
  1027. piocb->iocb.un.ulpWord[4] = ulpWord4;
  1028. (piocb->iocb_cmpl) (phba, piocb, piocb);
  1029. }
  1030. }
  1031. return;
  1032. }
  1033. /**
  1034. * lpfc_sli_iocb_cmd_type - Get the iocb type
  1035. * @iocb_cmnd: iocb command code.
  1036. *
  1037. * This function is called by ring event handler function to get the iocb type.
  1038. * This function translates the iocb command to an iocb command type used to
  1039. * decide the final disposition of each completed IOCB.
  1040. * The function returns
  1041. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  1042. * LPFC_SOL_IOCB if it is a solicited iocb completion
  1043. * LPFC_ABORT_IOCB if it is an abort iocb
  1044. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  1045. *
  1046. * The caller is not required to hold any lock.
  1047. **/
  1048. static lpfc_iocb_type
  1049. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1050. {
  1051. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1052. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1053. return 0;
  1054. switch (iocb_cmnd) {
  1055. case CMD_XMIT_SEQUENCE_CR:
  1056. case CMD_XMIT_SEQUENCE_CX:
  1057. case CMD_XMIT_BCAST_CN:
  1058. case CMD_XMIT_BCAST_CX:
  1059. case CMD_ELS_REQUEST_CR:
  1060. case CMD_ELS_REQUEST_CX:
  1061. case CMD_CREATE_XRI_CR:
  1062. case CMD_CREATE_XRI_CX:
  1063. case CMD_GET_RPI_CN:
  1064. case CMD_XMIT_ELS_RSP_CX:
  1065. case CMD_GET_RPI_CR:
  1066. case CMD_FCP_IWRITE_CR:
  1067. case CMD_FCP_IWRITE_CX:
  1068. case CMD_FCP_IREAD_CR:
  1069. case CMD_FCP_IREAD_CX:
  1070. case CMD_FCP_ICMND_CR:
  1071. case CMD_FCP_ICMND_CX:
  1072. case CMD_FCP_TSEND_CX:
  1073. case CMD_FCP_TRSP_CX:
  1074. case CMD_FCP_TRECEIVE_CX:
  1075. case CMD_FCP_AUTO_TRSP_CX:
  1076. case CMD_ADAPTER_MSG:
  1077. case CMD_ADAPTER_DUMP:
  1078. case CMD_XMIT_SEQUENCE64_CR:
  1079. case CMD_XMIT_SEQUENCE64_CX:
  1080. case CMD_XMIT_BCAST64_CN:
  1081. case CMD_XMIT_BCAST64_CX:
  1082. case CMD_ELS_REQUEST64_CR:
  1083. case CMD_ELS_REQUEST64_CX:
  1084. case CMD_FCP_IWRITE64_CR:
  1085. case CMD_FCP_IWRITE64_CX:
  1086. case CMD_FCP_IREAD64_CR:
  1087. case CMD_FCP_IREAD64_CX:
  1088. case CMD_FCP_ICMND64_CR:
  1089. case CMD_FCP_ICMND64_CX:
  1090. case CMD_FCP_TSEND64_CX:
  1091. case CMD_FCP_TRSP64_CX:
  1092. case CMD_FCP_TRECEIVE64_CX:
  1093. case CMD_GEN_REQUEST64_CR:
  1094. case CMD_GEN_REQUEST64_CX:
  1095. case CMD_XMIT_ELS_RSP64_CX:
  1096. case DSSCMD_IWRITE64_CR:
  1097. case DSSCMD_IWRITE64_CX:
  1098. case DSSCMD_IREAD64_CR:
  1099. case DSSCMD_IREAD64_CX:
  1100. type = LPFC_SOL_IOCB;
  1101. break;
  1102. case CMD_ABORT_XRI_CN:
  1103. case CMD_ABORT_XRI_CX:
  1104. case CMD_CLOSE_XRI_CN:
  1105. case CMD_CLOSE_XRI_CX:
  1106. case CMD_XRI_ABORTED_CX:
  1107. case CMD_ABORT_MXRI64_CN:
  1108. case CMD_XMIT_BLS_RSP64_CX:
  1109. type = LPFC_ABORT_IOCB;
  1110. break;
  1111. case CMD_RCV_SEQUENCE_CX:
  1112. case CMD_RCV_ELS_REQ_CX:
  1113. case CMD_RCV_SEQUENCE64_CX:
  1114. case CMD_RCV_ELS_REQ64_CX:
  1115. case CMD_ASYNC_STATUS:
  1116. case CMD_IOCB_RCV_SEQ64_CX:
  1117. case CMD_IOCB_RCV_ELS64_CX:
  1118. case CMD_IOCB_RCV_CONT64_CX:
  1119. case CMD_IOCB_RET_XRI64_CX:
  1120. type = LPFC_UNSOL_IOCB;
  1121. break;
  1122. case CMD_IOCB_XMIT_MSEQ64_CR:
  1123. case CMD_IOCB_XMIT_MSEQ64_CX:
  1124. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1125. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1126. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1127. case CMD_IOCB_ABORT_EXTENDED_CN:
  1128. case CMD_IOCB_RET_HBQE64_CN:
  1129. case CMD_IOCB_FCP_IBIDIR64_CR:
  1130. case CMD_IOCB_FCP_IBIDIR64_CX:
  1131. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1132. case CMD_IOCB_LOGENTRY_CN:
  1133. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1134. printk("%s - Unhandled SLI-3 Command x%x\n",
  1135. __func__, iocb_cmnd);
  1136. type = LPFC_UNKNOWN_IOCB;
  1137. break;
  1138. default:
  1139. type = LPFC_UNKNOWN_IOCB;
  1140. break;
  1141. }
  1142. return type;
  1143. }
  1144. /**
  1145. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1146. * @phba: Pointer to HBA context object.
  1147. *
  1148. * This function is called from SLI initialization code
  1149. * to configure every ring of the HBA's SLI interface. The
  1150. * caller is not required to hold any lock. This function issues
  1151. * a config_ring mailbox command for each ring.
  1152. * This function returns zero if successful else returns a negative
  1153. * error code.
  1154. **/
  1155. static int
  1156. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1157. {
  1158. struct lpfc_sli *psli = &phba->sli;
  1159. LPFC_MBOXQ_t *pmb;
  1160. MAILBOX_t *pmbox;
  1161. int i, rc, ret = 0;
  1162. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1163. if (!pmb)
  1164. return -ENOMEM;
  1165. pmbox = &pmb->u.mb;
  1166. phba->link_state = LPFC_INIT_MBX_CMDS;
  1167. for (i = 0; i < psli->num_rings; i++) {
  1168. lpfc_config_ring(phba, i, pmb);
  1169. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1170. if (rc != MBX_SUCCESS) {
  1171. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1172. "0446 Adapter failed to init (%d), "
  1173. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1174. "ring %d\n",
  1175. rc, pmbox->mbxCommand,
  1176. pmbox->mbxStatus, i);
  1177. phba->link_state = LPFC_HBA_ERROR;
  1178. ret = -ENXIO;
  1179. break;
  1180. }
  1181. }
  1182. mempool_free(pmb, phba->mbox_mem_pool);
  1183. return ret;
  1184. }
  1185. /**
  1186. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1187. * @phba: Pointer to HBA context object.
  1188. * @pring: Pointer to driver SLI ring object.
  1189. * @piocb: Pointer to the driver iocb object.
  1190. *
  1191. * This function is called with hbalock held. The function adds the
  1192. * new iocb to txcmplq of the given ring. This function always returns
  1193. * 0. If this function is called for ELS ring, this function checks if
  1194. * there is a vport associated with the ELS command. This function also
  1195. * starts els_tmofunc timer if this is an ELS command.
  1196. **/
  1197. static int
  1198. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1199. struct lpfc_iocbq *piocb)
  1200. {
  1201. list_add_tail(&piocb->list, &pring->txcmplq);
  1202. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1203. pring->txcmplq_cnt++;
  1204. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1205. pring->txcmplq_max = pring->txcmplq_cnt;
  1206. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1207. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1208. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1209. if (!piocb->vport)
  1210. BUG();
  1211. else
  1212. mod_timer(&piocb->vport->els_tmofunc,
  1213. jiffies + HZ * (phba->fc_ratov << 1));
  1214. }
  1215. return 0;
  1216. }
  1217. /**
  1218. * lpfc_sli_ringtx_get - Get first element of the txq
  1219. * @phba: Pointer to HBA context object.
  1220. * @pring: Pointer to driver SLI ring object.
  1221. *
  1222. * This function is called with hbalock held to get next
  1223. * iocb in txq of the given ring. If there is any iocb in
  1224. * the txq, the function returns first iocb in the list after
  1225. * removing the iocb from the list, else it returns NULL.
  1226. **/
  1227. struct lpfc_iocbq *
  1228. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1229. {
  1230. struct lpfc_iocbq *cmd_iocb;
  1231. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1232. if (cmd_iocb != NULL)
  1233. pring->txq_cnt--;
  1234. return cmd_iocb;
  1235. }
  1236. /**
  1237. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1238. * @phba: Pointer to HBA context object.
  1239. * @pring: Pointer to driver SLI ring object.
  1240. *
  1241. * This function is called with hbalock held and the caller must post the
  1242. * iocb without releasing the lock. If the caller releases the lock,
  1243. * iocb slot returned by the function is not guaranteed to be available.
  1244. * The function returns pointer to the next available iocb slot if there
  1245. * is available slot in the ring, else it returns NULL.
  1246. * If the get index of the ring is ahead of the put index, the function
  1247. * will post an error attention event to the worker thread to take the
  1248. * HBA to offline state.
  1249. **/
  1250. static IOCB_t *
  1251. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1252. {
  1253. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1254. uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
  1255. if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
  1256. (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
  1257. pring->sli.sli3.next_cmdidx = 0;
  1258. if (unlikely(pring->sli.sli3.local_getidx ==
  1259. pring->sli.sli3.next_cmdidx)) {
  1260. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1261. if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
  1262. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1263. "0315 Ring %d issue: portCmdGet %d "
  1264. "is bigger than cmd ring %d\n",
  1265. pring->ringno,
  1266. pring->sli.sli3.local_getidx,
  1267. max_cmd_idx);
  1268. phba->link_state = LPFC_HBA_ERROR;
  1269. /*
  1270. * All error attention handlers are posted to
  1271. * worker thread
  1272. */
  1273. phba->work_ha |= HA_ERATT;
  1274. phba->work_hs = HS_FFER3;
  1275. lpfc_worker_wake_up(phba);
  1276. return NULL;
  1277. }
  1278. if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
  1279. return NULL;
  1280. }
  1281. return lpfc_cmd_iocb(phba, pring);
  1282. }
  1283. /**
  1284. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1285. * @phba: Pointer to HBA context object.
  1286. * @iocbq: Pointer to driver iocb object.
  1287. *
  1288. * This function gets an iotag for the iocb. If there is no unused iotag and
  1289. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1290. * array and assigns a new iotag.
  1291. * The function returns the allocated iotag if successful, else returns zero.
  1292. * Zero is not a valid iotag.
  1293. * The caller is not required to hold any lock.
  1294. **/
  1295. uint16_t
  1296. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1297. {
  1298. struct lpfc_iocbq **new_arr;
  1299. struct lpfc_iocbq **old_arr;
  1300. size_t new_len;
  1301. struct lpfc_sli *psli = &phba->sli;
  1302. uint16_t iotag;
  1303. spin_lock_irq(&phba->hbalock);
  1304. iotag = psli->last_iotag;
  1305. if(++iotag < psli->iocbq_lookup_len) {
  1306. psli->last_iotag = iotag;
  1307. psli->iocbq_lookup[iotag] = iocbq;
  1308. spin_unlock_irq(&phba->hbalock);
  1309. iocbq->iotag = iotag;
  1310. return iotag;
  1311. } else if (psli->iocbq_lookup_len < (0xffff
  1312. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1313. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1314. spin_unlock_irq(&phba->hbalock);
  1315. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1316. GFP_KERNEL);
  1317. if (new_arr) {
  1318. spin_lock_irq(&phba->hbalock);
  1319. old_arr = psli->iocbq_lookup;
  1320. if (new_len <= psli->iocbq_lookup_len) {
  1321. /* highly unprobable case */
  1322. kfree(new_arr);
  1323. iotag = psli->last_iotag;
  1324. if(++iotag < psli->iocbq_lookup_len) {
  1325. psli->last_iotag = iotag;
  1326. psli->iocbq_lookup[iotag] = iocbq;
  1327. spin_unlock_irq(&phba->hbalock);
  1328. iocbq->iotag = iotag;
  1329. return iotag;
  1330. }
  1331. spin_unlock_irq(&phba->hbalock);
  1332. return 0;
  1333. }
  1334. if (psli->iocbq_lookup)
  1335. memcpy(new_arr, old_arr,
  1336. ((psli->last_iotag + 1) *
  1337. sizeof (struct lpfc_iocbq *)));
  1338. psli->iocbq_lookup = new_arr;
  1339. psli->iocbq_lookup_len = new_len;
  1340. psli->last_iotag = iotag;
  1341. psli->iocbq_lookup[iotag] = iocbq;
  1342. spin_unlock_irq(&phba->hbalock);
  1343. iocbq->iotag = iotag;
  1344. kfree(old_arr);
  1345. return iotag;
  1346. }
  1347. } else
  1348. spin_unlock_irq(&phba->hbalock);
  1349. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1350. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1351. psli->last_iotag);
  1352. return 0;
  1353. }
  1354. /**
  1355. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1356. * @phba: Pointer to HBA context object.
  1357. * @pring: Pointer to driver SLI ring object.
  1358. * @iocb: Pointer to iocb slot in the ring.
  1359. * @nextiocb: Pointer to driver iocb object which need to be
  1360. * posted to firmware.
  1361. *
  1362. * This function is called with hbalock held to post a new iocb to
  1363. * the firmware. This function copies the new iocb to ring iocb slot and
  1364. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1365. * a completion call back for this iocb else the function will free the
  1366. * iocb object.
  1367. **/
  1368. static void
  1369. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1370. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1371. {
  1372. /*
  1373. * Set up an iotag
  1374. */
  1375. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1376. if (pring->ringno == LPFC_ELS_RING) {
  1377. lpfc_debugfs_slow_ring_trc(phba,
  1378. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1379. *(((uint32_t *) &nextiocb->iocb) + 4),
  1380. *(((uint32_t *) &nextiocb->iocb) + 6),
  1381. *(((uint32_t *) &nextiocb->iocb) + 7));
  1382. }
  1383. /*
  1384. * Issue iocb command to adapter
  1385. */
  1386. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1387. wmb();
  1388. pring->stats.iocb_cmd++;
  1389. /*
  1390. * If there is no completion routine to call, we can release the
  1391. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1392. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1393. */
  1394. if (nextiocb->iocb_cmpl)
  1395. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1396. else
  1397. __lpfc_sli_release_iocbq(phba, nextiocb);
  1398. /*
  1399. * Let the HBA know what IOCB slot will be the next one the
  1400. * driver will put a command into.
  1401. */
  1402. pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
  1403. writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1404. }
  1405. /**
  1406. * lpfc_sli_update_full_ring - Update the chip attention register
  1407. * @phba: Pointer to HBA context object.
  1408. * @pring: Pointer to driver SLI ring object.
  1409. *
  1410. * The caller is not required to hold any lock for calling this function.
  1411. * This function updates the chip attention bits for the ring to inform firmware
  1412. * that there are pending work to be done for this ring and requests an
  1413. * interrupt when there is space available in the ring. This function is
  1414. * called when the driver is unable to post more iocbs to the ring due
  1415. * to unavailability of space in the ring.
  1416. **/
  1417. static void
  1418. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1419. {
  1420. int ringno = pring->ringno;
  1421. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1422. wmb();
  1423. /*
  1424. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1425. * The HBA will tell us when an IOCB entry is available.
  1426. */
  1427. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1428. readl(phba->CAregaddr); /* flush */
  1429. pring->stats.iocb_cmd_full++;
  1430. }
  1431. /**
  1432. * lpfc_sli_update_ring - Update chip attention register
  1433. * @phba: Pointer to HBA context object.
  1434. * @pring: Pointer to driver SLI ring object.
  1435. *
  1436. * This function updates the chip attention register bit for the
  1437. * given ring to inform HBA that there is more work to be done
  1438. * in this ring. The caller is not required to hold any lock.
  1439. **/
  1440. static void
  1441. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1442. {
  1443. int ringno = pring->ringno;
  1444. /*
  1445. * Tell the HBA that there is work to do in this ring.
  1446. */
  1447. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1448. wmb();
  1449. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1450. readl(phba->CAregaddr); /* flush */
  1451. }
  1452. }
  1453. /**
  1454. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1455. * @phba: Pointer to HBA context object.
  1456. * @pring: Pointer to driver SLI ring object.
  1457. *
  1458. * This function is called with hbalock held to post pending iocbs
  1459. * in the txq to the firmware. This function is called when driver
  1460. * detects space available in the ring.
  1461. **/
  1462. static void
  1463. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1464. {
  1465. IOCB_t *iocb;
  1466. struct lpfc_iocbq *nextiocb;
  1467. /*
  1468. * Check to see if:
  1469. * (a) there is anything on the txq to send
  1470. * (b) link is up
  1471. * (c) link attention events can be processed (fcp ring only)
  1472. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1473. */
  1474. if (pring->txq_cnt &&
  1475. lpfc_is_link_up(phba) &&
  1476. (pring->ringno != phba->sli.fcp_ring ||
  1477. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1478. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1479. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1480. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1481. if (iocb)
  1482. lpfc_sli_update_ring(phba, pring);
  1483. else
  1484. lpfc_sli_update_full_ring(phba, pring);
  1485. }
  1486. return;
  1487. }
  1488. /**
  1489. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1490. * @phba: Pointer to HBA context object.
  1491. * @hbqno: HBQ number.
  1492. *
  1493. * This function is called with hbalock held to get the next
  1494. * available slot for the given HBQ. If there is free slot
  1495. * available for the HBQ it will return pointer to the next available
  1496. * HBQ entry else it will return NULL.
  1497. **/
  1498. static struct lpfc_hbq_entry *
  1499. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1500. {
  1501. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1502. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1503. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1504. hbqp->next_hbqPutIdx = 0;
  1505. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1506. uint32_t raw_index = phba->hbq_get[hbqno];
  1507. uint32_t getidx = le32_to_cpu(raw_index);
  1508. hbqp->local_hbqGetIdx = getidx;
  1509. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1510. lpfc_printf_log(phba, KERN_ERR,
  1511. LOG_SLI | LOG_VPORT,
  1512. "1802 HBQ %d: local_hbqGetIdx "
  1513. "%u is > than hbqp->entry_count %u\n",
  1514. hbqno, hbqp->local_hbqGetIdx,
  1515. hbqp->entry_count);
  1516. phba->link_state = LPFC_HBA_ERROR;
  1517. return NULL;
  1518. }
  1519. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1520. return NULL;
  1521. }
  1522. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1523. hbqp->hbqPutIdx;
  1524. }
  1525. /**
  1526. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1527. * @phba: Pointer to HBA context object.
  1528. *
  1529. * This function is called with no lock held to free all the
  1530. * hbq buffers while uninitializing the SLI interface. It also
  1531. * frees the HBQ buffers returned by the firmware but not yet
  1532. * processed by the upper layers.
  1533. **/
  1534. void
  1535. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1536. {
  1537. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1538. struct hbq_dmabuf *hbq_buf;
  1539. unsigned long flags;
  1540. int i, hbq_count;
  1541. uint32_t hbqno;
  1542. hbq_count = lpfc_sli_hbq_count();
  1543. /* Return all memory used by all HBQs */
  1544. spin_lock_irqsave(&phba->hbalock, flags);
  1545. for (i = 0; i < hbq_count; ++i) {
  1546. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1547. &phba->hbqs[i].hbq_buffer_list, list) {
  1548. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1549. list_del(&hbq_buf->dbuf.list);
  1550. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1551. }
  1552. phba->hbqs[i].buffer_count = 0;
  1553. }
  1554. /* Return all HBQ buffer that are in-fly */
  1555. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1556. list) {
  1557. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1558. list_del(&hbq_buf->dbuf.list);
  1559. if (hbq_buf->tag == -1) {
  1560. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1561. (phba, hbq_buf);
  1562. } else {
  1563. hbqno = hbq_buf->tag >> 16;
  1564. if (hbqno >= LPFC_MAX_HBQS)
  1565. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1566. (phba, hbq_buf);
  1567. else
  1568. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1569. hbq_buf);
  1570. }
  1571. }
  1572. /* Mark the HBQs not in use */
  1573. phba->hbq_in_use = 0;
  1574. spin_unlock_irqrestore(&phba->hbalock, flags);
  1575. }
  1576. /**
  1577. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1578. * @phba: Pointer to HBA context object.
  1579. * @hbqno: HBQ number.
  1580. * @hbq_buf: Pointer to HBQ buffer.
  1581. *
  1582. * This function is called with the hbalock held to post a
  1583. * hbq buffer to the firmware. If the function finds an empty
  1584. * slot in the HBQ, it will post the buffer. The function will return
  1585. * pointer to the hbq entry if it successfully post the buffer
  1586. * else it will return NULL.
  1587. **/
  1588. static int
  1589. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1590. struct hbq_dmabuf *hbq_buf)
  1591. {
  1592. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1593. }
  1594. /**
  1595. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1596. * @phba: Pointer to HBA context object.
  1597. * @hbqno: HBQ number.
  1598. * @hbq_buf: Pointer to HBQ buffer.
  1599. *
  1600. * This function is called with the hbalock held to post a hbq buffer to the
  1601. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1602. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1603. * it successfully post the buffer else it will return an error.
  1604. **/
  1605. static int
  1606. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1607. struct hbq_dmabuf *hbq_buf)
  1608. {
  1609. struct lpfc_hbq_entry *hbqe;
  1610. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1611. /* Get next HBQ entry slot to use */
  1612. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1613. if (hbqe) {
  1614. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1615. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1616. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1617. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1618. hbqe->bde.tus.f.bdeFlags = 0;
  1619. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1620. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1621. /* Sync SLIM */
  1622. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1623. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1624. /* flush */
  1625. readl(phba->hbq_put + hbqno);
  1626. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1627. return 0;
  1628. } else
  1629. return -ENOMEM;
  1630. }
  1631. /**
  1632. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1633. * @phba: Pointer to HBA context object.
  1634. * @hbqno: HBQ number.
  1635. * @hbq_buf: Pointer to HBQ buffer.
  1636. *
  1637. * This function is called with the hbalock held to post an RQE to the SLI4
  1638. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1639. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1640. **/
  1641. static int
  1642. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1643. struct hbq_dmabuf *hbq_buf)
  1644. {
  1645. int rc;
  1646. struct lpfc_rqe hrqe;
  1647. struct lpfc_rqe drqe;
  1648. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1649. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1650. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1651. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1652. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1653. &hrqe, &drqe);
  1654. if (rc < 0)
  1655. return rc;
  1656. hbq_buf->tag = rc;
  1657. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1658. return 0;
  1659. }
  1660. /* HBQ for ELS and CT traffic. */
  1661. static struct lpfc_hbq_init lpfc_els_hbq = {
  1662. .rn = 1,
  1663. .entry_count = 256,
  1664. .mask_count = 0,
  1665. .profile = 0,
  1666. .ring_mask = (1 << LPFC_ELS_RING),
  1667. .buffer_count = 0,
  1668. .init_count = 40,
  1669. .add_count = 40,
  1670. };
  1671. /* HBQ for the extra ring if needed */
  1672. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1673. .rn = 1,
  1674. .entry_count = 200,
  1675. .mask_count = 0,
  1676. .profile = 0,
  1677. .ring_mask = (1 << LPFC_EXTRA_RING),
  1678. .buffer_count = 0,
  1679. .init_count = 0,
  1680. .add_count = 5,
  1681. };
  1682. /* Array of HBQs */
  1683. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1684. &lpfc_els_hbq,
  1685. &lpfc_extra_hbq,
  1686. };
  1687. /**
  1688. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1689. * @phba: Pointer to HBA context object.
  1690. * @hbqno: HBQ number.
  1691. * @count: Number of HBQ buffers to be posted.
  1692. *
  1693. * This function is called with no lock held to post more hbq buffers to the
  1694. * given HBQ. The function returns the number of HBQ buffers successfully
  1695. * posted.
  1696. **/
  1697. static int
  1698. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1699. {
  1700. uint32_t i, posted = 0;
  1701. unsigned long flags;
  1702. struct hbq_dmabuf *hbq_buffer;
  1703. LIST_HEAD(hbq_buf_list);
  1704. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1705. return 0;
  1706. if ((phba->hbqs[hbqno].buffer_count + count) >
  1707. lpfc_hbq_defs[hbqno]->entry_count)
  1708. count = lpfc_hbq_defs[hbqno]->entry_count -
  1709. phba->hbqs[hbqno].buffer_count;
  1710. if (!count)
  1711. return 0;
  1712. /* Allocate HBQ entries */
  1713. for (i = 0; i < count; i++) {
  1714. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1715. if (!hbq_buffer)
  1716. break;
  1717. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1718. }
  1719. /* Check whether HBQ is still in use */
  1720. spin_lock_irqsave(&phba->hbalock, flags);
  1721. if (!phba->hbq_in_use)
  1722. goto err;
  1723. while (!list_empty(&hbq_buf_list)) {
  1724. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1725. dbuf.list);
  1726. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1727. (hbqno << 16));
  1728. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1729. phba->hbqs[hbqno].buffer_count++;
  1730. posted++;
  1731. } else
  1732. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1733. }
  1734. spin_unlock_irqrestore(&phba->hbalock, flags);
  1735. return posted;
  1736. err:
  1737. spin_unlock_irqrestore(&phba->hbalock, flags);
  1738. while (!list_empty(&hbq_buf_list)) {
  1739. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1740. dbuf.list);
  1741. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1742. }
  1743. return 0;
  1744. }
  1745. /**
  1746. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1747. * @phba: Pointer to HBA context object.
  1748. * @qno: HBQ number.
  1749. *
  1750. * This function posts more buffers to the HBQ. This function
  1751. * is called with no lock held. The function returns the number of HBQ entries
  1752. * successfully allocated.
  1753. **/
  1754. int
  1755. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1756. {
  1757. if (phba->sli_rev == LPFC_SLI_REV4)
  1758. return 0;
  1759. else
  1760. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1761. lpfc_hbq_defs[qno]->add_count);
  1762. }
  1763. /**
  1764. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1765. * @phba: Pointer to HBA context object.
  1766. * @qno: HBQ queue number.
  1767. *
  1768. * This function is called from SLI initialization code path with
  1769. * no lock held to post initial HBQ buffers to firmware. The
  1770. * function returns the number of HBQ entries successfully allocated.
  1771. **/
  1772. static int
  1773. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1774. {
  1775. if (phba->sli_rev == LPFC_SLI_REV4)
  1776. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1777. lpfc_hbq_defs[qno]->entry_count);
  1778. else
  1779. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1780. lpfc_hbq_defs[qno]->init_count);
  1781. }
  1782. /**
  1783. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1784. * @phba: Pointer to HBA context object.
  1785. * @hbqno: HBQ number.
  1786. *
  1787. * This function removes the first hbq buffer on an hbq list and returns a
  1788. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1789. **/
  1790. static struct hbq_dmabuf *
  1791. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1792. {
  1793. struct lpfc_dmabuf *d_buf;
  1794. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1795. if (!d_buf)
  1796. return NULL;
  1797. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1798. }
  1799. /**
  1800. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1801. * @phba: Pointer to HBA context object.
  1802. * @tag: Tag of the hbq buffer.
  1803. *
  1804. * This function is called with hbalock held. This function searches
  1805. * for the hbq buffer associated with the given tag in the hbq buffer
  1806. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1807. * it returns NULL.
  1808. **/
  1809. static struct hbq_dmabuf *
  1810. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1811. {
  1812. struct lpfc_dmabuf *d_buf;
  1813. struct hbq_dmabuf *hbq_buf;
  1814. uint32_t hbqno;
  1815. hbqno = tag >> 16;
  1816. if (hbqno >= LPFC_MAX_HBQS)
  1817. return NULL;
  1818. spin_lock_irq(&phba->hbalock);
  1819. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1820. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1821. if (hbq_buf->tag == tag) {
  1822. spin_unlock_irq(&phba->hbalock);
  1823. return hbq_buf;
  1824. }
  1825. }
  1826. spin_unlock_irq(&phba->hbalock);
  1827. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1828. "1803 Bad hbq tag. Data: x%x x%x\n",
  1829. tag, phba->hbqs[tag >> 16].buffer_count);
  1830. return NULL;
  1831. }
  1832. /**
  1833. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1834. * @phba: Pointer to HBA context object.
  1835. * @hbq_buffer: Pointer to HBQ buffer.
  1836. *
  1837. * This function is called with hbalock. This function gives back
  1838. * the hbq buffer to firmware. If the HBQ does not have space to
  1839. * post the buffer, it will free the buffer.
  1840. **/
  1841. void
  1842. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1843. {
  1844. uint32_t hbqno;
  1845. if (hbq_buffer) {
  1846. hbqno = hbq_buffer->tag >> 16;
  1847. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1848. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1849. }
  1850. }
  1851. /**
  1852. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1853. * @mbxCommand: mailbox command code.
  1854. *
  1855. * This function is called by the mailbox event handler function to verify
  1856. * that the completed mailbox command is a legitimate mailbox command. If the
  1857. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1858. * and the mailbox event handler will take the HBA offline.
  1859. **/
  1860. static int
  1861. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1862. {
  1863. uint8_t ret;
  1864. switch (mbxCommand) {
  1865. case MBX_LOAD_SM:
  1866. case MBX_READ_NV:
  1867. case MBX_WRITE_NV:
  1868. case MBX_WRITE_VPARMS:
  1869. case MBX_RUN_BIU_DIAG:
  1870. case MBX_INIT_LINK:
  1871. case MBX_DOWN_LINK:
  1872. case MBX_CONFIG_LINK:
  1873. case MBX_CONFIG_RING:
  1874. case MBX_RESET_RING:
  1875. case MBX_READ_CONFIG:
  1876. case MBX_READ_RCONFIG:
  1877. case MBX_READ_SPARM:
  1878. case MBX_READ_STATUS:
  1879. case MBX_READ_RPI:
  1880. case MBX_READ_XRI:
  1881. case MBX_READ_REV:
  1882. case MBX_READ_LNK_STAT:
  1883. case MBX_REG_LOGIN:
  1884. case MBX_UNREG_LOGIN:
  1885. case MBX_CLEAR_LA:
  1886. case MBX_DUMP_MEMORY:
  1887. case MBX_DUMP_CONTEXT:
  1888. case MBX_RUN_DIAGS:
  1889. case MBX_RESTART:
  1890. case MBX_UPDATE_CFG:
  1891. case MBX_DOWN_LOAD:
  1892. case MBX_DEL_LD_ENTRY:
  1893. case MBX_RUN_PROGRAM:
  1894. case MBX_SET_MASK:
  1895. case MBX_SET_VARIABLE:
  1896. case MBX_UNREG_D_ID:
  1897. case MBX_KILL_BOARD:
  1898. case MBX_CONFIG_FARP:
  1899. case MBX_BEACON:
  1900. case MBX_LOAD_AREA:
  1901. case MBX_RUN_BIU_DIAG64:
  1902. case MBX_CONFIG_PORT:
  1903. case MBX_READ_SPARM64:
  1904. case MBX_READ_RPI64:
  1905. case MBX_REG_LOGIN64:
  1906. case MBX_READ_TOPOLOGY:
  1907. case MBX_WRITE_WWN:
  1908. case MBX_SET_DEBUG:
  1909. case MBX_LOAD_EXP_ROM:
  1910. case MBX_ASYNCEVT_ENABLE:
  1911. case MBX_REG_VPI:
  1912. case MBX_UNREG_VPI:
  1913. case MBX_HEARTBEAT:
  1914. case MBX_PORT_CAPABILITIES:
  1915. case MBX_PORT_IOV_CONTROL:
  1916. case MBX_SLI4_CONFIG:
  1917. case MBX_SLI4_REQ_FTRS:
  1918. case MBX_REG_FCFI:
  1919. case MBX_UNREG_FCFI:
  1920. case MBX_REG_VFI:
  1921. case MBX_UNREG_VFI:
  1922. case MBX_INIT_VPI:
  1923. case MBX_INIT_VFI:
  1924. case MBX_RESUME_RPI:
  1925. case MBX_READ_EVENT_LOG_STATUS:
  1926. case MBX_READ_EVENT_LOG:
  1927. case MBX_SECURITY_MGMT:
  1928. case MBX_AUTH_PORT:
  1929. case MBX_ACCESS_VDATA:
  1930. ret = mbxCommand;
  1931. break;
  1932. default:
  1933. ret = MBX_SHUTDOWN;
  1934. break;
  1935. }
  1936. return ret;
  1937. }
  1938. /**
  1939. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1940. * @phba: Pointer to HBA context object.
  1941. * @pmboxq: Pointer to mailbox command.
  1942. *
  1943. * This is completion handler function for mailbox commands issued from
  1944. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1945. * mailbox event handler function with no lock held. This function
  1946. * will wake up thread waiting on the wait queue pointed by context1
  1947. * of the mailbox.
  1948. **/
  1949. void
  1950. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1951. {
  1952. wait_queue_head_t *pdone_q;
  1953. unsigned long drvr_flag;
  1954. /*
  1955. * If pdone_q is empty, the driver thread gave up waiting and
  1956. * continued running.
  1957. */
  1958. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1959. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1960. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1961. if (pdone_q)
  1962. wake_up_interruptible(pdone_q);
  1963. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1964. return;
  1965. }
  1966. /**
  1967. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1968. * @phba: Pointer to HBA context object.
  1969. * @pmb: Pointer to mailbox object.
  1970. *
  1971. * This function is the default mailbox completion handler. It
  1972. * frees the memory resources associated with the completed mailbox
  1973. * command. If the completed command is a REG_LOGIN mailbox command,
  1974. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1975. **/
  1976. void
  1977. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1978. {
  1979. struct lpfc_vport *vport = pmb->vport;
  1980. struct lpfc_dmabuf *mp;
  1981. struct lpfc_nodelist *ndlp;
  1982. struct Scsi_Host *shost;
  1983. uint16_t rpi, vpi;
  1984. int rc;
  1985. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1986. if (mp) {
  1987. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1988. kfree(mp);
  1989. }
  1990. /*
  1991. * If a REG_LOGIN succeeded after node is destroyed or node
  1992. * is in re-discovery driver need to cleanup the RPI.
  1993. */
  1994. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1995. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1996. !pmb->u.mb.mbxStatus) {
  1997. rpi = pmb->u.mb.un.varWords[0];
  1998. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1999. lpfc_unreg_login(phba, vpi, rpi, pmb);
  2000. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  2001. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2002. if (rc != MBX_NOT_FINISHED)
  2003. return;
  2004. }
  2005. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  2006. !(phba->pport->load_flag & FC_UNLOADING) &&
  2007. !pmb->u.mb.mbxStatus) {
  2008. shost = lpfc_shost_from_vport(vport);
  2009. spin_lock_irq(shost->host_lock);
  2010. vport->vpi_state |= LPFC_VPI_REGISTERED;
  2011. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  2012. spin_unlock_irq(shost->host_lock);
  2013. }
  2014. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  2015. ndlp = (struct lpfc_nodelist *)pmb->context2;
  2016. lpfc_nlp_put(ndlp);
  2017. pmb->context2 = NULL;
  2018. }
  2019. /* Check security permission status on INIT_LINK mailbox command */
  2020. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  2021. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  2022. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2023. "2860 SLI authentication is required "
  2024. "for INIT_LINK but has not done yet\n");
  2025. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  2026. lpfc_sli4_mbox_cmd_free(phba, pmb);
  2027. else
  2028. mempool_free(pmb, phba->mbox_mem_pool);
  2029. }
  2030. /**
  2031. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  2032. * @phba: Pointer to HBA context object.
  2033. *
  2034. * This function is called with no lock held. This function processes all
  2035. * the completed mailbox commands and gives it to upper layers. The interrupt
  2036. * service routine processes mailbox completion interrupt and adds completed
  2037. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  2038. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  2039. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  2040. * function returns the mailbox commands to the upper layer by calling the
  2041. * completion handler function of each mailbox.
  2042. **/
  2043. int
  2044. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  2045. {
  2046. MAILBOX_t *pmbox;
  2047. LPFC_MBOXQ_t *pmb;
  2048. int rc;
  2049. LIST_HEAD(cmplq);
  2050. phba->sli.slistat.mbox_event++;
  2051. /* Get all completed mailboxe buffers into the cmplq */
  2052. spin_lock_irq(&phba->hbalock);
  2053. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2054. spin_unlock_irq(&phba->hbalock);
  2055. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2056. do {
  2057. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2058. if (pmb == NULL)
  2059. break;
  2060. pmbox = &pmb->u.mb;
  2061. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2062. if (pmb->vport) {
  2063. lpfc_debugfs_disc_trc(pmb->vport,
  2064. LPFC_DISC_TRC_MBOX_VPORT,
  2065. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2066. (uint32_t)pmbox->mbxCommand,
  2067. pmbox->un.varWords[0],
  2068. pmbox->un.varWords[1]);
  2069. }
  2070. else {
  2071. lpfc_debugfs_disc_trc(phba->pport,
  2072. LPFC_DISC_TRC_MBOX,
  2073. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2074. (uint32_t)pmbox->mbxCommand,
  2075. pmbox->un.varWords[0],
  2076. pmbox->un.varWords[1]);
  2077. }
  2078. }
  2079. /*
  2080. * It is a fatal error if unknown mbox command completion.
  2081. */
  2082. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2083. MBX_SHUTDOWN) {
  2084. /* Unknown mailbox command compl */
  2085. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2086. "(%d):0323 Unknown Mailbox command "
  2087. "x%x (x%x/x%x) Cmpl\n",
  2088. pmb->vport ? pmb->vport->vpi : 0,
  2089. pmbox->mbxCommand,
  2090. lpfc_sli_config_mbox_subsys_get(phba,
  2091. pmb),
  2092. lpfc_sli_config_mbox_opcode_get(phba,
  2093. pmb));
  2094. phba->link_state = LPFC_HBA_ERROR;
  2095. phba->work_hs = HS_FFER3;
  2096. lpfc_handle_eratt(phba);
  2097. continue;
  2098. }
  2099. if (pmbox->mbxStatus) {
  2100. phba->sli.slistat.mbox_stat_err++;
  2101. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2102. /* Mbox cmd cmpl error - RETRYing */
  2103. lpfc_printf_log(phba, KERN_INFO,
  2104. LOG_MBOX | LOG_SLI,
  2105. "(%d):0305 Mbox cmd cmpl "
  2106. "error - RETRYing Data: x%x "
  2107. "(x%x/x%x) x%x x%x x%x\n",
  2108. pmb->vport ? pmb->vport->vpi : 0,
  2109. pmbox->mbxCommand,
  2110. lpfc_sli_config_mbox_subsys_get(phba,
  2111. pmb),
  2112. lpfc_sli_config_mbox_opcode_get(phba,
  2113. pmb),
  2114. pmbox->mbxStatus,
  2115. pmbox->un.varWords[0],
  2116. pmb->vport->port_state);
  2117. pmbox->mbxStatus = 0;
  2118. pmbox->mbxOwner = OWN_HOST;
  2119. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2120. if (rc != MBX_NOT_FINISHED)
  2121. continue;
  2122. }
  2123. }
  2124. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2125. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2126. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2127. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2128. pmb->vport ? pmb->vport->vpi : 0,
  2129. pmbox->mbxCommand,
  2130. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2131. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2132. pmb->mbox_cmpl,
  2133. *((uint32_t *) pmbox),
  2134. pmbox->un.varWords[0],
  2135. pmbox->un.varWords[1],
  2136. pmbox->un.varWords[2],
  2137. pmbox->un.varWords[3],
  2138. pmbox->un.varWords[4],
  2139. pmbox->un.varWords[5],
  2140. pmbox->un.varWords[6],
  2141. pmbox->un.varWords[7]);
  2142. if (pmb->mbox_cmpl)
  2143. pmb->mbox_cmpl(phba,pmb);
  2144. } while (1);
  2145. return 0;
  2146. }
  2147. /**
  2148. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2149. * @phba: Pointer to HBA context object.
  2150. * @pring: Pointer to driver SLI ring object.
  2151. * @tag: buffer tag.
  2152. *
  2153. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2154. * is set in the tag the buffer is posted for a particular exchange,
  2155. * the function will return the buffer without replacing the buffer.
  2156. * If the buffer is for unsolicited ELS or CT traffic, this function
  2157. * returns the buffer and also posts another buffer to the firmware.
  2158. **/
  2159. static struct lpfc_dmabuf *
  2160. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2161. struct lpfc_sli_ring *pring,
  2162. uint32_t tag)
  2163. {
  2164. struct hbq_dmabuf *hbq_entry;
  2165. if (tag & QUE_BUFTAG_BIT)
  2166. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2167. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2168. if (!hbq_entry)
  2169. return NULL;
  2170. return &hbq_entry->dbuf;
  2171. }
  2172. /**
  2173. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2174. * @phba: Pointer to HBA context object.
  2175. * @pring: Pointer to driver SLI ring object.
  2176. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2177. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2178. * @fch_type: the type for the first frame of the sequence.
  2179. *
  2180. * This function is called with no lock held. This function uses the r_ctl and
  2181. * type of the received sequence to find the correct callback function to call
  2182. * to process the sequence.
  2183. **/
  2184. static int
  2185. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2186. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2187. uint32_t fch_type)
  2188. {
  2189. int i;
  2190. /* unSolicited Responses */
  2191. if (pring->prt[0].profile) {
  2192. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2193. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2194. saveq);
  2195. return 1;
  2196. }
  2197. /* We must search, based on rctl / type
  2198. for the right routine */
  2199. for (i = 0; i < pring->num_mask; i++) {
  2200. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2201. (pring->prt[i].type == fch_type)) {
  2202. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2203. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2204. (phba, pring, saveq);
  2205. return 1;
  2206. }
  2207. }
  2208. return 0;
  2209. }
  2210. /**
  2211. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2212. * @phba: Pointer to HBA context object.
  2213. * @pring: Pointer to driver SLI ring object.
  2214. * @saveq: Pointer to the unsolicited iocb.
  2215. *
  2216. * This function is called with no lock held by the ring event handler
  2217. * when there is an unsolicited iocb posted to the response ring by the
  2218. * firmware. This function gets the buffer associated with the iocbs
  2219. * and calls the event handler for the ring. This function handles both
  2220. * qring buffers and hbq buffers.
  2221. * When the function returns 1 the caller can free the iocb object otherwise
  2222. * upper layer functions will free the iocb objects.
  2223. **/
  2224. static int
  2225. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2226. struct lpfc_iocbq *saveq)
  2227. {
  2228. IOCB_t * irsp;
  2229. WORD5 * w5p;
  2230. uint32_t Rctl, Type;
  2231. uint32_t match;
  2232. struct lpfc_iocbq *iocbq;
  2233. struct lpfc_dmabuf *dmzbuf;
  2234. match = 0;
  2235. irsp = &(saveq->iocb);
  2236. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2237. if (pring->lpfc_sli_rcv_async_status)
  2238. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2239. else
  2240. lpfc_printf_log(phba,
  2241. KERN_WARNING,
  2242. LOG_SLI,
  2243. "0316 Ring %d handler: unexpected "
  2244. "ASYNC_STATUS iocb received evt_code "
  2245. "0x%x\n",
  2246. pring->ringno,
  2247. irsp->un.asyncstat.evt_code);
  2248. return 1;
  2249. }
  2250. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2251. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2252. if (irsp->ulpBdeCount > 0) {
  2253. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2254. irsp->un.ulpWord[3]);
  2255. lpfc_in_buf_free(phba, dmzbuf);
  2256. }
  2257. if (irsp->ulpBdeCount > 1) {
  2258. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2259. irsp->unsli3.sli3Words[3]);
  2260. lpfc_in_buf_free(phba, dmzbuf);
  2261. }
  2262. if (irsp->ulpBdeCount > 2) {
  2263. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2264. irsp->unsli3.sli3Words[7]);
  2265. lpfc_in_buf_free(phba, dmzbuf);
  2266. }
  2267. return 1;
  2268. }
  2269. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2270. if (irsp->ulpBdeCount != 0) {
  2271. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2272. irsp->un.ulpWord[3]);
  2273. if (!saveq->context2)
  2274. lpfc_printf_log(phba,
  2275. KERN_ERR,
  2276. LOG_SLI,
  2277. "0341 Ring %d Cannot find buffer for "
  2278. "an unsolicited iocb. tag 0x%x\n",
  2279. pring->ringno,
  2280. irsp->un.ulpWord[3]);
  2281. }
  2282. if (irsp->ulpBdeCount == 2) {
  2283. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2284. irsp->unsli3.sli3Words[7]);
  2285. if (!saveq->context3)
  2286. lpfc_printf_log(phba,
  2287. KERN_ERR,
  2288. LOG_SLI,
  2289. "0342 Ring %d Cannot find buffer for an"
  2290. " unsolicited iocb. tag 0x%x\n",
  2291. pring->ringno,
  2292. irsp->unsli3.sli3Words[7]);
  2293. }
  2294. list_for_each_entry(iocbq, &saveq->list, list) {
  2295. irsp = &(iocbq->iocb);
  2296. if (irsp->ulpBdeCount != 0) {
  2297. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2298. irsp->un.ulpWord[3]);
  2299. if (!iocbq->context2)
  2300. lpfc_printf_log(phba,
  2301. KERN_ERR,
  2302. LOG_SLI,
  2303. "0343 Ring %d Cannot find "
  2304. "buffer for an unsolicited iocb"
  2305. ". tag 0x%x\n", pring->ringno,
  2306. irsp->un.ulpWord[3]);
  2307. }
  2308. if (irsp->ulpBdeCount == 2) {
  2309. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2310. irsp->unsli3.sli3Words[7]);
  2311. if (!iocbq->context3)
  2312. lpfc_printf_log(phba,
  2313. KERN_ERR,
  2314. LOG_SLI,
  2315. "0344 Ring %d Cannot find "
  2316. "buffer for an unsolicited "
  2317. "iocb. tag 0x%x\n",
  2318. pring->ringno,
  2319. irsp->unsli3.sli3Words[7]);
  2320. }
  2321. }
  2322. }
  2323. if (irsp->ulpBdeCount != 0 &&
  2324. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2325. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2326. int found = 0;
  2327. /* search continue save q for same XRI */
  2328. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2329. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2330. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2331. list_add_tail(&saveq->list, &iocbq->list);
  2332. found = 1;
  2333. break;
  2334. }
  2335. }
  2336. if (!found)
  2337. list_add_tail(&saveq->clist,
  2338. &pring->iocb_continue_saveq);
  2339. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2340. list_del_init(&iocbq->clist);
  2341. saveq = iocbq;
  2342. irsp = &(saveq->iocb);
  2343. } else
  2344. return 0;
  2345. }
  2346. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2347. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2348. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2349. Rctl = FC_RCTL_ELS_REQ;
  2350. Type = FC_TYPE_ELS;
  2351. } else {
  2352. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2353. Rctl = w5p->hcsw.Rctl;
  2354. Type = w5p->hcsw.Type;
  2355. /* Firmware Workaround */
  2356. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2357. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2358. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2359. Rctl = FC_RCTL_ELS_REQ;
  2360. Type = FC_TYPE_ELS;
  2361. w5p->hcsw.Rctl = Rctl;
  2362. w5p->hcsw.Type = Type;
  2363. }
  2364. }
  2365. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2366. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2367. "0313 Ring %d handler: unexpected Rctl x%x "
  2368. "Type x%x received\n",
  2369. pring->ringno, Rctl, Type);
  2370. return 1;
  2371. }
  2372. /**
  2373. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2374. * @phba: Pointer to HBA context object.
  2375. * @pring: Pointer to driver SLI ring object.
  2376. * @prspiocb: Pointer to response iocb object.
  2377. *
  2378. * This function looks up the iocb_lookup table to get the command iocb
  2379. * corresponding to the given response iocb using the iotag of the
  2380. * response iocb. This function is called with the hbalock held.
  2381. * This function returns the command iocb object if it finds the command
  2382. * iocb else returns NULL.
  2383. **/
  2384. static struct lpfc_iocbq *
  2385. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2386. struct lpfc_sli_ring *pring,
  2387. struct lpfc_iocbq *prspiocb)
  2388. {
  2389. struct lpfc_iocbq *cmd_iocb = NULL;
  2390. uint16_t iotag;
  2391. iotag = prspiocb->iocb.ulpIoTag;
  2392. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2393. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2394. list_del_init(&cmd_iocb->list);
  2395. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2396. pring->txcmplq_cnt--;
  2397. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2398. }
  2399. return cmd_iocb;
  2400. }
  2401. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2402. "0317 iotag x%x is out off "
  2403. "range: max iotag x%x wd0 x%x\n",
  2404. iotag, phba->sli.last_iotag,
  2405. *(((uint32_t *) &prspiocb->iocb) + 7));
  2406. return NULL;
  2407. }
  2408. /**
  2409. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2410. * @phba: Pointer to HBA context object.
  2411. * @pring: Pointer to driver SLI ring object.
  2412. * @iotag: IOCB tag.
  2413. *
  2414. * This function looks up the iocb_lookup table to get the command iocb
  2415. * corresponding to the given iotag. This function is called with the
  2416. * hbalock held.
  2417. * This function returns the command iocb object if it finds the command
  2418. * iocb else returns NULL.
  2419. **/
  2420. static struct lpfc_iocbq *
  2421. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2422. struct lpfc_sli_ring *pring, uint16_t iotag)
  2423. {
  2424. struct lpfc_iocbq *cmd_iocb;
  2425. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2426. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2427. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2428. /* remove from txcmpl queue list */
  2429. list_del_init(&cmd_iocb->list);
  2430. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2431. pring->txcmplq_cnt--;
  2432. return cmd_iocb;
  2433. }
  2434. }
  2435. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2436. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2437. iotag, phba->sli.last_iotag);
  2438. return NULL;
  2439. }
  2440. /**
  2441. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2442. * @phba: Pointer to HBA context object.
  2443. * @pring: Pointer to driver SLI ring object.
  2444. * @saveq: Pointer to the response iocb to be processed.
  2445. *
  2446. * This function is called by the ring event handler for non-fcp
  2447. * rings when there is a new response iocb in the response ring.
  2448. * The caller is not required to hold any locks. This function
  2449. * gets the command iocb associated with the response iocb and
  2450. * calls the completion handler for the command iocb. If there
  2451. * is no completion handler, the function will free the resources
  2452. * associated with command iocb. If the response iocb is for
  2453. * an already aborted command iocb, the status of the completion
  2454. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2455. * This function always returns 1.
  2456. **/
  2457. static int
  2458. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2459. struct lpfc_iocbq *saveq)
  2460. {
  2461. struct lpfc_iocbq *cmdiocbp;
  2462. int rc = 1;
  2463. unsigned long iflag;
  2464. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2465. spin_lock_irqsave(&phba->hbalock, iflag);
  2466. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2467. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2468. if (cmdiocbp) {
  2469. if (cmdiocbp->iocb_cmpl) {
  2470. /*
  2471. * If an ELS command failed send an event to mgmt
  2472. * application.
  2473. */
  2474. if (saveq->iocb.ulpStatus &&
  2475. (pring->ringno == LPFC_ELS_RING) &&
  2476. (cmdiocbp->iocb.ulpCommand ==
  2477. CMD_ELS_REQUEST64_CR))
  2478. lpfc_send_els_failure_event(phba,
  2479. cmdiocbp, saveq);
  2480. /*
  2481. * Post all ELS completions to the worker thread.
  2482. * All other are passed to the completion callback.
  2483. */
  2484. if (pring->ringno == LPFC_ELS_RING) {
  2485. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2486. (cmdiocbp->iocb_flag &
  2487. LPFC_DRIVER_ABORTED)) {
  2488. spin_lock_irqsave(&phba->hbalock,
  2489. iflag);
  2490. cmdiocbp->iocb_flag &=
  2491. ~LPFC_DRIVER_ABORTED;
  2492. spin_unlock_irqrestore(&phba->hbalock,
  2493. iflag);
  2494. saveq->iocb.ulpStatus =
  2495. IOSTAT_LOCAL_REJECT;
  2496. saveq->iocb.un.ulpWord[4] =
  2497. IOERR_SLI_ABORTED;
  2498. /* Firmware could still be in progress
  2499. * of DMAing payload, so don't free data
  2500. * buffer till after a hbeat.
  2501. */
  2502. spin_lock_irqsave(&phba->hbalock,
  2503. iflag);
  2504. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2505. spin_unlock_irqrestore(&phba->hbalock,
  2506. iflag);
  2507. }
  2508. if (phba->sli_rev == LPFC_SLI_REV4) {
  2509. if (saveq->iocb_flag &
  2510. LPFC_EXCHANGE_BUSY) {
  2511. /* Set cmdiocb flag for the
  2512. * exchange busy so sgl (xri)
  2513. * will not be released until
  2514. * the abort xri is received
  2515. * from hba.
  2516. */
  2517. spin_lock_irqsave(
  2518. &phba->hbalock, iflag);
  2519. cmdiocbp->iocb_flag |=
  2520. LPFC_EXCHANGE_BUSY;
  2521. spin_unlock_irqrestore(
  2522. &phba->hbalock, iflag);
  2523. }
  2524. if (cmdiocbp->iocb_flag &
  2525. LPFC_DRIVER_ABORTED) {
  2526. /*
  2527. * Clear LPFC_DRIVER_ABORTED
  2528. * bit in case it was driver
  2529. * initiated abort.
  2530. */
  2531. spin_lock_irqsave(
  2532. &phba->hbalock, iflag);
  2533. cmdiocbp->iocb_flag &=
  2534. ~LPFC_DRIVER_ABORTED;
  2535. spin_unlock_irqrestore(
  2536. &phba->hbalock, iflag);
  2537. cmdiocbp->iocb.ulpStatus =
  2538. IOSTAT_LOCAL_REJECT;
  2539. cmdiocbp->iocb.un.ulpWord[4] =
  2540. IOERR_ABORT_REQUESTED;
  2541. /*
  2542. * For SLI4, irsiocb contains
  2543. * NO_XRI in sli_xritag, it
  2544. * shall not affect releasing
  2545. * sgl (xri) process.
  2546. */
  2547. saveq->iocb.ulpStatus =
  2548. IOSTAT_LOCAL_REJECT;
  2549. saveq->iocb.un.ulpWord[4] =
  2550. IOERR_SLI_ABORTED;
  2551. spin_lock_irqsave(
  2552. &phba->hbalock, iflag);
  2553. saveq->iocb_flag |=
  2554. LPFC_DELAY_MEM_FREE;
  2555. spin_unlock_irqrestore(
  2556. &phba->hbalock, iflag);
  2557. }
  2558. }
  2559. }
  2560. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2561. } else
  2562. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2563. } else {
  2564. /*
  2565. * Unknown initiating command based on the response iotag.
  2566. * This could be the case on the ELS ring because of
  2567. * lpfc_els_abort().
  2568. */
  2569. if (pring->ringno != LPFC_ELS_RING) {
  2570. /*
  2571. * Ring <ringno> handler: unexpected completion IoTag
  2572. * <IoTag>
  2573. */
  2574. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2575. "0322 Ring %d handler: "
  2576. "unexpected completion IoTag x%x "
  2577. "Data: x%x x%x x%x x%x\n",
  2578. pring->ringno,
  2579. saveq->iocb.ulpIoTag,
  2580. saveq->iocb.ulpStatus,
  2581. saveq->iocb.un.ulpWord[4],
  2582. saveq->iocb.ulpCommand,
  2583. saveq->iocb.ulpContext);
  2584. }
  2585. }
  2586. return rc;
  2587. }
  2588. /**
  2589. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2590. * @phba: Pointer to HBA context object.
  2591. * @pring: Pointer to driver SLI ring object.
  2592. *
  2593. * This function is called from the iocb ring event handlers when
  2594. * put pointer is ahead of the get pointer for a ring. This function signal
  2595. * an error attention condition to the worker thread and the worker
  2596. * thread will transition the HBA to offline state.
  2597. **/
  2598. static void
  2599. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2600. {
  2601. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2602. /*
  2603. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2604. * rsp ring <portRspMax>
  2605. */
  2606. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2607. "0312 Ring %d handler: portRspPut %d "
  2608. "is bigger than rsp ring %d\n",
  2609. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2610. pring->sli.sli3.numRiocb);
  2611. phba->link_state = LPFC_HBA_ERROR;
  2612. /*
  2613. * All error attention handlers are posted to
  2614. * worker thread
  2615. */
  2616. phba->work_ha |= HA_ERATT;
  2617. phba->work_hs = HS_FFER3;
  2618. lpfc_worker_wake_up(phba);
  2619. return;
  2620. }
  2621. /**
  2622. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2623. * @ptr: Pointer to address of HBA context object.
  2624. *
  2625. * This function is invoked by the Error Attention polling timer when the
  2626. * timer times out. It will check the SLI Error Attention register for
  2627. * possible attention events. If so, it will post an Error Attention event
  2628. * and wake up worker thread to process it. Otherwise, it will set up the
  2629. * Error Attention polling timer for the next poll.
  2630. **/
  2631. void lpfc_poll_eratt(unsigned long ptr)
  2632. {
  2633. struct lpfc_hba *phba;
  2634. uint32_t eratt = 0, rem;
  2635. uint64_t sli_intr, cnt;
  2636. phba = (struct lpfc_hba *)ptr;
  2637. /* Here we will also keep track of interrupts per sec of the hba */
  2638. sli_intr = phba->sli.slistat.sli_intr;
  2639. if (phba->sli.slistat.sli_prev_intr > sli_intr)
  2640. cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
  2641. sli_intr);
  2642. else
  2643. cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
  2644. /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
  2645. rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
  2646. phba->sli.slistat.sli_ips = cnt;
  2647. phba->sli.slistat.sli_prev_intr = sli_intr;
  2648. /* Check chip HA register for error event */
  2649. eratt = lpfc_sli_check_eratt(phba);
  2650. if (eratt)
  2651. /* Tell the worker thread there is work to do */
  2652. lpfc_worker_wake_up(phba);
  2653. else
  2654. /* Restart the timer for next eratt poll */
  2655. mod_timer(&phba->eratt_poll, jiffies +
  2656. HZ * LPFC_ERATT_POLL_INTERVAL);
  2657. return;
  2658. }
  2659. /**
  2660. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2661. * @phba: Pointer to HBA context object.
  2662. * @pring: Pointer to driver SLI ring object.
  2663. * @mask: Host attention register mask for this ring.
  2664. *
  2665. * This function is called from the interrupt context when there is a ring
  2666. * event for the fcp ring. The caller does not hold any lock.
  2667. * The function processes each response iocb in the response ring until it
  2668. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2669. * LE bit set. The function will call the completion handler of the command iocb
  2670. * if the response iocb indicates a completion for a command iocb or it is
  2671. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2672. * function if this is an unsolicited iocb.
  2673. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2674. * to check it explicitly.
  2675. */
  2676. int
  2677. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2678. struct lpfc_sli_ring *pring, uint32_t mask)
  2679. {
  2680. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2681. IOCB_t *irsp = NULL;
  2682. IOCB_t *entry = NULL;
  2683. struct lpfc_iocbq *cmdiocbq = NULL;
  2684. struct lpfc_iocbq rspiocbq;
  2685. uint32_t status;
  2686. uint32_t portRspPut, portRspMax;
  2687. int rc = 1;
  2688. lpfc_iocb_type type;
  2689. unsigned long iflag;
  2690. uint32_t rsp_cmpl = 0;
  2691. spin_lock_irqsave(&phba->hbalock, iflag);
  2692. pring->stats.iocb_event++;
  2693. /*
  2694. * The next available response entry should never exceed the maximum
  2695. * entries. If it does, treat it as an adapter hardware error.
  2696. */
  2697. portRspMax = pring->sli.sli3.numRiocb;
  2698. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2699. if (unlikely(portRspPut >= portRspMax)) {
  2700. lpfc_sli_rsp_pointers_error(phba, pring);
  2701. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2702. return 1;
  2703. }
  2704. if (phba->fcp_ring_in_use) {
  2705. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2706. return 1;
  2707. } else
  2708. phba->fcp_ring_in_use = 1;
  2709. rmb();
  2710. while (pring->sli.sli3.rspidx != portRspPut) {
  2711. /*
  2712. * Fetch an entry off the ring and copy it into a local data
  2713. * structure. The copy involves a byte-swap since the
  2714. * network byte order and pci byte orders are different.
  2715. */
  2716. entry = lpfc_resp_iocb(phba, pring);
  2717. phba->last_completion_time = jiffies;
  2718. if (++pring->sli.sli3.rspidx >= portRspMax)
  2719. pring->sli.sli3.rspidx = 0;
  2720. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2721. (uint32_t *) &rspiocbq.iocb,
  2722. phba->iocb_rsp_size);
  2723. INIT_LIST_HEAD(&(rspiocbq.list));
  2724. irsp = &rspiocbq.iocb;
  2725. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2726. pring->stats.iocb_rsp++;
  2727. rsp_cmpl++;
  2728. if (unlikely(irsp->ulpStatus)) {
  2729. /*
  2730. * If resource errors reported from HBA, reduce
  2731. * queuedepths of the SCSI device.
  2732. */
  2733. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2734. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2735. IOERR_NO_RESOURCES)) {
  2736. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2737. phba->lpfc_rampdown_queue_depth(phba);
  2738. spin_lock_irqsave(&phba->hbalock, iflag);
  2739. }
  2740. /* Rsp ring <ringno> error: IOCB */
  2741. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2742. "0336 Rsp Ring %d error: IOCB Data: "
  2743. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2744. pring->ringno,
  2745. irsp->un.ulpWord[0],
  2746. irsp->un.ulpWord[1],
  2747. irsp->un.ulpWord[2],
  2748. irsp->un.ulpWord[3],
  2749. irsp->un.ulpWord[4],
  2750. irsp->un.ulpWord[5],
  2751. *(uint32_t *)&irsp->un1,
  2752. *((uint32_t *)&irsp->un1 + 1));
  2753. }
  2754. switch (type) {
  2755. case LPFC_ABORT_IOCB:
  2756. case LPFC_SOL_IOCB:
  2757. /*
  2758. * Idle exchange closed via ABTS from port. No iocb
  2759. * resources need to be recovered.
  2760. */
  2761. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2762. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2763. "0333 IOCB cmd 0x%x"
  2764. " processed. Skipping"
  2765. " completion\n",
  2766. irsp->ulpCommand);
  2767. break;
  2768. }
  2769. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2770. &rspiocbq);
  2771. if (unlikely(!cmdiocbq))
  2772. break;
  2773. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2774. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2775. if (cmdiocbq->iocb_cmpl) {
  2776. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2777. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2778. &rspiocbq);
  2779. spin_lock_irqsave(&phba->hbalock, iflag);
  2780. }
  2781. break;
  2782. case LPFC_UNSOL_IOCB:
  2783. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2784. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2785. spin_lock_irqsave(&phba->hbalock, iflag);
  2786. break;
  2787. default:
  2788. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2789. char adaptermsg[LPFC_MAX_ADPTMSG];
  2790. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2791. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2792. MAX_MSG_DATA);
  2793. dev_warn(&((phba->pcidev)->dev),
  2794. "lpfc%d: %s\n",
  2795. phba->brd_no, adaptermsg);
  2796. } else {
  2797. /* Unknown IOCB command */
  2798. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2799. "0334 Unknown IOCB command "
  2800. "Data: x%x, x%x x%x x%x x%x\n",
  2801. type, irsp->ulpCommand,
  2802. irsp->ulpStatus,
  2803. irsp->ulpIoTag,
  2804. irsp->ulpContext);
  2805. }
  2806. break;
  2807. }
  2808. /*
  2809. * The response IOCB has been processed. Update the ring
  2810. * pointer in SLIM. If the port response put pointer has not
  2811. * been updated, sync the pgp->rspPutInx and fetch the new port
  2812. * response put pointer.
  2813. */
  2814. writel(pring->sli.sli3.rspidx,
  2815. &phba->host_gp[pring->ringno].rspGetInx);
  2816. if (pring->sli.sli3.rspidx == portRspPut)
  2817. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2818. }
  2819. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2820. pring->stats.iocb_rsp_full++;
  2821. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2822. writel(status, phba->CAregaddr);
  2823. readl(phba->CAregaddr);
  2824. }
  2825. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2826. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2827. pring->stats.iocb_cmd_empty++;
  2828. /* Force update of the local copy of cmdGetInx */
  2829. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2830. lpfc_sli_resume_iocb(phba, pring);
  2831. if ((pring->lpfc_sli_cmd_available))
  2832. (pring->lpfc_sli_cmd_available) (phba, pring);
  2833. }
  2834. phba->fcp_ring_in_use = 0;
  2835. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2836. return rc;
  2837. }
  2838. /**
  2839. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2840. * @phba: Pointer to HBA context object.
  2841. * @pring: Pointer to driver SLI ring object.
  2842. * @rspiocbp: Pointer to driver response IOCB object.
  2843. *
  2844. * This function is called from the worker thread when there is a slow-path
  2845. * response IOCB to process. This function chains all the response iocbs until
  2846. * seeing the iocb with the LE bit set. The function will call
  2847. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2848. * completion of a command iocb. The function will call the
  2849. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2850. * The function frees the resources or calls the completion handler if this
  2851. * iocb is an abort completion. The function returns NULL when the response
  2852. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2853. * this function shall chain the iocb on to the iocb_continueq and return the
  2854. * response iocb passed in.
  2855. **/
  2856. static struct lpfc_iocbq *
  2857. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2858. struct lpfc_iocbq *rspiocbp)
  2859. {
  2860. struct lpfc_iocbq *saveq;
  2861. struct lpfc_iocbq *cmdiocbp;
  2862. struct lpfc_iocbq *next_iocb;
  2863. IOCB_t *irsp = NULL;
  2864. uint32_t free_saveq;
  2865. uint8_t iocb_cmd_type;
  2866. lpfc_iocb_type type;
  2867. unsigned long iflag;
  2868. int rc;
  2869. spin_lock_irqsave(&phba->hbalock, iflag);
  2870. /* First add the response iocb to the countinueq list */
  2871. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2872. pring->iocb_continueq_cnt++;
  2873. /* Now, determine whether the list is completed for processing */
  2874. irsp = &rspiocbp->iocb;
  2875. if (irsp->ulpLe) {
  2876. /*
  2877. * By default, the driver expects to free all resources
  2878. * associated with this iocb completion.
  2879. */
  2880. free_saveq = 1;
  2881. saveq = list_get_first(&pring->iocb_continueq,
  2882. struct lpfc_iocbq, list);
  2883. irsp = &(saveq->iocb);
  2884. list_del_init(&pring->iocb_continueq);
  2885. pring->iocb_continueq_cnt = 0;
  2886. pring->stats.iocb_rsp++;
  2887. /*
  2888. * If resource errors reported from HBA, reduce
  2889. * queuedepths of the SCSI device.
  2890. */
  2891. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2892. ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
  2893. IOERR_NO_RESOURCES)) {
  2894. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2895. phba->lpfc_rampdown_queue_depth(phba);
  2896. spin_lock_irqsave(&phba->hbalock, iflag);
  2897. }
  2898. if (irsp->ulpStatus) {
  2899. /* Rsp ring <ringno> error: IOCB */
  2900. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2901. "0328 Rsp Ring %d error: "
  2902. "IOCB Data: "
  2903. "x%x x%x x%x x%x "
  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\n",
  2907. pring->ringno,
  2908. irsp->un.ulpWord[0],
  2909. irsp->un.ulpWord[1],
  2910. irsp->un.ulpWord[2],
  2911. irsp->un.ulpWord[3],
  2912. irsp->un.ulpWord[4],
  2913. irsp->un.ulpWord[5],
  2914. *(((uint32_t *) irsp) + 6),
  2915. *(((uint32_t *) irsp) + 7),
  2916. *(((uint32_t *) irsp) + 8),
  2917. *(((uint32_t *) irsp) + 9),
  2918. *(((uint32_t *) irsp) + 10),
  2919. *(((uint32_t *) irsp) + 11),
  2920. *(((uint32_t *) irsp) + 12),
  2921. *(((uint32_t *) irsp) + 13),
  2922. *(((uint32_t *) irsp) + 14),
  2923. *(((uint32_t *) irsp) + 15));
  2924. }
  2925. /*
  2926. * Fetch the IOCB command type and call the correct completion
  2927. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2928. * get freed back to the lpfc_iocb_list by the discovery
  2929. * kernel thread.
  2930. */
  2931. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2932. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2933. switch (type) {
  2934. case LPFC_SOL_IOCB:
  2935. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2936. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2937. spin_lock_irqsave(&phba->hbalock, iflag);
  2938. break;
  2939. case LPFC_UNSOL_IOCB:
  2940. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2941. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2942. spin_lock_irqsave(&phba->hbalock, iflag);
  2943. if (!rc)
  2944. free_saveq = 0;
  2945. break;
  2946. case LPFC_ABORT_IOCB:
  2947. cmdiocbp = NULL;
  2948. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2949. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2950. saveq);
  2951. if (cmdiocbp) {
  2952. /* Call the specified completion routine */
  2953. if (cmdiocbp->iocb_cmpl) {
  2954. spin_unlock_irqrestore(&phba->hbalock,
  2955. iflag);
  2956. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2957. saveq);
  2958. spin_lock_irqsave(&phba->hbalock,
  2959. iflag);
  2960. } else
  2961. __lpfc_sli_release_iocbq(phba,
  2962. cmdiocbp);
  2963. }
  2964. break;
  2965. case LPFC_UNKNOWN_IOCB:
  2966. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2967. char adaptermsg[LPFC_MAX_ADPTMSG];
  2968. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2969. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2970. MAX_MSG_DATA);
  2971. dev_warn(&((phba->pcidev)->dev),
  2972. "lpfc%d: %s\n",
  2973. phba->brd_no, adaptermsg);
  2974. } else {
  2975. /* Unknown IOCB command */
  2976. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2977. "0335 Unknown IOCB "
  2978. "command Data: x%x "
  2979. "x%x x%x x%x\n",
  2980. irsp->ulpCommand,
  2981. irsp->ulpStatus,
  2982. irsp->ulpIoTag,
  2983. irsp->ulpContext);
  2984. }
  2985. break;
  2986. }
  2987. if (free_saveq) {
  2988. list_for_each_entry_safe(rspiocbp, next_iocb,
  2989. &saveq->list, list) {
  2990. list_del(&rspiocbp->list);
  2991. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2992. }
  2993. __lpfc_sli_release_iocbq(phba, saveq);
  2994. }
  2995. rspiocbp = NULL;
  2996. }
  2997. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2998. return rspiocbp;
  2999. }
  3000. /**
  3001. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  3002. * @phba: Pointer to HBA context object.
  3003. * @pring: Pointer to driver SLI ring object.
  3004. * @mask: Host attention register mask for this ring.
  3005. *
  3006. * This routine wraps the actual slow_ring event process routine from the
  3007. * API jump table function pointer from the lpfc_hba struct.
  3008. **/
  3009. void
  3010. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  3011. struct lpfc_sli_ring *pring, uint32_t mask)
  3012. {
  3013. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  3014. }
  3015. /**
  3016. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  3017. * @phba: Pointer to HBA context object.
  3018. * @pring: Pointer to driver SLI ring object.
  3019. * @mask: Host attention register mask for this ring.
  3020. *
  3021. * This function is called from the worker thread when there is a ring event
  3022. * for non-fcp rings. The caller does not hold any lock. The function will
  3023. * remove each response iocb in the response ring and calls the handle
  3024. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3025. **/
  3026. static void
  3027. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  3028. struct lpfc_sli_ring *pring, uint32_t mask)
  3029. {
  3030. struct lpfc_pgp *pgp;
  3031. IOCB_t *entry;
  3032. IOCB_t *irsp = NULL;
  3033. struct lpfc_iocbq *rspiocbp = NULL;
  3034. uint32_t portRspPut, portRspMax;
  3035. unsigned long iflag;
  3036. uint32_t status;
  3037. pgp = &phba->port_gp[pring->ringno];
  3038. spin_lock_irqsave(&phba->hbalock, iflag);
  3039. pring->stats.iocb_event++;
  3040. /*
  3041. * The next available response entry should never exceed the maximum
  3042. * entries. If it does, treat it as an adapter hardware error.
  3043. */
  3044. portRspMax = pring->sli.sli3.numRiocb;
  3045. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3046. if (portRspPut >= portRspMax) {
  3047. /*
  3048. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  3049. * rsp ring <portRspMax>
  3050. */
  3051. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3052. "0303 Ring %d handler: portRspPut %d "
  3053. "is bigger than rsp ring %d\n",
  3054. pring->ringno, portRspPut, portRspMax);
  3055. phba->link_state = LPFC_HBA_ERROR;
  3056. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3057. phba->work_hs = HS_FFER3;
  3058. lpfc_handle_eratt(phba);
  3059. return;
  3060. }
  3061. rmb();
  3062. while (pring->sli.sli3.rspidx != portRspPut) {
  3063. /*
  3064. * Build a completion list and call the appropriate handler.
  3065. * The process is to get the next available response iocb, get
  3066. * a free iocb from the list, copy the response data into the
  3067. * free iocb, insert to the continuation list, and update the
  3068. * next response index to slim. This process makes response
  3069. * iocb's in the ring available to DMA as fast as possible but
  3070. * pays a penalty for a copy operation. Since the iocb is
  3071. * only 32 bytes, this penalty is considered small relative to
  3072. * the PCI reads for register values and a slim write. When
  3073. * the ulpLe field is set, the entire Command has been
  3074. * received.
  3075. */
  3076. entry = lpfc_resp_iocb(phba, pring);
  3077. phba->last_completion_time = jiffies;
  3078. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3079. if (rspiocbp == NULL) {
  3080. printk(KERN_ERR "%s: out of buffers! Failing "
  3081. "completion.\n", __func__);
  3082. break;
  3083. }
  3084. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3085. phba->iocb_rsp_size);
  3086. irsp = &rspiocbp->iocb;
  3087. if (++pring->sli.sli3.rspidx >= portRspMax)
  3088. pring->sli.sli3.rspidx = 0;
  3089. if (pring->ringno == LPFC_ELS_RING) {
  3090. lpfc_debugfs_slow_ring_trc(phba,
  3091. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3092. *(((uint32_t *) irsp) + 4),
  3093. *(((uint32_t *) irsp) + 6),
  3094. *(((uint32_t *) irsp) + 7));
  3095. }
  3096. writel(pring->sli.sli3.rspidx,
  3097. &phba->host_gp[pring->ringno].rspGetInx);
  3098. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3099. /* Handle the response IOCB */
  3100. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3101. spin_lock_irqsave(&phba->hbalock, iflag);
  3102. /*
  3103. * If the port response put pointer has not been updated, sync
  3104. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3105. * response put pointer.
  3106. */
  3107. if (pring->sli.sli3.rspidx == portRspPut) {
  3108. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3109. }
  3110. } /* while (pring->sli.sli3.rspidx != portRspPut) */
  3111. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3112. /* At least one response entry has been freed */
  3113. pring->stats.iocb_rsp_full++;
  3114. /* SET RxRE_RSP in Chip Att register */
  3115. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3116. writel(status, phba->CAregaddr);
  3117. readl(phba->CAregaddr); /* flush */
  3118. }
  3119. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3120. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3121. pring->stats.iocb_cmd_empty++;
  3122. /* Force update of the local copy of cmdGetInx */
  3123. pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3124. lpfc_sli_resume_iocb(phba, pring);
  3125. if ((pring->lpfc_sli_cmd_available))
  3126. (pring->lpfc_sli_cmd_available) (phba, pring);
  3127. }
  3128. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3129. return;
  3130. }
  3131. /**
  3132. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3133. * @phba: Pointer to HBA context object.
  3134. * @pring: Pointer to driver SLI ring object.
  3135. * @mask: Host attention register mask for this ring.
  3136. *
  3137. * This function is called from the worker thread when there is a pending
  3138. * ELS response iocb on the driver internal slow-path response iocb worker
  3139. * queue. The caller does not hold any lock. The function will remove each
  3140. * response iocb from the response worker queue and calls the handle
  3141. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3142. **/
  3143. static void
  3144. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3145. struct lpfc_sli_ring *pring, uint32_t mask)
  3146. {
  3147. struct lpfc_iocbq *irspiocbq;
  3148. struct hbq_dmabuf *dmabuf;
  3149. struct lpfc_cq_event *cq_event;
  3150. unsigned long iflag;
  3151. spin_lock_irqsave(&phba->hbalock, iflag);
  3152. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3153. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3154. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3155. /* Get the response iocb from the head of work queue */
  3156. spin_lock_irqsave(&phba->hbalock, iflag);
  3157. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3158. cq_event, struct lpfc_cq_event, list);
  3159. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3160. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3161. case CQE_CODE_COMPL_WQE:
  3162. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3163. cq_event);
  3164. /* Translate ELS WCQE to response IOCBQ */
  3165. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3166. irspiocbq);
  3167. if (irspiocbq)
  3168. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3169. irspiocbq);
  3170. break;
  3171. case CQE_CODE_RECEIVE:
  3172. case CQE_CODE_RECEIVE_V1:
  3173. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3174. cq_event);
  3175. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3176. break;
  3177. default:
  3178. break;
  3179. }
  3180. }
  3181. }
  3182. /**
  3183. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3184. * @phba: Pointer to HBA context object.
  3185. * @pring: Pointer to driver SLI ring object.
  3186. *
  3187. * This function aborts all iocbs in the given ring and frees all the iocb
  3188. * objects in txq. This function issues an abort iocb for all the iocb commands
  3189. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3190. * the return of this function. The caller is not required to hold any locks.
  3191. **/
  3192. void
  3193. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3194. {
  3195. LIST_HEAD(completions);
  3196. struct lpfc_iocbq *iocb, *next_iocb;
  3197. if (pring->ringno == LPFC_ELS_RING) {
  3198. lpfc_fabric_abort_hba(phba);
  3199. }
  3200. /* Error everything on txq and txcmplq
  3201. * First do the txq.
  3202. */
  3203. spin_lock_irq(&phba->hbalock);
  3204. list_splice_init(&pring->txq, &completions);
  3205. pring->txq_cnt = 0;
  3206. /* Next issue ABTS for everything on the txcmplq */
  3207. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3208. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3209. spin_unlock_irq(&phba->hbalock);
  3210. /* Cancel all the IOCBs from the completions list */
  3211. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3212. IOERR_SLI_ABORTED);
  3213. }
  3214. /**
  3215. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3216. * @phba: Pointer to HBA context object.
  3217. *
  3218. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3219. * objects in txq and txcmplq. This function will not issue abort iocbs
  3220. * for all the iocb commands in txcmplq, they will just be returned with
  3221. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3222. * slot has been permanently disabled.
  3223. **/
  3224. void
  3225. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3226. {
  3227. LIST_HEAD(txq);
  3228. LIST_HEAD(txcmplq);
  3229. struct lpfc_sli *psli = &phba->sli;
  3230. struct lpfc_sli_ring *pring;
  3231. /* Currently, only one fcp ring */
  3232. pring = &psli->ring[psli->fcp_ring];
  3233. spin_lock_irq(&phba->hbalock);
  3234. /* Retrieve everything on txq */
  3235. list_splice_init(&pring->txq, &txq);
  3236. pring->txq_cnt = 0;
  3237. /* Retrieve everything on the txcmplq */
  3238. list_splice_init(&pring->txcmplq, &txcmplq);
  3239. pring->txcmplq_cnt = 0;
  3240. /* Indicate the I/O queues are flushed */
  3241. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3242. spin_unlock_irq(&phba->hbalock);
  3243. /* Flush the txq */
  3244. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3245. IOERR_SLI_DOWN);
  3246. /* Flush the txcmpq */
  3247. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3248. IOERR_SLI_DOWN);
  3249. }
  3250. /**
  3251. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3252. * @phba: Pointer to HBA context object.
  3253. * @mask: Bit mask to be checked.
  3254. *
  3255. * This function reads the host status register and compares
  3256. * with the provided bit mask to check if HBA completed
  3257. * the restart. This function will wait in a loop for the
  3258. * HBA to complete restart. If the HBA does not restart within
  3259. * 15 iterations, the function will reset the HBA again. The
  3260. * function returns 1 when HBA fail to restart otherwise returns
  3261. * zero.
  3262. **/
  3263. static int
  3264. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3265. {
  3266. uint32_t status;
  3267. int i = 0;
  3268. int retval = 0;
  3269. /* Read the HBA Host Status Register */
  3270. if (lpfc_readl(phba->HSregaddr, &status))
  3271. return 1;
  3272. /*
  3273. * Check status register every 100ms for 5 retries, then every
  3274. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3275. * every 2.5 sec for 4.
  3276. * Break our of the loop if errors occurred during init.
  3277. */
  3278. while (((status & mask) != mask) &&
  3279. !(status & HS_FFERM) &&
  3280. i++ < 20) {
  3281. if (i <= 5)
  3282. msleep(10);
  3283. else if (i <= 10)
  3284. msleep(500);
  3285. else
  3286. msleep(2500);
  3287. if (i == 15) {
  3288. /* Do post */
  3289. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3290. lpfc_sli_brdrestart(phba);
  3291. }
  3292. /* Read the HBA Host Status Register */
  3293. if (lpfc_readl(phba->HSregaddr, &status)) {
  3294. retval = 1;
  3295. break;
  3296. }
  3297. }
  3298. /* Check to see if any errors occurred during init */
  3299. if ((status & HS_FFERM) || (i >= 20)) {
  3300. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3301. "2751 Adapter failed to restart, "
  3302. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3303. status,
  3304. readl(phba->MBslimaddr + 0xa8),
  3305. readl(phba->MBslimaddr + 0xac));
  3306. phba->link_state = LPFC_HBA_ERROR;
  3307. retval = 1;
  3308. }
  3309. return retval;
  3310. }
  3311. /**
  3312. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3313. * @phba: Pointer to HBA context object.
  3314. * @mask: Bit mask to be checked.
  3315. *
  3316. * This function checks the host status register to check if HBA is
  3317. * ready. This function will wait in a loop for the HBA to be ready
  3318. * If the HBA is not ready , the function will will reset the HBA PCI
  3319. * function again. The function returns 1 when HBA fail to be ready
  3320. * otherwise returns zero.
  3321. **/
  3322. static int
  3323. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3324. {
  3325. uint32_t status;
  3326. int retval = 0;
  3327. /* Read the HBA Host Status Register */
  3328. status = lpfc_sli4_post_status_check(phba);
  3329. if (status) {
  3330. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3331. lpfc_sli_brdrestart(phba);
  3332. status = lpfc_sli4_post_status_check(phba);
  3333. }
  3334. /* Check to see if any errors occurred during init */
  3335. if (status) {
  3336. phba->link_state = LPFC_HBA_ERROR;
  3337. retval = 1;
  3338. } else
  3339. phba->sli4_hba.intr_enable = 0;
  3340. return retval;
  3341. }
  3342. /**
  3343. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3344. * @phba: Pointer to HBA context object.
  3345. * @mask: Bit mask to be checked.
  3346. *
  3347. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3348. * from the API jump table function pointer from the lpfc_hba struct.
  3349. **/
  3350. int
  3351. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3352. {
  3353. return phba->lpfc_sli_brdready(phba, mask);
  3354. }
  3355. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3356. /**
  3357. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3358. * @phba: Pointer to HBA context object.
  3359. *
  3360. * This function is called before resetting an HBA. This function is called
  3361. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3362. **/
  3363. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3364. {
  3365. uint32_t __iomem *resp_buf;
  3366. uint32_t __iomem *mbox_buf;
  3367. volatile uint32_t mbox;
  3368. uint32_t hc_copy, ha_copy, resp_data;
  3369. int i;
  3370. uint8_t hdrtype;
  3371. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3372. if (hdrtype != 0x80 ||
  3373. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3374. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3375. return;
  3376. /*
  3377. * Tell the other part of the chip to suspend temporarily all
  3378. * its DMA activity.
  3379. */
  3380. resp_buf = phba->MBslimaddr;
  3381. /* Disable the error attention */
  3382. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3383. return;
  3384. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3385. readl(phba->HCregaddr); /* flush */
  3386. phba->link_flag |= LS_IGNORE_ERATT;
  3387. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3388. return;
  3389. if (ha_copy & HA_ERATT) {
  3390. /* Clear Chip error bit */
  3391. writel(HA_ERATT, phba->HAregaddr);
  3392. phba->pport->stopped = 1;
  3393. }
  3394. mbox = 0;
  3395. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3396. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3397. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3398. mbox_buf = phba->MBslimaddr;
  3399. writel(mbox, mbox_buf);
  3400. for (i = 0; i < 50; i++) {
  3401. if (lpfc_readl((resp_buf + 1), &resp_data))
  3402. return;
  3403. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3404. mdelay(1);
  3405. else
  3406. break;
  3407. }
  3408. resp_data = 0;
  3409. if (lpfc_readl((resp_buf + 1), &resp_data))
  3410. return;
  3411. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3412. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3413. phba->pport->stopped)
  3414. goto restore_hc;
  3415. else
  3416. goto clear_errat;
  3417. }
  3418. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3419. resp_data = 0;
  3420. for (i = 0; i < 500; i++) {
  3421. if (lpfc_readl(resp_buf, &resp_data))
  3422. return;
  3423. if (resp_data != mbox)
  3424. mdelay(1);
  3425. else
  3426. break;
  3427. }
  3428. clear_errat:
  3429. while (++i < 500) {
  3430. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3431. return;
  3432. if (!(ha_copy & HA_ERATT))
  3433. mdelay(1);
  3434. else
  3435. break;
  3436. }
  3437. if (readl(phba->HAregaddr) & HA_ERATT) {
  3438. writel(HA_ERATT, phba->HAregaddr);
  3439. phba->pport->stopped = 1;
  3440. }
  3441. restore_hc:
  3442. phba->link_flag &= ~LS_IGNORE_ERATT;
  3443. writel(hc_copy, phba->HCregaddr);
  3444. readl(phba->HCregaddr); /* flush */
  3445. }
  3446. /**
  3447. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3448. * @phba: Pointer to HBA context object.
  3449. *
  3450. * This function issues a kill_board mailbox command and waits for
  3451. * the error attention interrupt. This function is called for stopping
  3452. * the firmware processing. The caller is not required to hold any
  3453. * locks. This function calls lpfc_hba_down_post function to free
  3454. * any pending commands after the kill. The function will return 1 when it
  3455. * fails to kill the board else will return 0.
  3456. **/
  3457. int
  3458. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3459. {
  3460. struct lpfc_sli *psli;
  3461. LPFC_MBOXQ_t *pmb;
  3462. uint32_t status;
  3463. uint32_t ha_copy;
  3464. int retval;
  3465. int i = 0;
  3466. psli = &phba->sli;
  3467. /* Kill HBA */
  3468. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3469. "0329 Kill HBA Data: x%x x%x\n",
  3470. phba->pport->port_state, psli->sli_flag);
  3471. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3472. if (!pmb)
  3473. return 1;
  3474. /* Disable the error attention */
  3475. spin_lock_irq(&phba->hbalock);
  3476. if (lpfc_readl(phba->HCregaddr, &status)) {
  3477. spin_unlock_irq(&phba->hbalock);
  3478. mempool_free(pmb, phba->mbox_mem_pool);
  3479. return 1;
  3480. }
  3481. status &= ~HC_ERINT_ENA;
  3482. writel(status, phba->HCregaddr);
  3483. readl(phba->HCregaddr); /* flush */
  3484. phba->link_flag |= LS_IGNORE_ERATT;
  3485. spin_unlock_irq(&phba->hbalock);
  3486. lpfc_kill_board(phba, pmb);
  3487. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3488. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3489. if (retval != MBX_SUCCESS) {
  3490. if (retval != MBX_BUSY)
  3491. mempool_free(pmb, phba->mbox_mem_pool);
  3492. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3493. "2752 KILL_BOARD command failed retval %d\n",
  3494. retval);
  3495. spin_lock_irq(&phba->hbalock);
  3496. phba->link_flag &= ~LS_IGNORE_ERATT;
  3497. spin_unlock_irq(&phba->hbalock);
  3498. return 1;
  3499. }
  3500. spin_lock_irq(&phba->hbalock);
  3501. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3502. spin_unlock_irq(&phba->hbalock);
  3503. mempool_free(pmb, phba->mbox_mem_pool);
  3504. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3505. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3506. * 3 seconds we still set HBA_ERROR state because the status of the
  3507. * board is now undefined.
  3508. */
  3509. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3510. return 1;
  3511. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3512. mdelay(100);
  3513. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3514. return 1;
  3515. }
  3516. del_timer_sync(&psli->mbox_tmo);
  3517. if (ha_copy & HA_ERATT) {
  3518. writel(HA_ERATT, phba->HAregaddr);
  3519. phba->pport->stopped = 1;
  3520. }
  3521. spin_lock_irq(&phba->hbalock);
  3522. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3523. psli->mbox_active = NULL;
  3524. phba->link_flag &= ~LS_IGNORE_ERATT;
  3525. spin_unlock_irq(&phba->hbalock);
  3526. lpfc_hba_down_post(phba);
  3527. phba->link_state = LPFC_HBA_ERROR;
  3528. return ha_copy & HA_ERATT ? 0 : 1;
  3529. }
  3530. /**
  3531. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3532. * @phba: Pointer to HBA context object.
  3533. *
  3534. * This function resets the HBA by writing HC_INITFF to the control
  3535. * register. After the HBA resets, this function resets all the iocb ring
  3536. * indices. This function disables PCI layer parity checking during
  3537. * the reset.
  3538. * This function returns 0 always.
  3539. * The caller is not required to hold any locks.
  3540. **/
  3541. int
  3542. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3543. {
  3544. struct lpfc_sli *psli;
  3545. struct lpfc_sli_ring *pring;
  3546. uint16_t cfg_value;
  3547. int i;
  3548. psli = &phba->sli;
  3549. /* Reset HBA */
  3550. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3551. "0325 Reset HBA Data: x%x x%x\n",
  3552. phba->pport->port_state, psli->sli_flag);
  3553. /* perform board reset */
  3554. phba->fc_eventTag = 0;
  3555. phba->link_events = 0;
  3556. phba->pport->fc_myDID = 0;
  3557. phba->pport->fc_prevDID = 0;
  3558. /* Turn off parity checking and serr during the physical reset */
  3559. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3560. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3561. (cfg_value &
  3562. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3563. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3564. /* Now toggle INITFF bit in the Host Control Register */
  3565. writel(HC_INITFF, phba->HCregaddr);
  3566. mdelay(1);
  3567. readl(phba->HCregaddr); /* flush */
  3568. writel(0, phba->HCregaddr);
  3569. readl(phba->HCregaddr); /* flush */
  3570. /* Restore PCI cmd register */
  3571. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3572. /* Initialize relevant SLI info */
  3573. for (i = 0; i < psli->num_rings; i++) {
  3574. pring = &psli->ring[i];
  3575. pring->flag = 0;
  3576. pring->sli.sli3.rspidx = 0;
  3577. pring->sli.sli3.next_cmdidx = 0;
  3578. pring->sli.sli3.local_getidx = 0;
  3579. pring->sli.sli3.cmdidx = 0;
  3580. pring->missbufcnt = 0;
  3581. }
  3582. phba->link_state = LPFC_WARM_START;
  3583. return 0;
  3584. }
  3585. /**
  3586. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3587. * @phba: Pointer to HBA context object.
  3588. *
  3589. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3590. * checking during resets the device. The caller is not required to hold
  3591. * any locks.
  3592. *
  3593. * This function returns 0 always.
  3594. **/
  3595. int
  3596. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3597. {
  3598. struct lpfc_sli *psli = &phba->sli;
  3599. uint16_t cfg_value;
  3600. int rc;
  3601. /* Reset HBA */
  3602. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3603. "0295 Reset HBA Data: x%x x%x\n",
  3604. phba->pport->port_state, psli->sli_flag);
  3605. /* perform board reset */
  3606. phba->fc_eventTag = 0;
  3607. phba->link_events = 0;
  3608. phba->pport->fc_myDID = 0;
  3609. phba->pport->fc_prevDID = 0;
  3610. spin_lock_irq(&phba->hbalock);
  3611. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3612. phba->fcf.fcf_flag = 0;
  3613. spin_unlock_irq(&phba->hbalock);
  3614. /* Now physically reset the device */
  3615. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3616. "0389 Performing PCI function reset!\n");
  3617. /* Turn off parity checking and serr during the physical reset */
  3618. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3619. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3620. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3621. /* Perform FCoE PCI function reset before freeing queue memory */
  3622. rc = lpfc_pci_function_reset(phba);
  3623. lpfc_sli4_queue_destroy(phba);
  3624. /* Restore PCI cmd register */
  3625. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3626. return rc;
  3627. }
  3628. /**
  3629. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3630. * @phba: Pointer to HBA context object.
  3631. *
  3632. * This function is called in the SLI initialization code path to
  3633. * restart the HBA. The caller is not required to hold any lock.
  3634. * This function writes MBX_RESTART mailbox command to the SLIM and
  3635. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3636. * function to free any pending commands. The function enables
  3637. * POST only during the first initialization. The function returns zero.
  3638. * The function does not guarantee completion of MBX_RESTART mailbox
  3639. * command before the return of this function.
  3640. **/
  3641. static int
  3642. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3643. {
  3644. MAILBOX_t *mb;
  3645. struct lpfc_sli *psli;
  3646. volatile uint32_t word0;
  3647. void __iomem *to_slim;
  3648. uint32_t hba_aer_enabled;
  3649. spin_lock_irq(&phba->hbalock);
  3650. /* Take PCIe device Advanced Error Reporting (AER) state */
  3651. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3652. psli = &phba->sli;
  3653. /* Restart HBA */
  3654. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3655. "0337 Restart HBA Data: x%x x%x\n",
  3656. phba->pport->port_state, psli->sli_flag);
  3657. word0 = 0;
  3658. mb = (MAILBOX_t *) &word0;
  3659. mb->mbxCommand = MBX_RESTART;
  3660. mb->mbxHc = 1;
  3661. lpfc_reset_barrier(phba);
  3662. to_slim = phba->MBslimaddr;
  3663. writel(*(uint32_t *) mb, to_slim);
  3664. readl(to_slim); /* flush */
  3665. /* Only skip post after fc_ffinit is completed */
  3666. if (phba->pport->port_state)
  3667. word0 = 1; /* This is really setting up word1 */
  3668. else
  3669. word0 = 0; /* This is really setting up word1 */
  3670. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3671. writel(*(uint32_t *) mb, to_slim);
  3672. readl(to_slim); /* flush */
  3673. lpfc_sli_brdreset(phba);
  3674. phba->pport->stopped = 0;
  3675. phba->link_state = LPFC_INIT_START;
  3676. phba->hba_flag = 0;
  3677. spin_unlock_irq(&phba->hbalock);
  3678. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3679. psli->stats_start = get_seconds();
  3680. /* Give the INITFF and Post time to settle. */
  3681. mdelay(100);
  3682. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3683. if (hba_aer_enabled)
  3684. pci_disable_pcie_error_reporting(phba->pcidev);
  3685. lpfc_hba_down_post(phba);
  3686. return 0;
  3687. }
  3688. /**
  3689. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3690. * @phba: Pointer to HBA context object.
  3691. *
  3692. * This function is called in the SLI initialization code path to restart
  3693. * a SLI4 HBA. The caller is not required to hold any lock.
  3694. * At the end of the function, it calls lpfc_hba_down_post function to
  3695. * free any pending commands.
  3696. **/
  3697. static int
  3698. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3699. {
  3700. struct lpfc_sli *psli = &phba->sli;
  3701. uint32_t hba_aer_enabled;
  3702. int rc;
  3703. /* Restart HBA */
  3704. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3705. "0296 Restart HBA Data: x%x x%x\n",
  3706. phba->pport->port_state, psli->sli_flag);
  3707. /* Take PCIe device Advanced Error Reporting (AER) state */
  3708. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3709. rc = lpfc_sli4_brdreset(phba);
  3710. spin_lock_irq(&phba->hbalock);
  3711. phba->pport->stopped = 0;
  3712. phba->link_state = LPFC_INIT_START;
  3713. phba->hba_flag = 0;
  3714. spin_unlock_irq(&phba->hbalock);
  3715. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3716. psli->stats_start = get_seconds();
  3717. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3718. if (hba_aer_enabled)
  3719. pci_disable_pcie_error_reporting(phba->pcidev);
  3720. lpfc_hba_down_post(phba);
  3721. return rc;
  3722. }
  3723. /**
  3724. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3725. * @phba: Pointer to HBA context object.
  3726. *
  3727. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3728. * API jump table function pointer from the lpfc_hba struct.
  3729. **/
  3730. int
  3731. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3732. {
  3733. return phba->lpfc_sli_brdrestart(phba);
  3734. }
  3735. /**
  3736. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3737. * @phba: Pointer to HBA context object.
  3738. *
  3739. * This function is called after a HBA restart to wait for successful
  3740. * restart of the HBA. Successful restart of the HBA is indicated by
  3741. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3742. * iteration, the function will restart the HBA again. The function returns
  3743. * zero if HBA successfully restarted else returns negative error code.
  3744. **/
  3745. static int
  3746. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3747. {
  3748. uint32_t status, i = 0;
  3749. /* Read the HBA Host Status Register */
  3750. if (lpfc_readl(phba->HSregaddr, &status))
  3751. return -EIO;
  3752. /* Check status register to see what current state is */
  3753. i = 0;
  3754. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3755. /* Check every 10ms for 10 retries, then every 100ms for 90
  3756. * retries, then every 1 sec for 50 retires for a total of
  3757. * ~60 seconds before reset the board again and check every
  3758. * 1 sec for 50 retries. The up to 60 seconds before the
  3759. * board ready is required by the Falcon FIPS zeroization
  3760. * complete, and any reset the board in between shall cause
  3761. * restart of zeroization, further delay the board ready.
  3762. */
  3763. if (i++ >= 200) {
  3764. /* Adapter failed to init, timeout, status reg
  3765. <status> */
  3766. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3767. "0436 Adapter failed to init, "
  3768. "timeout, status reg x%x, "
  3769. "FW Data: A8 x%x AC x%x\n", status,
  3770. readl(phba->MBslimaddr + 0xa8),
  3771. readl(phba->MBslimaddr + 0xac));
  3772. phba->link_state = LPFC_HBA_ERROR;
  3773. return -ETIMEDOUT;
  3774. }
  3775. /* Check to see if any errors occurred during init */
  3776. if (status & HS_FFERM) {
  3777. /* ERROR: During chipset initialization */
  3778. /* Adapter failed to init, chipset, status reg
  3779. <status> */
  3780. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3781. "0437 Adapter failed to init, "
  3782. "chipset, status reg x%x, "
  3783. "FW Data: A8 x%x AC x%x\n", status,
  3784. readl(phba->MBslimaddr + 0xa8),
  3785. readl(phba->MBslimaddr + 0xac));
  3786. phba->link_state = LPFC_HBA_ERROR;
  3787. return -EIO;
  3788. }
  3789. if (i <= 10)
  3790. msleep(10);
  3791. else if (i <= 100)
  3792. msleep(100);
  3793. else
  3794. msleep(1000);
  3795. if (i == 150) {
  3796. /* Do post */
  3797. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3798. lpfc_sli_brdrestart(phba);
  3799. }
  3800. /* Read the HBA Host Status Register */
  3801. if (lpfc_readl(phba->HSregaddr, &status))
  3802. return -EIO;
  3803. }
  3804. /* Check to see if any errors occurred during init */
  3805. if (status & HS_FFERM) {
  3806. /* ERROR: During chipset initialization */
  3807. /* Adapter failed to init, chipset, status reg <status> */
  3808. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3809. "0438 Adapter failed to init, chipset, "
  3810. "status reg x%x, "
  3811. "FW Data: A8 x%x AC x%x\n", status,
  3812. readl(phba->MBslimaddr + 0xa8),
  3813. readl(phba->MBslimaddr + 0xac));
  3814. phba->link_state = LPFC_HBA_ERROR;
  3815. return -EIO;
  3816. }
  3817. /* Clear all interrupt enable conditions */
  3818. writel(0, phba->HCregaddr);
  3819. readl(phba->HCregaddr); /* flush */
  3820. /* setup host attn register */
  3821. writel(0xffffffff, phba->HAregaddr);
  3822. readl(phba->HAregaddr); /* flush */
  3823. return 0;
  3824. }
  3825. /**
  3826. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3827. *
  3828. * This function calculates and returns the number of HBQs required to be
  3829. * configured.
  3830. **/
  3831. int
  3832. lpfc_sli_hbq_count(void)
  3833. {
  3834. return ARRAY_SIZE(lpfc_hbq_defs);
  3835. }
  3836. /**
  3837. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3838. *
  3839. * This function adds the number of hbq entries in every HBQ to get
  3840. * the total number of hbq entries required for the HBA and returns
  3841. * the total count.
  3842. **/
  3843. static int
  3844. lpfc_sli_hbq_entry_count(void)
  3845. {
  3846. int hbq_count = lpfc_sli_hbq_count();
  3847. int count = 0;
  3848. int i;
  3849. for (i = 0; i < hbq_count; ++i)
  3850. count += lpfc_hbq_defs[i]->entry_count;
  3851. return count;
  3852. }
  3853. /**
  3854. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3855. *
  3856. * This function calculates amount of memory required for all hbq entries
  3857. * to be configured and returns the total memory required.
  3858. **/
  3859. int
  3860. lpfc_sli_hbq_size(void)
  3861. {
  3862. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3863. }
  3864. /**
  3865. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3866. * @phba: Pointer to HBA context object.
  3867. *
  3868. * This function is called during the SLI initialization to configure
  3869. * all the HBQs and post buffers to the HBQ. The caller is not
  3870. * required to hold any locks. This function will return zero if successful
  3871. * else it will return negative error code.
  3872. **/
  3873. static int
  3874. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3875. {
  3876. int hbq_count = lpfc_sli_hbq_count();
  3877. LPFC_MBOXQ_t *pmb;
  3878. MAILBOX_t *pmbox;
  3879. uint32_t hbqno;
  3880. uint32_t hbq_entry_index;
  3881. /* Get a Mailbox buffer to setup mailbox
  3882. * commands for HBA initialization
  3883. */
  3884. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3885. if (!pmb)
  3886. return -ENOMEM;
  3887. pmbox = &pmb->u.mb;
  3888. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3889. phba->link_state = LPFC_INIT_MBX_CMDS;
  3890. phba->hbq_in_use = 1;
  3891. hbq_entry_index = 0;
  3892. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3893. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3894. phba->hbqs[hbqno].hbqPutIdx = 0;
  3895. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3896. phba->hbqs[hbqno].entry_count =
  3897. lpfc_hbq_defs[hbqno]->entry_count;
  3898. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3899. hbq_entry_index, pmb);
  3900. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3901. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3902. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3903. mbxStatus <status>, ring <num> */
  3904. lpfc_printf_log(phba, KERN_ERR,
  3905. LOG_SLI | LOG_VPORT,
  3906. "1805 Adapter failed to init. "
  3907. "Data: x%x x%x x%x\n",
  3908. pmbox->mbxCommand,
  3909. pmbox->mbxStatus, hbqno);
  3910. phba->link_state = LPFC_HBA_ERROR;
  3911. mempool_free(pmb, phba->mbox_mem_pool);
  3912. return -ENXIO;
  3913. }
  3914. }
  3915. phba->hbq_count = hbq_count;
  3916. mempool_free(pmb, phba->mbox_mem_pool);
  3917. /* Initially populate or replenish the HBQs */
  3918. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3919. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3920. return 0;
  3921. }
  3922. /**
  3923. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3924. * @phba: Pointer to HBA context object.
  3925. *
  3926. * This function is called during the SLI initialization to configure
  3927. * all the HBQs and post buffers to the HBQ. The caller is not
  3928. * required to hold any locks. This function will return zero if successful
  3929. * else it will return negative error code.
  3930. **/
  3931. static int
  3932. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3933. {
  3934. phba->hbq_in_use = 1;
  3935. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3936. phba->hbq_count = 1;
  3937. /* Initially populate or replenish the HBQs */
  3938. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3939. return 0;
  3940. }
  3941. /**
  3942. * lpfc_sli_config_port - Issue config port mailbox command
  3943. * @phba: Pointer to HBA context object.
  3944. * @sli_mode: sli mode - 2/3
  3945. *
  3946. * This function is called by the sli intialization code path
  3947. * to issue config_port mailbox command. This function restarts the
  3948. * HBA firmware and issues a config_port mailbox command to configure
  3949. * the SLI interface in the sli mode specified by sli_mode
  3950. * variable. The caller is not required to hold any locks.
  3951. * The function returns 0 if successful, else returns negative error
  3952. * code.
  3953. **/
  3954. int
  3955. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3956. {
  3957. LPFC_MBOXQ_t *pmb;
  3958. uint32_t resetcount = 0, rc = 0, done = 0;
  3959. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3960. if (!pmb) {
  3961. phba->link_state = LPFC_HBA_ERROR;
  3962. return -ENOMEM;
  3963. }
  3964. phba->sli_rev = sli_mode;
  3965. while (resetcount < 2 && !done) {
  3966. spin_lock_irq(&phba->hbalock);
  3967. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3968. spin_unlock_irq(&phba->hbalock);
  3969. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3970. lpfc_sli_brdrestart(phba);
  3971. rc = lpfc_sli_chipset_init(phba);
  3972. if (rc)
  3973. break;
  3974. spin_lock_irq(&phba->hbalock);
  3975. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3976. spin_unlock_irq(&phba->hbalock);
  3977. resetcount++;
  3978. /* Call pre CONFIG_PORT mailbox command initialization. A
  3979. * value of 0 means the call was successful. Any other
  3980. * nonzero value is a failure, but if ERESTART is returned,
  3981. * the driver may reset the HBA and try again.
  3982. */
  3983. rc = lpfc_config_port_prep(phba);
  3984. if (rc == -ERESTART) {
  3985. phba->link_state = LPFC_LINK_UNKNOWN;
  3986. continue;
  3987. } else if (rc)
  3988. break;
  3989. phba->link_state = LPFC_INIT_MBX_CMDS;
  3990. lpfc_config_port(phba, pmb);
  3991. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3992. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3993. LPFC_SLI3_HBQ_ENABLED |
  3994. LPFC_SLI3_CRP_ENABLED |
  3995. LPFC_SLI3_BG_ENABLED |
  3996. LPFC_SLI3_DSS_ENABLED);
  3997. if (rc != MBX_SUCCESS) {
  3998. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3999. "0442 Adapter failed to init, mbxCmd x%x "
  4000. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  4001. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  4002. spin_lock_irq(&phba->hbalock);
  4003. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  4004. spin_unlock_irq(&phba->hbalock);
  4005. rc = -ENXIO;
  4006. } else {
  4007. /* Allow asynchronous mailbox command to go through */
  4008. spin_lock_irq(&phba->hbalock);
  4009. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  4010. spin_unlock_irq(&phba->hbalock);
  4011. done = 1;
  4012. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  4013. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  4014. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  4015. "3110 Port did not grant ASABT\n");
  4016. }
  4017. }
  4018. if (!done) {
  4019. rc = -EINVAL;
  4020. goto do_prep_failed;
  4021. }
  4022. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  4023. if (!pmb->u.mb.un.varCfgPort.cMA) {
  4024. rc = -ENXIO;
  4025. goto do_prep_failed;
  4026. }
  4027. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  4028. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  4029. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  4030. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  4031. phba->max_vpi : phba->max_vports;
  4032. } else
  4033. phba->max_vpi = 0;
  4034. phba->fips_level = 0;
  4035. phba->fips_spec_rev = 0;
  4036. if (pmb->u.mb.un.varCfgPort.gdss) {
  4037. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  4038. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  4039. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  4040. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4041. "2850 Security Crypto Active. FIPS x%d "
  4042. "(Spec Rev: x%d)",
  4043. phba->fips_level, phba->fips_spec_rev);
  4044. }
  4045. if (pmb->u.mb.un.varCfgPort.sec_err) {
  4046. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4047. "2856 Config Port Security Crypto "
  4048. "Error: x%x ",
  4049. pmb->u.mb.un.varCfgPort.sec_err);
  4050. }
  4051. if (pmb->u.mb.un.varCfgPort.gerbm)
  4052. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  4053. if (pmb->u.mb.un.varCfgPort.gcrp)
  4054. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  4055. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  4056. phba->port_gp = phba->mbox->us.s3_pgp.port;
  4057. if (phba->cfg_enable_bg) {
  4058. if (pmb->u.mb.un.varCfgPort.gbg)
  4059. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  4060. else
  4061. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4062. "0443 Adapter did not grant "
  4063. "BlockGuard\n");
  4064. }
  4065. } else {
  4066. phba->hbq_get = NULL;
  4067. phba->port_gp = phba->mbox->us.s2.port;
  4068. phba->max_vpi = 0;
  4069. }
  4070. do_prep_failed:
  4071. mempool_free(pmb, phba->mbox_mem_pool);
  4072. return rc;
  4073. }
  4074. /**
  4075. * lpfc_sli_hba_setup - SLI intialization function
  4076. * @phba: Pointer to HBA context object.
  4077. *
  4078. * This function is the main SLI intialization function. This function
  4079. * is called by the HBA intialization code, HBA reset code and HBA
  4080. * error attention handler code. Caller is not required to hold any
  4081. * locks. This function issues config_port mailbox command to configure
  4082. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4083. * calls the config_port_post function to issue init_link mailbox
  4084. * command and to start the discovery. The function will return zero
  4085. * if successful, else it will return negative error code.
  4086. **/
  4087. int
  4088. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4089. {
  4090. uint32_t rc;
  4091. int mode = 3, i;
  4092. int longs;
  4093. switch (lpfc_sli_mode) {
  4094. case 2:
  4095. if (phba->cfg_enable_npiv) {
  4096. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4097. "1824 NPIV enabled: Override lpfc_sli_mode "
  4098. "parameter (%d) to auto (0).\n",
  4099. lpfc_sli_mode);
  4100. break;
  4101. }
  4102. mode = 2;
  4103. break;
  4104. case 0:
  4105. case 3:
  4106. break;
  4107. default:
  4108. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4109. "1819 Unrecognized lpfc_sli_mode "
  4110. "parameter: %d.\n", lpfc_sli_mode);
  4111. break;
  4112. }
  4113. rc = lpfc_sli_config_port(phba, mode);
  4114. if (rc && lpfc_sli_mode == 3)
  4115. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4116. "1820 Unable to select SLI-3. "
  4117. "Not supported by adapter.\n");
  4118. if (rc && mode != 2)
  4119. rc = lpfc_sli_config_port(phba, 2);
  4120. if (rc)
  4121. goto lpfc_sli_hba_setup_error;
  4122. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4123. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4124. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4125. if (!rc) {
  4126. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4127. "2709 This device supports "
  4128. "Advanced Error Reporting (AER)\n");
  4129. spin_lock_irq(&phba->hbalock);
  4130. phba->hba_flag |= HBA_AER_ENABLED;
  4131. spin_unlock_irq(&phba->hbalock);
  4132. } else {
  4133. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4134. "2708 This device does not support "
  4135. "Advanced Error Reporting (AER)\n");
  4136. phba->cfg_aer_support = 0;
  4137. }
  4138. }
  4139. if (phba->sli_rev == 3) {
  4140. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4141. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4142. } else {
  4143. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4144. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4145. phba->sli3_options = 0;
  4146. }
  4147. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4148. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4149. phba->sli_rev, phba->max_vpi);
  4150. rc = lpfc_sli_ring_map(phba);
  4151. if (rc)
  4152. goto lpfc_sli_hba_setup_error;
  4153. /* Initialize VPIs. */
  4154. if (phba->sli_rev == LPFC_SLI_REV3) {
  4155. /*
  4156. * The VPI bitmask and physical ID array are allocated
  4157. * and initialized once only - at driver load. A port
  4158. * reset doesn't need to reinitialize this memory.
  4159. */
  4160. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4161. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4162. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4163. GFP_KERNEL);
  4164. if (!phba->vpi_bmask) {
  4165. rc = -ENOMEM;
  4166. goto lpfc_sli_hba_setup_error;
  4167. }
  4168. phba->vpi_ids = kzalloc(
  4169. (phba->max_vpi+1) * sizeof(uint16_t),
  4170. GFP_KERNEL);
  4171. if (!phba->vpi_ids) {
  4172. kfree(phba->vpi_bmask);
  4173. rc = -ENOMEM;
  4174. goto lpfc_sli_hba_setup_error;
  4175. }
  4176. for (i = 0; i < phba->max_vpi; i++)
  4177. phba->vpi_ids[i] = i;
  4178. }
  4179. }
  4180. /* Init HBQs */
  4181. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4182. rc = lpfc_sli_hbq_setup(phba);
  4183. if (rc)
  4184. goto lpfc_sli_hba_setup_error;
  4185. }
  4186. spin_lock_irq(&phba->hbalock);
  4187. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4188. spin_unlock_irq(&phba->hbalock);
  4189. rc = lpfc_config_port_post(phba);
  4190. if (rc)
  4191. goto lpfc_sli_hba_setup_error;
  4192. return rc;
  4193. lpfc_sli_hba_setup_error:
  4194. phba->link_state = LPFC_HBA_ERROR;
  4195. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4196. "0445 Firmware initialization failed\n");
  4197. return rc;
  4198. }
  4199. /**
  4200. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4201. * @phba: Pointer to HBA context object.
  4202. * @mboxq: mailbox pointer.
  4203. * This function issue a dump mailbox command to read config region
  4204. * 23 and parse the records in the region and populate driver
  4205. * data structure.
  4206. **/
  4207. static int
  4208. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4209. {
  4210. LPFC_MBOXQ_t *mboxq;
  4211. struct lpfc_dmabuf *mp;
  4212. struct lpfc_mqe *mqe;
  4213. uint32_t data_length;
  4214. int rc;
  4215. /* Program the default value of vlan_id and fc_map */
  4216. phba->valid_vlan = 0;
  4217. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4218. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4219. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4220. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4221. if (!mboxq)
  4222. return -ENOMEM;
  4223. mqe = &mboxq->u.mqe;
  4224. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4225. rc = -ENOMEM;
  4226. goto out_free_mboxq;
  4227. }
  4228. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4229. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4230. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4231. "(%d):2571 Mailbox cmd x%x Status x%x "
  4232. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4233. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4234. "CQ: x%x x%x x%x x%x\n",
  4235. mboxq->vport ? mboxq->vport->vpi : 0,
  4236. bf_get(lpfc_mqe_command, mqe),
  4237. bf_get(lpfc_mqe_status, mqe),
  4238. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4239. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4240. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4241. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4242. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4243. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4244. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4245. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4246. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4247. mboxq->mcqe.word0,
  4248. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4249. mboxq->mcqe.trailer);
  4250. if (rc) {
  4251. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4252. kfree(mp);
  4253. rc = -EIO;
  4254. goto out_free_mboxq;
  4255. }
  4256. data_length = mqe->un.mb_words[5];
  4257. if (data_length > DMP_RGN23_SIZE) {
  4258. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4259. kfree(mp);
  4260. rc = -EIO;
  4261. goto out_free_mboxq;
  4262. }
  4263. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4264. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4265. kfree(mp);
  4266. rc = 0;
  4267. out_free_mboxq:
  4268. mempool_free(mboxq, phba->mbox_mem_pool);
  4269. return rc;
  4270. }
  4271. /**
  4272. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4273. * @phba: pointer to lpfc hba data structure.
  4274. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4275. * @vpd: pointer to the memory to hold resulting port vpd data.
  4276. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4277. * On output, the number of data bytes in @vpd.
  4278. *
  4279. * This routine executes a READ_REV SLI4 mailbox command. In
  4280. * addition, this routine gets the port vpd data.
  4281. *
  4282. * Return codes
  4283. * 0 - successful
  4284. * -ENOMEM - could not allocated memory.
  4285. **/
  4286. static int
  4287. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4288. uint8_t *vpd, uint32_t *vpd_size)
  4289. {
  4290. int rc = 0;
  4291. uint32_t dma_size;
  4292. struct lpfc_dmabuf *dmabuf;
  4293. struct lpfc_mqe *mqe;
  4294. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4295. if (!dmabuf)
  4296. return -ENOMEM;
  4297. /*
  4298. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4299. * mailbox command.
  4300. */
  4301. dma_size = *vpd_size;
  4302. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4303. dma_size,
  4304. &dmabuf->phys,
  4305. GFP_KERNEL);
  4306. if (!dmabuf->virt) {
  4307. kfree(dmabuf);
  4308. return -ENOMEM;
  4309. }
  4310. memset(dmabuf->virt, 0, dma_size);
  4311. /*
  4312. * The SLI4 implementation of READ_REV conflicts at word1,
  4313. * bits 31:16 and SLI4 adds vpd functionality not present
  4314. * in SLI3. This code corrects the conflicts.
  4315. */
  4316. lpfc_read_rev(phba, mboxq);
  4317. mqe = &mboxq->u.mqe;
  4318. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4319. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4320. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4321. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4322. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4323. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4324. if (rc) {
  4325. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4326. dmabuf->virt, dmabuf->phys);
  4327. kfree(dmabuf);
  4328. return -EIO;
  4329. }
  4330. /*
  4331. * The available vpd length cannot be bigger than the
  4332. * DMA buffer passed to the port. Catch the less than
  4333. * case and update the caller's size.
  4334. */
  4335. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4336. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4337. memcpy(vpd, dmabuf->virt, *vpd_size);
  4338. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4339. dmabuf->virt, dmabuf->phys);
  4340. kfree(dmabuf);
  4341. return 0;
  4342. }
  4343. /**
  4344. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4345. * @phba: pointer to lpfc hba data structure.
  4346. *
  4347. * This routine retrieves SLI4 device physical port name this PCI function
  4348. * is attached to.
  4349. *
  4350. * Return codes
  4351. * 0 - successful
  4352. * otherwise - failed to retrieve physical port name
  4353. **/
  4354. static int
  4355. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4356. {
  4357. LPFC_MBOXQ_t *mboxq;
  4358. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4359. struct lpfc_controller_attribute *cntl_attr;
  4360. struct lpfc_mbx_get_port_name *get_port_name;
  4361. void *virtaddr = NULL;
  4362. uint32_t alloclen, reqlen;
  4363. uint32_t shdr_status, shdr_add_status;
  4364. union lpfc_sli4_cfg_shdr *shdr;
  4365. char cport_name = 0;
  4366. int rc;
  4367. /* We assume nothing at this point */
  4368. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4369. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4370. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4371. if (!mboxq)
  4372. return -ENOMEM;
  4373. /* obtain link type and link number via READ_CONFIG */
  4374. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4375. lpfc_sli4_read_config(phba);
  4376. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4377. goto retrieve_ppname;
  4378. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4379. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4380. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4381. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4382. LPFC_SLI4_MBX_NEMBED);
  4383. if (alloclen < reqlen) {
  4384. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4385. "3084 Allocated DMA memory size (%d) is "
  4386. "less than the requested DMA memory size "
  4387. "(%d)\n", alloclen, reqlen);
  4388. rc = -ENOMEM;
  4389. goto out_free_mboxq;
  4390. }
  4391. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4392. virtaddr = mboxq->sge_array->addr[0];
  4393. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4394. shdr = &mbx_cntl_attr->cfg_shdr;
  4395. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4396. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4397. if (shdr_status || shdr_add_status || rc) {
  4398. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4399. "3085 Mailbox x%x (x%x/x%x) failed, "
  4400. "rc:x%x, status:x%x, add_status:x%x\n",
  4401. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4402. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4403. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4404. rc, shdr_status, shdr_add_status);
  4405. rc = -ENXIO;
  4406. goto out_free_mboxq;
  4407. }
  4408. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4409. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4410. phba->sli4_hba.lnk_info.lnk_tp =
  4411. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4412. phba->sli4_hba.lnk_info.lnk_no =
  4413. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4414. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4415. "3086 lnk_type:%d, lnk_numb:%d\n",
  4416. phba->sli4_hba.lnk_info.lnk_tp,
  4417. phba->sli4_hba.lnk_info.lnk_no);
  4418. retrieve_ppname:
  4419. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4420. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4421. sizeof(struct lpfc_mbx_get_port_name) -
  4422. sizeof(struct lpfc_sli4_cfg_mhdr),
  4423. LPFC_SLI4_MBX_EMBED);
  4424. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4425. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4426. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4427. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4428. phba->sli4_hba.lnk_info.lnk_tp);
  4429. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4430. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4431. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4432. if (shdr_status || shdr_add_status || rc) {
  4433. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4434. "3087 Mailbox x%x (x%x/x%x) failed: "
  4435. "rc:x%x, status:x%x, add_status:x%x\n",
  4436. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4437. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4438. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4439. rc, shdr_status, shdr_add_status);
  4440. rc = -ENXIO;
  4441. goto out_free_mboxq;
  4442. }
  4443. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4444. case LPFC_LINK_NUMBER_0:
  4445. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4446. &get_port_name->u.response);
  4447. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4448. break;
  4449. case LPFC_LINK_NUMBER_1:
  4450. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4451. &get_port_name->u.response);
  4452. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4453. break;
  4454. case LPFC_LINK_NUMBER_2:
  4455. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4456. &get_port_name->u.response);
  4457. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4458. break;
  4459. case LPFC_LINK_NUMBER_3:
  4460. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4461. &get_port_name->u.response);
  4462. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4463. break;
  4464. default:
  4465. break;
  4466. }
  4467. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4468. phba->Port[0] = cport_name;
  4469. phba->Port[1] = '\0';
  4470. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4471. "3091 SLI get port name: %s\n", phba->Port);
  4472. }
  4473. out_free_mboxq:
  4474. if (rc != MBX_TIMEOUT) {
  4475. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4476. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4477. else
  4478. mempool_free(mboxq, phba->mbox_mem_pool);
  4479. }
  4480. return rc;
  4481. }
  4482. /**
  4483. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4484. * @phba: pointer to lpfc hba data structure.
  4485. *
  4486. * This routine is called to explicitly arm the SLI4 device's completion and
  4487. * event queues
  4488. **/
  4489. static void
  4490. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4491. {
  4492. int fcp_eqidx;
  4493. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4494. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4495. fcp_eqidx = 0;
  4496. if (phba->sli4_hba.fcp_cq) {
  4497. do {
  4498. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4499. LPFC_QUEUE_REARM);
  4500. } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
  4501. }
  4502. if (phba->sli4_hba.hba_eq) {
  4503. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
  4504. fcp_eqidx++)
  4505. lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
  4506. LPFC_QUEUE_REARM);
  4507. }
  4508. }
  4509. /**
  4510. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4511. * @phba: Pointer to HBA context object.
  4512. * @type: The resource extent type.
  4513. * @extnt_count: buffer to hold port available extent count.
  4514. * @extnt_size: buffer to hold element count per extent.
  4515. *
  4516. * This function calls the port and retrievs the number of available
  4517. * extents and their size for a particular extent type.
  4518. *
  4519. * Returns: 0 if successful. Nonzero otherwise.
  4520. **/
  4521. int
  4522. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4523. uint16_t *extnt_count, uint16_t *extnt_size)
  4524. {
  4525. int rc = 0;
  4526. uint32_t length;
  4527. uint32_t mbox_tmo;
  4528. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4529. LPFC_MBOXQ_t *mbox;
  4530. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4531. if (!mbox)
  4532. return -ENOMEM;
  4533. /* Find out how many extents are available for this resource type */
  4534. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4535. sizeof(struct lpfc_sli4_cfg_mhdr));
  4536. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4537. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4538. length, LPFC_SLI4_MBX_EMBED);
  4539. /* Send an extents count of 0 - the GET doesn't use it. */
  4540. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4541. LPFC_SLI4_MBX_EMBED);
  4542. if (unlikely(rc)) {
  4543. rc = -EIO;
  4544. goto err_exit;
  4545. }
  4546. if (!phba->sli4_hba.intr_enable)
  4547. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4548. else {
  4549. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4550. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4551. }
  4552. if (unlikely(rc)) {
  4553. rc = -EIO;
  4554. goto err_exit;
  4555. }
  4556. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4557. if (bf_get(lpfc_mbox_hdr_status,
  4558. &rsrc_info->header.cfg_shdr.response)) {
  4559. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4560. "2930 Failed to get resource extents "
  4561. "Status 0x%x Add'l Status 0x%x\n",
  4562. bf_get(lpfc_mbox_hdr_status,
  4563. &rsrc_info->header.cfg_shdr.response),
  4564. bf_get(lpfc_mbox_hdr_add_status,
  4565. &rsrc_info->header.cfg_shdr.response));
  4566. rc = -EIO;
  4567. goto err_exit;
  4568. }
  4569. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4570. &rsrc_info->u.rsp);
  4571. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4572. &rsrc_info->u.rsp);
  4573. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4574. "3162 Retrieved extents type-%d from port: count:%d, "
  4575. "size:%d\n", type, *extnt_count, *extnt_size);
  4576. err_exit:
  4577. mempool_free(mbox, phba->mbox_mem_pool);
  4578. return rc;
  4579. }
  4580. /**
  4581. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4582. * @phba: Pointer to HBA context object.
  4583. * @type: The extent type to check.
  4584. *
  4585. * This function reads the current available extents from the port and checks
  4586. * if the extent count or extent size has changed since the last access.
  4587. * Callers use this routine post port reset to understand if there is a
  4588. * extent reprovisioning requirement.
  4589. *
  4590. * Returns:
  4591. * -Error: error indicates problem.
  4592. * 1: Extent count or size has changed.
  4593. * 0: No changes.
  4594. **/
  4595. static int
  4596. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4597. {
  4598. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4599. uint16_t size_diff, rsrc_ext_size;
  4600. int rc = 0;
  4601. struct lpfc_rsrc_blks *rsrc_entry;
  4602. struct list_head *rsrc_blk_list = NULL;
  4603. size_diff = 0;
  4604. curr_ext_cnt = 0;
  4605. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4606. &rsrc_ext_cnt,
  4607. &rsrc_ext_size);
  4608. if (unlikely(rc))
  4609. return -EIO;
  4610. switch (type) {
  4611. case LPFC_RSC_TYPE_FCOE_RPI:
  4612. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4613. break;
  4614. case LPFC_RSC_TYPE_FCOE_VPI:
  4615. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4616. break;
  4617. case LPFC_RSC_TYPE_FCOE_XRI:
  4618. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4619. break;
  4620. case LPFC_RSC_TYPE_FCOE_VFI:
  4621. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4622. break;
  4623. default:
  4624. break;
  4625. }
  4626. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4627. curr_ext_cnt++;
  4628. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4629. size_diff++;
  4630. }
  4631. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4632. rc = 1;
  4633. return rc;
  4634. }
  4635. /**
  4636. * lpfc_sli4_cfg_post_extnts -
  4637. * @phba: Pointer to HBA context object.
  4638. * @extnt_cnt - number of available extents.
  4639. * @type - the extent type (rpi, xri, vfi, vpi).
  4640. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4641. * @mbox - pointer to the caller's allocated mailbox structure.
  4642. *
  4643. * This function executes the extents allocation request. It also
  4644. * takes care of the amount of memory needed to allocate or get the
  4645. * allocated extents. It is the caller's responsibility to evaluate
  4646. * the response.
  4647. *
  4648. * Returns:
  4649. * -Error: Error value describes the condition found.
  4650. * 0: if successful
  4651. **/
  4652. static int
  4653. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4654. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4655. {
  4656. int rc = 0;
  4657. uint32_t req_len;
  4658. uint32_t emb_len;
  4659. uint32_t alloc_len, mbox_tmo;
  4660. /* Calculate the total requested length of the dma memory */
  4661. req_len = extnt_cnt * sizeof(uint16_t);
  4662. /*
  4663. * Calculate the size of an embedded mailbox. The uint32_t
  4664. * accounts for extents-specific word.
  4665. */
  4666. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4667. sizeof(uint32_t);
  4668. /*
  4669. * Presume the allocation and response will fit into an embedded
  4670. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4671. */
  4672. *emb = LPFC_SLI4_MBX_EMBED;
  4673. if (req_len > emb_len) {
  4674. req_len = extnt_cnt * sizeof(uint16_t) +
  4675. sizeof(union lpfc_sli4_cfg_shdr) +
  4676. sizeof(uint32_t);
  4677. *emb = LPFC_SLI4_MBX_NEMBED;
  4678. }
  4679. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4680. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4681. req_len, *emb);
  4682. if (alloc_len < req_len) {
  4683. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4684. "2982 Allocated DMA memory size (x%x) is "
  4685. "less than the requested DMA memory "
  4686. "size (x%x)\n", alloc_len, req_len);
  4687. return -ENOMEM;
  4688. }
  4689. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4690. if (unlikely(rc))
  4691. return -EIO;
  4692. if (!phba->sli4_hba.intr_enable)
  4693. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4694. else {
  4695. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4696. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4697. }
  4698. if (unlikely(rc))
  4699. rc = -EIO;
  4700. return rc;
  4701. }
  4702. /**
  4703. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4704. * @phba: Pointer to HBA context object.
  4705. * @type: The resource extent type to allocate.
  4706. *
  4707. * This function allocates the number of elements for the specified
  4708. * resource type.
  4709. **/
  4710. static int
  4711. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4712. {
  4713. bool emb = false;
  4714. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4715. uint16_t rsrc_id, rsrc_start, j, k;
  4716. uint16_t *ids;
  4717. int i, rc;
  4718. unsigned long longs;
  4719. unsigned long *bmask;
  4720. struct lpfc_rsrc_blks *rsrc_blks;
  4721. LPFC_MBOXQ_t *mbox;
  4722. uint32_t length;
  4723. struct lpfc_id_range *id_array = NULL;
  4724. void *virtaddr = NULL;
  4725. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4726. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4727. struct list_head *ext_blk_list;
  4728. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4729. &rsrc_cnt,
  4730. &rsrc_size);
  4731. if (unlikely(rc))
  4732. return -EIO;
  4733. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4734. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4735. "3009 No available Resource Extents "
  4736. "for resource type 0x%x: Count: 0x%x, "
  4737. "Size 0x%x\n", type, rsrc_cnt,
  4738. rsrc_size);
  4739. return -ENOMEM;
  4740. }
  4741. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4742. "2903 Post resource extents type-0x%x: "
  4743. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4744. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4745. if (!mbox)
  4746. return -ENOMEM;
  4747. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4748. if (unlikely(rc)) {
  4749. rc = -EIO;
  4750. goto err_exit;
  4751. }
  4752. /*
  4753. * Figure out where the response is located. Then get local pointers
  4754. * to the response data. The port does not guarantee to respond to
  4755. * all extents counts request so update the local variable with the
  4756. * allocated count from the port.
  4757. */
  4758. if (emb == LPFC_SLI4_MBX_EMBED) {
  4759. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4760. id_array = &rsrc_ext->u.rsp.id[0];
  4761. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4762. } else {
  4763. virtaddr = mbox->sge_array->addr[0];
  4764. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4765. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4766. id_array = &n_rsrc->id;
  4767. }
  4768. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4769. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4770. /*
  4771. * Based on the resource size and count, correct the base and max
  4772. * resource values.
  4773. */
  4774. length = sizeof(struct lpfc_rsrc_blks);
  4775. switch (type) {
  4776. case LPFC_RSC_TYPE_FCOE_RPI:
  4777. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4778. sizeof(unsigned long),
  4779. GFP_KERNEL);
  4780. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4781. rc = -ENOMEM;
  4782. goto err_exit;
  4783. }
  4784. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4785. sizeof(uint16_t),
  4786. GFP_KERNEL);
  4787. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4788. kfree(phba->sli4_hba.rpi_bmask);
  4789. rc = -ENOMEM;
  4790. goto err_exit;
  4791. }
  4792. /*
  4793. * The next_rpi was initialized with the maximum available
  4794. * count but the port may allocate a smaller number. Catch
  4795. * that case and update the next_rpi.
  4796. */
  4797. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4798. /* Initialize local ptrs for common extent processing later. */
  4799. bmask = phba->sli4_hba.rpi_bmask;
  4800. ids = phba->sli4_hba.rpi_ids;
  4801. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4802. break;
  4803. case LPFC_RSC_TYPE_FCOE_VPI:
  4804. phba->vpi_bmask = kzalloc(longs *
  4805. sizeof(unsigned long),
  4806. GFP_KERNEL);
  4807. if (unlikely(!phba->vpi_bmask)) {
  4808. rc = -ENOMEM;
  4809. goto err_exit;
  4810. }
  4811. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4812. sizeof(uint16_t),
  4813. GFP_KERNEL);
  4814. if (unlikely(!phba->vpi_ids)) {
  4815. kfree(phba->vpi_bmask);
  4816. rc = -ENOMEM;
  4817. goto err_exit;
  4818. }
  4819. /* Initialize local ptrs for common extent processing later. */
  4820. bmask = phba->vpi_bmask;
  4821. ids = phba->vpi_ids;
  4822. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4823. break;
  4824. case LPFC_RSC_TYPE_FCOE_XRI:
  4825. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4826. sizeof(unsigned long),
  4827. GFP_KERNEL);
  4828. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4829. rc = -ENOMEM;
  4830. goto err_exit;
  4831. }
  4832. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4833. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4834. sizeof(uint16_t),
  4835. GFP_KERNEL);
  4836. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4837. kfree(phba->sli4_hba.xri_bmask);
  4838. rc = -ENOMEM;
  4839. goto err_exit;
  4840. }
  4841. /* Initialize local ptrs for common extent processing later. */
  4842. bmask = phba->sli4_hba.xri_bmask;
  4843. ids = phba->sli4_hba.xri_ids;
  4844. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4845. break;
  4846. case LPFC_RSC_TYPE_FCOE_VFI:
  4847. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4848. sizeof(unsigned long),
  4849. GFP_KERNEL);
  4850. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4851. rc = -ENOMEM;
  4852. goto err_exit;
  4853. }
  4854. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4855. sizeof(uint16_t),
  4856. GFP_KERNEL);
  4857. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4858. kfree(phba->sli4_hba.vfi_bmask);
  4859. rc = -ENOMEM;
  4860. goto err_exit;
  4861. }
  4862. /* Initialize local ptrs for common extent processing later. */
  4863. bmask = phba->sli4_hba.vfi_bmask;
  4864. ids = phba->sli4_hba.vfi_ids;
  4865. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4866. break;
  4867. default:
  4868. /* Unsupported Opcode. Fail call. */
  4869. id_array = NULL;
  4870. bmask = NULL;
  4871. ids = NULL;
  4872. ext_blk_list = NULL;
  4873. goto err_exit;
  4874. }
  4875. /*
  4876. * Complete initializing the extent configuration with the
  4877. * allocated ids assigned to this function. The bitmask serves
  4878. * as an index into the array and manages the available ids. The
  4879. * array just stores the ids communicated to the port via the wqes.
  4880. */
  4881. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4882. if ((i % 2) == 0)
  4883. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4884. &id_array[k]);
  4885. else
  4886. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4887. &id_array[k]);
  4888. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4889. if (unlikely(!rsrc_blks)) {
  4890. rc = -ENOMEM;
  4891. kfree(bmask);
  4892. kfree(ids);
  4893. goto err_exit;
  4894. }
  4895. rsrc_blks->rsrc_start = rsrc_id;
  4896. rsrc_blks->rsrc_size = rsrc_size;
  4897. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4898. rsrc_start = rsrc_id;
  4899. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4900. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4901. lpfc_sli4_get_els_iocb_cnt(phba);
  4902. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4903. ids[j] = rsrc_id;
  4904. rsrc_id++;
  4905. j++;
  4906. }
  4907. /* Entire word processed. Get next word.*/
  4908. if ((i % 2) == 1)
  4909. k++;
  4910. }
  4911. err_exit:
  4912. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4913. return rc;
  4914. }
  4915. /**
  4916. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4917. * @phba: Pointer to HBA context object.
  4918. * @type: the extent's type.
  4919. *
  4920. * This function deallocates all extents of a particular resource type.
  4921. * SLI4 does not allow for deallocating a particular extent range. It
  4922. * is the caller's responsibility to release all kernel memory resources.
  4923. **/
  4924. static int
  4925. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4926. {
  4927. int rc;
  4928. uint32_t length, mbox_tmo = 0;
  4929. LPFC_MBOXQ_t *mbox;
  4930. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4931. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4932. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4933. if (!mbox)
  4934. return -ENOMEM;
  4935. /*
  4936. * This function sends an embedded mailbox because it only sends the
  4937. * the resource type. All extents of this type are released by the
  4938. * port.
  4939. */
  4940. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4941. sizeof(struct lpfc_sli4_cfg_mhdr));
  4942. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4943. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4944. length, LPFC_SLI4_MBX_EMBED);
  4945. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4946. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4947. LPFC_SLI4_MBX_EMBED);
  4948. if (unlikely(rc)) {
  4949. rc = -EIO;
  4950. goto out_free_mbox;
  4951. }
  4952. if (!phba->sli4_hba.intr_enable)
  4953. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4954. else {
  4955. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4956. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4957. }
  4958. if (unlikely(rc)) {
  4959. rc = -EIO;
  4960. goto out_free_mbox;
  4961. }
  4962. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4963. if (bf_get(lpfc_mbox_hdr_status,
  4964. &dealloc_rsrc->header.cfg_shdr.response)) {
  4965. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4966. "2919 Failed to release resource extents "
  4967. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4968. "Resource memory not released.\n",
  4969. type,
  4970. bf_get(lpfc_mbox_hdr_status,
  4971. &dealloc_rsrc->header.cfg_shdr.response),
  4972. bf_get(lpfc_mbox_hdr_add_status,
  4973. &dealloc_rsrc->header.cfg_shdr.response));
  4974. rc = -EIO;
  4975. goto out_free_mbox;
  4976. }
  4977. /* Release kernel memory resources for the specific type. */
  4978. switch (type) {
  4979. case LPFC_RSC_TYPE_FCOE_VPI:
  4980. kfree(phba->vpi_bmask);
  4981. kfree(phba->vpi_ids);
  4982. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4983. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4984. &phba->lpfc_vpi_blk_list, list) {
  4985. list_del_init(&rsrc_blk->list);
  4986. kfree(rsrc_blk);
  4987. }
  4988. break;
  4989. case LPFC_RSC_TYPE_FCOE_XRI:
  4990. kfree(phba->sli4_hba.xri_bmask);
  4991. kfree(phba->sli4_hba.xri_ids);
  4992. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4993. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4994. list_del_init(&rsrc_blk->list);
  4995. kfree(rsrc_blk);
  4996. }
  4997. break;
  4998. case LPFC_RSC_TYPE_FCOE_VFI:
  4999. kfree(phba->sli4_hba.vfi_bmask);
  5000. kfree(phba->sli4_hba.vfi_ids);
  5001. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5002. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5003. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  5004. list_del_init(&rsrc_blk->list);
  5005. kfree(rsrc_blk);
  5006. }
  5007. break;
  5008. case LPFC_RSC_TYPE_FCOE_RPI:
  5009. /* RPI bitmask and physical id array are cleaned up earlier. */
  5010. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  5011. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  5012. list_del_init(&rsrc_blk->list);
  5013. kfree(rsrc_blk);
  5014. }
  5015. break;
  5016. default:
  5017. break;
  5018. }
  5019. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5020. out_free_mbox:
  5021. mempool_free(mbox, phba->mbox_mem_pool);
  5022. return rc;
  5023. }
  5024. /**
  5025. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  5026. * @phba: Pointer to HBA context object.
  5027. *
  5028. * This function allocates all SLI4 resource identifiers.
  5029. **/
  5030. int
  5031. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  5032. {
  5033. int i, rc, error = 0;
  5034. uint16_t count, base;
  5035. unsigned long longs;
  5036. if (!phba->sli4_hba.rpi_hdrs_in_use)
  5037. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  5038. if (phba->sli4_hba.extents_in_use) {
  5039. /*
  5040. * The port supports resource extents. The XRI, VPI, VFI, RPI
  5041. * resource extent count must be read and allocated before
  5042. * provisioning the resource id arrays.
  5043. */
  5044. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5045. LPFC_IDX_RSRC_RDY) {
  5046. /*
  5047. * Extent-based resources are set - the driver could
  5048. * be in a port reset. Figure out if any corrective
  5049. * actions need to be taken.
  5050. */
  5051. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5052. LPFC_RSC_TYPE_FCOE_VFI);
  5053. if (rc != 0)
  5054. error++;
  5055. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5056. LPFC_RSC_TYPE_FCOE_VPI);
  5057. if (rc != 0)
  5058. error++;
  5059. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5060. LPFC_RSC_TYPE_FCOE_XRI);
  5061. if (rc != 0)
  5062. error++;
  5063. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  5064. LPFC_RSC_TYPE_FCOE_RPI);
  5065. if (rc != 0)
  5066. error++;
  5067. /*
  5068. * It's possible that the number of resources
  5069. * provided to this port instance changed between
  5070. * resets. Detect this condition and reallocate
  5071. * resources. Otherwise, there is no action.
  5072. */
  5073. if (error) {
  5074. lpfc_printf_log(phba, KERN_INFO,
  5075. LOG_MBOX | LOG_INIT,
  5076. "2931 Detected extent resource "
  5077. "change. Reallocating all "
  5078. "extents.\n");
  5079. rc = lpfc_sli4_dealloc_extent(phba,
  5080. LPFC_RSC_TYPE_FCOE_VFI);
  5081. rc = lpfc_sli4_dealloc_extent(phba,
  5082. LPFC_RSC_TYPE_FCOE_VPI);
  5083. rc = lpfc_sli4_dealloc_extent(phba,
  5084. LPFC_RSC_TYPE_FCOE_XRI);
  5085. rc = lpfc_sli4_dealloc_extent(phba,
  5086. LPFC_RSC_TYPE_FCOE_RPI);
  5087. } else
  5088. return 0;
  5089. }
  5090. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5091. if (unlikely(rc))
  5092. goto err_exit;
  5093. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5094. if (unlikely(rc))
  5095. goto err_exit;
  5096. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5097. if (unlikely(rc))
  5098. goto err_exit;
  5099. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5100. if (unlikely(rc))
  5101. goto err_exit;
  5102. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5103. LPFC_IDX_RSRC_RDY);
  5104. return rc;
  5105. } else {
  5106. /*
  5107. * The port does not support resource extents. The XRI, VPI,
  5108. * VFI, RPI resource ids were determined from READ_CONFIG.
  5109. * Just allocate the bitmasks and provision the resource id
  5110. * arrays. If a port reset is active, the resources don't
  5111. * need any action - just exit.
  5112. */
  5113. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5114. LPFC_IDX_RSRC_RDY) {
  5115. lpfc_sli4_dealloc_resource_identifiers(phba);
  5116. lpfc_sli4_remove_rpis(phba);
  5117. }
  5118. /* RPIs. */
  5119. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5120. if (count <= 0) {
  5121. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5122. "3279 Invalid provisioning of "
  5123. "rpi:%d\n", count);
  5124. rc = -EINVAL;
  5125. goto err_exit;
  5126. }
  5127. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5128. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5129. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5130. sizeof(unsigned long),
  5131. GFP_KERNEL);
  5132. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5133. rc = -ENOMEM;
  5134. goto err_exit;
  5135. }
  5136. phba->sli4_hba.rpi_ids = kzalloc(count *
  5137. sizeof(uint16_t),
  5138. GFP_KERNEL);
  5139. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5140. rc = -ENOMEM;
  5141. goto free_rpi_bmask;
  5142. }
  5143. for (i = 0; i < count; i++)
  5144. phba->sli4_hba.rpi_ids[i] = base + i;
  5145. /* VPIs. */
  5146. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5147. if (count <= 0) {
  5148. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5149. "3280 Invalid provisioning of "
  5150. "vpi:%d\n", count);
  5151. rc = -EINVAL;
  5152. goto free_rpi_ids;
  5153. }
  5154. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5155. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5156. phba->vpi_bmask = kzalloc(longs *
  5157. sizeof(unsigned long),
  5158. GFP_KERNEL);
  5159. if (unlikely(!phba->vpi_bmask)) {
  5160. rc = -ENOMEM;
  5161. goto free_rpi_ids;
  5162. }
  5163. phba->vpi_ids = kzalloc(count *
  5164. sizeof(uint16_t),
  5165. GFP_KERNEL);
  5166. if (unlikely(!phba->vpi_ids)) {
  5167. rc = -ENOMEM;
  5168. goto free_vpi_bmask;
  5169. }
  5170. for (i = 0; i < count; i++)
  5171. phba->vpi_ids[i] = base + i;
  5172. /* XRIs. */
  5173. count = phba->sli4_hba.max_cfg_param.max_xri;
  5174. if (count <= 0) {
  5175. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5176. "3281 Invalid provisioning of "
  5177. "xri:%d\n", count);
  5178. rc = -EINVAL;
  5179. goto free_vpi_ids;
  5180. }
  5181. base = phba->sli4_hba.max_cfg_param.xri_base;
  5182. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5183. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5184. sizeof(unsigned long),
  5185. GFP_KERNEL);
  5186. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5187. rc = -ENOMEM;
  5188. goto free_vpi_ids;
  5189. }
  5190. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5191. phba->sli4_hba.xri_ids = kzalloc(count *
  5192. sizeof(uint16_t),
  5193. GFP_KERNEL);
  5194. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5195. rc = -ENOMEM;
  5196. goto free_xri_bmask;
  5197. }
  5198. for (i = 0; i < count; i++)
  5199. phba->sli4_hba.xri_ids[i] = base + i;
  5200. /* VFIs. */
  5201. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5202. if (count <= 0) {
  5203. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5204. "3282 Invalid provisioning of "
  5205. "vfi:%d\n", count);
  5206. rc = -EINVAL;
  5207. goto free_xri_ids;
  5208. }
  5209. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5210. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5211. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5212. sizeof(unsigned long),
  5213. GFP_KERNEL);
  5214. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5215. rc = -ENOMEM;
  5216. goto free_xri_ids;
  5217. }
  5218. phba->sli4_hba.vfi_ids = kzalloc(count *
  5219. sizeof(uint16_t),
  5220. GFP_KERNEL);
  5221. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5222. rc = -ENOMEM;
  5223. goto free_vfi_bmask;
  5224. }
  5225. for (i = 0; i < count; i++)
  5226. phba->sli4_hba.vfi_ids[i] = base + i;
  5227. /*
  5228. * Mark all resources ready. An HBA reset doesn't need
  5229. * to reset the initialization.
  5230. */
  5231. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5232. LPFC_IDX_RSRC_RDY);
  5233. return 0;
  5234. }
  5235. free_vfi_bmask:
  5236. kfree(phba->sli4_hba.vfi_bmask);
  5237. free_xri_ids:
  5238. kfree(phba->sli4_hba.xri_ids);
  5239. free_xri_bmask:
  5240. kfree(phba->sli4_hba.xri_bmask);
  5241. free_vpi_ids:
  5242. kfree(phba->vpi_ids);
  5243. free_vpi_bmask:
  5244. kfree(phba->vpi_bmask);
  5245. free_rpi_ids:
  5246. kfree(phba->sli4_hba.rpi_ids);
  5247. free_rpi_bmask:
  5248. kfree(phba->sli4_hba.rpi_bmask);
  5249. err_exit:
  5250. return rc;
  5251. }
  5252. /**
  5253. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5254. * @phba: Pointer to HBA context object.
  5255. *
  5256. * This function allocates the number of elements for the specified
  5257. * resource type.
  5258. **/
  5259. int
  5260. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5261. {
  5262. if (phba->sli4_hba.extents_in_use) {
  5263. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5264. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5265. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5266. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5267. } else {
  5268. kfree(phba->vpi_bmask);
  5269. kfree(phba->vpi_ids);
  5270. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5271. kfree(phba->sli4_hba.xri_bmask);
  5272. kfree(phba->sli4_hba.xri_ids);
  5273. kfree(phba->sli4_hba.vfi_bmask);
  5274. kfree(phba->sli4_hba.vfi_ids);
  5275. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5276. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5277. }
  5278. return 0;
  5279. }
  5280. /**
  5281. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5282. * @phba: Pointer to HBA context object.
  5283. * @type: The resource extent type.
  5284. * @extnt_count: buffer to hold port extent count response
  5285. * @extnt_size: buffer to hold port extent size response.
  5286. *
  5287. * This function calls the port to read the host allocated extents
  5288. * for a particular type.
  5289. **/
  5290. int
  5291. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5292. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5293. {
  5294. bool emb;
  5295. int rc = 0;
  5296. uint16_t curr_blks = 0;
  5297. uint32_t req_len, emb_len;
  5298. uint32_t alloc_len, mbox_tmo;
  5299. struct list_head *blk_list_head;
  5300. struct lpfc_rsrc_blks *rsrc_blk;
  5301. LPFC_MBOXQ_t *mbox;
  5302. void *virtaddr = NULL;
  5303. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5304. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5305. union lpfc_sli4_cfg_shdr *shdr;
  5306. switch (type) {
  5307. case LPFC_RSC_TYPE_FCOE_VPI:
  5308. blk_list_head = &phba->lpfc_vpi_blk_list;
  5309. break;
  5310. case LPFC_RSC_TYPE_FCOE_XRI:
  5311. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5312. break;
  5313. case LPFC_RSC_TYPE_FCOE_VFI:
  5314. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5315. break;
  5316. case LPFC_RSC_TYPE_FCOE_RPI:
  5317. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5318. break;
  5319. default:
  5320. return -EIO;
  5321. }
  5322. /* Count the number of extents currently allocatd for this type. */
  5323. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5324. if (curr_blks == 0) {
  5325. /*
  5326. * The GET_ALLOCATED mailbox does not return the size,
  5327. * just the count. The size should be just the size
  5328. * stored in the current allocated block and all sizes
  5329. * for an extent type are the same so set the return
  5330. * value now.
  5331. */
  5332. *extnt_size = rsrc_blk->rsrc_size;
  5333. }
  5334. curr_blks++;
  5335. }
  5336. /* Calculate the total requested length of the dma memory. */
  5337. req_len = curr_blks * sizeof(uint16_t);
  5338. /*
  5339. * Calculate the size of an embedded mailbox. The uint32_t
  5340. * accounts for extents-specific word.
  5341. */
  5342. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5343. sizeof(uint32_t);
  5344. /*
  5345. * Presume the allocation and response will fit into an embedded
  5346. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5347. */
  5348. emb = LPFC_SLI4_MBX_EMBED;
  5349. req_len = emb_len;
  5350. if (req_len > emb_len) {
  5351. req_len = curr_blks * sizeof(uint16_t) +
  5352. sizeof(union lpfc_sli4_cfg_shdr) +
  5353. sizeof(uint32_t);
  5354. emb = LPFC_SLI4_MBX_NEMBED;
  5355. }
  5356. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5357. if (!mbox)
  5358. return -ENOMEM;
  5359. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5360. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5361. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5362. req_len, emb);
  5363. if (alloc_len < req_len) {
  5364. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5365. "2983 Allocated DMA memory size (x%x) is "
  5366. "less than the requested DMA memory "
  5367. "size (x%x)\n", alloc_len, req_len);
  5368. rc = -ENOMEM;
  5369. goto err_exit;
  5370. }
  5371. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5372. if (unlikely(rc)) {
  5373. rc = -EIO;
  5374. goto err_exit;
  5375. }
  5376. if (!phba->sli4_hba.intr_enable)
  5377. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5378. else {
  5379. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5380. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5381. }
  5382. if (unlikely(rc)) {
  5383. rc = -EIO;
  5384. goto err_exit;
  5385. }
  5386. /*
  5387. * Figure out where the response is located. Then get local pointers
  5388. * to the response data. The port does not guarantee to respond to
  5389. * all extents counts request so update the local variable with the
  5390. * allocated count from the port.
  5391. */
  5392. if (emb == LPFC_SLI4_MBX_EMBED) {
  5393. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5394. shdr = &rsrc_ext->header.cfg_shdr;
  5395. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5396. } else {
  5397. virtaddr = mbox->sge_array->addr[0];
  5398. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5399. shdr = &n_rsrc->cfg_shdr;
  5400. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5401. }
  5402. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5403. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5404. "2984 Failed to read allocated resources "
  5405. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5406. type,
  5407. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5408. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5409. rc = -EIO;
  5410. goto err_exit;
  5411. }
  5412. err_exit:
  5413. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5414. return rc;
  5415. }
  5416. /**
  5417. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5418. * @phba: pointer to lpfc hba data structure.
  5419. *
  5420. * This routine walks the list of els buffers that have been allocated and
  5421. * repost them to the port by using SGL block post. This is needed after a
  5422. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5423. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5424. * SGL block post mailbox commands to post them to the port. For single els
  5425. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5426. * mailbox command for posting.
  5427. *
  5428. * Returns: 0 = success, non-zero failure.
  5429. **/
  5430. static int
  5431. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5432. {
  5433. struct lpfc_sglq *sglq_entry = NULL;
  5434. struct lpfc_sglq *sglq_entry_next = NULL;
  5435. struct lpfc_sglq *sglq_entry_first = NULL;
  5436. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5437. int last_xritag = NO_XRI;
  5438. LIST_HEAD(prep_sgl_list);
  5439. LIST_HEAD(blck_sgl_list);
  5440. LIST_HEAD(allc_sgl_list);
  5441. LIST_HEAD(post_sgl_list);
  5442. LIST_HEAD(free_sgl_list);
  5443. spin_lock(&phba->hbalock);
  5444. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5445. spin_unlock(&phba->hbalock);
  5446. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5447. &allc_sgl_list, list) {
  5448. list_del_init(&sglq_entry->list);
  5449. block_cnt++;
  5450. if ((last_xritag != NO_XRI) &&
  5451. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5452. /* a hole in xri block, form a sgl posting block */
  5453. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5454. post_cnt = block_cnt - 1;
  5455. /* prepare list for next posting block */
  5456. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5457. block_cnt = 1;
  5458. } else {
  5459. /* prepare list for next posting block */
  5460. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5461. /* enough sgls for non-embed sgl mbox command */
  5462. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5463. list_splice_init(&prep_sgl_list,
  5464. &blck_sgl_list);
  5465. post_cnt = block_cnt;
  5466. block_cnt = 0;
  5467. }
  5468. }
  5469. num_posted++;
  5470. /* keep track of last sgl's xritag */
  5471. last_xritag = sglq_entry->sli4_xritag;
  5472. /* end of repost sgl list condition for els buffers */
  5473. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5474. if (post_cnt == 0) {
  5475. list_splice_init(&prep_sgl_list,
  5476. &blck_sgl_list);
  5477. post_cnt = block_cnt;
  5478. } else if (block_cnt == 1) {
  5479. status = lpfc_sli4_post_sgl(phba,
  5480. sglq_entry->phys, 0,
  5481. sglq_entry->sli4_xritag);
  5482. if (!status) {
  5483. /* successful, put sgl to posted list */
  5484. list_add_tail(&sglq_entry->list,
  5485. &post_sgl_list);
  5486. } else {
  5487. /* Failure, put sgl to free list */
  5488. lpfc_printf_log(phba, KERN_WARNING,
  5489. LOG_SLI,
  5490. "3159 Failed to post els "
  5491. "sgl, xritag:x%x\n",
  5492. sglq_entry->sli4_xritag);
  5493. list_add_tail(&sglq_entry->list,
  5494. &free_sgl_list);
  5495. spin_lock_irq(&phba->hbalock);
  5496. phba->sli4_hba.els_xri_cnt--;
  5497. spin_unlock_irq(&phba->hbalock);
  5498. }
  5499. }
  5500. }
  5501. /* continue until a nembed page worth of sgls */
  5502. if (post_cnt == 0)
  5503. continue;
  5504. /* post the els buffer list sgls as a block */
  5505. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5506. post_cnt);
  5507. if (!status) {
  5508. /* success, put sgl list to posted sgl list */
  5509. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5510. } else {
  5511. /* Failure, put sgl list to free sgl list */
  5512. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5513. struct lpfc_sglq,
  5514. list);
  5515. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5516. "3160 Failed to post els sgl-list, "
  5517. "xritag:x%x-x%x\n",
  5518. sglq_entry_first->sli4_xritag,
  5519. (sglq_entry_first->sli4_xritag +
  5520. post_cnt - 1));
  5521. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5522. spin_lock_irq(&phba->hbalock);
  5523. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5524. spin_unlock_irq(&phba->hbalock);
  5525. }
  5526. /* don't reset xirtag due to hole in xri block */
  5527. if (block_cnt == 0)
  5528. last_xritag = NO_XRI;
  5529. /* reset els sgl post count for next round of posting */
  5530. post_cnt = 0;
  5531. }
  5532. /* free the els sgls failed to post */
  5533. lpfc_free_sgl_list(phba, &free_sgl_list);
  5534. /* push els sgls posted to the availble list */
  5535. if (!list_empty(&post_sgl_list)) {
  5536. spin_lock(&phba->hbalock);
  5537. list_splice_init(&post_sgl_list,
  5538. &phba->sli4_hba.lpfc_sgl_list);
  5539. spin_unlock(&phba->hbalock);
  5540. } else {
  5541. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5542. "3161 Failure to post els sgl to port.\n");
  5543. return -EIO;
  5544. }
  5545. return 0;
  5546. }
  5547. /**
  5548. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5549. * @phba: Pointer to HBA context object.
  5550. *
  5551. * This function is the main SLI4 device intialization PCI function. This
  5552. * function is called by the HBA intialization code, HBA reset code and
  5553. * HBA error attention handler code. Caller is not required to hold any
  5554. * locks.
  5555. **/
  5556. int
  5557. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5558. {
  5559. int rc;
  5560. LPFC_MBOXQ_t *mboxq;
  5561. struct lpfc_mqe *mqe;
  5562. uint8_t *vpd;
  5563. uint32_t vpd_size;
  5564. uint32_t ftr_rsp = 0;
  5565. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5566. struct lpfc_vport *vport = phba->pport;
  5567. struct lpfc_dmabuf *mp;
  5568. /* Perform a PCI function reset to start from clean */
  5569. rc = lpfc_pci_function_reset(phba);
  5570. if (unlikely(rc))
  5571. return -ENODEV;
  5572. /* Check the HBA Host Status Register for readyness */
  5573. rc = lpfc_sli4_post_status_check(phba);
  5574. if (unlikely(rc))
  5575. return -ENODEV;
  5576. else {
  5577. spin_lock_irq(&phba->hbalock);
  5578. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5579. spin_unlock_irq(&phba->hbalock);
  5580. }
  5581. /*
  5582. * Allocate a single mailbox container for initializing the
  5583. * port.
  5584. */
  5585. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5586. if (!mboxq)
  5587. return -ENOMEM;
  5588. /* Issue READ_REV to collect vpd and FW information. */
  5589. vpd_size = SLI4_PAGE_SIZE;
  5590. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5591. if (!vpd) {
  5592. rc = -ENOMEM;
  5593. goto out_free_mbox;
  5594. }
  5595. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5596. if (unlikely(rc)) {
  5597. kfree(vpd);
  5598. goto out_free_mbox;
  5599. }
  5600. mqe = &mboxq->u.mqe;
  5601. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5602. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5603. phba->hba_flag |= HBA_FCOE_MODE;
  5604. else
  5605. phba->hba_flag &= ~HBA_FCOE_MODE;
  5606. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5607. LPFC_DCBX_CEE_MODE)
  5608. phba->hba_flag |= HBA_FIP_SUPPORT;
  5609. else
  5610. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5611. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5612. if (phba->sli_rev != LPFC_SLI_REV4) {
  5613. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5614. "0376 READ_REV Error. SLI Level %d "
  5615. "FCoE enabled %d\n",
  5616. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5617. rc = -EIO;
  5618. kfree(vpd);
  5619. goto out_free_mbox;
  5620. }
  5621. /*
  5622. * Continue initialization with default values even if driver failed
  5623. * to read FCoE param config regions, only read parameters if the
  5624. * board is FCoE
  5625. */
  5626. if (phba->hba_flag & HBA_FCOE_MODE &&
  5627. lpfc_sli4_read_fcoe_params(phba))
  5628. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5629. "2570 Failed to read FCoE parameters\n");
  5630. /*
  5631. * Retrieve sli4 device physical port name, failure of doing it
  5632. * is considered as non-fatal.
  5633. */
  5634. rc = lpfc_sli4_retrieve_pport_name(phba);
  5635. if (!rc)
  5636. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5637. "3080 Successful retrieving SLI4 device "
  5638. "physical port name: %s.\n", phba->Port);
  5639. /*
  5640. * Evaluate the read rev and vpd data. Populate the driver
  5641. * state with the results. If this routine fails, the failure
  5642. * is not fatal as the driver will use generic values.
  5643. */
  5644. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5645. if (unlikely(!rc)) {
  5646. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5647. "0377 Error %d parsing vpd. "
  5648. "Using defaults.\n", rc);
  5649. rc = 0;
  5650. }
  5651. kfree(vpd);
  5652. /* Save information as VPD data */
  5653. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5654. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5655. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5656. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5657. &mqe->un.read_rev);
  5658. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5659. &mqe->un.read_rev);
  5660. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5661. &mqe->un.read_rev);
  5662. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5663. &mqe->un.read_rev);
  5664. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5665. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5666. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5667. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5668. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5669. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5670. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5671. "(%d):0380 READ_REV Status x%x "
  5672. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5673. mboxq->vport ? mboxq->vport->vpi : 0,
  5674. bf_get(lpfc_mqe_status, mqe),
  5675. phba->vpd.rev.opFwName,
  5676. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5677. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5678. /*
  5679. * Discover the port's supported feature set and match it against the
  5680. * hosts requests.
  5681. */
  5682. lpfc_request_features(phba, mboxq);
  5683. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5684. if (unlikely(rc)) {
  5685. rc = -EIO;
  5686. goto out_free_mbox;
  5687. }
  5688. /*
  5689. * The port must support FCP initiator mode as this is the
  5690. * only mode running in the host.
  5691. */
  5692. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5693. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5694. "0378 No support for fcpi mode.\n");
  5695. ftr_rsp++;
  5696. }
  5697. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5698. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5699. else
  5700. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5701. /*
  5702. * If the port cannot support the host's requested features
  5703. * then turn off the global config parameters to disable the
  5704. * feature in the driver. This is not a fatal error.
  5705. */
  5706. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5707. if (phba->cfg_enable_bg) {
  5708. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5709. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5710. else
  5711. ftr_rsp++;
  5712. }
  5713. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5714. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5715. ftr_rsp++;
  5716. if (ftr_rsp) {
  5717. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5718. "0379 Feature Mismatch Data: x%08x %08x "
  5719. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5720. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5721. phba->cfg_enable_npiv, phba->max_vpi);
  5722. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5723. phba->cfg_enable_bg = 0;
  5724. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5725. phba->cfg_enable_npiv = 0;
  5726. }
  5727. /* These SLI3 features are assumed in SLI4 */
  5728. spin_lock_irq(&phba->hbalock);
  5729. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5730. spin_unlock_irq(&phba->hbalock);
  5731. /*
  5732. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5733. * calls depends on these resources to complete port setup.
  5734. */
  5735. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5736. if (rc) {
  5737. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5738. "2920 Failed to alloc Resource IDs "
  5739. "rc = x%x\n", rc);
  5740. goto out_free_mbox;
  5741. }
  5742. /* Read the port's service parameters. */
  5743. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5744. if (rc) {
  5745. phba->link_state = LPFC_HBA_ERROR;
  5746. rc = -ENOMEM;
  5747. goto out_free_mbox;
  5748. }
  5749. mboxq->vport = vport;
  5750. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5751. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5752. if (rc == MBX_SUCCESS) {
  5753. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5754. rc = 0;
  5755. }
  5756. /*
  5757. * This memory was allocated by the lpfc_read_sparam routine. Release
  5758. * it to the mbuf pool.
  5759. */
  5760. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5761. kfree(mp);
  5762. mboxq->context1 = NULL;
  5763. if (unlikely(rc)) {
  5764. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5765. "0382 READ_SPARAM command failed "
  5766. "status %d, mbxStatus x%x\n",
  5767. rc, bf_get(lpfc_mqe_status, mqe));
  5768. phba->link_state = LPFC_HBA_ERROR;
  5769. rc = -EIO;
  5770. goto out_free_mbox;
  5771. }
  5772. lpfc_update_vport_wwn(vport);
  5773. /* Update the fc_host data structures with new wwn. */
  5774. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5775. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5776. /* update host els and scsi xri-sgl sizes and mappings */
  5777. rc = lpfc_sli4_xri_sgl_update(phba);
  5778. if (unlikely(rc)) {
  5779. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5780. "1400 Failed to update xri-sgl size and "
  5781. "mapping: %d\n", rc);
  5782. goto out_free_mbox;
  5783. }
  5784. /* register the els sgl pool to the port */
  5785. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5786. if (unlikely(rc)) {
  5787. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5788. "0582 Error %d during els sgl post "
  5789. "operation\n", rc);
  5790. rc = -ENODEV;
  5791. goto out_free_mbox;
  5792. }
  5793. /* register the allocated scsi sgl pool to the port */
  5794. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5795. if (unlikely(rc)) {
  5796. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5797. "0383 Error %d during scsi sgl post "
  5798. "operation\n", rc);
  5799. /* Some Scsi buffers were moved to the abort scsi list */
  5800. /* A pci function reset will repost them */
  5801. rc = -ENODEV;
  5802. goto out_free_mbox;
  5803. }
  5804. /* Post the rpi header region to the device. */
  5805. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5806. if (unlikely(rc)) {
  5807. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5808. "0393 Error %d during rpi post operation\n",
  5809. rc);
  5810. rc = -ENODEV;
  5811. goto out_free_mbox;
  5812. }
  5813. lpfc_sli4_node_prep(phba);
  5814. /* Create all the SLI4 queues */
  5815. rc = lpfc_sli4_queue_create(phba);
  5816. if (rc) {
  5817. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5818. "3089 Failed to allocate queues\n");
  5819. rc = -ENODEV;
  5820. goto out_stop_timers;
  5821. }
  5822. /* Set up all the queues to the device */
  5823. rc = lpfc_sli4_queue_setup(phba);
  5824. if (unlikely(rc)) {
  5825. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5826. "0381 Error %d during queue setup.\n ", rc);
  5827. goto out_destroy_queue;
  5828. }
  5829. /* Arm the CQs and then EQs on device */
  5830. lpfc_sli4_arm_cqeq_intr(phba);
  5831. /* Indicate device interrupt mode */
  5832. phba->sli4_hba.intr_enable = 1;
  5833. /* Allow asynchronous mailbox command to go through */
  5834. spin_lock_irq(&phba->hbalock);
  5835. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5836. spin_unlock_irq(&phba->hbalock);
  5837. /* Post receive buffers to the device */
  5838. lpfc_sli4_rb_setup(phba);
  5839. /* Reset HBA FCF states after HBA reset */
  5840. phba->fcf.fcf_flag = 0;
  5841. phba->fcf.current_rec.flag = 0;
  5842. /* Start the ELS watchdog timer */
  5843. mod_timer(&vport->els_tmofunc,
  5844. jiffies + HZ * (phba->fc_ratov * 2));
  5845. /* Start heart beat timer */
  5846. mod_timer(&phba->hb_tmofunc,
  5847. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5848. phba->hb_outstanding = 0;
  5849. phba->last_completion_time = jiffies;
  5850. /* Start error attention (ERATT) polling timer */
  5851. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5852. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5853. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5854. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5855. if (!rc) {
  5856. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5857. "2829 This device supports "
  5858. "Advanced Error Reporting (AER)\n");
  5859. spin_lock_irq(&phba->hbalock);
  5860. phba->hba_flag |= HBA_AER_ENABLED;
  5861. spin_unlock_irq(&phba->hbalock);
  5862. } else {
  5863. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5864. "2830 This device does not support "
  5865. "Advanced Error Reporting (AER)\n");
  5866. phba->cfg_aer_support = 0;
  5867. }
  5868. rc = 0;
  5869. }
  5870. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5871. /*
  5872. * The FC Port needs to register FCFI (index 0)
  5873. */
  5874. lpfc_reg_fcfi(phba, mboxq);
  5875. mboxq->vport = phba->pport;
  5876. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5877. if (rc != MBX_SUCCESS)
  5878. goto out_unset_queue;
  5879. rc = 0;
  5880. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5881. &mboxq->u.mqe.un.reg_fcfi);
  5882. /* Check if the port is configured to be disabled */
  5883. lpfc_sli_read_link_ste(phba);
  5884. }
  5885. /*
  5886. * The port is ready, set the host's link state to LINK_DOWN
  5887. * in preparation for link interrupts.
  5888. */
  5889. spin_lock_irq(&phba->hbalock);
  5890. phba->link_state = LPFC_LINK_DOWN;
  5891. spin_unlock_irq(&phba->hbalock);
  5892. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5893. (phba->hba_flag & LINK_DISABLED)) {
  5894. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5895. "3103 Adapter Link is disabled.\n");
  5896. lpfc_down_link(phba, mboxq);
  5897. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5898. if (rc != MBX_SUCCESS) {
  5899. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5900. "3104 Adapter failed to issue "
  5901. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5902. goto out_unset_queue;
  5903. }
  5904. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5905. /* don't perform init_link on SLI4 FC port loopback test */
  5906. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5907. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5908. if (rc)
  5909. goto out_unset_queue;
  5910. }
  5911. }
  5912. mempool_free(mboxq, phba->mbox_mem_pool);
  5913. return rc;
  5914. out_unset_queue:
  5915. /* Unset all the queues set up in this routine when error out */
  5916. lpfc_sli4_queue_unset(phba);
  5917. out_destroy_queue:
  5918. lpfc_sli4_queue_destroy(phba);
  5919. out_stop_timers:
  5920. lpfc_stop_hba_timers(phba);
  5921. out_free_mbox:
  5922. mempool_free(mboxq, phba->mbox_mem_pool);
  5923. return rc;
  5924. }
  5925. /**
  5926. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5927. * @ptr: context object - pointer to hba structure.
  5928. *
  5929. * This is the callback function for mailbox timer. The mailbox
  5930. * timer is armed when a new mailbox command is issued and the timer
  5931. * is deleted when the mailbox complete. The function is called by
  5932. * the kernel timer code when a mailbox does not complete within
  5933. * expected time. This function wakes up the worker thread to
  5934. * process the mailbox timeout and returns. All the processing is
  5935. * done by the worker thread function lpfc_mbox_timeout_handler.
  5936. **/
  5937. void
  5938. lpfc_mbox_timeout(unsigned long ptr)
  5939. {
  5940. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5941. unsigned long iflag;
  5942. uint32_t tmo_posted;
  5943. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5944. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5945. if (!tmo_posted)
  5946. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5947. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5948. if (!tmo_posted)
  5949. lpfc_worker_wake_up(phba);
  5950. return;
  5951. }
  5952. /**
  5953. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5954. * @phba: Pointer to HBA context object.
  5955. *
  5956. * This function is called from worker thread when a mailbox command times out.
  5957. * The caller is not required to hold any locks. This function will reset the
  5958. * HBA and recover all the pending commands.
  5959. **/
  5960. void
  5961. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5962. {
  5963. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5964. MAILBOX_t *mb = &pmbox->u.mb;
  5965. struct lpfc_sli *psli = &phba->sli;
  5966. struct lpfc_sli_ring *pring;
  5967. /* Check the pmbox pointer first. There is a race condition
  5968. * between the mbox timeout handler getting executed in the
  5969. * worklist and the mailbox actually completing. When this
  5970. * race condition occurs, the mbox_active will be NULL.
  5971. */
  5972. spin_lock_irq(&phba->hbalock);
  5973. if (pmbox == NULL) {
  5974. lpfc_printf_log(phba, KERN_WARNING,
  5975. LOG_MBOX | LOG_SLI,
  5976. "0353 Active Mailbox cleared - mailbox timeout "
  5977. "exiting\n");
  5978. spin_unlock_irq(&phba->hbalock);
  5979. return;
  5980. }
  5981. /* Mbox cmd <mbxCommand> timeout */
  5982. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5983. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5984. mb->mbxCommand,
  5985. phba->pport->port_state,
  5986. phba->sli.sli_flag,
  5987. phba->sli.mbox_active);
  5988. spin_unlock_irq(&phba->hbalock);
  5989. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5990. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5991. * it to fail all outstanding SCSI IO.
  5992. */
  5993. spin_lock_irq(&phba->pport->work_port_lock);
  5994. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5995. spin_unlock_irq(&phba->pport->work_port_lock);
  5996. spin_lock_irq(&phba->hbalock);
  5997. phba->link_state = LPFC_LINK_UNKNOWN;
  5998. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5999. spin_unlock_irq(&phba->hbalock);
  6000. pring = &psli->ring[psli->fcp_ring];
  6001. lpfc_sli_abort_iocb_ring(phba, pring);
  6002. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6003. "0345 Resetting board due to mailbox timeout\n");
  6004. /* Reset the HBA device */
  6005. lpfc_reset_hba(phba);
  6006. }
  6007. /**
  6008. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  6009. * @phba: Pointer to HBA context object.
  6010. * @pmbox: Pointer to mailbox object.
  6011. * @flag: Flag indicating how the mailbox need to be processed.
  6012. *
  6013. * This function is called by discovery code and HBA management code
  6014. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  6015. * function gets the hbalock to protect the data structures.
  6016. * The mailbox command can be submitted in polling mode, in which case
  6017. * this function will wait in a polling loop for the completion of the
  6018. * mailbox.
  6019. * If the mailbox is submitted in no_wait mode (not polling) the
  6020. * function will submit the command and returns immediately without waiting
  6021. * for the mailbox completion. The no_wait is supported only when HBA
  6022. * is in SLI2/SLI3 mode - interrupts are enabled.
  6023. * The SLI interface allows only one mailbox pending at a time. If the
  6024. * mailbox is issued in polling mode and there is already a mailbox
  6025. * pending, then the function will return an error. If the mailbox is issued
  6026. * in NO_WAIT mode and there is a mailbox pending already, the function
  6027. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  6028. * The sli layer owns the mailbox object until the completion of mailbox
  6029. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  6030. * return codes the caller owns the mailbox command after the return of
  6031. * the function.
  6032. **/
  6033. static int
  6034. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  6035. uint32_t flag)
  6036. {
  6037. MAILBOX_t *mbx;
  6038. struct lpfc_sli *psli = &phba->sli;
  6039. uint32_t status, evtctr;
  6040. uint32_t ha_copy, hc_copy;
  6041. int i;
  6042. unsigned long timeout;
  6043. unsigned long drvr_flag = 0;
  6044. uint32_t word0, ldata;
  6045. void __iomem *to_slim;
  6046. int processing_queue = 0;
  6047. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6048. if (!pmbox) {
  6049. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6050. /* processing mbox queue from intr_handler */
  6051. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6052. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6053. return MBX_SUCCESS;
  6054. }
  6055. processing_queue = 1;
  6056. pmbox = lpfc_mbox_get(phba);
  6057. if (!pmbox) {
  6058. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6059. return MBX_SUCCESS;
  6060. }
  6061. }
  6062. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  6063. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  6064. if(!pmbox->vport) {
  6065. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6066. lpfc_printf_log(phba, KERN_ERR,
  6067. LOG_MBOX | LOG_VPORT,
  6068. "1806 Mbox x%x failed. No vport\n",
  6069. pmbox->u.mb.mbxCommand);
  6070. dump_stack();
  6071. goto out_not_finished;
  6072. }
  6073. }
  6074. /* If the PCI channel is in offline state, do not post mbox. */
  6075. if (unlikely(pci_channel_offline(phba->pcidev))) {
  6076. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6077. goto out_not_finished;
  6078. }
  6079. /* If HBA has a deferred error attention, fail the iocb. */
  6080. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  6081. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6082. goto out_not_finished;
  6083. }
  6084. psli = &phba->sli;
  6085. mbx = &pmbox->u.mb;
  6086. status = MBX_SUCCESS;
  6087. if (phba->link_state == LPFC_HBA_ERROR) {
  6088. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6089. /* Mbox command <mbxCommand> cannot issue */
  6090. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6091. "(%d):0311 Mailbox command x%x cannot "
  6092. "issue Data: x%x x%x\n",
  6093. pmbox->vport ? pmbox->vport->vpi : 0,
  6094. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6095. goto out_not_finished;
  6096. }
  6097. if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6098. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6099. !(hc_copy & HC_MBINT_ENA)) {
  6100. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6101. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6102. "(%d):2528 Mailbox command x%x cannot "
  6103. "issue Data: x%x x%x\n",
  6104. pmbox->vport ? pmbox->vport->vpi : 0,
  6105. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6106. goto out_not_finished;
  6107. }
  6108. }
  6109. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6110. /* Polling for a mbox command when another one is already active
  6111. * is not allowed in SLI. Also, the driver must have established
  6112. * SLI2 mode to queue and process multiple mbox commands.
  6113. */
  6114. if (flag & MBX_POLL) {
  6115. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6116. /* Mbox command <mbxCommand> cannot issue */
  6117. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6118. "(%d):2529 Mailbox command x%x "
  6119. "cannot issue Data: x%x x%x\n",
  6120. pmbox->vport ? pmbox->vport->vpi : 0,
  6121. pmbox->u.mb.mbxCommand,
  6122. psli->sli_flag, flag);
  6123. goto out_not_finished;
  6124. }
  6125. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6126. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6127. /* Mbox command <mbxCommand> cannot issue */
  6128. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6129. "(%d):2530 Mailbox command x%x "
  6130. "cannot issue Data: x%x x%x\n",
  6131. pmbox->vport ? pmbox->vport->vpi : 0,
  6132. pmbox->u.mb.mbxCommand,
  6133. psli->sli_flag, flag);
  6134. goto out_not_finished;
  6135. }
  6136. /* Another mailbox command is still being processed, queue this
  6137. * command to be processed later.
  6138. */
  6139. lpfc_mbox_put(phba, pmbox);
  6140. /* Mbox cmd issue - BUSY */
  6141. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6142. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6143. "x%x x%x x%x x%x\n",
  6144. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6145. mbx->mbxCommand, phba->pport->port_state,
  6146. psli->sli_flag, flag);
  6147. psli->slistat.mbox_busy++;
  6148. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6149. if (pmbox->vport) {
  6150. lpfc_debugfs_disc_trc(pmbox->vport,
  6151. LPFC_DISC_TRC_MBOX_VPORT,
  6152. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6153. (uint32_t)mbx->mbxCommand,
  6154. mbx->un.varWords[0], mbx->un.varWords[1]);
  6155. }
  6156. else {
  6157. lpfc_debugfs_disc_trc(phba->pport,
  6158. LPFC_DISC_TRC_MBOX,
  6159. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6160. (uint32_t)mbx->mbxCommand,
  6161. mbx->un.varWords[0], mbx->un.varWords[1]);
  6162. }
  6163. return MBX_BUSY;
  6164. }
  6165. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6166. /* If we are not polling, we MUST be in SLI2 mode */
  6167. if (flag != MBX_POLL) {
  6168. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6169. (mbx->mbxCommand != MBX_KILL_BOARD)) {
  6170. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6171. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6172. /* Mbox command <mbxCommand> cannot issue */
  6173. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6174. "(%d):2531 Mailbox command x%x "
  6175. "cannot issue Data: x%x x%x\n",
  6176. pmbox->vport ? pmbox->vport->vpi : 0,
  6177. pmbox->u.mb.mbxCommand,
  6178. psli->sli_flag, flag);
  6179. goto out_not_finished;
  6180. }
  6181. /* timeout active mbox command */
  6182. mod_timer(&psli->mbox_tmo, (jiffies +
  6183. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6184. }
  6185. /* Mailbox cmd <cmd> issue */
  6186. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6187. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6188. "x%x\n",
  6189. pmbox->vport ? pmbox->vport->vpi : 0,
  6190. mbx->mbxCommand, phba->pport->port_state,
  6191. psli->sli_flag, flag);
  6192. if (mbx->mbxCommand != MBX_HEARTBEAT) {
  6193. if (pmbox->vport) {
  6194. lpfc_debugfs_disc_trc(pmbox->vport,
  6195. LPFC_DISC_TRC_MBOX_VPORT,
  6196. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6197. (uint32_t)mbx->mbxCommand,
  6198. mbx->un.varWords[0], mbx->un.varWords[1]);
  6199. }
  6200. else {
  6201. lpfc_debugfs_disc_trc(phba->pport,
  6202. LPFC_DISC_TRC_MBOX,
  6203. "MBOX Send: cmd:x%x mb:x%x x%x",
  6204. (uint32_t)mbx->mbxCommand,
  6205. mbx->un.varWords[0], mbx->un.varWords[1]);
  6206. }
  6207. }
  6208. psli->slistat.mbox_cmd++;
  6209. evtctr = psli->slistat.mbox_event;
  6210. /* next set own bit for the adapter and copy over command word */
  6211. mbx->mbxOwner = OWN_CHIP;
  6212. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6213. /* Populate mbox extension offset word. */
  6214. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6215. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6216. = (uint8_t *)phba->mbox_ext
  6217. - (uint8_t *)phba->mbox;
  6218. }
  6219. /* Copy the mailbox extension data */
  6220. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6221. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6222. (uint8_t *)phba->mbox_ext,
  6223. pmbox->in_ext_byte_len);
  6224. }
  6225. /* Copy command data to host SLIM area */
  6226. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6227. } else {
  6228. /* Populate mbox extension offset word. */
  6229. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6230. *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
  6231. = MAILBOX_HBA_EXT_OFFSET;
  6232. /* Copy the mailbox extension data */
  6233. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6234. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6235. MAILBOX_HBA_EXT_OFFSET,
  6236. pmbox->context2, pmbox->in_ext_byte_len);
  6237. }
  6238. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6239. /* copy command data into host mbox for cmpl */
  6240. lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
  6241. }
  6242. /* First copy mbox command data to HBA SLIM, skip past first
  6243. word */
  6244. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6245. lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
  6246. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6247. /* Next copy over first word, with mbxOwner set */
  6248. ldata = *((uint32_t *)mbx);
  6249. to_slim = phba->MBslimaddr;
  6250. writel(ldata, to_slim);
  6251. readl(to_slim); /* flush */
  6252. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6253. /* switch over to host mailbox */
  6254. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6255. }
  6256. }
  6257. wmb();
  6258. switch (flag) {
  6259. case MBX_NOWAIT:
  6260. /* Set up reference to mailbox command */
  6261. psli->mbox_active = pmbox;
  6262. /* Interrupt board to do it */
  6263. writel(CA_MBATT, phba->CAregaddr);
  6264. readl(phba->CAregaddr); /* flush */
  6265. /* Don't wait for it to finish, just return */
  6266. break;
  6267. case MBX_POLL:
  6268. /* Set up null reference to mailbox command */
  6269. psli->mbox_active = NULL;
  6270. /* Interrupt board to do it */
  6271. writel(CA_MBATT, phba->CAregaddr);
  6272. readl(phba->CAregaddr); /* flush */
  6273. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6274. /* First read mbox status word */
  6275. word0 = *((uint32_t *)phba->mbox);
  6276. word0 = le32_to_cpu(word0);
  6277. } else {
  6278. /* First read mbox status word */
  6279. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6280. spin_unlock_irqrestore(&phba->hbalock,
  6281. drvr_flag);
  6282. goto out_not_finished;
  6283. }
  6284. }
  6285. /* Read the HBA Host Attention Register */
  6286. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6287. spin_unlock_irqrestore(&phba->hbalock,
  6288. drvr_flag);
  6289. goto out_not_finished;
  6290. }
  6291. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6292. 1000) + jiffies;
  6293. i = 0;
  6294. /* Wait for command to complete */
  6295. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6296. (!(ha_copy & HA_MBATT) &&
  6297. (phba->link_state > LPFC_WARM_START))) {
  6298. if (time_after(jiffies, timeout)) {
  6299. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6300. spin_unlock_irqrestore(&phba->hbalock,
  6301. drvr_flag);
  6302. goto out_not_finished;
  6303. }
  6304. /* Check if we took a mbox interrupt while we were
  6305. polling */
  6306. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6307. && (evtctr != psli->slistat.mbox_event))
  6308. break;
  6309. if (i++ > 10) {
  6310. spin_unlock_irqrestore(&phba->hbalock,
  6311. drvr_flag);
  6312. msleep(1);
  6313. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6314. }
  6315. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6316. /* First copy command data */
  6317. word0 = *((uint32_t *)phba->mbox);
  6318. word0 = le32_to_cpu(word0);
  6319. if (mbx->mbxCommand == MBX_CONFIG_PORT) {
  6320. MAILBOX_t *slimmb;
  6321. uint32_t slimword0;
  6322. /* Check real SLIM for any errors */
  6323. slimword0 = readl(phba->MBslimaddr);
  6324. slimmb = (MAILBOX_t *) & slimword0;
  6325. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6326. && slimmb->mbxStatus) {
  6327. psli->sli_flag &=
  6328. ~LPFC_SLI_ACTIVE;
  6329. word0 = slimword0;
  6330. }
  6331. }
  6332. } else {
  6333. /* First copy command data */
  6334. word0 = readl(phba->MBslimaddr);
  6335. }
  6336. /* Read the HBA Host Attention Register */
  6337. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6338. spin_unlock_irqrestore(&phba->hbalock,
  6339. drvr_flag);
  6340. goto out_not_finished;
  6341. }
  6342. }
  6343. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6344. /* copy results back to user */
  6345. lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
  6346. /* Copy the mailbox extension data */
  6347. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6348. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6349. pmbox->context2,
  6350. pmbox->out_ext_byte_len);
  6351. }
  6352. } else {
  6353. /* First copy command data */
  6354. lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
  6355. MAILBOX_CMD_SIZE);
  6356. /* Copy the mailbox extension data */
  6357. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6358. lpfc_memcpy_from_slim(pmbox->context2,
  6359. phba->MBslimaddr +
  6360. MAILBOX_HBA_EXT_OFFSET,
  6361. pmbox->out_ext_byte_len);
  6362. }
  6363. }
  6364. writel(HA_MBATT, phba->HAregaddr);
  6365. readl(phba->HAregaddr); /* flush */
  6366. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6367. status = mbx->mbxStatus;
  6368. }
  6369. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6370. return status;
  6371. out_not_finished:
  6372. if (processing_queue) {
  6373. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6374. lpfc_mbox_cmpl_put(phba, pmbox);
  6375. }
  6376. return MBX_NOT_FINISHED;
  6377. }
  6378. /**
  6379. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6380. * @phba: Pointer to HBA context object.
  6381. *
  6382. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6383. * the driver internal pending mailbox queue. It will then try to wait out the
  6384. * possible outstanding mailbox command before return.
  6385. *
  6386. * Returns:
  6387. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6388. * the outstanding mailbox command timed out.
  6389. **/
  6390. static int
  6391. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6392. {
  6393. struct lpfc_sli *psli = &phba->sli;
  6394. int rc = 0;
  6395. unsigned long timeout = 0;
  6396. /* Mark the asynchronous mailbox command posting as blocked */
  6397. spin_lock_irq(&phba->hbalock);
  6398. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6399. /* Determine how long we might wait for the active mailbox
  6400. * command to be gracefully completed by firmware.
  6401. */
  6402. if (phba->sli.mbox_active)
  6403. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6404. phba->sli.mbox_active) *
  6405. 1000) + jiffies;
  6406. spin_unlock_irq(&phba->hbalock);
  6407. /* Wait for the outstnading mailbox command to complete */
  6408. while (phba->sli.mbox_active) {
  6409. /* Check active mailbox complete status every 2ms */
  6410. msleep(2);
  6411. if (time_after(jiffies, timeout)) {
  6412. /* Timeout, marked the outstanding cmd not complete */
  6413. rc = 1;
  6414. break;
  6415. }
  6416. }
  6417. /* Can not cleanly block async mailbox command, fails it */
  6418. if (rc) {
  6419. spin_lock_irq(&phba->hbalock);
  6420. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6421. spin_unlock_irq(&phba->hbalock);
  6422. }
  6423. return rc;
  6424. }
  6425. /**
  6426. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6427. * @phba: Pointer to HBA context object.
  6428. *
  6429. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6430. * commands from the driver internal pending mailbox queue. It makes sure
  6431. * that there is no outstanding mailbox command before resuming posting
  6432. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6433. * mailbox command, it will try to wait it out before resuming asynchronous
  6434. * mailbox command posting.
  6435. **/
  6436. static void
  6437. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6438. {
  6439. struct lpfc_sli *psli = &phba->sli;
  6440. spin_lock_irq(&phba->hbalock);
  6441. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6442. /* Asynchronous mailbox posting is not blocked, do nothing */
  6443. spin_unlock_irq(&phba->hbalock);
  6444. return;
  6445. }
  6446. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6447. * successful or timeout, after timing-out the outstanding mailbox
  6448. * command shall always be removed, so just unblock posting async
  6449. * mailbox command and resume
  6450. */
  6451. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6452. spin_unlock_irq(&phba->hbalock);
  6453. /* wake up worker thread to post asynchronlous mailbox command */
  6454. lpfc_worker_wake_up(phba);
  6455. }
  6456. /**
  6457. * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
  6458. * @phba: Pointer to HBA context object.
  6459. * @mboxq: Pointer to mailbox object.
  6460. *
  6461. * The function waits for the bootstrap mailbox register ready bit from
  6462. * port for twice the regular mailbox command timeout value.
  6463. *
  6464. * 0 - no timeout on waiting for bootstrap mailbox register ready.
  6465. * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
  6466. **/
  6467. static int
  6468. lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6469. {
  6470. uint32_t db_ready;
  6471. unsigned long timeout;
  6472. struct lpfc_register bmbx_reg;
  6473. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6474. * 1000) + jiffies;
  6475. do {
  6476. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6477. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6478. if (!db_ready)
  6479. msleep(2);
  6480. if (time_after(jiffies, timeout))
  6481. return MBXERR_ERROR;
  6482. } while (!db_ready);
  6483. return 0;
  6484. }
  6485. /**
  6486. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6487. * @phba: Pointer to HBA context object.
  6488. * @mboxq: Pointer to mailbox object.
  6489. *
  6490. * The function posts a mailbox to the port. The mailbox is expected
  6491. * to be comletely filled in and ready for the port to operate on it.
  6492. * This routine executes a synchronous completion operation on the
  6493. * mailbox by polling for its completion.
  6494. *
  6495. * The caller must not be holding any locks when calling this routine.
  6496. *
  6497. * Returns:
  6498. * MBX_SUCCESS - mailbox posted successfully
  6499. * Any of the MBX error values.
  6500. **/
  6501. static int
  6502. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6503. {
  6504. int rc = MBX_SUCCESS;
  6505. unsigned long iflag;
  6506. uint32_t mcqe_status;
  6507. uint32_t mbx_cmnd;
  6508. struct lpfc_sli *psli = &phba->sli;
  6509. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6510. struct lpfc_bmbx_create *mbox_rgn;
  6511. struct dma_address *dma_address;
  6512. /*
  6513. * Only one mailbox can be active to the bootstrap mailbox region
  6514. * at a time and there is no queueing provided.
  6515. */
  6516. spin_lock_irqsave(&phba->hbalock, iflag);
  6517. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6518. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6519. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6520. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6521. "cannot issue Data: x%x x%x\n",
  6522. mboxq->vport ? mboxq->vport->vpi : 0,
  6523. mboxq->u.mb.mbxCommand,
  6524. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6525. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6526. psli->sli_flag, MBX_POLL);
  6527. return MBXERR_ERROR;
  6528. }
  6529. /* The server grabs the token and owns it until release */
  6530. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6531. phba->sli.mbox_active = mboxq;
  6532. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6533. /* wait for bootstrap mbox register for readyness */
  6534. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6535. if (rc)
  6536. goto exit;
  6537. /*
  6538. * Initialize the bootstrap memory region to avoid stale data areas
  6539. * in the mailbox post. Then copy the caller's mailbox contents to
  6540. * the bmbx mailbox region.
  6541. */
  6542. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6543. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6544. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6545. sizeof(struct lpfc_mqe));
  6546. /* Post the high mailbox dma address to the port and wait for ready. */
  6547. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6548. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6549. /* wait for bootstrap mbox register for hi-address write done */
  6550. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6551. if (rc)
  6552. goto exit;
  6553. /* Post the low mailbox dma address to the port. */
  6554. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6555. /* wait for bootstrap mbox register for low address write done */
  6556. rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
  6557. if (rc)
  6558. goto exit;
  6559. /*
  6560. * Read the CQ to ensure the mailbox has completed.
  6561. * If so, update the mailbox status so that the upper layers
  6562. * can complete the request normally.
  6563. */
  6564. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6565. sizeof(struct lpfc_mqe));
  6566. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6567. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6568. sizeof(struct lpfc_mcqe));
  6569. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6570. /*
  6571. * When the CQE status indicates a failure and the mailbox status
  6572. * indicates success then copy the CQE status into the mailbox status
  6573. * (and prefix it with x4000).
  6574. */
  6575. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6576. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6577. bf_set(lpfc_mqe_status, mb,
  6578. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6579. rc = MBXERR_ERROR;
  6580. } else
  6581. lpfc_sli4_swap_str(phba, mboxq);
  6582. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6583. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6584. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6585. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6586. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6587. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6588. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6589. bf_get(lpfc_mqe_status, mb),
  6590. mb->un.mb_words[0], mb->un.mb_words[1],
  6591. mb->un.mb_words[2], mb->un.mb_words[3],
  6592. mb->un.mb_words[4], mb->un.mb_words[5],
  6593. mb->un.mb_words[6], mb->un.mb_words[7],
  6594. mb->un.mb_words[8], mb->un.mb_words[9],
  6595. mb->un.mb_words[10], mb->un.mb_words[11],
  6596. mb->un.mb_words[12], mboxq->mcqe.word0,
  6597. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6598. mboxq->mcqe.trailer);
  6599. exit:
  6600. /* We are holding the token, no needed for lock when release */
  6601. spin_lock_irqsave(&phba->hbalock, iflag);
  6602. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6603. phba->sli.mbox_active = NULL;
  6604. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6605. return rc;
  6606. }
  6607. /**
  6608. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6609. * @phba: Pointer to HBA context object.
  6610. * @pmbox: Pointer to mailbox object.
  6611. * @flag: Flag indicating how the mailbox need to be processed.
  6612. *
  6613. * This function is called by discovery code and HBA management code to submit
  6614. * a mailbox command to firmware with SLI-4 interface spec.
  6615. *
  6616. * Return codes the caller owns the mailbox command after the return of the
  6617. * function.
  6618. **/
  6619. static int
  6620. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6621. uint32_t flag)
  6622. {
  6623. struct lpfc_sli *psli = &phba->sli;
  6624. unsigned long iflags;
  6625. int rc;
  6626. /* dump from issue mailbox command if setup */
  6627. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6628. rc = lpfc_mbox_dev_check(phba);
  6629. if (unlikely(rc)) {
  6630. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6631. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6632. "cannot issue Data: x%x x%x\n",
  6633. mboxq->vport ? mboxq->vport->vpi : 0,
  6634. mboxq->u.mb.mbxCommand,
  6635. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6636. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6637. psli->sli_flag, flag);
  6638. goto out_not_finished;
  6639. }
  6640. /* Detect polling mode and jump to a handler */
  6641. if (!phba->sli4_hba.intr_enable) {
  6642. if (flag == MBX_POLL)
  6643. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6644. else
  6645. rc = -EIO;
  6646. if (rc != MBX_SUCCESS)
  6647. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6648. "(%d):2541 Mailbox command x%x "
  6649. "(x%x/x%x) failure: "
  6650. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6651. "Data: x%x x%x\n,",
  6652. mboxq->vport ? mboxq->vport->vpi : 0,
  6653. mboxq->u.mb.mbxCommand,
  6654. lpfc_sli_config_mbox_subsys_get(phba,
  6655. mboxq),
  6656. lpfc_sli_config_mbox_opcode_get(phba,
  6657. mboxq),
  6658. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6659. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6660. bf_get(lpfc_mcqe_ext_status,
  6661. &mboxq->mcqe),
  6662. psli->sli_flag, flag);
  6663. return rc;
  6664. } else if (flag == MBX_POLL) {
  6665. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6666. "(%d):2542 Try to issue mailbox command "
  6667. "x%x (x%x/x%x) synchronously ahead of async"
  6668. "mailbox command queue: x%x x%x\n",
  6669. mboxq->vport ? mboxq->vport->vpi : 0,
  6670. mboxq->u.mb.mbxCommand,
  6671. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6672. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6673. psli->sli_flag, flag);
  6674. /* Try to block the asynchronous mailbox posting */
  6675. rc = lpfc_sli4_async_mbox_block(phba);
  6676. if (!rc) {
  6677. /* Successfully blocked, now issue sync mbox cmd */
  6678. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6679. if (rc != MBX_SUCCESS)
  6680. lpfc_printf_log(phba, KERN_WARNING,
  6681. LOG_MBOX | LOG_SLI,
  6682. "(%d):2597 Sync Mailbox command "
  6683. "x%x (x%x/x%x) failure: "
  6684. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6685. "Data: x%x x%x\n,",
  6686. mboxq->vport ? mboxq->vport->vpi : 0,
  6687. mboxq->u.mb.mbxCommand,
  6688. lpfc_sli_config_mbox_subsys_get(phba,
  6689. mboxq),
  6690. lpfc_sli_config_mbox_opcode_get(phba,
  6691. mboxq),
  6692. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6693. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6694. bf_get(lpfc_mcqe_ext_status,
  6695. &mboxq->mcqe),
  6696. psli->sli_flag, flag);
  6697. /* Unblock the async mailbox posting afterward */
  6698. lpfc_sli4_async_mbox_unblock(phba);
  6699. }
  6700. return rc;
  6701. }
  6702. /* Now, interrupt mode asynchrous mailbox command */
  6703. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6704. if (rc) {
  6705. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6706. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6707. "cannot issue Data: x%x x%x\n",
  6708. mboxq->vport ? mboxq->vport->vpi : 0,
  6709. mboxq->u.mb.mbxCommand,
  6710. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6711. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6712. psli->sli_flag, flag);
  6713. goto out_not_finished;
  6714. }
  6715. /* Put the mailbox command to the driver internal FIFO */
  6716. psli->slistat.mbox_busy++;
  6717. spin_lock_irqsave(&phba->hbalock, iflags);
  6718. lpfc_mbox_put(phba, mboxq);
  6719. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6720. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6721. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6722. "x%x (x%x/x%x) x%x x%x x%x\n",
  6723. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6724. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6725. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6726. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6727. phba->pport->port_state,
  6728. psli->sli_flag, MBX_NOWAIT);
  6729. /* Wake up worker thread to transport mailbox command from head */
  6730. lpfc_worker_wake_up(phba);
  6731. return MBX_BUSY;
  6732. out_not_finished:
  6733. return MBX_NOT_FINISHED;
  6734. }
  6735. /**
  6736. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6737. * @phba: Pointer to HBA context object.
  6738. *
  6739. * This function is called by worker thread to send a mailbox command to
  6740. * SLI4 HBA firmware.
  6741. *
  6742. **/
  6743. int
  6744. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6745. {
  6746. struct lpfc_sli *psli = &phba->sli;
  6747. LPFC_MBOXQ_t *mboxq;
  6748. int rc = MBX_SUCCESS;
  6749. unsigned long iflags;
  6750. struct lpfc_mqe *mqe;
  6751. uint32_t mbx_cmnd;
  6752. /* Check interrupt mode before post async mailbox command */
  6753. if (unlikely(!phba->sli4_hba.intr_enable))
  6754. return MBX_NOT_FINISHED;
  6755. /* Check for mailbox command service token */
  6756. spin_lock_irqsave(&phba->hbalock, iflags);
  6757. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6758. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6759. return MBX_NOT_FINISHED;
  6760. }
  6761. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6762. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6763. return MBX_NOT_FINISHED;
  6764. }
  6765. if (unlikely(phba->sli.mbox_active)) {
  6766. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6767. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6768. "0384 There is pending active mailbox cmd\n");
  6769. return MBX_NOT_FINISHED;
  6770. }
  6771. /* Take the mailbox command service token */
  6772. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6773. /* Get the next mailbox command from head of queue */
  6774. mboxq = lpfc_mbox_get(phba);
  6775. /* If no more mailbox command waiting for post, we're done */
  6776. if (!mboxq) {
  6777. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6778. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6779. return MBX_SUCCESS;
  6780. }
  6781. phba->sli.mbox_active = mboxq;
  6782. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6783. /* Check device readiness for posting mailbox command */
  6784. rc = lpfc_mbox_dev_check(phba);
  6785. if (unlikely(rc))
  6786. /* Driver clean routine will clean up pending mailbox */
  6787. goto out_not_finished;
  6788. /* Prepare the mbox command to be posted */
  6789. mqe = &mboxq->u.mqe;
  6790. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6791. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6792. mod_timer(&psli->mbox_tmo, (jiffies +
  6793. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6794. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6795. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6796. "x%x x%x\n",
  6797. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6798. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6799. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6800. phba->pport->port_state, psli->sli_flag);
  6801. if (mbx_cmnd != MBX_HEARTBEAT) {
  6802. if (mboxq->vport) {
  6803. lpfc_debugfs_disc_trc(mboxq->vport,
  6804. LPFC_DISC_TRC_MBOX_VPORT,
  6805. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6806. mbx_cmnd, mqe->un.mb_words[0],
  6807. mqe->un.mb_words[1]);
  6808. } else {
  6809. lpfc_debugfs_disc_trc(phba->pport,
  6810. LPFC_DISC_TRC_MBOX,
  6811. "MBOX Send: cmd:x%x mb:x%x x%x",
  6812. mbx_cmnd, mqe->un.mb_words[0],
  6813. mqe->un.mb_words[1]);
  6814. }
  6815. }
  6816. psli->slistat.mbox_cmd++;
  6817. /* Post the mailbox command to the port */
  6818. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6819. if (rc != MBX_SUCCESS) {
  6820. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6821. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6822. "cannot issue Data: x%x x%x\n",
  6823. mboxq->vport ? mboxq->vport->vpi : 0,
  6824. mboxq->u.mb.mbxCommand,
  6825. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6826. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6827. psli->sli_flag, MBX_NOWAIT);
  6828. goto out_not_finished;
  6829. }
  6830. return rc;
  6831. out_not_finished:
  6832. spin_lock_irqsave(&phba->hbalock, iflags);
  6833. if (phba->sli.mbox_active) {
  6834. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6835. __lpfc_mbox_cmpl_put(phba, mboxq);
  6836. /* Release the token */
  6837. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6838. phba->sli.mbox_active = NULL;
  6839. }
  6840. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6841. return MBX_NOT_FINISHED;
  6842. }
  6843. /**
  6844. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6845. * @phba: Pointer to HBA context object.
  6846. * @pmbox: Pointer to mailbox object.
  6847. * @flag: Flag indicating how the mailbox need to be processed.
  6848. *
  6849. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6850. * the API jump table function pointer from the lpfc_hba struct.
  6851. *
  6852. * Return codes the caller owns the mailbox command after the return of the
  6853. * function.
  6854. **/
  6855. int
  6856. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6857. {
  6858. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6859. }
  6860. /**
  6861. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6862. * @phba: The hba struct for which this call is being executed.
  6863. * @dev_grp: The HBA PCI-Device group number.
  6864. *
  6865. * This routine sets up the mbox interface API function jump table in @phba
  6866. * struct.
  6867. * Returns: 0 - success, -ENODEV - failure.
  6868. **/
  6869. int
  6870. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6871. {
  6872. switch (dev_grp) {
  6873. case LPFC_PCI_DEV_LP:
  6874. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6875. phba->lpfc_sli_handle_slow_ring_event =
  6876. lpfc_sli_handle_slow_ring_event_s3;
  6877. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6878. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6879. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6880. break;
  6881. case LPFC_PCI_DEV_OC:
  6882. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6883. phba->lpfc_sli_handle_slow_ring_event =
  6884. lpfc_sli_handle_slow_ring_event_s4;
  6885. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6886. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6887. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6888. break;
  6889. default:
  6890. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6891. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6892. dev_grp);
  6893. return -ENODEV;
  6894. break;
  6895. }
  6896. return 0;
  6897. }
  6898. /**
  6899. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6900. * @phba: Pointer to HBA context object.
  6901. * @pring: Pointer to driver SLI ring object.
  6902. * @piocb: Pointer to address of newly added command iocb.
  6903. *
  6904. * This function is called with hbalock held to add a command
  6905. * iocb to the txq when SLI layer cannot submit the command iocb
  6906. * to the ring.
  6907. **/
  6908. void
  6909. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6910. struct lpfc_iocbq *piocb)
  6911. {
  6912. /* Insert the caller's iocb in the txq tail for later processing. */
  6913. list_add_tail(&piocb->list, &pring->txq);
  6914. pring->txq_cnt++;
  6915. }
  6916. /**
  6917. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6918. * @phba: Pointer to HBA context object.
  6919. * @pring: Pointer to driver SLI ring object.
  6920. * @piocb: Pointer to address of newly added command iocb.
  6921. *
  6922. * This function is called with hbalock held before a new
  6923. * iocb is submitted to the firmware. This function checks
  6924. * txq to flush the iocbs in txq to Firmware before
  6925. * submitting new iocbs to the Firmware.
  6926. * If there are iocbs in the txq which need to be submitted
  6927. * to firmware, lpfc_sli_next_iocb returns the first element
  6928. * of the txq after dequeuing it from txq.
  6929. * If there is no iocb in the txq then the function will return
  6930. * *piocb and *piocb is set to NULL. Caller needs to check
  6931. * *piocb to find if there are more commands in the txq.
  6932. **/
  6933. static struct lpfc_iocbq *
  6934. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6935. struct lpfc_iocbq **piocb)
  6936. {
  6937. struct lpfc_iocbq * nextiocb;
  6938. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6939. if (!nextiocb) {
  6940. nextiocb = *piocb;
  6941. *piocb = NULL;
  6942. }
  6943. return nextiocb;
  6944. }
  6945. /**
  6946. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6947. * @phba: Pointer to HBA context object.
  6948. * @ring_number: SLI ring number to issue iocb on.
  6949. * @piocb: Pointer to command iocb.
  6950. * @flag: Flag indicating if this command can be put into txq.
  6951. *
  6952. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6953. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6954. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6955. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6956. * this function allows only iocbs for posting buffers. This function finds
  6957. * next available slot in the command ring and posts the command to the
  6958. * available slot and writes the port attention register to request HBA start
  6959. * processing new iocb. If there is no slot available in the ring and
  6960. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6961. * the function returns IOCB_BUSY.
  6962. *
  6963. * This function is called with hbalock held. The function will return success
  6964. * after it successfully submit the iocb to firmware or after adding to the
  6965. * txq.
  6966. **/
  6967. static int
  6968. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6969. struct lpfc_iocbq *piocb, uint32_t flag)
  6970. {
  6971. struct lpfc_iocbq *nextiocb;
  6972. IOCB_t *iocb;
  6973. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6974. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6975. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6976. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6977. lpfc_printf_log(phba, KERN_ERR,
  6978. LOG_SLI | LOG_VPORT,
  6979. "1807 IOCB x%x failed. No vport\n",
  6980. piocb->iocb.ulpCommand);
  6981. dump_stack();
  6982. return IOCB_ERROR;
  6983. }
  6984. /* If the PCI channel is in offline state, do not post iocbs. */
  6985. if (unlikely(pci_channel_offline(phba->pcidev)))
  6986. return IOCB_ERROR;
  6987. /* If HBA has a deferred error attention, fail the iocb. */
  6988. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6989. return IOCB_ERROR;
  6990. /*
  6991. * We should never get an IOCB if we are in a < LINK_DOWN state
  6992. */
  6993. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6994. return IOCB_ERROR;
  6995. /*
  6996. * Check to see if we are blocking IOCB processing because of a
  6997. * outstanding event.
  6998. */
  6999. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  7000. goto iocb_busy;
  7001. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  7002. /*
  7003. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  7004. * can be issued if the link is not up.
  7005. */
  7006. switch (piocb->iocb.ulpCommand) {
  7007. case CMD_GEN_REQUEST64_CR:
  7008. case CMD_GEN_REQUEST64_CX:
  7009. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  7010. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  7011. FC_RCTL_DD_UNSOL_CMD) ||
  7012. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  7013. MENLO_TRANSPORT_TYPE))
  7014. goto iocb_busy;
  7015. break;
  7016. case CMD_QUE_RING_BUF_CN:
  7017. case CMD_QUE_RING_BUF64_CN:
  7018. /*
  7019. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  7020. * completion, iocb_cmpl MUST be 0.
  7021. */
  7022. if (piocb->iocb_cmpl)
  7023. piocb->iocb_cmpl = NULL;
  7024. /*FALLTHROUGH*/
  7025. case CMD_CREATE_XRI_CR:
  7026. case CMD_CLOSE_XRI_CN:
  7027. case CMD_CLOSE_XRI_CX:
  7028. break;
  7029. default:
  7030. goto iocb_busy;
  7031. }
  7032. /*
  7033. * For FCP commands, we must be in a state where we can process link
  7034. * attention events.
  7035. */
  7036. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  7037. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  7038. goto iocb_busy;
  7039. }
  7040. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  7041. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  7042. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  7043. if (iocb)
  7044. lpfc_sli_update_ring(phba, pring);
  7045. else
  7046. lpfc_sli_update_full_ring(phba, pring);
  7047. if (!piocb)
  7048. return IOCB_SUCCESS;
  7049. goto out_busy;
  7050. iocb_busy:
  7051. pring->stats.iocb_cmd_delay++;
  7052. out_busy:
  7053. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7054. __lpfc_sli_ringtx_put(phba, pring, piocb);
  7055. return IOCB_SUCCESS;
  7056. }
  7057. return IOCB_BUSY;
  7058. }
  7059. /**
  7060. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  7061. * @phba: Pointer to HBA context object.
  7062. * @piocb: Pointer to command iocb.
  7063. * @sglq: Pointer to the scatter gather queue object.
  7064. *
  7065. * This routine converts the bpl or bde that is in the IOCB
  7066. * to a sgl list for the sli4 hardware. The physical address
  7067. * of the bpl/bde is converted back to a virtual address.
  7068. * If the IOCB contains a BPL then the list of BDE's is
  7069. * converted to sli4_sge's. If the IOCB contains a single
  7070. * BDE then it is converted to a single sli_sge.
  7071. * The IOCB is still in cpu endianess so the contents of
  7072. * the bpl can be used without byte swapping.
  7073. *
  7074. * Returns valid XRI = Success, NO_XRI = Failure.
  7075. **/
  7076. static uint16_t
  7077. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  7078. struct lpfc_sglq *sglq)
  7079. {
  7080. uint16_t xritag = NO_XRI;
  7081. struct ulp_bde64 *bpl = NULL;
  7082. struct ulp_bde64 bde;
  7083. struct sli4_sge *sgl = NULL;
  7084. struct lpfc_dmabuf *dmabuf;
  7085. IOCB_t *icmd;
  7086. int numBdes = 0;
  7087. int i = 0;
  7088. uint32_t offset = 0; /* accumulated offset in the sg request list */
  7089. int inbound = 0; /* number of sg reply entries inbound from firmware */
  7090. if (!piocbq || !sglq)
  7091. return xritag;
  7092. sgl = (struct sli4_sge *)sglq->sgl;
  7093. icmd = &piocbq->iocb;
  7094. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7095. return sglq->sli4_xritag;
  7096. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7097. numBdes = icmd->un.genreq64.bdl.bdeSize /
  7098. sizeof(struct ulp_bde64);
  7099. /* The addrHigh and addrLow fields within the IOCB
  7100. * have not been byteswapped yet so there is no
  7101. * need to swap them back.
  7102. */
  7103. if (piocbq->context3)
  7104. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  7105. else
  7106. return xritag;
  7107. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7108. if (!bpl)
  7109. return xritag;
  7110. for (i = 0; i < numBdes; i++) {
  7111. /* Should already be byte swapped. */
  7112. sgl->addr_hi = bpl->addrHigh;
  7113. sgl->addr_lo = bpl->addrLow;
  7114. sgl->word2 = le32_to_cpu(sgl->word2);
  7115. if ((i+1) == numBdes)
  7116. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7117. else
  7118. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7119. /* swap the size field back to the cpu so we
  7120. * can assign it to the sgl.
  7121. */
  7122. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7123. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7124. /* The offsets in the sgl need to be accumulated
  7125. * separately for the request and reply lists.
  7126. * The request is always first, the reply follows.
  7127. */
  7128. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7129. /* add up the reply sg entries */
  7130. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7131. inbound++;
  7132. /* first inbound? reset the offset */
  7133. if (inbound == 1)
  7134. offset = 0;
  7135. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7136. bf_set(lpfc_sli4_sge_type, sgl,
  7137. LPFC_SGE_TYPE_DATA);
  7138. offset += bde.tus.f.bdeSize;
  7139. }
  7140. sgl->word2 = cpu_to_le32(sgl->word2);
  7141. bpl++;
  7142. sgl++;
  7143. }
  7144. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7145. /* The addrHigh and addrLow fields of the BDE have not
  7146. * been byteswapped yet so they need to be swapped
  7147. * before putting them in the sgl.
  7148. */
  7149. sgl->addr_hi =
  7150. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7151. sgl->addr_lo =
  7152. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7153. sgl->word2 = le32_to_cpu(sgl->word2);
  7154. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7155. sgl->word2 = cpu_to_le32(sgl->word2);
  7156. sgl->sge_len =
  7157. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7158. }
  7159. return sglq->sli4_xritag;
  7160. }
  7161. /**
  7162. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7163. * @phba: Pointer to HBA context object.
  7164. *
  7165. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7166. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7167. * held.
  7168. *
  7169. * Return: index into SLI4 fast-path FCP queue index.
  7170. **/
  7171. static inline uint32_t
  7172. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7173. {
  7174. int i;
  7175. if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
  7176. i = smp_processor_id();
  7177. else
  7178. i = atomic_add_return(1, &phba->fcp_qidx);
  7179. i = (i % phba->cfg_fcp_io_channel);
  7180. return i;
  7181. }
  7182. /**
  7183. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7184. * @phba: Pointer to HBA context object.
  7185. * @piocb: Pointer to command iocb.
  7186. * @wqe: Pointer to the work queue entry.
  7187. *
  7188. * This routine converts the iocb command to its Work Queue Entry
  7189. * equivalent. The wqe pointer should not have any fields set when
  7190. * this routine is called because it will memcpy over them.
  7191. * This routine does not set the CQ_ID or the WQEC bits in the
  7192. * wqe.
  7193. *
  7194. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7195. **/
  7196. static int
  7197. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7198. union lpfc_wqe *wqe)
  7199. {
  7200. uint32_t xmit_len = 0, total_len = 0;
  7201. uint8_t ct = 0;
  7202. uint32_t fip;
  7203. uint32_t abort_tag;
  7204. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7205. uint8_t cmnd;
  7206. uint16_t xritag;
  7207. uint16_t abrt_iotag;
  7208. struct lpfc_iocbq *abrtiocbq;
  7209. struct ulp_bde64 *bpl = NULL;
  7210. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7211. int numBdes, i;
  7212. struct ulp_bde64 bde;
  7213. struct lpfc_nodelist *ndlp;
  7214. uint32_t *pcmd;
  7215. uint32_t if_type;
  7216. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7217. /* The fcp commands will set command type */
  7218. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7219. command_type = FCP_COMMAND;
  7220. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7221. command_type = ELS_COMMAND_FIP;
  7222. else
  7223. command_type = ELS_COMMAND_NON_FIP;
  7224. /* Some of the fields are in the right position already */
  7225. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7226. abort_tag = (uint32_t) iocbq->iotag;
  7227. xritag = iocbq->sli4_xritag;
  7228. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7229. /* words0-2 bpl convert bde */
  7230. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7231. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7232. sizeof(struct ulp_bde64);
  7233. bpl = (struct ulp_bde64 *)
  7234. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7235. if (!bpl)
  7236. return IOCB_ERROR;
  7237. /* Should already be byte swapped. */
  7238. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7239. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7240. /* swap the size field back to the cpu so we
  7241. * can assign it to the sgl.
  7242. */
  7243. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7244. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7245. total_len = 0;
  7246. for (i = 0; i < numBdes; i++) {
  7247. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7248. total_len += bde.tus.f.bdeSize;
  7249. }
  7250. } else
  7251. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7252. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7253. cmnd = iocbq->iocb.ulpCommand;
  7254. switch (iocbq->iocb.ulpCommand) {
  7255. case CMD_ELS_REQUEST64_CR:
  7256. if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
  7257. ndlp = iocbq->context_un.ndlp;
  7258. else
  7259. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7260. if (!iocbq->iocb.ulpLe) {
  7261. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7262. "2007 Only Limited Edition cmd Format"
  7263. " supported 0x%x\n",
  7264. iocbq->iocb.ulpCommand);
  7265. return IOCB_ERROR;
  7266. }
  7267. wqe->els_req.payload_len = xmit_len;
  7268. /* Els_reguest64 has a TMO */
  7269. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7270. iocbq->iocb.ulpTimeout);
  7271. /* Need a VF for word 4 set the vf bit*/
  7272. bf_set(els_req64_vf, &wqe->els_req, 0);
  7273. /* And a VFID for word 12 */
  7274. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7275. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7276. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7277. iocbq->iocb.ulpContext);
  7278. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7279. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7280. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7281. if (command_type == ELS_COMMAND_FIP)
  7282. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7283. >> LPFC_FIP_ELS_ID_SHIFT);
  7284. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7285. iocbq->context2)->virt);
  7286. if_type = bf_get(lpfc_sli_intf_if_type,
  7287. &phba->sli4_hba.sli_intf);
  7288. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7289. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7290. *pcmd == ELS_CMD_SCR ||
  7291. *pcmd == ELS_CMD_FDISC ||
  7292. *pcmd == ELS_CMD_LOGO ||
  7293. *pcmd == ELS_CMD_PLOGI)) {
  7294. bf_set(els_req64_sp, &wqe->els_req, 1);
  7295. bf_set(els_req64_sid, &wqe->els_req,
  7296. iocbq->vport->fc_myDID);
  7297. if ((*pcmd == ELS_CMD_FLOGI) &&
  7298. !(phba->fc_topology ==
  7299. LPFC_TOPOLOGY_LOOP))
  7300. bf_set(els_req64_sid, &wqe->els_req, 0);
  7301. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7302. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7303. phba->vpi_ids[iocbq->vport->vpi]);
  7304. } else if (pcmd && iocbq->context1) {
  7305. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7306. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7307. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7308. }
  7309. }
  7310. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7311. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7312. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7313. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7314. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7315. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7316. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7317. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7318. break;
  7319. case CMD_XMIT_SEQUENCE64_CX:
  7320. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7321. iocbq->iocb.un.ulpWord[3]);
  7322. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7323. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7324. /* The entire sequence is transmitted for this IOCB */
  7325. xmit_len = total_len;
  7326. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7327. if (phba->link_flag & LS_LOOPBACK_MODE)
  7328. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7329. case CMD_XMIT_SEQUENCE64_CR:
  7330. /* word3 iocb=io_tag32 wqe=reserved */
  7331. wqe->xmit_sequence.rsvd3 = 0;
  7332. /* word4 relative_offset memcpy */
  7333. /* word5 r_ctl/df_ctl memcpy */
  7334. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7335. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7336. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7337. LPFC_WQE_IOD_WRITE);
  7338. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7339. LPFC_WQE_LENLOC_WORD12);
  7340. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7341. wqe->xmit_sequence.xmit_len = xmit_len;
  7342. command_type = OTHER_COMMAND;
  7343. break;
  7344. case CMD_XMIT_BCAST64_CN:
  7345. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7346. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7347. /* word4 iocb=rsvd wqe=rsvd */
  7348. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7349. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7350. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7351. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7352. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7353. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7354. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7355. LPFC_WQE_LENLOC_WORD3);
  7356. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7357. break;
  7358. case CMD_FCP_IWRITE64_CR:
  7359. command_type = FCP_COMMAND_DATA_OUT;
  7360. /* word3 iocb=iotag wqe=payload_offset_len */
  7361. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7362. wqe->fcp_iwrite.payload_offset_len =
  7363. xmit_len + sizeof(struct fcp_rsp);
  7364. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7365. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7366. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7367. iocbq->iocb.ulpFCP2Rcvy);
  7368. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7369. /* Always open the exchange */
  7370. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7371. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7372. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7373. LPFC_WQE_LENLOC_WORD4);
  7374. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7375. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7376. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7377. break;
  7378. case CMD_FCP_IREAD64_CR:
  7379. /* word3 iocb=iotag wqe=payload_offset_len */
  7380. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7381. wqe->fcp_iread.payload_offset_len =
  7382. xmit_len + sizeof(struct fcp_rsp);
  7383. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7384. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7385. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7386. iocbq->iocb.ulpFCP2Rcvy);
  7387. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7388. /* Always open the exchange */
  7389. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7390. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7391. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7392. LPFC_WQE_LENLOC_WORD4);
  7393. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7394. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7395. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7396. break;
  7397. case CMD_FCP_ICMND64_CR:
  7398. /* word3 iocb=IO_TAG wqe=reserved */
  7399. wqe->fcp_icmd.rsrvd3 = 0;
  7400. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7401. /* Always open the exchange */
  7402. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7403. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7404. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7405. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7406. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7407. LPFC_WQE_LENLOC_NONE);
  7408. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7409. bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
  7410. iocbq->iocb.ulpFCP2Rcvy);
  7411. break;
  7412. case CMD_GEN_REQUEST64_CR:
  7413. /* For this command calculate the xmit length of the
  7414. * request bde.
  7415. */
  7416. xmit_len = 0;
  7417. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7418. sizeof(struct ulp_bde64);
  7419. for (i = 0; i < numBdes; i++) {
  7420. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7421. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7422. break;
  7423. xmit_len += bde.tus.f.bdeSize;
  7424. }
  7425. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7426. wqe->gen_req.request_payload_len = xmit_len;
  7427. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7428. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7429. /* word6 context tag copied in memcpy */
  7430. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7431. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7432. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7433. "2015 Invalid CT %x command 0x%x\n",
  7434. ct, iocbq->iocb.ulpCommand);
  7435. return IOCB_ERROR;
  7436. }
  7437. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7438. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7439. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7440. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7441. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7442. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7443. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7444. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7445. command_type = OTHER_COMMAND;
  7446. break;
  7447. case CMD_XMIT_ELS_RSP64_CX:
  7448. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7449. /* words0-2 BDE memcpy */
  7450. /* word3 iocb=iotag32 wqe=response_payload_len */
  7451. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7452. /* word4 */
  7453. wqe->xmit_els_rsp.word4 = 0;
  7454. /* word5 iocb=rsvd wge=did */
  7455. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7456. iocbq->iocb.un.xseq64.xmit_els_remoteID);
  7457. if_type = bf_get(lpfc_sli_intf_if_type,
  7458. &phba->sli4_hba.sli_intf);
  7459. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7460. if (iocbq->vport->fc_flag & FC_PT2PT) {
  7461. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7462. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7463. iocbq->vport->fc_myDID);
  7464. if (iocbq->vport->fc_myDID == Fabric_DID) {
  7465. bf_set(wqe_els_did,
  7466. &wqe->xmit_els_rsp.wqe_dest, 0);
  7467. }
  7468. }
  7469. }
  7470. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7471. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7472. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7473. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7474. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7475. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7476. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7477. phba->vpi_ids[iocbq->vport->vpi]);
  7478. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7479. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7480. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7481. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7482. LPFC_WQE_LENLOC_WORD3);
  7483. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7484. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7485. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7486. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7487. iocbq->context2)->virt);
  7488. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7489. bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
  7490. bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
  7491. iocbq->vport->fc_myDID);
  7492. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
  7493. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7494. phba->vpi_ids[phba->pport->vpi]);
  7495. }
  7496. command_type = OTHER_COMMAND;
  7497. break;
  7498. case CMD_CLOSE_XRI_CN:
  7499. case CMD_ABORT_XRI_CN:
  7500. case CMD_ABORT_XRI_CX:
  7501. /* words 0-2 memcpy should be 0 rserved */
  7502. /* port will send abts */
  7503. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7504. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7505. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7506. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7507. } else
  7508. fip = 0;
  7509. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7510. /*
  7511. * The link is down, or the command was ELS_FIP
  7512. * so the fw does not need to send abts
  7513. * on the wire.
  7514. */
  7515. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7516. else
  7517. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7518. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7519. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7520. wqe->abort_cmd.rsrvd5 = 0;
  7521. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7522. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7523. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7524. /*
  7525. * The abort handler will send us CMD_ABORT_XRI_CN or
  7526. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7527. */
  7528. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7529. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7530. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7531. LPFC_WQE_LENLOC_NONE);
  7532. cmnd = CMD_ABORT_XRI_CX;
  7533. command_type = OTHER_COMMAND;
  7534. xritag = 0;
  7535. break;
  7536. case CMD_XMIT_BLS_RSP64_CX:
  7537. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7538. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7539. * we re-construct this WQE here based on information in
  7540. * iocbq from scratch.
  7541. */
  7542. memset(wqe, 0, sizeof(union lpfc_wqe));
  7543. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7544. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7545. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7546. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7547. LPFC_ABTS_UNSOL_INT) {
  7548. /* ABTS sent by initiator to CT exchange, the
  7549. * RX_ID field will be filled with the newly
  7550. * allocated responder XRI.
  7551. */
  7552. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7553. iocbq->sli4_xritag);
  7554. } else {
  7555. /* ABTS sent by responder to CT exchange, the
  7556. * RX_ID field will be filled with the responder
  7557. * RX_ID from ABTS.
  7558. */
  7559. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7560. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7561. }
  7562. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7563. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7564. /* Use CT=VPI */
  7565. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7566. ndlp->nlp_DID);
  7567. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7568. iocbq->iocb.ulpContext);
  7569. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7570. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7571. phba->vpi_ids[phba->pport->vpi]);
  7572. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7573. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7574. LPFC_WQE_LENLOC_NONE);
  7575. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7576. command_type = OTHER_COMMAND;
  7577. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7578. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7579. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7580. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7581. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7582. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7583. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7584. }
  7585. break;
  7586. case CMD_XRI_ABORTED_CX:
  7587. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7588. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7589. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7590. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7591. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7592. default:
  7593. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7594. "2014 Invalid command 0x%x\n",
  7595. iocbq->iocb.ulpCommand);
  7596. return IOCB_ERROR;
  7597. break;
  7598. }
  7599. if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
  7600. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
  7601. else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
  7602. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
  7603. else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
  7604. bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
  7605. iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
  7606. LPFC_IO_DIF_INSERT);
  7607. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7608. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7609. wqe->generic.wqe_com.abort_tag = abort_tag;
  7610. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7611. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7612. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7613. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7614. return 0;
  7615. }
  7616. /**
  7617. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7618. * @phba: Pointer to HBA context object.
  7619. * @ring_number: SLI ring number to issue iocb on.
  7620. * @piocb: Pointer to command iocb.
  7621. * @flag: Flag indicating if this command can be put into txq.
  7622. *
  7623. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7624. * an iocb command to an HBA with SLI-4 interface spec.
  7625. *
  7626. * This function is called with hbalock held. The function will return success
  7627. * after it successfully submit the iocb to firmware or after adding to the
  7628. * txq.
  7629. **/
  7630. static int
  7631. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7632. struct lpfc_iocbq *piocb, uint32_t flag)
  7633. {
  7634. struct lpfc_sglq *sglq;
  7635. union lpfc_wqe wqe;
  7636. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7637. if (piocb->sli4_xritag == NO_XRI) {
  7638. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7639. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7640. sglq = NULL;
  7641. else {
  7642. if (pring->txq_cnt) {
  7643. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7644. __lpfc_sli_ringtx_put(phba,
  7645. pring, piocb);
  7646. return IOCB_SUCCESS;
  7647. } else {
  7648. return IOCB_BUSY;
  7649. }
  7650. } else {
  7651. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7652. if (!sglq) {
  7653. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7654. __lpfc_sli_ringtx_put(phba,
  7655. pring,
  7656. piocb);
  7657. return IOCB_SUCCESS;
  7658. } else
  7659. return IOCB_BUSY;
  7660. }
  7661. }
  7662. }
  7663. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7664. /* These IO's already have an XRI and a mapped sgl. */
  7665. sglq = NULL;
  7666. } else {
  7667. /*
  7668. * This is a continuation of a commandi,(CX) so this
  7669. * sglq is on the active list
  7670. */
  7671. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
  7672. if (!sglq)
  7673. return IOCB_ERROR;
  7674. }
  7675. if (sglq) {
  7676. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7677. piocb->sli4_xritag = sglq->sli4_xritag;
  7678. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7679. return IOCB_ERROR;
  7680. }
  7681. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7682. return IOCB_ERROR;
  7683. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7684. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7685. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7686. &wqe))
  7687. return IOCB_ERROR;
  7688. } else {
  7689. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7690. return IOCB_ERROR;
  7691. }
  7692. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7693. return 0;
  7694. }
  7695. /**
  7696. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7697. *
  7698. * This routine wraps the actual lockless version for issusing IOCB function
  7699. * pointer from the lpfc_hba struct.
  7700. *
  7701. * Return codes:
  7702. * IOCB_ERROR - Error
  7703. * IOCB_SUCCESS - Success
  7704. * IOCB_BUSY - Busy
  7705. **/
  7706. int
  7707. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7708. struct lpfc_iocbq *piocb, uint32_t flag)
  7709. {
  7710. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7711. }
  7712. /**
  7713. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7714. * @phba: The hba struct for which this call is being executed.
  7715. * @dev_grp: The HBA PCI-Device group number.
  7716. *
  7717. * This routine sets up the SLI interface API function jump table in @phba
  7718. * struct.
  7719. * Returns: 0 - success, -ENODEV - failure.
  7720. **/
  7721. int
  7722. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7723. {
  7724. switch (dev_grp) {
  7725. case LPFC_PCI_DEV_LP:
  7726. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7727. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7728. break;
  7729. case LPFC_PCI_DEV_OC:
  7730. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7731. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7732. break;
  7733. default:
  7734. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7735. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7736. dev_grp);
  7737. return -ENODEV;
  7738. break;
  7739. }
  7740. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7741. return 0;
  7742. }
  7743. /**
  7744. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7745. * @phba: Pointer to HBA context object.
  7746. * @pring: Pointer to driver SLI ring object.
  7747. * @piocb: Pointer to command iocb.
  7748. * @flag: Flag indicating if this command can be put into txq.
  7749. *
  7750. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7751. * function. This function gets the hbalock and calls
  7752. * __lpfc_sli_issue_iocb function and will return the error returned
  7753. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7754. * functions which do not hold hbalock.
  7755. **/
  7756. int
  7757. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7758. struct lpfc_iocbq *piocb, uint32_t flag)
  7759. {
  7760. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  7761. struct lpfc_sli_ring *pring;
  7762. struct lpfc_queue *fpeq;
  7763. struct lpfc_eqe *eqe;
  7764. unsigned long iflags;
  7765. int rc, idx;
  7766. if (phba->sli_rev == LPFC_SLI_REV4) {
  7767. if (piocb->iocb_flag & LPFC_IO_FCP) {
  7768. if (unlikely(!phba->sli4_hba.fcp_wq))
  7769. return IOCB_ERROR;
  7770. idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7771. piocb->fcp_wqidx = idx;
  7772. ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
  7773. pring = &phba->sli.ring[ring_number];
  7774. spin_lock_irqsave(&pring->ring_lock, iflags);
  7775. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
  7776. flag);
  7777. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7778. if (lpfc_fcp_look_ahead) {
  7779. fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
  7780. if (atomic_dec_and_test(&fcp_eq_hdl->
  7781. fcp_eq_in_use)) {
  7782. /* Get associated EQ with this index */
  7783. fpeq = phba->sli4_hba.hba_eq[idx];
  7784. /* Turn off interrupts from this EQ */
  7785. lpfc_sli4_eq_clr_intr(fpeq);
  7786. /*
  7787. * Process all the events on FCP EQ
  7788. */
  7789. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  7790. lpfc_sli4_hba_handle_eqe(phba,
  7791. eqe, idx);
  7792. fpeq->EQ_processed++;
  7793. }
  7794. /* Always clear and re-arm the EQ */
  7795. lpfc_sli4_eq_release(fpeq,
  7796. LPFC_QUEUE_REARM);
  7797. }
  7798. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  7799. }
  7800. } else {
  7801. pring = &phba->sli.ring[ring_number];
  7802. spin_lock_irqsave(&pring->ring_lock, iflags);
  7803. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
  7804. flag);
  7805. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  7806. }
  7807. } else {
  7808. /* For now, SLI2/3 will still use hbalock */
  7809. spin_lock_irqsave(&phba->hbalock, iflags);
  7810. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7811. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7812. }
  7813. return rc;
  7814. }
  7815. /**
  7816. * lpfc_extra_ring_setup - Extra ring setup function
  7817. * @phba: Pointer to HBA context object.
  7818. *
  7819. * This function is called while driver attaches with the
  7820. * HBA to setup the extra ring. The extra ring is used
  7821. * only when driver needs to support target mode functionality
  7822. * or IP over FC functionalities.
  7823. *
  7824. * This function is called with no lock held.
  7825. **/
  7826. static int
  7827. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7828. {
  7829. struct lpfc_sli *psli;
  7830. struct lpfc_sli_ring *pring;
  7831. psli = &phba->sli;
  7832. /* Adjust cmd/rsp ring iocb entries more evenly */
  7833. /* Take some away from the FCP ring */
  7834. pring = &psli->ring[psli->fcp_ring];
  7835. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7836. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7837. pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7838. pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7839. /* and give them to the extra ring */
  7840. pring = &psli->ring[psli->extra_ring];
  7841. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7842. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7843. pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7844. pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7845. /* Setup default profile for this ring */
  7846. pring->iotag_max = 4096;
  7847. pring->num_mask = 1;
  7848. pring->prt[0].profile = 0; /* Mask 0 */
  7849. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7850. pring->prt[0].type = phba->cfg_multi_ring_type;
  7851. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7852. return 0;
  7853. }
  7854. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7855. * @phba: Pointer to HBA context object.
  7856. * @iocbq: Pointer to iocb object.
  7857. *
  7858. * The async_event handler calls this routine when it receives
  7859. * an ASYNC_STATUS_CN event from the port. The port generates
  7860. * this event when an Abort Sequence request to an rport fails
  7861. * twice in succession. The abort could be originated by the
  7862. * driver or by the port. The ABTS could have been for an ELS
  7863. * or FCP IO. The port only generates this event when an ABTS
  7864. * fails to complete after one retry.
  7865. */
  7866. static void
  7867. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7868. struct lpfc_iocbq *iocbq)
  7869. {
  7870. struct lpfc_nodelist *ndlp = NULL;
  7871. uint16_t rpi = 0, vpi = 0;
  7872. struct lpfc_vport *vport = NULL;
  7873. /* The rpi in the ulpContext is vport-sensitive. */
  7874. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7875. rpi = iocbq->iocb.ulpContext;
  7876. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7877. "3092 Port generated ABTS async event "
  7878. "on vpi %d rpi %d status 0x%x\n",
  7879. vpi, rpi, iocbq->iocb.ulpStatus);
  7880. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7881. if (!vport)
  7882. goto err_exit;
  7883. ndlp = lpfc_findnode_rpi(vport, rpi);
  7884. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7885. goto err_exit;
  7886. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7887. lpfc_sli_abts_recover_port(vport, ndlp);
  7888. return;
  7889. err_exit:
  7890. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7891. "3095 Event Context not found, no "
  7892. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7893. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7894. vpi, rpi);
  7895. }
  7896. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7897. * @phba: pointer to HBA context object.
  7898. * @ndlp: nodelist pointer for the impacted rport.
  7899. * @axri: pointer to the wcqe containing the failed exchange.
  7900. *
  7901. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7902. * port. The port generates this event when an abort exchange request to an
  7903. * rport fails twice in succession with no reply. The abort could be originated
  7904. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7905. */
  7906. void
  7907. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7908. struct lpfc_nodelist *ndlp,
  7909. struct sli4_wcqe_xri_aborted *axri)
  7910. {
  7911. struct lpfc_vport *vport;
  7912. uint32_t ext_status = 0;
  7913. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7914. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7915. "3115 Node Context not found, driver "
  7916. "ignoring abts err event\n");
  7917. return;
  7918. }
  7919. vport = ndlp->vport;
  7920. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7921. "3116 Port generated FCP XRI ABORT event on "
  7922. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7923. ndlp->vport->vpi, ndlp->nlp_rpi,
  7924. bf_get(lpfc_wcqe_xa_xri, axri),
  7925. bf_get(lpfc_wcqe_xa_status, axri),
  7926. axri->parameter);
  7927. /*
  7928. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7929. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7930. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7931. */
  7932. ext_status = axri->parameter & IOERR_PARAM_MASK;
  7933. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7934. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7935. lpfc_sli_abts_recover_port(vport, ndlp);
  7936. }
  7937. /**
  7938. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7939. * @phba: Pointer to HBA context object.
  7940. * @pring: Pointer to driver SLI ring object.
  7941. * @iocbq: Pointer to iocb object.
  7942. *
  7943. * This function is called by the slow ring event handler
  7944. * function when there is an ASYNC event iocb in the ring.
  7945. * This function is called with no lock held.
  7946. * Currently this function handles only temperature related
  7947. * ASYNC events. The function decodes the temperature sensor
  7948. * event message and posts events for the management applications.
  7949. **/
  7950. static void
  7951. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7952. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7953. {
  7954. IOCB_t *icmd;
  7955. uint16_t evt_code;
  7956. struct temp_event temp_event_data;
  7957. struct Scsi_Host *shost;
  7958. uint32_t *iocb_w;
  7959. icmd = &iocbq->iocb;
  7960. evt_code = icmd->un.asyncstat.evt_code;
  7961. switch (evt_code) {
  7962. case ASYNC_TEMP_WARN:
  7963. case ASYNC_TEMP_SAFE:
  7964. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7965. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7966. if (evt_code == ASYNC_TEMP_WARN) {
  7967. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7968. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7969. "0347 Adapter is very hot, please take "
  7970. "corrective action. temperature : %d Celsius\n",
  7971. (uint32_t) icmd->ulpContext);
  7972. } else {
  7973. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7974. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7975. "0340 Adapter temperature is OK now. "
  7976. "temperature : %d Celsius\n",
  7977. (uint32_t) icmd->ulpContext);
  7978. }
  7979. /* Send temperature change event to applications */
  7980. shost = lpfc_shost_from_vport(phba->pport);
  7981. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7982. sizeof(temp_event_data), (char *) &temp_event_data,
  7983. LPFC_NL_VENDOR_ID);
  7984. break;
  7985. case ASYNC_STATUS_CN:
  7986. lpfc_sli_abts_err_handler(phba, iocbq);
  7987. break;
  7988. default:
  7989. iocb_w = (uint32_t *) icmd;
  7990. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7991. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7992. " evt_code 0x%x\n"
  7993. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7994. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7995. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7996. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7997. pring->ringno, icmd->un.asyncstat.evt_code,
  7998. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7999. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  8000. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  8001. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  8002. break;
  8003. }
  8004. }
  8005. /**
  8006. * lpfc_sli_setup - SLI ring setup function
  8007. * @phba: Pointer to HBA context object.
  8008. *
  8009. * lpfc_sli_setup sets up rings of the SLI interface with
  8010. * number of iocbs per ring and iotags. This function is
  8011. * called while driver attach to the HBA and before the
  8012. * interrupts are enabled. So there is no need for locking.
  8013. *
  8014. * This function always returns 0.
  8015. **/
  8016. int
  8017. lpfc_sli_setup(struct lpfc_hba *phba)
  8018. {
  8019. int i, totiocbsize = 0;
  8020. struct lpfc_sli *psli = &phba->sli;
  8021. struct lpfc_sli_ring *pring;
  8022. psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
  8023. if (phba->sli_rev == LPFC_SLI_REV4)
  8024. psli->num_rings += phba->cfg_fcp_io_channel;
  8025. psli->sli_flag = 0;
  8026. psli->fcp_ring = LPFC_FCP_RING;
  8027. psli->next_ring = LPFC_FCP_NEXT_RING;
  8028. psli->extra_ring = LPFC_EXTRA_RING;
  8029. psli->iocbq_lookup = NULL;
  8030. psli->iocbq_lookup_len = 0;
  8031. psli->last_iotag = 0;
  8032. for (i = 0; i < psli->num_rings; i++) {
  8033. pring = &psli->ring[i];
  8034. switch (i) {
  8035. case LPFC_FCP_RING: /* ring 0 - FCP */
  8036. /* numCiocb and numRiocb are used in config_port */
  8037. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  8038. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  8039. pring->sli.sli3.numCiocb +=
  8040. SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  8041. pring->sli.sli3.numRiocb +=
  8042. SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  8043. pring->sli.sli3.numCiocb +=
  8044. SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  8045. pring->sli.sli3.numRiocb +=
  8046. SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  8047. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8048. SLI3_IOCB_CMD_SIZE :
  8049. SLI2_IOCB_CMD_SIZE;
  8050. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8051. SLI3_IOCB_RSP_SIZE :
  8052. SLI2_IOCB_RSP_SIZE;
  8053. pring->iotag_ctr = 0;
  8054. pring->iotag_max =
  8055. (phba->cfg_hba_queue_depth * 2);
  8056. pring->fast_iotag = pring->iotag_max;
  8057. pring->num_mask = 0;
  8058. break;
  8059. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  8060. /* numCiocb and numRiocb are used in config_port */
  8061. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  8062. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  8063. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8064. SLI3_IOCB_CMD_SIZE :
  8065. SLI2_IOCB_CMD_SIZE;
  8066. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8067. SLI3_IOCB_RSP_SIZE :
  8068. SLI2_IOCB_RSP_SIZE;
  8069. pring->iotag_max = phba->cfg_hba_queue_depth;
  8070. pring->num_mask = 0;
  8071. break;
  8072. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  8073. /* numCiocb and numRiocb are used in config_port */
  8074. pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  8075. pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  8076. pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
  8077. SLI3_IOCB_CMD_SIZE :
  8078. SLI2_IOCB_CMD_SIZE;
  8079. pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
  8080. SLI3_IOCB_RSP_SIZE :
  8081. SLI2_IOCB_RSP_SIZE;
  8082. pring->fast_iotag = 0;
  8083. pring->iotag_ctr = 0;
  8084. pring->iotag_max = 4096;
  8085. pring->lpfc_sli_rcv_async_status =
  8086. lpfc_sli_async_event_handler;
  8087. pring->num_mask = LPFC_MAX_RING_MASK;
  8088. pring->prt[0].profile = 0; /* Mask 0 */
  8089. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  8090. pring->prt[0].type = FC_TYPE_ELS;
  8091. pring->prt[0].lpfc_sli_rcv_unsol_event =
  8092. lpfc_els_unsol_event;
  8093. pring->prt[1].profile = 0; /* Mask 1 */
  8094. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  8095. pring->prt[1].type = FC_TYPE_ELS;
  8096. pring->prt[1].lpfc_sli_rcv_unsol_event =
  8097. lpfc_els_unsol_event;
  8098. pring->prt[2].profile = 0; /* Mask 2 */
  8099. /* NameServer Inquiry */
  8100. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  8101. /* NameServer */
  8102. pring->prt[2].type = FC_TYPE_CT;
  8103. pring->prt[2].lpfc_sli_rcv_unsol_event =
  8104. lpfc_ct_unsol_event;
  8105. pring->prt[3].profile = 0; /* Mask 3 */
  8106. /* NameServer response */
  8107. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  8108. /* NameServer */
  8109. pring->prt[3].type = FC_TYPE_CT;
  8110. pring->prt[3].lpfc_sli_rcv_unsol_event =
  8111. lpfc_ct_unsol_event;
  8112. break;
  8113. }
  8114. totiocbsize += (pring->sli.sli3.numCiocb *
  8115. pring->sli.sli3.sizeCiocb) +
  8116. (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
  8117. }
  8118. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  8119. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  8120. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  8121. "SLI2 SLIM Data: x%x x%lx\n",
  8122. phba->brd_no, totiocbsize,
  8123. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8124. }
  8125. if (phba->cfg_multi_ring_support == 2)
  8126. lpfc_extra_ring_setup(phba);
  8127. return 0;
  8128. }
  8129. /**
  8130. * lpfc_sli_queue_setup - Queue initialization function
  8131. * @phba: Pointer to HBA context object.
  8132. *
  8133. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8134. * ring. This function also initializes ring indices of each ring.
  8135. * This function is called during the initialization of the SLI
  8136. * interface of an HBA.
  8137. * This function is called with no lock held and always returns
  8138. * 1.
  8139. **/
  8140. int
  8141. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8142. {
  8143. struct lpfc_sli *psli;
  8144. struct lpfc_sli_ring *pring;
  8145. int i;
  8146. psli = &phba->sli;
  8147. spin_lock_irq(&phba->hbalock);
  8148. INIT_LIST_HEAD(&psli->mboxq);
  8149. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8150. /* Initialize list headers for txq and txcmplq as double linked lists */
  8151. for (i = 0; i < psli->num_rings; i++) {
  8152. pring = &psli->ring[i];
  8153. pring->ringno = i;
  8154. pring->sli.sli3.next_cmdidx = 0;
  8155. pring->sli.sli3.local_getidx = 0;
  8156. pring->sli.sli3.cmdidx = 0;
  8157. INIT_LIST_HEAD(&pring->txq);
  8158. INIT_LIST_HEAD(&pring->txcmplq);
  8159. INIT_LIST_HEAD(&pring->iocb_continueq);
  8160. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8161. INIT_LIST_HEAD(&pring->postbufq);
  8162. spin_lock_init(&pring->ring_lock);
  8163. }
  8164. spin_unlock_irq(&phba->hbalock);
  8165. return 1;
  8166. }
  8167. /**
  8168. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8169. * @phba: Pointer to HBA context object.
  8170. *
  8171. * This routine flushes the mailbox command subsystem. It will unconditionally
  8172. * flush all the mailbox commands in the three possible stages in the mailbox
  8173. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8174. * command; and completed mailbox command queue. It is caller's responsibility
  8175. * to make sure that the driver is in the proper state to flush the mailbox
  8176. * command sub-system. Namely, the posting of mailbox commands into the
  8177. * pending mailbox command queue from the various clients must be stopped;
  8178. * either the HBA is in a state that it will never works on the outstanding
  8179. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8180. * mailbox command has been completed.
  8181. **/
  8182. static void
  8183. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8184. {
  8185. LIST_HEAD(completions);
  8186. struct lpfc_sli *psli = &phba->sli;
  8187. LPFC_MBOXQ_t *pmb;
  8188. unsigned long iflag;
  8189. /* Flush all the mailbox commands in the mbox system */
  8190. spin_lock_irqsave(&phba->hbalock, iflag);
  8191. /* The pending mailbox command queue */
  8192. list_splice_init(&phba->sli.mboxq, &completions);
  8193. /* The outstanding active mailbox command */
  8194. if (psli->mbox_active) {
  8195. list_add_tail(&psli->mbox_active->list, &completions);
  8196. psli->mbox_active = NULL;
  8197. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8198. }
  8199. /* The completed mailbox command queue */
  8200. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8201. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8202. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8203. while (!list_empty(&completions)) {
  8204. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8205. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8206. if (pmb->mbox_cmpl)
  8207. pmb->mbox_cmpl(phba, pmb);
  8208. }
  8209. }
  8210. /**
  8211. * lpfc_sli_host_down - Vport cleanup function
  8212. * @vport: Pointer to virtual port object.
  8213. *
  8214. * lpfc_sli_host_down is called to clean up the resources
  8215. * associated with a vport before destroying virtual
  8216. * port data structures.
  8217. * This function does following operations:
  8218. * - Free discovery resources associated with this virtual
  8219. * port.
  8220. * - Free iocbs associated with this virtual port in
  8221. * the txq.
  8222. * - Send abort for all iocb commands associated with this
  8223. * vport in txcmplq.
  8224. *
  8225. * This function is called with no lock held and always returns 1.
  8226. **/
  8227. int
  8228. lpfc_sli_host_down(struct lpfc_vport *vport)
  8229. {
  8230. LIST_HEAD(completions);
  8231. struct lpfc_hba *phba = vport->phba;
  8232. struct lpfc_sli *psli = &phba->sli;
  8233. struct lpfc_sli_ring *pring;
  8234. struct lpfc_iocbq *iocb, *next_iocb;
  8235. int i;
  8236. unsigned long flags = 0;
  8237. uint16_t prev_pring_flag;
  8238. lpfc_cleanup_discovery_resources(vport);
  8239. spin_lock_irqsave(&phba->hbalock, flags);
  8240. for (i = 0; i < psli->num_rings; i++) {
  8241. pring = &psli->ring[i];
  8242. prev_pring_flag = pring->flag;
  8243. /* Only slow rings */
  8244. if (pring->ringno == LPFC_ELS_RING) {
  8245. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8246. /* Set the lpfc data pending flag */
  8247. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8248. }
  8249. /*
  8250. * Error everything on the txq since these iocbs have not been
  8251. * given to the FW yet.
  8252. */
  8253. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8254. if (iocb->vport != vport)
  8255. continue;
  8256. list_move_tail(&iocb->list, &completions);
  8257. pring->txq_cnt--;
  8258. }
  8259. /* Next issue ABTS for everything on the txcmplq */
  8260. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8261. list) {
  8262. if (iocb->vport != vport)
  8263. continue;
  8264. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8265. }
  8266. pring->flag = prev_pring_flag;
  8267. }
  8268. spin_unlock_irqrestore(&phba->hbalock, flags);
  8269. /* Cancel all the IOCBs from the completions list */
  8270. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8271. IOERR_SLI_DOWN);
  8272. return 1;
  8273. }
  8274. /**
  8275. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8276. * @phba: Pointer to HBA context object.
  8277. *
  8278. * This function cleans up all iocb, buffers, mailbox commands
  8279. * while shutting down the HBA. This function is called with no
  8280. * lock held and always returns 1.
  8281. * This function does the following to cleanup driver resources:
  8282. * - Free discovery resources for each virtual port
  8283. * - Cleanup any pending fabric iocbs
  8284. * - Iterate through the iocb txq and free each entry
  8285. * in the list.
  8286. * - Free up any buffer posted to the HBA
  8287. * - Free mailbox commands in the mailbox queue.
  8288. **/
  8289. int
  8290. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8291. {
  8292. LIST_HEAD(completions);
  8293. struct lpfc_sli *psli = &phba->sli;
  8294. struct lpfc_sli_ring *pring;
  8295. struct lpfc_dmabuf *buf_ptr;
  8296. unsigned long flags = 0;
  8297. int i;
  8298. /* Shutdown the mailbox command sub-system */
  8299. lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
  8300. lpfc_hba_down_prep(phba);
  8301. lpfc_fabric_abort_hba(phba);
  8302. spin_lock_irqsave(&phba->hbalock, flags);
  8303. for (i = 0; i < psli->num_rings; i++) {
  8304. pring = &psli->ring[i];
  8305. /* Only slow rings */
  8306. if (pring->ringno == LPFC_ELS_RING) {
  8307. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8308. /* Set the lpfc data pending flag */
  8309. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8310. }
  8311. /*
  8312. * Error everything on the txq since these iocbs have not been
  8313. * given to the FW yet.
  8314. */
  8315. list_splice_init(&pring->txq, &completions);
  8316. pring->txq_cnt = 0;
  8317. }
  8318. spin_unlock_irqrestore(&phba->hbalock, flags);
  8319. /* Cancel all the IOCBs from the completions list */
  8320. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8321. IOERR_SLI_DOWN);
  8322. spin_lock_irqsave(&phba->hbalock, flags);
  8323. list_splice_init(&phba->elsbuf, &completions);
  8324. phba->elsbuf_cnt = 0;
  8325. phba->elsbuf_prev_cnt = 0;
  8326. spin_unlock_irqrestore(&phba->hbalock, flags);
  8327. while (!list_empty(&completions)) {
  8328. list_remove_head(&completions, buf_ptr,
  8329. struct lpfc_dmabuf, list);
  8330. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8331. kfree(buf_ptr);
  8332. }
  8333. /* Return any active mbox cmds */
  8334. del_timer_sync(&psli->mbox_tmo);
  8335. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8336. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8337. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8338. return 1;
  8339. }
  8340. /**
  8341. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8342. * @srcp: Source memory pointer.
  8343. * @destp: Destination memory pointer.
  8344. * @cnt: Number of words required to be copied.
  8345. *
  8346. * This function is used for copying data between driver memory
  8347. * and the SLI memory. This function also changes the endianness
  8348. * of each word if native endianness is different from SLI
  8349. * endianness. This function can be called with or without
  8350. * lock.
  8351. **/
  8352. void
  8353. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8354. {
  8355. uint32_t *src = srcp;
  8356. uint32_t *dest = destp;
  8357. uint32_t ldata;
  8358. int i;
  8359. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8360. ldata = *src;
  8361. ldata = le32_to_cpu(ldata);
  8362. *dest = ldata;
  8363. src++;
  8364. dest++;
  8365. }
  8366. }
  8367. /**
  8368. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8369. * @srcp: Source memory pointer.
  8370. * @destp: Destination memory pointer.
  8371. * @cnt: Number of words required to be copied.
  8372. *
  8373. * This function is used for copying data between a data structure
  8374. * with big endian representation to local endianness.
  8375. * This function can be called with or without lock.
  8376. **/
  8377. void
  8378. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8379. {
  8380. uint32_t *src = srcp;
  8381. uint32_t *dest = destp;
  8382. uint32_t ldata;
  8383. int i;
  8384. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8385. ldata = *src;
  8386. ldata = be32_to_cpu(ldata);
  8387. *dest = ldata;
  8388. src++;
  8389. dest++;
  8390. }
  8391. }
  8392. /**
  8393. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8394. * @phba: Pointer to HBA context object.
  8395. * @pring: Pointer to driver SLI ring object.
  8396. * @mp: Pointer to driver buffer object.
  8397. *
  8398. * This function is called with no lock held.
  8399. * It always return zero after adding the buffer to the postbufq
  8400. * buffer list.
  8401. **/
  8402. int
  8403. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8404. struct lpfc_dmabuf *mp)
  8405. {
  8406. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8407. later */
  8408. spin_lock_irq(&phba->hbalock);
  8409. list_add_tail(&mp->list, &pring->postbufq);
  8410. pring->postbufq_cnt++;
  8411. spin_unlock_irq(&phba->hbalock);
  8412. return 0;
  8413. }
  8414. /**
  8415. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8416. * @phba: Pointer to HBA context object.
  8417. *
  8418. * When HBQ is enabled, buffers are searched based on tags. This function
  8419. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8420. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8421. * does not conflict with tags of buffer posted for unsolicited events.
  8422. * The function returns the allocated tag. The function is called with
  8423. * no locks held.
  8424. **/
  8425. uint32_t
  8426. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8427. {
  8428. spin_lock_irq(&phba->hbalock);
  8429. phba->buffer_tag_count++;
  8430. /*
  8431. * Always set the QUE_BUFTAG_BIT to distiguish between
  8432. * a tag assigned by HBQ.
  8433. */
  8434. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8435. spin_unlock_irq(&phba->hbalock);
  8436. return phba->buffer_tag_count;
  8437. }
  8438. /**
  8439. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8440. * @phba: Pointer to HBA context object.
  8441. * @pring: Pointer to driver SLI ring object.
  8442. * @tag: Buffer tag.
  8443. *
  8444. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8445. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8446. * iocb is posted to the response ring with the tag of the buffer.
  8447. * This function searches the pring->postbufq list using the tag
  8448. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8449. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8450. * buffer is returned to the caller else NULL is returned.
  8451. * This function is called with no lock held.
  8452. **/
  8453. struct lpfc_dmabuf *
  8454. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8455. uint32_t tag)
  8456. {
  8457. struct lpfc_dmabuf *mp, *next_mp;
  8458. struct list_head *slp = &pring->postbufq;
  8459. /* Search postbufq, from the beginning, looking for a match on tag */
  8460. spin_lock_irq(&phba->hbalock);
  8461. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8462. if (mp->buffer_tag == tag) {
  8463. list_del_init(&mp->list);
  8464. pring->postbufq_cnt--;
  8465. spin_unlock_irq(&phba->hbalock);
  8466. return mp;
  8467. }
  8468. }
  8469. spin_unlock_irq(&phba->hbalock);
  8470. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8471. "0402 Cannot find virtual addr for buffer tag on "
  8472. "ring %d Data x%lx x%p x%p x%x\n",
  8473. pring->ringno, (unsigned long) tag,
  8474. slp->next, slp->prev, pring->postbufq_cnt);
  8475. return NULL;
  8476. }
  8477. /**
  8478. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8479. * @phba: Pointer to HBA context object.
  8480. * @pring: Pointer to driver SLI ring object.
  8481. * @phys: DMA address of the buffer.
  8482. *
  8483. * This function searches the buffer list using the dma_address
  8484. * of unsolicited event to find the driver's lpfc_dmabuf object
  8485. * corresponding to the dma_address. The function returns the
  8486. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8487. * This function is called by the ct and els unsolicited event
  8488. * handlers to get the buffer associated with the unsolicited
  8489. * event.
  8490. *
  8491. * This function is called with no lock held.
  8492. **/
  8493. struct lpfc_dmabuf *
  8494. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8495. dma_addr_t phys)
  8496. {
  8497. struct lpfc_dmabuf *mp, *next_mp;
  8498. struct list_head *slp = &pring->postbufq;
  8499. /* Search postbufq, from the beginning, looking for a match on phys */
  8500. spin_lock_irq(&phba->hbalock);
  8501. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8502. if (mp->phys == phys) {
  8503. list_del_init(&mp->list);
  8504. pring->postbufq_cnt--;
  8505. spin_unlock_irq(&phba->hbalock);
  8506. return mp;
  8507. }
  8508. }
  8509. spin_unlock_irq(&phba->hbalock);
  8510. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8511. "0410 Cannot find virtual addr for mapped buf on "
  8512. "ring %d Data x%llx x%p x%p x%x\n",
  8513. pring->ringno, (unsigned long long)phys,
  8514. slp->next, slp->prev, pring->postbufq_cnt);
  8515. return NULL;
  8516. }
  8517. /**
  8518. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8519. * @phba: Pointer to HBA context object.
  8520. * @cmdiocb: Pointer to driver command iocb object.
  8521. * @rspiocb: Pointer to driver response iocb object.
  8522. *
  8523. * This function is the completion handler for the abort iocbs for
  8524. * ELS commands. This function is called from the ELS ring event
  8525. * handler with no lock held. This function frees memory resources
  8526. * associated with the abort iocb.
  8527. **/
  8528. static void
  8529. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8530. struct lpfc_iocbq *rspiocb)
  8531. {
  8532. IOCB_t *irsp = &rspiocb->iocb;
  8533. uint16_t abort_iotag, abort_context;
  8534. struct lpfc_iocbq *abort_iocb = NULL;
  8535. if (irsp->ulpStatus) {
  8536. /*
  8537. * Assume that the port already completed and returned, or
  8538. * will return the iocb. Just Log the message.
  8539. */
  8540. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8541. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8542. spin_lock_irq(&phba->hbalock);
  8543. if (phba->sli_rev < LPFC_SLI_REV4) {
  8544. if (abort_iotag != 0 &&
  8545. abort_iotag <= phba->sli.last_iotag)
  8546. abort_iocb =
  8547. phba->sli.iocbq_lookup[abort_iotag];
  8548. } else
  8549. /* For sli4 the abort_tag is the XRI,
  8550. * so the abort routine puts the iotag of the iocb
  8551. * being aborted in the context field of the abort
  8552. * IOCB.
  8553. */
  8554. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8555. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8556. "0327 Cannot abort els iocb %p "
  8557. "with tag %x context %x, abort status %x, "
  8558. "abort code %x\n",
  8559. abort_iocb, abort_iotag, abort_context,
  8560. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8561. spin_unlock_irq(&phba->hbalock);
  8562. }
  8563. lpfc_sli_release_iocbq(phba, cmdiocb);
  8564. return;
  8565. }
  8566. /**
  8567. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8568. * @phba: Pointer to HBA context object.
  8569. * @cmdiocb: Pointer to driver command iocb object.
  8570. * @rspiocb: Pointer to driver response iocb object.
  8571. *
  8572. * The function is called from SLI ring event handler with no
  8573. * lock held. This function is the completion handler for ELS commands
  8574. * which are aborted. The function frees memory resources used for
  8575. * the aborted ELS commands.
  8576. **/
  8577. static void
  8578. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8579. struct lpfc_iocbq *rspiocb)
  8580. {
  8581. IOCB_t *irsp = &rspiocb->iocb;
  8582. /* ELS cmd tag <ulpIoTag> completes */
  8583. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8584. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8585. "x%x x%x x%x\n",
  8586. irsp->ulpIoTag, irsp->ulpStatus,
  8587. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8588. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8589. lpfc_ct_free_iocb(phba, cmdiocb);
  8590. else
  8591. lpfc_els_free_iocb(phba, cmdiocb);
  8592. return;
  8593. }
  8594. /**
  8595. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8596. * @phba: Pointer to HBA context object.
  8597. * @pring: Pointer to driver SLI ring object.
  8598. * @cmdiocb: Pointer to driver command iocb object.
  8599. *
  8600. * This function issues an abort iocb for the provided command iocb down to
  8601. * the port. Other than the case the outstanding command iocb is an abort
  8602. * request, this function issues abort out unconditionally. This function is
  8603. * called with hbalock held. The function returns 0 when it fails due to
  8604. * memory allocation failure or when the command iocb is an abort request.
  8605. **/
  8606. static int
  8607. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8608. struct lpfc_iocbq *cmdiocb)
  8609. {
  8610. struct lpfc_vport *vport = cmdiocb->vport;
  8611. struct lpfc_iocbq *abtsiocbp;
  8612. IOCB_t *icmd = NULL;
  8613. IOCB_t *iabt = NULL;
  8614. int retval;
  8615. unsigned long iflags;
  8616. /*
  8617. * There are certain command types we don't want to abort. And we
  8618. * don't want to abort commands that are already in the process of
  8619. * being aborted.
  8620. */
  8621. icmd = &cmdiocb->iocb;
  8622. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8623. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8624. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8625. return 0;
  8626. /* issue ABTS for this IOCB based on iotag */
  8627. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8628. if (abtsiocbp == NULL)
  8629. return 0;
  8630. /* This signals the response to set the correct status
  8631. * before calling the completion handler
  8632. */
  8633. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8634. iabt = &abtsiocbp->iocb;
  8635. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8636. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8637. if (phba->sli_rev == LPFC_SLI_REV4) {
  8638. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8639. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8640. }
  8641. else
  8642. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8643. iabt->ulpLe = 1;
  8644. iabt->ulpClass = icmd->ulpClass;
  8645. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8646. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8647. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8648. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8649. if (phba->link_state >= LPFC_LINK_UP)
  8650. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8651. else
  8652. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8653. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8654. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8655. "0339 Abort xri x%x, original iotag x%x, "
  8656. "abort cmd iotag x%x\n",
  8657. iabt->un.acxri.abortIoTag,
  8658. iabt->un.acxri.abortContextTag,
  8659. abtsiocbp->iotag);
  8660. if (phba->sli_rev == LPFC_SLI_REV4) {
  8661. /* Note: both hbalock and ring_lock need to be set here */
  8662. spin_lock_irqsave(&pring->ring_lock, iflags);
  8663. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8664. abtsiocbp, 0);
  8665. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  8666. } else {
  8667. retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
  8668. abtsiocbp, 0);
  8669. }
  8670. if (retval)
  8671. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8672. /*
  8673. * Caller to this routine should check for IOCB_ERROR
  8674. * and handle it properly. This routine no longer removes
  8675. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8676. */
  8677. return retval;
  8678. }
  8679. /**
  8680. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8681. * @phba: Pointer to HBA context object.
  8682. * @pring: Pointer to driver SLI ring object.
  8683. * @cmdiocb: Pointer to driver command iocb object.
  8684. *
  8685. * This function issues an abort iocb for the provided command iocb. In case
  8686. * of unloading, the abort iocb will not be issued to commands on the ELS
  8687. * ring. Instead, the callback function shall be changed to those commands
  8688. * so that nothing happens when them finishes. This function is called with
  8689. * hbalock held. The function returns 0 when the command iocb is an abort
  8690. * request.
  8691. **/
  8692. int
  8693. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8694. struct lpfc_iocbq *cmdiocb)
  8695. {
  8696. struct lpfc_vport *vport = cmdiocb->vport;
  8697. int retval = IOCB_ERROR;
  8698. IOCB_t *icmd = NULL;
  8699. /*
  8700. * There are certain command types we don't want to abort. And we
  8701. * don't want to abort commands that are already in the process of
  8702. * being aborted.
  8703. */
  8704. icmd = &cmdiocb->iocb;
  8705. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8706. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8707. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8708. return 0;
  8709. /*
  8710. * If we're unloading, don't abort iocb on the ELS ring, but change
  8711. * the callback so that nothing happens when it finishes.
  8712. */
  8713. if ((vport->load_flag & FC_UNLOADING) &&
  8714. (pring->ringno == LPFC_ELS_RING)) {
  8715. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8716. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8717. else
  8718. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8719. goto abort_iotag_exit;
  8720. }
  8721. /* Now, we try to issue the abort to the cmdiocb out */
  8722. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8723. abort_iotag_exit:
  8724. /*
  8725. * Caller to this routine should check for IOCB_ERROR
  8726. * and handle it properly. This routine no longer removes
  8727. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8728. */
  8729. return retval;
  8730. }
  8731. /**
  8732. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8733. * @phba: Pointer to HBA context object.
  8734. * @pring: Pointer to driver SLI ring object.
  8735. *
  8736. * This function aborts all iocbs in the given ring and frees all the iocb
  8737. * objects in txq. This function issues abort iocbs unconditionally for all
  8738. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8739. * to complete before the return of this function. The caller is not required
  8740. * to hold any locks.
  8741. **/
  8742. static void
  8743. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8744. {
  8745. LIST_HEAD(completions);
  8746. struct lpfc_iocbq *iocb, *next_iocb;
  8747. if (pring->ringno == LPFC_ELS_RING)
  8748. lpfc_fabric_abort_hba(phba);
  8749. spin_lock_irq(&phba->hbalock);
  8750. /* Take off all the iocbs on txq for cancelling */
  8751. list_splice_init(&pring->txq, &completions);
  8752. pring->txq_cnt = 0;
  8753. /* Next issue ABTS for everything on the txcmplq */
  8754. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8755. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8756. spin_unlock_irq(&phba->hbalock);
  8757. /* Cancel all the IOCBs from the completions list */
  8758. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8759. IOERR_SLI_ABORTED);
  8760. }
  8761. /**
  8762. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8763. * @phba: pointer to lpfc HBA data structure.
  8764. *
  8765. * This routine will abort all pending and outstanding iocbs to an HBA.
  8766. **/
  8767. void
  8768. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8769. {
  8770. struct lpfc_sli *psli = &phba->sli;
  8771. struct lpfc_sli_ring *pring;
  8772. int i;
  8773. for (i = 0; i < psli->num_rings; i++) {
  8774. pring = &psli->ring[i];
  8775. lpfc_sli_iocb_ring_abort(phba, pring);
  8776. }
  8777. }
  8778. /**
  8779. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8780. * @iocbq: Pointer to driver iocb object.
  8781. * @vport: Pointer to driver virtual port object.
  8782. * @tgt_id: SCSI ID of the target.
  8783. * @lun_id: LUN ID of the scsi device.
  8784. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8785. *
  8786. * This function acts as an iocb filter for functions which abort or count
  8787. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8788. * 0 if the filtering criteria is met for the given iocb and will return
  8789. * 1 if the filtering criteria is not met.
  8790. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8791. * given iocb is for the SCSI device specified by vport, tgt_id and
  8792. * lun_id parameter.
  8793. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8794. * given iocb is for the SCSI target specified by vport and tgt_id
  8795. * parameters.
  8796. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8797. * given iocb is for the SCSI host associated with the given vport.
  8798. * This function is called with no locks held.
  8799. **/
  8800. static int
  8801. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8802. uint16_t tgt_id, uint64_t lun_id,
  8803. lpfc_ctx_cmd ctx_cmd)
  8804. {
  8805. struct lpfc_scsi_buf *lpfc_cmd;
  8806. int rc = 1;
  8807. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8808. return rc;
  8809. if (iocbq->vport != vport)
  8810. return rc;
  8811. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8812. if (lpfc_cmd->pCmd == NULL)
  8813. return rc;
  8814. switch (ctx_cmd) {
  8815. case LPFC_CTX_LUN:
  8816. if ((lpfc_cmd->rdata->pnode) &&
  8817. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8818. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8819. rc = 0;
  8820. break;
  8821. case LPFC_CTX_TGT:
  8822. if ((lpfc_cmd->rdata->pnode) &&
  8823. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8824. rc = 0;
  8825. break;
  8826. case LPFC_CTX_HOST:
  8827. rc = 0;
  8828. break;
  8829. default:
  8830. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8831. __func__, ctx_cmd);
  8832. break;
  8833. }
  8834. return rc;
  8835. }
  8836. /**
  8837. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8838. * @vport: Pointer to virtual port.
  8839. * @tgt_id: SCSI ID of the target.
  8840. * @lun_id: LUN ID of the scsi device.
  8841. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8842. *
  8843. * This function returns number of FCP commands pending for the vport.
  8844. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8845. * commands pending on the vport associated with SCSI device specified
  8846. * by tgt_id and lun_id parameters.
  8847. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8848. * commands pending on the vport associated with SCSI target specified
  8849. * by tgt_id parameter.
  8850. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8851. * commands pending on the vport.
  8852. * This function returns the number of iocbs which satisfy the filter.
  8853. * This function is called without any lock held.
  8854. **/
  8855. int
  8856. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8857. lpfc_ctx_cmd ctx_cmd)
  8858. {
  8859. struct lpfc_hba *phba = vport->phba;
  8860. struct lpfc_iocbq *iocbq;
  8861. int sum, i;
  8862. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8863. iocbq = phba->sli.iocbq_lookup[i];
  8864. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8865. ctx_cmd) == 0)
  8866. sum++;
  8867. }
  8868. return sum;
  8869. }
  8870. /**
  8871. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8872. * @phba: Pointer to HBA context object
  8873. * @cmdiocb: Pointer to command iocb object.
  8874. * @rspiocb: Pointer to response iocb object.
  8875. *
  8876. * This function is called when an aborted FCP iocb completes. This
  8877. * function is called by the ring event handler with no lock held.
  8878. * This function frees the iocb.
  8879. **/
  8880. void
  8881. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8882. struct lpfc_iocbq *rspiocb)
  8883. {
  8884. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8885. "3096 ABORT_XRI_CN completing on xri x%x "
  8886. "original iotag x%x, abort cmd iotag x%x "
  8887. "status 0x%x, reason 0x%x\n",
  8888. cmdiocb->iocb.un.acxri.abortContextTag,
  8889. cmdiocb->iocb.un.acxri.abortIoTag,
  8890. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8891. rspiocb->iocb.un.ulpWord[4]);
  8892. lpfc_sli_release_iocbq(phba, cmdiocb);
  8893. return;
  8894. }
  8895. /**
  8896. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8897. * @vport: Pointer to virtual port.
  8898. * @pring: Pointer to driver SLI ring object.
  8899. * @tgt_id: SCSI ID of the target.
  8900. * @lun_id: LUN ID of the scsi device.
  8901. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8902. *
  8903. * This function sends an abort command for every SCSI command
  8904. * associated with the given virtual port pending on the ring
  8905. * filtered by lpfc_sli_validate_fcp_iocb function.
  8906. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8907. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8908. * parameters
  8909. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8910. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8911. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8912. * FCP iocbs associated with virtual port.
  8913. * This function returns number of iocbs it failed to abort.
  8914. * This function is called with no locks held.
  8915. **/
  8916. int
  8917. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8918. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8919. {
  8920. struct lpfc_hba *phba = vport->phba;
  8921. struct lpfc_iocbq *iocbq;
  8922. struct lpfc_iocbq *abtsiocb;
  8923. IOCB_t *cmd = NULL;
  8924. int errcnt = 0, ret_val = 0;
  8925. int i;
  8926. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8927. iocbq = phba->sli.iocbq_lookup[i];
  8928. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8929. abort_cmd) != 0)
  8930. continue;
  8931. /* issue ABTS for this IOCB based on iotag */
  8932. abtsiocb = lpfc_sli_get_iocbq(phba);
  8933. if (abtsiocb == NULL) {
  8934. errcnt++;
  8935. continue;
  8936. }
  8937. cmd = &iocbq->iocb;
  8938. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8939. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8940. if (phba->sli_rev == LPFC_SLI_REV4)
  8941. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8942. else
  8943. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8944. abtsiocb->iocb.ulpLe = 1;
  8945. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8946. abtsiocb->vport = phba->pport;
  8947. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8948. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8949. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8950. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8951. if (lpfc_is_link_up(phba))
  8952. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8953. else
  8954. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8955. /* Setup callback routine and issue the command. */
  8956. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8957. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8958. abtsiocb, 0);
  8959. if (ret_val == IOCB_ERROR) {
  8960. lpfc_sli_release_iocbq(phba, abtsiocb);
  8961. errcnt++;
  8962. continue;
  8963. }
  8964. }
  8965. return errcnt;
  8966. }
  8967. /**
  8968. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8969. * @phba: Pointer to HBA context object.
  8970. * @cmdiocbq: Pointer to command iocb.
  8971. * @rspiocbq: Pointer to response iocb.
  8972. *
  8973. * This function is the completion handler for iocbs issued using
  8974. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8975. * ring event handler function without any lock held. This function
  8976. * can be called from both worker thread context and interrupt
  8977. * context. This function also can be called from other thread which
  8978. * cleans up the SLI layer objects.
  8979. * This function copy the contents of the response iocb to the
  8980. * response iocb memory object provided by the caller of
  8981. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8982. * sleeps for the iocb completion.
  8983. **/
  8984. static void
  8985. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8986. struct lpfc_iocbq *cmdiocbq,
  8987. struct lpfc_iocbq *rspiocbq)
  8988. {
  8989. wait_queue_head_t *pdone_q;
  8990. unsigned long iflags;
  8991. struct lpfc_scsi_buf *lpfc_cmd;
  8992. spin_lock_irqsave(&phba->hbalock, iflags);
  8993. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8994. if (cmdiocbq->context2 && rspiocbq)
  8995. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8996. &rspiocbq->iocb, sizeof(IOCB_t));
  8997. /* Set the exchange busy flag for task management commands */
  8998. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8999. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  9000. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  9001. cur_iocbq);
  9002. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  9003. }
  9004. pdone_q = cmdiocbq->context_un.wait_queue;
  9005. if (pdone_q)
  9006. wake_up(pdone_q);
  9007. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9008. return;
  9009. }
  9010. /**
  9011. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  9012. * @phba: Pointer to HBA context object..
  9013. * @piocbq: Pointer to command iocb.
  9014. * @flag: Flag to test.
  9015. *
  9016. * This routine grabs the hbalock and then test the iocb_flag to
  9017. * see if the passed in flag is set.
  9018. * Returns:
  9019. * 1 if flag is set.
  9020. * 0 if flag is not set.
  9021. **/
  9022. static int
  9023. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  9024. struct lpfc_iocbq *piocbq, uint32_t flag)
  9025. {
  9026. unsigned long iflags;
  9027. int ret;
  9028. spin_lock_irqsave(&phba->hbalock, iflags);
  9029. ret = piocbq->iocb_flag & flag;
  9030. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9031. return ret;
  9032. }
  9033. /**
  9034. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  9035. * @phba: Pointer to HBA context object..
  9036. * @pring: Pointer to sli ring.
  9037. * @piocb: Pointer to command iocb.
  9038. * @prspiocbq: Pointer to response iocb.
  9039. * @timeout: Timeout in number of seconds.
  9040. *
  9041. * This function issues the iocb to firmware and waits for the
  9042. * iocb to complete. If the iocb command is not
  9043. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  9044. * Caller should not free the iocb resources if this function
  9045. * returns IOCB_TIMEDOUT.
  9046. * The function waits for the iocb completion using an
  9047. * non-interruptible wait.
  9048. * This function will sleep while waiting for iocb completion.
  9049. * So, this function should not be called from any context which
  9050. * does not allow sleeping. Due to the same reason, this function
  9051. * cannot be called with interrupt disabled.
  9052. * This function assumes that the iocb completions occur while
  9053. * this function sleep. So, this function cannot be called from
  9054. * the thread which process iocb completion for this ring.
  9055. * This function clears the iocb_flag of the iocb object before
  9056. * issuing the iocb and the iocb completion handler sets this
  9057. * flag and wakes this thread when the iocb completes.
  9058. * The contents of the response iocb will be copied to prspiocbq
  9059. * by the completion handler when the command completes.
  9060. * This function returns IOCB_SUCCESS when success.
  9061. * This function is called with no lock held.
  9062. **/
  9063. int
  9064. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  9065. uint32_t ring_number,
  9066. struct lpfc_iocbq *piocb,
  9067. struct lpfc_iocbq *prspiocbq,
  9068. uint32_t timeout)
  9069. {
  9070. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9071. long timeleft, timeout_req = 0;
  9072. int retval = IOCB_SUCCESS;
  9073. uint32_t creg_val;
  9074. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9075. /*
  9076. * If the caller has provided a response iocbq buffer, then context2
  9077. * is NULL or its an error.
  9078. */
  9079. if (prspiocbq) {
  9080. if (piocb->context2)
  9081. return IOCB_ERROR;
  9082. piocb->context2 = prspiocbq;
  9083. }
  9084. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  9085. piocb->context_un.wait_queue = &done_q;
  9086. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  9087. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9088. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9089. return IOCB_ERROR;
  9090. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  9091. writel(creg_val, phba->HCregaddr);
  9092. readl(phba->HCregaddr); /* flush */
  9093. }
  9094. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  9095. SLI_IOCB_RET_IOCB);
  9096. if (retval == IOCB_SUCCESS) {
  9097. timeout_req = timeout * HZ;
  9098. timeleft = wait_event_timeout(done_q,
  9099. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  9100. timeout_req);
  9101. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  9102. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9103. "0331 IOCB wake signaled\n");
  9104. } else if (timeleft == 0) {
  9105. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9106. "0338 IOCB wait timeout error - no "
  9107. "wake response Data x%x\n", timeout);
  9108. retval = IOCB_TIMEDOUT;
  9109. } else {
  9110. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9111. "0330 IOCB wake NOT set, "
  9112. "Data x%x x%lx\n",
  9113. timeout, (timeleft / jiffies));
  9114. retval = IOCB_TIMEDOUT;
  9115. }
  9116. } else if (retval == IOCB_BUSY) {
  9117. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9118. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  9119. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  9120. return retval;
  9121. } else {
  9122. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9123. "0332 IOCB wait issue failed, Data x%x\n",
  9124. retval);
  9125. retval = IOCB_ERROR;
  9126. }
  9127. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  9128. if (lpfc_readl(phba->HCregaddr, &creg_val))
  9129. return IOCB_ERROR;
  9130. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  9131. writel(creg_val, phba->HCregaddr);
  9132. readl(phba->HCregaddr); /* flush */
  9133. }
  9134. if (prspiocbq)
  9135. piocb->context2 = NULL;
  9136. piocb->context_un.wait_queue = NULL;
  9137. piocb->iocb_cmpl = NULL;
  9138. return retval;
  9139. }
  9140. /**
  9141. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9142. * @phba: Pointer to HBA context object.
  9143. * @pmboxq: Pointer to driver mailbox object.
  9144. * @timeout: Timeout in number of seconds.
  9145. *
  9146. * This function issues the mailbox to firmware and waits for the
  9147. * mailbox command to complete. If the mailbox command is not
  9148. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9149. * The function waits for the mailbox completion using an
  9150. * interruptible wait. If the thread is woken up due to a
  9151. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9152. * should not free the mailbox resources, if this function returns
  9153. * MBX_TIMEOUT.
  9154. * This function will sleep while waiting for mailbox completion.
  9155. * So, this function should not be called from any context which
  9156. * does not allow sleeping. Due to the same reason, this function
  9157. * cannot be called with interrupt disabled.
  9158. * This function assumes that the mailbox completion occurs while
  9159. * this function sleep. So, this function cannot be called from
  9160. * the worker thread which processes mailbox completion.
  9161. * This function is called in the context of HBA management
  9162. * applications.
  9163. * This function returns MBX_SUCCESS when successful.
  9164. * This function is called with no lock held.
  9165. **/
  9166. int
  9167. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9168. uint32_t timeout)
  9169. {
  9170. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9171. int retval;
  9172. unsigned long flag;
  9173. /* The caller must leave context1 empty. */
  9174. if (pmboxq->context1)
  9175. return MBX_NOT_FINISHED;
  9176. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9177. /* setup wake call as IOCB callback */
  9178. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9179. /* setup context field to pass wait_queue pointer to wake function */
  9180. pmboxq->context1 = &done_q;
  9181. /* now issue the command */
  9182. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9183. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9184. wait_event_interruptible_timeout(done_q,
  9185. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9186. timeout * HZ);
  9187. spin_lock_irqsave(&phba->hbalock, flag);
  9188. pmboxq->context1 = NULL;
  9189. /*
  9190. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9191. * else do not free the resources.
  9192. */
  9193. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9194. retval = MBX_SUCCESS;
  9195. lpfc_sli4_swap_str(phba, pmboxq);
  9196. } else {
  9197. retval = MBX_TIMEOUT;
  9198. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9199. }
  9200. spin_unlock_irqrestore(&phba->hbalock, flag);
  9201. }
  9202. return retval;
  9203. }
  9204. /**
  9205. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9206. * @phba: Pointer to HBA context.
  9207. *
  9208. * This function is called to shutdown the driver's mailbox sub-system.
  9209. * It first marks the mailbox sub-system is in a block state to prevent
  9210. * the asynchronous mailbox command from issued off the pending mailbox
  9211. * command queue. If the mailbox command sub-system shutdown is due to
  9212. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9213. * the mailbox sub-system flush routine to forcefully bring down the
  9214. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9215. * as with offline or HBA function reset), this routine will wait for the
  9216. * outstanding mailbox command to complete before invoking the mailbox
  9217. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9218. **/
  9219. void
  9220. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
  9221. {
  9222. struct lpfc_sli *psli = &phba->sli;
  9223. unsigned long timeout;
  9224. if (mbx_action == LPFC_MBX_NO_WAIT) {
  9225. /* delay 100ms for port state */
  9226. msleep(100);
  9227. lpfc_sli_mbox_sys_flush(phba);
  9228. return;
  9229. }
  9230. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9231. spin_lock_irq(&phba->hbalock);
  9232. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9233. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9234. /* Determine how long we might wait for the active mailbox
  9235. * command to be gracefully completed by firmware.
  9236. */
  9237. if (phba->sli.mbox_active)
  9238. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9239. phba->sli.mbox_active) *
  9240. 1000) + jiffies;
  9241. spin_unlock_irq(&phba->hbalock);
  9242. while (phba->sli.mbox_active) {
  9243. /* Check active mailbox complete status every 2ms */
  9244. msleep(2);
  9245. if (time_after(jiffies, timeout))
  9246. /* Timeout, let the mailbox flush routine to
  9247. * forcefully release active mailbox command
  9248. */
  9249. break;
  9250. }
  9251. } else
  9252. spin_unlock_irq(&phba->hbalock);
  9253. lpfc_sli_mbox_sys_flush(phba);
  9254. }
  9255. /**
  9256. * lpfc_sli_eratt_read - read sli-3 error attention events
  9257. * @phba: Pointer to HBA context.
  9258. *
  9259. * This function is called to read the SLI3 device error attention registers
  9260. * for possible error attention events. The caller must hold the hostlock
  9261. * with spin_lock_irq().
  9262. *
  9263. * This function returns 1 when there is Error Attention in the Host Attention
  9264. * Register and returns 0 otherwise.
  9265. **/
  9266. static int
  9267. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9268. {
  9269. uint32_t ha_copy;
  9270. /* Read chip Host Attention (HA) register */
  9271. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9272. goto unplug_err;
  9273. if (ha_copy & HA_ERATT) {
  9274. /* Read host status register to retrieve error event */
  9275. if (lpfc_sli_read_hs(phba))
  9276. goto unplug_err;
  9277. /* Check if there is a deferred error condition is active */
  9278. if ((HS_FFER1 & phba->work_hs) &&
  9279. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9280. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9281. phba->hba_flag |= DEFER_ERATT;
  9282. /* Clear all interrupt enable conditions */
  9283. writel(0, phba->HCregaddr);
  9284. readl(phba->HCregaddr);
  9285. }
  9286. /* Set the driver HA work bitmap */
  9287. phba->work_ha |= HA_ERATT;
  9288. /* Indicate polling handles this ERATT */
  9289. phba->hba_flag |= HBA_ERATT_HANDLED;
  9290. return 1;
  9291. }
  9292. return 0;
  9293. unplug_err:
  9294. /* Set the driver HS work bitmap */
  9295. phba->work_hs |= UNPLUG_ERR;
  9296. /* Set the driver HA work bitmap */
  9297. phba->work_ha |= HA_ERATT;
  9298. /* Indicate polling handles this ERATT */
  9299. phba->hba_flag |= HBA_ERATT_HANDLED;
  9300. return 1;
  9301. }
  9302. /**
  9303. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9304. * @phba: Pointer to HBA context.
  9305. *
  9306. * This function is called to read the SLI4 device error attention registers
  9307. * for possible error attention events. The caller must hold the hostlock
  9308. * with spin_lock_irq().
  9309. *
  9310. * This function returns 1 when there is Error Attention in the Host Attention
  9311. * Register and returns 0 otherwise.
  9312. **/
  9313. static int
  9314. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9315. {
  9316. uint32_t uerr_sta_hi, uerr_sta_lo;
  9317. uint32_t if_type, portsmphr;
  9318. struct lpfc_register portstat_reg;
  9319. /*
  9320. * For now, use the SLI4 device internal unrecoverable error
  9321. * registers for error attention. This can be changed later.
  9322. */
  9323. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9324. switch (if_type) {
  9325. case LPFC_SLI_INTF_IF_TYPE_0:
  9326. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9327. &uerr_sta_lo) ||
  9328. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9329. &uerr_sta_hi)) {
  9330. phba->work_hs |= UNPLUG_ERR;
  9331. phba->work_ha |= HA_ERATT;
  9332. phba->hba_flag |= HBA_ERATT_HANDLED;
  9333. return 1;
  9334. }
  9335. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9336. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9337. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9338. "1423 HBA Unrecoverable error: "
  9339. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9340. "ue_mask_lo_reg=0x%x, "
  9341. "ue_mask_hi_reg=0x%x\n",
  9342. uerr_sta_lo, uerr_sta_hi,
  9343. phba->sli4_hba.ue_mask_lo,
  9344. phba->sli4_hba.ue_mask_hi);
  9345. phba->work_status[0] = uerr_sta_lo;
  9346. phba->work_status[1] = uerr_sta_hi;
  9347. phba->work_ha |= HA_ERATT;
  9348. phba->hba_flag |= HBA_ERATT_HANDLED;
  9349. return 1;
  9350. }
  9351. break;
  9352. case LPFC_SLI_INTF_IF_TYPE_2:
  9353. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9354. &portstat_reg.word0) ||
  9355. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9356. &portsmphr)){
  9357. phba->work_hs |= UNPLUG_ERR;
  9358. phba->work_ha |= HA_ERATT;
  9359. phba->hba_flag |= HBA_ERATT_HANDLED;
  9360. return 1;
  9361. }
  9362. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9363. phba->work_status[0] =
  9364. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9365. phba->work_status[1] =
  9366. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9367. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9368. "2885 Port Status Event: "
  9369. "port status reg 0x%x, "
  9370. "port smphr reg 0x%x, "
  9371. "error 1=0x%x, error 2=0x%x\n",
  9372. portstat_reg.word0,
  9373. portsmphr,
  9374. phba->work_status[0],
  9375. phba->work_status[1]);
  9376. phba->work_ha |= HA_ERATT;
  9377. phba->hba_flag |= HBA_ERATT_HANDLED;
  9378. return 1;
  9379. }
  9380. break;
  9381. case LPFC_SLI_INTF_IF_TYPE_1:
  9382. default:
  9383. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9384. "2886 HBA Error Attention on unsupported "
  9385. "if type %d.", if_type);
  9386. return 1;
  9387. }
  9388. return 0;
  9389. }
  9390. /**
  9391. * lpfc_sli_check_eratt - check error attention events
  9392. * @phba: Pointer to HBA context.
  9393. *
  9394. * This function is called from timer soft interrupt context to check HBA's
  9395. * error attention register bit for error attention events.
  9396. *
  9397. * This function returns 1 when there is Error Attention in the Host Attention
  9398. * Register and returns 0 otherwise.
  9399. **/
  9400. int
  9401. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9402. {
  9403. uint32_t ha_copy;
  9404. /* If somebody is waiting to handle an eratt, don't process it
  9405. * here. The brdkill function will do this.
  9406. */
  9407. if (phba->link_flag & LS_IGNORE_ERATT)
  9408. return 0;
  9409. /* Check if interrupt handler handles this ERATT */
  9410. spin_lock_irq(&phba->hbalock);
  9411. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9412. /* Interrupt handler has handled ERATT */
  9413. spin_unlock_irq(&phba->hbalock);
  9414. return 0;
  9415. }
  9416. /*
  9417. * If there is deferred error attention, do not check for error
  9418. * attention
  9419. */
  9420. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9421. spin_unlock_irq(&phba->hbalock);
  9422. return 0;
  9423. }
  9424. /* If PCI channel is offline, don't process it */
  9425. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9426. spin_unlock_irq(&phba->hbalock);
  9427. return 0;
  9428. }
  9429. switch (phba->sli_rev) {
  9430. case LPFC_SLI_REV2:
  9431. case LPFC_SLI_REV3:
  9432. /* Read chip Host Attention (HA) register */
  9433. ha_copy = lpfc_sli_eratt_read(phba);
  9434. break;
  9435. case LPFC_SLI_REV4:
  9436. /* Read device Uncoverable Error (UERR) registers */
  9437. ha_copy = lpfc_sli4_eratt_read(phba);
  9438. break;
  9439. default:
  9440. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9441. "0299 Invalid SLI revision (%d)\n",
  9442. phba->sli_rev);
  9443. ha_copy = 0;
  9444. break;
  9445. }
  9446. spin_unlock_irq(&phba->hbalock);
  9447. return ha_copy;
  9448. }
  9449. /**
  9450. * lpfc_intr_state_check - Check device state for interrupt handling
  9451. * @phba: Pointer to HBA context.
  9452. *
  9453. * This inline routine checks whether a device or its PCI slot is in a state
  9454. * that the interrupt should be handled.
  9455. *
  9456. * This function returns 0 if the device or the PCI slot is in a state that
  9457. * interrupt should be handled, otherwise -EIO.
  9458. */
  9459. static inline int
  9460. lpfc_intr_state_check(struct lpfc_hba *phba)
  9461. {
  9462. /* If the pci channel is offline, ignore all the interrupts */
  9463. if (unlikely(pci_channel_offline(phba->pcidev)))
  9464. return -EIO;
  9465. /* Update device level interrupt statistics */
  9466. phba->sli.slistat.sli_intr++;
  9467. /* Ignore all interrupts during initialization. */
  9468. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9469. return -EIO;
  9470. return 0;
  9471. }
  9472. /**
  9473. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9474. * @irq: Interrupt number.
  9475. * @dev_id: The device context pointer.
  9476. *
  9477. * This function is directly called from the PCI layer as an interrupt
  9478. * service routine when device with SLI-3 interface spec is enabled with
  9479. * MSI-X multi-message interrupt mode and there are slow-path events in
  9480. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9481. * interrupt mode, this function is called as part of the device-level
  9482. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9483. * is undergoing initialization, the interrupt handler will not process
  9484. * the interrupt. The link attention and ELS ring attention events are
  9485. * handled by the worker thread. The interrupt handler signals the worker
  9486. * thread and returns for these events. This function is called without
  9487. * any lock held. It gets the hbalock to access and update SLI data
  9488. * structures.
  9489. *
  9490. * This function returns IRQ_HANDLED when interrupt is handled else it
  9491. * returns IRQ_NONE.
  9492. **/
  9493. irqreturn_t
  9494. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9495. {
  9496. struct lpfc_hba *phba;
  9497. uint32_t ha_copy, hc_copy;
  9498. uint32_t work_ha_copy;
  9499. unsigned long status;
  9500. unsigned long iflag;
  9501. uint32_t control;
  9502. MAILBOX_t *mbox, *pmbox;
  9503. struct lpfc_vport *vport;
  9504. struct lpfc_nodelist *ndlp;
  9505. struct lpfc_dmabuf *mp;
  9506. LPFC_MBOXQ_t *pmb;
  9507. int rc;
  9508. /*
  9509. * Get the driver's phba structure from the dev_id and
  9510. * assume the HBA is not interrupting.
  9511. */
  9512. phba = (struct lpfc_hba *)dev_id;
  9513. if (unlikely(!phba))
  9514. return IRQ_NONE;
  9515. /*
  9516. * Stuff needs to be attented to when this function is invoked as an
  9517. * individual interrupt handler in MSI-X multi-message interrupt mode
  9518. */
  9519. if (phba->intr_type == MSIX) {
  9520. /* Check device state for handling interrupt */
  9521. if (lpfc_intr_state_check(phba))
  9522. return IRQ_NONE;
  9523. /* Need to read HA REG for slow-path events */
  9524. spin_lock_irqsave(&phba->hbalock, iflag);
  9525. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9526. goto unplug_error;
  9527. /* If somebody is waiting to handle an eratt don't process it
  9528. * here. The brdkill function will do this.
  9529. */
  9530. if (phba->link_flag & LS_IGNORE_ERATT)
  9531. ha_copy &= ~HA_ERATT;
  9532. /* Check the need for handling ERATT in interrupt handler */
  9533. if (ha_copy & HA_ERATT) {
  9534. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9535. /* ERATT polling has handled ERATT */
  9536. ha_copy &= ~HA_ERATT;
  9537. else
  9538. /* Indicate interrupt handler handles ERATT */
  9539. phba->hba_flag |= HBA_ERATT_HANDLED;
  9540. }
  9541. /*
  9542. * If there is deferred error attention, do not check for any
  9543. * interrupt.
  9544. */
  9545. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9546. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9547. return IRQ_NONE;
  9548. }
  9549. /* Clear up only attention source related to slow-path */
  9550. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9551. goto unplug_error;
  9552. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9553. HC_LAINT_ENA | HC_ERINT_ENA),
  9554. phba->HCregaddr);
  9555. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9556. phba->HAregaddr);
  9557. writel(hc_copy, phba->HCregaddr);
  9558. readl(phba->HAregaddr); /* flush */
  9559. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9560. } else
  9561. ha_copy = phba->ha_copy;
  9562. work_ha_copy = ha_copy & phba->work_ha_mask;
  9563. if (work_ha_copy) {
  9564. if (work_ha_copy & HA_LATT) {
  9565. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9566. /*
  9567. * Turn off Link Attention interrupts
  9568. * until CLEAR_LA done
  9569. */
  9570. spin_lock_irqsave(&phba->hbalock, iflag);
  9571. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9572. if (lpfc_readl(phba->HCregaddr, &control))
  9573. goto unplug_error;
  9574. control &= ~HC_LAINT_ENA;
  9575. writel(control, phba->HCregaddr);
  9576. readl(phba->HCregaddr); /* flush */
  9577. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9578. }
  9579. else
  9580. work_ha_copy &= ~HA_LATT;
  9581. }
  9582. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9583. /*
  9584. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9585. * the only slow ring.
  9586. */
  9587. status = (work_ha_copy &
  9588. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9589. status >>= (4*LPFC_ELS_RING);
  9590. if (status & HA_RXMASK) {
  9591. spin_lock_irqsave(&phba->hbalock, iflag);
  9592. if (lpfc_readl(phba->HCregaddr, &control))
  9593. goto unplug_error;
  9594. lpfc_debugfs_slow_ring_trc(phba,
  9595. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9596. control, status,
  9597. (uint32_t)phba->sli.slistat.sli_intr);
  9598. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9599. lpfc_debugfs_slow_ring_trc(phba,
  9600. "ISR Disable ring:"
  9601. "pwork:x%x hawork:x%x wait:x%x",
  9602. phba->work_ha, work_ha_copy,
  9603. (uint32_t)((unsigned long)
  9604. &phba->work_waitq));
  9605. control &=
  9606. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9607. writel(control, phba->HCregaddr);
  9608. readl(phba->HCregaddr); /* flush */
  9609. }
  9610. else {
  9611. lpfc_debugfs_slow_ring_trc(phba,
  9612. "ISR slow ring: pwork:"
  9613. "x%x hawork:x%x wait:x%x",
  9614. phba->work_ha, work_ha_copy,
  9615. (uint32_t)((unsigned long)
  9616. &phba->work_waitq));
  9617. }
  9618. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9619. }
  9620. }
  9621. spin_lock_irqsave(&phba->hbalock, iflag);
  9622. if (work_ha_copy & HA_ERATT) {
  9623. if (lpfc_sli_read_hs(phba))
  9624. goto unplug_error;
  9625. /*
  9626. * Check if there is a deferred error condition
  9627. * is active
  9628. */
  9629. if ((HS_FFER1 & phba->work_hs) &&
  9630. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9631. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9632. phba->work_hs)) {
  9633. phba->hba_flag |= DEFER_ERATT;
  9634. /* Clear all interrupt enable conditions */
  9635. writel(0, phba->HCregaddr);
  9636. readl(phba->HCregaddr);
  9637. }
  9638. }
  9639. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9640. pmb = phba->sli.mbox_active;
  9641. pmbox = &pmb->u.mb;
  9642. mbox = phba->mbox;
  9643. vport = pmb->vport;
  9644. /* First check out the status word */
  9645. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9646. if (pmbox->mbxOwner != OWN_HOST) {
  9647. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9648. /*
  9649. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9650. * mbxStatus <status>
  9651. */
  9652. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9653. LOG_SLI,
  9654. "(%d):0304 Stray Mailbox "
  9655. "Interrupt mbxCommand x%x "
  9656. "mbxStatus x%x\n",
  9657. (vport ? vport->vpi : 0),
  9658. pmbox->mbxCommand,
  9659. pmbox->mbxStatus);
  9660. /* clear mailbox attention bit */
  9661. work_ha_copy &= ~HA_MBATT;
  9662. } else {
  9663. phba->sli.mbox_active = NULL;
  9664. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9665. phba->last_completion_time = jiffies;
  9666. del_timer(&phba->sli.mbox_tmo);
  9667. if (pmb->mbox_cmpl) {
  9668. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9669. MAILBOX_CMD_SIZE);
  9670. if (pmb->out_ext_byte_len &&
  9671. pmb->context2)
  9672. lpfc_sli_pcimem_bcopy(
  9673. phba->mbox_ext,
  9674. pmb->context2,
  9675. pmb->out_ext_byte_len);
  9676. }
  9677. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9678. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9679. lpfc_debugfs_disc_trc(vport,
  9680. LPFC_DISC_TRC_MBOX_VPORT,
  9681. "MBOX dflt rpi: : "
  9682. "status:x%x rpi:x%x",
  9683. (uint32_t)pmbox->mbxStatus,
  9684. pmbox->un.varWords[0], 0);
  9685. if (!pmbox->mbxStatus) {
  9686. mp = (struct lpfc_dmabuf *)
  9687. (pmb->context1);
  9688. ndlp = (struct lpfc_nodelist *)
  9689. pmb->context2;
  9690. /* Reg_LOGIN of dflt RPI was
  9691. * successful. new lets get
  9692. * rid of the RPI using the
  9693. * same mbox buffer.
  9694. */
  9695. lpfc_unreg_login(phba,
  9696. vport->vpi,
  9697. pmbox->un.varWords[0],
  9698. pmb);
  9699. pmb->mbox_cmpl =
  9700. lpfc_mbx_cmpl_dflt_rpi;
  9701. pmb->context1 = mp;
  9702. pmb->context2 = ndlp;
  9703. pmb->vport = vport;
  9704. rc = lpfc_sli_issue_mbox(phba,
  9705. pmb,
  9706. MBX_NOWAIT);
  9707. if (rc != MBX_BUSY)
  9708. lpfc_printf_log(phba,
  9709. KERN_ERR,
  9710. LOG_MBOX | LOG_SLI,
  9711. "0350 rc should have"
  9712. "been MBX_BUSY\n");
  9713. if (rc != MBX_NOT_FINISHED)
  9714. goto send_current_mbox;
  9715. }
  9716. }
  9717. spin_lock_irqsave(
  9718. &phba->pport->work_port_lock,
  9719. iflag);
  9720. phba->pport->work_port_events &=
  9721. ~WORKER_MBOX_TMO;
  9722. spin_unlock_irqrestore(
  9723. &phba->pport->work_port_lock,
  9724. iflag);
  9725. lpfc_mbox_cmpl_put(phba, pmb);
  9726. }
  9727. } else
  9728. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9729. if ((work_ha_copy & HA_MBATT) &&
  9730. (phba->sli.mbox_active == NULL)) {
  9731. send_current_mbox:
  9732. /* Process next mailbox command if there is one */
  9733. do {
  9734. rc = lpfc_sli_issue_mbox(phba, NULL,
  9735. MBX_NOWAIT);
  9736. } while (rc == MBX_NOT_FINISHED);
  9737. if (rc != MBX_SUCCESS)
  9738. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9739. LOG_SLI, "0349 rc should be "
  9740. "MBX_SUCCESS\n");
  9741. }
  9742. spin_lock_irqsave(&phba->hbalock, iflag);
  9743. phba->work_ha |= work_ha_copy;
  9744. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9745. lpfc_worker_wake_up(phba);
  9746. }
  9747. return IRQ_HANDLED;
  9748. unplug_error:
  9749. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9750. return IRQ_HANDLED;
  9751. } /* lpfc_sli_sp_intr_handler */
  9752. /**
  9753. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9754. * @irq: Interrupt number.
  9755. * @dev_id: The device context pointer.
  9756. *
  9757. * This function is directly called from the PCI layer as an interrupt
  9758. * service routine when device with SLI-3 interface spec is enabled with
  9759. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9760. * ring event in the HBA. However, when the device is enabled with either
  9761. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9762. * device-level interrupt handler. When the PCI slot is in error recovery
  9763. * or the HBA is undergoing initialization, the interrupt handler will not
  9764. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9765. * the intrrupt context. This function is called without any lock held.
  9766. * It gets the hbalock to access and update SLI data structures.
  9767. *
  9768. * This function returns IRQ_HANDLED when interrupt is handled else it
  9769. * returns IRQ_NONE.
  9770. **/
  9771. irqreturn_t
  9772. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9773. {
  9774. struct lpfc_hba *phba;
  9775. uint32_t ha_copy;
  9776. unsigned long status;
  9777. unsigned long iflag;
  9778. /* Get the driver's phba structure from the dev_id and
  9779. * assume the HBA is not interrupting.
  9780. */
  9781. phba = (struct lpfc_hba *) dev_id;
  9782. if (unlikely(!phba))
  9783. return IRQ_NONE;
  9784. /*
  9785. * Stuff needs to be attented to when this function is invoked as an
  9786. * individual interrupt handler in MSI-X multi-message interrupt mode
  9787. */
  9788. if (phba->intr_type == MSIX) {
  9789. /* Check device state for handling interrupt */
  9790. if (lpfc_intr_state_check(phba))
  9791. return IRQ_NONE;
  9792. /* Need to read HA REG for FCP ring and other ring events */
  9793. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9794. return IRQ_HANDLED;
  9795. /* Clear up only attention source related to fast-path */
  9796. spin_lock_irqsave(&phba->hbalock, iflag);
  9797. /*
  9798. * If there is deferred error attention, do not check for
  9799. * any interrupt.
  9800. */
  9801. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9802. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9803. return IRQ_NONE;
  9804. }
  9805. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9806. phba->HAregaddr);
  9807. readl(phba->HAregaddr); /* flush */
  9808. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9809. } else
  9810. ha_copy = phba->ha_copy;
  9811. /*
  9812. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9813. */
  9814. ha_copy &= ~(phba->work_ha_mask);
  9815. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9816. status >>= (4*LPFC_FCP_RING);
  9817. if (status & HA_RXMASK)
  9818. lpfc_sli_handle_fast_ring_event(phba,
  9819. &phba->sli.ring[LPFC_FCP_RING],
  9820. status);
  9821. if (phba->cfg_multi_ring_support == 2) {
  9822. /*
  9823. * Process all events on extra ring. Take the optimized path
  9824. * for extra ring IO.
  9825. */
  9826. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9827. status >>= (4*LPFC_EXTRA_RING);
  9828. if (status & HA_RXMASK) {
  9829. lpfc_sli_handle_fast_ring_event(phba,
  9830. &phba->sli.ring[LPFC_EXTRA_RING],
  9831. status);
  9832. }
  9833. }
  9834. return IRQ_HANDLED;
  9835. } /* lpfc_sli_fp_intr_handler */
  9836. /**
  9837. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9838. * @irq: Interrupt number.
  9839. * @dev_id: The device context pointer.
  9840. *
  9841. * This function is the HBA device-level interrupt handler to device with
  9842. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9843. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9844. * requires driver attention. This function invokes the slow-path interrupt
  9845. * attention handling function and fast-path interrupt attention handling
  9846. * function in turn to process the relevant HBA attention events. This
  9847. * function is called without any lock held. It gets the hbalock to access
  9848. * and update SLI data structures.
  9849. *
  9850. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9851. * returns IRQ_NONE.
  9852. **/
  9853. irqreturn_t
  9854. lpfc_sli_intr_handler(int irq, void *dev_id)
  9855. {
  9856. struct lpfc_hba *phba;
  9857. irqreturn_t sp_irq_rc, fp_irq_rc;
  9858. unsigned long status1, status2;
  9859. uint32_t hc_copy;
  9860. /*
  9861. * Get the driver's phba structure from the dev_id and
  9862. * assume the HBA is not interrupting.
  9863. */
  9864. phba = (struct lpfc_hba *) dev_id;
  9865. if (unlikely(!phba))
  9866. return IRQ_NONE;
  9867. /* Check device state for handling interrupt */
  9868. if (lpfc_intr_state_check(phba))
  9869. return IRQ_NONE;
  9870. spin_lock(&phba->hbalock);
  9871. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9872. spin_unlock(&phba->hbalock);
  9873. return IRQ_HANDLED;
  9874. }
  9875. if (unlikely(!phba->ha_copy)) {
  9876. spin_unlock(&phba->hbalock);
  9877. return IRQ_NONE;
  9878. } else if (phba->ha_copy & HA_ERATT) {
  9879. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9880. /* ERATT polling has handled ERATT */
  9881. phba->ha_copy &= ~HA_ERATT;
  9882. else
  9883. /* Indicate interrupt handler handles ERATT */
  9884. phba->hba_flag |= HBA_ERATT_HANDLED;
  9885. }
  9886. /*
  9887. * If there is deferred error attention, do not check for any interrupt.
  9888. */
  9889. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9890. spin_unlock(&phba->hbalock);
  9891. return IRQ_NONE;
  9892. }
  9893. /* Clear attention sources except link and error attentions */
  9894. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9895. spin_unlock(&phba->hbalock);
  9896. return IRQ_HANDLED;
  9897. }
  9898. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9899. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9900. phba->HCregaddr);
  9901. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9902. writel(hc_copy, phba->HCregaddr);
  9903. readl(phba->HAregaddr); /* flush */
  9904. spin_unlock(&phba->hbalock);
  9905. /*
  9906. * Invokes slow-path host attention interrupt handling as appropriate.
  9907. */
  9908. /* status of events with mailbox and link attention */
  9909. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9910. /* status of events with ELS ring */
  9911. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9912. status2 >>= (4*LPFC_ELS_RING);
  9913. if (status1 || (status2 & HA_RXMASK))
  9914. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9915. else
  9916. sp_irq_rc = IRQ_NONE;
  9917. /*
  9918. * Invoke fast-path host attention interrupt handling as appropriate.
  9919. */
  9920. /* status of events with FCP ring */
  9921. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9922. status1 >>= (4*LPFC_FCP_RING);
  9923. /* status of events with extra ring */
  9924. if (phba->cfg_multi_ring_support == 2) {
  9925. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9926. status2 >>= (4*LPFC_EXTRA_RING);
  9927. } else
  9928. status2 = 0;
  9929. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9930. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9931. else
  9932. fp_irq_rc = IRQ_NONE;
  9933. /* Return device-level interrupt handling status */
  9934. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9935. } /* lpfc_sli_intr_handler */
  9936. /**
  9937. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9938. * @phba: pointer to lpfc hba data structure.
  9939. *
  9940. * This routine is invoked by the worker thread to process all the pending
  9941. * SLI4 FCP abort XRI events.
  9942. **/
  9943. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9944. {
  9945. struct lpfc_cq_event *cq_event;
  9946. /* First, declare the fcp xri abort event has been handled */
  9947. spin_lock_irq(&phba->hbalock);
  9948. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9949. spin_unlock_irq(&phba->hbalock);
  9950. /* Now, handle all the fcp xri abort events */
  9951. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9952. /* Get the first event from the head of the event queue */
  9953. spin_lock_irq(&phba->hbalock);
  9954. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9955. cq_event, struct lpfc_cq_event, list);
  9956. spin_unlock_irq(&phba->hbalock);
  9957. /* Notify aborted XRI for FCP work queue */
  9958. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9959. /* Free the event processed back to the free pool */
  9960. lpfc_sli4_cq_event_release(phba, cq_event);
  9961. }
  9962. }
  9963. /**
  9964. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9965. * @phba: pointer to lpfc hba data structure.
  9966. *
  9967. * This routine is invoked by the worker thread to process all the pending
  9968. * SLI4 els abort xri events.
  9969. **/
  9970. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9971. {
  9972. struct lpfc_cq_event *cq_event;
  9973. /* First, declare the els xri abort event has been handled */
  9974. spin_lock_irq(&phba->hbalock);
  9975. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9976. spin_unlock_irq(&phba->hbalock);
  9977. /* Now, handle all the els xri abort events */
  9978. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9979. /* Get the first event from the head of the event queue */
  9980. spin_lock_irq(&phba->hbalock);
  9981. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9982. cq_event, struct lpfc_cq_event, list);
  9983. spin_unlock_irq(&phba->hbalock);
  9984. /* Notify aborted XRI for ELS work queue */
  9985. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9986. /* Free the event processed back to the free pool */
  9987. lpfc_sli4_cq_event_release(phba, cq_event);
  9988. }
  9989. }
  9990. /**
  9991. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9992. * @phba: pointer to lpfc hba data structure
  9993. * @pIocbIn: pointer to the rspiocbq
  9994. * @pIocbOut: pointer to the cmdiocbq
  9995. * @wcqe: pointer to the complete wcqe
  9996. *
  9997. * This routine transfers the fields of a command iocbq to a response iocbq
  9998. * by copying all the IOCB fields from command iocbq and transferring the
  9999. * completion status information from the complete wcqe.
  10000. **/
  10001. static void
  10002. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  10003. struct lpfc_iocbq *pIocbIn,
  10004. struct lpfc_iocbq *pIocbOut,
  10005. struct lpfc_wcqe_complete *wcqe)
  10006. {
  10007. unsigned long iflags;
  10008. uint32_t status;
  10009. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  10010. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  10011. sizeof(struct lpfc_iocbq) - offset);
  10012. /* Map WCQE parameters into irspiocb parameters */
  10013. status = bf_get(lpfc_wcqe_c_status, wcqe);
  10014. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  10015. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  10016. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  10017. pIocbIn->iocb.un.fcpi.fcpi_parm =
  10018. pIocbOut->iocb.un.fcpi.fcpi_parm -
  10019. wcqe->total_data_placed;
  10020. else
  10021. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10022. else {
  10023. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  10024. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  10025. }
  10026. /* Convert BG errors for completion status */
  10027. if (status == CQE_STATUS_DI_ERROR) {
  10028. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  10029. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  10030. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  10031. else
  10032. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  10033. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  10034. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  10035. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10036. BGS_GUARD_ERR_MASK;
  10037. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  10038. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10039. BGS_APPTAG_ERR_MASK;
  10040. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  10041. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10042. BGS_REFTAG_ERR_MASK;
  10043. /* Check to see if there was any good data before the error */
  10044. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  10045. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10046. BGS_HI_WATER_MARK_PRESENT_MASK;
  10047. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  10048. wcqe->total_data_placed;
  10049. }
  10050. /*
  10051. * Set ALL the error bits to indicate we don't know what
  10052. * type of error it is.
  10053. */
  10054. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  10055. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  10056. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  10057. BGS_GUARD_ERR_MASK);
  10058. }
  10059. /* Pick up HBA exchange busy condition */
  10060. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  10061. spin_lock_irqsave(&phba->hbalock, iflags);
  10062. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  10063. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10064. }
  10065. }
  10066. /**
  10067. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  10068. * @phba: Pointer to HBA context object.
  10069. * @wcqe: Pointer to work-queue completion queue entry.
  10070. *
  10071. * This routine handles an ELS work-queue completion event and construct
  10072. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  10073. * discovery engine to handle.
  10074. *
  10075. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  10076. **/
  10077. static struct lpfc_iocbq *
  10078. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  10079. struct lpfc_iocbq *irspiocbq)
  10080. {
  10081. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  10082. struct lpfc_iocbq *cmdiocbq;
  10083. struct lpfc_wcqe_complete *wcqe;
  10084. unsigned long iflags;
  10085. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  10086. spin_lock_irqsave(&pring->ring_lock, iflags);
  10087. pring->stats.iocb_event++;
  10088. /* Look up the ELS command IOCB and create pseudo response IOCB */
  10089. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10090. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10091. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10092. if (unlikely(!cmdiocbq)) {
  10093. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10094. "0386 ELS complete with no corresponding "
  10095. "cmdiocb: iotag (%d)\n",
  10096. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10097. lpfc_sli_release_iocbq(phba, irspiocbq);
  10098. return NULL;
  10099. }
  10100. /* Fake the irspiocbq and copy necessary response information */
  10101. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  10102. return irspiocbq;
  10103. }
  10104. /**
  10105. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  10106. * @phba: Pointer to HBA context object.
  10107. * @cqe: Pointer to mailbox completion queue entry.
  10108. *
  10109. * This routine process a mailbox completion queue entry with asynchrous
  10110. * event.
  10111. *
  10112. * Return: true if work posted to worker thread, otherwise false.
  10113. **/
  10114. static bool
  10115. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10116. {
  10117. struct lpfc_cq_event *cq_event;
  10118. unsigned long iflags;
  10119. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  10120. "0392 Async Event: word0:x%x, word1:x%x, "
  10121. "word2:x%x, word3:x%x\n", mcqe->word0,
  10122. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  10123. /* Allocate a new internal CQ_EVENT entry */
  10124. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10125. if (!cq_event) {
  10126. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10127. "0394 Failed to allocate CQ_EVENT entry\n");
  10128. return false;
  10129. }
  10130. /* Move the CQE into an asynchronous event entry */
  10131. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  10132. spin_lock_irqsave(&phba->hbalock, iflags);
  10133. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  10134. /* Set the async event flag */
  10135. phba->hba_flag |= ASYNC_EVENT;
  10136. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10137. return true;
  10138. }
  10139. /**
  10140. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10141. * @phba: Pointer to HBA context object.
  10142. * @cqe: Pointer to mailbox completion queue entry.
  10143. *
  10144. * This routine process a mailbox completion queue entry with mailbox
  10145. * completion event.
  10146. *
  10147. * Return: true if work posted to worker thread, otherwise false.
  10148. **/
  10149. static bool
  10150. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10151. {
  10152. uint32_t mcqe_status;
  10153. MAILBOX_t *mbox, *pmbox;
  10154. struct lpfc_mqe *mqe;
  10155. struct lpfc_vport *vport;
  10156. struct lpfc_nodelist *ndlp;
  10157. struct lpfc_dmabuf *mp;
  10158. unsigned long iflags;
  10159. LPFC_MBOXQ_t *pmb;
  10160. bool workposted = false;
  10161. int rc;
  10162. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10163. if (!bf_get(lpfc_trailer_completed, mcqe))
  10164. goto out_no_mqe_complete;
  10165. /* Get the reference to the active mbox command */
  10166. spin_lock_irqsave(&phba->hbalock, iflags);
  10167. pmb = phba->sli.mbox_active;
  10168. if (unlikely(!pmb)) {
  10169. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10170. "1832 No pending MBOX command to handle\n");
  10171. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10172. goto out_no_mqe_complete;
  10173. }
  10174. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10175. mqe = &pmb->u.mqe;
  10176. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10177. mbox = phba->mbox;
  10178. vport = pmb->vport;
  10179. /* Reset heartbeat timer */
  10180. phba->last_completion_time = jiffies;
  10181. del_timer(&phba->sli.mbox_tmo);
  10182. /* Move mbox data to caller's mailbox region, do endian swapping */
  10183. if (pmb->mbox_cmpl && mbox)
  10184. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10185. /*
  10186. * For mcqe errors, conditionally move a modified error code to
  10187. * the mbox so that the error will not be missed.
  10188. */
  10189. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10190. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10191. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10192. bf_set(lpfc_mqe_status, mqe,
  10193. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10194. }
  10195. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10196. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10197. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10198. "MBOX dflt rpi: status:x%x rpi:x%x",
  10199. mcqe_status,
  10200. pmbox->un.varWords[0], 0);
  10201. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10202. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10203. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10204. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10205. * RID of the PPI using the same mbox buffer.
  10206. */
  10207. lpfc_unreg_login(phba, vport->vpi,
  10208. pmbox->un.varWords[0], pmb);
  10209. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10210. pmb->context1 = mp;
  10211. pmb->context2 = ndlp;
  10212. pmb->vport = vport;
  10213. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10214. if (rc != MBX_BUSY)
  10215. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10216. LOG_SLI, "0385 rc should "
  10217. "have been MBX_BUSY\n");
  10218. if (rc != MBX_NOT_FINISHED)
  10219. goto send_current_mbox;
  10220. }
  10221. }
  10222. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10223. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10224. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10225. /* There is mailbox completion work to do */
  10226. spin_lock_irqsave(&phba->hbalock, iflags);
  10227. __lpfc_mbox_cmpl_put(phba, pmb);
  10228. phba->work_ha |= HA_MBATT;
  10229. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10230. workposted = true;
  10231. send_current_mbox:
  10232. spin_lock_irqsave(&phba->hbalock, iflags);
  10233. /* Release the mailbox command posting token */
  10234. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10235. /* Setting active mailbox pointer need to be in sync to flag clear */
  10236. phba->sli.mbox_active = NULL;
  10237. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10238. /* Wake up worker thread to post the next pending mailbox command */
  10239. lpfc_worker_wake_up(phba);
  10240. out_no_mqe_complete:
  10241. if (bf_get(lpfc_trailer_consumed, mcqe))
  10242. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10243. return workposted;
  10244. }
  10245. /**
  10246. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10247. * @phba: Pointer to HBA context object.
  10248. * @cqe: Pointer to mailbox completion queue entry.
  10249. *
  10250. * This routine process a mailbox completion queue entry, it invokes the
  10251. * proper mailbox complete handling or asynchrous event handling routine
  10252. * according to the MCQE's async bit.
  10253. *
  10254. * Return: true if work posted to worker thread, otherwise false.
  10255. **/
  10256. static bool
  10257. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10258. {
  10259. struct lpfc_mcqe mcqe;
  10260. bool workposted;
  10261. /* Copy the mailbox MCQE and convert endian order as needed */
  10262. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10263. /* Invoke the proper event handling routine */
  10264. if (!bf_get(lpfc_trailer_async, &mcqe))
  10265. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10266. else
  10267. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10268. return workposted;
  10269. }
  10270. /**
  10271. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10272. * @phba: Pointer to HBA context object.
  10273. * @cq: Pointer to associated CQ
  10274. * @wcqe: Pointer to work-queue completion queue entry.
  10275. *
  10276. * This routine handles an ELS work-queue completion event.
  10277. *
  10278. * Return: true if work posted to worker thread, otherwise false.
  10279. **/
  10280. static bool
  10281. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10282. struct lpfc_wcqe_complete *wcqe)
  10283. {
  10284. struct lpfc_iocbq *irspiocbq;
  10285. unsigned long iflags;
  10286. struct lpfc_sli_ring *pring = cq->pring;
  10287. /* Get an irspiocbq for later ELS response processing use */
  10288. irspiocbq = lpfc_sli_get_iocbq(phba);
  10289. if (!irspiocbq) {
  10290. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10291. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10292. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10293. pring->txq_cnt, phba->iocb_cnt,
  10294. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10295. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10296. return false;
  10297. }
  10298. /* Save off the slow-path queue event for work thread to process */
  10299. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10300. spin_lock_irqsave(&phba->hbalock, iflags);
  10301. list_add_tail(&irspiocbq->cq_event.list,
  10302. &phba->sli4_hba.sp_queue_event);
  10303. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10304. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10305. return true;
  10306. }
  10307. /**
  10308. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10309. * @phba: Pointer to HBA context object.
  10310. * @wcqe: Pointer to work-queue completion queue entry.
  10311. *
  10312. * This routine handles slow-path WQ entry comsumed event by invoking the
  10313. * proper WQ release routine to the slow-path WQ.
  10314. **/
  10315. static void
  10316. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10317. struct lpfc_wcqe_release *wcqe)
  10318. {
  10319. /* sanity check on queue memory */
  10320. if (unlikely(!phba->sli4_hba.els_wq))
  10321. return;
  10322. /* Check for the slow-path ELS work queue */
  10323. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10324. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10325. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10326. else
  10327. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10328. "2579 Slow-path wqe consume event carries "
  10329. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10330. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10331. phba->sli4_hba.els_wq->queue_id);
  10332. }
  10333. /**
  10334. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10335. * @phba: Pointer to HBA context object.
  10336. * @cq: Pointer to a WQ completion queue.
  10337. * @wcqe: Pointer to work-queue completion queue entry.
  10338. *
  10339. * This routine handles an XRI abort event.
  10340. *
  10341. * Return: true if work posted to worker thread, otherwise false.
  10342. **/
  10343. static bool
  10344. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10345. struct lpfc_queue *cq,
  10346. struct sli4_wcqe_xri_aborted *wcqe)
  10347. {
  10348. bool workposted = false;
  10349. struct lpfc_cq_event *cq_event;
  10350. unsigned long iflags;
  10351. /* Allocate a new internal CQ_EVENT entry */
  10352. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10353. if (!cq_event) {
  10354. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10355. "0602 Failed to allocate CQ_EVENT entry\n");
  10356. return false;
  10357. }
  10358. /* Move the CQE into the proper xri abort event list */
  10359. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10360. switch (cq->subtype) {
  10361. case LPFC_FCP:
  10362. spin_lock_irqsave(&phba->hbalock, iflags);
  10363. list_add_tail(&cq_event->list,
  10364. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10365. /* Set the fcp xri abort event flag */
  10366. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10367. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10368. workposted = true;
  10369. break;
  10370. case LPFC_ELS:
  10371. spin_lock_irqsave(&phba->hbalock, iflags);
  10372. list_add_tail(&cq_event->list,
  10373. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10374. /* Set the els xri abort event flag */
  10375. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10376. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10377. workposted = true;
  10378. break;
  10379. default:
  10380. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10381. "0603 Invalid work queue CQE subtype (x%x)\n",
  10382. cq->subtype);
  10383. workposted = false;
  10384. break;
  10385. }
  10386. return workposted;
  10387. }
  10388. /**
  10389. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10390. * @phba: Pointer to HBA context object.
  10391. * @rcqe: Pointer to receive-queue completion queue entry.
  10392. *
  10393. * This routine process a receive-queue completion queue entry.
  10394. *
  10395. * Return: true if work posted to worker thread, otherwise false.
  10396. **/
  10397. static bool
  10398. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10399. {
  10400. bool workposted = false;
  10401. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10402. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10403. struct hbq_dmabuf *dma_buf;
  10404. uint32_t status, rq_id;
  10405. unsigned long iflags;
  10406. /* sanity check on queue memory */
  10407. if (unlikely(!hrq) || unlikely(!drq))
  10408. return workposted;
  10409. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10410. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10411. else
  10412. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10413. if (rq_id != hrq->queue_id)
  10414. goto out;
  10415. status = bf_get(lpfc_rcqe_status, rcqe);
  10416. switch (status) {
  10417. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10418. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10419. "2537 Receive Frame Truncated!!\n");
  10420. hrq->RQ_buf_trunc++;
  10421. case FC_STATUS_RQ_SUCCESS:
  10422. lpfc_sli4_rq_release(hrq, drq);
  10423. spin_lock_irqsave(&phba->hbalock, iflags);
  10424. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10425. if (!dma_buf) {
  10426. hrq->RQ_no_buf_found++;
  10427. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10428. goto out;
  10429. }
  10430. hrq->RQ_rcv_buf++;
  10431. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10432. /* save off the frame for the word thread to process */
  10433. list_add_tail(&dma_buf->cq_event.list,
  10434. &phba->sli4_hba.sp_queue_event);
  10435. /* Frame received */
  10436. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10437. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10438. workposted = true;
  10439. break;
  10440. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10441. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10442. hrq->RQ_no_posted_buf++;
  10443. /* Post more buffers if possible */
  10444. spin_lock_irqsave(&phba->hbalock, iflags);
  10445. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10446. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10447. workposted = true;
  10448. break;
  10449. }
  10450. out:
  10451. return workposted;
  10452. }
  10453. /**
  10454. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10455. * @phba: Pointer to HBA context object.
  10456. * @cq: Pointer to the completion queue.
  10457. * @wcqe: Pointer to a completion queue entry.
  10458. *
  10459. * This routine process a slow-path work-queue or receive queue completion queue
  10460. * entry.
  10461. *
  10462. * Return: true if work posted to worker thread, otherwise false.
  10463. **/
  10464. static bool
  10465. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10466. struct lpfc_cqe *cqe)
  10467. {
  10468. struct lpfc_cqe cqevt;
  10469. bool workposted = false;
  10470. /* Copy the work queue CQE and convert endian order if needed */
  10471. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10472. /* Check and process for different type of WCQE and dispatch */
  10473. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10474. case CQE_CODE_COMPL_WQE:
  10475. /* Process the WQ/RQ complete event */
  10476. phba->last_completion_time = jiffies;
  10477. workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
  10478. (struct lpfc_wcqe_complete *)&cqevt);
  10479. break;
  10480. case CQE_CODE_RELEASE_WQE:
  10481. /* Process the WQ release event */
  10482. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10483. (struct lpfc_wcqe_release *)&cqevt);
  10484. break;
  10485. case CQE_CODE_XRI_ABORTED:
  10486. /* Process the WQ XRI abort event */
  10487. phba->last_completion_time = jiffies;
  10488. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10489. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10490. break;
  10491. case CQE_CODE_RECEIVE:
  10492. case CQE_CODE_RECEIVE_V1:
  10493. /* Process the RQ event */
  10494. phba->last_completion_time = jiffies;
  10495. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10496. (struct lpfc_rcqe *)&cqevt);
  10497. break;
  10498. default:
  10499. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10500. "0388 Not a valid WCQE code: x%x\n",
  10501. bf_get(lpfc_cqe_code, &cqevt));
  10502. break;
  10503. }
  10504. return workposted;
  10505. }
  10506. /**
  10507. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10508. * @phba: Pointer to HBA context object.
  10509. * @eqe: Pointer to fast-path event queue entry.
  10510. *
  10511. * This routine process a event queue entry from the slow-path event queue.
  10512. * It will check the MajorCode and MinorCode to determine this is for a
  10513. * completion event on a completion queue, if not, an error shall be logged
  10514. * and just return. Otherwise, it will get to the corresponding completion
  10515. * queue and process all the entries on that completion queue, rearm the
  10516. * completion queue, and then return.
  10517. *
  10518. **/
  10519. static void
  10520. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10521. struct lpfc_queue *speq)
  10522. {
  10523. struct lpfc_queue *cq = NULL, *childq;
  10524. struct lpfc_cqe *cqe;
  10525. bool workposted = false;
  10526. int ecount = 0;
  10527. uint16_t cqid;
  10528. /* Get the reference to the corresponding CQ */
  10529. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10530. list_for_each_entry(childq, &speq->child_list, list) {
  10531. if (childq->queue_id == cqid) {
  10532. cq = childq;
  10533. break;
  10534. }
  10535. }
  10536. if (unlikely(!cq)) {
  10537. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10538. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10539. "0365 Slow-path CQ identifier "
  10540. "(%d) does not exist\n", cqid);
  10541. return;
  10542. }
  10543. /* Process all the entries to the CQ */
  10544. switch (cq->type) {
  10545. case LPFC_MCQ:
  10546. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10547. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10548. if (!(++ecount % cq->entry_repost))
  10549. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10550. cq->CQ_mbox++;
  10551. }
  10552. break;
  10553. case LPFC_WCQ:
  10554. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10555. if (cq->subtype == LPFC_FCP)
  10556. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10557. cqe);
  10558. else
  10559. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10560. cqe);
  10561. if (!(++ecount % cq->entry_repost))
  10562. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10563. }
  10564. /* Track the max number of CQEs processed in 1 EQ */
  10565. if (ecount > cq->CQ_max_cqe)
  10566. cq->CQ_max_cqe = ecount;
  10567. break;
  10568. default:
  10569. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10570. "0370 Invalid completion queue type (%d)\n",
  10571. cq->type);
  10572. return;
  10573. }
  10574. /* Catch the no cq entry condition, log an error */
  10575. if (unlikely(ecount == 0))
  10576. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10577. "0371 No entry from the CQ: identifier "
  10578. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10579. /* In any case, flash and re-arm the RCQ */
  10580. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10581. /* wake up worker thread if there are works to be done */
  10582. if (workposted)
  10583. lpfc_worker_wake_up(phba);
  10584. }
  10585. /**
  10586. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10587. * @phba: Pointer to HBA context object.
  10588. * @cq: Pointer to associated CQ
  10589. * @wcqe: Pointer to work-queue completion queue entry.
  10590. *
  10591. * This routine process a fast-path work queue completion entry from fast-path
  10592. * event queue for FCP command response completion.
  10593. **/
  10594. static void
  10595. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10596. struct lpfc_wcqe_complete *wcqe)
  10597. {
  10598. struct lpfc_sli_ring *pring = cq->pring;
  10599. struct lpfc_iocbq *cmdiocbq;
  10600. struct lpfc_iocbq irspiocbq;
  10601. unsigned long iflags;
  10602. /* Check for response status */
  10603. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10604. /* If resource errors reported from HBA, reduce queue
  10605. * depth of the SCSI device.
  10606. */
  10607. if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10608. IOSTAT_LOCAL_REJECT)) &&
  10609. ((wcqe->parameter & IOERR_PARAM_MASK) ==
  10610. IOERR_NO_RESOURCES))
  10611. phba->lpfc_rampdown_queue_depth(phba);
  10612. /* Log the error status */
  10613. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10614. "0373 FCP complete error: status=x%x, "
  10615. "hw_status=x%x, total_data_specified=%d, "
  10616. "parameter=x%x, word3=x%x\n",
  10617. bf_get(lpfc_wcqe_c_status, wcqe),
  10618. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10619. wcqe->total_data_placed, wcqe->parameter,
  10620. wcqe->word3);
  10621. }
  10622. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10623. spin_lock_irqsave(&pring->ring_lock, iflags);
  10624. pring->stats.iocb_event++;
  10625. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10626. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10627. spin_unlock_irqrestore(&pring->ring_lock, iflags);
  10628. if (unlikely(!cmdiocbq)) {
  10629. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10630. "0374 FCP complete with no corresponding "
  10631. "cmdiocb: iotag (%d)\n",
  10632. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10633. return;
  10634. }
  10635. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10636. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10637. "0375 FCP cmdiocb not callback function "
  10638. "iotag: (%d)\n",
  10639. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10640. return;
  10641. }
  10642. /* Fake the irspiocb and copy necessary response information */
  10643. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10644. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10645. spin_lock_irqsave(&phba->hbalock, iflags);
  10646. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10647. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10648. }
  10649. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10650. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10651. }
  10652. /**
  10653. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10654. * @phba: Pointer to HBA context object.
  10655. * @cq: Pointer to completion queue.
  10656. * @wcqe: Pointer to work-queue completion queue entry.
  10657. *
  10658. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10659. * proper WQ release routine to the slow-path WQ.
  10660. **/
  10661. static void
  10662. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10663. struct lpfc_wcqe_release *wcqe)
  10664. {
  10665. struct lpfc_queue *childwq;
  10666. bool wqid_matched = false;
  10667. uint16_t fcp_wqid;
  10668. /* Check for fast-path FCP work queue release */
  10669. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10670. list_for_each_entry(childwq, &cq->child_list, list) {
  10671. if (childwq->queue_id == fcp_wqid) {
  10672. lpfc_sli4_wq_release(childwq,
  10673. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10674. wqid_matched = true;
  10675. break;
  10676. }
  10677. }
  10678. /* Report warning log message if no match found */
  10679. if (wqid_matched != true)
  10680. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10681. "2580 Fast-path wqe consume event carries "
  10682. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10683. }
  10684. /**
  10685. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10686. * @cq: Pointer to the completion queue.
  10687. * @eqe: Pointer to fast-path completion queue entry.
  10688. *
  10689. * This routine process a fast-path work queue completion entry from fast-path
  10690. * event queue for FCP command response completion.
  10691. **/
  10692. static int
  10693. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10694. struct lpfc_cqe *cqe)
  10695. {
  10696. struct lpfc_wcqe_release wcqe;
  10697. bool workposted = false;
  10698. /* Copy the work queue CQE and convert endian order if needed */
  10699. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10700. /* Check and process for different type of WCQE and dispatch */
  10701. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10702. case CQE_CODE_COMPL_WQE:
  10703. cq->CQ_wq++;
  10704. /* Process the WQ complete event */
  10705. phba->last_completion_time = jiffies;
  10706. lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
  10707. (struct lpfc_wcqe_complete *)&wcqe);
  10708. break;
  10709. case CQE_CODE_RELEASE_WQE:
  10710. cq->CQ_release_wqe++;
  10711. /* Process the WQ release event */
  10712. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10713. (struct lpfc_wcqe_release *)&wcqe);
  10714. break;
  10715. case CQE_CODE_XRI_ABORTED:
  10716. cq->CQ_xri_aborted++;
  10717. /* Process the WQ XRI abort event */
  10718. phba->last_completion_time = jiffies;
  10719. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10720. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10721. break;
  10722. default:
  10723. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10724. "0144 Not a valid WCQE code: x%x\n",
  10725. bf_get(lpfc_wcqe_c_code, &wcqe));
  10726. break;
  10727. }
  10728. return workposted;
  10729. }
  10730. /**
  10731. * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
  10732. * @phba: Pointer to HBA context object.
  10733. * @eqe: Pointer to fast-path event queue entry.
  10734. *
  10735. * This routine process a event queue entry from the fast-path event queue.
  10736. * It will check the MajorCode and MinorCode to determine this is for a
  10737. * completion event on a completion queue, if not, an error shall be logged
  10738. * and just return. Otherwise, it will get to the corresponding completion
  10739. * queue and process all the entries on the completion queue, rearm the
  10740. * completion queue, and then return.
  10741. **/
  10742. static void
  10743. lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10744. uint32_t qidx)
  10745. {
  10746. struct lpfc_queue *cq;
  10747. struct lpfc_cqe *cqe;
  10748. bool workposted = false;
  10749. uint16_t cqid;
  10750. int ecount = 0;
  10751. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10752. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10753. "0366 Not a valid completion "
  10754. "event: majorcode=x%x, minorcode=x%x\n",
  10755. bf_get_le32(lpfc_eqe_major_code, eqe),
  10756. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10757. return;
  10758. }
  10759. /* Get the reference to the corresponding CQ */
  10760. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10761. /* Check if this is a Slow path event */
  10762. if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
  10763. lpfc_sli4_sp_handle_eqe(phba, eqe,
  10764. phba->sli4_hba.hba_eq[qidx]);
  10765. return;
  10766. }
  10767. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10768. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10769. "3146 Fast-path completion queues "
  10770. "does not exist\n");
  10771. return;
  10772. }
  10773. cq = phba->sli4_hba.fcp_cq[qidx];
  10774. if (unlikely(!cq)) {
  10775. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10776. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10777. "0367 Fast-path completion queue "
  10778. "(%d) does not exist\n", qidx);
  10779. return;
  10780. }
  10781. if (unlikely(cqid != cq->queue_id)) {
  10782. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10783. "0368 Miss-matched fast-path completion "
  10784. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10785. cqid, cq->queue_id);
  10786. return;
  10787. }
  10788. /* Process all the entries to the CQ */
  10789. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10790. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10791. if (!(++ecount % cq->entry_repost))
  10792. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10793. }
  10794. /* Track the max number of CQEs processed in 1 EQ */
  10795. if (ecount > cq->CQ_max_cqe)
  10796. cq->CQ_max_cqe = ecount;
  10797. /* Catch the no cq entry condition */
  10798. if (unlikely(ecount == 0))
  10799. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10800. "0369 No entry from fast-path completion "
  10801. "queue fcpcqid=%d\n", cq->queue_id);
  10802. /* In any case, flash and re-arm the CQ */
  10803. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10804. /* wake up worker thread if there are works to be done */
  10805. if (workposted)
  10806. lpfc_worker_wake_up(phba);
  10807. }
  10808. static void
  10809. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10810. {
  10811. struct lpfc_eqe *eqe;
  10812. /* walk all the EQ entries and drop on the floor */
  10813. while ((eqe = lpfc_sli4_eq_get(eq)))
  10814. ;
  10815. /* Clear and re-arm the EQ */
  10816. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10817. }
  10818. /**
  10819. * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
  10820. * @irq: Interrupt number.
  10821. * @dev_id: The device context pointer.
  10822. *
  10823. * This function is directly called from the PCI layer as an interrupt
  10824. * service routine when device with SLI-4 interface spec is enabled with
  10825. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10826. * ring event in the HBA. However, when the device is enabled with either
  10827. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10828. * device-level interrupt handler. When the PCI slot is in error recovery
  10829. * or the HBA is undergoing initialization, the interrupt handler will not
  10830. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10831. * the intrrupt context. This function is called without any lock held.
  10832. * It gets the hbalock to access and update SLI data structures. Note that,
  10833. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10834. * equal to that of FCP CQ index.
  10835. *
  10836. * The link attention and ELS ring attention events are handled
  10837. * by the worker thread. The interrupt handler signals the worker thread
  10838. * and returns for these events. This function is called without any lock
  10839. * held. It gets the hbalock to access and update SLI data structures.
  10840. *
  10841. * This function returns IRQ_HANDLED when interrupt is handled else it
  10842. * returns IRQ_NONE.
  10843. **/
  10844. irqreturn_t
  10845. lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
  10846. {
  10847. struct lpfc_hba *phba;
  10848. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10849. struct lpfc_queue *fpeq;
  10850. struct lpfc_eqe *eqe;
  10851. unsigned long iflag;
  10852. int ecount = 0;
  10853. int fcp_eqidx;
  10854. /* Get the driver's phba structure from the dev_id */
  10855. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10856. phba = fcp_eq_hdl->phba;
  10857. fcp_eqidx = fcp_eq_hdl->idx;
  10858. if (unlikely(!phba))
  10859. return IRQ_NONE;
  10860. if (unlikely(!phba->sli4_hba.hba_eq))
  10861. return IRQ_NONE;
  10862. /* Get to the EQ struct associated with this vector */
  10863. fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
  10864. if (unlikely(!fpeq))
  10865. return IRQ_NONE;
  10866. if (lpfc_fcp_look_ahead) {
  10867. if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
  10868. lpfc_sli4_eq_clr_intr(fpeq);
  10869. else {
  10870. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10871. return IRQ_NONE;
  10872. }
  10873. }
  10874. /* Check device state for handling interrupt */
  10875. if (unlikely(lpfc_intr_state_check(phba))) {
  10876. fpeq->EQ_badstate++;
  10877. /* Check again for link_state with lock held */
  10878. spin_lock_irqsave(&phba->hbalock, iflag);
  10879. if (phba->link_state < LPFC_LINK_DOWN)
  10880. /* Flush, clear interrupt, and rearm the EQ */
  10881. lpfc_sli4_eq_flush(phba, fpeq);
  10882. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10883. if (lpfc_fcp_look_ahead)
  10884. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10885. return IRQ_NONE;
  10886. }
  10887. /*
  10888. * Process all the event on FCP fast-path EQ
  10889. */
  10890. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10891. lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
  10892. if (!(++ecount % fpeq->entry_repost))
  10893. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10894. fpeq->EQ_processed++;
  10895. }
  10896. /* Track the max number of EQEs processed in 1 intr */
  10897. if (ecount > fpeq->EQ_max_eqe)
  10898. fpeq->EQ_max_eqe = ecount;
  10899. /* Always clear and re-arm the fast-path EQ */
  10900. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10901. if (unlikely(ecount == 0)) {
  10902. fpeq->EQ_no_entry++;
  10903. if (lpfc_fcp_look_ahead) {
  10904. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10905. return IRQ_NONE;
  10906. }
  10907. if (phba->intr_type == MSIX)
  10908. /* MSI-X treated interrupt served as no EQ share INT */
  10909. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10910. "0358 MSI-X interrupt with no EQE\n");
  10911. else
  10912. /* Non MSI-X treated on interrupt as EQ share INT */
  10913. return IRQ_NONE;
  10914. }
  10915. if (lpfc_fcp_look_ahead)
  10916. atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
  10917. return IRQ_HANDLED;
  10918. } /* lpfc_sli4_fp_intr_handler */
  10919. /**
  10920. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10921. * @irq: Interrupt number.
  10922. * @dev_id: The device context pointer.
  10923. *
  10924. * This function is the device-level interrupt handler to device with SLI-4
  10925. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10926. * interrupt mode is enabled and there is an event in the HBA which requires
  10927. * driver attention. This function invokes the slow-path interrupt attention
  10928. * handling function and fast-path interrupt attention handling function in
  10929. * turn to process the relevant HBA attention events. This function is called
  10930. * without any lock held. It gets the hbalock to access and update SLI data
  10931. * structures.
  10932. *
  10933. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10934. * returns IRQ_NONE.
  10935. **/
  10936. irqreturn_t
  10937. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10938. {
  10939. struct lpfc_hba *phba;
  10940. irqreturn_t hba_irq_rc;
  10941. bool hba_handled = false;
  10942. int fcp_eqidx;
  10943. /* Get the driver's phba structure from the dev_id */
  10944. phba = (struct lpfc_hba *)dev_id;
  10945. if (unlikely(!phba))
  10946. return IRQ_NONE;
  10947. /*
  10948. * Invoke fast-path host attention interrupt handling as appropriate.
  10949. */
  10950. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
  10951. hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
  10952. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10953. if (hba_irq_rc == IRQ_HANDLED)
  10954. hba_handled |= true;
  10955. }
  10956. return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
  10957. } /* lpfc_sli4_intr_handler */
  10958. /**
  10959. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10960. * @queue: The queue structure to free.
  10961. *
  10962. * This function frees a queue structure and the DMAable memory used for
  10963. * the host resident queue. This function must be called after destroying the
  10964. * queue on the HBA.
  10965. **/
  10966. void
  10967. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10968. {
  10969. struct lpfc_dmabuf *dmabuf;
  10970. if (!queue)
  10971. return;
  10972. while (!list_empty(&queue->page_list)) {
  10973. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10974. list);
  10975. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10976. dmabuf->virt, dmabuf->phys);
  10977. kfree(dmabuf);
  10978. }
  10979. kfree(queue);
  10980. return;
  10981. }
  10982. /**
  10983. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10984. * @phba: The HBA that this queue is being created on.
  10985. * @entry_size: The size of each queue entry for this queue.
  10986. * @entry count: The number of entries that this queue will handle.
  10987. *
  10988. * This function allocates a queue structure and the DMAable memory used for
  10989. * the host resident queue. This function must be called before creating the
  10990. * queue on the HBA.
  10991. **/
  10992. struct lpfc_queue *
  10993. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10994. uint32_t entry_count)
  10995. {
  10996. struct lpfc_queue *queue;
  10997. struct lpfc_dmabuf *dmabuf;
  10998. int x, total_qe_count;
  10999. void *dma_pointer;
  11000. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11001. if (!phba->sli4_hba.pc_sli4_params.supported)
  11002. hw_page_size = SLI4_PAGE_SIZE;
  11003. queue = kzalloc(sizeof(struct lpfc_queue) +
  11004. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  11005. if (!queue)
  11006. return NULL;
  11007. queue->page_count = (ALIGN(entry_size * entry_count,
  11008. hw_page_size))/hw_page_size;
  11009. INIT_LIST_HEAD(&queue->list);
  11010. INIT_LIST_HEAD(&queue->page_list);
  11011. INIT_LIST_HEAD(&queue->child_list);
  11012. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  11013. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  11014. if (!dmabuf)
  11015. goto out_fail;
  11016. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  11017. hw_page_size, &dmabuf->phys,
  11018. GFP_KERNEL);
  11019. if (!dmabuf->virt) {
  11020. kfree(dmabuf);
  11021. goto out_fail;
  11022. }
  11023. memset(dmabuf->virt, 0, hw_page_size);
  11024. dmabuf->buffer_tag = x;
  11025. list_add_tail(&dmabuf->list, &queue->page_list);
  11026. /* initialize queue's entry array */
  11027. dma_pointer = dmabuf->virt;
  11028. for (; total_qe_count < entry_count &&
  11029. dma_pointer < (hw_page_size + dmabuf->virt);
  11030. total_qe_count++, dma_pointer += entry_size) {
  11031. queue->qe[total_qe_count].address = dma_pointer;
  11032. }
  11033. }
  11034. queue->entry_size = entry_size;
  11035. queue->entry_count = entry_count;
  11036. /*
  11037. * entry_repost is calculated based on the number of entries in the
  11038. * queue. This works out except for RQs. If buffers are NOT initially
  11039. * posted for every RQE, entry_repost should be adjusted accordingly.
  11040. */
  11041. queue->entry_repost = (entry_count >> 3);
  11042. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  11043. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  11044. queue->phba = phba;
  11045. return queue;
  11046. out_fail:
  11047. lpfc_sli4_queue_free(queue);
  11048. return NULL;
  11049. }
  11050. /**
  11051. * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
  11052. * @phba: HBA structure that indicates port to create a queue on.
  11053. * @pci_barset: PCI BAR set flag.
  11054. *
  11055. * This function shall perform iomap of the specified PCI BAR address to host
  11056. * memory address if not already done so and return it. The returned host
  11057. * memory address can be NULL.
  11058. */
  11059. static void __iomem *
  11060. lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
  11061. {
  11062. struct pci_dev *pdev;
  11063. unsigned long bar_map, bar_map_len;
  11064. if (!phba->pcidev)
  11065. return NULL;
  11066. else
  11067. pdev = phba->pcidev;
  11068. switch (pci_barset) {
  11069. case WQ_PCI_BAR_0_AND_1:
  11070. if (!phba->pci_bar0_memmap_p) {
  11071. bar_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
  11072. bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
  11073. phba->pci_bar0_memmap_p = ioremap(bar_map, bar_map_len);
  11074. }
  11075. return phba->pci_bar0_memmap_p;
  11076. case WQ_PCI_BAR_2_AND_3:
  11077. if (!phba->pci_bar2_memmap_p) {
  11078. bar_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
  11079. bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
  11080. phba->pci_bar2_memmap_p = ioremap(bar_map, bar_map_len);
  11081. }
  11082. return phba->pci_bar2_memmap_p;
  11083. case WQ_PCI_BAR_4_AND_5:
  11084. if (!phba->pci_bar4_memmap_p) {
  11085. bar_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
  11086. bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
  11087. phba->pci_bar4_memmap_p = ioremap(bar_map, bar_map_len);
  11088. }
  11089. return phba->pci_bar4_memmap_p;
  11090. default:
  11091. break;
  11092. }
  11093. return NULL;
  11094. }
  11095. /**
  11096. * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
  11097. * @phba: HBA structure that indicates port to create a queue on.
  11098. * @startq: The starting FCP EQ to modify
  11099. *
  11100. * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
  11101. *
  11102. * The @phba struct is used to send mailbox command to HBA. The @startq
  11103. * is used to get the starting FCP EQ to change.
  11104. * This function is asynchronous and will wait for the mailbox
  11105. * command to finish before continuing.
  11106. *
  11107. * On success this function will return a zero. If unable to allocate enough
  11108. * memory this function will return -ENOMEM. If the queue create mailbox command
  11109. * fails this function will return -ENXIO.
  11110. **/
  11111. uint32_t
  11112. lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
  11113. {
  11114. struct lpfc_mbx_modify_eq_delay *eq_delay;
  11115. LPFC_MBOXQ_t *mbox;
  11116. struct lpfc_queue *eq;
  11117. int cnt, rc, length, status = 0;
  11118. uint32_t shdr_status, shdr_add_status;
  11119. uint32_t result;
  11120. int fcp_eqidx;
  11121. union lpfc_sli4_cfg_shdr *shdr;
  11122. uint16_t dmult;
  11123. if (startq >= phba->cfg_fcp_io_channel)
  11124. return 0;
  11125. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11126. if (!mbox)
  11127. return -ENOMEM;
  11128. length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
  11129. sizeof(struct lpfc_sli4_cfg_mhdr));
  11130. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11131. LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
  11132. length, LPFC_SLI4_MBX_EMBED);
  11133. eq_delay = &mbox->u.mqe.un.eq_delay;
  11134. /* Calculate delay multiper from maximum interrupt per second */
  11135. result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
  11136. if (result > LPFC_DMULT_CONST)
  11137. dmult = 0;
  11138. else
  11139. dmult = LPFC_DMULT_CONST/result - 1;
  11140. cnt = 0;
  11141. for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
  11142. fcp_eqidx++) {
  11143. eq = phba->sli4_hba.hba_eq[fcp_eqidx];
  11144. if (!eq)
  11145. continue;
  11146. eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
  11147. eq_delay->u.request.eq[cnt].phase = 0;
  11148. eq_delay->u.request.eq[cnt].delay_multi = dmult;
  11149. cnt++;
  11150. if (cnt >= LPFC_MAX_EQ_DELAY)
  11151. break;
  11152. }
  11153. eq_delay->u.request.num_eq = cnt;
  11154. mbox->vport = phba->pport;
  11155. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11156. mbox->context1 = NULL;
  11157. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11158. shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
  11159. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11160. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11161. if (shdr_status || shdr_add_status || rc) {
  11162. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11163. "2512 MODIFY_EQ_DELAY mailbox failed with "
  11164. "status x%x add_status x%x, mbx status x%x\n",
  11165. shdr_status, shdr_add_status, rc);
  11166. status = -ENXIO;
  11167. }
  11168. mempool_free(mbox, phba->mbox_mem_pool);
  11169. return status;
  11170. }
  11171. /**
  11172. * lpfc_eq_create - Create an Event Queue on the HBA
  11173. * @phba: HBA structure that indicates port to create a queue on.
  11174. * @eq: The queue structure to use to create the event queue.
  11175. * @imax: The maximum interrupt per second limit.
  11176. *
  11177. * This function creates an event queue, as detailed in @eq, on a port,
  11178. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  11179. *
  11180. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  11181. * is used to get the entry count and entry size that are necessary to
  11182. * determine the number of pages to allocate and use for this queue. This
  11183. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  11184. * event queue. This function is asynchronous and will wait for the mailbox
  11185. * command to finish before continuing.
  11186. *
  11187. * On success this function will return a zero. If unable to allocate enough
  11188. * memory this function will return -ENOMEM. If the queue create mailbox command
  11189. * fails this function will return -ENXIO.
  11190. **/
  11191. uint32_t
  11192. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
  11193. {
  11194. struct lpfc_mbx_eq_create *eq_create;
  11195. LPFC_MBOXQ_t *mbox;
  11196. int rc, length, status = 0;
  11197. struct lpfc_dmabuf *dmabuf;
  11198. uint32_t shdr_status, shdr_add_status;
  11199. union lpfc_sli4_cfg_shdr *shdr;
  11200. uint16_t dmult;
  11201. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11202. /* sanity check on queue memory */
  11203. if (!eq)
  11204. return -ENODEV;
  11205. if (!phba->sli4_hba.pc_sli4_params.supported)
  11206. hw_page_size = SLI4_PAGE_SIZE;
  11207. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11208. if (!mbox)
  11209. return -ENOMEM;
  11210. length = (sizeof(struct lpfc_mbx_eq_create) -
  11211. sizeof(struct lpfc_sli4_cfg_mhdr));
  11212. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11213. LPFC_MBOX_OPCODE_EQ_CREATE,
  11214. length, LPFC_SLI4_MBX_EMBED);
  11215. eq_create = &mbox->u.mqe.un.eq_create;
  11216. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  11217. eq->page_count);
  11218. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  11219. LPFC_EQE_SIZE);
  11220. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  11221. /* Calculate delay multiper from maximum interrupt per second */
  11222. if (imax > LPFC_DMULT_CONST)
  11223. dmult = 0;
  11224. else
  11225. dmult = LPFC_DMULT_CONST/imax - 1;
  11226. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11227. dmult);
  11228. switch (eq->entry_count) {
  11229. default:
  11230. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11231. "0360 Unsupported EQ count. (%d)\n",
  11232. eq->entry_count);
  11233. if (eq->entry_count < 256)
  11234. return -EINVAL;
  11235. /* otherwise default to smallest count (drop through) */
  11236. case 256:
  11237. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11238. LPFC_EQ_CNT_256);
  11239. break;
  11240. case 512:
  11241. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11242. LPFC_EQ_CNT_512);
  11243. break;
  11244. case 1024:
  11245. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11246. LPFC_EQ_CNT_1024);
  11247. break;
  11248. case 2048:
  11249. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11250. LPFC_EQ_CNT_2048);
  11251. break;
  11252. case 4096:
  11253. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11254. LPFC_EQ_CNT_4096);
  11255. break;
  11256. }
  11257. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11258. memset(dmabuf->virt, 0, hw_page_size);
  11259. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11260. putPaddrLow(dmabuf->phys);
  11261. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11262. putPaddrHigh(dmabuf->phys);
  11263. }
  11264. mbox->vport = phba->pport;
  11265. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11266. mbox->context1 = NULL;
  11267. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11268. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11269. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11270. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11271. if (shdr_status || shdr_add_status || rc) {
  11272. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11273. "2500 EQ_CREATE mailbox failed with "
  11274. "status x%x add_status x%x, mbx status x%x\n",
  11275. shdr_status, shdr_add_status, rc);
  11276. status = -ENXIO;
  11277. }
  11278. eq->type = LPFC_EQ;
  11279. eq->subtype = LPFC_NONE;
  11280. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11281. if (eq->queue_id == 0xFFFF)
  11282. status = -ENXIO;
  11283. eq->host_index = 0;
  11284. eq->hba_index = 0;
  11285. mempool_free(mbox, phba->mbox_mem_pool);
  11286. return status;
  11287. }
  11288. /**
  11289. * lpfc_cq_create - Create a Completion Queue on the HBA
  11290. * @phba: HBA structure that indicates port to create a queue on.
  11291. * @cq: The queue structure to use to create the completion queue.
  11292. * @eq: The event queue to bind this completion queue to.
  11293. *
  11294. * This function creates a completion queue, as detailed in @wq, on a port,
  11295. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11296. *
  11297. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11298. * is used to get the entry count and entry size that are necessary to
  11299. * determine the number of pages to allocate and use for this queue. The @eq
  11300. * is used to indicate which event queue to bind this completion queue to. This
  11301. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11302. * completion queue. This function is asynchronous and will wait for the mailbox
  11303. * command to finish before continuing.
  11304. *
  11305. * On success this function will return a zero. If unable to allocate enough
  11306. * memory this function will return -ENOMEM. If the queue create mailbox command
  11307. * fails this function will return -ENXIO.
  11308. **/
  11309. uint32_t
  11310. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11311. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11312. {
  11313. struct lpfc_mbx_cq_create *cq_create;
  11314. struct lpfc_dmabuf *dmabuf;
  11315. LPFC_MBOXQ_t *mbox;
  11316. int rc, length, status = 0;
  11317. uint32_t shdr_status, shdr_add_status;
  11318. union lpfc_sli4_cfg_shdr *shdr;
  11319. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11320. /* sanity check on queue memory */
  11321. if (!cq || !eq)
  11322. return -ENODEV;
  11323. if (!phba->sli4_hba.pc_sli4_params.supported)
  11324. hw_page_size = SLI4_PAGE_SIZE;
  11325. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11326. if (!mbox)
  11327. return -ENOMEM;
  11328. length = (sizeof(struct lpfc_mbx_cq_create) -
  11329. sizeof(struct lpfc_sli4_cfg_mhdr));
  11330. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11331. LPFC_MBOX_OPCODE_CQ_CREATE,
  11332. length, LPFC_SLI4_MBX_EMBED);
  11333. cq_create = &mbox->u.mqe.un.cq_create;
  11334. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11335. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11336. cq->page_count);
  11337. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11338. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11339. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11340. phba->sli4_hba.pc_sli4_params.cqv);
  11341. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11342. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11343. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11344. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11345. eq->queue_id);
  11346. } else {
  11347. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11348. eq->queue_id);
  11349. }
  11350. switch (cq->entry_count) {
  11351. default:
  11352. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11353. "0361 Unsupported CQ count. (%d)\n",
  11354. cq->entry_count);
  11355. if (cq->entry_count < 256) {
  11356. status = -EINVAL;
  11357. goto out;
  11358. }
  11359. /* otherwise default to smallest count (drop through) */
  11360. case 256:
  11361. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11362. LPFC_CQ_CNT_256);
  11363. break;
  11364. case 512:
  11365. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11366. LPFC_CQ_CNT_512);
  11367. break;
  11368. case 1024:
  11369. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11370. LPFC_CQ_CNT_1024);
  11371. break;
  11372. }
  11373. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11374. memset(dmabuf->virt, 0, hw_page_size);
  11375. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11376. putPaddrLow(dmabuf->phys);
  11377. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11378. putPaddrHigh(dmabuf->phys);
  11379. }
  11380. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11381. /* The IOCTL status is embedded in the mailbox subheader. */
  11382. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11383. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11384. if (shdr_status || shdr_add_status || rc) {
  11385. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11386. "2501 CQ_CREATE mailbox failed with "
  11387. "status x%x add_status x%x, mbx status x%x\n",
  11388. shdr_status, shdr_add_status, rc);
  11389. status = -ENXIO;
  11390. goto out;
  11391. }
  11392. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11393. if (cq->queue_id == 0xFFFF) {
  11394. status = -ENXIO;
  11395. goto out;
  11396. }
  11397. /* link the cq onto the parent eq child list */
  11398. list_add_tail(&cq->list, &eq->child_list);
  11399. /* Set up completion queue's type and subtype */
  11400. cq->type = type;
  11401. cq->subtype = subtype;
  11402. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11403. cq->assoc_qid = eq->queue_id;
  11404. cq->host_index = 0;
  11405. cq->hba_index = 0;
  11406. out:
  11407. mempool_free(mbox, phba->mbox_mem_pool);
  11408. return status;
  11409. }
  11410. /**
  11411. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11412. * @phba: HBA structure that indicates port to create a queue on.
  11413. * @mq: The queue structure to use to create the mailbox queue.
  11414. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11415. * @cq: The completion queue to associate with this cq.
  11416. *
  11417. * This function provides failback (fb) functionality when the
  11418. * mq_create_ext fails on older FW generations. It's purpose is identical
  11419. * to mq_create_ext otherwise.
  11420. *
  11421. * This routine cannot fail as all attributes were previously accessed and
  11422. * initialized in mq_create_ext.
  11423. **/
  11424. static void
  11425. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11426. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11427. {
  11428. struct lpfc_mbx_mq_create *mq_create;
  11429. struct lpfc_dmabuf *dmabuf;
  11430. int length;
  11431. length = (sizeof(struct lpfc_mbx_mq_create) -
  11432. sizeof(struct lpfc_sli4_cfg_mhdr));
  11433. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11434. LPFC_MBOX_OPCODE_MQ_CREATE,
  11435. length, LPFC_SLI4_MBX_EMBED);
  11436. mq_create = &mbox->u.mqe.un.mq_create;
  11437. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11438. mq->page_count);
  11439. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11440. cq->queue_id);
  11441. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11442. switch (mq->entry_count) {
  11443. case 16:
  11444. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11445. LPFC_MQ_RING_SIZE_16);
  11446. break;
  11447. case 32:
  11448. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11449. LPFC_MQ_RING_SIZE_32);
  11450. break;
  11451. case 64:
  11452. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11453. LPFC_MQ_RING_SIZE_64);
  11454. break;
  11455. case 128:
  11456. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11457. LPFC_MQ_RING_SIZE_128);
  11458. break;
  11459. }
  11460. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11461. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11462. putPaddrLow(dmabuf->phys);
  11463. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11464. putPaddrHigh(dmabuf->phys);
  11465. }
  11466. }
  11467. /**
  11468. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11469. * @phba: HBA structure that indicates port to create a queue on.
  11470. * @mq: The queue structure to use to create the mailbox queue.
  11471. * @cq: The completion queue to associate with this cq.
  11472. * @subtype: The queue's subtype.
  11473. *
  11474. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11475. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11476. *
  11477. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11478. * is used to get the entry count and entry size that are necessary to
  11479. * determine the number of pages to allocate and use for this queue. This
  11480. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11481. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11482. * command to finish before continuing.
  11483. *
  11484. * On success this function will return a zero. If unable to allocate enough
  11485. * memory this function will return -ENOMEM. If the queue create mailbox command
  11486. * fails this function will return -ENXIO.
  11487. **/
  11488. int32_t
  11489. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11490. struct lpfc_queue *cq, uint32_t subtype)
  11491. {
  11492. struct lpfc_mbx_mq_create *mq_create;
  11493. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11494. struct lpfc_dmabuf *dmabuf;
  11495. LPFC_MBOXQ_t *mbox;
  11496. int rc, length, status = 0;
  11497. uint32_t shdr_status, shdr_add_status;
  11498. union lpfc_sli4_cfg_shdr *shdr;
  11499. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11500. /* sanity check on queue memory */
  11501. if (!mq || !cq)
  11502. return -ENODEV;
  11503. if (!phba->sli4_hba.pc_sli4_params.supported)
  11504. hw_page_size = SLI4_PAGE_SIZE;
  11505. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11506. if (!mbox)
  11507. return -ENOMEM;
  11508. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11509. sizeof(struct lpfc_sli4_cfg_mhdr));
  11510. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11511. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11512. length, LPFC_SLI4_MBX_EMBED);
  11513. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11514. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11515. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11516. &mq_create_ext->u.request, mq->page_count);
  11517. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11518. &mq_create_ext->u.request, 1);
  11519. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11520. &mq_create_ext->u.request, 1);
  11521. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11522. &mq_create_ext->u.request, 1);
  11523. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11524. &mq_create_ext->u.request, 1);
  11525. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11526. &mq_create_ext->u.request, 1);
  11527. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11528. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11529. phba->sli4_hba.pc_sli4_params.mqv);
  11530. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11531. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11532. cq->queue_id);
  11533. else
  11534. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11535. cq->queue_id);
  11536. switch (mq->entry_count) {
  11537. default:
  11538. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11539. "0362 Unsupported MQ count. (%d)\n",
  11540. mq->entry_count);
  11541. if (mq->entry_count < 16) {
  11542. status = -EINVAL;
  11543. goto out;
  11544. }
  11545. /* otherwise default to smallest count (drop through) */
  11546. case 16:
  11547. bf_set(lpfc_mq_context_ring_size,
  11548. &mq_create_ext->u.request.context,
  11549. LPFC_MQ_RING_SIZE_16);
  11550. break;
  11551. case 32:
  11552. bf_set(lpfc_mq_context_ring_size,
  11553. &mq_create_ext->u.request.context,
  11554. LPFC_MQ_RING_SIZE_32);
  11555. break;
  11556. case 64:
  11557. bf_set(lpfc_mq_context_ring_size,
  11558. &mq_create_ext->u.request.context,
  11559. LPFC_MQ_RING_SIZE_64);
  11560. break;
  11561. case 128:
  11562. bf_set(lpfc_mq_context_ring_size,
  11563. &mq_create_ext->u.request.context,
  11564. LPFC_MQ_RING_SIZE_128);
  11565. break;
  11566. }
  11567. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11568. memset(dmabuf->virt, 0, hw_page_size);
  11569. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11570. putPaddrLow(dmabuf->phys);
  11571. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11572. putPaddrHigh(dmabuf->phys);
  11573. }
  11574. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11575. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11576. &mq_create_ext->u.response);
  11577. if (rc != MBX_SUCCESS) {
  11578. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11579. "2795 MQ_CREATE_EXT failed with "
  11580. "status x%x. Failback to MQ_CREATE.\n",
  11581. rc);
  11582. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11583. mq_create = &mbox->u.mqe.un.mq_create;
  11584. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11585. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11586. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11587. &mq_create->u.response);
  11588. }
  11589. /* The IOCTL status is embedded in the mailbox subheader. */
  11590. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11591. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11592. if (shdr_status || shdr_add_status || rc) {
  11593. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11594. "2502 MQ_CREATE mailbox failed with "
  11595. "status x%x add_status x%x, mbx status x%x\n",
  11596. shdr_status, shdr_add_status, rc);
  11597. status = -ENXIO;
  11598. goto out;
  11599. }
  11600. if (mq->queue_id == 0xFFFF) {
  11601. status = -ENXIO;
  11602. goto out;
  11603. }
  11604. mq->type = LPFC_MQ;
  11605. mq->assoc_qid = cq->queue_id;
  11606. mq->subtype = subtype;
  11607. mq->host_index = 0;
  11608. mq->hba_index = 0;
  11609. /* link the mq onto the parent cq child list */
  11610. list_add_tail(&mq->list, &cq->child_list);
  11611. out:
  11612. mempool_free(mbox, phba->mbox_mem_pool);
  11613. return status;
  11614. }
  11615. /**
  11616. * lpfc_wq_create - Create a Work Queue on the HBA
  11617. * @phba: HBA structure that indicates port to create a queue on.
  11618. * @wq: The queue structure to use to create the work queue.
  11619. * @cq: The completion queue to bind this work queue to.
  11620. * @subtype: The subtype of the work queue indicating its functionality.
  11621. *
  11622. * This function creates a work queue, as detailed in @wq, on a port, described
  11623. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11624. *
  11625. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11626. * is used to get the entry count and entry size that are necessary to
  11627. * determine the number of pages to allocate and use for this queue. The @cq
  11628. * is used to indicate which completion queue to bind this work queue to. This
  11629. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11630. * work queue. This function is asynchronous and will wait for the mailbox
  11631. * command to finish before continuing.
  11632. *
  11633. * On success this function will return a zero. If unable to allocate enough
  11634. * memory this function will return -ENOMEM. If the queue create mailbox command
  11635. * fails this function will return -ENXIO.
  11636. **/
  11637. uint32_t
  11638. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11639. struct lpfc_queue *cq, uint32_t subtype)
  11640. {
  11641. struct lpfc_mbx_wq_create *wq_create;
  11642. struct lpfc_dmabuf *dmabuf;
  11643. LPFC_MBOXQ_t *mbox;
  11644. int rc, length, status = 0;
  11645. uint32_t shdr_status, shdr_add_status;
  11646. union lpfc_sli4_cfg_shdr *shdr;
  11647. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11648. struct dma_address *page;
  11649. void __iomem *bar_memmap_p;
  11650. uint32_t db_offset;
  11651. uint16_t pci_barset;
  11652. /* sanity check on queue memory */
  11653. if (!wq || !cq)
  11654. return -ENODEV;
  11655. if (!phba->sli4_hba.pc_sli4_params.supported)
  11656. hw_page_size = SLI4_PAGE_SIZE;
  11657. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11658. if (!mbox)
  11659. return -ENOMEM;
  11660. length = (sizeof(struct lpfc_mbx_wq_create) -
  11661. sizeof(struct lpfc_sli4_cfg_mhdr));
  11662. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11663. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11664. length, LPFC_SLI4_MBX_EMBED);
  11665. wq_create = &mbox->u.mqe.un.wq_create;
  11666. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11667. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11668. wq->page_count);
  11669. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11670. cq->queue_id);
  11671. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11672. phba->sli4_hba.pc_sli4_params.wqv);
  11673. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11674. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11675. wq->entry_count);
  11676. switch (wq->entry_size) {
  11677. default:
  11678. case 64:
  11679. bf_set(lpfc_mbx_wq_create_wqe_size,
  11680. &wq_create->u.request_1,
  11681. LPFC_WQ_WQE_SIZE_64);
  11682. break;
  11683. case 128:
  11684. bf_set(lpfc_mbx_wq_create_wqe_size,
  11685. &wq_create->u.request_1,
  11686. LPFC_WQ_WQE_SIZE_128);
  11687. break;
  11688. }
  11689. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11690. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11691. page = wq_create->u.request_1.page;
  11692. } else {
  11693. page = wq_create->u.request.page;
  11694. }
  11695. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11696. memset(dmabuf->virt, 0, hw_page_size);
  11697. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11698. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11699. }
  11700. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  11701. bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
  11702. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11703. /* The IOCTL status is embedded in the mailbox subheader. */
  11704. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11705. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11706. if (shdr_status || shdr_add_status || rc) {
  11707. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11708. "2503 WQ_CREATE mailbox failed with "
  11709. "status x%x add_status x%x, mbx status x%x\n",
  11710. shdr_status, shdr_add_status, rc);
  11711. status = -ENXIO;
  11712. goto out;
  11713. }
  11714. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11715. if (wq->queue_id == 0xFFFF) {
  11716. status = -ENXIO;
  11717. goto out;
  11718. }
  11719. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  11720. wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
  11721. &wq_create->u.response);
  11722. if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
  11723. (wq->db_format != LPFC_DB_RING_FORMAT)) {
  11724. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11725. "3265 WQ[%d] doorbell format not "
  11726. "supported: x%x\n", wq->queue_id,
  11727. wq->db_format);
  11728. status = -EINVAL;
  11729. goto out;
  11730. }
  11731. pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
  11732. &wq_create->u.response);
  11733. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  11734. if (!bar_memmap_p) {
  11735. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11736. "3263 WQ[%d] failed to memmap pci "
  11737. "barset:x%x\n", wq->queue_id,
  11738. pci_barset);
  11739. status = -ENOMEM;
  11740. goto out;
  11741. }
  11742. db_offset = wq_create->u.response.doorbell_offset;
  11743. if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
  11744. (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
  11745. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11746. "3252 WQ[%d] doorbell offset not "
  11747. "supported: x%x\n", wq->queue_id,
  11748. db_offset);
  11749. status = -EINVAL;
  11750. goto out;
  11751. }
  11752. wq->db_regaddr = bar_memmap_p + db_offset;
  11753. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11754. "3264 WQ[%d]: barset:x%x, offset:x%x\n",
  11755. wq->queue_id, pci_barset, db_offset);
  11756. } else {
  11757. wq->db_format = LPFC_DB_LIST_FORMAT;
  11758. wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
  11759. }
  11760. wq->type = LPFC_WQ;
  11761. wq->assoc_qid = cq->queue_id;
  11762. wq->subtype = subtype;
  11763. wq->host_index = 0;
  11764. wq->hba_index = 0;
  11765. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11766. /* link the wq onto the parent cq child list */
  11767. list_add_tail(&wq->list, &cq->child_list);
  11768. out:
  11769. mempool_free(mbox, phba->mbox_mem_pool);
  11770. return status;
  11771. }
  11772. /**
  11773. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11774. * @phba: HBA structure that indicates port to create a queue on.
  11775. * @rq: The queue structure to use for the receive queue.
  11776. * @qno: The associated HBQ number
  11777. *
  11778. *
  11779. * For SLI4 we need to adjust the RQ repost value based on
  11780. * the number of buffers that are initially posted to the RQ.
  11781. */
  11782. void
  11783. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11784. {
  11785. uint32_t cnt;
  11786. /* sanity check on queue memory */
  11787. if (!rq)
  11788. return;
  11789. cnt = lpfc_hbq_defs[qno]->entry_count;
  11790. /* Recalc repost for RQs based on buffers initially posted */
  11791. cnt = (cnt >> 3);
  11792. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11793. cnt = LPFC_QUEUE_MIN_REPOST;
  11794. rq->entry_repost = cnt;
  11795. }
  11796. /**
  11797. * lpfc_rq_create - Create a Receive Queue on the HBA
  11798. * @phba: HBA structure that indicates port to create a queue on.
  11799. * @hrq: The queue structure to use to create the header receive queue.
  11800. * @drq: The queue structure to use to create the data receive queue.
  11801. * @cq: The completion queue to bind this work queue to.
  11802. *
  11803. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11804. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11805. * to the HBA.
  11806. *
  11807. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11808. * struct is used to get the entry count that is necessary to determine the
  11809. * number of pages to use for this queue. The @cq is used to indicate which
  11810. * completion queue to bind received buffers that are posted to these queues to.
  11811. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11812. * receive queue pair. This function is asynchronous and will wait for the
  11813. * mailbox command to finish before continuing.
  11814. *
  11815. * On success this function will return a zero. If unable to allocate enough
  11816. * memory this function will return -ENOMEM. If the queue create mailbox command
  11817. * fails this function will return -ENXIO.
  11818. **/
  11819. uint32_t
  11820. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11821. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11822. {
  11823. struct lpfc_mbx_rq_create *rq_create;
  11824. struct lpfc_dmabuf *dmabuf;
  11825. LPFC_MBOXQ_t *mbox;
  11826. int rc, length, status = 0;
  11827. uint32_t shdr_status, shdr_add_status;
  11828. union lpfc_sli4_cfg_shdr *shdr;
  11829. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11830. void __iomem *bar_memmap_p;
  11831. uint32_t db_offset;
  11832. uint16_t pci_barset;
  11833. /* sanity check on queue memory */
  11834. if (!hrq || !drq || !cq)
  11835. return -ENODEV;
  11836. if (!phba->sli4_hba.pc_sli4_params.supported)
  11837. hw_page_size = SLI4_PAGE_SIZE;
  11838. if (hrq->entry_count != drq->entry_count)
  11839. return -EINVAL;
  11840. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11841. if (!mbox)
  11842. return -ENOMEM;
  11843. length = (sizeof(struct lpfc_mbx_rq_create) -
  11844. sizeof(struct lpfc_sli4_cfg_mhdr));
  11845. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11846. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11847. length, LPFC_SLI4_MBX_EMBED);
  11848. rq_create = &mbox->u.mqe.un.rq_create;
  11849. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11850. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11851. phba->sli4_hba.pc_sli4_params.rqv);
  11852. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11853. bf_set(lpfc_rq_context_rqe_count_1,
  11854. &rq_create->u.request.context,
  11855. hrq->entry_count);
  11856. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11857. bf_set(lpfc_rq_context_rqe_size,
  11858. &rq_create->u.request.context,
  11859. LPFC_RQE_SIZE_8);
  11860. bf_set(lpfc_rq_context_page_size,
  11861. &rq_create->u.request.context,
  11862. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11863. } else {
  11864. switch (hrq->entry_count) {
  11865. default:
  11866. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11867. "2535 Unsupported RQ count. (%d)\n",
  11868. hrq->entry_count);
  11869. if (hrq->entry_count < 512) {
  11870. status = -EINVAL;
  11871. goto out;
  11872. }
  11873. /* otherwise default to smallest count (drop through) */
  11874. case 512:
  11875. bf_set(lpfc_rq_context_rqe_count,
  11876. &rq_create->u.request.context,
  11877. LPFC_RQ_RING_SIZE_512);
  11878. break;
  11879. case 1024:
  11880. bf_set(lpfc_rq_context_rqe_count,
  11881. &rq_create->u.request.context,
  11882. LPFC_RQ_RING_SIZE_1024);
  11883. break;
  11884. case 2048:
  11885. bf_set(lpfc_rq_context_rqe_count,
  11886. &rq_create->u.request.context,
  11887. LPFC_RQ_RING_SIZE_2048);
  11888. break;
  11889. case 4096:
  11890. bf_set(lpfc_rq_context_rqe_count,
  11891. &rq_create->u.request.context,
  11892. LPFC_RQ_RING_SIZE_4096);
  11893. break;
  11894. }
  11895. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11896. LPFC_HDR_BUF_SIZE);
  11897. }
  11898. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11899. cq->queue_id);
  11900. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11901. hrq->page_count);
  11902. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11903. memset(dmabuf->virt, 0, hw_page_size);
  11904. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11905. putPaddrLow(dmabuf->phys);
  11906. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11907. putPaddrHigh(dmabuf->phys);
  11908. }
  11909. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  11910. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  11911. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11912. /* The IOCTL status is embedded in the mailbox subheader. */
  11913. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11914. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11915. if (shdr_status || shdr_add_status || rc) {
  11916. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11917. "2504 RQ_CREATE mailbox failed with "
  11918. "status x%x add_status x%x, mbx status x%x\n",
  11919. shdr_status, shdr_add_status, rc);
  11920. status = -ENXIO;
  11921. goto out;
  11922. }
  11923. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11924. if (hrq->queue_id == 0xFFFF) {
  11925. status = -ENXIO;
  11926. goto out;
  11927. }
  11928. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
  11929. hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
  11930. &rq_create->u.response);
  11931. if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
  11932. (hrq->db_format != LPFC_DB_RING_FORMAT)) {
  11933. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11934. "3262 RQ [%d] doorbell format not "
  11935. "supported: x%x\n", hrq->queue_id,
  11936. hrq->db_format);
  11937. status = -EINVAL;
  11938. goto out;
  11939. }
  11940. pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
  11941. &rq_create->u.response);
  11942. bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
  11943. if (!bar_memmap_p) {
  11944. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11945. "3269 RQ[%d] failed to memmap pci "
  11946. "barset:x%x\n", hrq->queue_id,
  11947. pci_barset);
  11948. status = -ENOMEM;
  11949. goto out;
  11950. }
  11951. db_offset = rq_create->u.response.doorbell_offset;
  11952. if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
  11953. (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
  11954. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11955. "3270 RQ[%d] doorbell offset not "
  11956. "supported: x%x\n", hrq->queue_id,
  11957. db_offset);
  11958. status = -EINVAL;
  11959. goto out;
  11960. }
  11961. hrq->db_regaddr = bar_memmap_p + db_offset;
  11962. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11963. "3266 RQ[qid:%d]: barset:x%x, offset:x%x\n",
  11964. hrq->queue_id, pci_barset, db_offset);
  11965. } else {
  11966. hrq->db_format = LPFC_DB_RING_FORMAT;
  11967. hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
  11968. }
  11969. hrq->type = LPFC_HRQ;
  11970. hrq->assoc_qid = cq->queue_id;
  11971. hrq->subtype = subtype;
  11972. hrq->host_index = 0;
  11973. hrq->hba_index = 0;
  11974. /* now create the data queue */
  11975. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11976. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11977. length, LPFC_SLI4_MBX_EMBED);
  11978. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11979. phba->sli4_hba.pc_sli4_params.rqv);
  11980. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11981. bf_set(lpfc_rq_context_rqe_count_1,
  11982. &rq_create->u.request.context, hrq->entry_count);
  11983. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11984. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11985. LPFC_RQE_SIZE_8);
  11986. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11987. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11988. } else {
  11989. switch (drq->entry_count) {
  11990. default:
  11991. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11992. "2536 Unsupported RQ count. (%d)\n",
  11993. drq->entry_count);
  11994. if (drq->entry_count < 512) {
  11995. status = -EINVAL;
  11996. goto out;
  11997. }
  11998. /* otherwise default to smallest count (drop through) */
  11999. case 512:
  12000. bf_set(lpfc_rq_context_rqe_count,
  12001. &rq_create->u.request.context,
  12002. LPFC_RQ_RING_SIZE_512);
  12003. break;
  12004. case 1024:
  12005. bf_set(lpfc_rq_context_rqe_count,
  12006. &rq_create->u.request.context,
  12007. LPFC_RQ_RING_SIZE_1024);
  12008. break;
  12009. case 2048:
  12010. bf_set(lpfc_rq_context_rqe_count,
  12011. &rq_create->u.request.context,
  12012. LPFC_RQ_RING_SIZE_2048);
  12013. break;
  12014. case 4096:
  12015. bf_set(lpfc_rq_context_rqe_count,
  12016. &rq_create->u.request.context,
  12017. LPFC_RQ_RING_SIZE_4096);
  12018. break;
  12019. }
  12020. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  12021. LPFC_DATA_BUF_SIZE);
  12022. }
  12023. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  12024. cq->queue_id);
  12025. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  12026. drq->page_count);
  12027. list_for_each_entry(dmabuf, &drq->page_list, list) {
  12028. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  12029. putPaddrLow(dmabuf->phys);
  12030. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  12031. putPaddrHigh(dmabuf->phys);
  12032. }
  12033. if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
  12034. bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
  12035. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12036. /* The IOCTL status is embedded in the mailbox subheader. */
  12037. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  12038. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12039. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12040. if (shdr_status || shdr_add_status || rc) {
  12041. status = -ENXIO;
  12042. goto out;
  12043. }
  12044. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  12045. if (drq->queue_id == 0xFFFF) {
  12046. status = -ENXIO;
  12047. goto out;
  12048. }
  12049. drq->type = LPFC_DRQ;
  12050. drq->assoc_qid = cq->queue_id;
  12051. drq->subtype = subtype;
  12052. drq->host_index = 0;
  12053. drq->hba_index = 0;
  12054. /* link the header and data RQs onto the parent cq child list */
  12055. list_add_tail(&hrq->list, &cq->child_list);
  12056. list_add_tail(&drq->list, &cq->child_list);
  12057. out:
  12058. mempool_free(mbox, phba->mbox_mem_pool);
  12059. return status;
  12060. }
  12061. /**
  12062. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  12063. * @eq: The queue structure associated with the queue to destroy.
  12064. *
  12065. * This function destroys a queue, as detailed in @eq by sending an mailbox
  12066. * command, specific to the type of queue, to the HBA.
  12067. *
  12068. * The @eq struct is used to get the queue ID of the queue to destroy.
  12069. *
  12070. * On success this function will return a zero. If the queue destroy mailbox
  12071. * command fails this function will return -ENXIO.
  12072. **/
  12073. uint32_t
  12074. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  12075. {
  12076. LPFC_MBOXQ_t *mbox;
  12077. int rc, length, status = 0;
  12078. uint32_t shdr_status, shdr_add_status;
  12079. union lpfc_sli4_cfg_shdr *shdr;
  12080. /* sanity check on queue memory */
  12081. if (!eq)
  12082. return -ENODEV;
  12083. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  12084. if (!mbox)
  12085. return -ENOMEM;
  12086. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  12087. sizeof(struct lpfc_sli4_cfg_mhdr));
  12088. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12089. LPFC_MBOX_OPCODE_EQ_DESTROY,
  12090. length, LPFC_SLI4_MBX_EMBED);
  12091. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  12092. eq->queue_id);
  12093. mbox->vport = eq->phba->pport;
  12094. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12095. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  12096. /* The IOCTL status is embedded in the mailbox subheader. */
  12097. shdr = (union lpfc_sli4_cfg_shdr *)
  12098. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  12099. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12100. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12101. if (shdr_status || shdr_add_status || rc) {
  12102. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12103. "2505 EQ_DESTROY mailbox failed with "
  12104. "status x%x add_status x%x, mbx status x%x\n",
  12105. shdr_status, shdr_add_status, rc);
  12106. status = -ENXIO;
  12107. }
  12108. /* Remove eq from any list */
  12109. list_del_init(&eq->list);
  12110. mempool_free(mbox, eq->phba->mbox_mem_pool);
  12111. return status;
  12112. }
  12113. /**
  12114. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  12115. * @cq: The queue structure associated with the queue to destroy.
  12116. *
  12117. * This function destroys a queue, as detailed in @cq by sending an mailbox
  12118. * command, specific to the type of queue, to the HBA.
  12119. *
  12120. * The @cq struct is used to get the queue ID of the queue to destroy.
  12121. *
  12122. * On success this function will return a zero. If the queue destroy mailbox
  12123. * command fails this function will return -ENXIO.
  12124. **/
  12125. uint32_t
  12126. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  12127. {
  12128. LPFC_MBOXQ_t *mbox;
  12129. int rc, length, status = 0;
  12130. uint32_t shdr_status, shdr_add_status;
  12131. union lpfc_sli4_cfg_shdr *shdr;
  12132. /* sanity check on queue memory */
  12133. if (!cq)
  12134. return -ENODEV;
  12135. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  12136. if (!mbox)
  12137. return -ENOMEM;
  12138. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  12139. sizeof(struct lpfc_sli4_cfg_mhdr));
  12140. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12141. LPFC_MBOX_OPCODE_CQ_DESTROY,
  12142. length, LPFC_SLI4_MBX_EMBED);
  12143. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  12144. cq->queue_id);
  12145. mbox->vport = cq->phba->pport;
  12146. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12147. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  12148. /* The IOCTL status is embedded in the mailbox subheader. */
  12149. shdr = (union lpfc_sli4_cfg_shdr *)
  12150. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  12151. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12152. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12153. if (shdr_status || shdr_add_status || rc) {
  12154. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12155. "2506 CQ_DESTROY mailbox failed with "
  12156. "status x%x add_status x%x, mbx status x%x\n",
  12157. shdr_status, shdr_add_status, rc);
  12158. status = -ENXIO;
  12159. }
  12160. /* Remove cq from any list */
  12161. list_del_init(&cq->list);
  12162. mempool_free(mbox, cq->phba->mbox_mem_pool);
  12163. return status;
  12164. }
  12165. /**
  12166. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  12167. * @qm: The queue structure associated with the queue to destroy.
  12168. *
  12169. * This function destroys a queue, as detailed in @mq by sending an mailbox
  12170. * command, specific to the type of queue, to the HBA.
  12171. *
  12172. * The @mq struct is used to get the queue ID of the queue to destroy.
  12173. *
  12174. * On success this function will return a zero. If the queue destroy mailbox
  12175. * command fails this function will return -ENXIO.
  12176. **/
  12177. uint32_t
  12178. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  12179. {
  12180. LPFC_MBOXQ_t *mbox;
  12181. int rc, length, status = 0;
  12182. uint32_t shdr_status, shdr_add_status;
  12183. union lpfc_sli4_cfg_shdr *shdr;
  12184. /* sanity check on queue memory */
  12185. if (!mq)
  12186. return -ENODEV;
  12187. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  12188. if (!mbox)
  12189. return -ENOMEM;
  12190. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  12191. sizeof(struct lpfc_sli4_cfg_mhdr));
  12192. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  12193. LPFC_MBOX_OPCODE_MQ_DESTROY,
  12194. length, LPFC_SLI4_MBX_EMBED);
  12195. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  12196. mq->queue_id);
  12197. mbox->vport = mq->phba->pport;
  12198. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12199. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  12200. /* The IOCTL status is embedded in the mailbox subheader. */
  12201. shdr = (union lpfc_sli4_cfg_shdr *)
  12202. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  12203. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12204. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12205. if (shdr_status || shdr_add_status || rc) {
  12206. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12207. "2507 MQ_DESTROY mailbox failed with "
  12208. "status x%x add_status x%x, mbx status x%x\n",
  12209. shdr_status, shdr_add_status, rc);
  12210. status = -ENXIO;
  12211. }
  12212. /* Remove mq from any list */
  12213. list_del_init(&mq->list);
  12214. mempool_free(mbox, mq->phba->mbox_mem_pool);
  12215. return status;
  12216. }
  12217. /**
  12218. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  12219. * @wq: The queue structure associated with the queue to destroy.
  12220. *
  12221. * This function destroys a queue, as detailed in @wq by sending an mailbox
  12222. * command, specific to the type of queue, to the HBA.
  12223. *
  12224. * The @wq struct is used to get the queue ID of the queue to destroy.
  12225. *
  12226. * On success this function will return a zero. If the queue destroy mailbox
  12227. * command fails this function will return -ENXIO.
  12228. **/
  12229. uint32_t
  12230. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  12231. {
  12232. LPFC_MBOXQ_t *mbox;
  12233. int rc, length, status = 0;
  12234. uint32_t shdr_status, shdr_add_status;
  12235. union lpfc_sli4_cfg_shdr *shdr;
  12236. /* sanity check on queue memory */
  12237. if (!wq)
  12238. return -ENODEV;
  12239. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  12240. if (!mbox)
  12241. return -ENOMEM;
  12242. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  12243. sizeof(struct lpfc_sli4_cfg_mhdr));
  12244. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12245. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  12246. length, LPFC_SLI4_MBX_EMBED);
  12247. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  12248. wq->queue_id);
  12249. mbox->vport = wq->phba->pport;
  12250. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12251. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  12252. shdr = (union lpfc_sli4_cfg_shdr *)
  12253. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  12254. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12255. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12256. if (shdr_status || shdr_add_status || rc) {
  12257. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12258. "2508 WQ_DESTROY mailbox failed with "
  12259. "status x%x add_status x%x, mbx status x%x\n",
  12260. shdr_status, shdr_add_status, rc);
  12261. status = -ENXIO;
  12262. }
  12263. /* Remove wq from any list */
  12264. list_del_init(&wq->list);
  12265. mempool_free(mbox, wq->phba->mbox_mem_pool);
  12266. return status;
  12267. }
  12268. /**
  12269. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  12270. * @rq: The queue structure associated with the queue to destroy.
  12271. *
  12272. * This function destroys a queue, as detailed in @rq by sending an mailbox
  12273. * command, specific to the type of queue, to the HBA.
  12274. *
  12275. * The @rq struct is used to get the queue ID of the queue to destroy.
  12276. *
  12277. * On success this function will return a zero. If the queue destroy mailbox
  12278. * command fails this function will return -ENXIO.
  12279. **/
  12280. uint32_t
  12281. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  12282. struct lpfc_queue *drq)
  12283. {
  12284. LPFC_MBOXQ_t *mbox;
  12285. int rc, length, status = 0;
  12286. uint32_t shdr_status, shdr_add_status;
  12287. union lpfc_sli4_cfg_shdr *shdr;
  12288. /* sanity check on queue memory */
  12289. if (!hrq || !drq)
  12290. return -ENODEV;
  12291. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  12292. if (!mbox)
  12293. return -ENOMEM;
  12294. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  12295. sizeof(struct lpfc_sli4_cfg_mhdr));
  12296. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12297. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  12298. length, LPFC_SLI4_MBX_EMBED);
  12299. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12300. hrq->queue_id);
  12301. mbox->vport = hrq->phba->pport;
  12302. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  12303. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  12304. /* The IOCTL status is embedded in the mailbox subheader. */
  12305. shdr = (union lpfc_sli4_cfg_shdr *)
  12306. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12307. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12308. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12309. if (shdr_status || shdr_add_status || rc) {
  12310. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12311. "2509 RQ_DESTROY mailbox failed with "
  12312. "status x%x add_status x%x, mbx status x%x\n",
  12313. shdr_status, shdr_add_status, rc);
  12314. if (rc != MBX_TIMEOUT)
  12315. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12316. return -ENXIO;
  12317. }
  12318. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  12319. drq->queue_id);
  12320. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  12321. shdr = (union lpfc_sli4_cfg_shdr *)
  12322. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  12323. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12324. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12325. if (shdr_status || shdr_add_status || rc) {
  12326. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12327. "2510 RQ_DESTROY mailbox failed with "
  12328. "status x%x add_status x%x, mbx status x%x\n",
  12329. shdr_status, shdr_add_status, rc);
  12330. status = -ENXIO;
  12331. }
  12332. list_del_init(&hrq->list);
  12333. list_del_init(&drq->list);
  12334. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12335. return status;
  12336. }
  12337. /**
  12338. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12339. * @phba: The virtual port for which this call being executed.
  12340. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12341. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12342. * @xritag: the xritag that ties this io to the SGL pages.
  12343. *
  12344. * This routine will post the sgl pages for the IO that has the xritag
  12345. * that is in the iocbq structure. The xritag is assigned during iocbq
  12346. * creation and persists for as long as the driver is loaded.
  12347. * if the caller has fewer than 256 scatter gather segments to map then
  12348. * pdma_phys_addr1 should be 0.
  12349. * If the caller needs to map more than 256 scatter gather segment then
  12350. * pdma_phys_addr1 should be a valid physical address.
  12351. * physical address for SGLs must be 64 byte aligned.
  12352. * If you are going to map 2 SGL's then the first one must have 256 entries
  12353. * the second sgl can have between 1 and 256 entries.
  12354. *
  12355. * Return codes:
  12356. * 0 - Success
  12357. * -ENXIO, -ENOMEM - Failure
  12358. **/
  12359. int
  12360. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12361. dma_addr_t pdma_phys_addr0,
  12362. dma_addr_t pdma_phys_addr1,
  12363. uint16_t xritag)
  12364. {
  12365. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12366. LPFC_MBOXQ_t *mbox;
  12367. int rc;
  12368. uint32_t shdr_status, shdr_add_status;
  12369. uint32_t mbox_tmo;
  12370. union lpfc_sli4_cfg_shdr *shdr;
  12371. if (xritag == NO_XRI) {
  12372. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12373. "0364 Invalid param:\n");
  12374. return -EINVAL;
  12375. }
  12376. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12377. if (!mbox)
  12378. return -ENOMEM;
  12379. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12380. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12381. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12382. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12383. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12384. &mbox->u.mqe.un.post_sgl_pages;
  12385. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12386. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12387. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12388. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12389. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12390. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12391. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12392. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12393. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12394. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12395. if (!phba->sli4_hba.intr_enable)
  12396. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12397. else {
  12398. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12399. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12400. }
  12401. /* The IOCTL status is embedded in the mailbox subheader. */
  12402. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12403. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12404. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12405. if (rc != MBX_TIMEOUT)
  12406. mempool_free(mbox, phba->mbox_mem_pool);
  12407. if (shdr_status || shdr_add_status || rc) {
  12408. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12409. "2511 POST_SGL mailbox failed with "
  12410. "status x%x add_status x%x, mbx status x%x\n",
  12411. shdr_status, shdr_add_status, rc);
  12412. rc = -ENXIO;
  12413. }
  12414. return 0;
  12415. }
  12416. /**
  12417. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12418. * @phba: pointer to lpfc hba data structure.
  12419. *
  12420. * This routine is invoked to post rpi header templates to the
  12421. * HBA consistent with the SLI-4 interface spec. This routine
  12422. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12423. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12424. *
  12425. * Returns
  12426. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12427. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12428. **/
  12429. uint16_t
  12430. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12431. {
  12432. unsigned long xri;
  12433. /*
  12434. * Fetch the next logical xri. Because this index is logical,
  12435. * the driver starts at 0 each time.
  12436. */
  12437. spin_lock_irq(&phba->hbalock);
  12438. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12439. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12440. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12441. spin_unlock_irq(&phba->hbalock);
  12442. return NO_XRI;
  12443. } else {
  12444. set_bit(xri, phba->sli4_hba.xri_bmask);
  12445. phba->sli4_hba.max_cfg_param.xri_used++;
  12446. }
  12447. spin_unlock_irq(&phba->hbalock);
  12448. return xri;
  12449. }
  12450. /**
  12451. * lpfc_sli4_free_xri - Release an xri for reuse.
  12452. * @phba: pointer to lpfc hba data structure.
  12453. *
  12454. * This routine is invoked to release an xri to the pool of
  12455. * available rpis maintained by the driver.
  12456. **/
  12457. void
  12458. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12459. {
  12460. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12461. phba->sli4_hba.max_cfg_param.xri_used--;
  12462. }
  12463. }
  12464. /**
  12465. * lpfc_sli4_free_xri - Release an xri for reuse.
  12466. * @phba: pointer to lpfc hba data structure.
  12467. *
  12468. * This routine is invoked to release an xri to the pool of
  12469. * available rpis maintained by the driver.
  12470. **/
  12471. void
  12472. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12473. {
  12474. spin_lock_irq(&phba->hbalock);
  12475. __lpfc_sli4_free_xri(phba, xri);
  12476. spin_unlock_irq(&phba->hbalock);
  12477. }
  12478. /**
  12479. * lpfc_sli4_next_xritag - Get an xritag for the io
  12480. * @phba: Pointer to HBA context object.
  12481. *
  12482. * This function gets an xritag for the iocb. If there is no unused xritag
  12483. * it will return 0xffff.
  12484. * The function returns the allocated xritag if successful, else returns zero.
  12485. * Zero is not a valid xritag.
  12486. * The caller is not required to hold any lock.
  12487. **/
  12488. uint16_t
  12489. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12490. {
  12491. uint16_t xri_index;
  12492. xri_index = lpfc_sli4_alloc_xri(phba);
  12493. if (xri_index == NO_XRI)
  12494. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12495. "2004 Failed to allocate XRI.last XRITAG is %d"
  12496. " Max XRI is %d, Used XRI is %d\n",
  12497. xri_index,
  12498. phba->sli4_hba.max_cfg_param.max_xri,
  12499. phba->sli4_hba.max_cfg_param.xri_used);
  12500. return xri_index;
  12501. }
  12502. /**
  12503. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12504. * @phba: pointer to lpfc hba data structure.
  12505. * @post_sgl_list: pointer to els sgl entry list.
  12506. * @count: number of els sgl entries on the list.
  12507. *
  12508. * This routine is invoked to post a block of driver's sgl pages to the
  12509. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12510. * is only called when the driver is loading and after all IO has been
  12511. * stopped.
  12512. **/
  12513. static int
  12514. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12515. struct list_head *post_sgl_list,
  12516. int post_cnt)
  12517. {
  12518. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12519. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12520. struct sgl_page_pairs *sgl_pg_pairs;
  12521. void *viraddr;
  12522. LPFC_MBOXQ_t *mbox;
  12523. uint32_t reqlen, alloclen, pg_pairs;
  12524. uint32_t mbox_tmo;
  12525. uint16_t xritag_start = 0;
  12526. int rc = 0;
  12527. uint32_t shdr_status, shdr_add_status;
  12528. union lpfc_sli4_cfg_shdr *shdr;
  12529. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12530. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12531. if (reqlen > SLI4_PAGE_SIZE) {
  12532. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12533. "2559 Block sgl registration required DMA "
  12534. "size (%d) great than a page\n", reqlen);
  12535. return -ENOMEM;
  12536. }
  12537. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12538. if (!mbox)
  12539. return -ENOMEM;
  12540. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12541. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12542. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12543. LPFC_SLI4_MBX_NEMBED);
  12544. if (alloclen < reqlen) {
  12545. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12546. "0285 Allocated DMA memory size (%d) is "
  12547. "less than the requested DMA memory "
  12548. "size (%d)\n", alloclen, reqlen);
  12549. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12550. return -ENOMEM;
  12551. }
  12552. /* Set up the SGL pages in the non-embedded DMA pages */
  12553. viraddr = mbox->sge_array->addr[0];
  12554. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12555. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12556. pg_pairs = 0;
  12557. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12558. /* Set up the sge entry */
  12559. sgl_pg_pairs->sgl_pg0_addr_lo =
  12560. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12561. sgl_pg_pairs->sgl_pg0_addr_hi =
  12562. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12563. sgl_pg_pairs->sgl_pg1_addr_lo =
  12564. cpu_to_le32(putPaddrLow(0));
  12565. sgl_pg_pairs->sgl_pg1_addr_hi =
  12566. cpu_to_le32(putPaddrHigh(0));
  12567. /* Keep the first xritag on the list */
  12568. if (pg_pairs == 0)
  12569. xritag_start = sglq_entry->sli4_xritag;
  12570. sgl_pg_pairs++;
  12571. pg_pairs++;
  12572. }
  12573. /* Complete initialization and perform endian conversion. */
  12574. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12575. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12576. sgl->word0 = cpu_to_le32(sgl->word0);
  12577. if (!phba->sli4_hba.intr_enable)
  12578. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12579. else {
  12580. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12581. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12582. }
  12583. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12584. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12585. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12586. if (rc != MBX_TIMEOUT)
  12587. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12588. if (shdr_status || shdr_add_status || rc) {
  12589. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12590. "2513 POST_SGL_BLOCK mailbox command failed "
  12591. "status x%x add_status x%x mbx status x%x\n",
  12592. shdr_status, shdr_add_status, rc);
  12593. rc = -ENXIO;
  12594. }
  12595. return rc;
  12596. }
  12597. /**
  12598. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12599. * @phba: pointer to lpfc hba data structure.
  12600. * @sblist: pointer to scsi buffer list.
  12601. * @count: number of scsi buffers on the list.
  12602. *
  12603. * This routine is invoked to post a block of @count scsi sgl pages from a
  12604. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12605. * No Lock is held.
  12606. *
  12607. **/
  12608. int
  12609. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12610. struct list_head *sblist,
  12611. int count)
  12612. {
  12613. struct lpfc_scsi_buf *psb;
  12614. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12615. struct sgl_page_pairs *sgl_pg_pairs;
  12616. void *viraddr;
  12617. LPFC_MBOXQ_t *mbox;
  12618. uint32_t reqlen, alloclen, pg_pairs;
  12619. uint32_t mbox_tmo;
  12620. uint16_t xritag_start = 0;
  12621. int rc = 0;
  12622. uint32_t shdr_status, shdr_add_status;
  12623. dma_addr_t pdma_phys_bpl1;
  12624. union lpfc_sli4_cfg_shdr *shdr;
  12625. /* Calculate the requested length of the dma memory */
  12626. reqlen = count * sizeof(struct sgl_page_pairs) +
  12627. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12628. if (reqlen > SLI4_PAGE_SIZE) {
  12629. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12630. "0217 Block sgl registration required DMA "
  12631. "size (%d) great than a page\n", reqlen);
  12632. return -ENOMEM;
  12633. }
  12634. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12635. if (!mbox) {
  12636. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12637. "0283 Failed to allocate mbox cmd memory\n");
  12638. return -ENOMEM;
  12639. }
  12640. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12641. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12642. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12643. LPFC_SLI4_MBX_NEMBED);
  12644. if (alloclen < reqlen) {
  12645. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12646. "2561 Allocated DMA memory size (%d) is "
  12647. "less than the requested DMA memory "
  12648. "size (%d)\n", alloclen, reqlen);
  12649. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12650. return -ENOMEM;
  12651. }
  12652. /* Get the first SGE entry from the non-embedded DMA memory */
  12653. viraddr = mbox->sge_array->addr[0];
  12654. /* Set up the SGL pages in the non-embedded DMA pages */
  12655. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12656. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12657. pg_pairs = 0;
  12658. list_for_each_entry(psb, sblist, list) {
  12659. /* Set up the sge entry */
  12660. sgl_pg_pairs->sgl_pg0_addr_lo =
  12661. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12662. sgl_pg_pairs->sgl_pg0_addr_hi =
  12663. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12664. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12665. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12666. else
  12667. pdma_phys_bpl1 = 0;
  12668. sgl_pg_pairs->sgl_pg1_addr_lo =
  12669. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12670. sgl_pg_pairs->sgl_pg1_addr_hi =
  12671. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12672. /* Keep the first xritag on the list */
  12673. if (pg_pairs == 0)
  12674. xritag_start = psb->cur_iocbq.sli4_xritag;
  12675. sgl_pg_pairs++;
  12676. pg_pairs++;
  12677. }
  12678. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12679. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12680. /* Perform endian conversion if necessary */
  12681. sgl->word0 = cpu_to_le32(sgl->word0);
  12682. if (!phba->sli4_hba.intr_enable)
  12683. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12684. else {
  12685. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12686. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12687. }
  12688. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12689. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12690. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12691. if (rc != MBX_TIMEOUT)
  12692. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12693. if (shdr_status || shdr_add_status || rc) {
  12694. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12695. "2564 POST_SGL_BLOCK mailbox command failed "
  12696. "status x%x add_status x%x mbx status x%x\n",
  12697. shdr_status, shdr_add_status, rc);
  12698. rc = -ENXIO;
  12699. }
  12700. return rc;
  12701. }
  12702. /**
  12703. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12704. * @phba: pointer to lpfc_hba struct that the frame was received on
  12705. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12706. *
  12707. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12708. * valid type of frame that the LPFC driver will handle. This function will
  12709. * return a zero if the frame is a valid frame or a non zero value when the
  12710. * frame does not pass the check.
  12711. **/
  12712. static int
  12713. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12714. {
  12715. /* make rctl_names static to save stack space */
  12716. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12717. char *type_names[] = FC_TYPE_NAMES_INIT;
  12718. struct fc_vft_header *fc_vft_hdr;
  12719. uint32_t *header = (uint32_t *) fc_hdr;
  12720. switch (fc_hdr->fh_r_ctl) {
  12721. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12722. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12723. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12724. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12725. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12726. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12727. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12728. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12729. case FC_RCTL_ELS_REQ: /* extended link services request */
  12730. case FC_RCTL_ELS_REP: /* extended link services reply */
  12731. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12732. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12733. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12734. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12735. case FC_RCTL_BA_RMC: /* remove connection */
  12736. case FC_RCTL_BA_ACC: /* basic accept */
  12737. case FC_RCTL_BA_RJT: /* basic reject */
  12738. case FC_RCTL_BA_PRMT:
  12739. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12740. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12741. case FC_RCTL_P_RJT: /* port reject */
  12742. case FC_RCTL_F_RJT: /* fabric reject */
  12743. case FC_RCTL_P_BSY: /* port busy */
  12744. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12745. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12746. case FC_RCTL_LCR: /* link credit reset */
  12747. case FC_RCTL_END: /* end */
  12748. break;
  12749. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12750. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12751. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12752. return lpfc_fc_frame_check(phba, fc_hdr);
  12753. default:
  12754. goto drop;
  12755. }
  12756. switch (fc_hdr->fh_type) {
  12757. case FC_TYPE_BLS:
  12758. case FC_TYPE_ELS:
  12759. case FC_TYPE_FCP:
  12760. case FC_TYPE_CT:
  12761. break;
  12762. case FC_TYPE_IP:
  12763. case FC_TYPE_ILS:
  12764. default:
  12765. goto drop;
  12766. }
  12767. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12768. "2538 Received frame rctl:%s type:%s "
  12769. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12770. rctl_names[fc_hdr->fh_r_ctl],
  12771. type_names[fc_hdr->fh_type],
  12772. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12773. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12774. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12775. return 0;
  12776. drop:
  12777. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12778. "2539 Dropped frame rctl:%s type:%s\n",
  12779. rctl_names[fc_hdr->fh_r_ctl],
  12780. type_names[fc_hdr->fh_type]);
  12781. return 1;
  12782. }
  12783. /**
  12784. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12785. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12786. *
  12787. * This function processes the FC header to retrieve the VFI from the VF
  12788. * header, if one exists. This function will return the VFI if one exists
  12789. * or 0 if no VSAN Header exists.
  12790. **/
  12791. static uint32_t
  12792. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12793. {
  12794. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12795. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12796. return 0;
  12797. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12798. }
  12799. /**
  12800. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12801. * @phba: Pointer to the HBA structure to search for the vport on
  12802. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12803. * @fcfi: The FC Fabric ID that the frame came from
  12804. *
  12805. * This function searches the @phba for a vport that matches the content of the
  12806. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12807. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12808. * returns the matching vport pointer or NULL if unable to match frame to a
  12809. * vport.
  12810. **/
  12811. static struct lpfc_vport *
  12812. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12813. uint16_t fcfi)
  12814. {
  12815. struct lpfc_vport **vports;
  12816. struct lpfc_vport *vport = NULL;
  12817. int i;
  12818. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12819. fc_hdr->fh_d_id[1] << 8 |
  12820. fc_hdr->fh_d_id[2]);
  12821. if (did == Fabric_DID)
  12822. return phba->pport;
  12823. if ((phba->pport->fc_flag & FC_PT2PT) &&
  12824. !(phba->link_state == LPFC_HBA_READY))
  12825. return phba->pport;
  12826. vports = lpfc_create_vport_work_array(phba);
  12827. if (vports != NULL)
  12828. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12829. if (phba->fcf.fcfi == fcfi &&
  12830. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12831. vports[i]->fc_myDID == did) {
  12832. vport = vports[i];
  12833. break;
  12834. }
  12835. }
  12836. lpfc_destroy_vport_work_array(phba, vports);
  12837. return vport;
  12838. }
  12839. /**
  12840. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12841. * @vport: The vport to work on.
  12842. *
  12843. * This function updates the receive sequence time stamp for this vport. The
  12844. * receive sequence time stamp indicates the time that the last frame of the
  12845. * the sequence that has been idle for the longest amount of time was received.
  12846. * the driver uses this time stamp to indicate if any received sequences have
  12847. * timed out.
  12848. **/
  12849. void
  12850. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12851. {
  12852. struct lpfc_dmabuf *h_buf;
  12853. struct hbq_dmabuf *dmabuf = NULL;
  12854. /* get the oldest sequence on the rcv list */
  12855. h_buf = list_get_first(&vport->rcv_buffer_list,
  12856. struct lpfc_dmabuf, list);
  12857. if (!h_buf)
  12858. return;
  12859. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12860. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12861. }
  12862. /**
  12863. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12864. * @vport: The vport that the received sequences were sent to.
  12865. *
  12866. * This function cleans up all outstanding received sequences. This is called
  12867. * by the driver when a link event or user action invalidates all the received
  12868. * sequences.
  12869. **/
  12870. void
  12871. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12872. {
  12873. struct lpfc_dmabuf *h_buf, *hnext;
  12874. struct lpfc_dmabuf *d_buf, *dnext;
  12875. struct hbq_dmabuf *dmabuf = NULL;
  12876. /* start with the oldest sequence on the rcv list */
  12877. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12878. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12879. list_del_init(&dmabuf->hbuf.list);
  12880. list_for_each_entry_safe(d_buf, dnext,
  12881. &dmabuf->dbuf.list, list) {
  12882. list_del_init(&d_buf->list);
  12883. lpfc_in_buf_free(vport->phba, d_buf);
  12884. }
  12885. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12886. }
  12887. }
  12888. /**
  12889. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12890. * @vport: The vport that the received sequences were sent to.
  12891. *
  12892. * This function determines whether any received sequences have timed out by
  12893. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12894. * indicates that there is at least one timed out sequence this routine will
  12895. * go through the received sequences one at a time from most inactive to most
  12896. * active to determine which ones need to be cleaned up. Once it has determined
  12897. * that a sequence needs to be cleaned up it will simply free up the resources
  12898. * without sending an abort.
  12899. **/
  12900. void
  12901. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12902. {
  12903. struct lpfc_dmabuf *h_buf, *hnext;
  12904. struct lpfc_dmabuf *d_buf, *dnext;
  12905. struct hbq_dmabuf *dmabuf = NULL;
  12906. unsigned long timeout;
  12907. int abort_count = 0;
  12908. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12909. vport->rcv_buffer_time_stamp);
  12910. if (list_empty(&vport->rcv_buffer_list) ||
  12911. time_before(jiffies, timeout))
  12912. return;
  12913. /* start with the oldest sequence on the rcv list */
  12914. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12915. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12916. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12917. dmabuf->time_stamp);
  12918. if (time_before(jiffies, timeout))
  12919. break;
  12920. abort_count++;
  12921. list_del_init(&dmabuf->hbuf.list);
  12922. list_for_each_entry_safe(d_buf, dnext,
  12923. &dmabuf->dbuf.list, list) {
  12924. list_del_init(&d_buf->list);
  12925. lpfc_in_buf_free(vport->phba, d_buf);
  12926. }
  12927. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12928. }
  12929. if (abort_count)
  12930. lpfc_update_rcv_time_stamp(vport);
  12931. }
  12932. /**
  12933. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12934. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12935. *
  12936. * This function searches through the existing incomplete sequences that have
  12937. * been sent to this @vport. If the frame matches one of the incomplete
  12938. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12939. * make up that sequence. If no sequence is found that matches this frame then
  12940. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12941. * This function returns a pointer to the first dmabuf in the sequence list that
  12942. * the frame was linked to.
  12943. **/
  12944. static struct hbq_dmabuf *
  12945. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12946. {
  12947. struct fc_frame_header *new_hdr;
  12948. struct fc_frame_header *temp_hdr;
  12949. struct lpfc_dmabuf *d_buf;
  12950. struct lpfc_dmabuf *h_buf;
  12951. struct hbq_dmabuf *seq_dmabuf = NULL;
  12952. struct hbq_dmabuf *temp_dmabuf = NULL;
  12953. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12954. dmabuf->time_stamp = jiffies;
  12955. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12956. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12957. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12958. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12959. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12960. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12961. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12962. continue;
  12963. /* found a pending sequence that matches this frame */
  12964. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12965. break;
  12966. }
  12967. if (!seq_dmabuf) {
  12968. /*
  12969. * This indicates first frame received for this sequence.
  12970. * Queue the buffer on the vport's rcv_buffer_list.
  12971. */
  12972. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12973. lpfc_update_rcv_time_stamp(vport);
  12974. return dmabuf;
  12975. }
  12976. temp_hdr = seq_dmabuf->hbuf.virt;
  12977. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12978. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12979. list_del_init(&seq_dmabuf->hbuf.list);
  12980. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12981. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12982. lpfc_update_rcv_time_stamp(vport);
  12983. return dmabuf;
  12984. }
  12985. /* move this sequence to the tail to indicate a young sequence */
  12986. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12987. seq_dmabuf->time_stamp = jiffies;
  12988. lpfc_update_rcv_time_stamp(vport);
  12989. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12990. temp_hdr = dmabuf->hbuf.virt;
  12991. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12992. return seq_dmabuf;
  12993. }
  12994. /* find the correct place in the sequence to insert this frame */
  12995. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12996. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12997. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12998. /*
  12999. * If the frame's sequence count is greater than the frame on
  13000. * the list then insert the frame right after this frame
  13001. */
  13002. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  13003. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  13004. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  13005. return seq_dmabuf;
  13006. }
  13007. }
  13008. return NULL;
  13009. }
  13010. /**
  13011. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  13012. * @vport: pointer to a vitural port
  13013. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13014. *
  13015. * This function tries to abort from the partially assembed sequence, described
  13016. * by the information from basic abbort @dmabuf. It checks to see whether such
  13017. * partially assembled sequence held by the driver. If so, it shall free up all
  13018. * the frames from the partially assembled sequence.
  13019. *
  13020. * Return
  13021. * true -- if there is matching partially assembled sequence present and all
  13022. * the frames freed with the sequence;
  13023. * false -- if there is no matching partially assembled sequence present so
  13024. * nothing got aborted in the lower layer driver
  13025. **/
  13026. static bool
  13027. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  13028. struct hbq_dmabuf *dmabuf)
  13029. {
  13030. struct fc_frame_header *new_hdr;
  13031. struct fc_frame_header *temp_hdr;
  13032. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  13033. struct hbq_dmabuf *seq_dmabuf = NULL;
  13034. /* Use the hdr_buf to find the sequence that matches this frame */
  13035. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  13036. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  13037. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13038. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  13039. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  13040. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  13041. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  13042. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  13043. continue;
  13044. /* found a pending sequence that matches this frame */
  13045. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  13046. break;
  13047. }
  13048. /* Free up all the frames from the partially assembled sequence */
  13049. if (seq_dmabuf) {
  13050. list_for_each_entry_safe(d_buf, n_buf,
  13051. &seq_dmabuf->dbuf.list, list) {
  13052. list_del_init(&d_buf->list);
  13053. lpfc_in_buf_free(vport->phba, d_buf);
  13054. }
  13055. return true;
  13056. }
  13057. return false;
  13058. }
  13059. /**
  13060. * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
  13061. * @vport: pointer to a vitural port
  13062. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13063. *
  13064. * This function tries to abort from the assembed sequence from upper level
  13065. * protocol, described by the information from basic abbort @dmabuf. It
  13066. * checks to see whether such pending context exists at upper level protocol.
  13067. * If so, it shall clean up the pending context.
  13068. *
  13069. * Return
  13070. * true -- if there is matching pending context of the sequence cleaned
  13071. * at ulp;
  13072. * false -- if there is no matching pending context of the sequence present
  13073. * at ulp.
  13074. **/
  13075. static bool
  13076. lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  13077. {
  13078. struct lpfc_hba *phba = vport->phba;
  13079. int handled;
  13080. /* Accepting abort at ulp with SLI4 only */
  13081. if (phba->sli_rev < LPFC_SLI_REV4)
  13082. return false;
  13083. /* Register all caring upper level protocols to attend abort */
  13084. handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
  13085. if (handled)
  13086. return true;
  13087. return false;
  13088. }
  13089. /**
  13090. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  13091. * @phba: Pointer to HBA context object.
  13092. * @cmd_iocbq: pointer to the command iocbq structure.
  13093. * @rsp_iocbq: pointer to the response iocbq structure.
  13094. *
  13095. * This function handles the sequence abort response iocb command complete
  13096. * event. It properly releases the memory allocated to the sequence abort
  13097. * accept iocb.
  13098. **/
  13099. static void
  13100. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  13101. struct lpfc_iocbq *cmd_iocbq,
  13102. struct lpfc_iocbq *rsp_iocbq)
  13103. {
  13104. struct lpfc_nodelist *ndlp;
  13105. if (cmd_iocbq) {
  13106. ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
  13107. lpfc_nlp_put(ndlp);
  13108. lpfc_nlp_not_used(ndlp);
  13109. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  13110. }
  13111. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  13112. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  13113. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13114. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  13115. rsp_iocbq->iocb.ulpStatus,
  13116. rsp_iocbq->iocb.un.ulpWord[4]);
  13117. }
  13118. /**
  13119. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  13120. * @phba: Pointer to HBA context object.
  13121. * @xri: xri id in transaction.
  13122. *
  13123. * This function validates the xri maps to the known range of XRIs allocated an
  13124. * used by the driver.
  13125. **/
  13126. uint16_t
  13127. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  13128. uint16_t xri)
  13129. {
  13130. int i;
  13131. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  13132. if (xri == phba->sli4_hba.xri_ids[i])
  13133. return i;
  13134. }
  13135. return NO_XRI;
  13136. }
  13137. /**
  13138. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  13139. * @phba: Pointer to HBA context object.
  13140. * @fc_hdr: pointer to a FC frame header.
  13141. *
  13142. * This function sends a basic response to a previous unsol sequence abort
  13143. * event after aborting the sequence handling.
  13144. **/
  13145. static void
  13146. lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
  13147. struct fc_frame_header *fc_hdr, bool aborted)
  13148. {
  13149. struct lpfc_hba *phba = vport->phba;
  13150. struct lpfc_iocbq *ctiocb = NULL;
  13151. struct lpfc_nodelist *ndlp;
  13152. uint16_t oxid, rxid, xri, lxri;
  13153. uint32_t sid, fctl;
  13154. IOCB_t *icmd;
  13155. int rc;
  13156. if (!lpfc_is_link_up(phba))
  13157. return;
  13158. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13159. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  13160. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  13161. ndlp = lpfc_findnode_did(vport, sid);
  13162. if (!ndlp) {
  13163. ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
  13164. if (!ndlp) {
  13165. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13166. "1268 Failed to allocate ndlp for "
  13167. "oxid:x%x SID:x%x\n", oxid, sid);
  13168. return;
  13169. }
  13170. lpfc_nlp_init(vport, ndlp, sid);
  13171. /* Put ndlp onto pport node list */
  13172. lpfc_enqueue_node(vport, ndlp);
  13173. } else if (!NLP_CHK_NODE_ACT(ndlp)) {
  13174. /* re-setup ndlp without removing from node list */
  13175. ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
  13176. if (!ndlp) {
  13177. lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
  13178. "3275 Failed to active ndlp found "
  13179. "for oxid:x%x SID:x%x\n", oxid, sid);
  13180. return;
  13181. }
  13182. }
  13183. /* Allocate buffer for rsp iocb */
  13184. ctiocb = lpfc_sli_get_iocbq(phba);
  13185. if (!ctiocb)
  13186. return;
  13187. /* Extract the F_CTL field from FC_HDR */
  13188. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  13189. icmd = &ctiocb->iocb;
  13190. icmd->un.xseq64.bdl.bdeSize = 0;
  13191. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  13192. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  13193. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  13194. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  13195. /* Fill in the rest of iocb fields */
  13196. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  13197. icmd->ulpBdeCount = 0;
  13198. icmd->ulpLe = 1;
  13199. icmd->ulpClass = CLASS3;
  13200. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  13201. ctiocb->context1 = lpfc_nlp_get(ndlp);
  13202. ctiocb->iocb_cmpl = NULL;
  13203. ctiocb->vport = phba->pport;
  13204. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  13205. ctiocb->sli4_lxritag = NO_XRI;
  13206. ctiocb->sli4_xritag = NO_XRI;
  13207. if (fctl & FC_FC_EX_CTX)
  13208. /* Exchange responder sent the abort so we
  13209. * own the oxid.
  13210. */
  13211. xri = oxid;
  13212. else
  13213. xri = rxid;
  13214. lxri = lpfc_sli4_xri_inrange(phba, xri);
  13215. if (lxri != NO_XRI)
  13216. lpfc_set_rrq_active(phba, ndlp, lxri,
  13217. (xri == oxid) ? rxid : oxid, 0);
  13218. /* For BA_ABTS from exchange responder, if the logical xri with
  13219. * the oxid maps to the FCP XRI range, the port no longer has
  13220. * that exchange context, send a BLS_RJT. Override the IOCB for
  13221. * a BA_RJT.
  13222. */
  13223. if ((fctl & FC_FC_EX_CTX) &&
  13224. (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
  13225. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13226. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13227. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13228. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13229. }
  13230. /* If BA_ABTS failed to abort a partially assembled receive sequence,
  13231. * the driver no longer has that exchange, send a BLS_RJT. Override
  13232. * the IOCB for a BA_RJT.
  13233. */
  13234. if (aborted == false) {
  13235. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  13236. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  13237. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  13238. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  13239. }
  13240. if (fctl & FC_FC_EX_CTX) {
  13241. /* ABTS sent by responder to CT exchange, construction
  13242. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  13243. * field and RX_ID from ABTS for RX_ID field.
  13244. */
  13245. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  13246. } else {
  13247. /* ABTS sent by initiator to CT exchange, construction
  13248. * of BA_ACC will need to allocate a new XRI as for the
  13249. * XRI_TAG field.
  13250. */
  13251. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  13252. }
  13253. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  13254. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  13255. /* Xmit CT abts response on exchange <xid> */
  13256. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  13257. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  13258. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  13259. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  13260. if (rc == IOCB_ERROR) {
  13261. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  13262. "2925 Failed to issue CT ABTS RSP x%x on "
  13263. "xri x%x, Data x%x\n",
  13264. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  13265. phba->link_state);
  13266. lpfc_nlp_put(ndlp);
  13267. ctiocb->context1 = NULL;
  13268. lpfc_sli_release_iocbq(phba, ctiocb);
  13269. }
  13270. }
  13271. /**
  13272. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  13273. * @vport: Pointer to the vport on which this sequence was received
  13274. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13275. *
  13276. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  13277. * receive sequence is only partially assembed by the driver, it shall abort
  13278. * the partially assembled frames for the sequence. Otherwise, if the
  13279. * unsolicited receive sequence has been completely assembled and passed to
  13280. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  13281. * unsolicited sequence has been aborted. After that, it will issue a basic
  13282. * accept to accept the abort.
  13283. **/
  13284. void
  13285. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  13286. struct hbq_dmabuf *dmabuf)
  13287. {
  13288. struct lpfc_hba *phba = vport->phba;
  13289. struct fc_frame_header fc_hdr;
  13290. uint32_t fctl;
  13291. bool aborted;
  13292. /* Make a copy of fc_hdr before the dmabuf being released */
  13293. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  13294. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  13295. if (fctl & FC_FC_EX_CTX) {
  13296. /* ABTS by responder to exchange, no cleanup needed */
  13297. aborted = true;
  13298. } else {
  13299. /* ABTS by initiator to exchange, need to do cleanup */
  13300. aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  13301. if (aborted == false)
  13302. aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
  13303. }
  13304. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13305. /* Respond with BA_ACC or BA_RJT accordingly */
  13306. lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
  13307. }
  13308. /**
  13309. * lpfc_seq_complete - Indicates if a sequence is complete
  13310. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13311. *
  13312. * This function checks the sequence, starting with the frame described by
  13313. * @dmabuf, to see if all the frames associated with this sequence are present.
  13314. * the frames associated with this sequence are linked to the @dmabuf using the
  13315. * dbuf list. This function looks for two major things. 1) That the first frame
  13316. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  13317. * set. 3) That there are no holes in the sequence count. The function will
  13318. * return 1 when the sequence is complete, otherwise it will return 0.
  13319. **/
  13320. static int
  13321. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  13322. {
  13323. struct fc_frame_header *hdr;
  13324. struct lpfc_dmabuf *d_buf;
  13325. struct hbq_dmabuf *seq_dmabuf;
  13326. uint32_t fctl;
  13327. int seq_count = 0;
  13328. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13329. /* make sure first fame of sequence has a sequence count of zero */
  13330. if (hdr->fh_seq_cnt != seq_count)
  13331. return 0;
  13332. fctl = (hdr->fh_f_ctl[0] << 16 |
  13333. hdr->fh_f_ctl[1] << 8 |
  13334. hdr->fh_f_ctl[2]);
  13335. /* If last frame of sequence we can return success. */
  13336. if (fctl & FC_FC_END_SEQ)
  13337. return 1;
  13338. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  13339. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13340. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13341. /* If there is a hole in the sequence count then fail. */
  13342. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  13343. return 0;
  13344. fctl = (hdr->fh_f_ctl[0] << 16 |
  13345. hdr->fh_f_ctl[1] << 8 |
  13346. hdr->fh_f_ctl[2]);
  13347. /* If last frame of sequence we can return success. */
  13348. if (fctl & FC_FC_END_SEQ)
  13349. return 1;
  13350. }
  13351. return 0;
  13352. }
  13353. /**
  13354. * lpfc_prep_seq - Prep sequence for ULP processing
  13355. * @vport: Pointer to the vport on which this sequence was received
  13356. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  13357. *
  13358. * This function takes a sequence, described by a list of frames, and creates
  13359. * a list of iocbq structures to describe the sequence. This iocbq list will be
  13360. * used to issue to the generic unsolicited sequence handler. This routine
  13361. * returns a pointer to the first iocbq in the list. If the function is unable
  13362. * to allocate an iocbq then it throw out the received frames that were not
  13363. * able to be described and return a pointer to the first iocbq. If unable to
  13364. * allocate any iocbqs (including the first) this function will return NULL.
  13365. **/
  13366. static struct lpfc_iocbq *
  13367. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  13368. {
  13369. struct hbq_dmabuf *hbq_buf;
  13370. struct lpfc_dmabuf *d_buf, *n_buf;
  13371. struct lpfc_iocbq *first_iocbq, *iocbq;
  13372. struct fc_frame_header *fc_hdr;
  13373. uint32_t sid;
  13374. uint32_t len, tot_len;
  13375. struct ulp_bde64 *pbde;
  13376. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13377. /* remove from receive buffer list */
  13378. list_del_init(&seq_dmabuf->hbuf.list);
  13379. lpfc_update_rcv_time_stamp(vport);
  13380. /* get the Remote Port's SID */
  13381. sid = sli4_sid_from_fc_hdr(fc_hdr);
  13382. tot_len = 0;
  13383. /* Get an iocbq struct to fill in. */
  13384. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  13385. if (first_iocbq) {
  13386. /* Initialize the first IOCB. */
  13387. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  13388. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  13389. /* Check FC Header to see what TYPE of frame we are rcv'ing */
  13390. if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
  13391. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
  13392. first_iocbq->iocb.un.rcvels.parmRo =
  13393. sli4_did_from_fc_hdr(fc_hdr);
  13394. first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
  13395. } else
  13396. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  13397. first_iocbq->iocb.ulpContext = NO_XRI;
  13398. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  13399. be16_to_cpu(fc_hdr->fh_ox_id);
  13400. /* iocbq is prepped for internal consumption. Physical vpi. */
  13401. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  13402. vport->phba->vpi_ids[vport->vpi];
  13403. /* put the first buffer into the first IOCBq */
  13404. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13405. first_iocbq->context3 = NULL;
  13406. first_iocbq->iocb.ulpBdeCount = 1;
  13407. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13408. LPFC_DATA_BUF_SIZE;
  13409. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13410. tot_len = bf_get(lpfc_rcqe_length,
  13411. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13412. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13413. }
  13414. iocbq = first_iocbq;
  13415. /*
  13416. * Each IOCBq can have two Buffers assigned, so go through the list
  13417. * of buffers for this sequence and save two buffers in each IOCBq
  13418. */
  13419. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13420. if (!iocbq) {
  13421. lpfc_in_buf_free(vport->phba, d_buf);
  13422. continue;
  13423. }
  13424. if (!iocbq->context3) {
  13425. iocbq->context3 = d_buf;
  13426. iocbq->iocb.ulpBdeCount++;
  13427. pbde = (struct ulp_bde64 *)
  13428. &iocbq->iocb.unsli3.sli3Words[4];
  13429. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13430. /* We need to get the size out of the right CQE */
  13431. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13432. len = bf_get(lpfc_rcqe_length,
  13433. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13434. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13435. tot_len += len;
  13436. } else {
  13437. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13438. if (!iocbq) {
  13439. if (first_iocbq) {
  13440. first_iocbq->iocb.ulpStatus =
  13441. IOSTAT_FCP_RSP_ERROR;
  13442. first_iocbq->iocb.un.ulpWord[4] =
  13443. IOERR_NO_RESOURCES;
  13444. }
  13445. lpfc_in_buf_free(vport->phba, d_buf);
  13446. continue;
  13447. }
  13448. iocbq->context2 = d_buf;
  13449. iocbq->context3 = NULL;
  13450. iocbq->iocb.ulpBdeCount = 1;
  13451. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13452. LPFC_DATA_BUF_SIZE;
  13453. /* We need to get the size out of the right CQE */
  13454. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13455. len = bf_get(lpfc_rcqe_length,
  13456. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13457. tot_len += len;
  13458. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13459. iocbq->iocb.un.rcvels.remoteID = sid;
  13460. list_add_tail(&iocbq->list, &first_iocbq->list);
  13461. }
  13462. }
  13463. return first_iocbq;
  13464. }
  13465. static void
  13466. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13467. struct hbq_dmabuf *seq_dmabuf)
  13468. {
  13469. struct fc_frame_header *fc_hdr;
  13470. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13471. struct lpfc_hba *phba = vport->phba;
  13472. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13473. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13474. if (!iocbq) {
  13475. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13476. "2707 Ring %d handler: Failed to allocate "
  13477. "iocb Rctl x%x Type x%x received\n",
  13478. LPFC_ELS_RING,
  13479. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13480. return;
  13481. }
  13482. if (!lpfc_complete_unsol_iocb(phba,
  13483. &phba->sli.ring[LPFC_ELS_RING],
  13484. iocbq, fc_hdr->fh_r_ctl,
  13485. fc_hdr->fh_type))
  13486. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13487. "2540 Ring %d handler: unexpected Rctl "
  13488. "x%x Type x%x received\n",
  13489. LPFC_ELS_RING,
  13490. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13491. /* Free iocb created in lpfc_prep_seq */
  13492. list_for_each_entry_safe(curr_iocb, next_iocb,
  13493. &iocbq->list, list) {
  13494. list_del_init(&curr_iocb->list);
  13495. lpfc_sli_release_iocbq(phba, curr_iocb);
  13496. }
  13497. lpfc_sli_release_iocbq(phba, iocbq);
  13498. }
  13499. /**
  13500. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13501. * @phba: Pointer to HBA context object.
  13502. *
  13503. * This function is called with no lock held. This function processes all
  13504. * the received buffers and gives it to upper layers when a received buffer
  13505. * indicates that it is the final frame in the sequence. The interrupt
  13506. * service routine processes received buffers at interrupt contexts and adds
  13507. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13508. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13509. * appropriate receive function when the final frame in a sequence is received.
  13510. **/
  13511. void
  13512. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13513. struct hbq_dmabuf *dmabuf)
  13514. {
  13515. struct hbq_dmabuf *seq_dmabuf;
  13516. struct fc_frame_header *fc_hdr;
  13517. struct lpfc_vport *vport;
  13518. uint32_t fcfi;
  13519. uint32_t did;
  13520. /* Process each received buffer */
  13521. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13522. /* check to see if this a valid type of frame */
  13523. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13524. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13525. return;
  13526. }
  13527. if ((bf_get(lpfc_cqe_code,
  13528. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13529. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13530. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13531. else
  13532. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13533. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13534. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13535. if (!vport) {
  13536. /* throw out the frame */
  13537. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13538. return;
  13539. }
  13540. /* d_id this frame is directed to */
  13541. did = sli4_did_from_fc_hdr(fc_hdr);
  13542. /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
  13543. if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
  13544. (did != Fabric_DID)) {
  13545. /*
  13546. * Throw out the frame if we are not pt2pt.
  13547. * The pt2pt protocol allows for discovery frames
  13548. * to be received without a registered VPI.
  13549. */
  13550. if (!(vport->fc_flag & FC_PT2PT) ||
  13551. (phba->link_state == LPFC_HBA_READY)) {
  13552. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13553. return;
  13554. }
  13555. }
  13556. /* Handle the basic abort sequence (BA_ABTS) event */
  13557. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13558. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13559. return;
  13560. }
  13561. /* Link this frame */
  13562. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13563. if (!seq_dmabuf) {
  13564. /* unable to add frame to vport - throw it out */
  13565. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13566. return;
  13567. }
  13568. /* If not last frame in sequence continue processing frames. */
  13569. if (!lpfc_seq_complete(seq_dmabuf))
  13570. return;
  13571. /* Send the complete sequence to the upper layer protocol */
  13572. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13573. }
  13574. /**
  13575. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13576. * @phba: pointer to lpfc hba data structure.
  13577. *
  13578. * This routine is invoked to post rpi header templates to the
  13579. * HBA consistent with the SLI-4 interface spec. This routine
  13580. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13581. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13582. *
  13583. * This routine does not require any locks. It's usage is expected
  13584. * to be driver load or reset recovery when the driver is
  13585. * sequential.
  13586. *
  13587. * Return codes
  13588. * 0 - successful
  13589. * -EIO - The mailbox failed to complete successfully.
  13590. * When this error occurs, the driver is not guaranteed
  13591. * to have any rpi regions posted to the device and
  13592. * must either attempt to repost the regions or take a
  13593. * fatal error.
  13594. **/
  13595. int
  13596. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13597. {
  13598. struct lpfc_rpi_hdr *rpi_page;
  13599. uint32_t rc = 0;
  13600. uint16_t lrpi = 0;
  13601. /* SLI4 ports that support extents do not require RPI headers. */
  13602. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13603. goto exit;
  13604. if (phba->sli4_hba.extents_in_use)
  13605. return -EIO;
  13606. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13607. /*
  13608. * Assign the rpi headers a physical rpi only if the driver
  13609. * has not initialized those resources. A port reset only
  13610. * needs the headers posted.
  13611. */
  13612. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13613. LPFC_RPI_RSRC_RDY)
  13614. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13615. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13616. if (rc != MBX_SUCCESS) {
  13617. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13618. "2008 Error %d posting all rpi "
  13619. "headers\n", rc);
  13620. rc = -EIO;
  13621. break;
  13622. }
  13623. }
  13624. exit:
  13625. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13626. LPFC_RPI_RSRC_RDY);
  13627. return rc;
  13628. }
  13629. /**
  13630. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13631. * @phba: pointer to lpfc hba data structure.
  13632. * @rpi_page: pointer to the rpi memory region.
  13633. *
  13634. * This routine is invoked to post a single rpi header to the
  13635. * HBA consistent with the SLI-4 interface spec. This memory region
  13636. * maps up to 64 rpi context regions.
  13637. *
  13638. * Return codes
  13639. * 0 - successful
  13640. * -ENOMEM - No available memory
  13641. * -EIO - The mailbox failed to complete successfully.
  13642. **/
  13643. int
  13644. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13645. {
  13646. LPFC_MBOXQ_t *mboxq;
  13647. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13648. uint32_t rc = 0;
  13649. uint32_t shdr_status, shdr_add_status;
  13650. union lpfc_sli4_cfg_shdr *shdr;
  13651. /* SLI4 ports that support extents do not require RPI headers. */
  13652. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13653. return rc;
  13654. if (phba->sli4_hba.extents_in_use)
  13655. return -EIO;
  13656. /* The port is notified of the header region via a mailbox command. */
  13657. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13658. if (!mboxq) {
  13659. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13660. "2001 Unable to allocate memory for issuing "
  13661. "SLI_CONFIG_SPECIAL mailbox command\n");
  13662. return -ENOMEM;
  13663. }
  13664. /* Post all rpi memory regions to the port. */
  13665. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13666. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13667. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13668. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13669. sizeof(struct lpfc_sli4_cfg_mhdr),
  13670. LPFC_SLI4_MBX_EMBED);
  13671. /* Post the physical rpi to the port for this rpi header. */
  13672. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13673. rpi_page->start_rpi);
  13674. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13675. hdr_tmpl, rpi_page->page_count);
  13676. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13677. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13678. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13679. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13680. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13681. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13682. if (rc != MBX_TIMEOUT)
  13683. mempool_free(mboxq, phba->mbox_mem_pool);
  13684. if (shdr_status || shdr_add_status || rc) {
  13685. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13686. "2514 POST_RPI_HDR mailbox failed with "
  13687. "status x%x add_status x%x, mbx status x%x\n",
  13688. shdr_status, shdr_add_status, rc);
  13689. rc = -ENXIO;
  13690. }
  13691. return rc;
  13692. }
  13693. /**
  13694. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13695. * @phba: pointer to lpfc hba data structure.
  13696. *
  13697. * This routine is invoked to post rpi header templates to the
  13698. * HBA consistent with the SLI-4 interface spec. This routine
  13699. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13700. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13701. *
  13702. * Returns
  13703. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13704. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13705. **/
  13706. int
  13707. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13708. {
  13709. unsigned long rpi;
  13710. uint16_t max_rpi, rpi_limit;
  13711. uint16_t rpi_remaining, lrpi = 0;
  13712. struct lpfc_rpi_hdr *rpi_hdr;
  13713. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13714. rpi_limit = phba->sli4_hba.next_rpi;
  13715. /*
  13716. * Fetch the next logical rpi. Because this index is logical,
  13717. * the driver starts at 0 each time.
  13718. */
  13719. spin_lock_irq(&phba->hbalock);
  13720. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13721. if (rpi >= rpi_limit)
  13722. rpi = LPFC_RPI_ALLOC_ERROR;
  13723. else {
  13724. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13725. phba->sli4_hba.max_cfg_param.rpi_used++;
  13726. phba->sli4_hba.rpi_count++;
  13727. }
  13728. /*
  13729. * Don't try to allocate more rpi header regions if the device limit
  13730. * has been exhausted.
  13731. */
  13732. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13733. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13734. spin_unlock_irq(&phba->hbalock);
  13735. return rpi;
  13736. }
  13737. /*
  13738. * RPI header postings are not required for SLI4 ports capable of
  13739. * extents.
  13740. */
  13741. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13742. spin_unlock_irq(&phba->hbalock);
  13743. return rpi;
  13744. }
  13745. /*
  13746. * If the driver is running low on rpi resources, allocate another
  13747. * page now. Note that the next_rpi value is used because
  13748. * it represents how many are actually in use whereas max_rpi notes
  13749. * how many are supported max by the device.
  13750. */
  13751. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13752. spin_unlock_irq(&phba->hbalock);
  13753. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13754. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13755. if (!rpi_hdr) {
  13756. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13757. "2002 Error Could not grow rpi "
  13758. "count\n");
  13759. } else {
  13760. lrpi = rpi_hdr->start_rpi;
  13761. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13762. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13763. }
  13764. }
  13765. return rpi;
  13766. }
  13767. /**
  13768. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13769. * @phba: pointer to lpfc hba data structure.
  13770. *
  13771. * This routine is invoked to release an rpi to the pool of
  13772. * available rpis maintained by the driver.
  13773. **/
  13774. void
  13775. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13776. {
  13777. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13778. phba->sli4_hba.rpi_count--;
  13779. phba->sli4_hba.max_cfg_param.rpi_used--;
  13780. }
  13781. }
  13782. /**
  13783. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13784. * @phba: pointer to lpfc hba data structure.
  13785. *
  13786. * This routine is invoked to release an rpi to the pool of
  13787. * available rpis maintained by the driver.
  13788. **/
  13789. void
  13790. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13791. {
  13792. spin_lock_irq(&phba->hbalock);
  13793. __lpfc_sli4_free_rpi(phba, rpi);
  13794. spin_unlock_irq(&phba->hbalock);
  13795. }
  13796. /**
  13797. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13798. * @phba: pointer to lpfc hba data structure.
  13799. *
  13800. * This routine is invoked to remove the memory region that
  13801. * provided rpi via a bitmask.
  13802. **/
  13803. void
  13804. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13805. {
  13806. kfree(phba->sli4_hba.rpi_bmask);
  13807. kfree(phba->sli4_hba.rpi_ids);
  13808. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13809. }
  13810. /**
  13811. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13812. * @phba: pointer to lpfc hba data structure.
  13813. *
  13814. * This routine is invoked to remove the memory region that
  13815. * provided rpi via a bitmask.
  13816. **/
  13817. int
  13818. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13819. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13820. {
  13821. LPFC_MBOXQ_t *mboxq;
  13822. struct lpfc_hba *phba = ndlp->phba;
  13823. int rc;
  13824. /* The port is notified of the header region via a mailbox command. */
  13825. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13826. if (!mboxq)
  13827. return -ENOMEM;
  13828. /* Post all rpi memory regions to the port. */
  13829. lpfc_resume_rpi(mboxq, ndlp);
  13830. if (cmpl) {
  13831. mboxq->mbox_cmpl = cmpl;
  13832. mboxq->context1 = arg;
  13833. mboxq->context2 = ndlp;
  13834. } else
  13835. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13836. mboxq->vport = ndlp->vport;
  13837. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13838. if (rc == MBX_NOT_FINISHED) {
  13839. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13840. "2010 Resume RPI Mailbox failed "
  13841. "status %d, mbxStatus x%x\n", rc,
  13842. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13843. mempool_free(mboxq, phba->mbox_mem_pool);
  13844. return -EIO;
  13845. }
  13846. return 0;
  13847. }
  13848. /**
  13849. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13850. * @vport: Pointer to the vport for which the vpi is being initialized
  13851. *
  13852. * This routine is invoked to activate a vpi with the port.
  13853. *
  13854. * Returns:
  13855. * 0 success
  13856. * -Evalue otherwise
  13857. **/
  13858. int
  13859. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13860. {
  13861. LPFC_MBOXQ_t *mboxq;
  13862. int rc = 0;
  13863. int retval = MBX_SUCCESS;
  13864. uint32_t mbox_tmo;
  13865. struct lpfc_hba *phba = vport->phba;
  13866. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13867. if (!mboxq)
  13868. return -ENOMEM;
  13869. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13870. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13871. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13872. if (rc != MBX_SUCCESS) {
  13873. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13874. "2022 INIT VPI Mailbox failed "
  13875. "status %d, mbxStatus x%x\n", rc,
  13876. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13877. retval = -EIO;
  13878. }
  13879. if (rc != MBX_TIMEOUT)
  13880. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13881. return retval;
  13882. }
  13883. /**
  13884. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13885. * @phba: pointer to lpfc hba data structure.
  13886. * @mboxq: Pointer to mailbox object.
  13887. *
  13888. * This routine is invoked to manually add a single FCF record. The caller
  13889. * must pass a completely initialized FCF_Record. This routine takes
  13890. * care of the nonembedded mailbox operations.
  13891. **/
  13892. static void
  13893. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13894. {
  13895. void *virt_addr;
  13896. union lpfc_sli4_cfg_shdr *shdr;
  13897. uint32_t shdr_status, shdr_add_status;
  13898. virt_addr = mboxq->sge_array->addr[0];
  13899. /* The IOCTL status is embedded in the mailbox subheader. */
  13900. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13901. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13902. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13903. if ((shdr_status || shdr_add_status) &&
  13904. (shdr_status != STATUS_FCF_IN_USE))
  13905. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13906. "2558 ADD_FCF_RECORD mailbox failed with "
  13907. "status x%x add_status x%x\n",
  13908. shdr_status, shdr_add_status);
  13909. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13910. }
  13911. /**
  13912. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13913. * @phba: pointer to lpfc hba data structure.
  13914. * @fcf_record: pointer to the initialized fcf record to add.
  13915. *
  13916. * This routine is invoked to manually add a single FCF record. The caller
  13917. * must pass a completely initialized FCF_Record. This routine takes
  13918. * care of the nonembedded mailbox operations.
  13919. **/
  13920. int
  13921. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13922. {
  13923. int rc = 0;
  13924. LPFC_MBOXQ_t *mboxq;
  13925. uint8_t *bytep;
  13926. void *virt_addr;
  13927. dma_addr_t phys_addr;
  13928. struct lpfc_mbx_sge sge;
  13929. uint32_t alloc_len, req_len;
  13930. uint32_t fcfindex;
  13931. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13932. if (!mboxq) {
  13933. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13934. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13935. return -ENOMEM;
  13936. }
  13937. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13938. sizeof(uint32_t);
  13939. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13940. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13941. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13942. req_len, LPFC_SLI4_MBX_NEMBED);
  13943. if (alloc_len < req_len) {
  13944. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13945. "2523 Allocated DMA memory size (x%x) is "
  13946. "less than the requested DMA memory "
  13947. "size (x%x)\n", alloc_len, req_len);
  13948. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13949. return -ENOMEM;
  13950. }
  13951. /*
  13952. * Get the first SGE entry from the non-embedded DMA memory. This
  13953. * routine only uses a single SGE.
  13954. */
  13955. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13956. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13957. virt_addr = mboxq->sge_array->addr[0];
  13958. /*
  13959. * Configure the FCF record for FCFI 0. This is the driver's
  13960. * hardcoded default and gets used in nonFIP mode.
  13961. */
  13962. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13963. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13964. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13965. /*
  13966. * Copy the fcf_index and the FCF Record Data. The data starts after
  13967. * the FCoE header plus word10. The data copy needs to be endian
  13968. * correct.
  13969. */
  13970. bytep += sizeof(uint32_t);
  13971. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13972. mboxq->vport = phba->pport;
  13973. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13974. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13975. if (rc == MBX_NOT_FINISHED) {
  13976. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13977. "2515 ADD_FCF_RECORD mailbox failed with "
  13978. "status 0x%x\n", rc);
  13979. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13980. rc = -EIO;
  13981. } else
  13982. rc = 0;
  13983. return rc;
  13984. }
  13985. /**
  13986. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13987. * @phba: pointer to lpfc hba data structure.
  13988. * @fcf_record: pointer to the fcf record to write the default data.
  13989. * @fcf_index: FCF table entry index.
  13990. *
  13991. * This routine is invoked to build the driver's default FCF record. The
  13992. * values used are hardcoded. This routine handles memory initialization.
  13993. *
  13994. **/
  13995. void
  13996. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13997. struct fcf_record *fcf_record,
  13998. uint16_t fcf_index)
  13999. {
  14000. memset(fcf_record, 0, sizeof(struct fcf_record));
  14001. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  14002. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  14003. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  14004. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  14005. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  14006. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  14007. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  14008. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  14009. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  14010. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  14011. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  14012. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  14013. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  14014. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  14015. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  14016. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  14017. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  14018. /* Set the VLAN bit map */
  14019. if (phba->valid_vlan) {
  14020. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  14021. = 1 << (phba->vlan_id % 8);
  14022. }
  14023. }
  14024. /**
  14025. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  14026. * @phba: pointer to lpfc hba data structure.
  14027. * @fcf_index: FCF table entry offset.
  14028. *
  14029. * This routine is invoked to scan the entire FCF table by reading FCF
  14030. * record and processing it one at a time starting from the @fcf_index
  14031. * for initial FCF discovery or fast FCF failover rediscovery.
  14032. *
  14033. * Return 0 if the mailbox command is submitted successfully, none 0
  14034. * otherwise.
  14035. **/
  14036. int
  14037. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14038. {
  14039. int rc = 0, error;
  14040. LPFC_MBOXQ_t *mboxq;
  14041. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  14042. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  14043. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14044. if (!mboxq) {
  14045. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14046. "2000 Failed to allocate mbox for "
  14047. "READ_FCF cmd\n");
  14048. error = -ENOMEM;
  14049. goto fail_fcf_scan;
  14050. }
  14051. /* Construct the read FCF record mailbox command */
  14052. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14053. if (rc) {
  14054. error = -EINVAL;
  14055. goto fail_fcf_scan;
  14056. }
  14057. /* Issue the mailbox command asynchronously */
  14058. mboxq->vport = phba->pport;
  14059. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  14060. spin_lock_irq(&phba->hbalock);
  14061. phba->hba_flag |= FCF_TS_INPROG;
  14062. spin_unlock_irq(&phba->hbalock);
  14063. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14064. if (rc == MBX_NOT_FINISHED)
  14065. error = -EIO;
  14066. else {
  14067. /* Reset eligible FCF count for new scan */
  14068. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  14069. phba->fcf.eligible_fcf_cnt = 0;
  14070. error = 0;
  14071. }
  14072. fail_fcf_scan:
  14073. if (error) {
  14074. if (mboxq)
  14075. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14076. /* FCF scan failed, clear FCF_TS_INPROG flag */
  14077. spin_lock_irq(&phba->hbalock);
  14078. phba->hba_flag &= ~FCF_TS_INPROG;
  14079. spin_unlock_irq(&phba->hbalock);
  14080. }
  14081. return error;
  14082. }
  14083. /**
  14084. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  14085. * @phba: pointer to lpfc hba data structure.
  14086. * @fcf_index: FCF table entry offset.
  14087. *
  14088. * This routine is invoked to read an FCF record indicated by @fcf_index
  14089. * and to use it for FLOGI roundrobin FCF failover.
  14090. *
  14091. * Return 0 if the mailbox command is submitted successfully, none 0
  14092. * otherwise.
  14093. **/
  14094. int
  14095. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14096. {
  14097. int rc = 0, error;
  14098. LPFC_MBOXQ_t *mboxq;
  14099. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14100. if (!mboxq) {
  14101. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14102. "2763 Failed to allocate mbox for "
  14103. "READ_FCF cmd\n");
  14104. error = -ENOMEM;
  14105. goto fail_fcf_read;
  14106. }
  14107. /* Construct the read FCF record mailbox command */
  14108. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14109. if (rc) {
  14110. error = -EINVAL;
  14111. goto fail_fcf_read;
  14112. }
  14113. /* Issue the mailbox command asynchronously */
  14114. mboxq->vport = phba->pport;
  14115. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  14116. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14117. if (rc == MBX_NOT_FINISHED)
  14118. error = -EIO;
  14119. else
  14120. error = 0;
  14121. fail_fcf_read:
  14122. if (error && mboxq)
  14123. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14124. return error;
  14125. }
  14126. /**
  14127. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  14128. * @phba: pointer to lpfc hba data structure.
  14129. * @fcf_index: FCF table entry offset.
  14130. *
  14131. * This routine is invoked to read an FCF record indicated by @fcf_index to
  14132. * determine whether it's eligible for FLOGI roundrobin failover list.
  14133. *
  14134. * Return 0 if the mailbox command is submitted successfully, none 0
  14135. * otherwise.
  14136. **/
  14137. int
  14138. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  14139. {
  14140. int rc = 0, error;
  14141. LPFC_MBOXQ_t *mboxq;
  14142. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14143. if (!mboxq) {
  14144. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  14145. "2758 Failed to allocate mbox for "
  14146. "READ_FCF cmd\n");
  14147. error = -ENOMEM;
  14148. goto fail_fcf_read;
  14149. }
  14150. /* Construct the read FCF record mailbox command */
  14151. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  14152. if (rc) {
  14153. error = -EINVAL;
  14154. goto fail_fcf_read;
  14155. }
  14156. /* Issue the mailbox command asynchronously */
  14157. mboxq->vport = phba->pport;
  14158. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  14159. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  14160. if (rc == MBX_NOT_FINISHED)
  14161. error = -EIO;
  14162. else
  14163. error = 0;
  14164. fail_fcf_read:
  14165. if (error && mboxq)
  14166. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  14167. return error;
  14168. }
  14169. /**
  14170. * lpfc_check_next_fcf_pri
  14171. * phba pointer to the lpfc_hba struct for this port.
  14172. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  14173. * routine when the rr_bmask is empty. The FCF indecies are put into the
  14174. * rr_bmask based on their priority level. Starting from the highest priority
  14175. * to the lowest. The most likely FCF candidate will be in the highest
  14176. * priority group. When this routine is called it searches the fcf_pri list for
  14177. * next lowest priority group and repopulates the rr_bmask with only those
  14178. * fcf_indexes.
  14179. * returns:
  14180. * 1=success 0=failure
  14181. **/
  14182. int
  14183. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  14184. {
  14185. uint16_t next_fcf_pri;
  14186. uint16_t last_index;
  14187. struct lpfc_fcf_pri *fcf_pri;
  14188. int rc;
  14189. int ret = 0;
  14190. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  14191. LPFC_SLI4_FCF_TBL_INDX_MAX);
  14192. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14193. "3060 Last IDX %d\n", last_index);
  14194. if (list_empty(&phba->fcf.fcf_pri_list)) {
  14195. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14196. "3061 Last IDX %d\n", last_index);
  14197. return 0; /* Empty rr list */
  14198. }
  14199. next_fcf_pri = 0;
  14200. /*
  14201. * Clear the rr_bmask and set all of the bits that are at this
  14202. * priority.
  14203. */
  14204. memset(phba->fcf.fcf_rr_bmask, 0,
  14205. sizeof(*phba->fcf.fcf_rr_bmask));
  14206. spin_lock_irq(&phba->hbalock);
  14207. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14208. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  14209. continue;
  14210. /*
  14211. * the 1st priority that has not FLOGI failed
  14212. * will be the highest.
  14213. */
  14214. if (!next_fcf_pri)
  14215. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14216. spin_unlock_irq(&phba->hbalock);
  14217. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14218. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14219. fcf_pri->fcf_rec.fcf_index);
  14220. if (rc)
  14221. return 0;
  14222. }
  14223. spin_lock_irq(&phba->hbalock);
  14224. }
  14225. /*
  14226. * if next_fcf_pri was not set above and the list is not empty then
  14227. * we have failed flogis on all of them. So reset flogi failed
  14228. * and start at the beginning.
  14229. */
  14230. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  14231. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14232. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  14233. /*
  14234. * the 1st priority that has not FLOGI failed
  14235. * will be the highest.
  14236. */
  14237. if (!next_fcf_pri)
  14238. next_fcf_pri = fcf_pri->fcf_rec.priority;
  14239. spin_unlock_irq(&phba->hbalock);
  14240. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  14241. rc = lpfc_sli4_fcf_rr_index_set(phba,
  14242. fcf_pri->fcf_rec.fcf_index);
  14243. if (rc)
  14244. return 0;
  14245. }
  14246. spin_lock_irq(&phba->hbalock);
  14247. }
  14248. } else
  14249. ret = 1;
  14250. spin_unlock_irq(&phba->hbalock);
  14251. return ret;
  14252. }
  14253. /**
  14254. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  14255. * @phba: pointer to lpfc hba data structure.
  14256. *
  14257. * This routine is to get the next eligible FCF record index in a round
  14258. * robin fashion. If the next eligible FCF record index equals to the
  14259. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  14260. * shall be returned, otherwise, the next eligible FCF record's index
  14261. * shall be returned.
  14262. **/
  14263. uint16_t
  14264. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  14265. {
  14266. uint16_t next_fcf_index;
  14267. initial_priority:
  14268. /* Search start from next bit of currently registered FCF index */
  14269. next_fcf_index = phba->fcf.current_rec.fcf_indx;
  14270. next_priority:
  14271. /* Determine the next fcf index to check */
  14272. next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
  14273. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14274. LPFC_SLI4_FCF_TBL_INDX_MAX,
  14275. next_fcf_index);
  14276. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  14277. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14278. /*
  14279. * If we have wrapped then we need to clear the bits that
  14280. * have been tested so that we can detect when we should
  14281. * change the priority level.
  14282. */
  14283. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  14284. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  14285. }
  14286. /* Check roundrobin failover list empty condition */
  14287. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  14288. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  14289. /*
  14290. * If next fcf index is not found check if there are lower
  14291. * Priority level fcf's in the fcf_priority list.
  14292. * Set up the rr_bmask with all of the avaiable fcf bits
  14293. * at that level and continue the selection process.
  14294. */
  14295. if (lpfc_check_next_fcf_pri_level(phba))
  14296. goto initial_priority;
  14297. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14298. "2844 No roundrobin failover FCF available\n");
  14299. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  14300. return LPFC_FCOE_FCF_NEXT_NONE;
  14301. else {
  14302. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  14303. "3063 Only FCF available idx %d, flag %x\n",
  14304. next_fcf_index,
  14305. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  14306. return next_fcf_index;
  14307. }
  14308. }
  14309. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  14310. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  14311. LPFC_FCF_FLOGI_FAILED)
  14312. goto next_priority;
  14313. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14314. "2845 Get next roundrobin failover FCF (x%x)\n",
  14315. next_fcf_index);
  14316. return next_fcf_index;
  14317. }
  14318. /**
  14319. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  14320. * @phba: pointer to lpfc hba data structure.
  14321. *
  14322. * This routine sets the FCF record index in to the eligible bmask for
  14323. * roundrobin failover search. It checks to make sure that the index
  14324. * does not go beyond the range of the driver allocated bmask dimension
  14325. * before setting the bit.
  14326. *
  14327. * Returns 0 if the index bit successfully set, otherwise, it returns
  14328. * -EINVAL.
  14329. **/
  14330. int
  14331. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  14332. {
  14333. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14334. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14335. "2610 FCF (x%x) reached driver's book "
  14336. "keeping dimension:x%x\n",
  14337. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14338. return -EINVAL;
  14339. }
  14340. /* Set the eligible FCF record index bmask */
  14341. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14342. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14343. "2790 Set FCF (x%x) to roundrobin FCF failover "
  14344. "bmask\n", fcf_index);
  14345. return 0;
  14346. }
  14347. /**
  14348. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  14349. * @phba: pointer to lpfc hba data structure.
  14350. *
  14351. * This routine clears the FCF record index from the eligible bmask for
  14352. * roundrobin failover search. It checks to make sure that the index
  14353. * does not go beyond the range of the driver allocated bmask dimension
  14354. * before clearing the bit.
  14355. **/
  14356. void
  14357. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  14358. {
  14359. struct lpfc_fcf_pri *fcf_pri;
  14360. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  14361. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14362. "2762 FCF (x%x) reached driver's book "
  14363. "keeping dimension:x%x\n",
  14364. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  14365. return;
  14366. }
  14367. /* Clear the eligible FCF record index bmask */
  14368. spin_lock_irq(&phba->hbalock);
  14369. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  14370. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  14371. list_del_init(&fcf_pri->list);
  14372. break;
  14373. }
  14374. }
  14375. spin_unlock_irq(&phba->hbalock);
  14376. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  14377. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14378. "2791 Clear FCF (x%x) from roundrobin failover "
  14379. "bmask\n", fcf_index);
  14380. }
  14381. /**
  14382. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  14383. * @phba: pointer to lpfc hba data structure.
  14384. *
  14385. * This routine is the completion routine for the rediscover FCF table mailbox
  14386. * command. If the mailbox command returned failure, it will try to stop the
  14387. * FCF rediscover wait timer.
  14388. **/
  14389. void
  14390. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  14391. {
  14392. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14393. uint32_t shdr_status, shdr_add_status;
  14394. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14395. shdr_status = bf_get(lpfc_mbox_hdr_status,
  14396. &redisc_fcf->header.cfg_shdr.response);
  14397. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  14398. &redisc_fcf->header.cfg_shdr.response);
  14399. if (shdr_status || shdr_add_status) {
  14400. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  14401. "2746 Requesting for FCF rediscovery failed "
  14402. "status x%x add_status x%x\n",
  14403. shdr_status, shdr_add_status);
  14404. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  14405. spin_lock_irq(&phba->hbalock);
  14406. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  14407. spin_unlock_irq(&phba->hbalock);
  14408. /*
  14409. * CVL event triggered FCF rediscover request failed,
  14410. * last resort to re-try current registered FCF entry.
  14411. */
  14412. lpfc_retry_pport_discovery(phba);
  14413. } else {
  14414. spin_lock_irq(&phba->hbalock);
  14415. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  14416. spin_unlock_irq(&phba->hbalock);
  14417. /*
  14418. * DEAD FCF event triggered FCF rediscover request
  14419. * failed, last resort to fail over as a link down
  14420. * to FCF registration.
  14421. */
  14422. lpfc_sli4_fcf_dead_failthrough(phba);
  14423. }
  14424. } else {
  14425. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14426. "2775 Start FCF rediscover quiescent timer\n");
  14427. /*
  14428. * Start FCF rediscovery wait timer for pending FCF
  14429. * before rescan FCF record table.
  14430. */
  14431. lpfc_fcf_redisc_wait_start_timer(phba);
  14432. }
  14433. mempool_free(mbox, phba->mbox_mem_pool);
  14434. }
  14435. /**
  14436. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14437. * @phba: pointer to lpfc hba data structure.
  14438. *
  14439. * This routine is invoked to request for rediscovery of the entire FCF table
  14440. * by the port.
  14441. **/
  14442. int
  14443. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14444. {
  14445. LPFC_MBOXQ_t *mbox;
  14446. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14447. int rc, length;
  14448. /* Cancel retry delay timers to all vports before FCF rediscover */
  14449. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14450. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14451. if (!mbox) {
  14452. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14453. "2745 Failed to allocate mbox for "
  14454. "requesting FCF rediscover.\n");
  14455. return -ENOMEM;
  14456. }
  14457. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14458. sizeof(struct lpfc_sli4_cfg_mhdr));
  14459. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14460. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14461. length, LPFC_SLI4_MBX_EMBED);
  14462. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14463. /* Set count to 0 for invalidating the entire FCF database */
  14464. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14465. /* Issue the mailbox command asynchronously */
  14466. mbox->vport = phba->pport;
  14467. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14468. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14469. if (rc == MBX_NOT_FINISHED) {
  14470. mempool_free(mbox, phba->mbox_mem_pool);
  14471. return -EIO;
  14472. }
  14473. return 0;
  14474. }
  14475. /**
  14476. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14477. * @phba: pointer to lpfc hba data structure.
  14478. *
  14479. * This function is the failover routine as a last resort to the FCF DEAD
  14480. * event when driver failed to perform fast FCF failover.
  14481. **/
  14482. void
  14483. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14484. {
  14485. uint32_t link_state;
  14486. /*
  14487. * Last resort as FCF DEAD event failover will treat this as
  14488. * a link down, but save the link state because we don't want
  14489. * it to be changed to Link Down unless it is already down.
  14490. */
  14491. link_state = phba->link_state;
  14492. lpfc_linkdown(phba);
  14493. phba->link_state = link_state;
  14494. /* Unregister FCF if no devices connected to it */
  14495. lpfc_unregister_unused_fcf(phba);
  14496. }
  14497. /**
  14498. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14499. * @phba: pointer to lpfc hba data structure.
  14500. * @rgn23_data: pointer to configure region 23 data.
  14501. *
  14502. * This function gets SLI3 port configure region 23 data through memory dump
  14503. * mailbox command. When it successfully retrieves data, the size of the data
  14504. * will be returned, otherwise, 0 will be returned.
  14505. **/
  14506. static uint32_t
  14507. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14508. {
  14509. LPFC_MBOXQ_t *pmb = NULL;
  14510. MAILBOX_t *mb;
  14511. uint32_t offset = 0;
  14512. int rc;
  14513. if (!rgn23_data)
  14514. return 0;
  14515. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14516. if (!pmb) {
  14517. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14518. "2600 failed to allocate mailbox memory\n");
  14519. return 0;
  14520. }
  14521. mb = &pmb->u.mb;
  14522. do {
  14523. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14524. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14525. if (rc != MBX_SUCCESS) {
  14526. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14527. "2601 failed to read config "
  14528. "region 23, rc 0x%x Status 0x%x\n",
  14529. rc, mb->mbxStatus);
  14530. mb->un.varDmp.word_cnt = 0;
  14531. }
  14532. /*
  14533. * dump mem may return a zero when finished or we got a
  14534. * mailbox error, either way we are done.
  14535. */
  14536. if (mb->un.varDmp.word_cnt == 0)
  14537. break;
  14538. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14539. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14540. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14541. rgn23_data + offset,
  14542. mb->un.varDmp.word_cnt);
  14543. offset += mb->un.varDmp.word_cnt;
  14544. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14545. mempool_free(pmb, phba->mbox_mem_pool);
  14546. return offset;
  14547. }
  14548. /**
  14549. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14550. * @phba: pointer to lpfc hba data structure.
  14551. * @rgn23_data: pointer to configure region 23 data.
  14552. *
  14553. * This function gets SLI4 port configure region 23 data through memory dump
  14554. * mailbox command. When it successfully retrieves data, the size of the data
  14555. * will be returned, otherwise, 0 will be returned.
  14556. **/
  14557. static uint32_t
  14558. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14559. {
  14560. LPFC_MBOXQ_t *mboxq = NULL;
  14561. struct lpfc_dmabuf *mp = NULL;
  14562. struct lpfc_mqe *mqe;
  14563. uint32_t data_length = 0;
  14564. int rc;
  14565. if (!rgn23_data)
  14566. return 0;
  14567. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14568. if (!mboxq) {
  14569. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14570. "3105 failed to allocate mailbox memory\n");
  14571. return 0;
  14572. }
  14573. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14574. goto out;
  14575. mqe = &mboxq->u.mqe;
  14576. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14577. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14578. if (rc)
  14579. goto out;
  14580. data_length = mqe->un.mb_words[5];
  14581. if (data_length == 0)
  14582. goto out;
  14583. if (data_length > DMP_RGN23_SIZE) {
  14584. data_length = 0;
  14585. goto out;
  14586. }
  14587. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14588. out:
  14589. mempool_free(mboxq, phba->mbox_mem_pool);
  14590. if (mp) {
  14591. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14592. kfree(mp);
  14593. }
  14594. return data_length;
  14595. }
  14596. /**
  14597. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14598. * @phba: pointer to lpfc hba data structure.
  14599. *
  14600. * This function read region 23 and parse TLV for port status to
  14601. * decide if the user disaled the port. If the TLV indicates the
  14602. * port is disabled, the hba_flag is set accordingly.
  14603. **/
  14604. void
  14605. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14606. {
  14607. uint8_t *rgn23_data = NULL;
  14608. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14609. uint32_t offset = 0;
  14610. /* Get adapter Region 23 data */
  14611. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14612. if (!rgn23_data)
  14613. goto out;
  14614. if (phba->sli_rev < LPFC_SLI_REV4)
  14615. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14616. else {
  14617. if_type = bf_get(lpfc_sli_intf_if_type,
  14618. &phba->sli4_hba.sli_intf);
  14619. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14620. goto out;
  14621. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14622. }
  14623. if (!data_size)
  14624. goto out;
  14625. /* Check the region signature first */
  14626. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14627. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14628. "2619 Config region 23 has bad signature\n");
  14629. goto out;
  14630. }
  14631. offset += 4;
  14632. /* Check the data structure version */
  14633. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14634. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14635. "2620 Config region 23 has bad version\n");
  14636. goto out;
  14637. }
  14638. offset += 4;
  14639. /* Parse TLV entries in the region */
  14640. while (offset < data_size) {
  14641. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14642. break;
  14643. /*
  14644. * If the TLV is not driver specific TLV or driver id is
  14645. * not linux driver id, skip the record.
  14646. */
  14647. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14648. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14649. (rgn23_data[offset + 3] != 0)) {
  14650. offset += rgn23_data[offset + 1] * 4 + 4;
  14651. continue;
  14652. }
  14653. /* Driver found a driver specific TLV in the config region */
  14654. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14655. offset += 4;
  14656. tlv_offset = 0;
  14657. /*
  14658. * Search for configured port state sub-TLV.
  14659. */
  14660. while ((offset < data_size) &&
  14661. (tlv_offset < sub_tlv_len)) {
  14662. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14663. offset += 4;
  14664. tlv_offset += 4;
  14665. break;
  14666. }
  14667. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14668. offset += rgn23_data[offset + 1] * 4 + 4;
  14669. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14670. continue;
  14671. }
  14672. /* This HBA contains PORT_STE configured */
  14673. if (!rgn23_data[offset + 2])
  14674. phba->hba_flag |= LINK_DISABLED;
  14675. goto out;
  14676. }
  14677. }
  14678. out:
  14679. kfree(rgn23_data);
  14680. return;
  14681. }
  14682. /**
  14683. * lpfc_wr_object - write an object to the firmware
  14684. * @phba: HBA structure that indicates port to create a queue on.
  14685. * @dmabuf_list: list of dmabufs to write to the port.
  14686. * @size: the total byte value of the objects to write to the port.
  14687. * @offset: the current offset to be used to start the transfer.
  14688. *
  14689. * This routine will create a wr_object mailbox command to send to the port.
  14690. * the mailbox command will be constructed using the dma buffers described in
  14691. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14692. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14693. * used to indicate the starting offset of the transfer and will also return
  14694. * the offset after the write object mailbox has completed. @size is used to
  14695. * determine the end of the object and whether the eof bit should be set.
  14696. *
  14697. * Return 0 is successful and offset will contain the the new offset to use
  14698. * for the next write.
  14699. * Return negative value for error cases.
  14700. **/
  14701. int
  14702. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14703. uint32_t size, uint32_t *offset)
  14704. {
  14705. struct lpfc_mbx_wr_object *wr_object;
  14706. LPFC_MBOXQ_t *mbox;
  14707. int rc = 0, i = 0;
  14708. uint32_t shdr_status, shdr_add_status;
  14709. uint32_t mbox_tmo;
  14710. union lpfc_sli4_cfg_shdr *shdr;
  14711. struct lpfc_dmabuf *dmabuf;
  14712. uint32_t written = 0;
  14713. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14714. if (!mbox)
  14715. return -ENOMEM;
  14716. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14717. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14718. sizeof(struct lpfc_mbx_wr_object) -
  14719. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14720. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14721. wr_object->u.request.write_offset = *offset;
  14722. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14723. wr_object->u.request.object_name[0] =
  14724. cpu_to_le32(wr_object->u.request.object_name[0]);
  14725. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14726. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14727. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14728. break;
  14729. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14730. wr_object->u.request.bde[i].addrHigh =
  14731. putPaddrHigh(dmabuf->phys);
  14732. if (written + SLI4_PAGE_SIZE >= size) {
  14733. wr_object->u.request.bde[i].tus.f.bdeSize =
  14734. (size - written);
  14735. written += (size - written);
  14736. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14737. } else {
  14738. wr_object->u.request.bde[i].tus.f.bdeSize =
  14739. SLI4_PAGE_SIZE;
  14740. written += SLI4_PAGE_SIZE;
  14741. }
  14742. i++;
  14743. }
  14744. wr_object->u.request.bde_count = i;
  14745. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14746. if (!phba->sli4_hba.intr_enable)
  14747. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14748. else {
  14749. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14750. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14751. }
  14752. /* The IOCTL status is embedded in the mailbox subheader. */
  14753. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14754. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14755. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14756. if (rc != MBX_TIMEOUT)
  14757. mempool_free(mbox, phba->mbox_mem_pool);
  14758. if (shdr_status || shdr_add_status || rc) {
  14759. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14760. "3025 Write Object mailbox failed with "
  14761. "status x%x add_status x%x, mbx status x%x\n",
  14762. shdr_status, shdr_add_status, rc);
  14763. rc = -ENXIO;
  14764. } else
  14765. *offset += wr_object->u.response.actual_write_length;
  14766. return rc;
  14767. }
  14768. /**
  14769. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14770. * @vport: pointer to vport data structure.
  14771. *
  14772. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14773. * and REG_VPI mailbox commands associated with the vport. This function
  14774. * is called when driver want to restart discovery of the vport due to
  14775. * a Clear Virtual Link event.
  14776. **/
  14777. void
  14778. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14779. {
  14780. struct lpfc_hba *phba = vport->phba;
  14781. LPFC_MBOXQ_t *mb, *nextmb;
  14782. struct lpfc_dmabuf *mp;
  14783. struct lpfc_nodelist *ndlp;
  14784. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14785. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14786. LIST_HEAD(mbox_cmd_list);
  14787. uint8_t restart_loop;
  14788. /* Clean up internally queued mailbox commands with the vport */
  14789. spin_lock_irq(&phba->hbalock);
  14790. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14791. if (mb->vport != vport)
  14792. continue;
  14793. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14794. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14795. continue;
  14796. list_del(&mb->list);
  14797. list_add_tail(&mb->list, &mbox_cmd_list);
  14798. }
  14799. /* Clean up active mailbox command with the vport */
  14800. mb = phba->sli.mbox_active;
  14801. if (mb && (mb->vport == vport)) {
  14802. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14803. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14804. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14805. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14806. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14807. /* Put reference count for delayed processing */
  14808. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14809. /* Unregister the RPI when mailbox complete */
  14810. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14811. }
  14812. }
  14813. /* Cleanup any mailbox completions which are not yet processed */
  14814. do {
  14815. restart_loop = 0;
  14816. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14817. /*
  14818. * If this mailox is already processed or it is
  14819. * for another vport ignore it.
  14820. */
  14821. if ((mb->vport != vport) ||
  14822. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14823. continue;
  14824. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14825. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14826. continue;
  14827. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14828. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14829. ndlp = (struct lpfc_nodelist *)mb->context2;
  14830. /* Unregister the RPI when mailbox complete */
  14831. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14832. restart_loop = 1;
  14833. spin_unlock_irq(&phba->hbalock);
  14834. spin_lock(shost->host_lock);
  14835. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14836. spin_unlock(shost->host_lock);
  14837. spin_lock_irq(&phba->hbalock);
  14838. break;
  14839. }
  14840. }
  14841. } while (restart_loop);
  14842. spin_unlock_irq(&phba->hbalock);
  14843. /* Release the cleaned-up mailbox commands */
  14844. while (!list_empty(&mbox_cmd_list)) {
  14845. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14846. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14847. mp = (struct lpfc_dmabuf *) (mb->context1);
  14848. if (mp) {
  14849. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14850. kfree(mp);
  14851. }
  14852. ndlp = (struct lpfc_nodelist *) mb->context2;
  14853. mb->context2 = NULL;
  14854. if (ndlp) {
  14855. spin_lock(shost->host_lock);
  14856. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14857. spin_unlock(shost->host_lock);
  14858. lpfc_nlp_put(ndlp);
  14859. }
  14860. }
  14861. mempool_free(mb, phba->mbox_mem_pool);
  14862. }
  14863. /* Release the ndlp with the cleaned-up active mailbox command */
  14864. if (act_mbx_ndlp) {
  14865. spin_lock(shost->host_lock);
  14866. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14867. spin_unlock(shost->host_lock);
  14868. lpfc_nlp_put(act_mbx_ndlp);
  14869. }
  14870. }
  14871. /**
  14872. * lpfc_drain_txq - Drain the txq
  14873. * @phba: Pointer to HBA context object.
  14874. *
  14875. * This function attempt to submit IOCBs on the txq
  14876. * to the adapter. For SLI4 adapters, the txq contains
  14877. * ELS IOCBs that have been deferred because the there
  14878. * are no SGLs. This congestion can occur with large
  14879. * vport counts during node discovery.
  14880. **/
  14881. uint32_t
  14882. lpfc_drain_txq(struct lpfc_hba *phba)
  14883. {
  14884. LIST_HEAD(completions);
  14885. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14886. struct lpfc_iocbq *piocbq = 0;
  14887. unsigned long iflags = 0;
  14888. char *fail_msg = NULL;
  14889. struct lpfc_sglq *sglq;
  14890. union lpfc_wqe wqe;
  14891. spin_lock_irqsave(&phba->hbalock, iflags);
  14892. if (pring->txq_cnt > pring->txq_max)
  14893. pring->txq_max = pring->txq_cnt;
  14894. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14895. while (pring->txq_cnt) {
  14896. spin_lock_irqsave(&phba->hbalock, iflags);
  14897. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14898. if (!piocbq) {
  14899. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14900. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14901. "2823 txq empty and txq_cnt is %d\n ",
  14902. pring->txq_cnt);
  14903. break;
  14904. }
  14905. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14906. if (!sglq) {
  14907. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14908. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14909. break;
  14910. }
  14911. /* The xri and iocb resources secured,
  14912. * attempt to issue request
  14913. */
  14914. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14915. piocbq->sli4_xritag = sglq->sli4_xritag;
  14916. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14917. fail_msg = "to convert bpl to sgl";
  14918. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14919. fail_msg = "to convert iocb to wqe";
  14920. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14921. fail_msg = " - Wq is full";
  14922. else
  14923. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14924. if (fail_msg) {
  14925. /* Failed means we can't issue and need to cancel */
  14926. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14927. "2822 IOCB failed %s iotag 0x%x "
  14928. "xri 0x%x\n",
  14929. fail_msg,
  14930. piocbq->iotag, piocbq->sli4_xritag);
  14931. list_add_tail(&piocbq->list, &completions);
  14932. }
  14933. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14934. }
  14935. /* Cancel all the IOCBs that cannot be issued */
  14936. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14937. IOERR_SLI_ABORTED);
  14938. return pring->txq_cnt;
  14939. }