lpfc_sli.c 485 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834
  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. 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_TXCMPLQ;
  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_TXCMPLQ) {
  2347. pring->txcmplq_cnt--;
  2348. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  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. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2379. /* remove from txcmpl queue list */
  2380. list_del_init(&cmd_iocb->list);
  2381. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2382. pring->txcmplq_cnt--;
  2383. return cmd_iocb;
  2384. }
  2385. }
  2386. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2387. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2388. iotag, phba->sli.last_iotag);
  2389. return NULL;
  2390. }
  2391. /**
  2392. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2393. * @phba: Pointer to HBA context object.
  2394. * @pring: Pointer to driver SLI ring object.
  2395. * @saveq: Pointer to the response iocb to be processed.
  2396. *
  2397. * This function is called by the ring event handler for non-fcp
  2398. * rings when there is a new response iocb in the response ring.
  2399. * The caller is not required to hold any locks. This function
  2400. * gets the command iocb associated with the response iocb and
  2401. * calls the completion handler for the command iocb. If there
  2402. * is no completion handler, the function will free the resources
  2403. * associated with command iocb. If the response iocb is for
  2404. * an already aborted command iocb, the status of the completion
  2405. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2406. * This function always returns 1.
  2407. **/
  2408. static int
  2409. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2410. struct lpfc_iocbq *saveq)
  2411. {
  2412. struct lpfc_iocbq *cmdiocbp;
  2413. int rc = 1;
  2414. unsigned long iflag;
  2415. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2416. spin_lock_irqsave(&phba->hbalock, iflag);
  2417. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2418. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2419. if (cmdiocbp) {
  2420. if (cmdiocbp->iocb_cmpl) {
  2421. /*
  2422. * If an ELS command failed send an event to mgmt
  2423. * application.
  2424. */
  2425. if (saveq->iocb.ulpStatus &&
  2426. (pring->ringno == LPFC_ELS_RING) &&
  2427. (cmdiocbp->iocb.ulpCommand ==
  2428. CMD_ELS_REQUEST64_CR))
  2429. lpfc_send_els_failure_event(phba,
  2430. cmdiocbp, saveq);
  2431. /*
  2432. * Post all ELS completions to the worker thread.
  2433. * All other are passed to the completion callback.
  2434. */
  2435. if (pring->ringno == LPFC_ELS_RING) {
  2436. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2437. (cmdiocbp->iocb_flag &
  2438. LPFC_DRIVER_ABORTED)) {
  2439. spin_lock_irqsave(&phba->hbalock,
  2440. iflag);
  2441. cmdiocbp->iocb_flag &=
  2442. ~LPFC_DRIVER_ABORTED;
  2443. spin_unlock_irqrestore(&phba->hbalock,
  2444. iflag);
  2445. saveq->iocb.ulpStatus =
  2446. IOSTAT_LOCAL_REJECT;
  2447. saveq->iocb.un.ulpWord[4] =
  2448. IOERR_SLI_ABORTED;
  2449. /* Firmware could still be in progress
  2450. * of DMAing payload, so don't free data
  2451. * buffer till after a hbeat.
  2452. */
  2453. spin_lock_irqsave(&phba->hbalock,
  2454. iflag);
  2455. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2456. spin_unlock_irqrestore(&phba->hbalock,
  2457. iflag);
  2458. }
  2459. if (phba->sli_rev == LPFC_SLI_REV4) {
  2460. if (saveq->iocb_flag &
  2461. LPFC_EXCHANGE_BUSY) {
  2462. /* Set cmdiocb flag for the
  2463. * exchange busy so sgl (xri)
  2464. * will not be released until
  2465. * the abort xri is received
  2466. * from hba.
  2467. */
  2468. spin_lock_irqsave(
  2469. &phba->hbalock, iflag);
  2470. cmdiocbp->iocb_flag |=
  2471. LPFC_EXCHANGE_BUSY;
  2472. spin_unlock_irqrestore(
  2473. &phba->hbalock, iflag);
  2474. }
  2475. if (cmdiocbp->iocb_flag &
  2476. LPFC_DRIVER_ABORTED) {
  2477. /*
  2478. * Clear LPFC_DRIVER_ABORTED
  2479. * bit in case it was driver
  2480. * initiated abort.
  2481. */
  2482. spin_lock_irqsave(
  2483. &phba->hbalock, iflag);
  2484. cmdiocbp->iocb_flag &=
  2485. ~LPFC_DRIVER_ABORTED;
  2486. spin_unlock_irqrestore(
  2487. &phba->hbalock, iflag);
  2488. cmdiocbp->iocb.ulpStatus =
  2489. IOSTAT_LOCAL_REJECT;
  2490. cmdiocbp->iocb.un.ulpWord[4] =
  2491. IOERR_ABORT_REQUESTED;
  2492. /*
  2493. * For SLI4, irsiocb contains
  2494. * NO_XRI in sli_xritag, it
  2495. * shall not affect releasing
  2496. * sgl (xri) process.
  2497. */
  2498. saveq->iocb.ulpStatus =
  2499. IOSTAT_LOCAL_REJECT;
  2500. saveq->iocb.un.ulpWord[4] =
  2501. IOERR_SLI_ABORTED;
  2502. spin_lock_irqsave(
  2503. &phba->hbalock, iflag);
  2504. saveq->iocb_flag |=
  2505. LPFC_DELAY_MEM_FREE;
  2506. spin_unlock_irqrestore(
  2507. &phba->hbalock, iflag);
  2508. }
  2509. }
  2510. }
  2511. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2512. } else
  2513. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2514. } else {
  2515. /*
  2516. * Unknown initiating command based on the response iotag.
  2517. * This could be the case on the ELS ring because of
  2518. * lpfc_els_abort().
  2519. */
  2520. if (pring->ringno != LPFC_ELS_RING) {
  2521. /*
  2522. * Ring <ringno> handler: unexpected completion IoTag
  2523. * <IoTag>
  2524. */
  2525. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2526. "0322 Ring %d handler: "
  2527. "unexpected completion IoTag x%x "
  2528. "Data: x%x x%x x%x x%x\n",
  2529. pring->ringno,
  2530. saveq->iocb.ulpIoTag,
  2531. saveq->iocb.ulpStatus,
  2532. saveq->iocb.un.ulpWord[4],
  2533. saveq->iocb.ulpCommand,
  2534. saveq->iocb.ulpContext);
  2535. }
  2536. }
  2537. return rc;
  2538. }
  2539. /**
  2540. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2541. * @phba: Pointer to HBA context object.
  2542. * @pring: Pointer to driver SLI ring object.
  2543. *
  2544. * This function is called from the iocb ring event handlers when
  2545. * put pointer is ahead of the get pointer for a ring. This function signal
  2546. * an error attention condition to the worker thread and the worker
  2547. * thread will transition the HBA to offline state.
  2548. **/
  2549. static void
  2550. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2551. {
  2552. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2553. /*
  2554. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2555. * rsp ring <portRspMax>
  2556. */
  2557. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2558. "0312 Ring %d handler: portRspPut %d "
  2559. "is bigger than rsp ring %d\n",
  2560. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2561. pring->numRiocb);
  2562. phba->link_state = LPFC_HBA_ERROR;
  2563. /*
  2564. * All error attention handlers are posted to
  2565. * worker thread
  2566. */
  2567. phba->work_ha |= HA_ERATT;
  2568. phba->work_hs = HS_FFER3;
  2569. lpfc_worker_wake_up(phba);
  2570. return;
  2571. }
  2572. /**
  2573. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2574. * @ptr: Pointer to address of HBA context object.
  2575. *
  2576. * This function is invoked by the Error Attention polling timer when the
  2577. * timer times out. It will check the SLI Error Attention register for
  2578. * possible attention events. If so, it will post an Error Attention event
  2579. * and wake up worker thread to process it. Otherwise, it will set up the
  2580. * Error Attention polling timer for the next poll.
  2581. **/
  2582. void lpfc_poll_eratt(unsigned long ptr)
  2583. {
  2584. struct lpfc_hba *phba;
  2585. uint32_t eratt = 0;
  2586. phba = (struct lpfc_hba *)ptr;
  2587. /* Check chip HA register for error event */
  2588. eratt = lpfc_sli_check_eratt(phba);
  2589. if (eratt)
  2590. /* Tell the worker thread there is work to do */
  2591. lpfc_worker_wake_up(phba);
  2592. else
  2593. /* Restart the timer for next eratt poll */
  2594. mod_timer(&phba->eratt_poll, jiffies +
  2595. HZ * LPFC_ERATT_POLL_INTERVAL);
  2596. return;
  2597. }
  2598. /**
  2599. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2600. * @phba: Pointer to HBA context object.
  2601. * @pring: Pointer to driver SLI ring object.
  2602. * @mask: Host attention register mask for this ring.
  2603. *
  2604. * This function is called from the interrupt context when there is a ring
  2605. * event for the fcp ring. The caller does not hold any lock.
  2606. * The function processes each response iocb in the response ring until it
  2607. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2608. * LE bit set. The function will call the completion handler of the command iocb
  2609. * if the response iocb indicates a completion for a command iocb or it is
  2610. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2611. * function if this is an unsolicited iocb.
  2612. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2613. * to check it explicitly.
  2614. */
  2615. int
  2616. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2617. struct lpfc_sli_ring *pring, uint32_t mask)
  2618. {
  2619. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2620. IOCB_t *irsp = NULL;
  2621. IOCB_t *entry = NULL;
  2622. struct lpfc_iocbq *cmdiocbq = NULL;
  2623. struct lpfc_iocbq rspiocbq;
  2624. uint32_t status;
  2625. uint32_t portRspPut, portRspMax;
  2626. int rc = 1;
  2627. lpfc_iocb_type type;
  2628. unsigned long iflag;
  2629. uint32_t rsp_cmpl = 0;
  2630. spin_lock_irqsave(&phba->hbalock, iflag);
  2631. pring->stats.iocb_event++;
  2632. /*
  2633. * The next available response entry should never exceed the maximum
  2634. * entries. If it does, treat it as an adapter hardware error.
  2635. */
  2636. portRspMax = pring->numRiocb;
  2637. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2638. if (unlikely(portRspPut >= portRspMax)) {
  2639. lpfc_sli_rsp_pointers_error(phba, pring);
  2640. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2641. return 1;
  2642. }
  2643. if (phba->fcp_ring_in_use) {
  2644. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2645. return 1;
  2646. } else
  2647. phba->fcp_ring_in_use = 1;
  2648. rmb();
  2649. while (pring->rspidx != portRspPut) {
  2650. /*
  2651. * Fetch an entry off the ring and copy it into a local data
  2652. * structure. The copy involves a byte-swap since the
  2653. * network byte order and pci byte orders are different.
  2654. */
  2655. entry = lpfc_resp_iocb(phba, pring);
  2656. phba->last_completion_time = jiffies;
  2657. if (++pring->rspidx >= portRspMax)
  2658. pring->rspidx = 0;
  2659. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2660. (uint32_t *) &rspiocbq.iocb,
  2661. phba->iocb_rsp_size);
  2662. INIT_LIST_HEAD(&(rspiocbq.list));
  2663. irsp = &rspiocbq.iocb;
  2664. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2665. pring->stats.iocb_rsp++;
  2666. rsp_cmpl++;
  2667. if (unlikely(irsp->ulpStatus)) {
  2668. /*
  2669. * If resource errors reported from HBA, reduce
  2670. * queuedepths of the SCSI device.
  2671. */
  2672. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2673. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2674. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2675. phba->lpfc_rampdown_queue_depth(phba);
  2676. spin_lock_irqsave(&phba->hbalock, iflag);
  2677. }
  2678. /* Rsp ring <ringno> error: IOCB */
  2679. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2680. "0336 Rsp Ring %d error: IOCB Data: "
  2681. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2682. pring->ringno,
  2683. irsp->un.ulpWord[0],
  2684. irsp->un.ulpWord[1],
  2685. irsp->un.ulpWord[2],
  2686. irsp->un.ulpWord[3],
  2687. irsp->un.ulpWord[4],
  2688. irsp->un.ulpWord[5],
  2689. *(uint32_t *)&irsp->un1,
  2690. *((uint32_t *)&irsp->un1 + 1));
  2691. }
  2692. switch (type) {
  2693. case LPFC_ABORT_IOCB:
  2694. case LPFC_SOL_IOCB:
  2695. /*
  2696. * Idle exchange closed via ABTS from port. No iocb
  2697. * resources need to be recovered.
  2698. */
  2699. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2700. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2701. "0333 IOCB cmd 0x%x"
  2702. " processed. Skipping"
  2703. " completion\n",
  2704. irsp->ulpCommand);
  2705. break;
  2706. }
  2707. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2708. &rspiocbq);
  2709. if (unlikely(!cmdiocbq))
  2710. break;
  2711. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2712. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2713. if (cmdiocbq->iocb_cmpl) {
  2714. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2715. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2716. &rspiocbq);
  2717. spin_lock_irqsave(&phba->hbalock, iflag);
  2718. }
  2719. break;
  2720. case LPFC_UNSOL_IOCB:
  2721. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2722. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2723. spin_lock_irqsave(&phba->hbalock, iflag);
  2724. break;
  2725. default:
  2726. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2727. char adaptermsg[LPFC_MAX_ADPTMSG];
  2728. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2729. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2730. MAX_MSG_DATA);
  2731. dev_warn(&((phba->pcidev)->dev),
  2732. "lpfc%d: %s\n",
  2733. phba->brd_no, adaptermsg);
  2734. } else {
  2735. /* Unknown IOCB command */
  2736. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2737. "0334 Unknown IOCB command "
  2738. "Data: x%x, x%x x%x x%x x%x\n",
  2739. type, irsp->ulpCommand,
  2740. irsp->ulpStatus,
  2741. irsp->ulpIoTag,
  2742. irsp->ulpContext);
  2743. }
  2744. break;
  2745. }
  2746. /*
  2747. * The response IOCB has been processed. Update the ring
  2748. * pointer in SLIM. If the port response put pointer has not
  2749. * been updated, sync the pgp->rspPutInx and fetch the new port
  2750. * response put pointer.
  2751. */
  2752. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2753. if (pring->rspidx == portRspPut)
  2754. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2755. }
  2756. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2757. pring->stats.iocb_rsp_full++;
  2758. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2759. writel(status, phba->CAregaddr);
  2760. readl(phba->CAregaddr);
  2761. }
  2762. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2763. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2764. pring->stats.iocb_cmd_empty++;
  2765. /* Force update of the local copy of cmdGetInx */
  2766. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2767. lpfc_sli_resume_iocb(phba, pring);
  2768. if ((pring->lpfc_sli_cmd_available))
  2769. (pring->lpfc_sli_cmd_available) (phba, pring);
  2770. }
  2771. phba->fcp_ring_in_use = 0;
  2772. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2773. return rc;
  2774. }
  2775. /**
  2776. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2777. * @phba: Pointer to HBA context object.
  2778. * @pring: Pointer to driver SLI ring object.
  2779. * @rspiocbp: Pointer to driver response IOCB object.
  2780. *
  2781. * This function is called from the worker thread when there is a slow-path
  2782. * response IOCB to process. This function chains all the response iocbs until
  2783. * seeing the iocb with the LE bit set. The function will call
  2784. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2785. * completion of a command iocb. The function will call the
  2786. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2787. * The function frees the resources or calls the completion handler if this
  2788. * iocb is an abort completion. The function returns NULL when the response
  2789. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2790. * this function shall chain the iocb on to the iocb_continueq and return the
  2791. * response iocb passed in.
  2792. **/
  2793. static struct lpfc_iocbq *
  2794. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2795. struct lpfc_iocbq *rspiocbp)
  2796. {
  2797. struct lpfc_iocbq *saveq;
  2798. struct lpfc_iocbq *cmdiocbp;
  2799. struct lpfc_iocbq *next_iocb;
  2800. IOCB_t *irsp = NULL;
  2801. uint32_t free_saveq;
  2802. uint8_t iocb_cmd_type;
  2803. lpfc_iocb_type type;
  2804. unsigned long iflag;
  2805. int rc;
  2806. spin_lock_irqsave(&phba->hbalock, iflag);
  2807. /* First add the response iocb to the countinueq list */
  2808. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2809. pring->iocb_continueq_cnt++;
  2810. /* Now, determine whether the list is completed for processing */
  2811. irsp = &rspiocbp->iocb;
  2812. if (irsp->ulpLe) {
  2813. /*
  2814. * By default, the driver expects to free all resources
  2815. * associated with this iocb completion.
  2816. */
  2817. free_saveq = 1;
  2818. saveq = list_get_first(&pring->iocb_continueq,
  2819. struct lpfc_iocbq, list);
  2820. irsp = &(saveq->iocb);
  2821. list_del_init(&pring->iocb_continueq);
  2822. pring->iocb_continueq_cnt = 0;
  2823. pring->stats.iocb_rsp++;
  2824. /*
  2825. * If resource errors reported from HBA, reduce
  2826. * queuedepths of the SCSI device.
  2827. */
  2828. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2829. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2830. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2831. phba->lpfc_rampdown_queue_depth(phba);
  2832. spin_lock_irqsave(&phba->hbalock, iflag);
  2833. }
  2834. if (irsp->ulpStatus) {
  2835. /* Rsp ring <ringno> error: IOCB */
  2836. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2837. "0328 Rsp Ring %d error: "
  2838. "IOCB Data: "
  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 "
  2842. "x%x x%x x%x x%x\n",
  2843. pring->ringno,
  2844. irsp->un.ulpWord[0],
  2845. irsp->un.ulpWord[1],
  2846. irsp->un.ulpWord[2],
  2847. irsp->un.ulpWord[3],
  2848. irsp->un.ulpWord[4],
  2849. irsp->un.ulpWord[5],
  2850. *(((uint32_t *) irsp) + 6),
  2851. *(((uint32_t *) irsp) + 7),
  2852. *(((uint32_t *) irsp) + 8),
  2853. *(((uint32_t *) irsp) + 9),
  2854. *(((uint32_t *) irsp) + 10),
  2855. *(((uint32_t *) irsp) + 11),
  2856. *(((uint32_t *) irsp) + 12),
  2857. *(((uint32_t *) irsp) + 13),
  2858. *(((uint32_t *) irsp) + 14),
  2859. *(((uint32_t *) irsp) + 15));
  2860. }
  2861. /*
  2862. * Fetch the IOCB command type and call the correct completion
  2863. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2864. * get freed back to the lpfc_iocb_list by the discovery
  2865. * kernel thread.
  2866. */
  2867. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2868. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2869. switch (type) {
  2870. case LPFC_SOL_IOCB:
  2871. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2872. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2873. spin_lock_irqsave(&phba->hbalock, iflag);
  2874. break;
  2875. case LPFC_UNSOL_IOCB:
  2876. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2877. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2878. spin_lock_irqsave(&phba->hbalock, iflag);
  2879. if (!rc)
  2880. free_saveq = 0;
  2881. break;
  2882. case LPFC_ABORT_IOCB:
  2883. cmdiocbp = NULL;
  2884. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2885. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2886. saveq);
  2887. if (cmdiocbp) {
  2888. /* Call the specified completion routine */
  2889. if (cmdiocbp->iocb_cmpl) {
  2890. spin_unlock_irqrestore(&phba->hbalock,
  2891. iflag);
  2892. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2893. saveq);
  2894. spin_lock_irqsave(&phba->hbalock,
  2895. iflag);
  2896. } else
  2897. __lpfc_sli_release_iocbq(phba,
  2898. cmdiocbp);
  2899. }
  2900. break;
  2901. case LPFC_UNKNOWN_IOCB:
  2902. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2903. char adaptermsg[LPFC_MAX_ADPTMSG];
  2904. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2905. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2906. MAX_MSG_DATA);
  2907. dev_warn(&((phba->pcidev)->dev),
  2908. "lpfc%d: %s\n",
  2909. phba->brd_no, adaptermsg);
  2910. } else {
  2911. /* Unknown IOCB command */
  2912. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2913. "0335 Unknown IOCB "
  2914. "command Data: x%x "
  2915. "x%x x%x x%x\n",
  2916. irsp->ulpCommand,
  2917. irsp->ulpStatus,
  2918. irsp->ulpIoTag,
  2919. irsp->ulpContext);
  2920. }
  2921. break;
  2922. }
  2923. if (free_saveq) {
  2924. list_for_each_entry_safe(rspiocbp, next_iocb,
  2925. &saveq->list, list) {
  2926. list_del(&rspiocbp->list);
  2927. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2928. }
  2929. __lpfc_sli_release_iocbq(phba, saveq);
  2930. }
  2931. rspiocbp = NULL;
  2932. }
  2933. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2934. return rspiocbp;
  2935. }
  2936. /**
  2937. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2938. * @phba: Pointer to HBA context object.
  2939. * @pring: Pointer to driver SLI ring object.
  2940. * @mask: Host attention register mask for this ring.
  2941. *
  2942. * This routine wraps the actual slow_ring event process routine from the
  2943. * API jump table function pointer from the lpfc_hba struct.
  2944. **/
  2945. void
  2946. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2947. struct lpfc_sli_ring *pring, uint32_t mask)
  2948. {
  2949. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2950. }
  2951. /**
  2952. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2953. * @phba: Pointer to HBA context object.
  2954. * @pring: Pointer to driver SLI ring object.
  2955. * @mask: Host attention register mask for this ring.
  2956. *
  2957. * This function is called from the worker thread when there is a ring event
  2958. * for non-fcp rings. The caller does not hold any lock. The function will
  2959. * remove each response iocb in the response ring and calls the handle
  2960. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2961. **/
  2962. static void
  2963. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2964. struct lpfc_sli_ring *pring, uint32_t mask)
  2965. {
  2966. struct lpfc_pgp *pgp;
  2967. IOCB_t *entry;
  2968. IOCB_t *irsp = NULL;
  2969. struct lpfc_iocbq *rspiocbp = NULL;
  2970. uint32_t portRspPut, portRspMax;
  2971. unsigned long iflag;
  2972. uint32_t status;
  2973. pgp = &phba->port_gp[pring->ringno];
  2974. spin_lock_irqsave(&phba->hbalock, iflag);
  2975. pring->stats.iocb_event++;
  2976. /*
  2977. * The next available response entry should never exceed the maximum
  2978. * entries. If it does, treat it as an adapter hardware error.
  2979. */
  2980. portRspMax = pring->numRiocb;
  2981. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2982. if (portRspPut >= portRspMax) {
  2983. /*
  2984. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2985. * rsp ring <portRspMax>
  2986. */
  2987. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2988. "0303 Ring %d handler: portRspPut %d "
  2989. "is bigger than rsp ring %d\n",
  2990. pring->ringno, portRspPut, portRspMax);
  2991. phba->link_state = LPFC_HBA_ERROR;
  2992. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2993. phba->work_hs = HS_FFER3;
  2994. lpfc_handle_eratt(phba);
  2995. return;
  2996. }
  2997. rmb();
  2998. while (pring->rspidx != portRspPut) {
  2999. /*
  3000. * Build a completion list and call the appropriate handler.
  3001. * The process is to get the next available response iocb, get
  3002. * a free iocb from the list, copy the response data into the
  3003. * free iocb, insert to the continuation list, and update the
  3004. * next response index to slim. This process makes response
  3005. * iocb's in the ring available to DMA as fast as possible but
  3006. * pays a penalty for a copy operation. Since the iocb is
  3007. * only 32 bytes, this penalty is considered small relative to
  3008. * the PCI reads for register values and a slim write. When
  3009. * the ulpLe field is set, the entire Command has been
  3010. * received.
  3011. */
  3012. entry = lpfc_resp_iocb(phba, pring);
  3013. phba->last_completion_time = jiffies;
  3014. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3015. if (rspiocbp == NULL) {
  3016. printk(KERN_ERR "%s: out of buffers! Failing "
  3017. "completion.\n", __func__);
  3018. break;
  3019. }
  3020. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3021. phba->iocb_rsp_size);
  3022. irsp = &rspiocbp->iocb;
  3023. if (++pring->rspidx >= portRspMax)
  3024. pring->rspidx = 0;
  3025. if (pring->ringno == LPFC_ELS_RING) {
  3026. lpfc_debugfs_slow_ring_trc(phba,
  3027. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3028. *(((uint32_t *) irsp) + 4),
  3029. *(((uint32_t *) irsp) + 6),
  3030. *(((uint32_t *) irsp) + 7));
  3031. }
  3032. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3033. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3034. /* Handle the response IOCB */
  3035. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3036. spin_lock_irqsave(&phba->hbalock, iflag);
  3037. /*
  3038. * If the port response put pointer has not been updated, sync
  3039. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3040. * response put pointer.
  3041. */
  3042. if (pring->rspidx == portRspPut) {
  3043. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3044. }
  3045. } /* while (pring->rspidx != portRspPut) */
  3046. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3047. /* At least one response entry has been freed */
  3048. pring->stats.iocb_rsp_full++;
  3049. /* SET RxRE_RSP in Chip Att register */
  3050. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3051. writel(status, phba->CAregaddr);
  3052. readl(phba->CAregaddr); /* flush */
  3053. }
  3054. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3055. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3056. pring->stats.iocb_cmd_empty++;
  3057. /* Force update of the local copy of cmdGetInx */
  3058. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3059. lpfc_sli_resume_iocb(phba, pring);
  3060. if ((pring->lpfc_sli_cmd_available))
  3061. (pring->lpfc_sli_cmd_available) (phba, pring);
  3062. }
  3063. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3064. return;
  3065. }
  3066. /**
  3067. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3068. * @phba: Pointer to HBA context object.
  3069. * @pring: Pointer to driver SLI ring object.
  3070. * @mask: Host attention register mask for this ring.
  3071. *
  3072. * This function is called from the worker thread when there is a pending
  3073. * ELS response iocb on the driver internal slow-path response iocb worker
  3074. * queue. The caller does not hold any lock. The function will remove each
  3075. * response iocb from the response worker queue and calls the handle
  3076. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3077. **/
  3078. static void
  3079. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3080. struct lpfc_sli_ring *pring, uint32_t mask)
  3081. {
  3082. struct lpfc_iocbq *irspiocbq;
  3083. struct hbq_dmabuf *dmabuf;
  3084. struct lpfc_cq_event *cq_event;
  3085. unsigned long iflag;
  3086. spin_lock_irqsave(&phba->hbalock, iflag);
  3087. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3088. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3089. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3090. /* Get the response iocb from the head of work queue */
  3091. spin_lock_irqsave(&phba->hbalock, iflag);
  3092. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3093. cq_event, struct lpfc_cq_event, list);
  3094. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3095. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3096. case CQE_CODE_COMPL_WQE:
  3097. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3098. cq_event);
  3099. /* Translate ELS WCQE to response IOCBQ */
  3100. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3101. irspiocbq);
  3102. if (irspiocbq)
  3103. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3104. irspiocbq);
  3105. break;
  3106. case CQE_CODE_RECEIVE:
  3107. case CQE_CODE_RECEIVE_V1:
  3108. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3109. cq_event);
  3110. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3111. break;
  3112. default:
  3113. break;
  3114. }
  3115. }
  3116. }
  3117. /**
  3118. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3119. * @phba: Pointer to HBA context object.
  3120. * @pring: Pointer to driver SLI ring object.
  3121. *
  3122. * This function aborts all iocbs in the given ring and frees all the iocb
  3123. * objects in txq. This function issues an abort iocb for all the iocb commands
  3124. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3125. * the return of this function. The caller is not required to hold any locks.
  3126. **/
  3127. void
  3128. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3129. {
  3130. LIST_HEAD(completions);
  3131. struct lpfc_iocbq *iocb, *next_iocb;
  3132. if (pring->ringno == LPFC_ELS_RING) {
  3133. lpfc_fabric_abort_hba(phba);
  3134. }
  3135. /* Error everything on txq and txcmplq
  3136. * First do the txq.
  3137. */
  3138. spin_lock_irq(&phba->hbalock);
  3139. list_splice_init(&pring->txq, &completions);
  3140. pring->txq_cnt = 0;
  3141. /* Next issue ABTS for everything on the txcmplq */
  3142. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3143. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3144. spin_unlock_irq(&phba->hbalock);
  3145. /* Cancel all the IOCBs from the completions list */
  3146. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3147. IOERR_SLI_ABORTED);
  3148. }
  3149. /**
  3150. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3151. * @phba: Pointer to HBA context object.
  3152. *
  3153. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3154. * objects in txq and txcmplq. This function will not issue abort iocbs
  3155. * for all the iocb commands in txcmplq, they will just be returned with
  3156. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3157. * slot has been permanently disabled.
  3158. **/
  3159. void
  3160. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3161. {
  3162. LIST_HEAD(txq);
  3163. LIST_HEAD(txcmplq);
  3164. struct lpfc_sli *psli = &phba->sli;
  3165. struct lpfc_sli_ring *pring;
  3166. /* Currently, only one fcp ring */
  3167. pring = &psli->ring[psli->fcp_ring];
  3168. spin_lock_irq(&phba->hbalock);
  3169. /* Retrieve everything on txq */
  3170. list_splice_init(&pring->txq, &txq);
  3171. pring->txq_cnt = 0;
  3172. /* Retrieve everything on the txcmplq */
  3173. list_splice_init(&pring->txcmplq, &txcmplq);
  3174. pring->txcmplq_cnt = 0;
  3175. /* Indicate the I/O queues are flushed */
  3176. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3177. spin_unlock_irq(&phba->hbalock);
  3178. /* Flush the txq */
  3179. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3180. IOERR_SLI_DOWN);
  3181. /* Flush the txcmpq */
  3182. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3183. IOERR_SLI_DOWN);
  3184. }
  3185. /**
  3186. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3187. * @phba: Pointer to HBA context object.
  3188. * @mask: Bit mask to be checked.
  3189. *
  3190. * This function reads the host status register and compares
  3191. * with the provided bit mask to check if HBA completed
  3192. * the restart. This function will wait in a loop for the
  3193. * HBA to complete restart. If the HBA does not restart within
  3194. * 15 iterations, the function will reset the HBA again. The
  3195. * function returns 1 when HBA fail to restart otherwise returns
  3196. * zero.
  3197. **/
  3198. static int
  3199. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3200. {
  3201. uint32_t status;
  3202. int i = 0;
  3203. int retval = 0;
  3204. /* Read the HBA Host Status Register */
  3205. if (lpfc_readl(phba->HSregaddr, &status))
  3206. return 1;
  3207. /*
  3208. * Check status register every 100ms for 5 retries, then every
  3209. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3210. * every 2.5 sec for 4.
  3211. * Break our of the loop if errors occurred during init.
  3212. */
  3213. while (((status & mask) != mask) &&
  3214. !(status & HS_FFERM) &&
  3215. i++ < 20) {
  3216. if (i <= 5)
  3217. msleep(10);
  3218. else if (i <= 10)
  3219. msleep(500);
  3220. else
  3221. msleep(2500);
  3222. if (i == 15) {
  3223. /* Do post */
  3224. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3225. lpfc_sli_brdrestart(phba);
  3226. }
  3227. /* Read the HBA Host Status Register */
  3228. if (lpfc_readl(phba->HSregaddr, &status)) {
  3229. retval = 1;
  3230. break;
  3231. }
  3232. }
  3233. /* Check to see if any errors occurred during init */
  3234. if ((status & HS_FFERM) || (i >= 20)) {
  3235. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3236. "2751 Adapter failed to restart, "
  3237. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3238. status,
  3239. readl(phba->MBslimaddr + 0xa8),
  3240. readl(phba->MBslimaddr + 0xac));
  3241. phba->link_state = LPFC_HBA_ERROR;
  3242. retval = 1;
  3243. }
  3244. return retval;
  3245. }
  3246. /**
  3247. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3248. * @phba: Pointer to HBA context object.
  3249. * @mask: Bit mask to be checked.
  3250. *
  3251. * This function checks the host status register to check if HBA is
  3252. * ready. This function will wait in a loop for the HBA to be ready
  3253. * If the HBA is not ready , the function will will reset the HBA PCI
  3254. * function again. The function returns 1 when HBA fail to be ready
  3255. * otherwise returns zero.
  3256. **/
  3257. static int
  3258. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3259. {
  3260. uint32_t status;
  3261. int retval = 0;
  3262. /* Read the HBA Host Status Register */
  3263. status = lpfc_sli4_post_status_check(phba);
  3264. if (status) {
  3265. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3266. lpfc_sli_brdrestart(phba);
  3267. status = lpfc_sli4_post_status_check(phba);
  3268. }
  3269. /* Check to see if any errors occurred during init */
  3270. if (status) {
  3271. phba->link_state = LPFC_HBA_ERROR;
  3272. retval = 1;
  3273. } else
  3274. phba->sli4_hba.intr_enable = 0;
  3275. return retval;
  3276. }
  3277. /**
  3278. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3279. * @phba: Pointer to HBA context object.
  3280. * @mask: Bit mask to be checked.
  3281. *
  3282. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3283. * from the API jump table function pointer from the lpfc_hba struct.
  3284. **/
  3285. int
  3286. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3287. {
  3288. return phba->lpfc_sli_brdready(phba, mask);
  3289. }
  3290. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3291. /**
  3292. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3293. * @phba: Pointer to HBA context object.
  3294. *
  3295. * This function is called before resetting an HBA. This function is called
  3296. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3297. **/
  3298. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3299. {
  3300. uint32_t __iomem *resp_buf;
  3301. uint32_t __iomem *mbox_buf;
  3302. volatile uint32_t mbox;
  3303. uint32_t hc_copy, ha_copy, resp_data;
  3304. int i;
  3305. uint8_t hdrtype;
  3306. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3307. if (hdrtype != 0x80 ||
  3308. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3309. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3310. return;
  3311. /*
  3312. * Tell the other part of the chip to suspend temporarily all
  3313. * its DMA activity.
  3314. */
  3315. resp_buf = phba->MBslimaddr;
  3316. /* Disable the error attention */
  3317. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3318. return;
  3319. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3320. readl(phba->HCregaddr); /* flush */
  3321. phba->link_flag |= LS_IGNORE_ERATT;
  3322. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3323. return;
  3324. if (ha_copy & HA_ERATT) {
  3325. /* Clear Chip error bit */
  3326. writel(HA_ERATT, phba->HAregaddr);
  3327. phba->pport->stopped = 1;
  3328. }
  3329. mbox = 0;
  3330. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3331. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3332. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3333. mbox_buf = phba->MBslimaddr;
  3334. writel(mbox, mbox_buf);
  3335. for (i = 0; i < 50; i++) {
  3336. if (lpfc_readl((resp_buf + 1), &resp_data))
  3337. return;
  3338. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3339. mdelay(1);
  3340. else
  3341. break;
  3342. }
  3343. resp_data = 0;
  3344. if (lpfc_readl((resp_buf + 1), &resp_data))
  3345. return;
  3346. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3347. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3348. phba->pport->stopped)
  3349. goto restore_hc;
  3350. else
  3351. goto clear_errat;
  3352. }
  3353. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3354. resp_data = 0;
  3355. for (i = 0; i < 500; i++) {
  3356. if (lpfc_readl(resp_buf, &resp_data))
  3357. return;
  3358. if (resp_data != mbox)
  3359. mdelay(1);
  3360. else
  3361. break;
  3362. }
  3363. clear_errat:
  3364. while (++i < 500) {
  3365. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3366. return;
  3367. if (!(ha_copy & HA_ERATT))
  3368. mdelay(1);
  3369. else
  3370. break;
  3371. }
  3372. if (readl(phba->HAregaddr) & HA_ERATT) {
  3373. writel(HA_ERATT, phba->HAregaddr);
  3374. phba->pport->stopped = 1;
  3375. }
  3376. restore_hc:
  3377. phba->link_flag &= ~LS_IGNORE_ERATT;
  3378. writel(hc_copy, phba->HCregaddr);
  3379. readl(phba->HCregaddr); /* flush */
  3380. }
  3381. /**
  3382. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3383. * @phba: Pointer to HBA context object.
  3384. *
  3385. * This function issues a kill_board mailbox command and waits for
  3386. * the error attention interrupt. This function is called for stopping
  3387. * the firmware processing. The caller is not required to hold any
  3388. * locks. This function calls lpfc_hba_down_post function to free
  3389. * any pending commands after the kill. The function will return 1 when it
  3390. * fails to kill the board else will return 0.
  3391. **/
  3392. int
  3393. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3394. {
  3395. struct lpfc_sli *psli;
  3396. LPFC_MBOXQ_t *pmb;
  3397. uint32_t status;
  3398. uint32_t ha_copy;
  3399. int retval;
  3400. int i = 0;
  3401. psli = &phba->sli;
  3402. /* Kill HBA */
  3403. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3404. "0329 Kill HBA Data: x%x x%x\n",
  3405. phba->pport->port_state, psli->sli_flag);
  3406. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3407. if (!pmb)
  3408. return 1;
  3409. /* Disable the error attention */
  3410. spin_lock_irq(&phba->hbalock);
  3411. if (lpfc_readl(phba->HCregaddr, &status)) {
  3412. spin_unlock_irq(&phba->hbalock);
  3413. mempool_free(pmb, phba->mbox_mem_pool);
  3414. return 1;
  3415. }
  3416. status &= ~HC_ERINT_ENA;
  3417. writel(status, phba->HCregaddr);
  3418. readl(phba->HCregaddr); /* flush */
  3419. phba->link_flag |= LS_IGNORE_ERATT;
  3420. spin_unlock_irq(&phba->hbalock);
  3421. lpfc_kill_board(phba, pmb);
  3422. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3423. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3424. if (retval != MBX_SUCCESS) {
  3425. if (retval != MBX_BUSY)
  3426. mempool_free(pmb, phba->mbox_mem_pool);
  3427. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3428. "2752 KILL_BOARD command failed retval %d\n",
  3429. retval);
  3430. spin_lock_irq(&phba->hbalock);
  3431. phba->link_flag &= ~LS_IGNORE_ERATT;
  3432. spin_unlock_irq(&phba->hbalock);
  3433. return 1;
  3434. }
  3435. spin_lock_irq(&phba->hbalock);
  3436. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3437. spin_unlock_irq(&phba->hbalock);
  3438. mempool_free(pmb, phba->mbox_mem_pool);
  3439. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3440. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3441. * 3 seconds we still set HBA_ERROR state because the status of the
  3442. * board is now undefined.
  3443. */
  3444. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3445. return 1;
  3446. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3447. mdelay(100);
  3448. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3449. return 1;
  3450. }
  3451. del_timer_sync(&psli->mbox_tmo);
  3452. if (ha_copy & HA_ERATT) {
  3453. writel(HA_ERATT, phba->HAregaddr);
  3454. phba->pport->stopped = 1;
  3455. }
  3456. spin_lock_irq(&phba->hbalock);
  3457. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3458. psli->mbox_active = NULL;
  3459. phba->link_flag &= ~LS_IGNORE_ERATT;
  3460. spin_unlock_irq(&phba->hbalock);
  3461. lpfc_hba_down_post(phba);
  3462. phba->link_state = LPFC_HBA_ERROR;
  3463. return ha_copy & HA_ERATT ? 0 : 1;
  3464. }
  3465. /**
  3466. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3467. * @phba: Pointer to HBA context object.
  3468. *
  3469. * This function resets the HBA by writing HC_INITFF to the control
  3470. * register. After the HBA resets, this function resets all the iocb ring
  3471. * indices. This function disables PCI layer parity checking during
  3472. * the reset.
  3473. * This function returns 0 always.
  3474. * The caller is not required to hold any locks.
  3475. **/
  3476. int
  3477. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3478. {
  3479. struct lpfc_sli *psli;
  3480. struct lpfc_sli_ring *pring;
  3481. uint16_t cfg_value;
  3482. int i;
  3483. psli = &phba->sli;
  3484. /* Reset HBA */
  3485. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3486. "0325 Reset HBA Data: x%x x%x\n",
  3487. phba->pport->port_state, psli->sli_flag);
  3488. /* perform board reset */
  3489. phba->fc_eventTag = 0;
  3490. phba->link_events = 0;
  3491. phba->pport->fc_myDID = 0;
  3492. phba->pport->fc_prevDID = 0;
  3493. /* Turn off parity checking and serr during the physical reset */
  3494. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3495. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3496. (cfg_value &
  3497. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3498. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3499. /* Now toggle INITFF bit in the Host Control Register */
  3500. writel(HC_INITFF, phba->HCregaddr);
  3501. mdelay(1);
  3502. readl(phba->HCregaddr); /* flush */
  3503. writel(0, phba->HCregaddr);
  3504. readl(phba->HCregaddr); /* flush */
  3505. /* Restore PCI cmd register */
  3506. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3507. /* Initialize relevant SLI info */
  3508. for (i = 0; i < psli->num_rings; i++) {
  3509. pring = &psli->ring[i];
  3510. pring->flag = 0;
  3511. pring->rspidx = 0;
  3512. pring->next_cmdidx = 0;
  3513. pring->local_getidx = 0;
  3514. pring->cmdidx = 0;
  3515. pring->missbufcnt = 0;
  3516. }
  3517. phba->link_state = LPFC_WARM_START;
  3518. return 0;
  3519. }
  3520. /**
  3521. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3522. * @phba: Pointer to HBA context object.
  3523. *
  3524. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3525. * checking during resets the device. The caller is not required to hold
  3526. * any locks.
  3527. *
  3528. * This function returns 0 always.
  3529. **/
  3530. int
  3531. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3532. {
  3533. struct lpfc_sli *psli = &phba->sli;
  3534. uint16_t cfg_value;
  3535. /* Reset HBA */
  3536. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3537. "0295 Reset HBA Data: x%x x%x\n",
  3538. phba->pport->port_state, psli->sli_flag);
  3539. /* perform board reset */
  3540. phba->fc_eventTag = 0;
  3541. phba->link_events = 0;
  3542. phba->pport->fc_myDID = 0;
  3543. phba->pport->fc_prevDID = 0;
  3544. spin_lock_irq(&phba->hbalock);
  3545. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3546. phba->fcf.fcf_flag = 0;
  3547. spin_unlock_irq(&phba->hbalock);
  3548. /* Now physically reset the device */
  3549. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3550. "0389 Performing PCI function reset!\n");
  3551. /* Turn off parity checking and serr during the physical reset */
  3552. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3553. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3554. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3555. /* Perform FCoE PCI function reset */
  3556. lpfc_sli4_queue_destroy(phba);
  3557. lpfc_pci_function_reset(phba);
  3558. /* Restore PCI cmd register */
  3559. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3560. return 0;
  3561. }
  3562. /**
  3563. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3564. * @phba: Pointer to HBA context object.
  3565. *
  3566. * This function is called in the SLI initialization code path to
  3567. * restart the HBA. The caller is not required to hold any lock.
  3568. * This function writes MBX_RESTART mailbox command to the SLIM and
  3569. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3570. * function to free any pending commands. The function enables
  3571. * POST only during the first initialization. The function returns zero.
  3572. * The function does not guarantee completion of MBX_RESTART mailbox
  3573. * command before the return of this function.
  3574. **/
  3575. static int
  3576. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3577. {
  3578. MAILBOX_t *mb;
  3579. struct lpfc_sli *psli;
  3580. volatile uint32_t word0;
  3581. void __iomem *to_slim;
  3582. uint32_t hba_aer_enabled;
  3583. spin_lock_irq(&phba->hbalock);
  3584. /* Take PCIe device Advanced Error Reporting (AER) state */
  3585. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3586. psli = &phba->sli;
  3587. /* Restart HBA */
  3588. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3589. "0337 Restart HBA Data: x%x x%x\n",
  3590. phba->pport->port_state, psli->sli_flag);
  3591. word0 = 0;
  3592. mb = (MAILBOX_t *) &word0;
  3593. mb->mbxCommand = MBX_RESTART;
  3594. mb->mbxHc = 1;
  3595. lpfc_reset_barrier(phba);
  3596. to_slim = phba->MBslimaddr;
  3597. writel(*(uint32_t *) mb, to_slim);
  3598. readl(to_slim); /* flush */
  3599. /* Only skip post after fc_ffinit is completed */
  3600. if (phba->pport->port_state)
  3601. word0 = 1; /* This is really setting up word1 */
  3602. else
  3603. word0 = 0; /* This is really setting up word1 */
  3604. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3605. writel(*(uint32_t *) mb, to_slim);
  3606. readl(to_slim); /* flush */
  3607. lpfc_sli_brdreset(phba);
  3608. phba->pport->stopped = 0;
  3609. phba->link_state = LPFC_INIT_START;
  3610. phba->hba_flag = 0;
  3611. spin_unlock_irq(&phba->hbalock);
  3612. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3613. psli->stats_start = get_seconds();
  3614. /* Give the INITFF and Post time to settle. */
  3615. mdelay(100);
  3616. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3617. if (hba_aer_enabled)
  3618. pci_disable_pcie_error_reporting(phba->pcidev);
  3619. lpfc_hba_down_post(phba);
  3620. return 0;
  3621. }
  3622. /**
  3623. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3624. * @phba: Pointer to HBA context object.
  3625. *
  3626. * This function is called in the SLI initialization code path to restart
  3627. * a SLI4 HBA. The caller is not required to hold any lock.
  3628. * At the end of the function, it calls lpfc_hba_down_post function to
  3629. * free any pending commands.
  3630. **/
  3631. static int
  3632. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3633. {
  3634. struct lpfc_sli *psli = &phba->sli;
  3635. uint32_t hba_aer_enabled;
  3636. /* Restart HBA */
  3637. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3638. "0296 Restart HBA Data: x%x x%x\n",
  3639. phba->pport->port_state, psli->sli_flag);
  3640. /* Take PCIe device Advanced Error Reporting (AER) state */
  3641. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3642. lpfc_sli4_brdreset(phba);
  3643. spin_lock_irq(&phba->hbalock);
  3644. phba->pport->stopped = 0;
  3645. phba->link_state = LPFC_INIT_START;
  3646. phba->hba_flag = 0;
  3647. spin_unlock_irq(&phba->hbalock);
  3648. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3649. psli->stats_start = get_seconds();
  3650. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3651. if (hba_aer_enabled)
  3652. pci_disable_pcie_error_reporting(phba->pcidev);
  3653. lpfc_hba_down_post(phba);
  3654. return 0;
  3655. }
  3656. /**
  3657. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3658. * @phba: Pointer to HBA context object.
  3659. *
  3660. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3661. * API jump table function pointer from the lpfc_hba struct.
  3662. **/
  3663. int
  3664. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3665. {
  3666. return phba->lpfc_sli_brdrestart(phba);
  3667. }
  3668. /**
  3669. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3670. * @phba: Pointer to HBA context object.
  3671. *
  3672. * This function is called after a HBA restart to wait for successful
  3673. * restart of the HBA. Successful restart of the HBA is indicated by
  3674. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3675. * iteration, the function will restart the HBA again. The function returns
  3676. * zero if HBA successfully restarted else returns negative error code.
  3677. **/
  3678. static int
  3679. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3680. {
  3681. uint32_t status, i = 0;
  3682. /* Read the HBA Host Status Register */
  3683. if (lpfc_readl(phba->HSregaddr, &status))
  3684. return -EIO;
  3685. /* Check status register to see what current state is */
  3686. i = 0;
  3687. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3688. /* Check every 10ms for 10 retries, then every 100ms for 90
  3689. * retries, then every 1 sec for 50 retires for a total of
  3690. * ~60 seconds before reset the board again and check every
  3691. * 1 sec for 50 retries. The up to 60 seconds before the
  3692. * board ready is required by the Falcon FIPS zeroization
  3693. * complete, and any reset the board in between shall cause
  3694. * restart of zeroization, further delay the board ready.
  3695. */
  3696. if (i++ >= 200) {
  3697. /* Adapter failed to init, timeout, status reg
  3698. <status> */
  3699. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3700. "0436 Adapter failed to init, "
  3701. "timeout, status reg x%x, "
  3702. "FW Data: A8 x%x AC x%x\n", status,
  3703. readl(phba->MBslimaddr + 0xa8),
  3704. readl(phba->MBslimaddr + 0xac));
  3705. phba->link_state = LPFC_HBA_ERROR;
  3706. return -ETIMEDOUT;
  3707. }
  3708. /* Check to see if any errors occurred during init */
  3709. if (status & HS_FFERM) {
  3710. /* ERROR: During chipset initialization */
  3711. /* Adapter failed to init, chipset, status reg
  3712. <status> */
  3713. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3714. "0437 Adapter failed to init, "
  3715. "chipset, status reg x%x, "
  3716. "FW Data: A8 x%x AC x%x\n", status,
  3717. readl(phba->MBslimaddr + 0xa8),
  3718. readl(phba->MBslimaddr + 0xac));
  3719. phba->link_state = LPFC_HBA_ERROR;
  3720. return -EIO;
  3721. }
  3722. if (i <= 10)
  3723. msleep(10);
  3724. else if (i <= 100)
  3725. msleep(100);
  3726. else
  3727. msleep(1000);
  3728. if (i == 150) {
  3729. /* Do post */
  3730. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3731. lpfc_sli_brdrestart(phba);
  3732. }
  3733. /* Read the HBA Host Status Register */
  3734. if (lpfc_readl(phba->HSregaddr, &status))
  3735. return -EIO;
  3736. }
  3737. /* Check to see if any errors occurred during init */
  3738. if (status & HS_FFERM) {
  3739. /* ERROR: During chipset initialization */
  3740. /* Adapter failed to init, chipset, status reg <status> */
  3741. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3742. "0438 Adapter failed to init, chipset, "
  3743. "status reg x%x, "
  3744. "FW Data: A8 x%x AC x%x\n", status,
  3745. readl(phba->MBslimaddr + 0xa8),
  3746. readl(phba->MBslimaddr + 0xac));
  3747. phba->link_state = LPFC_HBA_ERROR;
  3748. return -EIO;
  3749. }
  3750. /* Clear all interrupt enable conditions */
  3751. writel(0, phba->HCregaddr);
  3752. readl(phba->HCregaddr); /* flush */
  3753. /* setup host attn register */
  3754. writel(0xffffffff, phba->HAregaddr);
  3755. readl(phba->HAregaddr); /* flush */
  3756. return 0;
  3757. }
  3758. /**
  3759. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3760. *
  3761. * This function calculates and returns the number of HBQs required to be
  3762. * configured.
  3763. **/
  3764. int
  3765. lpfc_sli_hbq_count(void)
  3766. {
  3767. return ARRAY_SIZE(lpfc_hbq_defs);
  3768. }
  3769. /**
  3770. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3771. *
  3772. * This function adds the number of hbq entries in every HBQ to get
  3773. * the total number of hbq entries required for the HBA and returns
  3774. * the total count.
  3775. **/
  3776. static int
  3777. lpfc_sli_hbq_entry_count(void)
  3778. {
  3779. int hbq_count = lpfc_sli_hbq_count();
  3780. int count = 0;
  3781. int i;
  3782. for (i = 0; i < hbq_count; ++i)
  3783. count += lpfc_hbq_defs[i]->entry_count;
  3784. return count;
  3785. }
  3786. /**
  3787. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3788. *
  3789. * This function calculates amount of memory required for all hbq entries
  3790. * to be configured and returns the total memory required.
  3791. **/
  3792. int
  3793. lpfc_sli_hbq_size(void)
  3794. {
  3795. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3796. }
  3797. /**
  3798. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3799. * @phba: Pointer to HBA context object.
  3800. *
  3801. * This function is called during the SLI initialization to configure
  3802. * all the HBQs and post buffers to the HBQ. The caller is not
  3803. * required to hold any locks. This function will return zero if successful
  3804. * else it will return negative error code.
  3805. **/
  3806. static int
  3807. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3808. {
  3809. int hbq_count = lpfc_sli_hbq_count();
  3810. LPFC_MBOXQ_t *pmb;
  3811. MAILBOX_t *pmbox;
  3812. uint32_t hbqno;
  3813. uint32_t hbq_entry_index;
  3814. /* Get a Mailbox buffer to setup mailbox
  3815. * commands for HBA initialization
  3816. */
  3817. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3818. if (!pmb)
  3819. return -ENOMEM;
  3820. pmbox = &pmb->u.mb;
  3821. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3822. phba->link_state = LPFC_INIT_MBX_CMDS;
  3823. phba->hbq_in_use = 1;
  3824. hbq_entry_index = 0;
  3825. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3826. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3827. phba->hbqs[hbqno].hbqPutIdx = 0;
  3828. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3829. phba->hbqs[hbqno].entry_count =
  3830. lpfc_hbq_defs[hbqno]->entry_count;
  3831. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3832. hbq_entry_index, pmb);
  3833. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3834. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3835. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3836. mbxStatus <status>, ring <num> */
  3837. lpfc_printf_log(phba, KERN_ERR,
  3838. LOG_SLI | LOG_VPORT,
  3839. "1805 Adapter failed to init. "
  3840. "Data: x%x x%x x%x\n",
  3841. pmbox->mbxCommand,
  3842. pmbox->mbxStatus, hbqno);
  3843. phba->link_state = LPFC_HBA_ERROR;
  3844. mempool_free(pmb, phba->mbox_mem_pool);
  3845. return -ENXIO;
  3846. }
  3847. }
  3848. phba->hbq_count = hbq_count;
  3849. mempool_free(pmb, phba->mbox_mem_pool);
  3850. /* Initially populate or replenish the HBQs */
  3851. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3852. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3853. return 0;
  3854. }
  3855. /**
  3856. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3857. * @phba: Pointer to HBA context object.
  3858. *
  3859. * This function is called during the SLI initialization to configure
  3860. * all the HBQs and post buffers to the HBQ. The caller is not
  3861. * required to hold any locks. This function will return zero if successful
  3862. * else it will return negative error code.
  3863. **/
  3864. static int
  3865. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3866. {
  3867. phba->hbq_in_use = 1;
  3868. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3869. phba->hbq_count = 1;
  3870. /* Initially populate or replenish the HBQs */
  3871. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3872. return 0;
  3873. }
  3874. /**
  3875. * lpfc_sli_config_port - Issue config port mailbox command
  3876. * @phba: Pointer to HBA context object.
  3877. * @sli_mode: sli mode - 2/3
  3878. *
  3879. * This function is called by the sli intialization code path
  3880. * to issue config_port mailbox command. This function restarts the
  3881. * HBA firmware and issues a config_port mailbox command to configure
  3882. * the SLI interface in the sli mode specified by sli_mode
  3883. * variable. The caller is not required to hold any locks.
  3884. * The function returns 0 if successful, else returns negative error
  3885. * code.
  3886. **/
  3887. int
  3888. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3889. {
  3890. LPFC_MBOXQ_t *pmb;
  3891. uint32_t resetcount = 0, rc = 0, done = 0;
  3892. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3893. if (!pmb) {
  3894. phba->link_state = LPFC_HBA_ERROR;
  3895. return -ENOMEM;
  3896. }
  3897. phba->sli_rev = sli_mode;
  3898. while (resetcount < 2 && !done) {
  3899. spin_lock_irq(&phba->hbalock);
  3900. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3901. spin_unlock_irq(&phba->hbalock);
  3902. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3903. lpfc_sli_brdrestart(phba);
  3904. rc = lpfc_sli_chipset_init(phba);
  3905. if (rc)
  3906. break;
  3907. spin_lock_irq(&phba->hbalock);
  3908. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3909. spin_unlock_irq(&phba->hbalock);
  3910. resetcount++;
  3911. /* Call pre CONFIG_PORT mailbox command initialization. A
  3912. * value of 0 means the call was successful. Any other
  3913. * nonzero value is a failure, but if ERESTART is returned,
  3914. * the driver may reset the HBA and try again.
  3915. */
  3916. rc = lpfc_config_port_prep(phba);
  3917. if (rc == -ERESTART) {
  3918. phba->link_state = LPFC_LINK_UNKNOWN;
  3919. continue;
  3920. } else if (rc)
  3921. break;
  3922. phba->link_state = LPFC_INIT_MBX_CMDS;
  3923. lpfc_config_port(phba, pmb);
  3924. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3925. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3926. LPFC_SLI3_HBQ_ENABLED |
  3927. LPFC_SLI3_CRP_ENABLED |
  3928. LPFC_SLI3_BG_ENABLED |
  3929. LPFC_SLI3_DSS_ENABLED);
  3930. if (rc != MBX_SUCCESS) {
  3931. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3932. "0442 Adapter failed to init, mbxCmd x%x "
  3933. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3934. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3935. spin_lock_irq(&phba->hbalock);
  3936. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3937. spin_unlock_irq(&phba->hbalock);
  3938. rc = -ENXIO;
  3939. } else {
  3940. /* Allow asynchronous mailbox command to go through */
  3941. spin_lock_irq(&phba->hbalock);
  3942. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3943. spin_unlock_irq(&phba->hbalock);
  3944. done = 1;
  3945. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3946. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3947. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3948. "3110 Port did not grant ASABT\n");
  3949. }
  3950. }
  3951. if (!done) {
  3952. rc = -EINVAL;
  3953. goto do_prep_failed;
  3954. }
  3955. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3956. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3957. rc = -ENXIO;
  3958. goto do_prep_failed;
  3959. }
  3960. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3961. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3962. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3963. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3964. phba->max_vpi : phba->max_vports;
  3965. } else
  3966. phba->max_vpi = 0;
  3967. phba->fips_level = 0;
  3968. phba->fips_spec_rev = 0;
  3969. if (pmb->u.mb.un.varCfgPort.gdss) {
  3970. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3971. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3972. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3973. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3974. "2850 Security Crypto Active. FIPS x%d "
  3975. "(Spec Rev: x%d)",
  3976. phba->fips_level, phba->fips_spec_rev);
  3977. }
  3978. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3979. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3980. "2856 Config Port Security Crypto "
  3981. "Error: x%x ",
  3982. pmb->u.mb.un.varCfgPort.sec_err);
  3983. }
  3984. if (pmb->u.mb.un.varCfgPort.gerbm)
  3985. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3986. if (pmb->u.mb.un.varCfgPort.gcrp)
  3987. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3988. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3989. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3990. if (phba->cfg_enable_bg) {
  3991. if (pmb->u.mb.un.varCfgPort.gbg)
  3992. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3993. else
  3994. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3995. "0443 Adapter did not grant "
  3996. "BlockGuard\n");
  3997. }
  3998. } else {
  3999. phba->hbq_get = NULL;
  4000. phba->port_gp = phba->mbox->us.s2.port;
  4001. phba->max_vpi = 0;
  4002. }
  4003. do_prep_failed:
  4004. mempool_free(pmb, phba->mbox_mem_pool);
  4005. return rc;
  4006. }
  4007. /**
  4008. * lpfc_sli_hba_setup - SLI intialization function
  4009. * @phba: Pointer to HBA context object.
  4010. *
  4011. * This function is the main SLI intialization function. This function
  4012. * is called by the HBA intialization code, HBA reset code and HBA
  4013. * error attention handler code. Caller is not required to hold any
  4014. * locks. This function issues config_port mailbox command to configure
  4015. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4016. * calls the config_port_post function to issue init_link mailbox
  4017. * command and to start the discovery. The function will return zero
  4018. * if successful, else it will return negative error code.
  4019. **/
  4020. int
  4021. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4022. {
  4023. uint32_t rc;
  4024. int mode = 3, i;
  4025. int longs;
  4026. switch (lpfc_sli_mode) {
  4027. case 2:
  4028. if (phba->cfg_enable_npiv) {
  4029. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4030. "1824 NPIV enabled: Override lpfc_sli_mode "
  4031. "parameter (%d) to auto (0).\n",
  4032. lpfc_sli_mode);
  4033. break;
  4034. }
  4035. mode = 2;
  4036. break;
  4037. case 0:
  4038. case 3:
  4039. break;
  4040. default:
  4041. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4042. "1819 Unrecognized lpfc_sli_mode "
  4043. "parameter: %d.\n", lpfc_sli_mode);
  4044. break;
  4045. }
  4046. rc = lpfc_sli_config_port(phba, mode);
  4047. if (rc && lpfc_sli_mode == 3)
  4048. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4049. "1820 Unable to select SLI-3. "
  4050. "Not supported by adapter.\n");
  4051. if (rc && mode != 2)
  4052. rc = lpfc_sli_config_port(phba, 2);
  4053. if (rc)
  4054. goto lpfc_sli_hba_setup_error;
  4055. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4056. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4057. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4058. if (!rc) {
  4059. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4060. "2709 This device supports "
  4061. "Advanced Error Reporting (AER)\n");
  4062. spin_lock_irq(&phba->hbalock);
  4063. phba->hba_flag |= HBA_AER_ENABLED;
  4064. spin_unlock_irq(&phba->hbalock);
  4065. } else {
  4066. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4067. "2708 This device does not support "
  4068. "Advanced Error Reporting (AER)\n");
  4069. phba->cfg_aer_support = 0;
  4070. }
  4071. }
  4072. if (phba->sli_rev == 3) {
  4073. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4074. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4075. } else {
  4076. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4077. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4078. phba->sli3_options = 0;
  4079. }
  4080. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4081. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4082. phba->sli_rev, phba->max_vpi);
  4083. rc = lpfc_sli_ring_map(phba);
  4084. if (rc)
  4085. goto lpfc_sli_hba_setup_error;
  4086. /* Initialize VPIs. */
  4087. if (phba->sli_rev == LPFC_SLI_REV3) {
  4088. /*
  4089. * The VPI bitmask and physical ID array are allocated
  4090. * and initialized once only - at driver load. A port
  4091. * reset doesn't need to reinitialize this memory.
  4092. */
  4093. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4094. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4095. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4096. GFP_KERNEL);
  4097. if (!phba->vpi_bmask) {
  4098. rc = -ENOMEM;
  4099. goto lpfc_sli_hba_setup_error;
  4100. }
  4101. phba->vpi_ids = kzalloc(
  4102. (phba->max_vpi+1) * sizeof(uint16_t),
  4103. GFP_KERNEL);
  4104. if (!phba->vpi_ids) {
  4105. kfree(phba->vpi_bmask);
  4106. rc = -ENOMEM;
  4107. goto lpfc_sli_hba_setup_error;
  4108. }
  4109. for (i = 0; i < phba->max_vpi; i++)
  4110. phba->vpi_ids[i] = i;
  4111. }
  4112. }
  4113. /* Init HBQs */
  4114. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4115. rc = lpfc_sli_hbq_setup(phba);
  4116. if (rc)
  4117. goto lpfc_sli_hba_setup_error;
  4118. }
  4119. spin_lock_irq(&phba->hbalock);
  4120. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4121. spin_unlock_irq(&phba->hbalock);
  4122. rc = lpfc_config_port_post(phba);
  4123. if (rc)
  4124. goto lpfc_sli_hba_setup_error;
  4125. return rc;
  4126. lpfc_sli_hba_setup_error:
  4127. phba->link_state = LPFC_HBA_ERROR;
  4128. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4129. "0445 Firmware initialization failed\n");
  4130. return rc;
  4131. }
  4132. /**
  4133. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4134. * @phba: Pointer to HBA context object.
  4135. * @mboxq: mailbox pointer.
  4136. * This function issue a dump mailbox command to read config region
  4137. * 23 and parse the records in the region and populate driver
  4138. * data structure.
  4139. **/
  4140. static int
  4141. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4142. {
  4143. LPFC_MBOXQ_t *mboxq;
  4144. struct lpfc_dmabuf *mp;
  4145. struct lpfc_mqe *mqe;
  4146. uint32_t data_length;
  4147. int rc;
  4148. /* Program the default value of vlan_id and fc_map */
  4149. phba->valid_vlan = 0;
  4150. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4151. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4152. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4153. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4154. if (!mboxq)
  4155. return -ENOMEM;
  4156. mqe = &mboxq->u.mqe;
  4157. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4158. rc = -ENOMEM;
  4159. goto out_free_mboxq;
  4160. }
  4161. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4162. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4163. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4164. "(%d):2571 Mailbox cmd x%x Status x%x "
  4165. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4166. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4167. "CQ: x%x x%x x%x x%x\n",
  4168. mboxq->vport ? mboxq->vport->vpi : 0,
  4169. bf_get(lpfc_mqe_command, mqe),
  4170. bf_get(lpfc_mqe_status, mqe),
  4171. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4172. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4173. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4174. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4175. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4176. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4177. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4178. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4179. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4180. mboxq->mcqe.word0,
  4181. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4182. mboxq->mcqe.trailer);
  4183. if (rc) {
  4184. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4185. kfree(mp);
  4186. rc = -EIO;
  4187. goto out_free_mboxq;
  4188. }
  4189. data_length = mqe->un.mb_words[5];
  4190. if (data_length > DMP_RGN23_SIZE) {
  4191. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4192. kfree(mp);
  4193. rc = -EIO;
  4194. goto out_free_mboxq;
  4195. }
  4196. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4197. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4198. kfree(mp);
  4199. rc = 0;
  4200. out_free_mboxq:
  4201. mempool_free(mboxq, phba->mbox_mem_pool);
  4202. return rc;
  4203. }
  4204. /**
  4205. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4206. * @phba: pointer to lpfc hba data structure.
  4207. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4208. * @vpd: pointer to the memory to hold resulting port vpd data.
  4209. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4210. * On output, the number of data bytes in @vpd.
  4211. *
  4212. * This routine executes a READ_REV SLI4 mailbox command. In
  4213. * addition, this routine gets the port vpd data.
  4214. *
  4215. * Return codes
  4216. * 0 - successful
  4217. * -ENOMEM - could not allocated memory.
  4218. **/
  4219. static int
  4220. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4221. uint8_t *vpd, uint32_t *vpd_size)
  4222. {
  4223. int rc = 0;
  4224. uint32_t dma_size;
  4225. struct lpfc_dmabuf *dmabuf;
  4226. struct lpfc_mqe *mqe;
  4227. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4228. if (!dmabuf)
  4229. return -ENOMEM;
  4230. /*
  4231. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4232. * mailbox command.
  4233. */
  4234. dma_size = *vpd_size;
  4235. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4236. dma_size,
  4237. &dmabuf->phys,
  4238. GFP_KERNEL);
  4239. if (!dmabuf->virt) {
  4240. kfree(dmabuf);
  4241. return -ENOMEM;
  4242. }
  4243. memset(dmabuf->virt, 0, dma_size);
  4244. /*
  4245. * The SLI4 implementation of READ_REV conflicts at word1,
  4246. * bits 31:16 and SLI4 adds vpd functionality not present
  4247. * in SLI3. This code corrects the conflicts.
  4248. */
  4249. lpfc_read_rev(phba, mboxq);
  4250. mqe = &mboxq->u.mqe;
  4251. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4252. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4253. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4254. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4255. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4256. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4257. if (rc) {
  4258. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4259. dmabuf->virt, dmabuf->phys);
  4260. kfree(dmabuf);
  4261. return -EIO;
  4262. }
  4263. /*
  4264. * The available vpd length cannot be bigger than the
  4265. * DMA buffer passed to the port. Catch the less than
  4266. * case and update the caller's size.
  4267. */
  4268. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4269. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4270. memcpy(vpd, dmabuf->virt, *vpd_size);
  4271. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4272. dmabuf->virt, dmabuf->phys);
  4273. kfree(dmabuf);
  4274. return 0;
  4275. }
  4276. /**
  4277. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4278. * @phba: pointer to lpfc hba data structure.
  4279. *
  4280. * This routine retrieves SLI4 device physical port name this PCI function
  4281. * is attached to.
  4282. *
  4283. * Return codes
  4284. * 0 - sucessful
  4285. * otherwise - failed to retrieve physical port name
  4286. **/
  4287. static int
  4288. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4289. {
  4290. LPFC_MBOXQ_t *mboxq;
  4291. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4292. struct lpfc_controller_attribute *cntl_attr;
  4293. struct lpfc_mbx_get_port_name *get_port_name;
  4294. void *virtaddr = NULL;
  4295. uint32_t alloclen, reqlen;
  4296. uint32_t shdr_status, shdr_add_status;
  4297. union lpfc_sli4_cfg_shdr *shdr;
  4298. char cport_name = 0;
  4299. int rc;
  4300. /* We assume nothing at this point */
  4301. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4302. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4303. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4304. if (!mboxq)
  4305. return -ENOMEM;
  4306. /* obtain link type and link number via READ_CONFIG */
  4307. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4308. lpfc_sli4_read_config(phba);
  4309. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4310. goto retrieve_ppname;
  4311. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4312. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4313. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4314. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4315. LPFC_SLI4_MBX_NEMBED);
  4316. if (alloclen < reqlen) {
  4317. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4318. "3084 Allocated DMA memory size (%d) is "
  4319. "less than the requested DMA memory size "
  4320. "(%d)\n", alloclen, reqlen);
  4321. rc = -ENOMEM;
  4322. goto out_free_mboxq;
  4323. }
  4324. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4325. virtaddr = mboxq->sge_array->addr[0];
  4326. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4327. shdr = &mbx_cntl_attr->cfg_shdr;
  4328. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4329. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4330. if (shdr_status || shdr_add_status || rc) {
  4331. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4332. "3085 Mailbox x%x (x%x/x%x) failed, "
  4333. "rc:x%x, status:x%x, add_status:x%x\n",
  4334. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4335. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4336. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4337. rc, shdr_status, shdr_add_status);
  4338. rc = -ENXIO;
  4339. goto out_free_mboxq;
  4340. }
  4341. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4342. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4343. phba->sli4_hba.lnk_info.lnk_tp =
  4344. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4345. phba->sli4_hba.lnk_info.lnk_no =
  4346. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4347. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4348. "3086 lnk_type:%d, lnk_numb:%d\n",
  4349. phba->sli4_hba.lnk_info.lnk_tp,
  4350. phba->sli4_hba.lnk_info.lnk_no);
  4351. retrieve_ppname:
  4352. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4353. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4354. sizeof(struct lpfc_mbx_get_port_name) -
  4355. sizeof(struct lpfc_sli4_cfg_mhdr),
  4356. LPFC_SLI4_MBX_EMBED);
  4357. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4358. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4359. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4360. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4361. phba->sli4_hba.lnk_info.lnk_tp);
  4362. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4363. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4364. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4365. if (shdr_status || shdr_add_status || rc) {
  4366. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4367. "3087 Mailbox x%x (x%x/x%x) failed: "
  4368. "rc:x%x, status:x%x, add_status:x%x\n",
  4369. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4370. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4371. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4372. rc, shdr_status, shdr_add_status);
  4373. rc = -ENXIO;
  4374. goto out_free_mboxq;
  4375. }
  4376. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4377. case LPFC_LINK_NUMBER_0:
  4378. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4379. &get_port_name->u.response);
  4380. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4381. break;
  4382. case LPFC_LINK_NUMBER_1:
  4383. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4384. &get_port_name->u.response);
  4385. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4386. break;
  4387. case LPFC_LINK_NUMBER_2:
  4388. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4389. &get_port_name->u.response);
  4390. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4391. break;
  4392. case LPFC_LINK_NUMBER_3:
  4393. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4394. &get_port_name->u.response);
  4395. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4396. break;
  4397. default:
  4398. break;
  4399. }
  4400. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4401. phba->Port[0] = cport_name;
  4402. phba->Port[1] = '\0';
  4403. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4404. "3091 SLI get port name: %s\n", phba->Port);
  4405. }
  4406. out_free_mboxq:
  4407. if (rc != MBX_TIMEOUT) {
  4408. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4409. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4410. else
  4411. mempool_free(mboxq, phba->mbox_mem_pool);
  4412. }
  4413. return rc;
  4414. }
  4415. /**
  4416. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4417. * @phba: pointer to lpfc hba data structure.
  4418. *
  4419. * This routine is called to explicitly arm the SLI4 device's completion and
  4420. * event queues
  4421. **/
  4422. static void
  4423. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4424. {
  4425. uint8_t fcp_eqidx;
  4426. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4427. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4428. fcp_eqidx = 0;
  4429. if (phba->sli4_hba.fcp_cq) {
  4430. do
  4431. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4432. LPFC_QUEUE_REARM);
  4433. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4434. }
  4435. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4436. if (phba->sli4_hba.fp_eq) {
  4437. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4438. fcp_eqidx++)
  4439. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4440. LPFC_QUEUE_REARM);
  4441. }
  4442. }
  4443. /**
  4444. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4445. * @phba: Pointer to HBA context object.
  4446. * @type: The resource extent type.
  4447. * @extnt_count: buffer to hold port available extent count.
  4448. * @extnt_size: buffer to hold element count per extent.
  4449. *
  4450. * This function calls the port and retrievs the number of available
  4451. * extents and their size for a particular extent type.
  4452. *
  4453. * Returns: 0 if successful. Nonzero otherwise.
  4454. **/
  4455. int
  4456. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4457. uint16_t *extnt_count, uint16_t *extnt_size)
  4458. {
  4459. int rc = 0;
  4460. uint32_t length;
  4461. uint32_t mbox_tmo;
  4462. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4463. LPFC_MBOXQ_t *mbox;
  4464. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4465. if (!mbox)
  4466. return -ENOMEM;
  4467. /* Find out how many extents are available for this resource type */
  4468. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4469. sizeof(struct lpfc_sli4_cfg_mhdr));
  4470. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4471. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4472. length, LPFC_SLI4_MBX_EMBED);
  4473. /* Send an extents count of 0 - the GET doesn't use it. */
  4474. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4475. LPFC_SLI4_MBX_EMBED);
  4476. if (unlikely(rc)) {
  4477. rc = -EIO;
  4478. goto err_exit;
  4479. }
  4480. if (!phba->sli4_hba.intr_enable)
  4481. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4482. else {
  4483. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4484. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4485. }
  4486. if (unlikely(rc)) {
  4487. rc = -EIO;
  4488. goto err_exit;
  4489. }
  4490. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4491. if (bf_get(lpfc_mbox_hdr_status,
  4492. &rsrc_info->header.cfg_shdr.response)) {
  4493. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4494. "2930 Failed to get resource extents "
  4495. "Status 0x%x Add'l Status 0x%x\n",
  4496. bf_get(lpfc_mbox_hdr_status,
  4497. &rsrc_info->header.cfg_shdr.response),
  4498. bf_get(lpfc_mbox_hdr_add_status,
  4499. &rsrc_info->header.cfg_shdr.response));
  4500. rc = -EIO;
  4501. goto err_exit;
  4502. }
  4503. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4504. &rsrc_info->u.rsp);
  4505. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4506. &rsrc_info->u.rsp);
  4507. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4508. "3162 Retrieved extents type-%d from port: count:%d, "
  4509. "size:%d\n", type, *extnt_count, *extnt_size);
  4510. err_exit:
  4511. mempool_free(mbox, phba->mbox_mem_pool);
  4512. return rc;
  4513. }
  4514. /**
  4515. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4516. * @phba: Pointer to HBA context object.
  4517. * @type: The extent type to check.
  4518. *
  4519. * This function reads the current available extents from the port and checks
  4520. * if the extent count or extent size has changed since the last access.
  4521. * Callers use this routine post port reset to understand if there is a
  4522. * extent reprovisioning requirement.
  4523. *
  4524. * Returns:
  4525. * -Error: error indicates problem.
  4526. * 1: Extent count or size has changed.
  4527. * 0: No changes.
  4528. **/
  4529. static int
  4530. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4531. {
  4532. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4533. uint16_t size_diff, rsrc_ext_size;
  4534. int rc = 0;
  4535. struct lpfc_rsrc_blks *rsrc_entry;
  4536. struct list_head *rsrc_blk_list = NULL;
  4537. size_diff = 0;
  4538. curr_ext_cnt = 0;
  4539. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4540. &rsrc_ext_cnt,
  4541. &rsrc_ext_size);
  4542. if (unlikely(rc))
  4543. return -EIO;
  4544. switch (type) {
  4545. case LPFC_RSC_TYPE_FCOE_RPI:
  4546. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4547. break;
  4548. case LPFC_RSC_TYPE_FCOE_VPI:
  4549. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4550. break;
  4551. case LPFC_RSC_TYPE_FCOE_XRI:
  4552. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4553. break;
  4554. case LPFC_RSC_TYPE_FCOE_VFI:
  4555. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4556. break;
  4557. default:
  4558. break;
  4559. }
  4560. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4561. curr_ext_cnt++;
  4562. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4563. size_diff++;
  4564. }
  4565. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4566. rc = 1;
  4567. return rc;
  4568. }
  4569. /**
  4570. * lpfc_sli4_cfg_post_extnts -
  4571. * @phba: Pointer to HBA context object.
  4572. * @extnt_cnt - number of available extents.
  4573. * @type - the extent type (rpi, xri, vfi, vpi).
  4574. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4575. * @mbox - pointer to the caller's allocated mailbox structure.
  4576. *
  4577. * This function executes the extents allocation request. It also
  4578. * takes care of the amount of memory needed to allocate or get the
  4579. * allocated extents. It is the caller's responsibility to evaluate
  4580. * the response.
  4581. *
  4582. * Returns:
  4583. * -Error: Error value describes the condition found.
  4584. * 0: if successful
  4585. **/
  4586. static int
  4587. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4588. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4589. {
  4590. int rc = 0;
  4591. uint32_t req_len;
  4592. uint32_t emb_len;
  4593. uint32_t alloc_len, mbox_tmo;
  4594. /* Calculate the total requested length of the dma memory */
  4595. req_len = extnt_cnt * sizeof(uint16_t);
  4596. /*
  4597. * Calculate the size of an embedded mailbox. The uint32_t
  4598. * accounts for extents-specific word.
  4599. */
  4600. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4601. sizeof(uint32_t);
  4602. /*
  4603. * Presume the allocation and response will fit into an embedded
  4604. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4605. */
  4606. *emb = LPFC_SLI4_MBX_EMBED;
  4607. if (req_len > emb_len) {
  4608. req_len = extnt_cnt * sizeof(uint16_t) +
  4609. sizeof(union lpfc_sli4_cfg_shdr) +
  4610. sizeof(uint32_t);
  4611. *emb = LPFC_SLI4_MBX_NEMBED;
  4612. }
  4613. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4614. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4615. req_len, *emb);
  4616. if (alloc_len < req_len) {
  4617. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4618. "2982 Allocated DMA memory size (x%x) is "
  4619. "less than the requested DMA memory "
  4620. "size (x%x)\n", alloc_len, req_len);
  4621. return -ENOMEM;
  4622. }
  4623. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4624. if (unlikely(rc))
  4625. return -EIO;
  4626. if (!phba->sli4_hba.intr_enable)
  4627. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4628. else {
  4629. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4630. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4631. }
  4632. if (unlikely(rc))
  4633. rc = -EIO;
  4634. return rc;
  4635. }
  4636. /**
  4637. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4638. * @phba: Pointer to HBA context object.
  4639. * @type: The resource extent type to allocate.
  4640. *
  4641. * This function allocates the number of elements for the specified
  4642. * resource type.
  4643. **/
  4644. static int
  4645. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4646. {
  4647. bool emb = false;
  4648. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4649. uint16_t rsrc_id, rsrc_start, j, k;
  4650. uint16_t *ids;
  4651. int i, rc;
  4652. unsigned long longs;
  4653. unsigned long *bmask;
  4654. struct lpfc_rsrc_blks *rsrc_blks;
  4655. LPFC_MBOXQ_t *mbox;
  4656. uint32_t length;
  4657. struct lpfc_id_range *id_array = NULL;
  4658. void *virtaddr = NULL;
  4659. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4660. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4661. struct list_head *ext_blk_list;
  4662. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4663. &rsrc_cnt,
  4664. &rsrc_size);
  4665. if (unlikely(rc))
  4666. return -EIO;
  4667. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4668. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4669. "3009 No available Resource Extents "
  4670. "for resource type 0x%x: Count: 0x%x, "
  4671. "Size 0x%x\n", type, rsrc_cnt,
  4672. rsrc_size);
  4673. return -ENOMEM;
  4674. }
  4675. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4676. "2903 Post resource extents type-0x%x: "
  4677. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4678. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4679. if (!mbox)
  4680. return -ENOMEM;
  4681. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4682. if (unlikely(rc)) {
  4683. rc = -EIO;
  4684. goto err_exit;
  4685. }
  4686. /*
  4687. * Figure out where the response is located. Then get local pointers
  4688. * to the response data. The port does not guarantee to respond to
  4689. * all extents counts request so update the local variable with the
  4690. * allocated count from the port.
  4691. */
  4692. if (emb == LPFC_SLI4_MBX_EMBED) {
  4693. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4694. id_array = &rsrc_ext->u.rsp.id[0];
  4695. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4696. } else {
  4697. virtaddr = mbox->sge_array->addr[0];
  4698. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4699. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4700. id_array = &n_rsrc->id;
  4701. }
  4702. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4703. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4704. /*
  4705. * Based on the resource size and count, correct the base and max
  4706. * resource values.
  4707. */
  4708. length = sizeof(struct lpfc_rsrc_blks);
  4709. switch (type) {
  4710. case LPFC_RSC_TYPE_FCOE_RPI:
  4711. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4712. sizeof(unsigned long),
  4713. GFP_KERNEL);
  4714. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4715. rc = -ENOMEM;
  4716. goto err_exit;
  4717. }
  4718. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4719. sizeof(uint16_t),
  4720. GFP_KERNEL);
  4721. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4722. kfree(phba->sli4_hba.rpi_bmask);
  4723. rc = -ENOMEM;
  4724. goto err_exit;
  4725. }
  4726. /*
  4727. * The next_rpi was initialized with the maximum available
  4728. * count but the port may allocate a smaller number. Catch
  4729. * that case and update the next_rpi.
  4730. */
  4731. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4732. /* Initialize local ptrs for common extent processing later. */
  4733. bmask = phba->sli4_hba.rpi_bmask;
  4734. ids = phba->sli4_hba.rpi_ids;
  4735. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4736. break;
  4737. case LPFC_RSC_TYPE_FCOE_VPI:
  4738. phba->vpi_bmask = kzalloc(longs *
  4739. sizeof(unsigned long),
  4740. GFP_KERNEL);
  4741. if (unlikely(!phba->vpi_bmask)) {
  4742. rc = -ENOMEM;
  4743. goto err_exit;
  4744. }
  4745. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4746. sizeof(uint16_t),
  4747. GFP_KERNEL);
  4748. if (unlikely(!phba->vpi_ids)) {
  4749. kfree(phba->vpi_bmask);
  4750. rc = -ENOMEM;
  4751. goto err_exit;
  4752. }
  4753. /* Initialize local ptrs for common extent processing later. */
  4754. bmask = phba->vpi_bmask;
  4755. ids = phba->vpi_ids;
  4756. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4757. break;
  4758. case LPFC_RSC_TYPE_FCOE_XRI:
  4759. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4760. sizeof(unsigned long),
  4761. GFP_KERNEL);
  4762. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4763. rc = -ENOMEM;
  4764. goto err_exit;
  4765. }
  4766. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4767. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4768. sizeof(uint16_t),
  4769. GFP_KERNEL);
  4770. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4771. kfree(phba->sli4_hba.xri_bmask);
  4772. rc = -ENOMEM;
  4773. goto err_exit;
  4774. }
  4775. /* Initialize local ptrs for common extent processing later. */
  4776. bmask = phba->sli4_hba.xri_bmask;
  4777. ids = phba->sli4_hba.xri_ids;
  4778. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4779. break;
  4780. case LPFC_RSC_TYPE_FCOE_VFI:
  4781. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4782. sizeof(unsigned long),
  4783. GFP_KERNEL);
  4784. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4785. rc = -ENOMEM;
  4786. goto err_exit;
  4787. }
  4788. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4789. sizeof(uint16_t),
  4790. GFP_KERNEL);
  4791. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4792. kfree(phba->sli4_hba.vfi_bmask);
  4793. rc = -ENOMEM;
  4794. goto err_exit;
  4795. }
  4796. /* Initialize local ptrs for common extent processing later. */
  4797. bmask = phba->sli4_hba.vfi_bmask;
  4798. ids = phba->sli4_hba.vfi_ids;
  4799. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4800. break;
  4801. default:
  4802. /* Unsupported Opcode. Fail call. */
  4803. id_array = NULL;
  4804. bmask = NULL;
  4805. ids = NULL;
  4806. ext_blk_list = NULL;
  4807. goto err_exit;
  4808. }
  4809. /*
  4810. * Complete initializing the extent configuration with the
  4811. * allocated ids assigned to this function. The bitmask serves
  4812. * as an index into the array and manages the available ids. The
  4813. * array just stores the ids communicated to the port via the wqes.
  4814. */
  4815. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4816. if ((i % 2) == 0)
  4817. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4818. &id_array[k]);
  4819. else
  4820. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4821. &id_array[k]);
  4822. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4823. if (unlikely(!rsrc_blks)) {
  4824. rc = -ENOMEM;
  4825. kfree(bmask);
  4826. kfree(ids);
  4827. goto err_exit;
  4828. }
  4829. rsrc_blks->rsrc_start = rsrc_id;
  4830. rsrc_blks->rsrc_size = rsrc_size;
  4831. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4832. rsrc_start = rsrc_id;
  4833. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4834. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4835. lpfc_sli4_get_els_iocb_cnt(phba);
  4836. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4837. ids[j] = rsrc_id;
  4838. rsrc_id++;
  4839. j++;
  4840. }
  4841. /* Entire word processed. Get next word.*/
  4842. if ((i % 2) == 1)
  4843. k++;
  4844. }
  4845. err_exit:
  4846. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4847. return rc;
  4848. }
  4849. /**
  4850. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4851. * @phba: Pointer to HBA context object.
  4852. * @type: the extent's type.
  4853. *
  4854. * This function deallocates all extents of a particular resource type.
  4855. * SLI4 does not allow for deallocating a particular extent range. It
  4856. * is the caller's responsibility to release all kernel memory resources.
  4857. **/
  4858. static int
  4859. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4860. {
  4861. int rc;
  4862. uint32_t length, mbox_tmo = 0;
  4863. LPFC_MBOXQ_t *mbox;
  4864. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4865. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4866. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4867. if (!mbox)
  4868. return -ENOMEM;
  4869. /*
  4870. * This function sends an embedded mailbox because it only sends the
  4871. * the resource type. All extents of this type are released by the
  4872. * port.
  4873. */
  4874. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4875. sizeof(struct lpfc_sli4_cfg_mhdr));
  4876. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4877. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4878. length, LPFC_SLI4_MBX_EMBED);
  4879. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4880. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4881. LPFC_SLI4_MBX_EMBED);
  4882. if (unlikely(rc)) {
  4883. rc = -EIO;
  4884. goto out_free_mbox;
  4885. }
  4886. if (!phba->sli4_hba.intr_enable)
  4887. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4888. else {
  4889. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4890. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4891. }
  4892. if (unlikely(rc)) {
  4893. rc = -EIO;
  4894. goto out_free_mbox;
  4895. }
  4896. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4897. if (bf_get(lpfc_mbox_hdr_status,
  4898. &dealloc_rsrc->header.cfg_shdr.response)) {
  4899. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4900. "2919 Failed to release resource extents "
  4901. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4902. "Resource memory not released.\n",
  4903. type,
  4904. bf_get(lpfc_mbox_hdr_status,
  4905. &dealloc_rsrc->header.cfg_shdr.response),
  4906. bf_get(lpfc_mbox_hdr_add_status,
  4907. &dealloc_rsrc->header.cfg_shdr.response));
  4908. rc = -EIO;
  4909. goto out_free_mbox;
  4910. }
  4911. /* Release kernel memory resources for the specific type. */
  4912. switch (type) {
  4913. case LPFC_RSC_TYPE_FCOE_VPI:
  4914. kfree(phba->vpi_bmask);
  4915. kfree(phba->vpi_ids);
  4916. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4917. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4918. &phba->lpfc_vpi_blk_list, list) {
  4919. list_del_init(&rsrc_blk->list);
  4920. kfree(rsrc_blk);
  4921. }
  4922. break;
  4923. case LPFC_RSC_TYPE_FCOE_XRI:
  4924. kfree(phba->sli4_hba.xri_bmask);
  4925. kfree(phba->sli4_hba.xri_ids);
  4926. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4927. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4928. list_del_init(&rsrc_blk->list);
  4929. kfree(rsrc_blk);
  4930. }
  4931. break;
  4932. case LPFC_RSC_TYPE_FCOE_VFI:
  4933. kfree(phba->sli4_hba.vfi_bmask);
  4934. kfree(phba->sli4_hba.vfi_ids);
  4935. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4936. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4937. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4938. list_del_init(&rsrc_blk->list);
  4939. kfree(rsrc_blk);
  4940. }
  4941. break;
  4942. case LPFC_RSC_TYPE_FCOE_RPI:
  4943. /* RPI bitmask and physical id array are cleaned up earlier. */
  4944. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4945. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4946. list_del_init(&rsrc_blk->list);
  4947. kfree(rsrc_blk);
  4948. }
  4949. break;
  4950. default:
  4951. break;
  4952. }
  4953. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4954. out_free_mbox:
  4955. mempool_free(mbox, phba->mbox_mem_pool);
  4956. return rc;
  4957. }
  4958. /**
  4959. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4960. * @phba: Pointer to HBA context object.
  4961. *
  4962. * This function allocates all SLI4 resource identifiers.
  4963. **/
  4964. int
  4965. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4966. {
  4967. int i, rc, error = 0;
  4968. uint16_t count, base;
  4969. unsigned long longs;
  4970. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4971. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4972. if (phba->sli4_hba.extents_in_use) {
  4973. /*
  4974. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4975. * resource extent count must be read and allocated before
  4976. * provisioning the resource id arrays.
  4977. */
  4978. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4979. LPFC_IDX_RSRC_RDY) {
  4980. /*
  4981. * Extent-based resources are set - the driver could
  4982. * be in a port reset. Figure out if any corrective
  4983. * actions need to be taken.
  4984. */
  4985. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4986. LPFC_RSC_TYPE_FCOE_VFI);
  4987. if (rc != 0)
  4988. error++;
  4989. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4990. LPFC_RSC_TYPE_FCOE_VPI);
  4991. if (rc != 0)
  4992. error++;
  4993. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4994. LPFC_RSC_TYPE_FCOE_XRI);
  4995. if (rc != 0)
  4996. error++;
  4997. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4998. LPFC_RSC_TYPE_FCOE_RPI);
  4999. if (rc != 0)
  5000. error++;
  5001. /*
  5002. * It's possible that the number of resources
  5003. * provided to this port instance changed between
  5004. * resets. Detect this condition and reallocate
  5005. * resources. Otherwise, there is no action.
  5006. */
  5007. if (error) {
  5008. lpfc_printf_log(phba, KERN_INFO,
  5009. LOG_MBOX | LOG_INIT,
  5010. "2931 Detected extent resource "
  5011. "change. Reallocating all "
  5012. "extents.\n");
  5013. rc = lpfc_sli4_dealloc_extent(phba,
  5014. LPFC_RSC_TYPE_FCOE_VFI);
  5015. rc = lpfc_sli4_dealloc_extent(phba,
  5016. LPFC_RSC_TYPE_FCOE_VPI);
  5017. rc = lpfc_sli4_dealloc_extent(phba,
  5018. LPFC_RSC_TYPE_FCOE_XRI);
  5019. rc = lpfc_sli4_dealloc_extent(phba,
  5020. LPFC_RSC_TYPE_FCOE_RPI);
  5021. } else
  5022. return 0;
  5023. }
  5024. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5025. if (unlikely(rc))
  5026. goto err_exit;
  5027. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5028. if (unlikely(rc))
  5029. goto err_exit;
  5030. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5031. if (unlikely(rc))
  5032. goto err_exit;
  5033. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5034. if (unlikely(rc))
  5035. goto err_exit;
  5036. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5037. LPFC_IDX_RSRC_RDY);
  5038. return rc;
  5039. } else {
  5040. /*
  5041. * The port does not support resource extents. The XRI, VPI,
  5042. * VFI, RPI resource ids were determined from READ_CONFIG.
  5043. * Just allocate the bitmasks and provision the resource id
  5044. * arrays. If a port reset is active, the resources don't
  5045. * need any action - just exit.
  5046. */
  5047. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5048. LPFC_IDX_RSRC_RDY) {
  5049. lpfc_sli4_dealloc_resource_identifiers(phba);
  5050. lpfc_sli4_remove_rpis(phba);
  5051. }
  5052. /* RPIs. */
  5053. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5054. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5055. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5056. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5057. sizeof(unsigned long),
  5058. GFP_KERNEL);
  5059. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5060. rc = -ENOMEM;
  5061. goto err_exit;
  5062. }
  5063. phba->sli4_hba.rpi_ids = kzalloc(count *
  5064. sizeof(uint16_t),
  5065. GFP_KERNEL);
  5066. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5067. rc = -ENOMEM;
  5068. goto free_rpi_bmask;
  5069. }
  5070. for (i = 0; i < count; i++)
  5071. phba->sli4_hba.rpi_ids[i] = base + i;
  5072. /* VPIs. */
  5073. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5074. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5075. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5076. phba->vpi_bmask = kzalloc(longs *
  5077. sizeof(unsigned long),
  5078. GFP_KERNEL);
  5079. if (unlikely(!phba->vpi_bmask)) {
  5080. rc = -ENOMEM;
  5081. goto free_rpi_ids;
  5082. }
  5083. phba->vpi_ids = kzalloc(count *
  5084. sizeof(uint16_t),
  5085. GFP_KERNEL);
  5086. if (unlikely(!phba->vpi_ids)) {
  5087. rc = -ENOMEM;
  5088. goto free_vpi_bmask;
  5089. }
  5090. for (i = 0; i < count; i++)
  5091. phba->vpi_ids[i] = base + i;
  5092. /* XRIs. */
  5093. count = phba->sli4_hba.max_cfg_param.max_xri;
  5094. base = phba->sli4_hba.max_cfg_param.xri_base;
  5095. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5096. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5097. sizeof(unsigned long),
  5098. GFP_KERNEL);
  5099. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5100. rc = -ENOMEM;
  5101. goto free_vpi_ids;
  5102. }
  5103. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5104. phba->sli4_hba.xri_ids = kzalloc(count *
  5105. sizeof(uint16_t),
  5106. GFP_KERNEL);
  5107. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5108. rc = -ENOMEM;
  5109. goto free_xri_bmask;
  5110. }
  5111. for (i = 0; i < count; i++)
  5112. phba->sli4_hba.xri_ids[i] = base + i;
  5113. /* VFIs. */
  5114. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5115. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5116. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5117. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5118. sizeof(unsigned long),
  5119. GFP_KERNEL);
  5120. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5121. rc = -ENOMEM;
  5122. goto free_xri_ids;
  5123. }
  5124. phba->sli4_hba.vfi_ids = kzalloc(count *
  5125. sizeof(uint16_t),
  5126. GFP_KERNEL);
  5127. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5128. rc = -ENOMEM;
  5129. goto free_vfi_bmask;
  5130. }
  5131. for (i = 0; i < count; i++)
  5132. phba->sli4_hba.vfi_ids[i] = base + i;
  5133. /*
  5134. * Mark all resources ready. An HBA reset doesn't need
  5135. * to reset the initialization.
  5136. */
  5137. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5138. LPFC_IDX_RSRC_RDY);
  5139. return 0;
  5140. }
  5141. free_vfi_bmask:
  5142. kfree(phba->sli4_hba.vfi_bmask);
  5143. free_xri_ids:
  5144. kfree(phba->sli4_hba.xri_ids);
  5145. free_xri_bmask:
  5146. kfree(phba->sli4_hba.xri_bmask);
  5147. free_vpi_ids:
  5148. kfree(phba->vpi_ids);
  5149. free_vpi_bmask:
  5150. kfree(phba->vpi_bmask);
  5151. free_rpi_ids:
  5152. kfree(phba->sli4_hba.rpi_ids);
  5153. free_rpi_bmask:
  5154. kfree(phba->sli4_hba.rpi_bmask);
  5155. err_exit:
  5156. return rc;
  5157. }
  5158. /**
  5159. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5160. * @phba: Pointer to HBA context object.
  5161. *
  5162. * This function allocates the number of elements for the specified
  5163. * resource type.
  5164. **/
  5165. int
  5166. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5167. {
  5168. if (phba->sli4_hba.extents_in_use) {
  5169. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5170. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5171. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5172. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5173. } else {
  5174. kfree(phba->vpi_bmask);
  5175. kfree(phba->vpi_ids);
  5176. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5177. kfree(phba->sli4_hba.xri_bmask);
  5178. kfree(phba->sli4_hba.xri_ids);
  5179. kfree(phba->sli4_hba.vfi_bmask);
  5180. kfree(phba->sli4_hba.vfi_ids);
  5181. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5182. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5183. }
  5184. return 0;
  5185. }
  5186. /**
  5187. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5188. * @phba: Pointer to HBA context object.
  5189. * @type: The resource extent type.
  5190. * @extnt_count: buffer to hold port extent count response
  5191. * @extnt_size: buffer to hold port extent size response.
  5192. *
  5193. * This function calls the port to read the host allocated extents
  5194. * for a particular type.
  5195. **/
  5196. int
  5197. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5198. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5199. {
  5200. bool emb;
  5201. int rc = 0;
  5202. uint16_t curr_blks = 0;
  5203. uint32_t req_len, emb_len;
  5204. uint32_t alloc_len, mbox_tmo;
  5205. struct list_head *blk_list_head;
  5206. struct lpfc_rsrc_blks *rsrc_blk;
  5207. LPFC_MBOXQ_t *mbox;
  5208. void *virtaddr = NULL;
  5209. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5210. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5211. union lpfc_sli4_cfg_shdr *shdr;
  5212. switch (type) {
  5213. case LPFC_RSC_TYPE_FCOE_VPI:
  5214. blk_list_head = &phba->lpfc_vpi_blk_list;
  5215. break;
  5216. case LPFC_RSC_TYPE_FCOE_XRI:
  5217. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5218. break;
  5219. case LPFC_RSC_TYPE_FCOE_VFI:
  5220. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5221. break;
  5222. case LPFC_RSC_TYPE_FCOE_RPI:
  5223. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5224. break;
  5225. default:
  5226. return -EIO;
  5227. }
  5228. /* Count the number of extents currently allocatd for this type. */
  5229. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5230. if (curr_blks == 0) {
  5231. /*
  5232. * The GET_ALLOCATED mailbox does not return the size,
  5233. * just the count. The size should be just the size
  5234. * stored in the current allocated block and all sizes
  5235. * for an extent type are the same so set the return
  5236. * value now.
  5237. */
  5238. *extnt_size = rsrc_blk->rsrc_size;
  5239. }
  5240. curr_blks++;
  5241. }
  5242. /* Calculate the total requested length of the dma memory. */
  5243. req_len = curr_blks * sizeof(uint16_t);
  5244. /*
  5245. * Calculate the size of an embedded mailbox. The uint32_t
  5246. * accounts for extents-specific word.
  5247. */
  5248. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5249. sizeof(uint32_t);
  5250. /*
  5251. * Presume the allocation and response will fit into an embedded
  5252. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5253. */
  5254. emb = LPFC_SLI4_MBX_EMBED;
  5255. req_len = emb_len;
  5256. if (req_len > emb_len) {
  5257. req_len = curr_blks * sizeof(uint16_t) +
  5258. sizeof(union lpfc_sli4_cfg_shdr) +
  5259. sizeof(uint32_t);
  5260. emb = LPFC_SLI4_MBX_NEMBED;
  5261. }
  5262. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5263. if (!mbox)
  5264. return -ENOMEM;
  5265. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5266. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5267. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5268. req_len, emb);
  5269. if (alloc_len < req_len) {
  5270. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5271. "2983 Allocated DMA memory size (x%x) is "
  5272. "less than the requested DMA memory "
  5273. "size (x%x)\n", alloc_len, req_len);
  5274. rc = -ENOMEM;
  5275. goto err_exit;
  5276. }
  5277. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5278. if (unlikely(rc)) {
  5279. rc = -EIO;
  5280. goto err_exit;
  5281. }
  5282. if (!phba->sli4_hba.intr_enable)
  5283. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5284. else {
  5285. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5286. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5287. }
  5288. if (unlikely(rc)) {
  5289. rc = -EIO;
  5290. goto err_exit;
  5291. }
  5292. /*
  5293. * Figure out where the response is located. Then get local pointers
  5294. * to the response data. The port does not guarantee to respond to
  5295. * all extents counts request so update the local variable with the
  5296. * allocated count from the port.
  5297. */
  5298. if (emb == LPFC_SLI4_MBX_EMBED) {
  5299. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5300. shdr = &rsrc_ext->header.cfg_shdr;
  5301. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5302. } else {
  5303. virtaddr = mbox->sge_array->addr[0];
  5304. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5305. shdr = &n_rsrc->cfg_shdr;
  5306. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5307. }
  5308. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5309. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5310. "2984 Failed to read allocated resources "
  5311. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5312. type,
  5313. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5314. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5315. rc = -EIO;
  5316. goto err_exit;
  5317. }
  5318. err_exit:
  5319. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5320. return rc;
  5321. }
  5322. /**
  5323. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5324. * @phba: pointer to lpfc hba data structure.
  5325. *
  5326. * This routine walks the list of els buffers that have been allocated and
  5327. * repost them to the port by using SGL block post. This is needed after a
  5328. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5329. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5330. * SGL block post mailbox commands to post them to the port. For single els
  5331. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5332. * mailbox command for posting.
  5333. *
  5334. * Returns: 0 = success, non-zero failure.
  5335. **/
  5336. static int
  5337. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5338. {
  5339. struct lpfc_sglq *sglq_entry = NULL;
  5340. struct lpfc_sglq *sglq_entry_next = NULL;
  5341. struct lpfc_sglq *sglq_entry_first = NULL;
  5342. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5343. int last_xritag = NO_XRI;
  5344. LIST_HEAD(prep_sgl_list);
  5345. LIST_HEAD(blck_sgl_list);
  5346. LIST_HEAD(allc_sgl_list);
  5347. LIST_HEAD(post_sgl_list);
  5348. LIST_HEAD(free_sgl_list);
  5349. spin_lock(&phba->hbalock);
  5350. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5351. spin_unlock(&phba->hbalock);
  5352. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5353. &allc_sgl_list, list) {
  5354. list_del_init(&sglq_entry->list);
  5355. block_cnt++;
  5356. if ((last_xritag != NO_XRI) &&
  5357. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5358. /* a hole in xri block, form a sgl posting block */
  5359. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5360. post_cnt = block_cnt - 1;
  5361. /* prepare list for next posting block */
  5362. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5363. block_cnt = 1;
  5364. } else {
  5365. /* prepare list for next posting block */
  5366. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5367. /* enough sgls for non-embed sgl mbox command */
  5368. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5369. list_splice_init(&prep_sgl_list,
  5370. &blck_sgl_list);
  5371. post_cnt = block_cnt;
  5372. block_cnt = 0;
  5373. }
  5374. }
  5375. num_posted++;
  5376. /* keep track of last sgl's xritag */
  5377. last_xritag = sglq_entry->sli4_xritag;
  5378. /* end of repost sgl list condition for els buffers */
  5379. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5380. if (post_cnt == 0) {
  5381. list_splice_init(&prep_sgl_list,
  5382. &blck_sgl_list);
  5383. post_cnt = block_cnt;
  5384. } else if (block_cnt == 1) {
  5385. status = lpfc_sli4_post_sgl(phba,
  5386. sglq_entry->phys, 0,
  5387. sglq_entry->sli4_xritag);
  5388. if (!status) {
  5389. /* successful, put sgl to posted list */
  5390. list_add_tail(&sglq_entry->list,
  5391. &post_sgl_list);
  5392. } else {
  5393. /* Failure, put sgl to free list */
  5394. lpfc_printf_log(phba, KERN_WARNING,
  5395. LOG_SLI,
  5396. "3159 Failed to post els "
  5397. "sgl, xritag:x%x\n",
  5398. sglq_entry->sli4_xritag);
  5399. list_add_tail(&sglq_entry->list,
  5400. &free_sgl_list);
  5401. spin_lock_irq(&phba->hbalock);
  5402. phba->sli4_hba.els_xri_cnt--;
  5403. spin_unlock_irq(&phba->hbalock);
  5404. }
  5405. }
  5406. }
  5407. /* continue until a nembed page worth of sgls */
  5408. if (post_cnt == 0)
  5409. continue;
  5410. /* post the els buffer list sgls as a block */
  5411. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5412. post_cnt);
  5413. if (!status) {
  5414. /* success, put sgl list to posted sgl list */
  5415. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5416. } else {
  5417. /* Failure, put sgl list to free sgl list */
  5418. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5419. struct lpfc_sglq,
  5420. list);
  5421. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5422. "3160 Failed to post els sgl-list, "
  5423. "xritag:x%x-x%x\n",
  5424. sglq_entry_first->sli4_xritag,
  5425. (sglq_entry_first->sli4_xritag +
  5426. post_cnt - 1));
  5427. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5428. spin_lock_irq(&phba->hbalock);
  5429. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5430. spin_unlock_irq(&phba->hbalock);
  5431. }
  5432. /* don't reset xirtag due to hole in xri block */
  5433. if (block_cnt == 0)
  5434. last_xritag = NO_XRI;
  5435. /* reset els sgl post count for next round of posting */
  5436. post_cnt = 0;
  5437. }
  5438. /* free the els sgls failed to post */
  5439. lpfc_free_sgl_list(phba, &free_sgl_list);
  5440. /* push els sgls posted to the availble list */
  5441. if (!list_empty(&post_sgl_list)) {
  5442. spin_lock(&phba->hbalock);
  5443. list_splice_init(&post_sgl_list,
  5444. &phba->sli4_hba.lpfc_sgl_list);
  5445. spin_unlock(&phba->hbalock);
  5446. } else {
  5447. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5448. "3161 Failure to post els sgl to port.\n");
  5449. return -EIO;
  5450. }
  5451. return 0;
  5452. }
  5453. /**
  5454. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5455. * @phba: Pointer to HBA context object.
  5456. *
  5457. * This function is the main SLI4 device intialization PCI function. This
  5458. * function is called by the HBA intialization code, HBA reset code and
  5459. * HBA error attention handler code. Caller is not required to hold any
  5460. * locks.
  5461. **/
  5462. int
  5463. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5464. {
  5465. int rc;
  5466. LPFC_MBOXQ_t *mboxq;
  5467. struct lpfc_mqe *mqe;
  5468. uint8_t *vpd;
  5469. uint32_t vpd_size;
  5470. uint32_t ftr_rsp = 0;
  5471. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5472. struct lpfc_vport *vport = phba->pport;
  5473. struct lpfc_dmabuf *mp;
  5474. /* Perform a PCI function reset to start from clean */
  5475. rc = lpfc_pci_function_reset(phba);
  5476. if (unlikely(rc))
  5477. return -ENODEV;
  5478. /* Check the HBA Host Status Register for readyness */
  5479. rc = lpfc_sli4_post_status_check(phba);
  5480. if (unlikely(rc))
  5481. return -ENODEV;
  5482. else {
  5483. spin_lock_irq(&phba->hbalock);
  5484. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5485. spin_unlock_irq(&phba->hbalock);
  5486. }
  5487. /*
  5488. * Allocate a single mailbox container for initializing the
  5489. * port.
  5490. */
  5491. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5492. if (!mboxq)
  5493. return -ENOMEM;
  5494. /* Issue READ_REV to collect vpd and FW information. */
  5495. vpd_size = SLI4_PAGE_SIZE;
  5496. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5497. if (!vpd) {
  5498. rc = -ENOMEM;
  5499. goto out_free_mbox;
  5500. }
  5501. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5502. if (unlikely(rc)) {
  5503. kfree(vpd);
  5504. goto out_free_mbox;
  5505. }
  5506. mqe = &mboxq->u.mqe;
  5507. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5508. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5509. phba->hba_flag |= HBA_FCOE_MODE;
  5510. else
  5511. phba->hba_flag &= ~HBA_FCOE_MODE;
  5512. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5513. LPFC_DCBX_CEE_MODE)
  5514. phba->hba_flag |= HBA_FIP_SUPPORT;
  5515. else
  5516. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5517. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5518. if (phba->sli_rev != LPFC_SLI_REV4) {
  5519. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5520. "0376 READ_REV Error. SLI Level %d "
  5521. "FCoE enabled %d\n",
  5522. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5523. rc = -EIO;
  5524. kfree(vpd);
  5525. goto out_free_mbox;
  5526. }
  5527. /*
  5528. * Continue initialization with default values even if driver failed
  5529. * to read FCoE param config regions, only read parameters if the
  5530. * board is FCoE
  5531. */
  5532. if (phba->hba_flag & HBA_FCOE_MODE &&
  5533. lpfc_sli4_read_fcoe_params(phba))
  5534. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5535. "2570 Failed to read FCoE parameters\n");
  5536. /*
  5537. * Retrieve sli4 device physical port name, failure of doing it
  5538. * is considered as non-fatal.
  5539. */
  5540. rc = lpfc_sli4_retrieve_pport_name(phba);
  5541. if (!rc)
  5542. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5543. "3080 Successful retrieving SLI4 device "
  5544. "physical port name: %s.\n", phba->Port);
  5545. /*
  5546. * Evaluate the read rev and vpd data. Populate the driver
  5547. * state with the results. If this routine fails, the failure
  5548. * is not fatal as the driver will use generic values.
  5549. */
  5550. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5551. if (unlikely(!rc)) {
  5552. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5553. "0377 Error %d parsing vpd. "
  5554. "Using defaults.\n", rc);
  5555. rc = 0;
  5556. }
  5557. kfree(vpd);
  5558. /* Save information as VPD data */
  5559. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5560. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5561. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5562. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5563. &mqe->un.read_rev);
  5564. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5565. &mqe->un.read_rev);
  5566. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5567. &mqe->un.read_rev);
  5568. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5569. &mqe->un.read_rev);
  5570. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5571. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5572. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5573. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5574. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5575. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5576. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5577. "(%d):0380 READ_REV Status x%x "
  5578. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5579. mboxq->vport ? mboxq->vport->vpi : 0,
  5580. bf_get(lpfc_mqe_status, mqe),
  5581. phba->vpd.rev.opFwName,
  5582. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5583. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5584. /*
  5585. * Discover the port's supported feature set and match it against the
  5586. * hosts requests.
  5587. */
  5588. lpfc_request_features(phba, mboxq);
  5589. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5590. if (unlikely(rc)) {
  5591. rc = -EIO;
  5592. goto out_free_mbox;
  5593. }
  5594. /*
  5595. * The port must support FCP initiator mode as this is the
  5596. * only mode running in the host.
  5597. */
  5598. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5599. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5600. "0378 No support for fcpi mode.\n");
  5601. ftr_rsp++;
  5602. }
  5603. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5604. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5605. else
  5606. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5607. /*
  5608. * If the port cannot support the host's requested features
  5609. * then turn off the global config parameters to disable the
  5610. * feature in the driver. This is not a fatal error.
  5611. */
  5612. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5613. if (phba->cfg_enable_bg) {
  5614. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5615. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5616. else
  5617. ftr_rsp++;
  5618. }
  5619. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5620. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5621. ftr_rsp++;
  5622. if (ftr_rsp) {
  5623. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5624. "0379 Feature Mismatch Data: x%08x %08x "
  5625. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5626. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5627. phba->cfg_enable_npiv, phba->max_vpi);
  5628. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5629. phba->cfg_enable_bg = 0;
  5630. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5631. phba->cfg_enable_npiv = 0;
  5632. }
  5633. /* These SLI3 features are assumed in SLI4 */
  5634. spin_lock_irq(&phba->hbalock);
  5635. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5636. spin_unlock_irq(&phba->hbalock);
  5637. /*
  5638. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5639. * calls depends on these resources to complete port setup.
  5640. */
  5641. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5642. if (rc) {
  5643. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5644. "2920 Failed to alloc Resource IDs "
  5645. "rc = x%x\n", rc);
  5646. goto out_free_mbox;
  5647. }
  5648. /* Read the port's service parameters. */
  5649. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5650. if (rc) {
  5651. phba->link_state = LPFC_HBA_ERROR;
  5652. rc = -ENOMEM;
  5653. goto out_free_mbox;
  5654. }
  5655. mboxq->vport = vport;
  5656. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5657. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5658. if (rc == MBX_SUCCESS) {
  5659. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5660. rc = 0;
  5661. }
  5662. /*
  5663. * This memory was allocated by the lpfc_read_sparam routine. Release
  5664. * it to the mbuf pool.
  5665. */
  5666. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5667. kfree(mp);
  5668. mboxq->context1 = NULL;
  5669. if (unlikely(rc)) {
  5670. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5671. "0382 READ_SPARAM command failed "
  5672. "status %d, mbxStatus x%x\n",
  5673. rc, bf_get(lpfc_mqe_status, mqe));
  5674. phba->link_state = LPFC_HBA_ERROR;
  5675. rc = -EIO;
  5676. goto out_free_mbox;
  5677. }
  5678. lpfc_update_vport_wwn(vport);
  5679. /* Update the fc_host data structures with new wwn. */
  5680. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5681. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5682. /* update host els and scsi xri-sgl sizes and mappings */
  5683. rc = lpfc_sli4_xri_sgl_update(phba);
  5684. if (unlikely(rc)) {
  5685. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5686. "1400 Failed to update xri-sgl size and "
  5687. "mapping: %d\n", rc);
  5688. goto out_free_mbox;
  5689. }
  5690. /* register the els sgl pool to the port */
  5691. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5692. if (unlikely(rc)) {
  5693. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5694. "0582 Error %d during els sgl post "
  5695. "operation\n", rc);
  5696. rc = -ENODEV;
  5697. goto out_free_mbox;
  5698. }
  5699. /* register the allocated scsi sgl pool to the port */
  5700. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5701. if (unlikely(rc)) {
  5702. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5703. "0383 Error %d during scsi sgl post "
  5704. "operation\n", rc);
  5705. /* Some Scsi buffers were moved to the abort scsi list */
  5706. /* A pci function reset will repost them */
  5707. rc = -ENODEV;
  5708. goto out_free_mbox;
  5709. }
  5710. /* Post the rpi header region to the device. */
  5711. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5712. if (unlikely(rc)) {
  5713. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5714. "0393 Error %d during rpi post operation\n",
  5715. rc);
  5716. rc = -ENODEV;
  5717. goto out_free_mbox;
  5718. }
  5719. lpfc_sli4_node_prep(phba);
  5720. /* Create all the SLI4 queues */
  5721. rc = lpfc_sli4_queue_create(phba);
  5722. if (rc) {
  5723. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5724. "3089 Failed to allocate queues\n");
  5725. rc = -ENODEV;
  5726. goto out_stop_timers;
  5727. }
  5728. /* Set up all the queues to the device */
  5729. rc = lpfc_sli4_queue_setup(phba);
  5730. if (unlikely(rc)) {
  5731. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5732. "0381 Error %d during queue setup.\n ", rc);
  5733. goto out_destroy_queue;
  5734. }
  5735. /* Arm the CQs and then EQs on device */
  5736. lpfc_sli4_arm_cqeq_intr(phba);
  5737. /* Indicate device interrupt mode */
  5738. phba->sli4_hba.intr_enable = 1;
  5739. /* Allow asynchronous mailbox command to go through */
  5740. spin_lock_irq(&phba->hbalock);
  5741. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5742. spin_unlock_irq(&phba->hbalock);
  5743. /* Post receive buffers to the device */
  5744. lpfc_sli4_rb_setup(phba);
  5745. /* Reset HBA FCF states after HBA reset */
  5746. phba->fcf.fcf_flag = 0;
  5747. phba->fcf.current_rec.flag = 0;
  5748. /* Start the ELS watchdog timer */
  5749. mod_timer(&vport->els_tmofunc,
  5750. jiffies + HZ * (phba->fc_ratov * 2));
  5751. /* Start heart beat timer */
  5752. mod_timer(&phba->hb_tmofunc,
  5753. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5754. phba->hb_outstanding = 0;
  5755. phba->last_completion_time = jiffies;
  5756. /* Start error attention (ERATT) polling timer */
  5757. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5758. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5759. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5760. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5761. if (!rc) {
  5762. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5763. "2829 This device supports "
  5764. "Advanced Error Reporting (AER)\n");
  5765. spin_lock_irq(&phba->hbalock);
  5766. phba->hba_flag |= HBA_AER_ENABLED;
  5767. spin_unlock_irq(&phba->hbalock);
  5768. } else {
  5769. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5770. "2830 This device does not support "
  5771. "Advanced Error Reporting (AER)\n");
  5772. phba->cfg_aer_support = 0;
  5773. }
  5774. rc = 0;
  5775. }
  5776. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5777. /*
  5778. * The FC Port needs to register FCFI (index 0)
  5779. */
  5780. lpfc_reg_fcfi(phba, mboxq);
  5781. mboxq->vport = phba->pport;
  5782. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5783. if (rc != MBX_SUCCESS)
  5784. goto out_unset_queue;
  5785. rc = 0;
  5786. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5787. &mboxq->u.mqe.un.reg_fcfi);
  5788. /* Check if the port is configured to be disabled */
  5789. lpfc_sli_read_link_ste(phba);
  5790. }
  5791. /*
  5792. * The port is ready, set the host's link state to LINK_DOWN
  5793. * in preparation for link interrupts.
  5794. */
  5795. spin_lock_irq(&phba->hbalock);
  5796. phba->link_state = LPFC_LINK_DOWN;
  5797. spin_unlock_irq(&phba->hbalock);
  5798. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5799. (phba->hba_flag & LINK_DISABLED)) {
  5800. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5801. "3103 Adapter Link is disabled.\n");
  5802. lpfc_down_link(phba, mboxq);
  5803. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5804. if (rc != MBX_SUCCESS) {
  5805. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5806. "3104 Adapter failed to issue "
  5807. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5808. goto out_unset_queue;
  5809. }
  5810. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5811. /* don't perform init_link on SLI4 FC port loopback test */
  5812. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5813. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5814. if (rc)
  5815. goto out_unset_queue;
  5816. }
  5817. }
  5818. mempool_free(mboxq, phba->mbox_mem_pool);
  5819. return rc;
  5820. out_unset_queue:
  5821. /* Unset all the queues set up in this routine when error out */
  5822. lpfc_sli4_queue_unset(phba);
  5823. out_destroy_queue:
  5824. lpfc_sli4_queue_destroy(phba);
  5825. out_stop_timers:
  5826. lpfc_stop_hba_timers(phba);
  5827. out_free_mbox:
  5828. mempool_free(mboxq, phba->mbox_mem_pool);
  5829. return rc;
  5830. }
  5831. /**
  5832. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5833. * @ptr: context object - pointer to hba structure.
  5834. *
  5835. * This is the callback function for mailbox timer. The mailbox
  5836. * timer is armed when a new mailbox command is issued and the timer
  5837. * is deleted when the mailbox complete. The function is called by
  5838. * the kernel timer code when a mailbox does not complete within
  5839. * expected time. This function wakes up the worker thread to
  5840. * process the mailbox timeout and returns. All the processing is
  5841. * done by the worker thread function lpfc_mbox_timeout_handler.
  5842. **/
  5843. void
  5844. lpfc_mbox_timeout(unsigned long ptr)
  5845. {
  5846. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5847. unsigned long iflag;
  5848. uint32_t tmo_posted;
  5849. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5850. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5851. if (!tmo_posted)
  5852. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5853. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5854. if (!tmo_posted)
  5855. lpfc_worker_wake_up(phba);
  5856. return;
  5857. }
  5858. /**
  5859. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5860. * @phba: Pointer to HBA context object.
  5861. *
  5862. * This function is called from worker thread when a mailbox command times out.
  5863. * The caller is not required to hold any locks. This function will reset the
  5864. * HBA and recover all the pending commands.
  5865. **/
  5866. void
  5867. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5868. {
  5869. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5870. MAILBOX_t *mb = &pmbox->u.mb;
  5871. struct lpfc_sli *psli = &phba->sli;
  5872. struct lpfc_sli_ring *pring;
  5873. /* Check the pmbox pointer first. There is a race condition
  5874. * between the mbox timeout handler getting executed in the
  5875. * worklist and the mailbox actually completing. When this
  5876. * race condition occurs, the mbox_active will be NULL.
  5877. */
  5878. spin_lock_irq(&phba->hbalock);
  5879. if (pmbox == NULL) {
  5880. lpfc_printf_log(phba, KERN_WARNING,
  5881. LOG_MBOX | LOG_SLI,
  5882. "0353 Active Mailbox cleared - mailbox timeout "
  5883. "exiting\n");
  5884. spin_unlock_irq(&phba->hbalock);
  5885. return;
  5886. }
  5887. /* Mbox cmd <mbxCommand> timeout */
  5888. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5889. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5890. mb->mbxCommand,
  5891. phba->pport->port_state,
  5892. phba->sli.sli_flag,
  5893. phba->sli.mbox_active);
  5894. spin_unlock_irq(&phba->hbalock);
  5895. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5896. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5897. * it to fail all outstanding SCSI IO.
  5898. */
  5899. spin_lock_irq(&phba->pport->work_port_lock);
  5900. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5901. spin_unlock_irq(&phba->pport->work_port_lock);
  5902. spin_lock_irq(&phba->hbalock);
  5903. phba->link_state = LPFC_LINK_UNKNOWN;
  5904. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5905. spin_unlock_irq(&phba->hbalock);
  5906. pring = &psli->ring[psli->fcp_ring];
  5907. lpfc_sli_abort_iocb_ring(phba, pring);
  5908. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5909. "0345 Resetting board due to mailbox timeout\n");
  5910. /* Reset the HBA device */
  5911. lpfc_reset_hba(phba);
  5912. }
  5913. /**
  5914. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5915. * @phba: Pointer to HBA context object.
  5916. * @pmbox: Pointer to mailbox object.
  5917. * @flag: Flag indicating how the mailbox need to be processed.
  5918. *
  5919. * This function is called by discovery code and HBA management code
  5920. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5921. * function gets the hbalock to protect the data structures.
  5922. * The mailbox command can be submitted in polling mode, in which case
  5923. * this function will wait in a polling loop for the completion of the
  5924. * mailbox.
  5925. * If the mailbox is submitted in no_wait mode (not polling) the
  5926. * function will submit the command and returns immediately without waiting
  5927. * for the mailbox completion. The no_wait is supported only when HBA
  5928. * is in SLI2/SLI3 mode - interrupts are enabled.
  5929. * The SLI interface allows only one mailbox pending at a time. If the
  5930. * mailbox is issued in polling mode and there is already a mailbox
  5931. * pending, then the function will return an error. If the mailbox is issued
  5932. * in NO_WAIT mode and there is a mailbox pending already, the function
  5933. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5934. * The sli layer owns the mailbox object until the completion of mailbox
  5935. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5936. * return codes the caller owns the mailbox command after the return of
  5937. * the function.
  5938. **/
  5939. static int
  5940. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5941. uint32_t flag)
  5942. {
  5943. MAILBOX_t *mb;
  5944. struct lpfc_sli *psli = &phba->sli;
  5945. uint32_t status, evtctr;
  5946. uint32_t ha_copy, hc_copy;
  5947. int i;
  5948. unsigned long timeout;
  5949. unsigned long drvr_flag = 0;
  5950. uint32_t word0, ldata;
  5951. void __iomem *to_slim;
  5952. int processing_queue = 0;
  5953. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5954. if (!pmbox) {
  5955. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5956. /* processing mbox queue from intr_handler */
  5957. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5958. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5959. return MBX_SUCCESS;
  5960. }
  5961. processing_queue = 1;
  5962. pmbox = lpfc_mbox_get(phba);
  5963. if (!pmbox) {
  5964. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5965. return MBX_SUCCESS;
  5966. }
  5967. }
  5968. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5969. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5970. if(!pmbox->vport) {
  5971. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5972. lpfc_printf_log(phba, KERN_ERR,
  5973. LOG_MBOX | LOG_VPORT,
  5974. "1806 Mbox x%x failed. No vport\n",
  5975. pmbox->u.mb.mbxCommand);
  5976. dump_stack();
  5977. goto out_not_finished;
  5978. }
  5979. }
  5980. /* If the PCI channel is in offline state, do not post mbox. */
  5981. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5982. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5983. goto out_not_finished;
  5984. }
  5985. /* If HBA has a deferred error attention, fail the iocb. */
  5986. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5987. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5988. goto out_not_finished;
  5989. }
  5990. psli = &phba->sli;
  5991. mb = &pmbox->u.mb;
  5992. status = MBX_SUCCESS;
  5993. if (phba->link_state == LPFC_HBA_ERROR) {
  5994. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5995. /* Mbox command <mbxCommand> cannot issue */
  5996. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5997. "(%d):0311 Mailbox command x%x cannot "
  5998. "issue Data: x%x x%x\n",
  5999. pmbox->vport ? pmbox->vport->vpi : 0,
  6000. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6001. goto out_not_finished;
  6002. }
  6003. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6004. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6005. !(hc_copy & HC_MBINT_ENA)) {
  6006. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6007. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6008. "(%d):2528 Mailbox command x%x cannot "
  6009. "issue Data: x%x x%x\n",
  6010. pmbox->vport ? pmbox->vport->vpi : 0,
  6011. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6012. goto out_not_finished;
  6013. }
  6014. }
  6015. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6016. /* Polling for a mbox command when another one is already active
  6017. * is not allowed in SLI. Also, the driver must have established
  6018. * SLI2 mode to queue and process multiple mbox commands.
  6019. */
  6020. if (flag & MBX_POLL) {
  6021. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6022. /* Mbox command <mbxCommand> cannot issue */
  6023. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6024. "(%d):2529 Mailbox command x%x "
  6025. "cannot issue Data: x%x x%x\n",
  6026. pmbox->vport ? pmbox->vport->vpi : 0,
  6027. pmbox->u.mb.mbxCommand,
  6028. psli->sli_flag, flag);
  6029. goto out_not_finished;
  6030. }
  6031. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6032. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6033. /* Mbox command <mbxCommand> cannot issue */
  6034. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6035. "(%d):2530 Mailbox command x%x "
  6036. "cannot issue Data: x%x x%x\n",
  6037. pmbox->vport ? pmbox->vport->vpi : 0,
  6038. pmbox->u.mb.mbxCommand,
  6039. psli->sli_flag, flag);
  6040. goto out_not_finished;
  6041. }
  6042. /* Another mailbox command is still being processed, queue this
  6043. * command to be processed later.
  6044. */
  6045. lpfc_mbox_put(phba, pmbox);
  6046. /* Mbox cmd issue - BUSY */
  6047. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6048. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6049. "x%x x%x x%x x%x\n",
  6050. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6051. mb->mbxCommand, phba->pport->port_state,
  6052. psli->sli_flag, flag);
  6053. psli->slistat.mbox_busy++;
  6054. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6055. if (pmbox->vport) {
  6056. lpfc_debugfs_disc_trc(pmbox->vport,
  6057. LPFC_DISC_TRC_MBOX_VPORT,
  6058. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6059. (uint32_t)mb->mbxCommand,
  6060. mb->un.varWords[0], mb->un.varWords[1]);
  6061. }
  6062. else {
  6063. lpfc_debugfs_disc_trc(phba->pport,
  6064. LPFC_DISC_TRC_MBOX,
  6065. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6066. (uint32_t)mb->mbxCommand,
  6067. mb->un.varWords[0], mb->un.varWords[1]);
  6068. }
  6069. return MBX_BUSY;
  6070. }
  6071. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6072. /* If we are not polling, we MUST be in SLI2 mode */
  6073. if (flag != MBX_POLL) {
  6074. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6075. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6076. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6077. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6078. /* Mbox command <mbxCommand> cannot issue */
  6079. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6080. "(%d):2531 Mailbox command x%x "
  6081. "cannot issue Data: x%x x%x\n",
  6082. pmbox->vport ? pmbox->vport->vpi : 0,
  6083. pmbox->u.mb.mbxCommand,
  6084. psli->sli_flag, flag);
  6085. goto out_not_finished;
  6086. }
  6087. /* timeout active mbox command */
  6088. mod_timer(&psli->mbox_tmo, (jiffies +
  6089. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6090. }
  6091. /* Mailbox cmd <cmd> issue */
  6092. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6093. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6094. "x%x\n",
  6095. pmbox->vport ? pmbox->vport->vpi : 0,
  6096. mb->mbxCommand, phba->pport->port_state,
  6097. psli->sli_flag, flag);
  6098. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6099. if (pmbox->vport) {
  6100. lpfc_debugfs_disc_trc(pmbox->vport,
  6101. LPFC_DISC_TRC_MBOX_VPORT,
  6102. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6103. (uint32_t)mb->mbxCommand,
  6104. mb->un.varWords[0], mb->un.varWords[1]);
  6105. }
  6106. else {
  6107. lpfc_debugfs_disc_trc(phba->pport,
  6108. LPFC_DISC_TRC_MBOX,
  6109. "MBOX Send: cmd:x%x mb:x%x x%x",
  6110. (uint32_t)mb->mbxCommand,
  6111. mb->un.varWords[0], mb->un.varWords[1]);
  6112. }
  6113. }
  6114. psli->slistat.mbox_cmd++;
  6115. evtctr = psli->slistat.mbox_event;
  6116. /* next set own bit for the adapter and copy over command word */
  6117. mb->mbxOwner = OWN_CHIP;
  6118. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6119. /* Populate mbox extension offset word. */
  6120. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6121. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6122. = (uint8_t *)phba->mbox_ext
  6123. - (uint8_t *)phba->mbox;
  6124. }
  6125. /* Copy the mailbox extension data */
  6126. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6127. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6128. (uint8_t *)phba->mbox_ext,
  6129. pmbox->in_ext_byte_len);
  6130. }
  6131. /* Copy command data to host SLIM area */
  6132. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6133. } else {
  6134. /* Populate mbox extension offset word. */
  6135. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6136. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6137. = MAILBOX_HBA_EXT_OFFSET;
  6138. /* Copy the mailbox extension data */
  6139. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6140. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6141. MAILBOX_HBA_EXT_OFFSET,
  6142. pmbox->context2, pmbox->in_ext_byte_len);
  6143. }
  6144. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6145. /* copy command data into host mbox for cmpl */
  6146. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6147. }
  6148. /* First copy mbox command data to HBA SLIM, skip past first
  6149. word */
  6150. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6151. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6152. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6153. /* Next copy over first word, with mbxOwner set */
  6154. ldata = *((uint32_t *)mb);
  6155. to_slim = phba->MBslimaddr;
  6156. writel(ldata, to_slim);
  6157. readl(to_slim); /* flush */
  6158. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6159. /* switch over to host mailbox */
  6160. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6161. }
  6162. }
  6163. wmb();
  6164. switch (flag) {
  6165. case MBX_NOWAIT:
  6166. /* Set up reference to mailbox command */
  6167. psli->mbox_active = pmbox;
  6168. /* Interrupt board to do it */
  6169. writel(CA_MBATT, phba->CAregaddr);
  6170. readl(phba->CAregaddr); /* flush */
  6171. /* Don't wait for it to finish, just return */
  6172. break;
  6173. case MBX_POLL:
  6174. /* Set up null reference to mailbox command */
  6175. psli->mbox_active = NULL;
  6176. /* Interrupt board to do it */
  6177. writel(CA_MBATT, phba->CAregaddr);
  6178. readl(phba->CAregaddr); /* flush */
  6179. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6180. /* First read mbox status word */
  6181. word0 = *((uint32_t *)phba->mbox);
  6182. word0 = le32_to_cpu(word0);
  6183. } else {
  6184. /* First read mbox status word */
  6185. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6186. spin_unlock_irqrestore(&phba->hbalock,
  6187. drvr_flag);
  6188. goto out_not_finished;
  6189. }
  6190. }
  6191. /* Read the HBA Host Attention Register */
  6192. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6193. spin_unlock_irqrestore(&phba->hbalock,
  6194. drvr_flag);
  6195. goto out_not_finished;
  6196. }
  6197. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6198. 1000) + jiffies;
  6199. i = 0;
  6200. /* Wait for command to complete */
  6201. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6202. (!(ha_copy & HA_MBATT) &&
  6203. (phba->link_state > LPFC_WARM_START))) {
  6204. if (time_after(jiffies, timeout)) {
  6205. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6206. spin_unlock_irqrestore(&phba->hbalock,
  6207. drvr_flag);
  6208. goto out_not_finished;
  6209. }
  6210. /* Check if we took a mbox interrupt while we were
  6211. polling */
  6212. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6213. && (evtctr != psli->slistat.mbox_event))
  6214. break;
  6215. if (i++ > 10) {
  6216. spin_unlock_irqrestore(&phba->hbalock,
  6217. drvr_flag);
  6218. msleep(1);
  6219. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6220. }
  6221. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6222. /* First copy command data */
  6223. word0 = *((uint32_t *)phba->mbox);
  6224. word0 = le32_to_cpu(word0);
  6225. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6226. MAILBOX_t *slimmb;
  6227. uint32_t slimword0;
  6228. /* Check real SLIM for any errors */
  6229. slimword0 = readl(phba->MBslimaddr);
  6230. slimmb = (MAILBOX_t *) & slimword0;
  6231. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6232. && slimmb->mbxStatus) {
  6233. psli->sli_flag &=
  6234. ~LPFC_SLI_ACTIVE;
  6235. word0 = slimword0;
  6236. }
  6237. }
  6238. } else {
  6239. /* First copy command data */
  6240. word0 = readl(phba->MBslimaddr);
  6241. }
  6242. /* Read the HBA Host Attention Register */
  6243. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6244. spin_unlock_irqrestore(&phba->hbalock,
  6245. drvr_flag);
  6246. goto out_not_finished;
  6247. }
  6248. }
  6249. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6250. /* copy results back to user */
  6251. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6252. /* Copy the mailbox extension data */
  6253. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6254. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6255. pmbox->context2,
  6256. pmbox->out_ext_byte_len);
  6257. }
  6258. } else {
  6259. /* First copy command data */
  6260. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6261. MAILBOX_CMD_SIZE);
  6262. /* Copy the mailbox extension data */
  6263. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6264. lpfc_memcpy_from_slim(pmbox->context2,
  6265. phba->MBslimaddr +
  6266. MAILBOX_HBA_EXT_OFFSET,
  6267. pmbox->out_ext_byte_len);
  6268. }
  6269. }
  6270. writel(HA_MBATT, phba->HAregaddr);
  6271. readl(phba->HAregaddr); /* flush */
  6272. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6273. status = mb->mbxStatus;
  6274. }
  6275. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6276. return status;
  6277. out_not_finished:
  6278. if (processing_queue) {
  6279. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6280. lpfc_mbox_cmpl_put(phba, pmbox);
  6281. }
  6282. return MBX_NOT_FINISHED;
  6283. }
  6284. /**
  6285. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6286. * @phba: Pointer to HBA context object.
  6287. *
  6288. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6289. * the driver internal pending mailbox queue. It will then try to wait out the
  6290. * possible outstanding mailbox command before return.
  6291. *
  6292. * Returns:
  6293. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6294. * the outstanding mailbox command timed out.
  6295. **/
  6296. static int
  6297. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6298. {
  6299. struct lpfc_sli *psli = &phba->sli;
  6300. int rc = 0;
  6301. unsigned long timeout = 0;
  6302. /* Mark the asynchronous mailbox command posting as blocked */
  6303. spin_lock_irq(&phba->hbalock);
  6304. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6305. /* Determine how long we might wait for the active mailbox
  6306. * command to be gracefully completed by firmware.
  6307. */
  6308. if (phba->sli.mbox_active)
  6309. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6310. phba->sli.mbox_active) *
  6311. 1000) + jiffies;
  6312. spin_unlock_irq(&phba->hbalock);
  6313. /* Wait for the outstnading mailbox command to complete */
  6314. while (phba->sli.mbox_active) {
  6315. /* Check active mailbox complete status every 2ms */
  6316. msleep(2);
  6317. if (time_after(jiffies, timeout)) {
  6318. /* Timeout, marked the outstanding cmd not complete */
  6319. rc = 1;
  6320. break;
  6321. }
  6322. }
  6323. /* Can not cleanly block async mailbox command, fails it */
  6324. if (rc) {
  6325. spin_lock_irq(&phba->hbalock);
  6326. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6327. spin_unlock_irq(&phba->hbalock);
  6328. }
  6329. return rc;
  6330. }
  6331. /**
  6332. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6333. * @phba: Pointer to HBA context object.
  6334. *
  6335. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6336. * commands from the driver internal pending mailbox queue. It makes sure
  6337. * that there is no outstanding mailbox command before resuming posting
  6338. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6339. * mailbox command, it will try to wait it out before resuming asynchronous
  6340. * mailbox command posting.
  6341. **/
  6342. static void
  6343. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6344. {
  6345. struct lpfc_sli *psli = &phba->sli;
  6346. spin_lock_irq(&phba->hbalock);
  6347. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6348. /* Asynchronous mailbox posting is not blocked, do nothing */
  6349. spin_unlock_irq(&phba->hbalock);
  6350. return;
  6351. }
  6352. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6353. * successful or timeout, after timing-out the outstanding mailbox
  6354. * command shall always be removed, so just unblock posting async
  6355. * mailbox command and resume
  6356. */
  6357. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6358. spin_unlock_irq(&phba->hbalock);
  6359. /* wake up worker thread to post asynchronlous mailbox command */
  6360. lpfc_worker_wake_up(phba);
  6361. }
  6362. /**
  6363. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6364. * @phba: Pointer to HBA context object.
  6365. * @mboxq: Pointer to mailbox object.
  6366. *
  6367. * The function posts a mailbox to the port. The mailbox is expected
  6368. * to be comletely filled in and ready for the port to operate on it.
  6369. * This routine executes a synchronous completion operation on the
  6370. * mailbox by polling for its completion.
  6371. *
  6372. * The caller must not be holding any locks when calling this routine.
  6373. *
  6374. * Returns:
  6375. * MBX_SUCCESS - mailbox posted successfully
  6376. * Any of the MBX error values.
  6377. **/
  6378. static int
  6379. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6380. {
  6381. int rc = MBX_SUCCESS;
  6382. unsigned long iflag;
  6383. uint32_t db_ready;
  6384. uint32_t mcqe_status;
  6385. uint32_t mbx_cmnd;
  6386. unsigned long timeout;
  6387. struct lpfc_sli *psli = &phba->sli;
  6388. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6389. struct lpfc_bmbx_create *mbox_rgn;
  6390. struct dma_address *dma_address;
  6391. struct lpfc_register bmbx_reg;
  6392. /*
  6393. * Only one mailbox can be active to the bootstrap mailbox region
  6394. * at a time and there is no queueing provided.
  6395. */
  6396. spin_lock_irqsave(&phba->hbalock, iflag);
  6397. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6398. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6399. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6400. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6401. "cannot issue Data: x%x x%x\n",
  6402. mboxq->vport ? mboxq->vport->vpi : 0,
  6403. mboxq->u.mb.mbxCommand,
  6404. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6405. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6406. psli->sli_flag, MBX_POLL);
  6407. return MBXERR_ERROR;
  6408. }
  6409. /* The server grabs the token and owns it until release */
  6410. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6411. phba->sli.mbox_active = mboxq;
  6412. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6413. /*
  6414. * Initialize the bootstrap memory region to avoid stale data areas
  6415. * in the mailbox post. Then copy the caller's mailbox contents to
  6416. * the bmbx mailbox region.
  6417. */
  6418. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6419. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6420. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6421. sizeof(struct lpfc_mqe));
  6422. /* Post the high mailbox dma address to the port and wait for ready. */
  6423. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6424. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6425. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6426. * 1000) + jiffies;
  6427. do {
  6428. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6429. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6430. if (!db_ready)
  6431. msleep(2);
  6432. if (time_after(jiffies, timeout)) {
  6433. rc = MBXERR_ERROR;
  6434. goto exit;
  6435. }
  6436. } while (!db_ready);
  6437. /* Post the low mailbox dma address to the port. */
  6438. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6439. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6440. * 1000) + jiffies;
  6441. do {
  6442. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6443. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6444. if (!db_ready)
  6445. msleep(2);
  6446. if (time_after(jiffies, timeout)) {
  6447. rc = MBXERR_ERROR;
  6448. goto exit;
  6449. }
  6450. } while (!db_ready);
  6451. /*
  6452. * Read the CQ to ensure the mailbox has completed.
  6453. * If so, update the mailbox status so that the upper layers
  6454. * can complete the request normally.
  6455. */
  6456. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6457. sizeof(struct lpfc_mqe));
  6458. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6459. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6460. sizeof(struct lpfc_mcqe));
  6461. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6462. /*
  6463. * When the CQE status indicates a failure and the mailbox status
  6464. * indicates success then copy the CQE status into the mailbox status
  6465. * (and prefix it with x4000).
  6466. */
  6467. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6468. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6469. bf_set(lpfc_mqe_status, mb,
  6470. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6471. rc = MBXERR_ERROR;
  6472. } else
  6473. lpfc_sli4_swap_str(phba, mboxq);
  6474. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6475. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6476. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6477. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6478. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6479. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6480. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6481. bf_get(lpfc_mqe_status, mb),
  6482. mb->un.mb_words[0], mb->un.mb_words[1],
  6483. mb->un.mb_words[2], mb->un.mb_words[3],
  6484. mb->un.mb_words[4], mb->un.mb_words[5],
  6485. mb->un.mb_words[6], mb->un.mb_words[7],
  6486. mb->un.mb_words[8], mb->un.mb_words[9],
  6487. mb->un.mb_words[10], mb->un.mb_words[11],
  6488. mb->un.mb_words[12], mboxq->mcqe.word0,
  6489. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6490. mboxq->mcqe.trailer);
  6491. exit:
  6492. /* We are holding the token, no needed for lock when release */
  6493. spin_lock_irqsave(&phba->hbalock, iflag);
  6494. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6495. phba->sli.mbox_active = NULL;
  6496. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6497. return rc;
  6498. }
  6499. /**
  6500. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6501. * @phba: Pointer to HBA context object.
  6502. * @pmbox: Pointer to mailbox object.
  6503. * @flag: Flag indicating how the mailbox need to be processed.
  6504. *
  6505. * This function is called by discovery code and HBA management code to submit
  6506. * a mailbox command to firmware with SLI-4 interface spec.
  6507. *
  6508. * Return codes the caller owns the mailbox command after the return of the
  6509. * function.
  6510. **/
  6511. static int
  6512. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6513. uint32_t flag)
  6514. {
  6515. struct lpfc_sli *psli = &phba->sli;
  6516. unsigned long iflags;
  6517. int rc;
  6518. /* dump from issue mailbox command if setup */
  6519. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6520. rc = lpfc_mbox_dev_check(phba);
  6521. if (unlikely(rc)) {
  6522. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6523. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6524. "cannot issue Data: x%x x%x\n",
  6525. mboxq->vport ? mboxq->vport->vpi : 0,
  6526. mboxq->u.mb.mbxCommand,
  6527. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6528. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6529. psli->sli_flag, flag);
  6530. goto out_not_finished;
  6531. }
  6532. /* Detect polling mode and jump to a handler */
  6533. if (!phba->sli4_hba.intr_enable) {
  6534. if (flag == MBX_POLL)
  6535. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6536. else
  6537. rc = -EIO;
  6538. if (rc != MBX_SUCCESS)
  6539. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6540. "(%d):2541 Mailbox command x%x "
  6541. "(x%x/x%x) cannot issue Data: "
  6542. "x%x x%x\n",
  6543. mboxq->vport ? mboxq->vport->vpi : 0,
  6544. mboxq->u.mb.mbxCommand,
  6545. lpfc_sli_config_mbox_subsys_get(phba,
  6546. mboxq),
  6547. lpfc_sli_config_mbox_opcode_get(phba,
  6548. mboxq),
  6549. psli->sli_flag, flag);
  6550. return rc;
  6551. } else if (flag == MBX_POLL) {
  6552. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6553. "(%d):2542 Try to issue mailbox command "
  6554. "x%x (x%x/x%x) synchronously ahead of async"
  6555. "mailbox command queue: x%x x%x\n",
  6556. mboxq->vport ? mboxq->vport->vpi : 0,
  6557. mboxq->u.mb.mbxCommand,
  6558. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6559. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6560. psli->sli_flag, flag);
  6561. /* Try to block the asynchronous mailbox posting */
  6562. rc = lpfc_sli4_async_mbox_block(phba);
  6563. if (!rc) {
  6564. /* Successfully blocked, now issue sync mbox cmd */
  6565. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6566. if (rc != MBX_SUCCESS)
  6567. lpfc_printf_log(phba, KERN_ERR,
  6568. LOG_MBOX | LOG_SLI,
  6569. "(%d):2597 Mailbox command "
  6570. "x%x (x%x/x%x) cannot issue "
  6571. "Data: x%x x%x\n",
  6572. mboxq->vport ?
  6573. mboxq->vport->vpi : 0,
  6574. mboxq->u.mb.mbxCommand,
  6575. lpfc_sli_config_mbox_subsys_get(phba,
  6576. mboxq),
  6577. lpfc_sli_config_mbox_opcode_get(phba,
  6578. mboxq),
  6579. psli->sli_flag, flag);
  6580. /* Unblock the async mailbox posting afterward */
  6581. lpfc_sli4_async_mbox_unblock(phba);
  6582. }
  6583. return rc;
  6584. }
  6585. /* Now, interrupt mode asynchrous mailbox command */
  6586. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6587. if (rc) {
  6588. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6589. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6590. "cannot issue Data: x%x x%x\n",
  6591. mboxq->vport ? mboxq->vport->vpi : 0,
  6592. mboxq->u.mb.mbxCommand,
  6593. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6594. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6595. psli->sli_flag, flag);
  6596. goto out_not_finished;
  6597. }
  6598. /* Put the mailbox command to the driver internal FIFO */
  6599. psli->slistat.mbox_busy++;
  6600. spin_lock_irqsave(&phba->hbalock, iflags);
  6601. lpfc_mbox_put(phba, mboxq);
  6602. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6603. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6604. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6605. "x%x (x%x/x%x) x%x x%x x%x\n",
  6606. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6607. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6608. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6609. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6610. phba->pport->port_state,
  6611. psli->sli_flag, MBX_NOWAIT);
  6612. /* Wake up worker thread to transport mailbox command from head */
  6613. lpfc_worker_wake_up(phba);
  6614. return MBX_BUSY;
  6615. out_not_finished:
  6616. return MBX_NOT_FINISHED;
  6617. }
  6618. /**
  6619. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6620. * @phba: Pointer to HBA context object.
  6621. *
  6622. * This function is called by worker thread to send a mailbox command to
  6623. * SLI4 HBA firmware.
  6624. *
  6625. **/
  6626. int
  6627. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6628. {
  6629. struct lpfc_sli *psli = &phba->sli;
  6630. LPFC_MBOXQ_t *mboxq;
  6631. int rc = MBX_SUCCESS;
  6632. unsigned long iflags;
  6633. struct lpfc_mqe *mqe;
  6634. uint32_t mbx_cmnd;
  6635. /* Check interrupt mode before post async mailbox command */
  6636. if (unlikely(!phba->sli4_hba.intr_enable))
  6637. return MBX_NOT_FINISHED;
  6638. /* Check for mailbox command service token */
  6639. spin_lock_irqsave(&phba->hbalock, iflags);
  6640. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6641. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6642. return MBX_NOT_FINISHED;
  6643. }
  6644. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6645. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6646. return MBX_NOT_FINISHED;
  6647. }
  6648. if (unlikely(phba->sli.mbox_active)) {
  6649. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6650. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6651. "0384 There is pending active mailbox cmd\n");
  6652. return MBX_NOT_FINISHED;
  6653. }
  6654. /* Take the mailbox command service token */
  6655. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6656. /* Get the next mailbox command from head of queue */
  6657. mboxq = lpfc_mbox_get(phba);
  6658. /* If no more mailbox command waiting for post, we're done */
  6659. if (!mboxq) {
  6660. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6661. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6662. return MBX_SUCCESS;
  6663. }
  6664. phba->sli.mbox_active = mboxq;
  6665. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6666. /* Check device readiness for posting mailbox command */
  6667. rc = lpfc_mbox_dev_check(phba);
  6668. if (unlikely(rc))
  6669. /* Driver clean routine will clean up pending mailbox */
  6670. goto out_not_finished;
  6671. /* Prepare the mbox command to be posted */
  6672. mqe = &mboxq->u.mqe;
  6673. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6674. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6675. mod_timer(&psli->mbox_tmo, (jiffies +
  6676. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6677. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6678. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6679. "x%x x%x\n",
  6680. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6681. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6682. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6683. phba->pport->port_state, psli->sli_flag);
  6684. if (mbx_cmnd != MBX_HEARTBEAT) {
  6685. if (mboxq->vport) {
  6686. lpfc_debugfs_disc_trc(mboxq->vport,
  6687. LPFC_DISC_TRC_MBOX_VPORT,
  6688. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6689. mbx_cmnd, mqe->un.mb_words[0],
  6690. mqe->un.mb_words[1]);
  6691. } else {
  6692. lpfc_debugfs_disc_trc(phba->pport,
  6693. LPFC_DISC_TRC_MBOX,
  6694. "MBOX Send: cmd:x%x mb:x%x x%x",
  6695. mbx_cmnd, mqe->un.mb_words[0],
  6696. mqe->un.mb_words[1]);
  6697. }
  6698. }
  6699. psli->slistat.mbox_cmd++;
  6700. /* Post the mailbox command to the port */
  6701. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6702. if (rc != MBX_SUCCESS) {
  6703. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6704. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6705. "cannot issue Data: x%x x%x\n",
  6706. mboxq->vport ? mboxq->vport->vpi : 0,
  6707. mboxq->u.mb.mbxCommand,
  6708. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6709. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6710. psli->sli_flag, MBX_NOWAIT);
  6711. goto out_not_finished;
  6712. }
  6713. return rc;
  6714. out_not_finished:
  6715. spin_lock_irqsave(&phba->hbalock, iflags);
  6716. if (phba->sli.mbox_active) {
  6717. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6718. __lpfc_mbox_cmpl_put(phba, mboxq);
  6719. /* Release the token */
  6720. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6721. phba->sli.mbox_active = NULL;
  6722. }
  6723. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6724. return MBX_NOT_FINISHED;
  6725. }
  6726. /**
  6727. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6728. * @phba: Pointer to HBA context object.
  6729. * @pmbox: Pointer to mailbox object.
  6730. * @flag: Flag indicating how the mailbox need to be processed.
  6731. *
  6732. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6733. * the API jump table function pointer from the lpfc_hba struct.
  6734. *
  6735. * Return codes the caller owns the mailbox command after the return of the
  6736. * function.
  6737. **/
  6738. int
  6739. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6740. {
  6741. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6742. }
  6743. /**
  6744. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6745. * @phba: The hba struct for which this call is being executed.
  6746. * @dev_grp: The HBA PCI-Device group number.
  6747. *
  6748. * This routine sets up the mbox interface API function jump table in @phba
  6749. * struct.
  6750. * Returns: 0 - success, -ENODEV - failure.
  6751. **/
  6752. int
  6753. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6754. {
  6755. switch (dev_grp) {
  6756. case LPFC_PCI_DEV_LP:
  6757. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6758. phba->lpfc_sli_handle_slow_ring_event =
  6759. lpfc_sli_handle_slow_ring_event_s3;
  6760. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6761. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6762. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6763. break;
  6764. case LPFC_PCI_DEV_OC:
  6765. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6766. phba->lpfc_sli_handle_slow_ring_event =
  6767. lpfc_sli_handle_slow_ring_event_s4;
  6768. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6769. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6770. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6771. break;
  6772. default:
  6773. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6774. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6775. dev_grp);
  6776. return -ENODEV;
  6777. break;
  6778. }
  6779. return 0;
  6780. }
  6781. /**
  6782. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6783. * @phba: Pointer to HBA context object.
  6784. * @pring: Pointer to driver SLI ring object.
  6785. * @piocb: Pointer to address of newly added command iocb.
  6786. *
  6787. * This function is called with hbalock held to add a command
  6788. * iocb to the txq when SLI layer cannot submit the command iocb
  6789. * to the ring.
  6790. **/
  6791. void
  6792. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6793. struct lpfc_iocbq *piocb)
  6794. {
  6795. /* Insert the caller's iocb in the txq tail for later processing. */
  6796. list_add_tail(&piocb->list, &pring->txq);
  6797. pring->txq_cnt++;
  6798. }
  6799. /**
  6800. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6801. * @phba: Pointer to HBA context object.
  6802. * @pring: Pointer to driver SLI ring object.
  6803. * @piocb: Pointer to address of newly added command iocb.
  6804. *
  6805. * This function is called with hbalock held before a new
  6806. * iocb is submitted to the firmware. This function checks
  6807. * txq to flush the iocbs in txq to Firmware before
  6808. * submitting new iocbs to the Firmware.
  6809. * If there are iocbs in the txq which need to be submitted
  6810. * to firmware, lpfc_sli_next_iocb returns the first element
  6811. * of the txq after dequeuing it from txq.
  6812. * If there is no iocb in the txq then the function will return
  6813. * *piocb and *piocb is set to NULL. Caller needs to check
  6814. * *piocb to find if there are more commands in the txq.
  6815. **/
  6816. static struct lpfc_iocbq *
  6817. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6818. struct lpfc_iocbq **piocb)
  6819. {
  6820. struct lpfc_iocbq * nextiocb;
  6821. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6822. if (!nextiocb) {
  6823. nextiocb = *piocb;
  6824. *piocb = NULL;
  6825. }
  6826. return nextiocb;
  6827. }
  6828. /**
  6829. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6830. * @phba: Pointer to HBA context object.
  6831. * @ring_number: SLI ring number to issue iocb on.
  6832. * @piocb: Pointer to command iocb.
  6833. * @flag: Flag indicating if this command can be put into txq.
  6834. *
  6835. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6836. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6837. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6838. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6839. * this function allows only iocbs for posting buffers. This function finds
  6840. * next available slot in the command ring and posts the command to the
  6841. * available slot and writes the port attention register to request HBA start
  6842. * processing new iocb. If there is no slot available in the ring and
  6843. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6844. * the function returns IOCB_BUSY.
  6845. *
  6846. * This function is called with hbalock held. The function will return success
  6847. * after it successfully submit the iocb to firmware or after adding to the
  6848. * txq.
  6849. **/
  6850. static int
  6851. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6852. struct lpfc_iocbq *piocb, uint32_t flag)
  6853. {
  6854. struct lpfc_iocbq *nextiocb;
  6855. IOCB_t *iocb;
  6856. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6857. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6858. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6859. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6860. lpfc_printf_log(phba, KERN_ERR,
  6861. LOG_SLI | LOG_VPORT,
  6862. "1807 IOCB x%x failed. No vport\n",
  6863. piocb->iocb.ulpCommand);
  6864. dump_stack();
  6865. return IOCB_ERROR;
  6866. }
  6867. /* If the PCI channel is in offline state, do not post iocbs. */
  6868. if (unlikely(pci_channel_offline(phba->pcidev)))
  6869. return IOCB_ERROR;
  6870. /* If HBA has a deferred error attention, fail the iocb. */
  6871. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6872. return IOCB_ERROR;
  6873. /*
  6874. * We should never get an IOCB if we are in a < LINK_DOWN state
  6875. */
  6876. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6877. return IOCB_ERROR;
  6878. /*
  6879. * Check to see if we are blocking IOCB processing because of a
  6880. * outstanding event.
  6881. */
  6882. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6883. goto iocb_busy;
  6884. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6885. /*
  6886. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6887. * can be issued if the link is not up.
  6888. */
  6889. switch (piocb->iocb.ulpCommand) {
  6890. case CMD_GEN_REQUEST64_CR:
  6891. case CMD_GEN_REQUEST64_CX:
  6892. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6893. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6894. FC_RCTL_DD_UNSOL_CMD) ||
  6895. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6896. MENLO_TRANSPORT_TYPE))
  6897. goto iocb_busy;
  6898. break;
  6899. case CMD_QUE_RING_BUF_CN:
  6900. case CMD_QUE_RING_BUF64_CN:
  6901. /*
  6902. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6903. * completion, iocb_cmpl MUST be 0.
  6904. */
  6905. if (piocb->iocb_cmpl)
  6906. piocb->iocb_cmpl = NULL;
  6907. /*FALLTHROUGH*/
  6908. case CMD_CREATE_XRI_CR:
  6909. case CMD_CLOSE_XRI_CN:
  6910. case CMD_CLOSE_XRI_CX:
  6911. break;
  6912. default:
  6913. goto iocb_busy;
  6914. }
  6915. /*
  6916. * For FCP commands, we must be in a state where we can process link
  6917. * attention events.
  6918. */
  6919. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6920. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6921. goto iocb_busy;
  6922. }
  6923. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6924. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6925. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6926. if (iocb)
  6927. lpfc_sli_update_ring(phba, pring);
  6928. else
  6929. lpfc_sli_update_full_ring(phba, pring);
  6930. if (!piocb)
  6931. return IOCB_SUCCESS;
  6932. goto out_busy;
  6933. iocb_busy:
  6934. pring->stats.iocb_cmd_delay++;
  6935. out_busy:
  6936. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6937. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6938. return IOCB_SUCCESS;
  6939. }
  6940. return IOCB_BUSY;
  6941. }
  6942. /**
  6943. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6944. * @phba: Pointer to HBA context object.
  6945. * @piocb: Pointer to command iocb.
  6946. * @sglq: Pointer to the scatter gather queue object.
  6947. *
  6948. * This routine converts the bpl or bde that is in the IOCB
  6949. * to a sgl list for the sli4 hardware. The physical address
  6950. * of the bpl/bde is converted back to a virtual address.
  6951. * If the IOCB contains a BPL then the list of BDE's is
  6952. * converted to sli4_sge's. If the IOCB contains a single
  6953. * BDE then it is converted to a single sli_sge.
  6954. * The IOCB is still in cpu endianess so the contents of
  6955. * the bpl can be used without byte swapping.
  6956. *
  6957. * Returns valid XRI = Success, NO_XRI = Failure.
  6958. **/
  6959. static uint16_t
  6960. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6961. struct lpfc_sglq *sglq)
  6962. {
  6963. uint16_t xritag = NO_XRI;
  6964. struct ulp_bde64 *bpl = NULL;
  6965. struct ulp_bde64 bde;
  6966. struct sli4_sge *sgl = NULL;
  6967. struct lpfc_dmabuf *dmabuf;
  6968. IOCB_t *icmd;
  6969. int numBdes = 0;
  6970. int i = 0;
  6971. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6972. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6973. if (!piocbq || !sglq)
  6974. return xritag;
  6975. sgl = (struct sli4_sge *)sglq->sgl;
  6976. icmd = &piocbq->iocb;
  6977. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6978. return sglq->sli4_xritag;
  6979. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6980. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6981. sizeof(struct ulp_bde64);
  6982. /* The addrHigh and addrLow fields within the IOCB
  6983. * have not been byteswapped yet so there is no
  6984. * need to swap them back.
  6985. */
  6986. if (piocbq->context3)
  6987. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  6988. else
  6989. return xritag;
  6990. bpl = (struct ulp_bde64 *)dmabuf->virt;
  6991. if (!bpl)
  6992. return xritag;
  6993. for (i = 0; i < numBdes; i++) {
  6994. /* Should already be byte swapped. */
  6995. sgl->addr_hi = bpl->addrHigh;
  6996. sgl->addr_lo = bpl->addrLow;
  6997. sgl->word2 = le32_to_cpu(sgl->word2);
  6998. if ((i+1) == numBdes)
  6999. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7000. else
  7001. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7002. /* swap the size field back to the cpu so we
  7003. * can assign it to the sgl.
  7004. */
  7005. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7006. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7007. /* The offsets in the sgl need to be accumulated
  7008. * separately for the request and reply lists.
  7009. * The request is always first, the reply follows.
  7010. */
  7011. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7012. /* add up the reply sg entries */
  7013. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7014. inbound++;
  7015. /* first inbound? reset the offset */
  7016. if (inbound == 1)
  7017. offset = 0;
  7018. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7019. bf_set(lpfc_sli4_sge_type, sgl,
  7020. LPFC_SGE_TYPE_DATA);
  7021. offset += bde.tus.f.bdeSize;
  7022. }
  7023. sgl->word2 = cpu_to_le32(sgl->word2);
  7024. bpl++;
  7025. sgl++;
  7026. }
  7027. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7028. /* The addrHigh and addrLow fields of the BDE have not
  7029. * been byteswapped yet so they need to be swapped
  7030. * before putting them in the sgl.
  7031. */
  7032. sgl->addr_hi =
  7033. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7034. sgl->addr_lo =
  7035. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7036. sgl->word2 = le32_to_cpu(sgl->word2);
  7037. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7038. sgl->word2 = cpu_to_le32(sgl->word2);
  7039. sgl->sge_len =
  7040. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7041. }
  7042. return sglq->sli4_xritag;
  7043. }
  7044. /**
  7045. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7046. * @phba: Pointer to HBA context object.
  7047. *
  7048. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7049. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7050. * held.
  7051. *
  7052. * Return: index into SLI4 fast-path FCP queue index.
  7053. **/
  7054. static uint32_t
  7055. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7056. {
  7057. ++phba->fcp_qidx;
  7058. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  7059. phba->fcp_qidx = 0;
  7060. return phba->fcp_qidx;
  7061. }
  7062. /**
  7063. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7064. * @phba: Pointer to HBA context object.
  7065. * @piocb: Pointer to command iocb.
  7066. * @wqe: Pointer to the work queue entry.
  7067. *
  7068. * This routine converts the iocb command to its Work Queue Entry
  7069. * equivalent. The wqe pointer should not have any fields set when
  7070. * this routine is called because it will memcpy over them.
  7071. * This routine does not set the CQ_ID or the WQEC bits in the
  7072. * wqe.
  7073. *
  7074. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7075. **/
  7076. static int
  7077. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7078. union lpfc_wqe *wqe)
  7079. {
  7080. uint32_t xmit_len = 0, total_len = 0;
  7081. uint8_t ct = 0;
  7082. uint32_t fip;
  7083. uint32_t abort_tag;
  7084. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7085. uint8_t cmnd;
  7086. uint16_t xritag;
  7087. uint16_t abrt_iotag;
  7088. struct lpfc_iocbq *abrtiocbq;
  7089. struct ulp_bde64 *bpl = NULL;
  7090. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7091. int numBdes, i;
  7092. struct ulp_bde64 bde;
  7093. struct lpfc_nodelist *ndlp;
  7094. uint32_t *pcmd;
  7095. uint32_t if_type;
  7096. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7097. /* The fcp commands will set command type */
  7098. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7099. command_type = FCP_COMMAND;
  7100. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7101. command_type = ELS_COMMAND_FIP;
  7102. else
  7103. command_type = ELS_COMMAND_NON_FIP;
  7104. /* Some of the fields are in the right position already */
  7105. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7106. abort_tag = (uint32_t) iocbq->iotag;
  7107. xritag = iocbq->sli4_xritag;
  7108. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7109. /* words0-2 bpl convert bde */
  7110. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7111. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7112. sizeof(struct ulp_bde64);
  7113. bpl = (struct ulp_bde64 *)
  7114. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7115. if (!bpl)
  7116. return IOCB_ERROR;
  7117. /* Should already be byte swapped. */
  7118. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7119. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7120. /* swap the size field back to the cpu so we
  7121. * can assign it to the sgl.
  7122. */
  7123. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7124. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7125. total_len = 0;
  7126. for (i = 0; i < numBdes; i++) {
  7127. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7128. total_len += bde.tus.f.bdeSize;
  7129. }
  7130. } else
  7131. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7132. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7133. cmnd = iocbq->iocb.ulpCommand;
  7134. switch (iocbq->iocb.ulpCommand) {
  7135. case CMD_ELS_REQUEST64_CR:
  7136. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7137. if (!iocbq->iocb.ulpLe) {
  7138. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7139. "2007 Only Limited Edition cmd Format"
  7140. " supported 0x%x\n",
  7141. iocbq->iocb.ulpCommand);
  7142. return IOCB_ERROR;
  7143. }
  7144. wqe->els_req.payload_len = xmit_len;
  7145. /* Els_reguest64 has a TMO */
  7146. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7147. iocbq->iocb.ulpTimeout);
  7148. /* Need a VF for word 4 set the vf bit*/
  7149. bf_set(els_req64_vf, &wqe->els_req, 0);
  7150. /* And a VFID for word 12 */
  7151. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7152. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7153. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7154. iocbq->iocb.ulpContext);
  7155. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7156. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7157. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7158. if (command_type == ELS_COMMAND_FIP)
  7159. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7160. >> LPFC_FIP_ELS_ID_SHIFT);
  7161. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7162. iocbq->context2)->virt);
  7163. if_type = bf_get(lpfc_sli_intf_if_type,
  7164. &phba->sli4_hba.sli_intf);
  7165. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7166. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7167. *pcmd == ELS_CMD_SCR ||
  7168. *pcmd == ELS_CMD_FDISC ||
  7169. *pcmd == ELS_CMD_LOGO ||
  7170. *pcmd == ELS_CMD_PLOGI)) {
  7171. bf_set(els_req64_sp, &wqe->els_req, 1);
  7172. bf_set(els_req64_sid, &wqe->els_req,
  7173. iocbq->vport->fc_myDID);
  7174. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7175. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7176. phba->vpi_ids[iocbq->vport->vpi]);
  7177. } else if (pcmd && iocbq->context1) {
  7178. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7179. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7180. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7181. }
  7182. }
  7183. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7184. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7185. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7186. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7187. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7188. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7189. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7190. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7191. break;
  7192. case CMD_XMIT_SEQUENCE64_CX:
  7193. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7194. iocbq->iocb.un.ulpWord[3]);
  7195. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7196. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7197. /* The entire sequence is transmitted for this IOCB */
  7198. xmit_len = total_len;
  7199. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7200. if (phba->link_flag & LS_LOOPBACK_MODE)
  7201. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7202. case CMD_XMIT_SEQUENCE64_CR:
  7203. /* word3 iocb=io_tag32 wqe=reserved */
  7204. wqe->xmit_sequence.rsvd3 = 0;
  7205. /* word4 relative_offset memcpy */
  7206. /* word5 r_ctl/df_ctl memcpy */
  7207. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7208. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7209. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7210. LPFC_WQE_IOD_WRITE);
  7211. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7212. LPFC_WQE_LENLOC_WORD12);
  7213. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7214. wqe->xmit_sequence.xmit_len = xmit_len;
  7215. command_type = OTHER_COMMAND;
  7216. break;
  7217. case CMD_XMIT_BCAST64_CN:
  7218. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7219. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7220. /* word4 iocb=rsvd wqe=rsvd */
  7221. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7222. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7223. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7224. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7225. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7226. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7227. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7228. LPFC_WQE_LENLOC_WORD3);
  7229. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7230. break;
  7231. case CMD_FCP_IWRITE64_CR:
  7232. command_type = FCP_COMMAND_DATA_OUT;
  7233. /* word3 iocb=iotag wqe=payload_offset_len */
  7234. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7235. wqe->fcp_iwrite.payload_offset_len =
  7236. xmit_len + sizeof(struct fcp_rsp);
  7237. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7238. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7239. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7240. iocbq->iocb.ulpFCP2Rcvy);
  7241. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7242. /* Always open the exchange */
  7243. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7244. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7245. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7246. LPFC_WQE_LENLOC_WORD4);
  7247. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7248. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7249. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7250. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7251. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7252. }
  7253. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7254. break;
  7255. case CMD_FCP_IREAD64_CR:
  7256. /* word3 iocb=iotag wqe=payload_offset_len */
  7257. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7258. wqe->fcp_iread.payload_offset_len =
  7259. xmit_len + sizeof(struct fcp_rsp);
  7260. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7261. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7262. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7263. iocbq->iocb.ulpFCP2Rcvy);
  7264. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7265. /* Always open the exchange */
  7266. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7267. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7268. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7269. LPFC_WQE_LENLOC_WORD4);
  7270. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7271. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7272. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7273. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7274. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7275. }
  7276. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7277. break;
  7278. case CMD_FCP_ICMND64_CR:
  7279. /* word3 iocb=IO_TAG wqe=reserved */
  7280. wqe->fcp_icmd.rsrvd3 = 0;
  7281. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7282. /* Always open the exchange */
  7283. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7284. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7285. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7286. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7287. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7288. LPFC_WQE_LENLOC_NONE);
  7289. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7290. break;
  7291. case CMD_GEN_REQUEST64_CR:
  7292. /* For this command calculate the xmit length of the
  7293. * request bde.
  7294. */
  7295. xmit_len = 0;
  7296. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7297. sizeof(struct ulp_bde64);
  7298. for (i = 0; i < numBdes; i++) {
  7299. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7300. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7301. break;
  7302. xmit_len += bde.tus.f.bdeSize;
  7303. }
  7304. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7305. wqe->gen_req.request_payload_len = xmit_len;
  7306. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7307. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7308. /* word6 context tag copied in memcpy */
  7309. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7310. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7311. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7312. "2015 Invalid CT %x command 0x%x\n",
  7313. ct, iocbq->iocb.ulpCommand);
  7314. return IOCB_ERROR;
  7315. }
  7316. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7317. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7318. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7319. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7320. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7321. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7322. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7323. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7324. command_type = OTHER_COMMAND;
  7325. break;
  7326. case CMD_XMIT_ELS_RSP64_CX:
  7327. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7328. /* words0-2 BDE memcpy */
  7329. /* word3 iocb=iotag32 wqe=response_payload_len */
  7330. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7331. /* word4 iocb=did wge=rsvd. */
  7332. wqe->xmit_els_rsp.rsvd4 = 0;
  7333. /* word5 iocb=rsvd wge=did */
  7334. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7335. iocbq->iocb.un.elsreq64.remoteID);
  7336. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7337. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7338. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7339. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7340. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7341. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7342. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7343. phba->vpi_ids[iocbq->vport->vpi]);
  7344. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7345. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7346. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7347. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7348. LPFC_WQE_LENLOC_WORD3);
  7349. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7350. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7351. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7352. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7353. iocbq->context2)->virt);
  7354. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7355. bf_set(els_req64_sp, &wqe->els_req, 1);
  7356. bf_set(els_req64_sid, &wqe->els_req,
  7357. iocbq->vport->fc_myDID);
  7358. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7359. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7360. phba->vpi_ids[phba->pport->vpi]);
  7361. }
  7362. command_type = OTHER_COMMAND;
  7363. break;
  7364. case CMD_CLOSE_XRI_CN:
  7365. case CMD_ABORT_XRI_CN:
  7366. case CMD_ABORT_XRI_CX:
  7367. /* words 0-2 memcpy should be 0 rserved */
  7368. /* port will send abts */
  7369. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7370. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7371. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7372. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7373. } else
  7374. fip = 0;
  7375. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7376. /*
  7377. * The link is down, or the command was ELS_FIP
  7378. * so the fw does not need to send abts
  7379. * on the wire.
  7380. */
  7381. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7382. else
  7383. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7384. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7385. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7386. wqe->abort_cmd.rsrvd5 = 0;
  7387. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7388. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7389. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7390. /*
  7391. * The abort handler will send us CMD_ABORT_XRI_CN or
  7392. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7393. */
  7394. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7395. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7396. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7397. LPFC_WQE_LENLOC_NONE);
  7398. cmnd = CMD_ABORT_XRI_CX;
  7399. command_type = OTHER_COMMAND;
  7400. xritag = 0;
  7401. break;
  7402. case CMD_XMIT_BLS_RSP64_CX:
  7403. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7404. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7405. * we re-construct this WQE here based on information in
  7406. * iocbq from scratch.
  7407. */
  7408. memset(wqe, 0, sizeof(union lpfc_wqe));
  7409. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7410. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7411. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7412. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7413. LPFC_ABTS_UNSOL_INT) {
  7414. /* ABTS sent by initiator to CT exchange, the
  7415. * RX_ID field will be filled with the newly
  7416. * allocated responder XRI.
  7417. */
  7418. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7419. iocbq->sli4_xritag);
  7420. } else {
  7421. /* ABTS sent by responder to CT exchange, the
  7422. * RX_ID field will be filled with the responder
  7423. * RX_ID from ABTS.
  7424. */
  7425. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7426. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7427. }
  7428. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7429. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7430. /* Use CT=VPI */
  7431. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7432. ndlp->nlp_DID);
  7433. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7434. iocbq->iocb.ulpContext);
  7435. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7436. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7437. phba->vpi_ids[phba->pport->vpi]);
  7438. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7439. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7440. LPFC_WQE_LENLOC_NONE);
  7441. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7442. command_type = OTHER_COMMAND;
  7443. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7444. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7445. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7446. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7447. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7448. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7449. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7450. }
  7451. break;
  7452. case CMD_XRI_ABORTED_CX:
  7453. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7454. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7455. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7456. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7457. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7458. default:
  7459. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7460. "2014 Invalid command 0x%x\n",
  7461. iocbq->iocb.ulpCommand);
  7462. return IOCB_ERROR;
  7463. break;
  7464. }
  7465. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7466. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7467. wqe->generic.wqe_com.abort_tag = abort_tag;
  7468. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7469. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7470. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7471. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7472. return 0;
  7473. }
  7474. /**
  7475. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7476. * @phba: Pointer to HBA context object.
  7477. * @ring_number: SLI ring number to issue iocb on.
  7478. * @piocb: Pointer to command iocb.
  7479. * @flag: Flag indicating if this command can be put into txq.
  7480. *
  7481. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7482. * an iocb command to an HBA with SLI-4 interface spec.
  7483. *
  7484. * This function is called with hbalock held. The function will return success
  7485. * after it successfully submit the iocb to firmware or after adding to the
  7486. * txq.
  7487. **/
  7488. static int
  7489. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7490. struct lpfc_iocbq *piocb, uint32_t flag)
  7491. {
  7492. struct lpfc_sglq *sglq;
  7493. union lpfc_wqe wqe;
  7494. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7495. if (piocb->sli4_xritag == NO_XRI) {
  7496. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7497. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7498. sglq = NULL;
  7499. else {
  7500. if (pring->txq_cnt) {
  7501. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7502. __lpfc_sli_ringtx_put(phba,
  7503. pring, piocb);
  7504. return IOCB_SUCCESS;
  7505. } else {
  7506. return IOCB_BUSY;
  7507. }
  7508. } else {
  7509. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7510. if (!sglq) {
  7511. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7512. __lpfc_sli_ringtx_put(phba,
  7513. pring,
  7514. piocb);
  7515. return IOCB_SUCCESS;
  7516. } else
  7517. return IOCB_BUSY;
  7518. }
  7519. }
  7520. }
  7521. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7522. /* These IO's already have an XRI and a mapped sgl. */
  7523. sglq = NULL;
  7524. } else {
  7525. /*
  7526. * This is a continuation of a commandi,(CX) so this
  7527. * sglq is on the active list
  7528. */
  7529. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7530. if (!sglq)
  7531. return IOCB_ERROR;
  7532. }
  7533. if (sglq) {
  7534. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7535. piocb->sli4_xritag = sglq->sli4_xritag;
  7536. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7537. return IOCB_ERROR;
  7538. }
  7539. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7540. return IOCB_ERROR;
  7541. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7542. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7543. /*
  7544. * For FCP command IOCB, get a new WQ index to distribute
  7545. * WQE across the WQsr. On the other hand, for abort IOCB,
  7546. * it carries the same WQ index to the original command
  7547. * IOCB.
  7548. */
  7549. if (piocb->iocb_flag & LPFC_IO_FCP)
  7550. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7551. if (unlikely(!phba->sli4_hba.fcp_wq))
  7552. return IOCB_ERROR;
  7553. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7554. &wqe))
  7555. return IOCB_ERROR;
  7556. } else {
  7557. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7558. return IOCB_ERROR;
  7559. }
  7560. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7561. return 0;
  7562. }
  7563. /**
  7564. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7565. *
  7566. * This routine wraps the actual lockless version for issusing IOCB function
  7567. * pointer from the lpfc_hba struct.
  7568. *
  7569. * Return codes:
  7570. * IOCB_ERROR - Error
  7571. * IOCB_SUCCESS - Success
  7572. * IOCB_BUSY - Busy
  7573. **/
  7574. int
  7575. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7576. struct lpfc_iocbq *piocb, uint32_t flag)
  7577. {
  7578. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7579. }
  7580. /**
  7581. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7582. * @phba: The hba struct for which this call is being executed.
  7583. * @dev_grp: The HBA PCI-Device group number.
  7584. *
  7585. * This routine sets up the SLI interface API function jump table in @phba
  7586. * struct.
  7587. * Returns: 0 - success, -ENODEV - failure.
  7588. **/
  7589. int
  7590. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7591. {
  7592. switch (dev_grp) {
  7593. case LPFC_PCI_DEV_LP:
  7594. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7595. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7596. break;
  7597. case LPFC_PCI_DEV_OC:
  7598. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7599. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7600. break;
  7601. default:
  7602. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7603. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7604. dev_grp);
  7605. return -ENODEV;
  7606. break;
  7607. }
  7608. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7609. return 0;
  7610. }
  7611. /**
  7612. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7613. * @phba: Pointer to HBA context object.
  7614. * @pring: Pointer to driver SLI ring object.
  7615. * @piocb: Pointer to command iocb.
  7616. * @flag: Flag indicating if this command can be put into txq.
  7617. *
  7618. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7619. * function. This function gets the hbalock and calls
  7620. * __lpfc_sli_issue_iocb function and will return the error returned
  7621. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7622. * functions which do not hold hbalock.
  7623. **/
  7624. int
  7625. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7626. struct lpfc_iocbq *piocb, uint32_t flag)
  7627. {
  7628. unsigned long iflags;
  7629. int rc;
  7630. spin_lock_irqsave(&phba->hbalock, iflags);
  7631. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7632. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7633. return rc;
  7634. }
  7635. /**
  7636. * lpfc_extra_ring_setup - Extra ring setup function
  7637. * @phba: Pointer to HBA context object.
  7638. *
  7639. * This function is called while driver attaches with the
  7640. * HBA to setup the extra ring. The extra ring is used
  7641. * only when driver needs to support target mode functionality
  7642. * or IP over FC functionalities.
  7643. *
  7644. * This function is called with no lock held.
  7645. **/
  7646. static int
  7647. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7648. {
  7649. struct lpfc_sli *psli;
  7650. struct lpfc_sli_ring *pring;
  7651. psli = &phba->sli;
  7652. /* Adjust cmd/rsp ring iocb entries more evenly */
  7653. /* Take some away from the FCP ring */
  7654. pring = &psli->ring[psli->fcp_ring];
  7655. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7656. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7657. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7658. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7659. /* and give them to the extra ring */
  7660. pring = &psli->ring[psli->extra_ring];
  7661. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7662. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7663. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7664. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7665. /* Setup default profile for this ring */
  7666. pring->iotag_max = 4096;
  7667. pring->num_mask = 1;
  7668. pring->prt[0].profile = 0; /* Mask 0 */
  7669. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7670. pring->prt[0].type = phba->cfg_multi_ring_type;
  7671. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7672. return 0;
  7673. }
  7674. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7675. * @vport: pointer to virtual port object.
  7676. * @ndlp: nodelist pointer for the impacted rport.
  7677. *
  7678. * The driver calls this routine in response to a XRI ABORT CQE
  7679. * event from the port. In this event, the driver is required to
  7680. * recover its login to the rport even though its login may be valid
  7681. * from the driver's perspective. The failed ABTS notice from the
  7682. * port indicates the rport is not responding.
  7683. */
  7684. static void
  7685. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7686. struct lpfc_nodelist *ndlp)
  7687. {
  7688. struct Scsi_Host *shost;
  7689. struct lpfc_hba *phba;
  7690. unsigned long flags = 0;
  7691. shost = lpfc_shost_from_vport(vport);
  7692. phba = vport->phba;
  7693. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7694. lpfc_printf_log(phba, KERN_INFO,
  7695. LOG_SLI, "3093 No rport recovery needed. "
  7696. "rport in state 0x%x\n",
  7697. ndlp->nlp_state);
  7698. return;
  7699. }
  7700. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7701. "3094 Start rport recovery on shost id 0x%x "
  7702. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7703. "flags 0x%x\n",
  7704. shost->host_no, ndlp->nlp_DID,
  7705. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7706. ndlp->nlp_flag);
  7707. /*
  7708. * The rport is not responding. Don't attempt ADISC recovery.
  7709. * Remove the FCP-2 flag to force a PLOGI.
  7710. */
  7711. spin_lock_irqsave(shost->host_lock, flags);
  7712. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7713. spin_unlock_irqrestore(shost->host_lock, flags);
  7714. lpfc_disc_state_machine(vport, ndlp, NULL,
  7715. NLP_EVT_DEVICE_RECOVERY);
  7716. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7717. spin_lock_irqsave(shost->host_lock, flags);
  7718. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7719. spin_unlock_irqrestore(shost->host_lock, flags);
  7720. lpfc_disc_start(vport);
  7721. }
  7722. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7723. * @phba: Pointer to HBA context object.
  7724. * @iocbq: Pointer to iocb object.
  7725. *
  7726. * The async_event handler calls this routine when it receives
  7727. * an ASYNC_STATUS_CN event from the port. The port generates
  7728. * this event when an Abort Sequence request to an rport fails
  7729. * twice in succession. The abort could be originated by the
  7730. * driver or by the port. The ABTS could have been for an ELS
  7731. * or FCP IO. The port only generates this event when an ABTS
  7732. * fails to complete after one retry.
  7733. */
  7734. static void
  7735. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7736. struct lpfc_iocbq *iocbq)
  7737. {
  7738. struct lpfc_nodelist *ndlp = NULL;
  7739. uint16_t rpi = 0, vpi = 0;
  7740. struct lpfc_vport *vport = NULL;
  7741. /* The rpi in the ulpContext is vport-sensitive. */
  7742. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7743. rpi = iocbq->iocb.ulpContext;
  7744. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7745. "3092 Port generated ABTS async event "
  7746. "on vpi %d rpi %d status 0x%x\n",
  7747. vpi, rpi, iocbq->iocb.ulpStatus);
  7748. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7749. if (!vport)
  7750. goto err_exit;
  7751. ndlp = lpfc_findnode_rpi(vport, rpi);
  7752. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7753. goto err_exit;
  7754. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7755. lpfc_sli_abts_recover_port(vport, ndlp);
  7756. return;
  7757. err_exit:
  7758. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7759. "3095 Event Context not found, no "
  7760. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7761. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7762. vpi, rpi);
  7763. }
  7764. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7765. * @phba: pointer to HBA context object.
  7766. * @ndlp: nodelist pointer for the impacted rport.
  7767. * @axri: pointer to the wcqe containing the failed exchange.
  7768. *
  7769. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7770. * port. The port generates this event when an abort exchange request to an
  7771. * rport fails twice in succession with no reply. The abort could be originated
  7772. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7773. */
  7774. void
  7775. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7776. struct lpfc_nodelist *ndlp,
  7777. struct sli4_wcqe_xri_aborted *axri)
  7778. {
  7779. struct lpfc_vport *vport;
  7780. uint32_t ext_status = 0;
  7781. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7782. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7783. "3115 Node Context not found, driver "
  7784. "ignoring abts err event\n");
  7785. return;
  7786. }
  7787. vport = ndlp->vport;
  7788. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7789. "3116 Port generated FCP XRI ABORT event on "
  7790. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7791. ndlp->vport->vpi, ndlp->nlp_rpi,
  7792. bf_get(lpfc_wcqe_xa_xri, axri),
  7793. bf_get(lpfc_wcqe_xa_status, axri),
  7794. axri->parameter);
  7795. /*
  7796. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7797. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7798. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7799. */
  7800. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7801. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7802. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7803. lpfc_sli_abts_recover_port(vport, ndlp);
  7804. }
  7805. /**
  7806. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7807. * @phba: Pointer to HBA context object.
  7808. * @pring: Pointer to driver SLI ring object.
  7809. * @iocbq: Pointer to iocb object.
  7810. *
  7811. * This function is called by the slow ring event handler
  7812. * function when there is an ASYNC event iocb in the ring.
  7813. * This function is called with no lock held.
  7814. * Currently this function handles only temperature related
  7815. * ASYNC events. The function decodes the temperature sensor
  7816. * event message and posts events for the management applications.
  7817. **/
  7818. static void
  7819. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7820. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7821. {
  7822. IOCB_t *icmd;
  7823. uint16_t evt_code;
  7824. struct temp_event temp_event_data;
  7825. struct Scsi_Host *shost;
  7826. uint32_t *iocb_w;
  7827. icmd = &iocbq->iocb;
  7828. evt_code = icmd->un.asyncstat.evt_code;
  7829. switch (evt_code) {
  7830. case ASYNC_TEMP_WARN:
  7831. case ASYNC_TEMP_SAFE:
  7832. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7833. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7834. if (evt_code == ASYNC_TEMP_WARN) {
  7835. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7836. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7837. "0347 Adapter is very hot, please take "
  7838. "corrective action. temperature : %d Celsius\n",
  7839. (uint32_t) icmd->ulpContext);
  7840. } else {
  7841. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7842. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7843. "0340 Adapter temperature is OK now. "
  7844. "temperature : %d Celsius\n",
  7845. (uint32_t) icmd->ulpContext);
  7846. }
  7847. /* Send temperature change event to applications */
  7848. shost = lpfc_shost_from_vport(phba->pport);
  7849. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7850. sizeof(temp_event_data), (char *) &temp_event_data,
  7851. LPFC_NL_VENDOR_ID);
  7852. break;
  7853. case ASYNC_STATUS_CN:
  7854. lpfc_sli_abts_err_handler(phba, iocbq);
  7855. break;
  7856. default:
  7857. iocb_w = (uint32_t *) icmd;
  7858. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7859. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7860. " evt_code 0x%x\n"
  7861. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7862. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7863. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7864. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7865. pring->ringno, icmd->un.asyncstat.evt_code,
  7866. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7867. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7868. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7869. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7870. break;
  7871. }
  7872. }
  7873. /**
  7874. * lpfc_sli_setup - SLI ring setup function
  7875. * @phba: Pointer to HBA context object.
  7876. *
  7877. * lpfc_sli_setup sets up rings of the SLI interface with
  7878. * number of iocbs per ring and iotags. This function is
  7879. * called while driver attach to the HBA and before the
  7880. * interrupts are enabled. So there is no need for locking.
  7881. *
  7882. * This function always returns 0.
  7883. **/
  7884. int
  7885. lpfc_sli_setup(struct lpfc_hba *phba)
  7886. {
  7887. int i, totiocbsize = 0;
  7888. struct lpfc_sli *psli = &phba->sli;
  7889. struct lpfc_sli_ring *pring;
  7890. psli->num_rings = MAX_CONFIGURED_RINGS;
  7891. psli->sli_flag = 0;
  7892. psli->fcp_ring = LPFC_FCP_RING;
  7893. psli->next_ring = LPFC_FCP_NEXT_RING;
  7894. psli->extra_ring = LPFC_EXTRA_RING;
  7895. psli->iocbq_lookup = NULL;
  7896. psli->iocbq_lookup_len = 0;
  7897. psli->last_iotag = 0;
  7898. for (i = 0; i < psli->num_rings; i++) {
  7899. pring = &psli->ring[i];
  7900. switch (i) {
  7901. case LPFC_FCP_RING: /* ring 0 - FCP */
  7902. /* numCiocb and numRiocb are used in config_port */
  7903. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7904. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7905. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7906. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7907. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7908. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7909. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7910. SLI3_IOCB_CMD_SIZE :
  7911. SLI2_IOCB_CMD_SIZE;
  7912. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7913. SLI3_IOCB_RSP_SIZE :
  7914. SLI2_IOCB_RSP_SIZE;
  7915. pring->iotag_ctr = 0;
  7916. pring->iotag_max =
  7917. (phba->cfg_hba_queue_depth * 2);
  7918. pring->fast_iotag = pring->iotag_max;
  7919. pring->num_mask = 0;
  7920. break;
  7921. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7922. /* numCiocb and numRiocb are used in config_port */
  7923. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7924. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7925. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7926. SLI3_IOCB_CMD_SIZE :
  7927. SLI2_IOCB_CMD_SIZE;
  7928. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7929. SLI3_IOCB_RSP_SIZE :
  7930. SLI2_IOCB_RSP_SIZE;
  7931. pring->iotag_max = phba->cfg_hba_queue_depth;
  7932. pring->num_mask = 0;
  7933. break;
  7934. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7935. /* numCiocb and numRiocb are used in config_port */
  7936. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7937. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7938. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7939. SLI3_IOCB_CMD_SIZE :
  7940. SLI2_IOCB_CMD_SIZE;
  7941. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7942. SLI3_IOCB_RSP_SIZE :
  7943. SLI2_IOCB_RSP_SIZE;
  7944. pring->fast_iotag = 0;
  7945. pring->iotag_ctr = 0;
  7946. pring->iotag_max = 4096;
  7947. pring->lpfc_sli_rcv_async_status =
  7948. lpfc_sli_async_event_handler;
  7949. pring->num_mask = LPFC_MAX_RING_MASK;
  7950. pring->prt[0].profile = 0; /* Mask 0 */
  7951. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7952. pring->prt[0].type = FC_TYPE_ELS;
  7953. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7954. lpfc_els_unsol_event;
  7955. pring->prt[1].profile = 0; /* Mask 1 */
  7956. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7957. pring->prt[1].type = FC_TYPE_ELS;
  7958. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7959. lpfc_els_unsol_event;
  7960. pring->prt[2].profile = 0; /* Mask 2 */
  7961. /* NameServer Inquiry */
  7962. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7963. /* NameServer */
  7964. pring->prt[2].type = FC_TYPE_CT;
  7965. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7966. lpfc_ct_unsol_event;
  7967. pring->prt[3].profile = 0; /* Mask 3 */
  7968. /* NameServer response */
  7969. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7970. /* NameServer */
  7971. pring->prt[3].type = FC_TYPE_CT;
  7972. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7973. lpfc_ct_unsol_event;
  7974. /* abort unsolicited sequence */
  7975. pring->prt[4].profile = 0; /* Mask 4 */
  7976. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7977. pring->prt[4].type = FC_TYPE_BLS;
  7978. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7979. lpfc_sli4_ct_abort_unsol_event;
  7980. break;
  7981. }
  7982. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7983. (pring->numRiocb * pring->sizeRiocb);
  7984. }
  7985. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7986. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7987. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7988. "SLI2 SLIM Data: x%x x%lx\n",
  7989. phba->brd_no, totiocbsize,
  7990. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7991. }
  7992. if (phba->cfg_multi_ring_support == 2)
  7993. lpfc_extra_ring_setup(phba);
  7994. return 0;
  7995. }
  7996. /**
  7997. * lpfc_sli_queue_setup - Queue initialization function
  7998. * @phba: Pointer to HBA context object.
  7999. *
  8000. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8001. * ring. This function also initializes ring indices of each ring.
  8002. * This function is called during the initialization of the SLI
  8003. * interface of an HBA.
  8004. * This function is called with no lock held and always returns
  8005. * 1.
  8006. **/
  8007. int
  8008. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8009. {
  8010. struct lpfc_sli *psli;
  8011. struct lpfc_sli_ring *pring;
  8012. int i;
  8013. psli = &phba->sli;
  8014. spin_lock_irq(&phba->hbalock);
  8015. INIT_LIST_HEAD(&psli->mboxq);
  8016. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8017. /* Initialize list headers for txq and txcmplq as double linked lists */
  8018. for (i = 0; i < psli->num_rings; i++) {
  8019. pring = &psli->ring[i];
  8020. pring->ringno = i;
  8021. pring->next_cmdidx = 0;
  8022. pring->local_getidx = 0;
  8023. pring->cmdidx = 0;
  8024. INIT_LIST_HEAD(&pring->txq);
  8025. INIT_LIST_HEAD(&pring->txcmplq);
  8026. INIT_LIST_HEAD(&pring->iocb_continueq);
  8027. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8028. INIT_LIST_HEAD(&pring->postbufq);
  8029. }
  8030. spin_unlock_irq(&phba->hbalock);
  8031. return 1;
  8032. }
  8033. /**
  8034. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8035. * @phba: Pointer to HBA context object.
  8036. *
  8037. * This routine flushes the mailbox command subsystem. It will unconditionally
  8038. * flush all the mailbox commands in the three possible stages in the mailbox
  8039. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8040. * command; and completed mailbox command queue. It is caller's responsibility
  8041. * to make sure that the driver is in the proper state to flush the mailbox
  8042. * command sub-system. Namely, the posting of mailbox commands into the
  8043. * pending mailbox command queue from the various clients must be stopped;
  8044. * either the HBA is in a state that it will never works on the outstanding
  8045. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8046. * mailbox command has been completed.
  8047. **/
  8048. static void
  8049. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8050. {
  8051. LIST_HEAD(completions);
  8052. struct lpfc_sli *psli = &phba->sli;
  8053. LPFC_MBOXQ_t *pmb;
  8054. unsigned long iflag;
  8055. /* Flush all the mailbox commands in the mbox system */
  8056. spin_lock_irqsave(&phba->hbalock, iflag);
  8057. /* The pending mailbox command queue */
  8058. list_splice_init(&phba->sli.mboxq, &completions);
  8059. /* The outstanding active mailbox command */
  8060. if (psli->mbox_active) {
  8061. list_add_tail(&psli->mbox_active->list, &completions);
  8062. psli->mbox_active = NULL;
  8063. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8064. }
  8065. /* The completed mailbox command queue */
  8066. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8067. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8068. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8069. while (!list_empty(&completions)) {
  8070. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8071. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8072. if (pmb->mbox_cmpl)
  8073. pmb->mbox_cmpl(phba, pmb);
  8074. }
  8075. }
  8076. /**
  8077. * lpfc_sli_host_down - Vport cleanup function
  8078. * @vport: Pointer to virtual port object.
  8079. *
  8080. * lpfc_sli_host_down is called to clean up the resources
  8081. * associated with a vport before destroying virtual
  8082. * port data structures.
  8083. * This function does following operations:
  8084. * - Free discovery resources associated with this virtual
  8085. * port.
  8086. * - Free iocbs associated with this virtual port in
  8087. * the txq.
  8088. * - Send abort for all iocb commands associated with this
  8089. * vport in txcmplq.
  8090. *
  8091. * This function is called with no lock held and always returns 1.
  8092. **/
  8093. int
  8094. lpfc_sli_host_down(struct lpfc_vport *vport)
  8095. {
  8096. LIST_HEAD(completions);
  8097. struct lpfc_hba *phba = vport->phba;
  8098. struct lpfc_sli *psli = &phba->sli;
  8099. struct lpfc_sli_ring *pring;
  8100. struct lpfc_iocbq *iocb, *next_iocb;
  8101. int i;
  8102. unsigned long flags = 0;
  8103. uint16_t prev_pring_flag;
  8104. lpfc_cleanup_discovery_resources(vport);
  8105. spin_lock_irqsave(&phba->hbalock, flags);
  8106. for (i = 0; i < psli->num_rings; i++) {
  8107. pring = &psli->ring[i];
  8108. prev_pring_flag = pring->flag;
  8109. /* Only slow rings */
  8110. if (pring->ringno == LPFC_ELS_RING) {
  8111. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8112. /* Set the lpfc data pending flag */
  8113. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8114. }
  8115. /*
  8116. * Error everything on the txq since these iocbs have not been
  8117. * given to the FW yet.
  8118. */
  8119. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8120. if (iocb->vport != vport)
  8121. continue;
  8122. list_move_tail(&iocb->list, &completions);
  8123. pring->txq_cnt--;
  8124. }
  8125. /* Next issue ABTS for everything on the txcmplq */
  8126. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8127. list) {
  8128. if (iocb->vport != vport)
  8129. continue;
  8130. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8131. }
  8132. pring->flag = prev_pring_flag;
  8133. }
  8134. spin_unlock_irqrestore(&phba->hbalock, flags);
  8135. /* Cancel all the IOCBs from the completions list */
  8136. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8137. IOERR_SLI_DOWN);
  8138. return 1;
  8139. }
  8140. /**
  8141. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8142. * @phba: Pointer to HBA context object.
  8143. *
  8144. * This function cleans up all iocb, buffers, mailbox commands
  8145. * while shutting down the HBA. This function is called with no
  8146. * lock held and always returns 1.
  8147. * This function does the following to cleanup driver resources:
  8148. * - Free discovery resources for each virtual port
  8149. * - Cleanup any pending fabric iocbs
  8150. * - Iterate through the iocb txq and free each entry
  8151. * in the list.
  8152. * - Free up any buffer posted to the HBA
  8153. * - Free mailbox commands in the mailbox queue.
  8154. **/
  8155. int
  8156. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8157. {
  8158. LIST_HEAD(completions);
  8159. struct lpfc_sli *psli = &phba->sli;
  8160. struct lpfc_sli_ring *pring;
  8161. struct lpfc_dmabuf *buf_ptr;
  8162. unsigned long flags = 0;
  8163. int i;
  8164. /* Shutdown the mailbox command sub-system */
  8165. lpfc_sli_mbox_sys_shutdown(phba);
  8166. lpfc_hba_down_prep(phba);
  8167. lpfc_fabric_abort_hba(phba);
  8168. spin_lock_irqsave(&phba->hbalock, flags);
  8169. for (i = 0; i < psli->num_rings; i++) {
  8170. pring = &psli->ring[i];
  8171. /* Only slow rings */
  8172. if (pring->ringno == LPFC_ELS_RING) {
  8173. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8174. /* Set the lpfc data pending flag */
  8175. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8176. }
  8177. /*
  8178. * Error everything on the txq since these iocbs have not been
  8179. * given to the FW yet.
  8180. */
  8181. list_splice_init(&pring->txq, &completions);
  8182. pring->txq_cnt = 0;
  8183. }
  8184. spin_unlock_irqrestore(&phba->hbalock, flags);
  8185. /* Cancel all the IOCBs from the completions list */
  8186. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8187. IOERR_SLI_DOWN);
  8188. spin_lock_irqsave(&phba->hbalock, flags);
  8189. list_splice_init(&phba->elsbuf, &completions);
  8190. phba->elsbuf_cnt = 0;
  8191. phba->elsbuf_prev_cnt = 0;
  8192. spin_unlock_irqrestore(&phba->hbalock, flags);
  8193. while (!list_empty(&completions)) {
  8194. list_remove_head(&completions, buf_ptr,
  8195. struct lpfc_dmabuf, list);
  8196. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8197. kfree(buf_ptr);
  8198. }
  8199. /* Return any active mbox cmds */
  8200. del_timer_sync(&psli->mbox_tmo);
  8201. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8202. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8203. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8204. return 1;
  8205. }
  8206. /**
  8207. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8208. * @srcp: Source memory pointer.
  8209. * @destp: Destination memory pointer.
  8210. * @cnt: Number of words required to be copied.
  8211. *
  8212. * This function is used for copying data between driver memory
  8213. * and the SLI memory. This function also changes the endianness
  8214. * of each word if native endianness is different from SLI
  8215. * endianness. This function can be called with or without
  8216. * lock.
  8217. **/
  8218. void
  8219. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8220. {
  8221. uint32_t *src = srcp;
  8222. uint32_t *dest = destp;
  8223. uint32_t ldata;
  8224. int i;
  8225. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8226. ldata = *src;
  8227. ldata = le32_to_cpu(ldata);
  8228. *dest = ldata;
  8229. src++;
  8230. dest++;
  8231. }
  8232. }
  8233. /**
  8234. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8235. * @srcp: Source memory pointer.
  8236. * @destp: Destination memory pointer.
  8237. * @cnt: Number of words required to be copied.
  8238. *
  8239. * This function is used for copying data between a data structure
  8240. * with big endian representation to local endianness.
  8241. * This function can be called with or without lock.
  8242. **/
  8243. void
  8244. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8245. {
  8246. uint32_t *src = srcp;
  8247. uint32_t *dest = destp;
  8248. uint32_t ldata;
  8249. int i;
  8250. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8251. ldata = *src;
  8252. ldata = be32_to_cpu(ldata);
  8253. *dest = ldata;
  8254. src++;
  8255. dest++;
  8256. }
  8257. }
  8258. /**
  8259. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8260. * @phba: Pointer to HBA context object.
  8261. * @pring: Pointer to driver SLI ring object.
  8262. * @mp: Pointer to driver buffer object.
  8263. *
  8264. * This function is called with no lock held.
  8265. * It always return zero after adding the buffer to the postbufq
  8266. * buffer list.
  8267. **/
  8268. int
  8269. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8270. struct lpfc_dmabuf *mp)
  8271. {
  8272. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8273. later */
  8274. spin_lock_irq(&phba->hbalock);
  8275. list_add_tail(&mp->list, &pring->postbufq);
  8276. pring->postbufq_cnt++;
  8277. spin_unlock_irq(&phba->hbalock);
  8278. return 0;
  8279. }
  8280. /**
  8281. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8282. * @phba: Pointer to HBA context object.
  8283. *
  8284. * When HBQ is enabled, buffers are searched based on tags. This function
  8285. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8286. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8287. * does not conflict with tags of buffer posted for unsolicited events.
  8288. * The function returns the allocated tag. The function is called with
  8289. * no locks held.
  8290. **/
  8291. uint32_t
  8292. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8293. {
  8294. spin_lock_irq(&phba->hbalock);
  8295. phba->buffer_tag_count++;
  8296. /*
  8297. * Always set the QUE_BUFTAG_BIT to distiguish between
  8298. * a tag assigned by HBQ.
  8299. */
  8300. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8301. spin_unlock_irq(&phba->hbalock);
  8302. return phba->buffer_tag_count;
  8303. }
  8304. /**
  8305. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8306. * @phba: Pointer to HBA context object.
  8307. * @pring: Pointer to driver SLI ring object.
  8308. * @tag: Buffer tag.
  8309. *
  8310. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8311. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8312. * iocb is posted to the response ring with the tag of the buffer.
  8313. * This function searches the pring->postbufq list using the tag
  8314. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8315. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8316. * buffer is returned to the caller else NULL is returned.
  8317. * This function is called with no lock held.
  8318. **/
  8319. struct lpfc_dmabuf *
  8320. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8321. uint32_t tag)
  8322. {
  8323. struct lpfc_dmabuf *mp, *next_mp;
  8324. struct list_head *slp = &pring->postbufq;
  8325. /* Search postbufq, from the beginning, looking for a match on tag */
  8326. spin_lock_irq(&phba->hbalock);
  8327. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8328. if (mp->buffer_tag == tag) {
  8329. list_del_init(&mp->list);
  8330. pring->postbufq_cnt--;
  8331. spin_unlock_irq(&phba->hbalock);
  8332. return mp;
  8333. }
  8334. }
  8335. spin_unlock_irq(&phba->hbalock);
  8336. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8337. "0402 Cannot find virtual addr for buffer tag on "
  8338. "ring %d Data x%lx x%p x%p x%x\n",
  8339. pring->ringno, (unsigned long) tag,
  8340. slp->next, slp->prev, pring->postbufq_cnt);
  8341. return NULL;
  8342. }
  8343. /**
  8344. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8345. * @phba: Pointer to HBA context object.
  8346. * @pring: Pointer to driver SLI ring object.
  8347. * @phys: DMA address of the buffer.
  8348. *
  8349. * This function searches the buffer list using the dma_address
  8350. * of unsolicited event to find the driver's lpfc_dmabuf object
  8351. * corresponding to the dma_address. The function returns the
  8352. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8353. * This function is called by the ct and els unsolicited event
  8354. * handlers to get the buffer associated with the unsolicited
  8355. * event.
  8356. *
  8357. * This function is called with no lock held.
  8358. **/
  8359. struct lpfc_dmabuf *
  8360. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8361. dma_addr_t phys)
  8362. {
  8363. struct lpfc_dmabuf *mp, *next_mp;
  8364. struct list_head *slp = &pring->postbufq;
  8365. /* Search postbufq, from the beginning, looking for a match on phys */
  8366. spin_lock_irq(&phba->hbalock);
  8367. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8368. if (mp->phys == phys) {
  8369. list_del_init(&mp->list);
  8370. pring->postbufq_cnt--;
  8371. spin_unlock_irq(&phba->hbalock);
  8372. return mp;
  8373. }
  8374. }
  8375. spin_unlock_irq(&phba->hbalock);
  8376. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8377. "0410 Cannot find virtual addr for mapped buf on "
  8378. "ring %d Data x%llx x%p x%p x%x\n",
  8379. pring->ringno, (unsigned long long)phys,
  8380. slp->next, slp->prev, pring->postbufq_cnt);
  8381. return NULL;
  8382. }
  8383. /**
  8384. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8385. * @phba: Pointer to HBA context object.
  8386. * @cmdiocb: Pointer to driver command iocb object.
  8387. * @rspiocb: Pointer to driver response iocb object.
  8388. *
  8389. * This function is the completion handler for the abort iocbs for
  8390. * ELS commands. This function is called from the ELS ring event
  8391. * handler with no lock held. This function frees memory resources
  8392. * associated with the abort iocb.
  8393. **/
  8394. static void
  8395. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8396. struct lpfc_iocbq *rspiocb)
  8397. {
  8398. IOCB_t *irsp = &rspiocb->iocb;
  8399. uint16_t abort_iotag, abort_context;
  8400. struct lpfc_iocbq *abort_iocb = NULL;
  8401. if (irsp->ulpStatus) {
  8402. /*
  8403. * Assume that the port already completed and returned, or
  8404. * will return the iocb. Just Log the message.
  8405. */
  8406. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8407. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8408. spin_lock_irq(&phba->hbalock);
  8409. if (phba->sli_rev < LPFC_SLI_REV4) {
  8410. if (abort_iotag != 0 &&
  8411. abort_iotag <= phba->sli.last_iotag)
  8412. abort_iocb =
  8413. phba->sli.iocbq_lookup[abort_iotag];
  8414. } else
  8415. /* For sli4 the abort_tag is the XRI,
  8416. * so the abort routine puts the iotag of the iocb
  8417. * being aborted in the context field of the abort
  8418. * IOCB.
  8419. */
  8420. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8421. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8422. "0327 Cannot abort els iocb %p "
  8423. "with tag %x context %x, abort status %x, "
  8424. "abort code %x\n",
  8425. abort_iocb, abort_iotag, abort_context,
  8426. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8427. spin_unlock_irq(&phba->hbalock);
  8428. }
  8429. lpfc_sli_release_iocbq(phba, cmdiocb);
  8430. return;
  8431. }
  8432. /**
  8433. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8434. * @phba: Pointer to HBA context object.
  8435. * @cmdiocb: Pointer to driver command iocb object.
  8436. * @rspiocb: Pointer to driver response iocb object.
  8437. *
  8438. * The function is called from SLI ring event handler with no
  8439. * lock held. This function is the completion handler for ELS commands
  8440. * which are aborted. The function frees memory resources used for
  8441. * the aborted ELS commands.
  8442. **/
  8443. static void
  8444. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8445. struct lpfc_iocbq *rspiocb)
  8446. {
  8447. IOCB_t *irsp = &rspiocb->iocb;
  8448. /* ELS cmd tag <ulpIoTag> completes */
  8449. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8450. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8451. "x%x x%x x%x\n",
  8452. irsp->ulpIoTag, irsp->ulpStatus,
  8453. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8454. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8455. lpfc_ct_free_iocb(phba, cmdiocb);
  8456. else
  8457. lpfc_els_free_iocb(phba, cmdiocb);
  8458. return;
  8459. }
  8460. /**
  8461. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8462. * @phba: Pointer to HBA context object.
  8463. * @pring: Pointer to driver SLI ring object.
  8464. * @cmdiocb: Pointer to driver command iocb object.
  8465. *
  8466. * This function issues an abort iocb for the provided command iocb down to
  8467. * the port. Other than the case the outstanding command iocb is an abort
  8468. * request, this function issues abort out unconditionally. This function is
  8469. * called with hbalock held. The function returns 0 when it fails due to
  8470. * memory allocation failure or when the command iocb is an abort request.
  8471. **/
  8472. static int
  8473. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8474. struct lpfc_iocbq *cmdiocb)
  8475. {
  8476. struct lpfc_vport *vport = cmdiocb->vport;
  8477. struct lpfc_iocbq *abtsiocbp;
  8478. IOCB_t *icmd = NULL;
  8479. IOCB_t *iabt = NULL;
  8480. int retval;
  8481. /*
  8482. * There are certain command types we don't want to abort. And we
  8483. * don't want to abort commands that are already in the process of
  8484. * being aborted.
  8485. */
  8486. icmd = &cmdiocb->iocb;
  8487. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8488. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8489. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8490. return 0;
  8491. /* issue ABTS for this IOCB based on iotag */
  8492. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8493. if (abtsiocbp == NULL)
  8494. return 0;
  8495. /* This signals the response to set the correct status
  8496. * before calling the completion handler
  8497. */
  8498. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8499. iabt = &abtsiocbp->iocb;
  8500. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8501. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8502. if (phba->sli_rev == LPFC_SLI_REV4) {
  8503. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8504. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8505. }
  8506. else
  8507. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8508. iabt->ulpLe = 1;
  8509. iabt->ulpClass = icmd->ulpClass;
  8510. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8511. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8512. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8513. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8514. if (phba->link_state >= LPFC_LINK_UP)
  8515. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8516. else
  8517. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8518. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8519. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8520. "0339 Abort xri x%x, original iotag x%x, "
  8521. "abort cmd iotag x%x\n",
  8522. iabt->un.acxri.abortIoTag,
  8523. iabt->un.acxri.abortContextTag,
  8524. abtsiocbp->iotag);
  8525. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8526. if (retval)
  8527. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8528. /*
  8529. * Caller to this routine should check for IOCB_ERROR
  8530. * and handle it properly. This routine no longer removes
  8531. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8532. */
  8533. return retval;
  8534. }
  8535. /**
  8536. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8537. * @phba: Pointer to HBA context object.
  8538. * @pring: Pointer to driver SLI ring object.
  8539. * @cmdiocb: Pointer to driver command iocb object.
  8540. *
  8541. * This function issues an abort iocb for the provided command iocb. In case
  8542. * of unloading, the abort iocb will not be issued to commands on the ELS
  8543. * ring. Instead, the callback function shall be changed to those commands
  8544. * so that nothing happens when them finishes. This function is called with
  8545. * hbalock held. The function returns 0 when the command iocb is an abort
  8546. * request.
  8547. **/
  8548. int
  8549. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8550. struct lpfc_iocbq *cmdiocb)
  8551. {
  8552. struct lpfc_vport *vport = cmdiocb->vport;
  8553. int retval = IOCB_ERROR;
  8554. IOCB_t *icmd = NULL;
  8555. /*
  8556. * There are certain command types we don't want to abort. And we
  8557. * don't want to abort commands that are already in the process of
  8558. * being aborted.
  8559. */
  8560. icmd = &cmdiocb->iocb;
  8561. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8562. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8563. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8564. return 0;
  8565. /*
  8566. * If we're unloading, don't abort iocb on the ELS ring, but change
  8567. * the callback so that nothing happens when it finishes.
  8568. */
  8569. if ((vport->load_flag & FC_UNLOADING) &&
  8570. (pring->ringno == LPFC_ELS_RING)) {
  8571. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8572. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8573. else
  8574. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8575. goto abort_iotag_exit;
  8576. }
  8577. /* Now, we try to issue the abort to the cmdiocb out */
  8578. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8579. abort_iotag_exit:
  8580. /*
  8581. * Caller to this routine should check for IOCB_ERROR
  8582. * and handle it properly. This routine no longer removes
  8583. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8584. */
  8585. return retval;
  8586. }
  8587. /**
  8588. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8589. * @phba: Pointer to HBA context object.
  8590. * @pring: Pointer to driver SLI ring object.
  8591. *
  8592. * This function aborts all iocbs in the given ring and frees all the iocb
  8593. * objects in txq. This function issues abort iocbs unconditionally for all
  8594. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8595. * to complete before the return of this function. The caller is not required
  8596. * to hold any locks.
  8597. **/
  8598. static void
  8599. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8600. {
  8601. LIST_HEAD(completions);
  8602. struct lpfc_iocbq *iocb, *next_iocb;
  8603. if (pring->ringno == LPFC_ELS_RING)
  8604. lpfc_fabric_abort_hba(phba);
  8605. spin_lock_irq(&phba->hbalock);
  8606. /* Take off all the iocbs on txq for cancelling */
  8607. list_splice_init(&pring->txq, &completions);
  8608. pring->txq_cnt = 0;
  8609. /* Next issue ABTS for everything on the txcmplq */
  8610. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8611. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8612. spin_unlock_irq(&phba->hbalock);
  8613. /* Cancel all the IOCBs from the completions list */
  8614. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8615. IOERR_SLI_ABORTED);
  8616. }
  8617. /**
  8618. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8619. * @phba: pointer to lpfc HBA data structure.
  8620. *
  8621. * This routine will abort all pending and outstanding iocbs to an HBA.
  8622. **/
  8623. void
  8624. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8625. {
  8626. struct lpfc_sli *psli = &phba->sli;
  8627. struct lpfc_sli_ring *pring;
  8628. int i;
  8629. for (i = 0; i < psli->num_rings; i++) {
  8630. pring = &psli->ring[i];
  8631. lpfc_sli_iocb_ring_abort(phba, pring);
  8632. }
  8633. }
  8634. /**
  8635. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8636. * @iocbq: Pointer to driver iocb object.
  8637. * @vport: Pointer to driver virtual port object.
  8638. * @tgt_id: SCSI ID of the target.
  8639. * @lun_id: LUN ID of the scsi device.
  8640. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8641. *
  8642. * This function acts as an iocb filter for functions which abort or count
  8643. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8644. * 0 if the filtering criteria is met for the given iocb and will return
  8645. * 1 if the filtering criteria is not met.
  8646. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8647. * given iocb is for the SCSI device specified by vport, tgt_id and
  8648. * lun_id parameter.
  8649. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8650. * given iocb is for the SCSI target specified by vport and tgt_id
  8651. * parameters.
  8652. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8653. * given iocb is for the SCSI host associated with the given vport.
  8654. * This function is called with no locks held.
  8655. **/
  8656. static int
  8657. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8658. uint16_t tgt_id, uint64_t lun_id,
  8659. lpfc_ctx_cmd ctx_cmd)
  8660. {
  8661. struct lpfc_scsi_buf *lpfc_cmd;
  8662. int rc = 1;
  8663. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8664. return rc;
  8665. if (iocbq->vport != vport)
  8666. return rc;
  8667. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8668. if (lpfc_cmd->pCmd == NULL)
  8669. return rc;
  8670. switch (ctx_cmd) {
  8671. case LPFC_CTX_LUN:
  8672. if ((lpfc_cmd->rdata->pnode) &&
  8673. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8674. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8675. rc = 0;
  8676. break;
  8677. case LPFC_CTX_TGT:
  8678. if ((lpfc_cmd->rdata->pnode) &&
  8679. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8680. rc = 0;
  8681. break;
  8682. case LPFC_CTX_HOST:
  8683. rc = 0;
  8684. break;
  8685. default:
  8686. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8687. __func__, ctx_cmd);
  8688. break;
  8689. }
  8690. return rc;
  8691. }
  8692. /**
  8693. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8694. * @vport: Pointer to virtual port.
  8695. * @tgt_id: SCSI ID of the target.
  8696. * @lun_id: LUN ID of the scsi device.
  8697. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8698. *
  8699. * This function returns number of FCP commands pending for the vport.
  8700. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8701. * commands pending on the vport associated with SCSI device specified
  8702. * by tgt_id and lun_id parameters.
  8703. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8704. * commands pending on the vport associated with SCSI target specified
  8705. * by tgt_id parameter.
  8706. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8707. * commands pending on the vport.
  8708. * This function returns the number of iocbs which satisfy the filter.
  8709. * This function is called without any lock held.
  8710. **/
  8711. int
  8712. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8713. lpfc_ctx_cmd ctx_cmd)
  8714. {
  8715. struct lpfc_hba *phba = vport->phba;
  8716. struct lpfc_iocbq *iocbq;
  8717. int sum, i;
  8718. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8719. iocbq = phba->sli.iocbq_lookup[i];
  8720. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8721. ctx_cmd) == 0)
  8722. sum++;
  8723. }
  8724. return sum;
  8725. }
  8726. /**
  8727. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8728. * @phba: Pointer to HBA context object
  8729. * @cmdiocb: Pointer to command iocb object.
  8730. * @rspiocb: Pointer to response iocb object.
  8731. *
  8732. * This function is called when an aborted FCP iocb completes. This
  8733. * function is called by the ring event handler with no lock held.
  8734. * This function frees the iocb.
  8735. **/
  8736. void
  8737. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8738. struct lpfc_iocbq *rspiocb)
  8739. {
  8740. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8741. "3096 ABORT_XRI_CN completing on xri x%x "
  8742. "original iotag x%x, abort cmd iotag x%x "
  8743. "status 0x%x, reason 0x%x\n",
  8744. cmdiocb->iocb.un.acxri.abortContextTag,
  8745. cmdiocb->iocb.un.acxri.abortIoTag,
  8746. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8747. rspiocb->iocb.un.ulpWord[4]);
  8748. lpfc_sli_release_iocbq(phba, cmdiocb);
  8749. return;
  8750. }
  8751. /**
  8752. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8753. * @vport: Pointer to virtual port.
  8754. * @pring: Pointer to driver SLI ring object.
  8755. * @tgt_id: SCSI ID of the target.
  8756. * @lun_id: LUN ID of the scsi device.
  8757. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8758. *
  8759. * This function sends an abort command for every SCSI command
  8760. * associated with the given virtual port pending on the ring
  8761. * filtered by lpfc_sli_validate_fcp_iocb function.
  8762. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8763. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8764. * parameters
  8765. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8766. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8767. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8768. * FCP iocbs associated with virtual port.
  8769. * This function returns number of iocbs it failed to abort.
  8770. * This function is called with no locks held.
  8771. **/
  8772. int
  8773. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8774. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8775. {
  8776. struct lpfc_hba *phba = vport->phba;
  8777. struct lpfc_iocbq *iocbq;
  8778. struct lpfc_iocbq *abtsiocb;
  8779. IOCB_t *cmd = NULL;
  8780. int errcnt = 0, ret_val = 0;
  8781. int i;
  8782. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8783. iocbq = phba->sli.iocbq_lookup[i];
  8784. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8785. abort_cmd) != 0)
  8786. continue;
  8787. /* issue ABTS for this IOCB based on iotag */
  8788. abtsiocb = lpfc_sli_get_iocbq(phba);
  8789. if (abtsiocb == NULL) {
  8790. errcnt++;
  8791. continue;
  8792. }
  8793. cmd = &iocbq->iocb;
  8794. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8795. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8796. if (phba->sli_rev == LPFC_SLI_REV4)
  8797. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8798. else
  8799. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8800. abtsiocb->iocb.ulpLe = 1;
  8801. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8802. abtsiocb->vport = phba->pport;
  8803. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8804. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8805. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8806. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8807. if (lpfc_is_link_up(phba))
  8808. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8809. else
  8810. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8811. /* Setup callback routine and issue the command. */
  8812. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8813. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8814. abtsiocb, 0);
  8815. if (ret_val == IOCB_ERROR) {
  8816. lpfc_sli_release_iocbq(phba, abtsiocb);
  8817. errcnt++;
  8818. continue;
  8819. }
  8820. }
  8821. return errcnt;
  8822. }
  8823. /**
  8824. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8825. * @phba: Pointer to HBA context object.
  8826. * @cmdiocbq: Pointer to command iocb.
  8827. * @rspiocbq: Pointer to response iocb.
  8828. *
  8829. * This function is the completion handler for iocbs issued using
  8830. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8831. * ring event handler function without any lock held. This function
  8832. * can be called from both worker thread context and interrupt
  8833. * context. This function also can be called from other thread which
  8834. * cleans up the SLI layer objects.
  8835. * This function copy the contents of the response iocb to the
  8836. * response iocb memory object provided by the caller of
  8837. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8838. * sleeps for the iocb completion.
  8839. **/
  8840. static void
  8841. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8842. struct lpfc_iocbq *cmdiocbq,
  8843. struct lpfc_iocbq *rspiocbq)
  8844. {
  8845. wait_queue_head_t *pdone_q;
  8846. unsigned long iflags;
  8847. struct lpfc_scsi_buf *lpfc_cmd;
  8848. spin_lock_irqsave(&phba->hbalock, iflags);
  8849. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8850. if (cmdiocbq->context2 && rspiocbq)
  8851. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8852. &rspiocbq->iocb, sizeof(IOCB_t));
  8853. /* Set the exchange busy flag for task management commands */
  8854. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8855. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8856. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8857. cur_iocbq);
  8858. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8859. }
  8860. pdone_q = cmdiocbq->context_un.wait_queue;
  8861. if (pdone_q)
  8862. wake_up(pdone_q);
  8863. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8864. return;
  8865. }
  8866. /**
  8867. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8868. * @phba: Pointer to HBA context object..
  8869. * @piocbq: Pointer to command iocb.
  8870. * @flag: Flag to test.
  8871. *
  8872. * This routine grabs the hbalock and then test the iocb_flag to
  8873. * see if the passed in flag is set.
  8874. * Returns:
  8875. * 1 if flag is set.
  8876. * 0 if flag is not set.
  8877. **/
  8878. static int
  8879. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8880. struct lpfc_iocbq *piocbq, uint32_t flag)
  8881. {
  8882. unsigned long iflags;
  8883. int ret;
  8884. spin_lock_irqsave(&phba->hbalock, iflags);
  8885. ret = piocbq->iocb_flag & flag;
  8886. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8887. return ret;
  8888. }
  8889. /**
  8890. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8891. * @phba: Pointer to HBA context object..
  8892. * @pring: Pointer to sli ring.
  8893. * @piocb: Pointer to command iocb.
  8894. * @prspiocbq: Pointer to response iocb.
  8895. * @timeout: Timeout in number of seconds.
  8896. *
  8897. * This function issues the iocb to firmware and waits for the
  8898. * iocb to complete. If the iocb command is not
  8899. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8900. * Caller should not free the iocb resources if this function
  8901. * returns IOCB_TIMEDOUT.
  8902. * The function waits for the iocb completion using an
  8903. * non-interruptible wait.
  8904. * This function will sleep while waiting for iocb completion.
  8905. * So, this function should not be called from any context which
  8906. * does not allow sleeping. Due to the same reason, this function
  8907. * cannot be called with interrupt disabled.
  8908. * This function assumes that the iocb completions occur while
  8909. * this function sleep. So, this function cannot be called from
  8910. * the thread which process iocb completion for this ring.
  8911. * This function clears the iocb_flag of the iocb object before
  8912. * issuing the iocb and the iocb completion handler sets this
  8913. * flag and wakes this thread when the iocb completes.
  8914. * The contents of the response iocb will be copied to prspiocbq
  8915. * by the completion handler when the command completes.
  8916. * This function returns IOCB_SUCCESS when success.
  8917. * This function is called with no lock held.
  8918. **/
  8919. int
  8920. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8921. uint32_t ring_number,
  8922. struct lpfc_iocbq *piocb,
  8923. struct lpfc_iocbq *prspiocbq,
  8924. uint32_t timeout)
  8925. {
  8926. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8927. long timeleft, timeout_req = 0;
  8928. int retval = IOCB_SUCCESS;
  8929. uint32_t creg_val;
  8930. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8931. /*
  8932. * If the caller has provided a response iocbq buffer, then context2
  8933. * is NULL or its an error.
  8934. */
  8935. if (prspiocbq) {
  8936. if (piocb->context2)
  8937. return IOCB_ERROR;
  8938. piocb->context2 = prspiocbq;
  8939. }
  8940. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8941. piocb->context_un.wait_queue = &done_q;
  8942. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8943. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8944. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8945. return IOCB_ERROR;
  8946. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8947. writel(creg_val, phba->HCregaddr);
  8948. readl(phba->HCregaddr); /* flush */
  8949. }
  8950. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8951. SLI_IOCB_RET_IOCB);
  8952. if (retval == IOCB_SUCCESS) {
  8953. timeout_req = timeout * HZ;
  8954. timeleft = wait_event_timeout(done_q,
  8955. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8956. timeout_req);
  8957. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8958. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8959. "0331 IOCB wake signaled\n");
  8960. } else if (timeleft == 0) {
  8961. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8962. "0338 IOCB wait timeout error - no "
  8963. "wake response Data x%x\n", timeout);
  8964. retval = IOCB_TIMEDOUT;
  8965. } else {
  8966. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8967. "0330 IOCB wake NOT set, "
  8968. "Data x%x x%lx\n",
  8969. timeout, (timeleft / jiffies));
  8970. retval = IOCB_TIMEDOUT;
  8971. }
  8972. } else if (retval == IOCB_BUSY) {
  8973. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8974. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8975. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8976. return retval;
  8977. } else {
  8978. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8979. "0332 IOCB wait issue failed, Data x%x\n",
  8980. retval);
  8981. retval = IOCB_ERROR;
  8982. }
  8983. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8984. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8985. return IOCB_ERROR;
  8986. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8987. writel(creg_val, phba->HCregaddr);
  8988. readl(phba->HCregaddr); /* flush */
  8989. }
  8990. if (prspiocbq)
  8991. piocb->context2 = NULL;
  8992. piocb->context_un.wait_queue = NULL;
  8993. piocb->iocb_cmpl = NULL;
  8994. return retval;
  8995. }
  8996. /**
  8997. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8998. * @phba: Pointer to HBA context object.
  8999. * @pmboxq: Pointer to driver mailbox object.
  9000. * @timeout: Timeout in number of seconds.
  9001. *
  9002. * This function issues the mailbox to firmware and waits for the
  9003. * mailbox command to complete. If the mailbox command is not
  9004. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9005. * The function waits for the mailbox completion using an
  9006. * interruptible wait. If the thread is woken up due to a
  9007. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9008. * should not free the mailbox resources, if this function returns
  9009. * MBX_TIMEOUT.
  9010. * This function will sleep while waiting for mailbox completion.
  9011. * So, this function should not be called from any context which
  9012. * does not allow sleeping. Due to the same reason, this function
  9013. * cannot be called with interrupt disabled.
  9014. * This function assumes that the mailbox completion occurs while
  9015. * this function sleep. So, this function cannot be called from
  9016. * the worker thread which processes mailbox completion.
  9017. * This function is called in the context of HBA management
  9018. * applications.
  9019. * This function returns MBX_SUCCESS when successful.
  9020. * This function is called with no lock held.
  9021. **/
  9022. int
  9023. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9024. uint32_t timeout)
  9025. {
  9026. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9027. int retval;
  9028. unsigned long flag;
  9029. /* The caller must leave context1 empty. */
  9030. if (pmboxq->context1)
  9031. return MBX_NOT_FINISHED;
  9032. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9033. /* setup wake call as IOCB callback */
  9034. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9035. /* setup context field to pass wait_queue pointer to wake function */
  9036. pmboxq->context1 = &done_q;
  9037. /* now issue the command */
  9038. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9039. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9040. wait_event_interruptible_timeout(done_q,
  9041. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9042. timeout * HZ);
  9043. spin_lock_irqsave(&phba->hbalock, flag);
  9044. pmboxq->context1 = NULL;
  9045. /*
  9046. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9047. * else do not free the resources.
  9048. */
  9049. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9050. retval = MBX_SUCCESS;
  9051. lpfc_sli4_swap_str(phba, pmboxq);
  9052. } else {
  9053. retval = MBX_TIMEOUT;
  9054. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9055. }
  9056. spin_unlock_irqrestore(&phba->hbalock, flag);
  9057. }
  9058. return retval;
  9059. }
  9060. /**
  9061. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9062. * @phba: Pointer to HBA context.
  9063. *
  9064. * This function is called to shutdown the driver's mailbox sub-system.
  9065. * It first marks the mailbox sub-system is in a block state to prevent
  9066. * the asynchronous mailbox command from issued off the pending mailbox
  9067. * command queue. If the mailbox command sub-system shutdown is due to
  9068. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9069. * the mailbox sub-system flush routine to forcefully bring down the
  9070. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9071. * as with offline or HBA function reset), this routine will wait for the
  9072. * outstanding mailbox command to complete before invoking the mailbox
  9073. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9074. **/
  9075. void
  9076. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  9077. {
  9078. struct lpfc_sli *psli = &phba->sli;
  9079. unsigned long timeout;
  9080. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9081. spin_lock_irq(&phba->hbalock);
  9082. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9083. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9084. /* Determine how long we might wait for the active mailbox
  9085. * command to be gracefully completed by firmware.
  9086. */
  9087. if (phba->sli.mbox_active)
  9088. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9089. phba->sli.mbox_active) *
  9090. 1000) + jiffies;
  9091. spin_unlock_irq(&phba->hbalock);
  9092. while (phba->sli.mbox_active) {
  9093. /* Check active mailbox complete status every 2ms */
  9094. msleep(2);
  9095. if (time_after(jiffies, timeout))
  9096. /* Timeout, let the mailbox flush routine to
  9097. * forcefully release active mailbox command
  9098. */
  9099. break;
  9100. }
  9101. } else
  9102. spin_unlock_irq(&phba->hbalock);
  9103. lpfc_sli_mbox_sys_flush(phba);
  9104. }
  9105. /**
  9106. * lpfc_sli_eratt_read - read sli-3 error attention events
  9107. * @phba: Pointer to HBA context.
  9108. *
  9109. * This function is called to read the SLI3 device error attention registers
  9110. * for possible error attention events. The caller must hold the hostlock
  9111. * with spin_lock_irq().
  9112. *
  9113. * This function returns 1 when there is Error Attention in the Host Attention
  9114. * Register and returns 0 otherwise.
  9115. **/
  9116. static int
  9117. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9118. {
  9119. uint32_t ha_copy;
  9120. /* Read chip Host Attention (HA) register */
  9121. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9122. goto unplug_err;
  9123. if (ha_copy & HA_ERATT) {
  9124. /* Read host status register to retrieve error event */
  9125. if (lpfc_sli_read_hs(phba))
  9126. goto unplug_err;
  9127. /* Check if there is a deferred error condition is active */
  9128. if ((HS_FFER1 & phba->work_hs) &&
  9129. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9130. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9131. phba->hba_flag |= DEFER_ERATT;
  9132. /* Clear all interrupt enable conditions */
  9133. writel(0, phba->HCregaddr);
  9134. readl(phba->HCregaddr);
  9135. }
  9136. /* Set the driver HA work bitmap */
  9137. phba->work_ha |= HA_ERATT;
  9138. /* Indicate polling handles this ERATT */
  9139. phba->hba_flag |= HBA_ERATT_HANDLED;
  9140. return 1;
  9141. }
  9142. return 0;
  9143. unplug_err:
  9144. /* Set the driver HS work bitmap */
  9145. phba->work_hs |= UNPLUG_ERR;
  9146. /* Set the driver HA work bitmap */
  9147. phba->work_ha |= HA_ERATT;
  9148. /* Indicate polling handles this ERATT */
  9149. phba->hba_flag |= HBA_ERATT_HANDLED;
  9150. return 1;
  9151. }
  9152. /**
  9153. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9154. * @phba: Pointer to HBA context.
  9155. *
  9156. * This function is called to read the SLI4 device error attention registers
  9157. * for possible error attention events. The caller must hold the hostlock
  9158. * with spin_lock_irq().
  9159. *
  9160. * This function returns 1 when there is Error Attention in the Host Attention
  9161. * Register and returns 0 otherwise.
  9162. **/
  9163. static int
  9164. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9165. {
  9166. uint32_t uerr_sta_hi, uerr_sta_lo;
  9167. uint32_t if_type, portsmphr;
  9168. struct lpfc_register portstat_reg;
  9169. /*
  9170. * For now, use the SLI4 device internal unrecoverable error
  9171. * registers for error attention. This can be changed later.
  9172. */
  9173. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9174. switch (if_type) {
  9175. case LPFC_SLI_INTF_IF_TYPE_0:
  9176. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9177. &uerr_sta_lo) ||
  9178. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9179. &uerr_sta_hi)) {
  9180. phba->work_hs |= UNPLUG_ERR;
  9181. phba->work_ha |= HA_ERATT;
  9182. phba->hba_flag |= HBA_ERATT_HANDLED;
  9183. return 1;
  9184. }
  9185. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9186. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9187. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9188. "1423 HBA Unrecoverable error: "
  9189. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9190. "ue_mask_lo_reg=0x%x, "
  9191. "ue_mask_hi_reg=0x%x\n",
  9192. uerr_sta_lo, uerr_sta_hi,
  9193. phba->sli4_hba.ue_mask_lo,
  9194. phba->sli4_hba.ue_mask_hi);
  9195. phba->work_status[0] = uerr_sta_lo;
  9196. phba->work_status[1] = uerr_sta_hi;
  9197. phba->work_ha |= HA_ERATT;
  9198. phba->hba_flag |= HBA_ERATT_HANDLED;
  9199. return 1;
  9200. }
  9201. break;
  9202. case LPFC_SLI_INTF_IF_TYPE_2:
  9203. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9204. &portstat_reg.word0) ||
  9205. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9206. &portsmphr)){
  9207. phba->work_hs |= UNPLUG_ERR;
  9208. phba->work_ha |= HA_ERATT;
  9209. phba->hba_flag |= HBA_ERATT_HANDLED;
  9210. return 1;
  9211. }
  9212. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9213. phba->work_status[0] =
  9214. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9215. phba->work_status[1] =
  9216. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9217. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9218. "2885 Port Status Event: "
  9219. "port status reg 0x%x, "
  9220. "port smphr reg 0x%x, "
  9221. "error 1=0x%x, error 2=0x%x\n",
  9222. portstat_reg.word0,
  9223. portsmphr,
  9224. phba->work_status[0],
  9225. phba->work_status[1]);
  9226. phba->work_ha |= HA_ERATT;
  9227. phba->hba_flag |= HBA_ERATT_HANDLED;
  9228. return 1;
  9229. }
  9230. break;
  9231. case LPFC_SLI_INTF_IF_TYPE_1:
  9232. default:
  9233. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9234. "2886 HBA Error Attention on unsupported "
  9235. "if type %d.", if_type);
  9236. return 1;
  9237. }
  9238. return 0;
  9239. }
  9240. /**
  9241. * lpfc_sli_check_eratt - check error attention events
  9242. * @phba: Pointer to HBA context.
  9243. *
  9244. * This function is called from timer soft interrupt context to check HBA's
  9245. * error attention register bit for error attention events.
  9246. *
  9247. * This function returns 1 when there is Error Attention in the Host Attention
  9248. * Register and returns 0 otherwise.
  9249. **/
  9250. int
  9251. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9252. {
  9253. uint32_t ha_copy;
  9254. /* If somebody is waiting to handle an eratt, don't process it
  9255. * here. The brdkill function will do this.
  9256. */
  9257. if (phba->link_flag & LS_IGNORE_ERATT)
  9258. return 0;
  9259. /* Check if interrupt handler handles this ERATT */
  9260. spin_lock_irq(&phba->hbalock);
  9261. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9262. /* Interrupt handler has handled ERATT */
  9263. spin_unlock_irq(&phba->hbalock);
  9264. return 0;
  9265. }
  9266. /*
  9267. * If there is deferred error attention, do not check for error
  9268. * attention
  9269. */
  9270. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9271. spin_unlock_irq(&phba->hbalock);
  9272. return 0;
  9273. }
  9274. /* If PCI channel is offline, don't process it */
  9275. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9276. spin_unlock_irq(&phba->hbalock);
  9277. return 0;
  9278. }
  9279. switch (phba->sli_rev) {
  9280. case LPFC_SLI_REV2:
  9281. case LPFC_SLI_REV3:
  9282. /* Read chip Host Attention (HA) register */
  9283. ha_copy = lpfc_sli_eratt_read(phba);
  9284. break;
  9285. case LPFC_SLI_REV4:
  9286. /* Read device Uncoverable Error (UERR) registers */
  9287. ha_copy = lpfc_sli4_eratt_read(phba);
  9288. break;
  9289. default:
  9290. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9291. "0299 Invalid SLI revision (%d)\n",
  9292. phba->sli_rev);
  9293. ha_copy = 0;
  9294. break;
  9295. }
  9296. spin_unlock_irq(&phba->hbalock);
  9297. return ha_copy;
  9298. }
  9299. /**
  9300. * lpfc_intr_state_check - Check device state for interrupt handling
  9301. * @phba: Pointer to HBA context.
  9302. *
  9303. * This inline routine checks whether a device or its PCI slot is in a state
  9304. * that the interrupt should be handled.
  9305. *
  9306. * This function returns 0 if the device or the PCI slot is in a state that
  9307. * interrupt should be handled, otherwise -EIO.
  9308. */
  9309. static inline int
  9310. lpfc_intr_state_check(struct lpfc_hba *phba)
  9311. {
  9312. /* If the pci channel is offline, ignore all the interrupts */
  9313. if (unlikely(pci_channel_offline(phba->pcidev)))
  9314. return -EIO;
  9315. /* Update device level interrupt statistics */
  9316. phba->sli.slistat.sli_intr++;
  9317. /* Ignore all interrupts during initialization. */
  9318. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9319. return -EIO;
  9320. return 0;
  9321. }
  9322. /**
  9323. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9324. * @irq: Interrupt number.
  9325. * @dev_id: The device context pointer.
  9326. *
  9327. * This function is directly called from the PCI layer as an interrupt
  9328. * service routine when device with SLI-3 interface spec is enabled with
  9329. * MSI-X multi-message interrupt mode and there are slow-path events in
  9330. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9331. * interrupt mode, this function is called as part of the device-level
  9332. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9333. * is undergoing initialization, the interrupt handler will not process
  9334. * the interrupt. The link attention and ELS ring attention events are
  9335. * handled by the worker thread. The interrupt handler signals the worker
  9336. * thread and returns for these events. This function is called without
  9337. * any lock held. It gets the hbalock to access and update SLI data
  9338. * structures.
  9339. *
  9340. * This function returns IRQ_HANDLED when interrupt is handled else it
  9341. * returns IRQ_NONE.
  9342. **/
  9343. irqreturn_t
  9344. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9345. {
  9346. struct lpfc_hba *phba;
  9347. uint32_t ha_copy, hc_copy;
  9348. uint32_t work_ha_copy;
  9349. unsigned long status;
  9350. unsigned long iflag;
  9351. uint32_t control;
  9352. MAILBOX_t *mbox, *pmbox;
  9353. struct lpfc_vport *vport;
  9354. struct lpfc_nodelist *ndlp;
  9355. struct lpfc_dmabuf *mp;
  9356. LPFC_MBOXQ_t *pmb;
  9357. int rc;
  9358. /*
  9359. * Get the driver's phba structure from the dev_id and
  9360. * assume the HBA is not interrupting.
  9361. */
  9362. phba = (struct lpfc_hba *)dev_id;
  9363. if (unlikely(!phba))
  9364. return IRQ_NONE;
  9365. /*
  9366. * Stuff needs to be attented to when this function is invoked as an
  9367. * individual interrupt handler in MSI-X multi-message interrupt mode
  9368. */
  9369. if (phba->intr_type == MSIX) {
  9370. /* Check device state for handling interrupt */
  9371. if (lpfc_intr_state_check(phba))
  9372. return IRQ_NONE;
  9373. /* Need to read HA REG for slow-path events */
  9374. spin_lock_irqsave(&phba->hbalock, iflag);
  9375. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9376. goto unplug_error;
  9377. /* If somebody is waiting to handle an eratt don't process it
  9378. * here. The brdkill function will do this.
  9379. */
  9380. if (phba->link_flag & LS_IGNORE_ERATT)
  9381. ha_copy &= ~HA_ERATT;
  9382. /* Check the need for handling ERATT in interrupt handler */
  9383. if (ha_copy & HA_ERATT) {
  9384. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9385. /* ERATT polling has handled ERATT */
  9386. ha_copy &= ~HA_ERATT;
  9387. else
  9388. /* Indicate interrupt handler handles ERATT */
  9389. phba->hba_flag |= HBA_ERATT_HANDLED;
  9390. }
  9391. /*
  9392. * If there is deferred error attention, do not check for any
  9393. * interrupt.
  9394. */
  9395. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9396. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9397. return IRQ_NONE;
  9398. }
  9399. /* Clear up only attention source related to slow-path */
  9400. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9401. goto unplug_error;
  9402. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9403. HC_LAINT_ENA | HC_ERINT_ENA),
  9404. phba->HCregaddr);
  9405. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9406. phba->HAregaddr);
  9407. writel(hc_copy, phba->HCregaddr);
  9408. readl(phba->HAregaddr); /* flush */
  9409. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9410. } else
  9411. ha_copy = phba->ha_copy;
  9412. work_ha_copy = ha_copy & phba->work_ha_mask;
  9413. if (work_ha_copy) {
  9414. if (work_ha_copy & HA_LATT) {
  9415. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9416. /*
  9417. * Turn off Link Attention interrupts
  9418. * until CLEAR_LA done
  9419. */
  9420. spin_lock_irqsave(&phba->hbalock, iflag);
  9421. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9422. if (lpfc_readl(phba->HCregaddr, &control))
  9423. goto unplug_error;
  9424. control &= ~HC_LAINT_ENA;
  9425. writel(control, phba->HCregaddr);
  9426. readl(phba->HCregaddr); /* flush */
  9427. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9428. }
  9429. else
  9430. work_ha_copy &= ~HA_LATT;
  9431. }
  9432. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9433. /*
  9434. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9435. * the only slow ring.
  9436. */
  9437. status = (work_ha_copy &
  9438. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9439. status >>= (4*LPFC_ELS_RING);
  9440. if (status & HA_RXMASK) {
  9441. spin_lock_irqsave(&phba->hbalock, iflag);
  9442. if (lpfc_readl(phba->HCregaddr, &control))
  9443. goto unplug_error;
  9444. lpfc_debugfs_slow_ring_trc(phba,
  9445. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9446. control, status,
  9447. (uint32_t)phba->sli.slistat.sli_intr);
  9448. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9449. lpfc_debugfs_slow_ring_trc(phba,
  9450. "ISR Disable ring:"
  9451. "pwork:x%x hawork:x%x wait:x%x",
  9452. phba->work_ha, work_ha_copy,
  9453. (uint32_t)((unsigned long)
  9454. &phba->work_waitq));
  9455. control &=
  9456. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9457. writel(control, phba->HCregaddr);
  9458. readl(phba->HCregaddr); /* flush */
  9459. }
  9460. else {
  9461. lpfc_debugfs_slow_ring_trc(phba,
  9462. "ISR slow ring: pwork:"
  9463. "x%x hawork:x%x wait:x%x",
  9464. phba->work_ha, work_ha_copy,
  9465. (uint32_t)((unsigned long)
  9466. &phba->work_waitq));
  9467. }
  9468. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9469. }
  9470. }
  9471. spin_lock_irqsave(&phba->hbalock, iflag);
  9472. if (work_ha_copy & HA_ERATT) {
  9473. if (lpfc_sli_read_hs(phba))
  9474. goto unplug_error;
  9475. /*
  9476. * Check if there is a deferred error condition
  9477. * is active
  9478. */
  9479. if ((HS_FFER1 & phba->work_hs) &&
  9480. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9481. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9482. phba->work_hs)) {
  9483. phba->hba_flag |= DEFER_ERATT;
  9484. /* Clear all interrupt enable conditions */
  9485. writel(0, phba->HCregaddr);
  9486. readl(phba->HCregaddr);
  9487. }
  9488. }
  9489. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9490. pmb = phba->sli.mbox_active;
  9491. pmbox = &pmb->u.mb;
  9492. mbox = phba->mbox;
  9493. vport = pmb->vport;
  9494. /* First check out the status word */
  9495. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9496. if (pmbox->mbxOwner != OWN_HOST) {
  9497. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9498. /*
  9499. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9500. * mbxStatus <status>
  9501. */
  9502. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9503. LOG_SLI,
  9504. "(%d):0304 Stray Mailbox "
  9505. "Interrupt mbxCommand x%x "
  9506. "mbxStatus x%x\n",
  9507. (vport ? vport->vpi : 0),
  9508. pmbox->mbxCommand,
  9509. pmbox->mbxStatus);
  9510. /* clear mailbox attention bit */
  9511. work_ha_copy &= ~HA_MBATT;
  9512. } else {
  9513. phba->sli.mbox_active = NULL;
  9514. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9515. phba->last_completion_time = jiffies;
  9516. del_timer(&phba->sli.mbox_tmo);
  9517. if (pmb->mbox_cmpl) {
  9518. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9519. MAILBOX_CMD_SIZE);
  9520. if (pmb->out_ext_byte_len &&
  9521. pmb->context2)
  9522. lpfc_sli_pcimem_bcopy(
  9523. phba->mbox_ext,
  9524. pmb->context2,
  9525. pmb->out_ext_byte_len);
  9526. }
  9527. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9528. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9529. lpfc_debugfs_disc_trc(vport,
  9530. LPFC_DISC_TRC_MBOX_VPORT,
  9531. "MBOX dflt rpi: : "
  9532. "status:x%x rpi:x%x",
  9533. (uint32_t)pmbox->mbxStatus,
  9534. pmbox->un.varWords[0], 0);
  9535. if (!pmbox->mbxStatus) {
  9536. mp = (struct lpfc_dmabuf *)
  9537. (pmb->context1);
  9538. ndlp = (struct lpfc_nodelist *)
  9539. pmb->context2;
  9540. /* Reg_LOGIN of dflt RPI was
  9541. * successful. new lets get
  9542. * rid of the RPI using the
  9543. * same mbox buffer.
  9544. */
  9545. lpfc_unreg_login(phba,
  9546. vport->vpi,
  9547. pmbox->un.varWords[0],
  9548. pmb);
  9549. pmb->mbox_cmpl =
  9550. lpfc_mbx_cmpl_dflt_rpi;
  9551. pmb->context1 = mp;
  9552. pmb->context2 = ndlp;
  9553. pmb->vport = vport;
  9554. rc = lpfc_sli_issue_mbox(phba,
  9555. pmb,
  9556. MBX_NOWAIT);
  9557. if (rc != MBX_BUSY)
  9558. lpfc_printf_log(phba,
  9559. KERN_ERR,
  9560. LOG_MBOX | LOG_SLI,
  9561. "0350 rc should have"
  9562. "been MBX_BUSY\n");
  9563. if (rc != MBX_NOT_FINISHED)
  9564. goto send_current_mbox;
  9565. }
  9566. }
  9567. spin_lock_irqsave(
  9568. &phba->pport->work_port_lock,
  9569. iflag);
  9570. phba->pport->work_port_events &=
  9571. ~WORKER_MBOX_TMO;
  9572. spin_unlock_irqrestore(
  9573. &phba->pport->work_port_lock,
  9574. iflag);
  9575. lpfc_mbox_cmpl_put(phba, pmb);
  9576. }
  9577. } else
  9578. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9579. if ((work_ha_copy & HA_MBATT) &&
  9580. (phba->sli.mbox_active == NULL)) {
  9581. send_current_mbox:
  9582. /* Process next mailbox command if there is one */
  9583. do {
  9584. rc = lpfc_sli_issue_mbox(phba, NULL,
  9585. MBX_NOWAIT);
  9586. } while (rc == MBX_NOT_FINISHED);
  9587. if (rc != MBX_SUCCESS)
  9588. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9589. LOG_SLI, "0349 rc should be "
  9590. "MBX_SUCCESS\n");
  9591. }
  9592. spin_lock_irqsave(&phba->hbalock, iflag);
  9593. phba->work_ha |= work_ha_copy;
  9594. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9595. lpfc_worker_wake_up(phba);
  9596. }
  9597. return IRQ_HANDLED;
  9598. unplug_error:
  9599. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9600. return IRQ_HANDLED;
  9601. } /* lpfc_sli_sp_intr_handler */
  9602. /**
  9603. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9604. * @irq: Interrupt number.
  9605. * @dev_id: The device context pointer.
  9606. *
  9607. * This function is directly called from the PCI layer as an interrupt
  9608. * service routine when device with SLI-3 interface spec is enabled with
  9609. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9610. * ring event in the HBA. However, when the device is enabled with either
  9611. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9612. * device-level interrupt handler. When the PCI slot is in error recovery
  9613. * or the HBA is undergoing initialization, the interrupt handler will not
  9614. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9615. * the intrrupt context. This function is called without any lock held.
  9616. * It gets the hbalock to access and update SLI data structures.
  9617. *
  9618. * This function returns IRQ_HANDLED when interrupt is handled else it
  9619. * returns IRQ_NONE.
  9620. **/
  9621. irqreturn_t
  9622. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9623. {
  9624. struct lpfc_hba *phba;
  9625. uint32_t ha_copy;
  9626. unsigned long status;
  9627. unsigned long iflag;
  9628. /* Get the driver's phba structure from the dev_id and
  9629. * assume the HBA is not interrupting.
  9630. */
  9631. phba = (struct lpfc_hba *) dev_id;
  9632. if (unlikely(!phba))
  9633. return IRQ_NONE;
  9634. /*
  9635. * Stuff needs to be attented to when this function is invoked as an
  9636. * individual interrupt handler in MSI-X multi-message interrupt mode
  9637. */
  9638. if (phba->intr_type == MSIX) {
  9639. /* Check device state for handling interrupt */
  9640. if (lpfc_intr_state_check(phba))
  9641. return IRQ_NONE;
  9642. /* Need to read HA REG for FCP ring and other ring events */
  9643. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9644. return IRQ_HANDLED;
  9645. /* Clear up only attention source related to fast-path */
  9646. spin_lock_irqsave(&phba->hbalock, iflag);
  9647. /*
  9648. * If there is deferred error attention, do not check for
  9649. * any interrupt.
  9650. */
  9651. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9652. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9653. return IRQ_NONE;
  9654. }
  9655. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9656. phba->HAregaddr);
  9657. readl(phba->HAregaddr); /* flush */
  9658. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9659. } else
  9660. ha_copy = phba->ha_copy;
  9661. /*
  9662. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9663. */
  9664. ha_copy &= ~(phba->work_ha_mask);
  9665. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9666. status >>= (4*LPFC_FCP_RING);
  9667. if (status & HA_RXMASK)
  9668. lpfc_sli_handle_fast_ring_event(phba,
  9669. &phba->sli.ring[LPFC_FCP_RING],
  9670. status);
  9671. if (phba->cfg_multi_ring_support == 2) {
  9672. /*
  9673. * Process all events on extra ring. Take the optimized path
  9674. * for extra ring IO.
  9675. */
  9676. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9677. status >>= (4*LPFC_EXTRA_RING);
  9678. if (status & HA_RXMASK) {
  9679. lpfc_sli_handle_fast_ring_event(phba,
  9680. &phba->sli.ring[LPFC_EXTRA_RING],
  9681. status);
  9682. }
  9683. }
  9684. return IRQ_HANDLED;
  9685. } /* lpfc_sli_fp_intr_handler */
  9686. /**
  9687. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9688. * @irq: Interrupt number.
  9689. * @dev_id: The device context pointer.
  9690. *
  9691. * This function is the HBA device-level interrupt handler to device with
  9692. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9693. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9694. * requires driver attention. This function invokes the slow-path interrupt
  9695. * attention handling function and fast-path interrupt attention handling
  9696. * function in turn to process the relevant HBA attention events. This
  9697. * function is called without any lock held. It gets the hbalock to access
  9698. * and update SLI data structures.
  9699. *
  9700. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9701. * returns IRQ_NONE.
  9702. **/
  9703. irqreturn_t
  9704. lpfc_sli_intr_handler(int irq, void *dev_id)
  9705. {
  9706. struct lpfc_hba *phba;
  9707. irqreturn_t sp_irq_rc, fp_irq_rc;
  9708. unsigned long status1, status2;
  9709. uint32_t hc_copy;
  9710. /*
  9711. * Get the driver's phba structure from the dev_id and
  9712. * assume the HBA is not interrupting.
  9713. */
  9714. phba = (struct lpfc_hba *) dev_id;
  9715. if (unlikely(!phba))
  9716. return IRQ_NONE;
  9717. /* Check device state for handling interrupt */
  9718. if (lpfc_intr_state_check(phba))
  9719. return IRQ_NONE;
  9720. spin_lock(&phba->hbalock);
  9721. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9722. spin_unlock(&phba->hbalock);
  9723. return IRQ_HANDLED;
  9724. }
  9725. if (unlikely(!phba->ha_copy)) {
  9726. spin_unlock(&phba->hbalock);
  9727. return IRQ_NONE;
  9728. } else if (phba->ha_copy & HA_ERATT) {
  9729. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9730. /* ERATT polling has handled ERATT */
  9731. phba->ha_copy &= ~HA_ERATT;
  9732. else
  9733. /* Indicate interrupt handler handles ERATT */
  9734. phba->hba_flag |= HBA_ERATT_HANDLED;
  9735. }
  9736. /*
  9737. * If there is deferred error attention, do not check for any interrupt.
  9738. */
  9739. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9740. spin_unlock(&phba->hbalock);
  9741. return IRQ_NONE;
  9742. }
  9743. /* Clear attention sources except link and error attentions */
  9744. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9745. spin_unlock(&phba->hbalock);
  9746. return IRQ_HANDLED;
  9747. }
  9748. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9749. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9750. phba->HCregaddr);
  9751. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9752. writel(hc_copy, phba->HCregaddr);
  9753. readl(phba->HAregaddr); /* flush */
  9754. spin_unlock(&phba->hbalock);
  9755. /*
  9756. * Invokes slow-path host attention interrupt handling as appropriate.
  9757. */
  9758. /* status of events with mailbox and link attention */
  9759. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9760. /* status of events with ELS ring */
  9761. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9762. status2 >>= (4*LPFC_ELS_RING);
  9763. if (status1 || (status2 & HA_RXMASK))
  9764. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9765. else
  9766. sp_irq_rc = IRQ_NONE;
  9767. /*
  9768. * Invoke fast-path host attention interrupt handling as appropriate.
  9769. */
  9770. /* status of events with FCP ring */
  9771. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9772. status1 >>= (4*LPFC_FCP_RING);
  9773. /* status of events with extra ring */
  9774. if (phba->cfg_multi_ring_support == 2) {
  9775. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9776. status2 >>= (4*LPFC_EXTRA_RING);
  9777. } else
  9778. status2 = 0;
  9779. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9780. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9781. else
  9782. fp_irq_rc = IRQ_NONE;
  9783. /* Return device-level interrupt handling status */
  9784. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9785. } /* lpfc_sli_intr_handler */
  9786. /**
  9787. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9788. * @phba: pointer to lpfc hba data structure.
  9789. *
  9790. * This routine is invoked by the worker thread to process all the pending
  9791. * SLI4 FCP abort XRI events.
  9792. **/
  9793. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9794. {
  9795. struct lpfc_cq_event *cq_event;
  9796. /* First, declare the fcp xri abort event has been handled */
  9797. spin_lock_irq(&phba->hbalock);
  9798. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9799. spin_unlock_irq(&phba->hbalock);
  9800. /* Now, handle all the fcp xri abort events */
  9801. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9802. /* Get the first event from the head of the event queue */
  9803. spin_lock_irq(&phba->hbalock);
  9804. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9805. cq_event, struct lpfc_cq_event, list);
  9806. spin_unlock_irq(&phba->hbalock);
  9807. /* Notify aborted XRI for FCP work queue */
  9808. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9809. /* Free the event processed back to the free pool */
  9810. lpfc_sli4_cq_event_release(phba, cq_event);
  9811. }
  9812. }
  9813. /**
  9814. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9815. * @phba: pointer to lpfc hba data structure.
  9816. *
  9817. * This routine is invoked by the worker thread to process all the pending
  9818. * SLI4 els abort xri events.
  9819. **/
  9820. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9821. {
  9822. struct lpfc_cq_event *cq_event;
  9823. /* First, declare the els xri abort event has been handled */
  9824. spin_lock_irq(&phba->hbalock);
  9825. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9826. spin_unlock_irq(&phba->hbalock);
  9827. /* Now, handle all the els xri abort events */
  9828. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9829. /* Get the first event from the head of the event queue */
  9830. spin_lock_irq(&phba->hbalock);
  9831. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9832. cq_event, struct lpfc_cq_event, list);
  9833. spin_unlock_irq(&phba->hbalock);
  9834. /* Notify aborted XRI for ELS work queue */
  9835. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9836. /* Free the event processed back to the free pool */
  9837. lpfc_sli4_cq_event_release(phba, cq_event);
  9838. }
  9839. }
  9840. /**
  9841. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9842. * @phba: pointer to lpfc hba data structure
  9843. * @pIocbIn: pointer to the rspiocbq
  9844. * @pIocbOut: pointer to the cmdiocbq
  9845. * @wcqe: pointer to the complete wcqe
  9846. *
  9847. * This routine transfers the fields of a command iocbq to a response iocbq
  9848. * by copying all the IOCB fields from command iocbq and transferring the
  9849. * completion status information from the complete wcqe.
  9850. **/
  9851. static void
  9852. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9853. struct lpfc_iocbq *pIocbIn,
  9854. struct lpfc_iocbq *pIocbOut,
  9855. struct lpfc_wcqe_complete *wcqe)
  9856. {
  9857. unsigned long iflags;
  9858. uint32_t status;
  9859. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9860. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9861. sizeof(struct lpfc_iocbq) - offset);
  9862. /* Map WCQE parameters into irspiocb parameters */
  9863. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9864. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9865. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9866. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9867. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9868. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9869. wcqe->total_data_placed;
  9870. else
  9871. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9872. else {
  9873. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9874. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9875. }
  9876. /* Convert BG errors for completion status */
  9877. if (status == CQE_STATUS_DI_ERROR) {
  9878. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9879. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9880. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9881. else
  9882. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9883. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9884. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9885. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9886. BGS_GUARD_ERR_MASK;
  9887. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9888. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9889. BGS_APPTAG_ERR_MASK;
  9890. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9891. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9892. BGS_REFTAG_ERR_MASK;
  9893. /* Check to see if there was any good data before the error */
  9894. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9895. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9896. BGS_HI_WATER_MARK_PRESENT_MASK;
  9897. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9898. wcqe->total_data_placed;
  9899. }
  9900. /*
  9901. * Set ALL the error bits to indicate we don't know what
  9902. * type of error it is.
  9903. */
  9904. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9905. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9906. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9907. BGS_GUARD_ERR_MASK);
  9908. }
  9909. /* Pick up HBA exchange busy condition */
  9910. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9911. spin_lock_irqsave(&phba->hbalock, iflags);
  9912. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9913. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9914. }
  9915. }
  9916. /**
  9917. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9918. * @phba: Pointer to HBA context object.
  9919. * @wcqe: Pointer to work-queue completion queue entry.
  9920. *
  9921. * This routine handles an ELS work-queue completion event and construct
  9922. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9923. * discovery engine to handle.
  9924. *
  9925. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9926. **/
  9927. static struct lpfc_iocbq *
  9928. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9929. struct lpfc_iocbq *irspiocbq)
  9930. {
  9931. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9932. struct lpfc_iocbq *cmdiocbq;
  9933. struct lpfc_wcqe_complete *wcqe;
  9934. unsigned long iflags;
  9935. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9936. spin_lock_irqsave(&phba->hbalock, iflags);
  9937. pring->stats.iocb_event++;
  9938. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9939. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9940. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9941. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9942. if (unlikely(!cmdiocbq)) {
  9943. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9944. "0386 ELS complete with no corresponding "
  9945. "cmdiocb: iotag (%d)\n",
  9946. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9947. lpfc_sli_release_iocbq(phba, irspiocbq);
  9948. return NULL;
  9949. }
  9950. /* Fake the irspiocbq and copy necessary response information */
  9951. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9952. return irspiocbq;
  9953. }
  9954. /**
  9955. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9956. * @phba: Pointer to HBA context object.
  9957. * @cqe: Pointer to mailbox completion queue entry.
  9958. *
  9959. * This routine process a mailbox completion queue entry with asynchrous
  9960. * event.
  9961. *
  9962. * Return: true if work posted to worker thread, otherwise false.
  9963. **/
  9964. static bool
  9965. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9966. {
  9967. struct lpfc_cq_event *cq_event;
  9968. unsigned long iflags;
  9969. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9970. "0392 Async Event: word0:x%x, word1:x%x, "
  9971. "word2:x%x, word3:x%x\n", mcqe->word0,
  9972. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9973. /* Allocate a new internal CQ_EVENT entry */
  9974. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9975. if (!cq_event) {
  9976. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9977. "0394 Failed to allocate CQ_EVENT entry\n");
  9978. return false;
  9979. }
  9980. /* Move the CQE into an asynchronous event entry */
  9981. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9982. spin_lock_irqsave(&phba->hbalock, iflags);
  9983. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9984. /* Set the async event flag */
  9985. phba->hba_flag |= ASYNC_EVENT;
  9986. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9987. return true;
  9988. }
  9989. /**
  9990. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9991. * @phba: Pointer to HBA context object.
  9992. * @cqe: Pointer to mailbox completion queue entry.
  9993. *
  9994. * This routine process a mailbox completion queue entry with mailbox
  9995. * completion event.
  9996. *
  9997. * Return: true if work posted to worker thread, otherwise false.
  9998. **/
  9999. static bool
  10000. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10001. {
  10002. uint32_t mcqe_status;
  10003. MAILBOX_t *mbox, *pmbox;
  10004. struct lpfc_mqe *mqe;
  10005. struct lpfc_vport *vport;
  10006. struct lpfc_nodelist *ndlp;
  10007. struct lpfc_dmabuf *mp;
  10008. unsigned long iflags;
  10009. LPFC_MBOXQ_t *pmb;
  10010. bool workposted = false;
  10011. int rc;
  10012. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10013. if (!bf_get(lpfc_trailer_completed, mcqe))
  10014. goto out_no_mqe_complete;
  10015. /* Get the reference to the active mbox command */
  10016. spin_lock_irqsave(&phba->hbalock, iflags);
  10017. pmb = phba->sli.mbox_active;
  10018. if (unlikely(!pmb)) {
  10019. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10020. "1832 No pending MBOX command to handle\n");
  10021. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10022. goto out_no_mqe_complete;
  10023. }
  10024. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10025. mqe = &pmb->u.mqe;
  10026. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10027. mbox = phba->mbox;
  10028. vport = pmb->vport;
  10029. /* Reset heartbeat timer */
  10030. phba->last_completion_time = jiffies;
  10031. del_timer(&phba->sli.mbox_tmo);
  10032. /* Move mbox data to caller's mailbox region, do endian swapping */
  10033. if (pmb->mbox_cmpl && mbox)
  10034. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10035. /*
  10036. * For mcqe errors, conditionally move a modified error code to
  10037. * the mbox so that the error will not be missed.
  10038. */
  10039. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10040. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10041. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10042. bf_set(lpfc_mqe_status, mqe,
  10043. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10044. }
  10045. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10046. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10047. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10048. "MBOX dflt rpi: status:x%x rpi:x%x",
  10049. mcqe_status,
  10050. pmbox->un.varWords[0], 0);
  10051. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10052. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10053. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10054. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10055. * RID of the PPI using the same mbox buffer.
  10056. */
  10057. lpfc_unreg_login(phba, vport->vpi,
  10058. pmbox->un.varWords[0], pmb);
  10059. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10060. pmb->context1 = mp;
  10061. pmb->context2 = ndlp;
  10062. pmb->vport = vport;
  10063. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10064. if (rc != MBX_BUSY)
  10065. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10066. LOG_SLI, "0385 rc should "
  10067. "have been MBX_BUSY\n");
  10068. if (rc != MBX_NOT_FINISHED)
  10069. goto send_current_mbox;
  10070. }
  10071. }
  10072. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10073. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10074. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10075. /* There is mailbox completion work to do */
  10076. spin_lock_irqsave(&phba->hbalock, iflags);
  10077. __lpfc_mbox_cmpl_put(phba, pmb);
  10078. phba->work_ha |= HA_MBATT;
  10079. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10080. workposted = true;
  10081. send_current_mbox:
  10082. spin_lock_irqsave(&phba->hbalock, iflags);
  10083. /* Release the mailbox command posting token */
  10084. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10085. /* Setting active mailbox pointer need to be in sync to flag clear */
  10086. phba->sli.mbox_active = NULL;
  10087. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10088. /* Wake up worker thread to post the next pending mailbox command */
  10089. lpfc_worker_wake_up(phba);
  10090. out_no_mqe_complete:
  10091. if (bf_get(lpfc_trailer_consumed, mcqe))
  10092. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10093. return workposted;
  10094. }
  10095. /**
  10096. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10097. * @phba: Pointer to HBA context object.
  10098. * @cqe: Pointer to mailbox completion queue entry.
  10099. *
  10100. * This routine process a mailbox completion queue entry, it invokes the
  10101. * proper mailbox complete handling or asynchrous event handling routine
  10102. * according to the MCQE's async bit.
  10103. *
  10104. * Return: true if work posted to worker thread, otherwise false.
  10105. **/
  10106. static bool
  10107. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10108. {
  10109. struct lpfc_mcqe mcqe;
  10110. bool workposted;
  10111. /* Copy the mailbox MCQE and convert endian order as needed */
  10112. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10113. /* Invoke the proper event handling routine */
  10114. if (!bf_get(lpfc_trailer_async, &mcqe))
  10115. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10116. else
  10117. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10118. return workposted;
  10119. }
  10120. /**
  10121. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10122. * @phba: Pointer to HBA context object.
  10123. * @wcqe: Pointer to work-queue completion queue entry.
  10124. *
  10125. * This routine handles an ELS work-queue completion event.
  10126. *
  10127. * Return: true if work posted to worker thread, otherwise false.
  10128. **/
  10129. static bool
  10130. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10131. struct lpfc_wcqe_complete *wcqe)
  10132. {
  10133. struct lpfc_iocbq *irspiocbq;
  10134. unsigned long iflags;
  10135. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10136. /* Get an irspiocbq for later ELS response processing use */
  10137. irspiocbq = lpfc_sli_get_iocbq(phba);
  10138. if (!irspiocbq) {
  10139. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10140. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10141. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10142. pring->txq_cnt, phba->iocb_cnt,
  10143. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10144. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10145. return false;
  10146. }
  10147. /* Save off the slow-path queue event for work thread to process */
  10148. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10149. spin_lock_irqsave(&phba->hbalock, iflags);
  10150. list_add_tail(&irspiocbq->cq_event.list,
  10151. &phba->sli4_hba.sp_queue_event);
  10152. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10153. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10154. return true;
  10155. }
  10156. /**
  10157. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10158. * @phba: Pointer to HBA context object.
  10159. * @wcqe: Pointer to work-queue completion queue entry.
  10160. *
  10161. * This routine handles slow-path WQ entry comsumed event by invoking the
  10162. * proper WQ release routine to the slow-path WQ.
  10163. **/
  10164. static void
  10165. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10166. struct lpfc_wcqe_release *wcqe)
  10167. {
  10168. /* sanity check on queue memory */
  10169. if (unlikely(!phba->sli4_hba.els_wq))
  10170. return;
  10171. /* Check for the slow-path ELS work queue */
  10172. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10173. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10174. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10175. else
  10176. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10177. "2579 Slow-path wqe consume event carries "
  10178. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10179. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10180. phba->sli4_hba.els_wq->queue_id);
  10181. }
  10182. /**
  10183. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10184. * @phba: Pointer to HBA context object.
  10185. * @cq: Pointer to a WQ completion queue.
  10186. * @wcqe: Pointer to work-queue completion queue entry.
  10187. *
  10188. * This routine handles an XRI abort event.
  10189. *
  10190. * Return: true if work posted to worker thread, otherwise false.
  10191. **/
  10192. static bool
  10193. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10194. struct lpfc_queue *cq,
  10195. struct sli4_wcqe_xri_aborted *wcqe)
  10196. {
  10197. bool workposted = false;
  10198. struct lpfc_cq_event *cq_event;
  10199. unsigned long iflags;
  10200. /* Allocate a new internal CQ_EVENT entry */
  10201. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10202. if (!cq_event) {
  10203. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10204. "0602 Failed to allocate CQ_EVENT entry\n");
  10205. return false;
  10206. }
  10207. /* Move the CQE into the proper xri abort event list */
  10208. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10209. switch (cq->subtype) {
  10210. case LPFC_FCP:
  10211. spin_lock_irqsave(&phba->hbalock, iflags);
  10212. list_add_tail(&cq_event->list,
  10213. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10214. /* Set the fcp xri abort event flag */
  10215. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10216. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10217. workposted = true;
  10218. break;
  10219. case LPFC_ELS:
  10220. spin_lock_irqsave(&phba->hbalock, iflags);
  10221. list_add_tail(&cq_event->list,
  10222. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10223. /* Set the els xri abort event flag */
  10224. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10225. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10226. workposted = true;
  10227. break;
  10228. default:
  10229. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10230. "0603 Invalid work queue CQE subtype (x%x)\n",
  10231. cq->subtype);
  10232. workposted = false;
  10233. break;
  10234. }
  10235. return workposted;
  10236. }
  10237. /**
  10238. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10239. * @phba: Pointer to HBA context object.
  10240. * @rcqe: Pointer to receive-queue completion queue entry.
  10241. *
  10242. * This routine process a receive-queue completion queue entry.
  10243. *
  10244. * Return: true if work posted to worker thread, otherwise false.
  10245. **/
  10246. static bool
  10247. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10248. {
  10249. bool workposted = false;
  10250. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10251. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10252. struct hbq_dmabuf *dma_buf;
  10253. uint32_t status, rq_id;
  10254. unsigned long iflags;
  10255. /* sanity check on queue memory */
  10256. if (unlikely(!hrq) || unlikely(!drq))
  10257. return workposted;
  10258. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10259. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10260. else
  10261. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10262. if (rq_id != hrq->queue_id)
  10263. goto out;
  10264. status = bf_get(lpfc_rcqe_status, rcqe);
  10265. switch (status) {
  10266. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10267. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10268. "2537 Receive Frame Truncated!!\n");
  10269. case FC_STATUS_RQ_SUCCESS:
  10270. lpfc_sli4_rq_release(hrq, drq);
  10271. spin_lock_irqsave(&phba->hbalock, iflags);
  10272. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10273. if (!dma_buf) {
  10274. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10275. goto out;
  10276. }
  10277. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10278. /* save off the frame for the word thread to process */
  10279. list_add_tail(&dma_buf->cq_event.list,
  10280. &phba->sli4_hba.sp_queue_event);
  10281. /* Frame received */
  10282. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10283. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10284. workposted = true;
  10285. break;
  10286. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10287. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10288. /* Post more buffers if possible */
  10289. spin_lock_irqsave(&phba->hbalock, iflags);
  10290. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10291. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10292. workposted = true;
  10293. break;
  10294. }
  10295. out:
  10296. return workposted;
  10297. }
  10298. /**
  10299. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10300. * @phba: Pointer to HBA context object.
  10301. * @cq: Pointer to the completion queue.
  10302. * @wcqe: Pointer to a completion queue entry.
  10303. *
  10304. * This routine process a slow-path work-queue or receive queue completion queue
  10305. * entry.
  10306. *
  10307. * Return: true if work posted to worker thread, otherwise false.
  10308. **/
  10309. static bool
  10310. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10311. struct lpfc_cqe *cqe)
  10312. {
  10313. struct lpfc_cqe cqevt;
  10314. bool workposted = false;
  10315. /* Copy the work queue CQE and convert endian order if needed */
  10316. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10317. /* Check and process for different type of WCQE and dispatch */
  10318. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10319. case CQE_CODE_COMPL_WQE:
  10320. /* Process the WQ/RQ complete event */
  10321. phba->last_completion_time = jiffies;
  10322. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10323. (struct lpfc_wcqe_complete *)&cqevt);
  10324. break;
  10325. case CQE_CODE_RELEASE_WQE:
  10326. /* Process the WQ release event */
  10327. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10328. (struct lpfc_wcqe_release *)&cqevt);
  10329. break;
  10330. case CQE_CODE_XRI_ABORTED:
  10331. /* Process the WQ XRI abort event */
  10332. phba->last_completion_time = jiffies;
  10333. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10334. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10335. break;
  10336. case CQE_CODE_RECEIVE:
  10337. case CQE_CODE_RECEIVE_V1:
  10338. /* Process the RQ event */
  10339. phba->last_completion_time = jiffies;
  10340. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10341. (struct lpfc_rcqe *)&cqevt);
  10342. break;
  10343. default:
  10344. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10345. "0388 Not a valid WCQE code: x%x\n",
  10346. bf_get(lpfc_cqe_code, &cqevt));
  10347. break;
  10348. }
  10349. return workposted;
  10350. }
  10351. /**
  10352. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10353. * @phba: Pointer to HBA context object.
  10354. * @eqe: Pointer to fast-path event queue entry.
  10355. *
  10356. * This routine process a event queue entry from the slow-path event queue.
  10357. * It will check the MajorCode and MinorCode to determine this is for a
  10358. * completion event on a completion queue, if not, an error shall be logged
  10359. * and just return. Otherwise, it will get to the corresponding completion
  10360. * queue and process all the entries on that completion queue, rearm the
  10361. * completion queue, and then return.
  10362. *
  10363. **/
  10364. static void
  10365. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10366. {
  10367. struct lpfc_queue *cq = NULL, *childq, *speq;
  10368. struct lpfc_cqe *cqe;
  10369. bool workposted = false;
  10370. int ecount = 0;
  10371. uint16_t cqid;
  10372. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10373. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10374. "0359 Not a valid slow-path completion "
  10375. "event: majorcode=x%x, minorcode=x%x\n",
  10376. bf_get_le32(lpfc_eqe_major_code, eqe),
  10377. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10378. return;
  10379. }
  10380. /* Get the reference to the corresponding CQ */
  10381. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10382. /* Search for completion queue pointer matching this cqid */
  10383. speq = phba->sli4_hba.sp_eq;
  10384. /* sanity check on queue memory */
  10385. if (unlikely(!speq))
  10386. return;
  10387. list_for_each_entry(childq, &speq->child_list, list) {
  10388. if (childq->queue_id == cqid) {
  10389. cq = childq;
  10390. break;
  10391. }
  10392. }
  10393. if (unlikely(!cq)) {
  10394. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10395. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10396. "0365 Slow-path CQ identifier "
  10397. "(%d) does not exist\n", cqid);
  10398. return;
  10399. }
  10400. /* Process all the entries to the CQ */
  10401. switch (cq->type) {
  10402. case LPFC_MCQ:
  10403. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10404. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10405. if (!(++ecount % cq->entry_repost))
  10406. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10407. }
  10408. break;
  10409. case LPFC_WCQ:
  10410. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10411. if (cq->subtype == LPFC_FCP)
  10412. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10413. cqe);
  10414. else
  10415. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10416. cqe);
  10417. if (!(++ecount % cq->entry_repost))
  10418. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10419. }
  10420. break;
  10421. default:
  10422. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10423. "0370 Invalid completion queue type (%d)\n",
  10424. cq->type);
  10425. return;
  10426. }
  10427. /* Catch the no cq entry condition, log an error */
  10428. if (unlikely(ecount == 0))
  10429. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10430. "0371 No entry from the CQ: identifier "
  10431. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10432. /* In any case, flash and re-arm the RCQ */
  10433. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10434. /* wake up worker thread if there are works to be done */
  10435. if (workposted)
  10436. lpfc_worker_wake_up(phba);
  10437. }
  10438. /**
  10439. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10440. * @eqe: Pointer to fast-path completion queue entry.
  10441. *
  10442. * This routine process a fast-path work queue completion entry from fast-path
  10443. * event queue for FCP command response completion.
  10444. **/
  10445. static void
  10446. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10447. struct lpfc_wcqe_complete *wcqe)
  10448. {
  10449. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10450. struct lpfc_iocbq *cmdiocbq;
  10451. struct lpfc_iocbq irspiocbq;
  10452. unsigned long iflags;
  10453. spin_lock_irqsave(&phba->hbalock, iflags);
  10454. pring->stats.iocb_event++;
  10455. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10456. /* Check for response status */
  10457. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10458. /* If resource errors reported from HBA, reduce queue
  10459. * depth of the SCSI device.
  10460. */
  10461. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10462. IOSTAT_LOCAL_REJECT) &&
  10463. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10464. phba->lpfc_rampdown_queue_depth(phba);
  10465. }
  10466. /* Log the error status */
  10467. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10468. "0373 FCP complete error: status=x%x, "
  10469. "hw_status=x%x, total_data_specified=%d, "
  10470. "parameter=x%x, word3=x%x\n",
  10471. bf_get(lpfc_wcqe_c_status, wcqe),
  10472. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10473. wcqe->total_data_placed, wcqe->parameter,
  10474. wcqe->word3);
  10475. }
  10476. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10477. spin_lock_irqsave(&phba->hbalock, iflags);
  10478. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10479. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10480. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10481. if (unlikely(!cmdiocbq)) {
  10482. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10483. "0374 FCP complete with no corresponding "
  10484. "cmdiocb: iotag (%d)\n",
  10485. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10486. return;
  10487. }
  10488. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10489. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10490. "0375 FCP cmdiocb not callback function "
  10491. "iotag: (%d)\n",
  10492. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10493. return;
  10494. }
  10495. /* Fake the irspiocb and copy necessary response information */
  10496. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10497. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10498. spin_lock_irqsave(&phba->hbalock, iflags);
  10499. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10500. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10501. }
  10502. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10503. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10504. }
  10505. /**
  10506. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10507. * @phba: Pointer to HBA context object.
  10508. * @cq: Pointer to completion queue.
  10509. * @wcqe: Pointer to work-queue completion queue entry.
  10510. *
  10511. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10512. * proper WQ release routine to the slow-path WQ.
  10513. **/
  10514. static void
  10515. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10516. struct lpfc_wcqe_release *wcqe)
  10517. {
  10518. struct lpfc_queue *childwq;
  10519. bool wqid_matched = false;
  10520. uint16_t fcp_wqid;
  10521. /* Check for fast-path FCP work queue release */
  10522. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10523. list_for_each_entry(childwq, &cq->child_list, list) {
  10524. if (childwq->queue_id == fcp_wqid) {
  10525. lpfc_sli4_wq_release(childwq,
  10526. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10527. wqid_matched = true;
  10528. break;
  10529. }
  10530. }
  10531. /* Report warning log message if no match found */
  10532. if (wqid_matched != true)
  10533. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10534. "2580 Fast-path wqe consume event carries "
  10535. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10536. }
  10537. /**
  10538. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10539. * @cq: Pointer to the completion queue.
  10540. * @eqe: Pointer to fast-path completion queue entry.
  10541. *
  10542. * This routine process a fast-path work queue completion entry from fast-path
  10543. * event queue for FCP command response completion.
  10544. **/
  10545. static int
  10546. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10547. struct lpfc_cqe *cqe)
  10548. {
  10549. struct lpfc_wcqe_release wcqe;
  10550. bool workposted = false;
  10551. /* Copy the work queue CQE and convert endian order if needed */
  10552. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10553. /* Check and process for different type of WCQE and dispatch */
  10554. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10555. case CQE_CODE_COMPL_WQE:
  10556. /* Process the WQ complete event */
  10557. phba->last_completion_time = jiffies;
  10558. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10559. (struct lpfc_wcqe_complete *)&wcqe);
  10560. break;
  10561. case CQE_CODE_RELEASE_WQE:
  10562. /* Process the WQ release event */
  10563. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10564. (struct lpfc_wcqe_release *)&wcqe);
  10565. break;
  10566. case CQE_CODE_XRI_ABORTED:
  10567. /* Process the WQ XRI abort event */
  10568. phba->last_completion_time = jiffies;
  10569. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10570. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10571. break;
  10572. default:
  10573. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10574. "0144 Not a valid WCQE code: x%x\n",
  10575. bf_get(lpfc_wcqe_c_code, &wcqe));
  10576. break;
  10577. }
  10578. return workposted;
  10579. }
  10580. /**
  10581. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10582. * @phba: Pointer to HBA context object.
  10583. * @eqe: Pointer to fast-path event queue entry.
  10584. *
  10585. * This routine process a event queue entry from the fast-path event queue.
  10586. * It will check the MajorCode and MinorCode to determine this is for a
  10587. * completion event on a completion queue, if not, an error shall be logged
  10588. * and just return. Otherwise, it will get to the corresponding completion
  10589. * queue and process all the entries on the completion queue, rearm the
  10590. * completion queue, and then return.
  10591. **/
  10592. static void
  10593. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10594. uint32_t fcp_cqidx)
  10595. {
  10596. struct lpfc_queue *cq;
  10597. struct lpfc_cqe *cqe;
  10598. bool workposted = false;
  10599. uint16_t cqid;
  10600. int ecount = 0;
  10601. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10602. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10603. "0366 Not a valid fast-path completion "
  10604. "event: majorcode=x%x, minorcode=x%x\n",
  10605. bf_get_le32(lpfc_eqe_major_code, eqe),
  10606. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10607. return;
  10608. }
  10609. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10610. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10611. "3146 Fast-path completion queues "
  10612. "does not exist\n");
  10613. return;
  10614. }
  10615. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10616. if (unlikely(!cq)) {
  10617. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10618. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10619. "0367 Fast-path completion queue "
  10620. "(%d) does not exist\n", fcp_cqidx);
  10621. return;
  10622. }
  10623. /* Get the reference to the corresponding CQ */
  10624. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10625. if (unlikely(cqid != cq->queue_id)) {
  10626. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10627. "0368 Miss-matched fast-path completion "
  10628. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10629. cqid, cq->queue_id);
  10630. return;
  10631. }
  10632. /* Process all the entries to the CQ */
  10633. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10634. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10635. if (!(++ecount % cq->entry_repost))
  10636. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10637. }
  10638. /* Catch the no cq entry condition */
  10639. if (unlikely(ecount == 0))
  10640. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10641. "0369 No entry from fast-path completion "
  10642. "queue fcpcqid=%d\n", cq->queue_id);
  10643. /* In any case, flash and re-arm the CQ */
  10644. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10645. /* wake up worker thread if there are works to be done */
  10646. if (workposted)
  10647. lpfc_worker_wake_up(phba);
  10648. }
  10649. static void
  10650. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10651. {
  10652. struct lpfc_eqe *eqe;
  10653. /* walk all the EQ entries and drop on the floor */
  10654. while ((eqe = lpfc_sli4_eq_get(eq)))
  10655. ;
  10656. /* Clear and re-arm the EQ */
  10657. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10658. }
  10659. /**
  10660. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10661. * @irq: Interrupt number.
  10662. * @dev_id: The device context pointer.
  10663. *
  10664. * This function is directly called from the PCI layer as an interrupt
  10665. * service routine when device with SLI-4 interface spec is enabled with
  10666. * MSI-X multi-message interrupt mode and there are slow-path events in
  10667. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10668. * interrupt mode, this function is called as part of the device-level
  10669. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10670. * undergoing initialization, the interrupt handler will not process the
  10671. * interrupt. The link attention and ELS ring attention events are handled
  10672. * by the worker thread. The interrupt handler signals the worker thread
  10673. * and returns for these events. This function is called without any lock
  10674. * held. It gets the hbalock to access and update SLI data structures.
  10675. *
  10676. * This function returns IRQ_HANDLED when interrupt is handled else it
  10677. * returns IRQ_NONE.
  10678. **/
  10679. irqreturn_t
  10680. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10681. {
  10682. struct lpfc_hba *phba;
  10683. struct lpfc_queue *speq;
  10684. struct lpfc_eqe *eqe;
  10685. unsigned long iflag;
  10686. int ecount = 0;
  10687. /*
  10688. * Get the driver's phba structure from the dev_id
  10689. */
  10690. phba = (struct lpfc_hba *)dev_id;
  10691. if (unlikely(!phba))
  10692. return IRQ_NONE;
  10693. /* Get to the EQ struct associated with this vector */
  10694. speq = phba->sli4_hba.sp_eq;
  10695. if (unlikely(!speq))
  10696. return IRQ_NONE;
  10697. /* Check device state for handling interrupt */
  10698. if (unlikely(lpfc_intr_state_check(phba))) {
  10699. /* Check again for link_state with lock held */
  10700. spin_lock_irqsave(&phba->hbalock, iflag);
  10701. if (phba->link_state < LPFC_LINK_DOWN)
  10702. /* Flush, clear interrupt, and rearm the EQ */
  10703. lpfc_sli4_eq_flush(phba, speq);
  10704. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10705. return IRQ_NONE;
  10706. }
  10707. /*
  10708. * Process all the event on FCP slow-path EQ
  10709. */
  10710. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10711. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10712. if (!(++ecount % speq->entry_repost))
  10713. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10714. }
  10715. /* Always clear and re-arm the slow-path EQ */
  10716. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10717. /* Catch the no cq entry condition */
  10718. if (unlikely(ecount == 0)) {
  10719. if (phba->intr_type == MSIX)
  10720. /* MSI-X treated interrupt served as no EQ share INT */
  10721. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10722. "0357 MSI-X interrupt with no EQE\n");
  10723. else
  10724. /* Non MSI-X treated on interrupt as EQ share INT */
  10725. return IRQ_NONE;
  10726. }
  10727. return IRQ_HANDLED;
  10728. } /* lpfc_sli4_sp_intr_handler */
  10729. /**
  10730. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10731. * @irq: Interrupt number.
  10732. * @dev_id: The device context pointer.
  10733. *
  10734. * This function is directly called from the PCI layer as an interrupt
  10735. * service routine when device with SLI-4 interface spec is enabled with
  10736. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10737. * ring event in the HBA. However, when the device is enabled with either
  10738. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10739. * device-level interrupt handler. When the PCI slot is in error recovery
  10740. * or the HBA is undergoing initialization, the interrupt handler will not
  10741. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10742. * the intrrupt context. This function is called without any lock held.
  10743. * It gets the hbalock to access and update SLI data structures. Note that,
  10744. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10745. * equal to that of FCP CQ index.
  10746. *
  10747. * This function returns IRQ_HANDLED when interrupt is handled else it
  10748. * returns IRQ_NONE.
  10749. **/
  10750. irqreturn_t
  10751. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10752. {
  10753. struct lpfc_hba *phba;
  10754. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10755. struct lpfc_queue *fpeq;
  10756. struct lpfc_eqe *eqe;
  10757. unsigned long iflag;
  10758. int ecount = 0;
  10759. uint32_t fcp_eqidx;
  10760. /* Get the driver's phba structure from the dev_id */
  10761. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10762. phba = fcp_eq_hdl->phba;
  10763. fcp_eqidx = fcp_eq_hdl->idx;
  10764. if (unlikely(!phba))
  10765. return IRQ_NONE;
  10766. if (unlikely(!phba->sli4_hba.fp_eq))
  10767. return IRQ_NONE;
  10768. /* Get to the EQ struct associated with this vector */
  10769. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10770. if (unlikely(!fpeq))
  10771. return IRQ_NONE;
  10772. /* Check device state for handling interrupt */
  10773. if (unlikely(lpfc_intr_state_check(phba))) {
  10774. /* Check again for link_state with lock held */
  10775. spin_lock_irqsave(&phba->hbalock, iflag);
  10776. if (phba->link_state < LPFC_LINK_DOWN)
  10777. /* Flush, clear interrupt, and rearm the EQ */
  10778. lpfc_sli4_eq_flush(phba, fpeq);
  10779. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10780. return IRQ_NONE;
  10781. }
  10782. /*
  10783. * Process all the event on FCP fast-path EQ
  10784. */
  10785. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10786. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10787. if (!(++ecount % fpeq->entry_repost))
  10788. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10789. }
  10790. /* Always clear and re-arm the fast-path EQ */
  10791. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10792. if (unlikely(ecount == 0)) {
  10793. if (phba->intr_type == MSIX)
  10794. /* MSI-X treated interrupt served as no EQ share INT */
  10795. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10796. "0358 MSI-X interrupt with no EQE\n");
  10797. else
  10798. /* Non MSI-X treated on interrupt as EQ share INT */
  10799. return IRQ_NONE;
  10800. }
  10801. return IRQ_HANDLED;
  10802. } /* lpfc_sli4_fp_intr_handler */
  10803. /**
  10804. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10805. * @irq: Interrupt number.
  10806. * @dev_id: The device context pointer.
  10807. *
  10808. * This function is the device-level interrupt handler to device with SLI-4
  10809. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10810. * interrupt mode is enabled and there is an event in the HBA which requires
  10811. * driver attention. This function invokes the slow-path interrupt attention
  10812. * handling function and fast-path interrupt attention handling function in
  10813. * turn to process the relevant HBA attention events. This function is called
  10814. * without any lock held. It gets the hbalock to access and update SLI data
  10815. * structures.
  10816. *
  10817. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10818. * returns IRQ_NONE.
  10819. **/
  10820. irqreturn_t
  10821. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10822. {
  10823. struct lpfc_hba *phba;
  10824. irqreturn_t sp_irq_rc, fp_irq_rc;
  10825. bool fp_handled = false;
  10826. uint32_t fcp_eqidx;
  10827. /* Get the driver's phba structure from the dev_id */
  10828. phba = (struct lpfc_hba *)dev_id;
  10829. if (unlikely(!phba))
  10830. return IRQ_NONE;
  10831. /*
  10832. * Invokes slow-path host attention interrupt handling as appropriate.
  10833. */
  10834. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10835. /*
  10836. * Invoke fast-path host attention interrupt handling as appropriate.
  10837. */
  10838. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10839. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10840. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10841. if (fp_irq_rc == IRQ_HANDLED)
  10842. fp_handled |= true;
  10843. }
  10844. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10845. } /* lpfc_sli4_intr_handler */
  10846. /**
  10847. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10848. * @queue: The queue structure to free.
  10849. *
  10850. * This function frees a queue structure and the DMAable memory used for
  10851. * the host resident queue. This function must be called after destroying the
  10852. * queue on the HBA.
  10853. **/
  10854. void
  10855. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10856. {
  10857. struct lpfc_dmabuf *dmabuf;
  10858. if (!queue)
  10859. return;
  10860. while (!list_empty(&queue->page_list)) {
  10861. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10862. list);
  10863. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10864. dmabuf->virt, dmabuf->phys);
  10865. kfree(dmabuf);
  10866. }
  10867. kfree(queue);
  10868. return;
  10869. }
  10870. /**
  10871. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10872. * @phba: The HBA that this queue is being created on.
  10873. * @entry_size: The size of each queue entry for this queue.
  10874. * @entry count: The number of entries that this queue will handle.
  10875. *
  10876. * This function allocates a queue structure and the DMAable memory used for
  10877. * the host resident queue. This function must be called before creating the
  10878. * queue on the HBA.
  10879. **/
  10880. struct lpfc_queue *
  10881. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10882. uint32_t entry_count)
  10883. {
  10884. struct lpfc_queue *queue;
  10885. struct lpfc_dmabuf *dmabuf;
  10886. int x, total_qe_count;
  10887. void *dma_pointer;
  10888. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10889. if (!phba->sli4_hba.pc_sli4_params.supported)
  10890. hw_page_size = SLI4_PAGE_SIZE;
  10891. queue = kzalloc(sizeof(struct lpfc_queue) +
  10892. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10893. if (!queue)
  10894. return NULL;
  10895. queue->page_count = (ALIGN(entry_size * entry_count,
  10896. hw_page_size))/hw_page_size;
  10897. INIT_LIST_HEAD(&queue->list);
  10898. INIT_LIST_HEAD(&queue->page_list);
  10899. INIT_LIST_HEAD(&queue->child_list);
  10900. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10901. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10902. if (!dmabuf)
  10903. goto out_fail;
  10904. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10905. hw_page_size, &dmabuf->phys,
  10906. GFP_KERNEL);
  10907. if (!dmabuf->virt) {
  10908. kfree(dmabuf);
  10909. goto out_fail;
  10910. }
  10911. memset(dmabuf->virt, 0, hw_page_size);
  10912. dmabuf->buffer_tag = x;
  10913. list_add_tail(&dmabuf->list, &queue->page_list);
  10914. /* initialize queue's entry array */
  10915. dma_pointer = dmabuf->virt;
  10916. for (; total_qe_count < entry_count &&
  10917. dma_pointer < (hw_page_size + dmabuf->virt);
  10918. total_qe_count++, dma_pointer += entry_size) {
  10919. queue->qe[total_qe_count].address = dma_pointer;
  10920. }
  10921. }
  10922. queue->entry_size = entry_size;
  10923. queue->entry_count = entry_count;
  10924. /*
  10925. * entry_repost is calculated based on the number of entries in the
  10926. * queue. This works out except for RQs. If buffers are NOT initially
  10927. * posted for every RQE, entry_repost should be adjusted accordingly.
  10928. */
  10929. queue->entry_repost = (entry_count >> 3);
  10930. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10931. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10932. queue->phba = phba;
  10933. return queue;
  10934. out_fail:
  10935. lpfc_sli4_queue_free(queue);
  10936. return NULL;
  10937. }
  10938. /**
  10939. * lpfc_eq_create - Create an Event Queue on the HBA
  10940. * @phba: HBA structure that indicates port to create a queue on.
  10941. * @eq: The queue structure to use to create the event queue.
  10942. * @imax: The maximum interrupt per second limit.
  10943. *
  10944. * This function creates an event queue, as detailed in @eq, on a port,
  10945. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10946. *
  10947. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10948. * is used to get the entry count and entry size that are necessary to
  10949. * determine the number of pages to allocate and use for this queue. This
  10950. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10951. * event queue. This function is asynchronous and will wait for the mailbox
  10952. * command to finish before continuing.
  10953. *
  10954. * On success this function will return a zero. If unable to allocate enough
  10955. * memory this function will return -ENOMEM. If the queue create mailbox command
  10956. * fails this function will return -ENXIO.
  10957. **/
  10958. uint32_t
  10959. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10960. {
  10961. struct lpfc_mbx_eq_create *eq_create;
  10962. LPFC_MBOXQ_t *mbox;
  10963. int rc, length, status = 0;
  10964. struct lpfc_dmabuf *dmabuf;
  10965. uint32_t shdr_status, shdr_add_status;
  10966. union lpfc_sli4_cfg_shdr *shdr;
  10967. uint16_t dmult;
  10968. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10969. /* sanity check on queue memory */
  10970. if (!eq)
  10971. return -ENODEV;
  10972. if (!phba->sli4_hba.pc_sli4_params.supported)
  10973. hw_page_size = SLI4_PAGE_SIZE;
  10974. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10975. if (!mbox)
  10976. return -ENOMEM;
  10977. length = (sizeof(struct lpfc_mbx_eq_create) -
  10978. sizeof(struct lpfc_sli4_cfg_mhdr));
  10979. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10980. LPFC_MBOX_OPCODE_EQ_CREATE,
  10981. length, LPFC_SLI4_MBX_EMBED);
  10982. eq_create = &mbox->u.mqe.un.eq_create;
  10983. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10984. eq->page_count);
  10985. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10986. LPFC_EQE_SIZE);
  10987. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10988. /* Calculate delay multiper from maximum interrupt per second */
  10989. dmult = LPFC_DMULT_CONST/imax - 1;
  10990. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10991. dmult);
  10992. switch (eq->entry_count) {
  10993. default:
  10994. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10995. "0360 Unsupported EQ count. (%d)\n",
  10996. eq->entry_count);
  10997. if (eq->entry_count < 256)
  10998. return -EINVAL;
  10999. /* otherwise default to smallest count (drop through) */
  11000. case 256:
  11001. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11002. LPFC_EQ_CNT_256);
  11003. break;
  11004. case 512:
  11005. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11006. LPFC_EQ_CNT_512);
  11007. break;
  11008. case 1024:
  11009. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11010. LPFC_EQ_CNT_1024);
  11011. break;
  11012. case 2048:
  11013. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11014. LPFC_EQ_CNT_2048);
  11015. break;
  11016. case 4096:
  11017. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11018. LPFC_EQ_CNT_4096);
  11019. break;
  11020. }
  11021. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11022. memset(dmabuf->virt, 0, hw_page_size);
  11023. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11024. putPaddrLow(dmabuf->phys);
  11025. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11026. putPaddrHigh(dmabuf->phys);
  11027. }
  11028. mbox->vport = phba->pport;
  11029. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11030. mbox->context1 = NULL;
  11031. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11032. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11033. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11034. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11035. if (shdr_status || shdr_add_status || rc) {
  11036. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11037. "2500 EQ_CREATE mailbox failed with "
  11038. "status x%x add_status x%x, mbx status x%x\n",
  11039. shdr_status, shdr_add_status, rc);
  11040. status = -ENXIO;
  11041. }
  11042. eq->type = LPFC_EQ;
  11043. eq->subtype = LPFC_NONE;
  11044. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11045. if (eq->queue_id == 0xFFFF)
  11046. status = -ENXIO;
  11047. eq->host_index = 0;
  11048. eq->hba_index = 0;
  11049. mempool_free(mbox, phba->mbox_mem_pool);
  11050. return status;
  11051. }
  11052. /**
  11053. * lpfc_cq_create - Create a Completion Queue on the HBA
  11054. * @phba: HBA structure that indicates port to create a queue on.
  11055. * @cq: The queue structure to use to create the completion queue.
  11056. * @eq: The event queue to bind this completion queue to.
  11057. *
  11058. * This function creates a completion queue, as detailed in @wq, on a port,
  11059. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11060. *
  11061. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11062. * is used to get the entry count and entry size that are necessary to
  11063. * determine the number of pages to allocate and use for this queue. The @eq
  11064. * is used to indicate which event queue to bind this completion queue to. This
  11065. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11066. * completion queue. This function is asynchronous and will wait for the mailbox
  11067. * command to finish before continuing.
  11068. *
  11069. * On success this function will return a zero. If unable to allocate enough
  11070. * memory this function will return -ENOMEM. If the queue create mailbox command
  11071. * fails this function will return -ENXIO.
  11072. **/
  11073. uint32_t
  11074. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11075. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11076. {
  11077. struct lpfc_mbx_cq_create *cq_create;
  11078. struct lpfc_dmabuf *dmabuf;
  11079. LPFC_MBOXQ_t *mbox;
  11080. int rc, length, status = 0;
  11081. uint32_t shdr_status, shdr_add_status;
  11082. union lpfc_sli4_cfg_shdr *shdr;
  11083. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11084. /* sanity check on queue memory */
  11085. if (!cq || !eq)
  11086. return -ENODEV;
  11087. if (!phba->sli4_hba.pc_sli4_params.supported)
  11088. hw_page_size = SLI4_PAGE_SIZE;
  11089. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11090. if (!mbox)
  11091. return -ENOMEM;
  11092. length = (sizeof(struct lpfc_mbx_cq_create) -
  11093. sizeof(struct lpfc_sli4_cfg_mhdr));
  11094. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11095. LPFC_MBOX_OPCODE_CQ_CREATE,
  11096. length, LPFC_SLI4_MBX_EMBED);
  11097. cq_create = &mbox->u.mqe.un.cq_create;
  11098. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11099. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11100. cq->page_count);
  11101. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11102. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11103. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11104. phba->sli4_hba.pc_sli4_params.cqv);
  11105. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11106. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11107. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11108. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11109. eq->queue_id);
  11110. } else {
  11111. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11112. eq->queue_id);
  11113. }
  11114. switch (cq->entry_count) {
  11115. default:
  11116. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11117. "0361 Unsupported CQ count. (%d)\n",
  11118. cq->entry_count);
  11119. if (cq->entry_count < 256)
  11120. return -EINVAL;
  11121. /* otherwise default to smallest count (drop through) */
  11122. case 256:
  11123. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11124. LPFC_CQ_CNT_256);
  11125. break;
  11126. case 512:
  11127. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11128. LPFC_CQ_CNT_512);
  11129. break;
  11130. case 1024:
  11131. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11132. LPFC_CQ_CNT_1024);
  11133. break;
  11134. }
  11135. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11136. memset(dmabuf->virt, 0, hw_page_size);
  11137. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11138. putPaddrLow(dmabuf->phys);
  11139. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11140. putPaddrHigh(dmabuf->phys);
  11141. }
  11142. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11143. /* The IOCTL status is embedded in the mailbox subheader. */
  11144. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11145. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11146. if (shdr_status || shdr_add_status || rc) {
  11147. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11148. "2501 CQ_CREATE mailbox failed with "
  11149. "status x%x add_status x%x, mbx status x%x\n",
  11150. shdr_status, shdr_add_status, rc);
  11151. status = -ENXIO;
  11152. goto out;
  11153. }
  11154. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11155. if (cq->queue_id == 0xFFFF) {
  11156. status = -ENXIO;
  11157. goto out;
  11158. }
  11159. /* link the cq onto the parent eq child list */
  11160. list_add_tail(&cq->list, &eq->child_list);
  11161. /* Set up completion queue's type and subtype */
  11162. cq->type = type;
  11163. cq->subtype = subtype;
  11164. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11165. cq->assoc_qid = eq->queue_id;
  11166. cq->host_index = 0;
  11167. cq->hba_index = 0;
  11168. out:
  11169. mempool_free(mbox, phba->mbox_mem_pool);
  11170. return status;
  11171. }
  11172. /**
  11173. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11174. * @phba: HBA structure that indicates port to create a queue on.
  11175. * @mq: The queue structure to use to create the mailbox queue.
  11176. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11177. * @cq: The completion queue to associate with this cq.
  11178. *
  11179. * This function provides failback (fb) functionality when the
  11180. * mq_create_ext fails on older FW generations. It's purpose is identical
  11181. * to mq_create_ext otherwise.
  11182. *
  11183. * This routine cannot fail as all attributes were previously accessed and
  11184. * initialized in mq_create_ext.
  11185. **/
  11186. static void
  11187. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11188. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11189. {
  11190. struct lpfc_mbx_mq_create *mq_create;
  11191. struct lpfc_dmabuf *dmabuf;
  11192. int length;
  11193. length = (sizeof(struct lpfc_mbx_mq_create) -
  11194. sizeof(struct lpfc_sli4_cfg_mhdr));
  11195. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11196. LPFC_MBOX_OPCODE_MQ_CREATE,
  11197. length, LPFC_SLI4_MBX_EMBED);
  11198. mq_create = &mbox->u.mqe.un.mq_create;
  11199. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11200. mq->page_count);
  11201. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11202. cq->queue_id);
  11203. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11204. switch (mq->entry_count) {
  11205. case 16:
  11206. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11207. LPFC_MQ_RING_SIZE_16);
  11208. break;
  11209. case 32:
  11210. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11211. LPFC_MQ_RING_SIZE_32);
  11212. break;
  11213. case 64:
  11214. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11215. LPFC_MQ_RING_SIZE_64);
  11216. break;
  11217. case 128:
  11218. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11219. LPFC_MQ_RING_SIZE_128);
  11220. break;
  11221. }
  11222. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11223. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11224. putPaddrLow(dmabuf->phys);
  11225. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11226. putPaddrHigh(dmabuf->phys);
  11227. }
  11228. }
  11229. /**
  11230. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11231. * @phba: HBA structure that indicates port to create a queue on.
  11232. * @mq: The queue structure to use to create the mailbox queue.
  11233. * @cq: The completion queue to associate with this cq.
  11234. * @subtype: The queue's subtype.
  11235. *
  11236. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11237. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11238. *
  11239. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11240. * is used to get the entry count and entry size that are necessary to
  11241. * determine the number of pages to allocate and use for this queue. This
  11242. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11243. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11244. * command to finish before continuing.
  11245. *
  11246. * On success this function will return a zero. If unable to allocate enough
  11247. * memory this function will return -ENOMEM. If the queue create mailbox command
  11248. * fails this function will return -ENXIO.
  11249. **/
  11250. int32_t
  11251. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11252. struct lpfc_queue *cq, uint32_t subtype)
  11253. {
  11254. struct lpfc_mbx_mq_create *mq_create;
  11255. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11256. struct lpfc_dmabuf *dmabuf;
  11257. LPFC_MBOXQ_t *mbox;
  11258. int rc, length, status = 0;
  11259. uint32_t shdr_status, shdr_add_status;
  11260. union lpfc_sli4_cfg_shdr *shdr;
  11261. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11262. /* sanity check on queue memory */
  11263. if (!mq || !cq)
  11264. return -ENODEV;
  11265. if (!phba->sli4_hba.pc_sli4_params.supported)
  11266. hw_page_size = SLI4_PAGE_SIZE;
  11267. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11268. if (!mbox)
  11269. return -ENOMEM;
  11270. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11271. sizeof(struct lpfc_sli4_cfg_mhdr));
  11272. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11273. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11274. length, LPFC_SLI4_MBX_EMBED);
  11275. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11276. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11277. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11278. &mq_create_ext->u.request, mq->page_count);
  11279. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11280. &mq_create_ext->u.request, 1);
  11281. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11282. &mq_create_ext->u.request, 1);
  11283. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11284. &mq_create_ext->u.request, 1);
  11285. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11286. &mq_create_ext->u.request, 1);
  11287. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11288. &mq_create_ext->u.request, 1);
  11289. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11290. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11291. phba->sli4_hba.pc_sli4_params.mqv);
  11292. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11293. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11294. cq->queue_id);
  11295. else
  11296. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11297. cq->queue_id);
  11298. switch (mq->entry_count) {
  11299. default:
  11300. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11301. "0362 Unsupported MQ count. (%d)\n",
  11302. mq->entry_count);
  11303. if (mq->entry_count < 16)
  11304. return -EINVAL;
  11305. /* otherwise default to smallest count (drop through) */
  11306. case 16:
  11307. bf_set(lpfc_mq_context_ring_size,
  11308. &mq_create_ext->u.request.context,
  11309. LPFC_MQ_RING_SIZE_16);
  11310. break;
  11311. case 32:
  11312. bf_set(lpfc_mq_context_ring_size,
  11313. &mq_create_ext->u.request.context,
  11314. LPFC_MQ_RING_SIZE_32);
  11315. break;
  11316. case 64:
  11317. bf_set(lpfc_mq_context_ring_size,
  11318. &mq_create_ext->u.request.context,
  11319. LPFC_MQ_RING_SIZE_64);
  11320. break;
  11321. case 128:
  11322. bf_set(lpfc_mq_context_ring_size,
  11323. &mq_create_ext->u.request.context,
  11324. LPFC_MQ_RING_SIZE_128);
  11325. break;
  11326. }
  11327. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11328. memset(dmabuf->virt, 0, hw_page_size);
  11329. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11330. putPaddrLow(dmabuf->phys);
  11331. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11332. putPaddrHigh(dmabuf->phys);
  11333. }
  11334. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11335. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11336. &mq_create_ext->u.response);
  11337. if (rc != MBX_SUCCESS) {
  11338. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11339. "2795 MQ_CREATE_EXT failed with "
  11340. "status x%x. Failback to MQ_CREATE.\n",
  11341. rc);
  11342. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11343. mq_create = &mbox->u.mqe.un.mq_create;
  11344. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11345. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11346. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11347. &mq_create->u.response);
  11348. }
  11349. /* The IOCTL status is embedded in the mailbox subheader. */
  11350. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11351. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11352. if (shdr_status || shdr_add_status || rc) {
  11353. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11354. "2502 MQ_CREATE mailbox failed with "
  11355. "status x%x add_status x%x, mbx status x%x\n",
  11356. shdr_status, shdr_add_status, rc);
  11357. status = -ENXIO;
  11358. goto out;
  11359. }
  11360. if (mq->queue_id == 0xFFFF) {
  11361. status = -ENXIO;
  11362. goto out;
  11363. }
  11364. mq->type = LPFC_MQ;
  11365. mq->assoc_qid = cq->queue_id;
  11366. mq->subtype = subtype;
  11367. mq->host_index = 0;
  11368. mq->hba_index = 0;
  11369. /* link the mq onto the parent cq child list */
  11370. list_add_tail(&mq->list, &cq->child_list);
  11371. out:
  11372. mempool_free(mbox, phba->mbox_mem_pool);
  11373. return status;
  11374. }
  11375. /**
  11376. * lpfc_wq_create - Create a Work Queue on the HBA
  11377. * @phba: HBA structure that indicates port to create a queue on.
  11378. * @wq: The queue structure to use to create the work queue.
  11379. * @cq: The completion queue to bind this work queue to.
  11380. * @subtype: The subtype of the work queue indicating its functionality.
  11381. *
  11382. * This function creates a work queue, as detailed in @wq, on a port, described
  11383. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11384. *
  11385. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11386. * is used to get the entry count and entry size that are necessary to
  11387. * determine the number of pages to allocate and use for this queue. The @cq
  11388. * is used to indicate which completion queue to bind this work queue to. This
  11389. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11390. * work queue. This function is asynchronous and will wait for the mailbox
  11391. * command to finish before continuing.
  11392. *
  11393. * On success this function will return a zero. If unable to allocate enough
  11394. * memory this function will return -ENOMEM. If the queue create mailbox command
  11395. * fails this function will return -ENXIO.
  11396. **/
  11397. uint32_t
  11398. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11399. struct lpfc_queue *cq, uint32_t subtype)
  11400. {
  11401. struct lpfc_mbx_wq_create *wq_create;
  11402. struct lpfc_dmabuf *dmabuf;
  11403. LPFC_MBOXQ_t *mbox;
  11404. int rc, length, status = 0;
  11405. uint32_t shdr_status, shdr_add_status;
  11406. union lpfc_sli4_cfg_shdr *shdr;
  11407. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11408. struct dma_address *page;
  11409. /* sanity check on queue memory */
  11410. if (!wq || !cq)
  11411. return -ENODEV;
  11412. if (!phba->sli4_hba.pc_sli4_params.supported)
  11413. hw_page_size = SLI4_PAGE_SIZE;
  11414. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11415. if (!mbox)
  11416. return -ENOMEM;
  11417. length = (sizeof(struct lpfc_mbx_wq_create) -
  11418. sizeof(struct lpfc_sli4_cfg_mhdr));
  11419. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11420. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11421. length, LPFC_SLI4_MBX_EMBED);
  11422. wq_create = &mbox->u.mqe.un.wq_create;
  11423. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11424. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11425. wq->page_count);
  11426. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11427. cq->queue_id);
  11428. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11429. phba->sli4_hba.pc_sli4_params.wqv);
  11430. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11431. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11432. wq->entry_count);
  11433. switch (wq->entry_size) {
  11434. default:
  11435. case 64:
  11436. bf_set(lpfc_mbx_wq_create_wqe_size,
  11437. &wq_create->u.request_1,
  11438. LPFC_WQ_WQE_SIZE_64);
  11439. break;
  11440. case 128:
  11441. bf_set(lpfc_mbx_wq_create_wqe_size,
  11442. &wq_create->u.request_1,
  11443. LPFC_WQ_WQE_SIZE_128);
  11444. break;
  11445. }
  11446. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11447. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11448. page = wq_create->u.request_1.page;
  11449. } else {
  11450. page = wq_create->u.request.page;
  11451. }
  11452. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11453. memset(dmabuf->virt, 0, hw_page_size);
  11454. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11455. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11456. }
  11457. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11458. /* The IOCTL status is embedded in the mailbox subheader. */
  11459. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11460. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11461. if (shdr_status || shdr_add_status || rc) {
  11462. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11463. "2503 WQ_CREATE mailbox failed with "
  11464. "status x%x add_status x%x, mbx status x%x\n",
  11465. shdr_status, shdr_add_status, rc);
  11466. status = -ENXIO;
  11467. goto out;
  11468. }
  11469. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11470. if (wq->queue_id == 0xFFFF) {
  11471. status = -ENXIO;
  11472. goto out;
  11473. }
  11474. wq->type = LPFC_WQ;
  11475. wq->assoc_qid = cq->queue_id;
  11476. wq->subtype = subtype;
  11477. wq->host_index = 0;
  11478. wq->hba_index = 0;
  11479. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11480. /* link the wq onto the parent cq child list */
  11481. list_add_tail(&wq->list, &cq->child_list);
  11482. out:
  11483. mempool_free(mbox, phba->mbox_mem_pool);
  11484. return status;
  11485. }
  11486. /**
  11487. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11488. * @phba: HBA structure that indicates port to create a queue on.
  11489. * @rq: The queue structure to use for the receive queue.
  11490. * @qno: The associated HBQ number
  11491. *
  11492. *
  11493. * For SLI4 we need to adjust the RQ repost value based on
  11494. * the number of buffers that are initially posted to the RQ.
  11495. */
  11496. void
  11497. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11498. {
  11499. uint32_t cnt;
  11500. /* sanity check on queue memory */
  11501. if (!rq)
  11502. return;
  11503. cnt = lpfc_hbq_defs[qno]->entry_count;
  11504. /* Recalc repost for RQs based on buffers initially posted */
  11505. cnt = (cnt >> 3);
  11506. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11507. cnt = LPFC_QUEUE_MIN_REPOST;
  11508. rq->entry_repost = cnt;
  11509. }
  11510. /**
  11511. * lpfc_rq_create - Create a Receive Queue on the HBA
  11512. * @phba: HBA structure that indicates port to create a queue on.
  11513. * @hrq: The queue structure to use to create the header receive queue.
  11514. * @drq: The queue structure to use to create the data receive queue.
  11515. * @cq: The completion queue to bind this work queue to.
  11516. *
  11517. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11518. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11519. * to the HBA.
  11520. *
  11521. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11522. * struct is used to get the entry count that is necessary to determine the
  11523. * number of pages to use for this queue. The @cq is used to indicate which
  11524. * completion queue to bind received buffers that are posted to these queues to.
  11525. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11526. * receive queue pair. This function is asynchronous and will wait for the
  11527. * mailbox command to finish before continuing.
  11528. *
  11529. * On success this function will return a zero. If unable to allocate enough
  11530. * memory this function will return -ENOMEM. If the queue create mailbox command
  11531. * fails this function will return -ENXIO.
  11532. **/
  11533. uint32_t
  11534. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11535. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11536. {
  11537. struct lpfc_mbx_rq_create *rq_create;
  11538. struct lpfc_dmabuf *dmabuf;
  11539. LPFC_MBOXQ_t *mbox;
  11540. int rc, length, status = 0;
  11541. uint32_t shdr_status, shdr_add_status;
  11542. union lpfc_sli4_cfg_shdr *shdr;
  11543. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11544. /* sanity check on queue memory */
  11545. if (!hrq || !drq || !cq)
  11546. return -ENODEV;
  11547. if (!phba->sli4_hba.pc_sli4_params.supported)
  11548. hw_page_size = SLI4_PAGE_SIZE;
  11549. if (hrq->entry_count != drq->entry_count)
  11550. return -EINVAL;
  11551. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11552. if (!mbox)
  11553. return -ENOMEM;
  11554. length = (sizeof(struct lpfc_mbx_rq_create) -
  11555. sizeof(struct lpfc_sli4_cfg_mhdr));
  11556. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11557. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11558. length, LPFC_SLI4_MBX_EMBED);
  11559. rq_create = &mbox->u.mqe.un.rq_create;
  11560. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11561. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11562. phba->sli4_hba.pc_sli4_params.rqv);
  11563. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11564. bf_set(lpfc_rq_context_rqe_count_1,
  11565. &rq_create->u.request.context,
  11566. hrq->entry_count);
  11567. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11568. bf_set(lpfc_rq_context_rqe_size,
  11569. &rq_create->u.request.context,
  11570. LPFC_RQE_SIZE_8);
  11571. bf_set(lpfc_rq_context_page_size,
  11572. &rq_create->u.request.context,
  11573. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11574. } else {
  11575. switch (hrq->entry_count) {
  11576. default:
  11577. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11578. "2535 Unsupported RQ count. (%d)\n",
  11579. hrq->entry_count);
  11580. if (hrq->entry_count < 512)
  11581. return -EINVAL;
  11582. /* otherwise default to smallest count (drop through) */
  11583. case 512:
  11584. bf_set(lpfc_rq_context_rqe_count,
  11585. &rq_create->u.request.context,
  11586. LPFC_RQ_RING_SIZE_512);
  11587. break;
  11588. case 1024:
  11589. bf_set(lpfc_rq_context_rqe_count,
  11590. &rq_create->u.request.context,
  11591. LPFC_RQ_RING_SIZE_1024);
  11592. break;
  11593. case 2048:
  11594. bf_set(lpfc_rq_context_rqe_count,
  11595. &rq_create->u.request.context,
  11596. LPFC_RQ_RING_SIZE_2048);
  11597. break;
  11598. case 4096:
  11599. bf_set(lpfc_rq_context_rqe_count,
  11600. &rq_create->u.request.context,
  11601. LPFC_RQ_RING_SIZE_4096);
  11602. break;
  11603. }
  11604. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11605. LPFC_HDR_BUF_SIZE);
  11606. }
  11607. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11608. cq->queue_id);
  11609. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11610. hrq->page_count);
  11611. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11612. memset(dmabuf->virt, 0, hw_page_size);
  11613. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11614. putPaddrLow(dmabuf->phys);
  11615. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11616. putPaddrHigh(dmabuf->phys);
  11617. }
  11618. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11619. /* The IOCTL status is embedded in the mailbox subheader. */
  11620. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11621. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11622. if (shdr_status || shdr_add_status || rc) {
  11623. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11624. "2504 RQ_CREATE mailbox failed with "
  11625. "status x%x add_status x%x, mbx status x%x\n",
  11626. shdr_status, shdr_add_status, rc);
  11627. status = -ENXIO;
  11628. goto out;
  11629. }
  11630. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11631. if (hrq->queue_id == 0xFFFF) {
  11632. status = -ENXIO;
  11633. goto out;
  11634. }
  11635. hrq->type = LPFC_HRQ;
  11636. hrq->assoc_qid = cq->queue_id;
  11637. hrq->subtype = subtype;
  11638. hrq->host_index = 0;
  11639. hrq->hba_index = 0;
  11640. /* now create the data queue */
  11641. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11642. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11643. length, LPFC_SLI4_MBX_EMBED);
  11644. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11645. phba->sli4_hba.pc_sli4_params.rqv);
  11646. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11647. bf_set(lpfc_rq_context_rqe_count_1,
  11648. &rq_create->u.request.context, hrq->entry_count);
  11649. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11650. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11651. LPFC_RQE_SIZE_8);
  11652. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11653. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11654. } else {
  11655. switch (drq->entry_count) {
  11656. default:
  11657. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11658. "2536 Unsupported RQ count. (%d)\n",
  11659. drq->entry_count);
  11660. if (drq->entry_count < 512)
  11661. return -EINVAL;
  11662. /* otherwise default to smallest count (drop through) */
  11663. case 512:
  11664. bf_set(lpfc_rq_context_rqe_count,
  11665. &rq_create->u.request.context,
  11666. LPFC_RQ_RING_SIZE_512);
  11667. break;
  11668. case 1024:
  11669. bf_set(lpfc_rq_context_rqe_count,
  11670. &rq_create->u.request.context,
  11671. LPFC_RQ_RING_SIZE_1024);
  11672. break;
  11673. case 2048:
  11674. bf_set(lpfc_rq_context_rqe_count,
  11675. &rq_create->u.request.context,
  11676. LPFC_RQ_RING_SIZE_2048);
  11677. break;
  11678. case 4096:
  11679. bf_set(lpfc_rq_context_rqe_count,
  11680. &rq_create->u.request.context,
  11681. LPFC_RQ_RING_SIZE_4096);
  11682. break;
  11683. }
  11684. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11685. LPFC_DATA_BUF_SIZE);
  11686. }
  11687. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11688. cq->queue_id);
  11689. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11690. drq->page_count);
  11691. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11692. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11693. putPaddrLow(dmabuf->phys);
  11694. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11695. putPaddrHigh(dmabuf->phys);
  11696. }
  11697. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11698. /* The IOCTL status is embedded in the mailbox subheader. */
  11699. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11700. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11701. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11702. if (shdr_status || shdr_add_status || rc) {
  11703. status = -ENXIO;
  11704. goto out;
  11705. }
  11706. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11707. if (drq->queue_id == 0xFFFF) {
  11708. status = -ENXIO;
  11709. goto out;
  11710. }
  11711. drq->type = LPFC_DRQ;
  11712. drq->assoc_qid = cq->queue_id;
  11713. drq->subtype = subtype;
  11714. drq->host_index = 0;
  11715. drq->hba_index = 0;
  11716. /* link the header and data RQs onto the parent cq child list */
  11717. list_add_tail(&hrq->list, &cq->child_list);
  11718. list_add_tail(&drq->list, &cq->child_list);
  11719. out:
  11720. mempool_free(mbox, phba->mbox_mem_pool);
  11721. return status;
  11722. }
  11723. /**
  11724. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11725. * @eq: The queue structure associated with the queue to destroy.
  11726. *
  11727. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11728. * command, specific to the type of queue, to the HBA.
  11729. *
  11730. * The @eq struct is used to get the queue ID of the queue to destroy.
  11731. *
  11732. * On success this function will return a zero. If the queue destroy mailbox
  11733. * command fails this function will return -ENXIO.
  11734. **/
  11735. uint32_t
  11736. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11737. {
  11738. LPFC_MBOXQ_t *mbox;
  11739. int rc, length, status = 0;
  11740. uint32_t shdr_status, shdr_add_status;
  11741. union lpfc_sli4_cfg_shdr *shdr;
  11742. /* sanity check on queue memory */
  11743. if (!eq)
  11744. return -ENODEV;
  11745. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11746. if (!mbox)
  11747. return -ENOMEM;
  11748. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11749. sizeof(struct lpfc_sli4_cfg_mhdr));
  11750. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11751. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11752. length, LPFC_SLI4_MBX_EMBED);
  11753. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11754. eq->queue_id);
  11755. mbox->vport = eq->phba->pport;
  11756. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11757. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11758. /* The IOCTL status is embedded in the mailbox subheader. */
  11759. shdr = (union lpfc_sli4_cfg_shdr *)
  11760. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11761. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11762. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11763. if (shdr_status || shdr_add_status || rc) {
  11764. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11765. "2505 EQ_DESTROY mailbox failed with "
  11766. "status x%x add_status x%x, mbx status x%x\n",
  11767. shdr_status, shdr_add_status, rc);
  11768. status = -ENXIO;
  11769. }
  11770. /* Remove eq from any list */
  11771. list_del_init(&eq->list);
  11772. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11773. return status;
  11774. }
  11775. /**
  11776. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11777. * @cq: The queue structure associated with the queue to destroy.
  11778. *
  11779. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11780. * command, specific to the type of queue, to the HBA.
  11781. *
  11782. * The @cq struct is used to get the queue ID of the queue to destroy.
  11783. *
  11784. * On success this function will return a zero. If the queue destroy mailbox
  11785. * command fails this function will return -ENXIO.
  11786. **/
  11787. uint32_t
  11788. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11789. {
  11790. LPFC_MBOXQ_t *mbox;
  11791. int rc, length, status = 0;
  11792. uint32_t shdr_status, shdr_add_status;
  11793. union lpfc_sli4_cfg_shdr *shdr;
  11794. /* sanity check on queue memory */
  11795. if (!cq)
  11796. return -ENODEV;
  11797. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11798. if (!mbox)
  11799. return -ENOMEM;
  11800. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11801. sizeof(struct lpfc_sli4_cfg_mhdr));
  11802. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11803. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11804. length, LPFC_SLI4_MBX_EMBED);
  11805. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11806. cq->queue_id);
  11807. mbox->vport = cq->phba->pport;
  11808. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11809. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11810. /* The IOCTL status is embedded in the mailbox subheader. */
  11811. shdr = (union lpfc_sli4_cfg_shdr *)
  11812. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11813. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11814. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11815. if (shdr_status || shdr_add_status || rc) {
  11816. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11817. "2506 CQ_DESTROY mailbox failed with "
  11818. "status x%x add_status x%x, mbx status x%x\n",
  11819. shdr_status, shdr_add_status, rc);
  11820. status = -ENXIO;
  11821. }
  11822. /* Remove cq from any list */
  11823. list_del_init(&cq->list);
  11824. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11825. return status;
  11826. }
  11827. /**
  11828. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11829. * @qm: The queue structure associated with the queue to destroy.
  11830. *
  11831. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11832. * command, specific to the type of queue, to the HBA.
  11833. *
  11834. * The @mq struct is used to get the queue ID of the queue to destroy.
  11835. *
  11836. * On success this function will return a zero. If the queue destroy mailbox
  11837. * command fails this function will return -ENXIO.
  11838. **/
  11839. uint32_t
  11840. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11841. {
  11842. LPFC_MBOXQ_t *mbox;
  11843. int rc, length, status = 0;
  11844. uint32_t shdr_status, shdr_add_status;
  11845. union lpfc_sli4_cfg_shdr *shdr;
  11846. /* sanity check on queue memory */
  11847. if (!mq)
  11848. return -ENODEV;
  11849. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11850. if (!mbox)
  11851. return -ENOMEM;
  11852. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11853. sizeof(struct lpfc_sli4_cfg_mhdr));
  11854. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11855. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11856. length, LPFC_SLI4_MBX_EMBED);
  11857. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11858. mq->queue_id);
  11859. mbox->vport = mq->phba->pport;
  11860. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11861. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11862. /* The IOCTL status is embedded in the mailbox subheader. */
  11863. shdr = (union lpfc_sli4_cfg_shdr *)
  11864. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11865. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11866. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11867. if (shdr_status || shdr_add_status || rc) {
  11868. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11869. "2507 MQ_DESTROY mailbox failed with "
  11870. "status x%x add_status x%x, mbx status x%x\n",
  11871. shdr_status, shdr_add_status, rc);
  11872. status = -ENXIO;
  11873. }
  11874. /* Remove mq from any list */
  11875. list_del_init(&mq->list);
  11876. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11877. return status;
  11878. }
  11879. /**
  11880. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11881. * @wq: The queue structure associated with the queue to destroy.
  11882. *
  11883. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11884. * command, specific to the type of queue, to the HBA.
  11885. *
  11886. * The @wq struct is used to get the queue ID of the queue to destroy.
  11887. *
  11888. * On success this function will return a zero. If the queue destroy mailbox
  11889. * command fails this function will return -ENXIO.
  11890. **/
  11891. uint32_t
  11892. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11893. {
  11894. LPFC_MBOXQ_t *mbox;
  11895. int rc, length, status = 0;
  11896. uint32_t shdr_status, shdr_add_status;
  11897. union lpfc_sli4_cfg_shdr *shdr;
  11898. /* sanity check on queue memory */
  11899. if (!wq)
  11900. return -ENODEV;
  11901. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11902. if (!mbox)
  11903. return -ENOMEM;
  11904. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11905. sizeof(struct lpfc_sli4_cfg_mhdr));
  11906. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11907. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11908. length, LPFC_SLI4_MBX_EMBED);
  11909. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11910. wq->queue_id);
  11911. mbox->vport = wq->phba->pport;
  11912. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11913. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11914. shdr = (union lpfc_sli4_cfg_shdr *)
  11915. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11916. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11917. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11918. if (shdr_status || shdr_add_status || rc) {
  11919. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11920. "2508 WQ_DESTROY mailbox failed with "
  11921. "status x%x add_status x%x, mbx status x%x\n",
  11922. shdr_status, shdr_add_status, rc);
  11923. status = -ENXIO;
  11924. }
  11925. /* Remove wq from any list */
  11926. list_del_init(&wq->list);
  11927. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11928. return status;
  11929. }
  11930. /**
  11931. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11932. * @rq: The queue structure associated with the queue to destroy.
  11933. *
  11934. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11935. * command, specific to the type of queue, to the HBA.
  11936. *
  11937. * The @rq struct is used to get the queue ID of the queue to destroy.
  11938. *
  11939. * On success this function will return a zero. If the queue destroy mailbox
  11940. * command fails this function will return -ENXIO.
  11941. **/
  11942. uint32_t
  11943. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11944. struct lpfc_queue *drq)
  11945. {
  11946. LPFC_MBOXQ_t *mbox;
  11947. int rc, length, status = 0;
  11948. uint32_t shdr_status, shdr_add_status;
  11949. union lpfc_sli4_cfg_shdr *shdr;
  11950. /* sanity check on queue memory */
  11951. if (!hrq || !drq)
  11952. return -ENODEV;
  11953. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11954. if (!mbox)
  11955. return -ENOMEM;
  11956. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11957. sizeof(struct lpfc_sli4_cfg_mhdr));
  11958. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11959. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11960. length, LPFC_SLI4_MBX_EMBED);
  11961. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11962. hrq->queue_id);
  11963. mbox->vport = hrq->phba->pport;
  11964. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11965. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11966. /* The IOCTL status is embedded in the mailbox subheader. */
  11967. shdr = (union lpfc_sli4_cfg_shdr *)
  11968. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11969. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11970. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11971. if (shdr_status || shdr_add_status || rc) {
  11972. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11973. "2509 RQ_DESTROY mailbox failed with "
  11974. "status x%x add_status x%x, mbx status x%x\n",
  11975. shdr_status, shdr_add_status, rc);
  11976. if (rc != MBX_TIMEOUT)
  11977. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11978. return -ENXIO;
  11979. }
  11980. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11981. drq->queue_id);
  11982. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11983. shdr = (union lpfc_sli4_cfg_shdr *)
  11984. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11985. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11986. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11987. if (shdr_status || shdr_add_status || rc) {
  11988. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11989. "2510 RQ_DESTROY mailbox failed with "
  11990. "status x%x add_status x%x, mbx status x%x\n",
  11991. shdr_status, shdr_add_status, rc);
  11992. status = -ENXIO;
  11993. }
  11994. list_del_init(&hrq->list);
  11995. list_del_init(&drq->list);
  11996. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11997. return status;
  11998. }
  11999. /**
  12000. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12001. * @phba: The virtual port for which this call being executed.
  12002. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12003. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12004. * @xritag: the xritag that ties this io to the SGL pages.
  12005. *
  12006. * This routine will post the sgl pages for the IO that has the xritag
  12007. * that is in the iocbq structure. The xritag is assigned during iocbq
  12008. * creation and persists for as long as the driver is loaded.
  12009. * if the caller has fewer than 256 scatter gather segments to map then
  12010. * pdma_phys_addr1 should be 0.
  12011. * If the caller needs to map more than 256 scatter gather segment then
  12012. * pdma_phys_addr1 should be a valid physical address.
  12013. * physical address for SGLs must be 64 byte aligned.
  12014. * If you are going to map 2 SGL's then the first one must have 256 entries
  12015. * the second sgl can have between 1 and 256 entries.
  12016. *
  12017. * Return codes:
  12018. * 0 - Success
  12019. * -ENXIO, -ENOMEM - Failure
  12020. **/
  12021. int
  12022. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12023. dma_addr_t pdma_phys_addr0,
  12024. dma_addr_t pdma_phys_addr1,
  12025. uint16_t xritag)
  12026. {
  12027. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12028. LPFC_MBOXQ_t *mbox;
  12029. int rc;
  12030. uint32_t shdr_status, shdr_add_status;
  12031. uint32_t mbox_tmo;
  12032. union lpfc_sli4_cfg_shdr *shdr;
  12033. if (xritag == NO_XRI) {
  12034. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12035. "0364 Invalid param:\n");
  12036. return -EINVAL;
  12037. }
  12038. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12039. if (!mbox)
  12040. return -ENOMEM;
  12041. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12042. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12043. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12044. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12045. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12046. &mbox->u.mqe.un.post_sgl_pages;
  12047. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12048. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12049. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12050. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12051. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12052. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12053. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12054. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12055. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12056. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12057. if (!phba->sli4_hba.intr_enable)
  12058. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12059. else {
  12060. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12061. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12062. }
  12063. /* The IOCTL status is embedded in the mailbox subheader. */
  12064. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12065. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12066. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12067. if (rc != MBX_TIMEOUT)
  12068. mempool_free(mbox, phba->mbox_mem_pool);
  12069. if (shdr_status || shdr_add_status || rc) {
  12070. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12071. "2511 POST_SGL mailbox failed with "
  12072. "status x%x add_status x%x, mbx status x%x\n",
  12073. shdr_status, shdr_add_status, rc);
  12074. rc = -ENXIO;
  12075. }
  12076. return 0;
  12077. }
  12078. /**
  12079. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12080. * @phba: pointer to lpfc hba data structure.
  12081. *
  12082. * This routine is invoked to post rpi header templates to the
  12083. * HBA consistent with the SLI-4 interface spec. This routine
  12084. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12085. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12086. *
  12087. * Returns
  12088. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12089. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12090. **/
  12091. uint16_t
  12092. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12093. {
  12094. unsigned long xri;
  12095. /*
  12096. * Fetch the next logical xri. Because this index is logical,
  12097. * the driver starts at 0 each time.
  12098. */
  12099. spin_lock_irq(&phba->hbalock);
  12100. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12101. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12102. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12103. spin_unlock_irq(&phba->hbalock);
  12104. return NO_XRI;
  12105. } else {
  12106. set_bit(xri, phba->sli4_hba.xri_bmask);
  12107. phba->sli4_hba.max_cfg_param.xri_used++;
  12108. }
  12109. spin_unlock_irq(&phba->hbalock);
  12110. return xri;
  12111. }
  12112. /**
  12113. * lpfc_sli4_free_xri - Release an xri for reuse.
  12114. * @phba: pointer to lpfc hba data structure.
  12115. *
  12116. * This routine is invoked to release an xri to the pool of
  12117. * available rpis maintained by the driver.
  12118. **/
  12119. void
  12120. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12121. {
  12122. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12123. phba->sli4_hba.max_cfg_param.xri_used--;
  12124. }
  12125. }
  12126. /**
  12127. * lpfc_sli4_free_xri - Release an xri for reuse.
  12128. * @phba: pointer to lpfc hba data structure.
  12129. *
  12130. * This routine is invoked to release an xri to the pool of
  12131. * available rpis maintained by the driver.
  12132. **/
  12133. void
  12134. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12135. {
  12136. spin_lock_irq(&phba->hbalock);
  12137. __lpfc_sli4_free_xri(phba, xri);
  12138. spin_unlock_irq(&phba->hbalock);
  12139. }
  12140. /**
  12141. * lpfc_sli4_next_xritag - Get an xritag for the io
  12142. * @phba: Pointer to HBA context object.
  12143. *
  12144. * This function gets an xritag for the iocb. If there is no unused xritag
  12145. * it will return 0xffff.
  12146. * The function returns the allocated xritag if successful, else returns zero.
  12147. * Zero is not a valid xritag.
  12148. * The caller is not required to hold any lock.
  12149. **/
  12150. uint16_t
  12151. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12152. {
  12153. uint16_t xri_index;
  12154. xri_index = lpfc_sli4_alloc_xri(phba);
  12155. if (xri_index != NO_XRI)
  12156. return xri_index;
  12157. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12158. "2004 Failed to allocate XRI.last XRITAG is %d"
  12159. " Max XRI is %d, Used XRI is %d\n",
  12160. xri_index,
  12161. phba->sli4_hba.max_cfg_param.max_xri,
  12162. phba->sli4_hba.max_cfg_param.xri_used);
  12163. return NO_XRI;
  12164. }
  12165. /**
  12166. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12167. * @phba: pointer to lpfc hba data structure.
  12168. * @post_sgl_list: pointer to els sgl entry list.
  12169. * @count: number of els sgl entries on the list.
  12170. *
  12171. * This routine is invoked to post a block of driver's sgl pages to the
  12172. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12173. * is only called when the driver is loading and after all IO has been
  12174. * stopped.
  12175. **/
  12176. static int
  12177. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12178. struct list_head *post_sgl_list,
  12179. int post_cnt)
  12180. {
  12181. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12182. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12183. struct sgl_page_pairs *sgl_pg_pairs;
  12184. void *viraddr;
  12185. LPFC_MBOXQ_t *mbox;
  12186. uint32_t reqlen, alloclen, pg_pairs;
  12187. uint32_t mbox_tmo;
  12188. uint16_t xritag_start = 0;
  12189. int rc = 0;
  12190. uint32_t shdr_status, shdr_add_status;
  12191. union lpfc_sli4_cfg_shdr *shdr;
  12192. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12193. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12194. if (reqlen > SLI4_PAGE_SIZE) {
  12195. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12196. "2559 Block sgl registration required DMA "
  12197. "size (%d) great than a page\n", reqlen);
  12198. return -ENOMEM;
  12199. }
  12200. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12201. if (!mbox)
  12202. return -ENOMEM;
  12203. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12204. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12205. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12206. LPFC_SLI4_MBX_NEMBED);
  12207. if (alloclen < reqlen) {
  12208. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12209. "0285 Allocated DMA memory size (%d) is "
  12210. "less than the requested DMA memory "
  12211. "size (%d)\n", alloclen, reqlen);
  12212. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12213. return -ENOMEM;
  12214. }
  12215. /* Set up the SGL pages in the non-embedded DMA pages */
  12216. viraddr = mbox->sge_array->addr[0];
  12217. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12218. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12219. pg_pairs = 0;
  12220. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12221. /* Set up the sge entry */
  12222. sgl_pg_pairs->sgl_pg0_addr_lo =
  12223. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12224. sgl_pg_pairs->sgl_pg0_addr_hi =
  12225. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12226. sgl_pg_pairs->sgl_pg1_addr_lo =
  12227. cpu_to_le32(putPaddrLow(0));
  12228. sgl_pg_pairs->sgl_pg1_addr_hi =
  12229. cpu_to_le32(putPaddrHigh(0));
  12230. /* Keep the first xritag on the list */
  12231. if (pg_pairs == 0)
  12232. xritag_start = sglq_entry->sli4_xritag;
  12233. sgl_pg_pairs++;
  12234. pg_pairs++;
  12235. }
  12236. /* Complete initialization and perform endian conversion. */
  12237. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12238. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12239. sgl->word0 = cpu_to_le32(sgl->word0);
  12240. if (!phba->sli4_hba.intr_enable)
  12241. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12242. else {
  12243. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12244. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12245. }
  12246. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12247. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12248. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12249. if (rc != MBX_TIMEOUT)
  12250. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12251. if (shdr_status || shdr_add_status || rc) {
  12252. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12253. "2513 POST_SGL_BLOCK mailbox command failed "
  12254. "status x%x add_status x%x mbx status x%x\n",
  12255. shdr_status, shdr_add_status, rc);
  12256. rc = -ENXIO;
  12257. }
  12258. return rc;
  12259. }
  12260. /**
  12261. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12262. * @phba: pointer to lpfc hba data structure.
  12263. * @sblist: pointer to scsi buffer list.
  12264. * @count: number of scsi buffers on the list.
  12265. *
  12266. * This routine is invoked to post a block of @count scsi sgl pages from a
  12267. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12268. * No Lock is held.
  12269. *
  12270. **/
  12271. int
  12272. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12273. struct list_head *sblist,
  12274. int count)
  12275. {
  12276. struct lpfc_scsi_buf *psb;
  12277. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12278. struct sgl_page_pairs *sgl_pg_pairs;
  12279. void *viraddr;
  12280. LPFC_MBOXQ_t *mbox;
  12281. uint32_t reqlen, alloclen, pg_pairs;
  12282. uint32_t mbox_tmo;
  12283. uint16_t xritag_start = 0;
  12284. int rc = 0;
  12285. uint32_t shdr_status, shdr_add_status;
  12286. dma_addr_t pdma_phys_bpl1;
  12287. union lpfc_sli4_cfg_shdr *shdr;
  12288. /* Calculate the requested length of the dma memory */
  12289. reqlen = count * sizeof(struct sgl_page_pairs) +
  12290. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12291. if (reqlen > SLI4_PAGE_SIZE) {
  12292. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12293. "0217 Block sgl registration required DMA "
  12294. "size (%d) great than a page\n", reqlen);
  12295. return -ENOMEM;
  12296. }
  12297. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12298. if (!mbox) {
  12299. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12300. "0283 Failed to allocate mbox cmd memory\n");
  12301. return -ENOMEM;
  12302. }
  12303. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12304. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12305. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12306. LPFC_SLI4_MBX_NEMBED);
  12307. if (alloclen < reqlen) {
  12308. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12309. "2561 Allocated DMA memory size (%d) is "
  12310. "less than the requested DMA memory "
  12311. "size (%d)\n", alloclen, reqlen);
  12312. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12313. return -ENOMEM;
  12314. }
  12315. /* Get the first SGE entry from the non-embedded DMA memory */
  12316. viraddr = mbox->sge_array->addr[0];
  12317. /* Set up the SGL pages in the non-embedded DMA pages */
  12318. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12319. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12320. pg_pairs = 0;
  12321. list_for_each_entry(psb, sblist, list) {
  12322. /* Set up the sge entry */
  12323. sgl_pg_pairs->sgl_pg0_addr_lo =
  12324. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12325. sgl_pg_pairs->sgl_pg0_addr_hi =
  12326. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12327. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12328. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12329. else
  12330. pdma_phys_bpl1 = 0;
  12331. sgl_pg_pairs->sgl_pg1_addr_lo =
  12332. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12333. sgl_pg_pairs->sgl_pg1_addr_hi =
  12334. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12335. /* Keep the first xritag on the list */
  12336. if (pg_pairs == 0)
  12337. xritag_start = psb->cur_iocbq.sli4_xritag;
  12338. sgl_pg_pairs++;
  12339. pg_pairs++;
  12340. }
  12341. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12342. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12343. /* Perform endian conversion if necessary */
  12344. sgl->word0 = cpu_to_le32(sgl->word0);
  12345. if (!phba->sli4_hba.intr_enable)
  12346. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12347. else {
  12348. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12349. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12350. }
  12351. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12352. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12353. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12354. if (rc != MBX_TIMEOUT)
  12355. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12356. if (shdr_status || shdr_add_status || rc) {
  12357. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12358. "2564 POST_SGL_BLOCK mailbox command failed "
  12359. "status x%x add_status x%x mbx status x%x\n",
  12360. shdr_status, shdr_add_status, rc);
  12361. rc = -ENXIO;
  12362. }
  12363. return rc;
  12364. }
  12365. /**
  12366. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12367. * @phba: pointer to lpfc_hba struct that the frame was received on
  12368. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12369. *
  12370. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12371. * valid type of frame that the LPFC driver will handle. This function will
  12372. * return a zero if the frame is a valid frame or a non zero value when the
  12373. * frame does not pass the check.
  12374. **/
  12375. static int
  12376. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12377. {
  12378. /* make rctl_names static to save stack space */
  12379. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12380. char *type_names[] = FC_TYPE_NAMES_INIT;
  12381. struct fc_vft_header *fc_vft_hdr;
  12382. uint32_t *header = (uint32_t *) fc_hdr;
  12383. switch (fc_hdr->fh_r_ctl) {
  12384. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12385. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12386. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12387. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12388. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12389. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12390. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12391. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12392. case FC_RCTL_ELS_REQ: /* extended link services request */
  12393. case FC_RCTL_ELS_REP: /* extended link services reply */
  12394. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12395. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12396. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12397. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12398. case FC_RCTL_BA_RMC: /* remove connection */
  12399. case FC_RCTL_BA_ACC: /* basic accept */
  12400. case FC_RCTL_BA_RJT: /* basic reject */
  12401. case FC_RCTL_BA_PRMT:
  12402. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12403. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12404. case FC_RCTL_P_RJT: /* port reject */
  12405. case FC_RCTL_F_RJT: /* fabric reject */
  12406. case FC_RCTL_P_BSY: /* port busy */
  12407. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12408. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12409. case FC_RCTL_LCR: /* link credit reset */
  12410. case FC_RCTL_END: /* end */
  12411. break;
  12412. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12413. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12414. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12415. return lpfc_fc_frame_check(phba, fc_hdr);
  12416. default:
  12417. goto drop;
  12418. }
  12419. switch (fc_hdr->fh_type) {
  12420. case FC_TYPE_BLS:
  12421. case FC_TYPE_ELS:
  12422. case FC_TYPE_FCP:
  12423. case FC_TYPE_CT:
  12424. break;
  12425. case FC_TYPE_IP:
  12426. case FC_TYPE_ILS:
  12427. default:
  12428. goto drop;
  12429. }
  12430. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12431. "2538 Received frame rctl:%s type:%s "
  12432. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12433. rctl_names[fc_hdr->fh_r_ctl],
  12434. type_names[fc_hdr->fh_type],
  12435. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12436. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12437. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12438. return 0;
  12439. drop:
  12440. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12441. "2539 Dropped frame rctl:%s type:%s\n",
  12442. rctl_names[fc_hdr->fh_r_ctl],
  12443. type_names[fc_hdr->fh_type]);
  12444. return 1;
  12445. }
  12446. /**
  12447. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12448. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12449. *
  12450. * This function processes the FC header to retrieve the VFI from the VF
  12451. * header, if one exists. This function will return the VFI if one exists
  12452. * or 0 if no VSAN Header exists.
  12453. **/
  12454. static uint32_t
  12455. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12456. {
  12457. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12458. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12459. return 0;
  12460. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12461. }
  12462. /**
  12463. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12464. * @phba: Pointer to the HBA structure to search for the vport on
  12465. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12466. * @fcfi: The FC Fabric ID that the frame came from
  12467. *
  12468. * This function searches the @phba for a vport that matches the content of the
  12469. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12470. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12471. * returns the matching vport pointer or NULL if unable to match frame to a
  12472. * vport.
  12473. **/
  12474. static struct lpfc_vport *
  12475. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12476. uint16_t fcfi)
  12477. {
  12478. struct lpfc_vport **vports;
  12479. struct lpfc_vport *vport = NULL;
  12480. int i;
  12481. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12482. fc_hdr->fh_d_id[1] << 8 |
  12483. fc_hdr->fh_d_id[2]);
  12484. if (did == Fabric_DID)
  12485. return phba->pport;
  12486. vports = lpfc_create_vport_work_array(phba);
  12487. if (vports != NULL)
  12488. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12489. if (phba->fcf.fcfi == fcfi &&
  12490. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12491. vports[i]->fc_myDID == did) {
  12492. vport = vports[i];
  12493. break;
  12494. }
  12495. }
  12496. lpfc_destroy_vport_work_array(phba, vports);
  12497. return vport;
  12498. }
  12499. /**
  12500. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12501. * @vport: The vport to work on.
  12502. *
  12503. * This function updates the receive sequence time stamp for this vport. The
  12504. * receive sequence time stamp indicates the time that the last frame of the
  12505. * the sequence that has been idle for the longest amount of time was received.
  12506. * the driver uses this time stamp to indicate if any received sequences have
  12507. * timed out.
  12508. **/
  12509. void
  12510. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12511. {
  12512. struct lpfc_dmabuf *h_buf;
  12513. struct hbq_dmabuf *dmabuf = NULL;
  12514. /* get the oldest sequence on the rcv list */
  12515. h_buf = list_get_first(&vport->rcv_buffer_list,
  12516. struct lpfc_dmabuf, list);
  12517. if (!h_buf)
  12518. return;
  12519. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12520. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12521. }
  12522. /**
  12523. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12524. * @vport: The vport that the received sequences were sent to.
  12525. *
  12526. * This function cleans up all outstanding received sequences. This is called
  12527. * by the driver when a link event or user action invalidates all the received
  12528. * sequences.
  12529. **/
  12530. void
  12531. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12532. {
  12533. struct lpfc_dmabuf *h_buf, *hnext;
  12534. struct lpfc_dmabuf *d_buf, *dnext;
  12535. struct hbq_dmabuf *dmabuf = NULL;
  12536. /* start with the oldest sequence on the rcv list */
  12537. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12538. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12539. list_del_init(&dmabuf->hbuf.list);
  12540. list_for_each_entry_safe(d_buf, dnext,
  12541. &dmabuf->dbuf.list, list) {
  12542. list_del_init(&d_buf->list);
  12543. lpfc_in_buf_free(vport->phba, d_buf);
  12544. }
  12545. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12546. }
  12547. }
  12548. /**
  12549. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12550. * @vport: The vport that the received sequences were sent to.
  12551. *
  12552. * This function determines whether any received sequences have timed out by
  12553. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12554. * indicates that there is at least one timed out sequence this routine will
  12555. * go through the received sequences one at a time from most inactive to most
  12556. * active to determine which ones need to be cleaned up. Once it has determined
  12557. * that a sequence needs to be cleaned up it will simply free up the resources
  12558. * without sending an abort.
  12559. **/
  12560. void
  12561. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12562. {
  12563. struct lpfc_dmabuf *h_buf, *hnext;
  12564. struct lpfc_dmabuf *d_buf, *dnext;
  12565. struct hbq_dmabuf *dmabuf = NULL;
  12566. unsigned long timeout;
  12567. int abort_count = 0;
  12568. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12569. vport->rcv_buffer_time_stamp);
  12570. if (list_empty(&vport->rcv_buffer_list) ||
  12571. time_before(jiffies, timeout))
  12572. return;
  12573. /* start with the oldest sequence on the rcv list */
  12574. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12575. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12576. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12577. dmabuf->time_stamp);
  12578. if (time_before(jiffies, timeout))
  12579. break;
  12580. abort_count++;
  12581. list_del_init(&dmabuf->hbuf.list);
  12582. list_for_each_entry_safe(d_buf, dnext,
  12583. &dmabuf->dbuf.list, list) {
  12584. list_del_init(&d_buf->list);
  12585. lpfc_in_buf_free(vport->phba, d_buf);
  12586. }
  12587. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12588. }
  12589. if (abort_count)
  12590. lpfc_update_rcv_time_stamp(vport);
  12591. }
  12592. /**
  12593. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12594. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12595. *
  12596. * This function searches through the existing incomplete sequences that have
  12597. * been sent to this @vport. If the frame matches one of the incomplete
  12598. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12599. * make up that sequence. If no sequence is found that matches this frame then
  12600. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12601. * This function returns a pointer to the first dmabuf in the sequence list that
  12602. * the frame was linked to.
  12603. **/
  12604. static struct hbq_dmabuf *
  12605. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12606. {
  12607. struct fc_frame_header *new_hdr;
  12608. struct fc_frame_header *temp_hdr;
  12609. struct lpfc_dmabuf *d_buf;
  12610. struct lpfc_dmabuf *h_buf;
  12611. struct hbq_dmabuf *seq_dmabuf = NULL;
  12612. struct hbq_dmabuf *temp_dmabuf = NULL;
  12613. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12614. dmabuf->time_stamp = jiffies;
  12615. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12616. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12617. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12618. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12619. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12620. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12621. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12622. continue;
  12623. /* found a pending sequence that matches this frame */
  12624. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12625. break;
  12626. }
  12627. if (!seq_dmabuf) {
  12628. /*
  12629. * This indicates first frame received for this sequence.
  12630. * Queue the buffer on the vport's rcv_buffer_list.
  12631. */
  12632. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12633. lpfc_update_rcv_time_stamp(vport);
  12634. return dmabuf;
  12635. }
  12636. temp_hdr = seq_dmabuf->hbuf.virt;
  12637. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12638. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12639. list_del_init(&seq_dmabuf->hbuf.list);
  12640. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12641. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12642. lpfc_update_rcv_time_stamp(vport);
  12643. return dmabuf;
  12644. }
  12645. /* move this sequence to the tail to indicate a young sequence */
  12646. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12647. seq_dmabuf->time_stamp = jiffies;
  12648. lpfc_update_rcv_time_stamp(vport);
  12649. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12650. temp_hdr = dmabuf->hbuf.virt;
  12651. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12652. return seq_dmabuf;
  12653. }
  12654. /* find the correct place in the sequence to insert this frame */
  12655. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12656. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12657. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12658. /*
  12659. * If the frame's sequence count is greater than the frame on
  12660. * the list then insert the frame right after this frame
  12661. */
  12662. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12663. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12664. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12665. return seq_dmabuf;
  12666. }
  12667. }
  12668. return NULL;
  12669. }
  12670. /**
  12671. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12672. * @vport: pointer to a vitural port
  12673. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12674. *
  12675. * This function tries to abort from the partially assembed sequence, described
  12676. * by the information from basic abbort @dmabuf. It checks to see whether such
  12677. * partially assembled sequence held by the driver. If so, it shall free up all
  12678. * the frames from the partially assembled sequence.
  12679. *
  12680. * Return
  12681. * true -- if there is matching partially assembled sequence present and all
  12682. * the frames freed with the sequence;
  12683. * false -- if there is no matching partially assembled sequence present so
  12684. * nothing got aborted in the lower layer driver
  12685. **/
  12686. static bool
  12687. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12688. struct hbq_dmabuf *dmabuf)
  12689. {
  12690. struct fc_frame_header *new_hdr;
  12691. struct fc_frame_header *temp_hdr;
  12692. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12693. struct hbq_dmabuf *seq_dmabuf = NULL;
  12694. /* Use the hdr_buf to find the sequence that matches this frame */
  12695. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12696. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12697. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12698. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12699. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12700. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12701. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12702. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12703. continue;
  12704. /* found a pending sequence that matches this frame */
  12705. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12706. break;
  12707. }
  12708. /* Free up all the frames from the partially assembled sequence */
  12709. if (seq_dmabuf) {
  12710. list_for_each_entry_safe(d_buf, n_buf,
  12711. &seq_dmabuf->dbuf.list, list) {
  12712. list_del_init(&d_buf->list);
  12713. lpfc_in_buf_free(vport->phba, d_buf);
  12714. }
  12715. return true;
  12716. }
  12717. return false;
  12718. }
  12719. /**
  12720. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12721. * @phba: Pointer to HBA context object.
  12722. * @cmd_iocbq: pointer to the command iocbq structure.
  12723. * @rsp_iocbq: pointer to the response iocbq structure.
  12724. *
  12725. * This function handles the sequence abort response iocb command complete
  12726. * event. It properly releases the memory allocated to the sequence abort
  12727. * accept iocb.
  12728. **/
  12729. static void
  12730. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12731. struct lpfc_iocbq *cmd_iocbq,
  12732. struct lpfc_iocbq *rsp_iocbq)
  12733. {
  12734. if (cmd_iocbq)
  12735. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12736. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12737. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12738. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12739. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12740. rsp_iocbq->iocb.ulpStatus,
  12741. rsp_iocbq->iocb.un.ulpWord[4]);
  12742. }
  12743. /**
  12744. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12745. * @phba: Pointer to HBA context object.
  12746. * @xri: xri id in transaction.
  12747. *
  12748. * This function validates the xri maps to the known range of XRIs allocated an
  12749. * used by the driver.
  12750. **/
  12751. uint16_t
  12752. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12753. uint16_t xri)
  12754. {
  12755. int i;
  12756. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12757. if (xri == phba->sli4_hba.xri_ids[i])
  12758. return i;
  12759. }
  12760. return NO_XRI;
  12761. }
  12762. /**
  12763. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12764. * @phba: Pointer to HBA context object.
  12765. * @fc_hdr: pointer to a FC frame header.
  12766. *
  12767. * This function sends a basic response to a previous unsol sequence abort
  12768. * event after aborting the sequence handling.
  12769. **/
  12770. static void
  12771. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12772. struct fc_frame_header *fc_hdr)
  12773. {
  12774. struct lpfc_iocbq *ctiocb = NULL;
  12775. struct lpfc_nodelist *ndlp;
  12776. uint16_t oxid, rxid;
  12777. uint32_t sid, fctl;
  12778. IOCB_t *icmd;
  12779. int rc;
  12780. if (!lpfc_is_link_up(phba))
  12781. return;
  12782. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12783. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12784. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12785. ndlp = lpfc_findnode_did(phba->pport, sid);
  12786. if (!ndlp) {
  12787. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12788. "1268 Find ndlp returned NULL for oxid:x%x "
  12789. "SID:x%x\n", oxid, sid);
  12790. return;
  12791. }
  12792. if (lpfc_sli4_xri_inrange(phba, rxid))
  12793. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12794. /* Allocate buffer for rsp iocb */
  12795. ctiocb = lpfc_sli_get_iocbq(phba);
  12796. if (!ctiocb)
  12797. return;
  12798. /* Extract the F_CTL field from FC_HDR */
  12799. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12800. icmd = &ctiocb->iocb;
  12801. icmd->un.xseq64.bdl.bdeSize = 0;
  12802. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12803. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12804. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12805. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12806. /* Fill in the rest of iocb fields */
  12807. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12808. icmd->ulpBdeCount = 0;
  12809. icmd->ulpLe = 1;
  12810. icmd->ulpClass = CLASS3;
  12811. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12812. ctiocb->context1 = ndlp;
  12813. ctiocb->iocb_cmpl = NULL;
  12814. ctiocb->vport = phba->pport;
  12815. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12816. ctiocb->sli4_lxritag = NO_XRI;
  12817. ctiocb->sli4_xritag = NO_XRI;
  12818. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12819. * send a BLS_RJT. The driver no longer has that exchange.
  12820. * Override the IOCB for a BA_RJT.
  12821. */
  12822. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  12823. phba->sli4_hba.max_cfg_param.xri_base) ||
  12824. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12825. phba->sli4_hba.max_cfg_param.xri_base)) {
  12826. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12827. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12828. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12829. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12830. }
  12831. if (fctl & FC_FC_EX_CTX) {
  12832. /* ABTS sent by responder to CT exchange, construction
  12833. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12834. * field and RX_ID from ABTS for RX_ID field.
  12835. */
  12836. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12837. } else {
  12838. /* ABTS sent by initiator to CT exchange, construction
  12839. * of BA_ACC will need to allocate a new XRI as for the
  12840. * XRI_TAG field.
  12841. */
  12842. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12843. }
  12844. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12845. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12846. /* Xmit CT abts response on exchange <xid> */
  12847. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12848. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12849. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12850. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12851. if (rc == IOCB_ERROR) {
  12852. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12853. "2925 Failed to issue CT ABTS RSP x%x on "
  12854. "xri x%x, Data x%x\n",
  12855. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12856. phba->link_state);
  12857. lpfc_sli_release_iocbq(phba, ctiocb);
  12858. }
  12859. }
  12860. /**
  12861. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12862. * @vport: Pointer to the vport on which this sequence was received
  12863. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12864. *
  12865. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12866. * receive sequence is only partially assembed by the driver, it shall abort
  12867. * the partially assembled frames for the sequence. Otherwise, if the
  12868. * unsolicited receive sequence has been completely assembled and passed to
  12869. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  12870. * unsolicited sequence has been aborted. After that, it will issue a basic
  12871. * accept to accept the abort.
  12872. **/
  12873. void
  12874. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  12875. struct hbq_dmabuf *dmabuf)
  12876. {
  12877. struct lpfc_hba *phba = vport->phba;
  12878. struct fc_frame_header fc_hdr;
  12879. uint32_t fctl;
  12880. bool abts_par;
  12881. /* Make a copy of fc_hdr before the dmabuf being released */
  12882. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  12883. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  12884. if (fctl & FC_FC_EX_CTX) {
  12885. /*
  12886. * ABTS sent by responder to exchange, just free the buffer
  12887. */
  12888. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12889. } else {
  12890. /*
  12891. * ABTS sent by initiator to exchange, need to do cleanup
  12892. */
  12893. /* Try to abort partially assembled seq */
  12894. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  12895. /* Send abort to ULP if partially seq abort failed */
  12896. if (abts_par == false)
  12897. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  12898. else
  12899. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12900. }
  12901. /* Send basic accept (BA_ACC) to the abort requester */
  12902. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  12903. }
  12904. /**
  12905. * lpfc_seq_complete - Indicates if a sequence is complete
  12906. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12907. *
  12908. * This function checks the sequence, starting with the frame described by
  12909. * @dmabuf, to see if all the frames associated with this sequence are present.
  12910. * the frames associated with this sequence are linked to the @dmabuf using the
  12911. * dbuf list. This function looks for two major things. 1) That the first frame
  12912. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  12913. * set. 3) That there are no holes in the sequence count. The function will
  12914. * return 1 when the sequence is complete, otherwise it will return 0.
  12915. **/
  12916. static int
  12917. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  12918. {
  12919. struct fc_frame_header *hdr;
  12920. struct lpfc_dmabuf *d_buf;
  12921. struct hbq_dmabuf *seq_dmabuf;
  12922. uint32_t fctl;
  12923. int seq_count = 0;
  12924. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12925. /* make sure first fame of sequence has a sequence count of zero */
  12926. if (hdr->fh_seq_cnt != seq_count)
  12927. return 0;
  12928. fctl = (hdr->fh_f_ctl[0] << 16 |
  12929. hdr->fh_f_ctl[1] << 8 |
  12930. hdr->fh_f_ctl[2]);
  12931. /* If last frame of sequence we can return success. */
  12932. if (fctl & FC_FC_END_SEQ)
  12933. return 1;
  12934. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  12935. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12936. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12937. /* If there is a hole in the sequence count then fail. */
  12938. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  12939. return 0;
  12940. fctl = (hdr->fh_f_ctl[0] << 16 |
  12941. hdr->fh_f_ctl[1] << 8 |
  12942. hdr->fh_f_ctl[2]);
  12943. /* If last frame of sequence we can return success. */
  12944. if (fctl & FC_FC_END_SEQ)
  12945. return 1;
  12946. }
  12947. return 0;
  12948. }
  12949. /**
  12950. * lpfc_prep_seq - Prep sequence for ULP processing
  12951. * @vport: Pointer to the vport on which this sequence was received
  12952. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12953. *
  12954. * This function takes a sequence, described by a list of frames, and creates
  12955. * a list of iocbq structures to describe the sequence. This iocbq list will be
  12956. * used to issue to the generic unsolicited sequence handler. This routine
  12957. * returns a pointer to the first iocbq in the list. If the function is unable
  12958. * to allocate an iocbq then it throw out the received frames that were not
  12959. * able to be described and return a pointer to the first iocbq. If unable to
  12960. * allocate any iocbqs (including the first) this function will return NULL.
  12961. **/
  12962. static struct lpfc_iocbq *
  12963. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  12964. {
  12965. struct hbq_dmabuf *hbq_buf;
  12966. struct lpfc_dmabuf *d_buf, *n_buf;
  12967. struct lpfc_iocbq *first_iocbq, *iocbq;
  12968. struct fc_frame_header *fc_hdr;
  12969. uint32_t sid;
  12970. uint32_t len, tot_len;
  12971. struct ulp_bde64 *pbde;
  12972. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12973. /* remove from receive buffer list */
  12974. list_del_init(&seq_dmabuf->hbuf.list);
  12975. lpfc_update_rcv_time_stamp(vport);
  12976. /* get the Remote Port's SID */
  12977. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12978. tot_len = 0;
  12979. /* Get an iocbq struct to fill in. */
  12980. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  12981. if (first_iocbq) {
  12982. /* Initialize the first IOCB. */
  12983. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  12984. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  12985. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  12986. first_iocbq->iocb.ulpContext = NO_XRI;
  12987. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  12988. be16_to_cpu(fc_hdr->fh_ox_id);
  12989. /* iocbq is prepped for internal consumption. Physical vpi. */
  12990. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  12991. vport->phba->vpi_ids[vport->vpi];
  12992. /* put the first buffer into the first IOCBq */
  12993. first_iocbq->context2 = &seq_dmabuf->dbuf;
  12994. first_iocbq->context3 = NULL;
  12995. first_iocbq->iocb.ulpBdeCount = 1;
  12996. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  12997. LPFC_DATA_BUF_SIZE;
  12998. first_iocbq->iocb.un.rcvels.remoteID = sid;
  12999. tot_len = bf_get(lpfc_rcqe_length,
  13000. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13001. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13002. }
  13003. iocbq = first_iocbq;
  13004. /*
  13005. * Each IOCBq can have two Buffers assigned, so go through the list
  13006. * of buffers for this sequence and save two buffers in each IOCBq
  13007. */
  13008. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13009. if (!iocbq) {
  13010. lpfc_in_buf_free(vport->phba, d_buf);
  13011. continue;
  13012. }
  13013. if (!iocbq->context3) {
  13014. iocbq->context3 = d_buf;
  13015. iocbq->iocb.ulpBdeCount++;
  13016. pbde = (struct ulp_bde64 *)
  13017. &iocbq->iocb.unsli3.sli3Words[4];
  13018. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13019. /* We need to get the size out of the right CQE */
  13020. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13021. len = bf_get(lpfc_rcqe_length,
  13022. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13023. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13024. tot_len += len;
  13025. } else {
  13026. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13027. if (!iocbq) {
  13028. if (first_iocbq) {
  13029. first_iocbq->iocb.ulpStatus =
  13030. IOSTAT_FCP_RSP_ERROR;
  13031. first_iocbq->iocb.un.ulpWord[4] =
  13032. IOERR_NO_RESOURCES;
  13033. }
  13034. lpfc_in_buf_free(vport->phba, d_buf);
  13035. continue;
  13036. }
  13037. iocbq->context2 = d_buf;
  13038. iocbq->context3 = NULL;
  13039. iocbq->iocb.ulpBdeCount = 1;
  13040. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13041. LPFC_DATA_BUF_SIZE;
  13042. /* We need to get the size out of the right CQE */
  13043. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13044. len = bf_get(lpfc_rcqe_length,
  13045. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13046. tot_len += len;
  13047. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13048. iocbq->iocb.un.rcvels.remoteID = sid;
  13049. list_add_tail(&iocbq->list, &first_iocbq->list);
  13050. }
  13051. }
  13052. return first_iocbq;
  13053. }
  13054. static void
  13055. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13056. struct hbq_dmabuf *seq_dmabuf)
  13057. {
  13058. struct fc_frame_header *fc_hdr;
  13059. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13060. struct lpfc_hba *phba = vport->phba;
  13061. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13062. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13063. if (!iocbq) {
  13064. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13065. "2707 Ring %d handler: Failed to allocate "
  13066. "iocb Rctl x%x Type x%x received\n",
  13067. LPFC_ELS_RING,
  13068. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13069. return;
  13070. }
  13071. if (!lpfc_complete_unsol_iocb(phba,
  13072. &phba->sli.ring[LPFC_ELS_RING],
  13073. iocbq, fc_hdr->fh_r_ctl,
  13074. fc_hdr->fh_type))
  13075. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13076. "2540 Ring %d handler: unexpected Rctl "
  13077. "x%x Type x%x received\n",
  13078. LPFC_ELS_RING,
  13079. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13080. /* Free iocb created in lpfc_prep_seq */
  13081. list_for_each_entry_safe(curr_iocb, next_iocb,
  13082. &iocbq->list, list) {
  13083. list_del_init(&curr_iocb->list);
  13084. lpfc_sli_release_iocbq(phba, curr_iocb);
  13085. }
  13086. lpfc_sli_release_iocbq(phba, iocbq);
  13087. }
  13088. /**
  13089. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13090. * @phba: Pointer to HBA context object.
  13091. *
  13092. * This function is called with no lock held. This function processes all
  13093. * the received buffers and gives it to upper layers when a received buffer
  13094. * indicates that it is the final frame in the sequence. The interrupt
  13095. * service routine processes received buffers at interrupt contexts and adds
  13096. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13097. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13098. * appropriate receive function when the final frame in a sequence is received.
  13099. **/
  13100. void
  13101. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13102. struct hbq_dmabuf *dmabuf)
  13103. {
  13104. struct hbq_dmabuf *seq_dmabuf;
  13105. struct fc_frame_header *fc_hdr;
  13106. struct lpfc_vport *vport;
  13107. uint32_t fcfi;
  13108. /* Process each received buffer */
  13109. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13110. /* check to see if this a valid type of frame */
  13111. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13112. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13113. return;
  13114. }
  13115. if ((bf_get(lpfc_cqe_code,
  13116. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13117. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13118. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13119. else
  13120. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13121. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13122. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13123. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13124. /* throw out the frame */
  13125. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13126. return;
  13127. }
  13128. /* Handle the basic abort sequence (BA_ABTS) event */
  13129. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13130. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13131. return;
  13132. }
  13133. /* Link this frame */
  13134. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13135. if (!seq_dmabuf) {
  13136. /* unable to add frame to vport - throw it out */
  13137. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13138. return;
  13139. }
  13140. /* If not last frame in sequence continue processing frames. */
  13141. if (!lpfc_seq_complete(seq_dmabuf))
  13142. return;
  13143. /* Send the complete sequence to the upper layer protocol */
  13144. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13145. }
  13146. /**
  13147. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13148. * @phba: pointer to lpfc hba data structure.
  13149. *
  13150. * This routine is invoked to post rpi header templates to the
  13151. * HBA consistent with the SLI-4 interface spec. This routine
  13152. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13153. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13154. *
  13155. * This routine does not require any locks. It's usage is expected
  13156. * to be driver load or reset recovery when the driver is
  13157. * sequential.
  13158. *
  13159. * Return codes
  13160. * 0 - successful
  13161. * -EIO - The mailbox failed to complete successfully.
  13162. * When this error occurs, the driver is not guaranteed
  13163. * to have any rpi regions posted to the device and
  13164. * must either attempt to repost the regions or take a
  13165. * fatal error.
  13166. **/
  13167. int
  13168. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13169. {
  13170. struct lpfc_rpi_hdr *rpi_page;
  13171. uint32_t rc = 0;
  13172. uint16_t lrpi = 0;
  13173. /* SLI4 ports that support extents do not require RPI headers. */
  13174. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13175. goto exit;
  13176. if (phba->sli4_hba.extents_in_use)
  13177. return -EIO;
  13178. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13179. /*
  13180. * Assign the rpi headers a physical rpi only if the driver
  13181. * has not initialized those resources. A port reset only
  13182. * needs the headers posted.
  13183. */
  13184. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13185. LPFC_RPI_RSRC_RDY)
  13186. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13187. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13188. if (rc != MBX_SUCCESS) {
  13189. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13190. "2008 Error %d posting all rpi "
  13191. "headers\n", rc);
  13192. rc = -EIO;
  13193. break;
  13194. }
  13195. }
  13196. exit:
  13197. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13198. LPFC_RPI_RSRC_RDY);
  13199. return rc;
  13200. }
  13201. /**
  13202. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13203. * @phba: pointer to lpfc hba data structure.
  13204. * @rpi_page: pointer to the rpi memory region.
  13205. *
  13206. * This routine is invoked to post a single rpi header to the
  13207. * HBA consistent with the SLI-4 interface spec. This memory region
  13208. * maps up to 64 rpi context regions.
  13209. *
  13210. * Return codes
  13211. * 0 - successful
  13212. * -ENOMEM - No available memory
  13213. * -EIO - The mailbox failed to complete successfully.
  13214. **/
  13215. int
  13216. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13217. {
  13218. LPFC_MBOXQ_t *mboxq;
  13219. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13220. uint32_t rc = 0;
  13221. uint32_t shdr_status, shdr_add_status;
  13222. union lpfc_sli4_cfg_shdr *shdr;
  13223. /* SLI4 ports that support extents do not require RPI headers. */
  13224. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13225. return rc;
  13226. if (phba->sli4_hba.extents_in_use)
  13227. return -EIO;
  13228. /* The port is notified of the header region via a mailbox command. */
  13229. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13230. if (!mboxq) {
  13231. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13232. "2001 Unable to allocate memory for issuing "
  13233. "SLI_CONFIG_SPECIAL mailbox command\n");
  13234. return -ENOMEM;
  13235. }
  13236. /* Post all rpi memory regions to the port. */
  13237. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13238. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13239. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13240. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13241. sizeof(struct lpfc_sli4_cfg_mhdr),
  13242. LPFC_SLI4_MBX_EMBED);
  13243. /* Post the physical rpi to the port for this rpi header. */
  13244. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13245. rpi_page->start_rpi);
  13246. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13247. hdr_tmpl, rpi_page->page_count);
  13248. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13249. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13250. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13251. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13252. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13253. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13254. if (rc != MBX_TIMEOUT)
  13255. mempool_free(mboxq, phba->mbox_mem_pool);
  13256. if (shdr_status || shdr_add_status || rc) {
  13257. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13258. "2514 POST_RPI_HDR mailbox failed with "
  13259. "status x%x add_status x%x, mbx status x%x\n",
  13260. shdr_status, shdr_add_status, rc);
  13261. rc = -ENXIO;
  13262. }
  13263. return rc;
  13264. }
  13265. /**
  13266. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13267. * @phba: pointer to lpfc hba data structure.
  13268. *
  13269. * This routine is invoked to post rpi header templates to the
  13270. * HBA consistent with the SLI-4 interface spec. This routine
  13271. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13272. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13273. *
  13274. * Returns
  13275. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13276. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13277. **/
  13278. int
  13279. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13280. {
  13281. unsigned long rpi;
  13282. uint16_t max_rpi, rpi_limit;
  13283. uint16_t rpi_remaining, lrpi = 0;
  13284. struct lpfc_rpi_hdr *rpi_hdr;
  13285. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13286. rpi_limit = phba->sli4_hba.next_rpi;
  13287. /*
  13288. * Fetch the next logical rpi. Because this index is logical,
  13289. * the driver starts at 0 each time.
  13290. */
  13291. spin_lock_irq(&phba->hbalock);
  13292. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13293. if (rpi >= rpi_limit)
  13294. rpi = LPFC_RPI_ALLOC_ERROR;
  13295. else {
  13296. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13297. phba->sli4_hba.max_cfg_param.rpi_used++;
  13298. phba->sli4_hba.rpi_count++;
  13299. }
  13300. /*
  13301. * Don't try to allocate more rpi header regions if the device limit
  13302. * has been exhausted.
  13303. */
  13304. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13305. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13306. spin_unlock_irq(&phba->hbalock);
  13307. return rpi;
  13308. }
  13309. /*
  13310. * RPI header postings are not required for SLI4 ports capable of
  13311. * extents.
  13312. */
  13313. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13314. spin_unlock_irq(&phba->hbalock);
  13315. return rpi;
  13316. }
  13317. /*
  13318. * If the driver is running low on rpi resources, allocate another
  13319. * page now. Note that the next_rpi value is used because
  13320. * it represents how many are actually in use whereas max_rpi notes
  13321. * how many are supported max by the device.
  13322. */
  13323. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13324. spin_unlock_irq(&phba->hbalock);
  13325. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13326. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13327. if (!rpi_hdr) {
  13328. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13329. "2002 Error Could not grow rpi "
  13330. "count\n");
  13331. } else {
  13332. lrpi = rpi_hdr->start_rpi;
  13333. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13334. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13335. }
  13336. }
  13337. return rpi;
  13338. }
  13339. /**
  13340. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13341. * @phba: pointer to lpfc hba data structure.
  13342. *
  13343. * This routine is invoked to release an rpi to the pool of
  13344. * available rpis maintained by the driver.
  13345. **/
  13346. void
  13347. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13348. {
  13349. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13350. phba->sli4_hba.rpi_count--;
  13351. phba->sli4_hba.max_cfg_param.rpi_used--;
  13352. }
  13353. }
  13354. /**
  13355. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13356. * @phba: pointer to lpfc hba data structure.
  13357. *
  13358. * This routine is invoked to release an rpi to the pool of
  13359. * available rpis maintained by the driver.
  13360. **/
  13361. void
  13362. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13363. {
  13364. spin_lock_irq(&phba->hbalock);
  13365. __lpfc_sli4_free_rpi(phba, rpi);
  13366. spin_unlock_irq(&phba->hbalock);
  13367. }
  13368. /**
  13369. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13370. * @phba: pointer to lpfc hba data structure.
  13371. *
  13372. * This routine is invoked to remove the memory region that
  13373. * provided rpi via a bitmask.
  13374. **/
  13375. void
  13376. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13377. {
  13378. kfree(phba->sli4_hba.rpi_bmask);
  13379. kfree(phba->sli4_hba.rpi_ids);
  13380. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13381. }
  13382. /**
  13383. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13384. * @phba: pointer to lpfc hba data structure.
  13385. *
  13386. * This routine is invoked to remove the memory region that
  13387. * provided rpi via a bitmask.
  13388. **/
  13389. int
  13390. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13391. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13392. {
  13393. LPFC_MBOXQ_t *mboxq;
  13394. struct lpfc_hba *phba = ndlp->phba;
  13395. int rc;
  13396. /* The port is notified of the header region via a mailbox command. */
  13397. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13398. if (!mboxq)
  13399. return -ENOMEM;
  13400. /* Post all rpi memory regions to the port. */
  13401. lpfc_resume_rpi(mboxq, ndlp);
  13402. if (cmpl) {
  13403. mboxq->mbox_cmpl = cmpl;
  13404. mboxq->context1 = arg;
  13405. mboxq->context2 = ndlp;
  13406. } else
  13407. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13408. mboxq->vport = ndlp->vport;
  13409. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13410. if (rc == MBX_NOT_FINISHED) {
  13411. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13412. "2010 Resume RPI Mailbox failed "
  13413. "status %d, mbxStatus x%x\n", rc,
  13414. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13415. mempool_free(mboxq, phba->mbox_mem_pool);
  13416. return -EIO;
  13417. }
  13418. return 0;
  13419. }
  13420. /**
  13421. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13422. * @vport: Pointer to the vport for which the vpi is being initialized
  13423. *
  13424. * This routine is invoked to activate a vpi with the port.
  13425. *
  13426. * Returns:
  13427. * 0 success
  13428. * -Evalue otherwise
  13429. **/
  13430. int
  13431. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13432. {
  13433. LPFC_MBOXQ_t *mboxq;
  13434. int rc = 0;
  13435. int retval = MBX_SUCCESS;
  13436. uint32_t mbox_tmo;
  13437. struct lpfc_hba *phba = vport->phba;
  13438. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13439. if (!mboxq)
  13440. return -ENOMEM;
  13441. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13442. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13443. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13444. if (rc != MBX_SUCCESS) {
  13445. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13446. "2022 INIT VPI Mailbox failed "
  13447. "status %d, mbxStatus x%x\n", rc,
  13448. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13449. retval = -EIO;
  13450. }
  13451. if (rc != MBX_TIMEOUT)
  13452. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13453. return retval;
  13454. }
  13455. /**
  13456. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13457. * @phba: pointer to lpfc hba data structure.
  13458. * @mboxq: Pointer to mailbox object.
  13459. *
  13460. * This routine is invoked to manually add a single FCF record. The caller
  13461. * must pass a completely initialized FCF_Record. This routine takes
  13462. * care of the nonembedded mailbox operations.
  13463. **/
  13464. static void
  13465. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13466. {
  13467. void *virt_addr;
  13468. union lpfc_sli4_cfg_shdr *shdr;
  13469. uint32_t shdr_status, shdr_add_status;
  13470. virt_addr = mboxq->sge_array->addr[0];
  13471. /* The IOCTL status is embedded in the mailbox subheader. */
  13472. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13473. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13474. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13475. if ((shdr_status || shdr_add_status) &&
  13476. (shdr_status != STATUS_FCF_IN_USE))
  13477. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13478. "2558 ADD_FCF_RECORD mailbox failed with "
  13479. "status x%x add_status x%x\n",
  13480. shdr_status, shdr_add_status);
  13481. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13482. }
  13483. /**
  13484. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13485. * @phba: pointer to lpfc hba data structure.
  13486. * @fcf_record: pointer to the initialized fcf record to add.
  13487. *
  13488. * This routine is invoked to manually add a single FCF record. The caller
  13489. * must pass a completely initialized FCF_Record. This routine takes
  13490. * care of the nonembedded mailbox operations.
  13491. **/
  13492. int
  13493. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13494. {
  13495. int rc = 0;
  13496. LPFC_MBOXQ_t *mboxq;
  13497. uint8_t *bytep;
  13498. void *virt_addr;
  13499. dma_addr_t phys_addr;
  13500. struct lpfc_mbx_sge sge;
  13501. uint32_t alloc_len, req_len;
  13502. uint32_t fcfindex;
  13503. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13504. if (!mboxq) {
  13505. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13506. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13507. return -ENOMEM;
  13508. }
  13509. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13510. sizeof(uint32_t);
  13511. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13512. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13513. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13514. req_len, LPFC_SLI4_MBX_NEMBED);
  13515. if (alloc_len < req_len) {
  13516. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13517. "2523 Allocated DMA memory size (x%x) is "
  13518. "less than the requested DMA memory "
  13519. "size (x%x)\n", alloc_len, req_len);
  13520. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13521. return -ENOMEM;
  13522. }
  13523. /*
  13524. * Get the first SGE entry from the non-embedded DMA memory. This
  13525. * routine only uses a single SGE.
  13526. */
  13527. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13528. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13529. virt_addr = mboxq->sge_array->addr[0];
  13530. /*
  13531. * Configure the FCF record for FCFI 0. This is the driver's
  13532. * hardcoded default and gets used in nonFIP mode.
  13533. */
  13534. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13535. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13536. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13537. /*
  13538. * Copy the fcf_index and the FCF Record Data. The data starts after
  13539. * the FCoE header plus word10. The data copy needs to be endian
  13540. * correct.
  13541. */
  13542. bytep += sizeof(uint32_t);
  13543. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13544. mboxq->vport = phba->pport;
  13545. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13546. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13547. if (rc == MBX_NOT_FINISHED) {
  13548. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13549. "2515 ADD_FCF_RECORD mailbox failed with "
  13550. "status 0x%x\n", rc);
  13551. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13552. rc = -EIO;
  13553. } else
  13554. rc = 0;
  13555. return rc;
  13556. }
  13557. /**
  13558. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13559. * @phba: pointer to lpfc hba data structure.
  13560. * @fcf_record: pointer to the fcf record to write the default data.
  13561. * @fcf_index: FCF table entry index.
  13562. *
  13563. * This routine is invoked to build the driver's default FCF record. The
  13564. * values used are hardcoded. This routine handles memory initialization.
  13565. *
  13566. **/
  13567. void
  13568. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13569. struct fcf_record *fcf_record,
  13570. uint16_t fcf_index)
  13571. {
  13572. memset(fcf_record, 0, sizeof(struct fcf_record));
  13573. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13574. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13575. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13576. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13577. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13578. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13579. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13580. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13581. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13582. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13583. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13584. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13585. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13586. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13587. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13588. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13589. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13590. /* Set the VLAN bit map */
  13591. if (phba->valid_vlan) {
  13592. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13593. = 1 << (phba->vlan_id % 8);
  13594. }
  13595. }
  13596. /**
  13597. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13598. * @phba: pointer to lpfc hba data structure.
  13599. * @fcf_index: FCF table entry offset.
  13600. *
  13601. * This routine is invoked to scan the entire FCF table by reading FCF
  13602. * record and processing it one at a time starting from the @fcf_index
  13603. * for initial FCF discovery or fast FCF failover rediscovery.
  13604. *
  13605. * Return 0 if the mailbox command is submitted successfully, none 0
  13606. * otherwise.
  13607. **/
  13608. int
  13609. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13610. {
  13611. int rc = 0, error;
  13612. LPFC_MBOXQ_t *mboxq;
  13613. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13614. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13615. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13616. if (!mboxq) {
  13617. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13618. "2000 Failed to allocate mbox for "
  13619. "READ_FCF cmd\n");
  13620. error = -ENOMEM;
  13621. goto fail_fcf_scan;
  13622. }
  13623. /* Construct the read FCF record mailbox command */
  13624. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13625. if (rc) {
  13626. error = -EINVAL;
  13627. goto fail_fcf_scan;
  13628. }
  13629. /* Issue the mailbox command asynchronously */
  13630. mboxq->vport = phba->pport;
  13631. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13632. spin_lock_irq(&phba->hbalock);
  13633. phba->hba_flag |= FCF_TS_INPROG;
  13634. spin_unlock_irq(&phba->hbalock);
  13635. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13636. if (rc == MBX_NOT_FINISHED)
  13637. error = -EIO;
  13638. else {
  13639. /* Reset eligible FCF count for new scan */
  13640. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13641. phba->fcf.eligible_fcf_cnt = 0;
  13642. error = 0;
  13643. }
  13644. fail_fcf_scan:
  13645. if (error) {
  13646. if (mboxq)
  13647. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13648. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13649. spin_lock_irq(&phba->hbalock);
  13650. phba->hba_flag &= ~FCF_TS_INPROG;
  13651. spin_unlock_irq(&phba->hbalock);
  13652. }
  13653. return error;
  13654. }
  13655. /**
  13656. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13657. * @phba: pointer to lpfc hba data structure.
  13658. * @fcf_index: FCF table entry offset.
  13659. *
  13660. * This routine is invoked to read an FCF record indicated by @fcf_index
  13661. * and to use it for FLOGI roundrobin FCF failover.
  13662. *
  13663. * Return 0 if the mailbox command is submitted successfully, none 0
  13664. * otherwise.
  13665. **/
  13666. int
  13667. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13668. {
  13669. int rc = 0, error;
  13670. LPFC_MBOXQ_t *mboxq;
  13671. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13672. if (!mboxq) {
  13673. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13674. "2763 Failed to allocate mbox for "
  13675. "READ_FCF cmd\n");
  13676. error = -ENOMEM;
  13677. goto fail_fcf_read;
  13678. }
  13679. /* Construct the read FCF record mailbox command */
  13680. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13681. if (rc) {
  13682. error = -EINVAL;
  13683. goto fail_fcf_read;
  13684. }
  13685. /* Issue the mailbox command asynchronously */
  13686. mboxq->vport = phba->pport;
  13687. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13688. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13689. if (rc == MBX_NOT_FINISHED)
  13690. error = -EIO;
  13691. else
  13692. error = 0;
  13693. fail_fcf_read:
  13694. if (error && mboxq)
  13695. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13696. return error;
  13697. }
  13698. /**
  13699. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13700. * @phba: pointer to lpfc hba data structure.
  13701. * @fcf_index: FCF table entry offset.
  13702. *
  13703. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13704. * determine whether it's eligible for FLOGI roundrobin failover list.
  13705. *
  13706. * Return 0 if the mailbox command is submitted successfully, none 0
  13707. * otherwise.
  13708. **/
  13709. int
  13710. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13711. {
  13712. int rc = 0, error;
  13713. LPFC_MBOXQ_t *mboxq;
  13714. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13715. if (!mboxq) {
  13716. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13717. "2758 Failed to allocate mbox for "
  13718. "READ_FCF cmd\n");
  13719. error = -ENOMEM;
  13720. goto fail_fcf_read;
  13721. }
  13722. /* Construct the read FCF record mailbox command */
  13723. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13724. if (rc) {
  13725. error = -EINVAL;
  13726. goto fail_fcf_read;
  13727. }
  13728. /* Issue the mailbox command asynchronously */
  13729. mboxq->vport = phba->pport;
  13730. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13731. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13732. if (rc == MBX_NOT_FINISHED)
  13733. error = -EIO;
  13734. else
  13735. error = 0;
  13736. fail_fcf_read:
  13737. if (error && mboxq)
  13738. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13739. return error;
  13740. }
  13741. /**
  13742. * lpfc_check_next_fcf_pri
  13743. * phba pointer to the lpfc_hba struct for this port.
  13744. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13745. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13746. * rr_bmask based on their priority level. Starting from the highest priority
  13747. * to the lowest. The most likely FCF candidate will be in the highest
  13748. * priority group. When this routine is called it searches the fcf_pri list for
  13749. * next lowest priority group and repopulates the rr_bmask with only those
  13750. * fcf_indexes.
  13751. * returns:
  13752. * 1=success 0=failure
  13753. **/
  13754. int
  13755. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13756. {
  13757. uint16_t next_fcf_pri;
  13758. uint16_t last_index;
  13759. struct lpfc_fcf_pri *fcf_pri;
  13760. int rc;
  13761. int ret = 0;
  13762. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13763. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13764. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13765. "3060 Last IDX %d\n", last_index);
  13766. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13767. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13768. "3061 Last IDX %d\n", last_index);
  13769. return 0; /* Empty rr list */
  13770. }
  13771. next_fcf_pri = 0;
  13772. /*
  13773. * Clear the rr_bmask and set all of the bits that are at this
  13774. * priority.
  13775. */
  13776. memset(phba->fcf.fcf_rr_bmask, 0,
  13777. sizeof(*phba->fcf.fcf_rr_bmask));
  13778. spin_lock_irq(&phba->hbalock);
  13779. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13780. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13781. continue;
  13782. /*
  13783. * the 1st priority that has not FLOGI failed
  13784. * will be the highest.
  13785. */
  13786. if (!next_fcf_pri)
  13787. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13788. spin_unlock_irq(&phba->hbalock);
  13789. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13790. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13791. fcf_pri->fcf_rec.fcf_index);
  13792. if (rc)
  13793. return 0;
  13794. }
  13795. spin_lock_irq(&phba->hbalock);
  13796. }
  13797. /*
  13798. * if next_fcf_pri was not set above and the list is not empty then
  13799. * we have failed flogis on all of them. So reset flogi failed
  13800. * and start at the begining.
  13801. */
  13802. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13803. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13804. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13805. /*
  13806. * the 1st priority that has not FLOGI failed
  13807. * will be the highest.
  13808. */
  13809. if (!next_fcf_pri)
  13810. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13811. spin_unlock_irq(&phba->hbalock);
  13812. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13813. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13814. fcf_pri->fcf_rec.fcf_index);
  13815. if (rc)
  13816. return 0;
  13817. }
  13818. spin_lock_irq(&phba->hbalock);
  13819. }
  13820. } else
  13821. ret = 1;
  13822. spin_unlock_irq(&phba->hbalock);
  13823. return ret;
  13824. }
  13825. /**
  13826. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13827. * @phba: pointer to lpfc hba data structure.
  13828. *
  13829. * This routine is to get the next eligible FCF record index in a round
  13830. * robin fashion. If the next eligible FCF record index equals to the
  13831. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13832. * shall be returned, otherwise, the next eligible FCF record's index
  13833. * shall be returned.
  13834. **/
  13835. uint16_t
  13836. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13837. {
  13838. uint16_t next_fcf_index;
  13839. /* Search start from next bit of currently registered FCF index */
  13840. next_priority:
  13841. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13842. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13843. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13844. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13845. next_fcf_index);
  13846. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13847. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13848. /*
  13849. * If we have wrapped then we need to clear the bits that
  13850. * have been tested so that we can detect when we should
  13851. * change the priority level.
  13852. */
  13853. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13854. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  13855. }
  13856. /* Check roundrobin failover list empty condition */
  13857. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  13858. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  13859. /*
  13860. * If next fcf index is not found check if there are lower
  13861. * Priority level fcf's in the fcf_priority list.
  13862. * Set up the rr_bmask with all of the avaiable fcf bits
  13863. * at that level and continue the selection process.
  13864. */
  13865. if (lpfc_check_next_fcf_pri_level(phba))
  13866. goto next_priority;
  13867. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13868. "2844 No roundrobin failover FCF available\n");
  13869. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  13870. return LPFC_FCOE_FCF_NEXT_NONE;
  13871. else {
  13872. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13873. "3063 Only FCF available idx %d, flag %x\n",
  13874. next_fcf_index,
  13875. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  13876. return next_fcf_index;
  13877. }
  13878. }
  13879. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  13880. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  13881. LPFC_FCF_FLOGI_FAILED)
  13882. goto next_priority;
  13883. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13884. "2845 Get next roundrobin failover FCF (x%x)\n",
  13885. next_fcf_index);
  13886. return next_fcf_index;
  13887. }
  13888. /**
  13889. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  13890. * @phba: pointer to lpfc hba data structure.
  13891. *
  13892. * This routine sets the FCF record index in to the eligible bmask for
  13893. * roundrobin failover search. It checks to make sure that the index
  13894. * does not go beyond the range of the driver allocated bmask dimension
  13895. * before setting the bit.
  13896. *
  13897. * Returns 0 if the index bit successfully set, otherwise, it returns
  13898. * -EINVAL.
  13899. **/
  13900. int
  13901. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  13902. {
  13903. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13904. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13905. "2610 FCF (x%x) reached driver's book "
  13906. "keeping dimension:x%x\n",
  13907. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13908. return -EINVAL;
  13909. }
  13910. /* Set the eligible FCF record index bmask */
  13911. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13912. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13913. "2790 Set FCF (x%x) to roundrobin FCF failover "
  13914. "bmask\n", fcf_index);
  13915. return 0;
  13916. }
  13917. /**
  13918. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  13919. * @phba: pointer to lpfc hba data structure.
  13920. *
  13921. * This routine clears the FCF record index from the eligible bmask for
  13922. * roundrobin failover search. It checks to make sure that the index
  13923. * does not go beyond the range of the driver allocated bmask dimension
  13924. * before clearing the bit.
  13925. **/
  13926. void
  13927. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  13928. {
  13929. struct lpfc_fcf_pri *fcf_pri;
  13930. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13931. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13932. "2762 FCF (x%x) reached driver's book "
  13933. "keeping dimension:x%x\n",
  13934. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13935. return;
  13936. }
  13937. /* Clear the eligible FCF record index bmask */
  13938. spin_lock_irq(&phba->hbalock);
  13939. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13940. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  13941. list_del_init(&fcf_pri->list);
  13942. break;
  13943. }
  13944. }
  13945. spin_unlock_irq(&phba->hbalock);
  13946. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13947. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13948. "2791 Clear FCF (x%x) from roundrobin failover "
  13949. "bmask\n", fcf_index);
  13950. }
  13951. /**
  13952. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  13953. * @phba: pointer to lpfc hba data structure.
  13954. *
  13955. * This routine is the completion routine for the rediscover FCF table mailbox
  13956. * command. If the mailbox command returned failure, it will try to stop the
  13957. * FCF rediscover wait timer.
  13958. **/
  13959. void
  13960. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  13961. {
  13962. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  13963. uint32_t shdr_status, shdr_add_status;
  13964. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  13965. shdr_status = bf_get(lpfc_mbox_hdr_status,
  13966. &redisc_fcf->header.cfg_shdr.response);
  13967. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  13968. &redisc_fcf->header.cfg_shdr.response);
  13969. if (shdr_status || shdr_add_status) {
  13970. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13971. "2746 Requesting for FCF rediscovery failed "
  13972. "status x%x add_status x%x\n",
  13973. shdr_status, shdr_add_status);
  13974. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  13975. spin_lock_irq(&phba->hbalock);
  13976. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  13977. spin_unlock_irq(&phba->hbalock);
  13978. /*
  13979. * CVL event triggered FCF rediscover request failed,
  13980. * last resort to re-try current registered FCF entry.
  13981. */
  13982. lpfc_retry_pport_discovery(phba);
  13983. } else {
  13984. spin_lock_irq(&phba->hbalock);
  13985. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  13986. spin_unlock_irq(&phba->hbalock);
  13987. /*
  13988. * DEAD FCF event triggered FCF rediscover request
  13989. * failed, last resort to fail over as a link down
  13990. * to FCF registration.
  13991. */
  13992. lpfc_sli4_fcf_dead_failthrough(phba);
  13993. }
  13994. } else {
  13995. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13996. "2775 Start FCF rediscover quiescent timer\n");
  13997. /*
  13998. * Start FCF rediscovery wait timer for pending FCF
  13999. * before rescan FCF record table.
  14000. */
  14001. lpfc_fcf_redisc_wait_start_timer(phba);
  14002. }
  14003. mempool_free(mbox, phba->mbox_mem_pool);
  14004. }
  14005. /**
  14006. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14007. * @phba: pointer to lpfc hba data structure.
  14008. *
  14009. * This routine is invoked to request for rediscovery of the entire FCF table
  14010. * by the port.
  14011. **/
  14012. int
  14013. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14014. {
  14015. LPFC_MBOXQ_t *mbox;
  14016. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14017. int rc, length;
  14018. /* Cancel retry delay timers to all vports before FCF rediscover */
  14019. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14020. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14021. if (!mbox) {
  14022. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14023. "2745 Failed to allocate mbox for "
  14024. "requesting FCF rediscover.\n");
  14025. return -ENOMEM;
  14026. }
  14027. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14028. sizeof(struct lpfc_sli4_cfg_mhdr));
  14029. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14030. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14031. length, LPFC_SLI4_MBX_EMBED);
  14032. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14033. /* Set count to 0 for invalidating the entire FCF database */
  14034. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14035. /* Issue the mailbox command asynchronously */
  14036. mbox->vport = phba->pport;
  14037. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14038. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14039. if (rc == MBX_NOT_FINISHED) {
  14040. mempool_free(mbox, phba->mbox_mem_pool);
  14041. return -EIO;
  14042. }
  14043. return 0;
  14044. }
  14045. /**
  14046. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14047. * @phba: pointer to lpfc hba data structure.
  14048. *
  14049. * This function is the failover routine as a last resort to the FCF DEAD
  14050. * event when driver failed to perform fast FCF failover.
  14051. **/
  14052. void
  14053. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14054. {
  14055. uint32_t link_state;
  14056. /*
  14057. * Last resort as FCF DEAD event failover will treat this as
  14058. * a link down, but save the link state because we don't want
  14059. * it to be changed to Link Down unless it is already down.
  14060. */
  14061. link_state = phba->link_state;
  14062. lpfc_linkdown(phba);
  14063. phba->link_state = link_state;
  14064. /* Unregister FCF if no devices connected to it */
  14065. lpfc_unregister_unused_fcf(phba);
  14066. }
  14067. /**
  14068. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14069. * @phba: pointer to lpfc hba data structure.
  14070. * @rgn23_data: pointer to configure region 23 data.
  14071. *
  14072. * This function gets SLI3 port configure region 23 data through memory dump
  14073. * mailbox command. When it successfully retrieves data, the size of the data
  14074. * will be returned, otherwise, 0 will be returned.
  14075. **/
  14076. static uint32_t
  14077. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14078. {
  14079. LPFC_MBOXQ_t *pmb = NULL;
  14080. MAILBOX_t *mb;
  14081. uint32_t offset = 0;
  14082. int rc;
  14083. if (!rgn23_data)
  14084. return 0;
  14085. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14086. if (!pmb) {
  14087. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14088. "2600 failed to allocate mailbox memory\n");
  14089. return 0;
  14090. }
  14091. mb = &pmb->u.mb;
  14092. do {
  14093. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14094. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14095. if (rc != MBX_SUCCESS) {
  14096. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14097. "2601 failed to read config "
  14098. "region 23, rc 0x%x Status 0x%x\n",
  14099. rc, mb->mbxStatus);
  14100. mb->un.varDmp.word_cnt = 0;
  14101. }
  14102. /*
  14103. * dump mem may return a zero when finished or we got a
  14104. * mailbox error, either way we are done.
  14105. */
  14106. if (mb->un.varDmp.word_cnt == 0)
  14107. break;
  14108. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14109. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14110. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14111. rgn23_data + offset,
  14112. mb->un.varDmp.word_cnt);
  14113. offset += mb->un.varDmp.word_cnt;
  14114. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14115. mempool_free(pmb, phba->mbox_mem_pool);
  14116. return offset;
  14117. }
  14118. /**
  14119. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14120. * @phba: pointer to lpfc hba data structure.
  14121. * @rgn23_data: pointer to configure region 23 data.
  14122. *
  14123. * This function gets SLI4 port configure region 23 data through memory dump
  14124. * mailbox command. When it successfully retrieves data, the size of the data
  14125. * will be returned, otherwise, 0 will be returned.
  14126. **/
  14127. static uint32_t
  14128. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14129. {
  14130. LPFC_MBOXQ_t *mboxq = NULL;
  14131. struct lpfc_dmabuf *mp = NULL;
  14132. struct lpfc_mqe *mqe;
  14133. uint32_t data_length = 0;
  14134. int rc;
  14135. if (!rgn23_data)
  14136. return 0;
  14137. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14138. if (!mboxq) {
  14139. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14140. "3105 failed to allocate mailbox memory\n");
  14141. return 0;
  14142. }
  14143. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14144. goto out;
  14145. mqe = &mboxq->u.mqe;
  14146. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14147. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14148. if (rc)
  14149. goto out;
  14150. data_length = mqe->un.mb_words[5];
  14151. if (data_length == 0)
  14152. goto out;
  14153. if (data_length > DMP_RGN23_SIZE) {
  14154. data_length = 0;
  14155. goto out;
  14156. }
  14157. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14158. out:
  14159. mempool_free(mboxq, phba->mbox_mem_pool);
  14160. if (mp) {
  14161. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14162. kfree(mp);
  14163. }
  14164. return data_length;
  14165. }
  14166. /**
  14167. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14168. * @phba: pointer to lpfc hba data structure.
  14169. *
  14170. * This function read region 23 and parse TLV for port status to
  14171. * decide if the user disaled the port. If the TLV indicates the
  14172. * port is disabled, the hba_flag is set accordingly.
  14173. **/
  14174. void
  14175. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14176. {
  14177. uint8_t *rgn23_data = NULL;
  14178. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14179. uint32_t offset = 0;
  14180. /* Get adapter Region 23 data */
  14181. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14182. if (!rgn23_data)
  14183. goto out;
  14184. if (phba->sli_rev < LPFC_SLI_REV4)
  14185. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14186. else {
  14187. if_type = bf_get(lpfc_sli_intf_if_type,
  14188. &phba->sli4_hba.sli_intf);
  14189. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14190. goto out;
  14191. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14192. }
  14193. if (!data_size)
  14194. goto out;
  14195. /* Check the region signature first */
  14196. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14197. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14198. "2619 Config region 23 has bad signature\n");
  14199. goto out;
  14200. }
  14201. offset += 4;
  14202. /* Check the data structure version */
  14203. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14204. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14205. "2620 Config region 23 has bad version\n");
  14206. goto out;
  14207. }
  14208. offset += 4;
  14209. /* Parse TLV entries in the region */
  14210. while (offset < data_size) {
  14211. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14212. break;
  14213. /*
  14214. * If the TLV is not driver specific TLV or driver id is
  14215. * not linux driver id, skip the record.
  14216. */
  14217. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14218. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14219. (rgn23_data[offset + 3] != 0)) {
  14220. offset += rgn23_data[offset + 1] * 4 + 4;
  14221. continue;
  14222. }
  14223. /* Driver found a driver specific TLV in the config region */
  14224. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14225. offset += 4;
  14226. tlv_offset = 0;
  14227. /*
  14228. * Search for configured port state sub-TLV.
  14229. */
  14230. while ((offset < data_size) &&
  14231. (tlv_offset < sub_tlv_len)) {
  14232. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14233. offset += 4;
  14234. tlv_offset += 4;
  14235. break;
  14236. }
  14237. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14238. offset += rgn23_data[offset + 1] * 4 + 4;
  14239. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14240. continue;
  14241. }
  14242. /* This HBA contains PORT_STE configured */
  14243. if (!rgn23_data[offset + 2])
  14244. phba->hba_flag |= LINK_DISABLED;
  14245. goto out;
  14246. }
  14247. }
  14248. out:
  14249. kfree(rgn23_data);
  14250. return;
  14251. }
  14252. /**
  14253. * lpfc_wr_object - write an object to the firmware
  14254. * @phba: HBA structure that indicates port to create a queue on.
  14255. * @dmabuf_list: list of dmabufs to write to the port.
  14256. * @size: the total byte value of the objects to write to the port.
  14257. * @offset: the current offset to be used to start the transfer.
  14258. *
  14259. * This routine will create a wr_object mailbox command to send to the port.
  14260. * the mailbox command will be constructed using the dma buffers described in
  14261. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14262. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14263. * used to indicate the starting offset of the transfer and will also return
  14264. * the offset after the write object mailbox has completed. @size is used to
  14265. * determine the end of the object and whether the eof bit should be set.
  14266. *
  14267. * Return 0 is successful and offset will contain the the new offset to use
  14268. * for the next write.
  14269. * Return negative value for error cases.
  14270. **/
  14271. int
  14272. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14273. uint32_t size, uint32_t *offset)
  14274. {
  14275. struct lpfc_mbx_wr_object *wr_object;
  14276. LPFC_MBOXQ_t *mbox;
  14277. int rc = 0, i = 0;
  14278. uint32_t shdr_status, shdr_add_status;
  14279. uint32_t mbox_tmo;
  14280. union lpfc_sli4_cfg_shdr *shdr;
  14281. struct lpfc_dmabuf *dmabuf;
  14282. uint32_t written = 0;
  14283. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14284. if (!mbox)
  14285. return -ENOMEM;
  14286. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14287. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14288. sizeof(struct lpfc_mbx_wr_object) -
  14289. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14290. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14291. wr_object->u.request.write_offset = *offset;
  14292. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14293. wr_object->u.request.object_name[0] =
  14294. cpu_to_le32(wr_object->u.request.object_name[0]);
  14295. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14296. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14297. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14298. break;
  14299. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14300. wr_object->u.request.bde[i].addrHigh =
  14301. putPaddrHigh(dmabuf->phys);
  14302. if (written + SLI4_PAGE_SIZE >= size) {
  14303. wr_object->u.request.bde[i].tus.f.bdeSize =
  14304. (size - written);
  14305. written += (size - written);
  14306. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14307. } else {
  14308. wr_object->u.request.bde[i].tus.f.bdeSize =
  14309. SLI4_PAGE_SIZE;
  14310. written += SLI4_PAGE_SIZE;
  14311. }
  14312. i++;
  14313. }
  14314. wr_object->u.request.bde_count = i;
  14315. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14316. if (!phba->sli4_hba.intr_enable)
  14317. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14318. else {
  14319. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14320. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14321. }
  14322. /* The IOCTL status is embedded in the mailbox subheader. */
  14323. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14324. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14325. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14326. if (rc != MBX_TIMEOUT)
  14327. mempool_free(mbox, phba->mbox_mem_pool);
  14328. if (shdr_status || shdr_add_status || rc) {
  14329. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14330. "3025 Write Object mailbox failed with "
  14331. "status x%x add_status x%x, mbx status x%x\n",
  14332. shdr_status, shdr_add_status, rc);
  14333. rc = -ENXIO;
  14334. } else
  14335. *offset += wr_object->u.response.actual_write_length;
  14336. return rc;
  14337. }
  14338. /**
  14339. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14340. * @vport: pointer to vport data structure.
  14341. *
  14342. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14343. * and REG_VPI mailbox commands associated with the vport. This function
  14344. * is called when driver want to restart discovery of the vport due to
  14345. * a Clear Virtual Link event.
  14346. **/
  14347. void
  14348. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14349. {
  14350. struct lpfc_hba *phba = vport->phba;
  14351. LPFC_MBOXQ_t *mb, *nextmb;
  14352. struct lpfc_dmabuf *mp;
  14353. struct lpfc_nodelist *ndlp;
  14354. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14355. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14356. LIST_HEAD(mbox_cmd_list);
  14357. uint8_t restart_loop;
  14358. /* Clean up internally queued mailbox commands with the vport */
  14359. spin_lock_irq(&phba->hbalock);
  14360. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14361. if (mb->vport != vport)
  14362. continue;
  14363. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14364. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14365. continue;
  14366. list_del(&mb->list);
  14367. list_add_tail(&mb->list, &mbox_cmd_list);
  14368. }
  14369. /* Clean up active mailbox command with the vport */
  14370. mb = phba->sli.mbox_active;
  14371. if (mb && (mb->vport == vport)) {
  14372. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14373. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14374. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14375. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14376. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14377. /* Put reference count for delayed processing */
  14378. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14379. /* Unregister the RPI when mailbox complete */
  14380. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14381. }
  14382. }
  14383. /* Cleanup any mailbox completions which are not yet processed */
  14384. do {
  14385. restart_loop = 0;
  14386. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14387. /*
  14388. * If this mailox is already processed or it is
  14389. * for another vport ignore it.
  14390. */
  14391. if ((mb->vport != vport) ||
  14392. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14393. continue;
  14394. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14395. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14396. continue;
  14397. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14398. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14399. ndlp = (struct lpfc_nodelist *)mb->context2;
  14400. /* Unregister the RPI when mailbox complete */
  14401. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14402. restart_loop = 1;
  14403. spin_unlock_irq(&phba->hbalock);
  14404. spin_lock(shost->host_lock);
  14405. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14406. spin_unlock(shost->host_lock);
  14407. spin_lock_irq(&phba->hbalock);
  14408. break;
  14409. }
  14410. }
  14411. } while (restart_loop);
  14412. spin_unlock_irq(&phba->hbalock);
  14413. /* Release the cleaned-up mailbox commands */
  14414. while (!list_empty(&mbox_cmd_list)) {
  14415. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14416. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14417. mp = (struct lpfc_dmabuf *) (mb->context1);
  14418. if (mp) {
  14419. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14420. kfree(mp);
  14421. }
  14422. ndlp = (struct lpfc_nodelist *) mb->context2;
  14423. mb->context2 = NULL;
  14424. if (ndlp) {
  14425. spin_lock(shost->host_lock);
  14426. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14427. spin_unlock(shost->host_lock);
  14428. lpfc_nlp_put(ndlp);
  14429. }
  14430. }
  14431. mempool_free(mb, phba->mbox_mem_pool);
  14432. }
  14433. /* Release the ndlp with the cleaned-up active mailbox command */
  14434. if (act_mbx_ndlp) {
  14435. spin_lock(shost->host_lock);
  14436. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14437. spin_unlock(shost->host_lock);
  14438. lpfc_nlp_put(act_mbx_ndlp);
  14439. }
  14440. }
  14441. /**
  14442. * lpfc_drain_txq - Drain the txq
  14443. * @phba: Pointer to HBA context object.
  14444. *
  14445. * This function attempt to submit IOCBs on the txq
  14446. * to the adapter. For SLI4 adapters, the txq contains
  14447. * ELS IOCBs that have been deferred because the there
  14448. * are no SGLs. This congestion can occur with large
  14449. * vport counts during node discovery.
  14450. **/
  14451. uint32_t
  14452. lpfc_drain_txq(struct lpfc_hba *phba)
  14453. {
  14454. LIST_HEAD(completions);
  14455. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14456. struct lpfc_iocbq *piocbq = 0;
  14457. unsigned long iflags = 0;
  14458. char *fail_msg = NULL;
  14459. struct lpfc_sglq *sglq;
  14460. union lpfc_wqe wqe;
  14461. spin_lock_irqsave(&phba->hbalock, iflags);
  14462. if (pring->txq_cnt > pring->txq_max)
  14463. pring->txq_max = pring->txq_cnt;
  14464. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14465. while (pring->txq_cnt) {
  14466. spin_lock_irqsave(&phba->hbalock, iflags);
  14467. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14468. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14469. if (!sglq) {
  14470. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14471. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14472. break;
  14473. } else {
  14474. if (!piocbq) {
  14475. /* The txq_cnt out of sync. This should
  14476. * never happen
  14477. */
  14478. sglq = __lpfc_clear_active_sglq(phba,
  14479. sglq->sli4_lxritag);
  14480. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14481. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14482. "2823 txq empty and txq_cnt is %d\n ",
  14483. pring->txq_cnt);
  14484. break;
  14485. }
  14486. }
  14487. /* The xri and iocb resources secured,
  14488. * attempt to issue request
  14489. */
  14490. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14491. piocbq->sli4_xritag = sglq->sli4_xritag;
  14492. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14493. fail_msg = "to convert bpl to sgl";
  14494. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14495. fail_msg = "to convert iocb to wqe";
  14496. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14497. fail_msg = " - Wq is full";
  14498. else
  14499. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14500. if (fail_msg) {
  14501. /* Failed means we can't issue and need to cancel */
  14502. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14503. "2822 IOCB failed %s iotag 0x%x "
  14504. "xri 0x%x\n",
  14505. fail_msg,
  14506. piocbq->iotag, piocbq->sli4_xritag);
  14507. list_add_tail(&piocbq->list, &completions);
  14508. }
  14509. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14510. }
  14511. /* Cancel all the IOCBs that cannot be issued */
  14512. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14513. IOERR_SLI_ABORTED);
  14514. return pring->txq_cnt;
  14515. }