lpfc_sli.c 484 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829
  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 IOCB_t *
  67. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  68. {
  69. return &iocbq->iocb;
  70. }
  71. /**
  72. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  73. * @q: The Work Queue to operate on.
  74. * @wqe: The work Queue Entry to put on the Work queue.
  75. *
  76. * This routine will copy the contents of @wqe to the next available entry on
  77. * the @q. This function will then ring the Work Queue Doorbell to signal the
  78. * HBA to start processing the Work Queue Entry. This function returns 0 if
  79. * successful. If no entries are available on @q then this function will return
  80. * -ENOMEM.
  81. * The caller is expected to hold the hbalock when calling this routine.
  82. **/
  83. static uint32_t
  84. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  85. {
  86. union lpfc_wqe *temp_wqe;
  87. struct lpfc_register doorbell;
  88. uint32_t host_index;
  89. /* sanity check on queue memory */
  90. if (unlikely(!q))
  91. return -ENOMEM;
  92. temp_wqe = q->qe[q->host_index].wqe;
  93. /* If the host has not yet processed the next entry then we are done */
  94. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  95. return -ENOMEM;
  96. /* set consumption flag every once in a while */
  97. if (!((q->host_index + 1) % q->entry_repost))
  98. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  99. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  100. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  101. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  102. /* Update the host index before invoking device */
  103. host_index = q->host_index;
  104. q->host_index = ((q->host_index + 1) % q->entry_count);
  105. /* Ring Doorbell */
  106. doorbell.word0 = 0;
  107. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  108. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  109. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  110. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  111. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  112. return 0;
  113. }
  114. /**
  115. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  116. * @q: The Work Queue to operate on.
  117. * @index: The index to advance the hba index to.
  118. *
  119. * This routine will update the HBA index of a queue to reflect consumption of
  120. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  121. * an entry the host calls this function to update the queue's internal
  122. * pointers. This routine returns the number of entries that were consumed by
  123. * the HBA.
  124. **/
  125. static uint32_t
  126. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  127. {
  128. uint32_t released = 0;
  129. /* sanity check on queue memory */
  130. if (unlikely(!q))
  131. return 0;
  132. if (q->hba_index == index)
  133. return 0;
  134. do {
  135. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  136. released++;
  137. } while (q->hba_index != index);
  138. return released;
  139. }
  140. /**
  141. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  142. * @q: The Mailbox Queue to operate on.
  143. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  144. *
  145. * This routine will copy the contents of @mqe to the next available entry on
  146. * the @q. This function will then ring the Work Queue Doorbell to signal the
  147. * HBA to start processing the Work Queue Entry. This function returns 0 if
  148. * successful. If no entries are available on @q then this function will return
  149. * -ENOMEM.
  150. * The caller is expected to hold the hbalock when calling this routine.
  151. **/
  152. static uint32_t
  153. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  154. {
  155. struct lpfc_mqe *temp_mqe;
  156. struct lpfc_register doorbell;
  157. uint32_t host_index;
  158. /* sanity check on queue memory */
  159. if (unlikely(!q))
  160. return -ENOMEM;
  161. temp_mqe = q->qe[q->host_index].mqe;
  162. /* If the host has not yet processed the next entry then we are done */
  163. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  164. return -ENOMEM;
  165. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  166. /* Save off the mailbox pointer for completion */
  167. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  168. /* Update the host index before invoking device */
  169. host_index = q->host_index;
  170. q->host_index = ((q->host_index + 1) % q->entry_count);
  171. /* Ring Doorbell */
  172. doorbell.word0 = 0;
  173. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  174. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  175. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  176. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  177. return 0;
  178. }
  179. /**
  180. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  181. * @q: The Mailbox Queue to operate on.
  182. *
  183. * This routine will update the HBA index of a queue to reflect consumption of
  184. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  185. * an entry the host calls this function to update the queue's internal
  186. * pointers. This routine returns the number of entries that were consumed by
  187. * the HBA.
  188. **/
  189. static uint32_t
  190. lpfc_sli4_mq_release(struct lpfc_queue *q)
  191. {
  192. /* sanity check on queue memory */
  193. if (unlikely(!q))
  194. return 0;
  195. /* Clear the mailbox pointer for completion */
  196. q->phba->mbox = NULL;
  197. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  198. return 1;
  199. }
  200. /**
  201. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  202. * @q: The Event Queue to get the first valid EQE from
  203. *
  204. * This routine will get the first valid Event Queue Entry from @q, update
  205. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  206. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  207. * processed, but not popped back to the HBA then this routine will return NULL.
  208. **/
  209. static struct lpfc_eqe *
  210. lpfc_sli4_eq_get(struct lpfc_queue *q)
  211. {
  212. struct lpfc_eqe *eqe;
  213. /* sanity check on queue memory */
  214. if (unlikely(!q))
  215. return NULL;
  216. eqe = q->qe[q->hba_index].eqe;
  217. /* If the next EQE is not valid then we are done */
  218. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  219. return NULL;
  220. /* If the host has not yet processed the next entry then we are done */
  221. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  222. return NULL;
  223. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  224. return eqe;
  225. }
  226. /**
  227. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  228. * @q: The Event Queue that the host has completed processing for.
  229. * @arm: Indicates whether the host wants to arms this CQ.
  230. *
  231. * This routine will mark all Event Queue Entries on @q, from the last
  232. * known completed entry to the last entry that was processed, as completed
  233. * by clearing the valid bit for each completion queue entry. Then it will
  234. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  235. * The internal host index in the @q will be updated by this routine to indicate
  236. * that the host has finished processing the entries. The @arm parameter
  237. * indicates that the queue should be rearmed when ringing the doorbell.
  238. *
  239. * This function will return the number of EQEs that were popped.
  240. **/
  241. uint32_t
  242. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  243. {
  244. uint32_t released = 0;
  245. struct lpfc_eqe *temp_eqe;
  246. struct lpfc_register doorbell;
  247. /* sanity check on queue memory */
  248. if (unlikely(!q))
  249. return 0;
  250. /* while there are valid entries */
  251. while (q->hba_index != q->host_index) {
  252. temp_eqe = q->qe[q->host_index].eqe;
  253. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  254. released++;
  255. q->host_index = ((q->host_index + 1) % q->entry_count);
  256. }
  257. if (unlikely(released == 0 && !arm))
  258. return 0;
  259. /* ring doorbell for number popped */
  260. doorbell.word0 = 0;
  261. if (arm) {
  262. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  263. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  264. }
  265. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  266. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  267. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  268. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  269. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  270. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  271. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  272. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  273. readl(q->phba->sli4_hba.EQCQDBregaddr);
  274. return released;
  275. }
  276. /**
  277. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  278. * @q: The Completion Queue to get the first valid CQE from
  279. *
  280. * This routine will get the first valid Completion Queue Entry from @q, update
  281. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  282. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  283. * processed, but not popped back to the HBA then this routine will return NULL.
  284. **/
  285. static struct lpfc_cqe *
  286. lpfc_sli4_cq_get(struct lpfc_queue *q)
  287. {
  288. struct lpfc_cqe *cqe;
  289. /* sanity check on queue memory */
  290. if (unlikely(!q))
  291. return NULL;
  292. /* If the next CQE is not valid then we are done */
  293. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  294. return NULL;
  295. /* If the host has not yet processed the next entry then we are done */
  296. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  297. return NULL;
  298. cqe = q->qe[q->hba_index].cqe;
  299. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  300. return cqe;
  301. }
  302. /**
  303. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  304. * @q: The Completion Queue that the host has completed processing for.
  305. * @arm: Indicates whether the host wants to arms this CQ.
  306. *
  307. * This routine will mark all Completion queue entries on @q, from the last
  308. * known completed entry to the last entry that was processed, as completed
  309. * by clearing the valid bit for each completion queue entry. Then it will
  310. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  311. * The internal host index in the @q will be updated by this routine to indicate
  312. * that the host has finished processing the entries. The @arm parameter
  313. * indicates that the queue should be rearmed when ringing the doorbell.
  314. *
  315. * This function will return the number of CQEs that were released.
  316. **/
  317. uint32_t
  318. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  319. {
  320. uint32_t released = 0;
  321. struct lpfc_cqe *temp_qe;
  322. struct lpfc_register doorbell;
  323. /* sanity check on queue memory */
  324. if (unlikely(!q))
  325. return 0;
  326. /* while there are valid entries */
  327. while (q->hba_index != q->host_index) {
  328. temp_qe = q->qe[q->host_index].cqe;
  329. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  330. released++;
  331. q->host_index = ((q->host_index + 1) % q->entry_count);
  332. }
  333. if (unlikely(released == 0 && !arm))
  334. return 0;
  335. /* ring doorbell for number popped */
  336. doorbell.word0 = 0;
  337. if (arm)
  338. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  339. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  340. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  341. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  342. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  343. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  344. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  345. return released;
  346. }
  347. /**
  348. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  349. * @q: The Header Receive Queue to operate on.
  350. * @wqe: The Receive Queue Entry to put on the Receive queue.
  351. *
  352. * This routine will copy the contents of @wqe to the next available entry on
  353. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  354. * HBA to start processing the Receive Queue Entry. This function returns the
  355. * index that the rqe was copied to if successful. If no entries are available
  356. * on @q then this function will return -ENOMEM.
  357. * The caller is expected to hold the hbalock when calling this routine.
  358. **/
  359. static int
  360. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  361. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  362. {
  363. struct lpfc_rqe *temp_hrqe;
  364. struct lpfc_rqe *temp_drqe;
  365. struct lpfc_register doorbell;
  366. int put_index = hq->host_index;
  367. /* sanity check on queue memory */
  368. if (unlikely(!hq) || unlikely(!dq))
  369. return -ENOMEM;
  370. temp_hrqe = hq->qe[hq->host_index].rqe;
  371. temp_drqe = dq->qe[dq->host_index].rqe;
  372. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  373. return -EINVAL;
  374. if (hq->host_index != dq->host_index)
  375. return -EINVAL;
  376. /* If the host has not yet processed the next entry then we are done */
  377. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  378. return -EBUSY;
  379. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  380. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  381. /* Update the host index to point to the next slot */
  382. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  383. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  384. /* Ring The Header Receive Queue Doorbell */
  385. if (!(hq->host_index % hq->entry_repost)) {
  386. doorbell.word0 = 0;
  387. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  388. hq->entry_repost);
  389. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  390. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  391. }
  392. return put_index;
  393. }
  394. /**
  395. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  396. * @q: The Header Receive Queue to operate on.
  397. *
  398. * This routine will update the HBA index of a queue to reflect consumption of
  399. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  400. * consumed an entry the host calls this function to update the queue's
  401. * internal pointers. This routine returns the number of entries that were
  402. * consumed by the HBA.
  403. **/
  404. static uint32_t
  405. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  406. {
  407. /* sanity check on queue memory */
  408. if (unlikely(!hq) || unlikely(!dq))
  409. return 0;
  410. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  411. return 0;
  412. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  413. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  414. return 1;
  415. }
  416. /**
  417. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  418. * @phba: Pointer to HBA context object.
  419. * @pring: Pointer to driver SLI ring object.
  420. *
  421. * This function returns pointer to next command iocb entry
  422. * in the command ring. The caller must hold hbalock to prevent
  423. * other threads consume the next command iocb.
  424. * SLI-2/SLI-3 provide different sized iocbs.
  425. **/
  426. static inline IOCB_t *
  427. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  428. {
  429. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  430. pring->cmdidx * phba->iocb_cmd_size);
  431. }
  432. /**
  433. * lpfc_resp_iocb - Get next response iocb entry in the ring
  434. * @phba: Pointer to HBA context object.
  435. * @pring: Pointer to driver SLI ring object.
  436. *
  437. * This function returns pointer to next response iocb entry
  438. * in the response ring. The caller must hold hbalock to make sure
  439. * that no other thread consume the next response iocb.
  440. * SLI-2/SLI-3 provide different sized iocbs.
  441. **/
  442. static inline IOCB_t *
  443. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  444. {
  445. return (IOCB_t *) (((char *) pring->rspringaddr) +
  446. pring->rspidx * phba->iocb_rsp_size);
  447. }
  448. /**
  449. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  450. * @phba: Pointer to HBA context object.
  451. *
  452. * This function is called with hbalock held. This function
  453. * allocates a new driver iocb object from the iocb pool. If the
  454. * allocation is successful, it returns pointer to the newly
  455. * allocated iocb object else it returns NULL.
  456. **/
  457. static struct lpfc_iocbq *
  458. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  459. {
  460. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  461. struct lpfc_iocbq * iocbq = NULL;
  462. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  463. if (iocbq)
  464. phba->iocb_cnt++;
  465. if (phba->iocb_cnt > phba->iocb_max)
  466. phba->iocb_max = phba->iocb_cnt;
  467. return iocbq;
  468. }
  469. /**
  470. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  471. * @phba: Pointer to HBA context object.
  472. * @xritag: XRI value.
  473. *
  474. * This function clears the sglq pointer from the array of acive
  475. * sglq's. The xritag that is passed in is used to index into the
  476. * array. Before the xritag can be used it needs to be adjusted
  477. * by subtracting the xribase.
  478. *
  479. * Returns sglq ponter = success, NULL = Failure.
  480. **/
  481. static struct lpfc_sglq *
  482. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  483. {
  484. struct lpfc_sglq *sglq;
  485. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  486. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  487. return sglq;
  488. }
  489. /**
  490. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  491. * @phba: Pointer to HBA context object.
  492. * @xritag: XRI value.
  493. *
  494. * This function returns the sglq pointer from the array of acive
  495. * sglq's. The xritag that is passed in is used to index into the
  496. * array. Before the xritag can be used it needs to be adjusted
  497. * by subtracting the xribase.
  498. *
  499. * Returns sglq ponter = success, NULL = Failure.
  500. **/
  501. struct lpfc_sglq *
  502. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  503. {
  504. struct lpfc_sglq *sglq;
  505. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  506. return sglq;
  507. }
  508. /**
  509. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  510. * @phba: Pointer to HBA context object.
  511. * @xritag: xri used in this exchange.
  512. * @rrq: The RRQ to be cleared.
  513. *
  514. **/
  515. void
  516. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  517. uint16_t xritag,
  518. struct lpfc_node_rrq *rrq)
  519. {
  520. struct lpfc_nodelist *ndlp = NULL;
  521. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  522. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  523. /* The target DID could have been swapped (cable swap)
  524. * we should use the ndlp from the findnode if it is
  525. * available.
  526. */
  527. if ((!ndlp) && rrq->ndlp)
  528. ndlp = rrq->ndlp;
  529. if (!ndlp)
  530. goto out;
  531. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  532. rrq->send_rrq = 0;
  533. rrq->xritag = 0;
  534. rrq->rrq_stop_time = 0;
  535. }
  536. out:
  537. mempool_free(rrq, phba->rrq_pool);
  538. }
  539. /**
  540. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  541. * @phba: Pointer to HBA context object.
  542. *
  543. * This function is called with hbalock held. This function
  544. * Checks if stop_time (ratov from setting rrq active) has
  545. * been reached, if it has and the send_rrq flag is set then
  546. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  547. * then it will just call the routine to clear the rrq and
  548. * free the rrq resource.
  549. * The timer is set to the next rrq that is going to expire before
  550. * leaving the routine.
  551. *
  552. **/
  553. void
  554. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  555. {
  556. struct lpfc_node_rrq *rrq;
  557. struct lpfc_node_rrq *nextrrq;
  558. unsigned long next_time;
  559. unsigned long iflags;
  560. LIST_HEAD(send_rrq);
  561. spin_lock_irqsave(&phba->hbalock, iflags);
  562. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  563. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  564. list_for_each_entry_safe(rrq, nextrrq,
  565. &phba->active_rrq_list, list) {
  566. if (time_after(jiffies, rrq->rrq_stop_time))
  567. list_move(&rrq->list, &send_rrq);
  568. else if (time_before(rrq->rrq_stop_time, next_time))
  569. next_time = rrq->rrq_stop_time;
  570. }
  571. spin_unlock_irqrestore(&phba->hbalock, iflags);
  572. if (!list_empty(&phba->active_rrq_list))
  573. mod_timer(&phba->rrq_tmr, next_time);
  574. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  575. list_del(&rrq->list);
  576. if (!rrq->send_rrq)
  577. /* this call will free the rrq */
  578. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  579. else if (lpfc_send_rrq(phba, rrq)) {
  580. /* if we send the rrq then the completion handler
  581. * will clear the bit in the xribitmap.
  582. */
  583. lpfc_clr_rrq_active(phba, rrq->xritag,
  584. rrq);
  585. }
  586. }
  587. }
  588. /**
  589. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  590. * @vport: Pointer to vport context object.
  591. * @xri: The xri used in the exchange.
  592. * @did: The targets DID for this exchange.
  593. *
  594. * returns NULL = rrq not found in the phba->active_rrq_list.
  595. * rrq = rrq for this xri and target.
  596. **/
  597. struct lpfc_node_rrq *
  598. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  599. {
  600. struct lpfc_hba *phba = vport->phba;
  601. struct lpfc_node_rrq *rrq;
  602. struct lpfc_node_rrq *nextrrq;
  603. unsigned long iflags;
  604. if (phba->sli_rev != LPFC_SLI_REV4)
  605. return NULL;
  606. spin_lock_irqsave(&phba->hbalock, iflags);
  607. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  608. if (rrq->vport == vport && rrq->xritag == xri &&
  609. rrq->nlp_DID == did){
  610. list_del(&rrq->list);
  611. spin_unlock_irqrestore(&phba->hbalock, iflags);
  612. return rrq;
  613. }
  614. }
  615. spin_unlock_irqrestore(&phba->hbalock, iflags);
  616. return NULL;
  617. }
  618. /**
  619. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  620. * @vport: Pointer to vport context object.
  621. * @ndlp: Pointer to the lpfc_node_list structure.
  622. * If ndlp is NULL Remove all active RRQs for this vport from the
  623. * phba->active_rrq_list and clear the rrq.
  624. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  625. **/
  626. void
  627. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  628. {
  629. struct lpfc_hba *phba = vport->phba;
  630. struct lpfc_node_rrq *rrq;
  631. struct lpfc_node_rrq *nextrrq;
  632. unsigned long iflags;
  633. LIST_HEAD(rrq_list);
  634. if (phba->sli_rev != LPFC_SLI_REV4)
  635. return;
  636. if (!ndlp) {
  637. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  638. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  639. }
  640. spin_lock_irqsave(&phba->hbalock, iflags);
  641. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  642. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  643. list_move(&rrq->list, &rrq_list);
  644. spin_unlock_irqrestore(&phba->hbalock, iflags);
  645. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  646. list_del(&rrq->list);
  647. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  648. }
  649. }
  650. /**
  651. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  652. * @phba: Pointer to HBA context object.
  653. *
  654. * Remove all rrqs from the phba->active_rrq_list and free them by
  655. * calling __lpfc_clr_active_rrq
  656. *
  657. **/
  658. void
  659. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  660. {
  661. struct lpfc_node_rrq *rrq;
  662. struct lpfc_node_rrq *nextrrq;
  663. unsigned long next_time;
  664. unsigned long iflags;
  665. LIST_HEAD(rrq_list);
  666. if (phba->sli_rev != LPFC_SLI_REV4)
  667. return;
  668. spin_lock_irqsave(&phba->hbalock, iflags);
  669. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  670. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  671. list_splice_init(&phba->active_rrq_list, &rrq_list);
  672. spin_unlock_irqrestore(&phba->hbalock, iflags);
  673. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  674. list_del(&rrq->list);
  675. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  676. }
  677. if (!list_empty(&phba->active_rrq_list))
  678. mod_timer(&phba->rrq_tmr, next_time);
  679. }
  680. /**
  681. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  682. * @phba: Pointer to HBA context object.
  683. * @ndlp: Targets nodelist pointer for this exchange.
  684. * @xritag the xri in the bitmap to test.
  685. *
  686. * This function is called with hbalock held. This function
  687. * returns 0 = rrq not active for this xri
  688. * 1 = rrq is valid for this xri.
  689. **/
  690. int
  691. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  692. uint16_t xritag)
  693. {
  694. if (!ndlp)
  695. return 0;
  696. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  697. return 1;
  698. else
  699. return 0;
  700. }
  701. /**
  702. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  703. * @phba: Pointer to HBA context object.
  704. * @ndlp: nodelist pointer for this target.
  705. * @xritag: xri used in this exchange.
  706. * @rxid: Remote Exchange ID.
  707. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  708. *
  709. * This function takes the hbalock.
  710. * The active bit is always set in the active rrq xri_bitmap even
  711. * if there is no slot avaiable for the other rrq information.
  712. *
  713. * returns 0 rrq actived for this xri
  714. * < 0 No memory or invalid ndlp.
  715. **/
  716. int
  717. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  718. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  719. {
  720. unsigned long iflags;
  721. struct lpfc_node_rrq *rrq;
  722. int empty;
  723. if (!ndlp)
  724. return -EINVAL;
  725. if (!phba->cfg_enable_rrq)
  726. return -EINVAL;
  727. spin_lock_irqsave(&phba->hbalock, iflags);
  728. if (phba->pport->load_flag & FC_UNLOADING) {
  729. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  730. goto out;
  731. }
  732. /*
  733. * set the active bit even if there is no mem available.
  734. */
  735. if (NLP_CHK_FREE_REQ(ndlp))
  736. goto out;
  737. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  738. goto out;
  739. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  740. goto out;
  741. spin_unlock_irqrestore(&phba->hbalock, iflags);
  742. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  743. if (!rrq) {
  744. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  745. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  746. " DID:0x%x Send:%d\n",
  747. xritag, rxid, ndlp->nlp_DID, send_rrq);
  748. return -EINVAL;
  749. }
  750. rrq->send_rrq = send_rrq;
  751. rrq->xritag = xritag;
  752. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  753. rrq->ndlp = ndlp;
  754. rrq->nlp_DID = ndlp->nlp_DID;
  755. rrq->vport = ndlp->vport;
  756. rrq->rxid = rxid;
  757. rrq->send_rrq = send_rrq;
  758. spin_lock_irqsave(&phba->hbalock, iflags);
  759. empty = list_empty(&phba->active_rrq_list);
  760. list_add_tail(&rrq->list, &phba->active_rrq_list);
  761. phba->hba_flag |= HBA_RRQ_ACTIVE;
  762. if (empty)
  763. lpfc_worker_wake_up(phba);
  764. spin_unlock_irqrestore(&phba->hbalock, iflags);
  765. return 0;
  766. out:
  767. spin_unlock_irqrestore(&phba->hbalock, iflags);
  768. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  769. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  770. " DID:0x%x Send:%d\n",
  771. xritag, rxid, ndlp->nlp_DID, send_rrq);
  772. return -EINVAL;
  773. }
  774. /**
  775. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  776. * @phba: Pointer to HBA context object.
  777. * @piocb: Pointer to the iocbq.
  778. *
  779. * This function is called with hbalock held. This function
  780. * gets a new driver sglq object from the sglq list. If the
  781. * list is not empty then it is successful, it returns pointer to the newly
  782. * allocated sglq object else it returns NULL.
  783. **/
  784. static struct lpfc_sglq *
  785. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  786. {
  787. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  788. struct lpfc_sglq *sglq = NULL;
  789. struct lpfc_sglq *start_sglq = NULL;
  790. struct lpfc_scsi_buf *lpfc_cmd;
  791. struct lpfc_nodelist *ndlp;
  792. int found = 0;
  793. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  794. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  795. ndlp = lpfc_cmd->rdata->pnode;
  796. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  797. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  798. ndlp = piocbq->context_un.ndlp;
  799. else
  800. ndlp = piocbq->context1;
  801. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  802. start_sglq = sglq;
  803. while (!found) {
  804. if (!sglq)
  805. return NULL;
  806. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
  807. /* This xri has an rrq outstanding for this DID.
  808. * put it back in the list and get another xri.
  809. */
  810. list_add_tail(&sglq->list, lpfc_sgl_list);
  811. sglq = NULL;
  812. list_remove_head(lpfc_sgl_list, sglq,
  813. struct lpfc_sglq, list);
  814. if (sglq == start_sglq) {
  815. sglq = NULL;
  816. break;
  817. } else
  818. continue;
  819. }
  820. sglq->ndlp = ndlp;
  821. found = 1;
  822. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  823. sglq->state = SGL_ALLOCATED;
  824. }
  825. return sglq;
  826. }
  827. /**
  828. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  829. * @phba: Pointer to HBA context object.
  830. *
  831. * This function is called with no lock held. This function
  832. * allocates a new driver iocb object from the iocb pool. If the
  833. * allocation is successful, it returns pointer to the newly
  834. * allocated iocb object else it returns NULL.
  835. **/
  836. struct lpfc_iocbq *
  837. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  838. {
  839. struct lpfc_iocbq * iocbq = NULL;
  840. unsigned long iflags;
  841. spin_lock_irqsave(&phba->hbalock, iflags);
  842. iocbq = __lpfc_sli_get_iocbq(phba);
  843. spin_unlock_irqrestore(&phba->hbalock, iflags);
  844. return iocbq;
  845. }
  846. /**
  847. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  848. * @phba: Pointer to HBA context object.
  849. * @iocbq: Pointer to driver iocb object.
  850. *
  851. * This function is called with hbalock held to release driver
  852. * iocb object to the iocb pool. The iotag in the iocb object
  853. * does not change for each use of the iocb object. This function
  854. * clears all other fields of the iocb object when it is freed.
  855. * The sqlq structure that holds the xritag and phys and virtual
  856. * mappings for the scatter gather list is retrieved from the
  857. * active array of sglq. The get of the sglq pointer also clears
  858. * the entry in the array. If the status of the IO indiactes that
  859. * this IO was aborted then the sglq entry it put on the
  860. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  861. * IO has good status or fails for any other reason then the sglq
  862. * entry is added to the free list (lpfc_sgl_list).
  863. **/
  864. static void
  865. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  866. {
  867. struct lpfc_sglq *sglq;
  868. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  869. unsigned long iflag = 0;
  870. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  871. if (iocbq->sli4_xritag == NO_XRI)
  872. sglq = NULL;
  873. else
  874. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  875. if (sglq) {
  876. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  877. (sglq->state != SGL_XRI_ABORTED)) {
  878. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  879. iflag);
  880. list_add(&sglq->list,
  881. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  882. spin_unlock_irqrestore(
  883. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  884. } else {
  885. sglq->state = SGL_FREED;
  886. sglq->ndlp = NULL;
  887. list_add_tail(&sglq->list,
  888. &phba->sli4_hba.lpfc_sgl_list);
  889. /* Check if TXQ queue needs to be serviced */
  890. if (pring->txq_cnt)
  891. lpfc_worker_wake_up(phba);
  892. }
  893. }
  894. /*
  895. * Clean all volatile data fields, preserve iotag and node struct.
  896. */
  897. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  898. iocbq->sli4_lxritag = NO_XRI;
  899. iocbq->sli4_xritag = NO_XRI;
  900. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  901. }
  902. /**
  903. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  904. * @phba: Pointer to HBA context object.
  905. * @iocbq: Pointer to driver iocb object.
  906. *
  907. * This function is called with hbalock held to release driver
  908. * iocb object to the iocb pool. The iotag in the iocb object
  909. * does not change for each use of the iocb object. This function
  910. * clears all other fields of the iocb object when it is freed.
  911. **/
  912. static void
  913. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  914. {
  915. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  916. /*
  917. * Clean all volatile data fields, preserve iotag and node struct.
  918. */
  919. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  920. iocbq->sli4_xritag = NO_XRI;
  921. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  922. }
  923. /**
  924. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  925. * @phba: Pointer to HBA context object.
  926. * @iocbq: Pointer to driver iocb object.
  927. *
  928. * This function is called with hbalock held to release driver
  929. * iocb object to the iocb pool. The iotag in the iocb object
  930. * does not change for each use of the iocb object. This function
  931. * clears all other fields of the iocb object when it is freed.
  932. **/
  933. static void
  934. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  935. {
  936. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  937. phba->iocb_cnt--;
  938. }
  939. /**
  940. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  941. * @phba: Pointer to HBA context object.
  942. * @iocbq: Pointer to driver iocb object.
  943. *
  944. * This function is called with no lock held to release the iocb to
  945. * iocb pool.
  946. **/
  947. void
  948. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  949. {
  950. unsigned long iflags;
  951. /*
  952. * Clean all volatile data fields, preserve iotag and node struct.
  953. */
  954. spin_lock_irqsave(&phba->hbalock, iflags);
  955. __lpfc_sli_release_iocbq(phba, iocbq);
  956. spin_unlock_irqrestore(&phba->hbalock, iflags);
  957. }
  958. /**
  959. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  960. * @phba: Pointer to HBA context object.
  961. * @iocblist: List of IOCBs.
  962. * @ulpstatus: ULP status in IOCB command field.
  963. * @ulpWord4: ULP word-4 in IOCB command field.
  964. *
  965. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  966. * on the list by invoking the complete callback function associated with the
  967. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  968. * fields.
  969. **/
  970. void
  971. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  972. uint32_t ulpstatus, uint32_t ulpWord4)
  973. {
  974. struct lpfc_iocbq *piocb;
  975. while (!list_empty(iocblist)) {
  976. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  977. if (!piocb->iocb_cmpl)
  978. lpfc_sli_release_iocbq(phba, piocb);
  979. else {
  980. piocb->iocb.ulpStatus = ulpstatus;
  981. piocb->iocb.un.ulpWord[4] = ulpWord4;
  982. (piocb->iocb_cmpl) (phba, piocb, piocb);
  983. }
  984. }
  985. return;
  986. }
  987. /**
  988. * lpfc_sli_iocb_cmd_type - Get the iocb type
  989. * @iocb_cmnd: iocb command code.
  990. *
  991. * This function is called by ring event handler function to get the iocb type.
  992. * This function translates the iocb command to an iocb command type used to
  993. * decide the final disposition of each completed IOCB.
  994. * The function returns
  995. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  996. * LPFC_SOL_IOCB if it is a solicited iocb completion
  997. * LPFC_ABORT_IOCB if it is an abort iocb
  998. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  999. *
  1000. * The caller is not required to hold any lock.
  1001. **/
  1002. static lpfc_iocb_type
  1003. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1004. {
  1005. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1006. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1007. return 0;
  1008. switch (iocb_cmnd) {
  1009. case CMD_XMIT_SEQUENCE_CR:
  1010. case CMD_XMIT_SEQUENCE_CX:
  1011. case CMD_XMIT_BCAST_CN:
  1012. case CMD_XMIT_BCAST_CX:
  1013. case CMD_ELS_REQUEST_CR:
  1014. case CMD_ELS_REQUEST_CX:
  1015. case CMD_CREATE_XRI_CR:
  1016. case CMD_CREATE_XRI_CX:
  1017. case CMD_GET_RPI_CN:
  1018. case CMD_XMIT_ELS_RSP_CX:
  1019. case CMD_GET_RPI_CR:
  1020. case CMD_FCP_IWRITE_CR:
  1021. case CMD_FCP_IWRITE_CX:
  1022. case CMD_FCP_IREAD_CR:
  1023. case CMD_FCP_IREAD_CX:
  1024. case CMD_FCP_ICMND_CR:
  1025. case CMD_FCP_ICMND_CX:
  1026. case CMD_FCP_TSEND_CX:
  1027. case CMD_FCP_TRSP_CX:
  1028. case CMD_FCP_TRECEIVE_CX:
  1029. case CMD_FCP_AUTO_TRSP_CX:
  1030. case CMD_ADAPTER_MSG:
  1031. case CMD_ADAPTER_DUMP:
  1032. case CMD_XMIT_SEQUENCE64_CR:
  1033. case CMD_XMIT_SEQUENCE64_CX:
  1034. case CMD_XMIT_BCAST64_CN:
  1035. case CMD_XMIT_BCAST64_CX:
  1036. case CMD_ELS_REQUEST64_CR:
  1037. case CMD_ELS_REQUEST64_CX:
  1038. case CMD_FCP_IWRITE64_CR:
  1039. case CMD_FCP_IWRITE64_CX:
  1040. case CMD_FCP_IREAD64_CR:
  1041. case CMD_FCP_IREAD64_CX:
  1042. case CMD_FCP_ICMND64_CR:
  1043. case CMD_FCP_ICMND64_CX:
  1044. case CMD_FCP_TSEND64_CX:
  1045. case CMD_FCP_TRSP64_CX:
  1046. case CMD_FCP_TRECEIVE64_CX:
  1047. case CMD_GEN_REQUEST64_CR:
  1048. case CMD_GEN_REQUEST64_CX:
  1049. case CMD_XMIT_ELS_RSP64_CX:
  1050. case DSSCMD_IWRITE64_CR:
  1051. case DSSCMD_IWRITE64_CX:
  1052. case DSSCMD_IREAD64_CR:
  1053. case DSSCMD_IREAD64_CX:
  1054. type = LPFC_SOL_IOCB;
  1055. break;
  1056. case CMD_ABORT_XRI_CN:
  1057. case CMD_ABORT_XRI_CX:
  1058. case CMD_CLOSE_XRI_CN:
  1059. case CMD_CLOSE_XRI_CX:
  1060. case CMD_XRI_ABORTED_CX:
  1061. case CMD_ABORT_MXRI64_CN:
  1062. case CMD_XMIT_BLS_RSP64_CX:
  1063. type = LPFC_ABORT_IOCB;
  1064. break;
  1065. case CMD_RCV_SEQUENCE_CX:
  1066. case CMD_RCV_ELS_REQ_CX:
  1067. case CMD_RCV_SEQUENCE64_CX:
  1068. case CMD_RCV_ELS_REQ64_CX:
  1069. case CMD_ASYNC_STATUS:
  1070. case CMD_IOCB_RCV_SEQ64_CX:
  1071. case CMD_IOCB_RCV_ELS64_CX:
  1072. case CMD_IOCB_RCV_CONT64_CX:
  1073. case CMD_IOCB_RET_XRI64_CX:
  1074. type = LPFC_UNSOL_IOCB;
  1075. break;
  1076. case CMD_IOCB_XMIT_MSEQ64_CR:
  1077. case CMD_IOCB_XMIT_MSEQ64_CX:
  1078. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1079. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1080. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1081. case CMD_IOCB_ABORT_EXTENDED_CN:
  1082. case CMD_IOCB_RET_HBQE64_CN:
  1083. case CMD_IOCB_FCP_IBIDIR64_CR:
  1084. case CMD_IOCB_FCP_IBIDIR64_CX:
  1085. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1086. case CMD_IOCB_LOGENTRY_CN:
  1087. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1088. printk("%s - Unhandled SLI-3 Command x%x\n",
  1089. __func__, iocb_cmnd);
  1090. type = LPFC_UNKNOWN_IOCB;
  1091. break;
  1092. default:
  1093. type = LPFC_UNKNOWN_IOCB;
  1094. break;
  1095. }
  1096. return type;
  1097. }
  1098. /**
  1099. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1100. * @phba: Pointer to HBA context object.
  1101. *
  1102. * This function is called from SLI initialization code
  1103. * to configure every ring of the HBA's SLI interface. The
  1104. * caller is not required to hold any lock. This function issues
  1105. * a config_ring mailbox command for each ring.
  1106. * This function returns zero if successful else returns a negative
  1107. * error code.
  1108. **/
  1109. static int
  1110. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1111. {
  1112. struct lpfc_sli *psli = &phba->sli;
  1113. LPFC_MBOXQ_t *pmb;
  1114. MAILBOX_t *pmbox;
  1115. int i, rc, ret = 0;
  1116. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1117. if (!pmb)
  1118. return -ENOMEM;
  1119. pmbox = &pmb->u.mb;
  1120. phba->link_state = LPFC_INIT_MBX_CMDS;
  1121. for (i = 0; i < psli->num_rings; i++) {
  1122. lpfc_config_ring(phba, i, pmb);
  1123. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1124. if (rc != MBX_SUCCESS) {
  1125. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1126. "0446 Adapter failed to init (%d), "
  1127. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1128. "ring %d\n",
  1129. rc, pmbox->mbxCommand,
  1130. pmbox->mbxStatus, i);
  1131. phba->link_state = LPFC_HBA_ERROR;
  1132. ret = -ENXIO;
  1133. break;
  1134. }
  1135. }
  1136. mempool_free(pmb, phba->mbox_mem_pool);
  1137. return ret;
  1138. }
  1139. /**
  1140. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1141. * @phba: Pointer to HBA context object.
  1142. * @pring: Pointer to driver SLI ring object.
  1143. * @piocb: Pointer to the driver iocb object.
  1144. *
  1145. * This function is called with hbalock held. The function adds the
  1146. * new iocb to txcmplq of the given ring. This function always returns
  1147. * 0. If this function is called for ELS ring, this function checks if
  1148. * there is a vport associated with the ELS command. This function also
  1149. * starts els_tmofunc timer if this is an ELS command.
  1150. **/
  1151. static int
  1152. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1153. struct lpfc_iocbq *piocb)
  1154. {
  1155. list_add_tail(&piocb->list, &pring->txcmplq);
  1156. piocb->iocb_flag |= LPFC_IO_ON_Q;
  1157. pring->txcmplq_cnt++;
  1158. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1159. pring->txcmplq_max = pring->txcmplq_cnt;
  1160. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1161. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1162. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1163. if (!piocb->vport)
  1164. BUG();
  1165. else
  1166. mod_timer(&piocb->vport->els_tmofunc,
  1167. jiffies + HZ * (phba->fc_ratov << 1));
  1168. }
  1169. return 0;
  1170. }
  1171. /**
  1172. * lpfc_sli_ringtx_get - Get first element of the txq
  1173. * @phba: Pointer to HBA context object.
  1174. * @pring: Pointer to driver SLI ring object.
  1175. *
  1176. * This function is called with hbalock held to get next
  1177. * iocb in txq of the given ring. If there is any iocb in
  1178. * the txq, the function returns first iocb in the list after
  1179. * removing the iocb from the list, else it returns NULL.
  1180. **/
  1181. struct lpfc_iocbq *
  1182. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1183. {
  1184. struct lpfc_iocbq *cmd_iocb;
  1185. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1186. if (cmd_iocb != NULL)
  1187. pring->txq_cnt--;
  1188. return cmd_iocb;
  1189. }
  1190. /**
  1191. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1192. * @phba: Pointer to HBA context object.
  1193. * @pring: Pointer to driver SLI ring object.
  1194. *
  1195. * This function is called with hbalock held and the caller must post the
  1196. * iocb without releasing the lock. If the caller releases the lock,
  1197. * iocb slot returned by the function is not guaranteed to be available.
  1198. * The function returns pointer to the next available iocb slot if there
  1199. * is available slot in the ring, else it returns NULL.
  1200. * If the get index of the ring is ahead of the put index, the function
  1201. * will post an error attention event to the worker thread to take the
  1202. * HBA to offline state.
  1203. **/
  1204. static IOCB_t *
  1205. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1206. {
  1207. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1208. uint32_t max_cmd_idx = pring->numCiocb;
  1209. if ((pring->next_cmdidx == pring->cmdidx) &&
  1210. (++pring->next_cmdidx >= max_cmd_idx))
  1211. pring->next_cmdidx = 0;
  1212. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1213. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1214. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1215. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1216. "0315 Ring %d issue: portCmdGet %d "
  1217. "is bigger than cmd ring %d\n",
  1218. pring->ringno,
  1219. pring->local_getidx, max_cmd_idx);
  1220. phba->link_state = LPFC_HBA_ERROR;
  1221. /*
  1222. * All error attention handlers are posted to
  1223. * worker thread
  1224. */
  1225. phba->work_ha |= HA_ERATT;
  1226. phba->work_hs = HS_FFER3;
  1227. lpfc_worker_wake_up(phba);
  1228. return NULL;
  1229. }
  1230. if (pring->local_getidx == pring->next_cmdidx)
  1231. return NULL;
  1232. }
  1233. return lpfc_cmd_iocb(phba, pring);
  1234. }
  1235. /**
  1236. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1237. * @phba: Pointer to HBA context object.
  1238. * @iocbq: Pointer to driver iocb object.
  1239. *
  1240. * This function gets an iotag for the iocb. If there is no unused iotag and
  1241. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1242. * array and assigns a new iotag.
  1243. * The function returns the allocated iotag if successful, else returns zero.
  1244. * Zero is not a valid iotag.
  1245. * The caller is not required to hold any lock.
  1246. **/
  1247. uint16_t
  1248. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1249. {
  1250. struct lpfc_iocbq **new_arr;
  1251. struct lpfc_iocbq **old_arr;
  1252. size_t new_len;
  1253. struct lpfc_sli *psli = &phba->sli;
  1254. uint16_t iotag;
  1255. spin_lock_irq(&phba->hbalock);
  1256. iotag = psli->last_iotag;
  1257. if(++iotag < psli->iocbq_lookup_len) {
  1258. psli->last_iotag = iotag;
  1259. psli->iocbq_lookup[iotag] = iocbq;
  1260. spin_unlock_irq(&phba->hbalock);
  1261. iocbq->iotag = iotag;
  1262. return iotag;
  1263. } else if (psli->iocbq_lookup_len < (0xffff
  1264. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1265. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1266. spin_unlock_irq(&phba->hbalock);
  1267. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1268. GFP_KERNEL);
  1269. if (new_arr) {
  1270. spin_lock_irq(&phba->hbalock);
  1271. old_arr = psli->iocbq_lookup;
  1272. if (new_len <= psli->iocbq_lookup_len) {
  1273. /* highly unprobable case */
  1274. kfree(new_arr);
  1275. iotag = psli->last_iotag;
  1276. if(++iotag < psli->iocbq_lookup_len) {
  1277. psli->last_iotag = iotag;
  1278. psli->iocbq_lookup[iotag] = iocbq;
  1279. spin_unlock_irq(&phba->hbalock);
  1280. iocbq->iotag = iotag;
  1281. return iotag;
  1282. }
  1283. spin_unlock_irq(&phba->hbalock);
  1284. return 0;
  1285. }
  1286. if (psli->iocbq_lookup)
  1287. memcpy(new_arr, old_arr,
  1288. ((psli->last_iotag + 1) *
  1289. sizeof (struct lpfc_iocbq *)));
  1290. psli->iocbq_lookup = new_arr;
  1291. psli->iocbq_lookup_len = new_len;
  1292. psli->last_iotag = iotag;
  1293. psli->iocbq_lookup[iotag] = iocbq;
  1294. spin_unlock_irq(&phba->hbalock);
  1295. iocbq->iotag = iotag;
  1296. kfree(old_arr);
  1297. return iotag;
  1298. }
  1299. } else
  1300. spin_unlock_irq(&phba->hbalock);
  1301. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1302. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1303. psli->last_iotag);
  1304. return 0;
  1305. }
  1306. /**
  1307. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1308. * @phba: Pointer to HBA context object.
  1309. * @pring: Pointer to driver SLI ring object.
  1310. * @iocb: Pointer to iocb slot in the ring.
  1311. * @nextiocb: Pointer to driver iocb object which need to be
  1312. * posted to firmware.
  1313. *
  1314. * This function is called with hbalock held to post a new iocb to
  1315. * the firmware. This function copies the new iocb to ring iocb slot and
  1316. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1317. * a completion call back for this iocb else the function will free the
  1318. * iocb object.
  1319. **/
  1320. static void
  1321. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1322. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1323. {
  1324. /*
  1325. * Set up an iotag
  1326. */
  1327. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1328. if (pring->ringno == LPFC_ELS_RING) {
  1329. lpfc_debugfs_slow_ring_trc(phba,
  1330. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1331. *(((uint32_t *) &nextiocb->iocb) + 4),
  1332. *(((uint32_t *) &nextiocb->iocb) + 6),
  1333. *(((uint32_t *) &nextiocb->iocb) + 7));
  1334. }
  1335. /*
  1336. * Issue iocb command to adapter
  1337. */
  1338. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1339. wmb();
  1340. pring->stats.iocb_cmd++;
  1341. /*
  1342. * If there is no completion routine to call, we can release the
  1343. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1344. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1345. */
  1346. if (nextiocb->iocb_cmpl)
  1347. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1348. else
  1349. __lpfc_sli_release_iocbq(phba, nextiocb);
  1350. /*
  1351. * Let the HBA know what IOCB slot will be the next one the
  1352. * driver will put a command into.
  1353. */
  1354. pring->cmdidx = pring->next_cmdidx;
  1355. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1356. }
  1357. /**
  1358. * lpfc_sli_update_full_ring - Update the chip attention register
  1359. * @phba: Pointer to HBA context object.
  1360. * @pring: Pointer to driver SLI ring object.
  1361. *
  1362. * The caller is not required to hold any lock for calling this function.
  1363. * This function updates the chip attention bits for the ring to inform firmware
  1364. * that there are pending work to be done for this ring and requests an
  1365. * interrupt when there is space available in the ring. This function is
  1366. * called when the driver is unable to post more iocbs to the ring due
  1367. * to unavailability of space in the ring.
  1368. **/
  1369. static void
  1370. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1371. {
  1372. int ringno = pring->ringno;
  1373. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1374. wmb();
  1375. /*
  1376. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1377. * The HBA will tell us when an IOCB entry is available.
  1378. */
  1379. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1380. readl(phba->CAregaddr); /* flush */
  1381. pring->stats.iocb_cmd_full++;
  1382. }
  1383. /**
  1384. * lpfc_sli_update_ring - Update chip attention register
  1385. * @phba: Pointer to HBA context object.
  1386. * @pring: Pointer to driver SLI ring object.
  1387. *
  1388. * This function updates the chip attention register bit for the
  1389. * given ring to inform HBA that there is more work to be done
  1390. * in this ring. The caller is not required to hold any lock.
  1391. **/
  1392. static void
  1393. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1394. {
  1395. int ringno = pring->ringno;
  1396. /*
  1397. * Tell the HBA that there is work to do in this ring.
  1398. */
  1399. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1400. wmb();
  1401. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1402. readl(phba->CAregaddr); /* flush */
  1403. }
  1404. }
  1405. /**
  1406. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1407. * @phba: Pointer to HBA context object.
  1408. * @pring: Pointer to driver SLI ring object.
  1409. *
  1410. * This function is called with hbalock held to post pending iocbs
  1411. * in the txq to the firmware. This function is called when driver
  1412. * detects space available in the ring.
  1413. **/
  1414. static void
  1415. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1416. {
  1417. IOCB_t *iocb;
  1418. struct lpfc_iocbq *nextiocb;
  1419. /*
  1420. * Check to see if:
  1421. * (a) there is anything on the txq to send
  1422. * (b) link is up
  1423. * (c) link attention events can be processed (fcp ring only)
  1424. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1425. */
  1426. if (pring->txq_cnt &&
  1427. lpfc_is_link_up(phba) &&
  1428. (pring->ringno != phba->sli.fcp_ring ||
  1429. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1430. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1431. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1432. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1433. if (iocb)
  1434. lpfc_sli_update_ring(phba, pring);
  1435. else
  1436. lpfc_sli_update_full_ring(phba, pring);
  1437. }
  1438. return;
  1439. }
  1440. /**
  1441. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1442. * @phba: Pointer to HBA context object.
  1443. * @hbqno: HBQ number.
  1444. *
  1445. * This function is called with hbalock held to get the next
  1446. * available slot for the given HBQ. If there is free slot
  1447. * available for the HBQ it will return pointer to the next available
  1448. * HBQ entry else it will return NULL.
  1449. **/
  1450. static struct lpfc_hbq_entry *
  1451. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1452. {
  1453. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1454. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1455. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1456. hbqp->next_hbqPutIdx = 0;
  1457. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1458. uint32_t raw_index = phba->hbq_get[hbqno];
  1459. uint32_t getidx = le32_to_cpu(raw_index);
  1460. hbqp->local_hbqGetIdx = getidx;
  1461. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1462. lpfc_printf_log(phba, KERN_ERR,
  1463. LOG_SLI | LOG_VPORT,
  1464. "1802 HBQ %d: local_hbqGetIdx "
  1465. "%u is > than hbqp->entry_count %u\n",
  1466. hbqno, hbqp->local_hbqGetIdx,
  1467. hbqp->entry_count);
  1468. phba->link_state = LPFC_HBA_ERROR;
  1469. return NULL;
  1470. }
  1471. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1472. return NULL;
  1473. }
  1474. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1475. hbqp->hbqPutIdx;
  1476. }
  1477. /**
  1478. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1479. * @phba: Pointer to HBA context object.
  1480. *
  1481. * This function is called with no lock held to free all the
  1482. * hbq buffers while uninitializing the SLI interface. It also
  1483. * frees the HBQ buffers returned by the firmware but not yet
  1484. * processed by the upper layers.
  1485. **/
  1486. void
  1487. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1488. {
  1489. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1490. struct hbq_dmabuf *hbq_buf;
  1491. unsigned long flags;
  1492. int i, hbq_count;
  1493. uint32_t hbqno;
  1494. hbq_count = lpfc_sli_hbq_count();
  1495. /* Return all memory used by all HBQs */
  1496. spin_lock_irqsave(&phba->hbalock, flags);
  1497. for (i = 0; i < hbq_count; ++i) {
  1498. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1499. &phba->hbqs[i].hbq_buffer_list, list) {
  1500. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1501. list_del(&hbq_buf->dbuf.list);
  1502. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1503. }
  1504. phba->hbqs[i].buffer_count = 0;
  1505. }
  1506. /* Return all HBQ buffer that are in-fly */
  1507. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1508. list) {
  1509. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1510. list_del(&hbq_buf->dbuf.list);
  1511. if (hbq_buf->tag == -1) {
  1512. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1513. (phba, hbq_buf);
  1514. } else {
  1515. hbqno = hbq_buf->tag >> 16;
  1516. if (hbqno >= LPFC_MAX_HBQS)
  1517. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1518. (phba, hbq_buf);
  1519. else
  1520. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1521. hbq_buf);
  1522. }
  1523. }
  1524. /* Mark the HBQs not in use */
  1525. phba->hbq_in_use = 0;
  1526. spin_unlock_irqrestore(&phba->hbalock, flags);
  1527. }
  1528. /**
  1529. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1530. * @phba: Pointer to HBA context object.
  1531. * @hbqno: HBQ number.
  1532. * @hbq_buf: Pointer to HBQ buffer.
  1533. *
  1534. * This function is called with the hbalock held to post a
  1535. * hbq buffer to the firmware. If the function finds an empty
  1536. * slot in the HBQ, it will post the buffer. The function will return
  1537. * pointer to the hbq entry if it successfully post the buffer
  1538. * else it will return NULL.
  1539. **/
  1540. static int
  1541. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1542. struct hbq_dmabuf *hbq_buf)
  1543. {
  1544. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1545. }
  1546. /**
  1547. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1548. * @phba: Pointer to HBA context object.
  1549. * @hbqno: HBQ number.
  1550. * @hbq_buf: Pointer to HBQ buffer.
  1551. *
  1552. * This function is called with the hbalock held to post a hbq buffer to the
  1553. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1554. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1555. * it successfully post the buffer else it will return an error.
  1556. **/
  1557. static int
  1558. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1559. struct hbq_dmabuf *hbq_buf)
  1560. {
  1561. struct lpfc_hbq_entry *hbqe;
  1562. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1563. /* Get next HBQ entry slot to use */
  1564. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1565. if (hbqe) {
  1566. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1567. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1568. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1569. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1570. hbqe->bde.tus.f.bdeFlags = 0;
  1571. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1572. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1573. /* Sync SLIM */
  1574. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1575. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1576. /* flush */
  1577. readl(phba->hbq_put + hbqno);
  1578. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1579. return 0;
  1580. } else
  1581. return -ENOMEM;
  1582. }
  1583. /**
  1584. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1585. * @phba: Pointer to HBA context object.
  1586. * @hbqno: HBQ number.
  1587. * @hbq_buf: Pointer to HBQ buffer.
  1588. *
  1589. * This function is called with the hbalock held to post an RQE to the SLI4
  1590. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1591. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1592. **/
  1593. static int
  1594. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1595. struct hbq_dmabuf *hbq_buf)
  1596. {
  1597. int rc;
  1598. struct lpfc_rqe hrqe;
  1599. struct lpfc_rqe drqe;
  1600. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1601. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1602. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1603. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1604. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1605. &hrqe, &drqe);
  1606. if (rc < 0)
  1607. return rc;
  1608. hbq_buf->tag = rc;
  1609. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1610. return 0;
  1611. }
  1612. /* HBQ for ELS and CT traffic. */
  1613. static struct lpfc_hbq_init lpfc_els_hbq = {
  1614. .rn = 1,
  1615. .entry_count = 256,
  1616. .mask_count = 0,
  1617. .profile = 0,
  1618. .ring_mask = (1 << LPFC_ELS_RING),
  1619. .buffer_count = 0,
  1620. .init_count = 40,
  1621. .add_count = 40,
  1622. };
  1623. /* HBQ for the extra ring if needed */
  1624. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1625. .rn = 1,
  1626. .entry_count = 200,
  1627. .mask_count = 0,
  1628. .profile = 0,
  1629. .ring_mask = (1 << LPFC_EXTRA_RING),
  1630. .buffer_count = 0,
  1631. .init_count = 0,
  1632. .add_count = 5,
  1633. };
  1634. /* Array of HBQs */
  1635. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1636. &lpfc_els_hbq,
  1637. &lpfc_extra_hbq,
  1638. };
  1639. /**
  1640. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1641. * @phba: Pointer to HBA context object.
  1642. * @hbqno: HBQ number.
  1643. * @count: Number of HBQ buffers to be posted.
  1644. *
  1645. * This function is called with no lock held to post more hbq buffers to the
  1646. * given HBQ. The function returns the number of HBQ buffers successfully
  1647. * posted.
  1648. **/
  1649. static int
  1650. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1651. {
  1652. uint32_t i, posted = 0;
  1653. unsigned long flags;
  1654. struct hbq_dmabuf *hbq_buffer;
  1655. LIST_HEAD(hbq_buf_list);
  1656. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1657. return 0;
  1658. if ((phba->hbqs[hbqno].buffer_count + count) >
  1659. lpfc_hbq_defs[hbqno]->entry_count)
  1660. count = lpfc_hbq_defs[hbqno]->entry_count -
  1661. phba->hbqs[hbqno].buffer_count;
  1662. if (!count)
  1663. return 0;
  1664. /* Allocate HBQ entries */
  1665. for (i = 0; i < count; i++) {
  1666. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1667. if (!hbq_buffer)
  1668. break;
  1669. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1670. }
  1671. /* Check whether HBQ is still in use */
  1672. spin_lock_irqsave(&phba->hbalock, flags);
  1673. if (!phba->hbq_in_use)
  1674. goto err;
  1675. while (!list_empty(&hbq_buf_list)) {
  1676. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1677. dbuf.list);
  1678. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1679. (hbqno << 16));
  1680. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1681. phba->hbqs[hbqno].buffer_count++;
  1682. posted++;
  1683. } else
  1684. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1685. }
  1686. spin_unlock_irqrestore(&phba->hbalock, flags);
  1687. return posted;
  1688. err:
  1689. spin_unlock_irqrestore(&phba->hbalock, flags);
  1690. while (!list_empty(&hbq_buf_list)) {
  1691. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1692. dbuf.list);
  1693. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1694. }
  1695. return 0;
  1696. }
  1697. /**
  1698. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1699. * @phba: Pointer to HBA context object.
  1700. * @qno: HBQ number.
  1701. *
  1702. * This function posts more buffers to the HBQ. This function
  1703. * is called with no lock held. The function returns the number of HBQ entries
  1704. * successfully allocated.
  1705. **/
  1706. int
  1707. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1708. {
  1709. if (phba->sli_rev == LPFC_SLI_REV4)
  1710. return 0;
  1711. else
  1712. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1713. lpfc_hbq_defs[qno]->add_count);
  1714. }
  1715. /**
  1716. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1717. * @phba: Pointer to HBA context object.
  1718. * @qno: HBQ queue number.
  1719. *
  1720. * This function is called from SLI initialization code path with
  1721. * no lock held to post initial HBQ buffers to firmware. The
  1722. * function returns the number of HBQ entries successfully allocated.
  1723. **/
  1724. static int
  1725. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1726. {
  1727. if (phba->sli_rev == LPFC_SLI_REV4)
  1728. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1729. lpfc_hbq_defs[qno]->entry_count);
  1730. else
  1731. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1732. lpfc_hbq_defs[qno]->init_count);
  1733. }
  1734. /**
  1735. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1736. * @phba: Pointer to HBA context object.
  1737. * @hbqno: HBQ number.
  1738. *
  1739. * This function removes the first hbq buffer on an hbq list and returns a
  1740. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1741. **/
  1742. static struct hbq_dmabuf *
  1743. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1744. {
  1745. struct lpfc_dmabuf *d_buf;
  1746. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1747. if (!d_buf)
  1748. return NULL;
  1749. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1750. }
  1751. /**
  1752. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1753. * @phba: Pointer to HBA context object.
  1754. * @tag: Tag of the hbq buffer.
  1755. *
  1756. * This function is called with hbalock held. This function searches
  1757. * for the hbq buffer associated with the given tag in the hbq buffer
  1758. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1759. * it returns NULL.
  1760. **/
  1761. static struct hbq_dmabuf *
  1762. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1763. {
  1764. struct lpfc_dmabuf *d_buf;
  1765. struct hbq_dmabuf *hbq_buf;
  1766. uint32_t hbqno;
  1767. hbqno = tag >> 16;
  1768. if (hbqno >= LPFC_MAX_HBQS)
  1769. return NULL;
  1770. spin_lock_irq(&phba->hbalock);
  1771. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1772. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1773. if (hbq_buf->tag == tag) {
  1774. spin_unlock_irq(&phba->hbalock);
  1775. return hbq_buf;
  1776. }
  1777. }
  1778. spin_unlock_irq(&phba->hbalock);
  1779. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1780. "1803 Bad hbq tag. Data: x%x x%x\n",
  1781. tag, phba->hbqs[tag >> 16].buffer_count);
  1782. return NULL;
  1783. }
  1784. /**
  1785. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1786. * @phba: Pointer to HBA context object.
  1787. * @hbq_buffer: Pointer to HBQ buffer.
  1788. *
  1789. * This function is called with hbalock. This function gives back
  1790. * the hbq buffer to firmware. If the HBQ does not have space to
  1791. * post the buffer, it will free the buffer.
  1792. **/
  1793. void
  1794. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1795. {
  1796. uint32_t hbqno;
  1797. if (hbq_buffer) {
  1798. hbqno = hbq_buffer->tag >> 16;
  1799. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1800. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1801. }
  1802. }
  1803. /**
  1804. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1805. * @mbxCommand: mailbox command code.
  1806. *
  1807. * This function is called by the mailbox event handler function to verify
  1808. * that the completed mailbox command is a legitimate mailbox command. If the
  1809. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1810. * and the mailbox event handler will take the HBA offline.
  1811. **/
  1812. static int
  1813. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1814. {
  1815. uint8_t ret;
  1816. switch (mbxCommand) {
  1817. case MBX_LOAD_SM:
  1818. case MBX_READ_NV:
  1819. case MBX_WRITE_NV:
  1820. case MBX_WRITE_VPARMS:
  1821. case MBX_RUN_BIU_DIAG:
  1822. case MBX_INIT_LINK:
  1823. case MBX_DOWN_LINK:
  1824. case MBX_CONFIG_LINK:
  1825. case MBX_CONFIG_RING:
  1826. case MBX_RESET_RING:
  1827. case MBX_READ_CONFIG:
  1828. case MBX_READ_RCONFIG:
  1829. case MBX_READ_SPARM:
  1830. case MBX_READ_STATUS:
  1831. case MBX_READ_RPI:
  1832. case MBX_READ_XRI:
  1833. case MBX_READ_REV:
  1834. case MBX_READ_LNK_STAT:
  1835. case MBX_REG_LOGIN:
  1836. case MBX_UNREG_LOGIN:
  1837. case MBX_CLEAR_LA:
  1838. case MBX_DUMP_MEMORY:
  1839. case MBX_DUMP_CONTEXT:
  1840. case MBX_RUN_DIAGS:
  1841. case MBX_RESTART:
  1842. case MBX_UPDATE_CFG:
  1843. case MBX_DOWN_LOAD:
  1844. case MBX_DEL_LD_ENTRY:
  1845. case MBX_RUN_PROGRAM:
  1846. case MBX_SET_MASK:
  1847. case MBX_SET_VARIABLE:
  1848. case MBX_UNREG_D_ID:
  1849. case MBX_KILL_BOARD:
  1850. case MBX_CONFIG_FARP:
  1851. case MBX_BEACON:
  1852. case MBX_LOAD_AREA:
  1853. case MBX_RUN_BIU_DIAG64:
  1854. case MBX_CONFIG_PORT:
  1855. case MBX_READ_SPARM64:
  1856. case MBX_READ_RPI64:
  1857. case MBX_REG_LOGIN64:
  1858. case MBX_READ_TOPOLOGY:
  1859. case MBX_WRITE_WWN:
  1860. case MBX_SET_DEBUG:
  1861. case MBX_LOAD_EXP_ROM:
  1862. case MBX_ASYNCEVT_ENABLE:
  1863. case MBX_REG_VPI:
  1864. case MBX_UNREG_VPI:
  1865. case MBX_HEARTBEAT:
  1866. case MBX_PORT_CAPABILITIES:
  1867. case MBX_PORT_IOV_CONTROL:
  1868. case MBX_SLI4_CONFIG:
  1869. case MBX_SLI4_REQ_FTRS:
  1870. case MBX_REG_FCFI:
  1871. case MBX_UNREG_FCFI:
  1872. case MBX_REG_VFI:
  1873. case MBX_UNREG_VFI:
  1874. case MBX_INIT_VPI:
  1875. case MBX_INIT_VFI:
  1876. case MBX_RESUME_RPI:
  1877. case MBX_READ_EVENT_LOG_STATUS:
  1878. case MBX_READ_EVENT_LOG:
  1879. case MBX_SECURITY_MGMT:
  1880. case MBX_AUTH_PORT:
  1881. ret = mbxCommand;
  1882. break;
  1883. default:
  1884. ret = MBX_SHUTDOWN;
  1885. break;
  1886. }
  1887. return ret;
  1888. }
  1889. /**
  1890. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1891. * @phba: Pointer to HBA context object.
  1892. * @pmboxq: Pointer to mailbox command.
  1893. *
  1894. * This is completion handler function for mailbox commands issued from
  1895. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1896. * mailbox event handler function with no lock held. This function
  1897. * will wake up thread waiting on the wait queue pointed by context1
  1898. * of the mailbox.
  1899. **/
  1900. void
  1901. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1902. {
  1903. wait_queue_head_t *pdone_q;
  1904. unsigned long drvr_flag;
  1905. /*
  1906. * If pdone_q is empty, the driver thread gave up waiting and
  1907. * continued running.
  1908. */
  1909. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1910. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1911. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1912. if (pdone_q)
  1913. wake_up_interruptible(pdone_q);
  1914. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1915. return;
  1916. }
  1917. /**
  1918. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1919. * @phba: Pointer to HBA context object.
  1920. * @pmb: Pointer to mailbox object.
  1921. *
  1922. * This function is the default mailbox completion handler. It
  1923. * frees the memory resources associated with the completed mailbox
  1924. * command. If the completed command is a REG_LOGIN mailbox command,
  1925. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1926. **/
  1927. void
  1928. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1929. {
  1930. struct lpfc_vport *vport = pmb->vport;
  1931. struct lpfc_dmabuf *mp;
  1932. struct lpfc_nodelist *ndlp;
  1933. struct Scsi_Host *shost;
  1934. uint16_t rpi, vpi;
  1935. int rc;
  1936. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1937. if (mp) {
  1938. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1939. kfree(mp);
  1940. }
  1941. /*
  1942. * If a REG_LOGIN succeeded after node is destroyed or node
  1943. * is in re-discovery driver need to cleanup the RPI.
  1944. */
  1945. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1946. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1947. !pmb->u.mb.mbxStatus) {
  1948. rpi = pmb->u.mb.un.varWords[0];
  1949. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1950. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1951. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1952. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1953. if (rc != MBX_NOT_FINISHED)
  1954. return;
  1955. }
  1956. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1957. !(phba->pport->load_flag & FC_UNLOADING) &&
  1958. !pmb->u.mb.mbxStatus) {
  1959. shost = lpfc_shost_from_vport(vport);
  1960. spin_lock_irq(shost->host_lock);
  1961. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1962. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1963. spin_unlock_irq(shost->host_lock);
  1964. }
  1965. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1966. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1967. lpfc_nlp_put(ndlp);
  1968. pmb->context2 = NULL;
  1969. }
  1970. /* Check security permission status on INIT_LINK mailbox command */
  1971. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1972. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1973. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1974. "2860 SLI authentication is required "
  1975. "for INIT_LINK but has not done yet\n");
  1976. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1977. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1978. else
  1979. mempool_free(pmb, phba->mbox_mem_pool);
  1980. }
  1981. /**
  1982. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1983. * @phba: Pointer to HBA context object.
  1984. *
  1985. * This function is called with no lock held. This function processes all
  1986. * the completed mailbox commands and gives it to upper layers. The interrupt
  1987. * service routine processes mailbox completion interrupt and adds completed
  1988. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1989. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1990. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1991. * function returns the mailbox commands to the upper layer by calling the
  1992. * completion handler function of each mailbox.
  1993. **/
  1994. int
  1995. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1996. {
  1997. MAILBOX_t *pmbox;
  1998. LPFC_MBOXQ_t *pmb;
  1999. int rc;
  2000. LIST_HEAD(cmplq);
  2001. phba->sli.slistat.mbox_event++;
  2002. /* Get all completed mailboxe buffers into the cmplq */
  2003. spin_lock_irq(&phba->hbalock);
  2004. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2005. spin_unlock_irq(&phba->hbalock);
  2006. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2007. do {
  2008. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2009. if (pmb == NULL)
  2010. break;
  2011. pmbox = &pmb->u.mb;
  2012. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2013. if (pmb->vport) {
  2014. lpfc_debugfs_disc_trc(pmb->vport,
  2015. LPFC_DISC_TRC_MBOX_VPORT,
  2016. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2017. (uint32_t)pmbox->mbxCommand,
  2018. pmbox->un.varWords[0],
  2019. pmbox->un.varWords[1]);
  2020. }
  2021. else {
  2022. lpfc_debugfs_disc_trc(phba->pport,
  2023. LPFC_DISC_TRC_MBOX,
  2024. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2025. (uint32_t)pmbox->mbxCommand,
  2026. pmbox->un.varWords[0],
  2027. pmbox->un.varWords[1]);
  2028. }
  2029. }
  2030. /*
  2031. * It is a fatal error if unknown mbox command completion.
  2032. */
  2033. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2034. MBX_SHUTDOWN) {
  2035. /* Unknown mailbox command compl */
  2036. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2037. "(%d):0323 Unknown Mailbox command "
  2038. "x%x (x%x/x%x) Cmpl\n",
  2039. pmb->vport ? pmb->vport->vpi : 0,
  2040. pmbox->mbxCommand,
  2041. lpfc_sli_config_mbox_subsys_get(phba,
  2042. pmb),
  2043. lpfc_sli_config_mbox_opcode_get(phba,
  2044. pmb));
  2045. phba->link_state = LPFC_HBA_ERROR;
  2046. phba->work_hs = HS_FFER3;
  2047. lpfc_handle_eratt(phba);
  2048. continue;
  2049. }
  2050. if (pmbox->mbxStatus) {
  2051. phba->sli.slistat.mbox_stat_err++;
  2052. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2053. /* Mbox cmd cmpl error - RETRYing */
  2054. lpfc_printf_log(phba, KERN_INFO,
  2055. LOG_MBOX | LOG_SLI,
  2056. "(%d):0305 Mbox cmd cmpl "
  2057. "error - RETRYing Data: x%x "
  2058. "(x%x/x%x) x%x x%x x%x\n",
  2059. pmb->vport ? pmb->vport->vpi : 0,
  2060. pmbox->mbxCommand,
  2061. lpfc_sli_config_mbox_subsys_get(phba,
  2062. pmb),
  2063. lpfc_sli_config_mbox_opcode_get(phba,
  2064. pmb),
  2065. pmbox->mbxStatus,
  2066. pmbox->un.varWords[0],
  2067. pmb->vport->port_state);
  2068. pmbox->mbxStatus = 0;
  2069. pmbox->mbxOwner = OWN_HOST;
  2070. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2071. if (rc != MBX_NOT_FINISHED)
  2072. continue;
  2073. }
  2074. }
  2075. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2076. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2077. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2078. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2079. pmb->vport ? pmb->vport->vpi : 0,
  2080. pmbox->mbxCommand,
  2081. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2082. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2083. pmb->mbox_cmpl,
  2084. *((uint32_t *) pmbox),
  2085. pmbox->un.varWords[0],
  2086. pmbox->un.varWords[1],
  2087. pmbox->un.varWords[2],
  2088. pmbox->un.varWords[3],
  2089. pmbox->un.varWords[4],
  2090. pmbox->un.varWords[5],
  2091. pmbox->un.varWords[6],
  2092. pmbox->un.varWords[7]);
  2093. if (pmb->mbox_cmpl)
  2094. pmb->mbox_cmpl(phba,pmb);
  2095. } while (1);
  2096. return 0;
  2097. }
  2098. /**
  2099. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2100. * @phba: Pointer to HBA context object.
  2101. * @pring: Pointer to driver SLI ring object.
  2102. * @tag: buffer tag.
  2103. *
  2104. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2105. * is set in the tag the buffer is posted for a particular exchange,
  2106. * the function will return the buffer without replacing the buffer.
  2107. * If the buffer is for unsolicited ELS or CT traffic, this function
  2108. * returns the buffer and also posts another buffer to the firmware.
  2109. **/
  2110. static struct lpfc_dmabuf *
  2111. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2112. struct lpfc_sli_ring *pring,
  2113. uint32_t tag)
  2114. {
  2115. struct hbq_dmabuf *hbq_entry;
  2116. if (tag & QUE_BUFTAG_BIT)
  2117. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2118. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2119. if (!hbq_entry)
  2120. return NULL;
  2121. return &hbq_entry->dbuf;
  2122. }
  2123. /**
  2124. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2125. * @phba: Pointer to HBA context object.
  2126. * @pring: Pointer to driver SLI ring object.
  2127. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2128. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2129. * @fch_type: the type for the first frame of the sequence.
  2130. *
  2131. * This function is called with no lock held. This function uses the r_ctl and
  2132. * type of the received sequence to find the correct callback function to call
  2133. * to process the sequence.
  2134. **/
  2135. static int
  2136. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2137. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2138. uint32_t fch_type)
  2139. {
  2140. int i;
  2141. /* unSolicited Responses */
  2142. if (pring->prt[0].profile) {
  2143. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2144. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2145. saveq);
  2146. return 1;
  2147. }
  2148. /* We must search, based on rctl / type
  2149. for the right routine */
  2150. for (i = 0; i < pring->num_mask; i++) {
  2151. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2152. (pring->prt[i].type == fch_type)) {
  2153. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2154. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2155. (phba, pring, saveq);
  2156. return 1;
  2157. }
  2158. }
  2159. return 0;
  2160. }
  2161. /**
  2162. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2163. * @phba: Pointer to HBA context object.
  2164. * @pring: Pointer to driver SLI ring object.
  2165. * @saveq: Pointer to the unsolicited iocb.
  2166. *
  2167. * This function is called with no lock held by the ring event handler
  2168. * when there is an unsolicited iocb posted to the response ring by the
  2169. * firmware. This function gets the buffer associated with the iocbs
  2170. * and calls the event handler for the ring. This function handles both
  2171. * qring buffers and hbq buffers.
  2172. * When the function returns 1 the caller can free the iocb object otherwise
  2173. * upper layer functions will free the iocb objects.
  2174. **/
  2175. static int
  2176. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2177. struct lpfc_iocbq *saveq)
  2178. {
  2179. IOCB_t * irsp;
  2180. WORD5 * w5p;
  2181. uint32_t Rctl, Type;
  2182. uint32_t match;
  2183. struct lpfc_iocbq *iocbq;
  2184. struct lpfc_dmabuf *dmzbuf;
  2185. match = 0;
  2186. irsp = &(saveq->iocb);
  2187. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2188. if (pring->lpfc_sli_rcv_async_status)
  2189. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2190. else
  2191. lpfc_printf_log(phba,
  2192. KERN_WARNING,
  2193. LOG_SLI,
  2194. "0316 Ring %d handler: unexpected "
  2195. "ASYNC_STATUS iocb received evt_code "
  2196. "0x%x\n",
  2197. pring->ringno,
  2198. irsp->un.asyncstat.evt_code);
  2199. return 1;
  2200. }
  2201. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2202. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2203. if (irsp->ulpBdeCount > 0) {
  2204. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2205. irsp->un.ulpWord[3]);
  2206. lpfc_in_buf_free(phba, dmzbuf);
  2207. }
  2208. if (irsp->ulpBdeCount > 1) {
  2209. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2210. irsp->unsli3.sli3Words[3]);
  2211. lpfc_in_buf_free(phba, dmzbuf);
  2212. }
  2213. if (irsp->ulpBdeCount > 2) {
  2214. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2215. irsp->unsli3.sli3Words[7]);
  2216. lpfc_in_buf_free(phba, dmzbuf);
  2217. }
  2218. return 1;
  2219. }
  2220. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2221. if (irsp->ulpBdeCount != 0) {
  2222. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2223. irsp->un.ulpWord[3]);
  2224. if (!saveq->context2)
  2225. lpfc_printf_log(phba,
  2226. KERN_ERR,
  2227. LOG_SLI,
  2228. "0341 Ring %d Cannot find buffer for "
  2229. "an unsolicited iocb. tag 0x%x\n",
  2230. pring->ringno,
  2231. irsp->un.ulpWord[3]);
  2232. }
  2233. if (irsp->ulpBdeCount == 2) {
  2234. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2235. irsp->unsli3.sli3Words[7]);
  2236. if (!saveq->context3)
  2237. lpfc_printf_log(phba,
  2238. KERN_ERR,
  2239. LOG_SLI,
  2240. "0342 Ring %d Cannot find buffer for an"
  2241. " unsolicited iocb. tag 0x%x\n",
  2242. pring->ringno,
  2243. irsp->unsli3.sli3Words[7]);
  2244. }
  2245. list_for_each_entry(iocbq, &saveq->list, list) {
  2246. irsp = &(iocbq->iocb);
  2247. if (irsp->ulpBdeCount != 0) {
  2248. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2249. irsp->un.ulpWord[3]);
  2250. if (!iocbq->context2)
  2251. lpfc_printf_log(phba,
  2252. KERN_ERR,
  2253. LOG_SLI,
  2254. "0343 Ring %d Cannot find "
  2255. "buffer for an unsolicited iocb"
  2256. ". tag 0x%x\n", pring->ringno,
  2257. irsp->un.ulpWord[3]);
  2258. }
  2259. if (irsp->ulpBdeCount == 2) {
  2260. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2261. irsp->unsli3.sli3Words[7]);
  2262. if (!iocbq->context3)
  2263. lpfc_printf_log(phba,
  2264. KERN_ERR,
  2265. LOG_SLI,
  2266. "0344 Ring %d Cannot find "
  2267. "buffer for an unsolicited "
  2268. "iocb. tag 0x%x\n",
  2269. pring->ringno,
  2270. irsp->unsli3.sli3Words[7]);
  2271. }
  2272. }
  2273. }
  2274. if (irsp->ulpBdeCount != 0 &&
  2275. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2276. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2277. int found = 0;
  2278. /* search continue save q for same XRI */
  2279. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2280. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2281. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2282. list_add_tail(&saveq->list, &iocbq->list);
  2283. found = 1;
  2284. break;
  2285. }
  2286. }
  2287. if (!found)
  2288. list_add_tail(&saveq->clist,
  2289. &pring->iocb_continue_saveq);
  2290. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2291. list_del_init(&iocbq->clist);
  2292. saveq = iocbq;
  2293. irsp = &(saveq->iocb);
  2294. } else
  2295. return 0;
  2296. }
  2297. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2298. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2299. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2300. Rctl = FC_RCTL_ELS_REQ;
  2301. Type = FC_TYPE_ELS;
  2302. } else {
  2303. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2304. Rctl = w5p->hcsw.Rctl;
  2305. Type = w5p->hcsw.Type;
  2306. /* Firmware Workaround */
  2307. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2308. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2309. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2310. Rctl = FC_RCTL_ELS_REQ;
  2311. Type = FC_TYPE_ELS;
  2312. w5p->hcsw.Rctl = Rctl;
  2313. w5p->hcsw.Type = Type;
  2314. }
  2315. }
  2316. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2317. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2318. "0313 Ring %d handler: unexpected Rctl x%x "
  2319. "Type x%x received\n",
  2320. pring->ringno, Rctl, Type);
  2321. return 1;
  2322. }
  2323. /**
  2324. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2325. * @phba: Pointer to HBA context object.
  2326. * @pring: Pointer to driver SLI ring object.
  2327. * @prspiocb: Pointer to response iocb object.
  2328. *
  2329. * This function looks up the iocb_lookup table to get the command iocb
  2330. * corresponding to the given response iocb using the iotag of the
  2331. * response iocb. This function is called with the hbalock held.
  2332. * This function returns the command iocb object if it finds the command
  2333. * iocb else returns NULL.
  2334. **/
  2335. static struct lpfc_iocbq *
  2336. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2337. struct lpfc_sli_ring *pring,
  2338. struct lpfc_iocbq *prspiocb)
  2339. {
  2340. struct lpfc_iocbq *cmd_iocb = NULL;
  2341. uint16_t iotag;
  2342. iotag = prspiocb->iocb.ulpIoTag;
  2343. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2344. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2345. list_del_init(&cmd_iocb->list);
  2346. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2347. pring->txcmplq_cnt--;
  2348. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2349. }
  2350. return cmd_iocb;
  2351. }
  2352. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2353. "0317 iotag x%x is out off "
  2354. "range: max iotag x%x wd0 x%x\n",
  2355. iotag, phba->sli.last_iotag,
  2356. *(((uint32_t *) &prspiocb->iocb) + 7));
  2357. return NULL;
  2358. }
  2359. /**
  2360. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2361. * @phba: Pointer to HBA context object.
  2362. * @pring: Pointer to driver SLI ring object.
  2363. * @iotag: IOCB tag.
  2364. *
  2365. * This function looks up the iocb_lookup table to get the command iocb
  2366. * corresponding to the given iotag. This function is called with the
  2367. * hbalock held.
  2368. * This function returns the command iocb object if it finds the command
  2369. * iocb else returns NULL.
  2370. **/
  2371. static struct lpfc_iocbq *
  2372. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2373. struct lpfc_sli_ring *pring, uint16_t iotag)
  2374. {
  2375. struct lpfc_iocbq *cmd_iocb;
  2376. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2377. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2378. list_del_init(&cmd_iocb->list);
  2379. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2380. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2381. pring->txcmplq_cnt--;
  2382. }
  2383. return cmd_iocb;
  2384. }
  2385. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2386. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2387. iotag, phba->sli.last_iotag);
  2388. return NULL;
  2389. }
  2390. /**
  2391. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2392. * @phba: Pointer to HBA context object.
  2393. * @pring: Pointer to driver SLI ring object.
  2394. * @saveq: Pointer to the response iocb to be processed.
  2395. *
  2396. * This function is called by the ring event handler for non-fcp
  2397. * rings when there is a new response iocb in the response ring.
  2398. * The caller is not required to hold any locks. This function
  2399. * gets the command iocb associated with the response iocb and
  2400. * calls the completion handler for the command iocb. If there
  2401. * is no completion handler, the function will free the resources
  2402. * associated with command iocb. If the response iocb is for
  2403. * an already aborted command iocb, the status of the completion
  2404. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2405. * This function always returns 1.
  2406. **/
  2407. static int
  2408. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2409. struct lpfc_iocbq *saveq)
  2410. {
  2411. struct lpfc_iocbq *cmdiocbp;
  2412. int rc = 1;
  2413. unsigned long iflag;
  2414. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2415. spin_lock_irqsave(&phba->hbalock, iflag);
  2416. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2417. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2418. if (cmdiocbp) {
  2419. if (cmdiocbp->iocb_cmpl) {
  2420. /*
  2421. * If an ELS command failed send an event to mgmt
  2422. * application.
  2423. */
  2424. if (saveq->iocb.ulpStatus &&
  2425. (pring->ringno == LPFC_ELS_RING) &&
  2426. (cmdiocbp->iocb.ulpCommand ==
  2427. CMD_ELS_REQUEST64_CR))
  2428. lpfc_send_els_failure_event(phba,
  2429. cmdiocbp, saveq);
  2430. /*
  2431. * Post all ELS completions to the worker thread.
  2432. * All other are passed to the completion callback.
  2433. */
  2434. if (pring->ringno == LPFC_ELS_RING) {
  2435. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2436. (cmdiocbp->iocb_flag &
  2437. LPFC_DRIVER_ABORTED)) {
  2438. spin_lock_irqsave(&phba->hbalock,
  2439. iflag);
  2440. cmdiocbp->iocb_flag &=
  2441. ~LPFC_DRIVER_ABORTED;
  2442. spin_unlock_irqrestore(&phba->hbalock,
  2443. iflag);
  2444. saveq->iocb.ulpStatus =
  2445. IOSTAT_LOCAL_REJECT;
  2446. saveq->iocb.un.ulpWord[4] =
  2447. IOERR_SLI_ABORTED;
  2448. /* Firmware could still be in progress
  2449. * of DMAing payload, so don't free data
  2450. * buffer till after a hbeat.
  2451. */
  2452. spin_lock_irqsave(&phba->hbalock,
  2453. iflag);
  2454. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2455. spin_unlock_irqrestore(&phba->hbalock,
  2456. iflag);
  2457. }
  2458. if (phba->sli_rev == LPFC_SLI_REV4) {
  2459. if (saveq->iocb_flag &
  2460. LPFC_EXCHANGE_BUSY) {
  2461. /* Set cmdiocb flag for the
  2462. * exchange busy so sgl (xri)
  2463. * will not be released until
  2464. * the abort xri is received
  2465. * from hba.
  2466. */
  2467. spin_lock_irqsave(
  2468. &phba->hbalock, iflag);
  2469. cmdiocbp->iocb_flag |=
  2470. LPFC_EXCHANGE_BUSY;
  2471. spin_unlock_irqrestore(
  2472. &phba->hbalock, iflag);
  2473. }
  2474. if (cmdiocbp->iocb_flag &
  2475. LPFC_DRIVER_ABORTED) {
  2476. /*
  2477. * Clear LPFC_DRIVER_ABORTED
  2478. * bit in case it was driver
  2479. * initiated abort.
  2480. */
  2481. spin_lock_irqsave(
  2482. &phba->hbalock, iflag);
  2483. cmdiocbp->iocb_flag &=
  2484. ~LPFC_DRIVER_ABORTED;
  2485. spin_unlock_irqrestore(
  2486. &phba->hbalock, iflag);
  2487. cmdiocbp->iocb.ulpStatus =
  2488. IOSTAT_LOCAL_REJECT;
  2489. cmdiocbp->iocb.un.ulpWord[4] =
  2490. IOERR_ABORT_REQUESTED;
  2491. /*
  2492. * For SLI4, irsiocb contains
  2493. * NO_XRI in sli_xritag, it
  2494. * shall not affect releasing
  2495. * sgl (xri) process.
  2496. */
  2497. saveq->iocb.ulpStatus =
  2498. IOSTAT_LOCAL_REJECT;
  2499. saveq->iocb.un.ulpWord[4] =
  2500. IOERR_SLI_ABORTED;
  2501. spin_lock_irqsave(
  2502. &phba->hbalock, iflag);
  2503. saveq->iocb_flag |=
  2504. LPFC_DELAY_MEM_FREE;
  2505. spin_unlock_irqrestore(
  2506. &phba->hbalock, iflag);
  2507. }
  2508. }
  2509. }
  2510. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2511. } else
  2512. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2513. } else {
  2514. /*
  2515. * Unknown initiating command based on the response iotag.
  2516. * This could be the case on the ELS ring because of
  2517. * lpfc_els_abort().
  2518. */
  2519. if (pring->ringno != LPFC_ELS_RING) {
  2520. /*
  2521. * Ring <ringno> handler: unexpected completion IoTag
  2522. * <IoTag>
  2523. */
  2524. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2525. "0322 Ring %d handler: "
  2526. "unexpected completion IoTag x%x "
  2527. "Data: x%x x%x x%x x%x\n",
  2528. pring->ringno,
  2529. saveq->iocb.ulpIoTag,
  2530. saveq->iocb.ulpStatus,
  2531. saveq->iocb.un.ulpWord[4],
  2532. saveq->iocb.ulpCommand,
  2533. saveq->iocb.ulpContext);
  2534. }
  2535. }
  2536. return rc;
  2537. }
  2538. /**
  2539. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2540. * @phba: Pointer to HBA context object.
  2541. * @pring: Pointer to driver SLI ring object.
  2542. *
  2543. * This function is called from the iocb ring event handlers when
  2544. * put pointer is ahead of the get pointer for a ring. This function signal
  2545. * an error attention condition to the worker thread and the worker
  2546. * thread will transition the HBA to offline state.
  2547. **/
  2548. static void
  2549. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2550. {
  2551. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2552. /*
  2553. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2554. * rsp ring <portRspMax>
  2555. */
  2556. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2557. "0312 Ring %d handler: portRspPut %d "
  2558. "is bigger than rsp ring %d\n",
  2559. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2560. pring->numRiocb);
  2561. phba->link_state = LPFC_HBA_ERROR;
  2562. /*
  2563. * All error attention handlers are posted to
  2564. * worker thread
  2565. */
  2566. phba->work_ha |= HA_ERATT;
  2567. phba->work_hs = HS_FFER3;
  2568. lpfc_worker_wake_up(phba);
  2569. return;
  2570. }
  2571. /**
  2572. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2573. * @ptr: Pointer to address of HBA context object.
  2574. *
  2575. * This function is invoked by the Error Attention polling timer when the
  2576. * timer times out. It will check the SLI Error Attention register for
  2577. * possible attention events. If so, it will post an Error Attention event
  2578. * and wake up worker thread to process it. Otherwise, it will set up the
  2579. * Error Attention polling timer for the next poll.
  2580. **/
  2581. void lpfc_poll_eratt(unsigned long ptr)
  2582. {
  2583. struct lpfc_hba *phba;
  2584. uint32_t eratt = 0;
  2585. phba = (struct lpfc_hba *)ptr;
  2586. /* Check chip HA register for error event */
  2587. eratt = lpfc_sli_check_eratt(phba);
  2588. if (eratt)
  2589. /* Tell the worker thread there is work to do */
  2590. lpfc_worker_wake_up(phba);
  2591. else
  2592. /* Restart the timer for next eratt poll */
  2593. mod_timer(&phba->eratt_poll, jiffies +
  2594. HZ * LPFC_ERATT_POLL_INTERVAL);
  2595. return;
  2596. }
  2597. /**
  2598. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2599. * @phba: Pointer to HBA context object.
  2600. * @pring: Pointer to driver SLI ring object.
  2601. * @mask: Host attention register mask for this ring.
  2602. *
  2603. * This function is called from the interrupt context when there is a ring
  2604. * event for the fcp ring. The caller does not hold any lock.
  2605. * The function processes each response iocb in the response ring until it
  2606. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2607. * LE bit set. The function will call the completion handler of the command iocb
  2608. * if the response iocb indicates a completion for a command iocb or it is
  2609. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2610. * function if this is an unsolicited iocb.
  2611. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2612. * to check it explicitly.
  2613. */
  2614. int
  2615. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2616. struct lpfc_sli_ring *pring, uint32_t mask)
  2617. {
  2618. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2619. IOCB_t *irsp = NULL;
  2620. IOCB_t *entry = NULL;
  2621. struct lpfc_iocbq *cmdiocbq = NULL;
  2622. struct lpfc_iocbq rspiocbq;
  2623. uint32_t status;
  2624. uint32_t portRspPut, portRspMax;
  2625. int rc = 1;
  2626. lpfc_iocb_type type;
  2627. unsigned long iflag;
  2628. uint32_t rsp_cmpl = 0;
  2629. spin_lock_irqsave(&phba->hbalock, iflag);
  2630. pring->stats.iocb_event++;
  2631. /*
  2632. * The next available response entry should never exceed the maximum
  2633. * entries. If it does, treat it as an adapter hardware error.
  2634. */
  2635. portRspMax = pring->numRiocb;
  2636. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2637. if (unlikely(portRspPut >= portRspMax)) {
  2638. lpfc_sli_rsp_pointers_error(phba, pring);
  2639. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2640. return 1;
  2641. }
  2642. if (phba->fcp_ring_in_use) {
  2643. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2644. return 1;
  2645. } else
  2646. phba->fcp_ring_in_use = 1;
  2647. rmb();
  2648. while (pring->rspidx != portRspPut) {
  2649. /*
  2650. * Fetch an entry off the ring and copy it into a local data
  2651. * structure. The copy involves a byte-swap since the
  2652. * network byte order and pci byte orders are different.
  2653. */
  2654. entry = lpfc_resp_iocb(phba, pring);
  2655. phba->last_completion_time = jiffies;
  2656. if (++pring->rspidx >= portRspMax)
  2657. pring->rspidx = 0;
  2658. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2659. (uint32_t *) &rspiocbq.iocb,
  2660. phba->iocb_rsp_size);
  2661. INIT_LIST_HEAD(&(rspiocbq.list));
  2662. irsp = &rspiocbq.iocb;
  2663. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2664. pring->stats.iocb_rsp++;
  2665. rsp_cmpl++;
  2666. if (unlikely(irsp->ulpStatus)) {
  2667. /*
  2668. * If resource errors reported from HBA, reduce
  2669. * queuedepths of the SCSI device.
  2670. */
  2671. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2672. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2673. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2674. phba->lpfc_rampdown_queue_depth(phba);
  2675. spin_lock_irqsave(&phba->hbalock, iflag);
  2676. }
  2677. /* Rsp ring <ringno> error: IOCB */
  2678. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2679. "0336 Rsp Ring %d error: IOCB Data: "
  2680. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2681. pring->ringno,
  2682. irsp->un.ulpWord[0],
  2683. irsp->un.ulpWord[1],
  2684. irsp->un.ulpWord[2],
  2685. irsp->un.ulpWord[3],
  2686. irsp->un.ulpWord[4],
  2687. irsp->un.ulpWord[5],
  2688. *(uint32_t *)&irsp->un1,
  2689. *((uint32_t *)&irsp->un1 + 1));
  2690. }
  2691. switch (type) {
  2692. case LPFC_ABORT_IOCB:
  2693. case LPFC_SOL_IOCB:
  2694. /*
  2695. * Idle exchange closed via ABTS from port. No iocb
  2696. * resources need to be recovered.
  2697. */
  2698. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2699. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2700. "0333 IOCB cmd 0x%x"
  2701. " processed. Skipping"
  2702. " completion\n",
  2703. irsp->ulpCommand);
  2704. break;
  2705. }
  2706. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2707. &rspiocbq);
  2708. if (unlikely(!cmdiocbq))
  2709. break;
  2710. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2711. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2712. if (cmdiocbq->iocb_cmpl) {
  2713. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2714. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2715. &rspiocbq);
  2716. spin_lock_irqsave(&phba->hbalock, iflag);
  2717. }
  2718. break;
  2719. case LPFC_UNSOL_IOCB:
  2720. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2721. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2722. spin_lock_irqsave(&phba->hbalock, iflag);
  2723. break;
  2724. default:
  2725. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2726. char adaptermsg[LPFC_MAX_ADPTMSG];
  2727. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2728. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2729. MAX_MSG_DATA);
  2730. dev_warn(&((phba->pcidev)->dev),
  2731. "lpfc%d: %s\n",
  2732. phba->brd_no, adaptermsg);
  2733. } else {
  2734. /* Unknown IOCB command */
  2735. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2736. "0334 Unknown IOCB command "
  2737. "Data: x%x, x%x x%x x%x x%x\n",
  2738. type, irsp->ulpCommand,
  2739. irsp->ulpStatus,
  2740. irsp->ulpIoTag,
  2741. irsp->ulpContext);
  2742. }
  2743. break;
  2744. }
  2745. /*
  2746. * The response IOCB has been processed. Update the ring
  2747. * pointer in SLIM. If the port response put pointer has not
  2748. * been updated, sync the pgp->rspPutInx and fetch the new port
  2749. * response put pointer.
  2750. */
  2751. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2752. if (pring->rspidx == portRspPut)
  2753. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2754. }
  2755. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2756. pring->stats.iocb_rsp_full++;
  2757. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2758. writel(status, phba->CAregaddr);
  2759. readl(phba->CAregaddr);
  2760. }
  2761. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2762. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2763. pring->stats.iocb_cmd_empty++;
  2764. /* Force update of the local copy of cmdGetInx */
  2765. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2766. lpfc_sli_resume_iocb(phba, pring);
  2767. if ((pring->lpfc_sli_cmd_available))
  2768. (pring->lpfc_sli_cmd_available) (phba, pring);
  2769. }
  2770. phba->fcp_ring_in_use = 0;
  2771. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2772. return rc;
  2773. }
  2774. /**
  2775. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2776. * @phba: Pointer to HBA context object.
  2777. * @pring: Pointer to driver SLI ring object.
  2778. * @rspiocbp: Pointer to driver response IOCB object.
  2779. *
  2780. * This function is called from the worker thread when there is a slow-path
  2781. * response IOCB to process. This function chains all the response iocbs until
  2782. * seeing the iocb with the LE bit set. The function will call
  2783. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2784. * completion of a command iocb. The function will call the
  2785. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2786. * The function frees the resources or calls the completion handler if this
  2787. * iocb is an abort completion. The function returns NULL when the response
  2788. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2789. * this function shall chain the iocb on to the iocb_continueq and return the
  2790. * response iocb passed in.
  2791. **/
  2792. static struct lpfc_iocbq *
  2793. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2794. struct lpfc_iocbq *rspiocbp)
  2795. {
  2796. struct lpfc_iocbq *saveq;
  2797. struct lpfc_iocbq *cmdiocbp;
  2798. struct lpfc_iocbq *next_iocb;
  2799. IOCB_t *irsp = NULL;
  2800. uint32_t free_saveq;
  2801. uint8_t iocb_cmd_type;
  2802. lpfc_iocb_type type;
  2803. unsigned long iflag;
  2804. int rc;
  2805. spin_lock_irqsave(&phba->hbalock, iflag);
  2806. /* First add the response iocb to the countinueq list */
  2807. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2808. pring->iocb_continueq_cnt++;
  2809. /* Now, determine whether the list is completed for processing */
  2810. irsp = &rspiocbp->iocb;
  2811. if (irsp->ulpLe) {
  2812. /*
  2813. * By default, the driver expects to free all resources
  2814. * associated with this iocb completion.
  2815. */
  2816. free_saveq = 1;
  2817. saveq = list_get_first(&pring->iocb_continueq,
  2818. struct lpfc_iocbq, list);
  2819. irsp = &(saveq->iocb);
  2820. list_del_init(&pring->iocb_continueq);
  2821. pring->iocb_continueq_cnt = 0;
  2822. pring->stats.iocb_rsp++;
  2823. /*
  2824. * If resource errors reported from HBA, reduce
  2825. * queuedepths of the SCSI device.
  2826. */
  2827. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2828. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2829. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2830. phba->lpfc_rampdown_queue_depth(phba);
  2831. spin_lock_irqsave(&phba->hbalock, iflag);
  2832. }
  2833. if (irsp->ulpStatus) {
  2834. /* Rsp ring <ringno> error: IOCB */
  2835. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2836. "0328 Rsp Ring %d error: "
  2837. "IOCB Data: "
  2838. "x%x x%x x%x x%x "
  2839. "x%x x%x x%x x%x "
  2840. "x%x x%x x%x x%x "
  2841. "x%x x%x x%x x%x\n",
  2842. pring->ringno,
  2843. irsp->un.ulpWord[0],
  2844. irsp->un.ulpWord[1],
  2845. irsp->un.ulpWord[2],
  2846. irsp->un.ulpWord[3],
  2847. irsp->un.ulpWord[4],
  2848. irsp->un.ulpWord[5],
  2849. *(((uint32_t *) irsp) + 6),
  2850. *(((uint32_t *) irsp) + 7),
  2851. *(((uint32_t *) irsp) + 8),
  2852. *(((uint32_t *) irsp) + 9),
  2853. *(((uint32_t *) irsp) + 10),
  2854. *(((uint32_t *) irsp) + 11),
  2855. *(((uint32_t *) irsp) + 12),
  2856. *(((uint32_t *) irsp) + 13),
  2857. *(((uint32_t *) irsp) + 14),
  2858. *(((uint32_t *) irsp) + 15));
  2859. }
  2860. /*
  2861. * Fetch the IOCB command type and call the correct completion
  2862. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2863. * get freed back to the lpfc_iocb_list by the discovery
  2864. * kernel thread.
  2865. */
  2866. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2867. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2868. switch (type) {
  2869. case LPFC_SOL_IOCB:
  2870. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2871. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2872. spin_lock_irqsave(&phba->hbalock, iflag);
  2873. break;
  2874. case LPFC_UNSOL_IOCB:
  2875. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2876. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2877. spin_lock_irqsave(&phba->hbalock, iflag);
  2878. if (!rc)
  2879. free_saveq = 0;
  2880. break;
  2881. case LPFC_ABORT_IOCB:
  2882. cmdiocbp = NULL;
  2883. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2884. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2885. saveq);
  2886. if (cmdiocbp) {
  2887. /* Call the specified completion routine */
  2888. if (cmdiocbp->iocb_cmpl) {
  2889. spin_unlock_irqrestore(&phba->hbalock,
  2890. iflag);
  2891. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2892. saveq);
  2893. spin_lock_irqsave(&phba->hbalock,
  2894. iflag);
  2895. } else
  2896. __lpfc_sli_release_iocbq(phba,
  2897. cmdiocbp);
  2898. }
  2899. break;
  2900. case LPFC_UNKNOWN_IOCB:
  2901. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2902. char adaptermsg[LPFC_MAX_ADPTMSG];
  2903. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2904. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2905. MAX_MSG_DATA);
  2906. dev_warn(&((phba->pcidev)->dev),
  2907. "lpfc%d: %s\n",
  2908. phba->brd_no, adaptermsg);
  2909. } else {
  2910. /* Unknown IOCB command */
  2911. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2912. "0335 Unknown IOCB "
  2913. "command Data: x%x "
  2914. "x%x x%x x%x\n",
  2915. irsp->ulpCommand,
  2916. irsp->ulpStatus,
  2917. irsp->ulpIoTag,
  2918. irsp->ulpContext);
  2919. }
  2920. break;
  2921. }
  2922. if (free_saveq) {
  2923. list_for_each_entry_safe(rspiocbp, next_iocb,
  2924. &saveq->list, list) {
  2925. list_del(&rspiocbp->list);
  2926. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2927. }
  2928. __lpfc_sli_release_iocbq(phba, saveq);
  2929. }
  2930. rspiocbp = NULL;
  2931. }
  2932. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2933. return rspiocbp;
  2934. }
  2935. /**
  2936. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2937. * @phba: Pointer to HBA context object.
  2938. * @pring: Pointer to driver SLI ring object.
  2939. * @mask: Host attention register mask for this ring.
  2940. *
  2941. * This routine wraps the actual slow_ring event process routine from the
  2942. * API jump table function pointer from the lpfc_hba struct.
  2943. **/
  2944. void
  2945. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2946. struct lpfc_sli_ring *pring, uint32_t mask)
  2947. {
  2948. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2949. }
  2950. /**
  2951. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2952. * @phba: Pointer to HBA context object.
  2953. * @pring: Pointer to driver SLI ring object.
  2954. * @mask: Host attention register mask for this ring.
  2955. *
  2956. * This function is called from the worker thread when there is a ring event
  2957. * for non-fcp rings. The caller does not hold any lock. The function will
  2958. * remove each response iocb in the response ring and calls the handle
  2959. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2960. **/
  2961. static void
  2962. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2963. struct lpfc_sli_ring *pring, uint32_t mask)
  2964. {
  2965. struct lpfc_pgp *pgp;
  2966. IOCB_t *entry;
  2967. IOCB_t *irsp = NULL;
  2968. struct lpfc_iocbq *rspiocbp = NULL;
  2969. uint32_t portRspPut, portRspMax;
  2970. unsigned long iflag;
  2971. uint32_t status;
  2972. pgp = &phba->port_gp[pring->ringno];
  2973. spin_lock_irqsave(&phba->hbalock, iflag);
  2974. pring->stats.iocb_event++;
  2975. /*
  2976. * The next available response entry should never exceed the maximum
  2977. * entries. If it does, treat it as an adapter hardware error.
  2978. */
  2979. portRspMax = pring->numRiocb;
  2980. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2981. if (portRspPut >= portRspMax) {
  2982. /*
  2983. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2984. * rsp ring <portRspMax>
  2985. */
  2986. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2987. "0303 Ring %d handler: portRspPut %d "
  2988. "is bigger than rsp ring %d\n",
  2989. pring->ringno, portRspPut, portRspMax);
  2990. phba->link_state = LPFC_HBA_ERROR;
  2991. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2992. phba->work_hs = HS_FFER3;
  2993. lpfc_handle_eratt(phba);
  2994. return;
  2995. }
  2996. rmb();
  2997. while (pring->rspidx != portRspPut) {
  2998. /*
  2999. * Build a completion list and call the appropriate handler.
  3000. * The process is to get the next available response iocb, get
  3001. * a free iocb from the list, copy the response data into the
  3002. * free iocb, insert to the continuation list, and update the
  3003. * next response index to slim. This process makes response
  3004. * iocb's in the ring available to DMA as fast as possible but
  3005. * pays a penalty for a copy operation. Since the iocb is
  3006. * only 32 bytes, this penalty is considered small relative to
  3007. * the PCI reads for register values and a slim write. When
  3008. * the ulpLe field is set, the entire Command has been
  3009. * received.
  3010. */
  3011. entry = lpfc_resp_iocb(phba, pring);
  3012. phba->last_completion_time = jiffies;
  3013. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3014. if (rspiocbp == NULL) {
  3015. printk(KERN_ERR "%s: out of buffers! Failing "
  3016. "completion.\n", __func__);
  3017. break;
  3018. }
  3019. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3020. phba->iocb_rsp_size);
  3021. irsp = &rspiocbp->iocb;
  3022. if (++pring->rspidx >= portRspMax)
  3023. pring->rspidx = 0;
  3024. if (pring->ringno == LPFC_ELS_RING) {
  3025. lpfc_debugfs_slow_ring_trc(phba,
  3026. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3027. *(((uint32_t *) irsp) + 4),
  3028. *(((uint32_t *) irsp) + 6),
  3029. *(((uint32_t *) irsp) + 7));
  3030. }
  3031. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3032. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3033. /* Handle the response IOCB */
  3034. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3035. spin_lock_irqsave(&phba->hbalock, iflag);
  3036. /*
  3037. * If the port response put pointer has not been updated, sync
  3038. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3039. * response put pointer.
  3040. */
  3041. if (pring->rspidx == portRspPut) {
  3042. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3043. }
  3044. } /* while (pring->rspidx != portRspPut) */
  3045. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3046. /* At least one response entry has been freed */
  3047. pring->stats.iocb_rsp_full++;
  3048. /* SET RxRE_RSP in Chip Att register */
  3049. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3050. writel(status, phba->CAregaddr);
  3051. readl(phba->CAregaddr); /* flush */
  3052. }
  3053. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3054. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3055. pring->stats.iocb_cmd_empty++;
  3056. /* Force update of the local copy of cmdGetInx */
  3057. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3058. lpfc_sli_resume_iocb(phba, pring);
  3059. if ((pring->lpfc_sli_cmd_available))
  3060. (pring->lpfc_sli_cmd_available) (phba, pring);
  3061. }
  3062. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3063. return;
  3064. }
  3065. /**
  3066. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3067. * @phba: Pointer to HBA context object.
  3068. * @pring: Pointer to driver SLI ring object.
  3069. * @mask: Host attention register mask for this ring.
  3070. *
  3071. * This function is called from the worker thread when there is a pending
  3072. * ELS response iocb on the driver internal slow-path response iocb worker
  3073. * queue. The caller does not hold any lock. The function will remove each
  3074. * response iocb from the response worker queue and calls the handle
  3075. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3076. **/
  3077. static void
  3078. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3079. struct lpfc_sli_ring *pring, uint32_t mask)
  3080. {
  3081. struct lpfc_iocbq *irspiocbq;
  3082. struct hbq_dmabuf *dmabuf;
  3083. struct lpfc_cq_event *cq_event;
  3084. unsigned long iflag;
  3085. spin_lock_irqsave(&phba->hbalock, iflag);
  3086. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3087. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3088. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3089. /* Get the response iocb from the head of work queue */
  3090. spin_lock_irqsave(&phba->hbalock, iflag);
  3091. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3092. cq_event, struct lpfc_cq_event, list);
  3093. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3094. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3095. case CQE_CODE_COMPL_WQE:
  3096. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3097. cq_event);
  3098. /* Translate ELS WCQE to response IOCBQ */
  3099. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3100. irspiocbq);
  3101. if (irspiocbq)
  3102. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3103. irspiocbq);
  3104. break;
  3105. case CQE_CODE_RECEIVE:
  3106. case CQE_CODE_RECEIVE_V1:
  3107. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3108. cq_event);
  3109. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3110. break;
  3111. default:
  3112. break;
  3113. }
  3114. }
  3115. }
  3116. /**
  3117. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3118. * @phba: Pointer to HBA context object.
  3119. * @pring: Pointer to driver SLI ring object.
  3120. *
  3121. * This function aborts all iocbs in the given ring and frees all the iocb
  3122. * objects in txq. This function issues an abort iocb for all the iocb commands
  3123. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3124. * the return of this function. The caller is not required to hold any locks.
  3125. **/
  3126. void
  3127. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3128. {
  3129. LIST_HEAD(completions);
  3130. struct lpfc_iocbq *iocb, *next_iocb;
  3131. if (pring->ringno == LPFC_ELS_RING) {
  3132. lpfc_fabric_abort_hba(phba);
  3133. }
  3134. /* Error everything on txq and txcmplq
  3135. * First do the txq.
  3136. */
  3137. spin_lock_irq(&phba->hbalock);
  3138. list_splice_init(&pring->txq, &completions);
  3139. pring->txq_cnt = 0;
  3140. /* Next issue ABTS for everything on the txcmplq */
  3141. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3142. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3143. spin_unlock_irq(&phba->hbalock);
  3144. /* Cancel all the IOCBs from the completions list */
  3145. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3146. IOERR_SLI_ABORTED);
  3147. }
  3148. /**
  3149. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3150. * @phba: Pointer to HBA context object.
  3151. *
  3152. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3153. * objects in txq and txcmplq. This function will not issue abort iocbs
  3154. * for all the iocb commands in txcmplq, they will just be returned with
  3155. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3156. * slot has been permanently disabled.
  3157. **/
  3158. void
  3159. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3160. {
  3161. LIST_HEAD(txq);
  3162. LIST_HEAD(txcmplq);
  3163. struct lpfc_sli *psli = &phba->sli;
  3164. struct lpfc_sli_ring *pring;
  3165. /* Currently, only one fcp ring */
  3166. pring = &psli->ring[psli->fcp_ring];
  3167. spin_lock_irq(&phba->hbalock);
  3168. /* Retrieve everything on txq */
  3169. list_splice_init(&pring->txq, &txq);
  3170. pring->txq_cnt = 0;
  3171. /* Retrieve everything on the txcmplq */
  3172. list_splice_init(&pring->txcmplq, &txcmplq);
  3173. pring->txcmplq_cnt = 0;
  3174. spin_unlock_irq(&phba->hbalock);
  3175. /* Flush the txq */
  3176. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3177. IOERR_SLI_DOWN);
  3178. /* Flush the txcmpq */
  3179. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3180. IOERR_SLI_DOWN);
  3181. }
  3182. /**
  3183. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3184. * @phba: Pointer to HBA context object.
  3185. * @mask: Bit mask to be checked.
  3186. *
  3187. * This function reads the host status register and compares
  3188. * with the provided bit mask to check if HBA completed
  3189. * the restart. This function will wait in a loop for the
  3190. * HBA to complete restart. If the HBA does not restart within
  3191. * 15 iterations, the function will reset the HBA again. The
  3192. * function returns 1 when HBA fail to restart otherwise returns
  3193. * zero.
  3194. **/
  3195. static int
  3196. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3197. {
  3198. uint32_t status;
  3199. int i = 0;
  3200. int retval = 0;
  3201. /* Read the HBA Host Status Register */
  3202. if (lpfc_readl(phba->HSregaddr, &status))
  3203. return 1;
  3204. /*
  3205. * Check status register every 100ms for 5 retries, then every
  3206. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3207. * every 2.5 sec for 4.
  3208. * Break our of the loop if errors occurred during init.
  3209. */
  3210. while (((status & mask) != mask) &&
  3211. !(status & HS_FFERM) &&
  3212. i++ < 20) {
  3213. if (i <= 5)
  3214. msleep(10);
  3215. else if (i <= 10)
  3216. msleep(500);
  3217. else
  3218. msleep(2500);
  3219. if (i == 15) {
  3220. /* Do post */
  3221. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3222. lpfc_sli_brdrestart(phba);
  3223. }
  3224. /* Read the HBA Host Status Register */
  3225. if (lpfc_readl(phba->HSregaddr, &status)) {
  3226. retval = 1;
  3227. break;
  3228. }
  3229. }
  3230. /* Check to see if any errors occurred during init */
  3231. if ((status & HS_FFERM) || (i >= 20)) {
  3232. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3233. "2751 Adapter failed to restart, "
  3234. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3235. status,
  3236. readl(phba->MBslimaddr + 0xa8),
  3237. readl(phba->MBslimaddr + 0xac));
  3238. phba->link_state = LPFC_HBA_ERROR;
  3239. retval = 1;
  3240. }
  3241. return retval;
  3242. }
  3243. /**
  3244. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3245. * @phba: Pointer to HBA context object.
  3246. * @mask: Bit mask to be checked.
  3247. *
  3248. * This function checks the host status register to check if HBA is
  3249. * ready. This function will wait in a loop for the HBA to be ready
  3250. * If the HBA is not ready , the function will will reset the HBA PCI
  3251. * function again. The function returns 1 when HBA fail to be ready
  3252. * otherwise returns zero.
  3253. **/
  3254. static int
  3255. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3256. {
  3257. uint32_t status;
  3258. int retval = 0;
  3259. /* Read the HBA Host Status Register */
  3260. status = lpfc_sli4_post_status_check(phba);
  3261. if (status) {
  3262. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3263. lpfc_sli_brdrestart(phba);
  3264. status = lpfc_sli4_post_status_check(phba);
  3265. }
  3266. /* Check to see if any errors occurred during init */
  3267. if (status) {
  3268. phba->link_state = LPFC_HBA_ERROR;
  3269. retval = 1;
  3270. } else
  3271. phba->sli4_hba.intr_enable = 0;
  3272. return retval;
  3273. }
  3274. /**
  3275. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3276. * @phba: Pointer to HBA context object.
  3277. * @mask: Bit mask to be checked.
  3278. *
  3279. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3280. * from the API jump table function pointer from the lpfc_hba struct.
  3281. **/
  3282. int
  3283. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3284. {
  3285. return phba->lpfc_sli_brdready(phba, mask);
  3286. }
  3287. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3288. /**
  3289. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3290. * @phba: Pointer to HBA context object.
  3291. *
  3292. * This function is called before resetting an HBA. This function is called
  3293. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3294. **/
  3295. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3296. {
  3297. uint32_t __iomem *resp_buf;
  3298. uint32_t __iomem *mbox_buf;
  3299. volatile uint32_t mbox;
  3300. uint32_t hc_copy, ha_copy, resp_data;
  3301. int i;
  3302. uint8_t hdrtype;
  3303. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3304. if (hdrtype != 0x80 ||
  3305. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3306. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3307. return;
  3308. /*
  3309. * Tell the other part of the chip to suspend temporarily all
  3310. * its DMA activity.
  3311. */
  3312. resp_buf = phba->MBslimaddr;
  3313. /* Disable the error attention */
  3314. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3315. return;
  3316. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3317. readl(phba->HCregaddr); /* flush */
  3318. phba->link_flag |= LS_IGNORE_ERATT;
  3319. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3320. return;
  3321. if (ha_copy & HA_ERATT) {
  3322. /* Clear Chip error bit */
  3323. writel(HA_ERATT, phba->HAregaddr);
  3324. phba->pport->stopped = 1;
  3325. }
  3326. mbox = 0;
  3327. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3328. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3329. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3330. mbox_buf = phba->MBslimaddr;
  3331. writel(mbox, mbox_buf);
  3332. for (i = 0; i < 50; i++) {
  3333. if (lpfc_readl((resp_buf + 1), &resp_data))
  3334. return;
  3335. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3336. mdelay(1);
  3337. else
  3338. break;
  3339. }
  3340. resp_data = 0;
  3341. if (lpfc_readl((resp_buf + 1), &resp_data))
  3342. return;
  3343. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3344. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3345. phba->pport->stopped)
  3346. goto restore_hc;
  3347. else
  3348. goto clear_errat;
  3349. }
  3350. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3351. resp_data = 0;
  3352. for (i = 0; i < 500; i++) {
  3353. if (lpfc_readl(resp_buf, &resp_data))
  3354. return;
  3355. if (resp_data != mbox)
  3356. mdelay(1);
  3357. else
  3358. break;
  3359. }
  3360. clear_errat:
  3361. while (++i < 500) {
  3362. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3363. return;
  3364. if (!(ha_copy & HA_ERATT))
  3365. mdelay(1);
  3366. else
  3367. break;
  3368. }
  3369. if (readl(phba->HAregaddr) & HA_ERATT) {
  3370. writel(HA_ERATT, phba->HAregaddr);
  3371. phba->pport->stopped = 1;
  3372. }
  3373. restore_hc:
  3374. phba->link_flag &= ~LS_IGNORE_ERATT;
  3375. writel(hc_copy, phba->HCregaddr);
  3376. readl(phba->HCregaddr); /* flush */
  3377. }
  3378. /**
  3379. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3380. * @phba: Pointer to HBA context object.
  3381. *
  3382. * This function issues a kill_board mailbox command and waits for
  3383. * the error attention interrupt. This function is called for stopping
  3384. * the firmware processing. The caller is not required to hold any
  3385. * locks. This function calls lpfc_hba_down_post function to free
  3386. * any pending commands after the kill. The function will return 1 when it
  3387. * fails to kill the board else will return 0.
  3388. **/
  3389. int
  3390. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3391. {
  3392. struct lpfc_sli *psli;
  3393. LPFC_MBOXQ_t *pmb;
  3394. uint32_t status;
  3395. uint32_t ha_copy;
  3396. int retval;
  3397. int i = 0;
  3398. psli = &phba->sli;
  3399. /* Kill HBA */
  3400. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3401. "0329 Kill HBA Data: x%x x%x\n",
  3402. phba->pport->port_state, psli->sli_flag);
  3403. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3404. if (!pmb)
  3405. return 1;
  3406. /* Disable the error attention */
  3407. spin_lock_irq(&phba->hbalock);
  3408. if (lpfc_readl(phba->HCregaddr, &status)) {
  3409. spin_unlock_irq(&phba->hbalock);
  3410. mempool_free(pmb, phba->mbox_mem_pool);
  3411. return 1;
  3412. }
  3413. status &= ~HC_ERINT_ENA;
  3414. writel(status, phba->HCregaddr);
  3415. readl(phba->HCregaddr); /* flush */
  3416. phba->link_flag |= LS_IGNORE_ERATT;
  3417. spin_unlock_irq(&phba->hbalock);
  3418. lpfc_kill_board(phba, pmb);
  3419. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3420. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3421. if (retval != MBX_SUCCESS) {
  3422. if (retval != MBX_BUSY)
  3423. mempool_free(pmb, phba->mbox_mem_pool);
  3424. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3425. "2752 KILL_BOARD command failed retval %d\n",
  3426. retval);
  3427. spin_lock_irq(&phba->hbalock);
  3428. phba->link_flag &= ~LS_IGNORE_ERATT;
  3429. spin_unlock_irq(&phba->hbalock);
  3430. return 1;
  3431. }
  3432. spin_lock_irq(&phba->hbalock);
  3433. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3434. spin_unlock_irq(&phba->hbalock);
  3435. mempool_free(pmb, phba->mbox_mem_pool);
  3436. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3437. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3438. * 3 seconds we still set HBA_ERROR state because the status of the
  3439. * board is now undefined.
  3440. */
  3441. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3442. return 1;
  3443. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3444. mdelay(100);
  3445. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3446. return 1;
  3447. }
  3448. del_timer_sync(&psli->mbox_tmo);
  3449. if (ha_copy & HA_ERATT) {
  3450. writel(HA_ERATT, phba->HAregaddr);
  3451. phba->pport->stopped = 1;
  3452. }
  3453. spin_lock_irq(&phba->hbalock);
  3454. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3455. psli->mbox_active = NULL;
  3456. phba->link_flag &= ~LS_IGNORE_ERATT;
  3457. spin_unlock_irq(&phba->hbalock);
  3458. lpfc_hba_down_post(phba);
  3459. phba->link_state = LPFC_HBA_ERROR;
  3460. return ha_copy & HA_ERATT ? 0 : 1;
  3461. }
  3462. /**
  3463. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3464. * @phba: Pointer to HBA context object.
  3465. *
  3466. * This function resets the HBA by writing HC_INITFF to the control
  3467. * register. After the HBA resets, this function resets all the iocb ring
  3468. * indices. This function disables PCI layer parity checking during
  3469. * the reset.
  3470. * This function returns 0 always.
  3471. * The caller is not required to hold any locks.
  3472. **/
  3473. int
  3474. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3475. {
  3476. struct lpfc_sli *psli;
  3477. struct lpfc_sli_ring *pring;
  3478. uint16_t cfg_value;
  3479. int i;
  3480. psli = &phba->sli;
  3481. /* Reset HBA */
  3482. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3483. "0325 Reset HBA Data: x%x x%x\n",
  3484. phba->pport->port_state, psli->sli_flag);
  3485. /* perform board reset */
  3486. phba->fc_eventTag = 0;
  3487. phba->link_events = 0;
  3488. phba->pport->fc_myDID = 0;
  3489. phba->pport->fc_prevDID = 0;
  3490. /* Turn off parity checking and serr during the physical reset */
  3491. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3492. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3493. (cfg_value &
  3494. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3495. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3496. /* Now toggle INITFF bit in the Host Control Register */
  3497. writel(HC_INITFF, phba->HCregaddr);
  3498. mdelay(1);
  3499. readl(phba->HCregaddr); /* flush */
  3500. writel(0, phba->HCregaddr);
  3501. readl(phba->HCregaddr); /* flush */
  3502. /* Restore PCI cmd register */
  3503. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3504. /* Initialize relevant SLI info */
  3505. for (i = 0; i < psli->num_rings; i++) {
  3506. pring = &psli->ring[i];
  3507. pring->flag = 0;
  3508. pring->rspidx = 0;
  3509. pring->next_cmdidx = 0;
  3510. pring->local_getidx = 0;
  3511. pring->cmdidx = 0;
  3512. pring->missbufcnt = 0;
  3513. }
  3514. phba->link_state = LPFC_WARM_START;
  3515. return 0;
  3516. }
  3517. /**
  3518. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3519. * @phba: Pointer to HBA context object.
  3520. *
  3521. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3522. * checking during resets the device. The caller is not required to hold
  3523. * any locks.
  3524. *
  3525. * This function returns 0 always.
  3526. **/
  3527. int
  3528. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3529. {
  3530. struct lpfc_sli *psli = &phba->sli;
  3531. uint16_t cfg_value;
  3532. /* Reset HBA */
  3533. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3534. "0295 Reset HBA Data: x%x x%x\n",
  3535. phba->pport->port_state, psli->sli_flag);
  3536. /* perform board reset */
  3537. phba->fc_eventTag = 0;
  3538. phba->link_events = 0;
  3539. phba->pport->fc_myDID = 0;
  3540. phba->pport->fc_prevDID = 0;
  3541. spin_lock_irq(&phba->hbalock);
  3542. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3543. phba->fcf.fcf_flag = 0;
  3544. spin_unlock_irq(&phba->hbalock);
  3545. /* Now physically reset the device */
  3546. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3547. "0389 Performing PCI function reset!\n");
  3548. /* Turn off parity checking and serr during the physical reset */
  3549. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3550. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3551. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3552. /* Perform FCoE PCI function reset */
  3553. lpfc_sli4_queue_destroy(phba);
  3554. lpfc_pci_function_reset(phba);
  3555. /* Restore PCI cmd register */
  3556. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3557. return 0;
  3558. }
  3559. /**
  3560. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3561. * @phba: Pointer to HBA context object.
  3562. *
  3563. * This function is called in the SLI initialization code path to
  3564. * restart the HBA. The caller is not required to hold any lock.
  3565. * This function writes MBX_RESTART mailbox command to the SLIM and
  3566. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3567. * function to free any pending commands. The function enables
  3568. * POST only during the first initialization. The function returns zero.
  3569. * The function does not guarantee completion of MBX_RESTART mailbox
  3570. * command before the return of this function.
  3571. **/
  3572. static int
  3573. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3574. {
  3575. MAILBOX_t *mb;
  3576. struct lpfc_sli *psli;
  3577. volatile uint32_t word0;
  3578. void __iomem *to_slim;
  3579. uint32_t hba_aer_enabled;
  3580. spin_lock_irq(&phba->hbalock);
  3581. /* Take PCIe device Advanced Error Reporting (AER) state */
  3582. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3583. psli = &phba->sli;
  3584. /* Restart HBA */
  3585. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3586. "0337 Restart HBA Data: x%x x%x\n",
  3587. phba->pport->port_state, psli->sli_flag);
  3588. word0 = 0;
  3589. mb = (MAILBOX_t *) &word0;
  3590. mb->mbxCommand = MBX_RESTART;
  3591. mb->mbxHc = 1;
  3592. lpfc_reset_barrier(phba);
  3593. to_slim = phba->MBslimaddr;
  3594. writel(*(uint32_t *) mb, to_slim);
  3595. readl(to_slim); /* flush */
  3596. /* Only skip post after fc_ffinit is completed */
  3597. if (phba->pport->port_state)
  3598. word0 = 1; /* This is really setting up word1 */
  3599. else
  3600. word0 = 0; /* This is really setting up word1 */
  3601. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3602. writel(*(uint32_t *) mb, to_slim);
  3603. readl(to_slim); /* flush */
  3604. lpfc_sli_brdreset(phba);
  3605. phba->pport->stopped = 0;
  3606. phba->link_state = LPFC_INIT_START;
  3607. phba->hba_flag = 0;
  3608. spin_unlock_irq(&phba->hbalock);
  3609. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3610. psli->stats_start = get_seconds();
  3611. /* Give the INITFF and Post time to settle. */
  3612. mdelay(100);
  3613. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3614. if (hba_aer_enabled)
  3615. pci_disable_pcie_error_reporting(phba->pcidev);
  3616. lpfc_hba_down_post(phba);
  3617. return 0;
  3618. }
  3619. /**
  3620. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3621. * @phba: Pointer to HBA context object.
  3622. *
  3623. * This function is called in the SLI initialization code path to restart
  3624. * a SLI4 HBA. The caller is not required to hold any lock.
  3625. * At the end of the function, it calls lpfc_hba_down_post function to
  3626. * free any pending commands.
  3627. **/
  3628. static int
  3629. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3630. {
  3631. struct lpfc_sli *psli = &phba->sli;
  3632. uint32_t hba_aer_enabled;
  3633. /* Restart HBA */
  3634. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3635. "0296 Restart HBA Data: x%x x%x\n",
  3636. phba->pport->port_state, psli->sli_flag);
  3637. /* Take PCIe device Advanced Error Reporting (AER) state */
  3638. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3639. lpfc_sli4_brdreset(phba);
  3640. spin_lock_irq(&phba->hbalock);
  3641. phba->pport->stopped = 0;
  3642. phba->link_state = LPFC_INIT_START;
  3643. phba->hba_flag = 0;
  3644. spin_unlock_irq(&phba->hbalock);
  3645. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3646. psli->stats_start = get_seconds();
  3647. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3648. if (hba_aer_enabled)
  3649. pci_disable_pcie_error_reporting(phba->pcidev);
  3650. lpfc_hba_down_post(phba);
  3651. return 0;
  3652. }
  3653. /**
  3654. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3655. * @phba: Pointer to HBA context object.
  3656. *
  3657. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3658. * API jump table function pointer from the lpfc_hba struct.
  3659. **/
  3660. int
  3661. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3662. {
  3663. return phba->lpfc_sli_brdrestart(phba);
  3664. }
  3665. /**
  3666. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3667. * @phba: Pointer to HBA context object.
  3668. *
  3669. * This function is called after a HBA restart to wait for successful
  3670. * restart of the HBA. Successful restart of the HBA is indicated by
  3671. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3672. * iteration, the function will restart the HBA again. The function returns
  3673. * zero if HBA successfully restarted else returns negative error code.
  3674. **/
  3675. static int
  3676. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3677. {
  3678. uint32_t status, i = 0;
  3679. /* Read the HBA Host Status Register */
  3680. if (lpfc_readl(phba->HSregaddr, &status))
  3681. return -EIO;
  3682. /* Check status register to see what current state is */
  3683. i = 0;
  3684. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3685. /* Check every 10ms for 10 retries, then every 100ms for 90
  3686. * retries, then every 1 sec for 50 retires for a total of
  3687. * ~60 seconds before reset the board again and check every
  3688. * 1 sec for 50 retries. The up to 60 seconds before the
  3689. * board ready is required by the Falcon FIPS zeroization
  3690. * complete, and any reset the board in between shall cause
  3691. * restart of zeroization, further delay the board ready.
  3692. */
  3693. if (i++ >= 200) {
  3694. /* Adapter failed to init, timeout, status reg
  3695. <status> */
  3696. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3697. "0436 Adapter failed to init, "
  3698. "timeout, status reg x%x, "
  3699. "FW Data: A8 x%x AC x%x\n", status,
  3700. readl(phba->MBslimaddr + 0xa8),
  3701. readl(phba->MBslimaddr + 0xac));
  3702. phba->link_state = LPFC_HBA_ERROR;
  3703. return -ETIMEDOUT;
  3704. }
  3705. /* Check to see if any errors occurred during init */
  3706. if (status & HS_FFERM) {
  3707. /* ERROR: During chipset initialization */
  3708. /* Adapter failed to init, chipset, status reg
  3709. <status> */
  3710. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3711. "0437 Adapter failed to init, "
  3712. "chipset, status reg x%x, "
  3713. "FW Data: A8 x%x AC x%x\n", status,
  3714. readl(phba->MBslimaddr + 0xa8),
  3715. readl(phba->MBslimaddr + 0xac));
  3716. phba->link_state = LPFC_HBA_ERROR;
  3717. return -EIO;
  3718. }
  3719. if (i <= 10)
  3720. msleep(10);
  3721. else if (i <= 100)
  3722. msleep(100);
  3723. else
  3724. msleep(1000);
  3725. if (i == 150) {
  3726. /* Do post */
  3727. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3728. lpfc_sli_brdrestart(phba);
  3729. }
  3730. /* Read the HBA Host Status Register */
  3731. if (lpfc_readl(phba->HSregaddr, &status))
  3732. return -EIO;
  3733. }
  3734. /* Check to see if any errors occurred during init */
  3735. if (status & HS_FFERM) {
  3736. /* ERROR: During chipset initialization */
  3737. /* Adapter failed to init, chipset, status reg <status> */
  3738. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3739. "0438 Adapter failed to init, chipset, "
  3740. "status reg x%x, "
  3741. "FW Data: A8 x%x AC x%x\n", status,
  3742. readl(phba->MBslimaddr + 0xa8),
  3743. readl(phba->MBslimaddr + 0xac));
  3744. phba->link_state = LPFC_HBA_ERROR;
  3745. return -EIO;
  3746. }
  3747. /* Clear all interrupt enable conditions */
  3748. writel(0, phba->HCregaddr);
  3749. readl(phba->HCregaddr); /* flush */
  3750. /* setup host attn register */
  3751. writel(0xffffffff, phba->HAregaddr);
  3752. readl(phba->HAregaddr); /* flush */
  3753. return 0;
  3754. }
  3755. /**
  3756. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3757. *
  3758. * This function calculates and returns the number of HBQs required to be
  3759. * configured.
  3760. **/
  3761. int
  3762. lpfc_sli_hbq_count(void)
  3763. {
  3764. return ARRAY_SIZE(lpfc_hbq_defs);
  3765. }
  3766. /**
  3767. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3768. *
  3769. * This function adds the number of hbq entries in every HBQ to get
  3770. * the total number of hbq entries required for the HBA and returns
  3771. * the total count.
  3772. **/
  3773. static int
  3774. lpfc_sli_hbq_entry_count(void)
  3775. {
  3776. int hbq_count = lpfc_sli_hbq_count();
  3777. int count = 0;
  3778. int i;
  3779. for (i = 0; i < hbq_count; ++i)
  3780. count += lpfc_hbq_defs[i]->entry_count;
  3781. return count;
  3782. }
  3783. /**
  3784. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3785. *
  3786. * This function calculates amount of memory required for all hbq entries
  3787. * to be configured and returns the total memory required.
  3788. **/
  3789. int
  3790. lpfc_sli_hbq_size(void)
  3791. {
  3792. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3793. }
  3794. /**
  3795. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3796. * @phba: Pointer to HBA context object.
  3797. *
  3798. * This function is called during the SLI initialization to configure
  3799. * all the HBQs and post buffers to the HBQ. The caller is not
  3800. * required to hold any locks. This function will return zero if successful
  3801. * else it will return negative error code.
  3802. **/
  3803. static int
  3804. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3805. {
  3806. int hbq_count = lpfc_sli_hbq_count();
  3807. LPFC_MBOXQ_t *pmb;
  3808. MAILBOX_t *pmbox;
  3809. uint32_t hbqno;
  3810. uint32_t hbq_entry_index;
  3811. /* Get a Mailbox buffer to setup mailbox
  3812. * commands for HBA initialization
  3813. */
  3814. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3815. if (!pmb)
  3816. return -ENOMEM;
  3817. pmbox = &pmb->u.mb;
  3818. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3819. phba->link_state = LPFC_INIT_MBX_CMDS;
  3820. phba->hbq_in_use = 1;
  3821. hbq_entry_index = 0;
  3822. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3823. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3824. phba->hbqs[hbqno].hbqPutIdx = 0;
  3825. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3826. phba->hbqs[hbqno].entry_count =
  3827. lpfc_hbq_defs[hbqno]->entry_count;
  3828. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3829. hbq_entry_index, pmb);
  3830. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3831. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3832. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3833. mbxStatus <status>, ring <num> */
  3834. lpfc_printf_log(phba, KERN_ERR,
  3835. LOG_SLI | LOG_VPORT,
  3836. "1805 Adapter failed to init. "
  3837. "Data: x%x x%x x%x\n",
  3838. pmbox->mbxCommand,
  3839. pmbox->mbxStatus, hbqno);
  3840. phba->link_state = LPFC_HBA_ERROR;
  3841. mempool_free(pmb, phba->mbox_mem_pool);
  3842. return -ENXIO;
  3843. }
  3844. }
  3845. phba->hbq_count = hbq_count;
  3846. mempool_free(pmb, phba->mbox_mem_pool);
  3847. /* Initially populate or replenish the HBQs */
  3848. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3849. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3850. return 0;
  3851. }
  3852. /**
  3853. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3854. * @phba: Pointer to HBA context object.
  3855. *
  3856. * This function is called during the SLI initialization to configure
  3857. * all the HBQs and post buffers to the HBQ. The caller is not
  3858. * required to hold any locks. This function will return zero if successful
  3859. * else it will return negative error code.
  3860. **/
  3861. static int
  3862. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3863. {
  3864. phba->hbq_in_use = 1;
  3865. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3866. phba->hbq_count = 1;
  3867. /* Initially populate or replenish the HBQs */
  3868. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3869. return 0;
  3870. }
  3871. /**
  3872. * lpfc_sli_config_port - Issue config port mailbox command
  3873. * @phba: Pointer to HBA context object.
  3874. * @sli_mode: sli mode - 2/3
  3875. *
  3876. * This function is called by the sli intialization code path
  3877. * to issue config_port mailbox command. This function restarts the
  3878. * HBA firmware and issues a config_port mailbox command to configure
  3879. * the SLI interface in the sli mode specified by sli_mode
  3880. * variable. The caller is not required to hold any locks.
  3881. * The function returns 0 if successful, else returns negative error
  3882. * code.
  3883. **/
  3884. int
  3885. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3886. {
  3887. LPFC_MBOXQ_t *pmb;
  3888. uint32_t resetcount = 0, rc = 0, done = 0;
  3889. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3890. if (!pmb) {
  3891. phba->link_state = LPFC_HBA_ERROR;
  3892. return -ENOMEM;
  3893. }
  3894. phba->sli_rev = sli_mode;
  3895. while (resetcount < 2 && !done) {
  3896. spin_lock_irq(&phba->hbalock);
  3897. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3898. spin_unlock_irq(&phba->hbalock);
  3899. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3900. lpfc_sli_brdrestart(phba);
  3901. rc = lpfc_sli_chipset_init(phba);
  3902. if (rc)
  3903. break;
  3904. spin_lock_irq(&phba->hbalock);
  3905. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3906. spin_unlock_irq(&phba->hbalock);
  3907. resetcount++;
  3908. /* Call pre CONFIG_PORT mailbox command initialization. A
  3909. * value of 0 means the call was successful. Any other
  3910. * nonzero value is a failure, but if ERESTART is returned,
  3911. * the driver may reset the HBA and try again.
  3912. */
  3913. rc = lpfc_config_port_prep(phba);
  3914. if (rc == -ERESTART) {
  3915. phba->link_state = LPFC_LINK_UNKNOWN;
  3916. continue;
  3917. } else if (rc)
  3918. break;
  3919. phba->link_state = LPFC_INIT_MBX_CMDS;
  3920. lpfc_config_port(phba, pmb);
  3921. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3922. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3923. LPFC_SLI3_HBQ_ENABLED |
  3924. LPFC_SLI3_CRP_ENABLED |
  3925. LPFC_SLI3_BG_ENABLED |
  3926. LPFC_SLI3_DSS_ENABLED);
  3927. if (rc != MBX_SUCCESS) {
  3928. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3929. "0442 Adapter failed to init, mbxCmd x%x "
  3930. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3931. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3932. spin_lock_irq(&phba->hbalock);
  3933. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3934. spin_unlock_irq(&phba->hbalock);
  3935. rc = -ENXIO;
  3936. } else {
  3937. /* Allow asynchronous mailbox command to go through */
  3938. spin_lock_irq(&phba->hbalock);
  3939. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3940. spin_unlock_irq(&phba->hbalock);
  3941. done = 1;
  3942. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3943. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3944. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3945. "3110 Port did not grant ASABT\n");
  3946. }
  3947. }
  3948. if (!done) {
  3949. rc = -EINVAL;
  3950. goto do_prep_failed;
  3951. }
  3952. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3953. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3954. rc = -ENXIO;
  3955. goto do_prep_failed;
  3956. }
  3957. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3958. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3959. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3960. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3961. phba->max_vpi : phba->max_vports;
  3962. } else
  3963. phba->max_vpi = 0;
  3964. phba->fips_level = 0;
  3965. phba->fips_spec_rev = 0;
  3966. if (pmb->u.mb.un.varCfgPort.gdss) {
  3967. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3968. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3969. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3970. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3971. "2850 Security Crypto Active. FIPS x%d "
  3972. "(Spec Rev: x%d)",
  3973. phba->fips_level, phba->fips_spec_rev);
  3974. }
  3975. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3976. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3977. "2856 Config Port Security Crypto "
  3978. "Error: x%x ",
  3979. pmb->u.mb.un.varCfgPort.sec_err);
  3980. }
  3981. if (pmb->u.mb.un.varCfgPort.gerbm)
  3982. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3983. if (pmb->u.mb.un.varCfgPort.gcrp)
  3984. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3985. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3986. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3987. if (phba->cfg_enable_bg) {
  3988. if (pmb->u.mb.un.varCfgPort.gbg)
  3989. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3990. else
  3991. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3992. "0443 Adapter did not grant "
  3993. "BlockGuard\n");
  3994. }
  3995. } else {
  3996. phba->hbq_get = NULL;
  3997. phba->port_gp = phba->mbox->us.s2.port;
  3998. phba->max_vpi = 0;
  3999. }
  4000. do_prep_failed:
  4001. mempool_free(pmb, phba->mbox_mem_pool);
  4002. return rc;
  4003. }
  4004. /**
  4005. * lpfc_sli_hba_setup - SLI intialization function
  4006. * @phba: Pointer to HBA context object.
  4007. *
  4008. * This function is the main SLI intialization function. This function
  4009. * is called by the HBA intialization code, HBA reset code and HBA
  4010. * error attention handler code. Caller is not required to hold any
  4011. * locks. This function issues config_port mailbox command to configure
  4012. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4013. * calls the config_port_post function to issue init_link mailbox
  4014. * command and to start the discovery. The function will return zero
  4015. * if successful, else it will return negative error code.
  4016. **/
  4017. int
  4018. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4019. {
  4020. uint32_t rc;
  4021. int mode = 3, i;
  4022. int longs;
  4023. switch (lpfc_sli_mode) {
  4024. case 2:
  4025. if (phba->cfg_enable_npiv) {
  4026. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4027. "1824 NPIV enabled: Override lpfc_sli_mode "
  4028. "parameter (%d) to auto (0).\n",
  4029. lpfc_sli_mode);
  4030. break;
  4031. }
  4032. mode = 2;
  4033. break;
  4034. case 0:
  4035. case 3:
  4036. break;
  4037. default:
  4038. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4039. "1819 Unrecognized lpfc_sli_mode "
  4040. "parameter: %d.\n", lpfc_sli_mode);
  4041. break;
  4042. }
  4043. rc = lpfc_sli_config_port(phba, mode);
  4044. if (rc && lpfc_sli_mode == 3)
  4045. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4046. "1820 Unable to select SLI-3. "
  4047. "Not supported by adapter.\n");
  4048. if (rc && mode != 2)
  4049. rc = lpfc_sli_config_port(phba, 2);
  4050. if (rc)
  4051. goto lpfc_sli_hba_setup_error;
  4052. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4053. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4054. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4055. if (!rc) {
  4056. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4057. "2709 This device supports "
  4058. "Advanced Error Reporting (AER)\n");
  4059. spin_lock_irq(&phba->hbalock);
  4060. phba->hba_flag |= HBA_AER_ENABLED;
  4061. spin_unlock_irq(&phba->hbalock);
  4062. } else {
  4063. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4064. "2708 This device does not support "
  4065. "Advanced Error Reporting (AER)\n");
  4066. phba->cfg_aer_support = 0;
  4067. }
  4068. }
  4069. if (phba->sli_rev == 3) {
  4070. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4071. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4072. } else {
  4073. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4074. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4075. phba->sli3_options = 0;
  4076. }
  4077. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4078. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4079. phba->sli_rev, phba->max_vpi);
  4080. rc = lpfc_sli_ring_map(phba);
  4081. if (rc)
  4082. goto lpfc_sli_hba_setup_error;
  4083. /* Initialize VPIs. */
  4084. if (phba->sli_rev == LPFC_SLI_REV3) {
  4085. /*
  4086. * The VPI bitmask and physical ID array are allocated
  4087. * and initialized once only - at driver load. A port
  4088. * reset doesn't need to reinitialize this memory.
  4089. */
  4090. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4091. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4092. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4093. GFP_KERNEL);
  4094. if (!phba->vpi_bmask) {
  4095. rc = -ENOMEM;
  4096. goto lpfc_sli_hba_setup_error;
  4097. }
  4098. phba->vpi_ids = kzalloc(
  4099. (phba->max_vpi+1) * sizeof(uint16_t),
  4100. GFP_KERNEL);
  4101. if (!phba->vpi_ids) {
  4102. kfree(phba->vpi_bmask);
  4103. rc = -ENOMEM;
  4104. goto lpfc_sli_hba_setup_error;
  4105. }
  4106. for (i = 0; i < phba->max_vpi; i++)
  4107. phba->vpi_ids[i] = i;
  4108. }
  4109. }
  4110. /* Init HBQs */
  4111. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4112. rc = lpfc_sli_hbq_setup(phba);
  4113. if (rc)
  4114. goto lpfc_sli_hba_setup_error;
  4115. }
  4116. spin_lock_irq(&phba->hbalock);
  4117. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4118. spin_unlock_irq(&phba->hbalock);
  4119. rc = lpfc_config_port_post(phba);
  4120. if (rc)
  4121. goto lpfc_sli_hba_setup_error;
  4122. return rc;
  4123. lpfc_sli_hba_setup_error:
  4124. phba->link_state = LPFC_HBA_ERROR;
  4125. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4126. "0445 Firmware initialization failed\n");
  4127. return rc;
  4128. }
  4129. /**
  4130. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4131. * @phba: Pointer to HBA context object.
  4132. * @mboxq: mailbox pointer.
  4133. * This function issue a dump mailbox command to read config region
  4134. * 23 and parse the records in the region and populate driver
  4135. * data structure.
  4136. **/
  4137. static int
  4138. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4139. {
  4140. LPFC_MBOXQ_t *mboxq;
  4141. struct lpfc_dmabuf *mp;
  4142. struct lpfc_mqe *mqe;
  4143. uint32_t data_length;
  4144. int rc;
  4145. /* Program the default value of vlan_id and fc_map */
  4146. phba->valid_vlan = 0;
  4147. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4148. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4149. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4150. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4151. if (!mboxq)
  4152. return -ENOMEM;
  4153. mqe = &mboxq->u.mqe;
  4154. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4155. rc = -ENOMEM;
  4156. goto out_free_mboxq;
  4157. }
  4158. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4159. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4160. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4161. "(%d):2571 Mailbox cmd x%x Status x%x "
  4162. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4163. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4164. "CQ: x%x x%x x%x x%x\n",
  4165. mboxq->vport ? mboxq->vport->vpi : 0,
  4166. bf_get(lpfc_mqe_command, mqe),
  4167. bf_get(lpfc_mqe_status, mqe),
  4168. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4169. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4170. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4171. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4172. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4173. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4174. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4175. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4176. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4177. mboxq->mcqe.word0,
  4178. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4179. mboxq->mcqe.trailer);
  4180. if (rc) {
  4181. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4182. kfree(mp);
  4183. rc = -EIO;
  4184. goto out_free_mboxq;
  4185. }
  4186. data_length = mqe->un.mb_words[5];
  4187. if (data_length > DMP_RGN23_SIZE) {
  4188. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4189. kfree(mp);
  4190. rc = -EIO;
  4191. goto out_free_mboxq;
  4192. }
  4193. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4194. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4195. kfree(mp);
  4196. rc = 0;
  4197. out_free_mboxq:
  4198. mempool_free(mboxq, phba->mbox_mem_pool);
  4199. return rc;
  4200. }
  4201. /**
  4202. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4203. * @phba: pointer to lpfc hba data structure.
  4204. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4205. * @vpd: pointer to the memory to hold resulting port vpd data.
  4206. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4207. * On output, the number of data bytes in @vpd.
  4208. *
  4209. * This routine executes a READ_REV SLI4 mailbox command. In
  4210. * addition, this routine gets the port vpd data.
  4211. *
  4212. * Return codes
  4213. * 0 - successful
  4214. * -ENOMEM - could not allocated memory.
  4215. **/
  4216. static int
  4217. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4218. uint8_t *vpd, uint32_t *vpd_size)
  4219. {
  4220. int rc = 0;
  4221. uint32_t dma_size;
  4222. struct lpfc_dmabuf *dmabuf;
  4223. struct lpfc_mqe *mqe;
  4224. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4225. if (!dmabuf)
  4226. return -ENOMEM;
  4227. /*
  4228. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4229. * mailbox command.
  4230. */
  4231. dma_size = *vpd_size;
  4232. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4233. dma_size,
  4234. &dmabuf->phys,
  4235. GFP_KERNEL);
  4236. if (!dmabuf->virt) {
  4237. kfree(dmabuf);
  4238. return -ENOMEM;
  4239. }
  4240. memset(dmabuf->virt, 0, dma_size);
  4241. /*
  4242. * The SLI4 implementation of READ_REV conflicts at word1,
  4243. * bits 31:16 and SLI4 adds vpd functionality not present
  4244. * in SLI3. This code corrects the conflicts.
  4245. */
  4246. lpfc_read_rev(phba, mboxq);
  4247. mqe = &mboxq->u.mqe;
  4248. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4249. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4250. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4251. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4252. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4253. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4254. if (rc) {
  4255. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4256. dmabuf->virt, dmabuf->phys);
  4257. kfree(dmabuf);
  4258. return -EIO;
  4259. }
  4260. /*
  4261. * The available vpd length cannot be bigger than the
  4262. * DMA buffer passed to the port. Catch the less than
  4263. * case and update the caller's size.
  4264. */
  4265. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4266. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4267. memcpy(vpd, dmabuf->virt, *vpd_size);
  4268. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4269. dmabuf->virt, dmabuf->phys);
  4270. kfree(dmabuf);
  4271. return 0;
  4272. }
  4273. /**
  4274. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4275. * @phba: pointer to lpfc hba data structure.
  4276. *
  4277. * This routine retrieves SLI4 device physical port name this PCI function
  4278. * is attached to.
  4279. *
  4280. * Return codes
  4281. * 0 - sucessful
  4282. * otherwise - failed to retrieve physical port name
  4283. **/
  4284. static int
  4285. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4286. {
  4287. LPFC_MBOXQ_t *mboxq;
  4288. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4289. struct lpfc_controller_attribute *cntl_attr;
  4290. struct lpfc_mbx_get_port_name *get_port_name;
  4291. void *virtaddr = NULL;
  4292. uint32_t alloclen, reqlen;
  4293. uint32_t shdr_status, shdr_add_status;
  4294. union lpfc_sli4_cfg_shdr *shdr;
  4295. char cport_name = 0;
  4296. int rc;
  4297. /* We assume nothing at this point */
  4298. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4299. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4300. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4301. if (!mboxq)
  4302. return -ENOMEM;
  4303. /* obtain link type and link number via READ_CONFIG */
  4304. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4305. lpfc_sli4_read_config(phba);
  4306. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4307. goto retrieve_ppname;
  4308. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4309. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4310. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4311. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4312. LPFC_SLI4_MBX_NEMBED);
  4313. if (alloclen < reqlen) {
  4314. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4315. "3084 Allocated DMA memory size (%d) is "
  4316. "less than the requested DMA memory size "
  4317. "(%d)\n", alloclen, reqlen);
  4318. rc = -ENOMEM;
  4319. goto out_free_mboxq;
  4320. }
  4321. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4322. virtaddr = mboxq->sge_array->addr[0];
  4323. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4324. shdr = &mbx_cntl_attr->cfg_shdr;
  4325. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4326. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4327. if (shdr_status || shdr_add_status || rc) {
  4328. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4329. "3085 Mailbox x%x (x%x/x%x) failed, "
  4330. "rc:x%x, status:x%x, add_status:x%x\n",
  4331. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4332. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4333. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4334. rc, shdr_status, shdr_add_status);
  4335. rc = -ENXIO;
  4336. goto out_free_mboxq;
  4337. }
  4338. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4339. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4340. phba->sli4_hba.lnk_info.lnk_tp =
  4341. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4342. phba->sli4_hba.lnk_info.lnk_no =
  4343. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4344. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4345. "3086 lnk_type:%d, lnk_numb:%d\n",
  4346. phba->sli4_hba.lnk_info.lnk_tp,
  4347. phba->sli4_hba.lnk_info.lnk_no);
  4348. retrieve_ppname:
  4349. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4350. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4351. sizeof(struct lpfc_mbx_get_port_name) -
  4352. sizeof(struct lpfc_sli4_cfg_mhdr),
  4353. LPFC_SLI4_MBX_EMBED);
  4354. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4355. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4356. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4357. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4358. phba->sli4_hba.lnk_info.lnk_tp);
  4359. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4360. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4361. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4362. if (shdr_status || shdr_add_status || rc) {
  4363. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4364. "3087 Mailbox x%x (x%x/x%x) failed: "
  4365. "rc:x%x, status:x%x, add_status:x%x\n",
  4366. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4367. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4368. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4369. rc, shdr_status, shdr_add_status);
  4370. rc = -ENXIO;
  4371. goto out_free_mboxq;
  4372. }
  4373. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4374. case LPFC_LINK_NUMBER_0:
  4375. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4376. &get_port_name->u.response);
  4377. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4378. break;
  4379. case LPFC_LINK_NUMBER_1:
  4380. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4381. &get_port_name->u.response);
  4382. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4383. break;
  4384. case LPFC_LINK_NUMBER_2:
  4385. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4386. &get_port_name->u.response);
  4387. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4388. break;
  4389. case LPFC_LINK_NUMBER_3:
  4390. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4391. &get_port_name->u.response);
  4392. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4393. break;
  4394. default:
  4395. break;
  4396. }
  4397. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4398. phba->Port[0] = cport_name;
  4399. phba->Port[1] = '\0';
  4400. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4401. "3091 SLI get port name: %s\n", phba->Port);
  4402. }
  4403. out_free_mboxq:
  4404. if (rc != MBX_TIMEOUT) {
  4405. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4406. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4407. else
  4408. mempool_free(mboxq, phba->mbox_mem_pool);
  4409. }
  4410. return rc;
  4411. }
  4412. /**
  4413. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4414. * @phba: pointer to lpfc hba data structure.
  4415. *
  4416. * This routine is called to explicitly arm the SLI4 device's completion and
  4417. * event queues
  4418. **/
  4419. static void
  4420. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4421. {
  4422. uint8_t fcp_eqidx;
  4423. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4424. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4425. fcp_eqidx = 0;
  4426. if (phba->sli4_hba.fcp_cq) {
  4427. do
  4428. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4429. LPFC_QUEUE_REARM);
  4430. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4431. }
  4432. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4433. if (phba->sli4_hba.fp_eq) {
  4434. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4435. fcp_eqidx++)
  4436. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4437. LPFC_QUEUE_REARM);
  4438. }
  4439. }
  4440. /**
  4441. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4442. * @phba: Pointer to HBA context object.
  4443. * @type: The resource extent type.
  4444. * @extnt_count: buffer to hold port available extent count.
  4445. * @extnt_size: buffer to hold element count per extent.
  4446. *
  4447. * This function calls the port and retrievs the number of available
  4448. * extents and their size for a particular extent type.
  4449. *
  4450. * Returns: 0 if successful. Nonzero otherwise.
  4451. **/
  4452. int
  4453. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4454. uint16_t *extnt_count, uint16_t *extnt_size)
  4455. {
  4456. int rc = 0;
  4457. uint32_t length;
  4458. uint32_t mbox_tmo;
  4459. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4460. LPFC_MBOXQ_t *mbox;
  4461. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4462. if (!mbox)
  4463. return -ENOMEM;
  4464. /* Find out how many extents are available for this resource type */
  4465. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4466. sizeof(struct lpfc_sli4_cfg_mhdr));
  4467. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4468. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4469. length, LPFC_SLI4_MBX_EMBED);
  4470. /* Send an extents count of 0 - the GET doesn't use it. */
  4471. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4472. LPFC_SLI4_MBX_EMBED);
  4473. if (unlikely(rc)) {
  4474. rc = -EIO;
  4475. goto err_exit;
  4476. }
  4477. if (!phba->sli4_hba.intr_enable)
  4478. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4479. else {
  4480. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4481. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4482. }
  4483. if (unlikely(rc)) {
  4484. rc = -EIO;
  4485. goto err_exit;
  4486. }
  4487. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4488. if (bf_get(lpfc_mbox_hdr_status,
  4489. &rsrc_info->header.cfg_shdr.response)) {
  4490. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4491. "2930 Failed to get resource extents "
  4492. "Status 0x%x Add'l Status 0x%x\n",
  4493. bf_get(lpfc_mbox_hdr_status,
  4494. &rsrc_info->header.cfg_shdr.response),
  4495. bf_get(lpfc_mbox_hdr_add_status,
  4496. &rsrc_info->header.cfg_shdr.response));
  4497. rc = -EIO;
  4498. goto err_exit;
  4499. }
  4500. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4501. &rsrc_info->u.rsp);
  4502. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4503. &rsrc_info->u.rsp);
  4504. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4505. "3162 Retrieved extents type-%d from port: count:%d, "
  4506. "size:%d\n", type, *extnt_count, *extnt_size);
  4507. err_exit:
  4508. mempool_free(mbox, phba->mbox_mem_pool);
  4509. return rc;
  4510. }
  4511. /**
  4512. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4513. * @phba: Pointer to HBA context object.
  4514. * @type: The extent type to check.
  4515. *
  4516. * This function reads the current available extents from the port and checks
  4517. * if the extent count or extent size has changed since the last access.
  4518. * Callers use this routine post port reset to understand if there is a
  4519. * extent reprovisioning requirement.
  4520. *
  4521. * Returns:
  4522. * -Error: error indicates problem.
  4523. * 1: Extent count or size has changed.
  4524. * 0: No changes.
  4525. **/
  4526. static int
  4527. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4528. {
  4529. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4530. uint16_t size_diff, rsrc_ext_size;
  4531. int rc = 0;
  4532. struct lpfc_rsrc_blks *rsrc_entry;
  4533. struct list_head *rsrc_blk_list = NULL;
  4534. size_diff = 0;
  4535. curr_ext_cnt = 0;
  4536. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4537. &rsrc_ext_cnt,
  4538. &rsrc_ext_size);
  4539. if (unlikely(rc))
  4540. return -EIO;
  4541. switch (type) {
  4542. case LPFC_RSC_TYPE_FCOE_RPI:
  4543. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4544. break;
  4545. case LPFC_RSC_TYPE_FCOE_VPI:
  4546. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4547. break;
  4548. case LPFC_RSC_TYPE_FCOE_XRI:
  4549. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4550. break;
  4551. case LPFC_RSC_TYPE_FCOE_VFI:
  4552. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4553. break;
  4554. default:
  4555. break;
  4556. }
  4557. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4558. curr_ext_cnt++;
  4559. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4560. size_diff++;
  4561. }
  4562. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4563. rc = 1;
  4564. return rc;
  4565. }
  4566. /**
  4567. * lpfc_sli4_cfg_post_extnts -
  4568. * @phba: Pointer to HBA context object.
  4569. * @extnt_cnt - number of available extents.
  4570. * @type - the extent type (rpi, xri, vfi, vpi).
  4571. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4572. * @mbox - pointer to the caller's allocated mailbox structure.
  4573. *
  4574. * This function executes the extents allocation request. It also
  4575. * takes care of the amount of memory needed to allocate or get the
  4576. * allocated extents. It is the caller's responsibility to evaluate
  4577. * the response.
  4578. *
  4579. * Returns:
  4580. * -Error: Error value describes the condition found.
  4581. * 0: if successful
  4582. **/
  4583. static int
  4584. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4585. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4586. {
  4587. int rc = 0;
  4588. uint32_t req_len;
  4589. uint32_t emb_len;
  4590. uint32_t alloc_len, mbox_tmo;
  4591. /* Calculate the total requested length of the dma memory */
  4592. req_len = extnt_cnt * sizeof(uint16_t);
  4593. /*
  4594. * Calculate the size of an embedded mailbox. The uint32_t
  4595. * accounts for extents-specific word.
  4596. */
  4597. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4598. sizeof(uint32_t);
  4599. /*
  4600. * Presume the allocation and response will fit into an embedded
  4601. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4602. */
  4603. *emb = LPFC_SLI4_MBX_EMBED;
  4604. if (req_len > emb_len) {
  4605. req_len = extnt_cnt * sizeof(uint16_t) +
  4606. sizeof(union lpfc_sli4_cfg_shdr) +
  4607. sizeof(uint32_t);
  4608. *emb = LPFC_SLI4_MBX_NEMBED;
  4609. }
  4610. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4611. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4612. req_len, *emb);
  4613. if (alloc_len < req_len) {
  4614. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4615. "2982 Allocated DMA memory size (x%x) is "
  4616. "less than the requested DMA memory "
  4617. "size (x%x)\n", alloc_len, req_len);
  4618. return -ENOMEM;
  4619. }
  4620. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4621. if (unlikely(rc))
  4622. return -EIO;
  4623. if (!phba->sli4_hba.intr_enable)
  4624. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4625. else {
  4626. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4627. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4628. }
  4629. if (unlikely(rc))
  4630. rc = -EIO;
  4631. return rc;
  4632. }
  4633. /**
  4634. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4635. * @phba: Pointer to HBA context object.
  4636. * @type: The resource extent type to allocate.
  4637. *
  4638. * This function allocates the number of elements for the specified
  4639. * resource type.
  4640. **/
  4641. static int
  4642. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4643. {
  4644. bool emb = false;
  4645. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4646. uint16_t rsrc_id, rsrc_start, j, k;
  4647. uint16_t *ids;
  4648. int i, rc;
  4649. unsigned long longs;
  4650. unsigned long *bmask;
  4651. struct lpfc_rsrc_blks *rsrc_blks;
  4652. LPFC_MBOXQ_t *mbox;
  4653. uint32_t length;
  4654. struct lpfc_id_range *id_array = NULL;
  4655. void *virtaddr = NULL;
  4656. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4657. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4658. struct list_head *ext_blk_list;
  4659. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4660. &rsrc_cnt,
  4661. &rsrc_size);
  4662. if (unlikely(rc))
  4663. return -EIO;
  4664. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4665. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4666. "3009 No available Resource Extents "
  4667. "for resource type 0x%x: Count: 0x%x, "
  4668. "Size 0x%x\n", type, rsrc_cnt,
  4669. rsrc_size);
  4670. return -ENOMEM;
  4671. }
  4672. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4673. "2903 Post resource extents type-0x%x: "
  4674. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4675. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4676. if (!mbox)
  4677. return -ENOMEM;
  4678. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4679. if (unlikely(rc)) {
  4680. rc = -EIO;
  4681. goto err_exit;
  4682. }
  4683. /*
  4684. * Figure out where the response is located. Then get local pointers
  4685. * to the response data. The port does not guarantee to respond to
  4686. * all extents counts request so update the local variable with the
  4687. * allocated count from the port.
  4688. */
  4689. if (emb == LPFC_SLI4_MBX_EMBED) {
  4690. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4691. id_array = &rsrc_ext->u.rsp.id[0];
  4692. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4693. } else {
  4694. virtaddr = mbox->sge_array->addr[0];
  4695. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4696. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4697. id_array = &n_rsrc->id;
  4698. }
  4699. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4700. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4701. /*
  4702. * Based on the resource size and count, correct the base and max
  4703. * resource values.
  4704. */
  4705. length = sizeof(struct lpfc_rsrc_blks);
  4706. switch (type) {
  4707. case LPFC_RSC_TYPE_FCOE_RPI:
  4708. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4709. sizeof(unsigned long),
  4710. GFP_KERNEL);
  4711. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4712. rc = -ENOMEM;
  4713. goto err_exit;
  4714. }
  4715. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4716. sizeof(uint16_t),
  4717. GFP_KERNEL);
  4718. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4719. kfree(phba->sli4_hba.rpi_bmask);
  4720. rc = -ENOMEM;
  4721. goto err_exit;
  4722. }
  4723. /*
  4724. * The next_rpi was initialized with the maximum available
  4725. * count but the port may allocate a smaller number. Catch
  4726. * that case and update the next_rpi.
  4727. */
  4728. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4729. /* Initialize local ptrs for common extent processing later. */
  4730. bmask = phba->sli4_hba.rpi_bmask;
  4731. ids = phba->sli4_hba.rpi_ids;
  4732. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4733. break;
  4734. case LPFC_RSC_TYPE_FCOE_VPI:
  4735. phba->vpi_bmask = kzalloc(longs *
  4736. sizeof(unsigned long),
  4737. GFP_KERNEL);
  4738. if (unlikely(!phba->vpi_bmask)) {
  4739. rc = -ENOMEM;
  4740. goto err_exit;
  4741. }
  4742. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4743. sizeof(uint16_t),
  4744. GFP_KERNEL);
  4745. if (unlikely(!phba->vpi_ids)) {
  4746. kfree(phba->vpi_bmask);
  4747. rc = -ENOMEM;
  4748. goto err_exit;
  4749. }
  4750. /* Initialize local ptrs for common extent processing later. */
  4751. bmask = phba->vpi_bmask;
  4752. ids = phba->vpi_ids;
  4753. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4754. break;
  4755. case LPFC_RSC_TYPE_FCOE_XRI:
  4756. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4757. sizeof(unsigned long),
  4758. GFP_KERNEL);
  4759. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4760. rc = -ENOMEM;
  4761. goto err_exit;
  4762. }
  4763. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4764. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4765. sizeof(uint16_t),
  4766. GFP_KERNEL);
  4767. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4768. kfree(phba->sli4_hba.xri_bmask);
  4769. rc = -ENOMEM;
  4770. goto err_exit;
  4771. }
  4772. /* Initialize local ptrs for common extent processing later. */
  4773. bmask = phba->sli4_hba.xri_bmask;
  4774. ids = phba->sli4_hba.xri_ids;
  4775. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4776. break;
  4777. case LPFC_RSC_TYPE_FCOE_VFI:
  4778. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4779. sizeof(unsigned long),
  4780. GFP_KERNEL);
  4781. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4782. rc = -ENOMEM;
  4783. goto err_exit;
  4784. }
  4785. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4786. sizeof(uint16_t),
  4787. GFP_KERNEL);
  4788. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4789. kfree(phba->sli4_hba.vfi_bmask);
  4790. rc = -ENOMEM;
  4791. goto err_exit;
  4792. }
  4793. /* Initialize local ptrs for common extent processing later. */
  4794. bmask = phba->sli4_hba.vfi_bmask;
  4795. ids = phba->sli4_hba.vfi_ids;
  4796. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4797. break;
  4798. default:
  4799. /* Unsupported Opcode. Fail call. */
  4800. id_array = NULL;
  4801. bmask = NULL;
  4802. ids = NULL;
  4803. ext_blk_list = NULL;
  4804. goto err_exit;
  4805. }
  4806. /*
  4807. * Complete initializing the extent configuration with the
  4808. * allocated ids assigned to this function. The bitmask serves
  4809. * as an index into the array and manages the available ids. The
  4810. * array just stores the ids communicated to the port via the wqes.
  4811. */
  4812. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4813. if ((i % 2) == 0)
  4814. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4815. &id_array[k]);
  4816. else
  4817. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4818. &id_array[k]);
  4819. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4820. if (unlikely(!rsrc_blks)) {
  4821. rc = -ENOMEM;
  4822. kfree(bmask);
  4823. kfree(ids);
  4824. goto err_exit;
  4825. }
  4826. rsrc_blks->rsrc_start = rsrc_id;
  4827. rsrc_blks->rsrc_size = rsrc_size;
  4828. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4829. rsrc_start = rsrc_id;
  4830. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4831. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4832. lpfc_sli4_get_els_iocb_cnt(phba);
  4833. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4834. ids[j] = rsrc_id;
  4835. rsrc_id++;
  4836. j++;
  4837. }
  4838. /* Entire word processed. Get next word.*/
  4839. if ((i % 2) == 1)
  4840. k++;
  4841. }
  4842. err_exit:
  4843. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4844. return rc;
  4845. }
  4846. /**
  4847. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4848. * @phba: Pointer to HBA context object.
  4849. * @type: the extent's type.
  4850. *
  4851. * This function deallocates all extents of a particular resource type.
  4852. * SLI4 does not allow for deallocating a particular extent range. It
  4853. * is the caller's responsibility to release all kernel memory resources.
  4854. **/
  4855. static int
  4856. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4857. {
  4858. int rc;
  4859. uint32_t length, mbox_tmo = 0;
  4860. LPFC_MBOXQ_t *mbox;
  4861. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4862. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4863. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4864. if (!mbox)
  4865. return -ENOMEM;
  4866. /*
  4867. * This function sends an embedded mailbox because it only sends the
  4868. * the resource type. All extents of this type are released by the
  4869. * port.
  4870. */
  4871. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4872. sizeof(struct lpfc_sli4_cfg_mhdr));
  4873. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4874. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4875. length, LPFC_SLI4_MBX_EMBED);
  4876. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4877. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4878. LPFC_SLI4_MBX_EMBED);
  4879. if (unlikely(rc)) {
  4880. rc = -EIO;
  4881. goto out_free_mbox;
  4882. }
  4883. if (!phba->sli4_hba.intr_enable)
  4884. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4885. else {
  4886. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4887. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4888. }
  4889. if (unlikely(rc)) {
  4890. rc = -EIO;
  4891. goto out_free_mbox;
  4892. }
  4893. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4894. if (bf_get(lpfc_mbox_hdr_status,
  4895. &dealloc_rsrc->header.cfg_shdr.response)) {
  4896. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4897. "2919 Failed to release resource extents "
  4898. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4899. "Resource memory not released.\n",
  4900. type,
  4901. bf_get(lpfc_mbox_hdr_status,
  4902. &dealloc_rsrc->header.cfg_shdr.response),
  4903. bf_get(lpfc_mbox_hdr_add_status,
  4904. &dealloc_rsrc->header.cfg_shdr.response));
  4905. rc = -EIO;
  4906. goto out_free_mbox;
  4907. }
  4908. /* Release kernel memory resources for the specific type. */
  4909. switch (type) {
  4910. case LPFC_RSC_TYPE_FCOE_VPI:
  4911. kfree(phba->vpi_bmask);
  4912. kfree(phba->vpi_ids);
  4913. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4914. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4915. &phba->lpfc_vpi_blk_list, list) {
  4916. list_del_init(&rsrc_blk->list);
  4917. kfree(rsrc_blk);
  4918. }
  4919. break;
  4920. case LPFC_RSC_TYPE_FCOE_XRI:
  4921. kfree(phba->sli4_hba.xri_bmask);
  4922. kfree(phba->sli4_hba.xri_ids);
  4923. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4924. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4925. list_del_init(&rsrc_blk->list);
  4926. kfree(rsrc_blk);
  4927. }
  4928. break;
  4929. case LPFC_RSC_TYPE_FCOE_VFI:
  4930. kfree(phba->sli4_hba.vfi_bmask);
  4931. kfree(phba->sli4_hba.vfi_ids);
  4932. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4933. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4934. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4935. list_del_init(&rsrc_blk->list);
  4936. kfree(rsrc_blk);
  4937. }
  4938. break;
  4939. case LPFC_RSC_TYPE_FCOE_RPI:
  4940. /* RPI bitmask and physical id array are cleaned up earlier. */
  4941. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4942. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4943. list_del_init(&rsrc_blk->list);
  4944. kfree(rsrc_blk);
  4945. }
  4946. break;
  4947. default:
  4948. break;
  4949. }
  4950. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4951. out_free_mbox:
  4952. mempool_free(mbox, phba->mbox_mem_pool);
  4953. return rc;
  4954. }
  4955. /**
  4956. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4957. * @phba: Pointer to HBA context object.
  4958. *
  4959. * This function allocates all SLI4 resource identifiers.
  4960. **/
  4961. int
  4962. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4963. {
  4964. int i, rc, error = 0;
  4965. uint16_t count, base;
  4966. unsigned long longs;
  4967. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4968. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4969. if (phba->sli4_hba.extents_in_use) {
  4970. /*
  4971. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4972. * resource extent count must be read and allocated before
  4973. * provisioning the resource id arrays.
  4974. */
  4975. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4976. LPFC_IDX_RSRC_RDY) {
  4977. /*
  4978. * Extent-based resources are set - the driver could
  4979. * be in a port reset. Figure out if any corrective
  4980. * actions need to be taken.
  4981. */
  4982. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4983. LPFC_RSC_TYPE_FCOE_VFI);
  4984. if (rc != 0)
  4985. error++;
  4986. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4987. LPFC_RSC_TYPE_FCOE_VPI);
  4988. if (rc != 0)
  4989. error++;
  4990. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4991. LPFC_RSC_TYPE_FCOE_XRI);
  4992. if (rc != 0)
  4993. error++;
  4994. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4995. LPFC_RSC_TYPE_FCOE_RPI);
  4996. if (rc != 0)
  4997. error++;
  4998. /*
  4999. * It's possible that the number of resources
  5000. * provided to this port instance changed between
  5001. * resets. Detect this condition and reallocate
  5002. * resources. Otherwise, there is no action.
  5003. */
  5004. if (error) {
  5005. lpfc_printf_log(phba, KERN_INFO,
  5006. LOG_MBOX | LOG_INIT,
  5007. "2931 Detected extent resource "
  5008. "change. Reallocating all "
  5009. "extents.\n");
  5010. rc = lpfc_sli4_dealloc_extent(phba,
  5011. LPFC_RSC_TYPE_FCOE_VFI);
  5012. rc = lpfc_sli4_dealloc_extent(phba,
  5013. LPFC_RSC_TYPE_FCOE_VPI);
  5014. rc = lpfc_sli4_dealloc_extent(phba,
  5015. LPFC_RSC_TYPE_FCOE_XRI);
  5016. rc = lpfc_sli4_dealloc_extent(phba,
  5017. LPFC_RSC_TYPE_FCOE_RPI);
  5018. } else
  5019. return 0;
  5020. }
  5021. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5022. if (unlikely(rc))
  5023. goto err_exit;
  5024. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5025. if (unlikely(rc))
  5026. goto err_exit;
  5027. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5028. if (unlikely(rc))
  5029. goto err_exit;
  5030. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5031. if (unlikely(rc))
  5032. goto err_exit;
  5033. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5034. LPFC_IDX_RSRC_RDY);
  5035. return rc;
  5036. } else {
  5037. /*
  5038. * The port does not support resource extents. The XRI, VPI,
  5039. * VFI, RPI resource ids were determined from READ_CONFIG.
  5040. * Just allocate the bitmasks and provision the resource id
  5041. * arrays. If a port reset is active, the resources don't
  5042. * need any action - just exit.
  5043. */
  5044. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5045. LPFC_IDX_RSRC_RDY) {
  5046. lpfc_sli4_dealloc_resource_identifiers(phba);
  5047. lpfc_sli4_remove_rpis(phba);
  5048. }
  5049. /* RPIs. */
  5050. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5051. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5052. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5053. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5054. sizeof(unsigned long),
  5055. GFP_KERNEL);
  5056. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5057. rc = -ENOMEM;
  5058. goto err_exit;
  5059. }
  5060. phba->sli4_hba.rpi_ids = kzalloc(count *
  5061. sizeof(uint16_t),
  5062. GFP_KERNEL);
  5063. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5064. rc = -ENOMEM;
  5065. goto free_rpi_bmask;
  5066. }
  5067. for (i = 0; i < count; i++)
  5068. phba->sli4_hba.rpi_ids[i] = base + i;
  5069. /* VPIs. */
  5070. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5071. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5072. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5073. phba->vpi_bmask = kzalloc(longs *
  5074. sizeof(unsigned long),
  5075. GFP_KERNEL);
  5076. if (unlikely(!phba->vpi_bmask)) {
  5077. rc = -ENOMEM;
  5078. goto free_rpi_ids;
  5079. }
  5080. phba->vpi_ids = kzalloc(count *
  5081. sizeof(uint16_t),
  5082. GFP_KERNEL);
  5083. if (unlikely(!phba->vpi_ids)) {
  5084. rc = -ENOMEM;
  5085. goto free_vpi_bmask;
  5086. }
  5087. for (i = 0; i < count; i++)
  5088. phba->vpi_ids[i] = base + i;
  5089. /* XRIs. */
  5090. count = phba->sli4_hba.max_cfg_param.max_xri;
  5091. base = phba->sli4_hba.max_cfg_param.xri_base;
  5092. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5093. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5094. sizeof(unsigned long),
  5095. GFP_KERNEL);
  5096. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5097. rc = -ENOMEM;
  5098. goto free_vpi_ids;
  5099. }
  5100. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5101. phba->sli4_hba.xri_ids = kzalloc(count *
  5102. sizeof(uint16_t),
  5103. GFP_KERNEL);
  5104. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5105. rc = -ENOMEM;
  5106. goto free_xri_bmask;
  5107. }
  5108. for (i = 0; i < count; i++)
  5109. phba->sli4_hba.xri_ids[i] = base + i;
  5110. /* VFIs. */
  5111. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5112. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5113. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5114. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5115. sizeof(unsigned long),
  5116. GFP_KERNEL);
  5117. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5118. rc = -ENOMEM;
  5119. goto free_xri_ids;
  5120. }
  5121. phba->sli4_hba.vfi_ids = kzalloc(count *
  5122. sizeof(uint16_t),
  5123. GFP_KERNEL);
  5124. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5125. rc = -ENOMEM;
  5126. goto free_vfi_bmask;
  5127. }
  5128. for (i = 0; i < count; i++)
  5129. phba->sli4_hba.vfi_ids[i] = base + i;
  5130. /*
  5131. * Mark all resources ready. An HBA reset doesn't need
  5132. * to reset the initialization.
  5133. */
  5134. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5135. LPFC_IDX_RSRC_RDY);
  5136. return 0;
  5137. }
  5138. free_vfi_bmask:
  5139. kfree(phba->sli4_hba.vfi_bmask);
  5140. free_xri_ids:
  5141. kfree(phba->sli4_hba.xri_ids);
  5142. free_xri_bmask:
  5143. kfree(phba->sli4_hba.xri_bmask);
  5144. free_vpi_ids:
  5145. kfree(phba->vpi_ids);
  5146. free_vpi_bmask:
  5147. kfree(phba->vpi_bmask);
  5148. free_rpi_ids:
  5149. kfree(phba->sli4_hba.rpi_ids);
  5150. free_rpi_bmask:
  5151. kfree(phba->sli4_hba.rpi_bmask);
  5152. err_exit:
  5153. return rc;
  5154. }
  5155. /**
  5156. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5157. * @phba: Pointer to HBA context object.
  5158. *
  5159. * This function allocates the number of elements for the specified
  5160. * resource type.
  5161. **/
  5162. int
  5163. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5164. {
  5165. if (phba->sli4_hba.extents_in_use) {
  5166. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5167. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5168. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5169. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5170. } else {
  5171. kfree(phba->vpi_bmask);
  5172. kfree(phba->vpi_ids);
  5173. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5174. kfree(phba->sli4_hba.xri_bmask);
  5175. kfree(phba->sli4_hba.xri_ids);
  5176. kfree(phba->sli4_hba.vfi_bmask);
  5177. kfree(phba->sli4_hba.vfi_ids);
  5178. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5179. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5180. }
  5181. return 0;
  5182. }
  5183. /**
  5184. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5185. * @phba: Pointer to HBA context object.
  5186. * @type: The resource extent type.
  5187. * @extnt_count: buffer to hold port extent count response
  5188. * @extnt_size: buffer to hold port extent size response.
  5189. *
  5190. * This function calls the port to read the host allocated extents
  5191. * for a particular type.
  5192. **/
  5193. int
  5194. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5195. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5196. {
  5197. bool emb;
  5198. int rc = 0;
  5199. uint16_t curr_blks = 0;
  5200. uint32_t req_len, emb_len;
  5201. uint32_t alloc_len, mbox_tmo;
  5202. struct list_head *blk_list_head;
  5203. struct lpfc_rsrc_blks *rsrc_blk;
  5204. LPFC_MBOXQ_t *mbox;
  5205. void *virtaddr = NULL;
  5206. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5207. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5208. union lpfc_sli4_cfg_shdr *shdr;
  5209. switch (type) {
  5210. case LPFC_RSC_TYPE_FCOE_VPI:
  5211. blk_list_head = &phba->lpfc_vpi_blk_list;
  5212. break;
  5213. case LPFC_RSC_TYPE_FCOE_XRI:
  5214. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5215. break;
  5216. case LPFC_RSC_TYPE_FCOE_VFI:
  5217. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5218. break;
  5219. case LPFC_RSC_TYPE_FCOE_RPI:
  5220. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5221. break;
  5222. default:
  5223. return -EIO;
  5224. }
  5225. /* Count the number of extents currently allocatd for this type. */
  5226. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5227. if (curr_blks == 0) {
  5228. /*
  5229. * The GET_ALLOCATED mailbox does not return the size,
  5230. * just the count. The size should be just the size
  5231. * stored in the current allocated block and all sizes
  5232. * for an extent type are the same so set the return
  5233. * value now.
  5234. */
  5235. *extnt_size = rsrc_blk->rsrc_size;
  5236. }
  5237. curr_blks++;
  5238. }
  5239. /* Calculate the total requested length of the dma memory. */
  5240. req_len = curr_blks * sizeof(uint16_t);
  5241. /*
  5242. * Calculate the size of an embedded mailbox. The uint32_t
  5243. * accounts for extents-specific word.
  5244. */
  5245. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5246. sizeof(uint32_t);
  5247. /*
  5248. * Presume the allocation and response will fit into an embedded
  5249. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5250. */
  5251. emb = LPFC_SLI4_MBX_EMBED;
  5252. req_len = emb_len;
  5253. if (req_len > emb_len) {
  5254. req_len = curr_blks * sizeof(uint16_t) +
  5255. sizeof(union lpfc_sli4_cfg_shdr) +
  5256. sizeof(uint32_t);
  5257. emb = LPFC_SLI4_MBX_NEMBED;
  5258. }
  5259. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5260. if (!mbox)
  5261. return -ENOMEM;
  5262. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5263. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5264. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5265. req_len, emb);
  5266. if (alloc_len < req_len) {
  5267. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5268. "2983 Allocated DMA memory size (x%x) is "
  5269. "less than the requested DMA memory "
  5270. "size (x%x)\n", alloc_len, req_len);
  5271. rc = -ENOMEM;
  5272. goto err_exit;
  5273. }
  5274. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5275. if (unlikely(rc)) {
  5276. rc = -EIO;
  5277. goto err_exit;
  5278. }
  5279. if (!phba->sli4_hba.intr_enable)
  5280. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5281. else {
  5282. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5283. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5284. }
  5285. if (unlikely(rc)) {
  5286. rc = -EIO;
  5287. goto err_exit;
  5288. }
  5289. /*
  5290. * Figure out where the response is located. Then get local pointers
  5291. * to the response data. The port does not guarantee to respond to
  5292. * all extents counts request so update the local variable with the
  5293. * allocated count from the port.
  5294. */
  5295. if (emb == LPFC_SLI4_MBX_EMBED) {
  5296. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5297. shdr = &rsrc_ext->header.cfg_shdr;
  5298. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5299. } else {
  5300. virtaddr = mbox->sge_array->addr[0];
  5301. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5302. shdr = &n_rsrc->cfg_shdr;
  5303. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5304. }
  5305. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5306. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5307. "2984 Failed to read allocated resources "
  5308. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5309. type,
  5310. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5311. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5312. rc = -EIO;
  5313. goto err_exit;
  5314. }
  5315. err_exit:
  5316. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5317. return rc;
  5318. }
  5319. /**
  5320. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5321. * @phba: pointer to lpfc hba data structure.
  5322. *
  5323. * This routine walks the list of els buffers that have been allocated and
  5324. * repost them to the port by using SGL block post. This is needed after a
  5325. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5326. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5327. * SGL block post mailbox commands to post them to the port. For single els
  5328. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5329. * mailbox command for posting.
  5330. *
  5331. * Returns: 0 = success, non-zero failure.
  5332. **/
  5333. static int
  5334. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5335. {
  5336. struct lpfc_sglq *sglq_entry = NULL;
  5337. struct lpfc_sglq *sglq_entry_next = NULL;
  5338. struct lpfc_sglq *sglq_entry_first = NULL;
  5339. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5340. int last_xritag = NO_XRI;
  5341. LIST_HEAD(prep_sgl_list);
  5342. LIST_HEAD(blck_sgl_list);
  5343. LIST_HEAD(allc_sgl_list);
  5344. LIST_HEAD(post_sgl_list);
  5345. LIST_HEAD(free_sgl_list);
  5346. spin_lock(&phba->hbalock);
  5347. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5348. spin_unlock(&phba->hbalock);
  5349. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5350. &allc_sgl_list, list) {
  5351. list_del_init(&sglq_entry->list);
  5352. block_cnt++;
  5353. if ((last_xritag != NO_XRI) &&
  5354. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5355. /* a hole in xri block, form a sgl posting block */
  5356. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5357. post_cnt = block_cnt - 1;
  5358. /* prepare list for next posting block */
  5359. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5360. block_cnt = 1;
  5361. } else {
  5362. /* prepare list for next posting block */
  5363. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5364. /* enough sgls for non-embed sgl mbox command */
  5365. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5366. list_splice_init(&prep_sgl_list,
  5367. &blck_sgl_list);
  5368. post_cnt = block_cnt;
  5369. block_cnt = 0;
  5370. }
  5371. }
  5372. num_posted++;
  5373. /* keep track of last sgl's xritag */
  5374. last_xritag = sglq_entry->sli4_xritag;
  5375. /* end of repost sgl list condition for els buffers */
  5376. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5377. if (post_cnt == 0) {
  5378. list_splice_init(&prep_sgl_list,
  5379. &blck_sgl_list);
  5380. post_cnt = block_cnt;
  5381. } else if (block_cnt == 1) {
  5382. status = lpfc_sli4_post_sgl(phba,
  5383. sglq_entry->phys, 0,
  5384. sglq_entry->sli4_xritag);
  5385. if (!status) {
  5386. /* successful, put sgl to posted list */
  5387. list_add_tail(&sglq_entry->list,
  5388. &post_sgl_list);
  5389. } else {
  5390. /* Failure, put sgl to free list */
  5391. lpfc_printf_log(phba, KERN_WARNING,
  5392. LOG_SLI,
  5393. "3159 Failed to post els "
  5394. "sgl, xritag:x%x\n",
  5395. sglq_entry->sli4_xritag);
  5396. list_add_tail(&sglq_entry->list,
  5397. &free_sgl_list);
  5398. spin_lock_irq(&phba->hbalock);
  5399. phba->sli4_hba.els_xri_cnt--;
  5400. spin_unlock_irq(&phba->hbalock);
  5401. }
  5402. }
  5403. }
  5404. /* continue until a nembed page worth of sgls */
  5405. if (post_cnt == 0)
  5406. continue;
  5407. /* post the els buffer list sgls as a block */
  5408. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5409. post_cnt);
  5410. if (!status) {
  5411. /* success, put sgl list to posted sgl list */
  5412. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5413. } else {
  5414. /* Failure, put sgl list to free sgl list */
  5415. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5416. struct lpfc_sglq,
  5417. list);
  5418. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5419. "3160 Failed to post els sgl-list, "
  5420. "xritag:x%x-x%x\n",
  5421. sglq_entry_first->sli4_xritag,
  5422. (sglq_entry_first->sli4_xritag +
  5423. post_cnt - 1));
  5424. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5425. spin_lock_irq(&phba->hbalock);
  5426. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5427. spin_unlock_irq(&phba->hbalock);
  5428. }
  5429. /* don't reset xirtag due to hole in xri block */
  5430. if (block_cnt == 0)
  5431. last_xritag = NO_XRI;
  5432. /* reset els sgl post count for next round of posting */
  5433. post_cnt = 0;
  5434. }
  5435. /* free the els sgls failed to post */
  5436. lpfc_free_sgl_list(phba, &free_sgl_list);
  5437. /* push els sgls posted to the availble list */
  5438. if (!list_empty(&post_sgl_list)) {
  5439. spin_lock(&phba->hbalock);
  5440. list_splice_init(&post_sgl_list,
  5441. &phba->sli4_hba.lpfc_sgl_list);
  5442. spin_unlock(&phba->hbalock);
  5443. } else {
  5444. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5445. "3161 Failure to post els sgl to port.\n");
  5446. return -EIO;
  5447. }
  5448. return 0;
  5449. }
  5450. /**
  5451. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5452. * @phba: Pointer to HBA context object.
  5453. *
  5454. * This function is the main SLI4 device intialization PCI function. This
  5455. * function is called by the HBA intialization code, HBA reset code and
  5456. * HBA error attention handler code. Caller is not required to hold any
  5457. * locks.
  5458. **/
  5459. int
  5460. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5461. {
  5462. int rc;
  5463. LPFC_MBOXQ_t *mboxq;
  5464. struct lpfc_mqe *mqe;
  5465. uint8_t *vpd;
  5466. uint32_t vpd_size;
  5467. uint32_t ftr_rsp = 0;
  5468. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5469. struct lpfc_vport *vport = phba->pport;
  5470. struct lpfc_dmabuf *mp;
  5471. /* Perform a PCI function reset to start from clean */
  5472. rc = lpfc_pci_function_reset(phba);
  5473. if (unlikely(rc))
  5474. return -ENODEV;
  5475. /* Check the HBA Host Status Register for readyness */
  5476. rc = lpfc_sli4_post_status_check(phba);
  5477. if (unlikely(rc))
  5478. return -ENODEV;
  5479. else {
  5480. spin_lock_irq(&phba->hbalock);
  5481. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5482. spin_unlock_irq(&phba->hbalock);
  5483. }
  5484. /*
  5485. * Allocate a single mailbox container for initializing the
  5486. * port.
  5487. */
  5488. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5489. if (!mboxq)
  5490. return -ENOMEM;
  5491. /* Issue READ_REV to collect vpd and FW information. */
  5492. vpd_size = SLI4_PAGE_SIZE;
  5493. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5494. if (!vpd) {
  5495. rc = -ENOMEM;
  5496. goto out_free_mbox;
  5497. }
  5498. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5499. if (unlikely(rc)) {
  5500. kfree(vpd);
  5501. goto out_free_mbox;
  5502. }
  5503. mqe = &mboxq->u.mqe;
  5504. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5505. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5506. phba->hba_flag |= HBA_FCOE_MODE;
  5507. else
  5508. phba->hba_flag &= ~HBA_FCOE_MODE;
  5509. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5510. LPFC_DCBX_CEE_MODE)
  5511. phba->hba_flag |= HBA_FIP_SUPPORT;
  5512. else
  5513. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5514. if (phba->sli_rev != LPFC_SLI_REV4) {
  5515. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5516. "0376 READ_REV Error. SLI Level %d "
  5517. "FCoE enabled %d\n",
  5518. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5519. rc = -EIO;
  5520. kfree(vpd);
  5521. goto out_free_mbox;
  5522. }
  5523. /*
  5524. * Continue initialization with default values even if driver failed
  5525. * to read FCoE param config regions, only read parameters if the
  5526. * board is FCoE
  5527. */
  5528. if (phba->hba_flag & HBA_FCOE_MODE &&
  5529. lpfc_sli4_read_fcoe_params(phba))
  5530. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5531. "2570 Failed to read FCoE parameters\n");
  5532. /*
  5533. * Retrieve sli4 device physical port name, failure of doing it
  5534. * is considered as non-fatal.
  5535. */
  5536. rc = lpfc_sli4_retrieve_pport_name(phba);
  5537. if (!rc)
  5538. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5539. "3080 Successful retrieving SLI4 device "
  5540. "physical port name: %s.\n", phba->Port);
  5541. /*
  5542. * Evaluate the read rev and vpd data. Populate the driver
  5543. * state with the results. If this routine fails, the failure
  5544. * is not fatal as the driver will use generic values.
  5545. */
  5546. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5547. if (unlikely(!rc)) {
  5548. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5549. "0377 Error %d parsing vpd. "
  5550. "Using defaults.\n", rc);
  5551. rc = 0;
  5552. }
  5553. kfree(vpd);
  5554. /* Save information as VPD data */
  5555. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5556. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5557. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5558. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5559. &mqe->un.read_rev);
  5560. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5561. &mqe->un.read_rev);
  5562. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5563. &mqe->un.read_rev);
  5564. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5565. &mqe->un.read_rev);
  5566. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5567. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5568. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5569. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5570. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5571. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5572. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5573. "(%d):0380 READ_REV Status x%x "
  5574. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5575. mboxq->vport ? mboxq->vport->vpi : 0,
  5576. bf_get(lpfc_mqe_status, mqe),
  5577. phba->vpd.rev.opFwName,
  5578. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5579. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5580. /*
  5581. * Discover the port's supported feature set and match it against the
  5582. * hosts requests.
  5583. */
  5584. lpfc_request_features(phba, mboxq);
  5585. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5586. if (unlikely(rc)) {
  5587. rc = -EIO;
  5588. goto out_free_mbox;
  5589. }
  5590. /*
  5591. * The port must support FCP initiator mode as this is the
  5592. * only mode running in the host.
  5593. */
  5594. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5595. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5596. "0378 No support for fcpi mode.\n");
  5597. ftr_rsp++;
  5598. }
  5599. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5600. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5601. else
  5602. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5603. /*
  5604. * If the port cannot support the host's requested features
  5605. * then turn off the global config parameters to disable the
  5606. * feature in the driver. This is not a fatal error.
  5607. */
  5608. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5609. if (phba->cfg_enable_bg) {
  5610. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5611. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5612. else
  5613. ftr_rsp++;
  5614. }
  5615. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5616. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5617. ftr_rsp++;
  5618. if (ftr_rsp) {
  5619. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5620. "0379 Feature Mismatch Data: x%08x %08x "
  5621. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5622. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5623. phba->cfg_enable_npiv, phba->max_vpi);
  5624. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5625. phba->cfg_enable_bg = 0;
  5626. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5627. phba->cfg_enable_npiv = 0;
  5628. }
  5629. /* These SLI3 features are assumed in SLI4 */
  5630. spin_lock_irq(&phba->hbalock);
  5631. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5632. spin_unlock_irq(&phba->hbalock);
  5633. /*
  5634. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5635. * calls depends on these resources to complete port setup.
  5636. */
  5637. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5638. if (rc) {
  5639. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5640. "2920 Failed to alloc Resource IDs "
  5641. "rc = x%x\n", rc);
  5642. goto out_free_mbox;
  5643. }
  5644. /* Read the port's service parameters. */
  5645. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5646. if (rc) {
  5647. phba->link_state = LPFC_HBA_ERROR;
  5648. rc = -ENOMEM;
  5649. goto out_free_mbox;
  5650. }
  5651. mboxq->vport = vport;
  5652. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5653. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5654. if (rc == MBX_SUCCESS) {
  5655. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5656. rc = 0;
  5657. }
  5658. /*
  5659. * This memory was allocated by the lpfc_read_sparam routine. Release
  5660. * it to the mbuf pool.
  5661. */
  5662. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5663. kfree(mp);
  5664. mboxq->context1 = NULL;
  5665. if (unlikely(rc)) {
  5666. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5667. "0382 READ_SPARAM command failed "
  5668. "status %d, mbxStatus x%x\n",
  5669. rc, bf_get(lpfc_mqe_status, mqe));
  5670. phba->link_state = LPFC_HBA_ERROR;
  5671. rc = -EIO;
  5672. goto out_free_mbox;
  5673. }
  5674. lpfc_update_vport_wwn(vport);
  5675. /* Update the fc_host data structures with new wwn. */
  5676. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5677. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5678. /* update host els and scsi xri-sgl sizes and mappings */
  5679. rc = lpfc_sli4_xri_sgl_update(phba);
  5680. if (unlikely(rc)) {
  5681. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5682. "1400 Failed to update xri-sgl size and "
  5683. "mapping: %d\n", rc);
  5684. goto out_free_mbox;
  5685. }
  5686. /* register the els sgl pool to the port */
  5687. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5688. if (unlikely(rc)) {
  5689. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5690. "0582 Error %d during els sgl post "
  5691. "operation\n", rc);
  5692. rc = -ENODEV;
  5693. goto out_free_mbox;
  5694. }
  5695. /* register the allocated scsi sgl pool to the port */
  5696. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5697. if (unlikely(rc)) {
  5698. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5699. "0383 Error %d during scsi sgl post "
  5700. "operation\n", rc);
  5701. /* Some Scsi buffers were moved to the abort scsi list */
  5702. /* A pci function reset will repost them */
  5703. rc = -ENODEV;
  5704. goto out_free_mbox;
  5705. }
  5706. /* Post the rpi header region to the device. */
  5707. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5708. if (unlikely(rc)) {
  5709. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5710. "0393 Error %d during rpi post operation\n",
  5711. rc);
  5712. rc = -ENODEV;
  5713. goto out_free_mbox;
  5714. }
  5715. lpfc_sli4_node_prep(phba);
  5716. /* Create all the SLI4 queues */
  5717. rc = lpfc_sli4_queue_create(phba);
  5718. if (rc) {
  5719. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5720. "3089 Failed to allocate queues\n");
  5721. rc = -ENODEV;
  5722. goto out_stop_timers;
  5723. }
  5724. /* Set up all the queues to the device */
  5725. rc = lpfc_sli4_queue_setup(phba);
  5726. if (unlikely(rc)) {
  5727. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5728. "0381 Error %d during queue setup.\n ", rc);
  5729. goto out_destroy_queue;
  5730. }
  5731. /* Arm the CQs and then EQs on device */
  5732. lpfc_sli4_arm_cqeq_intr(phba);
  5733. /* Indicate device interrupt mode */
  5734. phba->sli4_hba.intr_enable = 1;
  5735. /* Allow asynchronous mailbox command to go through */
  5736. spin_lock_irq(&phba->hbalock);
  5737. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5738. spin_unlock_irq(&phba->hbalock);
  5739. /* Post receive buffers to the device */
  5740. lpfc_sli4_rb_setup(phba);
  5741. /* Reset HBA FCF states after HBA reset */
  5742. phba->fcf.fcf_flag = 0;
  5743. phba->fcf.current_rec.flag = 0;
  5744. /* Start the ELS watchdog timer */
  5745. mod_timer(&vport->els_tmofunc,
  5746. jiffies + HZ * (phba->fc_ratov * 2));
  5747. /* Start heart beat timer */
  5748. mod_timer(&phba->hb_tmofunc,
  5749. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5750. phba->hb_outstanding = 0;
  5751. phba->last_completion_time = jiffies;
  5752. /* Start error attention (ERATT) polling timer */
  5753. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5754. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5755. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5756. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5757. if (!rc) {
  5758. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5759. "2829 This device supports "
  5760. "Advanced Error Reporting (AER)\n");
  5761. spin_lock_irq(&phba->hbalock);
  5762. phba->hba_flag |= HBA_AER_ENABLED;
  5763. spin_unlock_irq(&phba->hbalock);
  5764. } else {
  5765. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5766. "2830 This device does not support "
  5767. "Advanced Error Reporting (AER)\n");
  5768. phba->cfg_aer_support = 0;
  5769. }
  5770. rc = 0;
  5771. }
  5772. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5773. /*
  5774. * The FC Port needs to register FCFI (index 0)
  5775. */
  5776. lpfc_reg_fcfi(phba, mboxq);
  5777. mboxq->vport = phba->pport;
  5778. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5779. if (rc != MBX_SUCCESS)
  5780. goto out_unset_queue;
  5781. rc = 0;
  5782. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5783. &mboxq->u.mqe.un.reg_fcfi);
  5784. /* Check if the port is configured to be disabled */
  5785. lpfc_sli_read_link_ste(phba);
  5786. }
  5787. /*
  5788. * The port is ready, set the host's link state to LINK_DOWN
  5789. * in preparation for link interrupts.
  5790. */
  5791. spin_lock_irq(&phba->hbalock);
  5792. phba->link_state = LPFC_LINK_DOWN;
  5793. spin_unlock_irq(&phba->hbalock);
  5794. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5795. (phba->hba_flag & LINK_DISABLED)) {
  5796. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5797. "3103 Adapter Link is disabled.\n");
  5798. lpfc_down_link(phba, mboxq);
  5799. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5800. if (rc != MBX_SUCCESS) {
  5801. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5802. "3104 Adapter failed to issue "
  5803. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5804. goto out_unset_queue;
  5805. }
  5806. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5807. /* don't perform init_link on SLI4 FC port loopback test */
  5808. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5809. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5810. if (rc)
  5811. goto out_unset_queue;
  5812. }
  5813. }
  5814. mempool_free(mboxq, phba->mbox_mem_pool);
  5815. return rc;
  5816. out_unset_queue:
  5817. /* Unset all the queues set up in this routine when error out */
  5818. lpfc_sli4_queue_unset(phba);
  5819. out_destroy_queue:
  5820. lpfc_sli4_queue_destroy(phba);
  5821. out_stop_timers:
  5822. lpfc_stop_hba_timers(phba);
  5823. out_free_mbox:
  5824. mempool_free(mboxq, phba->mbox_mem_pool);
  5825. return rc;
  5826. }
  5827. /**
  5828. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5829. * @ptr: context object - pointer to hba structure.
  5830. *
  5831. * This is the callback function for mailbox timer. The mailbox
  5832. * timer is armed when a new mailbox command is issued and the timer
  5833. * is deleted when the mailbox complete. The function is called by
  5834. * the kernel timer code when a mailbox does not complete within
  5835. * expected time. This function wakes up the worker thread to
  5836. * process the mailbox timeout and returns. All the processing is
  5837. * done by the worker thread function lpfc_mbox_timeout_handler.
  5838. **/
  5839. void
  5840. lpfc_mbox_timeout(unsigned long ptr)
  5841. {
  5842. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5843. unsigned long iflag;
  5844. uint32_t tmo_posted;
  5845. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5846. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5847. if (!tmo_posted)
  5848. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5849. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5850. if (!tmo_posted)
  5851. lpfc_worker_wake_up(phba);
  5852. return;
  5853. }
  5854. /**
  5855. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5856. * @phba: Pointer to HBA context object.
  5857. *
  5858. * This function is called from worker thread when a mailbox command times out.
  5859. * The caller is not required to hold any locks. This function will reset the
  5860. * HBA and recover all the pending commands.
  5861. **/
  5862. void
  5863. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5864. {
  5865. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5866. MAILBOX_t *mb = &pmbox->u.mb;
  5867. struct lpfc_sli *psli = &phba->sli;
  5868. struct lpfc_sli_ring *pring;
  5869. /* Check the pmbox pointer first. There is a race condition
  5870. * between the mbox timeout handler getting executed in the
  5871. * worklist and the mailbox actually completing. When this
  5872. * race condition occurs, the mbox_active will be NULL.
  5873. */
  5874. spin_lock_irq(&phba->hbalock);
  5875. if (pmbox == NULL) {
  5876. lpfc_printf_log(phba, KERN_WARNING,
  5877. LOG_MBOX | LOG_SLI,
  5878. "0353 Active Mailbox cleared - mailbox timeout "
  5879. "exiting\n");
  5880. spin_unlock_irq(&phba->hbalock);
  5881. return;
  5882. }
  5883. /* Mbox cmd <mbxCommand> timeout */
  5884. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5885. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5886. mb->mbxCommand,
  5887. phba->pport->port_state,
  5888. phba->sli.sli_flag,
  5889. phba->sli.mbox_active);
  5890. spin_unlock_irq(&phba->hbalock);
  5891. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5892. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5893. * it to fail all outstanding SCSI IO.
  5894. */
  5895. spin_lock_irq(&phba->pport->work_port_lock);
  5896. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5897. spin_unlock_irq(&phba->pport->work_port_lock);
  5898. spin_lock_irq(&phba->hbalock);
  5899. phba->link_state = LPFC_LINK_UNKNOWN;
  5900. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5901. spin_unlock_irq(&phba->hbalock);
  5902. pring = &psli->ring[psli->fcp_ring];
  5903. lpfc_sli_abort_iocb_ring(phba, pring);
  5904. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5905. "0345 Resetting board due to mailbox timeout\n");
  5906. /* Reset the HBA device */
  5907. lpfc_reset_hba(phba);
  5908. }
  5909. /**
  5910. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5911. * @phba: Pointer to HBA context object.
  5912. * @pmbox: Pointer to mailbox object.
  5913. * @flag: Flag indicating how the mailbox need to be processed.
  5914. *
  5915. * This function is called by discovery code and HBA management code
  5916. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5917. * function gets the hbalock to protect the data structures.
  5918. * The mailbox command can be submitted in polling mode, in which case
  5919. * this function will wait in a polling loop for the completion of the
  5920. * mailbox.
  5921. * If the mailbox is submitted in no_wait mode (not polling) the
  5922. * function will submit the command and returns immediately without waiting
  5923. * for the mailbox completion. The no_wait is supported only when HBA
  5924. * is in SLI2/SLI3 mode - interrupts are enabled.
  5925. * The SLI interface allows only one mailbox pending at a time. If the
  5926. * mailbox is issued in polling mode and there is already a mailbox
  5927. * pending, then the function will return an error. If the mailbox is issued
  5928. * in NO_WAIT mode and there is a mailbox pending already, the function
  5929. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5930. * The sli layer owns the mailbox object until the completion of mailbox
  5931. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5932. * return codes the caller owns the mailbox command after the return of
  5933. * the function.
  5934. **/
  5935. static int
  5936. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5937. uint32_t flag)
  5938. {
  5939. MAILBOX_t *mb;
  5940. struct lpfc_sli *psli = &phba->sli;
  5941. uint32_t status, evtctr;
  5942. uint32_t ha_copy, hc_copy;
  5943. int i;
  5944. unsigned long timeout;
  5945. unsigned long drvr_flag = 0;
  5946. uint32_t word0, ldata;
  5947. void __iomem *to_slim;
  5948. int processing_queue = 0;
  5949. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5950. if (!pmbox) {
  5951. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5952. /* processing mbox queue from intr_handler */
  5953. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5954. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5955. return MBX_SUCCESS;
  5956. }
  5957. processing_queue = 1;
  5958. pmbox = lpfc_mbox_get(phba);
  5959. if (!pmbox) {
  5960. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5961. return MBX_SUCCESS;
  5962. }
  5963. }
  5964. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5965. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5966. if(!pmbox->vport) {
  5967. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5968. lpfc_printf_log(phba, KERN_ERR,
  5969. LOG_MBOX | LOG_VPORT,
  5970. "1806 Mbox x%x failed. No vport\n",
  5971. pmbox->u.mb.mbxCommand);
  5972. dump_stack();
  5973. goto out_not_finished;
  5974. }
  5975. }
  5976. /* If the PCI channel is in offline state, do not post mbox. */
  5977. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5978. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5979. goto out_not_finished;
  5980. }
  5981. /* If HBA has a deferred error attention, fail the iocb. */
  5982. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5983. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5984. goto out_not_finished;
  5985. }
  5986. psli = &phba->sli;
  5987. mb = &pmbox->u.mb;
  5988. status = MBX_SUCCESS;
  5989. if (phba->link_state == LPFC_HBA_ERROR) {
  5990. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5991. /* Mbox command <mbxCommand> cannot issue */
  5992. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5993. "(%d):0311 Mailbox command x%x cannot "
  5994. "issue Data: x%x x%x\n",
  5995. pmbox->vport ? pmbox->vport->vpi : 0,
  5996. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5997. goto out_not_finished;
  5998. }
  5999. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6000. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6001. !(hc_copy & HC_MBINT_ENA)) {
  6002. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6003. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6004. "(%d):2528 Mailbox command x%x cannot "
  6005. "issue Data: x%x x%x\n",
  6006. pmbox->vport ? pmbox->vport->vpi : 0,
  6007. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6008. goto out_not_finished;
  6009. }
  6010. }
  6011. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6012. /* Polling for a mbox command when another one is already active
  6013. * is not allowed in SLI. Also, the driver must have established
  6014. * SLI2 mode to queue and process multiple mbox commands.
  6015. */
  6016. if (flag & MBX_POLL) {
  6017. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6018. /* Mbox command <mbxCommand> cannot issue */
  6019. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6020. "(%d):2529 Mailbox command x%x "
  6021. "cannot issue Data: x%x x%x\n",
  6022. pmbox->vport ? pmbox->vport->vpi : 0,
  6023. pmbox->u.mb.mbxCommand,
  6024. psli->sli_flag, flag);
  6025. goto out_not_finished;
  6026. }
  6027. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6028. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6029. /* Mbox command <mbxCommand> cannot issue */
  6030. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6031. "(%d):2530 Mailbox command x%x "
  6032. "cannot issue Data: x%x x%x\n",
  6033. pmbox->vport ? pmbox->vport->vpi : 0,
  6034. pmbox->u.mb.mbxCommand,
  6035. psli->sli_flag, flag);
  6036. goto out_not_finished;
  6037. }
  6038. /* Another mailbox command is still being processed, queue this
  6039. * command to be processed later.
  6040. */
  6041. lpfc_mbox_put(phba, pmbox);
  6042. /* Mbox cmd issue - BUSY */
  6043. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6044. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6045. "x%x x%x x%x x%x\n",
  6046. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6047. mb->mbxCommand, phba->pport->port_state,
  6048. psli->sli_flag, flag);
  6049. psli->slistat.mbox_busy++;
  6050. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6051. if (pmbox->vport) {
  6052. lpfc_debugfs_disc_trc(pmbox->vport,
  6053. LPFC_DISC_TRC_MBOX_VPORT,
  6054. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6055. (uint32_t)mb->mbxCommand,
  6056. mb->un.varWords[0], mb->un.varWords[1]);
  6057. }
  6058. else {
  6059. lpfc_debugfs_disc_trc(phba->pport,
  6060. LPFC_DISC_TRC_MBOX,
  6061. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6062. (uint32_t)mb->mbxCommand,
  6063. mb->un.varWords[0], mb->un.varWords[1]);
  6064. }
  6065. return MBX_BUSY;
  6066. }
  6067. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6068. /* If we are not polling, we MUST be in SLI2 mode */
  6069. if (flag != MBX_POLL) {
  6070. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6071. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6072. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6073. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6074. /* Mbox command <mbxCommand> cannot issue */
  6075. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6076. "(%d):2531 Mailbox command x%x "
  6077. "cannot issue Data: x%x x%x\n",
  6078. pmbox->vport ? pmbox->vport->vpi : 0,
  6079. pmbox->u.mb.mbxCommand,
  6080. psli->sli_flag, flag);
  6081. goto out_not_finished;
  6082. }
  6083. /* timeout active mbox command */
  6084. mod_timer(&psli->mbox_tmo, (jiffies +
  6085. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6086. }
  6087. /* Mailbox cmd <cmd> issue */
  6088. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6089. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6090. "x%x\n",
  6091. pmbox->vport ? pmbox->vport->vpi : 0,
  6092. mb->mbxCommand, phba->pport->port_state,
  6093. psli->sli_flag, flag);
  6094. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6095. if (pmbox->vport) {
  6096. lpfc_debugfs_disc_trc(pmbox->vport,
  6097. LPFC_DISC_TRC_MBOX_VPORT,
  6098. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6099. (uint32_t)mb->mbxCommand,
  6100. mb->un.varWords[0], mb->un.varWords[1]);
  6101. }
  6102. else {
  6103. lpfc_debugfs_disc_trc(phba->pport,
  6104. LPFC_DISC_TRC_MBOX,
  6105. "MBOX Send: cmd:x%x mb:x%x x%x",
  6106. (uint32_t)mb->mbxCommand,
  6107. mb->un.varWords[0], mb->un.varWords[1]);
  6108. }
  6109. }
  6110. psli->slistat.mbox_cmd++;
  6111. evtctr = psli->slistat.mbox_event;
  6112. /* next set own bit for the adapter and copy over command word */
  6113. mb->mbxOwner = OWN_CHIP;
  6114. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6115. /* Populate mbox extension offset word. */
  6116. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6117. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6118. = (uint8_t *)phba->mbox_ext
  6119. - (uint8_t *)phba->mbox;
  6120. }
  6121. /* Copy the mailbox extension data */
  6122. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6123. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6124. (uint8_t *)phba->mbox_ext,
  6125. pmbox->in_ext_byte_len);
  6126. }
  6127. /* Copy command data to host SLIM area */
  6128. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6129. } else {
  6130. /* Populate mbox extension offset word. */
  6131. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6132. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6133. = MAILBOX_HBA_EXT_OFFSET;
  6134. /* Copy the mailbox extension data */
  6135. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6136. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6137. MAILBOX_HBA_EXT_OFFSET,
  6138. pmbox->context2, pmbox->in_ext_byte_len);
  6139. }
  6140. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6141. /* copy command data into host mbox for cmpl */
  6142. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6143. }
  6144. /* First copy mbox command data to HBA SLIM, skip past first
  6145. word */
  6146. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6147. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6148. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6149. /* Next copy over first word, with mbxOwner set */
  6150. ldata = *((uint32_t *)mb);
  6151. to_slim = phba->MBslimaddr;
  6152. writel(ldata, to_slim);
  6153. readl(to_slim); /* flush */
  6154. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6155. /* switch over to host mailbox */
  6156. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6157. }
  6158. }
  6159. wmb();
  6160. switch (flag) {
  6161. case MBX_NOWAIT:
  6162. /* Set up reference to mailbox command */
  6163. psli->mbox_active = pmbox;
  6164. /* Interrupt board to do it */
  6165. writel(CA_MBATT, phba->CAregaddr);
  6166. readl(phba->CAregaddr); /* flush */
  6167. /* Don't wait for it to finish, just return */
  6168. break;
  6169. case MBX_POLL:
  6170. /* Set up null reference to mailbox command */
  6171. psli->mbox_active = NULL;
  6172. /* Interrupt board to do it */
  6173. writel(CA_MBATT, phba->CAregaddr);
  6174. readl(phba->CAregaddr); /* flush */
  6175. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6176. /* First read mbox status word */
  6177. word0 = *((uint32_t *)phba->mbox);
  6178. word0 = le32_to_cpu(word0);
  6179. } else {
  6180. /* First read mbox status word */
  6181. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6182. spin_unlock_irqrestore(&phba->hbalock,
  6183. drvr_flag);
  6184. goto out_not_finished;
  6185. }
  6186. }
  6187. /* Read the HBA Host Attention Register */
  6188. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6189. spin_unlock_irqrestore(&phba->hbalock,
  6190. drvr_flag);
  6191. goto out_not_finished;
  6192. }
  6193. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6194. 1000) + jiffies;
  6195. i = 0;
  6196. /* Wait for command to complete */
  6197. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6198. (!(ha_copy & HA_MBATT) &&
  6199. (phba->link_state > LPFC_WARM_START))) {
  6200. if (time_after(jiffies, timeout)) {
  6201. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6202. spin_unlock_irqrestore(&phba->hbalock,
  6203. drvr_flag);
  6204. goto out_not_finished;
  6205. }
  6206. /* Check if we took a mbox interrupt while we were
  6207. polling */
  6208. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6209. && (evtctr != psli->slistat.mbox_event))
  6210. break;
  6211. if (i++ > 10) {
  6212. spin_unlock_irqrestore(&phba->hbalock,
  6213. drvr_flag);
  6214. msleep(1);
  6215. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6216. }
  6217. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6218. /* First copy command data */
  6219. word0 = *((uint32_t *)phba->mbox);
  6220. word0 = le32_to_cpu(word0);
  6221. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6222. MAILBOX_t *slimmb;
  6223. uint32_t slimword0;
  6224. /* Check real SLIM for any errors */
  6225. slimword0 = readl(phba->MBslimaddr);
  6226. slimmb = (MAILBOX_t *) & slimword0;
  6227. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6228. && slimmb->mbxStatus) {
  6229. psli->sli_flag &=
  6230. ~LPFC_SLI_ACTIVE;
  6231. word0 = slimword0;
  6232. }
  6233. }
  6234. } else {
  6235. /* First copy command data */
  6236. word0 = readl(phba->MBslimaddr);
  6237. }
  6238. /* Read the HBA Host Attention Register */
  6239. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6240. spin_unlock_irqrestore(&phba->hbalock,
  6241. drvr_flag);
  6242. goto out_not_finished;
  6243. }
  6244. }
  6245. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6246. /* copy results back to user */
  6247. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6248. /* Copy the mailbox extension data */
  6249. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6250. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6251. pmbox->context2,
  6252. pmbox->out_ext_byte_len);
  6253. }
  6254. } else {
  6255. /* First copy command data */
  6256. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6257. MAILBOX_CMD_SIZE);
  6258. /* Copy the mailbox extension data */
  6259. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6260. lpfc_memcpy_from_slim(pmbox->context2,
  6261. phba->MBslimaddr +
  6262. MAILBOX_HBA_EXT_OFFSET,
  6263. pmbox->out_ext_byte_len);
  6264. }
  6265. }
  6266. writel(HA_MBATT, phba->HAregaddr);
  6267. readl(phba->HAregaddr); /* flush */
  6268. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6269. status = mb->mbxStatus;
  6270. }
  6271. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6272. return status;
  6273. out_not_finished:
  6274. if (processing_queue) {
  6275. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6276. lpfc_mbox_cmpl_put(phba, pmbox);
  6277. }
  6278. return MBX_NOT_FINISHED;
  6279. }
  6280. /**
  6281. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6282. * @phba: Pointer to HBA context object.
  6283. *
  6284. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6285. * the driver internal pending mailbox queue. It will then try to wait out the
  6286. * possible outstanding mailbox command before return.
  6287. *
  6288. * Returns:
  6289. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6290. * the outstanding mailbox command timed out.
  6291. **/
  6292. static int
  6293. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6294. {
  6295. struct lpfc_sli *psli = &phba->sli;
  6296. int rc = 0;
  6297. unsigned long timeout = 0;
  6298. /* Mark the asynchronous mailbox command posting as blocked */
  6299. spin_lock_irq(&phba->hbalock);
  6300. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6301. /* Determine how long we might wait for the active mailbox
  6302. * command to be gracefully completed by firmware.
  6303. */
  6304. if (phba->sli.mbox_active)
  6305. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6306. phba->sli.mbox_active) *
  6307. 1000) + jiffies;
  6308. spin_unlock_irq(&phba->hbalock);
  6309. /* Wait for the outstnading mailbox command to complete */
  6310. while (phba->sli.mbox_active) {
  6311. /* Check active mailbox complete status every 2ms */
  6312. msleep(2);
  6313. if (time_after(jiffies, timeout)) {
  6314. /* Timeout, marked the outstanding cmd not complete */
  6315. rc = 1;
  6316. break;
  6317. }
  6318. }
  6319. /* Can not cleanly block async mailbox command, fails it */
  6320. if (rc) {
  6321. spin_lock_irq(&phba->hbalock);
  6322. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6323. spin_unlock_irq(&phba->hbalock);
  6324. }
  6325. return rc;
  6326. }
  6327. /**
  6328. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6329. * @phba: Pointer to HBA context object.
  6330. *
  6331. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6332. * commands from the driver internal pending mailbox queue. It makes sure
  6333. * that there is no outstanding mailbox command before resuming posting
  6334. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6335. * mailbox command, it will try to wait it out before resuming asynchronous
  6336. * mailbox command posting.
  6337. **/
  6338. static void
  6339. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6340. {
  6341. struct lpfc_sli *psli = &phba->sli;
  6342. spin_lock_irq(&phba->hbalock);
  6343. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6344. /* Asynchronous mailbox posting is not blocked, do nothing */
  6345. spin_unlock_irq(&phba->hbalock);
  6346. return;
  6347. }
  6348. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6349. * successful or timeout, after timing-out the outstanding mailbox
  6350. * command shall always be removed, so just unblock posting async
  6351. * mailbox command and resume
  6352. */
  6353. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6354. spin_unlock_irq(&phba->hbalock);
  6355. /* wake up worker thread to post asynchronlous mailbox command */
  6356. lpfc_worker_wake_up(phba);
  6357. }
  6358. /**
  6359. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6360. * @phba: Pointer to HBA context object.
  6361. * @mboxq: Pointer to mailbox object.
  6362. *
  6363. * The function posts a mailbox to the port. The mailbox is expected
  6364. * to be comletely filled in and ready for the port to operate on it.
  6365. * This routine executes a synchronous completion operation on the
  6366. * mailbox by polling for its completion.
  6367. *
  6368. * The caller must not be holding any locks when calling this routine.
  6369. *
  6370. * Returns:
  6371. * MBX_SUCCESS - mailbox posted successfully
  6372. * Any of the MBX error values.
  6373. **/
  6374. static int
  6375. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6376. {
  6377. int rc = MBX_SUCCESS;
  6378. unsigned long iflag;
  6379. uint32_t db_ready;
  6380. uint32_t mcqe_status;
  6381. uint32_t mbx_cmnd;
  6382. unsigned long timeout;
  6383. struct lpfc_sli *psli = &phba->sli;
  6384. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6385. struct lpfc_bmbx_create *mbox_rgn;
  6386. struct dma_address *dma_address;
  6387. struct lpfc_register bmbx_reg;
  6388. /*
  6389. * Only one mailbox can be active to the bootstrap mailbox region
  6390. * at a time and there is no queueing provided.
  6391. */
  6392. spin_lock_irqsave(&phba->hbalock, iflag);
  6393. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6394. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6395. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6396. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6397. "cannot issue Data: x%x x%x\n",
  6398. mboxq->vport ? mboxq->vport->vpi : 0,
  6399. mboxq->u.mb.mbxCommand,
  6400. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6401. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6402. psli->sli_flag, MBX_POLL);
  6403. return MBXERR_ERROR;
  6404. }
  6405. /* The server grabs the token and owns it until release */
  6406. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6407. phba->sli.mbox_active = mboxq;
  6408. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6409. /*
  6410. * Initialize the bootstrap memory region to avoid stale data areas
  6411. * in the mailbox post. Then copy the caller's mailbox contents to
  6412. * the bmbx mailbox region.
  6413. */
  6414. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6415. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6416. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6417. sizeof(struct lpfc_mqe));
  6418. /* Post the high mailbox dma address to the port and wait for ready. */
  6419. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6420. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6421. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6422. * 1000) + jiffies;
  6423. do {
  6424. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6425. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6426. if (!db_ready)
  6427. msleep(2);
  6428. if (time_after(jiffies, timeout)) {
  6429. rc = MBXERR_ERROR;
  6430. goto exit;
  6431. }
  6432. } while (!db_ready);
  6433. /* Post the low mailbox dma address to the port. */
  6434. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6435. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6436. * 1000) + jiffies;
  6437. do {
  6438. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6439. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6440. if (!db_ready)
  6441. msleep(2);
  6442. if (time_after(jiffies, timeout)) {
  6443. rc = MBXERR_ERROR;
  6444. goto exit;
  6445. }
  6446. } while (!db_ready);
  6447. /*
  6448. * Read the CQ to ensure the mailbox has completed.
  6449. * If so, update the mailbox status so that the upper layers
  6450. * can complete the request normally.
  6451. */
  6452. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6453. sizeof(struct lpfc_mqe));
  6454. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6455. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6456. sizeof(struct lpfc_mcqe));
  6457. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6458. /*
  6459. * When the CQE status indicates a failure and the mailbox status
  6460. * indicates success then copy the CQE status into the mailbox status
  6461. * (and prefix it with x4000).
  6462. */
  6463. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6464. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6465. bf_set(lpfc_mqe_status, mb,
  6466. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6467. rc = MBXERR_ERROR;
  6468. } else
  6469. lpfc_sli4_swap_str(phba, mboxq);
  6470. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6471. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6472. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6473. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6474. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6475. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6476. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6477. bf_get(lpfc_mqe_status, mb),
  6478. mb->un.mb_words[0], mb->un.mb_words[1],
  6479. mb->un.mb_words[2], mb->un.mb_words[3],
  6480. mb->un.mb_words[4], mb->un.mb_words[5],
  6481. mb->un.mb_words[6], mb->un.mb_words[7],
  6482. mb->un.mb_words[8], mb->un.mb_words[9],
  6483. mb->un.mb_words[10], mb->un.mb_words[11],
  6484. mb->un.mb_words[12], mboxq->mcqe.word0,
  6485. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6486. mboxq->mcqe.trailer);
  6487. exit:
  6488. /* We are holding the token, no needed for lock when release */
  6489. spin_lock_irqsave(&phba->hbalock, iflag);
  6490. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6491. phba->sli.mbox_active = NULL;
  6492. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6493. return rc;
  6494. }
  6495. /**
  6496. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6497. * @phba: Pointer to HBA context object.
  6498. * @pmbox: Pointer to mailbox object.
  6499. * @flag: Flag indicating how the mailbox need to be processed.
  6500. *
  6501. * This function is called by discovery code and HBA management code to submit
  6502. * a mailbox command to firmware with SLI-4 interface spec.
  6503. *
  6504. * Return codes the caller owns the mailbox command after the return of the
  6505. * function.
  6506. **/
  6507. static int
  6508. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6509. uint32_t flag)
  6510. {
  6511. struct lpfc_sli *psli = &phba->sli;
  6512. unsigned long iflags;
  6513. int rc;
  6514. /* dump from issue mailbox command if setup */
  6515. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6516. rc = lpfc_mbox_dev_check(phba);
  6517. if (unlikely(rc)) {
  6518. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6519. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6520. "cannot issue Data: x%x x%x\n",
  6521. mboxq->vport ? mboxq->vport->vpi : 0,
  6522. mboxq->u.mb.mbxCommand,
  6523. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6524. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6525. psli->sli_flag, flag);
  6526. goto out_not_finished;
  6527. }
  6528. /* Detect polling mode and jump to a handler */
  6529. if (!phba->sli4_hba.intr_enable) {
  6530. if (flag == MBX_POLL)
  6531. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6532. else
  6533. rc = -EIO;
  6534. if (rc != MBX_SUCCESS)
  6535. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6536. "(%d):2541 Mailbox command x%x "
  6537. "(x%x/x%x) cannot issue Data: "
  6538. "x%x x%x\n",
  6539. mboxq->vport ? mboxq->vport->vpi : 0,
  6540. mboxq->u.mb.mbxCommand,
  6541. lpfc_sli_config_mbox_subsys_get(phba,
  6542. mboxq),
  6543. lpfc_sli_config_mbox_opcode_get(phba,
  6544. mboxq),
  6545. psli->sli_flag, flag);
  6546. return rc;
  6547. } else if (flag == MBX_POLL) {
  6548. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6549. "(%d):2542 Try to issue mailbox command "
  6550. "x%x (x%x/x%x) synchronously ahead of async"
  6551. "mailbox command queue: x%x x%x\n",
  6552. mboxq->vport ? mboxq->vport->vpi : 0,
  6553. mboxq->u.mb.mbxCommand,
  6554. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6555. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6556. psli->sli_flag, flag);
  6557. /* Try to block the asynchronous mailbox posting */
  6558. rc = lpfc_sli4_async_mbox_block(phba);
  6559. if (!rc) {
  6560. /* Successfully blocked, now issue sync mbox cmd */
  6561. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6562. if (rc != MBX_SUCCESS)
  6563. lpfc_printf_log(phba, KERN_ERR,
  6564. LOG_MBOX | LOG_SLI,
  6565. "(%d):2597 Mailbox command "
  6566. "x%x (x%x/x%x) cannot issue "
  6567. "Data: x%x x%x\n",
  6568. mboxq->vport ?
  6569. mboxq->vport->vpi : 0,
  6570. mboxq->u.mb.mbxCommand,
  6571. lpfc_sli_config_mbox_subsys_get(phba,
  6572. mboxq),
  6573. lpfc_sli_config_mbox_opcode_get(phba,
  6574. mboxq),
  6575. psli->sli_flag, flag);
  6576. /* Unblock the async mailbox posting afterward */
  6577. lpfc_sli4_async_mbox_unblock(phba);
  6578. }
  6579. return rc;
  6580. }
  6581. /* Now, interrupt mode asynchrous mailbox command */
  6582. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6583. if (rc) {
  6584. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6585. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6586. "cannot issue Data: x%x x%x\n",
  6587. mboxq->vport ? mboxq->vport->vpi : 0,
  6588. mboxq->u.mb.mbxCommand,
  6589. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6590. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6591. psli->sli_flag, flag);
  6592. goto out_not_finished;
  6593. }
  6594. /* Put the mailbox command to the driver internal FIFO */
  6595. psli->slistat.mbox_busy++;
  6596. spin_lock_irqsave(&phba->hbalock, iflags);
  6597. lpfc_mbox_put(phba, mboxq);
  6598. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6599. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6600. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6601. "x%x (x%x/x%x) x%x x%x x%x\n",
  6602. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6603. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6604. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6605. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6606. phba->pport->port_state,
  6607. psli->sli_flag, MBX_NOWAIT);
  6608. /* Wake up worker thread to transport mailbox command from head */
  6609. lpfc_worker_wake_up(phba);
  6610. return MBX_BUSY;
  6611. out_not_finished:
  6612. return MBX_NOT_FINISHED;
  6613. }
  6614. /**
  6615. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6616. * @phba: Pointer to HBA context object.
  6617. *
  6618. * This function is called by worker thread to send a mailbox command to
  6619. * SLI4 HBA firmware.
  6620. *
  6621. **/
  6622. int
  6623. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6624. {
  6625. struct lpfc_sli *psli = &phba->sli;
  6626. LPFC_MBOXQ_t *mboxq;
  6627. int rc = MBX_SUCCESS;
  6628. unsigned long iflags;
  6629. struct lpfc_mqe *mqe;
  6630. uint32_t mbx_cmnd;
  6631. /* Check interrupt mode before post async mailbox command */
  6632. if (unlikely(!phba->sli4_hba.intr_enable))
  6633. return MBX_NOT_FINISHED;
  6634. /* Check for mailbox command service token */
  6635. spin_lock_irqsave(&phba->hbalock, iflags);
  6636. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6637. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6638. return MBX_NOT_FINISHED;
  6639. }
  6640. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6641. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6642. return MBX_NOT_FINISHED;
  6643. }
  6644. if (unlikely(phba->sli.mbox_active)) {
  6645. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6646. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6647. "0384 There is pending active mailbox cmd\n");
  6648. return MBX_NOT_FINISHED;
  6649. }
  6650. /* Take the mailbox command service token */
  6651. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6652. /* Get the next mailbox command from head of queue */
  6653. mboxq = lpfc_mbox_get(phba);
  6654. /* If no more mailbox command waiting for post, we're done */
  6655. if (!mboxq) {
  6656. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6657. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6658. return MBX_SUCCESS;
  6659. }
  6660. phba->sli.mbox_active = mboxq;
  6661. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6662. /* Check device readiness for posting mailbox command */
  6663. rc = lpfc_mbox_dev_check(phba);
  6664. if (unlikely(rc))
  6665. /* Driver clean routine will clean up pending mailbox */
  6666. goto out_not_finished;
  6667. /* Prepare the mbox command to be posted */
  6668. mqe = &mboxq->u.mqe;
  6669. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6670. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6671. mod_timer(&psli->mbox_tmo, (jiffies +
  6672. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6673. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6674. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6675. "x%x x%x\n",
  6676. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6677. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6678. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6679. phba->pport->port_state, psli->sli_flag);
  6680. if (mbx_cmnd != MBX_HEARTBEAT) {
  6681. if (mboxq->vport) {
  6682. lpfc_debugfs_disc_trc(mboxq->vport,
  6683. LPFC_DISC_TRC_MBOX_VPORT,
  6684. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6685. mbx_cmnd, mqe->un.mb_words[0],
  6686. mqe->un.mb_words[1]);
  6687. } else {
  6688. lpfc_debugfs_disc_trc(phba->pport,
  6689. LPFC_DISC_TRC_MBOX,
  6690. "MBOX Send: cmd:x%x mb:x%x x%x",
  6691. mbx_cmnd, mqe->un.mb_words[0],
  6692. mqe->un.mb_words[1]);
  6693. }
  6694. }
  6695. psli->slistat.mbox_cmd++;
  6696. /* Post the mailbox command to the port */
  6697. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6698. if (rc != MBX_SUCCESS) {
  6699. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6700. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6701. "cannot issue Data: x%x x%x\n",
  6702. mboxq->vport ? mboxq->vport->vpi : 0,
  6703. mboxq->u.mb.mbxCommand,
  6704. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6705. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6706. psli->sli_flag, MBX_NOWAIT);
  6707. goto out_not_finished;
  6708. }
  6709. return rc;
  6710. out_not_finished:
  6711. spin_lock_irqsave(&phba->hbalock, iflags);
  6712. if (phba->sli.mbox_active) {
  6713. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6714. __lpfc_mbox_cmpl_put(phba, mboxq);
  6715. /* Release the token */
  6716. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6717. phba->sli.mbox_active = NULL;
  6718. }
  6719. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6720. return MBX_NOT_FINISHED;
  6721. }
  6722. /**
  6723. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6724. * @phba: Pointer to HBA context object.
  6725. * @pmbox: Pointer to mailbox object.
  6726. * @flag: Flag indicating how the mailbox need to be processed.
  6727. *
  6728. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6729. * the API jump table function pointer from the lpfc_hba struct.
  6730. *
  6731. * Return codes the caller owns the mailbox command after the return of the
  6732. * function.
  6733. **/
  6734. int
  6735. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6736. {
  6737. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6738. }
  6739. /**
  6740. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6741. * @phba: The hba struct for which this call is being executed.
  6742. * @dev_grp: The HBA PCI-Device group number.
  6743. *
  6744. * This routine sets up the mbox interface API function jump table in @phba
  6745. * struct.
  6746. * Returns: 0 - success, -ENODEV - failure.
  6747. **/
  6748. int
  6749. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6750. {
  6751. switch (dev_grp) {
  6752. case LPFC_PCI_DEV_LP:
  6753. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6754. phba->lpfc_sli_handle_slow_ring_event =
  6755. lpfc_sli_handle_slow_ring_event_s3;
  6756. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6757. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6758. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6759. break;
  6760. case LPFC_PCI_DEV_OC:
  6761. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6762. phba->lpfc_sli_handle_slow_ring_event =
  6763. lpfc_sli_handle_slow_ring_event_s4;
  6764. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6765. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6766. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6767. break;
  6768. default:
  6769. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6770. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6771. dev_grp);
  6772. return -ENODEV;
  6773. break;
  6774. }
  6775. return 0;
  6776. }
  6777. /**
  6778. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6779. * @phba: Pointer to HBA context object.
  6780. * @pring: Pointer to driver SLI ring object.
  6781. * @piocb: Pointer to address of newly added command iocb.
  6782. *
  6783. * This function is called with hbalock held to add a command
  6784. * iocb to the txq when SLI layer cannot submit the command iocb
  6785. * to the ring.
  6786. **/
  6787. void
  6788. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6789. struct lpfc_iocbq *piocb)
  6790. {
  6791. /* Insert the caller's iocb in the txq tail for later processing. */
  6792. list_add_tail(&piocb->list, &pring->txq);
  6793. pring->txq_cnt++;
  6794. }
  6795. /**
  6796. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6797. * @phba: Pointer to HBA context object.
  6798. * @pring: Pointer to driver SLI ring object.
  6799. * @piocb: Pointer to address of newly added command iocb.
  6800. *
  6801. * This function is called with hbalock held before a new
  6802. * iocb is submitted to the firmware. This function checks
  6803. * txq to flush the iocbs in txq to Firmware before
  6804. * submitting new iocbs to the Firmware.
  6805. * If there are iocbs in the txq which need to be submitted
  6806. * to firmware, lpfc_sli_next_iocb returns the first element
  6807. * of the txq after dequeuing it from txq.
  6808. * If there is no iocb in the txq then the function will return
  6809. * *piocb and *piocb is set to NULL. Caller needs to check
  6810. * *piocb to find if there are more commands in the txq.
  6811. **/
  6812. static struct lpfc_iocbq *
  6813. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6814. struct lpfc_iocbq **piocb)
  6815. {
  6816. struct lpfc_iocbq * nextiocb;
  6817. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6818. if (!nextiocb) {
  6819. nextiocb = *piocb;
  6820. *piocb = NULL;
  6821. }
  6822. return nextiocb;
  6823. }
  6824. /**
  6825. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6826. * @phba: Pointer to HBA context object.
  6827. * @ring_number: SLI ring number to issue iocb on.
  6828. * @piocb: Pointer to command iocb.
  6829. * @flag: Flag indicating if this command can be put into txq.
  6830. *
  6831. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6832. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6833. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6834. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6835. * this function allows only iocbs for posting buffers. This function finds
  6836. * next available slot in the command ring and posts the command to the
  6837. * available slot and writes the port attention register to request HBA start
  6838. * processing new iocb. If there is no slot available in the ring and
  6839. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6840. * the function returns IOCB_BUSY.
  6841. *
  6842. * This function is called with hbalock held. The function will return success
  6843. * after it successfully submit the iocb to firmware or after adding to the
  6844. * txq.
  6845. **/
  6846. static int
  6847. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6848. struct lpfc_iocbq *piocb, uint32_t flag)
  6849. {
  6850. struct lpfc_iocbq *nextiocb;
  6851. IOCB_t *iocb;
  6852. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6853. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6854. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6855. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6856. lpfc_printf_log(phba, KERN_ERR,
  6857. LOG_SLI | LOG_VPORT,
  6858. "1807 IOCB x%x failed. No vport\n",
  6859. piocb->iocb.ulpCommand);
  6860. dump_stack();
  6861. return IOCB_ERROR;
  6862. }
  6863. /* If the PCI channel is in offline state, do not post iocbs. */
  6864. if (unlikely(pci_channel_offline(phba->pcidev)))
  6865. return IOCB_ERROR;
  6866. /* If HBA has a deferred error attention, fail the iocb. */
  6867. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6868. return IOCB_ERROR;
  6869. /*
  6870. * We should never get an IOCB if we are in a < LINK_DOWN state
  6871. */
  6872. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6873. return IOCB_ERROR;
  6874. /*
  6875. * Check to see if we are blocking IOCB processing because of a
  6876. * outstanding event.
  6877. */
  6878. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6879. goto iocb_busy;
  6880. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6881. /*
  6882. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6883. * can be issued if the link is not up.
  6884. */
  6885. switch (piocb->iocb.ulpCommand) {
  6886. case CMD_GEN_REQUEST64_CR:
  6887. case CMD_GEN_REQUEST64_CX:
  6888. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6889. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6890. FC_RCTL_DD_UNSOL_CMD) ||
  6891. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6892. MENLO_TRANSPORT_TYPE))
  6893. goto iocb_busy;
  6894. break;
  6895. case CMD_QUE_RING_BUF_CN:
  6896. case CMD_QUE_RING_BUF64_CN:
  6897. /*
  6898. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6899. * completion, iocb_cmpl MUST be 0.
  6900. */
  6901. if (piocb->iocb_cmpl)
  6902. piocb->iocb_cmpl = NULL;
  6903. /*FALLTHROUGH*/
  6904. case CMD_CREATE_XRI_CR:
  6905. case CMD_CLOSE_XRI_CN:
  6906. case CMD_CLOSE_XRI_CX:
  6907. break;
  6908. default:
  6909. goto iocb_busy;
  6910. }
  6911. /*
  6912. * For FCP commands, we must be in a state where we can process link
  6913. * attention events.
  6914. */
  6915. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6916. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6917. goto iocb_busy;
  6918. }
  6919. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6920. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6921. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6922. if (iocb)
  6923. lpfc_sli_update_ring(phba, pring);
  6924. else
  6925. lpfc_sli_update_full_ring(phba, pring);
  6926. if (!piocb)
  6927. return IOCB_SUCCESS;
  6928. goto out_busy;
  6929. iocb_busy:
  6930. pring->stats.iocb_cmd_delay++;
  6931. out_busy:
  6932. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6933. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6934. return IOCB_SUCCESS;
  6935. }
  6936. return IOCB_BUSY;
  6937. }
  6938. /**
  6939. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6940. * @phba: Pointer to HBA context object.
  6941. * @piocb: Pointer to command iocb.
  6942. * @sglq: Pointer to the scatter gather queue object.
  6943. *
  6944. * This routine converts the bpl or bde that is in the IOCB
  6945. * to a sgl list for the sli4 hardware. The physical address
  6946. * of the bpl/bde is converted back to a virtual address.
  6947. * If the IOCB contains a BPL then the list of BDE's is
  6948. * converted to sli4_sge's. If the IOCB contains a single
  6949. * BDE then it is converted to a single sli_sge.
  6950. * The IOCB is still in cpu endianess so the contents of
  6951. * the bpl can be used without byte swapping.
  6952. *
  6953. * Returns valid XRI = Success, NO_XRI = Failure.
  6954. **/
  6955. static uint16_t
  6956. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6957. struct lpfc_sglq *sglq)
  6958. {
  6959. uint16_t xritag = NO_XRI;
  6960. struct ulp_bde64 *bpl = NULL;
  6961. struct ulp_bde64 bde;
  6962. struct sli4_sge *sgl = NULL;
  6963. struct lpfc_dmabuf *dmabuf;
  6964. IOCB_t *icmd;
  6965. int numBdes = 0;
  6966. int i = 0;
  6967. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6968. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6969. if (!piocbq || !sglq)
  6970. return xritag;
  6971. sgl = (struct sli4_sge *)sglq->sgl;
  6972. icmd = &piocbq->iocb;
  6973. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6974. return sglq->sli4_xritag;
  6975. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6976. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6977. sizeof(struct ulp_bde64);
  6978. /* The addrHigh and addrLow fields within the IOCB
  6979. * have not been byteswapped yet so there is no
  6980. * need to swap them back.
  6981. */
  6982. if (piocbq->context3)
  6983. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  6984. else
  6985. return xritag;
  6986. bpl = (struct ulp_bde64 *)dmabuf->virt;
  6987. if (!bpl)
  6988. return xritag;
  6989. for (i = 0; i < numBdes; i++) {
  6990. /* Should already be byte swapped. */
  6991. sgl->addr_hi = bpl->addrHigh;
  6992. sgl->addr_lo = bpl->addrLow;
  6993. sgl->word2 = le32_to_cpu(sgl->word2);
  6994. if ((i+1) == numBdes)
  6995. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6996. else
  6997. bf_set(lpfc_sli4_sge_last, sgl, 0);
  6998. /* swap the size field back to the cpu so we
  6999. * can assign it to the sgl.
  7000. */
  7001. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7002. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7003. /* The offsets in the sgl need to be accumulated
  7004. * separately for the request and reply lists.
  7005. * The request is always first, the reply follows.
  7006. */
  7007. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7008. /* add up the reply sg entries */
  7009. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7010. inbound++;
  7011. /* first inbound? reset the offset */
  7012. if (inbound == 1)
  7013. offset = 0;
  7014. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7015. bf_set(lpfc_sli4_sge_type, sgl,
  7016. LPFC_SGE_TYPE_DATA);
  7017. offset += bde.tus.f.bdeSize;
  7018. }
  7019. sgl->word2 = cpu_to_le32(sgl->word2);
  7020. bpl++;
  7021. sgl++;
  7022. }
  7023. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7024. /* The addrHigh and addrLow fields of the BDE have not
  7025. * been byteswapped yet so they need to be swapped
  7026. * before putting them in the sgl.
  7027. */
  7028. sgl->addr_hi =
  7029. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7030. sgl->addr_lo =
  7031. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7032. sgl->word2 = le32_to_cpu(sgl->word2);
  7033. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7034. sgl->word2 = cpu_to_le32(sgl->word2);
  7035. sgl->sge_len =
  7036. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7037. }
  7038. return sglq->sli4_xritag;
  7039. }
  7040. /**
  7041. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7042. * @phba: Pointer to HBA context object.
  7043. *
  7044. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7045. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7046. * held.
  7047. *
  7048. * Return: index into SLI4 fast-path FCP queue index.
  7049. **/
  7050. static uint32_t
  7051. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7052. {
  7053. ++phba->fcp_qidx;
  7054. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  7055. phba->fcp_qidx = 0;
  7056. return phba->fcp_qidx;
  7057. }
  7058. /**
  7059. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7060. * @phba: Pointer to HBA context object.
  7061. * @piocb: Pointer to command iocb.
  7062. * @wqe: Pointer to the work queue entry.
  7063. *
  7064. * This routine converts the iocb command to its Work Queue Entry
  7065. * equivalent. The wqe pointer should not have any fields set when
  7066. * this routine is called because it will memcpy over them.
  7067. * This routine does not set the CQ_ID or the WQEC bits in the
  7068. * wqe.
  7069. *
  7070. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7071. **/
  7072. static int
  7073. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7074. union lpfc_wqe *wqe)
  7075. {
  7076. uint32_t xmit_len = 0, total_len = 0;
  7077. uint8_t ct = 0;
  7078. uint32_t fip;
  7079. uint32_t abort_tag;
  7080. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7081. uint8_t cmnd;
  7082. uint16_t xritag;
  7083. uint16_t abrt_iotag;
  7084. struct lpfc_iocbq *abrtiocbq;
  7085. struct ulp_bde64 *bpl = NULL;
  7086. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7087. int numBdes, i;
  7088. struct ulp_bde64 bde;
  7089. struct lpfc_nodelist *ndlp;
  7090. uint32_t *pcmd;
  7091. uint32_t if_type;
  7092. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7093. /* The fcp commands will set command type */
  7094. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7095. command_type = FCP_COMMAND;
  7096. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7097. command_type = ELS_COMMAND_FIP;
  7098. else
  7099. command_type = ELS_COMMAND_NON_FIP;
  7100. /* Some of the fields are in the right position already */
  7101. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7102. abort_tag = (uint32_t) iocbq->iotag;
  7103. xritag = iocbq->sli4_xritag;
  7104. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7105. /* words0-2 bpl convert bde */
  7106. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7107. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7108. sizeof(struct ulp_bde64);
  7109. bpl = (struct ulp_bde64 *)
  7110. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7111. if (!bpl)
  7112. return IOCB_ERROR;
  7113. /* Should already be byte swapped. */
  7114. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7115. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7116. /* swap the size field back to the cpu so we
  7117. * can assign it to the sgl.
  7118. */
  7119. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7120. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7121. total_len = 0;
  7122. for (i = 0; i < numBdes; i++) {
  7123. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7124. total_len += bde.tus.f.bdeSize;
  7125. }
  7126. } else
  7127. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7128. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7129. cmnd = iocbq->iocb.ulpCommand;
  7130. switch (iocbq->iocb.ulpCommand) {
  7131. case CMD_ELS_REQUEST64_CR:
  7132. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7133. if (!iocbq->iocb.ulpLe) {
  7134. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7135. "2007 Only Limited Edition cmd Format"
  7136. " supported 0x%x\n",
  7137. iocbq->iocb.ulpCommand);
  7138. return IOCB_ERROR;
  7139. }
  7140. wqe->els_req.payload_len = xmit_len;
  7141. /* Els_reguest64 has a TMO */
  7142. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7143. iocbq->iocb.ulpTimeout);
  7144. /* Need a VF for word 4 set the vf bit*/
  7145. bf_set(els_req64_vf, &wqe->els_req, 0);
  7146. /* And a VFID for word 12 */
  7147. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7148. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7149. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7150. iocbq->iocb.ulpContext);
  7151. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7152. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7153. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7154. if (command_type == ELS_COMMAND_FIP)
  7155. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7156. >> LPFC_FIP_ELS_ID_SHIFT);
  7157. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7158. iocbq->context2)->virt);
  7159. if_type = bf_get(lpfc_sli_intf_if_type,
  7160. &phba->sli4_hba.sli_intf);
  7161. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7162. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7163. *pcmd == ELS_CMD_SCR ||
  7164. *pcmd == ELS_CMD_FDISC ||
  7165. *pcmd == ELS_CMD_LOGO ||
  7166. *pcmd == ELS_CMD_PLOGI)) {
  7167. bf_set(els_req64_sp, &wqe->els_req, 1);
  7168. bf_set(els_req64_sid, &wqe->els_req,
  7169. iocbq->vport->fc_myDID);
  7170. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7171. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7172. phba->vpi_ids[iocbq->vport->vpi]);
  7173. } else if (pcmd && iocbq->context1) {
  7174. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7175. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7176. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7177. }
  7178. }
  7179. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7180. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7181. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7182. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7183. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7184. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7185. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7186. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7187. break;
  7188. case CMD_XMIT_SEQUENCE64_CX:
  7189. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7190. iocbq->iocb.un.ulpWord[3]);
  7191. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7192. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7193. /* The entire sequence is transmitted for this IOCB */
  7194. xmit_len = total_len;
  7195. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7196. if (phba->link_flag & LS_LOOPBACK_MODE)
  7197. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7198. case CMD_XMIT_SEQUENCE64_CR:
  7199. /* word3 iocb=io_tag32 wqe=reserved */
  7200. wqe->xmit_sequence.rsvd3 = 0;
  7201. /* word4 relative_offset memcpy */
  7202. /* word5 r_ctl/df_ctl memcpy */
  7203. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7204. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7205. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7206. LPFC_WQE_IOD_WRITE);
  7207. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7208. LPFC_WQE_LENLOC_WORD12);
  7209. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7210. wqe->xmit_sequence.xmit_len = xmit_len;
  7211. command_type = OTHER_COMMAND;
  7212. break;
  7213. case CMD_XMIT_BCAST64_CN:
  7214. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7215. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7216. /* word4 iocb=rsvd wqe=rsvd */
  7217. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7218. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7219. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7220. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7221. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7222. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7223. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7224. LPFC_WQE_LENLOC_WORD3);
  7225. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7226. break;
  7227. case CMD_FCP_IWRITE64_CR:
  7228. command_type = FCP_COMMAND_DATA_OUT;
  7229. /* word3 iocb=iotag wqe=payload_offset_len */
  7230. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7231. wqe->fcp_iwrite.payload_offset_len =
  7232. xmit_len + sizeof(struct fcp_rsp);
  7233. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7234. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7235. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7236. iocbq->iocb.ulpFCP2Rcvy);
  7237. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7238. /* Always open the exchange */
  7239. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7240. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7241. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7242. LPFC_WQE_LENLOC_WORD4);
  7243. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7244. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7245. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7246. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7247. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7248. }
  7249. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7250. break;
  7251. case CMD_FCP_IREAD64_CR:
  7252. /* word3 iocb=iotag wqe=payload_offset_len */
  7253. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7254. wqe->fcp_iread.payload_offset_len =
  7255. xmit_len + sizeof(struct fcp_rsp);
  7256. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7257. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7258. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7259. iocbq->iocb.ulpFCP2Rcvy);
  7260. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7261. /* Always open the exchange */
  7262. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7263. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7264. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7265. LPFC_WQE_LENLOC_WORD4);
  7266. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7267. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7268. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7269. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7270. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7271. }
  7272. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7273. break;
  7274. case CMD_FCP_ICMND64_CR:
  7275. /* word3 iocb=IO_TAG wqe=reserved */
  7276. wqe->fcp_icmd.rsrvd3 = 0;
  7277. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7278. /* Always open the exchange */
  7279. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7280. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7281. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7282. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7283. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7284. LPFC_WQE_LENLOC_NONE);
  7285. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7286. break;
  7287. case CMD_GEN_REQUEST64_CR:
  7288. /* For this command calculate the xmit length of the
  7289. * request bde.
  7290. */
  7291. xmit_len = 0;
  7292. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7293. sizeof(struct ulp_bde64);
  7294. for (i = 0; i < numBdes; i++) {
  7295. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7296. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7297. break;
  7298. xmit_len += bde.tus.f.bdeSize;
  7299. }
  7300. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7301. wqe->gen_req.request_payload_len = xmit_len;
  7302. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7303. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7304. /* word6 context tag copied in memcpy */
  7305. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7306. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7307. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7308. "2015 Invalid CT %x command 0x%x\n",
  7309. ct, iocbq->iocb.ulpCommand);
  7310. return IOCB_ERROR;
  7311. }
  7312. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7313. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7314. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7315. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7316. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7317. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7318. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7319. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7320. command_type = OTHER_COMMAND;
  7321. break;
  7322. case CMD_XMIT_ELS_RSP64_CX:
  7323. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7324. /* words0-2 BDE memcpy */
  7325. /* word3 iocb=iotag32 wqe=response_payload_len */
  7326. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7327. /* word4 iocb=did wge=rsvd. */
  7328. wqe->xmit_els_rsp.rsvd4 = 0;
  7329. /* word5 iocb=rsvd wge=did */
  7330. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7331. iocbq->iocb.un.elsreq64.remoteID);
  7332. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7333. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7334. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7335. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7336. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7337. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7338. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7339. phba->vpi_ids[iocbq->vport->vpi]);
  7340. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7341. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7342. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7343. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7344. LPFC_WQE_LENLOC_WORD3);
  7345. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7346. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7347. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7348. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7349. iocbq->context2)->virt);
  7350. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7351. bf_set(els_req64_sp, &wqe->els_req, 1);
  7352. bf_set(els_req64_sid, &wqe->els_req,
  7353. iocbq->vport->fc_myDID);
  7354. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7355. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7356. phba->vpi_ids[phba->pport->vpi]);
  7357. }
  7358. command_type = OTHER_COMMAND;
  7359. break;
  7360. case CMD_CLOSE_XRI_CN:
  7361. case CMD_ABORT_XRI_CN:
  7362. case CMD_ABORT_XRI_CX:
  7363. /* words 0-2 memcpy should be 0 rserved */
  7364. /* port will send abts */
  7365. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7366. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7367. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7368. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7369. } else
  7370. fip = 0;
  7371. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7372. /*
  7373. * The link is down, or the command was ELS_FIP
  7374. * so the fw does not need to send abts
  7375. * on the wire.
  7376. */
  7377. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7378. else
  7379. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7380. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7381. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7382. wqe->abort_cmd.rsrvd5 = 0;
  7383. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7384. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7385. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7386. /*
  7387. * The abort handler will send us CMD_ABORT_XRI_CN or
  7388. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7389. */
  7390. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7391. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7392. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7393. LPFC_WQE_LENLOC_NONE);
  7394. cmnd = CMD_ABORT_XRI_CX;
  7395. command_type = OTHER_COMMAND;
  7396. xritag = 0;
  7397. break;
  7398. case CMD_XMIT_BLS_RSP64_CX:
  7399. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7400. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7401. * we re-construct this WQE here based on information in
  7402. * iocbq from scratch.
  7403. */
  7404. memset(wqe, 0, sizeof(union lpfc_wqe));
  7405. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7406. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7407. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7408. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7409. LPFC_ABTS_UNSOL_INT) {
  7410. /* ABTS sent by initiator to CT exchange, the
  7411. * RX_ID field will be filled with the newly
  7412. * allocated responder XRI.
  7413. */
  7414. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7415. iocbq->sli4_xritag);
  7416. } else {
  7417. /* ABTS sent by responder to CT exchange, the
  7418. * RX_ID field will be filled with the responder
  7419. * RX_ID from ABTS.
  7420. */
  7421. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7422. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7423. }
  7424. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7425. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7426. /* Use CT=VPI */
  7427. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7428. ndlp->nlp_DID);
  7429. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7430. iocbq->iocb.ulpContext);
  7431. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7432. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7433. phba->vpi_ids[phba->pport->vpi]);
  7434. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7435. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7436. LPFC_WQE_LENLOC_NONE);
  7437. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7438. command_type = OTHER_COMMAND;
  7439. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7440. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7441. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7442. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7443. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7444. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7445. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7446. }
  7447. break;
  7448. case CMD_XRI_ABORTED_CX:
  7449. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7450. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7451. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7452. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7453. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7454. default:
  7455. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7456. "2014 Invalid command 0x%x\n",
  7457. iocbq->iocb.ulpCommand);
  7458. return IOCB_ERROR;
  7459. break;
  7460. }
  7461. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7462. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7463. wqe->generic.wqe_com.abort_tag = abort_tag;
  7464. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7465. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7466. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7467. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7468. return 0;
  7469. }
  7470. /**
  7471. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7472. * @phba: Pointer to HBA context object.
  7473. * @ring_number: SLI ring number to issue iocb on.
  7474. * @piocb: Pointer to command iocb.
  7475. * @flag: Flag indicating if this command can be put into txq.
  7476. *
  7477. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7478. * an iocb command to an HBA with SLI-4 interface spec.
  7479. *
  7480. * This function is called with hbalock held. The function will return success
  7481. * after it successfully submit the iocb to firmware or after adding to the
  7482. * txq.
  7483. **/
  7484. static int
  7485. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7486. struct lpfc_iocbq *piocb, uint32_t flag)
  7487. {
  7488. struct lpfc_sglq *sglq;
  7489. union lpfc_wqe wqe;
  7490. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7491. if (piocb->sli4_xritag == NO_XRI) {
  7492. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7493. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7494. sglq = NULL;
  7495. else {
  7496. if (pring->txq_cnt) {
  7497. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7498. __lpfc_sli_ringtx_put(phba,
  7499. pring, piocb);
  7500. return IOCB_SUCCESS;
  7501. } else {
  7502. return IOCB_BUSY;
  7503. }
  7504. } else {
  7505. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7506. if (!sglq) {
  7507. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7508. __lpfc_sli_ringtx_put(phba,
  7509. pring,
  7510. piocb);
  7511. return IOCB_SUCCESS;
  7512. } else
  7513. return IOCB_BUSY;
  7514. }
  7515. }
  7516. }
  7517. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7518. /* These IO's already have an XRI and a mapped sgl. */
  7519. sglq = NULL;
  7520. } else {
  7521. /*
  7522. * This is a continuation of a commandi,(CX) so this
  7523. * sglq is on the active list
  7524. */
  7525. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7526. if (!sglq)
  7527. return IOCB_ERROR;
  7528. }
  7529. if (sglq) {
  7530. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7531. piocb->sli4_xritag = sglq->sli4_xritag;
  7532. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7533. return IOCB_ERROR;
  7534. }
  7535. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7536. return IOCB_ERROR;
  7537. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7538. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7539. /*
  7540. * For FCP command IOCB, get a new WQ index to distribute
  7541. * WQE across the WQsr. On the other hand, for abort IOCB,
  7542. * it carries the same WQ index to the original command
  7543. * IOCB.
  7544. */
  7545. if (piocb->iocb_flag & LPFC_IO_FCP)
  7546. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7547. if (unlikely(!phba->sli4_hba.fcp_wq))
  7548. return IOCB_ERROR;
  7549. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7550. &wqe))
  7551. return IOCB_ERROR;
  7552. } else {
  7553. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7554. return IOCB_ERROR;
  7555. }
  7556. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7557. return 0;
  7558. }
  7559. /**
  7560. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7561. *
  7562. * This routine wraps the actual lockless version for issusing IOCB function
  7563. * pointer from the lpfc_hba struct.
  7564. *
  7565. * Return codes:
  7566. * IOCB_ERROR - Error
  7567. * IOCB_SUCCESS - Success
  7568. * IOCB_BUSY - Busy
  7569. **/
  7570. int
  7571. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7572. struct lpfc_iocbq *piocb, uint32_t flag)
  7573. {
  7574. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7575. }
  7576. /**
  7577. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7578. * @phba: The hba struct for which this call is being executed.
  7579. * @dev_grp: The HBA PCI-Device group number.
  7580. *
  7581. * This routine sets up the SLI interface API function jump table in @phba
  7582. * struct.
  7583. * Returns: 0 - success, -ENODEV - failure.
  7584. **/
  7585. int
  7586. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7587. {
  7588. switch (dev_grp) {
  7589. case LPFC_PCI_DEV_LP:
  7590. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7591. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7592. break;
  7593. case LPFC_PCI_DEV_OC:
  7594. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7595. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7596. break;
  7597. default:
  7598. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7599. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7600. dev_grp);
  7601. return -ENODEV;
  7602. break;
  7603. }
  7604. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7605. return 0;
  7606. }
  7607. /**
  7608. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7609. * @phba: Pointer to HBA context object.
  7610. * @pring: Pointer to driver SLI ring object.
  7611. * @piocb: Pointer to command iocb.
  7612. * @flag: Flag indicating if this command can be put into txq.
  7613. *
  7614. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7615. * function. This function gets the hbalock and calls
  7616. * __lpfc_sli_issue_iocb function and will return the error returned
  7617. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7618. * functions which do not hold hbalock.
  7619. **/
  7620. int
  7621. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7622. struct lpfc_iocbq *piocb, uint32_t flag)
  7623. {
  7624. unsigned long iflags;
  7625. int rc;
  7626. spin_lock_irqsave(&phba->hbalock, iflags);
  7627. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7628. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7629. return rc;
  7630. }
  7631. /**
  7632. * lpfc_extra_ring_setup - Extra ring setup function
  7633. * @phba: Pointer to HBA context object.
  7634. *
  7635. * This function is called while driver attaches with the
  7636. * HBA to setup the extra ring. The extra ring is used
  7637. * only when driver needs to support target mode functionality
  7638. * or IP over FC functionalities.
  7639. *
  7640. * This function is called with no lock held.
  7641. **/
  7642. static int
  7643. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7644. {
  7645. struct lpfc_sli *psli;
  7646. struct lpfc_sli_ring *pring;
  7647. psli = &phba->sli;
  7648. /* Adjust cmd/rsp ring iocb entries more evenly */
  7649. /* Take some away from the FCP ring */
  7650. pring = &psli->ring[psli->fcp_ring];
  7651. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7652. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7653. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7654. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7655. /* and give them to the extra ring */
  7656. pring = &psli->ring[psli->extra_ring];
  7657. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7658. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7659. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7660. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7661. /* Setup default profile for this ring */
  7662. pring->iotag_max = 4096;
  7663. pring->num_mask = 1;
  7664. pring->prt[0].profile = 0; /* Mask 0 */
  7665. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7666. pring->prt[0].type = phba->cfg_multi_ring_type;
  7667. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7668. return 0;
  7669. }
  7670. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7671. * @vport: pointer to virtual port object.
  7672. * @ndlp: nodelist pointer for the impacted rport.
  7673. *
  7674. * The driver calls this routine in response to a XRI ABORT CQE
  7675. * event from the port. In this event, the driver is required to
  7676. * recover its login to the rport even though its login may be valid
  7677. * from the driver's perspective. The failed ABTS notice from the
  7678. * port indicates the rport is not responding.
  7679. */
  7680. static void
  7681. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7682. struct lpfc_nodelist *ndlp)
  7683. {
  7684. struct Scsi_Host *shost;
  7685. struct lpfc_hba *phba;
  7686. unsigned long flags = 0;
  7687. shost = lpfc_shost_from_vport(vport);
  7688. phba = vport->phba;
  7689. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7690. lpfc_printf_log(phba, KERN_INFO,
  7691. LOG_SLI, "3093 No rport recovery needed. "
  7692. "rport in state 0x%x\n",
  7693. ndlp->nlp_state);
  7694. return;
  7695. }
  7696. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7697. "3094 Start rport recovery on shost id 0x%x "
  7698. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7699. "flags 0x%x\n",
  7700. shost->host_no, ndlp->nlp_DID,
  7701. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7702. ndlp->nlp_flag);
  7703. /*
  7704. * The rport is not responding. Don't attempt ADISC recovery.
  7705. * Remove the FCP-2 flag to force a PLOGI.
  7706. */
  7707. spin_lock_irqsave(shost->host_lock, flags);
  7708. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7709. spin_unlock_irqrestore(shost->host_lock, flags);
  7710. lpfc_disc_state_machine(vport, ndlp, NULL,
  7711. NLP_EVT_DEVICE_RECOVERY);
  7712. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7713. spin_lock_irqsave(shost->host_lock, flags);
  7714. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7715. spin_unlock_irqrestore(shost->host_lock, flags);
  7716. lpfc_disc_start(vport);
  7717. }
  7718. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7719. * @phba: Pointer to HBA context object.
  7720. * @iocbq: Pointer to iocb object.
  7721. *
  7722. * The async_event handler calls this routine when it receives
  7723. * an ASYNC_STATUS_CN event from the port. The port generates
  7724. * this event when an Abort Sequence request to an rport fails
  7725. * twice in succession. The abort could be originated by the
  7726. * driver or by the port. The ABTS could have been for an ELS
  7727. * or FCP IO. The port only generates this event when an ABTS
  7728. * fails to complete after one retry.
  7729. */
  7730. static void
  7731. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7732. struct lpfc_iocbq *iocbq)
  7733. {
  7734. struct lpfc_nodelist *ndlp = NULL;
  7735. uint16_t rpi = 0, vpi = 0;
  7736. struct lpfc_vport *vport = NULL;
  7737. /* The rpi in the ulpContext is vport-sensitive. */
  7738. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7739. rpi = iocbq->iocb.ulpContext;
  7740. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7741. "3092 Port generated ABTS async event "
  7742. "on vpi %d rpi %d status 0x%x\n",
  7743. vpi, rpi, iocbq->iocb.ulpStatus);
  7744. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7745. if (!vport)
  7746. goto err_exit;
  7747. ndlp = lpfc_findnode_rpi(vport, rpi);
  7748. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7749. goto err_exit;
  7750. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7751. lpfc_sli_abts_recover_port(vport, ndlp);
  7752. return;
  7753. err_exit:
  7754. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7755. "3095 Event Context not found, no "
  7756. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7757. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7758. vpi, rpi);
  7759. }
  7760. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7761. * @phba: pointer to HBA context object.
  7762. * @ndlp: nodelist pointer for the impacted rport.
  7763. * @axri: pointer to the wcqe containing the failed exchange.
  7764. *
  7765. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7766. * port. The port generates this event when an abort exchange request to an
  7767. * rport fails twice in succession with no reply. The abort could be originated
  7768. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7769. */
  7770. void
  7771. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7772. struct lpfc_nodelist *ndlp,
  7773. struct sli4_wcqe_xri_aborted *axri)
  7774. {
  7775. struct lpfc_vport *vport;
  7776. uint32_t ext_status = 0;
  7777. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7778. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7779. "3115 Node Context not found, driver "
  7780. "ignoring abts err event\n");
  7781. return;
  7782. }
  7783. vport = ndlp->vport;
  7784. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7785. "3116 Port generated FCP XRI ABORT event on "
  7786. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7787. ndlp->vport->vpi, ndlp->nlp_rpi,
  7788. bf_get(lpfc_wcqe_xa_xri, axri),
  7789. bf_get(lpfc_wcqe_xa_status, axri),
  7790. axri->parameter);
  7791. /*
  7792. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7793. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7794. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7795. */
  7796. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7797. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7798. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7799. lpfc_sli_abts_recover_port(vport, ndlp);
  7800. }
  7801. /**
  7802. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7803. * @phba: Pointer to HBA context object.
  7804. * @pring: Pointer to driver SLI ring object.
  7805. * @iocbq: Pointer to iocb object.
  7806. *
  7807. * This function is called by the slow ring event handler
  7808. * function when there is an ASYNC event iocb in the ring.
  7809. * This function is called with no lock held.
  7810. * Currently this function handles only temperature related
  7811. * ASYNC events. The function decodes the temperature sensor
  7812. * event message and posts events for the management applications.
  7813. **/
  7814. static void
  7815. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7816. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7817. {
  7818. IOCB_t *icmd;
  7819. uint16_t evt_code;
  7820. struct temp_event temp_event_data;
  7821. struct Scsi_Host *shost;
  7822. uint32_t *iocb_w;
  7823. icmd = &iocbq->iocb;
  7824. evt_code = icmd->un.asyncstat.evt_code;
  7825. switch (evt_code) {
  7826. case ASYNC_TEMP_WARN:
  7827. case ASYNC_TEMP_SAFE:
  7828. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7829. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7830. if (evt_code == ASYNC_TEMP_WARN) {
  7831. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7832. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7833. "0347 Adapter is very hot, please take "
  7834. "corrective action. temperature : %d Celsius\n",
  7835. (uint32_t) icmd->ulpContext);
  7836. } else {
  7837. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7838. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7839. "0340 Adapter temperature is OK now. "
  7840. "temperature : %d Celsius\n",
  7841. (uint32_t) icmd->ulpContext);
  7842. }
  7843. /* Send temperature change event to applications */
  7844. shost = lpfc_shost_from_vport(phba->pport);
  7845. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7846. sizeof(temp_event_data), (char *) &temp_event_data,
  7847. LPFC_NL_VENDOR_ID);
  7848. break;
  7849. case ASYNC_STATUS_CN:
  7850. lpfc_sli_abts_err_handler(phba, iocbq);
  7851. break;
  7852. default:
  7853. iocb_w = (uint32_t *) icmd;
  7854. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7855. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7856. " evt_code 0x%x\n"
  7857. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7858. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7859. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7860. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7861. pring->ringno, icmd->un.asyncstat.evt_code,
  7862. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7863. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7864. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7865. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7866. break;
  7867. }
  7868. }
  7869. /**
  7870. * lpfc_sli_setup - SLI ring setup function
  7871. * @phba: Pointer to HBA context object.
  7872. *
  7873. * lpfc_sli_setup sets up rings of the SLI interface with
  7874. * number of iocbs per ring and iotags. This function is
  7875. * called while driver attach to the HBA and before the
  7876. * interrupts are enabled. So there is no need for locking.
  7877. *
  7878. * This function always returns 0.
  7879. **/
  7880. int
  7881. lpfc_sli_setup(struct lpfc_hba *phba)
  7882. {
  7883. int i, totiocbsize = 0;
  7884. struct lpfc_sli *psli = &phba->sli;
  7885. struct lpfc_sli_ring *pring;
  7886. psli->num_rings = MAX_CONFIGURED_RINGS;
  7887. psli->sli_flag = 0;
  7888. psli->fcp_ring = LPFC_FCP_RING;
  7889. psli->next_ring = LPFC_FCP_NEXT_RING;
  7890. psli->extra_ring = LPFC_EXTRA_RING;
  7891. psli->iocbq_lookup = NULL;
  7892. psli->iocbq_lookup_len = 0;
  7893. psli->last_iotag = 0;
  7894. for (i = 0; i < psli->num_rings; i++) {
  7895. pring = &psli->ring[i];
  7896. switch (i) {
  7897. case LPFC_FCP_RING: /* ring 0 - FCP */
  7898. /* numCiocb and numRiocb are used in config_port */
  7899. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7900. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7901. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7902. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7903. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7904. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7905. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7906. SLI3_IOCB_CMD_SIZE :
  7907. SLI2_IOCB_CMD_SIZE;
  7908. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7909. SLI3_IOCB_RSP_SIZE :
  7910. SLI2_IOCB_RSP_SIZE;
  7911. pring->iotag_ctr = 0;
  7912. pring->iotag_max =
  7913. (phba->cfg_hba_queue_depth * 2);
  7914. pring->fast_iotag = pring->iotag_max;
  7915. pring->num_mask = 0;
  7916. break;
  7917. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7918. /* numCiocb and numRiocb are used in config_port */
  7919. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7920. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7921. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7922. SLI3_IOCB_CMD_SIZE :
  7923. SLI2_IOCB_CMD_SIZE;
  7924. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7925. SLI3_IOCB_RSP_SIZE :
  7926. SLI2_IOCB_RSP_SIZE;
  7927. pring->iotag_max = phba->cfg_hba_queue_depth;
  7928. pring->num_mask = 0;
  7929. break;
  7930. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7931. /* numCiocb and numRiocb are used in config_port */
  7932. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7933. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7934. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7935. SLI3_IOCB_CMD_SIZE :
  7936. SLI2_IOCB_CMD_SIZE;
  7937. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7938. SLI3_IOCB_RSP_SIZE :
  7939. SLI2_IOCB_RSP_SIZE;
  7940. pring->fast_iotag = 0;
  7941. pring->iotag_ctr = 0;
  7942. pring->iotag_max = 4096;
  7943. pring->lpfc_sli_rcv_async_status =
  7944. lpfc_sli_async_event_handler;
  7945. pring->num_mask = LPFC_MAX_RING_MASK;
  7946. pring->prt[0].profile = 0; /* Mask 0 */
  7947. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7948. pring->prt[0].type = FC_TYPE_ELS;
  7949. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7950. lpfc_els_unsol_event;
  7951. pring->prt[1].profile = 0; /* Mask 1 */
  7952. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7953. pring->prt[1].type = FC_TYPE_ELS;
  7954. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7955. lpfc_els_unsol_event;
  7956. pring->prt[2].profile = 0; /* Mask 2 */
  7957. /* NameServer Inquiry */
  7958. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7959. /* NameServer */
  7960. pring->prt[2].type = FC_TYPE_CT;
  7961. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7962. lpfc_ct_unsol_event;
  7963. pring->prt[3].profile = 0; /* Mask 3 */
  7964. /* NameServer response */
  7965. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7966. /* NameServer */
  7967. pring->prt[3].type = FC_TYPE_CT;
  7968. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7969. lpfc_ct_unsol_event;
  7970. /* abort unsolicited sequence */
  7971. pring->prt[4].profile = 0; /* Mask 4 */
  7972. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7973. pring->prt[4].type = FC_TYPE_BLS;
  7974. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7975. lpfc_sli4_ct_abort_unsol_event;
  7976. break;
  7977. }
  7978. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7979. (pring->numRiocb * pring->sizeRiocb);
  7980. }
  7981. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7982. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7983. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7984. "SLI2 SLIM Data: x%x x%lx\n",
  7985. phba->brd_no, totiocbsize,
  7986. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7987. }
  7988. if (phba->cfg_multi_ring_support == 2)
  7989. lpfc_extra_ring_setup(phba);
  7990. return 0;
  7991. }
  7992. /**
  7993. * lpfc_sli_queue_setup - Queue initialization function
  7994. * @phba: Pointer to HBA context object.
  7995. *
  7996. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  7997. * ring. This function also initializes ring indices of each ring.
  7998. * This function is called during the initialization of the SLI
  7999. * interface of an HBA.
  8000. * This function is called with no lock held and always returns
  8001. * 1.
  8002. **/
  8003. int
  8004. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8005. {
  8006. struct lpfc_sli *psli;
  8007. struct lpfc_sli_ring *pring;
  8008. int i;
  8009. psli = &phba->sli;
  8010. spin_lock_irq(&phba->hbalock);
  8011. INIT_LIST_HEAD(&psli->mboxq);
  8012. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8013. /* Initialize list headers for txq and txcmplq as double linked lists */
  8014. for (i = 0; i < psli->num_rings; i++) {
  8015. pring = &psli->ring[i];
  8016. pring->ringno = i;
  8017. pring->next_cmdidx = 0;
  8018. pring->local_getidx = 0;
  8019. pring->cmdidx = 0;
  8020. INIT_LIST_HEAD(&pring->txq);
  8021. INIT_LIST_HEAD(&pring->txcmplq);
  8022. INIT_LIST_HEAD(&pring->iocb_continueq);
  8023. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8024. INIT_LIST_HEAD(&pring->postbufq);
  8025. }
  8026. spin_unlock_irq(&phba->hbalock);
  8027. return 1;
  8028. }
  8029. /**
  8030. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8031. * @phba: Pointer to HBA context object.
  8032. *
  8033. * This routine flushes the mailbox command subsystem. It will unconditionally
  8034. * flush all the mailbox commands in the three possible stages in the mailbox
  8035. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8036. * command; and completed mailbox command queue. It is caller's responsibility
  8037. * to make sure that the driver is in the proper state to flush the mailbox
  8038. * command sub-system. Namely, the posting of mailbox commands into the
  8039. * pending mailbox command queue from the various clients must be stopped;
  8040. * either the HBA is in a state that it will never works on the outstanding
  8041. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8042. * mailbox command has been completed.
  8043. **/
  8044. static void
  8045. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8046. {
  8047. LIST_HEAD(completions);
  8048. struct lpfc_sli *psli = &phba->sli;
  8049. LPFC_MBOXQ_t *pmb;
  8050. unsigned long iflag;
  8051. /* Flush all the mailbox commands in the mbox system */
  8052. spin_lock_irqsave(&phba->hbalock, iflag);
  8053. /* The pending mailbox command queue */
  8054. list_splice_init(&phba->sli.mboxq, &completions);
  8055. /* The outstanding active mailbox command */
  8056. if (psli->mbox_active) {
  8057. list_add_tail(&psli->mbox_active->list, &completions);
  8058. psli->mbox_active = NULL;
  8059. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8060. }
  8061. /* The completed mailbox command queue */
  8062. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8063. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8064. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8065. while (!list_empty(&completions)) {
  8066. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8067. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8068. if (pmb->mbox_cmpl)
  8069. pmb->mbox_cmpl(phba, pmb);
  8070. }
  8071. }
  8072. /**
  8073. * lpfc_sli_host_down - Vport cleanup function
  8074. * @vport: Pointer to virtual port object.
  8075. *
  8076. * lpfc_sli_host_down is called to clean up the resources
  8077. * associated with a vport before destroying virtual
  8078. * port data structures.
  8079. * This function does following operations:
  8080. * - Free discovery resources associated with this virtual
  8081. * port.
  8082. * - Free iocbs associated with this virtual port in
  8083. * the txq.
  8084. * - Send abort for all iocb commands associated with this
  8085. * vport in txcmplq.
  8086. *
  8087. * This function is called with no lock held and always returns 1.
  8088. **/
  8089. int
  8090. lpfc_sli_host_down(struct lpfc_vport *vport)
  8091. {
  8092. LIST_HEAD(completions);
  8093. struct lpfc_hba *phba = vport->phba;
  8094. struct lpfc_sli *psli = &phba->sli;
  8095. struct lpfc_sli_ring *pring;
  8096. struct lpfc_iocbq *iocb, *next_iocb;
  8097. int i;
  8098. unsigned long flags = 0;
  8099. uint16_t prev_pring_flag;
  8100. lpfc_cleanup_discovery_resources(vport);
  8101. spin_lock_irqsave(&phba->hbalock, flags);
  8102. for (i = 0; i < psli->num_rings; i++) {
  8103. pring = &psli->ring[i];
  8104. prev_pring_flag = pring->flag;
  8105. /* Only slow rings */
  8106. if (pring->ringno == LPFC_ELS_RING) {
  8107. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8108. /* Set the lpfc data pending flag */
  8109. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8110. }
  8111. /*
  8112. * Error everything on the txq since these iocbs have not been
  8113. * given to the FW yet.
  8114. */
  8115. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8116. if (iocb->vport != vport)
  8117. continue;
  8118. list_move_tail(&iocb->list, &completions);
  8119. pring->txq_cnt--;
  8120. }
  8121. /* Next issue ABTS for everything on the txcmplq */
  8122. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8123. list) {
  8124. if (iocb->vport != vport)
  8125. continue;
  8126. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8127. }
  8128. pring->flag = prev_pring_flag;
  8129. }
  8130. spin_unlock_irqrestore(&phba->hbalock, flags);
  8131. /* Cancel all the IOCBs from the completions list */
  8132. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8133. IOERR_SLI_DOWN);
  8134. return 1;
  8135. }
  8136. /**
  8137. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8138. * @phba: Pointer to HBA context object.
  8139. *
  8140. * This function cleans up all iocb, buffers, mailbox commands
  8141. * while shutting down the HBA. This function is called with no
  8142. * lock held and always returns 1.
  8143. * This function does the following to cleanup driver resources:
  8144. * - Free discovery resources for each virtual port
  8145. * - Cleanup any pending fabric iocbs
  8146. * - Iterate through the iocb txq and free each entry
  8147. * in the list.
  8148. * - Free up any buffer posted to the HBA
  8149. * - Free mailbox commands in the mailbox queue.
  8150. **/
  8151. int
  8152. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8153. {
  8154. LIST_HEAD(completions);
  8155. struct lpfc_sli *psli = &phba->sli;
  8156. struct lpfc_sli_ring *pring;
  8157. struct lpfc_dmabuf *buf_ptr;
  8158. unsigned long flags = 0;
  8159. int i;
  8160. /* Shutdown the mailbox command sub-system */
  8161. lpfc_sli_mbox_sys_shutdown(phba);
  8162. lpfc_hba_down_prep(phba);
  8163. lpfc_fabric_abort_hba(phba);
  8164. spin_lock_irqsave(&phba->hbalock, flags);
  8165. for (i = 0; i < psli->num_rings; i++) {
  8166. pring = &psli->ring[i];
  8167. /* Only slow rings */
  8168. if (pring->ringno == LPFC_ELS_RING) {
  8169. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8170. /* Set the lpfc data pending flag */
  8171. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8172. }
  8173. /*
  8174. * Error everything on the txq since these iocbs have not been
  8175. * given to the FW yet.
  8176. */
  8177. list_splice_init(&pring->txq, &completions);
  8178. pring->txq_cnt = 0;
  8179. }
  8180. spin_unlock_irqrestore(&phba->hbalock, flags);
  8181. /* Cancel all the IOCBs from the completions list */
  8182. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8183. IOERR_SLI_DOWN);
  8184. spin_lock_irqsave(&phba->hbalock, flags);
  8185. list_splice_init(&phba->elsbuf, &completions);
  8186. phba->elsbuf_cnt = 0;
  8187. phba->elsbuf_prev_cnt = 0;
  8188. spin_unlock_irqrestore(&phba->hbalock, flags);
  8189. while (!list_empty(&completions)) {
  8190. list_remove_head(&completions, buf_ptr,
  8191. struct lpfc_dmabuf, list);
  8192. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8193. kfree(buf_ptr);
  8194. }
  8195. /* Return any active mbox cmds */
  8196. del_timer_sync(&psli->mbox_tmo);
  8197. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8198. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8199. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8200. return 1;
  8201. }
  8202. /**
  8203. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8204. * @srcp: Source memory pointer.
  8205. * @destp: Destination memory pointer.
  8206. * @cnt: Number of words required to be copied.
  8207. *
  8208. * This function is used for copying data between driver memory
  8209. * and the SLI memory. This function also changes the endianness
  8210. * of each word if native endianness is different from SLI
  8211. * endianness. This function can be called with or without
  8212. * lock.
  8213. **/
  8214. void
  8215. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8216. {
  8217. uint32_t *src = srcp;
  8218. uint32_t *dest = destp;
  8219. uint32_t ldata;
  8220. int i;
  8221. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8222. ldata = *src;
  8223. ldata = le32_to_cpu(ldata);
  8224. *dest = ldata;
  8225. src++;
  8226. dest++;
  8227. }
  8228. }
  8229. /**
  8230. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8231. * @srcp: Source memory pointer.
  8232. * @destp: Destination memory pointer.
  8233. * @cnt: Number of words required to be copied.
  8234. *
  8235. * This function is used for copying data between a data structure
  8236. * with big endian representation to local endianness.
  8237. * This function can be called with or without lock.
  8238. **/
  8239. void
  8240. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8241. {
  8242. uint32_t *src = srcp;
  8243. uint32_t *dest = destp;
  8244. uint32_t ldata;
  8245. int i;
  8246. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8247. ldata = *src;
  8248. ldata = be32_to_cpu(ldata);
  8249. *dest = ldata;
  8250. src++;
  8251. dest++;
  8252. }
  8253. }
  8254. /**
  8255. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8256. * @phba: Pointer to HBA context object.
  8257. * @pring: Pointer to driver SLI ring object.
  8258. * @mp: Pointer to driver buffer object.
  8259. *
  8260. * This function is called with no lock held.
  8261. * It always return zero after adding the buffer to the postbufq
  8262. * buffer list.
  8263. **/
  8264. int
  8265. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8266. struct lpfc_dmabuf *mp)
  8267. {
  8268. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8269. later */
  8270. spin_lock_irq(&phba->hbalock);
  8271. list_add_tail(&mp->list, &pring->postbufq);
  8272. pring->postbufq_cnt++;
  8273. spin_unlock_irq(&phba->hbalock);
  8274. return 0;
  8275. }
  8276. /**
  8277. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8278. * @phba: Pointer to HBA context object.
  8279. *
  8280. * When HBQ is enabled, buffers are searched based on tags. This function
  8281. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8282. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8283. * does not conflict with tags of buffer posted for unsolicited events.
  8284. * The function returns the allocated tag. The function is called with
  8285. * no locks held.
  8286. **/
  8287. uint32_t
  8288. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8289. {
  8290. spin_lock_irq(&phba->hbalock);
  8291. phba->buffer_tag_count++;
  8292. /*
  8293. * Always set the QUE_BUFTAG_BIT to distiguish between
  8294. * a tag assigned by HBQ.
  8295. */
  8296. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8297. spin_unlock_irq(&phba->hbalock);
  8298. return phba->buffer_tag_count;
  8299. }
  8300. /**
  8301. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8302. * @phba: Pointer to HBA context object.
  8303. * @pring: Pointer to driver SLI ring object.
  8304. * @tag: Buffer tag.
  8305. *
  8306. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8307. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8308. * iocb is posted to the response ring with the tag of the buffer.
  8309. * This function searches the pring->postbufq list using the tag
  8310. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8311. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8312. * buffer is returned to the caller else NULL is returned.
  8313. * This function is called with no lock held.
  8314. **/
  8315. struct lpfc_dmabuf *
  8316. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8317. uint32_t tag)
  8318. {
  8319. struct lpfc_dmabuf *mp, *next_mp;
  8320. struct list_head *slp = &pring->postbufq;
  8321. /* Search postbufq, from the beginning, looking for a match on tag */
  8322. spin_lock_irq(&phba->hbalock);
  8323. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8324. if (mp->buffer_tag == tag) {
  8325. list_del_init(&mp->list);
  8326. pring->postbufq_cnt--;
  8327. spin_unlock_irq(&phba->hbalock);
  8328. return mp;
  8329. }
  8330. }
  8331. spin_unlock_irq(&phba->hbalock);
  8332. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8333. "0402 Cannot find virtual addr for buffer tag on "
  8334. "ring %d Data x%lx x%p x%p x%x\n",
  8335. pring->ringno, (unsigned long) tag,
  8336. slp->next, slp->prev, pring->postbufq_cnt);
  8337. return NULL;
  8338. }
  8339. /**
  8340. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8341. * @phba: Pointer to HBA context object.
  8342. * @pring: Pointer to driver SLI ring object.
  8343. * @phys: DMA address of the buffer.
  8344. *
  8345. * This function searches the buffer list using the dma_address
  8346. * of unsolicited event to find the driver's lpfc_dmabuf object
  8347. * corresponding to the dma_address. The function returns the
  8348. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8349. * This function is called by the ct and els unsolicited event
  8350. * handlers to get the buffer associated with the unsolicited
  8351. * event.
  8352. *
  8353. * This function is called with no lock held.
  8354. **/
  8355. struct lpfc_dmabuf *
  8356. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8357. dma_addr_t phys)
  8358. {
  8359. struct lpfc_dmabuf *mp, *next_mp;
  8360. struct list_head *slp = &pring->postbufq;
  8361. /* Search postbufq, from the beginning, looking for a match on phys */
  8362. spin_lock_irq(&phba->hbalock);
  8363. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8364. if (mp->phys == phys) {
  8365. list_del_init(&mp->list);
  8366. pring->postbufq_cnt--;
  8367. spin_unlock_irq(&phba->hbalock);
  8368. return mp;
  8369. }
  8370. }
  8371. spin_unlock_irq(&phba->hbalock);
  8372. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8373. "0410 Cannot find virtual addr for mapped buf on "
  8374. "ring %d Data x%llx x%p x%p x%x\n",
  8375. pring->ringno, (unsigned long long)phys,
  8376. slp->next, slp->prev, pring->postbufq_cnt);
  8377. return NULL;
  8378. }
  8379. /**
  8380. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8381. * @phba: Pointer to HBA context object.
  8382. * @cmdiocb: Pointer to driver command iocb object.
  8383. * @rspiocb: Pointer to driver response iocb object.
  8384. *
  8385. * This function is the completion handler for the abort iocbs for
  8386. * ELS commands. This function is called from the ELS ring event
  8387. * handler with no lock held. This function frees memory resources
  8388. * associated with the abort iocb.
  8389. **/
  8390. static void
  8391. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8392. struct lpfc_iocbq *rspiocb)
  8393. {
  8394. IOCB_t *irsp = &rspiocb->iocb;
  8395. uint16_t abort_iotag, abort_context;
  8396. struct lpfc_iocbq *abort_iocb = NULL;
  8397. if (irsp->ulpStatus) {
  8398. /*
  8399. * Assume that the port already completed and returned, or
  8400. * will return the iocb. Just Log the message.
  8401. */
  8402. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8403. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8404. spin_lock_irq(&phba->hbalock);
  8405. if (phba->sli_rev < LPFC_SLI_REV4) {
  8406. if (abort_iotag != 0 &&
  8407. abort_iotag <= phba->sli.last_iotag)
  8408. abort_iocb =
  8409. phba->sli.iocbq_lookup[abort_iotag];
  8410. } else
  8411. /* For sli4 the abort_tag is the XRI,
  8412. * so the abort routine puts the iotag of the iocb
  8413. * being aborted in the context field of the abort
  8414. * IOCB.
  8415. */
  8416. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8417. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8418. "0327 Cannot abort els iocb %p "
  8419. "with tag %x context %x, abort status %x, "
  8420. "abort code %x\n",
  8421. abort_iocb, abort_iotag, abort_context,
  8422. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8423. spin_unlock_irq(&phba->hbalock);
  8424. }
  8425. lpfc_sli_release_iocbq(phba, cmdiocb);
  8426. return;
  8427. }
  8428. /**
  8429. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8430. * @phba: Pointer to HBA context object.
  8431. * @cmdiocb: Pointer to driver command iocb object.
  8432. * @rspiocb: Pointer to driver response iocb object.
  8433. *
  8434. * The function is called from SLI ring event handler with no
  8435. * lock held. This function is the completion handler for ELS commands
  8436. * which are aborted. The function frees memory resources used for
  8437. * the aborted ELS commands.
  8438. **/
  8439. static void
  8440. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8441. struct lpfc_iocbq *rspiocb)
  8442. {
  8443. IOCB_t *irsp = &rspiocb->iocb;
  8444. /* ELS cmd tag <ulpIoTag> completes */
  8445. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8446. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8447. "x%x x%x x%x\n",
  8448. irsp->ulpIoTag, irsp->ulpStatus,
  8449. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8450. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8451. lpfc_ct_free_iocb(phba, cmdiocb);
  8452. else
  8453. lpfc_els_free_iocb(phba, cmdiocb);
  8454. return;
  8455. }
  8456. /**
  8457. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8458. * @phba: Pointer to HBA context object.
  8459. * @pring: Pointer to driver SLI ring object.
  8460. * @cmdiocb: Pointer to driver command iocb object.
  8461. *
  8462. * This function issues an abort iocb for the provided command iocb down to
  8463. * the port. Other than the case the outstanding command iocb is an abort
  8464. * request, this function issues abort out unconditionally. This function is
  8465. * called with hbalock held. The function returns 0 when it fails due to
  8466. * memory allocation failure or when the command iocb is an abort request.
  8467. **/
  8468. static int
  8469. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8470. struct lpfc_iocbq *cmdiocb)
  8471. {
  8472. struct lpfc_vport *vport = cmdiocb->vport;
  8473. struct lpfc_iocbq *abtsiocbp;
  8474. IOCB_t *icmd = NULL;
  8475. IOCB_t *iabt = NULL;
  8476. int retval;
  8477. /*
  8478. * There are certain command types we don't want to abort. And we
  8479. * don't want to abort commands that are already in the process of
  8480. * being aborted.
  8481. */
  8482. icmd = &cmdiocb->iocb;
  8483. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8484. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8485. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8486. return 0;
  8487. /* issue ABTS for this IOCB based on iotag */
  8488. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8489. if (abtsiocbp == NULL)
  8490. return 0;
  8491. /* This signals the response to set the correct status
  8492. * before calling the completion handler
  8493. */
  8494. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8495. iabt = &abtsiocbp->iocb;
  8496. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8497. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8498. if (phba->sli_rev == LPFC_SLI_REV4) {
  8499. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8500. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8501. }
  8502. else
  8503. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8504. iabt->ulpLe = 1;
  8505. iabt->ulpClass = icmd->ulpClass;
  8506. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8507. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8508. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8509. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8510. if (phba->link_state >= LPFC_LINK_UP)
  8511. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8512. else
  8513. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8514. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8515. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8516. "0339 Abort xri x%x, original iotag x%x, "
  8517. "abort cmd iotag x%x\n",
  8518. iabt->un.acxri.abortIoTag,
  8519. iabt->un.acxri.abortContextTag,
  8520. abtsiocbp->iotag);
  8521. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8522. if (retval)
  8523. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8524. /*
  8525. * Caller to this routine should check for IOCB_ERROR
  8526. * and handle it properly. This routine no longer removes
  8527. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8528. */
  8529. return retval;
  8530. }
  8531. /**
  8532. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8533. * @phba: Pointer to HBA context object.
  8534. * @pring: Pointer to driver SLI ring object.
  8535. * @cmdiocb: Pointer to driver command iocb object.
  8536. *
  8537. * This function issues an abort iocb for the provided command iocb. In case
  8538. * of unloading, the abort iocb will not be issued to commands on the ELS
  8539. * ring. Instead, the callback function shall be changed to those commands
  8540. * so that nothing happens when them finishes. This function is called with
  8541. * hbalock held. The function returns 0 when the command iocb is an abort
  8542. * request.
  8543. **/
  8544. int
  8545. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8546. struct lpfc_iocbq *cmdiocb)
  8547. {
  8548. struct lpfc_vport *vport = cmdiocb->vport;
  8549. int retval = IOCB_ERROR;
  8550. IOCB_t *icmd = NULL;
  8551. /*
  8552. * There are certain command types we don't want to abort. And we
  8553. * don't want to abort commands that are already in the process of
  8554. * being aborted.
  8555. */
  8556. icmd = &cmdiocb->iocb;
  8557. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8558. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8559. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8560. return 0;
  8561. /*
  8562. * If we're unloading, don't abort iocb on the ELS ring, but change
  8563. * the callback so that nothing happens when it finishes.
  8564. */
  8565. if ((vport->load_flag & FC_UNLOADING) &&
  8566. (pring->ringno == LPFC_ELS_RING)) {
  8567. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8568. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8569. else
  8570. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8571. goto abort_iotag_exit;
  8572. }
  8573. /* Now, we try to issue the abort to the cmdiocb out */
  8574. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8575. abort_iotag_exit:
  8576. /*
  8577. * Caller to this routine should check for IOCB_ERROR
  8578. * and handle it properly. This routine no longer removes
  8579. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8580. */
  8581. return retval;
  8582. }
  8583. /**
  8584. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8585. * @phba: Pointer to HBA context object.
  8586. * @pring: Pointer to driver SLI ring object.
  8587. *
  8588. * This function aborts all iocbs in the given ring and frees all the iocb
  8589. * objects in txq. This function issues abort iocbs unconditionally for all
  8590. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8591. * to complete before the return of this function. The caller is not required
  8592. * to hold any locks.
  8593. **/
  8594. static void
  8595. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8596. {
  8597. LIST_HEAD(completions);
  8598. struct lpfc_iocbq *iocb, *next_iocb;
  8599. if (pring->ringno == LPFC_ELS_RING)
  8600. lpfc_fabric_abort_hba(phba);
  8601. spin_lock_irq(&phba->hbalock);
  8602. /* Take off all the iocbs on txq for cancelling */
  8603. list_splice_init(&pring->txq, &completions);
  8604. pring->txq_cnt = 0;
  8605. /* Next issue ABTS for everything on the txcmplq */
  8606. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8607. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8608. spin_unlock_irq(&phba->hbalock);
  8609. /* Cancel all the IOCBs from the completions list */
  8610. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8611. IOERR_SLI_ABORTED);
  8612. }
  8613. /**
  8614. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8615. * @phba: pointer to lpfc HBA data structure.
  8616. *
  8617. * This routine will abort all pending and outstanding iocbs to an HBA.
  8618. **/
  8619. void
  8620. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8621. {
  8622. struct lpfc_sli *psli = &phba->sli;
  8623. struct lpfc_sli_ring *pring;
  8624. int i;
  8625. for (i = 0; i < psli->num_rings; i++) {
  8626. pring = &psli->ring[i];
  8627. lpfc_sli_iocb_ring_abort(phba, pring);
  8628. }
  8629. }
  8630. /**
  8631. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8632. * @iocbq: Pointer to driver iocb object.
  8633. * @vport: Pointer to driver virtual port object.
  8634. * @tgt_id: SCSI ID of the target.
  8635. * @lun_id: LUN ID of the scsi device.
  8636. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8637. *
  8638. * This function acts as an iocb filter for functions which abort or count
  8639. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8640. * 0 if the filtering criteria is met for the given iocb and will return
  8641. * 1 if the filtering criteria is not met.
  8642. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8643. * given iocb is for the SCSI device specified by vport, tgt_id and
  8644. * lun_id parameter.
  8645. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8646. * given iocb is for the SCSI target specified by vport and tgt_id
  8647. * parameters.
  8648. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8649. * given iocb is for the SCSI host associated with the given vport.
  8650. * This function is called with no locks held.
  8651. **/
  8652. static int
  8653. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8654. uint16_t tgt_id, uint64_t lun_id,
  8655. lpfc_ctx_cmd ctx_cmd)
  8656. {
  8657. struct lpfc_scsi_buf *lpfc_cmd;
  8658. int rc = 1;
  8659. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8660. return rc;
  8661. if (iocbq->vport != vport)
  8662. return rc;
  8663. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8664. if (lpfc_cmd->pCmd == NULL)
  8665. return rc;
  8666. switch (ctx_cmd) {
  8667. case LPFC_CTX_LUN:
  8668. if ((lpfc_cmd->rdata->pnode) &&
  8669. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8670. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8671. rc = 0;
  8672. break;
  8673. case LPFC_CTX_TGT:
  8674. if ((lpfc_cmd->rdata->pnode) &&
  8675. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8676. rc = 0;
  8677. break;
  8678. case LPFC_CTX_HOST:
  8679. rc = 0;
  8680. break;
  8681. default:
  8682. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8683. __func__, ctx_cmd);
  8684. break;
  8685. }
  8686. return rc;
  8687. }
  8688. /**
  8689. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8690. * @vport: Pointer to virtual port.
  8691. * @tgt_id: SCSI ID of the target.
  8692. * @lun_id: LUN ID of the scsi device.
  8693. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8694. *
  8695. * This function returns number of FCP commands pending for the vport.
  8696. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8697. * commands pending on the vport associated with SCSI device specified
  8698. * by tgt_id and lun_id parameters.
  8699. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8700. * commands pending on the vport associated with SCSI target specified
  8701. * by tgt_id parameter.
  8702. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8703. * commands pending on the vport.
  8704. * This function returns the number of iocbs which satisfy the filter.
  8705. * This function is called without any lock held.
  8706. **/
  8707. int
  8708. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8709. lpfc_ctx_cmd ctx_cmd)
  8710. {
  8711. struct lpfc_hba *phba = vport->phba;
  8712. struct lpfc_iocbq *iocbq;
  8713. int sum, i;
  8714. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8715. iocbq = phba->sli.iocbq_lookup[i];
  8716. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8717. ctx_cmd) == 0)
  8718. sum++;
  8719. }
  8720. return sum;
  8721. }
  8722. /**
  8723. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8724. * @phba: Pointer to HBA context object
  8725. * @cmdiocb: Pointer to command iocb object.
  8726. * @rspiocb: Pointer to response iocb object.
  8727. *
  8728. * This function is called when an aborted FCP iocb completes. This
  8729. * function is called by the ring event handler with no lock held.
  8730. * This function frees the iocb.
  8731. **/
  8732. void
  8733. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8734. struct lpfc_iocbq *rspiocb)
  8735. {
  8736. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8737. "3096 ABORT_XRI_CN completing on xri x%x "
  8738. "original iotag x%x, abort cmd iotag x%x "
  8739. "status 0x%x, reason 0x%x\n",
  8740. cmdiocb->iocb.un.acxri.abortContextTag,
  8741. cmdiocb->iocb.un.acxri.abortIoTag,
  8742. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8743. rspiocb->iocb.un.ulpWord[4]);
  8744. lpfc_sli_release_iocbq(phba, cmdiocb);
  8745. return;
  8746. }
  8747. /**
  8748. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8749. * @vport: Pointer to virtual port.
  8750. * @pring: Pointer to driver SLI ring object.
  8751. * @tgt_id: SCSI ID of the target.
  8752. * @lun_id: LUN ID of the scsi device.
  8753. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8754. *
  8755. * This function sends an abort command for every SCSI command
  8756. * associated with the given virtual port pending on the ring
  8757. * filtered by lpfc_sli_validate_fcp_iocb function.
  8758. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8759. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8760. * parameters
  8761. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8762. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8763. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8764. * FCP iocbs associated with virtual port.
  8765. * This function returns number of iocbs it failed to abort.
  8766. * This function is called with no locks held.
  8767. **/
  8768. int
  8769. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8770. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8771. {
  8772. struct lpfc_hba *phba = vport->phba;
  8773. struct lpfc_iocbq *iocbq;
  8774. struct lpfc_iocbq *abtsiocb;
  8775. IOCB_t *cmd = NULL;
  8776. int errcnt = 0, ret_val = 0;
  8777. int i;
  8778. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8779. iocbq = phba->sli.iocbq_lookup[i];
  8780. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8781. abort_cmd) != 0)
  8782. continue;
  8783. /* issue ABTS for this IOCB based on iotag */
  8784. abtsiocb = lpfc_sli_get_iocbq(phba);
  8785. if (abtsiocb == NULL) {
  8786. errcnt++;
  8787. continue;
  8788. }
  8789. cmd = &iocbq->iocb;
  8790. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8791. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8792. if (phba->sli_rev == LPFC_SLI_REV4)
  8793. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8794. else
  8795. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8796. abtsiocb->iocb.ulpLe = 1;
  8797. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8798. abtsiocb->vport = phba->pport;
  8799. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8800. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8801. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8802. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8803. if (lpfc_is_link_up(phba))
  8804. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8805. else
  8806. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8807. /* Setup callback routine and issue the command. */
  8808. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8809. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8810. abtsiocb, 0);
  8811. if (ret_val == IOCB_ERROR) {
  8812. lpfc_sli_release_iocbq(phba, abtsiocb);
  8813. errcnt++;
  8814. continue;
  8815. }
  8816. }
  8817. return errcnt;
  8818. }
  8819. /**
  8820. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8821. * @phba: Pointer to HBA context object.
  8822. * @cmdiocbq: Pointer to command iocb.
  8823. * @rspiocbq: Pointer to response iocb.
  8824. *
  8825. * This function is the completion handler for iocbs issued using
  8826. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8827. * ring event handler function without any lock held. This function
  8828. * can be called from both worker thread context and interrupt
  8829. * context. This function also can be called from other thread which
  8830. * cleans up the SLI layer objects.
  8831. * This function copy the contents of the response iocb to the
  8832. * response iocb memory object provided by the caller of
  8833. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8834. * sleeps for the iocb completion.
  8835. **/
  8836. static void
  8837. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8838. struct lpfc_iocbq *cmdiocbq,
  8839. struct lpfc_iocbq *rspiocbq)
  8840. {
  8841. wait_queue_head_t *pdone_q;
  8842. unsigned long iflags;
  8843. struct lpfc_scsi_buf *lpfc_cmd;
  8844. spin_lock_irqsave(&phba->hbalock, iflags);
  8845. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8846. if (cmdiocbq->context2 && rspiocbq)
  8847. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8848. &rspiocbq->iocb, sizeof(IOCB_t));
  8849. /* Set the exchange busy flag for task management commands */
  8850. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8851. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8852. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8853. cur_iocbq);
  8854. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8855. }
  8856. pdone_q = cmdiocbq->context_un.wait_queue;
  8857. if (pdone_q)
  8858. wake_up(pdone_q);
  8859. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8860. return;
  8861. }
  8862. /**
  8863. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8864. * @phba: Pointer to HBA context object..
  8865. * @piocbq: Pointer to command iocb.
  8866. * @flag: Flag to test.
  8867. *
  8868. * This routine grabs the hbalock and then test the iocb_flag to
  8869. * see if the passed in flag is set.
  8870. * Returns:
  8871. * 1 if flag is set.
  8872. * 0 if flag is not set.
  8873. **/
  8874. static int
  8875. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8876. struct lpfc_iocbq *piocbq, uint32_t flag)
  8877. {
  8878. unsigned long iflags;
  8879. int ret;
  8880. spin_lock_irqsave(&phba->hbalock, iflags);
  8881. ret = piocbq->iocb_flag & flag;
  8882. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8883. return ret;
  8884. }
  8885. /**
  8886. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8887. * @phba: Pointer to HBA context object..
  8888. * @pring: Pointer to sli ring.
  8889. * @piocb: Pointer to command iocb.
  8890. * @prspiocbq: Pointer to response iocb.
  8891. * @timeout: Timeout in number of seconds.
  8892. *
  8893. * This function issues the iocb to firmware and waits for the
  8894. * iocb to complete. If the iocb command is not
  8895. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8896. * Caller should not free the iocb resources if this function
  8897. * returns IOCB_TIMEDOUT.
  8898. * The function waits for the iocb completion using an
  8899. * non-interruptible wait.
  8900. * This function will sleep while waiting for iocb completion.
  8901. * So, this function should not be called from any context which
  8902. * does not allow sleeping. Due to the same reason, this function
  8903. * cannot be called with interrupt disabled.
  8904. * This function assumes that the iocb completions occur while
  8905. * this function sleep. So, this function cannot be called from
  8906. * the thread which process iocb completion for this ring.
  8907. * This function clears the iocb_flag of the iocb object before
  8908. * issuing the iocb and the iocb completion handler sets this
  8909. * flag and wakes this thread when the iocb completes.
  8910. * The contents of the response iocb will be copied to prspiocbq
  8911. * by the completion handler when the command completes.
  8912. * This function returns IOCB_SUCCESS when success.
  8913. * This function is called with no lock held.
  8914. **/
  8915. int
  8916. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8917. uint32_t ring_number,
  8918. struct lpfc_iocbq *piocb,
  8919. struct lpfc_iocbq *prspiocbq,
  8920. uint32_t timeout)
  8921. {
  8922. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8923. long timeleft, timeout_req = 0;
  8924. int retval = IOCB_SUCCESS;
  8925. uint32_t creg_val;
  8926. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8927. /*
  8928. * If the caller has provided a response iocbq buffer, then context2
  8929. * is NULL or its an error.
  8930. */
  8931. if (prspiocbq) {
  8932. if (piocb->context2)
  8933. return IOCB_ERROR;
  8934. piocb->context2 = prspiocbq;
  8935. }
  8936. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8937. piocb->context_un.wait_queue = &done_q;
  8938. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8939. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8940. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8941. return IOCB_ERROR;
  8942. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8943. writel(creg_val, phba->HCregaddr);
  8944. readl(phba->HCregaddr); /* flush */
  8945. }
  8946. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8947. SLI_IOCB_RET_IOCB);
  8948. if (retval == IOCB_SUCCESS) {
  8949. timeout_req = timeout * HZ;
  8950. timeleft = wait_event_timeout(done_q,
  8951. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8952. timeout_req);
  8953. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8954. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8955. "0331 IOCB wake signaled\n");
  8956. } else if (timeleft == 0) {
  8957. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8958. "0338 IOCB wait timeout error - no "
  8959. "wake response Data x%x\n", timeout);
  8960. retval = IOCB_TIMEDOUT;
  8961. } else {
  8962. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8963. "0330 IOCB wake NOT set, "
  8964. "Data x%x x%lx\n",
  8965. timeout, (timeleft / jiffies));
  8966. retval = IOCB_TIMEDOUT;
  8967. }
  8968. } else if (retval == IOCB_BUSY) {
  8969. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8970. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8971. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8972. return retval;
  8973. } else {
  8974. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8975. "0332 IOCB wait issue failed, Data x%x\n",
  8976. retval);
  8977. retval = IOCB_ERROR;
  8978. }
  8979. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8980. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8981. return IOCB_ERROR;
  8982. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8983. writel(creg_val, phba->HCregaddr);
  8984. readl(phba->HCregaddr); /* flush */
  8985. }
  8986. if (prspiocbq)
  8987. piocb->context2 = NULL;
  8988. piocb->context_un.wait_queue = NULL;
  8989. piocb->iocb_cmpl = NULL;
  8990. return retval;
  8991. }
  8992. /**
  8993. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8994. * @phba: Pointer to HBA context object.
  8995. * @pmboxq: Pointer to driver mailbox object.
  8996. * @timeout: Timeout in number of seconds.
  8997. *
  8998. * This function issues the mailbox to firmware and waits for the
  8999. * mailbox command to complete. If the mailbox command is not
  9000. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9001. * The function waits for the mailbox completion using an
  9002. * interruptible wait. If the thread is woken up due to a
  9003. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9004. * should not free the mailbox resources, if this function returns
  9005. * MBX_TIMEOUT.
  9006. * This function will sleep while waiting for mailbox completion.
  9007. * So, this function should not be called from any context which
  9008. * does not allow sleeping. Due to the same reason, this function
  9009. * cannot be called with interrupt disabled.
  9010. * This function assumes that the mailbox completion occurs while
  9011. * this function sleep. So, this function cannot be called from
  9012. * the worker thread which processes mailbox completion.
  9013. * This function is called in the context of HBA management
  9014. * applications.
  9015. * This function returns MBX_SUCCESS when successful.
  9016. * This function is called with no lock held.
  9017. **/
  9018. int
  9019. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9020. uint32_t timeout)
  9021. {
  9022. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9023. int retval;
  9024. unsigned long flag;
  9025. /* The caller must leave context1 empty. */
  9026. if (pmboxq->context1)
  9027. return MBX_NOT_FINISHED;
  9028. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9029. /* setup wake call as IOCB callback */
  9030. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9031. /* setup context field to pass wait_queue pointer to wake function */
  9032. pmboxq->context1 = &done_q;
  9033. /* now issue the command */
  9034. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9035. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9036. wait_event_interruptible_timeout(done_q,
  9037. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9038. timeout * HZ);
  9039. spin_lock_irqsave(&phba->hbalock, flag);
  9040. pmboxq->context1 = NULL;
  9041. /*
  9042. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9043. * else do not free the resources.
  9044. */
  9045. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9046. retval = MBX_SUCCESS;
  9047. lpfc_sli4_swap_str(phba, pmboxq);
  9048. } else {
  9049. retval = MBX_TIMEOUT;
  9050. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9051. }
  9052. spin_unlock_irqrestore(&phba->hbalock, flag);
  9053. }
  9054. return retval;
  9055. }
  9056. /**
  9057. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9058. * @phba: Pointer to HBA context.
  9059. *
  9060. * This function is called to shutdown the driver's mailbox sub-system.
  9061. * It first marks the mailbox sub-system is in a block state to prevent
  9062. * the asynchronous mailbox command from issued off the pending mailbox
  9063. * command queue. If the mailbox command sub-system shutdown is due to
  9064. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9065. * the mailbox sub-system flush routine to forcefully bring down the
  9066. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9067. * as with offline or HBA function reset), this routine will wait for the
  9068. * outstanding mailbox command to complete before invoking the mailbox
  9069. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9070. **/
  9071. void
  9072. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  9073. {
  9074. struct lpfc_sli *psli = &phba->sli;
  9075. unsigned long timeout;
  9076. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9077. spin_lock_irq(&phba->hbalock);
  9078. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9079. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9080. /* Determine how long we might wait for the active mailbox
  9081. * command to be gracefully completed by firmware.
  9082. */
  9083. if (phba->sli.mbox_active)
  9084. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9085. phba->sli.mbox_active) *
  9086. 1000) + jiffies;
  9087. spin_unlock_irq(&phba->hbalock);
  9088. while (phba->sli.mbox_active) {
  9089. /* Check active mailbox complete status every 2ms */
  9090. msleep(2);
  9091. if (time_after(jiffies, timeout))
  9092. /* Timeout, let the mailbox flush routine to
  9093. * forcefully release active mailbox command
  9094. */
  9095. break;
  9096. }
  9097. } else
  9098. spin_unlock_irq(&phba->hbalock);
  9099. lpfc_sli_mbox_sys_flush(phba);
  9100. }
  9101. /**
  9102. * lpfc_sli_eratt_read - read sli-3 error attention events
  9103. * @phba: Pointer to HBA context.
  9104. *
  9105. * This function is called to read the SLI3 device error attention registers
  9106. * for possible error attention events. The caller must hold the hostlock
  9107. * with spin_lock_irq().
  9108. *
  9109. * This function returns 1 when there is Error Attention in the Host Attention
  9110. * Register and returns 0 otherwise.
  9111. **/
  9112. static int
  9113. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9114. {
  9115. uint32_t ha_copy;
  9116. /* Read chip Host Attention (HA) register */
  9117. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9118. goto unplug_err;
  9119. if (ha_copy & HA_ERATT) {
  9120. /* Read host status register to retrieve error event */
  9121. if (lpfc_sli_read_hs(phba))
  9122. goto unplug_err;
  9123. /* Check if there is a deferred error condition is active */
  9124. if ((HS_FFER1 & phba->work_hs) &&
  9125. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9126. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9127. phba->hba_flag |= DEFER_ERATT;
  9128. /* Clear all interrupt enable conditions */
  9129. writel(0, phba->HCregaddr);
  9130. readl(phba->HCregaddr);
  9131. }
  9132. /* Set the driver HA work bitmap */
  9133. phba->work_ha |= HA_ERATT;
  9134. /* Indicate polling handles this ERATT */
  9135. phba->hba_flag |= HBA_ERATT_HANDLED;
  9136. return 1;
  9137. }
  9138. return 0;
  9139. unplug_err:
  9140. /* Set the driver HS work bitmap */
  9141. phba->work_hs |= UNPLUG_ERR;
  9142. /* Set the driver HA work bitmap */
  9143. phba->work_ha |= HA_ERATT;
  9144. /* Indicate polling handles this ERATT */
  9145. phba->hba_flag |= HBA_ERATT_HANDLED;
  9146. return 1;
  9147. }
  9148. /**
  9149. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9150. * @phba: Pointer to HBA context.
  9151. *
  9152. * This function is called to read the SLI4 device error attention registers
  9153. * for possible error attention events. The caller must hold the hostlock
  9154. * with spin_lock_irq().
  9155. *
  9156. * This function returns 1 when there is Error Attention in the Host Attention
  9157. * Register and returns 0 otherwise.
  9158. **/
  9159. static int
  9160. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9161. {
  9162. uint32_t uerr_sta_hi, uerr_sta_lo;
  9163. uint32_t if_type, portsmphr;
  9164. struct lpfc_register portstat_reg;
  9165. /*
  9166. * For now, use the SLI4 device internal unrecoverable error
  9167. * registers for error attention. This can be changed later.
  9168. */
  9169. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9170. switch (if_type) {
  9171. case LPFC_SLI_INTF_IF_TYPE_0:
  9172. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9173. &uerr_sta_lo) ||
  9174. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9175. &uerr_sta_hi)) {
  9176. phba->work_hs |= UNPLUG_ERR;
  9177. phba->work_ha |= HA_ERATT;
  9178. phba->hba_flag |= HBA_ERATT_HANDLED;
  9179. return 1;
  9180. }
  9181. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9182. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9183. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9184. "1423 HBA Unrecoverable error: "
  9185. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9186. "ue_mask_lo_reg=0x%x, "
  9187. "ue_mask_hi_reg=0x%x\n",
  9188. uerr_sta_lo, uerr_sta_hi,
  9189. phba->sli4_hba.ue_mask_lo,
  9190. phba->sli4_hba.ue_mask_hi);
  9191. phba->work_status[0] = uerr_sta_lo;
  9192. phba->work_status[1] = uerr_sta_hi;
  9193. phba->work_ha |= HA_ERATT;
  9194. phba->hba_flag |= HBA_ERATT_HANDLED;
  9195. return 1;
  9196. }
  9197. break;
  9198. case LPFC_SLI_INTF_IF_TYPE_2:
  9199. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9200. &portstat_reg.word0) ||
  9201. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9202. &portsmphr)){
  9203. phba->work_hs |= UNPLUG_ERR;
  9204. phba->work_ha |= HA_ERATT;
  9205. phba->hba_flag |= HBA_ERATT_HANDLED;
  9206. return 1;
  9207. }
  9208. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9209. phba->work_status[0] =
  9210. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9211. phba->work_status[1] =
  9212. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9213. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9214. "2885 Port Status Event: "
  9215. "port status reg 0x%x, "
  9216. "port smphr reg 0x%x, "
  9217. "error 1=0x%x, error 2=0x%x\n",
  9218. portstat_reg.word0,
  9219. portsmphr,
  9220. phba->work_status[0],
  9221. phba->work_status[1]);
  9222. phba->work_ha |= HA_ERATT;
  9223. phba->hba_flag |= HBA_ERATT_HANDLED;
  9224. return 1;
  9225. }
  9226. break;
  9227. case LPFC_SLI_INTF_IF_TYPE_1:
  9228. default:
  9229. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9230. "2886 HBA Error Attention on unsupported "
  9231. "if type %d.", if_type);
  9232. return 1;
  9233. }
  9234. return 0;
  9235. }
  9236. /**
  9237. * lpfc_sli_check_eratt - check error attention events
  9238. * @phba: Pointer to HBA context.
  9239. *
  9240. * This function is called from timer soft interrupt context to check HBA's
  9241. * error attention register bit for error attention events.
  9242. *
  9243. * This function returns 1 when there is Error Attention in the Host Attention
  9244. * Register and returns 0 otherwise.
  9245. **/
  9246. int
  9247. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9248. {
  9249. uint32_t ha_copy;
  9250. /* If somebody is waiting to handle an eratt, don't process it
  9251. * here. The brdkill function will do this.
  9252. */
  9253. if (phba->link_flag & LS_IGNORE_ERATT)
  9254. return 0;
  9255. /* Check if interrupt handler handles this ERATT */
  9256. spin_lock_irq(&phba->hbalock);
  9257. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9258. /* Interrupt handler has handled ERATT */
  9259. spin_unlock_irq(&phba->hbalock);
  9260. return 0;
  9261. }
  9262. /*
  9263. * If there is deferred error attention, do not check for error
  9264. * attention
  9265. */
  9266. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9267. spin_unlock_irq(&phba->hbalock);
  9268. return 0;
  9269. }
  9270. /* If PCI channel is offline, don't process it */
  9271. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9272. spin_unlock_irq(&phba->hbalock);
  9273. return 0;
  9274. }
  9275. switch (phba->sli_rev) {
  9276. case LPFC_SLI_REV2:
  9277. case LPFC_SLI_REV3:
  9278. /* Read chip Host Attention (HA) register */
  9279. ha_copy = lpfc_sli_eratt_read(phba);
  9280. break;
  9281. case LPFC_SLI_REV4:
  9282. /* Read device Uncoverable Error (UERR) registers */
  9283. ha_copy = lpfc_sli4_eratt_read(phba);
  9284. break;
  9285. default:
  9286. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9287. "0299 Invalid SLI revision (%d)\n",
  9288. phba->sli_rev);
  9289. ha_copy = 0;
  9290. break;
  9291. }
  9292. spin_unlock_irq(&phba->hbalock);
  9293. return ha_copy;
  9294. }
  9295. /**
  9296. * lpfc_intr_state_check - Check device state for interrupt handling
  9297. * @phba: Pointer to HBA context.
  9298. *
  9299. * This inline routine checks whether a device or its PCI slot is in a state
  9300. * that the interrupt should be handled.
  9301. *
  9302. * This function returns 0 if the device or the PCI slot is in a state that
  9303. * interrupt should be handled, otherwise -EIO.
  9304. */
  9305. static inline int
  9306. lpfc_intr_state_check(struct lpfc_hba *phba)
  9307. {
  9308. /* If the pci channel is offline, ignore all the interrupts */
  9309. if (unlikely(pci_channel_offline(phba->pcidev)))
  9310. return -EIO;
  9311. /* Update device level interrupt statistics */
  9312. phba->sli.slistat.sli_intr++;
  9313. /* Ignore all interrupts during initialization. */
  9314. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9315. return -EIO;
  9316. return 0;
  9317. }
  9318. /**
  9319. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9320. * @irq: Interrupt number.
  9321. * @dev_id: The device context pointer.
  9322. *
  9323. * This function is directly called from the PCI layer as an interrupt
  9324. * service routine when device with SLI-3 interface spec is enabled with
  9325. * MSI-X multi-message interrupt mode and there are slow-path events in
  9326. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9327. * interrupt mode, this function is called as part of the device-level
  9328. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9329. * is undergoing initialization, the interrupt handler will not process
  9330. * the interrupt. The link attention and ELS ring attention events are
  9331. * handled by the worker thread. The interrupt handler signals the worker
  9332. * thread and returns for these events. This function is called without
  9333. * any lock held. It gets the hbalock to access and update SLI data
  9334. * structures.
  9335. *
  9336. * This function returns IRQ_HANDLED when interrupt is handled else it
  9337. * returns IRQ_NONE.
  9338. **/
  9339. irqreturn_t
  9340. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9341. {
  9342. struct lpfc_hba *phba;
  9343. uint32_t ha_copy, hc_copy;
  9344. uint32_t work_ha_copy;
  9345. unsigned long status;
  9346. unsigned long iflag;
  9347. uint32_t control;
  9348. MAILBOX_t *mbox, *pmbox;
  9349. struct lpfc_vport *vport;
  9350. struct lpfc_nodelist *ndlp;
  9351. struct lpfc_dmabuf *mp;
  9352. LPFC_MBOXQ_t *pmb;
  9353. int rc;
  9354. /*
  9355. * Get the driver's phba structure from the dev_id and
  9356. * assume the HBA is not interrupting.
  9357. */
  9358. phba = (struct lpfc_hba *)dev_id;
  9359. if (unlikely(!phba))
  9360. return IRQ_NONE;
  9361. /*
  9362. * Stuff needs to be attented to when this function is invoked as an
  9363. * individual interrupt handler in MSI-X multi-message interrupt mode
  9364. */
  9365. if (phba->intr_type == MSIX) {
  9366. /* Check device state for handling interrupt */
  9367. if (lpfc_intr_state_check(phba))
  9368. return IRQ_NONE;
  9369. /* Need to read HA REG for slow-path events */
  9370. spin_lock_irqsave(&phba->hbalock, iflag);
  9371. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9372. goto unplug_error;
  9373. /* If somebody is waiting to handle an eratt don't process it
  9374. * here. The brdkill function will do this.
  9375. */
  9376. if (phba->link_flag & LS_IGNORE_ERATT)
  9377. ha_copy &= ~HA_ERATT;
  9378. /* Check the need for handling ERATT in interrupt handler */
  9379. if (ha_copy & HA_ERATT) {
  9380. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9381. /* ERATT polling has handled ERATT */
  9382. ha_copy &= ~HA_ERATT;
  9383. else
  9384. /* Indicate interrupt handler handles ERATT */
  9385. phba->hba_flag |= HBA_ERATT_HANDLED;
  9386. }
  9387. /*
  9388. * If there is deferred error attention, do not check for any
  9389. * interrupt.
  9390. */
  9391. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9392. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9393. return IRQ_NONE;
  9394. }
  9395. /* Clear up only attention source related to slow-path */
  9396. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9397. goto unplug_error;
  9398. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9399. HC_LAINT_ENA | HC_ERINT_ENA),
  9400. phba->HCregaddr);
  9401. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9402. phba->HAregaddr);
  9403. writel(hc_copy, phba->HCregaddr);
  9404. readl(phba->HAregaddr); /* flush */
  9405. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9406. } else
  9407. ha_copy = phba->ha_copy;
  9408. work_ha_copy = ha_copy & phba->work_ha_mask;
  9409. if (work_ha_copy) {
  9410. if (work_ha_copy & HA_LATT) {
  9411. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9412. /*
  9413. * Turn off Link Attention interrupts
  9414. * until CLEAR_LA done
  9415. */
  9416. spin_lock_irqsave(&phba->hbalock, iflag);
  9417. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9418. if (lpfc_readl(phba->HCregaddr, &control))
  9419. goto unplug_error;
  9420. control &= ~HC_LAINT_ENA;
  9421. writel(control, phba->HCregaddr);
  9422. readl(phba->HCregaddr); /* flush */
  9423. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9424. }
  9425. else
  9426. work_ha_copy &= ~HA_LATT;
  9427. }
  9428. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9429. /*
  9430. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9431. * the only slow ring.
  9432. */
  9433. status = (work_ha_copy &
  9434. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9435. status >>= (4*LPFC_ELS_RING);
  9436. if (status & HA_RXMASK) {
  9437. spin_lock_irqsave(&phba->hbalock, iflag);
  9438. if (lpfc_readl(phba->HCregaddr, &control))
  9439. goto unplug_error;
  9440. lpfc_debugfs_slow_ring_trc(phba,
  9441. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9442. control, status,
  9443. (uint32_t)phba->sli.slistat.sli_intr);
  9444. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9445. lpfc_debugfs_slow_ring_trc(phba,
  9446. "ISR Disable ring:"
  9447. "pwork:x%x hawork:x%x wait:x%x",
  9448. phba->work_ha, work_ha_copy,
  9449. (uint32_t)((unsigned long)
  9450. &phba->work_waitq));
  9451. control &=
  9452. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9453. writel(control, phba->HCregaddr);
  9454. readl(phba->HCregaddr); /* flush */
  9455. }
  9456. else {
  9457. lpfc_debugfs_slow_ring_trc(phba,
  9458. "ISR slow ring: pwork:"
  9459. "x%x hawork:x%x wait:x%x",
  9460. phba->work_ha, work_ha_copy,
  9461. (uint32_t)((unsigned long)
  9462. &phba->work_waitq));
  9463. }
  9464. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9465. }
  9466. }
  9467. spin_lock_irqsave(&phba->hbalock, iflag);
  9468. if (work_ha_copy & HA_ERATT) {
  9469. if (lpfc_sli_read_hs(phba))
  9470. goto unplug_error;
  9471. /*
  9472. * Check if there is a deferred error condition
  9473. * is active
  9474. */
  9475. if ((HS_FFER1 & phba->work_hs) &&
  9476. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9477. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9478. phba->work_hs)) {
  9479. phba->hba_flag |= DEFER_ERATT;
  9480. /* Clear all interrupt enable conditions */
  9481. writel(0, phba->HCregaddr);
  9482. readl(phba->HCregaddr);
  9483. }
  9484. }
  9485. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9486. pmb = phba->sli.mbox_active;
  9487. pmbox = &pmb->u.mb;
  9488. mbox = phba->mbox;
  9489. vport = pmb->vport;
  9490. /* First check out the status word */
  9491. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9492. if (pmbox->mbxOwner != OWN_HOST) {
  9493. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9494. /*
  9495. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9496. * mbxStatus <status>
  9497. */
  9498. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9499. LOG_SLI,
  9500. "(%d):0304 Stray Mailbox "
  9501. "Interrupt mbxCommand x%x "
  9502. "mbxStatus x%x\n",
  9503. (vport ? vport->vpi : 0),
  9504. pmbox->mbxCommand,
  9505. pmbox->mbxStatus);
  9506. /* clear mailbox attention bit */
  9507. work_ha_copy &= ~HA_MBATT;
  9508. } else {
  9509. phba->sli.mbox_active = NULL;
  9510. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9511. phba->last_completion_time = jiffies;
  9512. del_timer(&phba->sli.mbox_tmo);
  9513. if (pmb->mbox_cmpl) {
  9514. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9515. MAILBOX_CMD_SIZE);
  9516. if (pmb->out_ext_byte_len &&
  9517. pmb->context2)
  9518. lpfc_sli_pcimem_bcopy(
  9519. phba->mbox_ext,
  9520. pmb->context2,
  9521. pmb->out_ext_byte_len);
  9522. }
  9523. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9524. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9525. lpfc_debugfs_disc_trc(vport,
  9526. LPFC_DISC_TRC_MBOX_VPORT,
  9527. "MBOX dflt rpi: : "
  9528. "status:x%x rpi:x%x",
  9529. (uint32_t)pmbox->mbxStatus,
  9530. pmbox->un.varWords[0], 0);
  9531. if (!pmbox->mbxStatus) {
  9532. mp = (struct lpfc_dmabuf *)
  9533. (pmb->context1);
  9534. ndlp = (struct lpfc_nodelist *)
  9535. pmb->context2;
  9536. /* Reg_LOGIN of dflt RPI was
  9537. * successful. new lets get
  9538. * rid of the RPI using the
  9539. * same mbox buffer.
  9540. */
  9541. lpfc_unreg_login(phba,
  9542. vport->vpi,
  9543. pmbox->un.varWords[0],
  9544. pmb);
  9545. pmb->mbox_cmpl =
  9546. lpfc_mbx_cmpl_dflt_rpi;
  9547. pmb->context1 = mp;
  9548. pmb->context2 = ndlp;
  9549. pmb->vport = vport;
  9550. rc = lpfc_sli_issue_mbox(phba,
  9551. pmb,
  9552. MBX_NOWAIT);
  9553. if (rc != MBX_BUSY)
  9554. lpfc_printf_log(phba,
  9555. KERN_ERR,
  9556. LOG_MBOX | LOG_SLI,
  9557. "0350 rc should have"
  9558. "been MBX_BUSY\n");
  9559. if (rc != MBX_NOT_FINISHED)
  9560. goto send_current_mbox;
  9561. }
  9562. }
  9563. spin_lock_irqsave(
  9564. &phba->pport->work_port_lock,
  9565. iflag);
  9566. phba->pport->work_port_events &=
  9567. ~WORKER_MBOX_TMO;
  9568. spin_unlock_irqrestore(
  9569. &phba->pport->work_port_lock,
  9570. iflag);
  9571. lpfc_mbox_cmpl_put(phba, pmb);
  9572. }
  9573. } else
  9574. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9575. if ((work_ha_copy & HA_MBATT) &&
  9576. (phba->sli.mbox_active == NULL)) {
  9577. send_current_mbox:
  9578. /* Process next mailbox command if there is one */
  9579. do {
  9580. rc = lpfc_sli_issue_mbox(phba, NULL,
  9581. MBX_NOWAIT);
  9582. } while (rc == MBX_NOT_FINISHED);
  9583. if (rc != MBX_SUCCESS)
  9584. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9585. LOG_SLI, "0349 rc should be "
  9586. "MBX_SUCCESS\n");
  9587. }
  9588. spin_lock_irqsave(&phba->hbalock, iflag);
  9589. phba->work_ha |= work_ha_copy;
  9590. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9591. lpfc_worker_wake_up(phba);
  9592. }
  9593. return IRQ_HANDLED;
  9594. unplug_error:
  9595. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9596. return IRQ_HANDLED;
  9597. } /* lpfc_sli_sp_intr_handler */
  9598. /**
  9599. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9600. * @irq: Interrupt number.
  9601. * @dev_id: The device context pointer.
  9602. *
  9603. * This function is directly called from the PCI layer as an interrupt
  9604. * service routine when device with SLI-3 interface spec is enabled with
  9605. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9606. * ring event in the HBA. However, when the device is enabled with either
  9607. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9608. * device-level interrupt handler. When the PCI slot is in error recovery
  9609. * or the HBA is undergoing initialization, the interrupt handler will not
  9610. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9611. * the intrrupt context. This function is called without any lock held.
  9612. * It gets the hbalock to access and update SLI data structures.
  9613. *
  9614. * This function returns IRQ_HANDLED when interrupt is handled else it
  9615. * returns IRQ_NONE.
  9616. **/
  9617. irqreturn_t
  9618. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9619. {
  9620. struct lpfc_hba *phba;
  9621. uint32_t ha_copy;
  9622. unsigned long status;
  9623. unsigned long iflag;
  9624. /* Get the driver's phba structure from the dev_id and
  9625. * assume the HBA is not interrupting.
  9626. */
  9627. phba = (struct lpfc_hba *) dev_id;
  9628. if (unlikely(!phba))
  9629. return IRQ_NONE;
  9630. /*
  9631. * Stuff needs to be attented to when this function is invoked as an
  9632. * individual interrupt handler in MSI-X multi-message interrupt mode
  9633. */
  9634. if (phba->intr_type == MSIX) {
  9635. /* Check device state for handling interrupt */
  9636. if (lpfc_intr_state_check(phba))
  9637. return IRQ_NONE;
  9638. /* Need to read HA REG for FCP ring and other ring events */
  9639. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9640. return IRQ_HANDLED;
  9641. /* Clear up only attention source related to fast-path */
  9642. spin_lock_irqsave(&phba->hbalock, iflag);
  9643. /*
  9644. * If there is deferred error attention, do not check for
  9645. * any interrupt.
  9646. */
  9647. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9648. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9649. return IRQ_NONE;
  9650. }
  9651. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9652. phba->HAregaddr);
  9653. readl(phba->HAregaddr); /* flush */
  9654. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9655. } else
  9656. ha_copy = phba->ha_copy;
  9657. /*
  9658. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9659. */
  9660. ha_copy &= ~(phba->work_ha_mask);
  9661. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9662. status >>= (4*LPFC_FCP_RING);
  9663. if (status & HA_RXMASK)
  9664. lpfc_sli_handle_fast_ring_event(phba,
  9665. &phba->sli.ring[LPFC_FCP_RING],
  9666. status);
  9667. if (phba->cfg_multi_ring_support == 2) {
  9668. /*
  9669. * Process all events on extra ring. Take the optimized path
  9670. * for extra ring IO.
  9671. */
  9672. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9673. status >>= (4*LPFC_EXTRA_RING);
  9674. if (status & HA_RXMASK) {
  9675. lpfc_sli_handle_fast_ring_event(phba,
  9676. &phba->sli.ring[LPFC_EXTRA_RING],
  9677. status);
  9678. }
  9679. }
  9680. return IRQ_HANDLED;
  9681. } /* lpfc_sli_fp_intr_handler */
  9682. /**
  9683. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9684. * @irq: Interrupt number.
  9685. * @dev_id: The device context pointer.
  9686. *
  9687. * This function is the HBA device-level interrupt handler to device with
  9688. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9689. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9690. * requires driver attention. This function invokes the slow-path interrupt
  9691. * attention handling function and fast-path interrupt attention handling
  9692. * function in turn to process the relevant HBA attention events. This
  9693. * function is called without any lock held. It gets the hbalock to access
  9694. * and update SLI data structures.
  9695. *
  9696. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9697. * returns IRQ_NONE.
  9698. **/
  9699. irqreturn_t
  9700. lpfc_sli_intr_handler(int irq, void *dev_id)
  9701. {
  9702. struct lpfc_hba *phba;
  9703. irqreturn_t sp_irq_rc, fp_irq_rc;
  9704. unsigned long status1, status2;
  9705. uint32_t hc_copy;
  9706. /*
  9707. * Get the driver's phba structure from the dev_id and
  9708. * assume the HBA is not interrupting.
  9709. */
  9710. phba = (struct lpfc_hba *) dev_id;
  9711. if (unlikely(!phba))
  9712. return IRQ_NONE;
  9713. /* Check device state for handling interrupt */
  9714. if (lpfc_intr_state_check(phba))
  9715. return IRQ_NONE;
  9716. spin_lock(&phba->hbalock);
  9717. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9718. spin_unlock(&phba->hbalock);
  9719. return IRQ_HANDLED;
  9720. }
  9721. if (unlikely(!phba->ha_copy)) {
  9722. spin_unlock(&phba->hbalock);
  9723. return IRQ_NONE;
  9724. } else if (phba->ha_copy & HA_ERATT) {
  9725. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9726. /* ERATT polling has handled ERATT */
  9727. phba->ha_copy &= ~HA_ERATT;
  9728. else
  9729. /* Indicate interrupt handler handles ERATT */
  9730. phba->hba_flag |= HBA_ERATT_HANDLED;
  9731. }
  9732. /*
  9733. * If there is deferred error attention, do not check for any interrupt.
  9734. */
  9735. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9736. spin_unlock(&phba->hbalock);
  9737. return IRQ_NONE;
  9738. }
  9739. /* Clear attention sources except link and error attentions */
  9740. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9741. spin_unlock(&phba->hbalock);
  9742. return IRQ_HANDLED;
  9743. }
  9744. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9745. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9746. phba->HCregaddr);
  9747. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9748. writel(hc_copy, phba->HCregaddr);
  9749. readl(phba->HAregaddr); /* flush */
  9750. spin_unlock(&phba->hbalock);
  9751. /*
  9752. * Invokes slow-path host attention interrupt handling as appropriate.
  9753. */
  9754. /* status of events with mailbox and link attention */
  9755. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9756. /* status of events with ELS ring */
  9757. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9758. status2 >>= (4*LPFC_ELS_RING);
  9759. if (status1 || (status2 & HA_RXMASK))
  9760. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9761. else
  9762. sp_irq_rc = IRQ_NONE;
  9763. /*
  9764. * Invoke fast-path host attention interrupt handling as appropriate.
  9765. */
  9766. /* status of events with FCP ring */
  9767. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9768. status1 >>= (4*LPFC_FCP_RING);
  9769. /* status of events with extra ring */
  9770. if (phba->cfg_multi_ring_support == 2) {
  9771. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9772. status2 >>= (4*LPFC_EXTRA_RING);
  9773. } else
  9774. status2 = 0;
  9775. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9776. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9777. else
  9778. fp_irq_rc = IRQ_NONE;
  9779. /* Return device-level interrupt handling status */
  9780. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9781. } /* lpfc_sli_intr_handler */
  9782. /**
  9783. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9784. * @phba: pointer to lpfc hba data structure.
  9785. *
  9786. * This routine is invoked by the worker thread to process all the pending
  9787. * SLI4 FCP abort XRI events.
  9788. **/
  9789. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9790. {
  9791. struct lpfc_cq_event *cq_event;
  9792. /* First, declare the fcp xri abort event has been handled */
  9793. spin_lock_irq(&phba->hbalock);
  9794. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9795. spin_unlock_irq(&phba->hbalock);
  9796. /* Now, handle all the fcp xri abort events */
  9797. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9798. /* Get the first event from the head of the event queue */
  9799. spin_lock_irq(&phba->hbalock);
  9800. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9801. cq_event, struct lpfc_cq_event, list);
  9802. spin_unlock_irq(&phba->hbalock);
  9803. /* Notify aborted XRI for FCP work queue */
  9804. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9805. /* Free the event processed back to the free pool */
  9806. lpfc_sli4_cq_event_release(phba, cq_event);
  9807. }
  9808. }
  9809. /**
  9810. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9811. * @phba: pointer to lpfc hba data structure.
  9812. *
  9813. * This routine is invoked by the worker thread to process all the pending
  9814. * SLI4 els abort xri events.
  9815. **/
  9816. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9817. {
  9818. struct lpfc_cq_event *cq_event;
  9819. /* First, declare the els xri abort event has been handled */
  9820. spin_lock_irq(&phba->hbalock);
  9821. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9822. spin_unlock_irq(&phba->hbalock);
  9823. /* Now, handle all the els xri abort events */
  9824. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9825. /* Get the first event from the head of the event queue */
  9826. spin_lock_irq(&phba->hbalock);
  9827. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9828. cq_event, struct lpfc_cq_event, list);
  9829. spin_unlock_irq(&phba->hbalock);
  9830. /* Notify aborted XRI for ELS work queue */
  9831. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9832. /* Free the event processed back to the free pool */
  9833. lpfc_sli4_cq_event_release(phba, cq_event);
  9834. }
  9835. }
  9836. /**
  9837. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9838. * @phba: pointer to lpfc hba data structure
  9839. * @pIocbIn: pointer to the rspiocbq
  9840. * @pIocbOut: pointer to the cmdiocbq
  9841. * @wcqe: pointer to the complete wcqe
  9842. *
  9843. * This routine transfers the fields of a command iocbq to a response iocbq
  9844. * by copying all the IOCB fields from command iocbq and transferring the
  9845. * completion status information from the complete wcqe.
  9846. **/
  9847. static void
  9848. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9849. struct lpfc_iocbq *pIocbIn,
  9850. struct lpfc_iocbq *pIocbOut,
  9851. struct lpfc_wcqe_complete *wcqe)
  9852. {
  9853. unsigned long iflags;
  9854. uint32_t status;
  9855. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9856. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9857. sizeof(struct lpfc_iocbq) - offset);
  9858. /* Map WCQE parameters into irspiocb parameters */
  9859. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9860. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9861. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9862. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9863. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9864. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9865. wcqe->total_data_placed;
  9866. else
  9867. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9868. else {
  9869. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9870. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9871. }
  9872. /* Convert BG errors for completion status */
  9873. if (status == CQE_STATUS_DI_ERROR) {
  9874. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9875. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9876. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9877. else
  9878. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9879. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9880. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9881. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9882. BGS_GUARD_ERR_MASK;
  9883. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9884. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9885. BGS_APPTAG_ERR_MASK;
  9886. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9887. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9888. BGS_REFTAG_ERR_MASK;
  9889. /* Check to see if there was any good data before the error */
  9890. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9891. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9892. BGS_HI_WATER_MARK_PRESENT_MASK;
  9893. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9894. wcqe->total_data_placed;
  9895. }
  9896. /*
  9897. * Set ALL the error bits to indicate we don't know what
  9898. * type of error it is.
  9899. */
  9900. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9901. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9902. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9903. BGS_GUARD_ERR_MASK);
  9904. }
  9905. /* Pick up HBA exchange busy condition */
  9906. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9907. spin_lock_irqsave(&phba->hbalock, iflags);
  9908. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9909. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9910. }
  9911. }
  9912. /**
  9913. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9914. * @phba: Pointer to HBA context object.
  9915. * @wcqe: Pointer to work-queue completion queue entry.
  9916. *
  9917. * This routine handles an ELS work-queue completion event and construct
  9918. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9919. * discovery engine to handle.
  9920. *
  9921. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9922. **/
  9923. static struct lpfc_iocbq *
  9924. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9925. struct lpfc_iocbq *irspiocbq)
  9926. {
  9927. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9928. struct lpfc_iocbq *cmdiocbq;
  9929. struct lpfc_wcqe_complete *wcqe;
  9930. unsigned long iflags;
  9931. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9932. spin_lock_irqsave(&phba->hbalock, iflags);
  9933. pring->stats.iocb_event++;
  9934. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9935. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9936. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9937. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9938. if (unlikely(!cmdiocbq)) {
  9939. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9940. "0386 ELS complete with no corresponding "
  9941. "cmdiocb: iotag (%d)\n",
  9942. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9943. lpfc_sli_release_iocbq(phba, irspiocbq);
  9944. return NULL;
  9945. }
  9946. /* Fake the irspiocbq and copy necessary response information */
  9947. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9948. return irspiocbq;
  9949. }
  9950. /**
  9951. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9952. * @phba: Pointer to HBA context object.
  9953. * @cqe: Pointer to mailbox completion queue entry.
  9954. *
  9955. * This routine process a mailbox completion queue entry with asynchrous
  9956. * event.
  9957. *
  9958. * Return: true if work posted to worker thread, otherwise false.
  9959. **/
  9960. static bool
  9961. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9962. {
  9963. struct lpfc_cq_event *cq_event;
  9964. unsigned long iflags;
  9965. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9966. "0392 Async Event: word0:x%x, word1:x%x, "
  9967. "word2:x%x, word3:x%x\n", mcqe->word0,
  9968. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9969. /* Allocate a new internal CQ_EVENT entry */
  9970. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9971. if (!cq_event) {
  9972. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9973. "0394 Failed to allocate CQ_EVENT entry\n");
  9974. return false;
  9975. }
  9976. /* Move the CQE into an asynchronous event entry */
  9977. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9978. spin_lock_irqsave(&phba->hbalock, iflags);
  9979. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9980. /* Set the async event flag */
  9981. phba->hba_flag |= ASYNC_EVENT;
  9982. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9983. return true;
  9984. }
  9985. /**
  9986. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9987. * @phba: Pointer to HBA context object.
  9988. * @cqe: Pointer to mailbox completion queue entry.
  9989. *
  9990. * This routine process a mailbox completion queue entry with mailbox
  9991. * completion event.
  9992. *
  9993. * Return: true if work posted to worker thread, otherwise false.
  9994. **/
  9995. static bool
  9996. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9997. {
  9998. uint32_t mcqe_status;
  9999. MAILBOX_t *mbox, *pmbox;
  10000. struct lpfc_mqe *mqe;
  10001. struct lpfc_vport *vport;
  10002. struct lpfc_nodelist *ndlp;
  10003. struct lpfc_dmabuf *mp;
  10004. unsigned long iflags;
  10005. LPFC_MBOXQ_t *pmb;
  10006. bool workposted = false;
  10007. int rc;
  10008. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10009. if (!bf_get(lpfc_trailer_completed, mcqe))
  10010. goto out_no_mqe_complete;
  10011. /* Get the reference to the active mbox command */
  10012. spin_lock_irqsave(&phba->hbalock, iflags);
  10013. pmb = phba->sli.mbox_active;
  10014. if (unlikely(!pmb)) {
  10015. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10016. "1832 No pending MBOX command to handle\n");
  10017. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10018. goto out_no_mqe_complete;
  10019. }
  10020. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10021. mqe = &pmb->u.mqe;
  10022. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10023. mbox = phba->mbox;
  10024. vport = pmb->vport;
  10025. /* Reset heartbeat timer */
  10026. phba->last_completion_time = jiffies;
  10027. del_timer(&phba->sli.mbox_tmo);
  10028. /* Move mbox data to caller's mailbox region, do endian swapping */
  10029. if (pmb->mbox_cmpl && mbox)
  10030. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10031. /*
  10032. * For mcqe errors, conditionally move a modified error code to
  10033. * the mbox so that the error will not be missed.
  10034. */
  10035. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10036. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10037. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10038. bf_set(lpfc_mqe_status, mqe,
  10039. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10040. }
  10041. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10042. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10043. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10044. "MBOX dflt rpi: status:x%x rpi:x%x",
  10045. mcqe_status,
  10046. pmbox->un.varWords[0], 0);
  10047. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10048. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10049. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10050. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10051. * RID of the PPI using the same mbox buffer.
  10052. */
  10053. lpfc_unreg_login(phba, vport->vpi,
  10054. pmbox->un.varWords[0], pmb);
  10055. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10056. pmb->context1 = mp;
  10057. pmb->context2 = ndlp;
  10058. pmb->vport = vport;
  10059. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10060. if (rc != MBX_BUSY)
  10061. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10062. LOG_SLI, "0385 rc should "
  10063. "have been MBX_BUSY\n");
  10064. if (rc != MBX_NOT_FINISHED)
  10065. goto send_current_mbox;
  10066. }
  10067. }
  10068. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10069. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10070. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10071. /* There is mailbox completion work to do */
  10072. spin_lock_irqsave(&phba->hbalock, iflags);
  10073. __lpfc_mbox_cmpl_put(phba, pmb);
  10074. phba->work_ha |= HA_MBATT;
  10075. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10076. workposted = true;
  10077. send_current_mbox:
  10078. spin_lock_irqsave(&phba->hbalock, iflags);
  10079. /* Release the mailbox command posting token */
  10080. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10081. /* Setting active mailbox pointer need to be in sync to flag clear */
  10082. phba->sli.mbox_active = NULL;
  10083. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10084. /* Wake up worker thread to post the next pending mailbox command */
  10085. lpfc_worker_wake_up(phba);
  10086. out_no_mqe_complete:
  10087. if (bf_get(lpfc_trailer_consumed, mcqe))
  10088. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10089. return workposted;
  10090. }
  10091. /**
  10092. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10093. * @phba: Pointer to HBA context object.
  10094. * @cqe: Pointer to mailbox completion queue entry.
  10095. *
  10096. * This routine process a mailbox completion queue entry, it invokes the
  10097. * proper mailbox complete handling or asynchrous event handling routine
  10098. * according to the MCQE's async bit.
  10099. *
  10100. * Return: true if work posted to worker thread, otherwise false.
  10101. **/
  10102. static bool
  10103. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10104. {
  10105. struct lpfc_mcqe mcqe;
  10106. bool workposted;
  10107. /* Copy the mailbox MCQE and convert endian order as needed */
  10108. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10109. /* Invoke the proper event handling routine */
  10110. if (!bf_get(lpfc_trailer_async, &mcqe))
  10111. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10112. else
  10113. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10114. return workposted;
  10115. }
  10116. /**
  10117. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10118. * @phba: Pointer to HBA context object.
  10119. * @wcqe: Pointer to work-queue completion queue entry.
  10120. *
  10121. * This routine handles an ELS work-queue completion event.
  10122. *
  10123. * Return: true if work posted to worker thread, otherwise false.
  10124. **/
  10125. static bool
  10126. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10127. struct lpfc_wcqe_complete *wcqe)
  10128. {
  10129. struct lpfc_iocbq *irspiocbq;
  10130. unsigned long iflags;
  10131. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10132. /* Get an irspiocbq for later ELS response processing use */
  10133. irspiocbq = lpfc_sli_get_iocbq(phba);
  10134. if (!irspiocbq) {
  10135. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10136. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10137. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10138. pring->txq_cnt, phba->iocb_cnt,
  10139. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10140. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10141. return false;
  10142. }
  10143. /* Save off the slow-path queue event for work thread to process */
  10144. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10145. spin_lock_irqsave(&phba->hbalock, iflags);
  10146. list_add_tail(&irspiocbq->cq_event.list,
  10147. &phba->sli4_hba.sp_queue_event);
  10148. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10149. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10150. return true;
  10151. }
  10152. /**
  10153. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10154. * @phba: Pointer to HBA context object.
  10155. * @wcqe: Pointer to work-queue completion queue entry.
  10156. *
  10157. * This routine handles slow-path WQ entry comsumed event by invoking the
  10158. * proper WQ release routine to the slow-path WQ.
  10159. **/
  10160. static void
  10161. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10162. struct lpfc_wcqe_release *wcqe)
  10163. {
  10164. /* sanity check on queue memory */
  10165. if (unlikely(!phba->sli4_hba.els_wq))
  10166. return;
  10167. /* Check for the slow-path ELS work queue */
  10168. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10169. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10170. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10171. else
  10172. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10173. "2579 Slow-path wqe consume event carries "
  10174. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10175. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10176. phba->sli4_hba.els_wq->queue_id);
  10177. }
  10178. /**
  10179. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10180. * @phba: Pointer to HBA context object.
  10181. * @cq: Pointer to a WQ completion queue.
  10182. * @wcqe: Pointer to work-queue completion queue entry.
  10183. *
  10184. * This routine handles an XRI abort event.
  10185. *
  10186. * Return: true if work posted to worker thread, otherwise false.
  10187. **/
  10188. static bool
  10189. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10190. struct lpfc_queue *cq,
  10191. struct sli4_wcqe_xri_aborted *wcqe)
  10192. {
  10193. bool workposted = false;
  10194. struct lpfc_cq_event *cq_event;
  10195. unsigned long iflags;
  10196. /* Allocate a new internal CQ_EVENT entry */
  10197. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10198. if (!cq_event) {
  10199. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10200. "0602 Failed to allocate CQ_EVENT entry\n");
  10201. return false;
  10202. }
  10203. /* Move the CQE into the proper xri abort event list */
  10204. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10205. switch (cq->subtype) {
  10206. case LPFC_FCP:
  10207. spin_lock_irqsave(&phba->hbalock, iflags);
  10208. list_add_tail(&cq_event->list,
  10209. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10210. /* Set the fcp xri abort event flag */
  10211. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10212. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10213. workposted = true;
  10214. break;
  10215. case LPFC_ELS:
  10216. spin_lock_irqsave(&phba->hbalock, iflags);
  10217. list_add_tail(&cq_event->list,
  10218. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10219. /* Set the els xri abort event flag */
  10220. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10221. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10222. workposted = true;
  10223. break;
  10224. default:
  10225. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10226. "0603 Invalid work queue CQE subtype (x%x)\n",
  10227. cq->subtype);
  10228. workposted = false;
  10229. break;
  10230. }
  10231. return workposted;
  10232. }
  10233. /**
  10234. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10235. * @phba: Pointer to HBA context object.
  10236. * @rcqe: Pointer to receive-queue completion queue entry.
  10237. *
  10238. * This routine process a receive-queue completion queue entry.
  10239. *
  10240. * Return: true if work posted to worker thread, otherwise false.
  10241. **/
  10242. static bool
  10243. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10244. {
  10245. bool workposted = false;
  10246. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10247. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10248. struct hbq_dmabuf *dma_buf;
  10249. uint32_t status, rq_id;
  10250. unsigned long iflags;
  10251. /* sanity check on queue memory */
  10252. if (unlikely(!hrq) || unlikely(!drq))
  10253. return workposted;
  10254. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10255. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10256. else
  10257. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10258. if (rq_id != hrq->queue_id)
  10259. goto out;
  10260. status = bf_get(lpfc_rcqe_status, rcqe);
  10261. switch (status) {
  10262. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10263. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10264. "2537 Receive Frame Truncated!!\n");
  10265. case FC_STATUS_RQ_SUCCESS:
  10266. lpfc_sli4_rq_release(hrq, drq);
  10267. spin_lock_irqsave(&phba->hbalock, iflags);
  10268. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10269. if (!dma_buf) {
  10270. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10271. goto out;
  10272. }
  10273. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10274. /* save off the frame for the word thread to process */
  10275. list_add_tail(&dma_buf->cq_event.list,
  10276. &phba->sli4_hba.sp_queue_event);
  10277. /* Frame received */
  10278. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10279. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10280. workposted = true;
  10281. break;
  10282. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10283. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10284. /* Post more buffers if possible */
  10285. spin_lock_irqsave(&phba->hbalock, iflags);
  10286. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10287. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10288. workposted = true;
  10289. break;
  10290. }
  10291. out:
  10292. return workposted;
  10293. }
  10294. /**
  10295. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10296. * @phba: Pointer to HBA context object.
  10297. * @cq: Pointer to the completion queue.
  10298. * @wcqe: Pointer to a completion queue entry.
  10299. *
  10300. * This routine process a slow-path work-queue or receive queue completion queue
  10301. * entry.
  10302. *
  10303. * Return: true if work posted to worker thread, otherwise false.
  10304. **/
  10305. static bool
  10306. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10307. struct lpfc_cqe *cqe)
  10308. {
  10309. struct lpfc_cqe cqevt;
  10310. bool workposted = false;
  10311. /* Copy the work queue CQE and convert endian order if needed */
  10312. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10313. /* Check and process for different type of WCQE and dispatch */
  10314. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10315. case CQE_CODE_COMPL_WQE:
  10316. /* Process the WQ/RQ complete event */
  10317. phba->last_completion_time = jiffies;
  10318. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10319. (struct lpfc_wcqe_complete *)&cqevt);
  10320. break;
  10321. case CQE_CODE_RELEASE_WQE:
  10322. /* Process the WQ release event */
  10323. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10324. (struct lpfc_wcqe_release *)&cqevt);
  10325. break;
  10326. case CQE_CODE_XRI_ABORTED:
  10327. /* Process the WQ XRI abort event */
  10328. phba->last_completion_time = jiffies;
  10329. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10330. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10331. break;
  10332. case CQE_CODE_RECEIVE:
  10333. case CQE_CODE_RECEIVE_V1:
  10334. /* Process the RQ event */
  10335. phba->last_completion_time = jiffies;
  10336. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10337. (struct lpfc_rcqe *)&cqevt);
  10338. break;
  10339. default:
  10340. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10341. "0388 Not a valid WCQE code: x%x\n",
  10342. bf_get(lpfc_cqe_code, &cqevt));
  10343. break;
  10344. }
  10345. return workposted;
  10346. }
  10347. /**
  10348. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10349. * @phba: Pointer to HBA context object.
  10350. * @eqe: Pointer to fast-path event queue entry.
  10351. *
  10352. * This routine process a event queue entry from the slow-path event queue.
  10353. * It will check the MajorCode and MinorCode to determine this is for a
  10354. * completion event on a completion queue, if not, an error shall be logged
  10355. * and just return. Otherwise, it will get to the corresponding completion
  10356. * queue and process all the entries on that completion queue, rearm the
  10357. * completion queue, and then return.
  10358. *
  10359. **/
  10360. static void
  10361. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10362. {
  10363. struct lpfc_queue *cq = NULL, *childq, *speq;
  10364. struct lpfc_cqe *cqe;
  10365. bool workposted = false;
  10366. int ecount = 0;
  10367. uint16_t cqid;
  10368. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10369. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10370. "0359 Not a valid slow-path completion "
  10371. "event: majorcode=x%x, minorcode=x%x\n",
  10372. bf_get_le32(lpfc_eqe_major_code, eqe),
  10373. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10374. return;
  10375. }
  10376. /* Get the reference to the corresponding CQ */
  10377. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10378. /* Search for completion queue pointer matching this cqid */
  10379. speq = phba->sli4_hba.sp_eq;
  10380. /* sanity check on queue memory */
  10381. if (unlikely(!speq))
  10382. return;
  10383. list_for_each_entry(childq, &speq->child_list, list) {
  10384. if (childq->queue_id == cqid) {
  10385. cq = childq;
  10386. break;
  10387. }
  10388. }
  10389. if (unlikely(!cq)) {
  10390. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10391. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10392. "0365 Slow-path CQ identifier "
  10393. "(%d) does not exist\n", cqid);
  10394. return;
  10395. }
  10396. /* Process all the entries to the CQ */
  10397. switch (cq->type) {
  10398. case LPFC_MCQ:
  10399. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10400. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10401. if (!(++ecount % cq->entry_repost))
  10402. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10403. }
  10404. break;
  10405. case LPFC_WCQ:
  10406. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10407. if (cq->subtype == LPFC_FCP)
  10408. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10409. cqe);
  10410. else
  10411. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10412. cqe);
  10413. if (!(++ecount % cq->entry_repost))
  10414. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10415. }
  10416. break;
  10417. default:
  10418. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10419. "0370 Invalid completion queue type (%d)\n",
  10420. cq->type);
  10421. return;
  10422. }
  10423. /* Catch the no cq entry condition, log an error */
  10424. if (unlikely(ecount == 0))
  10425. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10426. "0371 No entry from the CQ: identifier "
  10427. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10428. /* In any case, flash and re-arm the RCQ */
  10429. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10430. /* wake up worker thread if there are works to be done */
  10431. if (workposted)
  10432. lpfc_worker_wake_up(phba);
  10433. }
  10434. /**
  10435. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10436. * @eqe: Pointer to fast-path completion queue entry.
  10437. *
  10438. * This routine process a fast-path work queue completion entry from fast-path
  10439. * event queue for FCP command response completion.
  10440. **/
  10441. static void
  10442. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10443. struct lpfc_wcqe_complete *wcqe)
  10444. {
  10445. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10446. struct lpfc_iocbq *cmdiocbq;
  10447. struct lpfc_iocbq irspiocbq;
  10448. unsigned long iflags;
  10449. spin_lock_irqsave(&phba->hbalock, iflags);
  10450. pring->stats.iocb_event++;
  10451. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10452. /* Check for response status */
  10453. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10454. /* If resource errors reported from HBA, reduce queue
  10455. * depth of the SCSI device.
  10456. */
  10457. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10458. IOSTAT_LOCAL_REJECT) &&
  10459. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10460. phba->lpfc_rampdown_queue_depth(phba);
  10461. }
  10462. /* Log the error status */
  10463. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10464. "0373 FCP complete error: status=x%x, "
  10465. "hw_status=x%x, total_data_specified=%d, "
  10466. "parameter=x%x, word3=x%x\n",
  10467. bf_get(lpfc_wcqe_c_status, wcqe),
  10468. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10469. wcqe->total_data_placed, wcqe->parameter,
  10470. wcqe->word3);
  10471. }
  10472. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10473. spin_lock_irqsave(&phba->hbalock, iflags);
  10474. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10475. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10476. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10477. if (unlikely(!cmdiocbq)) {
  10478. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10479. "0374 FCP complete with no corresponding "
  10480. "cmdiocb: iotag (%d)\n",
  10481. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10482. return;
  10483. }
  10484. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10485. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10486. "0375 FCP cmdiocb not callback function "
  10487. "iotag: (%d)\n",
  10488. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10489. return;
  10490. }
  10491. /* Fake the irspiocb and copy necessary response information */
  10492. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10493. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10494. spin_lock_irqsave(&phba->hbalock, iflags);
  10495. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10496. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10497. }
  10498. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10499. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10500. }
  10501. /**
  10502. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10503. * @phba: Pointer to HBA context object.
  10504. * @cq: Pointer to completion queue.
  10505. * @wcqe: Pointer to work-queue completion queue entry.
  10506. *
  10507. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10508. * proper WQ release routine to the slow-path WQ.
  10509. **/
  10510. static void
  10511. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10512. struct lpfc_wcqe_release *wcqe)
  10513. {
  10514. struct lpfc_queue *childwq;
  10515. bool wqid_matched = false;
  10516. uint16_t fcp_wqid;
  10517. /* Check for fast-path FCP work queue release */
  10518. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10519. list_for_each_entry(childwq, &cq->child_list, list) {
  10520. if (childwq->queue_id == fcp_wqid) {
  10521. lpfc_sli4_wq_release(childwq,
  10522. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10523. wqid_matched = true;
  10524. break;
  10525. }
  10526. }
  10527. /* Report warning log message if no match found */
  10528. if (wqid_matched != true)
  10529. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10530. "2580 Fast-path wqe consume event carries "
  10531. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10532. }
  10533. /**
  10534. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10535. * @cq: Pointer to the completion queue.
  10536. * @eqe: Pointer to fast-path completion queue entry.
  10537. *
  10538. * This routine process a fast-path work queue completion entry from fast-path
  10539. * event queue for FCP command response completion.
  10540. **/
  10541. static int
  10542. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10543. struct lpfc_cqe *cqe)
  10544. {
  10545. struct lpfc_wcqe_release wcqe;
  10546. bool workposted = false;
  10547. /* Copy the work queue CQE and convert endian order if needed */
  10548. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10549. /* Check and process for different type of WCQE and dispatch */
  10550. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10551. case CQE_CODE_COMPL_WQE:
  10552. /* Process the WQ complete event */
  10553. phba->last_completion_time = jiffies;
  10554. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10555. (struct lpfc_wcqe_complete *)&wcqe);
  10556. break;
  10557. case CQE_CODE_RELEASE_WQE:
  10558. /* Process the WQ release event */
  10559. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10560. (struct lpfc_wcqe_release *)&wcqe);
  10561. break;
  10562. case CQE_CODE_XRI_ABORTED:
  10563. /* Process the WQ XRI abort event */
  10564. phba->last_completion_time = jiffies;
  10565. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10566. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10567. break;
  10568. default:
  10569. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10570. "0144 Not a valid WCQE code: x%x\n",
  10571. bf_get(lpfc_wcqe_c_code, &wcqe));
  10572. break;
  10573. }
  10574. return workposted;
  10575. }
  10576. /**
  10577. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10578. * @phba: Pointer to HBA context object.
  10579. * @eqe: Pointer to fast-path event queue entry.
  10580. *
  10581. * This routine process a event queue entry from the fast-path event queue.
  10582. * It will check the MajorCode and MinorCode to determine this is for a
  10583. * completion event on a completion queue, if not, an error shall be logged
  10584. * and just return. Otherwise, it will get to the corresponding completion
  10585. * queue and process all the entries on the completion queue, rearm the
  10586. * completion queue, and then return.
  10587. **/
  10588. static void
  10589. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10590. uint32_t fcp_cqidx)
  10591. {
  10592. struct lpfc_queue *cq;
  10593. struct lpfc_cqe *cqe;
  10594. bool workposted = false;
  10595. uint16_t cqid;
  10596. int ecount = 0;
  10597. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10598. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10599. "0366 Not a valid fast-path completion "
  10600. "event: majorcode=x%x, minorcode=x%x\n",
  10601. bf_get_le32(lpfc_eqe_major_code, eqe),
  10602. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10603. return;
  10604. }
  10605. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10606. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10607. "3146 Fast-path completion queues "
  10608. "does not exist\n");
  10609. return;
  10610. }
  10611. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10612. if (unlikely(!cq)) {
  10613. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10614. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10615. "0367 Fast-path completion queue "
  10616. "(%d) does not exist\n", fcp_cqidx);
  10617. return;
  10618. }
  10619. /* Get the reference to the corresponding CQ */
  10620. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10621. if (unlikely(cqid != cq->queue_id)) {
  10622. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10623. "0368 Miss-matched fast-path completion "
  10624. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10625. cqid, cq->queue_id);
  10626. return;
  10627. }
  10628. /* Process all the entries to the CQ */
  10629. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10630. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10631. if (!(++ecount % cq->entry_repost))
  10632. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10633. }
  10634. /* Catch the no cq entry condition */
  10635. if (unlikely(ecount == 0))
  10636. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10637. "0369 No entry from fast-path completion "
  10638. "queue fcpcqid=%d\n", cq->queue_id);
  10639. /* In any case, flash and re-arm the CQ */
  10640. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10641. /* wake up worker thread if there are works to be done */
  10642. if (workposted)
  10643. lpfc_worker_wake_up(phba);
  10644. }
  10645. static void
  10646. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10647. {
  10648. struct lpfc_eqe *eqe;
  10649. /* walk all the EQ entries and drop on the floor */
  10650. while ((eqe = lpfc_sli4_eq_get(eq)))
  10651. ;
  10652. /* Clear and re-arm the EQ */
  10653. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10654. }
  10655. /**
  10656. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10657. * @irq: Interrupt number.
  10658. * @dev_id: The device context pointer.
  10659. *
  10660. * This function is directly called from the PCI layer as an interrupt
  10661. * service routine when device with SLI-4 interface spec is enabled with
  10662. * MSI-X multi-message interrupt mode and there are slow-path events in
  10663. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10664. * interrupt mode, this function is called as part of the device-level
  10665. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10666. * undergoing initialization, the interrupt handler will not process the
  10667. * interrupt. The link attention and ELS ring attention events are handled
  10668. * by the worker thread. The interrupt handler signals the worker thread
  10669. * and returns for these events. This function is called without any lock
  10670. * held. It gets the hbalock to access and update SLI data structures.
  10671. *
  10672. * This function returns IRQ_HANDLED when interrupt is handled else it
  10673. * returns IRQ_NONE.
  10674. **/
  10675. irqreturn_t
  10676. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10677. {
  10678. struct lpfc_hba *phba;
  10679. struct lpfc_queue *speq;
  10680. struct lpfc_eqe *eqe;
  10681. unsigned long iflag;
  10682. int ecount = 0;
  10683. /*
  10684. * Get the driver's phba structure from the dev_id
  10685. */
  10686. phba = (struct lpfc_hba *)dev_id;
  10687. if (unlikely(!phba))
  10688. return IRQ_NONE;
  10689. /* Get to the EQ struct associated with this vector */
  10690. speq = phba->sli4_hba.sp_eq;
  10691. if (unlikely(!speq))
  10692. return IRQ_NONE;
  10693. /* Check device state for handling interrupt */
  10694. if (unlikely(lpfc_intr_state_check(phba))) {
  10695. /* Check again for link_state with lock held */
  10696. spin_lock_irqsave(&phba->hbalock, iflag);
  10697. if (phba->link_state < LPFC_LINK_DOWN)
  10698. /* Flush, clear interrupt, and rearm the EQ */
  10699. lpfc_sli4_eq_flush(phba, speq);
  10700. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10701. return IRQ_NONE;
  10702. }
  10703. /*
  10704. * Process all the event on FCP slow-path EQ
  10705. */
  10706. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10707. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10708. if (!(++ecount % speq->entry_repost))
  10709. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10710. }
  10711. /* Always clear and re-arm the slow-path EQ */
  10712. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10713. /* Catch the no cq entry condition */
  10714. if (unlikely(ecount == 0)) {
  10715. if (phba->intr_type == MSIX)
  10716. /* MSI-X treated interrupt served as no EQ share INT */
  10717. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10718. "0357 MSI-X interrupt with no EQE\n");
  10719. else
  10720. /* Non MSI-X treated on interrupt as EQ share INT */
  10721. return IRQ_NONE;
  10722. }
  10723. return IRQ_HANDLED;
  10724. } /* lpfc_sli4_sp_intr_handler */
  10725. /**
  10726. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10727. * @irq: Interrupt number.
  10728. * @dev_id: The device context pointer.
  10729. *
  10730. * This function is directly called from the PCI layer as an interrupt
  10731. * service routine when device with SLI-4 interface spec is enabled with
  10732. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10733. * ring event in the HBA. However, when the device is enabled with either
  10734. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10735. * device-level interrupt handler. When the PCI slot is in error recovery
  10736. * or the HBA is undergoing initialization, the interrupt handler will not
  10737. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10738. * the intrrupt context. This function is called without any lock held.
  10739. * It gets the hbalock to access and update SLI data structures. Note that,
  10740. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10741. * equal to that of FCP CQ index.
  10742. *
  10743. * This function returns IRQ_HANDLED when interrupt is handled else it
  10744. * returns IRQ_NONE.
  10745. **/
  10746. irqreturn_t
  10747. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10748. {
  10749. struct lpfc_hba *phba;
  10750. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10751. struct lpfc_queue *fpeq;
  10752. struct lpfc_eqe *eqe;
  10753. unsigned long iflag;
  10754. int ecount = 0;
  10755. uint32_t fcp_eqidx;
  10756. /* Get the driver's phba structure from the dev_id */
  10757. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10758. phba = fcp_eq_hdl->phba;
  10759. fcp_eqidx = fcp_eq_hdl->idx;
  10760. if (unlikely(!phba))
  10761. return IRQ_NONE;
  10762. if (unlikely(!phba->sli4_hba.fp_eq))
  10763. return IRQ_NONE;
  10764. /* Get to the EQ struct associated with this vector */
  10765. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10766. if (unlikely(!fpeq))
  10767. return IRQ_NONE;
  10768. /* Check device state for handling interrupt */
  10769. if (unlikely(lpfc_intr_state_check(phba))) {
  10770. /* Check again for link_state with lock held */
  10771. spin_lock_irqsave(&phba->hbalock, iflag);
  10772. if (phba->link_state < LPFC_LINK_DOWN)
  10773. /* Flush, clear interrupt, and rearm the EQ */
  10774. lpfc_sli4_eq_flush(phba, fpeq);
  10775. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10776. return IRQ_NONE;
  10777. }
  10778. /*
  10779. * Process all the event on FCP fast-path EQ
  10780. */
  10781. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10782. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10783. if (!(++ecount % fpeq->entry_repost))
  10784. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10785. }
  10786. /* Always clear and re-arm the fast-path EQ */
  10787. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10788. if (unlikely(ecount == 0)) {
  10789. if (phba->intr_type == MSIX)
  10790. /* MSI-X treated interrupt served as no EQ share INT */
  10791. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10792. "0358 MSI-X interrupt with no EQE\n");
  10793. else
  10794. /* Non MSI-X treated on interrupt as EQ share INT */
  10795. return IRQ_NONE;
  10796. }
  10797. return IRQ_HANDLED;
  10798. } /* lpfc_sli4_fp_intr_handler */
  10799. /**
  10800. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10801. * @irq: Interrupt number.
  10802. * @dev_id: The device context pointer.
  10803. *
  10804. * This function is the device-level interrupt handler to device with SLI-4
  10805. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10806. * interrupt mode is enabled and there is an event in the HBA which requires
  10807. * driver attention. This function invokes the slow-path interrupt attention
  10808. * handling function and fast-path interrupt attention handling function in
  10809. * turn to process the relevant HBA attention events. This function is called
  10810. * without any lock held. It gets the hbalock to access and update SLI data
  10811. * structures.
  10812. *
  10813. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10814. * returns IRQ_NONE.
  10815. **/
  10816. irqreturn_t
  10817. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10818. {
  10819. struct lpfc_hba *phba;
  10820. irqreturn_t sp_irq_rc, fp_irq_rc;
  10821. bool fp_handled = false;
  10822. uint32_t fcp_eqidx;
  10823. /* Get the driver's phba structure from the dev_id */
  10824. phba = (struct lpfc_hba *)dev_id;
  10825. if (unlikely(!phba))
  10826. return IRQ_NONE;
  10827. /*
  10828. * Invokes slow-path host attention interrupt handling as appropriate.
  10829. */
  10830. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10831. /*
  10832. * Invoke fast-path host attention interrupt handling as appropriate.
  10833. */
  10834. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10835. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10836. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10837. if (fp_irq_rc == IRQ_HANDLED)
  10838. fp_handled |= true;
  10839. }
  10840. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10841. } /* lpfc_sli4_intr_handler */
  10842. /**
  10843. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10844. * @queue: The queue structure to free.
  10845. *
  10846. * This function frees a queue structure and the DMAable memory used for
  10847. * the host resident queue. This function must be called after destroying the
  10848. * queue on the HBA.
  10849. **/
  10850. void
  10851. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10852. {
  10853. struct lpfc_dmabuf *dmabuf;
  10854. if (!queue)
  10855. return;
  10856. while (!list_empty(&queue->page_list)) {
  10857. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10858. list);
  10859. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10860. dmabuf->virt, dmabuf->phys);
  10861. kfree(dmabuf);
  10862. }
  10863. kfree(queue);
  10864. return;
  10865. }
  10866. /**
  10867. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10868. * @phba: The HBA that this queue is being created on.
  10869. * @entry_size: The size of each queue entry for this queue.
  10870. * @entry count: The number of entries that this queue will handle.
  10871. *
  10872. * This function allocates a queue structure and the DMAable memory used for
  10873. * the host resident queue. This function must be called before creating the
  10874. * queue on the HBA.
  10875. **/
  10876. struct lpfc_queue *
  10877. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10878. uint32_t entry_count)
  10879. {
  10880. struct lpfc_queue *queue;
  10881. struct lpfc_dmabuf *dmabuf;
  10882. int x, total_qe_count;
  10883. void *dma_pointer;
  10884. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10885. if (!phba->sli4_hba.pc_sli4_params.supported)
  10886. hw_page_size = SLI4_PAGE_SIZE;
  10887. queue = kzalloc(sizeof(struct lpfc_queue) +
  10888. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10889. if (!queue)
  10890. return NULL;
  10891. queue->page_count = (ALIGN(entry_size * entry_count,
  10892. hw_page_size))/hw_page_size;
  10893. INIT_LIST_HEAD(&queue->list);
  10894. INIT_LIST_HEAD(&queue->page_list);
  10895. INIT_LIST_HEAD(&queue->child_list);
  10896. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10897. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10898. if (!dmabuf)
  10899. goto out_fail;
  10900. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10901. hw_page_size, &dmabuf->phys,
  10902. GFP_KERNEL);
  10903. if (!dmabuf->virt) {
  10904. kfree(dmabuf);
  10905. goto out_fail;
  10906. }
  10907. memset(dmabuf->virt, 0, hw_page_size);
  10908. dmabuf->buffer_tag = x;
  10909. list_add_tail(&dmabuf->list, &queue->page_list);
  10910. /* initialize queue's entry array */
  10911. dma_pointer = dmabuf->virt;
  10912. for (; total_qe_count < entry_count &&
  10913. dma_pointer < (hw_page_size + dmabuf->virt);
  10914. total_qe_count++, dma_pointer += entry_size) {
  10915. queue->qe[total_qe_count].address = dma_pointer;
  10916. }
  10917. }
  10918. queue->entry_size = entry_size;
  10919. queue->entry_count = entry_count;
  10920. /*
  10921. * entry_repost is calculated based on the number of entries in the
  10922. * queue. This works out except for RQs. If buffers are NOT initially
  10923. * posted for every RQE, entry_repost should be adjusted accordingly.
  10924. */
  10925. queue->entry_repost = (entry_count >> 3);
  10926. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10927. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10928. queue->phba = phba;
  10929. return queue;
  10930. out_fail:
  10931. lpfc_sli4_queue_free(queue);
  10932. return NULL;
  10933. }
  10934. /**
  10935. * lpfc_eq_create - Create an Event Queue on the HBA
  10936. * @phba: HBA structure that indicates port to create a queue on.
  10937. * @eq: The queue structure to use to create the event queue.
  10938. * @imax: The maximum interrupt per second limit.
  10939. *
  10940. * This function creates an event queue, as detailed in @eq, on a port,
  10941. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10942. *
  10943. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10944. * is used to get the entry count and entry size that are necessary to
  10945. * determine the number of pages to allocate and use for this queue. This
  10946. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10947. * event queue. This function is asynchronous and will wait for the mailbox
  10948. * command to finish before continuing.
  10949. *
  10950. * On success this function will return a zero. If unable to allocate enough
  10951. * memory this function will return -ENOMEM. If the queue create mailbox command
  10952. * fails this function will return -ENXIO.
  10953. **/
  10954. uint32_t
  10955. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10956. {
  10957. struct lpfc_mbx_eq_create *eq_create;
  10958. LPFC_MBOXQ_t *mbox;
  10959. int rc, length, status = 0;
  10960. struct lpfc_dmabuf *dmabuf;
  10961. uint32_t shdr_status, shdr_add_status;
  10962. union lpfc_sli4_cfg_shdr *shdr;
  10963. uint16_t dmult;
  10964. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10965. /* sanity check on queue memory */
  10966. if (!eq)
  10967. return -ENODEV;
  10968. if (!phba->sli4_hba.pc_sli4_params.supported)
  10969. hw_page_size = SLI4_PAGE_SIZE;
  10970. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10971. if (!mbox)
  10972. return -ENOMEM;
  10973. length = (sizeof(struct lpfc_mbx_eq_create) -
  10974. sizeof(struct lpfc_sli4_cfg_mhdr));
  10975. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10976. LPFC_MBOX_OPCODE_EQ_CREATE,
  10977. length, LPFC_SLI4_MBX_EMBED);
  10978. eq_create = &mbox->u.mqe.un.eq_create;
  10979. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10980. eq->page_count);
  10981. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10982. LPFC_EQE_SIZE);
  10983. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10984. /* Calculate delay multiper from maximum interrupt per second */
  10985. dmult = LPFC_DMULT_CONST/imax - 1;
  10986. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10987. dmult);
  10988. switch (eq->entry_count) {
  10989. default:
  10990. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10991. "0360 Unsupported EQ count. (%d)\n",
  10992. eq->entry_count);
  10993. if (eq->entry_count < 256)
  10994. return -EINVAL;
  10995. /* otherwise default to smallest count (drop through) */
  10996. case 256:
  10997. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10998. LPFC_EQ_CNT_256);
  10999. break;
  11000. case 512:
  11001. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11002. LPFC_EQ_CNT_512);
  11003. break;
  11004. case 1024:
  11005. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11006. LPFC_EQ_CNT_1024);
  11007. break;
  11008. case 2048:
  11009. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11010. LPFC_EQ_CNT_2048);
  11011. break;
  11012. case 4096:
  11013. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11014. LPFC_EQ_CNT_4096);
  11015. break;
  11016. }
  11017. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11018. memset(dmabuf->virt, 0, hw_page_size);
  11019. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11020. putPaddrLow(dmabuf->phys);
  11021. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11022. putPaddrHigh(dmabuf->phys);
  11023. }
  11024. mbox->vport = phba->pport;
  11025. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11026. mbox->context1 = NULL;
  11027. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11028. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11029. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11030. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11031. if (shdr_status || shdr_add_status || rc) {
  11032. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11033. "2500 EQ_CREATE mailbox failed with "
  11034. "status x%x add_status x%x, mbx status x%x\n",
  11035. shdr_status, shdr_add_status, rc);
  11036. status = -ENXIO;
  11037. }
  11038. eq->type = LPFC_EQ;
  11039. eq->subtype = LPFC_NONE;
  11040. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11041. if (eq->queue_id == 0xFFFF)
  11042. status = -ENXIO;
  11043. eq->host_index = 0;
  11044. eq->hba_index = 0;
  11045. mempool_free(mbox, phba->mbox_mem_pool);
  11046. return status;
  11047. }
  11048. /**
  11049. * lpfc_cq_create - Create a Completion Queue on the HBA
  11050. * @phba: HBA structure that indicates port to create a queue on.
  11051. * @cq: The queue structure to use to create the completion queue.
  11052. * @eq: The event queue to bind this completion queue to.
  11053. *
  11054. * This function creates a completion queue, as detailed in @wq, on a port,
  11055. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11056. *
  11057. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11058. * is used to get the entry count and entry size that are necessary to
  11059. * determine the number of pages to allocate and use for this queue. The @eq
  11060. * is used to indicate which event queue to bind this completion queue to. This
  11061. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11062. * completion queue. This function is asynchronous and will wait for the mailbox
  11063. * command to finish before continuing.
  11064. *
  11065. * On success this function will return a zero. If unable to allocate enough
  11066. * memory this function will return -ENOMEM. If the queue create mailbox command
  11067. * fails this function will return -ENXIO.
  11068. **/
  11069. uint32_t
  11070. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11071. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11072. {
  11073. struct lpfc_mbx_cq_create *cq_create;
  11074. struct lpfc_dmabuf *dmabuf;
  11075. LPFC_MBOXQ_t *mbox;
  11076. int rc, length, status = 0;
  11077. uint32_t shdr_status, shdr_add_status;
  11078. union lpfc_sli4_cfg_shdr *shdr;
  11079. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11080. /* sanity check on queue memory */
  11081. if (!cq || !eq)
  11082. return -ENODEV;
  11083. if (!phba->sli4_hba.pc_sli4_params.supported)
  11084. hw_page_size = SLI4_PAGE_SIZE;
  11085. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11086. if (!mbox)
  11087. return -ENOMEM;
  11088. length = (sizeof(struct lpfc_mbx_cq_create) -
  11089. sizeof(struct lpfc_sli4_cfg_mhdr));
  11090. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11091. LPFC_MBOX_OPCODE_CQ_CREATE,
  11092. length, LPFC_SLI4_MBX_EMBED);
  11093. cq_create = &mbox->u.mqe.un.cq_create;
  11094. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11095. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11096. cq->page_count);
  11097. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11098. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11099. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11100. phba->sli4_hba.pc_sli4_params.cqv);
  11101. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11102. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11103. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11104. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11105. eq->queue_id);
  11106. } else {
  11107. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11108. eq->queue_id);
  11109. }
  11110. switch (cq->entry_count) {
  11111. default:
  11112. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11113. "0361 Unsupported CQ count. (%d)\n",
  11114. cq->entry_count);
  11115. if (cq->entry_count < 256)
  11116. return -EINVAL;
  11117. /* otherwise default to smallest count (drop through) */
  11118. case 256:
  11119. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11120. LPFC_CQ_CNT_256);
  11121. break;
  11122. case 512:
  11123. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11124. LPFC_CQ_CNT_512);
  11125. break;
  11126. case 1024:
  11127. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11128. LPFC_CQ_CNT_1024);
  11129. break;
  11130. }
  11131. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11132. memset(dmabuf->virt, 0, hw_page_size);
  11133. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11134. putPaddrLow(dmabuf->phys);
  11135. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11136. putPaddrHigh(dmabuf->phys);
  11137. }
  11138. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11139. /* The IOCTL status is embedded in the mailbox subheader. */
  11140. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11141. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11142. if (shdr_status || shdr_add_status || rc) {
  11143. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11144. "2501 CQ_CREATE mailbox failed with "
  11145. "status x%x add_status x%x, mbx status x%x\n",
  11146. shdr_status, shdr_add_status, rc);
  11147. status = -ENXIO;
  11148. goto out;
  11149. }
  11150. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11151. if (cq->queue_id == 0xFFFF) {
  11152. status = -ENXIO;
  11153. goto out;
  11154. }
  11155. /* link the cq onto the parent eq child list */
  11156. list_add_tail(&cq->list, &eq->child_list);
  11157. /* Set up completion queue's type and subtype */
  11158. cq->type = type;
  11159. cq->subtype = subtype;
  11160. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11161. cq->assoc_qid = eq->queue_id;
  11162. cq->host_index = 0;
  11163. cq->hba_index = 0;
  11164. out:
  11165. mempool_free(mbox, phba->mbox_mem_pool);
  11166. return status;
  11167. }
  11168. /**
  11169. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11170. * @phba: HBA structure that indicates port to create a queue on.
  11171. * @mq: The queue structure to use to create the mailbox queue.
  11172. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11173. * @cq: The completion queue to associate with this cq.
  11174. *
  11175. * This function provides failback (fb) functionality when the
  11176. * mq_create_ext fails on older FW generations. It's purpose is identical
  11177. * to mq_create_ext otherwise.
  11178. *
  11179. * This routine cannot fail as all attributes were previously accessed and
  11180. * initialized in mq_create_ext.
  11181. **/
  11182. static void
  11183. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11184. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11185. {
  11186. struct lpfc_mbx_mq_create *mq_create;
  11187. struct lpfc_dmabuf *dmabuf;
  11188. int length;
  11189. length = (sizeof(struct lpfc_mbx_mq_create) -
  11190. sizeof(struct lpfc_sli4_cfg_mhdr));
  11191. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11192. LPFC_MBOX_OPCODE_MQ_CREATE,
  11193. length, LPFC_SLI4_MBX_EMBED);
  11194. mq_create = &mbox->u.mqe.un.mq_create;
  11195. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11196. mq->page_count);
  11197. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11198. cq->queue_id);
  11199. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11200. switch (mq->entry_count) {
  11201. case 16:
  11202. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11203. LPFC_MQ_RING_SIZE_16);
  11204. break;
  11205. case 32:
  11206. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11207. LPFC_MQ_RING_SIZE_32);
  11208. break;
  11209. case 64:
  11210. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11211. LPFC_MQ_RING_SIZE_64);
  11212. break;
  11213. case 128:
  11214. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11215. LPFC_MQ_RING_SIZE_128);
  11216. break;
  11217. }
  11218. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11219. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11220. putPaddrLow(dmabuf->phys);
  11221. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11222. putPaddrHigh(dmabuf->phys);
  11223. }
  11224. }
  11225. /**
  11226. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11227. * @phba: HBA structure that indicates port to create a queue on.
  11228. * @mq: The queue structure to use to create the mailbox queue.
  11229. * @cq: The completion queue to associate with this cq.
  11230. * @subtype: The queue's subtype.
  11231. *
  11232. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11233. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11234. *
  11235. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11236. * is used to get the entry count and entry size that are necessary to
  11237. * determine the number of pages to allocate and use for this queue. This
  11238. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11239. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11240. * command to finish before continuing.
  11241. *
  11242. * On success this function will return a zero. If unable to allocate enough
  11243. * memory this function will return -ENOMEM. If the queue create mailbox command
  11244. * fails this function will return -ENXIO.
  11245. **/
  11246. int32_t
  11247. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11248. struct lpfc_queue *cq, uint32_t subtype)
  11249. {
  11250. struct lpfc_mbx_mq_create *mq_create;
  11251. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11252. struct lpfc_dmabuf *dmabuf;
  11253. LPFC_MBOXQ_t *mbox;
  11254. int rc, length, status = 0;
  11255. uint32_t shdr_status, shdr_add_status;
  11256. union lpfc_sli4_cfg_shdr *shdr;
  11257. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11258. /* sanity check on queue memory */
  11259. if (!mq || !cq)
  11260. return -ENODEV;
  11261. if (!phba->sli4_hba.pc_sli4_params.supported)
  11262. hw_page_size = SLI4_PAGE_SIZE;
  11263. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11264. if (!mbox)
  11265. return -ENOMEM;
  11266. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11267. sizeof(struct lpfc_sli4_cfg_mhdr));
  11268. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11269. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11270. length, LPFC_SLI4_MBX_EMBED);
  11271. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11272. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11273. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11274. &mq_create_ext->u.request, mq->page_count);
  11275. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11276. &mq_create_ext->u.request, 1);
  11277. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11278. &mq_create_ext->u.request, 1);
  11279. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11280. &mq_create_ext->u.request, 1);
  11281. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11282. &mq_create_ext->u.request, 1);
  11283. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11284. &mq_create_ext->u.request, 1);
  11285. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11286. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11287. phba->sli4_hba.pc_sli4_params.mqv);
  11288. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11289. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11290. cq->queue_id);
  11291. else
  11292. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11293. cq->queue_id);
  11294. switch (mq->entry_count) {
  11295. default:
  11296. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11297. "0362 Unsupported MQ count. (%d)\n",
  11298. mq->entry_count);
  11299. if (mq->entry_count < 16)
  11300. return -EINVAL;
  11301. /* otherwise default to smallest count (drop through) */
  11302. case 16:
  11303. bf_set(lpfc_mq_context_ring_size,
  11304. &mq_create_ext->u.request.context,
  11305. LPFC_MQ_RING_SIZE_16);
  11306. break;
  11307. case 32:
  11308. bf_set(lpfc_mq_context_ring_size,
  11309. &mq_create_ext->u.request.context,
  11310. LPFC_MQ_RING_SIZE_32);
  11311. break;
  11312. case 64:
  11313. bf_set(lpfc_mq_context_ring_size,
  11314. &mq_create_ext->u.request.context,
  11315. LPFC_MQ_RING_SIZE_64);
  11316. break;
  11317. case 128:
  11318. bf_set(lpfc_mq_context_ring_size,
  11319. &mq_create_ext->u.request.context,
  11320. LPFC_MQ_RING_SIZE_128);
  11321. break;
  11322. }
  11323. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11324. memset(dmabuf->virt, 0, hw_page_size);
  11325. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11326. putPaddrLow(dmabuf->phys);
  11327. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11328. putPaddrHigh(dmabuf->phys);
  11329. }
  11330. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11331. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11332. &mq_create_ext->u.response);
  11333. if (rc != MBX_SUCCESS) {
  11334. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11335. "2795 MQ_CREATE_EXT failed with "
  11336. "status x%x. Failback to MQ_CREATE.\n",
  11337. rc);
  11338. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11339. mq_create = &mbox->u.mqe.un.mq_create;
  11340. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11341. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11342. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11343. &mq_create->u.response);
  11344. }
  11345. /* The IOCTL status is embedded in the mailbox subheader. */
  11346. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11347. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11348. if (shdr_status || shdr_add_status || rc) {
  11349. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11350. "2502 MQ_CREATE mailbox failed with "
  11351. "status x%x add_status x%x, mbx status x%x\n",
  11352. shdr_status, shdr_add_status, rc);
  11353. status = -ENXIO;
  11354. goto out;
  11355. }
  11356. if (mq->queue_id == 0xFFFF) {
  11357. status = -ENXIO;
  11358. goto out;
  11359. }
  11360. mq->type = LPFC_MQ;
  11361. mq->assoc_qid = cq->queue_id;
  11362. mq->subtype = subtype;
  11363. mq->host_index = 0;
  11364. mq->hba_index = 0;
  11365. /* link the mq onto the parent cq child list */
  11366. list_add_tail(&mq->list, &cq->child_list);
  11367. out:
  11368. mempool_free(mbox, phba->mbox_mem_pool);
  11369. return status;
  11370. }
  11371. /**
  11372. * lpfc_wq_create - Create a Work Queue on the HBA
  11373. * @phba: HBA structure that indicates port to create a queue on.
  11374. * @wq: The queue structure to use to create the work queue.
  11375. * @cq: The completion queue to bind this work queue to.
  11376. * @subtype: The subtype of the work queue indicating its functionality.
  11377. *
  11378. * This function creates a work queue, as detailed in @wq, on a port, described
  11379. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11380. *
  11381. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11382. * is used to get the entry count and entry size that are necessary to
  11383. * determine the number of pages to allocate and use for this queue. The @cq
  11384. * is used to indicate which completion queue to bind this work queue to. This
  11385. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11386. * work queue. This function is asynchronous and will wait for the mailbox
  11387. * command to finish before continuing.
  11388. *
  11389. * On success this function will return a zero. If unable to allocate enough
  11390. * memory this function will return -ENOMEM. If the queue create mailbox command
  11391. * fails this function will return -ENXIO.
  11392. **/
  11393. uint32_t
  11394. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11395. struct lpfc_queue *cq, uint32_t subtype)
  11396. {
  11397. struct lpfc_mbx_wq_create *wq_create;
  11398. struct lpfc_dmabuf *dmabuf;
  11399. LPFC_MBOXQ_t *mbox;
  11400. int rc, length, status = 0;
  11401. uint32_t shdr_status, shdr_add_status;
  11402. union lpfc_sli4_cfg_shdr *shdr;
  11403. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11404. struct dma_address *page;
  11405. /* sanity check on queue memory */
  11406. if (!wq || !cq)
  11407. return -ENODEV;
  11408. if (!phba->sli4_hba.pc_sli4_params.supported)
  11409. hw_page_size = SLI4_PAGE_SIZE;
  11410. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11411. if (!mbox)
  11412. return -ENOMEM;
  11413. length = (sizeof(struct lpfc_mbx_wq_create) -
  11414. sizeof(struct lpfc_sli4_cfg_mhdr));
  11415. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11416. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11417. length, LPFC_SLI4_MBX_EMBED);
  11418. wq_create = &mbox->u.mqe.un.wq_create;
  11419. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11420. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11421. wq->page_count);
  11422. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11423. cq->queue_id);
  11424. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11425. phba->sli4_hba.pc_sli4_params.wqv);
  11426. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11427. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11428. wq->entry_count);
  11429. switch (wq->entry_size) {
  11430. default:
  11431. case 64:
  11432. bf_set(lpfc_mbx_wq_create_wqe_size,
  11433. &wq_create->u.request_1,
  11434. LPFC_WQ_WQE_SIZE_64);
  11435. break;
  11436. case 128:
  11437. bf_set(lpfc_mbx_wq_create_wqe_size,
  11438. &wq_create->u.request_1,
  11439. LPFC_WQ_WQE_SIZE_128);
  11440. break;
  11441. }
  11442. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11443. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11444. page = wq_create->u.request_1.page;
  11445. } else {
  11446. page = wq_create->u.request.page;
  11447. }
  11448. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11449. memset(dmabuf->virt, 0, hw_page_size);
  11450. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11451. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11452. }
  11453. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11454. /* The IOCTL status is embedded in the mailbox subheader. */
  11455. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11456. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11457. if (shdr_status || shdr_add_status || rc) {
  11458. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11459. "2503 WQ_CREATE mailbox failed with "
  11460. "status x%x add_status x%x, mbx status x%x\n",
  11461. shdr_status, shdr_add_status, rc);
  11462. status = -ENXIO;
  11463. goto out;
  11464. }
  11465. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11466. if (wq->queue_id == 0xFFFF) {
  11467. status = -ENXIO;
  11468. goto out;
  11469. }
  11470. wq->type = LPFC_WQ;
  11471. wq->assoc_qid = cq->queue_id;
  11472. wq->subtype = subtype;
  11473. wq->host_index = 0;
  11474. wq->hba_index = 0;
  11475. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11476. /* link the wq onto the parent cq child list */
  11477. list_add_tail(&wq->list, &cq->child_list);
  11478. out:
  11479. mempool_free(mbox, phba->mbox_mem_pool);
  11480. return status;
  11481. }
  11482. /**
  11483. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11484. * @phba: HBA structure that indicates port to create a queue on.
  11485. * @rq: The queue structure to use for the receive queue.
  11486. * @qno: The associated HBQ number
  11487. *
  11488. *
  11489. * For SLI4 we need to adjust the RQ repost value based on
  11490. * the number of buffers that are initially posted to the RQ.
  11491. */
  11492. void
  11493. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11494. {
  11495. uint32_t cnt;
  11496. /* sanity check on queue memory */
  11497. if (!rq)
  11498. return;
  11499. cnt = lpfc_hbq_defs[qno]->entry_count;
  11500. /* Recalc repost for RQs based on buffers initially posted */
  11501. cnt = (cnt >> 3);
  11502. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11503. cnt = LPFC_QUEUE_MIN_REPOST;
  11504. rq->entry_repost = cnt;
  11505. }
  11506. /**
  11507. * lpfc_rq_create - Create a Receive Queue on the HBA
  11508. * @phba: HBA structure that indicates port to create a queue on.
  11509. * @hrq: The queue structure to use to create the header receive queue.
  11510. * @drq: The queue structure to use to create the data receive queue.
  11511. * @cq: The completion queue to bind this work queue to.
  11512. *
  11513. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11514. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11515. * to the HBA.
  11516. *
  11517. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11518. * struct is used to get the entry count that is necessary to determine the
  11519. * number of pages to use for this queue. The @cq is used to indicate which
  11520. * completion queue to bind received buffers that are posted to these queues to.
  11521. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11522. * receive queue pair. This function is asynchronous and will wait for the
  11523. * mailbox command to finish before continuing.
  11524. *
  11525. * On success this function will return a zero. If unable to allocate enough
  11526. * memory this function will return -ENOMEM. If the queue create mailbox command
  11527. * fails this function will return -ENXIO.
  11528. **/
  11529. uint32_t
  11530. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11531. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11532. {
  11533. struct lpfc_mbx_rq_create *rq_create;
  11534. struct lpfc_dmabuf *dmabuf;
  11535. LPFC_MBOXQ_t *mbox;
  11536. int rc, length, status = 0;
  11537. uint32_t shdr_status, shdr_add_status;
  11538. union lpfc_sli4_cfg_shdr *shdr;
  11539. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11540. /* sanity check on queue memory */
  11541. if (!hrq || !drq || !cq)
  11542. return -ENODEV;
  11543. if (!phba->sli4_hba.pc_sli4_params.supported)
  11544. hw_page_size = SLI4_PAGE_SIZE;
  11545. if (hrq->entry_count != drq->entry_count)
  11546. return -EINVAL;
  11547. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11548. if (!mbox)
  11549. return -ENOMEM;
  11550. length = (sizeof(struct lpfc_mbx_rq_create) -
  11551. sizeof(struct lpfc_sli4_cfg_mhdr));
  11552. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11553. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11554. length, LPFC_SLI4_MBX_EMBED);
  11555. rq_create = &mbox->u.mqe.un.rq_create;
  11556. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11557. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11558. phba->sli4_hba.pc_sli4_params.rqv);
  11559. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11560. bf_set(lpfc_rq_context_rqe_count_1,
  11561. &rq_create->u.request.context,
  11562. hrq->entry_count);
  11563. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11564. bf_set(lpfc_rq_context_rqe_size,
  11565. &rq_create->u.request.context,
  11566. LPFC_RQE_SIZE_8);
  11567. bf_set(lpfc_rq_context_page_size,
  11568. &rq_create->u.request.context,
  11569. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11570. } else {
  11571. switch (hrq->entry_count) {
  11572. default:
  11573. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11574. "2535 Unsupported RQ count. (%d)\n",
  11575. hrq->entry_count);
  11576. if (hrq->entry_count < 512)
  11577. return -EINVAL;
  11578. /* otherwise default to smallest count (drop through) */
  11579. case 512:
  11580. bf_set(lpfc_rq_context_rqe_count,
  11581. &rq_create->u.request.context,
  11582. LPFC_RQ_RING_SIZE_512);
  11583. break;
  11584. case 1024:
  11585. bf_set(lpfc_rq_context_rqe_count,
  11586. &rq_create->u.request.context,
  11587. LPFC_RQ_RING_SIZE_1024);
  11588. break;
  11589. case 2048:
  11590. bf_set(lpfc_rq_context_rqe_count,
  11591. &rq_create->u.request.context,
  11592. LPFC_RQ_RING_SIZE_2048);
  11593. break;
  11594. case 4096:
  11595. bf_set(lpfc_rq_context_rqe_count,
  11596. &rq_create->u.request.context,
  11597. LPFC_RQ_RING_SIZE_4096);
  11598. break;
  11599. }
  11600. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11601. LPFC_HDR_BUF_SIZE);
  11602. }
  11603. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11604. cq->queue_id);
  11605. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11606. hrq->page_count);
  11607. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11608. memset(dmabuf->virt, 0, hw_page_size);
  11609. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11610. putPaddrLow(dmabuf->phys);
  11611. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11612. putPaddrHigh(dmabuf->phys);
  11613. }
  11614. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11615. /* The IOCTL status is embedded in the mailbox subheader. */
  11616. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11617. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11618. if (shdr_status || shdr_add_status || rc) {
  11619. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11620. "2504 RQ_CREATE mailbox failed with "
  11621. "status x%x add_status x%x, mbx status x%x\n",
  11622. shdr_status, shdr_add_status, rc);
  11623. status = -ENXIO;
  11624. goto out;
  11625. }
  11626. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11627. if (hrq->queue_id == 0xFFFF) {
  11628. status = -ENXIO;
  11629. goto out;
  11630. }
  11631. hrq->type = LPFC_HRQ;
  11632. hrq->assoc_qid = cq->queue_id;
  11633. hrq->subtype = subtype;
  11634. hrq->host_index = 0;
  11635. hrq->hba_index = 0;
  11636. /* now create the data queue */
  11637. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11638. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11639. length, LPFC_SLI4_MBX_EMBED);
  11640. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11641. phba->sli4_hba.pc_sli4_params.rqv);
  11642. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11643. bf_set(lpfc_rq_context_rqe_count_1,
  11644. &rq_create->u.request.context, hrq->entry_count);
  11645. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11646. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11647. LPFC_RQE_SIZE_8);
  11648. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11649. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11650. } else {
  11651. switch (drq->entry_count) {
  11652. default:
  11653. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11654. "2536 Unsupported RQ count. (%d)\n",
  11655. drq->entry_count);
  11656. if (drq->entry_count < 512)
  11657. return -EINVAL;
  11658. /* otherwise default to smallest count (drop through) */
  11659. case 512:
  11660. bf_set(lpfc_rq_context_rqe_count,
  11661. &rq_create->u.request.context,
  11662. LPFC_RQ_RING_SIZE_512);
  11663. break;
  11664. case 1024:
  11665. bf_set(lpfc_rq_context_rqe_count,
  11666. &rq_create->u.request.context,
  11667. LPFC_RQ_RING_SIZE_1024);
  11668. break;
  11669. case 2048:
  11670. bf_set(lpfc_rq_context_rqe_count,
  11671. &rq_create->u.request.context,
  11672. LPFC_RQ_RING_SIZE_2048);
  11673. break;
  11674. case 4096:
  11675. bf_set(lpfc_rq_context_rqe_count,
  11676. &rq_create->u.request.context,
  11677. LPFC_RQ_RING_SIZE_4096);
  11678. break;
  11679. }
  11680. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11681. LPFC_DATA_BUF_SIZE);
  11682. }
  11683. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11684. cq->queue_id);
  11685. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11686. drq->page_count);
  11687. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11688. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11689. putPaddrLow(dmabuf->phys);
  11690. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11691. putPaddrHigh(dmabuf->phys);
  11692. }
  11693. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11694. /* The IOCTL status is embedded in the mailbox subheader. */
  11695. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11696. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11697. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11698. if (shdr_status || shdr_add_status || rc) {
  11699. status = -ENXIO;
  11700. goto out;
  11701. }
  11702. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11703. if (drq->queue_id == 0xFFFF) {
  11704. status = -ENXIO;
  11705. goto out;
  11706. }
  11707. drq->type = LPFC_DRQ;
  11708. drq->assoc_qid = cq->queue_id;
  11709. drq->subtype = subtype;
  11710. drq->host_index = 0;
  11711. drq->hba_index = 0;
  11712. /* link the header and data RQs onto the parent cq child list */
  11713. list_add_tail(&hrq->list, &cq->child_list);
  11714. list_add_tail(&drq->list, &cq->child_list);
  11715. out:
  11716. mempool_free(mbox, phba->mbox_mem_pool);
  11717. return status;
  11718. }
  11719. /**
  11720. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11721. * @eq: The queue structure associated with the queue to destroy.
  11722. *
  11723. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11724. * command, specific to the type of queue, to the HBA.
  11725. *
  11726. * The @eq struct is used to get the queue ID of the queue to destroy.
  11727. *
  11728. * On success this function will return a zero. If the queue destroy mailbox
  11729. * command fails this function will return -ENXIO.
  11730. **/
  11731. uint32_t
  11732. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11733. {
  11734. LPFC_MBOXQ_t *mbox;
  11735. int rc, length, status = 0;
  11736. uint32_t shdr_status, shdr_add_status;
  11737. union lpfc_sli4_cfg_shdr *shdr;
  11738. /* sanity check on queue memory */
  11739. if (!eq)
  11740. return -ENODEV;
  11741. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11742. if (!mbox)
  11743. return -ENOMEM;
  11744. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11745. sizeof(struct lpfc_sli4_cfg_mhdr));
  11746. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11747. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11748. length, LPFC_SLI4_MBX_EMBED);
  11749. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11750. eq->queue_id);
  11751. mbox->vport = eq->phba->pport;
  11752. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11753. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11754. /* The IOCTL status is embedded in the mailbox subheader. */
  11755. shdr = (union lpfc_sli4_cfg_shdr *)
  11756. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11757. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11758. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11759. if (shdr_status || shdr_add_status || rc) {
  11760. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11761. "2505 EQ_DESTROY mailbox failed with "
  11762. "status x%x add_status x%x, mbx status x%x\n",
  11763. shdr_status, shdr_add_status, rc);
  11764. status = -ENXIO;
  11765. }
  11766. /* Remove eq from any list */
  11767. list_del_init(&eq->list);
  11768. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11769. return status;
  11770. }
  11771. /**
  11772. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11773. * @cq: The queue structure associated with the queue to destroy.
  11774. *
  11775. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11776. * command, specific to the type of queue, to the HBA.
  11777. *
  11778. * The @cq struct is used to get the queue ID of the queue to destroy.
  11779. *
  11780. * On success this function will return a zero. If the queue destroy mailbox
  11781. * command fails this function will return -ENXIO.
  11782. **/
  11783. uint32_t
  11784. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11785. {
  11786. LPFC_MBOXQ_t *mbox;
  11787. int rc, length, status = 0;
  11788. uint32_t shdr_status, shdr_add_status;
  11789. union lpfc_sli4_cfg_shdr *shdr;
  11790. /* sanity check on queue memory */
  11791. if (!cq)
  11792. return -ENODEV;
  11793. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11794. if (!mbox)
  11795. return -ENOMEM;
  11796. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11797. sizeof(struct lpfc_sli4_cfg_mhdr));
  11798. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11799. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11800. length, LPFC_SLI4_MBX_EMBED);
  11801. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11802. cq->queue_id);
  11803. mbox->vport = cq->phba->pport;
  11804. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11805. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11806. /* The IOCTL status is embedded in the mailbox subheader. */
  11807. shdr = (union lpfc_sli4_cfg_shdr *)
  11808. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11809. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11810. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11811. if (shdr_status || shdr_add_status || rc) {
  11812. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11813. "2506 CQ_DESTROY mailbox failed with "
  11814. "status x%x add_status x%x, mbx status x%x\n",
  11815. shdr_status, shdr_add_status, rc);
  11816. status = -ENXIO;
  11817. }
  11818. /* Remove cq from any list */
  11819. list_del_init(&cq->list);
  11820. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11821. return status;
  11822. }
  11823. /**
  11824. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11825. * @qm: The queue structure associated with the queue to destroy.
  11826. *
  11827. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11828. * command, specific to the type of queue, to the HBA.
  11829. *
  11830. * The @mq struct is used to get the queue ID of the queue to destroy.
  11831. *
  11832. * On success this function will return a zero. If the queue destroy mailbox
  11833. * command fails this function will return -ENXIO.
  11834. **/
  11835. uint32_t
  11836. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11837. {
  11838. LPFC_MBOXQ_t *mbox;
  11839. int rc, length, status = 0;
  11840. uint32_t shdr_status, shdr_add_status;
  11841. union lpfc_sli4_cfg_shdr *shdr;
  11842. /* sanity check on queue memory */
  11843. if (!mq)
  11844. return -ENODEV;
  11845. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11846. if (!mbox)
  11847. return -ENOMEM;
  11848. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11849. sizeof(struct lpfc_sli4_cfg_mhdr));
  11850. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11851. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11852. length, LPFC_SLI4_MBX_EMBED);
  11853. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11854. mq->queue_id);
  11855. mbox->vport = mq->phba->pport;
  11856. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11857. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11858. /* The IOCTL status is embedded in the mailbox subheader. */
  11859. shdr = (union lpfc_sli4_cfg_shdr *)
  11860. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11861. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11862. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11863. if (shdr_status || shdr_add_status || rc) {
  11864. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11865. "2507 MQ_DESTROY mailbox failed with "
  11866. "status x%x add_status x%x, mbx status x%x\n",
  11867. shdr_status, shdr_add_status, rc);
  11868. status = -ENXIO;
  11869. }
  11870. /* Remove mq from any list */
  11871. list_del_init(&mq->list);
  11872. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11873. return status;
  11874. }
  11875. /**
  11876. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11877. * @wq: The queue structure associated with the queue to destroy.
  11878. *
  11879. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11880. * command, specific to the type of queue, to the HBA.
  11881. *
  11882. * The @wq struct is used to get the queue ID of the queue to destroy.
  11883. *
  11884. * On success this function will return a zero. If the queue destroy mailbox
  11885. * command fails this function will return -ENXIO.
  11886. **/
  11887. uint32_t
  11888. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11889. {
  11890. LPFC_MBOXQ_t *mbox;
  11891. int rc, length, status = 0;
  11892. uint32_t shdr_status, shdr_add_status;
  11893. union lpfc_sli4_cfg_shdr *shdr;
  11894. /* sanity check on queue memory */
  11895. if (!wq)
  11896. return -ENODEV;
  11897. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11898. if (!mbox)
  11899. return -ENOMEM;
  11900. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11901. sizeof(struct lpfc_sli4_cfg_mhdr));
  11902. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11903. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11904. length, LPFC_SLI4_MBX_EMBED);
  11905. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11906. wq->queue_id);
  11907. mbox->vport = wq->phba->pport;
  11908. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11909. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11910. shdr = (union lpfc_sli4_cfg_shdr *)
  11911. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11912. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11913. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11914. if (shdr_status || shdr_add_status || rc) {
  11915. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11916. "2508 WQ_DESTROY mailbox failed with "
  11917. "status x%x add_status x%x, mbx status x%x\n",
  11918. shdr_status, shdr_add_status, rc);
  11919. status = -ENXIO;
  11920. }
  11921. /* Remove wq from any list */
  11922. list_del_init(&wq->list);
  11923. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11924. return status;
  11925. }
  11926. /**
  11927. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11928. * @rq: The queue structure associated with the queue to destroy.
  11929. *
  11930. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11931. * command, specific to the type of queue, to the HBA.
  11932. *
  11933. * The @rq struct is used to get the queue ID of the queue to destroy.
  11934. *
  11935. * On success this function will return a zero. If the queue destroy mailbox
  11936. * command fails this function will return -ENXIO.
  11937. **/
  11938. uint32_t
  11939. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11940. struct lpfc_queue *drq)
  11941. {
  11942. LPFC_MBOXQ_t *mbox;
  11943. int rc, length, status = 0;
  11944. uint32_t shdr_status, shdr_add_status;
  11945. union lpfc_sli4_cfg_shdr *shdr;
  11946. /* sanity check on queue memory */
  11947. if (!hrq || !drq)
  11948. return -ENODEV;
  11949. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11950. if (!mbox)
  11951. return -ENOMEM;
  11952. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11953. sizeof(struct lpfc_sli4_cfg_mhdr));
  11954. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11955. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11956. length, LPFC_SLI4_MBX_EMBED);
  11957. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11958. hrq->queue_id);
  11959. mbox->vport = hrq->phba->pport;
  11960. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11961. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11962. /* The IOCTL status is embedded in the mailbox subheader. */
  11963. shdr = (union lpfc_sli4_cfg_shdr *)
  11964. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11965. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11966. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11967. if (shdr_status || shdr_add_status || rc) {
  11968. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11969. "2509 RQ_DESTROY mailbox failed with "
  11970. "status x%x add_status x%x, mbx status x%x\n",
  11971. shdr_status, shdr_add_status, rc);
  11972. if (rc != MBX_TIMEOUT)
  11973. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11974. return -ENXIO;
  11975. }
  11976. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11977. drq->queue_id);
  11978. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11979. shdr = (union lpfc_sli4_cfg_shdr *)
  11980. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11981. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11982. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11983. if (shdr_status || shdr_add_status || rc) {
  11984. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11985. "2510 RQ_DESTROY mailbox failed with "
  11986. "status x%x add_status x%x, mbx status x%x\n",
  11987. shdr_status, shdr_add_status, rc);
  11988. status = -ENXIO;
  11989. }
  11990. list_del_init(&hrq->list);
  11991. list_del_init(&drq->list);
  11992. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11993. return status;
  11994. }
  11995. /**
  11996. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  11997. * @phba: The virtual port for which this call being executed.
  11998. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  11999. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12000. * @xritag: the xritag that ties this io to the SGL pages.
  12001. *
  12002. * This routine will post the sgl pages for the IO that has the xritag
  12003. * that is in the iocbq structure. The xritag is assigned during iocbq
  12004. * creation and persists for as long as the driver is loaded.
  12005. * if the caller has fewer than 256 scatter gather segments to map then
  12006. * pdma_phys_addr1 should be 0.
  12007. * If the caller needs to map more than 256 scatter gather segment then
  12008. * pdma_phys_addr1 should be a valid physical address.
  12009. * physical address for SGLs must be 64 byte aligned.
  12010. * If you are going to map 2 SGL's then the first one must have 256 entries
  12011. * the second sgl can have between 1 and 256 entries.
  12012. *
  12013. * Return codes:
  12014. * 0 - Success
  12015. * -ENXIO, -ENOMEM - Failure
  12016. **/
  12017. int
  12018. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12019. dma_addr_t pdma_phys_addr0,
  12020. dma_addr_t pdma_phys_addr1,
  12021. uint16_t xritag)
  12022. {
  12023. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12024. LPFC_MBOXQ_t *mbox;
  12025. int rc;
  12026. uint32_t shdr_status, shdr_add_status;
  12027. uint32_t mbox_tmo;
  12028. union lpfc_sli4_cfg_shdr *shdr;
  12029. if (xritag == NO_XRI) {
  12030. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12031. "0364 Invalid param:\n");
  12032. return -EINVAL;
  12033. }
  12034. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12035. if (!mbox)
  12036. return -ENOMEM;
  12037. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12038. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12039. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12040. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12041. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12042. &mbox->u.mqe.un.post_sgl_pages;
  12043. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12044. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12045. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12046. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12047. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12048. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12049. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12050. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12051. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12052. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12053. if (!phba->sli4_hba.intr_enable)
  12054. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12055. else {
  12056. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12057. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12058. }
  12059. /* The IOCTL status is embedded in the mailbox subheader. */
  12060. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12061. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12062. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12063. if (rc != MBX_TIMEOUT)
  12064. mempool_free(mbox, phba->mbox_mem_pool);
  12065. if (shdr_status || shdr_add_status || rc) {
  12066. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12067. "2511 POST_SGL mailbox failed with "
  12068. "status x%x add_status x%x, mbx status x%x\n",
  12069. shdr_status, shdr_add_status, rc);
  12070. rc = -ENXIO;
  12071. }
  12072. return 0;
  12073. }
  12074. /**
  12075. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12076. * @phba: pointer to lpfc hba data structure.
  12077. *
  12078. * This routine is invoked to post rpi header templates to the
  12079. * HBA consistent with the SLI-4 interface spec. This routine
  12080. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12081. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12082. *
  12083. * Returns
  12084. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12085. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12086. **/
  12087. uint16_t
  12088. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12089. {
  12090. unsigned long xri;
  12091. /*
  12092. * Fetch the next logical xri. Because this index is logical,
  12093. * the driver starts at 0 each time.
  12094. */
  12095. spin_lock_irq(&phba->hbalock);
  12096. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12097. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12098. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12099. spin_unlock_irq(&phba->hbalock);
  12100. return NO_XRI;
  12101. } else {
  12102. set_bit(xri, phba->sli4_hba.xri_bmask);
  12103. phba->sli4_hba.max_cfg_param.xri_used++;
  12104. }
  12105. spin_unlock_irq(&phba->hbalock);
  12106. return xri;
  12107. }
  12108. /**
  12109. * lpfc_sli4_free_xri - Release an xri for reuse.
  12110. * @phba: pointer to lpfc hba data structure.
  12111. *
  12112. * This routine is invoked to release an xri to the pool of
  12113. * available rpis maintained by the driver.
  12114. **/
  12115. void
  12116. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12117. {
  12118. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12119. phba->sli4_hba.max_cfg_param.xri_used--;
  12120. }
  12121. }
  12122. /**
  12123. * lpfc_sli4_free_xri - Release an xri for reuse.
  12124. * @phba: pointer to lpfc hba data structure.
  12125. *
  12126. * This routine is invoked to release an xri to the pool of
  12127. * available rpis maintained by the driver.
  12128. **/
  12129. void
  12130. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12131. {
  12132. spin_lock_irq(&phba->hbalock);
  12133. __lpfc_sli4_free_xri(phba, xri);
  12134. spin_unlock_irq(&phba->hbalock);
  12135. }
  12136. /**
  12137. * lpfc_sli4_next_xritag - Get an xritag for the io
  12138. * @phba: Pointer to HBA context object.
  12139. *
  12140. * This function gets an xritag for the iocb. If there is no unused xritag
  12141. * it will return 0xffff.
  12142. * The function returns the allocated xritag if successful, else returns zero.
  12143. * Zero is not a valid xritag.
  12144. * The caller is not required to hold any lock.
  12145. **/
  12146. uint16_t
  12147. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12148. {
  12149. uint16_t xri_index;
  12150. xri_index = lpfc_sli4_alloc_xri(phba);
  12151. if (xri_index != NO_XRI)
  12152. return xri_index;
  12153. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12154. "2004 Failed to allocate XRI.last XRITAG is %d"
  12155. " Max XRI is %d, Used XRI is %d\n",
  12156. xri_index,
  12157. phba->sli4_hba.max_cfg_param.max_xri,
  12158. phba->sli4_hba.max_cfg_param.xri_used);
  12159. return NO_XRI;
  12160. }
  12161. /**
  12162. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12163. * @phba: pointer to lpfc hba data structure.
  12164. * @post_sgl_list: pointer to els sgl entry list.
  12165. * @count: number of els sgl entries on the list.
  12166. *
  12167. * This routine is invoked to post a block of driver's sgl pages to the
  12168. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12169. * is only called when the driver is loading and after all IO has been
  12170. * stopped.
  12171. **/
  12172. static int
  12173. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12174. struct list_head *post_sgl_list,
  12175. int post_cnt)
  12176. {
  12177. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12178. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12179. struct sgl_page_pairs *sgl_pg_pairs;
  12180. void *viraddr;
  12181. LPFC_MBOXQ_t *mbox;
  12182. uint32_t reqlen, alloclen, pg_pairs;
  12183. uint32_t mbox_tmo;
  12184. uint16_t xritag_start = 0;
  12185. int rc = 0;
  12186. uint32_t shdr_status, shdr_add_status;
  12187. union lpfc_sli4_cfg_shdr *shdr;
  12188. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12189. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12190. if (reqlen > SLI4_PAGE_SIZE) {
  12191. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12192. "2559 Block sgl registration required DMA "
  12193. "size (%d) great than a page\n", reqlen);
  12194. return -ENOMEM;
  12195. }
  12196. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12197. if (!mbox)
  12198. return -ENOMEM;
  12199. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12200. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12201. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12202. LPFC_SLI4_MBX_NEMBED);
  12203. if (alloclen < reqlen) {
  12204. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12205. "0285 Allocated DMA memory size (%d) is "
  12206. "less than the requested DMA memory "
  12207. "size (%d)\n", alloclen, reqlen);
  12208. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12209. return -ENOMEM;
  12210. }
  12211. /* Set up the SGL pages in the non-embedded DMA pages */
  12212. viraddr = mbox->sge_array->addr[0];
  12213. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12214. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12215. pg_pairs = 0;
  12216. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12217. /* Set up the sge entry */
  12218. sgl_pg_pairs->sgl_pg0_addr_lo =
  12219. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12220. sgl_pg_pairs->sgl_pg0_addr_hi =
  12221. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12222. sgl_pg_pairs->sgl_pg1_addr_lo =
  12223. cpu_to_le32(putPaddrLow(0));
  12224. sgl_pg_pairs->sgl_pg1_addr_hi =
  12225. cpu_to_le32(putPaddrHigh(0));
  12226. /* Keep the first xritag on the list */
  12227. if (pg_pairs == 0)
  12228. xritag_start = sglq_entry->sli4_xritag;
  12229. sgl_pg_pairs++;
  12230. pg_pairs++;
  12231. }
  12232. /* Complete initialization and perform endian conversion. */
  12233. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12234. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12235. sgl->word0 = cpu_to_le32(sgl->word0);
  12236. if (!phba->sli4_hba.intr_enable)
  12237. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12238. else {
  12239. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12240. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12241. }
  12242. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12243. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12244. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12245. if (rc != MBX_TIMEOUT)
  12246. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12247. if (shdr_status || shdr_add_status || rc) {
  12248. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12249. "2513 POST_SGL_BLOCK mailbox command failed "
  12250. "status x%x add_status x%x mbx status x%x\n",
  12251. shdr_status, shdr_add_status, rc);
  12252. rc = -ENXIO;
  12253. }
  12254. return rc;
  12255. }
  12256. /**
  12257. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12258. * @phba: pointer to lpfc hba data structure.
  12259. * @sblist: pointer to scsi buffer list.
  12260. * @count: number of scsi buffers on the list.
  12261. *
  12262. * This routine is invoked to post a block of @count scsi sgl pages from a
  12263. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12264. * No Lock is held.
  12265. *
  12266. **/
  12267. int
  12268. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12269. struct list_head *sblist,
  12270. int count)
  12271. {
  12272. struct lpfc_scsi_buf *psb;
  12273. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12274. struct sgl_page_pairs *sgl_pg_pairs;
  12275. void *viraddr;
  12276. LPFC_MBOXQ_t *mbox;
  12277. uint32_t reqlen, alloclen, pg_pairs;
  12278. uint32_t mbox_tmo;
  12279. uint16_t xritag_start = 0;
  12280. int rc = 0;
  12281. uint32_t shdr_status, shdr_add_status;
  12282. dma_addr_t pdma_phys_bpl1;
  12283. union lpfc_sli4_cfg_shdr *shdr;
  12284. /* Calculate the requested length of the dma memory */
  12285. reqlen = count * sizeof(struct sgl_page_pairs) +
  12286. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12287. if (reqlen > SLI4_PAGE_SIZE) {
  12288. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12289. "0217 Block sgl registration required DMA "
  12290. "size (%d) great than a page\n", reqlen);
  12291. return -ENOMEM;
  12292. }
  12293. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12294. if (!mbox) {
  12295. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12296. "0283 Failed to allocate mbox cmd memory\n");
  12297. return -ENOMEM;
  12298. }
  12299. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12300. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12301. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12302. LPFC_SLI4_MBX_NEMBED);
  12303. if (alloclen < reqlen) {
  12304. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12305. "2561 Allocated DMA memory size (%d) is "
  12306. "less than the requested DMA memory "
  12307. "size (%d)\n", alloclen, reqlen);
  12308. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12309. return -ENOMEM;
  12310. }
  12311. /* Get the first SGE entry from the non-embedded DMA memory */
  12312. viraddr = mbox->sge_array->addr[0];
  12313. /* Set up the SGL pages in the non-embedded DMA pages */
  12314. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12315. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12316. pg_pairs = 0;
  12317. list_for_each_entry(psb, sblist, list) {
  12318. /* Set up the sge entry */
  12319. sgl_pg_pairs->sgl_pg0_addr_lo =
  12320. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12321. sgl_pg_pairs->sgl_pg0_addr_hi =
  12322. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12323. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12324. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12325. else
  12326. pdma_phys_bpl1 = 0;
  12327. sgl_pg_pairs->sgl_pg1_addr_lo =
  12328. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12329. sgl_pg_pairs->sgl_pg1_addr_hi =
  12330. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12331. /* Keep the first xritag on the list */
  12332. if (pg_pairs == 0)
  12333. xritag_start = psb->cur_iocbq.sli4_xritag;
  12334. sgl_pg_pairs++;
  12335. pg_pairs++;
  12336. }
  12337. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12338. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12339. /* Perform endian conversion if necessary */
  12340. sgl->word0 = cpu_to_le32(sgl->word0);
  12341. if (!phba->sli4_hba.intr_enable)
  12342. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12343. else {
  12344. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12345. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12346. }
  12347. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12348. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12349. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12350. if (rc != MBX_TIMEOUT)
  12351. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12352. if (shdr_status || shdr_add_status || rc) {
  12353. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12354. "2564 POST_SGL_BLOCK mailbox command failed "
  12355. "status x%x add_status x%x mbx status x%x\n",
  12356. shdr_status, shdr_add_status, rc);
  12357. rc = -ENXIO;
  12358. }
  12359. return rc;
  12360. }
  12361. /**
  12362. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12363. * @phba: pointer to lpfc_hba struct that the frame was received on
  12364. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12365. *
  12366. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12367. * valid type of frame that the LPFC driver will handle. This function will
  12368. * return a zero if the frame is a valid frame or a non zero value when the
  12369. * frame does not pass the check.
  12370. **/
  12371. static int
  12372. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12373. {
  12374. /* make rctl_names static to save stack space */
  12375. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12376. char *type_names[] = FC_TYPE_NAMES_INIT;
  12377. struct fc_vft_header *fc_vft_hdr;
  12378. uint32_t *header = (uint32_t *) fc_hdr;
  12379. switch (fc_hdr->fh_r_ctl) {
  12380. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12381. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12382. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12383. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12384. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12385. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12386. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12387. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12388. case FC_RCTL_ELS_REQ: /* extended link services request */
  12389. case FC_RCTL_ELS_REP: /* extended link services reply */
  12390. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12391. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12392. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12393. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12394. case FC_RCTL_BA_RMC: /* remove connection */
  12395. case FC_RCTL_BA_ACC: /* basic accept */
  12396. case FC_RCTL_BA_RJT: /* basic reject */
  12397. case FC_RCTL_BA_PRMT:
  12398. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12399. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12400. case FC_RCTL_P_RJT: /* port reject */
  12401. case FC_RCTL_F_RJT: /* fabric reject */
  12402. case FC_RCTL_P_BSY: /* port busy */
  12403. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12404. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12405. case FC_RCTL_LCR: /* link credit reset */
  12406. case FC_RCTL_END: /* end */
  12407. break;
  12408. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12409. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12410. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12411. return lpfc_fc_frame_check(phba, fc_hdr);
  12412. default:
  12413. goto drop;
  12414. }
  12415. switch (fc_hdr->fh_type) {
  12416. case FC_TYPE_BLS:
  12417. case FC_TYPE_ELS:
  12418. case FC_TYPE_FCP:
  12419. case FC_TYPE_CT:
  12420. break;
  12421. case FC_TYPE_IP:
  12422. case FC_TYPE_ILS:
  12423. default:
  12424. goto drop;
  12425. }
  12426. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12427. "2538 Received frame rctl:%s type:%s "
  12428. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12429. rctl_names[fc_hdr->fh_r_ctl],
  12430. type_names[fc_hdr->fh_type],
  12431. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12432. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12433. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12434. return 0;
  12435. drop:
  12436. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12437. "2539 Dropped frame rctl:%s type:%s\n",
  12438. rctl_names[fc_hdr->fh_r_ctl],
  12439. type_names[fc_hdr->fh_type]);
  12440. return 1;
  12441. }
  12442. /**
  12443. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12444. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12445. *
  12446. * This function processes the FC header to retrieve the VFI from the VF
  12447. * header, if one exists. This function will return the VFI if one exists
  12448. * or 0 if no VSAN Header exists.
  12449. **/
  12450. static uint32_t
  12451. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12452. {
  12453. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12454. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12455. return 0;
  12456. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12457. }
  12458. /**
  12459. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12460. * @phba: Pointer to the HBA structure to search for the vport on
  12461. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12462. * @fcfi: The FC Fabric ID that the frame came from
  12463. *
  12464. * This function searches the @phba for a vport that matches the content of the
  12465. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12466. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12467. * returns the matching vport pointer or NULL if unable to match frame to a
  12468. * vport.
  12469. **/
  12470. static struct lpfc_vport *
  12471. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12472. uint16_t fcfi)
  12473. {
  12474. struct lpfc_vport **vports;
  12475. struct lpfc_vport *vport = NULL;
  12476. int i;
  12477. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12478. fc_hdr->fh_d_id[1] << 8 |
  12479. fc_hdr->fh_d_id[2]);
  12480. if (did == Fabric_DID)
  12481. return phba->pport;
  12482. vports = lpfc_create_vport_work_array(phba);
  12483. if (vports != NULL)
  12484. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12485. if (phba->fcf.fcfi == fcfi &&
  12486. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12487. vports[i]->fc_myDID == did) {
  12488. vport = vports[i];
  12489. break;
  12490. }
  12491. }
  12492. lpfc_destroy_vport_work_array(phba, vports);
  12493. return vport;
  12494. }
  12495. /**
  12496. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12497. * @vport: The vport to work on.
  12498. *
  12499. * This function updates the receive sequence time stamp for this vport. The
  12500. * receive sequence time stamp indicates the time that the last frame of the
  12501. * the sequence that has been idle for the longest amount of time was received.
  12502. * the driver uses this time stamp to indicate if any received sequences have
  12503. * timed out.
  12504. **/
  12505. void
  12506. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12507. {
  12508. struct lpfc_dmabuf *h_buf;
  12509. struct hbq_dmabuf *dmabuf = NULL;
  12510. /* get the oldest sequence on the rcv list */
  12511. h_buf = list_get_first(&vport->rcv_buffer_list,
  12512. struct lpfc_dmabuf, list);
  12513. if (!h_buf)
  12514. return;
  12515. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12516. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12517. }
  12518. /**
  12519. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12520. * @vport: The vport that the received sequences were sent to.
  12521. *
  12522. * This function cleans up all outstanding received sequences. This is called
  12523. * by the driver when a link event or user action invalidates all the received
  12524. * sequences.
  12525. **/
  12526. void
  12527. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12528. {
  12529. struct lpfc_dmabuf *h_buf, *hnext;
  12530. struct lpfc_dmabuf *d_buf, *dnext;
  12531. struct hbq_dmabuf *dmabuf = NULL;
  12532. /* start with the oldest sequence on the rcv list */
  12533. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12534. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12535. list_del_init(&dmabuf->hbuf.list);
  12536. list_for_each_entry_safe(d_buf, dnext,
  12537. &dmabuf->dbuf.list, list) {
  12538. list_del_init(&d_buf->list);
  12539. lpfc_in_buf_free(vport->phba, d_buf);
  12540. }
  12541. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12542. }
  12543. }
  12544. /**
  12545. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12546. * @vport: The vport that the received sequences were sent to.
  12547. *
  12548. * This function determines whether any received sequences have timed out by
  12549. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12550. * indicates that there is at least one timed out sequence this routine will
  12551. * go through the received sequences one at a time from most inactive to most
  12552. * active to determine which ones need to be cleaned up. Once it has determined
  12553. * that a sequence needs to be cleaned up it will simply free up the resources
  12554. * without sending an abort.
  12555. **/
  12556. void
  12557. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12558. {
  12559. struct lpfc_dmabuf *h_buf, *hnext;
  12560. struct lpfc_dmabuf *d_buf, *dnext;
  12561. struct hbq_dmabuf *dmabuf = NULL;
  12562. unsigned long timeout;
  12563. int abort_count = 0;
  12564. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12565. vport->rcv_buffer_time_stamp);
  12566. if (list_empty(&vport->rcv_buffer_list) ||
  12567. time_before(jiffies, timeout))
  12568. return;
  12569. /* start with the oldest sequence on the rcv list */
  12570. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12571. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12572. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12573. dmabuf->time_stamp);
  12574. if (time_before(jiffies, timeout))
  12575. break;
  12576. abort_count++;
  12577. list_del_init(&dmabuf->hbuf.list);
  12578. list_for_each_entry_safe(d_buf, dnext,
  12579. &dmabuf->dbuf.list, list) {
  12580. list_del_init(&d_buf->list);
  12581. lpfc_in_buf_free(vport->phba, d_buf);
  12582. }
  12583. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12584. }
  12585. if (abort_count)
  12586. lpfc_update_rcv_time_stamp(vport);
  12587. }
  12588. /**
  12589. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12590. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12591. *
  12592. * This function searches through the existing incomplete sequences that have
  12593. * been sent to this @vport. If the frame matches one of the incomplete
  12594. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12595. * make up that sequence. If no sequence is found that matches this frame then
  12596. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12597. * This function returns a pointer to the first dmabuf in the sequence list that
  12598. * the frame was linked to.
  12599. **/
  12600. static struct hbq_dmabuf *
  12601. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12602. {
  12603. struct fc_frame_header *new_hdr;
  12604. struct fc_frame_header *temp_hdr;
  12605. struct lpfc_dmabuf *d_buf;
  12606. struct lpfc_dmabuf *h_buf;
  12607. struct hbq_dmabuf *seq_dmabuf = NULL;
  12608. struct hbq_dmabuf *temp_dmabuf = NULL;
  12609. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12610. dmabuf->time_stamp = jiffies;
  12611. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12612. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12613. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12614. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12615. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12616. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12617. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12618. continue;
  12619. /* found a pending sequence that matches this frame */
  12620. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12621. break;
  12622. }
  12623. if (!seq_dmabuf) {
  12624. /*
  12625. * This indicates first frame received for this sequence.
  12626. * Queue the buffer on the vport's rcv_buffer_list.
  12627. */
  12628. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12629. lpfc_update_rcv_time_stamp(vport);
  12630. return dmabuf;
  12631. }
  12632. temp_hdr = seq_dmabuf->hbuf.virt;
  12633. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12634. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12635. list_del_init(&seq_dmabuf->hbuf.list);
  12636. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12637. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12638. lpfc_update_rcv_time_stamp(vport);
  12639. return dmabuf;
  12640. }
  12641. /* move this sequence to the tail to indicate a young sequence */
  12642. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12643. seq_dmabuf->time_stamp = jiffies;
  12644. lpfc_update_rcv_time_stamp(vport);
  12645. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12646. temp_hdr = dmabuf->hbuf.virt;
  12647. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12648. return seq_dmabuf;
  12649. }
  12650. /* find the correct place in the sequence to insert this frame */
  12651. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12652. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12653. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12654. /*
  12655. * If the frame's sequence count is greater than the frame on
  12656. * the list then insert the frame right after this frame
  12657. */
  12658. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12659. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12660. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12661. return seq_dmabuf;
  12662. }
  12663. }
  12664. return NULL;
  12665. }
  12666. /**
  12667. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12668. * @vport: pointer to a vitural port
  12669. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12670. *
  12671. * This function tries to abort from the partially assembed sequence, described
  12672. * by the information from basic abbort @dmabuf. It checks to see whether such
  12673. * partially assembled sequence held by the driver. If so, it shall free up all
  12674. * the frames from the partially assembled sequence.
  12675. *
  12676. * Return
  12677. * true -- if there is matching partially assembled sequence present and all
  12678. * the frames freed with the sequence;
  12679. * false -- if there is no matching partially assembled sequence present so
  12680. * nothing got aborted in the lower layer driver
  12681. **/
  12682. static bool
  12683. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12684. struct hbq_dmabuf *dmabuf)
  12685. {
  12686. struct fc_frame_header *new_hdr;
  12687. struct fc_frame_header *temp_hdr;
  12688. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12689. struct hbq_dmabuf *seq_dmabuf = NULL;
  12690. /* Use the hdr_buf to find the sequence that matches this frame */
  12691. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12692. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12693. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12694. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12695. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12696. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12697. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12698. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12699. continue;
  12700. /* found a pending sequence that matches this frame */
  12701. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12702. break;
  12703. }
  12704. /* Free up all the frames from the partially assembled sequence */
  12705. if (seq_dmabuf) {
  12706. list_for_each_entry_safe(d_buf, n_buf,
  12707. &seq_dmabuf->dbuf.list, list) {
  12708. list_del_init(&d_buf->list);
  12709. lpfc_in_buf_free(vport->phba, d_buf);
  12710. }
  12711. return true;
  12712. }
  12713. return false;
  12714. }
  12715. /**
  12716. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12717. * @phba: Pointer to HBA context object.
  12718. * @cmd_iocbq: pointer to the command iocbq structure.
  12719. * @rsp_iocbq: pointer to the response iocbq structure.
  12720. *
  12721. * This function handles the sequence abort response iocb command complete
  12722. * event. It properly releases the memory allocated to the sequence abort
  12723. * accept iocb.
  12724. **/
  12725. static void
  12726. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12727. struct lpfc_iocbq *cmd_iocbq,
  12728. struct lpfc_iocbq *rsp_iocbq)
  12729. {
  12730. if (cmd_iocbq)
  12731. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12732. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12733. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12734. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12735. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12736. rsp_iocbq->iocb.ulpStatus,
  12737. rsp_iocbq->iocb.un.ulpWord[4]);
  12738. }
  12739. /**
  12740. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12741. * @phba: Pointer to HBA context object.
  12742. * @xri: xri id in transaction.
  12743. *
  12744. * This function validates the xri maps to the known range of XRIs allocated an
  12745. * used by the driver.
  12746. **/
  12747. uint16_t
  12748. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12749. uint16_t xri)
  12750. {
  12751. int i;
  12752. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12753. if (xri == phba->sli4_hba.xri_ids[i])
  12754. return i;
  12755. }
  12756. return NO_XRI;
  12757. }
  12758. /**
  12759. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12760. * @phba: Pointer to HBA context object.
  12761. * @fc_hdr: pointer to a FC frame header.
  12762. *
  12763. * This function sends a basic response to a previous unsol sequence abort
  12764. * event after aborting the sequence handling.
  12765. **/
  12766. static void
  12767. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12768. struct fc_frame_header *fc_hdr)
  12769. {
  12770. struct lpfc_iocbq *ctiocb = NULL;
  12771. struct lpfc_nodelist *ndlp;
  12772. uint16_t oxid, rxid;
  12773. uint32_t sid, fctl;
  12774. IOCB_t *icmd;
  12775. int rc;
  12776. if (!lpfc_is_link_up(phba))
  12777. return;
  12778. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12779. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12780. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12781. ndlp = lpfc_findnode_did(phba->pport, sid);
  12782. if (!ndlp) {
  12783. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12784. "1268 Find ndlp returned NULL for oxid:x%x "
  12785. "SID:x%x\n", oxid, sid);
  12786. return;
  12787. }
  12788. if (lpfc_sli4_xri_inrange(phba, rxid))
  12789. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12790. /* Allocate buffer for rsp iocb */
  12791. ctiocb = lpfc_sli_get_iocbq(phba);
  12792. if (!ctiocb)
  12793. return;
  12794. /* Extract the F_CTL field from FC_HDR */
  12795. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12796. icmd = &ctiocb->iocb;
  12797. icmd->un.xseq64.bdl.bdeSize = 0;
  12798. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12799. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12800. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12801. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12802. /* Fill in the rest of iocb fields */
  12803. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12804. icmd->ulpBdeCount = 0;
  12805. icmd->ulpLe = 1;
  12806. icmd->ulpClass = CLASS3;
  12807. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12808. ctiocb->context1 = ndlp;
  12809. ctiocb->iocb_cmpl = NULL;
  12810. ctiocb->vport = phba->pport;
  12811. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12812. ctiocb->sli4_lxritag = NO_XRI;
  12813. ctiocb->sli4_xritag = NO_XRI;
  12814. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12815. * send a BLS_RJT. The driver no longer has that exchange.
  12816. * Override the IOCB for a BA_RJT.
  12817. */
  12818. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  12819. phba->sli4_hba.max_cfg_param.xri_base) ||
  12820. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12821. phba->sli4_hba.max_cfg_param.xri_base)) {
  12822. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12823. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12824. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12825. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12826. }
  12827. if (fctl & FC_FC_EX_CTX) {
  12828. /* ABTS sent by responder to CT exchange, construction
  12829. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12830. * field and RX_ID from ABTS for RX_ID field.
  12831. */
  12832. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12833. } else {
  12834. /* ABTS sent by initiator to CT exchange, construction
  12835. * of BA_ACC will need to allocate a new XRI as for the
  12836. * XRI_TAG field.
  12837. */
  12838. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12839. }
  12840. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12841. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12842. /* Xmit CT abts response on exchange <xid> */
  12843. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12844. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12845. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12846. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12847. if (rc == IOCB_ERROR) {
  12848. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12849. "2925 Failed to issue CT ABTS RSP x%x on "
  12850. "xri x%x, Data x%x\n",
  12851. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12852. phba->link_state);
  12853. lpfc_sli_release_iocbq(phba, ctiocb);
  12854. }
  12855. }
  12856. /**
  12857. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12858. * @vport: Pointer to the vport on which this sequence was received
  12859. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12860. *
  12861. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12862. * receive sequence is only partially assembed by the driver, it shall abort
  12863. * the partially assembled frames for the sequence. Otherwise, if the
  12864. * unsolicited receive sequence has been completely assembled and passed to
  12865. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  12866. * unsolicited sequence has been aborted. After that, it will issue a basic
  12867. * accept to accept the abort.
  12868. **/
  12869. void
  12870. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  12871. struct hbq_dmabuf *dmabuf)
  12872. {
  12873. struct lpfc_hba *phba = vport->phba;
  12874. struct fc_frame_header fc_hdr;
  12875. uint32_t fctl;
  12876. bool abts_par;
  12877. /* Make a copy of fc_hdr before the dmabuf being released */
  12878. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  12879. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  12880. if (fctl & FC_FC_EX_CTX) {
  12881. /*
  12882. * ABTS sent by responder to exchange, just free the buffer
  12883. */
  12884. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12885. } else {
  12886. /*
  12887. * ABTS sent by initiator to exchange, need to do cleanup
  12888. */
  12889. /* Try to abort partially assembled seq */
  12890. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  12891. /* Send abort to ULP if partially seq abort failed */
  12892. if (abts_par == false)
  12893. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  12894. else
  12895. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12896. }
  12897. /* Send basic accept (BA_ACC) to the abort requester */
  12898. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  12899. }
  12900. /**
  12901. * lpfc_seq_complete - Indicates if a sequence is complete
  12902. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12903. *
  12904. * This function checks the sequence, starting with the frame described by
  12905. * @dmabuf, to see if all the frames associated with this sequence are present.
  12906. * the frames associated with this sequence are linked to the @dmabuf using the
  12907. * dbuf list. This function looks for two major things. 1) That the first frame
  12908. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  12909. * set. 3) That there are no holes in the sequence count. The function will
  12910. * return 1 when the sequence is complete, otherwise it will return 0.
  12911. **/
  12912. static int
  12913. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  12914. {
  12915. struct fc_frame_header *hdr;
  12916. struct lpfc_dmabuf *d_buf;
  12917. struct hbq_dmabuf *seq_dmabuf;
  12918. uint32_t fctl;
  12919. int seq_count = 0;
  12920. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12921. /* make sure first fame of sequence has a sequence count of zero */
  12922. if (hdr->fh_seq_cnt != seq_count)
  12923. return 0;
  12924. fctl = (hdr->fh_f_ctl[0] << 16 |
  12925. hdr->fh_f_ctl[1] << 8 |
  12926. hdr->fh_f_ctl[2]);
  12927. /* If last frame of sequence we can return success. */
  12928. if (fctl & FC_FC_END_SEQ)
  12929. return 1;
  12930. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  12931. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12932. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12933. /* If there is a hole in the sequence count then fail. */
  12934. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  12935. return 0;
  12936. fctl = (hdr->fh_f_ctl[0] << 16 |
  12937. hdr->fh_f_ctl[1] << 8 |
  12938. hdr->fh_f_ctl[2]);
  12939. /* If last frame of sequence we can return success. */
  12940. if (fctl & FC_FC_END_SEQ)
  12941. return 1;
  12942. }
  12943. return 0;
  12944. }
  12945. /**
  12946. * lpfc_prep_seq - Prep sequence for ULP processing
  12947. * @vport: Pointer to the vport on which this sequence was received
  12948. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12949. *
  12950. * This function takes a sequence, described by a list of frames, and creates
  12951. * a list of iocbq structures to describe the sequence. This iocbq list will be
  12952. * used to issue to the generic unsolicited sequence handler. This routine
  12953. * returns a pointer to the first iocbq in the list. If the function is unable
  12954. * to allocate an iocbq then it throw out the received frames that were not
  12955. * able to be described and return a pointer to the first iocbq. If unable to
  12956. * allocate any iocbqs (including the first) this function will return NULL.
  12957. **/
  12958. static struct lpfc_iocbq *
  12959. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  12960. {
  12961. struct hbq_dmabuf *hbq_buf;
  12962. struct lpfc_dmabuf *d_buf, *n_buf;
  12963. struct lpfc_iocbq *first_iocbq, *iocbq;
  12964. struct fc_frame_header *fc_hdr;
  12965. uint32_t sid;
  12966. uint32_t len, tot_len;
  12967. struct ulp_bde64 *pbde;
  12968. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12969. /* remove from receive buffer list */
  12970. list_del_init(&seq_dmabuf->hbuf.list);
  12971. lpfc_update_rcv_time_stamp(vport);
  12972. /* get the Remote Port's SID */
  12973. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12974. tot_len = 0;
  12975. /* Get an iocbq struct to fill in. */
  12976. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  12977. if (first_iocbq) {
  12978. /* Initialize the first IOCB. */
  12979. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  12980. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  12981. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  12982. first_iocbq->iocb.ulpContext = NO_XRI;
  12983. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  12984. be16_to_cpu(fc_hdr->fh_ox_id);
  12985. /* iocbq is prepped for internal consumption. Physical vpi. */
  12986. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  12987. vport->phba->vpi_ids[vport->vpi];
  12988. /* put the first buffer into the first IOCBq */
  12989. first_iocbq->context2 = &seq_dmabuf->dbuf;
  12990. first_iocbq->context3 = NULL;
  12991. first_iocbq->iocb.ulpBdeCount = 1;
  12992. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  12993. LPFC_DATA_BUF_SIZE;
  12994. first_iocbq->iocb.un.rcvels.remoteID = sid;
  12995. tot_len = bf_get(lpfc_rcqe_length,
  12996. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  12997. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  12998. }
  12999. iocbq = first_iocbq;
  13000. /*
  13001. * Each IOCBq can have two Buffers assigned, so go through the list
  13002. * of buffers for this sequence and save two buffers in each IOCBq
  13003. */
  13004. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13005. if (!iocbq) {
  13006. lpfc_in_buf_free(vport->phba, d_buf);
  13007. continue;
  13008. }
  13009. if (!iocbq->context3) {
  13010. iocbq->context3 = d_buf;
  13011. iocbq->iocb.ulpBdeCount++;
  13012. pbde = (struct ulp_bde64 *)
  13013. &iocbq->iocb.unsli3.sli3Words[4];
  13014. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13015. /* We need to get the size out of the right CQE */
  13016. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13017. len = bf_get(lpfc_rcqe_length,
  13018. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13019. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13020. tot_len += len;
  13021. } else {
  13022. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13023. if (!iocbq) {
  13024. if (first_iocbq) {
  13025. first_iocbq->iocb.ulpStatus =
  13026. IOSTAT_FCP_RSP_ERROR;
  13027. first_iocbq->iocb.un.ulpWord[4] =
  13028. IOERR_NO_RESOURCES;
  13029. }
  13030. lpfc_in_buf_free(vport->phba, d_buf);
  13031. continue;
  13032. }
  13033. iocbq->context2 = d_buf;
  13034. iocbq->context3 = NULL;
  13035. iocbq->iocb.ulpBdeCount = 1;
  13036. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13037. LPFC_DATA_BUF_SIZE;
  13038. /* We need to get the size out of the right CQE */
  13039. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13040. len = bf_get(lpfc_rcqe_length,
  13041. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13042. tot_len += len;
  13043. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13044. iocbq->iocb.un.rcvels.remoteID = sid;
  13045. list_add_tail(&iocbq->list, &first_iocbq->list);
  13046. }
  13047. }
  13048. return first_iocbq;
  13049. }
  13050. static void
  13051. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13052. struct hbq_dmabuf *seq_dmabuf)
  13053. {
  13054. struct fc_frame_header *fc_hdr;
  13055. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13056. struct lpfc_hba *phba = vport->phba;
  13057. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13058. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13059. if (!iocbq) {
  13060. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13061. "2707 Ring %d handler: Failed to allocate "
  13062. "iocb Rctl x%x Type x%x received\n",
  13063. LPFC_ELS_RING,
  13064. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13065. return;
  13066. }
  13067. if (!lpfc_complete_unsol_iocb(phba,
  13068. &phba->sli.ring[LPFC_ELS_RING],
  13069. iocbq, fc_hdr->fh_r_ctl,
  13070. fc_hdr->fh_type))
  13071. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13072. "2540 Ring %d handler: unexpected Rctl "
  13073. "x%x Type x%x received\n",
  13074. LPFC_ELS_RING,
  13075. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13076. /* Free iocb created in lpfc_prep_seq */
  13077. list_for_each_entry_safe(curr_iocb, next_iocb,
  13078. &iocbq->list, list) {
  13079. list_del_init(&curr_iocb->list);
  13080. lpfc_sli_release_iocbq(phba, curr_iocb);
  13081. }
  13082. lpfc_sli_release_iocbq(phba, iocbq);
  13083. }
  13084. /**
  13085. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13086. * @phba: Pointer to HBA context object.
  13087. *
  13088. * This function is called with no lock held. This function processes all
  13089. * the received buffers and gives it to upper layers when a received buffer
  13090. * indicates that it is the final frame in the sequence. The interrupt
  13091. * service routine processes received buffers at interrupt contexts and adds
  13092. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13093. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13094. * appropriate receive function when the final frame in a sequence is received.
  13095. **/
  13096. void
  13097. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13098. struct hbq_dmabuf *dmabuf)
  13099. {
  13100. struct hbq_dmabuf *seq_dmabuf;
  13101. struct fc_frame_header *fc_hdr;
  13102. struct lpfc_vport *vport;
  13103. uint32_t fcfi;
  13104. /* Process each received buffer */
  13105. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13106. /* check to see if this a valid type of frame */
  13107. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13108. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13109. return;
  13110. }
  13111. if ((bf_get(lpfc_cqe_code,
  13112. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13113. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13114. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13115. else
  13116. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13117. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13118. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13119. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13120. /* throw out the frame */
  13121. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13122. return;
  13123. }
  13124. /* Handle the basic abort sequence (BA_ABTS) event */
  13125. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13126. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13127. return;
  13128. }
  13129. /* Link this frame */
  13130. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13131. if (!seq_dmabuf) {
  13132. /* unable to add frame to vport - throw it out */
  13133. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13134. return;
  13135. }
  13136. /* If not last frame in sequence continue processing frames. */
  13137. if (!lpfc_seq_complete(seq_dmabuf))
  13138. return;
  13139. /* Send the complete sequence to the upper layer protocol */
  13140. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13141. }
  13142. /**
  13143. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13144. * @phba: pointer to lpfc hba data structure.
  13145. *
  13146. * This routine is invoked to post rpi header templates to the
  13147. * HBA consistent with the SLI-4 interface spec. This routine
  13148. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13149. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13150. *
  13151. * This routine does not require any locks. It's usage is expected
  13152. * to be driver load or reset recovery when the driver is
  13153. * sequential.
  13154. *
  13155. * Return codes
  13156. * 0 - successful
  13157. * -EIO - The mailbox failed to complete successfully.
  13158. * When this error occurs, the driver is not guaranteed
  13159. * to have any rpi regions posted to the device and
  13160. * must either attempt to repost the regions or take a
  13161. * fatal error.
  13162. **/
  13163. int
  13164. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13165. {
  13166. struct lpfc_rpi_hdr *rpi_page;
  13167. uint32_t rc = 0;
  13168. uint16_t lrpi = 0;
  13169. /* SLI4 ports that support extents do not require RPI headers. */
  13170. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13171. goto exit;
  13172. if (phba->sli4_hba.extents_in_use)
  13173. return -EIO;
  13174. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13175. /*
  13176. * Assign the rpi headers a physical rpi only if the driver
  13177. * has not initialized those resources. A port reset only
  13178. * needs the headers posted.
  13179. */
  13180. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13181. LPFC_RPI_RSRC_RDY)
  13182. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13183. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13184. if (rc != MBX_SUCCESS) {
  13185. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13186. "2008 Error %d posting all rpi "
  13187. "headers\n", rc);
  13188. rc = -EIO;
  13189. break;
  13190. }
  13191. }
  13192. exit:
  13193. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13194. LPFC_RPI_RSRC_RDY);
  13195. return rc;
  13196. }
  13197. /**
  13198. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13199. * @phba: pointer to lpfc hba data structure.
  13200. * @rpi_page: pointer to the rpi memory region.
  13201. *
  13202. * This routine is invoked to post a single rpi header to the
  13203. * HBA consistent with the SLI-4 interface spec. This memory region
  13204. * maps up to 64 rpi context regions.
  13205. *
  13206. * Return codes
  13207. * 0 - successful
  13208. * -ENOMEM - No available memory
  13209. * -EIO - The mailbox failed to complete successfully.
  13210. **/
  13211. int
  13212. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13213. {
  13214. LPFC_MBOXQ_t *mboxq;
  13215. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13216. uint32_t rc = 0;
  13217. uint32_t shdr_status, shdr_add_status;
  13218. union lpfc_sli4_cfg_shdr *shdr;
  13219. /* SLI4 ports that support extents do not require RPI headers. */
  13220. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13221. return rc;
  13222. if (phba->sli4_hba.extents_in_use)
  13223. return -EIO;
  13224. /* The port is notified of the header region via a mailbox command. */
  13225. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13226. if (!mboxq) {
  13227. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13228. "2001 Unable to allocate memory for issuing "
  13229. "SLI_CONFIG_SPECIAL mailbox command\n");
  13230. return -ENOMEM;
  13231. }
  13232. /* Post all rpi memory regions to the port. */
  13233. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13234. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13235. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13236. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13237. sizeof(struct lpfc_sli4_cfg_mhdr),
  13238. LPFC_SLI4_MBX_EMBED);
  13239. /* Post the physical rpi to the port for this rpi header. */
  13240. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13241. rpi_page->start_rpi);
  13242. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13243. hdr_tmpl, rpi_page->page_count);
  13244. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13245. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13246. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13247. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13248. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13249. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13250. if (rc != MBX_TIMEOUT)
  13251. mempool_free(mboxq, phba->mbox_mem_pool);
  13252. if (shdr_status || shdr_add_status || rc) {
  13253. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13254. "2514 POST_RPI_HDR mailbox failed with "
  13255. "status x%x add_status x%x, mbx status x%x\n",
  13256. shdr_status, shdr_add_status, rc);
  13257. rc = -ENXIO;
  13258. }
  13259. return rc;
  13260. }
  13261. /**
  13262. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13263. * @phba: pointer to lpfc hba data structure.
  13264. *
  13265. * This routine is invoked to post rpi header templates to the
  13266. * HBA consistent with the SLI-4 interface spec. This routine
  13267. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13268. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13269. *
  13270. * Returns
  13271. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13272. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13273. **/
  13274. int
  13275. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13276. {
  13277. unsigned long rpi;
  13278. uint16_t max_rpi, rpi_limit;
  13279. uint16_t rpi_remaining, lrpi = 0;
  13280. struct lpfc_rpi_hdr *rpi_hdr;
  13281. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13282. rpi_limit = phba->sli4_hba.next_rpi;
  13283. /*
  13284. * Fetch the next logical rpi. Because this index is logical,
  13285. * the driver starts at 0 each time.
  13286. */
  13287. spin_lock_irq(&phba->hbalock);
  13288. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13289. if (rpi >= rpi_limit)
  13290. rpi = LPFC_RPI_ALLOC_ERROR;
  13291. else {
  13292. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13293. phba->sli4_hba.max_cfg_param.rpi_used++;
  13294. phba->sli4_hba.rpi_count++;
  13295. }
  13296. /*
  13297. * Don't try to allocate more rpi header regions if the device limit
  13298. * has been exhausted.
  13299. */
  13300. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13301. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13302. spin_unlock_irq(&phba->hbalock);
  13303. return rpi;
  13304. }
  13305. /*
  13306. * RPI header postings are not required for SLI4 ports capable of
  13307. * extents.
  13308. */
  13309. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13310. spin_unlock_irq(&phba->hbalock);
  13311. return rpi;
  13312. }
  13313. /*
  13314. * If the driver is running low on rpi resources, allocate another
  13315. * page now. Note that the next_rpi value is used because
  13316. * it represents how many are actually in use whereas max_rpi notes
  13317. * how many are supported max by the device.
  13318. */
  13319. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13320. spin_unlock_irq(&phba->hbalock);
  13321. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13322. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13323. if (!rpi_hdr) {
  13324. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13325. "2002 Error Could not grow rpi "
  13326. "count\n");
  13327. } else {
  13328. lrpi = rpi_hdr->start_rpi;
  13329. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13330. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13331. }
  13332. }
  13333. return rpi;
  13334. }
  13335. /**
  13336. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13337. * @phba: pointer to lpfc hba data structure.
  13338. *
  13339. * This routine is invoked to release an rpi to the pool of
  13340. * available rpis maintained by the driver.
  13341. **/
  13342. void
  13343. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13344. {
  13345. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13346. phba->sli4_hba.rpi_count--;
  13347. phba->sli4_hba.max_cfg_param.rpi_used--;
  13348. }
  13349. }
  13350. /**
  13351. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13352. * @phba: pointer to lpfc hba data structure.
  13353. *
  13354. * This routine is invoked to release an rpi to the pool of
  13355. * available rpis maintained by the driver.
  13356. **/
  13357. void
  13358. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13359. {
  13360. spin_lock_irq(&phba->hbalock);
  13361. __lpfc_sli4_free_rpi(phba, rpi);
  13362. spin_unlock_irq(&phba->hbalock);
  13363. }
  13364. /**
  13365. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13366. * @phba: pointer to lpfc hba data structure.
  13367. *
  13368. * This routine is invoked to remove the memory region that
  13369. * provided rpi via a bitmask.
  13370. **/
  13371. void
  13372. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13373. {
  13374. kfree(phba->sli4_hba.rpi_bmask);
  13375. kfree(phba->sli4_hba.rpi_ids);
  13376. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13377. }
  13378. /**
  13379. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13380. * @phba: pointer to lpfc hba data structure.
  13381. *
  13382. * This routine is invoked to remove the memory region that
  13383. * provided rpi via a bitmask.
  13384. **/
  13385. int
  13386. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13387. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13388. {
  13389. LPFC_MBOXQ_t *mboxq;
  13390. struct lpfc_hba *phba = ndlp->phba;
  13391. int rc;
  13392. /* The port is notified of the header region via a mailbox command. */
  13393. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13394. if (!mboxq)
  13395. return -ENOMEM;
  13396. /* Post all rpi memory regions to the port. */
  13397. lpfc_resume_rpi(mboxq, ndlp);
  13398. if (cmpl) {
  13399. mboxq->mbox_cmpl = cmpl;
  13400. mboxq->context1 = arg;
  13401. mboxq->context2 = ndlp;
  13402. } else
  13403. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13404. mboxq->vport = ndlp->vport;
  13405. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13406. if (rc == MBX_NOT_FINISHED) {
  13407. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13408. "2010 Resume RPI Mailbox failed "
  13409. "status %d, mbxStatus x%x\n", rc,
  13410. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13411. mempool_free(mboxq, phba->mbox_mem_pool);
  13412. return -EIO;
  13413. }
  13414. return 0;
  13415. }
  13416. /**
  13417. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13418. * @vport: Pointer to the vport for which the vpi is being initialized
  13419. *
  13420. * This routine is invoked to activate a vpi with the port.
  13421. *
  13422. * Returns:
  13423. * 0 success
  13424. * -Evalue otherwise
  13425. **/
  13426. int
  13427. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13428. {
  13429. LPFC_MBOXQ_t *mboxq;
  13430. int rc = 0;
  13431. int retval = MBX_SUCCESS;
  13432. uint32_t mbox_tmo;
  13433. struct lpfc_hba *phba = vport->phba;
  13434. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13435. if (!mboxq)
  13436. return -ENOMEM;
  13437. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13438. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13439. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13440. if (rc != MBX_SUCCESS) {
  13441. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13442. "2022 INIT VPI Mailbox failed "
  13443. "status %d, mbxStatus x%x\n", rc,
  13444. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13445. retval = -EIO;
  13446. }
  13447. if (rc != MBX_TIMEOUT)
  13448. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13449. return retval;
  13450. }
  13451. /**
  13452. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13453. * @phba: pointer to lpfc hba data structure.
  13454. * @mboxq: Pointer to mailbox object.
  13455. *
  13456. * This routine is invoked to manually add a single FCF record. The caller
  13457. * must pass a completely initialized FCF_Record. This routine takes
  13458. * care of the nonembedded mailbox operations.
  13459. **/
  13460. static void
  13461. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13462. {
  13463. void *virt_addr;
  13464. union lpfc_sli4_cfg_shdr *shdr;
  13465. uint32_t shdr_status, shdr_add_status;
  13466. virt_addr = mboxq->sge_array->addr[0];
  13467. /* The IOCTL status is embedded in the mailbox subheader. */
  13468. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13469. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13470. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13471. if ((shdr_status || shdr_add_status) &&
  13472. (shdr_status != STATUS_FCF_IN_USE))
  13473. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13474. "2558 ADD_FCF_RECORD mailbox failed with "
  13475. "status x%x add_status x%x\n",
  13476. shdr_status, shdr_add_status);
  13477. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13478. }
  13479. /**
  13480. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13481. * @phba: pointer to lpfc hba data structure.
  13482. * @fcf_record: pointer to the initialized fcf record to add.
  13483. *
  13484. * This routine is invoked to manually add a single FCF record. The caller
  13485. * must pass a completely initialized FCF_Record. This routine takes
  13486. * care of the nonembedded mailbox operations.
  13487. **/
  13488. int
  13489. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13490. {
  13491. int rc = 0;
  13492. LPFC_MBOXQ_t *mboxq;
  13493. uint8_t *bytep;
  13494. void *virt_addr;
  13495. dma_addr_t phys_addr;
  13496. struct lpfc_mbx_sge sge;
  13497. uint32_t alloc_len, req_len;
  13498. uint32_t fcfindex;
  13499. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13500. if (!mboxq) {
  13501. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13502. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13503. return -ENOMEM;
  13504. }
  13505. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13506. sizeof(uint32_t);
  13507. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13508. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13509. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13510. req_len, LPFC_SLI4_MBX_NEMBED);
  13511. if (alloc_len < req_len) {
  13512. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13513. "2523 Allocated DMA memory size (x%x) is "
  13514. "less than the requested DMA memory "
  13515. "size (x%x)\n", alloc_len, req_len);
  13516. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13517. return -ENOMEM;
  13518. }
  13519. /*
  13520. * Get the first SGE entry from the non-embedded DMA memory. This
  13521. * routine only uses a single SGE.
  13522. */
  13523. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13524. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13525. virt_addr = mboxq->sge_array->addr[0];
  13526. /*
  13527. * Configure the FCF record for FCFI 0. This is the driver's
  13528. * hardcoded default and gets used in nonFIP mode.
  13529. */
  13530. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13531. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13532. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13533. /*
  13534. * Copy the fcf_index and the FCF Record Data. The data starts after
  13535. * the FCoE header plus word10. The data copy needs to be endian
  13536. * correct.
  13537. */
  13538. bytep += sizeof(uint32_t);
  13539. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13540. mboxq->vport = phba->pport;
  13541. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13542. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13543. if (rc == MBX_NOT_FINISHED) {
  13544. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13545. "2515 ADD_FCF_RECORD mailbox failed with "
  13546. "status 0x%x\n", rc);
  13547. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13548. rc = -EIO;
  13549. } else
  13550. rc = 0;
  13551. return rc;
  13552. }
  13553. /**
  13554. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13555. * @phba: pointer to lpfc hba data structure.
  13556. * @fcf_record: pointer to the fcf record to write the default data.
  13557. * @fcf_index: FCF table entry index.
  13558. *
  13559. * This routine is invoked to build the driver's default FCF record. The
  13560. * values used are hardcoded. This routine handles memory initialization.
  13561. *
  13562. **/
  13563. void
  13564. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13565. struct fcf_record *fcf_record,
  13566. uint16_t fcf_index)
  13567. {
  13568. memset(fcf_record, 0, sizeof(struct fcf_record));
  13569. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13570. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13571. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13572. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13573. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13574. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13575. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13576. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13577. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13578. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13579. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13580. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13581. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13582. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13583. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13584. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13585. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13586. /* Set the VLAN bit map */
  13587. if (phba->valid_vlan) {
  13588. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13589. = 1 << (phba->vlan_id % 8);
  13590. }
  13591. }
  13592. /**
  13593. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13594. * @phba: pointer to lpfc hba data structure.
  13595. * @fcf_index: FCF table entry offset.
  13596. *
  13597. * This routine is invoked to scan the entire FCF table by reading FCF
  13598. * record and processing it one at a time starting from the @fcf_index
  13599. * for initial FCF discovery or fast FCF failover rediscovery.
  13600. *
  13601. * Return 0 if the mailbox command is submitted successfully, none 0
  13602. * otherwise.
  13603. **/
  13604. int
  13605. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13606. {
  13607. int rc = 0, error;
  13608. LPFC_MBOXQ_t *mboxq;
  13609. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13610. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13611. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13612. if (!mboxq) {
  13613. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13614. "2000 Failed to allocate mbox for "
  13615. "READ_FCF cmd\n");
  13616. error = -ENOMEM;
  13617. goto fail_fcf_scan;
  13618. }
  13619. /* Construct the read FCF record mailbox command */
  13620. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13621. if (rc) {
  13622. error = -EINVAL;
  13623. goto fail_fcf_scan;
  13624. }
  13625. /* Issue the mailbox command asynchronously */
  13626. mboxq->vport = phba->pport;
  13627. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13628. spin_lock_irq(&phba->hbalock);
  13629. phba->hba_flag |= FCF_TS_INPROG;
  13630. spin_unlock_irq(&phba->hbalock);
  13631. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13632. if (rc == MBX_NOT_FINISHED)
  13633. error = -EIO;
  13634. else {
  13635. /* Reset eligible FCF count for new scan */
  13636. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13637. phba->fcf.eligible_fcf_cnt = 0;
  13638. error = 0;
  13639. }
  13640. fail_fcf_scan:
  13641. if (error) {
  13642. if (mboxq)
  13643. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13644. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13645. spin_lock_irq(&phba->hbalock);
  13646. phba->hba_flag &= ~FCF_TS_INPROG;
  13647. spin_unlock_irq(&phba->hbalock);
  13648. }
  13649. return error;
  13650. }
  13651. /**
  13652. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13653. * @phba: pointer to lpfc hba data structure.
  13654. * @fcf_index: FCF table entry offset.
  13655. *
  13656. * This routine is invoked to read an FCF record indicated by @fcf_index
  13657. * and to use it for FLOGI roundrobin FCF failover.
  13658. *
  13659. * Return 0 if the mailbox command is submitted successfully, none 0
  13660. * otherwise.
  13661. **/
  13662. int
  13663. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13664. {
  13665. int rc = 0, error;
  13666. LPFC_MBOXQ_t *mboxq;
  13667. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13668. if (!mboxq) {
  13669. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13670. "2763 Failed to allocate mbox for "
  13671. "READ_FCF cmd\n");
  13672. error = -ENOMEM;
  13673. goto fail_fcf_read;
  13674. }
  13675. /* Construct the read FCF record mailbox command */
  13676. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13677. if (rc) {
  13678. error = -EINVAL;
  13679. goto fail_fcf_read;
  13680. }
  13681. /* Issue the mailbox command asynchronously */
  13682. mboxq->vport = phba->pport;
  13683. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13684. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13685. if (rc == MBX_NOT_FINISHED)
  13686. error = -EIO;
  13687. else
  13688. error = 0;
  13689. fail_fcf_read:
  13690. if (error && mboxq)
  13691. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13692. return error;
  13693. }
  13694. /**
  13695. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13696. * @phba: pointer to lpfc hba data structure.
  13697. * @fcf_index: FCF table entry offset.
  13698. *
  13699. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13700. * determine whether it's eligible for FLOGI roundrobin failover list.
  13701. *
  13702. * Return 0 if the mailbox command is submitted successfully, none 0
  13703. * otherwise.
  13704. **/
  13705. int
  13706. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13707. {
  13708. int rc = 0, error;
  13709. LPFC_MBOXQ_t *mboxq;
  13710. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13711. if (!mboxq) {
  13712. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13713. "2758 Failed to allocate mbox for "
  13714. "READ_FCF cmd\n");
  13715. error = -ENOMEM;
  13716. goto fail_fcf_read;
  13717. }
  13718. /* Construct the read FCF record mailbox command */
  13719. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13720. if (rc) {
  13721. error = -EINVAL;
  13722. goto fail_fcf_read;
  13723. }
  13724. /* Issue the mailbox command asynchronously */
  13725. mboxq->vport = phba->pport;
  13726. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13727. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13728. if (rc == MBX_NOT_FINISHED)
  13729. error = -EIO;
  13730. else
  13731. error = 0;
  13732. fail_fcf_read:
  13733. if (error && mboxq)
  13734. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13735. return error;
  13736. }
  13737. /**
  13738. * lpfc_check_next_fcf_pri
  13739. * phba pointer to the lpfc_hba struct for this port.
  13740. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13741. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13742. * rr_bmask based on their priority level. Starting from the highest priority
  13743. * to the lowest. The most likely FCF candidate will be in the highest
  13744. * priority group. When this routine is called it searches the fcf_pri list for
  13745. * next lowest priority group and repopulates the rr_bmask with only those
  13746. * fcf_indexes.
  13747. * returns:
  13748. * 1=success 0=failure
  13749. **/
  13750. int
  13751. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13752. {
  13753. uint16_t next_fcf_pri;
  13754. uint16_t last_index;
  13755. struct lpfc_fcf_pri *fcf_pri;
  13756. int rc;
  13757. int ret = 0;
  13758. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13759. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13760. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13761. "3060 Last IDX %d\n", last_index);
  13762. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13763. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13764. "3061 Last IDX %d\n", last_index);
  13765. return 0; /* Empty rr list */
  13766. }
  13767. next_fcf_pri = 0;
  13768. /*
  13769. * Clear the rr_bmask and set all of the bits that are at this
  13770. * priority.
  13771. */
  13772. memset(phba->fcf.fcf_rr_bmask, 0,
  13773. sizeof(*phba->fcf.fcf_rr_bmask));
  13774. spin_lock_irq(&phba->hbalock);
  13775. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13776. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13777. continue;
  13778. /*
  13779. * the 1st priority that has not FLOGI failed
  13780. * will be the highest.
  13781. */
  13782. if (!next_fcf_pri)
  13783. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13784. spin_unlock_irq(&phba->hbalock);
  13785. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13786. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13787. fcf_pri->fcf_rec.fcf_index);
  13788. if (rc)
  13789. return 0;
  13790. }
  13791. spin_lock_irq(&phba->hbalock);
  13792. }
  13793. /*
  13794. * if next_fcf_pri was not set above and the list is not empty then
  13795. * we have failed flogis on all of them. So reset flogi failed
  13796. * and start at the begining.
  13797. */
  13798. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13799. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13800. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13801. /*
  13802. * the 1st priority that has not FLOGI failed
  13803. * will be the highest.
  13804. */
  13805. if (!next_fcf_pri)
  13806. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13807. spin_unlock_irq(&phba->hbalock);
  13808. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13809. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13810. fcf_pri->fcf_rec.fcf_index);
  13811. if (rc)
  13812. return 0;
  13813. }
  13814. spin_lock_irq(&phba->hbalock);
  13815. }
  13816. } else
  13817. ret = 1;
  13818. spin_unlock_irq(&phba->hbalock);
  13819. return ret;
  13820. }
  13821. /**
  13822. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13823. * @phba: pointer to lpfc hba data structure.
  13824. *
  13825. * This routine is to get the next eligible FCF record index in a round
  13826. * robin fashion. If the next eligible FCF record index equals to the
  13827. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13828. * shall be returned, otherwise, the next eligible FCF record's index
  13829. * shall be returned.
  13830. **/
  13831. uint16_t
  13832. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13833. {
  13834. uint16_t next_fcf_index;
  13835. /* Search start from next bit of currently registered FCF index */
  13836. next_priority:
  13837. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13838. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13839. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13840. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13841. next_fcf_index);
  13842. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13843. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13844. /*
  13845. * If we have wrapped then we need to clear the bits that
  13846. * have been tested so that we can detect when we should
  13847. * change the priority level.
  13848. */
  13849. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13850. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  13851. }
  13852. /* Check roundrobin failover list empty condition */
  13853. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  13854. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  13855. /*
  13856. * If next fcf index is not found check if there are lower
  13857. * Priority level fcf's in the fcf_priority list.
  13858. * Set up the rr_bmask with all of the avaiable fcf bits
  13859. * at that level and continue the selection process.
  13860. */
  13861. if (lpfc_check_next_fcf_pri_level(phba))
  13862. goto next_priority;
  13863. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13864. "2844 No roundrobin failover FCF available\n");
  13865. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  13866. return LPFC_FCOE_FCF_NEXT_NONE;
  13867. else {
  13868. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13869. "3063 Only FCF available idx %d, flag %x\n",
  13870. next_fcf_index,
  13871. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  13872. return next_fcf_index;
  13873. }
  13874. }
  13875. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  13876. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  13877. LPFC_FCF_FLOGI_FAILED)
  13878. goto next_priority;
  13879. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13880. "2845 Get next roundrobin failover FCF (x%x)\n",
  13881. next_fcf_index);
  13882. return next_fcf_index;
  13883. }
  13884. /**
  13885. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  13886. * @phba: pointer to lpfc hba data structure.
  13887. *
  13888. * This routine sets the FCF record index in to the eligible bmask for
  13889. * roundrobin failover search. It checks to make sure that the index
  13890. * does not go beyond the range of the driver allocated bmask dimension
  13891. * before setting the bit.
  13892. *
  13893. * Returns 0 if the index bit successfully set, otherwise, it returns
  13894. * -EINVAL.
  13895. **/
  13896. int
  13897. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  13898. {
  13899. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13900. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13901. "2610 FCF (x%x) reached driver's book "
  13902. "keeping dimension:x%x\n",
  13903. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13904. return -EINVAL;
  13905. }
  13906. /* Set the eligible FCF record index bmask */
  13907. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13908. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13909. "2790 Set FCF (x%x) to roundrobin FCF failover "
  13910. "bmask\n", fcf_index);
  13911. return 0;
  13912. }
  13913. /**
  13914. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  13915. * @phba: pointer to lpfc hba data structure.
  13916. *
  13917. * This routine clears the FCF record index from the eligible bmask for
  13918. * roundrobin failover search. It checks to make sure that the index
  13919. * does not go beyond the range of the driver allocated bmask dimension
  13920. * before clearing the bit.
  13921. **/
  13922. void
  13923. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  13924. {
  13925. struct lpfc_fcf_pri *fcf_pri;
  13926. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13927. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13928. "2762 FCF (x%x) reached driver's book "
  13929. "keeping dimension:x%x\n",
  13930. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13931. return;
  13932. }
  13933. /* Clear the eligible FCF record index bmask */
  13934. spin_lock_irq(&phba->hbalock);
  13935. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13936. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  13937. list_del_init(&fcf_pri->list);
  13938. break;
  13939. }
  13940. }
  13941. spin_unlock_irq(&phba->hbalock);
  13942. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13943. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13944. "2791 Clear FCF (x%x) from roundrobin failover "
  13945. "bmask\n", fcf_index);
  13946. }
  13947. /**
  13948. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  13949. * @phba: pointer to lpfc hba data structure.
  13950. *
  13951. * This routine is the completion routine for the rediscover FCF table mailbox
  13952. * command. If the mailbox command returned failure, it will try to stop the
  13953. * FCF rediscover wait timer.
  13954. **/
  13955. void
  13956. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  13957. {
  13958. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  13959. uint32_t shdr_status, shdr_add_status;
  13960. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  13961. shdr_status = bf_get(lpfc_mbox_hdr_status,
  13962. &redisc_fcf->header.cfg_shdr.response);
  13963. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  13964. &redisc_fcf->header.cfg_shdr.response);
  13965. if (shdr_status || shdr_add_status) {
  13966. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13967. "2746 Requesting for FCF rediscovery failed "
  13968. "status x%x add_status x%x\n",
  13969. shdr_status, shdr_add_status);
  13970. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  13971. spin_lock_irq(&phba->hbalock);
  13972. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  13973. spin_unlock_irq(&phba->hbalock);
  13974. /*
  13975. * CVL event triggered FCF rediscover request failed,
  13976. * last resort to re-try current registered FCF entry.
  13977. */
  13978. lpfc_retry_pport_discovery(phba);
  13979. } else {
  13980. spin_lock_irq(&phba->hbalock);
  13981. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  13982. spin_unlock_irq(&phba->hbalock);
  13983. /*
  13984. * DEAD FCF event triggered FCF rediscover request
  13985. * failed, last resort to fail over as a link down
  13986. * to FCF registration.
  13987. */
  13988. lpfc_sli4_fcf_dead_failthrough(phba);
  13989. }
  13990. } else {
  13991. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13992. "2775 Start FCF rediscover quiescent timer\n");
  13993. /*
  13994. * Start FCF rediscovery wait timer for pending FCF
  13995. * before rescan FCF record table.
  13996. */
  13997. lpfc_fcf_redisc_wait_start_timer(phba);
  13998. }
  13999. mempool_free(mbox, phba->mbox_mem_pool);
  14000. }
  14001. /**
  14002. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14003. * @phba: pointer to lpfc hba data structure.
  14004. *
  14005. * This routine is invoked to request for rediscovery of the entire FCF table
  14006. * by the port.
  14007. **/
  14008. int
  14009. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14010. {
  14011. LPFC_MBOXQ_t *mbox;
  14012. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14013. int rc, length;
  14014. /* Cancel retry delay timers to all vports before FCF rediscover */
  14015. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14016. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14017. if (!mbox) {
  14018. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14019. "2745 Failed to allocate mbox for "
  14020. "requesting FCF rediscover.\n");
  14021. return -ENOMEM;
  14022. }
  14023. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14024. sizeof(struct lpfc_sli4_cfg_mhdr));
  14025. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14026. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14027. length, LPFC_SLI4_MBX_EMBED);
  14028. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14029. /* Set count to 0 for invalidating the entire FCF database */
  14030. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14031. /* Issue the mailbox command asynchronously */
  14032. mbox->vport = phba->pport;
  14033. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14034. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14035. if (rc == MBX_NOT_FINISHED) {
  14036. mempool_free(mbox, phba->mbox_mem_pool);
  14037. return -EIO;
  14038. }
  14039. return 0;
  14040. }
  14041. /**
  14042. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14043. * @phba: pointer to lpfc hba data structure.
  14044. *
  14045. * This function is the failover routine as a last resort to the FCF DEAD
  14046. * event when driver failed to perform fast FCF failover.
  14047. **/
  14048. void
  14049. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14050. {
  14051. uint32_t link_state;
  14052. /*
  14053. * Last resort as FCF DEAD event failover will treat this as
  14054. * a link down, but save the link state because we don't want
  14055. * it to be changed to Link Down unless it is already down.
  14056. */
  14057. link_state = phba->link_state;
  14058. lpfc_linkdown(phba);
  14059. phba->link_state = link_state;
  14060. /* Unregister FCF if no devices connected to it */
  14061. lpfc_unregister_unused_fcf(phba);
  14062. }
  14063. /**
  14064. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14065. * @phba: pointer to lpfc hba data structure.
  14066. * @rgn23_data: pointer to configure region 23 data.
  14067. *
  14068. * This function gets SLI3 port configure region 23 data through memory dump
  14069. * mailbox command. When it successfully retrieves data, the size of the data
  14070. * will be returned, otherwise, 0 will be returned.
  14071. **/
  14072. static uint32_t
  14073. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14074. {
  14075. LPFC_MBOXQ_t *pmb = NULL;
  14076. MAILBOX_t *mb;
  14077. uint32_t offset = 0;
  14078. int rc;
  14079. if (!rgn23_data)
  14080. return 0;
  14081. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14082. if (!pmb) {
  14083. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14084. "2600 failed to allocate mailbox memory\n");
  14085. return 0;
  14086. }
  14087. mb = &pmb->u.mb;
  14088. do {
  14089. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14090. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14091. if (rc != MBX_SUCCESS) {
  14092. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14093. "2601 failed to read config "
  14094. "region 23, rc 0x%x Status 0x%x\n",
  14095. rc, mb->mbxStatus);
  14096. mb->un.varDmp.word_cnt = 0;
  14097. }
  14098. /*
  14099. * dump mem may return a zero when finished or we got a
  14100. * mailbox error, either way we are done.
  14101. */
  14102. if (mb->un.varDmp.word_cnt == 0)
  14103. break;
  14104. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14105. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14106. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14107. rgn23_data + offset,
  14108. mb->un.varDmp.word_cnt);
  14109. offset += mb->un.varDmp.word_cnt;
  14110. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14111. mempool_free(pmb, phba->mbox_mem_pool);
  14112. return offset;
  14113. }
  14114. /**
  14115. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14116. * @phba: pointer to lpfc hba data structure.
  14117. * @rgn23_data: pointer to configure region 23 data.
  14118. *
  14119. * This function gets SLI4 port configure region 23 data through memory dump
  14120. * mailbox command. When it successfully retrieves data, the size of the data
  14121. * will be returned, otherwise, 0 will be returned.
  14122. **/
  14123. static uint32_t
  14124. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14125. {
  14126. LPFC_MBOXQ_t *mboxq = NULL;
  14127. struct lpfc_dmabuf *mp = NULL;
  14128. struct lpfc_mqe *mqe;
  14129. uint32_t data_length = 0;
  14130. int rc;
  14131. if (!rgn23_data)
  14132. return 0;
  14133. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14134. if (!mboxq) {
  14135. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14136. "3105 failed to allocate mailbox memory\n");
  14137. return 0;
  14138. }
  14139. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14140. goto out;
  14141. mqe = &mboxq->u.mqe;
  14142. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14143. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14144. if (rc)
  14145. goto out;
  14146. data_length = mqe->un.mb_words[5];
  14147. if (data_length == 0)
  14148. goto out;
  14149. if (data_length > DMP_RGN23_SIZE) {
  14150. data_length = 0;
  14151. goto out;
  14152. }
  14153. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14154. out:
  14155. mempool_free(mboxq, phba->mbox_mem_pool);
  14156. if (mp) {
  14157. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14158. kfree(mp);
  14159. }
  14160. return data_length;
  14161. }
  14162. /**
  14163. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14164. * @phba: pointer to lpfc hba data structure.
  14165. *
  14166. * This function read region 23 and parse TLV for port status to
  14167. * decide if the user disaled the port. If the TLV indicates the
  14168. * port is disabled, the hba_flag is set accordingly.
  14169. **/
  14170. void
  14171. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14172. {
  14173. uint8_t *rgn23_data = NULL;
  14174. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14175. uint32_t offset = 0;
  14176. /* Get adapter Region 23 data */
  14177. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14178. if (!rgn23_data)
  14179. goto out;
  14180. if (phba->sli_rev < LPFC_SLI_REV4)
  14181. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14182. else {
  14183. if_type = bf_get(lpfc_sli_intf_if_type,
  14184. &phba->sli4_hba.sli_intf);
  14185. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14186. goto out;
  14187. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14188. }
  14189. if (!data_size)
  14190. goto out;
  14191. /* Check the region signature first */
  14192. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14193. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14194. "2619 Config region 23 has bad signature\n");
  14195. goto out;
  14196. }
  14197. offset += 4;
  14198. /* Check the data structure version */
  14199. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14200. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14201. "2620 Config region 23 has bad version\n");
  14202. goto out;
  14203. }
  14204. offset += 4;
  14205. /* Parse TLV entries in the region */
  14206. while (offset < data_size) {
  14207. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14208. break;
  14209. /*
  14210. * If the TLV is not driver specific TLV or driver id is
  14211. * not linux driver id, skip the record.
  14212. */
  14213. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14214. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14215. (rgn23_data[offset + 3] != 0)) {
  14216. offset += rgn23_data[offset + 1] * 4 + 4;
  14217. continue;
  14218. }
  14219. /* Driver found a driver specific TLV in the config region */
  14220. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14221. offset += 4;
  14222. tlv_offset = 0;
  14223. /*
  14224. * Search for configured port state sub-TLV.
  14225. */
  14226. while ((offset < data_size) &&
  14227. (tlv_offset < sub_tlv_len)) {
  14228. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14229. offset += 4;
  14230. tlv_offset += 4;
  14231. break;
  14232. }
  14233. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14234. offset += rgn23_data[offset + 1] * 4 + 4;
  14235. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14236. continue;
  14237. }
  14238. /* This HBA contains PORT_STE configured */
  14239. if (!rgn23_data[offset + 2])
  14240. phba->hba_flag |= LINK_DISABLED;
  14241. goto out;
  14242. }
  14243. }
  14244. out:
  14245. kfree(rgn23_data);
  14246. return;
  14247. }
  14248. /**
  14249. * lpfc_wr_object - write an object to the firmware
  14250. * @phba: HBA structure that indicates port to create a queue on.
  14251. * @dmabuf_list: list of dmabufs to write to the port.
  14252. * @size: the total byte value of the objects to write to the port.
  14253. * @offset: the current offset to be used to start the transfer.
  14254. *
  14255. * This routine will create a wr_object mailbox command to send to the port.
  14256. * the mailbox command will be constructed using the dma buffers described in
  14257. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14258. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14259. * used to indicate the starting offset of the transfer and will also return
  14260. * the offset after the write object mailbox has completed. @size is used to
  14261. * determine the end of the object and whether the eof bit should be set.
  14262. *
  14263. * Return 0 is successful and offset will contain the the new offset to use
  14264. * for the next write.
  14265. * Return negative value for error cases.
  14266. **/
  14267. int
  14268. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14269. uint32_t size, uint32_t *offset)
  14270. {
  14271. struct lpfc_mbx_wr_object *wr_object;
  14272. LPFC_MBOXQ_t *mbox;
  14273. int rc = 0, i = 0;
  14274. uint32_t shdr_status, shdr_add_status;
  14275. uint32_t mbox_tmo;
  14276. union lpfc_sli4_cfg_shdr *shdr;
  14277. struct lpfc_dmabuf *dmabuf;
  14278. uint32_t written = 0;
  14279. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14280. if (!mbox)
  14281. return -ENOMEM;
  14282. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14283. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14284. sizeof(struct lpfc_mbx_wr_object) -
  14285. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14286. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14287. wr_object->u.request.write_offset = *offset;
  14288. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14289. wr_object->u.request.object_name[0] =
  14290. cpu_to_le32(wr_object->u.request.object_name[0]);
  14291. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14292. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14293. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14294. break;
  14295. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14296. wr_object->u.request.bde[i].addrHigh =
  14297. putPaddrHigh(dmabuf->phys);
  14298. if (written + SLI4_PAGE_SIZE >= size) {
  14299. wr_object->u.request.bde[i].tus.f.bdeSize =
  14300. (size - written);
  14301. written += (size - written);
  14302. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14303. } else {
  14304. wr_object->u.request.bde[i].tus.f.bdeSize =
  14305. SLI4_PAGE_SIZE;
  14306. written += SLI4_PAGE_SIZE;
  14307. }
  14308. i++;
  14309. }
  14310. wr_object->u.request.bde_count = i;
  14311. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14312. if (!phba->sli4_hba.intr_enable)
  14313. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14314. else {
  14315. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14316. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14317. }
  14318. /* The IOCTL status is embedded in the mailbox subheader. */
  14319. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14320. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14321. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14322. if (rc != MBX_TIMEOUT)
  14323. mempool_free(mbox, phba->mbox_mem_pool);
  14324. if (shdr_status || shdr_add_status || rc) {
  14325. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14326. "3025 Write Object mailbox failed with "
  14327. "status x%x add_status x%x, mbx status x%x\n",
  14328. shdr_status, shdr_add_status, rc);
  14329. rc = -ENXIO;
  14330. } else
  14331. *offset += wr_object->u.response.actual_write_length;
  14332. return rc;
  14333. }
  14334. /**
  14335. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14336. * @vport: pointer to vport data structure.
  14337. *
  14338. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14339. * and REG_VPI mailbox commands associated with the vport. This function
  14340. * is called when driver want to restart discovery of the vport due to
  14341. * a Clear Virtual Link event.
  14342. **/
  14343. void
  14344. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14345. {
  14346. struct lpfc_hba *phba = vport->phba;
  14347. LPFC_MBOXQ_t *mb, *nextmb;
  14348. struct lpfc_dmabuf *mp;
  14349. struct lpfc_nodelist *ndlp;
  14350. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14351. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14352. LIST_HEAD(mbox_cmd_list);
  14353. uint8_t restart_loop;
  14354. /* Clean up internally queued mailbox commands with the vport */
  14355. spin_lock_irq(&phba->hbalock);
  14356. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14357. if (mb->vport != vport)
  14358. continue;
  14359. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14360. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14361. continue;
  14362. list_del(&mb->list);
  14363. list_add_tail(&mb->list, &mbox_cmd_list);
  14364. }
  14365. /* Clean up active mailbox command with the vport */
  14366. mb = phba->sli.mbox_active;
  14367. if (mb && (mb->vport == vport)) {
  14368. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14369. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14370. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14371. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14372. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14373. /* Put reference count for delayed processing */
  14374. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14375. /* Unregister the RPI when mailbox complete */
  14376. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14377. }
  14378. }
  14379. /* Cleanup any mailbox completions which are not yet processed */
  14380. do {
  14381. restart_loop = 0;
  14382. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14383. /*
  14384. * If this mailox is already processed or it is
  14385. * for another vport ignore it.
  14386. */
  14387. if ((mb->vport != vport) ||
  14388. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14389. continue;
  14390. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14391. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14392. continue;
  14393. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14394. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14395. ndlp = (struct lpfc_nodelist *)mb->context2;
  14396. /* Unregister the RPI when mailbox complete */
  14397. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14398. restart_loop = 1;
  14399. spin_unlock_irq(&phba->hbalock);
  14400. spin_lock(shost->host_lock);
  14401. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14402. spin_unlock(shost->host_lock);
  14403. spin_lock_irq(&phba->hbalock);
  14404. break;
  14405. }
  14406. }
  14407. } while (restart_loop);
  14408. spin_unlock_irq(&phba->hbalock);
  14409. /* Release the cleaned-up mailbox commands */
  14410. while (!list_empty(&mbox_cmd_list)) {
  14411. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14412. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14413. mp = (struct lpfc_dmabuf *) (mb->context1);
  14414. if (mp) {
  14415. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14416. kfree(mp);
  14417. }
  14418. ndlp = (struct lpfc_nodelist *) mb->context2;
  14419. mb->context2 = NULL;
  14420. if (ndlp) {
  14421. spin_lock(shost->host_lock);
  14422. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14423. spin_unlock(shost->host_lock);
  14424. lpfc_nlp_put(ndlp);
  14425. }
  14426. }
  14427. mempool_free(mb, phba->mbox_mem_pool);
  14428. }
  14429. /* Release the ndlp with the cleaned-up active mailbox command */
  14430. if (act_mbx_ndlp) {
  14431. spin_lock(shost->host_lock);
  14432. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14433. spin_unlock(shost->host_lock);
  14434. lpfc_nlp_put(act_mbx_ndlp);
  14435. }
  14436. }
  14437. /**
  14438. * lpfc_drain_txq - Drain the txq
  14439. * @phba: Pointer to HBA context object.
  14440. *
  14441. * This function attempt to submit IOCBs on the txq
  14442. * to the adapter. For SLI4 adapters, the txq contains
  14443. * ELS IOCBs that have been deferred because the there
  14444. * are no SGLs. This congestion can occur with large
  14445. * vport counts during node discovery.
  14446. **/
  14447. uint32_t
  14448. lpfc_drain_txq(struct lpfc_hba *phba)
  14449. {
  14450. LIST_HEAD(completions);
  14451. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14452. struct lpfc_iocbq *piocbq = 0;
  14453. unsigned long iflags = 0;
  14454. char *fail_msg = NULL;
  14455. struct lpfc_sglq *sglq;
  14456. union lpfc_wqe wqe;
  14457. spin_lock_irqsave(&phba->hbalock, iflags);
  14458. if (pring->txq_cnt > pring->txq_max)
  14459. pring->txq_max = pring->txq_cnt;
  14460. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14461. while (pring->txq_cnt) {
  14462. spin_lock_irqsave(&phba->hbalock, iflags);
  14463. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14464. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14465. if (!sglq) {
  14466. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14467. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14468. break;
  14469. } else {
  14470. if (!piocbq) {
  14471. /* The txq_cnt out of sync. This should
  14472. * never happen
  14473. */
  14474. sglq = __lpfc_clear_active_sglq(phba,
  14475. sglq->sli4_lxritag);
  14476. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14477. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14478. "2823 txq empty and txq_cnt is %d\n ",
  14479. pring->txq_cnt);
  14480. break;
  14481. }
  14482. }
  14483. /* The xri and iocb resources secured,
  14484. * attempt to issue request
  14485. */
  14486. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14487. piocbq->sli4_xritag = sglq->sli4_xritag;
  14488. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14489. fail_msg = "to convert bpl to sgl";
  14490. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14491. fail_msg = "to convert iocb to wqe";
  14492. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14493. fail_msg = " - Wq is full";
  14494. else
  14495. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14496. if (fail_msg) {
  14497. /* Failed means we can't issue and need to cancel */
  14498. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14499. "2822 IOCB failed %s iotag 0x%x "
  14500. "xri 0x%x\n",
  14501. fail_msg,
  14502. piocbq->iotag, piocbq->sli4_xritag);
  14503. list_add_tail(&piocbq->list, &completions);
  14504. }
  14505. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14506. }
  14507. /* Cancel all the IOCBs that cannot be issued */
  14508. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14509. IOERR_SLI_ABORTED);
  14510. return pring->txq_cnt;
  14511. }