lpfc_sli.c 480 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 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 IOCB_t *
  65. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  66. {
  67. return &iocbq->iocb;
  68. }
  69. /**
  70. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  71. * @q: The Work Queue to operate on.
  72. * @wqe: The work Queue Entry to put on the Work queue.
  73. *
  74. * This routine will copy the contents of @wqe to the next available entry on
  75. * the @q. This function will then ring the Work Queue Doorbell to signal the
  76. * HBA to start processing the Work Queue Entry. This function returns 0 if
  77. * successful. If no entries are available on @q then this function will return
  78. * -ENOMEM.
  79. * The caller is expected to hold the hbalock when calling this routine.
  80. **/
  81. static uint32_t
  82. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  83. {
  84. union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
  85. struct lpfc_register doorbell;
  86. uint32_t host_index;
  87. /* If the host has not yet processed the next entry then we are done */
  88. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  89. return -ENOMEM;
  90. /* set consumption flag every once in a while */
  91. if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
  92. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  93. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  94. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  95. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  96. /* Update the host index before invoking device */
  97. host_index = q->host_index;
  98. q->host_index = ((q->host_index + 1) % q->entry_count);
  99. /* Ring Doorbell */
  100. doorbell.word0 = 0;
  101. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  102. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  103. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  104. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  105. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  106. return 0;
  107. }
  108. /**
  109. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  110. * @q: The Work Queue to operate on.
  111. * @index: The index to advance the hba index to.
  112. *
  113. * This routine will update the HBA index of a queue to reflect consumption of
  114. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  115. * an entry the host calls this function to update the queue's internal
  116. * pointers. This routine returns the number of entries that were consumed by
  117. * the HBA.
  118. **/
  119. static uint32_t
  120. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  121. {
  122. uint32_t released = 0;
  123. if (q->hba_index == index)
  124. return 0;
  125. do {
  126. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  127. released++;
  128. } while (q->hba_index != index);
  129. return released;
  130. }
  131. /**
  132. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  133. * @q: The Mailbox Queue to operate on.
  134. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  135. *
  136. * This routine will copy the contents of @mqe to the next available entry on
  137. * the @q. This function will then ring the Work Queue Doorbell to signal the
  138. * HBA to start processing the Work Queue Entry. This function returns 0 if
  139. * successful. If no entries are available on @q then this function will return
  140. * -ENOMEM.
  141. * The caller is expected to hold the hbalock when calling this routine.
  142. **/
  143. static uint32_t
  144. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  145. {
  146. struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
  147. struct lpfc_register doorbell;
  148. uint32_t host_index;
  149. /* If the host has not yet processed the next entry then we are done */
  150. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  151. return -ENOMEM;
  152. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  153. /* Save off the mailbox pointer for completion */
  154. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  155. /* Update the host index before invoking device */
  156. host_index = q->host_index;
  157. q->host_index = ((q->host_index + 1) % q->entry_count);
  158. /* Ring Doorbell */
  159. doorbell.word0 = 0;
  160. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  161. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  162. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  163. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  164. return 0;
  165. }
  166. /**
  167. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  168. * @q: The Mailbox Queue to operate on.
  169. *
  170. * This routine will update the HBA index of a queue to reflect consumption of
  171. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  172. * an entry the host calls this function to update the queue's internal
  173. * pointers. This routine returns the number of entries that were consumed by
  174. * the HBA.
  175. **/
  176. static uint32_t
  177. lpfc_sli4_mq_release(struct lpfc_queue *q)
  178. {
  179. /* Clear the mailbox pointer for completion */
  180. q->phba->mbox = NULL;
  181. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  182. return 1;
  183. }
  184. /**
  185. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  186. * @q: The Event Queue to get the first valid EQE from
  187. *
  188. * This routine will get the first valid Event Queue Entry from @q, update
  189. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  190. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  191. * processed, but not popped back to the HBA then this routine will return NULL.
  192. **/
  193. static struct lpfc_eqe *
  194. lpfc_sli4_eq_get(struct lpfc_queue *q)
  195. {
  196. struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
  197. /* If the next EQE is not valid then we are done */
  198. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  199. return NULL;
  200. /* If the host has not yet processed the next entry then we are done */
  201. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  202. return NULL;
  203. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  204. return eqe;
  205. }
  206. /**
  207. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  208. * @q: The Event Queue that the host has completed processing for.
  209. * @arm: Indicates whether the host wants to arms this CQ.
  210. *
  211. * This routine will mark all Event Queue Entries on @q, from the last
  212. * known completed entry to the last entry that was processed, as completed
  213. * by clearing the valid bit for each completion queue entry. Then it will
  214. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  215. * The internal host index in the @q will be updated by this routine to indicate
  216. * that the host has finished processing the entries. The @arm parameter
  217. * indicates that the queue should be rearmed when ringing the doorbell.
  218. *
  219. * This function will return the number of EQEs that were popped.
  220. **/
  221. uint32_t
  222. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  223. {
  224. uint32_t released = 0;
  225. struct lpfc_eqe *temp_eqe;
  226. struct lpfc_register doorbell;
  227. /* while there are valid entries */
  228. while (q->hba_index != q->host_index) {
  229. temp_eqe = q->qe[q->host_index].eqe;
  230. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  231. released++;
  232. q->host_index = ((q->host_index + 1) % q->entry_count);
  233. }
  234. if (unlikely(released == 0 && !arm))
  235. return 0;
  236. /* ring doorbell for number popped */
  237. doorbell.word0 = 0;
  238. if (arm) {
  239. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  240. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  241. }
  242. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  243. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  244. bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
  245. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  246. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  247. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  248. readl(q->phba->sli4_hba.EQCQDBregaddr);
  249. return released;
  250. }
  251. /**
  252. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  253. * @q: The Completion Queue to get the first valid CQE from
  254. *
  255. * This routine will get the first valid Completion Queue Entry from @q, update
  256. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  257. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  258. * processed, but not popped back to the HBA then this routine will return NULL.
  259. **/
  260. static struct lpfc_cqe *
  261. lpfc_sli4_cq_get(struct lpfc_queue *q)
  262. {
  263. struct lpfc_cqe *cqe;
  264. /* If the next CQE is not valid then we are done */
  265. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  266. return NULL;
  267. /* If the host has not yet processed the next entry then we are done */
  268. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  269. return NULL;
  270. cqe = q->qe[q->hba_index].cqe;
  271. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  272. return cqe;
  273. }
  274. /**
  275. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  276. * @q: The Completion Queue that the host has completed processing for.
  277. * @arm: Indicates whether the host wants to arms this CQ.
  278. *
  279. * This routine will mark all Completion queue entries on @q, from the last
  280. * known completed entry to the last entry that was processed, as completed
  281. * by clearing the valid bit for each completion queue entry. Then it will
  282. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  283. * The internal host index in the @q will be updated by this routine to indicate
  284. * that the host has finished processing the entries. The @arm parameter
  285. * indicates that the queue should be rearmed when ringing the doorbell.
  286. *
  287. * This function will return the number of CQEs that were released.
  288. **/
  289. uint32_t
  290. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  291. {
  292. uint32_t released = 0;
  293. struct lpfc_cqe *temp_qe;
  294. struct lpfc_register doorbell;
  295. /* while there are valid entries */
  296. while (q->hba_index != q->host_index) {
  297. temp_qe = q->qe[q->host_index].cqe;
  298. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  299. released++;
  300. q->host_index = ((q->host_index + 1) % q->entry_count);
  301. }
  302. if (unlikely(released == 0 && !arm))
  303. return 0;
  304. /* ring doorbell for number popped */
  305. doorbell.word0 = 0;
  306. if (arm)
  307. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  308. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  309. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  310. bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
  311. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  312. return released;
  313. }
  314. /**
  315. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  316. * @q: The Header Receive Queue to operate on.
  317. * @wqe: The Receive Queue Entry to put on the Receive queue.
  318. *
  319. * This routine will copy the contents of @wqe to the next available entry on
  320. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  321. * HBA to start processing the Receive Queue Entry. This function returns the
  322. * index that the rqe was copied to if successful. If no entries are available
  323. * on @q then this function will return -ENOMEM.
  324. * The caller is expected to hold the hbalock when calling this routine.
  325. **/
  326. static int
  327. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  328. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  329. {
  330. struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
  331. struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
  332. struct lpfc_register doorbell;
  333. int put_index = hq->host_index;
  334. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  335. return -EINVAL;
  336. if (hq->host_index != dq->host_index)
  337. return -EINVAL;
  338. /* If the host has not yet processed the next entry then we are done */
  339. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  340. return -EBUSY;
  341. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  342. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  343. /* Update the host index to point to the next slot */
  344. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  345. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  346. /* Ring The Header Receive Queue Doorbell */
  347. if (!(hq->host_index % hq->entry_repost)) {
  348. doorbell.word0 = 0;
  349. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  350. hq->entry_repost);
  351. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  352. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  353. }
  354. return put_index;
  355. }
  356. /**
  357. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  358. * @q: The Header Receive Queue to operate on.
  359. *
  360. * This routine will update the HBA index of a queue to reflect consumption of
  361. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  362. * consumed an entry the host calls this function to update the queue's
  363. * internal pointers. This routine returns the number of entries that were
  364. * consumed by the HBA.
  365. **/
  366. static uint32_t
  367. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  368. {
  369. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  370. return 0;
  371. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  372. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  373. return 1;
  374. }
  375. /**
  376. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  377. * @phba: Pointer to HBA context object.
  378. * @pring: Pointer to driver SLI ring object.
  379. *
  380. * This function returns pointer to next command iocb entry
  381. * in the command ring. The caller must hold hbalock to prevent
  382. * other threads consume the next command iocb.
  383. * SLI-2/SLI-3 provide different sized iocbs.
  384. **/
  385. static inline IOCB_t *
  386. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  387. {
  388. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  389. pring->cmdidx * phba->iocb_cmd_size);
  390. }
  391. /**
  392. * lpfc_resp_iocb - Get next response iocb entry in the ring
  393. * @phba: Pointer to HBA context object.
  394. * @pring: Pointer to driver SLI ring object.
  395. *
  396. * This function returns pointer to next response iocb entry
  397. * in the response ring. The caller must hold hbalock to make sure
  398. * that no other thread consume the next response iocb.
  399. * SLI-2/SLI-3 provide different sized iocbs.
  400. **/
  401. static inline IOCB_t *
  402. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  403. {
  404. return (IOCB_t *) (((char *) pring->rspringaddr) +
  405. pring->rspidx * phba->iocb_rsp_size);
  406. }
  407. /**
  408. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  409. * @phba: Pointer to HBA context object.
  410. *
  411. * This function is called with hbalock held. This function
  412. * allocates a new driver iocb object from the iocb pool. If the
  413. * allocation is successful, it returns pointer to the newly
  414. * allocated iocb object else it returns NULL.
  415. **/
  416. static struct lpfc_iocbq *
  417. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  418. {
  419. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  420. struct lpfc_iocbq * iocbq = NULL;
  421. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  422. if (iocbq)
  423. phba->iocb_cnt++;
  424. if (phba->iocb_cnt > phba->iocb_max)
  425. phba->iocb_max = phba->iocb_cnt;
  426. return iocbq;
  427. }
  428. /**
  429. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  430. * @phba: Pointer to HBA context object.
  431. * @xritag: XRI value.
  432. *
  433. * This function clears the sglq pointer from the array of acive
  434. * sglq's. The xritag that is passed in is used to index into the
  435. * array. Before the xritag can be used it needs to be adjusted
  436. * by subtracting the xribase.
  437. *
  438. * Returns sglq ponter = success, NULL = Failure.
  439. **/
  440. static struct lpfc_sglq *
  441. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  442. {
  443. struct lpfc_sglq *sglq;
  444. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  445. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  446. return sglq;
  447. }
  448. /**
  449. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  450. * @phba: Pointer to HBA context object.
  451. * @xritag: XRI value.
  452. *
  453. * This function returns the sglq pointer from the array of acive
  454. * sglq's. The xritag that is passed in is used to index into the
  455. * array. Before the xritag can be used it needs to be adjusted
  456. * by subtracting the xribase.
  457. *
  458. * Returns sglq ponter = success, NULL = Failure.
  459. **/
  460. struct lpfc_sglq *
  461. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  462. {
  463. struct lpfc_sglq *sglq;
  464. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  465. return sglq;
  466. }
  467. /**
  468. * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
  469. * @phba: Pointer to HBA context object.
  470. * @ndlp: nodelist pointer for this target.
  471. * @xritag: xri used in this exchange.
  472. * @rxid: Remote Exchange ID.
  473. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  474. *
  475. * This function is called with hbalock held.
  476. * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
  477. * rrq struct and adds it to the active_rrq_list.
  478. *
  479. * returns 0 for rrq slot for this xri
  480. * < 0 Were not able to get rrq mem or invalid parameter.
  481. **/
  482. static int
  483. __lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  484. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  485. {
  486. struct lpfc_node_rrq *rrq;
  487. int empty;
  488. uint32_t did = 0;
  489. if (!ndlp)
  490. return -EINVAL;
  491. if (!phba->cfg_enable_rrq)
  492. return -EINVAL;
  493. if (phba->pport->load_flag & FC_UNLOADING) {
  494. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  495. goto out;
  496. }
  497. did = ndlp->nlp_DID;
  498. /*
  499. * set the active bit even if there is no mem available.
  500. */
  501. if (NLP_CHK_FREE_REQ(ndlp))
  502. goto out;
  503. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  504. goto out;
  505. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  506. goto out;
  507. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  508. if (rrq) {
  509. rrq->send_rrq = send_rrq;
  510. rrq->xritag = xritag;
  511. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  512. rrq->ndlp = ndlp;
  513. rrq->nlp_DID = ndlp->nlp_DID;
  514. rrq->vport = ndlp->vport;
  515. rrq->rxid = rxid;
  516. empty = list_empty(&phba->active_rrq_list);
  517. rrq->send_rrq = send_rrq;
  518. list_add_tail(&rrq->list, &phba->active_rrq_list);
  519. if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
  520. phba->hba_flag |= HBA_RRQ_ACTIVE;
  521. if (empty)
  522. lpfc_worker_wake_up(phba);
  523. }
  524. return 0;
  525. }
  526. out:
  527. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  528. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  529. " DID:0x%x Send:%d\n",
  530. xritag, rxid, did, send_rrq);
  531. return -EINVAL;
  532. }
  533. /**
  534. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  535. * @phba: Pointer to HBA context object.
  536. * @xritag: xri used in this exchange.
  537. * @rrq: The RRQ to be cleared.
  538. *
  539. **/
  540. void
  541. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  542. uint16_t xritag,
  543. struct lpfc_node_rrq *rrq)
  544. {
  545. struct lpfc_nodelist *ndlp = NULL;
  546. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  547. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  548. /* The target DID could have been swapped (cable swap)
  549. * we should use the ndlp from the findnode if it is
  550. * available.
  551. */
  552. if ((!ndlp) && rrq->ndlp)
  553. ndlp = rrq->ndlp;
  554. if (!ndlp)
  555. goto out;
  556. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  557. rrq->send_rrq = 0;
  558. rrq->xritag = 0;
  559. rrq->rrq_stop_time = 0;
  560. }
  561. out:
  562. mempool_free(rrq, phba->rrq_pool);
  563. }
  564. /**
  565. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  566. * @phba: Pointer to HBA context object.
  567. *
  568. * This function is called with hbalock held. This function
  569. * Checks if stop_time (ratov from setting rrq active) has
  570. * been reached, if it has and the send_rrq flag is set then
  571. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  572. * then it will just call the routine to clear the rrq and
  573. * free the rrq resource.
  574. * The timer is set to the next rrq that is going to expire before
  575. * leaving the routine.
  576. *
  577. **/
  578. void
  579. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  580. {
  581. struct lpfc_node_rrq *rrq;
  582. struct lpfc_node_rrq *nextrrq;
  583. unsigned long next_time;
  584. unsigned long iflags;
  585. LIST_HEAD(send_rrq);
  586. spin_lock_irqsave(&phba->hbalock, iflags);
  587. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  588. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  589. list_for_each_entry_safe(rrq, nextrrq,
  590. &phba->active_rrq_list, list) {
  591. if (time_after(jiffies, rrq->rrq_stop_time))
  592. list_move(&rrq->list, &send_rrq);
  593. else if (time_before(rrq->rrq_stop_time, next_time))
  594. next_time = rrq->rrq_stop_time;
  595. }
  596. spin_unlock_irqrestore(&phba->hbalock, iflags);
  597. if (!list_empty(&phba->active_rrq_list))
  598. mod_timer(&phba->rrq_tmr, next_time);
  599. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  600. list_del(&rrq->list);
  601. if (!rrq->send_rrq)
  602. /* this call will free the rrq */
  603. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  604. else if (lpfc_send_rrq(phba, rrq)) {
  605. /* if we send the rrq then the completion handler
  606. * will clear the bit in the xribitmap.
  607. */
  608. lpfc_clr_rrq_active(phba, rrq->xritag,
  609. rrq);
  610. }
  611. }
  612. }
  613. /**
  614. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  615. * @vport: Pointer to vport context object.
  616. * @xri: The xri used in the exchange.
  617. * @did: The targets DID for this exchange.
  618. *
  619. * returns NULL = rrq not found in the phba->active_rrq_list.
  620. * rrq = rrq for this xri and target.
  621. **/
  622. struct lpfc_node_rrq *
  623. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  624. {
  625. struct lpfc_hba *phba = vport->phba;
  626. struct lpfc_node_rrq *rrq;
  627. struct lpfc_node_rrq *nextrrq;
  628. unsigned long iflags;
  629. if (phba->sli_rev != LPFC_SLI_REV4)
  630. return NULL;
  631. spin_lock_irqsave(&phba->hbalock, iflags);
  632. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  633. if (rrq->vport == vport && rrq->xritag == xri &&
  634. rrq->nlp_DID == did){
  635. list_del(&rrq->list);
  636. spin_unlock_irqrestore(&phba->hbalock, iflags);
  637. return rrq;
  638. }
  639. }
  640. spin_unlock_irqrestore(&phba->hbalock, iflags);
  641. return NULL;
  642. }
  643. /**
  644. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  645. * @vport: Pointer to vport context object.
  646. * @ndlp: Pointer to the lpfc_node_list structure.
  647. * If ndlp is NULL Remove all active RRQs for this vport from the
  648. * phba->active_rrq_list and clear the rrq.
  649. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  650. **/
  651. void
  652. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  653. {
  654. struct lpfc_hba *phba = vport->phba;
  655. struct lpfc_node_rrq *rrq;
  656. struct lpfc_node_rrq *nextrrq;
  657. unsigned long iflags;
  658. LIST_HEAD(rrq_list);
  659. if (phba->sli_rev != LPFC_SLI_REV4)
  660. return;
  661. if (!ndlp) {
  662. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  663. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  664. }
  665. spin_lock_irqsave(&phba->hbalock, iflags);
  666. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  667. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  668. list_move(&rrq->list, &rrq_list);
  669. spin_unlock_irqrestore(&phba->hbalock, iflags);
  670. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  671. list_del(&rrq->list);
  672. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  673. }
  674. }
  675. /**
  676. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  677. * @phba: Pointer to HBA context object.
  678. *
  679. * Remove all rrqs from the phba->active_rrq_list and free them by
  680. * calling __lpfc_clr_active_rrq
  681. *
  682. **/
  683. void
  684. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  685. {
  686. struct lpfc_node_rrq *rrq;
  687. struct lpfc_node_rrq *nextrrq;
  688. unsigned long next_time;
  689. unsigned long iflags;
  690. LIST_HEAD(rrq_list);
  691. if (phba->sli_rev != LPFC_SLI_REV4)
  692. return;
  693. spin_lock_irqsave(&phba->hbalock, iflags);
  694. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  695. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  696. list_splice_init(&phba->active_rrq_list, &rrq_list);
  697. spin_unlock_irqrestore(&phba->hbalock, iflags);
  698. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  699. list_del(&rrq->list);
  700. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  701. }
  702. if (!list_empty(&phba->active_rrq_list))
  703. mod_timer(&phba->rrq_tmr, next_time);
  704. }
  705. /**
  706. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  707. * @phba: Pointer to HBA context object.
  708. * @ndlp: Targets nodelist pointer for this exchange.
  709. * @xritag the xri in the bitmap to test.
  710. *
  711. * This function is called with hbalock held. This function
  712. * returns 0 = rrq not active for this xri
  713. * 1 = rrq is valid for this xri.
  714. **/
  715. int
  716. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  717. uint16_t xritag)
  718. {
  719. if (!ndlp)
  720. return 0;
  721. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  722. return 1;
  723. else
  724. return 0;
  725. }
  726. /**
  727. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  728. * @phba: Pointer to HBA context object.
  729. * @ndlp: nodelist pointer for this target.
  730. * @xritag: xri used in this exchange.
  731. * @rxid: Remote Exchange ID.
  732. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  733. *
  734. * This function takes the hbalock.
  735. * The active bit is always set in the active rrq xri_bitmap even
  736. * if there is no slot avaiable for the other rrq information.
  737. *
  738. * returns 0 rrq actived for this xri
  739. * < 0 No memory or invalid ndlp.
  740. **/
  741. int
  742. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  743. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  744. {
  745. int ret;
  746. unsigned long iflags;
  747. spin_lock_irqsave(&phba->hbalock, iflags);
  748. ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
  749. spin_unlock_irqrestore(&phba->hbalock, iflags);
  750. return ret;
  751. }
  752. /**
  753. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  754. * @phba: Pointer to HBA context object.
  755. * @piocb: Pointer to the iocbq.
  756. *
  757. * This function is called with hbalock held. This function
  758. * gets a new driver sglq object from the sglq list. If the
  759. * list is not empty then it is successful, it returns pointer to the newly
  760. * allocated sglq object else it returns NULL.
  761. **/
  762. static struct lpfc_sglq *
  763. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  764. {
  765. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  766. struct lpfc_sglq *sglq = NULL;
  767. struct lpfc_sglq *start_sglq = NULL;
  768. struct lpfc_scsi_buf *lpfc_cmd;
  769. struct lpfc_nodelist *ndlp;
  770. int found = 0;
  771. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  772. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  773. ndlp = lpfc_cmd->rdata->pnode;
  774. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  775. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  776. ndlp = piocbq->context_un.ndlp;
  777. else
  778. ndlp = piocbq->context1;
  779. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  780. start_sglq = sglq;
  781. while (!found) {
  782. if (!sglq)
  783. return NULL;
  784. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
  785. /* This xri has an rrq outstanding for this DID.
  786. * put it back in the list and get another xri.
  787. */
  788. list_add_tail(&sglq->list, lpfc_sgl_list);
  789. sglq = NULL;
  790. list_remove_head(lpfc_sgl_list, sglq,
  791. struct lpfc_sglq, list);
  792. if (sglq == start_sglq) {
  793. sglq = NULL;
  794. break;
  795. } else
  796. continue;
  797. }
  798. sglq->ndlp = ndlp;
  799. found = 1;
  800. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  801. sglq->state = SGL_ALLOCATED;
  802. }
  803. return sglq;
  804. }
  805. /**
  806. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  807. * @phba: Pointer to HBA context object.
  808. *
  809. * This function is called with no lock held. This function
  810. * allocates a new driver iocb object from the iocb pool. If the
  811. * allocation is successful, it returns pointer to the newly
  812. * allocated iocb object else it returns NULL.
  813. **/
  814. struct lpfc_iocbq *
  815. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  816. {
  817. struct lpfc_iocbq * iocbq = NULL;
  818. unsigned long iflags;
  819. spin_lock_irqsave(&phba->hbalock, iflags);
  820. iocbq = __lpfc_sli_get_iocbq(phba);
  821. spin_unlock_irqrestore(&phba->hbalock, iflags);
  822. return iocbq;
  823. }
  824. /**
  825. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  826. * @phba: Pointer to HBA context object.
  827. * @iocbq: Pointer to driver iocb object.
  828. *
  829. * This function is called with hbalock held to release driver
  830. * iocb object to the iocb pool. The iotag in the iocb object
  831. * does not change for each use of the iocb object. This function
  832. * clears all other fields of the iocb object when it is freed.
  833. * The sqlq structure that holds the xritag and phys and virtual
  834. * mappings for the scatter gather list is retrieved from the
  835. * active array of sglq. The get of the sglq pointer also clears
  836. * the entry in the array. If the status of the IO indiactes that
  837. * this IO was aborted then the sglq entry it put on the
  838. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  839. * IO has good status or fails for any other reason then the sglq
  840. * entry is added to the free list (lpfc_sgl_list).
  841. **/
  842. static void
  843. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  844. {
  845. struct lpfc_sglq *sglq;
  846. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  847. unsigned long iflag = 0;
  848. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  849. if (iocbq->sli4_xritag == NO_XRI)
  850. sglq = NULL;
  851. else
  852. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  853. if (sglq) {
  854. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  855. (sglq->state != SGL_XRI_ABORTED)) {
  856. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  857. iflag);
  858. list_add(&sglq->list,
  859. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  860. spin_unlock_irqrestore(
  861. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  862. } else {
  863. sglq->state = SGL_FREED;
  864. sglq->ndlp = NULL;
  865. list_add_tail(&sglq->list,
  866. &phba->sli4_hba.lpfc_sgl_list);
  867. /* Check if TXQ queue needs to be serviced */
  868. if (pring->txq_cnt)
  869. lpfc_worker_wake_up(phba);
  870. }
  871. }
  872. /*
  873. * Clean all volatile data fields, preserve iotag and node struct.
  874. */
  875. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  876. iocbq->sli4_lxritag = NO_XRI;
  877. iocbq->sli4_xritag = NO_XRI;
  878. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  879. }
  880. /**
  881. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  882. * @phba: Pointer to HBA context object.
  883. * @iocbq: Pointer to driver iocb object.
  884. *
  885. * This function is called with hbalock held to release driver
  886. * iocb object to the iocb pool. The iotag in the iocb object
  887. * does not change for each use of the iocb object. This function
  888. * clears all other fields of the iocb object when it is freed.
  889. **/
  890. static void
  891. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  892. {
  893. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  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_xritag = NO_XRI;
  899. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  900. }
  901. /**
  902. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  903. * @phba: Pointer to HBA context object.
  904. * @iocbq: Pointer to driver iocb object.
  905. *
  906. * This function is called with hbalock held to release driver
  907. * iocb object to the iocb pool. The iotag in the iocb object
  908. * does not change for each use of the iocb object. This function
  909. * clears all other fields of the iocb object when it is freed.
  910. **/
  911. static void
  912. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  913. {
  914. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  915. phba->iocb_cnt--;
  916. }
  917. /**
  918. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  919. * @phba: Pointer to HBA context object.
  920. * @iocbq: Pointer to driver iocb object.
  921. *
  922. * This function is called with no lock held to release the iocb to
  923. * iocb pool.
  924. **/
  925. void
  926. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  927. {
  928. unsigned long iflags;
  929. /*
  930. * Clean all volatile data fields, preserve iotag and node struct.
  931. */
  932. spin_lock_irqsave(&phba->hbalock, iflags);
  933. __lpfc_sli_release_iocbq(phba, iocbq);
  934. spin_unlock_irqrestore(&phba->hbalock, iflags);
  935. }
  936. /**
  937. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  938. * @phba: Pointer to HBA context object.
  939. * @iocblist: List of IOCBs.
  940. * @ulpstatus: ULP status in IOCB command field.
  941. * @ulpWord4: ULP word-4 in IOCB command field.
  942. *
  943. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  944. * on the list by invoking the complete callback function associated with the
  945. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  946. * fields.
  947. **/
  948. void
  949. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  950. uint32_t ulpstatus, uint32_t ulpWord4)
  951. {
  952. struct lpfc_iocbq *piocb;
  953. while (!list_empty(iocblist)) {
  954. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  955. if (!piocb->iocb_cmpl)
  956. lpfc_sli_release_iocbq(phba, piocb);
  957. else {
  958. piocb->iocb.ulpStatus = ulpstatus;
  959. piocb->iocb.un.ulpWord[4] = ulpWord4;
  960. (piocb->iocb_cmpl) (phba, piocb, piocb);
  961. }
  962. }
  963. return;
  964. }
  965. /**
  966. * lpfc_sli_iocb_cmd_type - Get the iocb type
  967. * @iocb_cmnd: iocb command code.
  968. *
  969. * This function is called by ring event handler function to get the iocb type.
  970. * This function translates the iocb command to an iocb command type used to
  971. * decide the final disposition of each completed IOCB.
  972. * The function returns
  973. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  974. * LPFC_SOL_IOCB if it is a solicited iocb completion
  975. * LPFC_ABORT_IOCB if it is an abort iocb
  976. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  977. *
  978. * The caller is not required to hold any lock.
  979. **/
  980. static lpfc_iocb_type
  981. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  982. {
  983. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  984. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  985. return 0;
  986. switch (iocb_cmnd) {
  987. case CMD_XMIT_SEQUENCE_CR:
  988. case CMD_XMIT_SEQUENCE_CX:
  989. case CMD_XMIT_BCAST_CN:
  990. case CMD_XMIT_BCAST_CX:
  991. case CMD_ELS_REQUEST_CR:
  992. case CMD_ELS_REQUEST_CX:
  993. case CMD_CREATE_XRI_CR:
  994. case CMD_CREATE_XRI_CX:
  995. case CMD_GET_RPI_CN:
  996. case CMD_XMIT_ELS_RSP_CX:
  997. case CMD_GET_RPI_CR:
  998. case CMD_FCP_IWRITE_CR:
  999. case CMD_FCP_IWRITE_CX:
  1000. case CMD_FCP_IREAD_CR:
  1001. case CMD_FCP_IREAD_CX:
  1002. case CMD_FCP_ICMND_CR:
  1003. case CMD_FCP_ICMND_CX:
  1004. case CMD_FCP_TSEND_CX:
  1005. case CMD_FCP_TRSP_CX:
  1006. case CMD_FCP_TRECEIVE_CX:
  1007. case CMD_FCP_AUTO_TRSP_CX:
  1008. case CMD_ADAPTER_MSG:
  1009. case CMD_ADAPTER_DUMP:
  1010. case CMD_XMIT_SEQUENCE64_CR:
  1011. case CMD_XMIT_SEQUENCE64_CX:
  1012. case CMD_XMIT_BCAST64_CN:
  1013. case CMD_XMIT_BCAST64_CX:
  1014. case CMD_ELS_REQUEST64_CR:
  1015. case CMD_ELS_REQUEST64_CX:
  1016. case CMD_FCP_IWRITE64_CR:
  1017. case CMD_FCP_IWRITE64_CX:
  1018. case CMD_FCP_IREAD64_CR:
  1019. case CMD_FCP_IREAD64_CX:
  1020. case CMD_FCP_ICMND64_CR:
  1021. case CMD_FCP_ICMND64_CX:
  1022. case CMD_FCP_TSEND64_CX:
  1023. case CMD_FCP_TRSP64_CX:
  1024. case CMD_FCP_TRECEIVE64_CX:
  1025. case CMD_GEN_REQUEST64_CR:
  1026. case CMD_GEN_REQUEST64_CX:
  1027. case CMD_XMIT_ELS_RSP64_CX:
  1028. case DSSCMD_IWRITE64_CR:
  1029. case DSSCMD_IWRITE64_CX:
  1030. case DSSCMD_IREAD64_CR:
  1031. case DSSCMD_IREAD64_CX:
  1032. type = LPFC_SOL_IOCB;
  1033. break;
  1034. case CMD_ABORT_XRI_CN:
  1035. case CMD_ABORT_XRI_CX:
  1036. case CMD_CLOSE_XRI_CN:
  1037. case CMD_CLOSE_XRI_CX:
  1038. case CMD_XRI_ABORTED_CX:
  1039. case CMD_ABORT_MXRI64_CN:
  1040. case CMD_XMIT_BLS_RSP64_CX:
  1041. type = LPFC_ABORT_IOCB;
  1042. break;
  1043. case CMD_RCV_SEQUENCE_CX:
  1044. case CMD_RCV_ELS_REQ_CX:
  1045. case CMD_RCV_SEQUENCE64_CX:
  1046. case CMD_RCV_ELS_REQ64_CX:
  1047. case CMD_ASYNC_STATUS:
  1048. case CMD_IOCB_RCV_SEQ64_CX:
  1049. case CMD_IOCB_RCV_ELS64_CX:
  1050. case CMD_IOCB_RCV_CONT64_CX:
  1051. case CMD_IOCB_RET_XRI64_CX:
  1052. type = LPFC_UNSOL_IOCB;
  1053. break;
  1054. case CMD_IOCB_XMIT_MSEQ64_CR:
  1055. case CMD_IOCB_XMIT_MSEQ64_CX:
  1056. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1057. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1058. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1059. case CMD_IOCB_ABORT_EXTENDED_CN:
  1060. case CMD_IOCB_RET_HBQE64_CN:
  1061. case CMD_IOCB_FCP_IBIDIR64_CR:
  1062. case CMD_IOCB_FCP_IBIDIR64_CX:
  1063. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1064. case CMD_IOCB_LOGENTRY_CN:
  1065. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1066. printk("%s - Unhandled SLI-3 Command x%x\n",
  1067. __func__, iocb_cmnd);
  1068. type = LPFC_UNKNOWN_IOCB;
  1069. break;
  1070. default:
  1071. type = LPFC_UNKNOWN_IOCB;
  1072. break;
  1073. }
  1074. return type;
  1075. }
  1076. /**
  1077. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1078. * @phba: Pointer to HBA context object.
  1079. *
  1080. * This function is called from SLI initialization code
  1081. * to configure every ring of the HBA's SLI interface. The
  1082. * caller is not required to hold any lock. This function issues
  1083. * a config_ring mailbox command for each ring.
  1084. * This function returns zero if successful else returns a negative
  1085. * error code.
  1086. **/
  1087. static int
  1088. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1089. {
  1090. struct lpfc_sli *psli = &phba->sli;
  1091. LPFC_MBOXQ_t *pmb;
  1092. MAILBOX_t *pmbox;
  1093. int i, rc, ret = 0;
  1094. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1095. if (!pmb)
  1096. return -ENOMEM;
  1097. pmbox = &pmb->u.mb;
  1098. phba->link_state = LPFC_INIT_MBX_CMDS;
  1099. for (i = 0; i < psli->num_rings; i++) {
  1100. lpfc_config_ring(phba, i, pmb);
  1101. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1102. if (rc != MBX_SUCCESS) {
  1103. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1104. "0446 Adapter failed to init (%d), "
  1105. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1106. "ring %d\n",
  1107. rc, pmbox->mbxCommand,
  1108. pmbox->mbxStatus, i);
  1109. phba->link_state = LPFC_HBA_ERROR;
  1110. ret = -ENXIO;
  1111. break;
  1112. }
  1113. }
  1114. mempool_free(pmb, phba->mbox_mem_pool);
  1115. return ret;
  1116. }
  1117. /**
  1118. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1119. * @phba: Pointer to HBA context object.
  1120. * @pring: Pointer to driver SLI ring object.
  1121. * @piocb: Pointer to the driver iocb object.
  1122. *
  1123. * This function is called with hbalock held. The function adds the
  1124. * new iocb to txcmplq of the given ring. This function always returns
  1125. * 0. If this function is called for ELS ring, this function checks if
  1126. * there is a vport associated with the ELS command. This function also
  1127. * starts els_tmofunc timer if this is an ELS command.
  1128. **/
  1129. static int
  1130. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1131. struct lpfc_iocbq *piocb)
  1132. {
  1133. list_add_tail(&piocb->list, &pring->txcmplq);
  1134. piocb->iocb_flag |= LPFC_IO_ON_Q;
  1135. pring->txcmplq_cnt++;
  1136. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1137. pring->txcmplq_max = pring->txcmplq_cnt;
  1138. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1139. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1140. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1141. if (!piocb->vport)
  1142. BUG();
  1143. else
  1144. mod_timer(&piocb->vport->els_tmofunc,
  1145. jiffies + HZ * (phba->fc_ratov << 1));
  1146. }
  1147. return 0;
  1148. }
  1149. /**
  1150. * lpfc_sli_ringtx_get - Get first element of the txq
  1151. * @phba: Pointer to HBA context object.
  1152. * @pring: Pointer to driver SLI ring object.
  1153. *
  1154. * This function is called with hbalock held to get next
  1155. * iocb in txq of the given ring. If there is any iocb in
  1156. * the txq, the function returns first iocb in the list after
  1157. * removing the iocb from the list, else it returns NULL.
  1158. **/
  1159. struct lpfc_iocbq *
  1160. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1161. {
  1162. struct lpfc_iocbq *cmd_iocb;
  1163. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1164. if (cmd_iocb != NULL)
  1165. pring->txq_cnt--;
  1166. return cmd_iocb;
  1167. }
  1168. /**
  1169. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1170. * @phba: Pointer to HBA context object.
  1171. * @pring: Pointer to driver SLI ring object.
  1172. *
  1173. * This function is called with hbalock held and the caller must post the
  1174. * iocb without releasing the lock. If the caller releases the lock,
  1175. * iocb slot returned by the function is not guaranteed to be available.
  1176. * The function returns pointer to the next available iocb slot if there
  1177. * is available slot in the ring, else it returns NULL.
  1178. * If the get index of the ring is ahead of the put index, the function
  1179. * will post an error attention event to the worker thread to take the
  1180. * HBA to offline state.
  1181. **/
  1182. static IOCB_t *
  1183. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1184. {
  1185. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1186. uint32_t max_cmd_idx = pring->numCiocb;
  1187. if ((pring->next_cmdidx == pring->cmdidx) &&
  1188. (++pring->next_cmdidx >= max_cmd_idx))
  1189. pring->next_cmdidx = 0;
  1190. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1191. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1192. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1193. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1194. "0315 Ring %d issue: portCmdGet %d "
  1195. "is bigger than cmd ring %d\n",
  1196. pring->ringno,
  1197. pring->local_getidx, max_cmd_idx);
  1198. phba->link_state = LPFC_HBA_ERROR;
  1199. /*
  1200. * All error attention handlers are posted to
  1201. * worker thread
  1202. */
  1203. phba->work_ha |= HA_ERATT;
  1204. phba->work_hs = HS_FFER3;
  1205. lpfc_worker_wake_up(phba);
  1206. return NULL;
  1207. }
  1208. if (pring->local_getidx == pring->next_cmdidx)
  1209. return NULL;
  1210. }
  1211. return lpfc_cmd_iocb(phba, pring);
  1212. }
  1213. /**
  1214. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1215. * @phba: Pointer to HBA context object.
  1216. * @iocbq: Pointer to driver iocb object.
  1217. *
  1218. * This function gets an iotag for the iocb. If there is no unused iotag and
  1219. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1220. * array and assigns a new iotag.
  1221. * The function returns the allocated iotag if successful, else returns zero.
  1222. * Zero is not a valid iotag.
  1223. * The caller is not required to hold any lock.
  1224. **/
  1225. uint16_t
  1226. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1227. {
  1228. struct lpfc_iocbq **new_arr;
  1229. struct lpfc_iocbq **old_arr;
  1230. size_t new_len;
  1231. struct lpfc_sli *psli = &phba->sli;
  1232. uint16_t iotag;
  1233. spin_lock_irq(&phba->hbalock);
  1234. iotag = psli->last_iotag;
  1235. if(++iotag < psli->iocbq_lookup_len) {
  1236. psli->last_iotag = iotag;
  1237. psli->iocbq_lookup[iotag] = iocbq;
  1238. spin_unlock_irq(&phba->hbalock);
  1239. iocbq->iotag = iotag;
  1240. return iotag;
  1241. } else if (psli->iocbq_lookup_len < (0xffff
  1242. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1243. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1244. spin_unlock_irq(&phba->hbalock);
  1245. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1246. GFP_KERNEL);
  1247. if (new_arr) {
  1248. spin_lock_irq(&phba->hbalock);
  1249. old_arr = psli->iocbq_lookup;
  1250. if (new_len <= psli->iocbq_lookup_len) {
  1251. /* highly unprobable case */
  1252. kfree(new_arr);
  1253. iotag = psli->last_iotag;
  1254. if(++iotag < psli->iocbq_lookup_len) {
  1255. psli->last_iotag = iotag;
  1256. psli->iocbq_lookup[iotag] = iocbq;
  1257. spin_unlock_irq(&phba->hbalock);
  1258. iocbq->iotag = iotag;
  1259. return iotag;
  1260. }
  1261. spin_unlock_irq(&phba->hbalock);
  1262. return 0;
  1263. }
  1264. if (psli->iocbq_lookup)
  1265. memcpy(new_arr, old_arr,
  1266. ((psli->last_iotag + 1) *
  1267. sizeof (struct lpfc_iocbq *)));
  1268. psli->iocbq_lookup = new_arr;
  1269. psli->iocbq_lookup_len = new_len;
  1270. psli->last_iotag = iotag;
  1271. psli->iocbq_lookup[iotag] = iocbq;
  1272. spin_unlock_irq(&phba->hbalock);
  1273. iocbq->iotag = iotag;
  1274. kfree(old_arr);
  1275. return iotag;
  1276. }
  1277. } else
  1278. spin_unlock_irq(&phba->hbalock);
  1279. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1280. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1281. psli->last_iotag);
  1282. return 0;
  1283. }
  1284. /**
  1285. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1286. * @phba: Pointer to HBA context object.
  1287. * @pring: Pointer to driver SLI ring object.
  1288. * @iocb: Pointer to iocb slot in the ring.
  1289. * @nextiocb: Pointer to driver iocb object which need to be
  1290. * posted to firmware.
  1291. *
  1292. * This function is called with hbalock held to post a new iocb to
  1293. * the firmware. This function copies the new iocb to ring iocb slot and
  1294. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1295. * a completion call back for this iocb else the function will free the
  1296. * iocb object.
  1297. **/
  1298. static void
  1299. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1300. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1301. {
  1302. /*
  1303. * Set up an iotag
  1304. */
  1305. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1306. if (pring->ringno == LPFC_ELS_RING) {
  1307. lpfc_debugfs_slow_ring_trc(phba,
  1308. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1309. *(((uint32_t *) &nextiocb->iocb) + 4),
  1310. *(((uint32_t *) &nextiocb->iocb) + 6),
  1311. *(((uint32_t *) &nextiocb->iocb) + 7));
  1312. }
  1313. /*
  1314. * Issue iocb command to adapter
  1315. */
  1316. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1317. wmb();
  1318. pring->stats.iocb_cmd++;
  1319. /*
  1320. * If there is no completion routine to call, we can release the
  1321. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1322. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1323. */
  1324. if (nextiocb->iocb_cmpl)
  1325. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1326. else
  1327. __lpfc_sli_release_iocbq(phba, nextiocb);
  1328. /*
  1329. * Let the HBA know what IOCB slot will be the next one the
  1330. * driver will put a command into.
  1331. */
  1332. pring->cmdidx = pring->next_cmdidx;
  1333. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1334. }
  1335. /**
  1336. * lpfc_sli_update_full_ring - Update the chip attention register
  1337. * @phba: Pointer to HBA context object.
  1338. * @pring: Pointer to driver SLI ring object.
  1339. *
  1340. * The caller is not required to hold any lock for calling this function.
  1341. * This function updates the chip attention bits for the ring to inform firmware
  1342. * that there are pending work to be done for this ring and requests an
  1343. * interrupt when there is space available in the ring. This function is
  1344. * called when the driver is unable to post more iocbs to the ring due
  1345. * to unavailability of space in the ring.
  1346. **/
  1347. static void
  1348. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1349. {
  1350. int ringno = pring->ringno;
  1351. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1352. wmb();
  1353. /*
  1354. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1355. * The HBA will tell us when an IOCB entry is available.
  1356. */
  1357. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1358. readl(phba->CAregaddr); /* flush */
  1359. pring->stats.iocb_cmd_full++;
  1360. }
  1361. /**
  1362. * lpfc_sli_update_ring - Update chip attention register
  1363. * @phba: Pointer to HBA context object.
  1364. * @pring: Pointer to driver SLI ring object.
  1365. *
  1366. * This function updates the chip attention register bit for the
  1367. * given ring to inform HBA that there is more work to be done
  1368. * in this ring. The caller is not required to hold any lock.
  1369. **/
  1370. static void
  1371. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1372. {
  1373. int ringno = pring->ringno;
  1374. /*
  1375. * Tell the HBA that there is work to do in this ring.
  1376. */
  1377. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1378. wmb();
  1379. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1380. readl(phba->CAregaddr); /* flush */
  1381. }
  1382. }
  1383. /**
  1384. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1385. * @phba: Pointer to HBA context object.
  1386. * @pring: Pointer to driver SLI ring object.
  1387. *
  1388. * This function is called with hbalock held to post pending iocbs
  1389. * in the txq to the firmware. This function is called when driver
  1390. * detects space available in the ring.
  1391. **/
  1392. static void
  1393. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1394. {
  1395. IOCB_t *iocb;
  1396. struct lpfc_iocbq *nextiocb;
  1397. /*
  1398. * Check to see if:
  1399. * (a) there is anything on the txq to send
  1400. * (b) link is up
  1401. * (c) link attention events can be processed (fcp ring only)
  1402. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1403. */
  1404. if (pring->txq_cnt &&
  1405. lpfc_is_link_up(phba) &&
  1406. (pring->ringno != phba->sli.fcp_ring ||
  1407. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1408. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1409. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1410. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1411. if (iocb)
  1412. lpfc_sli_update_ring(phba, pring);
  1413. else
  1414. lpfc_sli_update_full_ring(phba, pring);
  1415. }
  1416. return;
  1417. }
  1418. /**
  1419. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1420. * @phba: Pointer to HBA context object.
  1421. * @hbqno: HBQ number.
  1422. *
  1423. * This function is called with hbalock held to get the next
  1424. * available slot for the given HBQ. If there is free slot
  1425. * available for the HBQ it will return pointer to the next available
  1426. * HBQ entry else it will return NULL.
  1427. **/
  1428. static struct lpfc_hbq_entry *
  1429. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1430. {
  1431. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1432. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1433. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1434. hbqp->next_hbqPutIdx = 0;
  1435. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1436. uint32_t raw_index = phba->hbq_get[hbqno];
  1437. uint32_t getidx = le32_to_cpu(raw_index);
  1438. hbqp->local_hbqGetIdx = getidx;
  1439. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1440. lpfc_printf_log(phba, KERN_ERR,
  1441. LOG_SLI | LOG_VPORT,
  1442. "1802 HBQ %d: local_hbqGetIdx "
  1443. "%u is > than hbqp->entry_count %u\n",
  1444. hbqno, hbqp->local_hbqGetIdx,
  1445. hbqp->entry_count);
  1446. phba->link_state = LPFC_HBA_ERROR;
  1447. return NULL;
  1448. }
  1449. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1450. return NULL;
  1451. }
  1452. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1453. hbqp->hbqPutIdx;
  1454. }
  1455. /**
  1456. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1457. * @phba: Pointer to HBA context object.
  1458. *
  1459. * This function is called with no lock held to free all the
  1460. * hbq buffers while uninitializing the SLI interface. It also
  1461. * frees the HBQ buffers returned by the firmware but not yet
  1462. * processed by the upper layers.
  1463. **/
  1464. void
  1465. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1466. {
  1467. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1468. struct hbq_dmabuf *hbq_buf;
  1469. unsigned long flags;
  1470. int i, hbq_count;
  1471. uint32_t hbqno;
  1472. hbq_count = lpfc_sli_hbq_count();
  1473. /* Return all memory used by all HBQs */
  1474. spin_lock_irqsave(&phba->hbalock, flags);
  1475. for (i = 0; i < hbq_count; ++i) {
  1476. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1477. &phba->hbqs[i].hbq_buffer_list, list) {
  1478. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1479. list_del(&hbq_buf->dbuf.list);
  1480. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1481. }
  1482. phba->hbqs[i].buffer_count = 0;
  1483. }
  1484. /* Return all HBQ buffer that are in-fly */
  1485. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1486. list) {
  1487. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1488. list_del(&hbq_buf->dbuf.list);
  1489. if (hbq_buf->tag == -1) {
  1490. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1491. (phba, hbq_buf);
  1492. } else {
  1493. hbqno = hbq_buf->tag >> 16;
  1494. if (hbqno >= LPFC_MAX_HBQS)
  1495. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1496. (phba, hbq_buf);
  1497. else
  1498. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1499. hbq_buf);
  1500. }
  1501. }
  1502. /* Mark the HBQs not in use */
  1503. phba->hbq_in_use = 0;
  1504. spin_unlock_irqrestore(&phba->hbalock, flags);
  1505. }
  1506. /**
  1507. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1508. * @phba: Pointer to HBA context object.
  1509. * @hbqno: HBQ number.
  1510. * @hbq_buf: Pointer to HBQ buffer.
  1511. *
  1512. * This function is called with the hbalock held to post a
  1513. * hbq buffer to the firmware. If the function finds an empty
  1514. * slot in the HBQ, it will post the buffer. The function will return
  1515. * pointer to the hbq entry if it successfully post the buffer
  1516. * else it will return NULL.
  1517. **/
  1518. static int
  1519. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1520. struct hbq_dmabuf *hbq_buf)
  1521. {
  1522. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1523. }
  1524. /**
  1525. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1526. * @phba: Pointer to HBA context object.
  1527. * @hbqno: HBQ number.
  1528. * @hbq_buf: Pointer to HBQ buffer.
  1529. *
  1530. * This function is called with the hbalock held to post a hbq buffer to the
  1531. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1532. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1533. * it successfully post the buffer else it will return an error.
  1534. **/
  1535. static int
  1536. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1537. struct hbq_dmabuf *hbq_buf)
  1538. {
  1539. struct lpfc_hbq_entry *hbqe;
  1540. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1541. /* Get next HBQ entry slot to use */
  1542. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1543. if (hbqe) {
  1544. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1545. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1546. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1547. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1548. hbqe->bde.tus.f.bdeFlags = 0;
  1549. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1550. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1551. /* Sync SLIM */
  1552. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1553. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1554. /* flush */
  1555. readl(phba->hbq_put + hbqno);
  1556. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1557. return 0;
  1558. } else
  1559. return -ENOMEM;
  1560. }
  1561. /**
  1562. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1563. * @phba: Pointer to HBA context object.
  1564. * @hbqno: HBQ number.
  1565. * @hbq_buf: Pointer to HBQ buffer.
  1566. *
  1567. * This function is called with the hbalock held to post an RQE to the SLI4
  1568. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1569. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1570. **/
  1571. static int
  1572. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1573. struct hbq_dmabuf *hbq_buf)
  1574. {
  1575. int rc;
  1576. struct lpfc_rqe hrqe;
  1577. struct lpfc_rqe drqe;
  1578. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1579. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1580. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1581. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1582. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1583. &hrqe, &drqe);
  1584. if (rc < 0)
  1585. return rc;
  1586. hbq_buf->tag = rc;
  1587. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1588. return 0;
  1589. }
  1590. /* HBQ for ELS and CT traffic. */
  1591. static struct lpfc_hbq_init lpfc_els_hbq = {
  1592. .rn = 1,
  1593. .entry_count = 256,
  1594. .mask_count = 0,
  1595. .profile = 0,
  1596. .ring_mask = (1 << LPFC_ELS_RING),
  1597. .buffer_count = 0,
  1598. .init_count = 40,
  1599. .add_count = 40,
  1600. };
  1601. /* HBQ for the extra ring if needed */
  1602. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1603. .rn = 1,
  1604. .entry_count = 200,
  1605. .mask_count = 0,
  1606. .profile = 0,
  1607. .ring_mask = (1 << LPFC_EXTRA_RING),
  1608. .buffer_count = 0,
  1609. .init_count = 0,
  1610. .add_count = 5,
  1611. };
  1612. /* Array of HBQs */
  1613. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1614. &lpfc_els_hbq,
  1615. &lpfc_extra_hbq,
  1616. };
  1617. /**
  1618. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1619. * @phba: Pointer to HBA context object.
  1620. * @hbqno: HBQ number.
  1621. * @count: Number of HBQ buffers to be posted.
  1622. *
  1623. * This function is called with no lock held to post more hbq buffers to the
  1624. * given HBQ. The function returns the number of HBQ buffers successfully
  1625. * posted.
  1626. **/
  1627. static int
  1628. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1629. {
  1630. uint32_t i, posted = 0;
  1631. unsigned long flags;
  1632. struct hbq_dmabuf *hbq_buffer;
  1633. LIST_HEAD(hbq_buf_list);
  1634. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1635. return 0;
  1636. if ((phba->hbqs[hbqno].buffer_count + count) >
  1637. lpfc_hbq_defs[hbqno]->entry_count)
  1638. count = lpfc_hbq_defs[hbqno]->entry_count -
  1639. phba->hbqs[hbqno].buffer_count;
  1640. if (!count)
  1641. return 0;
  1642. /* Allocate HBQ entries */
  1643. for (i = 0; i < count; i++) {
  1644. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1645. if (!hbq_buffer)
  1646. break;
  1647. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1648. }
  1649. /* Check whether HBQ is still in use */
  1650. spin_lock_irqsave(&phba->hbalock, flags);
  1651. if (!phba->hbq_in_use)
  1652. goto err;
  1653. while (!list_empty(&hbq_buf_list)) {
  1654. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1655. dbuf.list);
  1656. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1657. (hbqno << 16));
  1658. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1659. phba->hbqs[hbqno].buffer_count++;
  1660. posted++;
  1661. } else
  1662. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1663. }
  1664. spin_unlock_irqrestore(&phba->hbalock, flags);
  1665. return posted;
  1666. err:
  1667. spin_unlock_irqrestore(&phba->hbalock, flags);
  1668. while (!list_empty(&hbq_buf_list)) {
  1669. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1670. dbuf.list);
  1671. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1672. }
  1673. return 0;
  1674. }
  1675. /**
  1676. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1677. * @phba: Pointer to HBA context object.
  1678. * @qno: HBQ number.
  1679. *
  1680. * This function posts more buffers to the HBQ. This function
  1681. * is called with no lock held. The function returns the number of HBQ entries
  1682. * successfully allocated.
  1683. **/
  1684. int
  1685. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1686. {
  1687. if (phba->sli_rev == LPFC_SLI_REV4)
  1688. return 0;
  1689. else
  1690. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1691. lpfc_hbq_defs[qno]->add_count);
  1692. }
  1693. /**
  1694. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1695. * @phba: Pointer to HBA context object.
  1696. * @qno: HBQ queue number.
  1697. *
  1698. * This function is called from SLI initialization code path with
  1699. * no lock held to post initial HBQ buffers to firmware. The
  1700. * function returns the number of HBQ entries successfully allocated.
  1701. **/
  1702. static int
  1703. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1704. {
  1705. if (phba->sli_rev == LPFC_SLI_REV4)
  1706. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1707. lpfc_hbq_defs[qno]->entry_count);
  1708. else
  1709. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1710. lpfc_hbq_defs[qno]->init_count);
  1711. }
  1712. /**
  1713. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1714. * @phba: Pointer to HBA context object.
  1715. * @hbqno: HBQ number.
  1716. *
  1717. * This function removes the first hbq buffer on an hbq list and returns a
  1718. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1719. **/
  1720. static struct hbq_dmabuf *
  1721. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1722. {
  1723. struct lpfc_dmabuf *d_buf;
  1724. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1725. if (!d_buf)
  1726. return NULL;
  1727. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1728. }
  1729. /**
  1730. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1731. * @phba: Pointer to HBA context object.
  1732. * @tag: Tag of the hbq buffer.
  1733. *
  1734. * This function is called with hbalock held. This function searches
  1735. * for the hbq buffer associated with the given tag in the hbq buffer
  1736. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1737. * it returns NULL.
  1738. **/
  1739. static struct hbq_dmabuf *
  1740. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1741. {
  1742. struct lpfc_dmabuf *d_buf;
  1743. struct hbq_dmabuf *hbq_buf;
  1744. uint32_t hbqno;
  1745. hbqno = tag >> 16;
  1746. if (hbqno >= LPFC_MAX_HBQS)
  1747. return NULL;
  1748. spin_lock_irq(&phba->hbalock);
  1749. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1750. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1751. if (hbq_buf->tag == tag) {
  1752. spin_unlock_irq(&phba->hbalock);
  1753. return hbq_buf;
  1754. }
  1755. }
  1756. spin_unlock_irq(&phba->hbalock);
  1757. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1758. "1803 Bad hbq tag. Data: x%x x%x\n",
  1759. tag, phba->hbqs[tag >> 16].buffer_count);
  1760. return NULL;
  1761. }
  1762. /**
  1763. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1764. * @phba: Pointer to HBA context object.
  1765. * @hbq_buffer: Pointer to HBQ buffer.
  1766. *
  1767. * This function is called with hbalock. This function gives back
  1768. * the hbq buffer to firmware. If the HBQ does not have space to
  1769. * post the buffer, it will free the buffer.
  1770. **/
  1771. void
  1772. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1773. {
  1774. uint32_t hbqno;
  1775. if (hbq_buffer) {
  1776. hbqno = hbq_buffer->tag >> 16;
  1777. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1778. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1779. }
  1780. }
  1781. /**
  1782. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1783. * @mbxCommand: mailbox command code.
  1784. *
  1785. * This function is called by the mailbox event handler function to verify
  1786. * that the completed mailbox command is a legitimate mailbox command. If the
  1787. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1788. * and the mailbox event handler will take the HBA offline.
  1789. **/
  1790. static int
  1791. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1792. {
  1793. uint8_t ret;
  1794. switch (mbxCommand) {
  1795. case MBX_LOAD_SM:
  1796. case MBX_READ_NV:
  1797. case MBX_WRITE_NV:
  1798. case MBX_WRITE_VPARMS:
  1799. case MBX_RUN_BIU_DIAG:
  1800. case MBX_INIT_LINK:
  1801. case MBX_DOWN_LINK:
  1802. case MBX_CONFIG_LINK:
  1803. case MBX_CONFIG_RING:
  1804. case MBX_RESET_RING:
  1805. case MBX_READ_CONFIG:
  1806. case MBX_READ_RCONFIG:
  1807. case MBX_READ_SPARM:
  1808. case MBX_READ_STATUS:
  1809. case MBX_READ_RPI:
  1810. case MBX_READ_XRI:
  1811. case MBX_READ_REV:
  1812. case MBX_READ_LNK_STAT:
  1813. case MBX_REG_LOGIN:
  1814. case MBX_UNREG_LOGIN:
  1815. case MBX_CLEAR_LA:
  1816. case MBX_DUMP_MEMORY:
  1817. case MBX_DUMP_CONTEXT:
  1818. case MBX_RUN_DIAGS:
  1819. case MBX_RESTART:
  1820. case MBX_UPDATE_CFG:
  1821. case MBX_DOWN_LOAD:
  1822. case MBX_DEL_LD_ENTRY:
  1823. case MBX_RUN_PROGRAM:
  1824. case MBX_SET_MASK:
  1825. case MBX_SET_VARIABLE:
  1826. case MBX_UNREG_D_ID:
  1827. case MBX_KILL_BOARD:
  1828. case MBX_CONFIG_FARP:
  1829. case MBX_BEACON:
  1830. case MBX_LOAD_AREA:
  1831. case MBX_RUN_BIU_DIAG64:
  1832. case MBX_CONFIG_PORT:
  1833. case MBX_READ_SPARM64:
  1834. case MBX_READ_RPI64:
  1835. case MBX_REG_LOGIN64:
  1836. case MBX_READ_TOPOLOGY:
  1837. case MBX_WRITE_WWN:
  1838. case MBX_SET_DEBUG:
  1839. case MBX_LOAD_EXP_ROM:
  1840. case MBX_ASYNCEVT_ENABLE:
  1841. case MBX_REG_VPI:
  1842. case MBX_UNREG_VPI:
  1843. case MBX_HEARTBEAT:
  1844. case MBX_PORT_CAPABILITIES:
  1845. case MBX_PORT_IOV_CONTROL:
  1846. case MBX_SLI4_CONFIG:
  1847. case MBX_SLI4_REQ_FTRS:
  1848. case MBX_REG_FCFI:
  1849. case MBX_UNREG_FCFI:
  1850. case MBX_REG_VFI:
  1851. case MBX_UNREG_VFI:
  1852. case MBX_INIT_VPI:
  1853. case MBX_INIT_VFI:
  1854. case MBX_RESUME_RPI:
  1855. case MBX_READ_EVENT_LOG_STATUS:
  1856. case MBX_READ_EVENT_LOG:
  1857. case MBX_SECURITY_MGMT:
  1858. case MBX_AUTH_PORT:
  1859. ret = mbxCommand;
  1860. break;
  1861. default:
  1862. ret = MBX_SHUTDOWN;
  1863. break;
  1864. }
  1865. return ret;
  1866. }
  1867. /**
  1868. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1869. * @phba: Pointer to HBA context object.
  1870. * @pmboxq: Pointer to mailbox command.
  1871. *
  1872. * This is completion handler function for mailbox commands issued from
  1873. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1874. * mailbox event handler function with no lock held. This function
  1875. * will wake up thread waiting on the wait queue pointed by context1
  1876. * of the mailbox.
  1877. **/
  1878. void
  1879. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1880. {
  1881. wait_queue_head_t *pdone_q;
  1882. unsigned long drvr_flag;
  1883. /*
  1884. * If pdone_q is empty, the driver thread gave up waiting and
  1885. * continued running.
  1886. */
  1887. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1888. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1889. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1890. if (pdone_q)
  1891. wake_up_interruptible(pdone_q);
  1892. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1893. return;
  1894. }
  1895. /**
  1896. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1897. * @phba: Pointer to HBA context object.
  1898. * @pmb: Pointer to mailbox object.
  1899. *
  1900. * This function is the default mailbox completion handler. It
  1901. * frees the memory resources associated with the completed mailbox
  1902. * command. If the completed command is a REG_LOGIN mailbox command,
  1903. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1904. **/
  1905. void
  1906. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1907. {
  1908. struct lpfc_vport *vport = pmb->vport;
  1909. struct lpfc_dmabuf *mp;
  1910. struct lpfc_nodelist *ndlp;
  1911. struct Scsi_Host *shost;
  1912. uint16_t rpi, vpi;
  1913. int rc;
  1914. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1915. if (mp) {
  1916. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1917. kfree(mp);
  1918. }
  1919. /*
  1920. * If a REG_LOGIN succeeded after node is destroyed or node
  1921. * is in re-discovery driver need to cleanup the RPI.
  1922. */
  1923. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1924. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1925. !pmb->u.mb.mbxStatus) {
  1926. rpi = pmb->u.mb.un.varWords[0];
  1927. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1928. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1929. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1930. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1931. if (rc != MBX_NOT_FINISHED)
  1932. return;
  1933. }
  1934. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1935. !(phba->pport->load_flag & FC_UNLOADING) &&
  1936. !pmb->u.mb.mbxStatus) {
  1937. shost = lpfc_shost_from_vport(vport);
  1938. spin_lock_irq(shost->host_lock);
  1939. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1940. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1941. spin_unlock_irq(shost->host_lock);
  1942. }
  1943. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1944. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1945. lpfc_nlp_put(ndlp);
  1946. pmb->context2 = NULL;
  1947. }
  1948. /* Check security permission status on INIT_LINK mailbox command */
  1949. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1950. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1951. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1952. "2860 SLI authentication is required "
  1953. "for INIT_LINK but has not done yet\n");
  1954. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1955. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1956. else
  1957. mempool_free(pmb, phba->mbox_mem_pool);
  1958. }
  1959. /**
  1960. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1961. * @phba: Pointer to HBA context object.
  1962. *
  1963. * This function is called with no lock held. This function processes all
  1964. * the completed mailbox commands and gives it to upper layers. The interrupt
  1965. * service routine processes mailbox completion interrupt and adds completed
  1966. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1967. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1968. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1969. * function returns the mailbox commands to the upper layer by calling the
  1970. * completion handler function of each mailbox.
  1971. **/
  1972. int
  1973. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1974. {
  1975. MAILBOX_t *pmbox;
  1976. LPFC_MBOXQ_t *pmb;
  1977. int rc;
  1978. LIST_HEAD(cmplq);
  1979. phba->sli.slistat.mbox_event++;
  1980. /* Get all completed mailboxe buffers into the cmplq */
  1981. spin_lock_irq(&phba->hbalock);
  1982. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  1983. spin_unlock_irq(&phba->hbalock);
  1984. /* Get a Mailbox buffer to setup mailbox commands for callback */
  1985. do {
  1986. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  1987. if (pmb == NULL)
  1988. break;
  1989. pmbox = &pmb->u.mb;
  1990. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  1991. if (pmb->vport) {
  1992. lpfc_debugfs_disc_trc(pmb->vport,
  1993. LPFC_DISC_TRC_MBOX_VPORT,
  1994. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  1995. (uint32_t)pmbox->mbxCommand,
  1996. pmbox->un.varWords[0],
  1997. pmbox->un.varWords[1]);
  1998. }
  1999. else {
  2000. lpfc_debugfs_disc_trc(phba->pport,
  2001. LPFC_DISC_TRC_MBOX,
  2002. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2003. (uint32_t)pmbox->mbxCommand,
  2004. pmbox->un.varWords[0],
  2005. pmbox->un.varWords[1]);
  2006. }
  2007. }
  2008. /*
  2009. * It is a fatal error if unknown mbox command completion.
  2010. */
  2011. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2012. MBX_SHUTDOWN) {
  2013. /* Unknown mailbox command compl */
  2014. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2015. "(%d):0323 Unknown Mailbox command "
  2016. "x%x (x%x/x%x) Cmpl\n",
  2017. pmb->vport ? pmb->vport->vpi : 0,
  2018. pmbox->mbxCommand,
  2019. lpfc_sli_config_mbox_subsys_get(phba,
  2020. pmb),
  2021. lpfc_sli_config_mbox_opcode_get(phba,
  2022. pmb));
  2023. phba->link_state = LPFC_HBA_ERROR;
  2024. phba->work_hs = HS_FFER3;
  2025. lpfc_handle_eratt(phba);
  2026. continue;
  2027. }
  2028. if (pmbox->mbxStatus) {
  2029. phba->sli.slistat.mbox_stat_err++;
  2030. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2031. /* Mbox cmd cmpl error - RETRYing */
  2032. lpfc_printf_log(phba, KERN_INFO,
  2033. LOG_MBOX | LOG_SLI,
  2034. "(%d):0305 Mbox cmd cmpl "
  2035. "error - RETRYing Data: x%x "
  2036. "(x%x/x%x) x%x x%x x%x\n",
  2037. pmb->vport ? pmb->vport->vpi : 0,
  2038. pmbox->mbxCommand,
  2039. lpfc_sli_config_mbox_subsys_get(phba,
  2040. pmb),
  2041. lpfc_sli_config_mbox_opcode_get(phba,
  2042. pmb),
  2043. pmbox->mbxStatus,
  2044. pmbox->un.varWords[0],
  2045. pmb->vport->port_state);
  2046. pmbox->mbxStatus = 0;
  2047. pmbox->mbxOwner = OWN_HOST;
  2048. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2049. if (rc != MBX_NOT_FINISHED)
  2050. continue;
  2051. }
  2052. }
  2053. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2054. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2055. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2056. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2057. pmb->vport ? pmb->vport->vpi : 0,
  2058. pmbox->mbxCommand,
  2059. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2060. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2061. pmb->mbox_cmpl,
  2062. *((uint32_t *) pmbox),
  2063. pmbox->un.varWords[0],
  2064. pmbox->un.varWords[1],
  2065. pmbox->un.varWords[2],
  2066. pmbox->un.varWords[3],
  2067. pmbox->un.varWords[4],
  2068. pmbox->un.varWords[5],
  2069. pmbox->un.varWords[6],
  2070. pmbox->un.varWords[7]);
  2071. if (pmb->mbox_cmpl)
  2072. pmb->mbox_cmpl(phba,pmb);
  2073. } while (1);
  2074. return 0;
  2075. }
  2076. /**
  2077. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2078. * @phba: Pointer to HBA context object.
  2079. * @pring: Pointer to driver SLI ring object.
  2080. * @tag: buffer tag.
  2081. *
  2082. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2083. * is set in the tag the buffer is posted for a particular exchange,
  2084. * the function will return the buffer without replacing the buffer.
  2085. * If the buffer is for unsolicited ELS or CT traffic, this function
  2086. * returns the buffer and also posts another buffer to the firmware.
  2087. **/
  2088. static struct lpfc_dmabuf *
  2089. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2090. struct lpfc_sli_ring *pring,
  2091. uint32_t tag)
  2092. {
  2093. struct hbq_dmabuf *hbq_entry;
  2094. if (tag & QUE_BUFTAG_BIT)
  2095. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2096. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2097. if (!hbq_entry)
  2098. return NULL;
  2099. return &hbq_entry->dbuf;
  2100. }
  2101. /**
  2102. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2103. * @phba: Pointer to HBA context object.
  2104. * @pring: Pointer to driver SLI ring object.
  2105. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2106. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2107. * @fch_type: the type for the first frame of the sequence.
  2108. *
  2109. * This function is called with no lock held. This function uses the r_ctl and
  2110. * type of the received sequence to find the correct callback function to call
  2111. * to process the sequence.
  2112. **/
  2113. static int
  2114. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2115. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2116. uint32_t fch_type)
  2117. {
  2118. int i;
  2119. /* unSolicited Responses */
  2120. if (pring->prt[0].profile) {
  2121. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2122. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2123. saveq);
  2124. return 1;
  2125. }
  2126. /* We must search, based on rctl / type
  2127. for the right routine */
  2128. for (i = 0; i < pring->num_mask; i++) {
  2129. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2130. (pring->prt[i].type == fch_type)) {
  2131. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2132. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2133. (phba, pring, saveq);
  2134. return 1;
  2135. }
  2136. }
  2137. return 0;
  2138. }
  2139. /**
  2140. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2141. * @phba: Pointer to HBA context object.
  2142. * @pring: Pointer to driver SLI ring object.
  2143. * @saveq: Pointer to the unsolicited iocb.
  2144. *
  2145. * This function is called with no lock held by the ring event handler
  2146. * when there is an unsolicited iocb posted to the response ring by the
  2147. * firmware. This function gets the buffer associated with the iocbs
  2148. * and calls the event handler for the ring. This function handles both
  2149. * qring buffers and hbq buffers.
  2150. * When the function returns 1 the caller can free the iocb object otherwise
  2151. * upper layer functions will free the iocb objects.
  2152. **/
  2153. static int
  2154. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2155. struct lpfc_iocbq *saveq)
  2156. {
  2157. IOCB_t * irsp;
  2158. WORD5 * w5p;
  2159. uint32_t Rctl, Type;
  2160. uint32_t match;
  2161. struct lpfc_iocbq *iocbq;
  2162. struct lpfc_dmabuf *dmzbuf;
  2163. match = 0;
  2164. irsp = &(saveq->iocb);
  2165. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2166. if (pring->lpfc_sli_rcv_async_status)
  2167. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2168. else
  2169. lpfc_printf_log(phba,
  2170. KERN_WARNING,
  2171. LOG_SLI,
  2172. "0316 Ring %d handler: unexpected "
  2173. "ASYNC_STATUS iocb received evt_code "
  2174. "0x%x\n",
  2175. pring->ringno,
  2176. irsp->un.asyncstat.evt_code);
  2177. return 1;
  2178. }
  2179. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2180. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2181. if (irsp->ulpBdeCount > 0) {
  2182. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2183. irsp->un.ulpWord[3]);
  2184. lpfc_in_buf_free(phba, dmzbuf);
  2185. }
  2186. if (irsp->ulpBdeCount > 1) {
  2187. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2188. irsp->unsli3.sli3Words[3]);
  2189. lpfc_in_buf_free(phba, dmzbuf);
  2190. }
  2191. if (irsp->ulpBdeCount > 2) {
  2192. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2193. irsp->unsli3.sli3Words[7]);
  2194. lpfc_in_buf_free(phba, dmzbuf);
  2195. }
  2196. return 1;
  2197. }
  2198. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2199. if (irsp->ulpBdeCount != 0) {
  2200. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2201. irsp->un.ulpWord[3]);
  2202. if (!saveq->context2)
  2203. lpfc_printf_log(phba,
  2204. KERN_ERR,
  2205. LOG_SLI,
  2206. "0341 Ring %d Cannot find buffer for "
  2207. "an unsolicited iocb. tag 0x%x\n",
  2208. pring->ringno,
  2209. irsp->un.ulpWord[3]);
  2210. }
  2211. if (irsp->ulpBdeCount == 2) {
  2212. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2213. irsp->unsli3.sli3Words[7]);
  2214. if (!saveq->context3)
  2215. lpfc_printf_log(phba,
  2216. KERN_ERR,
  2217. LOG_SLI,
  2218. "0342 Ring %d Cannot find buffer for an"
  2219. " unsolicited iocb. tag 0x%x\n",
  2220. pring->ringno,
  2221. irsp->unsli3.sli3Words[7]);
  2222. }
  2223. list_for_each_entry(iocbq, &saveq->list, list) {
  2224. irsp = &(iocbq->iocb);
  2225. if (irsp->ulpBdeCount != 0) {
  2226. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2227. irsp->un.ulpWord[3]);
  2228. if (!iocbq->context2)
  2229. lpfc_printf_log(phba,
  2230. KERN_ERR,
  2231. LOG_SLI,
  2232. "0343 Ring %d Cannot find "
  2233. "buffer for an unsolicited iocb"
  2234. ". tag 0x%x\n", pring->ringno,
  2235. irsp->un.ulpWord[3]);
  2236. }
  2237. if (irsp->ulpBdeCount == 2) {
  2238. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2239. irsp->unsli3.sli3Words[7]);
  2240. if (!iocbq->context3)
  2241. lpfc_printf_log(phba,
  2242. KERN_ERR,
  2243. LOG_SLI,
  2244. "0344 Ring %d Cannot find "
  2245. "buffer for an unsolicited "
  2246. "iocb. tag 0x%x\n",
  2247. pring->ringno,
  2248. irsp->unsli3.sli3Words[7]);
  2249. }
  2250. }
  2251. }
  2252. if (irsp->ulpBdeCount != 0 &&
  2253. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2254. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2255. int found = 0;
  2256. /* search continue save q for same XRI */
  2257. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2258. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2259. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2260. list_add_tail(&saveq->list, &iocbq->list);
  2261. found = 1;
  2262. break;
  2263. }
  2264. }
  2265. if (!found)
  2266. list_add_tail(&saveq->clist,
  2267. &pring->iocb_continue_saveq);
  2268. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2269. list_del_init(&iocbq->clist);
  2270. saveq = iocbq;
  2271. irsp = &(saveq->iocb);
  2272. } else
  2273. return 0;
  2274. }
  2275. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2276. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2277. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2278. Rctl = FC_RCTL_ELS_REQ;
  2279. Type = FC_TYPE_ELS;
  2280. } else {
  2281. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2282. Rctl = w5p->hcsw.Rctl;
  2283. Type = w5p->hcsw.Type;
  2284. /* Firmware Workaround */
  2285. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2286. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2287. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2288. Rctl = FC_RCTL_ELS_REQ;
  2289. Type = FC_TYPE_ELS;
  2290. w5p->hcsw.Rctl = Rctl;
  2291. w5p->hcsw.Type = Type;
  2292. }
  2293. }
  2294. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2295. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2296. "0313 Ring %d handler: unexpected Rctl x%x "
  2297. "Type x%x received\n",
  2298. pring->ringno, Rctl, Type);
  2299. return 1;
  2300. }
  2301. /**
  2302. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2303. * @phba: Pointer to HBA context object.
  2304. * @pring: Pointer to driver SLI ring object.
  2305. * @prspiocb: Pointer to response iocb object.
  2306. *
  2307. * This function looks up the iocb_lookup table to get the command iocb
  2308. * corresponding to the given response iocb using the iotag of the
  2309. * response iocb. This function is called with the hbalock held.
  2310. * This function returns the command iocb object if it finds the command
  2311. * iocb else returns NULL.
  2312. **/
  2313. static struct lpfc_iocbq *
  2314. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2315. struct lpfc_sli_ring *pring,
  2316. struct lpfc_iocbq *prspiocb)
  2317. {
  2318. struct lpfc_iocbq *cmd_iocb = NULL;
  2319. uint16_t iotag;
  2320. iotag = prspiocb->iocb.ulpIoTag;
  2321. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2322. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2323. list_del_init(&cmd_iocb->list);
  2324. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2325. pring->txcmplq_cnt--;
  2326. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2327. }
  2328. return cmd_iocb;
  2329. }
  2330. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2331. "0317 iotag x%x is out off "
  2332. "range: max iotag x%x wd0 x%x\n",
  2333. iotag, phba->sli.last_iotag,
  2334. *(((uint32_t *) &prspiocb->iocb) + 7));
  2335. return NULL;
  2336. }
  2337. /**
  2338. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2339. * @phba: Pointer to HBA context object.
  2340. * @pring: Pointer to driver SLI ring object.
  2341. * @iotag: IOCB tag.
  2342. *
  2343. * This function looks up the iocb_lookup table to get the command iocb
  2344. * corresponding to the given iotag. This function is called with the
  2345. * hbalock held.
  2346. * This function returns the command iocb object if it finds the command
  2347. * iocb else returns NULL.
  2348. **/
  2349. static struct lpfc_iocbq *
  2350. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2351. struct lpfc_sli_ring *pring, uint16_t iotag)
  2352. {
  2353. struct lpfc_iocbq *cmd_iocb;
  2354. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2355. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2356. list_del_init(&cmd_iocb->list);
  2357. if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
  2358. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  2359. pring->txcmplq_cnt--;
  2360. }
  2361. return cmd_iocb;
  2362. }
  2363. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2364. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2365. iotag, phba->sli.last_iotag);
  2366. return NULL;
  2367. }
  2368. /**
  2369. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2370. * @phba: Pointer to HBA context object.
  2371. * @pring: Pointer to driver SLI ring object.
  2372. * @saveq: Pointer to the response iocb to be processed.
  2373. *
  2374. * This function is called by the ring event handler for non-fcp
  2375. * rings when there is a new response iocb in the response ring.
  2376. * The caller is not required to hold any locks. This function
  2377. * gets the command iocb associated with the response iocb and
  2378. * calls the completion handler for the command iocb. If there
  2379. * is no completion handler, the function will free the resources
  2380. * associated with command iocb. If the response iocb is for
  2381. * an already aborted command iocb, the status of the completion
  2382. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2383. * This function always returns 1.
  2384. **/
  2385. static int
  2386. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2387. struct lpfc_iocbq *saveq)
  2388. {
  2389. struct lpfc_iocbq *cmdiocbp;
  2390. int rc = 1;
  2391. unsigned long iflag;
  2392. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2393. spin_lock_irqsave(&phba->hbalock, iflag);
  2394. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2395. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2396. if (cmdiocbp) {
  2397. if (cmdiocbp->iocb_cmpl) {
  2398. /*
  2399. * If an ELS command failed send an event to mgmt
  2400. * application.
  2401. */
  2402. if (saveq->iocb.ulpStatus &&
  2403. (pring->ringno == LPFC_ELS_RING) &&
  2404. (cmdiocbp->iocb.ulpCommand ==
  2405. CMD_ELS_REQUEST64_CR))
  2406. lpfc_send_els_failure_event(phba,
  2407. cmdiocbp, saveq);
  2408. /*
  2409. * Post all ELS completions to the worker thread.
  2410. * All other are passed to the completion callback.
  2411. */
  2412. if (pring->ringno == LPFC_ELS_RING) {
  2413. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2414. (cmdiocbp->iocb_flag &
  2415. LPFC_DRIVER_ABORTED)) {
  2416. spin_lock_irqsave(&phba->hbalock,
  2417. iflag);
  2418. cmdiocbp->iocb_flag &=
  2419. ~LPFC_DRIVER_ABORTED;
  2420. spin_unlock_irqrestore(&phba->hbalock,
  2421. iflag);
  2422. saveq->iocb.ulpStatus =
  2423. IOSTAT_LOCAL_REJECT;
  2424. saveq->iocb.un.ulpWord[4] =
  2425. IOERR_SLI_ABORTED;
  2426. /* Firmware could still be in progress
  2427. * of DMAing payload, so don't free data
  2428. * buffer till after a hbeat.
  2429. */
  2430. spin_lock_irqsave(&phba->hbalock,
  2431. iflag);
  2432. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2433. spin_unlock_irqrestore(&phba->hbalock,
  2434. iflag);
  2435. }
  2436. if (phba->sli_rev == LPFC_SLI_REV4) {
  2437. if (saveq->iocb_flag &
  2438. LPFC_EXCHANGE_BUSY) {
  2439. /* Set cmdiocb flag for the
  2440. * exchange busy so sgl (xri)
  2441. * will not be released until
  2442. * the abort xri is received
  2443. * from hba.
  2444. */
  2445. spin_lock_irqsave(
  2446. &phba->hbalock, iflag);
  2447. cmdiocbp->iocb_flag |=
  2448. LPFC_EXCHANGE_BUSY;
  2449. spin_unlock_irqrestore(
  2450. &phba->hbalock, iflag);
  2451. }
  2452. if (cmdiocbp->iocb_flag &
  2453. LPFC_DRIVER_ABORTED) {
  2454. /*
  2455. * Clear LPFC_DRIVER_ABORTED
  2456. * bit in case it was driver
  2457. * initiated abort.
  2458. */
  2459. spin_lock_irqsave(
  2460. &phba->hbalock, iflag);
  2461. cmdiocbp->iocb_flag &=
  2462. ~LPFC_DRIVER_ABORTED;
  2463. spin_unlock_irqrestore(
  2464. &phba->hbalock, iflag);
  2465. cmdiocbp->iocb.ulpStatus =
  2466. IOSTAT_LOCAL_REJECT;
  2467. cmdiocbp->iocb.un.ulpWord[4] =
  2468. IOERR_ABORT_REQUESTED;
  2469. /*
  2470. * For SLI4, irsiocb contains
  2471. * NO_XRI in sli_xritag, it
  2472. * shall not affect releasing
  2473. * sgl (xri) process.
  2474. */
  2475. saveq->iocb.ulpStatus =
  2476. IOSTAT_LOCAL_REJECT;
  2477. saveq->iocb.un.ulpWord[4] =
  2478. IOERR_SLI_ABORTED;
  2479. spin_lock_irqsave(
  2480. &phba->hbalock, iflag);
  2481. saveq->iocb_flag |=
  2482. LPFC_DELAY_MEM_FREE;
  2483. spin_unlock_irqrestore(
  2484. &phba->hbalock, iflag);
  2485. }
  2486. }
  2487. }
  2488. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2489. } else
  2490. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2491. } else {
  2492. /*
  2493. * Unknown initiating command based on the response iotag.
  2494. * This could be the case on the ELS ring because of
  2495. * lpfc_els_abort().
  2496. */
  2497. if (pring->ringno != LPFC_ELS_RING) {
  2498. /*
  2499. * Ring <ringno> handler: unexpected completion IoTag
  2500. * <IoTag>
  2501. */
  2502. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2503. "0322 Ring %d handler: "
  2504. "unexpected completion IoTag x%x "
  2505. "Data: x%x x%x x%x x%x\n",
  2506. pring->ringno,
  2507. saveq->iocb.ulpIoTag,
  2508. saveq->iocb.ulpStatus,
  2509. saveq->iocb.un.ulpWord[4],
  2510. saveq->iocb.ulpCommand,
  2511. saveq->iocb.ulpContext);
  2512. }
  2513. }
  2514. return rc;
  2515. }
  2516. /**
  2517. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2518. * @phba: Pointer to HBA context object.
  2519. * @pring: Pointer to driver SLI ring object.
  2520. *
  2521. * This function is called from the iocb ring event handlers when
  2522. * put pointer is ahead of the get pointer for a ring. This function signal
  2523. * an error attention condition to the worker thread and the worker
  2524. * thread will transition the HBA to offline state.
  2525. **/
  2526. static void
  2527. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2528. {
  2529. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2530. /*
  2531. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2532. * rsp ring <portRspMax>
  2533. */
  2534. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2535. "0312 Ring %d handler: portRspPut %d "
  2536. "is bigger than rsp ring %d\n",
  2537. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2538. pring->numRiocb);
  2539. phba->link_state = LPFC_HBA_ERROR;
  2540. /*
  2541. * All error attention handlers are posted to
  2542. * worker thread
  2543. */
  2544. phba->work_ha |= HA_ERATT;
  2545. phba->work_hs = HS_FFER3;
  2546. lpfc_worker_wake_up(phba);
  2547. return;
  2548. }
  2549. /**
  2550. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2551. * @ptr: Pointer to address of HBA context object.
  2552. *
  2553. * This function is invoked by the Error Attention polling timer when the
  2554. * timer times out. It will check the SLI Error Attention register for
  2555. * possible attention events. If so, it will post an Error Attention event
  2556. * and wake up worker thread to process it. Otherwise, it will set up the
  2557. * Error Attention polling timer for the next poll.
  2558. **/
  2559. void lpfc_poll_eratt(unsigned long ptr)
  2560. {
  2561. struct lpfc_hba *phba;
  2562. uint32_t eratt = 0;
  2563. phba = (struct lpfc_hba *)ptr;
  2564. /* Check chip HA register for error event */
  2565. eratt = lpfc_sli_check_eratt(phba);
  2566. if (eratt)
  2567. /* Tell the worker thread there is work to do */
  2568. lpfc_worker_wake_up(phba);
  2569. else
  2570. /* Restart the timer for next eratt poll */
  2571. mod_timer(&phba->eratt_poll, jiffies +
  2572. HZ * LPFC_ERATT_POLL_INTERVAL);
  2573. return;
  2574. }
  2575. /**
  2576. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2577. * @phba: Pointer to HBA context object.
  2578. * @pring: Pointer to driver SLI ring object.
  2579. * @mask: Host attention register mask for this ring.
  2580. *
  2581. * This function is called from the interrupt context when there is a ring
  2582. * event for the fcp ring. The caller does not hold any lock.
  2583. * The function processes each response iocb in the response ring until it
  2584. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2585. * LE bit set. The function will call the completion handler of the command iocb
  2586. * if the response iocb indicates a completion for a command iocb or it is
  2587. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2588. * function if this is an unsolicited iocb.
  2589. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2590. * to check it explicitly.
  2591. */
  2592. int
  2593. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2594. struct lpfc_sli_ring *pring, uint32_t mask)
  2595. {
  2596. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2597. IOCB_t *irsp = NULL;
  2598. IOCB_t *entry = NULL;
  2599. struct lpfc_iocbq *cmdiocbq = NULL;
  2600. struct lpfc_iocbq rspiocbq;
  2601. uint32_t status;
  2602. uint32_t portRspPut, portRspMax;
  2603. int rc = 1;
  2604. lpfc_iocb_type type;
  2605. unsigned long iflag;
  2606. uint32_t rsp_cmpl = 0;
  2607. spin_lock_irqsave(&phba->hbalock, iflag);
  2608. pring->stats.iocb_event++;
  2609. /*
  2610. * The next available response entry should never exceed the maximum
  2611. * entries. If it does, treat it as an adapter hardware error.
  2612. */
  2613. portRspMax = pring->numRiocb;
  2614. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2615. if (unlikely(portRspPut >= portRspMax)) {
  2616. lpfc_sli_rsp_pointers_error(phba, pring);
  2617. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2618. return 1;
  2619. }
  2620. if (phba->fcp_ring_in_use) {
  2621. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2622. return 1;
  2623. } else
  2624. phba->fcp_ring_in_use = 1;
  2625. rmb();
  2626. while (pring->rspidx != portRspPut) {
  2627. /*
  2628. * Fetch an entry off the ring and copy it into a local data
  2629. * structure. The copy involves a byte-swap since the
  2630. * network byte order and pci byte orders are different.
  2631. */
  2632. entry = lpfc_resp_iocb(phba, pring);
  2633. phba->last_completion_time = jiffies;
  2634. if (++pring->rspidx >= portRspMax)
  2635. pring->rspidx = 0;
  2636. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2637. (uint32_t *) &rspiocbq.iocb,
  2638. phba->iocb_rsp_size);
  2639. INIT_LIST_HEAD(&(rspiocbq.list));
  2640. irsp = &rspiocbq.iocb;
  2641. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2642. pring->stats.iocb_rsp++;
  2643. rsp_cmpl++;
  2644. if (unlikely(irsp->ulpStatus)) {
  2645. /*
  2646. * If resource errors reported from HBA, reduce
  2647. * queuedepths of the SCSI device.
  2648. */
  2649. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2650. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2651. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2652. phba->lpfc_rampdown_queue_depth(phba);
  2653. spin_lock_irqsave(&phba->hbalock, iflag);
  2654. }
  2655. /* Rsp ring <ringno> error: IOCB */
  2656. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2657. "0336 Rsp Ring %d error: IOCB Data: "
  2658. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2659. pring->ringno,
  2660. irsp->un.ulpWord[0],
  2661. irsp->un.ulpWord[1],
  2662. irsp->un.ulpWord[2],
  2663. irsp->un.ulpWord[3],
  2664. irsp->un.ulpWord[4],
  2665. irsp->un.ulpWord[5],
  2666. *(uint32_t *)&irsp->un1,
  2667. *((uint32_t *)&irsp->un1 + 1));
  2668. }
  2669. switch (type) {
  2670. case LPFC_ABORT_IOCB:
  2671. case LPFC_SOL_IOCB:
  2672. /*
  2673. * Idle exchange closed via ABTS from port. No iocb
  2674. * resources need to be recovered.
  2675. */
  2676. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2677. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2678. "0333 IOCB cmd 0x%x"
  2679. " processed. Skipping"
  2680. " completion\n",
  2681. irsp->ulpCommand);
  2682. break;
  2683. }
  2684. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2685. &rspiocbq);
  2686. if (unlikely(!cmdiocbq))
  2687. break;
  2688. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2689. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2690. if (cmdiocbq->iocb_cmpl) {
  2691. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2692. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2693. &rspiocbq);
  2694. spin_lock_irqsave(&phba->hbalock, iflag);
  2695. }
  2696. break;
  2697. case LPFC_UNSOL_IOCB:
  2698. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2699. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2700. spin_lock_irqsave(&phba->hbalock, iflag);
  2701. break;
  2702. default:
  2703. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2704. char adaptermsg[LPFC_MAX_ADPTMSG];
  2705. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2706. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2707. MAX_MSG_DATA);
  2708. dev_warn(&((phba->pcidev)->dev),
  2709. "lpfc%d: %s\n",
  2710. phba->brd_no, adaptermsg);
  2711. } else {
  2712. /* Unknown IOCB command */
  2713. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2714. "0334 Unknown IOCB command "
  2715. "Data: x%x, x%x x%x x%x x%x\n",
  2716. type, irsp->ulpCommand,
  2717. irsp->ulpStatus,
  2718. irsp->ulpIoTag,
  2719. irsp->ulpContext);
  2720. }
  2721. break;
  2722. }
  2723. /*
  2724. * The response IOCB has been processed. Update the ring
  2725. * pointer in SLIM. If the port response put pointer has not
  2726. * been updated, sync the pgp->rspPutInx and fetch the new port
  2727. * response put pointer.
  2728. */
  2729. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2730. if (pring->rspidx == portRspPut)
  2731. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2732. }
  2733. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2734. pring->stats.iocb_rsp_full++;
  2735. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2736. writel(status, phba->CAregaddr);
  2737. readl(phba->CAregaddr);
  2738. }
  2739. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2740. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2741. pring->stats.iocb_cmd_empty++;
  2742. /* Force update of the local copy of cmdGetInx */
  2743. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2744. lpfc_sli_resume_iocb(phba, pring);
  2745. if ((pring->lpfc_sli_cmd_available))
  2746. (pring->lpfc_sli_cmd_available) (phba, pring);
  2747. }
  2748. phba->fcp_ring_in_use = 0;
  2749. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2750. return rc;
  2751. }
  2752. /**
  2753. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2754. * @phba: Pointer to HBA context object.
  2755. * @pring: Pointer to driver SLI ring object.
  2756. * @rspiocbp: Pointer to driver response IOCB object.
  2757. *
  2758. * This function is called from the worker thread when there is a slow-path
  2759. * response IOCB to process. This function chains all the response iocbs until
  2760. * seeing the iocb with the LE bit set. The function will call
  2761. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2762. * completion of a command iocb. The function will call the
  2763. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2764. * The function frees the resources or calls the completion handler if this
  2765. * iocb is an abort completion. The function returns NULL when the response
  2766. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2767. * this function shall chain the iocb on to the iocb_continueq and return the
  2768. * response iocb passed in.
  2769. **/
  2770. static struct lpfc_iocbq *
  2771. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2772. struct lpfc_iocbq *rspiocbp)
  2773. {
  2774. struct lpfc_iocbq *saveq;
  2775. struct lpfc_iocbq *cmdiocbp;
  2776. struct lpfc_iocbq *next_iocb;
  2777. IOCB_t *irsp = NULL;
  2778. uint32_t free_saveq;
  2779. uint8_t iocb_cmd_type;
  2780. lpfc_iocb_type type;
  2781. unsigned long iflag;
  2782. int rc;
  2783. spin_lock_irqsave(&phba->hbalock, iflag);
  2784. /* First add the response iocb to the countinueq list */
  2785. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2786. pring->iocb_continueq_cnt++;
  2787. /* Now, determine whether the list is completed for processing */
  2788. irsp = &rspiocbp->iocb;
  2789. if (irsp->ulpLe) {
  2790. /*
  2791. * By default, the driver expects to free all resources
  2792. * associated with this iocb completion.
  2793. */
  2794. free_saveq = 1;
  2795. saveq = list_get_first(&pring->iocb_continueq,
  2796. struct lpfc_iocbq, list);
  2797. irsp = &(saveq->iocb);
  2798. list_del_init(&pring->iocb_continueq);
  2799. pring->iocb_continueq_cnt = 0;
  2800. pring->stats.iocb_rsp++;
  2801. /*
  2802. * If resource errors reported from HBA, reduce
  2803. * queuedepths of the SCSI device.
  2804. */
  2805. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2806. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2807. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2808. phba->lpfc_rampdown_queue_depth(phba);
  2809. spin_lock_irqsave(&phba->hbalock, iflag);
  2810. }
  2811. if (irsp->ulpStatus) {
  2812. /* Rsp ring <ringno> error: IOCB */
  2813. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2814. "0328 Rsp Ring %d error: "
  2815. "IOCB Data: "
  2816. "x%x x%x x%x x%x "
  2817. "x%x x%x x%x x%x "
  2818. "x%x x%x x%x x%x "
  2819. "x%x x%x x%x x%x\n",
  2820. pring->ringno,
  2821. irsp->un.ulpWord[0],
  2822. irsp->un.ulpWord[1],
  2823. irsp->un.ulpWord[2],
  2824. irsp->un.ulpWord[3],
  2825. irsp->un.ulpWord[4],
  2826. irsp->un.ulpWord[5],
  2827. *(((uint32_t *) irsp) + 6),
  2828. *(((uint32_t *) irsp) + 7),
  2829. *(((uint32_t *) irsp) + 8),
  2830. *(((uint32_t *) irsp) + 9),
  2831. *(((uint32_t *) irsp) + 10),
  2832. *(((uint32_t *) irsp) + 11),
  2833. *(((uint32_t *) irsp) + 12),
  2834. *(((uint32_t *) irsp) + 13),
  2835. *(((uint32_t *) irsp) + 14),
  2836. *(((uint32_t *) irsp) + 15));
  2837. }
  2838. /*
  2839. * Fetch the IOCB command type and call the correct completion
  2840. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2841. * get freed back to the lpfc_iocb_list by the discovery
  2842. * kernel thread.
  2843. */
  2844. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2845. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2846. switch (type) {
  2847. case LPFC_SOL_IOCB:
  2848. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2849. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2850. spin_lock_irqsave(&phba->hbalock, iflag);
  2851. break;
  2852. case LPFC_UNSOL_IOCB:
  2853. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2854. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2855. spin_lock_irqsave(&phba->hbalock, iflag);
  2856. if (!rc)
  2857. free_saveq = 0;
  2858. break;
  2859. case LPFC_ABORT_IOCB:
  2860. cmdiocbp = NULL;
  2861. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2862. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2863. saveq);
  2864. if (cmdiocbp) {
  2865. /* Call the specified completion routine */
  2866. if (cmdiocbp->iocb_cmpl) {
  2867. spin_unlock_irqrestore(&phba->hbalock,
  2868. iflag);
  2869. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2870. saveq);
  2871. spin_lock_irqsave(&phba->hbalock,
  2872. iflag);
  2873. } else
  2874. __lpfc_sli_release_iocbq(phba,
  2875. cmdiocbp);
  2876. }
  2877. break;
  2878. case LPFC_UNKNOWN_IOCB:
  2879. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2880. char adaptermsg[LPFC_MAX_ADPTMSG];
  2881. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2882. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2883. MAX_MSG_DATA);
  2884. dev_warn(&((phba->pcidev)->dev),
  2885. "lpfc%d: %s\n",
  2886. phba->brd_no, adaptermsg);
  2887. } else {
  2888. /* Unknown IOCB command */
  2889. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2890. "0335 Unknown IOCB "
  2891. "command Data: x%x "
  2892. "x%x x%x x%x\n",
  2893. irsp->ulpCommand,
  2894. irsp->ulpStatus,
  2895. irsp->ulpIoTag,
  2896. irsp->ulpContext);
  2897. }
  2898. break;
  2899. }
  2900. if (free_saveq) {
  2901. list_for_each_entry_safe(rspiocbp, next_iocb,
  2902. &saveq->list, list) {
  2903. list_del(&rspiocbp->list);
  2904. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2905. }
  2906. __lpfc_sli_release_iocbq(phba, saveq);
  2907. }
  2908. rspiocbp = NULL;
  2909. }
  2910. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2911. return rspiocbp;
  2912. }
  2913. /**
  2914. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2915. * @phba: Pointer to HBA context object.
  2916. * @pring: Pointer to driver SLI ring object.
  2917. * @mask: Host attention register mask for this ring.
  2918. *
  2919. * This routine wraps the actual slow_ring event process routine from the
  2920. * API jump table function pointer from the lpfc_hba struct.
  2921. **/
  2922. void
  2923. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2924. struct lpfc_sli_ring *pring, uint32_t mask)
  2925. {
  2926. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2927. }
  2928. /**
  2929. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2930. * @phba: Pointer to HBA context object.
  2931. * @pring: Pointer to driver SLI ring object.
  2932. * @mask: Host attention register mask for this ring.
  2933. *
  2934. * This function is called from the worker thread when there is a ring event
  2935. * for non-fcp rings. The caller does not hold any lock. The function will
  2936. * remove each response iocb in the response ring and calls the handle
  2937. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2938. **/
  2939. static void
  2940. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2941. struct lpfc_sli_ring *pring, uint32_t mask)
  2942. {
  2943. struct lpfc_pgp *pgp;
  2944. IOCB_t *entry;
  2945. IOCB_t *irsp = NULL;
  2946. struct lpfc_iocbq *rspiocbp = NULL;
  2947. uint32_t portRspPut, portRspMax;
  2948. unsigned long iflag;
  2949. uint32_t status;
  2950. pgp = &phba->port_gp[pring->ringno];
  2951. spin_lock_irqsave(&phba->hbalock, iflag);
  2952. pring->stats.iocb_event++;
  2953. /*
  2954. * The next available response entry should never exceed the maximum
  2955. * entries. If it does, treat it as an adapter hardware error.
  2956. */
  2957. portRspMax = pring->numRiocb;
  2958. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2959. if (portRspPut >= portRspMax) {
  2960. /*
  2961. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2962. * rsp ring <portRspMax>
  2963. */
  2964. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2965. "0303 Ring %d handler: portRspPut %d "
  2966. "is bigger than rsp ring %d\n",
  2967. pring->ringno, portRspPut, portRspMax);
  2968. phba->link_state = LPFC_HBA_ERROR;
  2969. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2970. phba->work_hs = HS_FFER3;
  2971. lpfc_handle_eratt(phba);
  2972. return;
  2973. }
  2974. rmb();
  2975. while (pring->rspidx != portRspPut) {
  2976. /*
  2977. * Build a completion list and call the appropriate handler.
  2978. * The process is to get the next available response iocb, get
  2979. * a free iocb from the list, copy the response data into the
  2980. * free iocb, insert to the continuation list, and update the
  2981. * next response index to slim. This process makes response
  2982. * iocb's in the ring available to DMA as fast as possible but
  2983. * pays a penalty for a copy operation. Since the iocb is
  2984. * only 32 bytes, this penalty is considered small relative to
  2985. * the PCI reads for register values and a slim write. When
  2986. * the ulpLe field is set, the entire Command has been
  2987. * received.
  2988. */
  2989. entry = lpfc_resp_iocb(phba, pring);
  2990. phba->last_completion_time = jiffies;
  2991. rspiocbp = __lpfc_sli_get_iocbq(phba);
  2992. if (rspiocbp == NULL) {
  2993. printk(KERN_ERR "%s: out of buffers! Failing "
  2994. "completion.\n", __func__);
  2995. break;
  2996. }
  2997. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  2998. phba->iocb_rsp_size);
  2999. irsp = &rspiocbp->iocb;
  3000. if (++pring->rspidx >= portRspMax)
  3001. pring->rspidx = 0;
  3002. if (pring->ringno == LPFC_ELS_RING) {
  3003. lpfc_debugfs_slow_ring_trc(phba,
  3004. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3005. *(((uint32_t *) irsp) + 4),
  3006. *(((uint32_t *) irsp) + 6),
  3007. *(((uint32_t *) irsp) + 7));
  3008. }
  3009. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3010. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3011. /* Handle the response IOCB */
  3012. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3013. spin_lock_irqsave(&phba->hbalock, iflag);
  3014. /*
  3015. * If the port response put pointer has not been updated, sync
  3016. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3017. * response put pointer.
  3018. */
  3019. if (pring->rspidx == portRspPut) {
  3020. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3021. }
  3022. } /* while (pring->rspidx != portRspPut) */
  3023. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3024. /* At least one response entry has been freed */
  3025. pring->stats.iocb_rsp_full++;
  3026. /* SET RxRE_RSP in Chip Att register */
  3027. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3028. writel(status, phba->CAregaddr);
  3029. readl(phba->CAregaddr); /* flush */
  3030. }
  3031. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3032. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3033. pring->stats.iocb_cmd_empty++;
  3034. /* Force update of the local copy of cmdGetInx */
  3035. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3036. lpfc_sli_resume_iocb(phba, pring);
  3037. if ((pring->lpfc_sli_cmd_available))
  3038. (pring->lpfc_sli_cmd_available) (phba, pring);
  3039. }
  3040. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3041. return;
  3042. }
  3043. /**
  3044. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3045. * @phba: Pointer to HBA context object.
  3046. * @pring: Pointer to driver SLI ring object.
  3047. * @mask: Host attention register mask for this ring.
  3048. *
  3049. * This function is called from the worker thread when there is a pending
  3050. * ELS response iocb on the driver internal slow-path response iocb worker
  3051. * queue. The caller does not hold any lock. The function will remove each
  3052. * response iocb from the response worker queue and calls the handle
  3053. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3054. **/
  3055. static void
  3056. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3057. struct lpfc_sli_ring *pring, uint32_t mask)
  3058. {
  3059. struct lpfc_iocbq *irspiocbq;
  3060. struct hbq_dmabuf *dmabuf;
  3061. struct lpfc_cq_event *cq_event;
  3062. unsigned long iflag;
  3063. spin_lock_irqsave(&phba->hbalock, iflag);
  3064. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3065. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3066. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3067. /* Get the response iocb from the head of work queue */
  3068. spin_lock_irqsave(&phba->hbalock, iflag);
  3069. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3070. cq_event, struct lpfc_cq_event, list);
  3071. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3072. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3073. case CQE_CODE_COMPL_WQE:
  3074. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3075. cq_event);
  3076. /* Translate ELS WCQE to response IOCBQ */
  3077. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3078. irspiocbq);
  3079. if (irspiocbq)
  3080. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3081. irspiocbq);
  3082. break;
  3083. case CQE_CODE_RECEIVE:
  3084. case CQE_CODE_RECEIVE_V1:
  3085. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3086. cq_event);
  3087. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3088. break;
  3089. default:
  3090. break;
  3091. }
  3092. }
  3093. }
  3094. /**
  3095. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3096. * @phba: Pointer to HBA context object.
  3097. * @pring: Pointer to driver SLI ring object.
  3098. *
  3099. * This function aborts all iocbs in the given ring and frees all the iocb
  3100. * objects in txq. This function issues an abort iocb for all the iocb commands
  3101. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3102. * the return of this function. The caller is not required to hold any locks.
  3103. **/
  3104. void
  3105. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3106. {
  3107. LIST_HEAD(completions);
  3108. struct lpfc_iocbq *iocb, *next_iocb;
  3109. if (pring->ringno == LPFC_ELS_RING) {
  3110. lpfc_fabric_abort_hba(phba);
  3111. }
  3112. /* Error everything on txq and txcmplq
  3113. * First do the txq.
  3114. */
  3115. spin_lock_irq(&phba->hbalock);
  3116. list_splice_init(&pring->txq, &completions);
  3117. pring->txq_cnt = 0;
  3118. /* Next issue ABTS for everything on the txcmplq */
  3119. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3120. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3121. spin_unlock_irq(&phba->hbalock);
  3122. /* Cancel all the IOCBs from the completions list */
  3123. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3124. IOERR_SLI_ABORTED);
  3125. }
  3126. /**
  3127. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3128. * @phba: Pointer to HBA context object.
  3129. *
  3130. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3131. * objects in txq and txcmplq. This function will not issue abort iocbs
  3132. * for all the iocb commands in txcmplq, they will just be returned with
  3133. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3134. * slot has been permanently disabled.
  3135. **/
  3136. void
  3137. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3138. {
  3139. LIST_HEAD(txq);
  3140. LIST_HEAD(txcmplq);
  3141. struct lpfc_sli *psli = &phba->sli;
  3142. struct lpfc_sli_ring *pring;
  3143. /* Currently, only one fcp ring */
  3144. pring = &psli->ring[psli->fcp_ring];
  3145. spin_lock_irq(&phba->hbalock);
  3146. /* Retrieve everything on txq */
  3147. list_splice_init(&pring->txq, &txq);
  3148. pring->txq_cnt = 0;
  3149. /* Retrieve everything on the txcmplq */
  3150. list_splice_init(&pring->txcmplq, &txcmplq);
  3151. pring->txcmplq_cnt = 0;
  3152. spin_unlock_irq(&phba->hbalock);
  3153. /* Flush the txq */
  3154. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3155. IOERR_SLI_DOWN);
  3156. /* Flush the txcmpq */
  3157. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3158. IOERR_SLI_DOWN);
  3159. }
  3160. /**
  3161. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3162. * @phba: Pointer to HBA context object.
  3163. * @mask: Bit mask to be checked.
  3164. *
  3165. * This function reads the host status register and compares
  3166. * with the provided bit mask to check if HBA completed
  3167. * the restart. This function will wait in a loop for the
  3168. * HBA to complete restart. If the HBA does not restart within
  3169. * 15 iterations, the function will reset the HBA again. The
  3170. * function returns 1 when HBA fail to restart otherwise returns
  3171. * zero.
  3172. **/
  3173. static int
  3174. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3175. {
  3176. uint32_t status;
  3177. int i = 0;
  3178. int retval = 0;
  3179. /* Read the HBA Host Status Register */
  3180. if (lpfc_readl(phba->HSregaddr, &status))
  3181. return 1;
  3182. /*
  3183. * Check status register every 100ms for 5 retries, then every
  3184. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3185. * every 2.5 sec for 4.
  3186. * Break our of the loop if errors occurred during init.
  3187. */
  3188. while (((status & mask) != mask) &&
  3189. !(status & HS_FFERM) &&
  3190. i++ < 20) {
  3191. if (i <= 5)
  3192. msleep(10);
  3193. else if (i <= 10)
  3194. msleep(500);
  3195. else
  3196. msleep(2500);
  3197. if (i == 15) {
  3198. /* Do post */
  3199. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3200. lpfc_sli_brdrestart(phba);
  3201. }
  3202. /* Read the HBA Host Status Register */
  3203. if (lpfc_readl(phba->HSregaddr, &status)) {
  3204. retval = 1;
  3205. break;
  3206. }
  3207. }
  3208. /* Check to see if any errors occurred during init */
  3209. if ((status & HS_FFERM) || (i >= 20)) {
  3210. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3211. "2751 Adapter failed to restart, "
  3212. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3213. status,
  3214. readl(phba->MBslimaddr + 0xa8),
  3215. readl(phba->MBslimaddr + 0xac));
  3216. phba->link_state = LPFC_HBA_ERROR;
  3217. retval = 1;
  3218. }
  3219. return retval;
  3220. }
  3221. /**
  3222. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3223. * @phba: Pointer to HBA context object.
  3224. * @mask: Bit mask to be checked.
  3225. *
  3226. * This function checks the host status register to check if HBA is
  3227. * ready. This function will wait in a loop for the HBA to be ready
  3228. * If the HBA is not ready , the function will will reset the HBA PCI
  3229. * function again. The function returns 1 when HBA fail to be ready
  3230. * otherwise returns zero.
  3231. **/
  3232. static int
  3233. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3234. {
  3235. uint32_t status;
  3236. int retval = 0;
  3237. /* Read the HBA Host Status Register */
  3238. status = lpfc_sli4_post_status_check(phba);
  3239. if (status) {
  3240. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3241. lpfc_sli_brdrestart(phba);
  3242. status = lpfc_sli4_post_status_check(phba);
  3243. }
  3244. /* Check to see if any errors occurred during init */
  3245. if (status) {
  3246. phba->link_state = LPFC_HBA_ERROR;
  3247. retval = 1;
  3248. } else
  3249. phba->sli4_hba.intr_enable = 0;
  3250. return retval;
  3251. }
  3252. /**
  3253. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3254. * @phba: Pointer to HBA context object.
  3255. * @mask: Bit mask to be checked.
  3256. *
  3257. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3258. * from the API jump table function pointer from the lpfc_hba struct.
  3259. **/
  3260. int
  3261. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3262. {
  3263. return phba->lpfc_sli_brdready(phba, mask);
  3264. }
  3265. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3266. /**
  3267. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3268. * @phba: Pointer to HBA context object.
  3269. *
  3270. * This function is called before resetting an HBA. This
  3271. * function requests HBA to quiesce DMAs before a reset.
  3272. **/
  3273. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3274. {
  3275. uint32_t __iomem *resp_buf;
  3276. uint32_t __iomem *mbox_buf;
  3277. volatile uint32_t mbox;
  3278. uint32_t hc_copy, ha_copy, resp_data;
  3279. int i;
  3280. uint8_t hdrtype;
  3281. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3282. if (hdrtype != 0x80 ||
  3283. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3284. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3285. return;
  3286. /*
  3287. * Tell the other part of the chip to suspend temporarily all
  3288. * its DMA activity.
  3289. */
  3290. resp_buf = phba->MBslimaddr;
  3291. /* Disable the error attention */
  3292. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3293. return;
  3294. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3295. readl(phba->HCregaddr); /* flush */
  3296. phba->link_flag |= LS_IGNORE_ERATT;
  3297. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3298. return;
  3299. if (ha_copy & HA_ERATT) {
  3300. /* Clear Chip error bit */
  3301. writel(HA_ERATT, phba->HAregaddr);
  3302. phba->pport->stopped = 1;
  3303. }
  3304. mbox = 0;
  3305. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3306. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3307. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3308. mbox_buf = phba->MBslimaddr;
  3309. writel(mbox, mbox_buf);
  3310. for (i = 0; i < 50; i++) {
  3311. if (lpfc_readl((resp_buf + 1), &resp_data))
  3312. return;
  3313. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3314. mdelay(1);
  3315. else
  3316. break;
  3317. }
  3318. resp_data = 0;
  3319. if (lpfc_readl((resp_buf + 1), &resp_data))
  3320. return;
  3321. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3322. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3323. phba->pport->stopped)
  3324. goto restore_hc;
  3325. else
  3326. goto clear_errat;
  3327. }
  3328. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3329. resp_data = 0;
  3330. for (i = 0; i < 500; i++) {
  3331. if (lpfc_readl(resp_buf, &resp_data))
  3332. return;
  3333. if (resp_data != mbox)
  3334. mdelay(1);
  3335. else
  3336. break;
  3337. }
  3338. clear_errat:
  3339. while (++i < 500) {
  3340. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3341. return;
  3342. if (!(ha_copy & HA_ERATT))
  3343. mdelay(1);
  3344. else
  3345. break;
  3346. }
  3347. if (readl(phba->HAregaddr) & HA_ERATT) {
  3348. writel(HA_ERATT, phba->HAregaddr);
  3349. phba->pport->stopped = 1;
  3350. }
  3351. restore_hc:
  3352. phba->link_flag &= ~LS_IGNORE_ERATT;
  3353. writel(hc_copy, phba->HCregaddr);
  3354. readl(phba->HCregaddr); /* flush */
  3355. }
  3356. /**
  3357. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3358. * @phba: Pointer to HBA context object.
  3359. *
  3360. * This function issues a kill_board mailbox command and waits for
  3361. * the error attention interrupt. This function is called for stopping
  3362. * the firmware processing. The caller is not required to hold any
  3363. * locks. This function calls lpfc_hba_down_post function to free
  3364. * any pending commands after the kill. The function will return 1 when it
  3365. * fails to kill the board else will return 0.
  3366. **/
  3367. int
  3368. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3369. {
  3370. struct lpfc_sli *psli;
  3371. LPFC_MBOXQ_t *pmb;
  3372. uint32_t status;
  3373. uint32_t ha_copy;
  3374. int retval;
  3375. int i = 0;
  3376. psli = &phba->sli;
  3377. /* Kill HBA */
  3378. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3379. "0329 Kill HBA Data: x%x x%x\n",
  3380. phba->pport->port_state, psli->sli_flag);
  3381. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3382. if (!pmb)
  3383. return 1;
  3384. /* Disable the error attention */
  3385. spin_lock_irq(&phba->hbalock);
  3386. if (lpfc_readl(phba->HCregaddr, &status)) {
  3387. spin_unlock_irq(&phba->hbalock);
  3388. mempool_free(pmb, phba->mbox_mem_pool);
  3389. return 1;
  3390. }
  3391. status &= ~HC_ERINT_ENA;
  3392. writel(status, phba->HCregaddr);
  3393. readl(phba->HCregaddr); /* flush */
  3394. phba->link_flag |= LS_IGNORE_ERATT;
  3395. spin_unlock_irq(&phba->hbalock);
  3396. lpfc_kill_board(phba, pmb);
  3397. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3398. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3399. if (retval != MBX_SUCCESS) {
  3400. if (retval != MBX_BUSY)
  3401. mempool_free(pmb, phba->mbox_mem_pool);
  3402. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3403. "2752 KILL_BOARD command failed retval %d\n",
  3404. retval);
  3405. spin_lock_irq(&phba->hbalock);
  3406. phba->link_flag &= ~LS_IGNORE_ERATT;
  3407. spin_unlock_irq(&phba->hbalock);
  3408. return 1;
  3409. }
  3410. spin_lock_irq(&phba->hbalock);
  3411. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3412. spin_unlock_irq(&phba->hbalock);
  3413. mempool_free(pmb, phba->mbox_mem_pool);
  3414. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3415. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3416. * 3 seconds we still set HBA_ERROR state because the status of the
  3417. * board is now undefined.
  3418. */
  3419. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3420. return 1;
  3421. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3422. mdelay(100);
  3423. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3424. return 1;
  3425. }
  3426. del_timer_sync(&psli->mbox_tmo);
  3427. if (ha_copy & HA_ERATT) {
  3428. writel(HA_ERATT, phba->HAregaddr);
  3429. phba->pport->stopped = 1;
  3430. }
  3431. spin_lock_irq(&phba->hbalock);
  3432. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3433. psli->mbox_active = NULL;
  3434. phba->link_flag &= ~LS_IGNORE_ERATT;
  3435. spin_unlock_irq(&phba->hbalock);
  3436. lpfc_hba_down_post(phba);
  3437. phba->link_state = LPFC_HBA_ERROR;
  3438. return ha_copy & HA_ERATT ? 0 : 1;
  3439. }
  3440. /**
  3441. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3442. * @phba: Pointer to HBA context object.
  3443. *
  3444. * This function resets the HBA by writing HC_INITFF to the control
  3445. * register. After the HBA resets, this function resets all the iocb ring
  3446. * indices. This function disables PCI layer parity checking during
  3447. * the reset.
  3448. * This function returns 0 always.
  3449. * The caller is not required to hold any locks.
  3450. **/
  3451. int
  3452. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3453. {
  3454. struct lpfc_sli *psli;
  3455. struct lpfc_sli_ring *pring;
  3456. uint16_t cfg_value;
  3457. int i;
  3458. psli = &phba->sli;
  3459. /* Reset HBA */
  3460. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3461. "0325 Reset HBA Data: x%x x%x\n",
  3462. phba->pport->port_state, psli->sli_flag);
  3463. /* perform board reset */
  3464. phba->fc_eventTag = 0;
  3465. phba->link_events = 0;
  3466. phba->pport->fc_myDID = 0;
  3467. phba->pport->fc_prevDID = 0;
  3468. /* Turn off parity checking and serr during the physical reset */
  3469. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3470. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3471. (cfg_value &
  3472. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3473. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3474. /* Now toggle INITFF bit in the Host Control Register */
  3475. writel(HC_INITFF, phba->HCregaddr);
  3476. mdelay(1);
  3477. readl(phba->HCregaddr); /* flush */
  3478. writel(0, phba->HCregaddr);
  3479. readl(phba->HCregaddr); /* flush */
  3480. /* Restore PCI cmd register */
  3481. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3482. /* Initialize relevant SLI info */
  3483. for (i = 0; i < psli->num_rings; i++) {
  3484. pring = &psli->ring[i];
  3485. pring->flag = 0;
  3486. pring->rspidx = 0;
  3487. pring->next_cmdidx = 0;
  3488. pring->local_getidx = 0;
  3489. pring->cmdidx = 0;
  3490. pring->missbufcnt = 0;
  3491. }
  3492. phba->link_state = LPFC_WARM_START;
  3493. return 0;
  3494. }
  3495. /**
  3496. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3497. * @phba: Pointer to HBA context object.
  3498. *
  3499. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3500. * checking during resets the device. The caller is not required to hold
  3501. * any locks.
  3502. *
  3503. * This function returns 0 always.
  3504. **/
  3505. int
  3506. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3507. {
  3508. struct lpfc_sli *psli = &phba->sli;
  3509. uint16_t cfg_value;
  3510. uint8_t qindx;
  3511. /* Reset HBA */
  3512. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3513. "0295 Reset HBA Data: x%x x%x\n",
  3514. phba->pport->port_state, psli->sli_flag);
  3515. /* perform board reset */
  3516. phba->fc_eventTag = 0;
  3517. phba->link_events = 0;
  3518. phba->pport->fc_myDID = 0;
  3519. phba->pport->fc_prevDID = 0;
  3520. spin_lock_irq(&phba->hbalock);
  3521. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3522. phba->fcf.fcf_flag = 0;
  3523. /* Clean up the child queue list for the CQs */
  3524. list_del_init(&phba->sli4_hba.mbx_wq->list);
  3525. list_del_init(&phba->sli4_hba.els_wq->list);
  3526. list_del_init(&phba->sli4_hba.hdr_rq->list);
  3527. list_del_init(&phba->sli4_hba.dat_rq->list);
  3528. list_del_init(&phba->sli4_hba.mbx_cq->list);
  3529. list_del_init(&phba->sli4_hba.els_cq->list);
  3530. for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
  3531. list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
  3532. qindx = 0;
  3533. do
  3534. list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
  3535. while (++qindx < phba->cfg_fcp_eq_count);
  3536. spin_unlock_irq(&phba->hbalock);
  3537. /* Now physically reset the device */
  3538. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3539. "0389 Performing PCI function reset!\n");
  3540. /* Turn off parity checking and serr during the physical reset */
  3541. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3542. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3543. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3544. /* Perform FCoE PCI function reset */
  3545. lpfc_pci_function_reset(phba);
  3546. /* Restore PCI cmd register */
  3547. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3548. return 0;
  3549. }
  3550. /**
  3551. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3552. * @phba: Pointer to HBA context object.
  3553. *
  3554. * This function is called in the SLI initialization code path to
  3555. * restart the HBA. The caller is not required to hold any lock.
  3556. * This function writes MBX_RESTART mailbox command to the SLIM and
  3557. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3558. * function to free any pending commands. The function enables
  3559. * POST only during the first initialization. The function returns zero.
  3560. * The function does not guarantee completion of MBX_RESTART mailbox
  3561. * command before the return of this function.
  3562. **/
  3563. static int
  3564. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3565. {
  3566. MAILBOX_t *mb;
  3567. struct lpfc_sli *psli;
  3568. volatile uint32_t word0;
  3569. void __iomem *to_slim;
  3570. uint32_t hba_aer_enabled;
  3571. spin_lock_irq(&phba->hbalock);
  3572. /* Take PCIe device Advanced Error Reporting (AER) state */
  3573. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3574. psli = &phba->sli;
  3575. /* Restart HBA */
  3576. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3577. "0337 Restart HBA Data: x%x x%x\n",
  3578. phba->pport->port_state, psli->sli_flag);
  3579. word0 = 0;
  3580. mb = (MAILBOX_t *) &word0;
  3581. mb->mbxCommand = MBX_RESTART;
  3582. mb->mbxHc = 1;
  3583. lpfc_reset_barrier(phba);
  3584. to_slim = phba->MBslimaddr;
  3585. writel(*(uint32_t *) mb, to_slim);
  3586. readl(to_slim); /* flush */
  3587. /* Only skip post after fc_ffinit is completed */
  3588. if (phba->pport->port_state)
  3589. word0 = 1; /* This is really setting up word1 */
  3590. else
  3591. word0 = 0; /* This is really setting up word1 */
  3592. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3593. writel(*(uint32_t *) mb, to_slim);
  3594. readl(to_slim); /* flush */
  3595. lpfc_sli_brdreset(phba);
  3596. phba->pport->stopped = 0;
  3597. phba->link_state = LPFC_INIT_START;
  3598. phba->hba_flag = 0;
  3599. spin_unlock_irq(&phba->hbalock);
  3600. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3601. psli->stats_start = get_seconds();
  3602. /* Give the INITFF and Post time to settle. */
  3603. mdelay(100);
  3604. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3605. if (hba_aer_enabled)
  3606. pci_disable_pcie_error_reporting(phba->pcidev);
  3607. lpfc_hba_down_post(phba);
  3608. return 0;
  3609. }
  3610. /**
  3611. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3612. * @phba: Pointer to HBA context object.
  3613. *
  3614. * This function is called in the SLI initialization code path to restart
  3615. * a SLI4 HBA. The caller is not required to hold any lock.
  3616. * At the end of the function, it calls lpfc_hba_down_post function to
  3617. * free any pending commands.
  3618. **/
  3619. static int
  3620. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3621. {
  3622. struct lpfc_sli *psli = &phba->sli;
  3623. uint32_t hba_aer_enabled;
  3624. /* Restart HBA */
  3625. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3626. "0296 Restart HBA Data: x%x x%x\n",
  3627. phba->pport->port_state, psli->sli_flag);
  3628. /* Take PCIe device Advanced Error Reporting (AER) state */
  3629. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3630. lpfc_sli4_brdreset(phba);
  3631. spin_lock_irq(&phba->hbalock);
  3632. phba->pport->stopped = 0;
  3633. phba->link_state = LPFC_INIT_START;
  3634. phba->hba_flag = 0;
  3635. spin_unlock_irq(&phba->hbalock);
  3636. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3637. psli->stats_start = get_seconds();
  3638. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3639. if (hba_aer_enabled)
  3640. pci_disable_pcie_error_reporting(phba->pcidev);
  3641. lpfc_hba_down_post(phba);
  3642. return 0;
  3643. }
  3644. /**
  3645. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3646. * @phba: Pointer to HBA context object.
  3647. *
  3648. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3649. * API jump table function pointer from the lpfc_hba struct.
  3650. **/
  3651. int
  3652. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3653. {
  3654. return phba->lpfc_sli_brdrestart(phba);
  3655. }
  3656. /**
  3657. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3658. * @phba: Pointer to HBA context object.
  3659. *
  3660. * This function is called after a HBA restart to wait for successful
  3661. * restart of the HBA. Successful restart of the HBA is indicated by
  3662. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3663. * iteration, the function will restart the HBA again. The function returns
  3664. * zero if HBA successfully restarted else returns negative error code.
  3665. **/
  3666. static int
  3667. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3668. {
  3669. uint32_t status, i = 0;
  3670. /* Read the HBA Host Status Register */
  3671. if (lpfc_readl(phba->HSregaddr, &status))
  3672. return -EIO;
  3673. /* Check status register to see what current state is */
  3674. i = 0;
  3675. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3676. /* Check every 10ms for 10 retries, then every 100ms for 90
  3677. * retries, then every 1 sec for 50 retires for a total of
  3678. * ~60 seconds before reset the board again and check every
  3679. * 1 sec for 50 retries. The up to 60 seconds before the
  3680. * board ready is required by the Falcon FIPS zeroization
  3681. * complete, and any reset the board in between shall cause
  3682. * restart of zeroization, further delay the board ready.
  3683. */
  3684. if (i++ >= 200) {
  3685. /* Adapter failed to init, timeout, status reg
  3686. <status> */
  3687. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3688. "0436 Adapter failed to init, "
  3689. "timeout, status reg x%x, "
  3690. "FW Data: A8 x%x AC x%x\n", status,
  3691. readl(phba->MBslimaddr + 0xa8),
  3692. readl(phba->MBslimaddr + 0xac));
  3693. phba->link_state = LPFC_HBA_ERROR;
  3694. return -ETIMEDOUT;
  3695. }
  3696. /* Check to see if any errors occurred during init */
  3697. if (status & HS_FFERM) {
  3698. /* ERROR: During chipset initialization */
  3699. /* Adapter failed to init, chipset, status reg
  3700. <status> */
  3701. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3702. "0437 Adapter failed to init, "
  3703. "chipset, status reg x%x, "
  3704. "FW Data: A8 x%x AC x%x\n", status,
  3705. readl(phba->MBslimaddr + 0xa8),
  3706. readl(phba->MBslimaddr + 0xac));
  3707. phba->link_state = LPFC_HBA_ERROR;
  3708. return -EIO;
  3709. }
  3710. if (i <= 10)
  3711. msleep(10);
  3712. else if (i <= 100)
  3713. msleep(100);
  3714. else
  3715. msleep(1000);
  3716. if (i == 150) {
  3717. /* Do post */
  3718. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3719. lpfc_sli_brdrestart(phba);
  3720. }
  3721. /* Read the HBA Host Status Register */
  3722. if (lpfc_readl(phba->HSregaddr, &status))
  3723. return -EIO;
  3724. }
  3725. /* Check to see if any errors occurred during init */
  3726. if (status & HS_FFERM) {
  3727. /* ERROR: During chipset initialization */
  3728. /* Adapter failed to init, chipset, status reg <status> */
  3729. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3730. "0438 Adapter failed to init, chipset, "
  3731. "status reg x%x, "
  3732. "FW Data: A8 x%x AC x%x\n", status,
  3733. readl(phba->MBslimaddr + 0xa8),
  3734. readl(phba->MBslimaddr + 0xac));
  3735. phba->link_state = LPFC_HBA_ERROR;
  3736. return -EIO;
  3737. }
  3738. /* Clear all interrupt enable conditions */
  3739. writel(0, phba->HCregaddr);
  3740. readl(phba->HCregaddr); /* flush */
  3741. /* setup host attn register */
  3742. writel(0xffffffff, phba->HAregaddr);
  3743. readl(phba->HAregaddr); /* flush */
  3744. return 0;
  3745. }
  3746. /**
  3747. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3748. *
  3749. * This function calculates and returns the number of HBQs required to be
  3750. * configured.
  3751. **/
  3752. int
  3753. lpfc_sli_hbq_count(void)
  3754. {
  3755. return ARRAY_SIZE(lpfc_hbq_defs);
  3756. }
  3757. /**
  3758. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3759. *
  3760. * This function adds the number of hbq entries in every HBQ to get
  3761. * the total number of hbq entries required for the HBA and returns
  3762. * the total count.
  3763. **/
  3764. static int
  3765. lpfc_sli_hbq_entry_count(void)
  3766. {
  3767. int hbq_count = lpfc_sli_hbq_count();
  3768. int count = 0;
  3769. int i;
  3770. for (i = 0; i < hbq_count; ++i)
  3771. count += lpfc_hbq_defs[i]->entry_count;
  3772. return count;
  3773. }
  3774. /**
  3775. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3776. *
  3777. * This function calculates amount of memory required for all hbq entries
  3778. * to be configured and returns the total memory required.
  3779. **/
  3780. int
  3781. lpfc_sli_hbq_size(void)
  3782. {
  3783. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3784. }
  3785. /**
  3786. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3787. * @phba: Pointer to HBA context object.
  3788. *
  3789. * This function is called during the SLI initialization to configure
  3790. * all the HBQs and post buffers to the HBQ. The caller is not
  3791. * required to hold any locks. This function will return zero if successful
  3792. * else it will return negative error code.
  3793. **/
  3794. static int
  3795. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3796. {
  3797. int hbq_count = lpfc_sli_hbq_count();
  3798. LPFC_MBOXQ_t *pmb;
  3799. MAILBOX_t *pmbox;
  3800. uint32_t hbqno;
  3801. uint32_t hbq_entry_index;
  3802. /* Get a Mailbox buffer to setup mailbox
  3803. * commands for HBA initialization
  3804. */
  3805. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3806. if (!pmb)
  3807. return -ENOMEM;
  3808. pmbox = &pmb->u.mb;
  3809. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3810. phba->link_state = LPFC_INIT_MBX_CMDS;
  3811. phba->hbq_in_use = 1;
  3812. hbq_entry_index = 0;
  3813. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3814. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3815. phba->hbqs[hbqno].hbqPutIdx = 0;
  3816. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3817. phba->hbqs[hbqno].entry_count =
  3818. lpfc_hbq_defs[hbqno]->entry_count;
  3819. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3820. hbq_entry_index, pmb);
  3821. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3822. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3823. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3824. mbxStatus <status>, ring <num> */
  3825. lpfc_printf_log(phba, KERN_ERR,
  3826. LOG_SLI | LOG_VPORT,
  3827. "1805 Adapter failed to init. "
  3828. "Data: x%x x%x x%x\n",
  3829. pmbox->mbxCommand,
  3830. pmbox->mbxStatus, hbqno);
  3831. phba->link_state = LPFC_HBA_ERROR;
  3832. mempool_free(pmb, phba->mbox_mem_pool);
  3833. return -ENXIO;
  3834. }
  3835. }
  3836. phba->hbq_count = hbq_count;
  3837. mempool_free(pmb, phba->mbox_mem_pool);
  3838. /* Initially populate or replenish the HBQs */
  3839. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3840. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3841. return 0;
  3842. }
  3843. /**
  3844. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3845. * @phba: Pointer to HBA context object.
  3846. *
  3847. * This function is called during the SLI initialization to configure
  3848. * all the HBQs and post buffers to the HBQ. The caller is not
  3849. * required to hold any locks. This function will return zero if successful
  3850. * else it will return negative error code.
  3851. **/
  3852. static int
  3853. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3854. {
  3855. phba->hbq_in_use = 1;
  3856. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3857. phba->hbq_count = 1;
  3858. /* Initially populate or replenish the HBQs */
  3859. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3860. return 0;
  3861. }
  3862. /**
  3863. * lpfc_sli_config_port - Issue config port mailbox command
  3864. * @phba: Pointer to HBA context object.
  3865. * @sli_mode: sli mode - 2/3
  3866. *
  3867. * This function is called by the sli intialization code path
  3868. * to issue config_port mailbox command. This function restarts the
  3869. * HBA firmware and issues a config_port mailbox command to configure
  3870. * the SLI interface in the sli mode specified by sli_mode
  3871. * variable. The caller is not required to hold any locks.
  3872. * The function returns 0 if successful, else returns negative error
  3873. * code.
  3874. **/
  3875. int
  3876. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3877. {
  3878. LPFC_MBOXQ_t *pmb;
  3879. uint32_t resetcount = 0, rc = 0, done = 0;
  3880. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3881. if (!pmb) {
  3882. phba->link_state = LPFC_HBA_ERROR;
  3883. return -ENOMEM;
  3884. }
  3885. phba->sli_rev = sli_mode;
  3886. while (resetcount < 2 && !done) {
  3887. spin_lock_irq(&phba->hbalock);
  3888. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3889. spin_unlock_irq(&phba->hbalock);
  3890. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3891. lpfc_sli_brdrestart(phba);
  3892. rc = lpfc_sli_chipset_init(phba);
  3893. if (rc)
  3894. break;
  3895. spin_lock_irq(&phba->hbalock);
  3896. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3897. spin_unlock_irq(&phba->hbalock);
  3898. resetcount++;
  3899. /* Call pre CONFIG_PORT mailbox command initialization. A
  3900. * value of 0 means the call was successful. Any other
  3901. * nonzero value is a failure, but if ERESTART is returned,
  3902. * the driver may reset the HBA and try again.
  3903. */
  3904. rc = lpfc_config_port_prep(phba);
  3905. if (rc == -ERESTART) {
  3906. phba->link_state = LPFC_LINK_UNKNOWN;
  3907. continue;
  3908. } else if (rc)
  3909. break;
  3910. phba->link_state = LPFC_INIT_MBX_CMDS;
  3911. lpfc_config_port(phba, pmb);
  3912. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3913. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3914. LPFC_SLI3_HBQ_ENABLED |
  3915. LPFC_SLI3_CRP_ENABLED |
  3916. LPFC_SLI3_BG_ENABLED |
  3917. LPFC_SLI3_DSS_ENABLED);
  3918. if (rc != MBX_SUCCESS) {
  3919. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3920. "0442 Adapter failed to init, mbxCmd x%x "
  3921. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3922. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3923. spin_lock_irq(&phba->hbalock);
  3924. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3925. spin_unlock_irq(&phba->hbalock);
  3926. rc = -ENXIO;
  3927. } else {
  3928. /* Allow asynchronous mailbox command to go through */
  3929. spin_lock_irq(&phba->hbalock);
  3930. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3931. spin_unlock_irq(&phba->hbalock);
  3932. done = 1;
  3933. }
  3934. }
  3935. if (!done) {
  3936. rc = -EINVAL;
  3937. goto do_prep_failed;
  3938. }
  3939. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3940. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3941. rc = -ENXIO;
  3942. goto do_prep_failed;
  3943. }
  3944. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3945. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3946. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3947. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3948. phba->max_vpi : phba->max_vports;
  3949. } else
  3950. phba->max_vpi = 0;
  3951. phba->fips_level = 0;
  3952. phba->fips_spec_rev = 0;
  3953. if (pmb->u.mb.un.varCfgPort.gdss) {
  3954. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3955. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3956. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3957. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3958. "2850 Security Crypto Active. FIPS x%d "
  3959. "(Spec Rev: x%d)",
  3960. phba->fips_level, phba->fips_spec_rev);
  3961. }
  3962. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3963. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3964. "2856 Config Port Security Crypto "
  3965. "Error: x%x ",
  3966. pmb->u.mb.un.varCfgPort.sec_err);
  3967. }
  3968. if (pmb->u.mb.un.varCfgPort.gerbm)
  3969. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3970. if (pmb->u.mb.un.varCfgPort.gcrp)
  3971. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3972. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3973. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3974. if (phba->cfg_enable_bg) {
  3975. if (pmb->u.mb.un.varCfgPort.gbg)
  3976. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3977. else
  3978. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3979. "0443 Adapter did not grant "
  3980. "BlockGuard\n");
  3981. }
  3982. } else {
  3983. phba->hbq_get = NULL;
  3984. phba->port_gp = phba->mbox->us.s2.port;
  3985. phba->max_vpi = 0;
  3986. }
  3987. do_prep_failed:
  3988. mempool_free(pmb, phba->mbox_mem_pool);
  3989. return rc;
  3990. }
  3991. /**
  3992. * lpfc_sli_hba_setup - SLI intialization function
  3993. * @phba: Pointer to HBA context object.
  3994. *
  3995. * This function is the main SLI intialization function. This function
  3996. * is called by the HBA intialization code, HBA reset code and HBA
  3997. * error attention handler code. Caller is not required to hold any
  3998. * locks. This function issues config_port mailbox command to configure
  3999. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4000. * calls the config_port_post function to issue init_link mailbox
  4001. * command and to start the discovery. The function will return zero
  4002. * if successful, else it will return negative error code.
  4003. **/
  4004. int
  4005. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4006. {
  4007. uint32_t rc;
  4008. int mode = 3, i;
  4009. int longs;
  4010. switch (lpfc_sli_mode) {
  4011. case 2:
  4012. if (phba->cfg_enable_npiv) {
  4013. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4014. "1824 NPIV enabled: Override lpfc_sli_mode "
  4015. "parameter (%d) to auto (0).\n",
  4016. lpfc_sli_mode);
  4017. break;
  4018. }
  4019. mode = 2;
  4020. break;
  4021. case 0:
  4022. case 3:
  4023. break;
  4024. default:
  4025. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4026. "1819 Unrecognized lpfc_sli_mode "
  4027. "parameter: %d.\n", lpfc_sli_mode);
  4028. break;
  4029. }
  4030. rc = lpfc_sli_config_port(phba, mode);
  4031. if (rc && lpfc_sli_mode == 3)
  4032. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4033. "1820 Unable to select SLI-3. "
  4034. "Not supported by adapter.\n");
  4035. if (rc && mode != 2)
  4036. rc = lpfc_sli_config_port(phba, 2);
  4037. if (rc)
  4038. goto lpfc_sli_hba_setup_error;
  4039. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4040. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4041. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4042. if (!rc) {
  4043. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4044. "2709 This device supports "
  4045. "Advanced Error Reporting (AER)\n");
  4046. spin_lock_irq(&phba->hbalock);
  4047. phba->hba_flag |= HBA_AER_ENABLED;
  4048. spin_unlock_irq(&phba->hbalock);
  4049. } else {
  4050. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4051. "2708 This device does not support "
  4052. "Advanced Error Reporting (AER)\n");
  4053. phba->cfg_aer_support = 0;
  4054. }
  4055. }
  4056. if (phba->sli_rev == 3) {
  4057. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4058. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4059. } else {
  4060. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4061. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4062. phba->sli3_options = 0;
  4063. }
  4064. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4065. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4066. phba->sli_rev, phba->max_vpi);
  4067. rc = lpfc_sli_ring_map(phba);
  4068. if (rc)
  4069. goto lpfc_sli_hba_setup_error;
  4070. /* Initialize VPIs. */
  4071. if (phba->sli_rev == LPFC_SLI_REV3) {
  4072. /*
  4073. * The VPI bitmask and physical ID array are allocated
  4074. * and initialized once only - at driver load. A port
  4075. * reset doesn't need to reinitialize this memory.
  4076. */
  4077. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4078. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4079. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4080. GFP_KERNEL);
  4081. if (!phba->vpi_bmask) {
  4082. rc = -ENOMEM;
  4083. goto lpfc_sli_hba_setup_error;
  4084. }
  4085. phba->vpi_ids = kzalloc(
  4086. (phba->max_vpi+1) * sizeof(uint16_t),
  4087. GFP_KERNEL);
  4088. if (!phba->vpi_ids) {
  4089. kfree(phba->vpi_bmask);
  4090. rc = -ENOMEM;
  4091. goto lpfc_sli_hba_setup_error;
  4092. }
  4093. for (i = 0; i < phba->max_vpi; i++)
  4094. phba->vpi_ids[i] = i;
  4095. }
  4096. }
  4097. /* Init HBQs */
  4098. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4099. rc = lpfc_sli_hbq_setup(phba);
  4100. if (rc)
  4101. goto lpfc_sli_hba_setup_error;
  4102. }
  4103. spin_lock_irq(&phba->hbalock);
  4104. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4105. spin_unlock_irq(&phba->hbalock);
  4106. rc = lpfc_config_port_post(phba);
  4107. if (rc)
  4108. goto lpfc_sli_hba_setup_error;
  4109. return rc;
  4110. lpfc_sli_hba_setup_error:
  4111. phba->link_state = LPFC_HBA_ERROR;
  4112. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4113. "0445 Firmware initialization failed\n");
  4114. return rc;
  4115. }
  4116. /**
  4117. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4118. * @phba: Pointer to HBA context object.
  4119. * @mboxq: mailbox pointer.
  4120. * This function issue a dump mailbox command to read config region
  4121. * 23 and parse the records in the region and populate driver
  4122. * data structure.
  4123. **/
  4124. static int
  4125. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
  4126. LPFC_MBOXQ_t *mboxq)
  4127. {
  4128. struct lpfc_dmabuf *mp;
  4129. struct lpfc_mqe *mqe;
  4130. uint32_t data_length;
  4131. int rc;
  4132. /* Program the default value of vlan_id and fc_map */
  4133. phba->valid_vlan = 0;
  4134. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4135. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4136. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4137. mqe = &mboxq->u.mqe;
  4138. if (lpfc_dump_fcoe_param(phba, mboxq))
  4139. return -ENOMEM;
  4140. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4141. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4142. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4143. "(%d):2571 Mailbox cmd x%x Status x%x "
  4144. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4145. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4146. "CQ: x%x x%x x%x x%x\n",
  4147. mboxq->vport ? mboxq->vport->vpi : 0,
  4148. bf_get(lpfc_mqe_command, mqe),
  4149. bf_get(lpfc_mqe_status, mqe),
  4150. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4151. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4152. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4153. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4154. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4155. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4156. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4157. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4158. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4159. mboxq->mcqe.word0,
  4160. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4161. mboxq->mcqe.trailer);
  4162. if (rc) {
  4163. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4164. kfree(mp);
  4165. return -EIO;
  4166. }
  4167. data_length = mqe->un.mb_words[5];
  4168. if (data_length > DMP_RGN23_SIZE) {
  4169. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4170. kfree(mp);
  4171. return -EIO;
  4172. }
  4173. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4174. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4175. kfree(mp);
  4176. return 0;
  4177. }
  4178. /**
  4179. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4180. * @phba: pointer to lpfc hba data structure.
  4181. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4182. * @vpd: pointer to the memory to hold resulting port vpd data.
  4183. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4184. * On output, the number of data bytes in @vpd.
  4185. *
  4186. * This routine executes a READ_REV SLI4 mailbox command. In
  4187. * addition, this routine gets the port vpd data.
  4188. *
  4189. * Return codes
  4190. * 0 - successful
  4191. * -ENOMEM - could not allocated memory.
  4192. **/
  4193. static int
  4194. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4195. uint8_t *vpd, uint32_t *vpd_size)
  4196. {
  4197. int rc = 0;
  4198. uint32_t dma_size;
  4199. struct lpfc_dmabuf *dmabuf;
  4200. struct lpfc_mqe *mqe;
  4201. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4202. if (!dmabuf)
  4203. return -ENOMEM;
  4204. /*
  4205. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4206. * mailbox command.
  4207. */
  4208. dma_size = *vpd_size;
  4209. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4210. dma_size,
  4211. &dmabuf->phys,
  4212. GFP_KERNEL);
  4213. if (!dmabuf->virt) {
  4214. kfree(dmabuf);
  4215. return -ENOMEM;
  4216. }
  4217. memset(dmabuf->virt, 0, dma_size);
  4218. /*
  4219. * The SLI4 implementation of READ_REV conflicts at word1,
  4220. * bits 31:16 and SLI4 adds vpd functionality not present
  4221. * in SLI3. This code corrects the conflicts.
  4222. */
  4223. lpfc_read_rev(phba, mboxq);
  4224. mqe = &mboxq->u.mqe;
  4225. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4226. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4227. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4228. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4229. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4230. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4231. if (rc) {
  4232. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4233. dmabuf->virt, dmabuf->phys);
  4234. kfree(dmabuf);
  4235. return -EIO;
  4236. }
  4237. /*
  4238. * The available vpd length cannot be bigger than the
  4239. * DMA buffer passed to the port. Catch the less than
  4240. * case and update the caller's size.
  4241. */
  4242. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4243. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4244. memcpy(vpd, dmabuf->virt, *vpd_size);
  4245. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4246. dmabuf->virt, dmabuf->phys);
  4247. kfree(dmabuf);
  4248. return 0;
  4249. }
  4250. /**
  4251. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4252. * @phba: pointer to lpfc hba data structure.
  4253. *
  4254. * This routine retrieves SLI4 device physical port name this PCI function
  4255. * is attached to.
  4256. *
  4257. * Return codes
  4258. * 0 - sucessful
  4259. * otherwise - failed to retrieve physical port name
  4260. **/
  4261. static int
  4262. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4263. {
  4264. LPFC_MBOXQ_t *mboxq;
  4265. struct lpfc_mbx_read_config *rd_config;
  4266. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4267. struct lpfc_controller_attribute *cntl_attr;
  4268. struct lpfc_mbx_get_port_name *get_port_name;
  4269. void *virtaddr = NULL;
  4270. uint32_t alloclen, reqlen;
  4271. uint32_t shdr_status, shdr_add_status;
  4272. union lpfc_sli4_cfg_shdr *shdr;
  4273. char cport_name = 0;
  4274. int rc;
  4275. /* We assume nothing at this point */
  4276. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4277. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4278. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4279. if (!mboxq)
  4280. return -ENOMEM;
  4281. /* obtain link type and link number via READ_CONFIG */
  4282. lpfc_read_config(phba, mboxq);
  4283. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4284. if (rc == MBX_SUCCESS) {
  4285. rd_config = &mboxq->u.mqe.un.rd_config;
  4286. if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
  4287. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4288. phba->sli4_hba.lnk_info.lnk_tp =
  4289. bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
  4290. phba->sli4_hba.lnk_info.lnk_no =
  4291. bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
  4292. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4293. "3081 lnk_type:%d, lnk_numb:%d\n",
  4294. phba->sli4_hba.lnk_info.lnk_tp,
  4295. phba->sli4_hba.lnk_info.lnk_no);
  4296. goto retrieve_ppname;
  4297. } else
  4298. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4299. "3082 Mailbox (x%x) returned ldv:x0\n",
  4300. bf_get(lpfc_mqe_command,
  4301. &mboxq->u.mqe));
  4302. } else
  4303. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4304. "3083 Mailbox (x%x) failed, status:x%x\n",
  4305. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4306. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  4307. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4308. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4309. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4310. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4311. LPFC_SLI4_MBX_NEMBED);
  4312. if (alloclen < reqlen) {
  4313. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4314. "3084 Allocated DMA memory size (%d) is "
  4315. "less than the requested DMA memory size "
  4316. "(%d)\n", alloclen, reqlen);
  4317. rc = -ENOMEM;
  4318. goto out_free_mboxq;
  4319. }
  4320. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4321. virtaddr = mboxq->sge_array->addr[0];
  4322. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4323. shdr = &mbx_cntl_attr->cfg_shdr;
  4324. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4325. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4326. if (shdr_status || shdr_add_status || rc) {
  4327. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4328. "3085 Mailbox x%x (x%x/x%x) failed, "
  4329. "rc:x%x, status:x%x, add_status:x%x\n",
  4330. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4331. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4332. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4333. rc, shdr_status, shdr_add_status);
  4334. rc = -ENXIO;
  4335. goto out_free_mboxq;
  4336. }
  4337. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4338. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4339. phba->sli4_hba.lnk_info.lnk_tp =
  4340. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4341. phba->sli4_hba.lnk_info.lnk_no =
  4342. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4343. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4344. "3086 lnk_type:%d, lnk_numb:%d\n",
  4345. phba->sli4_hba.lnk_info.lnk_tp,
  4346. phba->sli4_hba.lnk_info.lnk_no);
  4347. retrieve_ppname:
  4348. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4349. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4350. sizeof(struct lpfc_mbx_get_port_name) -
  4351. sizeof(struct lpfc_sli4_cfg_mhdr),
  4352. LPFC_SLI4_MBX_EMBED);
  4353. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4354. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4355. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4356. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4357. phba->sli4_hba.lnk_info.lnk_tp);
  4358. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4359. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4360. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4361. if (shdr_status || shdr_add_status || rc) {
  4362. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4363. "3087 Mailbox x%x (x%x/x%x) failed: "
  4364. "rc:x%x, status:x%x, add_status:x%x\n",
  4365. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4366. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4367. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4368. rc, shdr_status, shdr_add_status);
  4369. rc = -ENXIO;
  4370. goto out_free_mboxq;
  4371. }
  4372. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4373. case LPFC_LINK_NUMBER_0:
  4374. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4375. &get_port_name->u.response);
  4376. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4377. break;
  4378. case LPFC_LINK_NUMBER_1:
  4379. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4380. &get_port_name->u.response);
  4381. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4382. break;
  4383. case LPFC_LINK_NUMBER_2:
  4384. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4385. &get_port_name->u.response);
  4386. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4387. break;
  4388. case LPFC_LINK_NUMBER_3:
  4389. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4390. &get_port_name->u.response);
  4391. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4392. break;
  4393. default:
  4394. break;
  4395. }
  4396. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4397. phba->Port[0] = cport_name;
  4398. phba->Port[1] = '\0';
  4399. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4400. "3091 SLI get port name: %s\n", phba->Port);
  4401. }
  4402. out_free_mboxq:
  4403. if (rc != MBX_TIMEOUT) {
  4404. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4405. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4406. else
  4407. mempool_free(mboxq, phba->mbox_mem_pool);
  4408. }
  4409. return rc;
  4410. }
  4411. /**
  4412. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4413. * @phba: pointer to lpfc hba data structure.
  4414. *
  4415. * This routine is called to explicitly arm the SLI4 device's completion and
  4416. * event queues
  4417. **/
  4418. static void
  4419. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4420. {
  4421. uint8_t fcp_eqidx;
  4422. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4423. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4424. fcp_eqidx = 0;
  4425. do
  4426. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4427. LPFC_QUEUE_REARM);
  4428. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4429. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4430. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
  4431. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4432. LPFC_QUEUE_REARM);
  4433. }
  4434. /**
  4435. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4436. * @phba: Pointer to HBA context object.
  4437. * @type: The resource extent type.
  4438. * @extnt_count: buffer to hold port available extent count.
  4439. * @extnt_size: buffer to hold element count per extent.
  4440. *
  4441. * This function calls the port and retrievs the number of available
  4442. * extents and their size for a particular extent type.
  4443. *
  4444. * Returns: 0 if successful. Nonzero otherwise.
  4445. **/
  4446. int
  4447. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4448. uint16_t *extnt_count, uint16_t *extnt_size)
  4449. {
  4450. int rc = 0;
  4451. uint32_t length;
  4452. uint32_t mbox_tmo;
  4453. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4454. LPFC_MBOXQ_t *mbox;
  4455. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4456. if (!mbox)
  4457. return -ENOMEM;
  4458. /* Find out how many extents are available for this resource type */
  4459. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4460. sizeof(struct lpfc_sli4_cfg_mhdr));
  4461. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4462. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4463. length, LPFC_SLI4_MBX_EMBED);
  4464. /* Send an extents count of 0 - the GET doesn't use it. */
  4465. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4466. LPFC_SLI4_MBX_EMBED);
  4467. if (unlikely(rc)) {
  4468. rc = -EIO;
  4469. goto err_exit;
  4470. }
  4471. if (!phba->sli4_hba.intr_enable)
  4472. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4473. else {
  4474. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4475. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4476. }
  4477. if (unlikely(rc)) {
  4478. rc = -EIO;
  4479. goto err_exit;
  4480. }
  4481. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4482. if (bf_get(lpfc_mbox_hdr_status,
  4483. &rsrc_info->header.cfg_shdr.response)) {
  4484. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4485. "2930 Failed to get resource extents "
  4486. "Status 0x%x Add'l Status 0x%x\n",
  4487. bf_get(lpfc_mbox_hdr_status,
  4488. &rsrc_info->header.cfg_shdr.response),
  4489. bf_get(lpfc_mbox_hdr_add_status,
  4490. &rsrc_info->header.cfg_shdr.response));
  4491. rc = -EIO;
  4492. goto err_exit;
  4493. }
  4494. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4495. &rsrc_info->u.rsp);
  4496. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4497. &rsrc_info->u.rsp);
  4498. err_exit:
  4499. mempool_free(mbox, phba->mbox_mem_pool);
  4500. return rc;
  4501. }
  4502. /**
  4503. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4504. * @phba: Pointer to HBA context object.
  4505. * @type: The extent type to check.
  4506. *
  4507. * This function reads the current available extents from the port and checks
  4508. * if the extent count or extent size has changed since the last access.
  4509. * Callers use this routine post port reset to understand if there is a
  4510. * extent reprovisioning requirement.
  4511. *
  4512. * Returns:
  4513. * -Error: error indicates problem.
  4514. * 1: Extent count or size has changed.
  4515. * 0: No changes.
  4516. **/
  4517. static int
  4518. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4519. {
  4520. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4521. uint16_t size_diff, rsrc_ext_size;
  4522. int rc = 0;
  4523. struct lpfc_rsrc_blks *rsrc_entry;
  4524. struct list_head *rsrc_blk_list = NULL;
  4525. size_diff = 0;
  4526. curr_ext_cnt = 0;
  4527. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4528. &rsrc_ext_cnt,
  4529. &rsrc_ext_size);
  4530. if (unlikely(rc))
  4531. return -EIO;
  4532. switch (type) {
  4533. case LPFC_RSC_TYPE_FCOE_RPI:
  4534. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4535. break;
  4536. case LPFC_RSC_TYPE_FCOE_VPI:
  4537. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4538. break;
  4539. case LPFC_RSC_TYPE_FCOE_XRI:
  4540. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4541. break;
  4542. case LPFC_RSC_TYPE_FCOE_VFI:
  4543. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4544. break;
  4545. default:
  4546. break;
  4547. }
  4548. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4549. curr_ext_cnt++;
  4550. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4551. size_diff++;
  4552. }
  4553. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4554. rc = 1;
  4555. return rc;
  4556. }
  4557. /**
  4558. * lpfc_sli4_cfg_post_extnts -
  4559. * @phba: Pointer to HBA context object.
  4560. * @extnt_cnt - number of available extents.
  4561. * @type - the extent type (rpi, xri, vfi, vpi).
  4562. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4563. * @mbox - pointer to the caller's allocated mailbox structure.
  4564. *
  4565. * This function executes the extents allocation request. It also
  4566. * takes care of the amount of memory needed to allocate or get the
  4567. * allocated extents. It is the caller's responsibility to evaluate
  4568. * the response.
  4569. *
  4570. * Returns:
  4571. * -Error: Error value describes the condition found.
  4572. * 0: if successful
  4573. **/
  4574. static int
  4575. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
  4576. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4577. {
  4578. int rc = 0;
  4579. uint32_t req_len;
  4580. uint32_t emb_len;
  4581. uint32_t alloc_len, mbox_tmo;
  4582. /* Calculate the total requested length of the dma memory */
  4583. req_len = *extnt_cnt * sizeof(uint16_t);
  4584. /*
  4585. * Calculate the size of an embedded mailbox. The uint32_t
  4586. * accounts for extents-specific word.
  4587. */
  4588. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4589. sizeof(uint32_t);
  4590. /*
  4591. * Presume the allocation and response will fit into an embedded
  4592. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4593. */
  4594. *emb = LPFC_SLI4_MBX_EMBED;
  4595. if (req_len > emb_len) {
  4596. req_len = *extnt_cnt * sizeof(uint16_t) +
  4597. sizeof(union lpfc_sli4_cfg_shdr) +
  4598. sizeof(uint32_t);
  4599. *emb = LPFC_SLI4_MBX_NEMBED;
  4600. }
  4601. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4602. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4603. req_len, *emb);
  4604. if (alloc_len < req_len) {
  4605. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4606. "2982 Allocated DMA memory size (x%x) is "
  4607. "less than the requested DMA memory "
  4608. "size (x%x)\n", alloc_len, req_len);
  4609. return -ENOMEM;
  4610. }
  4611. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
  4612. if (unlikely(rc))
  4613. return -EIO;
  4614. if (!phba->sli4_hba.intr_enable)
  4615. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4616. else {
  4617. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4618. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4619. }
  4620. if (unlikely(rc))
  4621. rc = -EIO;
  4622. return rc;
  4623. }
  4624. /**
  4625. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4626. * @phba: Pointer to HBA context object.
  4627. * @type: The resource extent type to allocate.
  4628. *
  4629. * This function allocates the number of elements for the specified
  4630. * resource type.
  4631. **/
  4632. static int
  4633. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4634. {
  4635. bool emb = false;
  4636. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4637. uint16_t rsrc_id, rsrc_start, j, k;
  4638. uint16_t *ids;
  4639. int i, rc;
  4640. unsigned long longs;
  4641. unsigned long *bmask;
  4642. struct lpfc_rsrc_blks *rsrc_blks;
  4643. LPFC_MBOXQ_t *mbox;
  4644. uint32_t length;
  4645. struct lpfc_id_range *id_array = NULL;
  4646. void *virtaddr = NULL;
  4647. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4648. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4649. struct list_head *ext_blk_list;
  4650. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4651. &rsrc_cnt,
  4652. &rsrc_size);
  4653. if (unlikely(rc))
  4654. return -EIO;
  4655. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4656. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4657. "3009 No available Resource Extents "
  4658. "for resource type 0x%x: Count: 0x%x, "
  4659. "Size 0x%x\n", type, rsrc_cnt,
  4660. rsrc_size);
  4661. return -ENOMEM;
  4662. }
  4663. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
  4664. "2903 Available Resource Extents "
  4665. "for resource type 0x%x: Count: 0x%x, "
  4666. "Size 0x%x\n", type, rsrc_cnt,
  4667. rsrc_size);
  4668. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4669. if (!mbox)
  4670. return -ENOMEM;
  4671. rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
  4672. if (unlikely(rc)) {
  4673. rc = -EIO;
  4674. goto err_exit;
  4675. }
  4676. /*
  4677. * Figure out where the response is located. Then get local pointers
  4678. * to the response data. The port does not guarantee to respond to
  4679. * all extents counts request so update the local variable with the
  4680. * allocated count from the port.
  4681. */
  4682. if (emb == LPFC_SLI4_MBX_EMBED) {
  4683. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4684. id_array = &rsrc_ext->u.rsp.id[0];
  4685. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4686. } else {
  4687. virtaddr = mbox->sge_array->addr[0];
  4688. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4689. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4690. id_array = &n_rsrc->id;
  4691. }
  4692. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4693. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4694. /*
  4695. * Based on the resource size and count, correct the base and max
  4696. * resource values.
  4697. */
  4698. length = sizeof(struct lpfc_rsrc_blks);
  4699. switch (type) {
  4700. case LPFC_RSC_TYPE_FCOE_RPI:
  4701. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4702. sizeof(unsigned long),
  4703. GFP_KERNEL);
  4704. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4705. rc = -ENOMEM;
  4706. goto err_exit;
  4707. }
  4708. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4709. sizeof(uint16_t),
  4710. GFP_KERNEL);
  4711. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4712. kfree(phba->sli4_hba.rpi_bmask);
  4713. rc = -ENOMEM;
  4714. goto err_exit;
  4715. }
  4716. /*
  4717. * The next_rpi was initialized with the maximum available
  4718. * count but the port may allocate a smaller number. Catch
  4719. * that case and update the next_rpi.
  4720. */
  4721. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4722. /* Initialize local ptrs for common extent processing later. */
  4723. bmask = phba->sli4_hba.rpi_bmask;
  4724. ids = phba->sli4_hba.rpi_ids;
  4725. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4726. break;
  4727. case LPFC_RSC_TYPE_FCOE_VPI:
  4728. phba->vpi_bmask = kzalloc(longs *
  4729. sizeof(unsigned long),
  4730. GFP_KERNEL);
  4731. if (unlikely(!phba->vpi_bmask)) {
  4732. rc = -ENOMEM;
  4733. goto err_exit;
  4734. }
  4735. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4736. sizeof(uint16_t),
  4737. GFP_KERNEL);
  4738. if (unlikely(!phba->vpi_ids)) {
  4739. kfree(phba->vpi_bmask);
  4740. rc = -ENOMEM;
  4741. goto err_exit;
  4742. }
  4743. /* Initialize local ptrs for common extent processing later. */
  4744. bmask = phba->vpi_bmask;
  4745. ids = phba->vpi_ids;
  4746. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4747. break;
  4748. case LPFC_RSC_TYPE_FCOE_XRI:
  4749. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4750. sizeof(unsigned long),
  4751. GFP_KERNEL);
  4752. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4753. rc = -ENOMEM;
  4754. goto err_exit;
  4755. }
  4756. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4757. sizeof(uint16_t),
  4758. GFP_KERNEL);
  4759. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4760. kfree(phba->sli4_hba.xri_bmask);
  4761. rc = -ENOMEM;
  4762. goto err_exit;
  4763. }
  4764. /* Initialize local ptrs for common extent processing later. */
  4765. bmask = phba->sli4_hba.xri_bmask;
  4766. ids = phba->sli4_hba.xri_ids;
  4767. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4768. break;
  4769. case LPFC_RSC_TYPE_FCOE_VFI:
  4770. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4771. sizeof(unsigned long),
  4772. GFP_KERNEL);
  4773. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4774. rc = -ENOMEM;
  4775. goto err_exit;
  4776. }
  4777. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4778. sizeof(uint16_t),
  4779. GFP_KERNEL);
  4780. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4781. kfree(phba->sli4_hba.vfi_bmask);
  4782. rc = -ENOMEM;
  4783. goto err_exit;
  4784. }
  4785. /* Initialize local ptrs for common extent processing later. */
  4786. bmask = phba->sli4_hba.vfi_bmask;
  4787. ids = phba->sli4_hba.vfi_ids;
  4788. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4789. break;
  4790. default:
  4791. /* Unsupported Opcode. Fail call. */
  4792. id_array = NULL;
  4793. bmask = NULL;
  4794. ids = NULL;
  4795. ext_blk_list = NULL;
  4796. goto err_exit;
  4797. }
  4798. /*
  4799. * Complete initializing the extent configuration with the
  4800. * allocated ids assigned to this function. The bitmask serves
  4801. * as an index into the array and manages the available ids. The
  4802. * array just stores the ids communicated to the port via the wqes.
  4803. */
  4804. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4805. if ((i % 2) == 0)
  4806. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4807. &id_array[k]);
  4808. else
  4809. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4810. &id_array[k]);
  4811. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4812. if (unlikely(!rsrc_blks)) {
  4813. rc = -ENOMEM;
  4814. kfree(bmask);
  4815. kfree(ids);
  4816. goto err_exit;
  4817. }
  4818. rsrc_blks->rsrc_start = rsrc_id;
  4819. rsrc_blks->rsrc_size = rsrc_size;
  4820. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4821. rsrc_start = rsrc_id;
  4822. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4823. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4824. lpfc_sli4_get_els_iocb_cnt(phba);
  4825. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4826. ids[j] = rsrc_id;
  4827. rsrc_id++;
  4828. j++;
  4829. }
  4830. /* Entire word processed. Get next word.*/
  4831. if ((i % 2) == 1)
  4832. k++;
  4833. }
  4834. err_exit:
  4835. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4836. return rc;
  4837. }
  4838. /**
  4839. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4840. * @phba: Pointer to HBA context object.
  4841. * @type: the extent's type.
  4842. *
  4843. * This function deallocates all extents of a particular resource type.
  4844. * SLI4 does not allow for deallocating a particular extent range. It
  4845. * is the caller's responsibility to release all kernel memory resources.
  4846. **/
  4847. static int
  4848. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4849. {
  4850. int rc;
  4851. uint32_t length, mbox_tmo = 0;
  4852. LPFC_MBOXQ_t *mbox;
  4853. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4854. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4855. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4856. if (!mbox)
  4857. return -ENOMEM;
  4858. /*
  4859. * This function sends an embedded mailbox because it only sends the
  4860. * the resource type. All extents of this type are released by the
  4861. * port.
  4862. */
  4863. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4864. sizeof(struct lpfc_sli4_cfg_mhdr));
  4865. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4866. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4867. length, LPFC_SLI4_MBX_EMBED);
  4868. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4869. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4870. LPFC_SLI4_MBX_EMBED);
  4871. if (unlikely(rc)) {
  4872. rc = -EIO;
  4873. goto out_free_mbox;
  4874. }
  4875. if (!phba->sli4_hba.intr_enable)
  4876. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4877. else {
  4878. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4879. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4880. }
  4881. if (unlikely(rc)) {
  4882. rc = -EIO;
  4883. goto out_free_mbox;
  4884. }
  4885. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4886. if (bf_get(lpfc_mbox_hdr_status,
  4887. &dealloc_rsrc->header.cfg_shdr.response)) {
  4888. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4889. "2919 Failed to release resource extents "
  4890. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4891. "Resource memory not released.\n",
  4892. type,
  4893. bf_get(lpfc_mbox_hdr_status,
  4894. &dealloc_rsrc->header.cfg_shdr.response),
  4895. bf_get(lpfc_mbox_hdr_add_status,
  4896. &dealloc_rsrc->header.cfg_shdr.response));
  4897. rc = -EIO;
  4898. goto out_free_mbox;
  4899. }
  4900. /* Release kernel memory resources for the specific type. */
  4901. switch (type) {
  4902. case LPFC_RSC_TYPE_FCOE_VPI:
  4903. kfree(phba->vpi_bmask);
  4904. kfree(phba->vpi_ids);
  4905. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4906. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4907. &phba->lpfc_vpi_blk_list, list) {
  4908. list_del_init(&rsrc_blk->list);
  4909. kfree(rsrc_blk);
  4910. }
  4911. break;
  4912. case LPFC_RSC_TYPE_FCOE_XRI:
  4913. kfree(phba->sli4_hba.xri_bmask);
  4914. kfree(phba->sli4_hba.xri_ids);
  4915. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4916. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4917. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4918. list_del_init(&rsrc_blk->list);
  4919. kfree(rsrc_blk);
  4920. }
  4921. break;
  4922. case LPFC_RSC_TYPE_FCOE_VFI:
  4923. kfree(phba->sli4_hba.vfi_bmask);
  4924. kfree(phba->sli4_hba.vfi_ids);
  4925. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4926. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4927. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4928. list_del_init(&rsrc_blk->list);
  4929. kfree(rsrc_blk);
  4930. }
  4931. break;
  4932. case LPFC_RSC_TYPE_FCOE_RPI:
  4933. /* RPI bitmask and physical id array are cleaned up earlier. */
  4934. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4935. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4936. list_del_init(&rsrc_blk->list);
  4937. kfree(rsrc_blk);
  4938. }
  4939. break;
  4940. default:
  4941. break;
  4942. }
  4943. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4944. out_free_mbox:
  4945. mempool_free(mbox, phba->mbox_mem_pool);
  4946. return rc;
  4947. }
  4948. /**
  4949. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4950. * @phba: Pointer to HBA context object.
  4951. *
  4952. * This function allocates all SLI4 resource identifiers.
  4953. **/
  4954. int
  4955. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4956. {
  4957. int i, rc, error = 0;
  4958. uint16_t count, base;
  4959. unsigned long longs;
  4960. if (phba->sli4_hba.extents_in_use) {
  4961. /*
  4962. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4963. * resource extent count must be read and allocated before
  4964. * provisioning the resource id arrays.
  4965. */
  4966. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4967. LPFC_IDX_RSRC_RDY) {
  4968. /*
  4969. * Extent-based resources are set - the driver could
  4970. * be in a port reset. Figure out if any corrective
  4971. * actions need to be taken.
  4972. */
  4973. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4974. LPFC_RSC_TYPE_FCOE_VFI);
  4975. if (rc != 0)
  4976. error++;
  4977. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4978. LPFC_RSC_TYPE_FCOE_VPI);
  4979. if (rc != 0)
  4980. error++;
  4981. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4982. LPFC_RSC_TYPE_FCOE_XRI);
  4983. if (rc != 0)
  4984. error++;
  4985. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4986. LPFC_RSC_TYPE_FCOE_RPI);
  4987. if (rc != 0)
  4988. error++;
  4989. /*
  4990. * It's possible that the number of resources
  4991. * provided to this port instance changed between
  4992. * resets. Detect this condition and reallocate
  4993. * resources. Otherwise, there is no action.
  4994. */
  4995. if (error) {
  4996. lpfc_printf_log(phba, KERN_INFO,
  4997. LOG_MBOX | LOG_INIT,
  4998. "2931 Detected extent resource "
  4999. "change. Reallocating all "
  5000. "extents.\n");
  5001. rc = lpfc_sli4_dealloc_extent(phba,
  5002. LPFC_RSC_TYPE_FCOE_VFI);
  5003. rc = lpfc_sli4_dealloc_extent(phba,
  5004. LPFC_RSC_TYPE_FCOE_VPI);
  5005. rc = lpfc_sli4_dealloc_extent(phba,
  5006. LPFC_RSC_TYPE_FCOE_XRI);
  5007. rc = lpfc_sli4_dealloc_extent(phba,
  5008. LPFC_RSC_TYPE_FCOE_RPI);
  5009. } else
  5010. return 0;
  5011. }
  5012. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5013. if (unlikely(rc))
  5014. goto err_exit;
  5015. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5016. if (unlikely(rc))
  5017. goto err_exit;
  5018. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5019. if (unlikely(rc))
  5020. goto err_exit;
  5021. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5022. if (unlikely(rc))
  5023. goto err_exit;
  5024. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5025. LPFC_IDX_RSRC_RDY);
  5026. return rc;
  5027. } else {
  5028. /*
  5029. * The port does not support resource extents. The XRI, VPI,
  5030. * VFI, RPI resource ids were determined from READ_CONFIG.
  5031. * Just allocate the bitmasks and provision the resource id
  5032. * arrays. If a port reset is active, the resources don't
  5033. * need any action - just exit.
  5034. */
  5035. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5036. LPFC_IDX_RSRC_RDY)
  5037. return 0;
  5038. /* RPIs. */
  5039. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5040. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5041. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5042. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5043. sizeof(unsigned long),
  5044. GFP_KERNEL);
  5045. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5046. rc = -ENOMEM;
  5047. goto err_exit;
  5048. }
  5049. phba->sli4_hba.rpi_ids = kzalloc(count *
  5050. sizeof(uint16_t),
  5051. GFP_KERNEL);
  5052. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5053. rc = -ENOMEM;
  5054. goto free_rpi_bmask;
  5055. }
  5056. for (i = 0; i < count; i++)
  5057. phba->sli4_hba.rpi_ids[i] = base + i;
  5058. /* VPIs. */
  5059. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5060. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5061. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5062. phba->vpi_bmask = kzalloc(longs *
  5063. sizeof(unsigned long),
  5064. GFP_KERNEL);
  5065. if (unlikely(!phba->vpi_bmask)) {
  5066. rc = -ENOMEM;
  5067. goto free_rpi_ids;
  5068. }
  5069. phba->vpi_ids = kzalloc(count *
  5070. sizeof(uint16_t),
  5071. GFP_KERNEL);
  5072. if (unlikely(!phba->vpi_ids)) {
  5073. rc = -ENOMEM;
  5074. goto free_vpi_bmask;
  5075. }
  5076. for (i = 0; i < count; i++)
  5077. phba->vpi_ids[i] = base + i;
  5078. /* XRIs. */
  5079. count = phba->sli4_hba.max_cfg_param.max_xri;
  5080. base = phba->sli4_hba.max_cfg_param.xri_base;
  5081. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5082. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5083. sizeof(unsigned long),
  5084. GFP_KERNEL);
  5085. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5086. rc = -ENOMEM;
  5087. goto free_vpi_ids;
  5088. }
  5089. phba->sli4_hba.xri_ids = kzalloc(count *
  5090. sizeof(uint16_t),
  5091. GFP_KERNEL);
  5092. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5093. rc = -ENOMEM;
  5094. goto free_xri_bmask;
  5095. }
  5096. for (i = 0; i < count; i++)
  5097. phba->sli4_hba.xri_ids[i] = base + i;
  5098. /* VFIs. */
  5099. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5100. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5101. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5102. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5103. sizeof(unsigned long),
  5104. GFP_KERNEL);
  5105. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5106. rc = -ENOMEM;
  5107. goto free_xri_ids;
  5108. }
  5109. phba->sli4_hba.vfi_ids = kzalloc(count *
  5110. sizeof(uint16_t),
  5111. GFP_KERNEL);
  5112. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5113. rc = -ENOMEM;
  5114. goto free_vfi_bmask;
  5115. }
  5116. for (i = 0; i < count; i++)
  5117. phba->sli4_hba.vfi_ids[i] = base + i;
  5118. /*
  5119. * Mark all resources ready. An HBA reset doesn't need
  5120. * to reset the initialization.
  5121. */
  5122. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5123. LPFC_IDX_RSRC_RDY);
  5124. return 0;
  5125. }
  5126. free_vfi_bmask:
  5127. kfree(phba->sli4_hba.vfi_bmask);
  5128. free_xri_ids:
  5129. kfree(phba->sli4_hba.xri_ids);
  5130. free_xri_bmask:
  5131. kfree(phba->sli4_hba.xri_bmask);
  5132. free_vpi_ids:
  5133. kfree(phba->vpi_ids);
  5134. free_vpi_bmask:
  5135. kfree(phba->vpi_bmask);
  5136. free_rpi_ids:
  5137. kfree(phba->sli4_hba.rpi_ids);
  5138. free_rpi_bmask:
  5139. kfree(phba->sli4_hba.rpi_bmask);
  5140. err_exit:
  5141. return rc;
  5142. }
  5143. /**
  5144. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5145. * @phba: Pointer to HBA context object.
  5146. *
  5147. * This function allocates the number of elements for the specified
  5148. * resource type.
  5149. **/
  5150. int
  5151. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5152. {
  5153. if (phba->sli4_hba.extents_in_use) {
  5154. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5155. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5156. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5157. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5158. } else {
  5159. kfree(phba->vpi_bmask);
  5160. kfree(phba->vpi_ids);
  5161. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5162. kfree(phba->sli4_hba.xri_bmask);
  5163. kfree(phba->sli4_hba.xri_ids);
  5164. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5165. kfree(phba->sli4_hba.vfi_bmask);
  5166. kfree(phba->sli4_hba.vfi_ids);
  5167. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5168. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5169. }
  5170. return 0;
  5171. }
  5172. /**
  5173. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5174. * @phba: Pointer to HBA context object.
  5175. * @type: The resource extent type.
  5176. * @extnt_count: buffer to hold port extent count response
  5177. * @extnt_size: buffer to hold port extent size response.
  5178. *
  5179. * This function calls the port to read the host allocated extents
  5180. * for a particular type.
  5181. **/
  5182. int
  5183. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5184. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5185. {
  5186. bool emb;
  5187. int rc = 0;
  5188. uint16_t curr_blks = 0;
  5189. uint32_t req_len, emb_len;
  5190. uint32_t alloc_len, mbox_tmo;
  5191. struct list_head *blk_list_head;
  5192. struct lpfc_rsrc_blks *rsrc_blk;
  5193. LPFC_MBOXQ_t *mbox;
  5194. void *virtaddr = NULL;
  5195. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5196. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5197. union lpfc_sli4_cfg_shdr *shdr;
  5198. switch (type) {
  5199. case LPFC_RSC_TYPE_FCOE_VPI:
  5200. blk_list_head = &phba->lpfc_vpi_blk_list;
  5201. break;
  5202. case LPFC_RSC_TYPE_FCOE_XRI:
  5203. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5204. break;
  5205. case LPFC_RSC_TYPE_FCOE_VFI:
  5206. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5207. break;
  5208. case LPFC_RSC_TYPE_FCOE_RPI:
  5209. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5210. break;
  5211. default:
  5212. return -EIO;
  5213. }
  5214. /* Count the number of extents currently allocatd for this type. */
  5215. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5216. if (curr_blks == 0) {
  5217. /*
  5218. * The GET_ALLOCATED mailbox does not return the size,
  5219. * just the count. The size should be just the size
  5220. * stored in the current allocated block and all sizes
  5221. * for an extent type are the same so set the return
  5222. * value now.
  5223. */
  5224. *extnt_size = rsrc_blk->rsrc_size;
  5225. }
  5226. curr_blks++;
  5227. }
  5228. /* Calculate the total requested length of the dma memory. */
  5229. req_len = curr_blks * sizeof(uint16_t);
  5230. /*
  5231. * Calculate the size of an embedded mailbox. The uint32_t
  5232. * accounts for extents-specific word.
  5233. */
  5234. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5235. sizeof(uint32_t);
  5236. /*
  5237. * Presume the allocation and response will fit into an embedded
  5238. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5239. */
  5240. emb = LPFC_SLI4_MBX_EMBED;
  5241. req_len = emb_len;
  5242. if (req_len > emb_len) {
  5243. req_len = curr_blks * sizeof(uint16_t) +
  5244. sizeof(union lpfc_sli4_cfg_shdr) +
  5245. sizeof(uint32_t);
  5246. emb = LPFC_SLI4_MBX_NEMBED;
  5247. }
  5248. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5249. if (!mbox)
  5250. return -ENOMEM;
  5251. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5252. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5253. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5254. req_len, emb);
  5255. if (alloc_len < req_len) {
  5256. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5257. "2983 Allocated DMA memory size (x%x) is "
  5258. "less than the requested DMA memory "
  5259. "size (x%x)\n", alloc_len, req_len);
  5260. rc = -ENOMEM;
  5261. goto err_exit;
  5262. }
  5263. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5264. if (unlikely(rc)) {
  5265. rc = -EIO;
  5266. goto err_exit;
  5267. }
  5268. if (!phba->sli4_hba.intr_enable)
  5269. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5270. else {
  5271. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5272. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5273. }
  5274. if (unlikely(rc)) {
  5275. rc = -EIO;
  5276. goto err_exit;
  5277. }
  5278. /*
  5279. * Figure out where the response is located. Then get local pointers
  5280. * to the response data. The port does not guarantee to respond to
  5281. * all extents counts request so update the local variable with the
  5282. * allocated count from the port.
  5283. */
  5284. if (emb == LPFC_SLI4_MBX_EMBED) {
  5285. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5286. shdr = &rsrc_ext->header.cfg_shdr;
  5287. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5288. } else {
  5289. virtaddr = mbox->sge_array->addr[0];
  5290. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5291. shdr = &n_rsrc->cfg_shdr;
  5292. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5293. }
  5294. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5295. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5296. "2984 Failed to read allocated resources "
  5297. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5298. type,
  5299. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5300. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5301. rc = -EIO;
  5302. goto err_exit;
  5303. }
  5304. err_exit:
  5305. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5306. return rc;
  5307. }
  5308. /**
  5309. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5310. * @phba: Pointer to HBA context object.
  5311. *
  5312. * This function is the main SLI4 device intialization PCI function. This
  5313. * function is called by the HBA intialization code, HBA reset code and
  5314. * HBA error attention handler code. Caller is not required to hold any
  5315. * locks.
  5316. **/
  5317. int
  5318. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5319. {
  5320. int rc;
  5321. LPFC_MBOXQ_t *mboxq;
  5322. struct lpfc_mqe *mqe;
  5323. uint8_t *vpd;
  5324. uint32_t vpd_size;
  5325. uint32_t ftr_rsp = 0;
  5326. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5327. struct lpfc_vport *vport = phba->pport;
  5328. struct lpfc_dmabuf *mp;
  5329. /* Perform a PCI function reset to start from clean */
  5330. rc = lpfc_pci_function_reset(phba);
  5331. if (unlikely(rc))
  5332. return -ENODEV;
  5333. /* Check the HBA Host Status Register for readyness */
  5334. rc = lpfc_sli4_post_status_check(phba);
  5335. if (unlikely(rc))
  5336. return -ENODEV;
  5337. else {
  5338. spin_lock_irq(&phba->hbalock);
  5339. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5340. spin_unlock_irq(&phba->hbalock);
  5341. }
  5342. /*
  5343. * Allocate a single mailbox container for initializing the
  5344. * port.
  5345. */
  5346. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5347. if (!mboxq)
  5348. return -ENOMEM;
  5349. /*
  5350. * Continue initialization with default values even if driver failed
  5351. * to read FCoE param config regions
  5352. */
  5353. if (lpfc_sli4_read_fcoe_params(phba, mboxq))
  5354. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5355. "2570 Failed to read FCoE parameters\n");
  5356. /* Issue READ_REV to collect vpd and FW information. */
  5357. vpd_size = SLI4_PAGE_SIZE;
  5358. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5359. if (!vpd) {
  5360. rc = -ENOMEM;
  5361. goto out_free_mbox;
  5362. }
  5363. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5364. if (unlikely(rc)) {
  5365. kfree(vpd);
  5366. goto out_free_mbox;
  5367. }
  5368. mqe = &mboxq->u.mqe;
  5369. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5370. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5371. phba->hba_flag |= HBA_FCOE_MODE;
  5372. else
  5373. phba->hba_flag &= ~HBA_FCOE_MODE;
  5374. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5375. LPFC_DCBX_CEE_MODE)
  5376. phba->hba_flag |= HBA_FIP_SUPPORT;
  5377. else
  5378. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5379. if (phba->sli_rev != LPFC_SLI_REV4) {
  5380. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5381. "0376 READ_REV Error. SLI Level %d "
  5382. "FCoE enabled %d\n",
  5383. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5384. rc = -EIO;
  5385. kfree(vpd);
  5386. goto out_free_mbox;
  5387. }
  5388. /*
  5389. * Retrieve sli4 device physical port name, failure of doing it
  5390. * is considered as non-fatal.
  5391. */
  5392. rc = lpfc_sli4_retrieve_pport_name(phba);
  5393. if (!rc)
  5394. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5395. "3080 Successful retrieving SLI4 device "
  5396. "physical port name: %s.\n", phba->Port);
  5397. /*
  5398. * Evaluate the read rev and vpd data. Populate the driver
  5399. * state with the results. If this routine fails, the failure
  5400. * is not fatal as the driver will use generic values.
  5401. */
  5402. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5403. if (unlikely(!rc)) {
  5404. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5405. "0377 Error %d parsing vpd. "
  5406. "Using defaults.\n", rc);
  5407. rc = 0;
  5408. }
  5409. kfree(vpd);
  5410. /* Save information as VPD data */
  5411. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5412. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5413. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5414. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5415. &mqe->un.read_rev);
  5416. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5417. &mqe->un.read_rev);
  5418. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5419. &mqe->un.read_rev);
  5420. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5421. &mqe->un.read_rev);
  5422. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5423. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5424. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5425. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5426. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5427. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5428. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5429. "(%d):0380 READ_REV Status x%x "
  5430. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5431. mboxq->vport ? mboxq->vport->vpi : 0,
  5432. bf_get(lpfc_mqe_status, mqe),
  5433. phba->vpd.rev.opFwName,
  5434. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5435. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5436. /*
  5437. * Discover the port's supported feature set and match it against the
  5438. * hosts requests.
  5439. */
  5440. lpfc_request_features(phba, mboxq);
  5441. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5442. if (unlikely(rc)) {
  5443. rc = -EIO;
  5444. goto out_free_mbox;
  5445. }
  5446. /*
  5447. * The port must support FCP initiator mode as this is the
  5448. * only mode running in the host.
  5449. */
  5450. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5451. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5452. "0378 No support for fcpi mode.\n");
  5453. ftr_rsp++;
  5454. }
  5455. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5456. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5457. else
  5458. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5459. /*
  5460. * If the port cannot support the host's requested features
  5461. * then turn off the global config parameters to disable the
  5462. * feature in the driver. This is not a fatal error.
  5463. */
  5464. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5465. if (phba->cfg_enable_bg) {
  5466. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5467. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5468. else
  5469. ftr_rsp++;
  5470. }
  5471. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5472. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5473. ftr_rsp++;
  5474. if (ftr_rsp) {
  5475. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5476. "0379 Feature Mismatch Data: x%08x %08x "
  5477. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5478. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5479. phba->cfg_enable_npiv, phba->max_vpi);
  5480. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5481. phba->cfg_enable_bg = 0;
  5482. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5483. phba->cfg_enable_npiv = 0;
  5484. }
  5485. /* These SLI3 features are assumed in SLI4 */
  5486. spin_lock_irq(&phba->hbalock);
  5487. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5488. spin_unlock_irq(&phba->hbalock);
  5489. /*
  5490. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5491. * calls depends on these resources to complete port setup.
  5492. */
  5493. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5494. if (rc) {
  5495. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5496. "2920 Failed to alloc Resource IDs "
  5497. "rc = x%x\n", rc);
  5498. goto out_free_mbox;
  5499. }
  5500. /* Read the port's service parameters. */
  5501. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5502. if (rc) {
  5503. phba->link_state = LPFC_HBA_ERROR;
  5504. rc = -ENOMEM;
  5505. goto out_free_mbox;
  5506. }
  5507. mboxq->vport = vport;
  5508. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5509. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5510. if (rc == MBX_SUCCESS) {
  5511. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5512. rc = 0;
  5513. }
  5514. /*
  5515. * This memory was allocated by the lpfc_read_sparam routine. Release
  5516. * it to the mbuf pool.
  5517. */
  5518. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5519. kfree(mp);
  5520. mboxq->context1 = NULL;
  5521. if (unlikely(rc)) {
  5522. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5523. "0382 READ_SPARAM command failed "
  5524. "status %d, mbxStatus x%x\n",
  5525. rc, bf_get(lpfc_mqe_status, mqe));
  5526. phba->link_state = LPFC_HBA_ERROR;
  5527. rc = -EIO;
  5528. goto out_free_mbox;
  5529. }
  5530. lpfc_update_vport_wwn(vport);
  5531. /* Update the fc_host data structures with new wwn. */
  5532. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5533. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5534. /* Register SGL pool to the device using non-embedded mailbox command */
  5535. if (!phba->sli4_hba.extents_in_use) {
  5536. rc = lpfc_sli4_post_els_sgl_list(phba);
  5537. if (unlikely(rc)) {
  5538. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5539. "0582 Error %d during els sgl post "
  5540. "operation\n", rc);
  5541. rc = -ENODEV;
  5542. goto out_free_mbox;
  5543. }
  5544. } else {
  5545. rc = lpfc_sli4_post_els_sgl_list_ext(phba);
  5546. if (unlikely(rc)) {
  5547. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5548. "2560 Error %d during els sgl post "
  5549. "operation\n", rc);
  5550. rc = -ENODEV;
  5551. goto out_free_mbox;
  5552. }
  5553. }
  5554. /* Register SCSI SGL pool to the device */
  5555. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5556. if (unlikely(rc)) {
  5557. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5558. "0383 Error %d during scsi sgl post "
  5559. "operation\n", rc);
  5560. /* Some Scsi buffers were moved to the abort scsi list */
  5561. /* A pci function reset will repost them */
  5562. rc = -ENODEV;
  5563. goto out_free_mbox;
  5564. }
  5565. /* Post the rpi header region to the device. */
  5566. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5567. if (unlikely(rc)) {
  5568. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5569. "0393 Error %d during rpi post operation\n",
  5570. rc);
  5571. rc = -ENODEV;
  5572. goto out_free_mbox;
  5573. }
  5574. /* Create all the SLI4 queues */
  5575. rc = lpfc_sli4_queue_create(phba);
  5576. if (rc) {
  5577. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5578. "3089 Failed to allocate queues\n");
  5579. rc = -ENODEV;
  5580. goto out_stop_timers;
  5581. }
  5582. /* Set up all the queues to the device */
  5583. rc = lpfc_sli4_queue_setup(phba);
  5584. if (unlikely(rc)) {
  5585. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5586. "0381 Error %d during queue setup.\n ", rc);
  5587. goto out_destroy_queue;
  5588. }
  5589. /* Arm the CQs and then EQs on device */
  5590. lpfc_sli4_arm_cqeq_intr(phba);
  5591. /* Indicate device interrupt mode */
  5592. phba->sli4_hba.intr_enable = 1;
  5593. /* Allow asynchronous mailbox command to go through */
  5594. spin_lock_irq(&phba->hbalock);
  5595. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5596. spin_unlock_irq(&phba->hbalock);
  5597. /* Post receive buffers to the device */
  5598. lpfc_sli4_rb_setup(phba);
  5599. /* Reset HBA FCF states after HBA reset */
  5600. phba->fcf.fcf_flag = 0;
  5601. phba->fcf.current_rec.flag = 0;
  5602. /* Start the ELS watchdog timer */
  5603. mod_timer(&vport->els_tmofunc,
  5604. jiffies + HZ * (phba->fc_ratov * 2));
  5605. /* Start heart beat timer */
  5606. mod_timer(&phba->hb_tmofunc,
  5607. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5608. phba->hb_outstanding = 0;
  5609. phba->last_completion_time = jiffies;
  5610. /* Start error attention (ERATT) polling timer */
  5611. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5612. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5613. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5614. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5615. if (!rc) {
  5616. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5617. "2829 This device supports "
  5618. "Advanced Error Reporting (AER)\n");
  5619. spin_lock_irq(&phba->hbalock);
  5620. phba->hba_flag |= HBA_AER_ENABLED;
  5621. spin_unlock_irq(&phba->hbalock);
  5622. } else {
  5623. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5624. "2830 This device does not support "
  5625. "Advanced Error Reporting (AER)\n");
  5626. phba->cfg_aer_support = 0;
  5627. }
  5628. rc = 0;
  5629. }
  5630. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5631. /*
  5632. * The FC Port needs to register FCFI (index 0)
  5633. */
  5634. lpfc_reg_fcfi(phba, mboxq);
  5635. mboxq->vport = phba->pport;
  5636. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5637. if (rc != MBX_SUCCESS)
  5638. goto out_unset_queue;
  5639. rc = 0;
  5640. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5641. &mboxq->u.mqe.un.reg_fcfi);
  5642. }
  5643. /*
  5644. * The port is ready, set the host's link state to LINK_DOWN
  5645. * in preparation for link interrupts.
  5646. */
  5647. spin_lock_irq(&phba->hbalock);
  5648. phba->link_state = LPFC_LINK_DOWN;
  5649. spin_unlock_irq(&phba->hbalock);
  5650. if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5651. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5652. if (rc)
  5653. goto out_unset_queue;
  5654. }
  5655. mempool_free(mboxq, phba->mbox_mem_pool);
  5656. return rc;
  5657. out_unset_queue:
  5658. /* Unset all the queues set up in this routine when error out */
  5659. lpfc_sli4_queue_unset(phba);
  5660. out_destroy_queue:
  5661. lpfc_sli4_queue_destroy(phba);
  5662. out_stop_timers:
  5663. lpfc_stop_hba_timers(phba);
  5664. out_free_mbox:
  5665. mempool_free(mboxq, phba->mbox_mem_pool);
  5666. return rc;
  5667. }
  5668. /**
  5669. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5670. * @ptr: context object - pointer to hba structure.
  5671. *
  5672. * This is the callback function for mailbox timer. The mailbox
  5673. * timer is armed when a new mailbox command is issued and the timer
  5674. * is deleted when the mailbox complete. The function is called by
  5675. * the kernel timer code when a mailbox does not complete within
  5676. * expected time. This function wakes up the worker thread to
  5677. * process the mailbox timeout and returns. All the processing is
  5678. * done by the worker thread function lpfc_mbox_timeout_handler.
  5679. **/
  5680. void
  5681. lpfc_mbox_timeout(unsigned long ptr)
  5682. {
  5683. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5684. unsigned long iflag;
  5685. uint32_t tmo_posted;
  5686. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5687. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5688. if (!tmo_posted)
  5689. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5690. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5691. if (!tmo_posted)
  5692. lpfc_worker_wake_up(phba);
  5693. return;
  5694. }
  5695. /**
  5696. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5697. * @phba: Pointer to HBA context object.
  5698. *
  5699. * This function is called from worker thread when a mailbox command times out.
  5700. * The caller is not required to hold any locks. This function will reset the
  5701. * HBA and recover all the pending commands.
  5702. **/
  5703. void
  5704. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5705. {
  5706. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5707. MAILBOX_t *mb = &pmbox->u.mb;
  5708. struct lpfc_sli *psli = &phba->sli;
  5709. struct lpfc_sli_ring *pring;
  5710. /* Check the pmbox pointer first. There is a race condition
  5711. * between the mbox timeout handler getting executed in the
  5712. * worklist and the mailbox actually completing. When this
  5713. * race condition occurs, the mbox_active will be NULL.
  5714. */
  5715. spin_lock_irq(&phba->hbalock);
  5716. if (pmbox == NULL) {
  5717. lpfc_printf_log(phba, KERN_WARNING,
  5718. LOG_MBOX | LOG_SLI,
  5719. "0353 Active Mailbox cleared - mailbox timeout "
  5720. "exiting\n");
  5721. spin_unlock_irq(&phba->hbalock);
  5722. return;
  5723. }
  5724. /* Mbox cmd <mbxCommand> timeout */
  5725. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5726. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5727. mb->mbxCommand,
  5728. phba->pport->port_state,
  5729. phba->sli.sli_flag,
  5730. phba->sli.mbox_active);
  5731. spin_unlock_irq(&phba->hbalock);
  5732. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5733. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5734. * it to fail all outstanding SCSI IO.
  5735. */
  5736. spin_lock_irq(&phba->pport->work_port_lock);
  5737. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5738. spin_unlock_irq(&phba->pport->work_port_lock);
  5739. spin_lock_irq(&phba->hbalock);
  5740. phba->link_state = LPFC_LINK_UNKNOWN;
  5741. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5742. spin_unlock_irq(&phba->hbalock);
  5743. pring = &psli->ring[psli->fcp_ring];
  5744. lpfc_sli_abort_iocb_ring(phba, pring);
  5745. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5746. "0345 Resetting board due to mailbox timeout\n");
  5747. /* Reset the HBA device */
  5748. lpfc_reset_hba(phba);
  5749. }
  5750. /**
  5751. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5752. * @phba: Pointer to HBA context object.
  5753. * @pmbox: Pointer to mailbox object.
  5754. * @flag: Flag indicating how the mailbox need to be processed.
  5755. *
  5756. * This function is called by discovery code and HBA management code
  5757. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5758. * function gets the hbalock to protect the data structures.
  5759. * The mailbox command can be submitted in polling mode, in which case
  5760. * this function will wait in a polling loop for the completion of the
  5761. * mailbox.
  5762. * If the mailbox is submitted in no_wait mode (not polling) the
  5763. * function will submit the command and returns immediately without waiting
  5764. * for the mailbox completion. The no_wait is supported only when HBA
  5765. * is in SLI2/SLI3 mode - interrupts are enabled.
  5766. * The SLI interface allows only one mailbox pending at a time. If the
  5767. * mailbox is issued in polling mode and there is already a mailbox
  5768. * pending, then the function will return an error. If the mailbox is issued
  5769. * in NO_WAIT mode and there is a mailbox pending already, the function
  5770. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5771. * The sli layer owns the mailbox object until the completion of mailbox
  5772. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5773. * return codes the caller owns the mailbox command after the return of
  5774. * the function.
  5775. **/
  5776. static int
  5777. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5778. uint32_t flag)
  5779. {
  5780. MAILBOX_t *mb;
  5781. struct lpfc_sli *psli = &phba->sli;
  5782. uint32_t status, evtctr;
  5783. uint32_t ha_copy, hc_copy;
  5784. int i;
  5785. unsigned long timeout;
  5786. unsigned long drvr_flag = 0;
  5787. uint32_t word0, ldata;
  5788. void __iomem *to_slim;
  5789. int processing_queue = 0;
  5790. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5791. if (!pmbox) {
  5792. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5793. /* processing mbox queue from intr_handler */
  5794. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5795. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5796. return MBX_SUCCESS;
  5797. }
  5798. processing_queue = 1;
  5799. pmbox = lpfc_mbox_get(phba);
  5800. if (!pmbox) {
  5801. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5802. return MBX_SUCCESS;
  5803. }
  5804. }
  5805. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5806. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5807. if(!pmbox->vport) {
  5808. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5809. lpfc_printf_log(phba, KERN_ERR,
  5810. LOG_MBOX | LOG_VPORT,
  5811. "1806 Mbox x%x failed. No vport\n",
  5812. pmbox->u.mb.mbxCommand);
  5813. dump_stack();
  5814. goto out_not_finished;
  5815. }
  5816. }
  5817. /* If the PCI channel is in offline state, do not post mbox. */
  5818. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5819. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5820. goto out_not_finished;
  5821. }
  5822. /* If HBA has a deferred error attention, fail the iocb. */
  5823. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5824. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5825. goto out_not_finished;
  5826. }
  5827. psli = &phba->sli;
  5828. mb = &pmbox->u.mb;
  5829. status = MBX_SUCCESS;
  5830. if (phba->link_state == LPFC_HBA_ERROR) {
  5831. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5832. /* Mbox command <mbxCommand> cannot issue */
  5833. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5834. "(%d):0311 Mailbox command x%x cannot "
  5835. "issue Data: x%x x%x\n",
  5836. pmbox->vport ? pmbox->vport->vpi : 0,
  5837. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5838. goto out_not_finished;
  5839. }
  5840. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  5841. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  5842. !(hc_copy & HC_MBINT_ENA)) {
  5843. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5844. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5845. "(%d):2528 Mailbox command x%x cannot "
  5846. "issue Data: x%x x%x\n",
  5847. pmbox->vport ? pmbox->vport->vpi : 0,
  5848. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  5849. goto out_not_finished;
  5850. }
  5851. }
  5852. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  5853. /* Polling for a mbox command when another one is already active
  5854. * is not allowed in SLI. Also, the driver must have established
  5855. * SLI2 mode to queue and process multiple mbox commands.
  5856. */
  5857. if (flag & MBX_POLL) {
  5858. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5859. /* Mbox command <mbxCommand> cannot issue */
  5860. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5861. "(%d):2529 Mailbox command x%x "
  5862. "cannot issue Data: x%x x%x\n",
  5863. pmbox->vport ? pmbox->vport->vpi : 0,
  5864. pmbox->u.mb.mbxCommand,
  5865. psli->sli_flag, flag);
  5866. goto out_not_finished;
  5867. }
  5868. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  5869. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5870. /* Mbox command <mbxCommand> cannot issue */
  5871. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5872. "(%d):2530 Mailbox command x%x "
  5873. "cannot issue Data: x%x x%x\n",
  5874. pmbox->vport ? pmbox->vport->vpi : 0,
  5875. pmbox->u.mb.mbxCommand,
  5876. psli->sli_flag, flag);
  5877. goto out_not_finished;
  5878. }
  5879. /* Another mailbox command is still being processed, queue this
  5880. * command to be processed later.
  5881. */
  5882. lpfc_mbox_put(phba, pmbox);
  5883. /* Mbox cmd issue - BUSY */
  5884. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5885. "(%d):0308 Mbox cmd issue - BUSY Data: "
  5886. "x%x x%x x%x x%x\n",
  5887. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  5888. mb->mbxCommand, phba->pport->port_state,
  5889. psli->sli_flag, flag);
  5890. psli->slistat.mbox_busy++;
  5891. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5892. if (pmbox->vport) {
  5893. lpfc_debugfs_disc_trc(pmbox->vport,
  5894. LPFC_DISC_TRC_MBOX_VPORT,
  5895. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  5896. (uint32_t)mb->mbxCommand,
  5897. mb->un.varWords[0], mb->un.varWords[1]);
  5898. }
  5899. else {
  5900. lpfc_debugfs_disc_trc(phba->pport,
  5901. LPFC_DISC_TRC_MBOX,
  5902. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  5903. (uint32_t)mb->mbxCommand,
  5904. mb->un.varWords[0], mb->un.varWords[1]);
  5905. }
  5906. return MBX_BUSY;
  5907. }
  5908. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  5909. /* If we are not polling, we MUST be in SLI2 mode */
  5910. if (flag != MBX_POLL) {
  5911. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  5912. (mb->mbxCommand != MBX_KILL_BOARD)) {
  5913. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5914. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5915. /* Mbox command <mbxCommand> cannot issue */
  5916. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5917. "(%d):2531 Mailbox command x%x "
  5918. "cannot issue Data: x%x x%x\n",
  5919. pmbox->vport ? pmbox->vport->vpi : 0,
  5920. pmbox->u.mb.mbxCommand,
  5921. psli->sli_flag, flag);
  5922. goto out_not_finished;
  5923. }
  5924. /* timeout active mbox command */
  5925. mod_timer(&psli->mbox_tmo, (jiffies +
  5926. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  5927. }
  5928. /* Mailbox cmd <cmd> issue */
  5929. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5930. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  5931. "x%x\n",
  5932. pmbox->vport ? pmbox->vport->vpi : 0,
  5933. mb->mbxCommand, phba->pport->port_state,
  5934. psli->sli_flag, flag);
  5935. if (mb->mbxCommand != MBX_HEARTBEAT) {
  5936. if (pmbox->vport) {
  5937. lpfc_debugfs_disc_trc(pmbox->vport,
  5938. LPFC_DISC_TRC_MBOX_VPORT,
  5939. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  5940. (uint32_t)mb->mbxCommand,
  5941. mb->un.varWords[0], mb->un.varWords[1]);
  5942. }
  5943. else {
  5944. lpfc_debugfs_disc_trc(phba->pport,
  5945. LPFC_DISC_TRC_MBOX,
  5946. "MBOX Send: cmd:x%x mb:x%x x%x",
  5947. (uint32_t)mb->mbxCommand,
  5948. mb->un.varWords[0], mb->un.varWords[1]);
  5949. }
  5950. }
  5951. psli->slistat.mbox_cmd++;
  5952. evtctr = psli->slistat.mbox_event;
  5953. /* next set own bit for the adapter and copy over command word */
  5954. mb->mbxOwner = OWN_CHIP;
  5955. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  5956. /* Populate mbox extension offset word. */
  5957. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  5958. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  5959. = (uint8_t *)phba->mbox_ext
  5960. - (uint8_t *)phba->mbox;
  5961. }
  5962. /* Copy the mailbox extension data */
  5963. if (pmbox->in_ext_byte_len && pmbox->context2) {
  5964. lpfc_sli_pcimem_bcopy(pmbox->context2,
  5965. (uint8_t *)phba->mbox_ext,
  5966. pmbox->in_ext_byte_len);
  5967. }
  5968. /* Copy command data to host SLIM area */
  5969. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  5970. } else {
  5971. /* Populate mbox extension offset word. */
  5972. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  5973. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  5974. = MAILBOX_HBA_EXT_OFFSET;
  5975. /* Copy the mailbox extension data */
  5976. if (pmbox->in_ext_byte_len && pmbox->context2) {
  5977. lpfc_memcpy_to_slim(phba->MBslimaddr +
  5978. MAILBOX_HBA_EXT_OFFSET,
  5979. pmbox->context2, pmbox->in_ext_byte_len);
  5980. }
  5981. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  5982. /* copy command data into host mbox for cmpl */
  5983. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  5984. }
  5985. /* First copy mbox command data to HBA SLIM, skip past first
  5986. word */
  5987. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  5988. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  5989. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  5990. /* Next copy over first word, with mbxOwner set */
  5991. ldata = *((uint32_t *)mb);
  5992. to_slim = phba->MBslimaddr;
  5993. writel(ldata, to_slim);
  5994. readl(to_slim); /* flush */
  5995. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  5996. /* switch over to host mailbox */
  5997. psli->sli_flag |= LPFC_SLI_ACTIVE;
  5998. }
  5999. }
  6000. wmb();
  6001. switch (flag) {
  6002. case MBX_NOWAIT:
  6003. /* Set up reference to mailbox command */
  6004. psli->mbox_active = pmbox;
  6005. /* Interrupt board to do it */
  6006. writel(CA_MBATT, phba->CAregaddr);
  6007. readl(phba->CAregaddr); /* flush */
  6008. /* Don't wait for it to finish, just return */
  6009. break;
  6010. case MBX_POLL:
  6011. /* Set up null reference to mailbox command */
  6012. psli->mbox_active = NULL;
  6013. /* Interrupt board to do it */
  6014. writel(CA_MBATT, phba->CAregaddr);
  6015. readl(phba->CAregaddr); /* flush */
  6016. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6017. /* First read mbox status word */
  6018. word0 = *((uint32_t *)phba->mbox);
  6019. word0 = le32_to_cpu(word0);
  6020. } else {
  6021. /* First read mbox status word */
  6022. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6023. spin_unlock_irqrestore(&phba->hbalock,
  6024. drvr_flag);
  6025. goto out_not_finished;
  6026. }
  6027. }
  6028. /* Read the HBA Host Attention Register */
  6029. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6030. spin_unlock_irqrestore(&phba->hbalock,
  6031. drvr_flag);
  6032. goto out_not_finished;
  6033. }
  6034. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6035. 1000) + jiffies;
  6036. i = 0;
  6037. /* Wait for command to complete */
  6038. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6039. (!(ha_copy & HA_MBATT) &&
  6040. (phba->link_state > LPFC_WARM_START))) {
  6041. if (time_after(jiffies, timeout)) {
  6042. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6043. spin_unlock_irqrestore(&phba->hbalock,
  6044. drvr_flag);
  6045. goto out_not_finished;
  6046. }
  6047. /* Check if we took a mbox interrupt while we were
  6048. polling */
  6049. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6050. && (evtctr != psli->slistat.mbox_event))
  6051. break;
  6052. if (i++ > 10) {
  6053. spin_unlock_irqrestore(&phba->hbalock,
  6054. drvr_flag);
  6055. msleep(1);
  6056. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6057. }
  6058. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6059. /* First copy command data */
  6060. word0 = *((uint32_t *)phba->mbox);
  6061. word0 = le32_to_cpu(word0);
  6062. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6063. MAILBOX_t *slimmb;
  6064. uint32_t slimword0;
  6065. /* Check real SLIM for any errors */
  6066. slimword0 = readl(phba->MBslimaddr);
  6067. slimmb = (MAILBOX_t *) & slimword0;
  6068. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6069. && slimmb->mbxStatus) {
  6070. psli->sli_flag &=
  6071. ~LPFC_SLI_ACTIVE;
  6072. word0 = slimword0;
  6073. }
  6074. }
  6075. } else {
  6076. /* First copy command data */
  6077. word0 = readl(phba->MBslimaddr);
  6078. }
  6079. /* Read the HBA Host Attention Register */
  6080. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6081. spin_unlock_irqrestore(&phba->hbalock,
  6082. drvr_flag);
  6083. goto out_not_finished;
  6084. }
  6085. }
  6086. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6087. /* copy results back to user */
  6088. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6089. /* Copy the mailbox extension data */
  6090. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6091. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6092. pmbox->context2,
  6093. pmbox->out_ext_byte_len);
  6094. }
  6095. } else {
  6096. /* First copy command data */
  6097. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6098. MAILBOX_CMD_SIZE);
  6099. /* Copy the mailbox extension data */
  6100. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6101. lpfc_memcpy_from_slim(pmbox->context2,
  6102. phba->MBslimaddr +
  6103. MAILBOX_HBA_EXT_OFFSET,
  6104. pmbox->out_ext_byte_len);
  6105. }
  6106. }
  6107. writel(HA_MBATT, phba->HAregaddr);
  6108. readl(phba->HAregaddr); /* flush */
  6109. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6110. status = mb->mbxStatus;
  6111. }
  6112. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6113. return status;
  6114. out_not_finished:
  6115. if (processing_queue) {
  6116. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6117. lpfc_mbox_cmpl_put(phba, pmbox);
  6118. }
  6119. return MBX_NOT_FINISHED;
  6120. }
  6121. /**
  6122. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6123. * @phba: Pointer to HBA context object.
  6124. *
  6125. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6126. * the driver internal pending mailbox queue. It will then try to wait out the
  6127. * possible outstanding mailbox command before return.
  6128. *
  6129. * Returns:
  6130. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6131. * the outstanding mailbox command timed out.
  6132. **/
  6133. static int
  6134. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6135. {
  6136. struct lpfc_sli *psli = &phba->sli;
  6137. int rc = 0;
  6138. unsigned long timeout = 0;
  6139. /* Mark the asynchronous mailbox command posting as blocked */
  6140. spin_lock_irq(&phba->hbalock);
  6141. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6142. /* Determine how long we might wait for the active mailbox
  6143. * command to be gracefully completed by firmware.
  6144. */
  6145. if (phba->sli.mbox_active)
  6146. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6147. phba->sli.mbox_active) *
  6148. 1000) + jiffies;
  6149. spin_unlock_irq(&phba->hbalock);
  6150. /* Wait for the outstnading mailbox command to complete */
  6151. while (phba->sli.mbox_active) {
  6152. /* Check active mailbox complete status every 2ms */
  6153. msleep(2);
  6154. if (time_after(jiffies, timeout)) {
  6155. /* Timeout, marked the outstanding cmd not complete */
  6156. rc = 1;
  6157. break;
  6158. }
  6159. }
  6160. /* Can not cleanly block async mailbox command, fails it */
  6161. if (rc) {
  6162. spin_lock_irq(&phba->hbalock);
  6163. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6164. spin_unlock_irq(&phba->hbalock);
  6165. }
  6166. return rc;
  6167. }
  6168. /**
  6169. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6170. * @phba: Pointer to HBA context object.
  6171. *
  6172. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6173. * commands from the driver internal pending mailbox queue. It makes sure
  6174. * that there is no outstanding mailbox command before resuming posting
  6175. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6176. * mailbox command, it will try to wait it out before resuming asynchronous
  6177. * mailbox command posting.
  6178. **/
  6179. static void
  6180. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6181. {
  6182. struct lpfc_sli *psli = &phba->sli;
  6183. spin_lock_irq(&phba->hbalock);
  6184. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6185. /* Asynchronous mailbox posting is not blocked, do nothing */
  6186. spin_unlock_irq(&phba->hbalock);
  6187. return;
  6188. }
  6189. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6190. * successful or timeout, after timing-out the outstanding mailbox
  6191. * command shall always be removed, so just unblock posting async
  6192. * mailbox command and resume
  6193. */
  6194. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6195. spin_unlock_irq(&phba->hbalock);
  6196. /* wake up worker thread to post asynchronlous mailbox command */
  6197. lpfc_worker_wake_up(phba);
  6198. }
  6199. /**
  6200. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6201. * @phba: Pointer to HBA context object.
  6202. * @mboxq: Pointer to mailbox object.
  6203. *
  6204. * The function posts a mailbox to the port. The mailbox is expected
  6205. * to be comletely filled in and ready for the port to operate on it.
  6206. * This routine executes a synchronous completion operation on the
  6207. * mailbox by polling for its completion.
  6208. *
  6209. * The caller must not be holding any locks when calling this routine.
  6210. *
  6211. * Returns:
  6212. * MBX_SUCCESS - mailbox posted successfully
  6213. * Any of the MBX error values.
  6214. **/
  6215. static int
  6216. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6217. {
  6218. int rc = MBX_SUCCESS;
  6219. unsigned long iflag;
  6220. uint32_t db_ready;
  6221. uint32_t mcqe_status;
  6222. uint32_t mbx_cmnd;
  6223. unsigned long timeout;
  6224. struct lpfc_sli *psli = &phba->sli;
  6225. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6226. struct lpfc_bmbx_create *mbox_rgn;
  6227. struct dma_address *dma_address;
  6228. struct lpfc_register bmbx_reg;
  6229. /*
  6230. * Only one mailbox can be active to the bootstrap mailbox region
  6231. * at a time and there is no queueing provided.
  6232. */
  6233. spin_lock_irqsave(&phba->hbalock, iflag);
  6234. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6235. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6236. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6237. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6238. "cannot issue Data: x%x x%x\n",
  6239. mboxq->vport ? mboxq->vport->vpi : 0,
  6240. mboxq->u.mb.mbxCommand,
  6241. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6242. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6243. psli->sli_flag, MBX_POLL);
  6244. return MBXERR_ERROR;
  6245. }
  6246. /* The server grabs the token and owns it until release */
  6247. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6248. phba->sli.mbox_active = mboxq;
  6249. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6250. /*
  6251. * Initialize the bootstrap memory region to avoid stale data areas
  6252. * in the mailbox post. Then copy the caller's mailbox contents to
  6253. * the bmbx mailbox region.
  6254. */
  6255. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6256. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6257. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6258. sizeof(struct lpfc_mqe));
  6259. /* Post the high mailbox dma address to the port and wait for ready. */
  6260. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6261. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6262. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6263. * 1000) + jiffies;
  6264. do {
  6265. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6266. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6267. if (!db_ready)
  6268. msleep(2);
  6269. if (time_after(jiffies, timeout)) {
  6270. rc = MBXERR_ERROR;
  6271. goto exit;
  6272. }
  6273. } while (!db_ready);
  6274. /* Post the low mailbox dma address to the port. */
  6275. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6276. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6277. * 1000) + jiffies;
  6278. do {
  6279. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6280. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6281. if (!db_ready)
  6282. msleep(2);
  6283. if (time_after(jiffies, timeout)) {
  6284. rc = MBXERR_ERROR;
  6285. goto exit;
  6286. }
  6287. } while (!db_ready);
  6288. /*
  6289. * Read the CQ to ensure the mailbox has completed.
  6290. * If so, update the mailbox status so that the upper layers
  6291. * can complete the request normally.
  6292. */
  6293. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6294. sizeof(struct lpfc_mqe));
  6295. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6296. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6297. sizeof(struct lpfc_mcqe));
  6298. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6299. /*
  6300. * When the CQE status indicates a failure and the mailbox status
  6301. * indicates success then copy the CQE status into the mailbox status
  6302. * (and prefix it with x4000).
  6303. */
  6304. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6305. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6306. bf_set(lpfc_mqe_status, mb,
  6307. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6308. rc = MBXERR_ERROR;
  6309. } else
  6310. lpfc_sli4_swap_str(phba, mboxq);
  6311. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6312. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6313. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6314. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6315. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6316. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6317. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6318. bf_get(lpfc_mqe_status, mb),
  6319. mb->un.mb_words[0], mb->un.mb_words[1],
  6320. mb->un.mb_words[2], mb->un.mb_words[3],
  6321. mb->un.mb_words[4], mb->un.mb_words[5],
  6322. mb->un.mb_words[6], mb->un.mb_words[7],
  6323. mb->un.mb_words[8], mb->un.mb_words[9],
  6324. mb->un.mb_words[10], mb->un.mb_words[11],
  6325. mb->un.mb_words[12], mboxq->mcqe.word0,
  6326. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6327. mboxq->mcqe.trailer);
  6328. exit:
  6329. /* We are holding the token, no needed for lock when release */
  6330. spin_lock_irqsave(&phba->hbalock, iflag);
  6331. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6332. phba->sli.mbox_active = NULL;
  6333. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6334. return rc;
  6335. }
  6336. /**
  6337. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6338. * @phba: Pointer to HBA context object.
  6339. * @pmbox: Pointer to mailbox object.
  6340. * @flag: Flag indicating how the mailbox need to be processed.
  6341. *
  6342. * This function is called by discovery code and HBA management code to submit
  6343. * a mailbox command to firmware with SLI-4 interface spec.
  6344. *
  6345. * Return codes the caller owns the mailbox command after the return of the
  6346. * function.
  6347. **/
  6348. static int
  6349. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6350. uint32_t flag)
  6351. {
  6352. struct lpfc_sli *psli = &phba->sli;
  6353. unsigned long iflags;
  6354. int rc;
  6355. /* dump from issue mailbox command if setup */
  6356. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6357. rc = lpfc_mbox_dev_check(phba);
  6358. if (unlikely(rc)) {
  6359. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6360. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6361. "cannot issue Data: x%x x%x\n",
  6362. mboxq->vport ? mboxq->vport->vpi : 0,
  6363. mboxq->u.mb.mbxCommand,
  6364. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6365. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6366. psli->sli_flag, flag);
  6367. goto out_not_finished;
  6368. }
  6369. /* Detect polling mode and jump to a handler */
  6370. if (!phba->sli4_hba.intr_enable) {
  6371. if (flag == MBX_POLL)
  6372. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6373. else
  6374. rc = -EIO;
  6375. if (rc != MBX_SUCCESS)
  6376. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6377. "(%d):2541 Mailbox command x%x "
  6378. "(x%x/x%x) cannot issue Data: "
  6379. "x%x x%x\n",
  6380. mboxq->vport ? mboxq->vport->vpi : 0,
  6381. mboxq->u.mb.mbxCommand,
  6382. lpfc_sli_config_mbox_subsys_get(phba,
  6383. mboxq),
  6384. lpfc_sli_config_mbox_opcode_get(phba,
  6385. mboxq),
  6386. psli->sli_flag, flag);
  6387. return rc;
  6388. } else if (flag == MBX_POLL) {
  6389. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6390. "(%d):2542 Try to issue mailbox command "
  6391. "x%x (x%x/x%x) synchronously ahead of async"
  6392. "mailbox command queue: x%x x%x\n",
  6393. mboxq->vport ? mboxq->vport->vpi : 0,
  6394. mboxq->u.mb.mbxCommand,
  6395. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6396. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6397. psli->sli_flag, flag);
  6398. /* Try to block the asynchronous mailbox posting */
  6399. rc = lpfc_sli4_async_mbox_block(phba);
  6400. if (!rc) {
  6401. /* Successfully blocked, now issue sync mbox cmd */
  6402. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6403. if (rc != MBX_SUCCESS)
  6404. lpfc_printf_log(phba, KERN_ERR,
  6405. LOG_MBOX | LOG_SLI,
  6406. "(%d):2597 Mailbox command "
  6407. "x%x (x%x/x%x) cannot issue "
  6408. "Data: x%x x%x\n",
  6409. mboxq->vport ?
  6410. mboxq->vport->vpi : 0,
  6411. mboxq->u.mb.mbxCommand,
  6412. lpfc_sli_config_mbox_subsys_get(phba,
  6413. mboxq),
  6414. lpfc_sli_config_mbox_opcode_get(phba,
  6415. mboxq),
  6416. psli->sli_flag, flag);
  6417. /* Unblock the async mailbox posting afterward */
  6418. lpfc_sli4_async_mbox_unblock(phba);
  6419. }
  6420. return rc;
  6421. }
  6422. /* Now, interrupt mode asynchrous mailbox command */
  6423. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6424. if (rc) {
  6425. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6426. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6427. "cannot issue Data: x%x x%x\n",
  6428. mboxq->vport ? mboxq->vport->vpi : 0,
  6429. mboxq->u.mb.mbxCommand,
  6430. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6431. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6432. psli->sli_flag, flag);
  6433. goto out_not_finished;
  6434. }
  6435. /* Put the mailbox command to the driver internal FIFO */
  6436. psli->slistat.mbox_busy++;
  6437. spin_lock_irqsave(&phba->hbalock, iflags);
  6438. lpfc_mbox_put(phba, mboxq);
  6439. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6440. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6441. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6442. "x%x (x%x/x%x) x%x x%x x%x\n",
  6443. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6444. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6445. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6446. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6447. phba->pport->port_state,
  6448. psli->sli_flag, MBX_NOWAIT);
  6449. /* Wake up worker thread to transport mailbox command from head */
  6450. lpfc_worker_wake_up(phba);
  6451. return MBX_BUSY;
  6452. out_not_finished:
  6453. return MBX_NOT_FINISHED;
  6454. }
  6455. /**
  6456. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6457. * @phba: Pointer to HBA context object.
  6458. *
  6459. * This function is called by worker thread to send a mailbox command to
  6460. * SLI4 HBA firmware.
  6461. *
  6462. **/
  6463. int
  6464. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6465. {
  6466. struct lpfc_sli *psli = &phba->sli;
  6467. LPFC_MBOXQ_t *mboxq;
  6468. int rc = MBX_SUCCESS;
  6469. unsigned long iflags;
  6470. struct lpfc_mqe *mqe;
  6471. uint32_t mbx_cmnd;
  6472. /* Check interrupt mode before post async mailbox command */
  6473. if (unlikely(!phba->sli4_hba.intr_enable))
  6474. return MBX_NOT_FINISHED;
  6475. /* Check for mailbox command service token */
  6476. spin_lock_irqsave(&phba->hbalock, iflags);
  6477. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6478. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6479. return MBX_NOT_FINISHED;
  6480. }
  6481. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6482. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6483. return MBX_NOT_FINISHED;
  6484. }
  6485. if (unlikely(phba->sli.mbox_active)) {
  6486. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6487. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6488. "0384 There is pending active mailbox cmd\n");
  6489. return MBX_NOT_FINISHED;
  6490. }
  6491. /* Take the mailbox command service token */
  6492. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6493. /* Get the next mailbox command from head of queue */
  6494. mboxq = lpfc_mbox_get(phba);
  6495. /* If no more mailbox command waiting for post, we're done */
  6496. if (!mboxq) {
  6497. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6498. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6499. return MBX_SUCCESS;
  6500. }
  6501. phba->sli.mbox_active = mboxq;
  6502. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6503. /* Check device readiness for posting mailbox command */
  6504. rc = lpfc_mbox_dev_check(phba);
  6505. if (unlikely(rc))
  6506. /* Driver clean routine will clean up pending mailbox */
  6507. goto out_not_finished;
  6508. /* Prepare the mbox command to be posted */
  6509. mqe = &mboxq->u.mqe;
  6510. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6511. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6512. mod_timer(&psli->mbox_tmo, (jiffies +
  6513. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6514. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6515. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6516. "x%x x%x\n",
  6517. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6518. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6519. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6520. phba->pport->port_state, psli->sli_flag);
  6521. if (mbx_cmnd != MBX_HEARTBEAT) {
  6522. if (mboxq->vport) {
  6523. lpfc_debugfs_disc_trc(mboxq->vport,
  6524. LPFC_DISC_TRC_MBOX_VPORT,
  6525. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6526. mbx_cmnd, mqe->un.mb_words[0],
  6527. mqe->un.mb_words[1]);
  6528. } else {
  6529. lpfc_debugfs_disc_trc(phba->pport,
  6530. LPFC_DISC_TRC_MBOX,
  6531. "MBOX Send: cmd:x%x mb:x%x x%x",
  6532. mbx_cmnd, mqe->un.mb_words[0],
  6533. mqe->un.mb_words[1]);
  6534. }
  6535. }
  6536. psli->slistat.mbox_cmd++;
  6537. /* Post the mailbox command to the port */
  6538. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6539. if (rc != MBX_SUCCESS) {
  6540. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6541. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6542. "cannot issue Data: 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, mboxq),
  6546. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6547. psli->sli_flag, MBX_NOWAIT);
  6548. goto out_not_finished;
  6549. }
  6550. return rc;
  6551. out_not_finished:
  6552. spin_lock_irqsave(&phba->hbalock, iflags);
  6553. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6554. __lpfc_mbox_cmpl_put(phba, mboxq);
  6555. /* Release the token */
  6556. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6557. phba->sli.mbox_active = NULL;
  6558. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6559. return MBX_NOT_FINISHED;
  6560. }
  6561. /**
  6562. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6563. * @phba: Pointer to HBA context object.
  6564. * @pmbox: Pointer to mailbox object.
  6565. * @flag: Flag indicating how the mailbox need to be processed.
  6566. *
  6567. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6568. * the API jump table function pointer from the lpfc_hba struct.
  6569. *
  6570. * Return codes the caller owns the mailbox command after the return of the
  6571. * function.
  6572. **/
  6573. int
  6574. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6575. {
  6576. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6577. }
  6578. /**
  6579. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6580. * @phba: The hba struct for which this call is being executed.
  6581. * @dev_grp: The HBA PCI-Device group number.
  6582. *
  6583. * This routine sets up the mbox interface API function jump table in @phba
  6584. * struct.
  6585. * Returns: 0 - success, -ENODEV - failure.
  6586. **/
  6587. int
  6588. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6589. {
  6590. switch (dev_grp) {
  6591. case LPFC_PCI_DEV_LP:
  6592. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6593. phba->lpfc_sli_handle_slow_ring_event =
  6594. lpfc_sli_handle_slow_ring_event_s3;
  6595. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6596. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6597. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6598. break;
  6599. case LPFC_PCI_DEV_OC:
  6600. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6601. phba->lpfc_sli_handle_slow_ring_event =
  6602. lpfc_sli_handle_slow_ring_event_s4;
  6603. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6604. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6605. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6606. break;
  6607. default:
  6608. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6609. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6610. dev_grp);
  6611. return -ENODEV;
  6612. break;
  6613. }
  6614. return 0;
  6615. }
  6616. /**
  6617. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6618. * @phba: Pointer to HBA context object.
  6619. * @pring: Pointer to driver SLI ring object.
  6620. * @piocb: Pointer to address of newly added command iocb.
  6621. *
  6622. * This function is called with hbalock held to add a command
  6623. * iocb to the txq when SLI layer cannot submit the command iocb
  6624. * to the ring.
  6625. **/
  6626. void
  6627. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6628. struct lpfc_iocbq *piocb)
  6629. {
  6630. /* Insert the caller's iocb in the txq tail for later processing. */
  6631. list_add_tail(&piocb->list, &pring->txq);
  6632. pring->txq_cnt++;
  6633. }
  6634. /**
  6635. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6636. * @phba: Pointer to HBA context object.
  6637. * @pring: Pointer to driver SLI ring object.
  6638. * @piocb: Pointer to address of newly added command iocb.
  6639. *
  6640. * This function is called with hbalock held before a new
  6641. * iocb is submitted to the firmware. This function checks
  6642. * txq to flush the iocbs in txq to Firmware before
  6643. * submitting new iocbs to the Firmware.
  6644. * If there are iocbs in the txq which need to be submitted
  6645. * to firmware, lpfc_sli_next_iocb returns the first element
  6646. * of the txq after dequeuing it from txq.
  6647. * If there is no iocb in the txq then the function will return
  6648. * *piocb and *piocb is set to NULL. Caller needs to check
  6649. * *piocb to find if there are more commands in the txq.
  6650. **/
  6651. static struct lpfc_iocbq *
  6652. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6653. struct lpfc_iocbq **piocb)
  6654. {
  6655. struct lpfc_iocbq * nextiocb;
  6656. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6657. if (!nextiocb) {
  6658. nextiocb = *piocb;
  6659. *piocb = NULL;
  6660. }
  6661. return nextiocb;
  6662. }
  6663. /**
  6664. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6665. * @phba: Pointer to HBA context object.
  6666. * @ring_number: SLI ring number to issue iocb on.
  6667. * @piocb: Pointer to command iocb.
  6668. * @flag: Flag indicating if this command can be put into txq.
  6669. *
  6670. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6671. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6672. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6673. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6674. * this function allows only iocbs for posting buffers. This function finds
  6675. * next available slot in the command ring and posts the command to the
  6676. * available slot and writes the port attention register to request HBA start
  6677. * processing new iocb. If there is no slot available in the ring and
  6678. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6679. * the function returns IOCB_BUSY.
  6680. *
  6681. * This function is called with hbalock held. The function will return success
  6682. * after it successfully submit the iocb to firmware or after adding to the
  6683. * txq.
  6684. **/
  6685. static int
  6686. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6687. struct lpfc_iocbq *piocb, uint32_t flag)
  6688. {
  6689. struct lpfc_iocbq *nextiocb;
  6690. IOCB_t *iocb;
  6691. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6692. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6693. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6694. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6695. lpfc_printf_log(phba, KERN_ERR,
  6696. LOG_SLI | LOG_VPORT,
  6697. "1807 IOCB x%x failed. No vport\n",
  6698. piocb->iocb.ulpCommand);
  6699. dump_stack();
  6700. return IOCB_ERROR;
  6701. }
  6702. /* If the PCI channel is in offline state, do not post iocbs. */
  6703. if (unlikely(pci_channel_offline(phba->pcidev)))
  6704. return IOCB_ERROR;
  6705. /* If HBA has a deferred error attention, fail the iocb. */
  6706. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6707. return IOCB_ERROR;
  6708. /*
  6709. * We should never get an IOCB if we are in a < LINK_DOWN state
  6710. */
  6711. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6712. return IOCB_ERROR;
  6713. /*
  6714. * Check to see if we are blocking IOCB processing because of a
  6715. * outstanding event.
  6716. */
  6717. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6718. goto iocb_busy;
  6719. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6720. /*
  6721. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6722. * can be issued if the link is not up.
  6723. */
  6724. switch (piocb->iocb.ulpCommand) {
  6725. case CMD_GEN_REQUEST64_CR:
  6726. case CMD_GEN_REQUEST64_CX:
  6727. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6728. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6729. FC_RCTL_DD_UNSOL_CMD) ||
  6730. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6731. MENLO_TRANSPORT_TYPE))
  6732. goto iocb_busy;
  6733. break;
  6734. case CMD_QUE_RING_BUF_CN:
  6735. case CMD_QUE_RING_BUF64_CN:
  6736. /*
  6737. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6738. * completion, iocb_cmpl MUST be 0.
  6739. */
  6740. if (piocb->iocb_cmpl)
  6741. piocb->iocb_cmpl = NULL;
  6742. /*FALLTHROUGH*/
  6743. case CMD_CREATE_XRI_CR:
  6744. case CMD_CLOSE_XRI_CN:
  6745. case CMD_CLOSE_XRI_CX:
  6746. break;
  6747. default:
  6748. goto iocb_busy;
  6749. }
  6750. /*
  6751. * For FCP commands, we must be in a state where we can process link
  6752. * attention events.
  6753. */
  6754. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6755. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6756. goto iocb_busy;
  6757. }
  6758. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6759. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6760. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6761. if (iocb)
  6762. lpfc_sli_update_ring(phba, pring);
  6763. else
  6764. lpfc_sli_update_full_ring(phba, pring);
  6765. if (!piocb)
  6766. return IOCB_SUCCESS;
  6767. goto out_busy;
  6768. iocb_busy:
  6769. pring->stats.iocb_cmd_delay++;
  6770. out_busy:
  6771. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6772. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6773. return IOCB_SUCCESS;
  6774. }
  6775. return IOCB_BUSY;
  6776. }
  6777. /**
  6778. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6779. * @phba: Pointer to HBA context object.
  6780. * @piocb: Pointer to command iocb.
  6781. * @sglq: Pointer to the scatter gather queue object.
  6782. *
  6783. * This routine converts the bpl or bde that is in the IOCB
  6784. * to a sgl list for the sli4 hardware. The physical address
  6785. * of the bpl/bde is converted back to a virtual address.
  6786. * If the IOCB contains a BPL then the list of BDE's is
  6787. * converted to sli4_sge's. If the IOCB contains a single
  6788. * BDE then it is converted to a single sli_sge.
  6789. * The IOCB is still in cpu endianess so the contents of
  6790. * the bpl can be used without byte swapping.
  6791. *
  6792. * Returns valid XRI = Success, NO_XRI = Failure.
  6793. **/
  6794. static uint16_t
  6795. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6796. struct lpfc_sglq *sglq)
  6797. {
  6798. uint16_t xritag = NO_XRI;
  6799. struct ulp_bde64 *bpl = NULL;
  6800. struct ulp_bde64 bde;
  6801. struct sli4_sge *sgl = NULL;
  6802. IOCB_t *icmd;
  6803. int numBdes = 0;
  6804. int i = 0;
  6805. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6806. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6807. if (!piocbq || !sglq)
  6808. return xritag;
  6809. sgl = (struct sli4_sge *)sglq->sgl;
  6810. icmd = &piocbq->iocb;
  6811. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6812. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6813. sizeof(struct ulp_bde64);
  6814. /* The addrHigh and addrLow fields within the IOCB
  6815. * have not been byteswapped yet so there is no
  6816. * need to swap them back.
  6817. */
  6818. bpl = (struct ulp_bde64 *)
  6819. ((struct lpfc_dmabuf *)piocbq->context3)->virt;
  6820. if (!bpl)
  6821. return xritag;
  6822. for (i = 0; i < numBdes; i++) {
  6823. /* Should already be byte swapped. */
  6824. sgl->addr_hi = bpl->addrHigh;
  6825. sgl->addr_lo = bpl->addrLow;
  6826. sgl->word2 = le32_to_cpu(sgl->word2);
  6827. if ((i+1) == numBdes)
  6828. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6829. else
  6830. bf_set(lpfc_sli4_sge_last, sgl, 0);
  6831. /* swap the size field back to the cpu so we
  6832. * can assign it to the sgl.
  6833. */
  6834. bde.tus.w = le32_to_cpu(bpl->tus.w);
  6835. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  6836. /* The offsets in the sgl need to be accumulated
  6837. * separately for the request and reply lists.
  6838. * The request is always first, the reply follows.
  6839. */
  6840. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  6841. /* add up the reply sg entries */
  6842. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  6843. inbound++;
  6844. /* first inbound? reset the offset */
  6845. if (inbound == 1)
  6846. offset = 0;
  6847. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  6848. bf_set(lpfc_sli4_sge_type, sgl,
  6849. LPFC_SGE_TYPE_DATA);
  6850. offset += bde.tus.f.bdeSize;
  6851. }
  6852. sgl->word2 = cpu_to_le32(sgl->word2);
  6853. bpl++;
  6854. sgl++;
  6855. }
  6856. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  6857. /* The addrHigh and addrLow fields of the BDE have not
  6858. * been byteswapped yet so they need to be swapped
  6859. * before putting them in the sgl.
  6860. */
  6861. sgl->addr_hi =
  6862. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  6863. sgl->addr_lo =
  6864. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  6865. sgl->word2 = le32_to_cpu(sgl->word2);
  6866. bf_set(lpfc_sli4_sge_last, sgl, 1);
  6867. sgl->word2 = cpu_to_le32(sgl->word2);
  6868. sgl->sge_len =
  6869. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  6870. }
  6871. return sglq->sli4_xritag;
  6872. }
  6873. /**
  6874. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  6875. * @phba: Pointer to HBA context object.
  6876. *
  6877. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  6878. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  6879. * held.
  6880. *
  6881. * Return: index into SLI4 fast-path FCP queue index.
  6882. **/
  6883. static uint32_t
  6884. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  6885. {
  6886. ++phba->fcp_qidx;
  6887. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  6888. phba->fcp_qidx = 0;
  6889. return phba->fcp_qidx;
  6890. }
  6891. /**
  6892. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  6893. * @phba: Pointer to HBA context object.
  6894. * @piocb: Pointer to command iocb.
  6895. * @wqe: Pointer to the work queue entry.
  6896. *
  6897. * This routine converts the iocb command to its Work Queue Entry
  6898. * equivalent. The wqe pointer should not have any fields set when
  6899. * this routine is called because it will memcpy over them.
  6900. * This routine does not set the CQ_ID or the WQEC bits in the
  6901. * wqe.
  6902. *
  6903. * Returns: 0 = Success, IOCB_ERROR = Failure.
  6904. **/
  6905. static int
  6906. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  6907. union lpfc_wqe *wqe)
  6908. {
  6909. uint32_t xmit_len = 0, total_len = 0;
  6910. uint8_t ct = 0;
  6911. uint32_t fip;
  6912. uint32_t abort_tag;
  6913. uint8_t command_type = ELS_COMMAND_NON_FIP;
  6914. uint8_t cmnd;
  6915. uint16_t xritag;
  6916. uint16_t abrt_iotag;
  6917. struct lpfc_iocbq *abrtiocbq;
  6918. struct ulp_bde64 *bpl = NULL;
  6919. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  6920. int numBdes, i;
  6921. struct ulp_bde64 bde;
  6922. struct lpfc_nodelist *ndlp;
  6923. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  6924. /* The fcp commands will set command type */
  6925. if (iocbq->iocb_flag & LPFC_IO_FCP)
  6926. command_type = FCP_COMMAND;
  6927. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  6928. command_type = ELS_COMMAND_FIP;
  6929. else
  6930. command_type = ELS_COMMAND_NON_FIP;
  6931. /* Some of the fields are in the right position already */
  6932. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  6933. abort_tag = (uint32_t) iocbq->iotag;
  6934. xritag = iocbq->sli4_xritag;
  6935. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  6936. /* words0-2 bpl convert bde */
  6937. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6938. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  6939. sizeof(struct ulp_bde64);
  6940. bpl = (struct ulp_bde64 *)
  6941. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  6942. if (!bpl)
  6943. return IOCB_ERROR;
  6944. /* Should already be byte swapped. */
  6945. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  6946. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  6947. /* swap the size field back to the cpu so we
  6948. * can assign it to the sgl.
  6949. */
  6950. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  6951. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  6952. total_len = 0;
  6953. for (i = 0; i < numBdes; i++) {
  6954. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  6955. total_len += bde.tus.f.bdeSize;
  6956. }
  6957. } else
  6958. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  6959. iocbq->iocb.ulpIoTag = iocbq->iotag;
  6960. cmnd = iocbq->iocb.ulpCommand;
  6961. switch (iocbq->iocb.ulpCommand) {
  6962. case CMD_ELS_REQUEST64_CR:
  6963. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  6964. if (!iocbq->iocb.ulpLe) {
  6965. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  6966. "2007 Only Limited Edition cmd Format"
  6967. " supported 0x%x\n",
  6968. iocbq->iocb.ulpCommand);
  6969. return IOCB_ERROR;
  6970. }
  6971. wqe->els_req.payload_len = xmit_len;
  6972. /* Els_reguest64 has a TMO */
  6973. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  6974. iocbq->iocb.ulpTimeout);
  6975. /* Need a VF for word 4 set the vf bit*/
  6976. bf_set(els_req64_vf, &wqe->els_req, 0);
  6977. /* And a VFID for word 12 */
  6978. bf_set(els_req64_vfid, &wqe->els_req, 0);
  6979. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  6980. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  6981. iocbq->iocb.ulpContext);
  6982. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  6983. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  6984. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  6985. if (command_type == ELS_COMMAND_FIP) {
  6986. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  6987. >> LPFC_FIP_ELS_ID_SHIFT);
  6988. }
  6989. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  6990. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  6991. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  6992. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  6993. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  6994. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  6995. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  6996. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  6997. break;
  6998. case CMD_XMIT_SEQUENCE64_CX:
  6999. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7000. iocbq->iocb.un.ulpWord[3]);
  7001. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7002. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7003. /* The entire sequence is transmitted for this IOCB */
  7004. xmit_len = total_len;
  7005. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7006. case CMD_XMIT_SEQUENCE64_CR:
  7007. /* word3 iocb=io_tag32 wqe=reserved */
  7008. wqe->xmit_sequence.rsvd3 = 0;
  7009. /* word4 relative_offset memcpy */
  7010. /* word5 r_ctl/df_ctl memcpy */
  7011. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7012. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7013. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7014. LPFC_WQE_IOD_WRITE);
  7015. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7016. LPFC_WQE_LENLOC_WORD12);
  7017. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7018. wqe->xmit_sequence.xmit_len = xmit_len;
  7019. command_type = OTHER_COMMAND;
  7020. break;
  7021. case CMD_XMIT_BCAST64_CN:
  7022. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7023. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7024. /* word4 iocb=rsvd wqe=rsvd */
  7025. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7026. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7027. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7028. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7029. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7030. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7031. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7032. LPFC_WQE_LENLOC_WORD3);
  7033. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7034. break;
  7035. case CMD_FCP_IWRITE64_CR:
  7036. command_type = FCP_COMMAND_DATA_OUT;
  7037. /* word3 iocb=iotag wqe=payload_offset_len */
  7038. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7039. wqe->fcp_iwrite.payload_offset_len =
  7040. xmit_len + sizeof(struct fcp_rsp);
  7041. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7042. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7043. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7044. iocbq->iocb.ulpFCP2Rcvy);
  7045. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7046. /* Always open the exchange */
  7047. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7048. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7049. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7050. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7051. LPFC_WQE_LENLOC_WORD4);
  7052. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7053. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7054. break;
  7055. case CMD_FCP_IREAD64_CR:
  7056. /* word3 iocb=iotag wqe=payload_offset_len */
  7057. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7058. wqe->fcp_iread.payload_offset_len =
  7059. xmit_len + sizeof(struct fcp_rsp);
  7060. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7061. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7062. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7063. iocbq->iocb.ulpFCP2Rcvy);
  7064. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7065. /* Always open the exchange */
  7066. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7067. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7068. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7069. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7070. LPFC_WQE_LENLOC_WORD4);
  7071. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7072. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7073. break;
  7074. case CMD_FCP_ICMND64_CR:
  7075. /* word3 iocb=IO_TAG wqe=reserved */
  7076. wqe->fcp_icmd.rsrvd3 = 0;
  7077. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7078. /* Always open the exchange */
  7079. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7080. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7081. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7082. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7083. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7084. LPFC_WQE_LENLOC_NONE);
  7085. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7086. break;
  7087. case CMD_GEN_REQUEST64_CR:
  7088. /* For this command calculate the xmit length of the
  7089. * request bde.
  7090. */
  7091. xmit_len = 0;
  7092. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7093. sizeof(struct ulp_bde64);
  7094. for (i = 0; i < numBdes; i++) {
  7095. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7096. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7097. break;
  7098. xmit_len += bde.tus.f.bdeSize;
  7099. }
  7100. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7101. wqe->gen_req.request_payload_len = xmit_len;
  7102. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7103. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7104. /* word6 context tag copied in memcpy */
  7105. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7106. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7107. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7108. "2015 Invalid CT %x command 0x%x\n",
  7109. ct, iocbq->iocb.ulpCommand);
  7110. return IOCB_ERROR;
  7111. }
  7112. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7113. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7114. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7115. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7116. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7117. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7118. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7119. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7120. command_type = OTHER_COMMAND;
  7121. break;
  7122. case CMD_XMIT_ELS_RSP64_CX:
  7123. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7124. /* words0-2 BDE memcpy */
  7125. /* word3 iocb=iotag32 wqe=response_payload_len */
  7126. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7127. /* word4 iocb=did wge=rsvd. */
  7128. wqe->xmit_els_rsp.rsvd4 = 0;
  7129. /* word5 iocb=rsvd wge=did */
  7130. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7131. iocbq->iocb.un.elsreq64.remoteID);
  7132. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7133. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7134. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7135. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7136. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7137. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7138. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7139. phba->vpi_ids[iocbq->vport->vpi]);
  7140. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7141. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7142. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7143. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7144. LPFC_WQE_LENLOC_WORD3);
  7145. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7146. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7147. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7148. command_type = OTHER_COMMAND;
  7149. break;
  7150. case CMD_CLOSE_XRI_CN:
  7151. case CMD_ABORT_XRI_CN:
  7152. case CMD_ABORT_XRI_CX:
  7153. /* words 0-2 memcpy should be 0 rserved */
  7154. /* port will send abts */
  7155. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7156. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7157. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7158. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7159. } else
  7160. fip = 0;
  7161. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7162. /*
  7163. * The link is down, or the command was ELS_FIP
  7164. * so the fw does not need to send abts
  7165. * on the wire.
  7166. */
  7167. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7168. else
  7169. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7170. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7171. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7172. wqe->abort_cmd.rsrvd5 = 0;
  7173. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7174. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7175. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7176. /*
  7177. * The abort handler will send us CMD_ABORT_XRI_CN or
  7178. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7179. */
  7180. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7181. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7182. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7183. LPFC_WQE_LENLOC_NONE);
  7184. cmnd = CMD_ABORT_XRI_CX;
  7185. command_type = OTHER_COMMAND;
  7186. xritag = 0;
  7187. break;
  7188. case CMD_XMIT_BLS_RSP64_CX:
  7189. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7190. * we re-construct this WQE here based on information in
  7191. * iocbq from scratch.
  7192. */
  7193. memset(wqe, 0, sizeof(union lpfc_wqe));
  7194. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7195. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7196. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7197. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7198. LPFC_ABTS_UNSOL_INT) {
  7199. /* ABTS sent by initiator to CT exchange, the
  7200. * RX_ID field will be filled with the newly
  7201. * allocated responder XRI.
  7202. */
  7203. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7204. iocbq->sli4_xritag);
  7205. } else {
  7206. /* ABTS sent by responder to CT exchange, the
  7207. * RX_ID field will be filled with the responder
  7208. * RX_ID from ABTS.
  7209. */
  7210. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7211. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7212. }
  7213. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7214. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7215. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7216. iocbq->iocb.ulpContext);
  7217. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7218. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7219. LPFC_WQE_LENLOC_NONE);
  7220. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7221. command_type = OTHER_COMMAND;
  7222. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7223. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7224. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7225. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7226. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7227. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7228. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7229. }
  7230. break;
  7231. case CMD_XRI_ABORTED_CX:
  7232. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7233. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7234. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7235. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7236. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7237. default:
  7238. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7239. "2014 Invalid command 0x%x\n",
  7240. iocbq->iocb.ulpCommand);
  7241. return IOCB_ERROR;
  7242. break;
  7243. }
  7244. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7245. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7246. wqe->generic.wqe_com.abort_tag = abort_tag;
  7247. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7248. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7249. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7250. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7251. return 0;
  7252. }
  7253. /**
  7254. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7255. * @phba: Pointer to HBA context object.
  7256. * @ring_number: SLI ring number to issue iocb on.
  7257. * @piocb: Pointer to command iocb.
  7258. * @flag: Flag indicating if this command can be put into txq.
  7259. *
  7260. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7261. * an iocb command to an HBA with SLI-4 interface spec.
  7262. *
  7263. * This function is called with hbalock held. The function will return success
  7264. * after it successfully submit the iocb to firmware or after adding to the
  7265. * txq.
  7266. **/
  7267. static int
  7268. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7269. struct lpfc_iocbq *piocb, uint32_t flag)
  7270. {
  7271. struct lpfc_sglq *sglq;
  7272. union lpfc_wqe wqe;
  7273. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7274. if (piocb->sli4_xritag == NO_XRI) {
  7275. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7276. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
  7277. piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  7278. sglq = NULL;
  7279. else {
  7280. if (pring->txq_cnt) {
  7281. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7282. __lpfc_sli_ringtx_put(phba,
  7283. pring, piocb);
  7284. return IOCB_SUCCESS;
  7285. } else {
  7286. return IOCB_BUSY;
  7287. }
  7288. } else {
  7289. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7290. if (!sglq) {
  7291. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7292. __lpfc_sli_ringtx_put(phba,
  7293. pring,
  7294. piocb);
  7295. return IOCB_SUCCESS;
  7296. } else
  7297. return IOCB_BUSY;
  7298. }
  7299. }
  7300. }
  7301. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7302. /* These IO's already have an XRI and a mapped sgl. */
  7303. sglq = NULL;
  7304. } else {
  7305. /*
  7306. * This is a continuation of a commandi,(CX) so this
  7307. * sglq is on the active list
  7308. */
  7309. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7310. if (!sglq)
  7311. return IOCB_ERROR;
  7312. }
  7313. if (sglq) {
  7314. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7315. piocb->sli4_xritag = sglq->sli4_xritag;
  7316. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7317. return IOCB_ERROR;
  7318. }
  7319. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7320. return IOCB_ERROR;
  7321. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7322. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7323. /*
  7324. * For FCP command IOCB, get a new WQ index to distribute
  7325. * WQE across the WQsr. On the other hand, for abort IOCB,
  7326. * it carries the same WQ index to the original command
  7327. * IOCB.
  7328. */
  7329. if (piocb->iocb_flag & LPFC_IO_FCP)
  7330. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7331. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7332. &wqe))
  7333. return IOCB_ERROR;
  7334. } else {
  7335. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7336. return IOCB_ERROR;
  7337. }
  7338. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7339. return 0;
  7340. }
  7341. /**
  7342. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7343. *
  7344. * This routine wraps the actual lockless version for issusing IOCB function
  7345. * pointer from the lpfc_hba struct.
  7346. *
  7347. * Return codes:
  7348. * IOCB_ERROR - Error
  7349. * IOCB_SUCCESS - Success
  7350. * IOCB_BUSY - Busy
  7351. **/
  7352. int
  7353. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7354. struct lpfc_iocbq *piocb, uint32_t flag)
  7355. {
  7356. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7357. }
  7358. /**
  7359. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7360. * @phba: The hba struct for which this call is being executed.
  7361. * @dev_grp: The HBA PCI-Device group number.
  7362. *
  7363. * This routine sets up the SLI interface API function jump table in @phba
  7364. * struct.
  7365. * Returns: 0 - success, -ENODEV - failure.
  7366. **/
  7367. int
  7368. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7369. {
  7370. switch (dev_grp) {
  7371. case LPFC_PCI_DEV_LP:
  7372. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7373. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7374. break;
  7375. case LPFC_PCI_DEV_OC:
  7376. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7377. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7378. break;
  7379. default:
  7380. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7381. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7382. dev_grp);
  7383. return -ENODEV;
  7384. break;
  7385. }
  7386. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7387. return 0;
  7388. }
  7389. /**
  7390. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7391. * @phba: Pointer to HBA context object.
  7392. * @pring: Pointer to driver SLI ring object.
  7393. * @piocb: Pointer to command iocb.
  7394. * @flag: Flag indicating if this command can be put into txq.
  7395. *
  7396. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7397. * function. This function gets the hbalock and calls
  7398. * __lpfc_sli_issue_iocb function and will return the error returned
  7399. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7400. * functions which do not hold hbalock.
  7401. **/
  7402. int
  7403. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7404. struct lpfc_iocbq *piocb, uint32_t flag)
  7405. {
  7406. unsigned long iflags;
  7407. int rc;
  7408. spin_lock_irqsave(&phba->hbalock, iflags);
  7409. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7410. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7411. return rc;
  7412. }
  7413. /**
  7414. * lpfc_extra_ring_setup - Extra ring setup function
  7415. * @phba: Pointer to HBA context object.
  7416. *
  7417. * This function is called while driver attaches with the
  7418. * HBA to setup the extra ring. The extra ring is used
  7419. * only when driver needs to support target mode functionality
  7420. * or IP over FC functionalities.
  7421. *
  7422. * This function is called with no lock held.
  7423. **/
  7424. static int
  7425. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7426. {
  7427. struct lpfc_sli *psli;
  7428. struct lpfc_sli_ring *pring;
  7429. psli = &phba->sli;
  7430. /* Adjust cmd/rsp ring iocb entries more evenly */
  7431. /* Take some away from the FCP ring */
  7432. pring = &psli->ring[psli->fcp_ring];
  7433. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7434. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7435. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7436. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7437. /* and give them to the extra ring */
  7438. pring = &psli->ring[psli->extra_ring];
  7439. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7440. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7441. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7442. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7443. /* Setup default profile for this ring */
  7444. pring->iotag_max = 4096;
  7445. pring->num_mask = 1;
  7446. pring->prt[0].profile = 0; /* Mask 0 */
  7447. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7448. pring->prt[0].type = phba->cfg_multi_ring_type;
  7449. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7450. return 0;
  7451. }
  7452. /**
  7453. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7454. * @phba: Pointer to HBA context object.
  7455. * @pring: Pointer to driver SLI ring object.
  7456. * @iocbq: Pointer to iocb object.
  7457. *
  7458. * This function is called by the slow ring event handler
  7459. * function when there is an ASYNC event iocb in the ring.
  7460. * This function is called with no lock held.
  7461. * Currently this function handles only temperature related
  7462. * ASYNC events. The function decodes the temperature sensor
  7463. * event message and posts events for the management applications.
  7464. **/
  7465. static void
  7466. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7467. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7468. {
  7469. IOCB_t *icmd;
  7470. uint16_t evt_code;
  7471. uint16_t temp;
  7472. struct temp_event temp_event_data;
  7473. struct Scsi_Host *shost;
  7474. uint32_t *iocb_w;
  7475. icmd = &iocbq->iocb;
  7476. evt_code = icmd->un.asyncstat.evt_code;
  7477. temp = icmd->ulpContext;
  7478. if ((evt_code != ASYNC_TEMP_WARN) &&
  7479. (evt_code != ASYNC_TEMP_SAFE)) {
  7480. iocb_w = (uint32_t *) icmd;
  7481. lpfc_printf_log(phba,
  7482. KERN_ERR,
  7483. LOG_SLI,
  7484. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7485. " evt_code 0x%x\n"
  7486. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7487. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7488. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7489. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7490. pring->ringno,
  7491. icmd->un.asyncstat.evt_code,
  7492. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7493. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7494. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7495. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7496. return;
  7497. }
  7498. temp_event_data.data = (uint32_t)temp;
  7499. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7500. if (evt_code == ASYNC_TEMP_WARN) {
  7501. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7502. lpfc_printf_log(phba,
  7503. KERN_ERR,
  7504. LOG_TEMP,
  7505. "0347 Adapter is very hot, please take "
  7506. "corrective action. temperature : %d Celsius\n",
  7507. temp);
  7508. }
  7509. if (evt_code == ASYNC_TEMP_SAFE) {
  7510. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7511. lpfc_printf_log(phba,
  7512. KERN_ERR,
  7513. LOG_TEMP,
  7514. "0340 Adapter temperature is OK now. "
  7515. "temperature : %d Celsius\n",
  7516. temp);
  7517. }
  7518. /* Send temperature change event to applications */
  7519. shost = lpfc_shost_from_vport(phba->pport);
  7520. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7521. sizeof(temp_event_data), (char *) &temp_event_data,
  7522. LPFC_NL_VENDOR_ID);
  7523. }
  7524. /**
  7525. * lpfc_sli_setup - SLI ring setup function
  7526. * @phba: Pointer to HBA context object.
  7527. *
  7528. * lpfc_sli_setup sets up rings of the SLI interface with
  7529. * number of iocbs per ring and iotags. This function is
  7530. * called while driver attach to the HBA and before the
  7531. * interrupts are enabled. So there is no need for locking.
  7532. *
  7533. * This function always returns 0.
  7534. **/
  7535. int
  7536. lpfc_sli_setup(struct lpfc_hba *phba)
  7537. {
  7538. int i, totiocbsize = 0;
  7539. struct lpfc_sli *psli = &phba->sli;
  7540. struct lpfc_sli_ring *pring;
  7541. psli->num_rings = MAX_CONFIGURED_RINGS;
  7542. psli->sli_flag = 0;
  7543. psli->fcp_ring = LPFC_FCP_RING;
  7544. psli->next_ring = LPFC_FCP_NEXT_RING;
  7545. psli->extra_ring = LPFC_EXTRA_RING;
  7546. psli->iocbq_lookup = NULL;
  7547. psli->iocbq_lookup_len = 0;
  7548. psli->last_iotag = 0;
  7549. for (i = 0; i < psli->num_rings; i++) {
  7550. pring = &psli->ring[i];
  7551. switch (i) {
  7552. case LPFC_FCP_RING: /* ring 0 - FCP */
  7553. /* numCiocb and numRiocb are used in config_port */
  7554. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7555. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7556. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7557. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7558. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7559. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7560. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7561. SLI3_IOCB_CMD_SIZE :
  7562. SLI2_IOCB_CMD_SIZE;
  7563. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7564. SLI3_IOCB_RSP_SIZE :
  7565. SLI2_IOCB_RSP_SIZE;
  7566. pring->iotag_ctr = 0;
  7567. pring->iotag_max =
  7568. (phba->cfg_hba_queue_depth * 2);
  7569. pring->fast_iotag = pring->iotag_max;
  7570. pring->num_mask = 0;
  7571. break;
  7572. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7573. /* numCiocb and numRiocb are used in config_port */
  7574. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7575. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7576. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7577. SLI3_IOCB_CMD_SIZE :
  7578. SLI2_IOCB_CMD_SIZE;
  7579. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7580. SLI3_IOCB_RSP_SIZE :
  7581. SLI2_IOCB_RSP_SIZE;
  7582. pring->iotag_max = phba->cfg_hba_queue_depth;
  7583. pring->num_mask = 0;
  7584. break;
  7585. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7586. /* numCiocb and numRiocb are used in config_port */
  7587. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7588. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7589. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7590. SLI3_IOCB_CMD_SIZE :
  7591. SLI2_IOCB_CMD_SIZE;
  7592. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7593. SLI3_IOCB_RSP_SIZE :
  7594. SLI2_IOCB_RSP_SIZE;
  7595. pring->fast_iotag = 0;
  7596. pring->iotag_ctr = 0;
  7597. pring->iotag_max = 4096;
  7598. pring->lpfc_sli_rcv_async_status =
  7599. lpfc_sli_async_event_handler;
  7600. pring->num_mask = LPFC_MAX_RING_MASK;
  7601. pring->prt[0].profile = 0; /* Mask 0 */
  7602. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7603. pring->prt[0].type = FC_TYPE_ELS;
  7604. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7605. lpfc_els_unsol_event;
  7606. pring->prt[1].profile = 0; /* Mask 1 */
  7607. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7608. pring->prt[1].type = FC_TYPE_ELS;
  7609. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7610. lpfc_els_unsol_event;
  7611. pring->prt[2].profile = 0; /* Mask 2 */
  7612. /* NameServer Inquiry */
  7613. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7614. /* NameServer */
  7615. pring->prt[2].type = FC_TYPE_CT;
  7616. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7617. lpfc_ct_unsol_event;
  7618. pring->prt[3].profile = 0; /* Mask 3 */
  7619. /* NameServer response */
  7620. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7621. /* NameServer */
  7622. pring->prt[3].type = FC_TYPE_CT;
  7623. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7624. lpfc_ct_unsol_event;
  7625. /* abort unsolicited sequence */
  7626. pring->prt[4].profile = 0; /* Mask 4 */
  7627. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7628. pring->prt[4].type = FC_TYPE_BLS;
  7629. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7630. lpfc_sli4_ct_abort_unsol_event;
  7631. break;
  7632. }
  7633. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7634. (pring->numRiocb * pring->sizeRiocb);
  7635. }
  7636. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7637. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7638. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7639. "SLI2 SLIM Data: x%x x%lx\n",
  7640. phba->brd_no, totiocbsize,
  7641. (unsigned long) MAX_SLIM_IOCB_SIZE);
  7642. }
  7643. if (phba->cfg_multi_ring_support == 2)
  7644. lpfc_extra_ring_setup(phba);
  7645. return 0;
  7646. }
  7647. /**
  7648. * lpfc_sli_queue_setup - Queue initialization function
  7649. * @phba: Pointer to HBA context object.
  7650. *
  7651. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  7652. * ring. This function also initializes ring indices of each ring.
  7653. * This function is called during the initialization of the SLI
  7654. * interface of an HBA.
  7655. * This function is called with no lock held and always returns
  7656. * 1.
  7657. **/
  7658. int
  7659. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  7660. {
  7661. struct lpfc_sli *psli;
  7662. struct lpfc_sli_ring *pring;
  7663. int i;
  7664. psli = &phba->sli;
  7665. spin_lock_irq(&phba->hbalock);
  7666. INIT_LIST_HEAD(&psli->mboxq);
  7667. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  7668. /* Initialize list headers for txq and txcmplq as double linked lists */
  7669. for (i = 0; i < psli->num_rings; i++) {
  7670. pring = &psli->ring[i];
  7671. pring->ringno = i;
  7672. pring->next_cmdidx = 0;
  7673. pring->local_getidx = 0;
  7674. pring->cmdidx = 0;
  7675. INIT_LIST_HEAD(&pring->txq);
  7676. INIT_LIST_HEAD(&pring->txcmplq);
  7677. INIT_LIST_HEAD(&pring->iocb_continueq);
  7678. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  7679. INIT_LIST_HEAD(&pring->postbufq);
  7680. }
  7681. spin_unlock_irq(&phba->hbalock);
  7682. return 1;
  7683. }
  7684. /**
  7685. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  7686. * @phba: Pointer to HBA context object.
  7687. *
  7688. * This routine flushes the mailbox command subsystem. It will unconditionally
  7689. * flush all the mailbox commands in the three possible stages in the mailbox
  7690. * command sub-system: pending mailbox command queue; the outstanding mailbox
  7691. * command; and completed mailbox command queue. It is caller's responsibility
  7692. * to make sure that the driver is in the proper state to flush the mailbox
  7693. * command sub-system. Namely, the posting of mailbox commands into the
  7694. * pending mailbox command queue from the various clients must be stopped;
  7695. * either the HBA is in a state that it will never works on the outstanding
  7696. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  7697. * mailbox command has been completed.
  7698. **/
  7699. static void
  7700. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  7701. {
  7702. LIST_HEAD(completions);
  7703. struct lpfc_sli *psli = &phba->sli;
  7704. LPFC_MBOXQ_t *pmb;
  7705. unsigned long iflag;
  7706. /* Flush all the mailbox commands in the mbox system */
  7707. spin_lock_irqsave(&phba->hbalock, iflag);
  7708. /* The pending mailbox command queue */
  7709. list_splice_init(&phba->sli.mboxq, &completions);
  7710. /* The outstanding active mailbox command */
  7711. if (psli->mbox_active) {
  7712. list_add_tail(&psli->mbox_active->list, &completions);
  7713. psli->mbox_active = NULL;
  7714. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  7715. }
  7716. /* The completed mailbox command queue */
  7717. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  7718. spin_unlock_irqrestore(&phba->hbalock, iflag);
  7719. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  7720. while (!list_empty(&completions)) {
  7721. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  7722. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  7723. if (pmb->mbox_cmpl)
  7724. pmb->mbox_cmpl(phba, pmb);
  7725. }
  7726. }
  7727. /**
  7728. * lpfc_sli_host_down - Vport cleanup function
  7729. * @vport: Pointer to virtual port object.
  7730. *
  7731. * lpfc_sli_host_down is called to clean up the resources
  7732. * associated with a vport before destroying virtual
  7733. * port data structures.
  7734. * This function does following operations:
  7735. * - Free discovery resources associated with this virtual
  7736. * port.
  7737. * - Free iocbs associated with this virtual port in
  7738. * the txq.
  7739. * - Send abort for all iocb commands associated with this
  7740. * vport in txcmplq.
  7741. *
  7742. * This function is called with no lock held and always returns 1.
  7743. **/
  7744. int
  7745. lpfc_sli_host_down(struct lpfc_vport *vport)
  7746. {
  7747. LIST_HEAD(completions);
  7748. struct lpfc_hba *phba = vport->phba;
  7749. struct lpfc_sli *psli = &phba->sli;
  7750. struct lpfc_sli_ring *pring;
  7751. struct lpfc_iocbq *iocb, *next_iocb;
  7752. int i;
  7753. unsigned long flags = 0;
  7754. uint16_t prev_pring_flag;
  7755. lpfc_cleanup_discovery_resources(vport);
  7756. spin_lock_irqsave(&phba->hbalock, flags);
  7757. for (i = 0; i < psli->num_rings; i++) {
  7758. pring = &psli->ring[i];
  7759. prev_pring_flag = pring->flag;
  7760. /* Only slow rings */
  7761. if (pring->ringno == LPFC_ELS_RING) {
  7762. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7763. /* Set the lpfc data pending flag */
  7764. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7765. }
  7766. /*
  7767. * Error everything on the txq since these iocbs have not been
  7768. * given to the FW yet.
  7769. */
  7770. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  7771. if (iocb->vport != vport)
  7772. continue;
  7773. list_move_tail(&iocb->list, &completions);
  7774. pring->txq_cnt--;
  7775. }
  7776. /* Next issue ABTS for everything on the txcmplq */
  7777. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  7778. list) {
  7779. if (iocb->vport != vport)
  7780. continue;
  7781. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  7782. }
  7783. pring->flag = prev_pring_flag;
  7784. }
  7785. spin_unlock_irqrestore(&phba->hbalock, flags);
  7786. /* Cancel all the IOCBs from the completions list */
  7787. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  7788. IOERR_SLI_DOWN);
  7789. return 1;
  7790. }
  7791. /**
  7792. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  7793. * @phba: Pointer to HBA context object.
  7794. *
  7795. * This function cleans up all iocb, buffers, mailbox commands
  7796. * while shutting down the HBA. This function is called with no
  7797. * lock held and always returns 1.
  7798. * This function does the following to cleanup driver resources:
  7799. * - Free discovery resources for each virtual port
  7800. * - Cleanup any pending fabric iocbs
  7801. * - Iterate through the iocb txq and free each entry
  7802. * in the list.
  7803. * - Free up any buffer posted to the HBA
  7804. * - Free mailbox commands in the mailbox queue.
  7805. **/
  7806. int
  7807. lpfc_sli_hba_down(struct lpfc_hba *phba)
  7808. {
  7809. LIST_HEAD(completions);
  7810. struct lpfc_sli *psli = &phba->sli;
  7811. struct lpfc_sli_ring *pring;
  7812. struct lpfc_dmabuf *buf_ptr;
  7813. unsigned long flags = 0;
  7814. int i;
  7815. /* Shutdown the mailbox command sub-system */
  7816. lpfc_sli_mbox_sys_shutdown(phba);
  7817. lpfc_hba_down_prep(phba);
  7818. lpfc_fabric_abort_hba(phba);
  7819. spin_lock_irqsave(&phba->hbalock, flags);
  7820. for (i = 0; i < psli->num_rings; i++) {
  7821. pring = &psli->ring[i];
  7822. /* Only slow rings */
  7823. if (pring->ringno == LPFC_ELS_RING) {
  7824. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  7825. /* Set the lpfc data pending flag */
  7826. set_bit(LPFC_DATA_READY, &phba->data_flags);
  7827. }
  7828. /*
  7829. * Error everything on the txq since these iocbs have not been
  7830. * given to the FW yet.
  7831. */
  7832. list_splice_init(&pring->txq, &completions);
  7833. pring->txq_cnt = 0;
  7834. }
  7835. spin_unlock_irqrestore(&phba->hbalock, flags);
  7836. /* Cancel all the IOCBs from the completions list */
  7837. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  7838. IOERR_SLI_DOWN);
  7839. spin_lock_irqsave(&phba->hbalock, flags);
  7840. list_splice_init(&phba->elsbuf, &completions);
  7841. phba->elsbuf_cnt = 0;
  7842. phba->elsbuf_prev_cnt = 0;
  7843. spin_unlock_irqrestore(&phba->hbalock, flags);
  7844. while (!list_empty(&completions)) {
  7845. list_remove_head(&completions, buf_ptr,
  7846. struct lpfc_dmabuf, list);
  7847. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  7848. kfree(buf_ptr);
  7849. }
  7850. /* Return any active mbox cmds */
  7851. del_timer_sync(&psli->mbox_tmo);
  7852. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  7853. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  7854. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  7855. return 1;
  7856. }
  7857. /**
  7858. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  7859. * @srcp: Source memory pointer.
  7860. * @destp: Destination memory pointer.
  7861. * @cnt: Number of words required to be copied.
  7862. *
  7863. * This function is used for copying data between driver memory
  7864. * and the SLI memory. This function also changes the endianness
  7865. * of each word if native endianness is different from SLI
  7866. * endianness. This function can be called with or without
  7867. * lock.
  7868. **/
  7869. void
  7870. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  7871. {
  7872. uint32_t *src = srcp;
  7873. uint32_t *dest = destp;
  7874. uint32_t ldata;
  7875. int i;
  7876. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  7877. ldata = *src;
  7878. ldata = le32_to_cpu(ldata);
  7879. *dest = ldata;
  7880. src++;
  7881. dest++;
  7882. }
  7883. }
  7884. /**
  7885. * lpfc_sli_bemem_bcopy - SLI memory copy function
  7886. * @srcp: Source memory pointer.
  7887. * @destp: Destination memory pointer.
  7888. * @cnt: Number of words required to be copied.
  7889. *
  7890. * This function is used for copying data between a data structure
  7891. * with big endian representation to local endianness.
  7892. * This function can be called with or without lock.
  7893. **/
  7894. void
  7895. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  7896. {
  7897. uint32_t *src = srcp;
  7898. uint32_t *dest = destp;
  7899. uint32_t ldata;
  7900. int i;
  7901. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  7902. ldata = *src;
  7903. ldata = be32_to_cpu(ldata);
  7904. *dest = ldata;
  7905. src++;
  7906. dest++;
  7907. }
  7908. }
  7909. /**
  7910. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  7911. * @phba: Pointer to HBA context object.
  7912. * @pring: Pointer to driver SLI ring object.
  7913. * @mp: Pointer to driver buffer object.
  7914. *
  7915. * This function is called with no lock held.
  7916. * It always return zero after adding the buffer to the postbufq
  7917. * buffer list.
  7918. **/
  7919. int
  7920. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7921. struct lpfc_dmabuf *mp)
  7922. {
  7923. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  7924. later */
  7925. spin_lock_irq(&phba->hbalock);
  7926. list_add_tail(&mp->list, &pring->postbufq);
  7927. pring->postbufq_cnt++;
  7928. spin_unlock_irq(&phba->hbalock);
  7929. return 0;
  7930. }
  7931. /**
  7932. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  7933. * @phba: Pointer to HBA context object.
  7934. *
  7935. * When HBQ is enabled, buffers are searched based on tags. This function
  7936. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  7937. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  7938. * does not conflict with tags of buffer posted for unsolicited events.
  7939. * The function returns the allocated tag. The function is called with
  7940. * no locks held.
  7941. **/
  7942. uint32_t
  7943. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  7944. {
  7945. spin_lock_irq(&phba->hbalock);
  7946. phba->buffer_tag_count++;
  7947. /*
  7948. * Always set the QUE_BUFTAG_BIT to distiguish between
  7949. * a tag assigned by HBQ.
  7950. */
  7951. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  7952. spin_unlock_irq(&phba->hbalock);
  7953. return phba->buffer_tag_count;
  7954. }
  7955. /**
  7956. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  7957. * @phba: Pointer to HBA context object.
  7958. * @pring: Pointer to driver SLI ring object.
  7959. * @tag: Buffer tag.
  7960. *
  7961. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  7962. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  7963. * iocb is posted to the response ring with the tag of the buffer.
  7964. * This function searches the pring->postbufq list using the tag
  7965. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  7966. * iocb. If the buffer is found then lpfc_dmabuf object of the
  7967. * buffer is returned to the caller else NULL is returned.
  7968. * This function is called with no lock held.
  7969. **/
  7970. struct lpfc_dmabuf *
  7971. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  7972. uint32_t tag)
  7973. {
  7974. struct lpfc_dmabuf *mp, *next_mp;
  7975. struct list_head *slp = &pring->postbufq;
  7976. /* Search postbufq, from the beginning, looking for a match on tag */
  7977. spin_lock_irq(&phba->hbalock);
  7978. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  7979. if (mp->buffer_tag == tag) {
  7980. list_del_init(&mp->list);
  7981. pring->postbufq_cnt--;
  7982. spin_unlock_irq(&phba->hbalock);
  7983. return mp;
  7984. }
  7985. }
  7986. spin_unlock_irq(&phba->hbalock);
  7987. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7988. "0402 Cannot find virtual addr for buffer tag on "
  7989. "ring %d Data x%lx x%p x%p x%x\n",
  7990. pring->ringno, (unsigned long) tag,
  7991. slp->next, slp->prev, pring->postbufq_cnt);
  7992. return NULL;
  7993. }
  7994. /**
  7995. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  7996. * @phba: Pointer to HBA context object.
  7997. * @pring: Pointer to driver SLI ring object.
  7998. * @phys: DMA address of the buffer.
  7999. *
  8000. * This function searches the buffer list using the dma_address
  8001. * of unsolicited event to find the driver's lpfc_dmabuf object
  8002. * corresponding to the dma_address. The function returns the
  8003. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8004. * This function is called by the ct and els unsolicited event
  8005. * handlers to get the buffer associated with the unsolicited
  8006. * event.
  8007. *
  8008. * This function is called with no lock held.
  8009. **/
  8010. struct lpfc_dmabuf *
  8011. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8012. dma_addr_t phys)
  8013. {
  8014. struct lpfc_dmabuf *mp, *next_mp;
  8015. struct list_head *slp = &pring->postbufq;
  8016. /* Search postbufq, from the beginning, looking for a match on phys */
  8017. spin_lock_irq(&phba->hbalock);
  8018. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8019. if (mp->phys == phys) {
  8020. list_del_init(&mp->list);
  8021. pring->postbufq_cnt--;
  8022. spin_unlock_irq(&phba->hbalock);
  8023. return mp;
  8024. }
  8025. }
  8026. spin_unlock_irq(&phba->hbalock);
  8027. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8028. "0410 Cannot find virtual addr for mapped buf on "
  8029. "ring %d Data x%llx x%p x%p x%x\n",
  8030. pring->ringno, (unsigned long long)phys,
  8031. slp->next, slp->prev, pring->postbufq_cnt);
  8032. return NULL;
  8033. }
  8034. /**
  8035. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8036. * @phba: Pointer to HBA context object.
  8037. * @cmdiocb: Pointer to driver command iocb object.
  8038. * @rspiocb: Pointer to driver response iocb object.
  8039. *
  8040. * This function is the completion handler for the abort iocbs for
  8041. * ELS commands. This function is called from the ELS ring event
  8042. * handler with no lock held. This function frees memory resources
  8043. * associated with the abort iocb.
  8044. **/
  8045. static void
  8046. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8047. struct lpfc_iocbq *rspiocb)
  8048. {
  8049. IOCB_t *irsp = &rspiocb->iocb;
  8050. uint16_t abort_iotag, abort_context;
  8051. struct lpfc_iocbq *abort_iocb;
  8052. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8053. abort_iocb = NULL;
  8054. if (irsp->ulpStatus) {
  8055. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8056. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8057. spin_lock_irq(&phba->hbalock);
  8058. if (phba->sli_rev < LPFC_SLI_REV4) {
  8059. if (abort_iotag != 0 &&
  8060. abort_iotag <= phba->sli.last_iotag)
  8061. abort_iocb =
  8062. phba->sli.iocbq_lookup[abort_iotag];
  8063. } else
  8064. /* For sli4 the abort_tag is the XRI,
  8065. * so the abort routine puts the iotag of the iocb
  8066. * being aborted in the context field of the abort
  8067. * IOCB.
  8068. */
  8069. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8070. /*
  8071. * If the iocb is not found in Firmware queue the iocb
  8072. * might have completed already. Do not free it again.
  8073. */
  8074. if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
  8075. if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
  8076. spin_unlock_irq(&phba->hbalock);
  8077. lpfc_sli_release_iocbq(phba, cmdiocb);
  8078. return;
  8079. }
  8080. /* For SLI4 the ulpContext field for abort IOCB
  8081. * holds the iotag of the IOCB being aborted so
  8082. * the local abort_context needs to be reset to
  8083. * match the aborted IOCBs ulpContext.
  8084. */
  8085. if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
  8086. abort_context = abort_iocb->iocb.ulpContext;
  8087. }
  8088. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8089. "0327 Cannot abort els iocb %p "
  8090. "with tag %x context %x, abort status %x, "
  8091. "abort code %x\n",
  8092. abort_iocb, abort_iotag, abort_context,
  8093. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8094. /*
  8095. * make sure we have the right iocbq before taking it
  8096. * off the txcmplq and try to call completion routine.
  8097. */
  8098. if (!abort_iocb ||
  8099. abort_iocb->iocb.ulpContext != abort_context ||
  8100. (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
  8101. spin_unlock_irq(&phba->hbalock);
  8102. else if (phba->sli_rev < LPFC_SLI_REV4) {
  8103. /*
  8104. * leave the SLI4 aborted command on the txcmplq
  8105. * list and the command complete WCQE's XB bit
  8106. * will tell whether the SGL (XRI) can be released
  8107. * immediately or to the aborted SGL list for the
  8108. * following abort XRI from the HBA.
  8109. */
  8110. list_del_init(&abort_iocb->list);
  8111. if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
  8112. abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
  8113. pring->txcmplq_cnt--;
  8114. }
  8115. /* Firmware could still be in progress of DMAing
  8116. * payload, so don't free data buffer till after
  8117. * a hbeat.
  8118. */
  8119. abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
  8120. abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  8121. spin_unlock_irq(&phba->hbalock);
  8122. abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  8123. abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
  8124. (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
  8125. } else
  8126. spin_unlock_irq(&phba->hbalock);
  8127. }
  8128. lpfc_sli_release_iocbq(phba, cmdiocb);
  8129. return;
  8130. }
  8131. /**
  8132. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8133. * @phba: Pointer to HBA context object.
  8134. * @cmdiocb: Pointer to driver command iocb object.
  8135. * @rspiocb: Pointer to driver response iocb object.
  8136. *
  8137. * The function is called from SLI ring event handler with no
  8138. * lock held. This function is the completion handler for ELS commands
  8139. * which are aborted. The function frees memory resources used for
  8140. * the aborted ELS commands.
  8141. **/
  8142. static void
  8143. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8144. struct lpfc_iocbq *rspiocb)
  8145. {
  8146. IOCB_t *irsp = &rspiocb->iocb;
  8147. /* ELS cmd tag <ulpIoTag> completes */
  8148. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8149. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8150. "x%x x%x x%x\n",
  8151. irsp->ulpIoTag, irsp->ulpStatus,
  8152. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8153. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8154. lpfc_ct_free_iocb(phba, cmdiocb);
  8155. else
  8156. lpfc_els_free_iocb(phba, cmdiocb);
  8157. return;
  8158. }
  8159. /**
  8160. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8161. * @phba: Pointer to HBA context object.
  8162. * @pring: Pointer to driver SLI ring object.
  8163. * @cmdiocb: Pointer to driver command iocb object.
  8164. *
  8165. * This function issues an abort iocb for the provided command iocb down to
  8166. * the port. Other than the case the outstanding command iocb is an abort
  8167. * request, this function issues abort out unconditionally. This function is
  8168. * called with hbalock held. The function returns 0 when it fails due to
  8169. * memory allocation failure or when the command iocb is an abort request.
  8170. **/
  8171. static int
  8172. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8173. struct lpfc_iocbq *cmdiocb)
  8174. {
  8175. struct lpfc_vport *vport = cmdiocb->vport;
  8176. struct lpfc_iocbq *abtsiocbp;
  8177. IOCB_t *icmd = NULL;
  8178. IOCB_t *iabt = NULL;
  8179. int retval;
  8180. /*
  8181. * There are certain command types we don't want to abort. And we
  8182. * don't want to abort commands that are already in the process of
  8183. * being aborted.
  8184. */
  8185. icmd = &cmdiocb->iocb;
  8186. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8187. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8188. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8189. return 0;
  8190. /* issue ABTS for this IOCB based on iotag */
  8191. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8192. if (abtsiocbp == NULL)
  8193. return 0;
  8194. /* This signals the response to set the correct status
  8195. * before calling the completion handler
  8196. */
  8197. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8198. iabt = &abtsiocbp->iocb;
  8199. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8200. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8201. if (phba->sli_rev == LPFC_SLI_REV4) {
  8202. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8203. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8204. }
  8205. else
  8206. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8207. iabt->ulpLe = 1;
  8208. iabt->ulpClass = icmd->ulpClass;
  8209. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8210. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8211. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8212. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8213. if (phba->link_state >= LPFC_LINK_UP)
  8214. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8215. else
  8216. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8217. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8218. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8219. "0339 Abort xri x%x, original iotag x%x, "
  8220. "abort cmd iotag x%x\n",
  8221. iabt->un.acxri.abortIoTag,
  8222. iabt->un.acxri.abortContextTag,
  8223. abtsiocbp->iotag);
  8224. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8225. if (retval)
  8226. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8227. /*
  8228. * Caller to this routine should check for IOCB_ERROR
  8229. * and handle it properly. This routine no longer removes
  8230. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8231. */
  8232. return retval;
  8233. }
  8234. /**
  8235. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8236. * @phba: Pointer to HBA context object.
  8237. * @pring: Pointer to driver SLI ring object.
  8238. * @cmdiocb: Pointer to driver command iocb object.
  8239. *
  8240. * This function issues an abort iocb for the provided command iocb. In case
  8241. * of unloading, the abort iocb will not be issued to commands on the ELS
  8242. * ring. Instead, the callback function shall be changed to those commands
  8243. * so that nothing happens when them finishes. This function is called with
  8244. * hbalock held. The function returns 0 when the command iocb is an abort
  8245. * request.
  8246. **/
  8247. int
  8248. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8249. struct lpfc_iocbq *cmdiocb)
  8250. {
  8251. struct lpfc_vport *vport = cmdiocb->vport;
  8252. int retval = IOCB_ERROR;
  8253. IOCB_t *icmd = NULL;
  8254. /*
  8255. * There are certain command types we don't want to abort. And we
  8256. * don't want to abort commands that are already in the process of
  8257. * being aborted.
  8258. */
  8259. icmd = &cmdiocb->iocb;
  8260. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8261. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8262. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8263. return 0;
  8264. /*
  8265. * If we're unloading, don't abort iocb on the ELS ring, but change
  8266. * the callback so that nothing happens when it finishes.
  8267. */
  8268. if ((vport->load_flag & FC_UNLOADING) &&
  8269. (pring->ringno == LPFC_ELS_RING)) {
  8270. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8271. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8272. else
  8273. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8274. goto abort_iotag_exit;
  8275. }
  8276. /* Now, we try to issue the abort to the cmdiocb out */
  8277. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8278. abort_iotag_exit:
  8279. /*
  8280. * Caller to this routine should check for IOCB_ERROR
  8281. * and handle it properly. This routine no longer removes
  8282. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8283. */
  8284. return retval;
  8285. }
  8286. /**
  8287. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8288. * @phba: Pointer to HBA context object.
  8289. * @pring: Pointer to driver SLI ring object.
  8290. *
  8291. * This function aborts all iocbs in the given ring and frees all the iocb
  8292. * objects in txq. This function issues abort iocbs unconditionally for all
  8293. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8294. * to complete before the return of this function. The caller is not required
  8295. * to hold any locks.
  8296. **/
  8297. static void
  8298. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8299. {
  8300. LIST_HEAD(completions);
  8301. struct lpfc_iocbq *iocb, *next_iocb;
  8302. if (pring->ringno == LPFC_ELS_RING)
  8303. lpfc_fabric_abort_hba(phba);
  8304. spin_lock_irq(&phba->hbalock);
  8305. /* Take off all the iocbs on txq for cancelling */
  8306. list_splice_init(&pring->txq, &completions);
  8307. pring->txq_cnt = 0;
  8308. /* Next issue ABTS for everything on the txcmplq */
  8309. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8310. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8311. spin_unlock_irq(&phba->hbalock);
  8312. /* Cancel all the IOCBs from the completions list */
  8313. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8314. IOERR_SLI_ABORTED);
  8315. }
  8316. /**
  8317. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8318. * @phba: pointer to lpfc HBA data structure.
  8319. *
  8320. * This routine will abort all pending and outstanding iocbs to an HBA.
  8321. **/
  8322. void
  8323. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8324. {
  8325. struct lpfc_sli *psli = &phba->sli;
  8326. struct lpfc_sli_ring *pring;
  8327. int i;
  8328. for (i = 0; i < psli->num_rings; i++) {
  8329. pring = &psli->ring[i];
  8330. lpfc_sli_iocb_ring_abort(phba, pring);
  8331. }
  8332. }
  8333. /**
  8334. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8335. * @iocbq: Pointer to driver iocb object.
  8336. * @vport: Pointer to driver virtual port object.
  8337. * @tgt_id: SCSI ID of the target.
  8338. * @lun_id: LUN ID of the scsi device.
  8339. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8340. *
  8341. * This function acts as an iocb filter for functions which abort or count
  8342. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8343. * 0 if the filtering criteria is met for the given iocb and will return
  8344. * 1 if the filtering criteria is not met.
  8345. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8346. * given iocb is for the SCSI device specified by vport, tgt_id and
  8347. * lun_id parameter.
  8348. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8349. * given iocb is for the SCSI target specified by vport and tgt_id
  8350. * parameters.
  8351. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8352. * given iocb is for the SCSI host associated with the given vport.
  8353. * This function is called with no locks held.
  8354. **/
  8355. static int
  8356. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8357. uint16_t tgt_id, uint64_t lun_id,
  8358. lpfc_ctx_cmd ctx_cmd)
  8359. {
  8360. struct lpfc_scsi_buf *lpfc_cmd;
  8361. int rc = 1;
  8362. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8363. return rc;
  8364. if (iocbq->vport != vport)
  8365. return rc;
  8366. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8367. if (lpfc_cmd->pCmd == NULL)
  8368. return rc;
  8369. switch (ctx_cmd) {
  8370. case LPFC_CTX_LUN:
  8371. if ((lpfc_cmd->rdata->pnode) &&
  8372. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8373. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8374. rc = 0;
  8375. break;
  8376. case LPFC_CTX_TGT:
  8377. if ((lpfc_cmd->rdata->pnode) &&
  8378. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8379. rc = 0;
  8380. break;
  8381. case LPFC_CTX_HOST:
  8382. rc = 0;
  8383. break;
  8384. default:
  8385. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8386. __func__, ctx_cmd);
  8387. break;
  8388. }
  8389. return rc;
  8390. }
  8391. /**
  8392. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8393. * @vport: Pointer to virtual port.
  8394. * @tgt_id: SCSI ID of the target.
  8395. * @lun_id: LUN ID of the scsi device.
  8396. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8397. *
  8398. * This function returns number of FCP commands pending for the vport.
  8399. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8400. * commands pending on the vport associated with SCSI device specified
  8401. * by tgt_id and lun_id parameters.
  8402. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8403. * commands pending on the vport associated with SCSI target specified
  8404. * by tgt_id parameter.
  8405. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8406. * commands pending on the vport.
  8407. * This function returns the number of iocbs which satisfy the filter.
  8408. * This function is called without any lock held.
  8409. **/
  8410. int
  8411. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8412. lpfc_ctx_cmd ctx_cmd)
  8413. {
  8414. struct lpfc_hba *phba = vport->phba;
  8415. struct lpfc_iocbq *iocbq;
  8416. int sum, i;
  8417. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8418. iocbq = phba->sli.iocbq_lookup[i];
  8419. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8420. ctx_cmd) == 0)
  8421. sum++;
  8422. }
  8423. return sum;
  8424. }
  8425. /**
  8426. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8427. * @phba: Pointer to HBA context object
  8428. * @cmdiocb: Pointer to command iocb object.
  8429. * @rspiocb: Pointer to response iocb object.
  8430. *
  8431. * This function is called when an aborted FCP iocb completes. This
  8432. * function is called by the ring event handler with no lock held.
  8433. * This function frees the iocb.
  8434. **/
  8435. void
  8436. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8437. struct lpfc_iocbq *rspiocb)
  8438. {
  8439. lpfc_sli_release_iocbq(phba, cmdiocb);
  8440. return;
  8441. }
  8442. /**
  8443. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8444. * @vport: Pointer to virtual port.
  8445. * @pring: Pointer to driver SLI ring object.
  8446. * @tgt_id: SCSI ID of the target.
  8447. * @lun_id: LUN ID of the scsi device.
  8448. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8449. *
  8450. * This function sends an abort command for every SCSI command
  8451. * associated with the given virtual port pending on the ring
  8452. * filtered by lpfc_sli_validate_fcp_iocb function.
  8453. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8454. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8455. * parameters
  8456. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8457. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8458. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8459. * FCP iocbs associated with virtual port.
  8460. * This function returns number of iocbs it failed to abort.
  8461. * This function is called with no locks held.
  8462. **/
  8463. int
  8464. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8465. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8466. {
  8467. struct lpfc_hba *phba = vport->phba;
  8468. struct lpfc_iocbq *iocbq;
  8469. struct lpfc_iocbq *abtsiocb;
  8470. IOCB_t *cmd = NULL;
  8471. int errcnt = 0, ret_val = 0;
  8472. int i;
  8473. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8474. iocbq = phba->sli.iocbq_lookup[i];
  8475. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8476. abort_cmd) != 0)
  8477. continue;
  8478. /* issue ABTS for this IOCB based on iotag */
  8479. abtsiocb = lpfc_sli_get_iocbq(phba);
  8480. if (abtsiocb == NULL) {
  8481. errcnt++;
  8482. continue;
  8483. }
  8484. cmd = &iocbq->iocb;
  8485. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8486. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8487. if (phba->sli_rev == LPFC_SLI_REV4)
  8488. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8489. else
  8490. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8491. abtsiocb->iocb.ulpLe = 1;
  8492. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8493. abtsiocb->vport = phba->pport;
  8494. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8495. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8496. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8497. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8498. if (lpfc_is_link_up(phba))
  8499. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8500. else
  8501. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8502. /* Setup callback routine and issue the command. */
  8503. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8504. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8505. abtsiocb, 0);
  8506. if (ret_val == IOCB_ERROR) {
  8507. lpfc_sli_release_iocbq(phba, abtsiocb);
  8508. errcnt++;
  8509. continue;
  8510. }
  8511. }
  8512. return errcnt;
  8513. }
  8514. /**
  8515. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8516. * @phba: Pointer to HBA context object.
  8517. * @cmdiocbq: Pointer to command iocb.
  8518. * @rspiocbq: Pointer to response iocb.
  8519. *
  8520. * This function is the completion handler for iocbs issued using
  8521. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8522. * ring event handler function without any lock held. This function
  8523. * can be called from both worker thread context and interrupt
  8524. * context. This function also can be called from other thread which
  8525. * cleans up the SLI layer objects.
  8526. * This function copy the contents of the response iocb to the
  8527. * response iocb memory object provided by the caller of
  8528. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8529. * sleeps for the iocb completion.
  8530. **/
  8531. static void
  8532. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8533. struct lpfc_iocbq *cmdiocbq,
  8534. struct lpfc_iocbq *rspiocbq)
  8535. {
  8536. wait_queue_head_t *pdone_q;
  8537. unsigned long iflags;
  8538. struct lpfc_scsi_buf *lpfc_cmd;
  8539. spin_lock_irqsave(&phba->hbalock, iflags);
  8540. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8541. if (cmdiocbq->context2 && rspiocbq)
  8542. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8543. &rspiocbq->iocb, sizeof(IOCB_t));
  8544. /* Set the exchange busy flag for task management commands */
  8545. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8546. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8547. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8548. cur_iocbq);
  8549. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8550. }
  8551. pdone_q = cmdiocbq->context_un.wait_queue;
  8552. if (pdone_q)
  8553. wake_up(pdone_q);
  8554. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8555. return;
  8556. }
  8557. /**
  8558. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8559. * @phba: Pointer to HBA context object..
  8560. * @piocbq: Pointer to command iocb.
  8561. * @flag: Flag to test.
  8562. *
  8563. * This routine grabs the hbalock and then test the iocb_flag to
  8564. * see if the passed in flag is set.
  8565. * Returns:
  8566. * 1 if flag is set.
  8567. * 0 if flag is not set.
  8568. **/
  8569. static int
  8570. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8571. struct lpfc_iocbq *piocbq, uint32_t flag)
  8572. {
  8573. unsigned long iflags;
  8574. int ret;
  8575. spin_lock_irqsave(&phba->hbalock, iflags);
  8576. ret = piocbq->iocb_flag & flag;
  8577. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8578. return ret;
  8579. }
  8580. /**
  8581. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8582. * @phba: Pointer to HBA context object..
  8583. * @pring: Pointer to sli ring.
  8584. * @piocb: Pointer to command iocb.
  8585. * @prspiocbq: Pointer to response iocb.
  8586. * @timeout: Timeout in number of seconds.
  8587. *
  8588. * This function issues the iocb to firmware and waits for the
  8589. * iocb to complete. If the iocb command is not
  8590. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8591. * Caller should not free the iocb resources if this function
  8592. * returns IOCB_TIMEDOUT.
  8593. * The function waits for the iocb completion using an
  8594. * non-interruptible wait.
  8595. * This function will sleep while waiting for iocb completion.
  8596. * So, this function should not be called from any context which
  8597. * does not allow sleeping. Due to the same reason, this function
  8598. * cannot be called with interrupt disabled.
  8599. * This function assumes that the iocb completions occur while
  8600. * this function sleep. So, this function cannot be called from
  8601. * the thread which process iocb completion for this ring.
  8602. * This function clears the iocb_flag of the iocb object before
  8603. * issuing the iocb and the iocb completion handler sets this
  8604. * flag and wakes this thread when the iocb completes.
  8605. * The contents of the response iocb will be copied to prspiocbq
  8606. * by the completion handler when the command completes.
  8607. * This function returns IOCB_SUCCESS when success.
  8608. * This function is called with no lock held.
  8609. **/
  8610. int
  8611. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8612. uint32_t ring_number,
  8613. struct lpfc_iocbq *piocb,
  8614. struct lpfc_iocbq *prspiocbq,
  8615. uint32_t timeout)
  8616. {
  8617. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8618. long timeleft, timeout_req = 0;
  8619. int retval = IOCB_SUCCESS;
  8620. uint32_t creg_val;
  8621. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8622. /*
  8623. * If the caller has provided a response iocbq buffer, then context2
  8624. * is NULL or its an error.
  8625. */
  8626. if (prspiocbq) {
  8627. if (piocb->context2)
  8628. return IOCB_ERROR;
  8629. piocb->context2 = prspiocbq;
  8630. }
  8631. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8632. piocb->context_un.wait_queue = &done_q;
  8633. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8634. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8635. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8636. return IOCB_ERROR;
  8637. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8638. writel(creg_val, phba->HCregaddr);
  8639. readl(phba->HCregaddr); /* flush */
  8640. }
  8641. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8642. SLI_IOCB_RET_IOCB);
  8643. if (retval == IOCB_SUCCESS) {
  8644. timeout_req = timeout * HZ;
  8645. timeleft = wait_event_timeout(done_q,
  8646. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8647. timeout_req);
  8648. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8649. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8650. "0331 IOCB wake signaled\n");
  8651. } else if (timeleft == 0) {
  8652. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8653. "0338 IOCB wait timeout error - no "
  8654. "wake response Data x%x\n", timeout);
  8655. retval = IOCB_TIMEDOUT;
  8656. } else {
  8657. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8658. "0330 IOCB wake NOT set, "
  8659. "Data x%x x%lx\n",
  8660. timeout, (timeleft / jiffies));
  8661. retval = IOCB_TIMEDOUT;
  8662. }
  8663. } else if (retval == IOCB_BUSY) {
  8664. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8665. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8666. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8667. return retval;
  8668. } else {
  8669. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8670. "0332 IOCB wait issue failed, Data x%x\n",
  8671. retval);
  8672. retval = IOCB_ERROR;
  8673. }
  8674. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8675. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8676. return IOCB_ERROR;
  8677. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8678. writel(creg_val, phba->HCregaddr);
  8679. readl(phba->HCregaddr); /* flush */
  8680. }
  8681. if (prspiocbq)
  8682. piocb->context2 = NULL;
  8683. piocb->context_un.wait_queue = NULL;
  8684. piocb->iocb_cmpl = NULL;
  8685. return retval;
  8686. }
  8687. /**
  8688. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  8689. * @phba: Pointer to HBA context object.
  8690. * @pmboxq: Pointer to driver mailbox object.
  8691. * @timeout: Timeout in number of seconds.
  8692. *
  8693. * This function issues the mailbox to firmware and waits for the
  8694. * mailbox command to complete. If the mailbox command is not
  8695. * completed within timeout seconds, it returns MBX_TIMEOUT.
  8696. * The function waits for the mailbox completion using an
  8697. * interruptible wait. If the thread is woken up due to a
  8698. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  8699. * should not free the mailbox resources, if this function returns
  8700. * MBX_TIMEOUT.
  8701. * This function will sleep while waiting for mailbox completion.
  8702. * So, this function should not be called from any context which
  8703. * does not allow sleeping. Due to the same reason, this function
  8704. * cannot be called with interrupt disabled.
  8705. * This function assumes that the mailbox completion occurs while
  8706. * this function sleep. So, this function cannot be called from
  8707. * the worker thread which processes mailbox completion.
  8708. * This function is called in the context of HBA management
  8709. * applications.
  8710. * This function returns MBX_SUCCESS when successful.
  8711. * This function is called with no lock held.
  8712. **/
  8713. int
  8714. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  8715. uint32_t timeout)
  8716. {
  8717. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8718. int retval;
  8719. unsigned long flag;
  8720. /* The caller must leave context1 empty. */
  8721. if (pmboxq->context1)
  8722. return MBX_NOT_FINISHED;
  8723. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  8724. /* setup wake call as IOCB callback */
  8725. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  8726. /* setup context field to pass wait_queue pointer to wake function */
  8727. pmboxq->context1 = &done_q;
  8728. /* now issue the command */
  8729. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  8730. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  8731. wait_event_interruptible_timeout(done_q,
  8732. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  8733. timeout * HZ);
  8734. spin_lock_irqsave(&phba->hbalock, flag);
  8735. pmboxq->context1 = NULL;
  8736. /*
  8737. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  8738. * else do not free the resources.
  8739. */
  8740. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  8741. retval = MBX_SUCCESS;
  8742. lpfc_sli4_swap_str(phba, pmboxq);
  8743. } else {
  8744. retval = MBX_TIMEOUT;
  8745. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  8746. }
  8747. spin_unlock_irqrestore(&phba->hbalock, flag);
  8748. }
  8749. return retval;
  8750. }
  8751. /**
  8752. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  8753. * @phba: Pointer to HBA context.
  8754. *
  8755. * This function is called to shutdown the driver's mailbox sub-system.
  8756. * It first marks the mailbox sub-system is in a block state to prevent
  8757. * the asynchronous mailbox command from issued off the pending mailbox
  8758. * command queue. If the mailbox command sub-system shutdown is due to
  8759. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  8760. * the mailbox sub-system flush routine to forcefully bring down the
  8761. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  8762. * as with offline or HBA function reset), this routine will wait for the
  8763. * outstanding mailbox command to complete before invoking the mailbox
  8764. * sub-system flush routine to gracefully bring down mailbox sub-system.
  8765. **/
  8766. void
  8767. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  8768. {
  8769. struct lpfc_sli *psli = &phba->sli;
  8770. unsigned long timeout;
  8771. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  8772. spin_lock_irq(&phba->hbalock);
  8773. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  8774. spin_unlock_irq(&phba->hbalock);
  8775. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  8776. spin_lock_irq(&phba->hbalock);
  8777. /* Determine how long we might wait for the active mailbox
  8778. * command to be gracefully completed by firmware.
  8779. */
  8780. if (phba->sli.mbox_active)
  8781. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  8782. phba->sli.mbox_active) *
  8783. 1000) + jiffies;
  8784. spin_unlock_irq(&phba->hbalock);
  8785. while (phba->sli.mbox_active) {
  8786. /* Check active mailbox complete status every 2ms */
  8787. msleep(2);
  8788. if (time_after(jiffies, timeout))
  8789. /* Timeout, let the mailbox flush routine to
  8790. * forcefully release active mailbox command
  8791. */
  8792. break;
  8793. }
  8794. }
  8795. lpfc_sli_mbox_sys_flush(phba);
  8796. }
  8797. /**
  8798. * lpfc_sli_eratt_read - read sli-3 error attention events
  8799. * @phba: Pointer to HBA context.
  8800. *
  8801. * This function is called to read the SLI3 device error attention registers
  8802. * for possible error attention events. The caller must hold the hostlock
  8803. * with spin_lock_irq().
  8804. *
  8805. * This function returns 1 when there is Error Attention in the Host Attention
  8806. * Register and returns 0 otherwise.
  8807. **/
  8808. static int
  8809. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  8810. {
  8811. uint32_t ha_copy;
  8812. /* Read chip Host Attention (HA) register */
  8813. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  8814. goto unplug_err;
  8815. if (ha_copy & HA_ERATT) {
  8816. /* Read host status register to retrieve error event */
  8817. if (lpfc_sli_read_hs(phba))
  8818. goto unplug_err;
  8819. /* Check if there is a deferred error condition is active */
  8820. if ((HS_FFER1 & phba->work_hs) &&
  8821. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  8822. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  8823. phba->hba_flag |= DEFER_ERATT;
  8824. /* Clear all interrupt enable conditions */
  8825. writel(0, phba->HCregaddr);
  8826. readl(phba->HCregaddr);
  8827. }
  8828. /* Set the driver HA work bitmap */
  8829. phba->work_ha |= HA_ERATT;
  8830. /* Indicate polling handles this ERATT */
  8831. phba->hba_flag |= HBA_ERATT_HANDLED;
  8832. return 1;
  8833. }
  8834. return 0;
  8835. unplug_err:
  8836. /* Set the driver HS work bitmap */
  8837. phba->work_hs |= UNPLUG_ERR;
  8838. /* Set the driver HA work bitmap */
  8839. phba->work_ha |= HA_ERATT;
  8840. /* Indicate polling handles this ERATT */
  8841. phba->hba_flag |= HBA_ERATT_HANDLED;
  8842. return 1;
  8843. }
  8844. /**
  8845. * lpfc_sli4_eratt_read - read sli-4 error attention events
  8846. * @phba: Pointer to HBA context.
  8847. *
  8848. * This function is called to read the SLI4 device error attention registers
  8849. * for possible error attention events. The caller must hold the hostlock
  8850. * with spin_lock_irq().
  8851. *
  8852. * This function returns 1 when there is Error Attention in the Host Attention
  8853. * Register and returns 0 otherwise.
  8854. **/
  8855. static int
  8856. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  8857. {
  8858. uint32_t uerr_sta_hi, uerr_sta_lo;
  8859. uint32_t if_type, portsmphr;
  8860. struct lpfc_register portstat_reg;
  8861. /*
  8862. * For now, use the SLI4 device internal unrecoverable error
  8863. * registers for error attention. This can be changed later.
  8864. */
  8865. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  8866. switch (if_type) {
  8867. case LPFC_SLI_INTF_IF_TYPE_0:
  8868. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  8869. &uerr_sta_lo) ||
  8870. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  8871. &uerr_sta_hi)) {
  8872. phba->work_hs |= UNPLUG_ERR;
  8873. phba->work_ha |= HA_ERATT;
  8874. phba->hba_flag |= HBA_ERATT_HANDLED;
  8875. return 1;
  8876. }
  8877. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  8878. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  8879. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8880. "1423 HBA Unrecoverable error: "
  8881. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  8882. "ue_mask_lo_reg=0x%x, "
  8883. "ue_mask_hi_reg=0x%x\n",
  8884. uerr_sta_lo, uerr_sta_hi,
  8885. phba->sli4_hba.ue_mask_lo,
  8886. phba->sli4_hba.ue_mask_hi);
  8887. phba->work_status[0] = uerr_sta_lo;
  8888. phba->work_status[1] = uerr_sta_hi;
  8889. phba->work_ha |= HA_ERATT;
  8890. phba->hba_flag |= HBA_ERATT_HANDLED;
  8891. return 1;
  8892. }
  8893. break;
  8894. case LPFC_SLI_INTF_IF_TYPE_2:
  8895. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  8896. &portstat_reg.word0) ||
  8897. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  8898. &portsmphr)){
  8899. phba->work_hs |= UNPLUG_ERR;
  8900. phba->work_ha |= HA_ERATT;
  8901. phba->hba_flag |= HBA_ERATT_HANDLED;
  8902. return 1;
  8903. }
  8904. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  8905. phba->work_status[0] =
  8906. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  8907. phba->work_status[1] =
  8908. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  8909. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8910. "2885 Port Error Detected: "
  8911. "port status reg 0x%x, "
  8912. "port smphr reg 0x%x, "
  8913. "error 1=0x%x, error 2=0x%x\n",
  8914. portstat_reg.word0,
  8915. portsmphr,
  8916. phba->work_status[0],
  8917. phba->work_status[1]);
  8918. phba->work_ha |= HA_ERATT;
  8919. phba->hba_flag |= HBA_ERATT_HANDLED;
  8920. return 1;
  8921. }
  8922. break;
  8923. case LPFC_SLI_INTF_IF_TYPE_1:
  8924. default:
  8925. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8926. "2886 HBA Error Attention on unsupported "
  8927. "if type %d.", if_type);
  8928. return 1;
  8929. }
  8930. return 0;
  8931. }
  8932. /**
  8933. * lpfc_sli_check_eratt - check error attention events
  8934. * @phba: Pointer to HBA context.
  8935. *
  8936. * This function is called from timer soft interrupt context to check HBA's
  8937. * error attention register bit for error attention events.
  8938. *
  8939. * This function returns 1 when there is Error Attention in the Host Attention
  8940. * Register and returns 0 otherwise.
  8941. **/
  8942. int
  8943. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  8944. {
  8945. uint32_t ha_copy;
  8946. /* If somebody is waiting to handle an eratt, don't process it
  8947. * here. The brdkill function will do this.
  8948. */
  8949. if (phba->link_flag & LS_IGNORE_ERATT)
  8950. return 0;
  8951. /* Check if interrupt handler handles this ERATT */
  8952. spin_lock_irq(&phba->hbalock);
  8953. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  8954. /* Interrupt handler has handled ERATT */
  8955. spin_unlock_irq(&phba->hbalock);
  8956. return 0;
  8957. }
  8958. /*
  8959. * If there is deferred error attention, do not check for error
  8960. * attention
  8961. */
  8962. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  8963. spin_unlock_irq(&phba->hbalock);
  8964. return 0;
  8965. }
  8966. /* If PCI channel is offline, don't process it */
  8967. if (unlikely(pci_channel_offline(phba->pcidev))) {
  8968. spin_unlock_irq(&phba->hbalock);
  8969. return 0;
  8970. }
  8971. switch (phba->sli_rev) {
  8972. case LPFC_SLI_REV2:
  8973. case LPFC_SLI_REV3:
  8974. /* Read chip Host Attention (HA) register */
  8975. ha_copy = lpfc_sli_eratt_read(phba);
  8976. break;
  8977. case LPFC_SLI_REV4:
  8978. /* Read device Uncoverable Error (UERR) registers */
  8979. ha_copy = lpfc_sli4_eratt_read(phba);
  8980. break;
  8981. default:
  8982. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8983. "0299 Invalid SLI revision (%d)\n",
  8984. phba->sli_rev);
  8985. ha_copy = 0;
  8986. break;
  8987. }
  8988. spin_unlock_irq(&phba->hbalock);
  8989. return ha_copy;
  8990. }
  8991. /**
  8992. * lpfc_intr_state_check - Check device state for interrupt handling
  8993. * @phba: Pointer to HBA context.
  8994. *
  8995. * This inline routine checks whether a device or its PCI slot is in a state
  8996. * that the interrupt should be handled.
  8997. *
  8998. * This function returns 0 if the device or the PCI slot is in a state that
  8999. * interrupt should be handled, otherwise -EIO.
  9000. */
  9001. static inline int
  9002. lpfc_intr_state_check(struct lpfc_hba *phba)
  9003. {
  9004. /* If the pci channel is offline, ignore all the interrupts */
  9005. if (unlikely(pci_channel_offline(phba->pcidev)))
  9006. return -EIO;
  9007. /* Update device level interrupt statistics */
  9008. phba->sli.slistat.sli_intr++;
  9009. /* Ignore all interrupts during initialization. */
  9010. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9011. return -EIO;
  9012. return 0;
  9013. }
  9014. /**
  9015. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9016. * @irq: Interrupt number.
  9017. * @dev_id: The device context pointer.
  9018. *
  9019. * This function is directly called from the PCI layer as an interrupt
  9020. * service routine when device with SLI-3 interface spec is enabled with
  9021. * MSI-X multi-message interrupt mode and there are slow-path events in
  9022. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9023. * interrupt mode, this function is called as part of the device-level
  9024. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9025. * is undergoing initialization, the interrupt handler will not process
  9026. * the interrupt. The link attention and ELS ring attention events are
  9027. * handled by the worker thread. The interrupt handler signals the worker
  9028. * thread and returns for these events. This function is called without
  9029. * any lock held. It gets the hbalock to access and update SLI data
  9030. * structures.
  9031. *
  9032. * This function returns IRQ_HANDLED when interrupt is handled else it
  9033. * returns IRQ_NONE.
  9034. **/
  9035. irqreturn_t
  9036. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9037. {
  9038. struct lpfc_hba *phba;
  9039. uint32_t ha_copy, hc_copy;
  9040. uint32_t work_ha_copy;
  9041. unsigned long status;
  9042. unsigned long iflag;
  9043. uint32_t control;
  9044. MAILBOX_t *mbox, *pmbox;
  9045. struct lpfc_vport *vport;
  9046. struct lpfc_nodelist *ndlp;
  9047. struct lpfc_dmabuf *mp;
  9048. LPFC_MBOXQ_t *pmb;
  9049. int rc;
  9050. /*
  9051. * Get the driver's phba structure from the dev_id and
  9052. * assume the HBA is not interrupting.
  9053. */
  9054. phba = (struct lpfc_hba *)dev_id;
  9055. if (unlikely(!phba))
  9056. return IRQ_NONE;
  9057. /*
  9058. * Stuff needs to be attented to when this function is invoked as an
  9059. * individual interrupt handler in MSI-X multi-message interrupt mode
  9060. */
  9061. if (phba->intr_type == MSIX) {
  9062. /* Check device state for handling interrupt */
  9063. if (lpfc_intr_state_check(phba))
  9064. return IRQ_NONE;
  9065. /* Need to read HA REG for slow-path events */
  9066. spin_lock_irqsave(&phba->hbalock, iflag);
  9067. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9068. goto unplug_error;
  9069. /* If somebody is waiting to handle an eratt don't process it
  9070. * here. The brdkill function will do this.
  9071. */
  9072. if (phba->link_flag & LS_IGNORE_ERATT)
  9073. ha_copy &= ~HA_ERATT;
  9074. /* Check the need for handling ERATT in interrupt handler */
  9075. if (ha_copy & HA_ERATT) {
  9076. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9077. /* ERATT polling has handled ERATT */
  9078. ha_copy &= ~HA_ERATT;
  9079. else
  9080. /* Indicate interrupt handler handles ERATT */
  9081. phba->hba_flag |= HBA_ERATT_HANDLED;
  9082. }
  9083. /*
  9084. * If there is deferred error attention, do not check for any
  9085. * interrupt.
  9086. */
  9087. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9088. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9089. return IRQ_NONE;
  9090. }
  9091. /* Clear up only attention source related to slow-path */
  9092. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9093. goto unplug_error;
  9094. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9095. HC_LAINT_ENA | HC_ERINT_ENA),
  9096. phba->HCregaddr);
  9097. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9098. phba->HAregaddr);
  9099. writel(hc_copy, phba->HCregaddr);
  9100. readl(phba->HAregaddr); /* flush */
  9101. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9102. } else
  9103. ha_copy = phba->ha_copy;
  9104. work_ha_copy = ha_copy & phba->work_ha_mask;
  9105. if (work_ha_copy) {
  9106. if (work_ha_copy & HA_LATT) {
  9107. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9108. /*
  9109. * Turn off Link Attention interrupts
  9110. * until CLEAR_LA done
  9111. */
  9112. spin_lock_irqsave(&phba->hbalock, iflag);
  9113. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9114. if (lpfc_readl(phba->HCregaddr, &control))
  9115. goto unplug_error;
  9116. control &= ~HC_LAINT_ENA;
  9117. writel(control, phba->HCregaddr);
  9118. readl(phba->HCregaddr); /* flush */
  9119. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9120. }
  9121. else
  9122. work_ha_copy &= ~HA_LATT;
  9123. }
  9124. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9125. /*
  9126. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9127. * the only slow ring.
  9128. */
  9129. status = (work_ha_copy &
  9130. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9131. status >>= (4*LPFC_ELS_RING);
  9132. if (status & HA_RXMASK) {
  9133. spin_lock_irqsave(&phba->hbalock, iflag);
  9134. if (lpfc_readl(phba->HCregaddr, &control))
  9135. goto unplug_error;
  9136. lpfc_debugfs_slow_ring_trc(phba,
  9137. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9138. control, status,
  9139. (uint32_t)phba->sli.slistat.sli_intr);
  9140. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9141. lpfc_debugfs_slow_ring_trc(phba,
  9142. "ISR Disable ring:"
  9143. "pwork:x%x hawork:x%x wait:x%x",
  9144. phba->work_ha, work_ha_copy,
  9145. (uint32_t)((unsigned long)
  9146. &phba->work_waitq));
  9147. control &=
  9148. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9149. writel(control, phba->HCregaddr);
  9150. readl(phba->HCregaddr); /* flush */
  9151. }
  9152. else {
  9153. lpfc_debugfs_slow_ring_trc(phba,
  9154. "ISR slow ring: pwork:"
  9155. "x%x hawork:x%x wait:x%x",
  9156. phba->work_ha, work_ha_copy,
  9157. (uint32_t)((unsigned long)
  9158. &phba->work_waitq));
  9159. }
  9160. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9161. }
  9162. }
  9163. spin_lock_irqsave(&phba->hbalock, iflag);
  9164. if (work_ha_copy & HA_ERATT) {
  9165. if (lpfc_sli_read_hs(phba))
  9166. goto unplug_error;
  9167. /*
  9168. * Check if there is a deferred error condition
  9169. * is active
  9170. */
  9171. if ((HS_FFER1 & phba->work_hs) &&
  9172. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9173. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9174. phba->work_hs)) {
  9175. phba->hba_flag |= DEFER_ERATT;
  9176. /* Clear all interrupt enable conditions */
  9177. writel(0, phba->HCregaddr);
  9178. readl(phba->HCregaddr);
  9179. }
  9180. }
  9181. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9182. pmb = phba->sli.mbox_active;
  9183. pmbox = &pmb->u.mb;
  9184. mbox = phba->mbox;
  9185. vport = pmb->vport;
  9186. /* First check out the status word */
  9187. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9188. if (pmbox->mbxOwner != OWN_HOST) {
  9189. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9190. /*
  9191. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9192. * mbxStatus <status>
  9193. */
  9194. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9195. LOG_SLI,
  9196. "(%d):0304 Stray Mailbox "
  9197. "Interrupt mbxCommand x%x "
  9198. "mbxStatus x%x\n",
  9199. (vport ? vport->vpi : 0),
  9200. pmbox->mbxCommand,
  9201. pmbox->mbxStatus);
  9202. /* clear mailbox attention bit */
  9203. work_ha_copy &= ~HA_MBATT;
  9204. } else {
  9205. phba->sli.mbox_active = NULL;
  9206. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9207. phba->last_completion_time = jiffies;
  9208. del_timer(&phba->sli.mbox_tmo);
  9209. if (pmb->mbox_cmpl) {
  9210. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9211. MAILBOX_CMD_SIZE);
  9212. if (pmb->out_ext_byte_len &&
  9213. pmb->context2)
  9214. lpfc_sli_pcimem_bcopy(
  9215. phba->mbox_ext,
  9216. pmb->context2,
  9217. pmb->out_ext_byte_len);
  9218. }
  9219. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9220. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9221. lpfc_debugfs_disc_trc(vport,
  9222. LPFC_DISC_TRC_MBOX_VPORT,
  9223. "MBOX dflt rpi: : "
  9224. "status:x%x rpi:x%x",
  9225. (uint32_t)pmbox->mbxStatus,
  9226. pmbox->un.varWords[0], 0);
  9227. if (!pmbox->mbxStatus) {
  9228. mp = (struct lpfc_dmabuf *)
  9229. (pmb->context1);
  9230. ndlp = (struct lpfc_nodelist *)
  9231. pmb->context2;
  9232. /* Reg_LOGIN of dflt RPI was
  9233. * successful. new lets get
  9234. * rid of the RPI using the
  9235. * same mbox buffer.
  9236. */
  9237. lpfc_unreg_login(phba,
  9238. vport->vpi,
  9239. pmbox->un.varWords[0],
  9240. pmb);
  9241. pmb->mbox_cmpl =
  9242. lpfc_mbx_cmpl_dflt_rpi;
  9243. pmb->context1 = mp;
  9244. pmb->context2 = ndlp;
  9245. pmb->vport = vport;
  9246. rc = lpfc_sli_issue_mbox(phba,
  9247. pmb,
  9248. MBX_NOWAIT);
  9249. if (rc != MBX_BUSY)
  9250. lpfc_printf_log(phba,
  9251. KERN_ERR,
  9252. LOG_MBOX | LOG_SLI,
  9253. "0350 rc should have"
  9254. "been MBX_BUSY\n");
  9255. if (rc != MBX_NOT_FINISHED)
  9256. goto send_current_mbox;
  9257. }
  9258. }
  9259. spin_lock_irqsave(
  9260. &phba->pport->work_port_lock,
  9261. iflag);
  9262. phba->pport->work_port_events &=
  9263. ~WORKER_MBOX_TMO;
  9264. spin_unlock_irqrestore(
  9265. &phba->pport->work_port_lock,
  9266. iflag);
  9267. lpfc_mbox_cmpl_put(phba, pmb);
  9268. }
  9269. } else
  9270. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9271. if ((work_ha_copy & HA_MBATT) &&
  9272. (phba->sli.mbox_active == NULL)) {
  9273. send_current_mbox:
  9274. /* Process next mailbox command if there is one */
  9275. do {
  9276. rc = lpfc_sli_issue_mbox(phba, NULL,
  9277. MBX_NOWAIT);
  9278. } while (rc == MBX_NOT_FINISHED);
  9279. if (rc != MBX_SUCCESS)
  9280. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9281. LOG_SLI, "0349 rc should be "
  9282. "MBX_SUCCESS\n");
  9283. }
  9284. spin_lock_irqsave(&phba->hbalock, iflag);
  9285. phba->work_ha |= work_ha_copy;
  9286. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9287. lpfc_worker_wake_up(phba);
  9288. }
  9289. return IRQ_HANDLED;
  9290. unplug_error:
  9291. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9292. return IRQ_HANDLED;
  9293. } /* lpfc_sli_sp_intr_handler */
  9294. /**
  9295. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9296. * @irq: Interrupt number.
  9297. * @dev_id: The device context pointer.
  9298. *
  9299. * This function is directly called from the PCI layer as an interrupt
  9300. * service routine when device with SLI-3 interface spec is enabled with
  9301. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9302. * ring event in the HBA. However, when the device is enabled with either
  9303. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9304. * device-level interrupt handler. When the PCI slot is in error recovery
  9305. * or the HBA is undergoing initialization, the interrupt handler will not
  9306. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9307. * the intrrupt context. This function is called without any lock held.
  9308. * It gets the hbalock to access and update SLI data structures.
  9309. *
  9310. * This function returns IRQ_HANDLED when interrupt is handled else it
  9311. * returns IRQ_NONE.
  9312. **/
  9313. irqreturn_t
  9314. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9315. {
  9316. struct lpfc_hba *phba;
  9317. uint32_t ha_copy;
  9318. unsigned long status;
  9319. unsigned long iflag;
  9320. /* Get the driver's phba structure from the dev_id and
  9321. * assume the HBA is not interrupting.
  9322. */
  9323. phba = (struct lpfc_hba *) dev_id;
  9324. if (unlikely(!phba))
  9325. return IRQ_NONE;
  9326. /*
  9327. * Stuff needs to be attented to when this function is invoked as an
  9328. * individual interrupt handler in MSI-X multi-message interrupt mode
  9329. */
  9330. if (phba->intr_type == MSIX) {
  9331. /* Check device state for handling interrupt */
  9332. if (lpfc_intr_state_check(phba))
  9333. return IRQ_NONE;
  9334. /* Need to read HA REG for FCP ring and other ring events */
  9335. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9336. return IRQ_HANDLED;
  9337. /* Clear up only attention source related to fast-path */
  9338. spin_lock_irqsave(&phba->hbalock, iflag);
  9339. /*
  9340. * If there is deferred error attention, do not check for
  9341. * any interrupt.
  9342. */
  9343. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9344. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9345. return IRQ_NONE;
  9346. }
  9347. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9348. phba->HAregaddr);
  9349. readl(phba->HAregaddr); /* flush */
  9350. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9351. } else
  9352. ha_copy = phba->ha_copy;
  9353. /*
  9354. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9355. */
  9356. ha_copy &= ~(phba->work_ha_mask);
  9357. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9358. status >>= (4*LPFC_FCP_RING);
  9359. if (status & HA_RXMASK)
  9360. lpfc_sli_handle_fast_ring_event(phba,
  9361. &phba->sli.ring[LPFC_FCP_RING],
  9362. status);
  9363. if (phba->cfg_multi_ring_support == 2) {
  9364. /*
  9365. * Process all events on extra ring. Take the optimized path
  9366. * for extra ring IO.
  9367. */
  9368. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9369. status >>= (4*LPFC_EXTRA_RING);
  9370. if (status & HA_RXMASK) {
  9371. lpfc_sli_handle_fast_ring_event(phba,
  9372. &phba->sli.ring[LPFC_EXTRA_RING],
  9373. status);
  9374. }
  9375. }
  9376. return IRQ_HANDLED;
  9377. } /* lpfc_sli_fp_intr_handler */
  9378. /**
  9379. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9380. * @irq: Interrupt number.
  9381. * @dev_id: The device context pointer.
  9382. *
  9383. * This function is the HBA device-level interrupt handler to device with
  9384. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9385. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9386. * requires driver attention. This function invokes the slow-path interrupt
  9387. * attention handling function and fast-path interrupt attention handling
  9388. * function in turn to process the relevant HBA attention events. This
  9389. * function is called without any lock held. It gets the hbalock to access
  9390. * and update SLI data structures.
  9391. *
  9392. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9393. * returns IRQ_NONE.
  9394. **/
  9395. irqreturn_t
  9396. lpfc_sli_intr_handler(int irq, void *dev_id)
  9397. {
  9398. struct lpfc_hba *phba;
  9399. irqreturn_t sp_irq_rc, fp_irq_rc;
  9400. unsigned long status1, status2;
  9401. uint32_t hc_copy;
  9402. /*
  9403. * Get the driver's phba structure from the dev_id and
  9404. * assume the HBA is not interrupting.
  9405. */
  9406. phba = (struct lpfc_hba *) dev_id;
  9407. if (unlikely(!phba))
  9408. return IRQ_NONE;
  9409. /* Check device state for handling interrupt */
  9410. if (lpfc_intr_state_check(phba))
  9411. return IRQ_NONE;
  9412. spin_lock(&phba->hbalock);
  9413. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9414. spin_unlock(&phba->hbalock);
  9415. return IRQ_HANDLED;
  9416. }
  9417. if (unlikely(!phba->ha_copy)) {
  9418. spin_unlock(&phba->hbalock);
  9419. return IRQ_NONE;
  9420. } else if (phba->ha_copy & HA_ERATT) {
  9421. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9422. /* ERATT polling has handled ERATT */
  9423. phba->ha_copy &= ~HA_ERATT;
  9424. else
  9425. /* Indicate interrupt handler handles ERATT */
  9426. phba->hba_flag |= HBA_ERATT_HANDLED;
  9427. }
  9428. /*
  9429. * If there is deferred error attention, do not check for any interrupt.
  9430. */
  9431. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9432. spin_unlock(&phba->hbalock);
  9433. return IRQ_NONE;
  9434. }
  9435. /* Clear attention sources except link and error attentions */
  9436. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9437. spin_unlock(&phba->hbalock);
  9438. return IRQ_HANDLED;
  9439. }
  9440. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9441. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9442. phba->HCregaddr);
  9443. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9444. writel(hc_copy, phba->HCregaddr);
  9445. readl(phba->HAregaddr); /* flush */
  9446. spin_unlock(&phba->hbalock);
  9447. /*
  9448. * Invokes slow-path host attention interrupt handling as appropriate.
  9449. */
  9450. /* status of events with mailbox and link attention */
  9451. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9452. /* status of events with ELS ring */
  9453. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9454. status2 >>= (4*LPFC_ELS_RING);
  9455. if (status1 || (status2 & HA_RXMASK))
  9456. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9457. else
  9458. sp_irq_rc = IRQ_NONE;
  9459. /*
  9460. * Invoke fast-path host attention interrupt handling as appropriate.
  9461. */
  9462. /* status of events with FCP ring */
  9463. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9464. status1 >>= (4*LPFC_FCP_RING);
  9465. /* status of events with extra ring */
  9466. if (phba->cfg_multi_ring_support == 2) {
  9467. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9468. status2 >>= (4*LPFC_EXTRA_RING);
  9469. } else
  9470. status2 = 0;
  9471. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9472. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9473. else
  9474. fp_irq_rc = IRQ_NONE;
  9475. /* Return device-level interrupt handling status */
  9476. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9477. } /* lpfc_sli_intr_handler */
  9478. /**
  9479. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9480. * @phba: pointer to lpfc hba data structure.
  9481. *
  9482. * This routine is invoked by the worker thread to process all the pending
  9483. * SLI4 FCP abort XRI events.
  9484. **/
  9485. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9486. {
  9487. struct lpfc_cq_event *cq_event;
  9488. /* First, declare the fcp xri abort event has been handled */
  9489. spin_lock_irq(&phba->hbalock);
  9490. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9491. spin_unlock_irq(&phba->hbalock);
  9492. /* Now, handle all the fcp xri abort events */
  9493. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9494. /* Get the first event from the head of the event queue */
  9495. spin_lock_irq(&phba->hbalock);
  9496. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9497. cq_event, struct lpfc_cq_event, list);
  9498. spin_unlock_irq(&phba->hbalock);
  9499. /* Notify aborted XRI for FCP work queue */
  9500. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9501. /* Free the event processed back to the free pool */
  9502. lpfc_sli4_cq_event_release(phba, cq_event);
  9503. }
  9504. }
  9505. /**
  9506. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9507. * @phba: pointer to lpfc hba data structure.
  9508. *
  9509. * This routine is invoked by the worker thread to process all the pending
  9510. * SLI4 els abort xri events.
  9511. **/
  9512. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9513. {
  9514. struct lpfc_cq_event *cq_event;
  9515. /* First, declare the els xri abort event has been handled */
  9516. spin_lock_irq(&phba->hbalock);
  9517. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9518. spin_unlock_irq(&phba->hbalock);
  9519. /* Now, handle all the els xri abort events */
  9520. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9521. /* Get the first event from the head of the event queue */
  9522. spin_lock_irq(&phba->hbalock);
  9523. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9524. cq_event, struct lpfc_cq_event, list);
  9525. spin_unlock_irq(&phba->hbalock);
  9526. /* Notify aborted XRI for ELS work queue */
  9527. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9528. /* Free the event processed back to the free pool */
  9529. lpfc_sli4_cq_event_release(phba, cq_event);
  9530. }
  9531. }
  9532. /**
  9533. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9534. * @phba: pointer to lpfc hba data structure
  9535. * @pIocbIn: pointer to the rspiocbq
  9536. * @pIocbOut: pointer to the cmdiocbq
  9537. * @wcqe: pointer to the complete wcqe
  9538. *
  9539. * This routine transfers the fields of a command iocbq to a response iocbq
  9540. * by copying all the IOCB fields from command iocbq and transferring the
  9541. * completion status information from the complete wcqe.
  9542. **/
  9543. static void
  9544. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9545. struct lpfc_iocbq *pIocbIn,
  9546. struct lpfc_iocbq *pIocbOut,
  9547. struct lpfc_wcqe_complete *wcqe)
  9548. {
  9549. unsigned long iflags;
  9550. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9551. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9552. sizeof(struct lpfc_iocbq) - offset);
  9553. /* Map WCQE parameters into irspiocb parameters */
  9554. pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
  9555. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9556. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9557. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9558. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9559. wcqe->total_data_placed;
  9560. else
  9561. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9562. else {
  9563. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9564. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9565. }
  9566. /* Pick up HBA exchange busy condition */
  9567. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9568. spin_lock_irqsave(&phba->hbalock, iflags);
  9569. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9570. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9571. }
  9572. }
  9573. /**
  9574. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9575. * @phba: Pointer to HBA context object.
  9576. * @wcqe: Pointer to work-queue completion queue entry.
  9577. *
  9578. * This routine handles an ELS work-queue completion event and construct
  9579. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9580. * discovery engine to handle.
  9581. *
  9582. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9583. **/
  9584. static struct lpfc_iocbq *
  9585. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9586. struct lpfc_iocbq *irspiocbq)
  9587. {
  9588. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9589. struct lpfc_iocbq *cmdiocbq;
  9590. struct lpfc_wcqe_complete *wcqe;
  9591. unsigned long iflags;
  9592. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9593. spin_lock_irqsave(&phba->hbalock, iflags);
  9594. pring->stats.iocb_event++;
  9595. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9596. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9597. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9598. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9599. if (unlikely(!cmdiocbq)) {
  9600. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9601. "0386 ELS complete with no corresponding "
  9602. "cmdiocb: iotag (%d)\n",
  9603. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9604. lpfc_sli_release_iocbq(phba, irspiocbq);
  9605. return NULL;
  9606. }
  9607. /* Fake the irspiocbq and copy necessary response information */
  9608. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9609. return irspiocbq;
  9610. }
  9611. /**
  9612. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9613. * @phba: Pointer to HBA context object.
  9614. * @cqe: Pointer to mailbox completion queue entry.
  9615. *
  9616. * This routine process a mailbox completion queue entry with asynchrous
  9617. * event.
  9618. *
  9619. * Return: true if work posted to worker thread, otherwise false.
  9620. **/
  9621. static bool
  9622. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9623. {
  9624. struct lpfc_cq_event *cq_event;
  9625. unsigned long iflags;
  9626. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9627. "0392 Async Event: word0:x%x, word1:x%x, "
  9628. "word2:x%x, word3:x%x\n", mcqe->word0,
  9629. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9630. /* Allocate a new internal CQ_EVENT entry */
  9631. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9632. if (!cq_event) {
  9633. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9634. "0394 Failed to allocate CQ_EVENT entry\n");
  9635. return false;
  9636. }
  9637. /* Move the CQE into an asynchronous event entry */
  9638. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9639. spin_lock_irqsave(&phba->hbalock, iflags);
  9640. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9641. /* Set the async event flag */
  9642. phba->hba_flag |= ASYNC_EVENT;
  9643. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9644. return true;
  9645. }
  9646. /**
  9647. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  9648. * @phba: Pointer to HBA context object.
  9649. * @cqe: Pointer to mailbox completion queue entry.
  9650. *
  9651. * This routine process a mailbox completion queue entry with mailbox
  9652. * completion event.
  9653. *
  9654. * Return: true if work posted to worker thread, otherwise false.
  9655. **/
  9656. static bool
  9657. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9658. {
  9659. uint32_t mcqe_status;
  9660. MAILBOX_t *mbox, *pmbox;
  9661. struct lpfc_mqe *mqe;
  9662. struct lpfc_vport *vport;
  9663. struct lpfc_nodelist *ndlp;
  9664. struct lpfc_dmabuf *mp;
  9665. unsigned long iflags;
  9666. LPFC_MBOXQ_t *pmb;
  9667. bool workposted = false;
  9668. int rc;
  9669. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  9670. if (!bf_get(lpfc_trailer_completed, mcqe))
  9671. goto out_no_mqe_complete;
  9672. /* Get the reference to the active mbox command */
  9673. spin_lock_irqsave(&phba->hbalock, iflags);
  9674. pmb = phba->sli.mbox_active;
  9675. if (unlikely(!pmb)) {
  9676. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  9677. "1832 No pending MBOX command to handle\n");
  9678. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9679. goto out_no_mqe_complete;
  9680. }
  9681. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9682. mqe = &pmb->u.mqe;
  9683. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  9684. mbox = phba->mbox;
  9685. vport = pmb->vport;
  9686. /* Reset heartbeat timer */
  9687. phba->last_completion_time = jiffies;
  9688. del_timer(&phba->sli.mbox_tmo);
  9689. /* Move mbox data to caller's mailbox region, do endian swapping */
  9690. if (pmb->mbox_cmpl && mbox)
  9691. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  9692. /*
  9693. * For mcqe errors, conditionally move a modified error code to
  9694. * the mbox so that the error will not be missed.
  9695. */
  9696. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  9697. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  9698. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  9699. bf_set(lpfc_mqe_status, mqe,
  9700. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  9701. }
  9702. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9703. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9704. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  9705. "MBOX dflt rpi: status:x%x rpi:x%x",
  9706. mcqe_status,
  9707. pmbox->un.varWords[0], 0);
  9708. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  9709. mp = (struct lpfc_dmabuf *)(pmb->context1);
  9710. ndlp = (struct lpfc_nodelist *)pmb->context2;
  9711. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  9712. * RID of the PPI using the same mbox buffer.
  9713. */
  9714. lpfc_unreg_login(phba, vport->vpi,
  9715. pmbox->un.varWords[0], pmb);
  9716. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  9717. pmb->context1 = mp;
  9718. pmb->context2 = ndlp;
  9719. pmb->vport = vport;
  9720. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  9721. if (rc != MBX_BUSY)
  9722. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9723. LOG_SLI, "0385 rc should "
  9724. "have been MBX_BUSY\n");
  9725. if (rc != MBX_NOT_FINISHED)
  9726. goto send_current_mbox;
  9727. }
  9728. }
  9729. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  9730. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  9731. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  9732. /* There is mailbox completion work to do */
  9733. spin_lock_irqsave(&phba->hbalock, iflags);
  9734. __lpfc_mbox_cmpl_put(phba, pmb);
  9735. phba->work_ha |= HA_MBATT;
  9736. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9737. workposted = true;
  9738. send_current_mbox:
  9739. spin_lock_irqsave(&phba->hbalock, iflags);
  9740. /* Release the mailbox command posting token */
  9741. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  9742. /* Setting active mailbox pointer need to be in sync to flag clear */
  9743. phba->sli.mbox_active = NULL;
  9744. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9745. /* Wake up worker thread to post the next pending mailbox command */
  9746. lpfc_worker_wake_up(phba);
  9747. out_no_mqe_complete:
  9748. if (bf_get(lpfc_trailer_consumed, mcqe))
  9749. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  9750. return workposted;
  9751. }
  9752. /**
  9753. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  9754. * @phba: Pointer to HBA context object.
  9755. * @cqe: Pointer to mailbox completion queue entry.
  9756. *
  9757. * This routine process a mailbox completion queue entry, it invokes the
  9758. * proper mailbox complete handling or asynchrous event handling routine
  9759. * according to the MCQE's async bit.
  9760. *
  9761. * Return: true if work posted to worker thread, otherwise false.
  9762. **/
  9763. static bool
  9764. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  9765. {
  9766. struct lpfc_mcqe mcqe;
  9767. bool workposted;
  9768. /* Copy the mailbox MCQE and convert endian order as needed */
  9769. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  9770. /* Invoke the proper event handling routine */
  9771. if (!bf_get(lpfc_trailer_async, &mcqe))
  9772. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  9773. else
  9774. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  9775. return workposted;
  9776. }
  9777. /**
  9778. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  9779. * @phba: Pointer to HBA context object.
  9780. * @wcqe: Pointer to work-queue completion queue entry.
  9781. *
  9782. * This routine handles an ELS work-queue completion event.
  9783. *
  9784. * Return: true if work posted to worker thread, otherwise false.
  9785. **/
  9786. static bool
  9787. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  9788. struct lpfc_wcqe_complete *wcqe)
  9789. {
  9790. struct lpfc_iocbq *irspiocbq;
  9791. unsigned long iflags;
  9792. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  9793. /* Get an irspiocbq for later ELS response processing use */
  9794. irspiocbq = lpfc_sli_get_iocbq(phba);
  9795. if (!irspiocbq) {
  9796. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9797. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  9798. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  9799. pring->txq_cnt, phba->iocb_cnt,
  9800. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  9801. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  9802. return false;
  9803. }
  9804. /* Save off the slow-path queue event for work thread to process */
  9805. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  9806. spin_lock_irqsave(&phba->hbalock, iflags);
  9807. list_add_tail(&irspiocbq->cq_event.list,
  9808. &phba->sli4_hba.sp_queue_event);
  9809. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  9810. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9811. return true;
  9812. }
  9813. /**
  9814. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  9815. * @phba: Pointer to HBA context object.
  9816. * @wcqe: Pointer to work-queue completion queue entry.
  9817. *
  9818. * This routine handles slow-path WQ entry comsumed event by invoking the
  9819. * proper WQ release routine to the slow-path WQ.
  9820. **/
  9821. static void
  9822. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  9823. struct lpfc_wcqe_release *wcqe)
  9824. {
  9825. /* Check for the slow-path ELS work queue */
  9826. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  9827. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  9828. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  9829. else
  9830. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9831. "2579 Slow-path wqe consume event carries "
  9832. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  9833. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  9834. phba->sli4_hba.els_wq->queue_id);
  9835. }
  9836. /**
  9837. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  9838. * @phba: Pointer to HBA context object.
  9839. * @cq: Pointer to a WQ completion queue.
  9840. * @wcqe: Pointer to work-queue completion queue entry.
  9841. *
  9842. * This routine handles an XRI abort event.
  9843. *
  9844. * Return: true if work posted to worker thread, otherwise false.
  9845. **/
  9846. static bool
  9847. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  9848. struct lpfc_queue *cq,
  9849. struct sli4_wcqe_xri_aborted *wcqe)
  9850. {
  9851. bool workposted = false;
  9852. struct lpfc_cq_event *cq_event;
  9853. unsigned long iflags;
  9854. /* Allocate a new internal CQ_EVENT entry */
  9855. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9856. if (!cq_event) {
  9857. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9858. "0602 Failed to allocate CQ_EVENT entry\n");
  9859. return false;
  9860. }
  9861. /* Move the CQE into the proper xri abort event list */
  9862. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  9863. switch (cq->subtype) {
  9864. case LPFC_FCP:
  9865. spin_lock_irqsave(&phba->hbalock, iflags);
  9866. list_add_tail(&cq_event->list,
  9867. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  9868. /* Set the fcp xri abort event flag */
  9869. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  9870. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9871. workposted = true;
  9872. break;
  9873. case LPFC_ELS:
  9874. spin_lock_irqsave(&phba->hbalock, iflags);
  9875. list_add_tail(&cq_event->list,
  9876. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  9877. /* Set the els xri abort event flag */
  9878. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  9879. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9880. workposted = true;
  9881. break;
  9882. default:
  9883. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9884. "0603 Invalid work queue CQE subtype (x%x)\n",
  9885. cq->subtype);
  9886. workposted = false;
  9887. break;
  9888. }
  9889. return workposted;
  9890. }
  9891. /**
  9892. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  9893. * @phba: Pointer to HBA context object.
  9894. * @rcqe: Pointer to receive-queue completion queue entry.
  9895. *
  9896. * This routine process a receive-queue completion queue entry.
  9897. *
  9898. * Return: true if work posted to worker thread, otherwise false.
  9899. **/
  9900. static bool
  9901. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  9902. {
  9903. bool workposted = false;
  9904. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  9905. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  9906. struct hbq_dmabuf *dma_buf;
  9907. uint32_t status, rq_id;
  9908. unsigned long iflags;
  9909. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  9910. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  9911. else
  9912. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  9913. if (rq_id != hrq->queue_id)
  9914. goto out;
  9915. status = bf_get(lpfc_rcqe_status, rcqe);
  9916. switch (status) {
  9917. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  9918. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9919. "2537 Receive Frame Truncated!!\n");
  9920. case FC_STATUS_RQ_SUCCESS:
  9921. lpfc_sli4_rq_release(hrq, drq);
  9922. spin_lock_irqsave(&phba->hbalock, iflags);
  9923. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  9924. if (!dma_buf) {
  9925. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9926. goto out;
  9927. }
  9928. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  9929. /* save off the frame for the word thread to process */
  9930. list_add_tail(&dma_buf->cq_event.list,
  9931. &phba->sli4_hba.sp_queue_event);
  9932. /* Frame received */
  9933. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  9934. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9935. workposted = true;
  9936. break;
  9937. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  9938. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  9939. /* Post more buffers if possible */
  9940. spin_lock_irqsave(&phba->hbalock, iflags);
  9941. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  9942. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9943. workposted = true;
  9944. break;
  9945. }
  9946. out:
  9947. return workposted;
  9948. }
  9949. /**
  9950. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  9951. * @phba: Pointer to HBA context object.
  9952. * @cq: Pointer to the completion queue.
  9953. * @wcqe: Pointer to a completion queue entry.
  9954. *
  9955. * This routine process a slow-path work-queue or receive queue completion queue
  9956. * entry.
  9957. *
  9958. * Return: true if work posted to worker thread, otherwise false.
  9959. **/
  9960. static bool
  9961. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  9962. struct lpfc_cqe *cqe)
  9963. {
  9964. struct lpfc_cqe cqevt;
  9965. bool workposted = false;
  9966. /* Copy the work queue CQE and convert endian order if needed */
  9967. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  9968. /* Check and process for different type of WCQE and dispatch */
  9969. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  9970. case CQE_CODE_COMPL_WQE:
  9971. /* Process the WQ/RQ complete event */
  9972. phba->last_completion_time = jiffies;
  9973. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  9974. (struct lpfc_wcqe_complete *)&cqevt);
  9975. break;
  9976. case CQE_CODE_RELEASE_WQE:
  9977. /* Process the WQ release event */
  9978. lpfc_sli4_sp_handle_rel_wcqe(phba,
  9979. (struct lpfc_wcqe_release *)&cqevt);
  9980. break;
  9981. case CQE_CODE_XRI_ABORTED:
  9982. /* Process the WQ XRI abort event */
  9983. phba->last_completion_time = jiffies;
  9984. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  9985. (struct sli4_wcqe_xri_aborted *)&cqevt);
  9986. break;
  9987. case CQE_CODE_RECEIVE:
  9988. case CQE_CODE_RECEIVE_V1:
  9989. /* Process the RQ event */
  9990. phba->last_completion_time = jiffies;
  9991. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  9992. (struct lpfc_rcqe *)&cqevt);
  9993. break;
  9994. default:
  9995. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9996. "0388 Not a valid WCQE code: x%x\n",
  9997. bf_get(lpfc_cqe_code, &cqevt));
  9998. break;
  9999. }
  10000. return workposted;
  10001. }
  10002. /**
  10003. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10004. * @phba: Pointer to HBA context object.
  10005. * @eqe: Pointer to fast-path event queue entry.
  10006. *
  10007. * This routine process a event queue entry from the slow-path event queue.
  10008. * It will check the MajorCode and MinorCode to determine this is for a
  10009. * completion event on a completion queue, if not, an error shall be logged
  10010. * and just return. Otherwise, it will get to the corresponding completion
  10011. * queue and process all the entries on that completion queue, rearm the
  10012. * completion queue, and then return.
  10013. *
  10014. **/
  10015. static void
  10016. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10017. {
  10018. struct lpfc_queue *cq = NULL, *childq, *speq;
  10019. struct lpfc_cqe *cqe;
  10020. bool workposted = false;
  10021. int ecount = 0;
  10022. uint16_t cqid;
  10023. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10024. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10025. "0359 Not a valid slow-path completion "
  10026. "event: majorcode=x%x, minorcode=x%x\n",
  10027. bf_get_le32(lpfc_eqe_major_code, eqe),
  10028. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10029. return;
  10030. }
  10031. /* Get the reference to the corresponding CQ */
  10032. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10033. /* Search for completion queue pointer matching this cqid */
  10034. speq = phba->sli4_hba.sp_eq;
  10035. list_for_each_entry(childq, &speq->child_list, list) {
  10036. if (childq->queue_id == cqid) {
  10037. cq = childq;
  10038. break;
  10039. }
  10040. }
  10041. if (unlikely(!cq)) {
  10042. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10043. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10044. "0365 Slow-path CQ identifier "
  10045. "(%d) does not exist\n", cqid);
  10046. return;
  10047. }
  10048. /* Process all the entries to the CQ */
  10049. switch (cq->type) {
  10050. case LPFC_MCQ:
  10051. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10052. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10053. if (!(++ecount % cq->entry_repost))
  10054. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10055. }
  10056. break;
  10057. case LPFC_WCQ:
  10058. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10059. if (cq->subtype == LPFC_FCP)
  10060. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10061. cqe);
  10062. else
  10063. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10064. cqe);
  10065. if (!(++ecount % cq->entry_repost))
  10066. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10067. }
  10068. break;
  10069. default:
  10070. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10071. "0370 Invalid completion queue type (%d)\n",
  10072. cq->type);
  10073. return;
  10074. }
  10075. /* Catch the no cq entry condition, log an error */
  10076. if (unlikely(ecount == 0))
  10077. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10078. "0371 No entry from the CQ: identifier "
  10079. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10080. /* In any case, flash and re-arm the RCQ */
  10081. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10082. /* wake up worker thread if there are works to be done */
  10083. if (workposted)
  10084. lpfc_worker_wake_up(phba);
  10085. }
  10086. /**
  10087. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10088. * @eqe: Pointer to fast-path completion queue entry.
  10089. *
  10090. * This routine process a fast-path work queue completion entry from fast-path
  10091. * event queue for FCP command response completion.
  10092. **/
  10093. static void
  10094. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10095. struct lpfc_wcqe_complete *wcqe)
  10096. {
  10097. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10098. struct lpfc_iocbq *cmdiocbq;
  10099. struct lpfc_iocbq irspiocbq;
  10100. unsigned long iflags;
  10101. spin_lock_irqsave(&phba->hbalock, iflags);
  10102. pring->stats.iocb_event++;
  10103. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10104. /* Check for response status */
  10105. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10106. /* If resource errors reported from HBA, reduce queue
  10107. * depth of the SCSI device.
  10108. */
  10109. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10110. IOSTAT_LOCAL_REJECT) &&
  10111. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10112. phba->lpfc_rampdown_queue_depth(phba);
  10113. }
  10114. /* Log the error status */
  10115. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10116. "0373 FCP complete error: status=x%x, "
  10117. "hw_status=x%x, total_data_specified=%d, "
  10118. "parameter=x%x, word3=x%x\n",
  10119. bf_get(lpfc_wcqe_c_status, wcqe),
  10120. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10121. wcqe->total_data_placed, wcqe->parameter,
  10122. wcqe->word3);
  10123. }
  10124. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10125. spin_lock_irqsave(&phba->hbalock, iflags);
  10126. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10127. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10128. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10129. if (unlikely(!cmdiocbq)) {
  10130. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10131. "0374 FCP complete with no corresponding "
  10132. "cmdiocb: iotag (%d)\n",
  10133. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10134. return;
  10135. }
  10136. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10137. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10138. "0375 FCP cmdiocb not callback function "
  10139. "iotag: (%d)\n",
  10140. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10141. return;
  10142. }
  10143. /* Fake the irspiocb and copy necessary response information */
  10144. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10145. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10146. spin_lock_irqsave(&phba->hbalock, iflags);
  10147. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10148. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10149. }
  10150. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10151. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10152. }
  10153. /**
  10154. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10155. * @phba: Pointer to HBA context object.
  10156. * @cq: Pointer to completion queue.
  10157. * @wcqe: Pointer to work-queue completion queue entry.
  10158. *
  10159. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10160. * proper WQ release routine to the slow-path WQ.
  10161. **/
  10162. static void
  10163. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10164. struct lpfc_wcqe_release *wcqe)
  10165. {
  10166. struct lpfc_queue *childwq;
  10167. bool wqid_matched = false;
  10168. uint16_t fcp_wqid;
  10169. /* Check for fast-path FCP work queue release */
  10170. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10171. list_for_each_entry(childwq, &cq->child_list, list) {
  10172. if (childwq->queue_id == fcp_wqid) {
  10173. lpfc_sli4_wq_release(childwq,
  10174. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10175. wqid_matched = true;
  10176. break;
  10177. }
  10178. }
  10179. /* Report warning log message if no match found */
  10180. if (wqid_matched != true)
  10181. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10182. "2580 Fast-path wqe consume event carries "
  10183. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10184. }
  10185. /**
  10186. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10187. * @cq: Pointer to the completion queue.
  10188. * @eqe: Pointer to fast-path completion queue entry.
  10189. *
  10190. * This routine process a fast-path work queue completion entry from fast-path
  10191. * event queue for FCP command response completion.
  10192. **/
  10193. static int
  10194. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10195. struct lpfc_cqe *cqe)
  10196. {
  10197. struct lpfc_wcqe_release wcqe;
  10198. bool workposted = false;
  10199. /* Copy the work queue CQE and convert endian order if needed */
  10200. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10201. /* Check and process for different type of WCQE and dispatch */
  10202. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10203. case CQE_CODE_COMPL_WQE:
  10204. /* Process the WQ complete event */
  10205. phba->last_completion_time = jiffies;
  10206. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10207. (struct lpfc_wcqe_complete *)&wcqe);
  10208. break;
  10209. case CQE_CODE_RELEASE_WQE:
  10210. /* Process the WQ release event */
  10211. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10212. (struct lpfc_wcqe_release *)&wcqe);
  10213. break;
  10214. case CQE_CODE_XRI_ABORTED:
  10215. /* Process the WQ XRI abort event */
  10216. phba->last_completion_time = jiffies;
  10217. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10218. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10219. break;
  10220. default:
  10221. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10222. "0144 Not a valid WCQE code: x%x\n",
  10223. bf_get(lpfc_wcqe_c_code, &wcqe));
  10224. break;
  10225. }
  10226. return workposted;
  10227. }
  10228. /**
  10229. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10230. * @phba: Pointer to HBA context object.
  10231. * @eqe: Pointer to fast-path event queue entry.
  10232. *
  10233. * This routine process a event queue entry from the fast-path event queue.
  10234. * It will check the MajorCode and MinorCode to determine this is for a
  10235. * completion event on a completion queue, if not, an error shall be logged
  10236. * and just return. Otherwise, it will get to the corresponding completion
  10237. * queue and process all the entries on the completion queue, rearm the
  10238. * completion queue, and then return.
  10239. **/
  10240. static void
  10241. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10242. uint32_t fcp_cqidx)
  10243. {
  10244. struct lpfc_queue *cq;
  10245. struct lpfc_cqe *cqe;
  10246. bool workposted = false;
  10247. uint16_t cqid;
  10248. int ecount = 0;
  10249. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10250. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10251. "0366 Not a valid fast-path completion "
  10252. "event: majorcode=x%x, minorcode=x%x\n",
  10253. bf_get_le32(lpfc_eqe_major_code, eqe),
  10254. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10255. return;
  10256. }
  10257. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10258. if (unlikely(!cq)) {
  10259. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10260. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10261. "0367 Fast-path completion queue "
  10262. "does not exist\n");
  10263. return;
  10264. }
  10265. /* Get the reference to the corresponding CQ */
  10266. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10267. if (unlikely(cqid != cq->queue_id)) {
  10268. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10269. "0368 Miss-matched fast-path completion "
  10270. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10271. cqid, cq->queue_id);
  10272. return;
  10273. }
  10274. /* Process all the entries to the CQ */
  10275. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10276. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10277. if (!(++ecount % cq->entry_repost))
  10278. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10279. }
  10280. /* Catch the no cq entry condition */
  10281. if (unlikely(ecount == 0))
  10282. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10283. "0369 No entry from fast-path completion "
  10284. "queue fcpcqid=%d\n", cq->queue_id);
  10285. /* In any case, flash and re-arm the CQ */
  10286. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10287. /* wake up worker thread if there are works to be done */
  10288. if (workposted)
  10289. lpfc_worker_wake_up(phba);
  10290. }
  10291. static void
  10292. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10293. {
  10294. struct lpfc_eqe *eqe;
  10295. /* walk all the EQ entries and drop on the floor */
  10296. while ((eqe = lpfc_sli4_eq_get(eq)))
  10297. ;
  10298. /* Clear and re-arm the EQ */
  10299. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10300. }
  10301. /**
  10302. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10303. * @irq: Interrupt number.
  10304. * @dev_id: The device context pointer.
  10305. *
  10306. * This function is directly called from the PCI layer as an interrupt
  10307. * service routine when device with SLI-4 interface spec is enabled with
  10308. * MSI-X multi-message interrupt mode and there are slow-path events in
  10309. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10310. * interrupt mode, this function is called as part of the device-level
  10311. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10312. * undergoing initialization, the interrupt handler will not process the
  10313. * interrupt. The link attention and ELS ring attention events are handled
  10314. * by the worker thread. The interrupt handler signals the worker thread
  10315. * and returns for these events. This function is called without any lock
  10316. * held. It gets the hbalock to access and update SLI data structures.
  10317. *
  10318. * This function returns IRQ_HANDLED when interrupt is handled else it
  10319. * returns IRQ_NONE.
  10320. **/
  10321. irqreturn_t
  10322. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10323. {
  10324. struct lpfc_hba *phba;
  10325. struct lpfc_queue *speq;
  10326. struct lpfc_eqe *eqe;
  10327. unsigned long iflag;
  10328. int ecount = 0;
  10329. /*
  10330. * Get the driver's phba structure from the dev_id
  10331. */
  10332. phba = (struct lpfc_hba *)dev_id;
  10333. if (unlikely(!phba))
  10334. return IRQ_NONE;
  10335. /* Get to the EQ struct associated with this vector */
  10336. speq = phba->sli4_hba.sp_eq;
  10337. if (unlikely(!speq))
  10338. return IRQ_NONE;
  10339. /* Check device state for handling interrupt */
  10340. if (unlikely(lpfc_intr_state_check(phba))) {
  10341. /* Check again for link_state with lock held */
  10342. spin_lock_irqsave(&phba->hbalock, iflag);
  10343. if (phba->link_state < LPFC_LINK_DOWN)
  10344. /* Flush, clear interrupt, and rearm the EQ */
  10345. lpfc_sli4_eq_flush(phba, speq);
  10346. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10347. return IRQ_NONE;
  10348. }
  10349. /*
  10350. * Process all the event on FCP slow-path EQ
  10351. */
  10352. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10353. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10354. if (!(++ecount % speq->entry_repost))
  10355. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10356. }
  10357. /* Always clear and re-arm the slow-path EQ */
  10358. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10359. /* Catch the no cq entry condition */
  10360. if (unlikely(ecount == 0)) {
  10361. if (phba->intr_type == MSIX)
  10362. /* MSI-X treated interrupt served as no EQ share INT */
  10363. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10364. "0357 MSI-X interrupt with no EQE\n");
  10365. else
  10366. /* Non MSI-X treated on interrupt as EQ share INT */
  10367. return IRQ_NONE;
  10368. }
  10369. return IRQ_HANDLED;
  10370. } /* lpfc_sli4_sp_intr_handler */
  10371. /**
  10372. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10373. * @irq: Interrupt number.
  10374. * @dev_id: The device context pointer.
  10375. *
  10376. * This function is directly called from the PCI layer as an interrupt
  10377. * service routine when device with SLI-4 interface spec is enabled with
  10378. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10379. * ring event in the HBA. However, when the device is enabled with either
  10380. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10381. * device-level interrupt handler. When the PCI slot is in error recovery
  10382. * or the HBA is undergoing initialization, the interrupt handler will not
  10383. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10384. * the intrrupt context. This function is called without any lock held.
  10385. * It gets the hbalock to access and update SLI data structures. Note that,
  10386. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10387. * equal to that of FCP CQ index.
  10388. *
  10389. * This function returns IRQ_HANDLED when interrupt is handled else it
  10390. * returns IRQ_NONE.
  10391. **/
  10392. irqreturn_t
  10393. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10394. {
  10395. struct lpfc_hba *phba;
  10396. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10397. struct lpfc_queue *fpeq;
  10398. struct lpfc_eqe *eqe;
  10399. unsigned long iflag;
  10400. int ecount = 0;
  10401. uint32_t fcp_eqidx;
  10402. /* Get the driver's phba structure from the dev_id */
  10403. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10404. phba = fcp_eq_hdl->phba;
  10405. fcp_eqidx = fcp_eq_hdl->idx;
  10406. if (unlikely(!phba))
  10407. return IRQ_NONE;
  10408. if (unlikely(!phba->sli4_hba.fp_eq))
  10409. return IRQ_NONE;
  10410. /* Get to the EQ struct associated with this vector */
  10411. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10412. /* Check device state for handling interrupt */
  10413. if (unlikely(lpfc_intr_state_check(phba))) {
  10414. /* Check again for link_state with lock held */
  10415. spin_lock_irqsave(&phba->hbalock, iflag);
  10416. if (phba->link_state < LPFC_LINK_DOWN)
  10417. /* Flush, clear interrupt, and rearm the EQ */
  10418. lpfc_sli4_eq_flush(phba, fpeq);
  10419. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10420. return IRQ_NONE;
  10421. }
  10422. /*
  10423. * Process all the event on FCP fast-path EQ
  10424. */
  10425. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10426. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10427. if (!(++ecount % fpeq->entry_repost))
  10428. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10429. }
  10430. /* Always clear and re-arm the fast-path EQ */
  10431. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10432. if (unlikely(ecount == 0)) {
  10433. if (phba->intr_type == MSIX)
  10434. /* MSI-X treated interrupt served as no EQ share INT */
  10435. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10436. "0358 MSI-X interrupt with no EQE\n");
  10437. else
  10438. /* Non MSI-X treated on interrupt as EQ share INT */
  10439. return IRQ_NONE;
  10440. }
  10441. return IRQ_HANDLED;
  10442. } /* lpfc_sli4_fp_intr_handler */
  10443. /**
  10444. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10445. * @irq: Interrupt number.
  10446. * @dev_id: The device context pointer.
  10447. *
  10448. * This function is the device-level interrupt handler to device with SLI-4
  10449. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10450. * interrupt mode is enabled and there is an event in the HBA which requires
  10451. * driver attention. This function invokes the slow-path interrupt attention
  10452. * handling function and fast-path interrupt attention handling function in
  10453. * turn to process the relevant HBA attention events. This function is called
  10454. * without any lock held. It gets the hbalock to access and update SLI data
  10455. * structures.
  10456. *
  10457. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10458. * returns IRQ_NONE.
  10459. **/
  10460. irqreturn_t
  10461. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10462. {
  10463. struct lpfc_hba *phba;
  10464. irqreturn_t sp_irq_rc, fp_irq_rc;
  10465. bool fp_handled = false;
  10466. uint32_t fcp_eqidx;
  10467. /* Get the driver's phba structure from the dev_id */
  10468. phba = (struct lpfc_hba *)dev_id;
  10469. if (unlikely(!phba))
  10470. return IRQ_NONE;
  10471. /*
  10472. * Invokes slow-path host attention interrupt handling as appropriate.
  10473. */
  10474. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10475. /*
  10476. * Invoke fast-path host attention interrupt handling as appropriate.
  10477. */
  10478. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10479. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10480. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10481. if (fp_irq_rc == IRQ_HANDLED)
  10482. fp_handled |= true;
  10483. }
  10484. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10485. } /* lpfc_sli4_intr_handler */
  10486. /**
  10487. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10488. * @queue: The queue structure to free.
  10489. *
  10490. * This function frees a queue structure and the DMAable memory used for
  10491. * the host resident queue. This function must be called after destroying the
  10492. * queue on the HBA.
  10493. **/
  10494. void
  10495. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10496. {
  10497. struct lpfc_dmabuf *dmabuf;
  10498. if (!queue)
  10499. return;
  10500. while (!list_empty(&queue->page_list)) {
  10501. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10502. list);
  10503. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10504. dmabuf->virt, dmabuf->phys);
  10505. kfree(dmabuf);
  10506. }
  10507. kfree(queue);
  10508. return;
  10509. }
  10510. /**
  10511. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10512. * @phba: The HBA that this queue is being created on.
  10513. * @entry_size: The size of each queue entry for this queue.
  10514. * @entry count: The number of entries that this queue will handle.
  10515. *
  10516. * This function allocates a queue structure and the DMAable memory used for
  10517. * the host resident queue. This function must be called before creating the
  10518. * queue on the HBA.
  10519. **/
  10520. struct lpfc_queue *
  10521. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10522. uint32_t entry_count)
  10523. {
  10524. struct lpfc_queue *queue;
  10525. struct lpfc_dmabuf *dmabuf;
  10526. int x, total_qe_count;
  10527. void *dma_pointer;
  10528. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10529. if (!phba->sli4_hba.pc_sli4_params.supported)
  10530. hw_page_size = SLI4_PAGE_SIZE;
  10531. queue = kzalloc(sizeof(struct lpfc_queue) +
  10532. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10533. if (!queue)
  10534. return NULL;
  10535. queue->page_count = (ALIGN(entry_size * entry_count,
  10536. hw_page_size))/hw_page_size;
  10537. INIT_LIST_HEAD(&queue->list);
  10538. INIT_LIST_HEAD(&queue->page_list);
  10539. INIT_LIST_HEAD(&queue->child_list);
  10540. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10541. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10542. if (!dmabuf)
  10543. goto out_fail;
  10544. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10545. hw_page_size, &dmabuf->phys,
  10546. GFP_KERNEL);
  10547. if (!dmabuf->virt) {
  10548. kfree(dmabuf);
  10549. goto out_fail;
  10550. }
  10551. memset(dmabuf->virt, 0, hw_page_size);
  10552. dmabuf->buffer_tag = x;
  10553. list_add_tail(&dmabuf->list, &queue->page_list);
  10554. /* initialize queue's entry array */
  10555. dma_pointer = dmabuf->virt;
  10556. for (; total_qe_count < entry_count &&
  10557. dma_pointer < (hw_page_size + dmabuf->virt);
  10558. total_qe_count++, dma_pointer += entry_size) {
  10559. queue->qe[total_qe_count].address = dma_pointer;
  10560. }
  10561. }
  10562. queue->entry_size = entry_size;
  10563. queue->entry_count = entry_count;
  10564. /*
  10565. * entry_repost is calculated based on the number of entries in the
  10566. * queue. This works out except for RQs. If buffers are NOT initially
  10567. * posted for every RQE, entry_repost should be adjusted accordingly.
  10568. */
  10569. queue->entry_repost = (entry_count >> 3);
  10570. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10571. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10572. queue->phba = phba;
  10573. return queue;
  10574. out_fail:
  10575. lpfc_sli4_queue_free(queue);
  10576. return NULL;
  10577. }
  10578. /**
  10579. * lpfc_eq_create - Create an Event Queue on the HBA
  10580. * @phba: HBA structure that indicates port to create a queue on.
  10581. * @eq: The queue structure to use to create the event queue.
  10582. * @imax: The maximum interrupt per second limit.
  10583. *
  10584. * This function creates an event queue, as detailed in @eq, on a port,
  10585. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10586. *
  10587. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10588. * is used to get the entry count and entry size that are necessary to
  10589. * determine the number of pages to allocate and use for this queue. This
  10590. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10591. * event queue. This function is asynchronous and will wait for the mailbox
  10592. * command to finish before continuing.
  10593. *
  10594. * On success this function will return a zero. If unable to allocate enough
  10595. * memory this function will return -ENOMEM. If the queue create mailbox command
  10596. * fails this function will return -ENXIO.
  10597. **/
  10598. uint32_t
  10599. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10600. {
  10601. struct lpfc_mbx_eq_create *eq_create;
  10602. LPFC_MBOXQ_t *mbox;
  10603. int rc, length, status = 0;
  10604. struct lpfc_dmabuf *dmabuf;
  10605. uint32_t shdr_status, shdr_add_status;
  10606. union lpfc_sli4_cfg_shdr *shdr;
  10607. uint16_t dmult;
  10608. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10609. if (!phba->sli4_hba.pc_sli4_params.supported)
  10610. hw_page_size = SLI4_PAGE_SIZE;
  10611. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10612. if (!mbox)
  10613. return -ENOMEM;
  10614. length = (sizeof(struct lpfc_mbx_eq_create) -
  10615. sizeof(struct lpfc_sli4_cfg_mhdr));
  10616. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10617. LPFC_MBOX_OPCODE_EQ_CREATE,
  10618. length, LPFC_SLI4_MBX_EMBED);
  10619. eq_create = &mbox->u.mqe.un.eq_create;
  10620. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10621. eq->page_count);
  10622. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10623. LPFC_EQE_SIZE);
  10624. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10625. /* Calculate delay multiper from maximum interrupt per second */
  10626. dmult = LPFC_DMULT_CONST/imax - 1;
  10627. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  10628. dmult);
  10629. switch (eq->entry_count) {
  10630. default:
  10631. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10632. "0360 Unsupported EQ count. (%d)\n",
  10633. eq->entry_count);
  10634. if (eq->entry_count < 256)
  10635. return -EINVAL;
  10636. /* otherwise default to smallest count (drop through) */
  10637. case 256:
  10638. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10639. LPFC_EQ_CNT_256);
  10640. break;
  10641. case 512:
  10642. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10643. LPFC_EQ_CNT_512);
  10644. break;
  10645. case 1024:
  10646. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10647. LPFC_EQ_CNT_1024);
  10648. break;
  10649. case 2048:
  10650. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10651. LPFC_EQ_CNT_2048);
  10652. break;
  10653. case 4096:
  10654. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  10655. LPFC_EQ_CNT_4096);
  10656. break;
  10657. }
  10658. list_for_each_entry(dmabuf, &eq->page_list, list) {
  10659. memset(dmabuf->virt, 0, hw_page_size);
  10660. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10661. putPaddrLow(dmabuf->phys);
  10662. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10663. putPaddrHigh(dmabuf->phys);
  10664. }
  10665. mbox->vport = phba->pport;
  10666. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  10667. mbox->context1 = NULL;
  10668. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10669. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  10670. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10671. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10672. if (shdr_status || shdr_add_status || rc) {
  10673. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10674. "2500 EQ_CREATE mailbox failed with "
  10675. "status x%x add_status x%x, mbx status x%x\n",
  10676. shdr_status, shdr_add_status, rc);
  10677. status = -ENXIO;
  10678. }
  10679. eq->type = LPFC_EQ;
  10680. eq->subtype = LPFC_NONE;
  10681. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  10682. if (eq->queue_id == 0xFFFF)
  10683. status = -ENXIO;
  10684. eq->host_index = 0;
  10685. eq->hba_index = 0;
  10686. mempool_free(mbox, phba->mbox_mem_pool);
  10687. return status;
  10688. }
  10689. /**
  10690. * lpfc_cq_create - Create a Completion Queue on the HBA
  10691. * @phba: HBA structure that indicates port to create a queue on.
  10692. * @cq: The queue structure to use to create the completion queue.
  10693. * @eq: The event queue to bind this completion queue to.
  10694. *
  10695. * This function creates a completion queue, as detailed in @wq, on a port,
  10696. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  10697. *
  10698. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10699. * is used to get the entry count and entry size that are necessary to
  10700. * determine the number of pages to allocate and use for this queue. The @eq
  10701. * is used to indicate which event queue to bind this completion queue to. This
  10702. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  10703. * completion queue. This function is asynchronous and will wait for the mailbox
  10704. * command to finish before continuing.
  10705. *
  10706. * On success this function will return a zero. If unable to allocate enough
  10707. * memory this function will return -ENOMEM. If the queue create mailbox command
  10708. * fails this function will return -ENXIO.
  10709. **/
  10710. uint32_t
  10711. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10712. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  10713. {
  10714. struct lpfc_mbx_cq_create *cq_create;
  10715. struct lpfc_dmabuf *dmabuf;
  10716. LPFC_MBOXQ_t *mbox;
  10717. int rc, length, status = 0;
  10718. uint32_t shdr_status, shdr_add_status;
  10719. union lpfc_sli4_cfg_shdr *shdr;
  10720. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10721. if (!phba->sli4_hba.pc_sli4_params.supported)
  10722. hw_page_size = SLI4_PAGE_SIZE;
  10723. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10724. if (!mbox)
  10725. return -ENOMEM;
  10726. length = (sizeof(struct lpfc_mbx_cq_create) -
  10727. sizeof(struct lpfc_sli4_cfg_mhdr));
  10728. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10729. LPFC_MBOX_OPCODE_CQ_CREATE,
  10730. length, LPFC_SLI4_MBX_EMBED);
  10731. cq_create = &mbox->u.mqe.un.cq_create;
  10732. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  10733. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  10734. cq->page_count);
  10735. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  10736. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  10737. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10738. phba->sli4_hba.pc_sli4_params.cqv);
  10739. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  10740. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  10741. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  10742. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  10743. eq->queue_id);
  10744. } else {
  10745. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  10746. eq->queue_id);
  10747. }
  10748. switch (cq->entry_count) {
  10749. default:
  10750. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10751. "0361 Unsupported CQ count. (%d)\n",
  10752. cq->entry_count);
  10753. if (cq->entry_count < 256)
  10754. return -EINVAL;
  10755. /* otherwise default to smallest count (drop through) */
  10756. case 256:
  10757. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10758. LPFC_CQ_CNT_256);
  10759. break;
  10760. case 512:
  10761. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10762. LPFC_CQ_CNT_512);
  10763. break;
  10764. case 1024:
  10765. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  10766. LPFC_CQ_CNT_1024);
  10767. break;
  10768. }
  10769. list_for_each_entry(dmabuf, &cq->page_list, list) {
  10770. memset(dmabuf->virt, 0, hw_page_size);
  10771. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10772. putPaddrLow(dmabuf->phys);
  10773. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10774. putPaddrHigh(dmabuf->phys);
  10775. }
  10776. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10777. /* The IOCTL status is embedded in the mailbox subheader. */
  10778. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10779. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10780. if (shdr_status || shdr_add_status || rc) {
  10781. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10782. "2501 CQ_CREATE mailbox failed with "
  10783. "status x%x add_status x%x, mbx status x%x\n",
  10784. shdr_status, shdr_add_status, rc);
  10785. status = -ENXIO;
  10786. goto out;
  10787. }
  10788. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  10789. if (cq->queue_id == 0xFFFF) {
  10790. status = -ENXIO;
  10791. goto out;
  10792. }
  10793. /* link the cq onto the parent eq child list */
  10794. list_add_tail(&cq->list, &eq->child_list);
  10795. /* Set up completion queue's type and subtype */
  10796. cq->type = type;
  10797. cq->subtype = subtype;
  10798. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  10799. cq->assoc_qid = eq->queue_id;
  10800. cq->host_index = 0;
  10801. cq->hba_index = 0;
  10802. out:
  10803. mempool_free(mbox, phba->mbox_mem_pool);
  10804. return status;
  10805. }
  10806. /**
  10807. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  10808. * @phba: HBA structure that indicates port to create a queue on.
  10809. * @mq: The queue structure to use to create the mailbox queue.
  10810. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  10811. * @cq: The completion queue to associate with this cq.
  10812. *
  10813. * This function provides failback (fb) functionality when the
  10814. * mq_create_ext fails on older FW generations. It's purpose is identical
  10815. * to mq_create_ext otherwise.
  10816. *
  10817. * This routine cannot fail as all attributes were previously accessed and
  10818. * initialized in mq_create_ext.
  10819. **/
  10820. static void
  10821. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  10822. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  10823. {
  10824. struct lpfc_mbx_mq_create *mq_create;
  10825. struct lpfc_dmabuf *dmabuf;
  10826. int length;
  10827. length = (sizeof(struct lpfc_mbx_mq_create) -
  10828. sizeof(struct lpfc_sli4_cfg_mhdr));
  10829. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10830. LPFC_MBOX_OPCODE_MQ_CREATE,
  10831. length, LPFC_SLI4_MBX_EMBED);
  10832. mq_create = &mbox->u.mqe.un.mq_create;
  10833. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  10834. mq->page_count);
  10835. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  10836. cq->queue_id);
  10837. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  10838. switch (mq->entry_count) {
  10839. case 16:
  10840. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10841. LPFC_MQ_RING_SIZE_16);
  10842. break;
  10843. case 32:
  10844. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10845. LPFC_MQ_RING_SIZE_32);
  10846. break;
  10847. case 64:
  10848. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10849. LPFC_MQ_RING_SIZE_64);
  10850. break;
  10851. case 128:
  10852. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  10853. LPFC_MQ_RING_SIZE_128);
  10854. break;
  10855. }
  10856. list_for_each_entry(dmabuf, &mq->page_list, list) {
  10857. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  10858. putPaddrLow(dmabuf->phys);
  10859. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  10860. putPaddrHigh(dmabuf->phys);
  10861. }
  10862. }
  10863. /**
  10864. * lpfc_mq_create - Create a mailbox Queue on the HBA
  10865. * @phba: HBA structure that indicates port to create a queue on.
  10866. * @mq: The queue structure to use to create the mailbox queue.
  10867. * @cq: The completion queue to associate with this cq.
  10868. * @subtype: The queue's subtype.
  10869. *
  10870. * This function creates a mailbox queue, as detailed in @mq, on a port,
  10871. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  10872. *
  10873. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  10874. * is used to get the entry count and entry size that are necessary to
  10875. * determine the number of pages to allocate and use for this queue. This
  10876. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  10877. * mailbox queue. This function is asynchronous and will wait for the mailbox
  10878. * command to finish before continuing.
  10879. *
  10880. * On success this function will return a zero. If unable to allocate enough
  10881. * memory this function will return -ENOMEM. If the queue create mailbox command
  10882. * fails this function will return -ENXIO.
  10883. **/
  10884. int32_t
  10885. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  10886. struct lpfc_queue *cq, uint32_t subtype)
  10887. {
  10888. struct lpfc_mbx_mq_create *mq_create;
  10889. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  10890. struct lpfc_dmabuf *dmabuf;
  10891. LPFC_MBOXQ_t *mbox;
  10892. int rc, length, status = 0;
  10893. uint32_t shdr_status, shdr_add_status;
  10894. union lpfc_sli4_cfg_shdr *shdr;
  10895. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10896. if (!phba->sli4_hba.pc_sli4_params.supported)
  10897. hw_page_size = SLI4_PAGE_SIZE;
  10898. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10899. if (!mbox)
  10900. return -ENOMEM;
  10901. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  10902. sizeof(struct lpfc_sli4_cfg_mhdr));
  10903. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10904. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  10905. length, LPFC_SLI4_MBX_EMBED);
  10906. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  10907. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  10908. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  10909. &mq_create_ext->u.request, mq->page_count);
  10910. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  10911. &mq_create_ext->u.request, 1);
  10912. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  10913. &mq_create_ext->u.request, 1);
  10914. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  10915. &mq_create_ext->u.request, 1);
  10916. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  10917. &mq_create_ext->u.request, 1);
  10918. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  10919. &mq_create_ext->u.request, 1);
  10920. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  10921. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  10922. phba->sli4_hba.pc_sli4_params.mqv);
  10923. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  10924. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  10925. cq->queue_id);
  10926. else
  10927. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  10928. cq->queue_id);
  10929. switch (mq->entry_count) {
  10930. default:
  10931. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10932. "0362 Unsupported MQ count. (%d)\n",
  10933. mq->entry_count);
  10934. if (mq->entry_count < 16)
  10935. return -EINVAL;
  10936. /* otherwise default to smallest count (drop through) */
  10937. case 16:
  10938. bf_set(lpfc_mq_context_ring_size,
  10939. &mq_create_ext->u.request.context,
  10940. LPFC_MQ_RING_SIZE_16);
  10941. break;
  10942. case 32:
  10943. bf_set(lpfc_mq_context_ring_size,
  10944. &mq_create_ext->u.request.context,
  10945. LPFC_MQ_RING_SIZE_32);
  10946. break;
  10947. case 64:
  10948. bf_set(lpfc_mq_context_ring_size,
  10949. &mq_create_ext->u.request.context,
  10950. LPFC_MQ_RING_SIZE_64);
  10951. break;
  10952. case 128:
  10953. bf_set(lpfc_mq_context_ring_size,
  10954. &mq_create_ext->u.request.context,
  10955. LPFC_MQ_RING_SIZE_128);
  10956. break;
  10957. }
  10958. list_for_each_entry(dmabuf, &mq->page_list, list) {
  10959. memset(dmabuf->virt, 0, hw_page_size);
  10960. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  10961. putPaddrLow(dmabuf->phys);
  10962. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  10963. putPaddrHigh(dmabuf->phys);
  10964. }
  10965. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10966. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  10967. &mq_create_ext->u.response);
  10968. if (rc != MBX_SUCCESS) {
  10969. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  10970. "2795 MQ_CREATE_EXT failed with "
  10971. "status x%x. Failback to MQ_CREATE.\n",
  10972. rc);
  10973. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  10974. mq_create = &mbox->u.mqe.un.mq_create;
  10975. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  10976. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  10977. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  10978. &mq_create->u.response);
  10979. }
  10980. /* The IOCTL status is embedded in the mailbox subheader. */
  10981. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  10982. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  10983. if (shdr_status || shdr_add_status || rc) {
  10984. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  10985. "2502 MQ_CREATE mailbox failed with "
  10986. "status x%x add_status x%x, mbx status x%x\n",
  10987. shdr_status, shdr_add_status, rc);
  10988. status = -ENXIO;
  10989. goto out;
  10990. }
  10991. if (mq->queue_id == 0xFFFF) {
  10992. status = -ENXIO;
  10993. goto out;
  10994. }
  10995. mq->type = LPFC_MQ;
  10996. mq->assoc_qid = cq->queue_id;
  10997. mq->subtype = subtype;
  10998. mq->host_index = 0;
  10999. mq->hba_index = 0;
  11000. /* link the mq onto the parent cq child list */
  11001. list_add_tail(&mq->list, &cq->child_list);
  11002. out:
  11003. mempool_free(mbox, phba->mbox_mem_pool);
  11004. return status;
  11005. }
  11006. /**
  11007. * lpfc_wq_create - Create a Work Queue on the HBA
  11008. * @phba: HBA structure that indicates port to create a queue on.
  11009. * @wq: The queue structure to use to create the work queue.
  11010. * @cq: The completion queue to bind this work queue to.
  11011. * @subtype: The subtype of the work queue indicating its functionality.
  11012. *
  11013. * This function creates a work queue, as detailed in @wq, on a port, described
  11014. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11015. *
  11016. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11017. * is used to get the entry count and entry size that are necessary to
  11018. * determine the number of pages to allocate and use for this queue. The @cq
  11019. * is used to indicate which completion queue to bind this work queue to. This
  11020. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11021. * work queue. This function is asynchronous and will wait for the mailbox
  11022. * command to finish before continuing.
  11023. *
  11024. * On success this function will return a zero. If unable to allocate enough
  11025. * memory this function will return -ENOMEM. If the queue create mailbox command
  11026. * fails this function will return -ENXIO.
  11027. **/
  11028. uint32_t
  11029. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11030. struct lpfc_queue *cq, uint32_t subtype)
  11031. {
  11032. struct lpfc_mbx_wq_create *wq_create;
  11033. struct lpfc_dmabuf *dmabuf;
  11034. LPFC_MBOXQ_t *mbox;
  11035. int rc, length, status = 0;
  11036. uint32_t shdr_status, shdr_add_status;
  11037. union lpfc_sli4_cfg_shdr *shdr;
  11038. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11039. struct dma_address *page;
  11040. if (!phba->sli4_hba.pc_sli4_params.supported)
  11041. hw_page_size = SLI4_PAGE_SIZE;
  11042. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11043. if (!mbox)
  11044. return -ENOMEM;
  11045. length = (sizeof(struct lpfc_mbx_wq_create) -
  11046. sizeof(struct lpfc_sli4_cfg_mhdr));
  11047. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11048. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11049. length, LPFC_SLI4_MBX_EMBED);
  11050. wq_create = &mbox->u.mqe.un.wq_create;
  11051. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11052. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11053. wq->page_count);
  11054. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11055. cq->queue_id);
  11056. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11057. phba->sli4_hba.pc_sli4_params.wqv);
  11058. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11059. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11060. wq->entry_count);
  11061. switch (wq->entry_size) {
  11062. default:
  11063. case 64:
  11064. bf_set(lpfc_mbx_wq_create_wqe_size,
  11065. &wq_create->u.request_1,
  11066. LPFC_WQ_WQE_SIZE_64);
  11067. break;
  11068. case 128:
  11069. bf_set(lpfc_mbx_wq_create_wqe_size,
  11070. &wq_create->u.request_1,
  11071. LPFC_WQ_WQE_SIZE_128);
  11072. break;
  11073. }
  11074. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11075. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11076. page = wq_create->u.request_1.page;
  11077. } else {
  11078. page = wq_create->u.request.page;
  11079. }
  11080. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11081. memset(dmabuf->virt, 0, hw_page_size);
  11082. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11083. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11084. }
  11085. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11086. /* The IOCTL status is embedded in the mailbox subheader. */
  11087. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11088. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11089. if (shdr_status || shdr_add_status || rc) {
  11090. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11091. "2503 WQ_CREATE mailbox failed with "
  11092. "status x%x add_status x%x, mbx status x%x\n",
  11093. shdr_status, shdr_add_status, rc);
  11094. status = -ENXIO;
  11095. goto out;
  11096. }
  11097. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11098. if (wq->queue_id == 0xFFFF) {
  11099. status = -ENXIO;
  11100. goto out;
  11101. }
  11102. wq->type = LPFC_WQ;
  11103. wq->assoc_qid = cq->queue_id;
  11104. wq->subtype = subtype;
  11105. wq->host_index = 0;
  11106. wq->hba_index = 0;
  11107. /* link the wq onto the parent cq child list */
  11108. list_add_tail(&wq->list, &cq->child_list);
  11109. out:
  11110. mempool_free(mbox, phba->mbox_mem_pool);
  11111. return status;
  11112. }
  11113. /**
  11114. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11115. * @phba: HBA structure that indicates port to create a queue on.
  11116. * @rq: The queue structure to use for the receive queue.
  11117. * @qno: The associated HBQ number
  11118. *
  11119. *
  11120. * For SLI4 we need to adjust the RQ repost value based on
  11121. * the number of buffers that are initially posted to the RQ.
  11122. */
  11123. void
  11124. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11125. {
  11126. uint32_t cnt;
  11127. cnt = lpfc_hbq_defs[qno]->entry_count;
  11128. /* Recalc repost for RQs based on buffers initially posted */
  11129. cnt = (cnt >> 3);
  11130. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11131. cnt = LPFC_QUEUE_MIN_REPOST;
  11132. rq->entry_repost = cnt;
  11133. }
  11134. /**
  11135. * lpfc_rq_create - Create a Receive Queue on the HBA
  11136. * @phba: HBA structure that indicates port to create a queue on.
  11137. * @hrq: The queue structure to use to create the header receive queue.
  11138. * @drq: The queue structure to use to create the data receive queue.
  11139. * @cq: The completion queue to bind this work queue to.
  11140. *
  11141. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11142. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11143. * to the HBA.
  11144. *
  11145. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11146. * struct is used to get the entry count that is necessary to determine the
  11147. * number of pages to use for this queue. The @cq is used to indicate which
  11148. * completion queue to bind received buffers that are posted to these queues to.
  11149. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11150. * receive queue pair. This function is asynchronous and will wait for the
  11151. * mailbox command to finish before continuing.
  11152. *
  11153. * On success this function will return a zero. If unable to allocate enough
  11154. * memory this function will return -ENOMEM. If the queue create mailbox command
  11155. * fails this function will return -ENXIO.
  11156. **/
  11157. uint32_t
  11158. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11159. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11160. {
  11161. struct lpfc_mbx_rq_create *rq_create;
  11162. struct lpfc_dmabuf *dmabuf;
  11163. LPFC_MBOXQ_t *mbox;
  11164. int rc, length, status = 0;
  11165. uint32_t shdr_status, shdr_add_status;
  11166. union lpfc_sli4_cfg_shdr *shdr;
  11167. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11168. if (!phba->sli4_hba.pc_sli4_params.supported)
  11169. hw_page_size = SLI4_PAGE_SIZE;
  11170. if (hrq->entry_count != drq->entry_count)
  11171. return -EINVAL;
  11172. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11173. if (!mbox)
  11174. return -ENOMEM;
  11175. length = (sizeof(struct lpfc_mbx_rq_create) -
  11176. sizeof(struct lpfc_sli4_cfg_mhdr));
  11177. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11178. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11179. length, LPFC_SLI4_MBX_EMBED);
  11180. rq_create = &mbox->u.mqe.un.rq_create;
  11181. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11182. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11183. phba->sli4_hba.pc_sli4_params.rqv);
  11184. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11185. bf_set(lpfc_rq_context_rqe_count_1,
  11186. &rq_create->u.request.context,
  11187. hrq->entry_count);
  11188. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11189. bf_set(lpfc_rq_context_rqe_size,
  11190. &rq_create->u.request.context,
  11191. LPFC_RQE_SIZE_8);
  11192. bf_set(lpfc_rq_context_page_size,
  11193. &rq_create->u.request.context,
  11194. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11195. } else {
  11196. switch (hrq->entry_count) {
  11197. default:
  11198. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11199. "2535 Unsupported RQ count. (%d)\n",
  11200. hrq->entry_count);
  11201. if (hrq->entry_count < 512)
  11202. return -EINVAL;
  11203. /* otherwise default to smallest count (drop through) */
  11204. case 512:
  11205. bf_set(lpfc_rq_context_rqe_count,
  11206. &rq_create->u.request.context,
  11207. LPFC_RQ_RING_SIZE_512);
  11208. break;
  11209. case 1024:
  11210. bf_set(lpfc_rq_context_rqe_count,
  11211. &rq_create->u.request.context,
  11212. LPFC_RQ_RING_SIZE_1024);
  11213. break;
  11214. case 2048:
  11215. bf_set(lpfc_rq_context_rqe_count,
  11216. &rq_create->u.request.context,
  11217. LPFC_RQ_RING_SIZE_2048);
  11218. break;
  11219. case 4096:
  11220. bf_set(lpfc_rq_context_rqe_count,
  11221. &rq_create->u.request.context,
  11222. LPFC_RQ_RING_SIZE_4096);
  11223. break;
  11224. }
  11225. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11226. LPFC_HDR_BUF_SIZE);
  11227. }
  11228. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11229. cq->queue_id);
  11230. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11231. hrq->page_count);
  11232. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11233. memset(dmabuf->virt, 0, hw_page_size);
  11234. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11235. putPaddrLow(dmabuf->phys);
  11236. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11237. putPaddrHigh(dmabuf->phys);
  11238. }
  11239. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11240. /* The IOCTL status is embedded in the mailbox subheader. */
  11241. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11242. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11243. if (shdr_status || shdr_add_status || rc) {
  11244. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11245. "2504 RQ_CREATE mailbox failed with "
  11246. "status x%x add_status x%x, mbx status x%x\n",
  11247. shdr_status, shdr_add_status, rc);
  11248. status = -ENXIO;
  11249. goto out;
  11250. }
  11251. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11252. if (hrq->queue_id == 0xFFFF) {
  11253. status = -ENXIO;
  11254. goto out;
  11255. }
  11256. hrq->type = LPFC_HRQ;
  11257. hrq->assoc_qid = cq->queue_id;
  11258. hrq->subtype = subtype;
  11259. hrq->host_index = 0;
  11260. hrq->hba_index = 0;
  11261. /* now create the data queue */
  11262. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11263. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11264. length, LPFC_SLI4_MBX_EMBED);
  11265. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11266. phba->sli4_hba.pc_sli4_params.rqv);
  11267. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11268. bf_set(lpfc_rq_context_rqe_count_1,
  11269. &rq_create->u.request.context, hrq->entry_count);
  11270. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11271. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11272. LPFC_RQE_SIZE_8);
  11273. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11274. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11275. } else {
  11276. switch (drq->entry_count) {
  11277. default:
  11278. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11279. "2536 Unsupported RQ count. (%d)\n",
  11280. drq->entry_count);
  11281. if (drq->entry_count < 512)
  11282. return -EINVAL;
  11283. /* otherwise default to smallest count (drop through) */
  11284. case 512:
  11285. bf_set(lpfc_rq_context_rqe_count,
  11286. &rq_create->u.request.context,
  11287. LPFC_RQ_RING_SIZE_512);
  11288. break;
  11289. case 1024:
  11290. bf_set(lpfc_rq_context_rqe_count,
  11291. &rq_create->u.request.context,
  11292. LPFC_RQ_RING_SIZE_1024);
  11293. break;
  11294. case 2048:
  11295. bf_set(lpfc_rq_context_rqe_count,
  11296. &rq_create->u.request.context,
  11297. LPFC_RQ_RING_SIZE_2048);
  11298. break;
  11299. case 4096:
  11300. bf_set(lpfc_rq_context_rqe_count,
  11301. &rq_create->u.request.context,
  11302. LPFC_RQ_RING_SIZE_4096);
  11303. break;
  11304. }
  11305. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11306. LPFC_DATA_BUF_SIZE);
  11307. }
  11308. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11309. cq->queue_id);
  11310. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11311. drq->page_count);
  11312. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11313. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11314. putPaddrLow(dmabuf->phys);
  11315. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11316. putPaddrHigh(dmabuf->phys);
  11317. }
  11318. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11319. /* The IOCTL status is embedded in the mailbox subheader. */
  11320. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11321. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11322. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11323. if (shdr_status || shdr_add_status || rc) {
  11324. status = -ENXIO;
  11325. goto out;
  11326. }
  11327. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11328. if (drq->queue_id == 0xFFFF) {
  11329. status = -ENXIO;
  11330. goto out;
  11331. }
  11332. drq->type = LPFC_DRQ;
  11333. drq->assoc_qid = cq->queue_id;
  11334. drq->subtype = subtype;
  11335. drq->host_index = 0;
  11336. drq->hba_index = 0;
  11337. /* link the header and data RQs onto the parent cq child list */
  11338. list_add_tail(&hrq->list, &cq->child_list);
  11339. list_add_tail(&drq->list, &cq->child_list);
  11340. out:
  11341. mempool_free(mbox, phba->mbox_mem_pool);
  11342. return status;
  11343. }
  11344. /**
  11345. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11346. * @eq: The queue structure associated with the queue to destroy.
  11347. *
  11348. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11349. * command, specific to the type of queue, to the HBA.
  11350. *
  11351. * The @eq struct is used to get the queue ID of the queue to destroy.
  11352. *
  11353. * On success this function will return a zero. If the queue destroy mailbox
  11354. * command fails this function will return -ENXIO.
  11355. **/
  11356. uint32_t
  11357. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11358. {
  11359. LPFC_MBOXQ_t *mbox;
  11360. int rc, length, status = 0;
  11361. uint32_t shdr_status, shdr_add_status;
  11362. union lpfc_sli4_cfg_shdr *shdr;
  11363. if (!eq)
  11364. return -ENODEV;
  11365. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11366. if (!mbox)
  11367. return -ENOMEM;
  11368. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11369. sizeof(struct lpfc_sli4_cfg_mhdr));
  11370. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11371. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11372. length, LPFC_SLI4_MBX_EMBED);
  11373. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11374. eq->queue_id);
  11375. mbox->vport = eq->phba->pport;
  11376. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11377. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11378. /* The IOCTL status is embedded in the mailbox subheader. */
  11379. shdr = (union lpfc_sli4_cfg_shdr *)
  11380. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11381. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11382. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11383. if (shdr_status || shdr_add_status || rc) {
  11384. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11385. "2505 EQ_DESTROY mailbox failed with "
  11386. "status x%x add_status x%x, mbx status x%x\n",
  11387. shdr_status, shdr_add_status, rc);
  11388. status = -ENXIO;
  11389. }
  11390. /* Remove eq from any list */
  11391. list_del_init(&eq->list);
  11392. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11393. return status;
  11394. }
  11395. /**
  11396. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11397. * @cq: The queue structure associated with the queue to destroy.
  11398. *
  11399. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11400. * command, specific to the type of queue, to the HBA.
  11401. *
  11402. * The @cq struct is used to get the queue ID of the queue to destroy.
  11403. *
  11404. * On success this function will return a zero. If the queue destroy mailbox
  11405. * command fails this function will return -ENXIO.
  11406. **/
  11407. uint32_t
  11408. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11409. {
  11410. LPFC_MBOXQ_t *mbox;
  11411. int rc, length, status = 0;
  11412. uint32_t shdr_status, shdr_add_status;
  11413. union lpfc_sli4_cfg_shdr *shdr;
  11414. if (!cq)
  11415. return -ENODEV;
  11416. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11417. if (!mbox)
  11418. return -ENOMEM;
  11419. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11420. sizeof(struct lpfc_sli4_cfg_mhdr));
  11421. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11422. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11423. length, LPFC_SLI4_MBX_EMBED);
  11424. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11425. cq->queue_id);
  11426. mbox->vport = cq->phba->pport;
  11427. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11428. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11429. /* The IOCTL status is embedded in the mailbox subheader. */
  11430. shdr = (union lpfc_sli4_cfg_shdr *)
  11431. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11432. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11433. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11434. if (shdr_status || shdr_add_status || rc) {
  11435. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11436. "2506 CQ_DESTROY mailbox failed with "
  11437. "status x%x add_status x%x, mbx status x%x\n",
  11438. shdr_status, shdr_add_status, rc);
  11439. status = -ENXIO;
  11440. }
  11441. /* Remove cq from any list */
  11442. list_del_init(&cq->list);
  11443. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11444. return status;
  11445. }
  11446. /**
  11447. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11448. * @qm: The queue structure associated with the queue to destroy.
  11449. *
  11450. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11451. * command, specific to the type of queue, to the HBA.
  11452. *
  11453. * The @mq struct is used to get the queue ID of the queue to destroy.
  11454. *
  11455. * On success this function will return a zero. If the queue destroy mailbox
  11456. * command fails this function will return -ENXIO.
  11457. **/
  11458. uint32_t
  11459. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11460. {
  11461. LPFC_MBOXQ_t *mbox;
  11462. int rc, length, status = 0;
  11463. uint32_t shdr_status, shdr_add_status;
  11464. union lpfc_sli4_cfg_shdr *shdr;
  11465. if (!mq)
  11466. return -ENODEV;
  11467. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11468. if (!mbox)
  11469. return -ENOMEM;
  11470. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11471. sizeof(struct lpfc_sli4_cfg_mhdr));
  11472. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11473. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11474. length, LPFC_SLI4_MBX_EMBED);
  11475. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11476. mq->queue_id);
  11477. mbox->vport = mq->phba->pport;
  11478. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11479. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11480. /* The IOCTL status is embedded in the mailbox subheader. */
  11481. shdr = (union lpfc_sli4_cfg_shdr *)
  11482. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11483. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11484. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11485. if (shdr_status || shdr_add_status || rc) {
  11486. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11487. "2507 MQ_DESTROY mailbox failed with "
  11488. "status x%x add_status x%x, mbx status x%x\n",
  11489. shdr_status, shdr_add_status, rc);
  11490. status = -ENXIO;
  11491. }
  11492. /* Remove mq from any list */
  11493. list_del_init(&mq->list);
  11494. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11495. return status;
  11496. }
  11497. /**
  11498. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11499. * @wq: The queue structure associated with the queue to destroy.
  11500. *
  11501. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11502. * command, specific to the type of queue, to the HBA.
  11503. *
  11504. * The @wq struct is used to get the queue ID of the queue to destroy.
  11505. *
  11506. * On success this function will return a zero. If the queue destroy mailbox
  11507. * command fails this function will return -ENXIO.
  11508. **/
  11509. uint32_t
  11510. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11511. {
  11512. LPFC_MBOXQ_t *mbox;
  11513. int rc, length, status = 0;
  11514. uint32_t shdr_status, shdr_add_status;
  11515. union lpfc_sli4_cfg_shdr *shdr;
  11516. if (!wq)
  11517. return -ENODEV;
  11518. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11519. if (!mbox)
  11520. return -ENOMEM;
  11521. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11522. sizeof(struct lpfc_sli4_cfg_mhdr));
  11523. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11524. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11525. length, LPFC_SLI4_MBX_EMBED);
  11526. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11527. wq->queue_id);
  11528. mbox->vport = wq->phba->pport;
  11529. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11530. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11531. shdr = (union lpfc_sli4_cfg_shdr *)
  11532. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11533. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11534. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11535. if (shdr_status || shdr_add_status || rc) {
  11536. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11537. "2508 WQ_DESTROY mailbox failed with "
  11538. "status x%x add_status x%x, mbx status x%x\n",
  11539. shdr_status, shdr_add_status, rc);
  11540. status = -ENXIO;
  11541. }
  11542. /* Remove wq from any list */
  11543. list_del_init(&wq->list);
  11544. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11545. return status;
  11546. }
  11547. /**
  11548. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11549. * @rq: The queue structure associated with the queue to destroy.
  11550. *
  11551. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11552. * command, specific to the type of queue, to the HBA.
  11553. *
  11554. * The @rq struct is used to get the queue ID of the queue to destroy.
  11555. *
  11556. * On success this function will return a zero. If the queue destroy mailbox
  11557. * command fails this function will return -ENXIO.
  11558. **/
  11559. uint32_t
  11560. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11561. struct lpfc_queue *drq)
  11562. {
  11563. LPFC_MBOXQ_t *mbox;
  11564. int rc, length, status = 0;
  11565. uint32_t shdr_status, shdr_add_status;
  11566. union lpfc_sli4_cfg_shdr *shdr;
  11567. if (!hrq || !drq)
  11568. return -ENODEV;
  11569. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11570. if (!mbox)
  11571. return -ENOMEM;
  11572. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11573. sizeof(struct lpfc_sli4_cfg_mhdr));
  11574. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11575. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11576. length, LPFC_SLI4_MBX_EMBED);
  11577. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11578. hrq->queue_id);
  11579. mbox->vport = hrq->phba->pport;
  11580. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11581. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11582. /* The IOCTL status is embedded in the mailbox subheader. */
  11583. shdr = (union lpfc_sli4_cfg_shdr *)
  11584. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11585. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11586. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11587. if (shdr_status || shdr_add_status || rc) {
  11588. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11589. "2509 RQ_DESTROY mailbox failed with "
  11590. "status x%x add_status x%x, mbx status x%x\n",
  11591. shdr_status, shdr_add_status, rc);
  11592. if (rc != MBX_TIMEOUT)
  11593. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11594. return -ENXIO;
  11595. }
  11596. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11597. drq->queue_id);
  11598. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11599. shdr = (union lpfc_sli4_cfg_shdr *)
  11600. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11601. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11602. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11603. if (shdr_status || shdr_add_status || rc) {
  11604. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11605. "2510 RQ_DESTROY mailbox failed with "
  11606. "status x%x add_status x%x, mbx status x%x\n",
  11607. shdr_status, shdr_add_status, rc);
  11608. status = -ENXIO;
  11609. }
  11610. list_del_init(&hrq->list);
  11611. list_del_init(&drq->list);
  11612. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11613. return status;
  11614. }
  11615. /**
  11616. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  11617. * @phba: The virtual port for which this call being executed.
  11618. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  11619. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  11620. * @xritag: the xritag that ties this io to the SGL pages.
  11621. *
  11622. * This routine will post the sgl pages for the IO that has the xritag
  11623. * that is in the iocbq structure. The xritag is assigned during iocbq
  11624. * creation and persists for as long as the driver is loaded.
  11625. * if the caller has fewer than 256 scatter gather segments to map then
  11626. * pdma_phys_addr1 should be 0.
  11627. * If the caller needs to map more than 256 scatter gather segment then
  11628. * pdma_phys_addr1 should be a valid physical address.
  11629. * physical address for SGLs must be 64 byte aligned.
  11630. * If you are going to map 2 SGL's then the first one must have 256 entries
  11631. * the second sgl can have between 1 and 256 entries.
  11632. *
  11633. * Return codes:
  11634. * 0 - Success
  11635. * -ENXIO, -ENOMEM - Failure
  11636. **/
  11637. int
  11638. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  11639. dma_addr_t pdma_phys_addr0,
  11640. dma_addr_t pdma_phys_addr1,
  11641. uint16_t xritag)
  11642. {
  11643. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  11644. LPFC_MBOXQ_t *mbox;
  11645. int rc;
  11646. uint32_t shdr_status, shdr_add_status;
  11647. uint32_t mbox_tmo;
  11648. union lpfc_sli4_cfg_shdr *shdr;
  11649. if (xritag == NO_XRI) {
  11650. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11651. "0364 Invalid param:\n");
  11652. return -EINVAL;
  11653. }
  11654. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11655. if (!mbox)
  11656. return -ENOMEM;
  11657. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11658. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11659. sizeof(struct lpfc_mbx_post_sgl_pages) -
  11660. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  11661. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  11662. &mbox->u.mqe.un.post_sgl_pages;
  11663. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  11664. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  11665. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  11666. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  11667. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  11668. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  11669. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  11670. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  11671. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  11672. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  11673. if (!phba->sli4_hba.intr_enable)
  11674. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11675. else {
  11676. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  11677. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11678. }
  11679. /* The IOCTL status is embedded in the mailbox subheader. */
  11680. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  11681. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11682. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11683. if (rc != MBX_TIMEOUT)
  11684. mempool_free(mbox, phba->mbox_mem_pool);
  11685. if (shdr_status || shdr_add_status || rc) {
  11686. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11687. "2511 POST_SGL mailbox failed with "
  11688. "status x%x add_status x%x, mbx status x%x\n",
  11689. shdr_status, shdr_add_status, rc);
  11690. rc = -ENXIO;
  11691. }
  11692. return 0;
  11693. }
  11694. /**
  11695. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  11696. * @phba: pointer to lpfc hba data structure.
  11697. *
  11698. * This routine is invoked to post rpi header templates to the
  11699. * HBA consistent with the SLI-4 interface spec. This routine
  11700. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  11701. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  11702. *
  11703. * Returns
  11704. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  11705. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  11706. **/
  11707. uint16_t
  11708. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  11709. {
  11710. unsigned long xri;
  11711. /*
  11712. * Fetch the next logical xri. Because this index is logical,
  11713. * the driver starts at 0 each time.
  11714. */
  11715. spin_lock_irq(&phba->hbalock);
  11716. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  11717. phba->sli4_hba.max_cfg_param.max_xri, 0);
  11718. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  11719. spin_unlock_irq(&phba->hbalock);
  11720. return NO_XRI;
  11721. } else {
  11722. set_bit(xri, phba->sli4_hba.xri_bmask);
  11723. phba->sli4_hba.max_cfg_param.xri_used++;
  11724. phba->sli4_hba.xri_count++;
  11725. }
  11726. spin_unlock_irq(&phba->hbalock);
  11727. return xri;
  11728. }
  11729. /**
  11730. * lpfc_sli4_free_xri - Release an xri for reuse.
  11731. * @phba: pointer to lpfc hba data structure.
  11732. *
  11733. * This routine is invoked to release an xri to the pool of
  11734. * available rpis maintained by the driver.
  11735. **/
  11736. void
  11737. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11738. {
  11739. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  11740. phba->sli4_hba.xri_count--;
  11741. phba->sli4_hba.max_cfg_param.xri_used--;
  11742. }
  11743. }
  11744. /**
  11745. * lpfc_sli4_free_xri - Release an xri for reuse.
  11746. * @phba: pointer to lpfc hba data structure.
  11747. *
  11748. * This routine is invoked to release an xri to the pool of
  11749. * available rpis maintained by the driver.
  11750. **/
  11751. void
  11752. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  11753. {
  11754. spin_lock_irq(&phba->hbalock);
  11755. __lpfc_sli4_free_xri(phba, xri);
  11756. spin_unlock_irq(&phba->hbalock);
  11757. }
  11758. /**
  11759. * lpfc_sli4_next_xritag - Get an xritag for the io
  11760. * @phba: Pointer to HBA context object.
  11761. *
  11762. * This function gets an xritag for the iocb. If there is no unused xritag
  11763. * it will return 0xffff.
  11764. * The function returns the allocated xritag if successful, else returns zero.
  11765. * Zero is not a valid xritag.
  11766. * The caller is not required to hold any lock.
  11767. **/
  11768. uint16_t
  11769. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  11770. {
  11771. uint16_t xri_index;
  11772. xri_index = lpfc_sli4_alloc_xri(phba);
  11773. if (xri_index != NO_XRI)
  11774. return xri_index;
  11775. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11776. "2004 Failed to allocate XRI.last XRITAG is %d"
  11777. " Max XRI is %d, Used XRI is %d\n",
  11778. xri_index,
  11779. phba->sli4_hba.max_cfg_param.max_xri,
  11780. phba->sli4_hba.max_cfg_param.xri_used);
  11781. return NO_XRI;
  11782. }
  11783. /**
  11784. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  11785. * @phba: pointer to lpfc hba data structure.
  11786. *
  11787. * This routine is invoked to post a block of driver's sgl pages to the
  11788. * HBA using non-embedded mailbox command. No Lock is held. This routine
  11789. * is only called when the driver is loading and after all IO has been
  11790. * stopped.
  11791. **/
  11792. int
  11793. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
  11794. {
  11795. struct lpfc_sglq *sglq_entry;
  11796. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  11797. struct sgl_page_pairs *sgl_pg_pairs;
  11798. void *viraddr;
  11799. LPFC_MBOXQ_t *mbox;
  11800. uint32_t reqlen, alloclen, pg_pairs;
  11801. uint32_t mbox_tmo;
  11802. uint16_t xritag_start = 0, lxri = 0;
  11803. int els_xri_cnt, rc = 0;
  11804. uint32_t shdr_status, shdr_add_status;
  11805. union lpfc_sli4_cfg_shdr *shdr;
  11806. /* The number of sgls to be posted */
  11807. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  11808. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  11809. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  11810. if (reqlen > SLI4_PAGE_SIZE) {
  11811. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11812. "2559 Block sgl registration required DMA "
  11813. "size (%d) great than a page\n", reqlen);
  11814. return -ENOMEM;
  11815. }
  11816. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11817. if (!mbox)
  11818. return -ENOMEM;
  11819. /* Allocate DMA memory and set up the non-embedded mailbox command */
  11820. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11821. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  11822. LPFC_SLI4_MBX_NEMBED);
  11823. if (alloclen < reqlen) {
  11824. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11825. "0285 Allocated DMA memory size (%d) is "
  11826. "less than the requested DMA memory "
  11827. "size (%d)\n", alloclen, reqlen);
  11828. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11829. return -ENOMEM;
  11830. }
  11831. /* Set up the SGL pages in the non-embedded DMA pages */
  11832. viraddr = mbox->sge_array->addr[0];
  11833. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  11834. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  11835. for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
  11836. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
  11837. /*
  11838. * Assign the sglq a physical xri only if the driver has not
  11839. * initialized those resources. A port reset only needs
  11840. * the sglq's posted.
  11841. */
  11842. if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  11843. LPFC_XRI_RSRC_RDY) {
  11844. lxri = lpfc_sli4_next_xritag(phba);
  11845. if (lxri == NO_XRI) {
  11846. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11847. return -ENOMEM;
  11848. }
  11849. sglq_entry->sli4_lxritag = lxri;
  11850. sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  11851. }
  11852. /* Set up the sge entry */
  11853. sgl_pg_pairs->sgl_pg0_addr_lo =
  11854. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  11855. sgl_pg_pairs->sgl_pg0_addr_hi =
  11856. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  11857. sgl_pg_pairs->sgl_pg1_addr_lo =
  11858. cpu_to_le32(putPaddrLow(0));
  11859. sgl_pg_pairs->sgl_pg1_addr_hi =
  11860. cpu_to_le32(putPaddrHigh(0));
  11861. /* Keep the first xritag on the list */
  11862. if (pg_pairs == 0)
  11863. xritag_start = sglq_entry->sli4_xritag;
  11864. sgl_pg_pairs++;
  11865. }
  11866. /* Complete initialization and perform endian conversion. */
  11867. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  11868. bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
  11869. sgl->word0 = cpu_to_le32(sgl->word0);
  11870. if (!phba->sli4_hba.intr_enable)
  11871. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11872. else {
  11873. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  11874. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  11875. }
  11876. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  11877. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11878. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11879. if (rc != MBX_TIMEOUT)
  11880. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11881. if (shdr_status || shdr_add_status || rc) {
  11882. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11883. "2513 POST_SGL_BLOCK mailbox command failed "
  11884. "status x%x add_status x%x mbx status x%x\n",
  11885. shdr_status, shdr_add_status, rc);
  11886. rc = -ENXIO;
  11887. }
  11888. if (rc == 0)
  11889. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  11890. LPFC_XRI_RSRC_RDY);
  11891. return rc;
  11892. }
  11893. /**
  11894. * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
  11895. * @phba: pointer to lpfc hba data structure.
  11896. *
  11897. * This routine is invoked to post a block of driver's sgl pages to the
  11898. * HBA using non-embedded mailbox command. No Lock is held. This routine
  11899. * is only called when the driver is loading and after all IO has been
  11900. * stopped.
  11901. **/
  11902. int
  11903. lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
  11904. {
  11905. struct lpfc_sglq *sglq_entry;
  11906. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  11907. struct sgl_page_pairs *sgl_pg_pairs;
  11908. void *viraddr;
  11909. LPFC_MBOXQ_t *mbox;
  11910. uint32_t reqlen, alloclen, index;
  11911. uint32_t mbox_tmo;
  11912. uint16_t rsrc_start, rsrc_size, els_xri_cnt;
  11913. uint16_t xritag_start = 0, lxri = 0;
  11914. struct lpfc_rsrc_blks *rsrc_blk;
  11915. int cnt, ttl_cnt, rc = 0;
  11916. int loop_cnt;
  11917. uint32_t shdr_status, shdr_add_status;
  11918. union lpfc_sli4_cfg_shdr *shdr;
  11919. /* The number of sgls to be posted */
  11920. els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
  11921. reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
  11922. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  11923. if (reqlen > SLI4_PAGE_SIZE) {
  11924. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  11925. "2989 Block sgl registration required DMA "
  11926. "size (%d) great than a page\n", reqlen);
  11927. return -ENOMEM;
  11928. }
  11929. cnt = 0;
  11930. ttl_cnt = 0;
  11931. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  11932. list) {
  11933. rsrc_start = rsrc_blk->rsrc_start;
  11934. rsrc_size = rsrc_blk->rsrc_size;
  11935. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11936. "3014 Working ELS Extent start %d, cnt %d\n",
  11937. rsrc_start, rsrc_size);
  11938. loop_cnt = min(els_xri_cnt, rsrc_size);
  11939. if (ttl_cnt + loop_cnt >= els_xri_cnt) {
  11940. loop_cnt = els_xri_cnt - ttl_cnt;
  11941. ttl_cnt = els_xri_cnt;
  11942. }
  11943. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11944. if (!mbox)
  11945. return -ENOMEM;
  11946. /*
  11947. * Allocate DMA memory and set up the non-embedded mailbox
  11948. * command.
  11949. */
  11950. alloclen = lpfc_sli4_config(phba, mbox,
  11951. LPFC_MBOX_SUBSYSTEM_FCOE,
  11952. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  11953. reqlen, LPFC_SLI4_MBX_NEMBED);
  11954. if (alloclen < reqlen) {
  11955. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11956. "2987 Allocated DMA memory size (%d) "
  11957. "is less than the requested DMA memory "
  11958. "size (%d)\n", alloclen, reqlen);
  11959. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11960. return -ENOMEM;
  11961. }
  11962. /* Set up the SGL pages in the non-embedded DMA pages */
  11963. viraddr = mbox->sge_array->addr[0];
  11964. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  11965. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  11966. /*
  11967. * The starting resource may not begin at zero. Control
  11968. * the loop variants via the block resource parameters,
  11969. * but handle the sge pointers with a zero-based index
  11970. * that doesn't get reset per loop pass.
  11971. */
  11972. for (index = rsrc_start;
  11973. index < rsrc_start + loop_cnt;
  11974. index++) {
  11975. sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
  11976. /*
  11977. * Assign the sglq a physical xri only if the driver
  11978. * has not initialized those resources. A port reset
  11979. * only needs the sglq's posted.
  11980. */
  11981. if (bf_get(lpfc_xri_rsrc_rdy,
  11982. &phba->sli4_hba.sli4_flags) !=
  11983. LPFC_XRI_RSRC_RDY) {
  11984. lxri = lpfc_sli4_next_xritag(phba);
  11985. if (lxri == NO_XRI) {
  11986. lpfc_sli4_mbox_cmd_free(phba, mbox);
  11987. rc = -ENOMEM;
  11988. goto err_exit;
  11989. }
  11990. sglq_entry->sli4_lxritag = lxri;
  11991. sglq_entry->sli4_xritag =
  11992. phba->sli4_hba.xri_ids[lxri];
  11993. }
  11994. /* Set up the sge entry */
  11995. sgl_pg_pairs->sgl_pg0_addr_lo =
  11996. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  11997. sgl_pg_pairs->sgl_pg0_addr_hi =
  11998. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  11999. sgl_pg_pairs->sgl_pg1_addr_lo =
  12000. cpu_to_le32(putPaddrLow(0));
  12001. sgl_pg_pairs->sgl_pg1_addr_hi =
  12002. cpu_to_le32(putPaddrHigh(0));
  12003. /* Track the starting physical XRI for the mailbox. */
  12004. if (index == rsrc_start)
  12005. xritag_start = sglq_entry->sli4_xritag;
  12006. sgl_pg_pairs++;
  12007. cnt++;
  12008. }
  12009. /* Complete initialization and perform endian conversion. */
  12010. rsrc_blk->rsrc_used += loop_cnt;
  12011. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12012. bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
  12013. sgl->word0 = cpu_to_le32(sgl->word0);
  12014. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12015. "3015 Post ELS Extent SGL, start %d, "
  12016. "cnt %d, used %d\n",
  12017. xritag_start, loop_cnt, rsrc_blk->rsrc_used);
  12018. if (!phba->sli4_hba.intr_enable)
  12019. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12020. else {
  12021. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12022. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12023. }
  12024. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12025. shdr_status = bf_get(lpfc_mbox_hdr_status,
  12026. &shdr->response);
  12027. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12028. &shdr->response);
  12029. if (rc != MBX_TIMEOUT)
  12030. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12031. if (shdr_status || shdr_add_status || rc) {
  12032. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12033. "2988 POST_SGL_BLOCK mailbox "
  12034. "command failed status x%x "
  12035. "add_status x%x mbx status x%x\n",
  12036. shdr_status, shdr_add_status, rc);
  12037. rc = -ENXIO;
  12038. goto err_exit;
  12039. }
  12040. if (ttl_cnt >= els_xri_cnt)
  12041. break;
  12042. }
  12043. err_exit:
  12044. if (rc == 0)
  12045. bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  12046. LPFC_XRI_RSRC_RDY);
  12047. return rc;
  12048. }
  12049. /**
  12050. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12051. * @phba: pointer to lpfc hba data structure.
  12052. * @sblist: pointer to scsi buffer list.
  12053. * @count: number of scsi buffers on the list.
  12054. *
  12055. * This routine is invoked to post a block of @count scsi sgl pages from a
  12056. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12057. * No Lock is held.
  12058. *
  12059. **/
  12060. int
  12061. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
  12062. int cnt)
  12063. {
  12064. struct lpfc_scsi_buf *psb;
  12065. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12066. struct sgl_page_pairs *sgl_pg_pairs;
  12067. void *viraddr;
  12068. LPFC_MBOXQ_t *mbox;
  12069. uint32_t reqlen, alloclen, pg_pairs;
  12070. uint32_t mbox_tmo;
  12071. uint16_t xritag_start = 0;
  12072. int rc = 0;
  12073. uint32_t shdr_status, shdr_add_status;
  12074. dma_addr_t pdma_phys_bpl1;
  12075. union lpfc_sli4_cfg_shdr *shdr;
  12076. /* Calculate the requested length of the dma memory */
  12077. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12078. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12079. if (reqlen > SLI4_PAGE_SIZE) {
  12080. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12081. "0217 Block sgl registration required DMA "
  12082. "size (%d) great than a page\n", reqlen);
  12083. return -ENOMEM;
  12084. }
  12085. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12086. if (!mbox) {
  12087. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12088. "0283 Failed to allocate mbox cmd memory\n");
  12089. return -ENOMEM;
  12090. }
  12091. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12092. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12093. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12094. LPFC_SLI4_MBX_NEMBED);
  12095. if (alloclen < reqlen) {
  12096. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12097. "2561 Allocated DMA memory size (%d) is "
  12098. "less than the requested DMA memory "
  12099. "size (%d)\n", alloclen, reqlen);
  12100. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12101. return -ENOMEM;
  12102. }
  12103. /* Get the first SGE entry from the non-embedded DMA memory */
  12104. viraddr = mbox->sge_array->addr[0];
  12105. /* Set up the SGL pages in the non-embedded DMA pages */
  12106. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12107. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12108. pg_pairs = 0;
  12109. list_for_each_entry(psb, sblist, list) {
  12110. /* Set up the sge entry */
  12111. sgl_pg_pairs->sgl_pg0_addr_lo =
  12112. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12113. sgl_pg_pairs->sgl_pg0_addr_hi =
  12114. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12115. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12116. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12117. else
  12118. pdma_phys_bpl1 = 0;
  12119. sgl_pg_pairs->sgl_pg1_addr_lo =
  12120. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12121. sgl_pg_pairs->sgl_pg1_addr_hi =
  12122. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12123. /* Keep the first xritag on the list */
  12124. if (pg_pairs == 0)
  12125. xritag_start = psb->cur_iocbq.sli4_xritag;
  12126. sgl_pg_pairs++;
  12127. pg_pairs++;
  12128. }
  12129. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12130. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12131. /* Perform endian conversion if necessary */
  12132. sgl->word0 = cpu_to_le32(sgl->word0);
  12133. if (!phba->sli4_hba.intr_enable)
  12134. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12135. else {
  12136. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12137. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12138. }
  12139. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12140. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12141. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12142. if (rc != MBX_TIMEOUT)
  12143. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12144. if (shdr_status || shdr_add_status || rc) {
  12145. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12146. "2564 POST_SGL_BLOCK mailbox command failed "
  12147. "status x%x add_status x%x mbx status x%x\n",
  12148. shdr_status, shdr_add_status, rc);
  12149. rc = -ENXIO;
  12150. }
  12151. return rc;
  12152. }
  12153. /**
  12154. * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
  12155. * @phba: pointer to lpfc hba data structure.
  12156. * @sblist: pointer to scsi buffer list.
  12157. * @count: number of scsi buffers on the list.
  12158. *
  12159. * This routine is invoked to post a block of @count scsi sgl pages from a
  12160. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12161. * No Lock is held.
  12162. *
  12163. **/
  12164. int
  12165. lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
  12166. int cnt)
  12167. {
  12168. struct lpfc_scsi_buf *psb = NULL;
  12169. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12170. struct sgl_page_pairs *sgl_pg_pairs;
  12171. void *viraddr;
  12172. LPFC_MBOXQ_t *mbox;
  12173. uint32_t reqlen, alloclen, pg_pairs;
  12174. uint32_t mbox_tmo;
  12175. uint16_t xri_start = 0, scsi_xri_start;
  12176. uint16_t rsrc_range;
  12177. int rc = 0, avail_cnt;
  12178. uint32_t shdr_status, shdr_add_status;
  12179. dma_addr_t pdma_phys_bpl1;
  12180. union lpfc_sli4_cfg_shdr *shdr;
  12181. struct lpfc_rsrc_blks *rsrc_blk;
  12182. uint32_t xri_cnt = 0;
  12183. /* Calculate the total requested length of the dma memory */
  12184. reqlen = cnt * sizeof(struct sgl_page_pairs) +
  12185. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12186. if (reqlen > SLI4_PAGE_SIZE) {
  12187. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12188. "2932 Block sgl registration required DMA "
  12189. "size (%d) great than a page\n", reqlen);
  12190. return -ENOMEM;
  12191. }
  12192. /*
  12193. * The use of extents requires the driver to post the sgl headers
  12194. * in multiple postings to meet the contiguous resource assignment.
  12195. */
  12196. psb = list_prepare_entry(psb, sblist, list);
  12197. scsi_xri_start = phba->sli4_hba.scsi_xri_start;
  12198. list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
  12199. list) {
  12200. rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
  12201. if (rsrc_range < scsi_xri_start)
  12202. continue;
  12203. else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
  12204. continue;
  12205. else
  12206. avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
  12207. reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
  12208. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12209. /*
  12210. * Allocate DMA memory and set up the non-embedded mailbox
  12211. * command. The mbox is used to post an SGL page per loop
  12212. * but the DMA memory has a use-once semantic so the mailbox
  12213. * is used and freed per loop pass.
  12214. */
  12215. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12216. if (!mbox) {
  12217. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12218. "2933 Failed to allocate mbox cmd "
  12219. "memory\n");
  12220. return -ENOMEM;
  12221. }
  12222. alloclen = lpfc_sli4_config(phba, mbox,
  12223. LPFC_MBOX_SUBSYSTEM_FCOE,
  12224. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12225. reqlen,
  12226. LPFC_SLI4_MBX_NEMBED);
  12227. if (alloclen < reqlen) {
  12228. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12229. "2934 Allocated DMA memory size (%d) "
  12230. "is less than the requested DMA memory "
  12231. "size (%d)\n", alloclen, reqlen);
  12232. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12233. return -ENOMEM;
  12234. }
  12235. /* Get the first SGE entry from the non-embedded DMA memory */
  12236. viraddr = mbox->sge_array->addr[0];
  12237. /* Set up the SGL pages in the non-embedded DMA pages */
  12238. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12239. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12240. /* pg_pairs tracks posted SGEs per loop iteration. */
  12241. pg_pairs = 0;
  12242. list_for_each_entry_continue(psb, sblist, list) {
  12243. /* Set up the sge entry */
  12244. sgl_pg_pairs->sgl_pg0_addr_lo =
  12245. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12246. sgl_pg_pairs->sgl_pg0_addr_hi =
  12247. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12248. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12249. pdma_phys_bpl1 = psb->dma_phys_bpl +
  12250. SGL_PAGE_SIZE;
  12251. else
  12252. pdma_phys_bpl1 = 0;
  12253. sgl_pg_pairs->sgl_pg1_addr_lo =
  12254. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12255. sgl_pg_pairs->sgl_pg1_addr_hi =
  12256. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12257. /* Keep the first xri for this extent. */
  12258. if (pg_pairs == 0)
  12259. xri_start = psb->cur_iocbq.sli4_xritag;
  12260. sgl_pg_pairs++;
  12261. pg_pairs++;
  12262. xri_cnt++;
  12263. /*
  12264. * Track two exit conditions - the loop has constructed
  12265. * all of the caller's SGE pairs or all available
  12266. * resource IDs in this extent are consumed.
  12267. */
  12268. if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
  12269. break;
  12270. }
  12271. rsrc_blk->rsrc_used += pg_pairs;
  12272. bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
  12273. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12274. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  12275. "3016 Post SCSI Extent SGL, start %d, cnt %d "
  12276. "blk use %d\n",
  12277. xri_start, pg_pairs, rsrc_blk->rsrc_used);
  12278. /* Perform endian conversion if necessary */
  12279. sgl->word0 = cpu_to_le32(sgl->word0);
  12280. if (!phba->sli4_hba.intr_enable)
  12281. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12282. else {
  12283. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12284. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12285. }
  12286. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12287. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12288. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  12289. &shdr->response);
  12290. if (rc != MBX_TIMEOUT)
  12291. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12292. if (shdr_status || shdr_add_status || rc) {
  12293. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12294. "2935 POST_SGL_BLOCK mailbox command "
  12295. "failed status x%x add_status x%x "
  12296. "mbx status x%x\n",
  12297. shdr_status, shdr_add_status, rc);
  12298. return -ENXIO;
  12299. }
  12300. /* Post only what is requested. */
  12301. if (xri_cnt >= cnt)
  12302. break;
  12303. }
  12304. return rc;
  12305. }
  12306. /**
  12307. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12308. * @phba: pointer to lpfc_hba struct that the frame was received on
  12309. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12310. *
  12311. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12312. * valid type of frame that the LPFC driver will handle. This function will
  12313. * return a zero if the frame is a valid frame or a non zero value when the
  12314. * frame does not pass the check.
  12315. **/
  12316. static int
  12317. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12318. {
  12319. /* make rctl_names static to save stack space */
  12320. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12321. char *type_names[] = FC_TYPE_NAMES_INIT;
  12322. struct fc_vft_header *fc_vft_hdr;
  12323. uint32_t *header = (uint32_t *) fc_hdr;
  12324. switch (fc_hdr->fh_r_ctl) {
  12325. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12326. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12327. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12328. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12329. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12330. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12331. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12332. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12333. case FC_RCTL_ELS_REQ: /* extended link services request */
  12334. case FC_RCTL_ELS_REP: /* extended link services reply */
  12335. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12336. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12337. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12338. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12339. case FC_RCTL_BA_RMC: /* remove connection */
  12340. case FC_RCTL_BA_ACC: /* basic accept */
  12341. case FC_RCTL_BA_RJT: /* basic reject */
  12342. case FC_RCTL_BA_PRMT:
  12343. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12344. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12345. case FC_RCTL_P_RJT: /* port reject */
  12346. case FC_RCTL_F_RJT: /* fabric reject */
  12347. case FC_RCTL_P_BSY: /* port busy */
  12348. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12349. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12350. case FC_RCTL_LCR: /* link credit reset */
  12351. case FC_RCTL_END: /* end */
  12352. break;
  12353. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12354. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12355. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12356. return lpfc_fc_frame_check(phba, fc_hdr);
  12357. default:
  12358. goto drop;
  12359. }
  12360. switch (fc_hdr->fh_type) {
  12361. case FC_TYPE_BLS:
  12362. case FC_TYPE_ELS:
  12363. case FC_TYPE_FCP:
  12364. case FC_TYPE_CT:
  12365. break;
  12366. case FC_TYPE_IP:
  12367. case FC_TYPE_ILS:
  12368. default:
  12369. goto drop;
  12370. }
  12371. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12372. "2538 Received frame rctl:%s type:%s "
  12373. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12374. rctl_names[fc_hdr->fh_r_ctl],
  12375. type_names[fc_hdr->fh_type],
  12376. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12377. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12378. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12379. return 0;
  12380. drop:
  12381. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12382. "2539 Dropped frame rctl:%s type:%s\n",
  12383. rctl_names[fc_hdr->fh_r_ctl],
  12384. type_names[fc_hdr->fh_type]);
  12385. return 1;
  12386. }
  12387. /**
  12388. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12389. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12390. *
  12391. * This function processes the FC header to retrieve the VFI from the VF
  12392. * header, if one exists. This function will return the VFI if one exists
  12393. * or 0 if no VSAN Header exists.
  12394. **/
  12395. static uint32_t
  12396. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12397. {
  12398. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12399. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12400. return 0;
  12401. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12402. }
  12403. /**
  12404. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12405. * @phba: Pointer to the HBA structure to search for the vport on
  12406. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12407. * @fcfi: The FC Fabric ID that the frame came from
  12408. *
  12409. * This function searches the @phba for a vport that matches the content of the
  12410. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12411. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12412. * returns the matching vport pointer or NULL if unable to match frame to a
  12413. * vport.
  12414. **/
  12415. static struct lpfc_vport *
  12416. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12417. uint16_t fcfi)
  12418. {
  12419. struct lpfc_vport **vports;
  12420. struct lpfc_vport *vport = NULL;
  12421. int i;
  12422. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12423. fc_hdr->fh_d_id[1] << 8 |
  12424. fc_hdr->fh_d_id[2]);
  12425. if (did == Fabric_DID)
  12426. return phba->pport;
  12427. vports = lpfc_create_vport_work_array(phba);
  12428. if (vports != NULL)
  12429. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12430. if (phba->fcf.fcfi == fcfi &&
  12431. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12432. vports[i]->fc_myDID == did) {
  12433. vport = vports[i];
  12434. break;
  12435. }
  12436. }
  12437. lpfc_destroy_vport_work_array(phba, vports);
  12438. return vport;
  12439. }
  12440. /**
  12441. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12442. * @vport: The vport to work on.
  12443. *
  12444. * This function updates the receive sequence time stamp for this vport. The
  12445. * receive sequence time stamp indicates the time that the last frame of the
  12446. * the sequence that has been idle for the longest amount of time was received.
  12447. * the driver uses this time stamp to indicate if any received sequences have
  12448. * timed out.
  12449. **/
  12450. void
  12451. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12452. {
  12453. struct lpfc_dmabuf *h_buf;
  12454. struct hbq_dmabuf *dmabuf = NULL;
  12455. /* get the oldest sequence on the rcv list */
  12456. h_buf = list_get_first(&vport->rcv_buffer_list,
  12457. struct lpfc_dmabuf, list);
  12458. if (!h_buf)
  12459. return;
  12460. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12461. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12462. }
  12463. /**
  12464. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12465. * @vport: The vport that the received sequences were sent to.
  12466. *
  12467. * This function cleans up all outstanding received sequences. This is called
  12468. * by the driver when a link event or user action invalidates all the received
  12469. * sequences.
  12470. **/
  12471. void
  12472. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12473. {
  12474. struct lpfc_dmabuf *h_buf, *hnext;
  12475. struct lpfc_dmabuf *d_buf, *dnext;
  12476. struct hbq_dmabuf *dmabuf = NULL;
  12477. /* start with the oldest sequence on the rcv list */
  12478. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12479. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12480. list_del_init(&dmabuf->hbuf.list);
  12481. list_for_each_entry_safe(d_buf, dnext,
  12482. &dmabuf->dbuf.list, list) {
  12483. list_del_init(&d_buf->list);
  12484. lpfc_in_buf_free(vport->phba, d_buf);
  12485. }
  12486. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12487. }
  12488. }
  12489. /**
  12490. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12491. * @vport: The vport that the received sequences were sent to.
  12492. *
  12493. * This function determines whether any received sequences have timed out by
  12494. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12495. * indicates that there is at least one timed out sequence this routine will
  12496. * go through the received sequences one at a time from most inactive to most
  12497. * active to determine which ones need to be cleaned up. Once it has determined
  12498. * that a sequence needs to be cleaned up it will simply free up the resources
  12499. * without sending an abort.
  12500. **/
  12501. void
  12502. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12503. {
  12504. struct lpfc_dmabuf *h_buf, *hnext;
  12505. struct lpfc_dmabuf *d_buf, *dnext;
  12506. struct hbq_dmabuf *dmabuf = NULL;
  12507. unsigned long timeout;
  12508. int abort_count = 0;
  12509. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12510. vport->rcv_buffer_time_stamp);
  12511. if (list_empty(&vport->rcv_buffer_list) ||
  12512. time_before(jiffies, timeout))
  12513. return;
  12514. /* start with the oldest sequence on the rcv list */
  12515. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12516. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12517. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12518. dmabuf->time_stamp);
  12519. if (time_before(jiffies, timeout))
  12520. break;
  12521. abort_count++;
  12522. list_del_init(&dmabuf->hbuf.list);
  12523. list_for_each_entry_safe(d_buf, dnext,
  12524. &dmabuf->dbuf.list, list) {
  12525. list_del_init(&d_buf->list);
  12526. lpfc_in_buf_free(vport->phba, d_buf);
  12527. }
  12528. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12529. }
  12530. if (abort_count)
  12531. lpfc_update_rcv_time_stamp(vport);
  12532. }
  12533. /**
  12534. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12535. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12536. *
  12537. * This function searches through the existing incomplete sequences that have
  12538. * been sent to this @vport. If the frame matches one of the incomplete
  12539. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12540. * make up that sequence. If no sequence is found that matches this frame then
  12541. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12542. * This function returns a pointer to the first dmabuf in the sequence list that
  12543. * the frame was linked to.
  12544. **/
  12545. static struct hbq_dmabuf *
  12546. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12547. {
  12548. struct fc_frame_header *new_hdr;
  12549. struct fc_frame_header *temp_hdr;
  12550. struct lpfc_dmabuf *d_buf;
  12551. struct lpfc_dmabuf *h_buf;
  12552. struct hbq_dmabuf *seq_dmabuf = NULL;
  12553. struct hbq_dmabuf *temp_dmabuf = NULL;
  12554. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12555. dmabuf->time_stamp = jiffies;
  12556. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12557. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12558. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12559. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12560. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12561. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12562. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12563. continue;
  12564. /* found a pending sequence that matches this frame */
  12565. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12566. break;
  12567. }
  12568. if (!seq_dmabuf) {
  12569. /*
  12570. * This indicates first frame received for this sequence.
  12571. * Queue the buffer on the vport's rcv_buffer_list.
  12572. */
  12573. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12574. lpfc_update_rcv_time_stamp(vport);
  12575. return dmabuf;
  12576. }
  12577. temp_hdr = seq_dmabuf->hbuf.virt;
  12578. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12579. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12580. list_del_init(&seq_dmabuf->hbuf.list);
  12581. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12582. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12583. lpfc_update_rcv_time_stamp(vport);
  12584. return dmabuf;
  12585. }
  12586. /* move this sequence to the tail to indicate a young sequence */
  12587. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12588. seq_dmabuf->time_stamp = jiffies;
  12589. lpfc_update_rcv_time_stamp(vport);
  12590. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12591. temp_hdr = dmabuf->hbuf.virt;
  12592. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12593. return seq_dmabuf;
  12594. }
  12595. /* find the correct place in the sequence to insert this frame */
  12596. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12597. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12598. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12599. /*
  12600. * If the frame's sequence count is greater than the frame on
  12601. * the list then insert the frame right after this frame
  12602. */
  12603. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12604. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12605. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12606. return seq_dmabuf;
  12607. }
  12608. }
  12609. return NULL;
  12610. }
  12611. /**
  12612. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12613. * @vport: pointer to a vitural port
  12614. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12615. *
  12616. * This function tries to abort from the partially assembed sequence, described
  12617. * by the information from basic abbort @dmabuf. It checks to see whether such
  12618. * partially assembled sequence held by the driver. If so, it shall free up all
  12619. * the frames from the partially assembled sequence.
  12620. *
  12621. * Return
  12622. * true -- if there is matching partially assembled sequence present and all
  12623. * the frames freed with the sequence;
  12624. * false -- if there is no matching partially assembled sequence present so
  12625. * nothing got aborted in the lower layer driver
  12626. **/
  12627. static bool
  12628. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12629. struct hbq_dmabuf *dmabuf)
  12630. {
  12631. struct fc_frame_header *new_hdr;
  12632. struct fc_frame_header *temp_hdr;
  12633. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12634. struct hbq_dmabuf *seq_dmabuf = NULL;
  12635. /* Use the hdr_buf to find the sequence that matches this frame */
  12636. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12637. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12638. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12639. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12640. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12641. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12642. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12643. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12644. continue;
  12645. /* found a pending sequence that matches this frame */
  12646. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12647. break;
  12648. }
  12649. /* Free up all the frames from the partially assembled sequence */
  12650. if (seq_dmabuf) {
  12651. list_for_each_entry_safe(d_buf, n_buf,
  12652. &seq_dmabuf->dbuf.list, list) {
  12653. list_del_init(&d_buf->list);
  12654. lpfc_in_buf_free(vport->phba, d_buf);
  12655. }
  12656. return true;
  12657. }
  12658. return false;
  12659. }
  12660. /**
  12661. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12662. * @phba: Pointer to HBA context object.
  12663. * @cmd_iocbq: pointer to the command iocbq structure.
  12664. * @rsp_iocbq: pointer to the response iocbq structure.
  12665. *
  12666. * This function handles the sequence abort response iocb command complete
  12667. * event. It properly releases the memory allocated to the sequence abort
  12668. * accept iocb.
  12669. **/
  12670. static void
  12671. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12672. struct lpfc_iocbq *cmd_iocbq,
  12673. struct lpfc_iocbq *rsp_iocbq)
  12674. {
  12675. if (cmd_iocbq)
  12676. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12677. }
  12678. /**
  12679. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12680. * @phba: Pointer to HBA context object.
  12681. * @xri: xri id in transaction.
  12682. *
  12683. * This function validates the xri maps to the known range of XRIs allocated an
  12684. * used by the driver.
  12685. **/
  12686. uint16_t
  12687. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12688. uint16_t xri)
  12689. {
  12690. int i;
  12691. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12692. if (xri == phba->sli4_hba.xri_ids[i])
  12693. return i;
  12694. }
  12695. return NO_XRI;
  12696. }
  12697. /**
  12698. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12699. * @phba: Pointer to HBA context object.
  12700. * @fc_hdr: pointer to a FC frame header.
  12701. *
  12702. * This function sends a basic response to a previous unsol sequence abort
  12703. * event after aborting the sequence handling.
  12704. **/
  12705. static void
  12706. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12707. struct fc_frame_header *fc_hdr)
  12708. {
  12709. struct lpfc_iocbq *ctiocb = NULL;
  12710. struct lpfc_nodelist *ndlp;
  12711. uint16_t oxid, rxid;
  12712. uint32_t sid, fctl;
  12713. IOCB_t *icmd;
  12714. int rc;
  12715. if (!lpfc_is_link_up(phba))
  12716. return;
  12717. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12718. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12719. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12720. ndlp = lpfc_findnode_did(phba->pport, sid);
  12721. if (!ndlp) {
  12722. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12723. "1268 Find ndlp returned NULL for oxid:x%x "
  12724. "SID:x%x\n", oxid, sid);
  12725. return;
  12726. }
  12727. if (lpfc_sli4_xri_inrange(phba, rxid))
  12728. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12729. /* Allocate buffer for rsp iocb */
  12730. ctiocb = lpfc_sli_get_iocbq(phba);
  12731. if (!ctiocb)
  12732. return;
  12733. /* Extract the F_CTL field from FC_HDR */
  12734. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12735. icmd = &ctiocb->iocb;
  12736. icmd->un.xseq64.bdl.bdeSize = 0;
  12737. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12738. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12739. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12740. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12741. /* Fill in the rest of iocb fields */
  12742. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12743. icmd->ulpBdeCount = 0;
  12744. icmd->ulpLe = 1;
  12745. icmd->ulpClass = CLASS3;
  12746. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12747. ctiocb->context1 = ndlp;
  12748. ctiocb->iocb_cmpl = NULL;
  12749. ctiocb->vport = phba->pport;
  12750. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12751. ctiocb->sli4_lxritag = NO_XRI;
  12752. ctiocb->sli4_xritag = NO_XRI;
  12753. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12754. * send a BLS_RJT. The driver no longer has that exchange.
  12755. * Override the IOCB for a BA_RJT.
  12756. */
  12757. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  12758. phba->sli4_hba.max_cfg_param.xri_base) ||
  12759. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12760. phba->sli4_hba.max_cfg_param.xri_base)) {
  12761. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12762. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12763. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12764. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12765. }
  12766. if (fctl & FC_FC_EX_CTX) {
  12767. /* ABTS sent by responder to CT exchange, construction
  12768. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12769. * field and RX_ID from ABTS for RX_ID field.
  12770. */
  12771. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12772. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12773. } else {
  12774. /* ABTS sent by initiator to CT exchange, construction
  12775. * of BA_ACC will need to allocate a new XRI as for the
  12776. * XRI_TAG and RX_ID fields.
  12777. */
  12778. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12779. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
  12780. }
  12781. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12782. /* Xmit CT abts response on exchange <xid> */
  12783. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12784. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12785. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12786. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12787. if (rc == IOCB_ERROR) {
  12788. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12789. "2925 Failed to issue CT ABTS RSP x%x on "
  12790. "xri x%x, Data x%x\n",
  12791. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12792. phba->link_state);
  12793. lpfc_sli_release_iocbq(phba, ctiocb);
  12794. }
  12795. }
  12796. /**
  12797. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12798. * @vport: Pointer to the vport on which this sequence was received
  12799. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12800. *
  12801. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12802. * receive sequence is only partially assembed by the driver, it shall abort
  12803. * the partially assembled frames for the sequence. Otherwise, if the
  12804. * unsolicited receive sequence has been completely assembled and passed to
  12805. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  12806. * unsolicited sequence has been aborted. After that, it will issue a basic
  12807. * accept to accept the abort.
  12808. **/
  12809. void
  12810. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  12811. struct hbq_dmabuf *dmabuf)
  12812. {
  12813. struct lpfc_hba *phba = vport->phba;
  12814. struct fc_frame_header fc_hdr;
  12815. uint32_t fctl;
  12816. bool abts_par;
  12817. /* Make a copy of fc_hdr before the dmabuf being released */
  12818. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  12819. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  12820. if (fctl & FC_FC_EX_CTX) {
  12821. /*
  12822. * ABTS sent by responder to exchange, just free the buffer
  12823. */
  12824. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12825. } else {
  12826. /*
  12827. * ABTS sent by initiator to exchange, need to do cleanup
  12828. */
  12829. /* Try to abort partially assembled seq */
  12830. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  12831. /* Send abort to ULP if partially seq abort failed */
  12832. if (abts_par == false)
  12833. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  12834. else
  12835. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12836. }
  12837. /* Send basic accept (BA_ACC) to the abort requester */
  12838. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  12839. }
  12840. /**
  12841. * lpfc_seq_complete - Indicates if a sequence is complete
  12842. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12843. *
  12844. * This function checks the sequence, starting with the frame described by
  12845. * @dmabuf, to see if all the frames associated with this sequence are present.
  12846. * the frames associated with this sequence are linked to the @dmabuf using the
  12847. * dbuf list. This function looks for two major things. 1) That the first frame
  12848. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  12849. * set. 3) That there are no holes in the sequence count. The function will
  12850. * return 1 when the sequence is complete, otherwise it will return 0.
  12851. **/
  12852. static int
  12853. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  12854. {
  12855. struct fc_frame_header *hdr;
  12856. struct lpfc_dmabuf *d_buf;
  12857. struct hbq_dmabuf *seq_dmabuf;
  12858. uint32_t fctl;
  12859. int seq_count = 0;
  12860. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12861. /* make sure first fame of sequence has a sequence count of zero */
  12862. if (hdr->fh_seq_cnt != seq_count)
  12863. return 0;
  12864. fctl = (hdr->fh_f_ctl[0] << 16 |
  12865. hdr->fh_f_ctl[1] << 8 |
  12866. hdr->fh_f_ctl[2]);
  12867. /* If last frame of sequence we can return success. */
  12868. if (fctl & FC_FC_END_SEQ)
  12869. return 1;
  12870. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  12871. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12872. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12873. /* If there is a hole in the sequence count then fail. */
  12874. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  12875. return 0;
  12876. fctl = (hdr->fh_f_ctl[0] << 16 |
  12877. hdr->fh_f_ctl[1] << 8 |
  12878. hdr->fh_f_ctl[2]);
  12879. /* If last frame of sequence we can return success. */
  12880. if (fctl & FC_FC_END_SEQ)
  12881. return 1;
  12882. }
  12883. return 0;
  12884. }
  12885. /**
  12886. * lpfc_prep_seq - Prep sequence for ULP processing
  12887. * @vport: Pointer to the vport on which this sequence was received
  12888. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12889. *
  12890. * This function takes a sequence, described by a list of frames, and creates
  12891. * a list of iocbq structures to describe the sequence. This iocbq list will be
  12892. * used to issue to the generic unsolicited sequence handler. This routine
  12893. * returns a pointer to the first iocbq in the list. If the function is unable
  12894. * to allocate an iocbq then it throw out the received frames that were not
  12895. * able to be described and return a pointer to the first iocbq. If unable to
  12896. * allocate any iocbqs (including the first) this function will return NULL.
  12897. **/
  12898. static struct lpfc_iocbq *
  12899. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  12900. {
  12901. struct hbq_dmabuf *hbq_buf;
  12902. struct lpfc_dmabuf *d_buf, *n_buf;
  12903. struct lpfc_iocbq *first_iocbq, *iocbq;
  12904. struct fc_frame_header *fc_hdr;
  12905. uint32_t sid;
  12906. uint32_t len, tot_len;
  12907. struct ulp_bde64 *pbde;
  12908. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12909. /* remove from receive buffer list */
  12910. list_del_init(&seq_dmabuf->hbuf.list);
  12911. lpfc_update_rcv_time_stamp(vport);
  12912. /* get the Remote Port's SID */
  12913. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12914. tot_len = 0;
  12915. /* Get an iocbq struct to fill in. */
  12916. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  12917. if (first_iocbq) {
  12918. /* Initialize the first IOCB. */
  12919. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  12920. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  12921. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  12922. first_iocbq->iocb.ulpContext = NO_XRI;
  12923. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  12924. be16_to_cpu(fc_hdr->fh_ox_id);
  12925. /* iocbq is prepped for internal consumption. Physical vpi. */
  12926. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  12927. vport->phba->vpi_ids[vport->vpi];
  12928. /* put the first buffer into the first IOCBq */
  12929. first_iocbq->context2 = &seq_dmabuf->dbuf;
  12930. first_iocbq->context3 = NULL;
  12931. first_iocbq->iocb.ulpBdeCount = 1;
  12932. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  12933. LPFC_DATA_BUF_SIZE;
  12934. first_iocbq->iocb.un.rcvels.remoteID = sid;
  12935. tot_len = bf_get(lpfc_rcqe_length,
  12936. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  12937. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  12938. }
  12939. iocbq = first_iocbq;
  12940. /*
  12941. * Each IOCBq can have two Buffers assigned, so go through the list
  12942. * of buffers for this sequence and save two buffers in each IOCBq
  12943. */
  12944. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  12945. if (!iocbq) {
  12946. lpfc_in_buf_free(vport->phba, d_buf);
  12947. continue;
  12948. }
  12949. if (!iocbq->context3) {
  12950. iocbq->context3 = d_buf;
  12951. iocbq->iocb.ulpBdeCount++;
  12952. pbde = (struct ulp_bde64 *)
  12953. &iocbq->iocb.unsli3.sli3Words[4];
  12954. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  12955. /* We need to get the size out of the right CQE */
  12956. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12957. len = bf_get(lpfc_rcqe_length,
  12958. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  12959. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  12960. tot_len += len;
  12961. } else {
  12962. iocbq = lpfc_sli_get_iocbq(vport->phba);
  12963. if (!iocbq) {
  12964. if (first_iocbq) {
  12965. first_iocbq->iocb.ulpStatus =
  12966. IOSTAT_FCP_RSP_ERROR;
  12967. first_iocbq->iocb.un.ulpWord[4] =
  12968. IOERR_NO_RESOURCES;
  12969. }
  12970. lpfc_in_buf_free(vport->phba, d_buf);
  12971. continue;
  12972. }
  12973. iocbq->context2 = d_buf;
  12974. iocbq->context3 = NULL;
  12975. iocbq->iocb.ulpBdeCount = 1;
  12976. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  12977. LPFC_DATA_BUF_SIZE;
  12978. /* We need to get the size out of the right CQE */
  12979. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12980. len = bf_get(lpfc_rcqe_length,
  12981. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  12982. tot_len += len;
  12983. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  12984. iocbq->iocb.un.rcvels.remoteID = sid;
  12985. list_add_tail(&iocbq->list, &first_iocbq->list);
  12986. }
  12987. }
  12988. return first_iocbq;
  12989. }
  12990. static void
  12991. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  12992. struct hbq_dmabuf *seq_dmabuf)
  12993. {
  12994. struct fc_frame_header *fc_hdr;
  12995. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  12996. struct lpfc_hba *phba = vport->phba;
  12997. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12998. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  12999. if (!iocbq) {
  13000. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13001. "2707 Ring %d handler: Failed to allocate "
  13002. "iocb Rctl x%x Type x%x received\n",
  13003. LPFC_ELS_RING,
  13004. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13005. return;
  13006. }
  13007. if (!lpfc_complete_unsol_iocb(phba,
  13008. &phba->sli.ring[LPFC_ELS_RING],
  13009. iocbq, fc_hdr->fh_r_ctl,
  13010. fc_hdr->fh_type))
  13011. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13012. "2540 Ring %d handler: unexpected Rctl "
  13013. "x%x Type x%x received\n",
  13014. LPFC_ELS_RING,
  13015. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13016. /* Free iocb created in lpfc_prep_seq */
  13017. list_for_each_entry_safe(curr_iocb, next_iocb,
  13018. &iocbq->list, list) {
  13019. list_del_init(&curr_iocb->list);
  13020. lpfc_sli_release_iocbq(phba, curr_iocb);
  13021. }
  13022. lpfc_sli_release_iocbq(phba, iocbq);
  13023. }
  13024. /**
  13025. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13026. * @phba: Pointer to HBA context object.
  13027. *
  13028. * This function is called with no lock held. This function processes all
  13029. * the received buffers and gives it to upper layers when a received buffer
  13030. * indicates that it is the final frame in the sequence. The interrupt
  13031. * service routine processes received buffers at interrupt contexts and adds
  13032. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13033. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13034. * appropriate receive function when the final frame in a sequence is received.
  13035. **/
  13036. void
  13037. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13038. struct hbq_dmabuf *dmabuf)
  13039. {
  13040. struct hbq_dmabuf *seq_dmabuf;
  13041. struct fc_frame_header *fc_hdr;
  13042. struct lpfc_vport *vport;
  13043. uint32_t fcfi;
  13044. /* Process each received buffer */
  13045. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13046. /* check to see if this a valid type of frame */
  13047. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13048. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13049. return;
  13050. }
  13051. if ((bf_get(lpfc_cqe_code,
  13052. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13053. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13054. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13055. else
  13056. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13057. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13058. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13059. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13060. /* throw out the frame */
  13061. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13062. return;
  13063. }
  13064. /* Handle the basic abort sequence (BA_ABTS) event */
  13065. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13066. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13067. return;
  13068. }
  13069. /* Link this frame */
  13070. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13071. if (!seq_dmabuf) {
  13072. /* unable to add frame to vport - throw it out */
  13073. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13074. return;
  13075. }
  13076. /* If not last frame in sequence continue processing frames. */
  13077. if (!lpfc_seq_complete(seq_dmabuf))
  13078. return;
  13079. /* Send the complete sequence to the upper layer protocol */
  13080. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13081. }
  13082. /**
  13083. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13084. * @phba: pointer to lpfc hba data structure.
  13085. *
  13086. * This routine is invoked to post rpi header templates to the
  13087. * HBA consistent with the SLI-4 interface spec. This routine
  13088. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13089. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13090. *
  13091. * This routine does not require any locks. It's usage is expected
  13092. * to be driver load or reset recovery when the driver is
  13093. * sequential.
  13094. *
  13095. * Return codes
  13096. * 0 - successful
  13097. * -EIO - The mailbox failed to complete successfully.
  13098. * When this error occurs, the driver is not guaranteed
  13099. * to have any rpi regions posted to the device and
  13100. * must either attempt to repost the regions or take a
  13101. * fatal error.
  13102. **/
  13103. int
  13104. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13105. {
  13106. struct lpfc_rpi_hdr *rpi_page;
  13107. uint32_t rc = 0;
  13108. uint16_t lrpi = 0;
  13109. /* SLI4 ports that support extents do not require RPI headers. */
  13110. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13111. goto exit;
  13112. if (phba->sli4_hba.extents_in_use)
  13113. return -EIO;
  13114. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13115. /*
  13116. * Assign the rpi headers a physical rpi only if the driver
  13117. * has not initialized those resources. A port reset only
  13118. * needs the headers posted.
  13119. */
  13120. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13121. LPFC_RPI_RSRC_RDY)
  13122. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13123. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13124. if (rc != MBX_SUCCESS) {
  13125. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13126. "2008 Error %d posting all rpi "
  13127. "headers\n", rc);
  13128. rc = -EIO;
  13129. break;
  13130. }
  13131. }
  13132. exit:
  13133. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13134. LPFC_RPI_RSRC_RDY);
  13135. return rc;
  13136. }
  13137. /**
  13138. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13139. * @phba: pointer to lpfc hba data structure.
  13140. * @rpi_page: pointer to the rpi memory region.
  13141. *
  13142. * This routine is invoked to post a single rpi header to the
  13143. * HBA consistent with the SLI-4 interface spec. This memory region
  13144. * maps up to 64 rpi context regions.
  13145. *
  13146. * Return codes
  13147. * 0 - successful
  13148. * -ENOMEM - No available memory
  13149. * -EIO - The mailbox failed to complete successfully.
  13150. **/
  13151. int
  13152. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13153. {
  13154. LPFC_MBOXQ_t *mboxq;
  13155. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13156. uint32_t rc = 0;
  13157. uint32_t shdr_status, shdr_add_status;
  13158. union lpfc_sli4_cfg_shdr *shdr;
  13159. /* SLI4 ports that support extents do not require RPI headers. */
  13160. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13161. return rc;
  13162. if (phba->sli4_hba.extents_in_use)
  13163. return -EIO;
  13164. /* The port is notified of the header region via a mailbox command. */
  13165. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13166. if (!mboxq) {
  13167. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13168. "2001 Unable to allocate memory for issuing "
  13169. "SLI_CONFIG_SPECIAL mailbox command\n");
  13170. return -ENOMEM;
  13171. }
  13172. /* Post all rpi memory regions to the port. */
  13173. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13174. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13175. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13176. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13177. sizeof(struct lpfc_sli4_cfg_mhdr),
  13178. LPFC_SLI4_MBX_EMBED);
  13179. /* Post the physical rpi to the port for this rpi header. */
  13180. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13181. rpi_page->start_rpi);
  13182. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13183. hdr_tmpl, rpi_page->page_count);
  13184. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13185. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13186. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13187. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13188. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13189. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13190. if (rc != MBX_TIMEOUT)
  13191. mempool_free(mboxq, phba->mbox_mem_pool);
  13192. if (shdr_status || shdr_add_status || rc) {
  13193. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13194. "2514 POST_RPI_HDR mailbox failed with "
  13195. "status x%x add_status x%x, mbx status x%x\n",
  13196. shdr_status, shdr_add_status, rc);
  13197. rc = -ENXIO;
  13198. }
  13199. return rc;
  13200. }
  13201. /**
  13202. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13203. * @phba: pointer to lpfc hba data structure.
  13204. *
  13205. * This routine is invoked to post rpi header templates to the
  13206. * HBA consistent with the SLI-4 interface spec. This routine
  13207. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13208. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13209. *
  13210. * Returns
  13211. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13212. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13213. **/
  13214. int
  13215. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13216. {
  13217. unsigned long rpi;
  13218. uint16_t max_rpi, rpi_limit;
  13219. uint16_t rpi_remaining, lrpi = 0;
  13220. struct lpfc_rpi_hdr *rpi_hdr;
  13221. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13222. rpi_limit = phba->sli4_hba.next_rpi;
  13223. /*
  13224. * Fetch the next logical rpi. Because this index is logical,
  13225. * the driver starts at 0 each time.
  13226. */
  13227. spin_lock_irq(&phba->hbalock);
  13228. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13229. if (rpi >= rpi_limit)
  13230. rpi = LPFC_RPI_ALLOC_ERROR;
  13231. else {
  13232. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13233. phba->sli4_hba.max_cfg_param.rpi_used++;
  13234. phba->sli4_hba.rpi_count++;
  13235. }
  13236. /*
  13237. * Don't try to allocate more rpi header regions if the device limit
  13238. * has been exhausted.
  13239. */
  13240. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13241. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13242. spin_unlock_irq(&phba->hbalock);
  13243. return rpi;
  13244. }
  13245. /*
  13246. * RPI header postings are not required for SLI4 ports capable of
  13247. * extents.
  13248. */
  13249. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13250. spin_unlock_irq(&phba->hbalock);
  13251. return rpi;
  13252. }
  13253. /*
  13254. * If the driver is running low on rpi resources, allocate another
  13255. * page now. Note that the next_rpi value is used because
  13256. * it represents how many are actually in use whereas max_rpi notes
  13257. * how many are supported max by the device.
  13258. */
  13259. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13260. spin_unlock_irq(&phba->hbalock);
  13261. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13262. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13263. if (!rpi_hdr) {
  13264. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13265. "2002 Error Could not grow rpi "
  13266. "count\n");
  13267. } else {
  13268. lrpi = rpi_hdr->start_rpi;
  13269. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13270. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13271. }
  13272. }
  13273. return rpi;
  13274. }
  13275. /**
  13276. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13277. * @phba: pointer to lpfc hba data structure.
  13278. *
  13279. * This routine is invoked to release an rpi to the pool of
  13280. * available rpis maintained by the driver.
  13281. **/
  13282. void
  13283. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13284. {
  13285. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13286. phba->sli4_hba.rpi_count--;
  13287. phba->sli4_hba.max_cfg_param.rpi_used--;
  13288. }
  13289. }
  13290. /**
  13291. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13292. * @phba: pointer to lpfc hba data structure.
  13293. *
  13294. * This routine is invoked to release an rpi to the pool of
  13295. * available rpis maintained by the driver.
  13296. **/
  13297. void
  13298. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13299. {
  13300. spin_lock_irq(&phba->hbalock);
  13301. __lpfc_sli4_free_rpi(phba, rpi);
  13302. spin_unlock_irq(&phba->hbalock);
  13303. }
  13304. /**
  13305. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13306. * @phba: pointer to lpfc hba data structure.
  13307. *
  13308. * This routine is invoked to remove the memory region that
  13309. * provided rpi via a bitmask.
  13310. **/
  13311. void
  13312. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13313. {
  13314. kfree(phba->sli4_hba.rpi_bmask);
  13315. kfree(phba->sli4_hba.rpi_ids);
  13316. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13317. }
  13318. /**
  13319. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13320. * @phba: pointer to lpfc hba data structure.
  13321. *
  13322. * This routine is invoked to remove the memory region that
  13323. * provided rpi via a bitmask.
  13324. **/
  13325. int
  13326. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
  13327. {
  13328. LPFC_MBOXQ_t *mboxq;
  13329. struct lpfc_hba *phba = ndlp->phba;
  13330. int rc;
  13331. /* The port is notified of the header region via a mailbox command. */
  13332. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13333. if (!mboxq)
  13334. return -ENOMEM;
  13335. /* Post all rpi memory regions to the port. */
  13336. lpfc_resume_rpi(mboxq, ndlp);
  13337. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13338. if (rc == MBX_NOT_FINISHED) {
  13339. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13340. "2010 Resume RPI Mailbox failed "
  13341. "status %d, mbxStatus x%x\n", rc,
  13342. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13343. mempool_free(mboxq, phba->mbox_mem_pool);
  13344. return -EIO;
  13345. }
  13346. return 0;
  13347. }
  13348. /**
  13349. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13350. * @vport: Pointer to the vport for which the vpi is being initialized
  13351. *
  13352. * This routine is invoked to activate a vpi with the port.
  13353. *
  13354. * Returns:
  13355. * 0 success
  13356. * -Evalue otherwise
  13357. **/
  13358. int
  13359. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13360. {
  13361. LPFC_MBOXQ_t *mboxq;
  13362. int rc = 0;
  13363. int retval = MBX_SUCCESS;
  13364. uint32_t mbox_tmo;
  13365. struct lpfc_hba *phba = vport->phba;
  13366. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13367. if (!mboxq)
  13368. return -ENOMEM;
  13369. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13370. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13371. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13372. if (rc != MBX_SUCCESS) {
  13373. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13374. "2022 INIT VPI Mailbox failed "
  13375. "status %d, mbxStatus x%x\n", rc,
  13376. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13377. retval = -EIO;
  13378. }
  13379. if (rc != MBX_TIMEOUT)
  13380. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13381. return retval;
  13382. }
  13383. /**
  13384. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13385. * @phba: pointer to lpfc hba data structure.
  13386. * @mboxq: Pointer to mailbox object.
  13387. *
  13388. * This routine is invoked to manually add a single FCF record. The caller
  13389. * must pass a completely initialized FCF_Record. This routine takes
  13390. * care of the nonembedded mailbox operations.
  13391. **/
  13392. static void
  13393. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13394. {
  13395. void *virt_addr;
  13396. union lpfc_sli4_cfg_shdr *shdr;
  13397. uint32_t shdr_status, shdr_add_status;
  13398. virt_addr = mboxq->sge_array->addr[0];
  13399. /* The IOCTL status is embedded in the mailbox subheader. */
  13400. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13401. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13402. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13403. if ((shdr_status || shdr_add_status) &&
  13404. (shdr_status != STATUS_FCF_IN_USE))
  13405. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13406. "2558 ADD_FCF_RECORD mailbox failed with "
  13407. "status x%x add_status x%x\n",
  13408. shdr_status, shdr_add_status);
  13409. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13410. }
  13411. /**
  13412. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13413. * @phba: pointer to lpfc hba data structure.
  13414. * @fcf_record: pointer to the initialized fcf record to add.
  13415. *
  13416. * This routine is invoked to manually add a single FCF record. The caller
  13417. * must pass a completely initialized FCF_Record. This routine takes
  13418. * care of the nonembedded mailbox operations.
  13419. **/
  13420. int
  13421. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13422. {
  13423. int rc = 0;
  13424. LPFC_MBOXQ_t *mboxq;
  13425. uint8_t *bytep;
  13426. void *virt_addr;
  13427. dma_addr_t phys_addr;
  13428. struct lpfc_mbx_sge sge;
  13429. uint32_t alloc_len, req_len;
  13430. uint32_t fcfindex;
  13431. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13432. if (!mboxq) {
  13433. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13434. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13435. return -ENOMEM;
  13436. }
  13437. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13438. sizeof(uint32_t);
  13439. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13440. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13441. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13442. req_len, LPFC_SLI4_MBX_NEMBED);
  13443. if (alloc_len < req_len) {
  13444. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13445. "2523 Allocated DMA memory size (x%x) is "
  13446. "less than the requested DMA memory "
  13447. "size (x%x)\n", alloc_len, req_len);
  13448. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13449. return -ENOMEM;
  13450. }
  13451. /*
  13452. * Get the first SGE entry from the non-embedded DMA memory. This
  13453. * routine only uses a single SGE.
  13454. */
  13455. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13456. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13457. virt_addr = mboxq->sge_array->addr[0];
  13458. /*
  13459. * Configure the FCF record for FCFI 0. This is the driver's
  13460. * hardcoded default and gets used in nonFIP mode.
  13461. */
  13462. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13463. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13464. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13465. /*
  13466. * Copy the fcf_index and the FCF Record Data. The data starts after
  13467. * the FCoE header plus word10. The data copy needs to be endian
  13468. * correct.
  13469. */
  13470. bytep += sizeof(uint32_t);
  13471. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13472. mboxq->vport = phba->pport;
  13473. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13474. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13475. if (rc == MBX_NOT_FINISHED) {
  13476. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13477. "2515 ADD_FCF_RECORD mailbox failed with "
  13478. "status 0x%x\n", rc);
  13479. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13480. rc = -EIO;
  13481. } else
  13482. rc = 0;
  13483. return rc;
  13484. }
  13485. /**
  13486. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13487. * @phba: pointer to lpfc hba data structure.
  13488. * @fcf_record: pointer to the fcf record to write the default data.
  13489. * @fcf_index: FCF table entry index.
  13490. *
  13491. * This routine is invoked to build the driver's default FCF record. The
  13492. * values used are hardcoded. This routine handles memory initialization.
  13493. *
  13494. **/
  13495. void
  13496. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13497. struct fcf_record *fcf_record,
  13498. uint16_t fcf_index)
  13499. {
  13500. memset(fcf_record, 0, sizeof(struct fcf_record));
  13501. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13502. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13503. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13504. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13505. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13506. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13507. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13508. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13509. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13510. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13511. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13512. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13513. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13514. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13515. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13516. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13517. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13518. /* Set the VLAN bit map */
  13519. if (phba->valid_vlan) {
  13520. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13521. = 1 << (phba->vlan_id % 8);
  13522. }
  13523. }
  13524. /**
  13525. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13526. * @phba: pointer to lpfc hba data structure.
  13527. * @fcf_index: FCF table entry offset.
  13528. *
  13529. * This routine is invoked to scan the entire FCF table by reading FCF
  13530. * record and processing it one at a time starting from the @fcf_index
  13531. * for initial FCF discovery or fast FCF failover rediscovery.
  13532. *
  13533. * Return 0 if the mailbox command is submitted successfully, none 0
  13534. * otherwise.
  13535. **/
  13536. int
  13537. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13538. {
  13539. int rc = 0, error;
  13540. LPFC_MBOXQ_t *mboxq;
  13541. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13542. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13543. if (!mboxq) {
  13544. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13545. "2000 Failed to allocate mbox for "
  13546. "READ_FCF cmd\n");
  13547. error = -ENOMEM;
  13548. goto fail_fcf_scan;
  13549. }
  13550. /* Construct the read FCF record mailbox command */
  13551. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13552. if (rc) {
  13553. error = -EINVAL;
  13554. goto fail_fcf_scan;
  13555. }
  13556. /* Issue the mailbox command asynchronously */
  13557. mboxq->vport = phba->pport;
  13558. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13559. spin_lock_irq(&phba->hbalock);
  13560. phba->hba_flag |= FCF_TS_INPROG;
  13561. spin_unlock_irq(&phba->hbalock);
  13562. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13563. if (rc == MBX_NOT_FINISHED)
  13564. error = -EIO;
  13565. else {
  13566. /* Reset eligible FCF count for new scan */
  13567. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13568. phba->fcf.eligible_fcf_cnt = 0;
  13569. error = 0;
  13570. }
  13571. fail_fcf_scan:
  13572. if (error) {
  13573. if (mboxq)
  13574. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13575. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13576. spin_lock_irq(&phba->hbalock);
  13577. phba->hba_flag &= ~FCF_TS_INPROG;
  13578. spin_unlock_irq(&phba->hbalock);
  13579. }
  13580. return error;
  13581. }
  13582. /**
  13583. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13584. * @phba: pointer to lpfc hba data structure.
  13585. * @fcf_index: FCF table entry offset.
  13586. *
  13587. * This routine is invoked to read an FCF record indicated by @fcf_index
  13588. * and to use it for FLOGI roundrobin FCF failover.
  13589. *
  13590. * Return 0 if the mailbox command is submitted successfully, none 0
  13591. * otherwise.
  13592. **/
  13593. int
  13594. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13595. {
  13596. int rc = 0, error;
  13597. LPFC_MBOXQ_t *mboxq;
  13598. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13599. if (!mboxq) {
  13600. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13601. "2763 Failed to allocate mbox for "
  13602. "READ_FCF cmd\n");
  13603. error = -ENOMEM;
  13604. goto fail_fcf_read;
  13605. }
  13606. /* Construct the read FCF record mailbox command */
  13607. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13608. if (rc) {
  13609. error = -EINVAL;
  13610. goto fail_fcf_read;
  13611. }
  13612. /* Issue the mailbox command asynchronously */
  13613. mboxq->vport = phba->pport;
  13614. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13615. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13616. if (rc == MBX_NOT_FINISHED)
  13617. error = -EIO;
  13618. else
  13619. error = 0;
  13620. fail_fcf_read:
  13621. if (error && mboxq)
  13622. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13623. return error;
  13624. }
  13625. /**
  13626. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13627. * @phba: pointer to lpfc hba data structure.
  13628. * @fcf_index: FCF table entry offset.
  13629. *
  13630. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13631. * determine whether it's eligible for FLOGI roundrobin failover list.
  13632. *
  13633. * Return 0 if the mailbox command is submitted successfully, none 0
  13634. * otherwise.
  13635. **/
  13636. int
  13637. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13638. {
  13639. int rc = 0, error;
  13640. LPFC_MBOXQ_t *mboxq;
  13641. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13642. if (!mboxq) {
  13643. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13644. "2758 Failed to allocate mbox for "
  13645. "READ_FCF cmd\n");
  13646. error = -ENOMEM;
  13647. goto fail_fcf_read;
  13648. }
  13649. /* Construct the read FCF record mailbox command */
  13650. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13651. if (rc) {
  13652. error = -EINVAL;
  13653. goto fail_fcf_read;
  13654. }
  13655. /* Issue the mailbox command asynchronously */
  13656. mboxq->vport = phba->pport;
  13657. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13658. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13659. if (rc == MBX_NOT_FINISHED)
  13660. error = -EIO;
  13661. else
  13662. error = 0;
  13663. fail_fcf_read:
  13664. if (error && mboxq)
  13665. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13666. return error;
  13667. }
  13668. /**
  13669. * lpfc_check_next_fcf_pri
  13670. * phba pointer to the lpfc_hba struct for this port.
  13671. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13672. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13673. * rr_bmask based on their priority level. Starting from the highest priority
  13674. * to the lowest. The most likely FCF candidate will be in the highest
  13675. * priority group. When this routine is called it searches the fcf_pri list for
  13676. * next lowest priority group and repopulates the rr_bmask with only those
  13677. * fcf_indexes.
  13678. * returns:
  13679. * 1=success 0=failure
  13680. **/
  13681. int
  13682. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13683. {
  13684. uint16_t next_fcf_pri;
  13685. uint16_t last_index;
  13686. struct lpfc_fcf_pri *fcf_pri;
  13687. int rc;
  13688. int ret = 0;
  13689. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13690. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13691. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13692. "3060 Last IDX %d\n", last_index);
  13693. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13694. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13695. "3061 Last IDX %d\n", last_index);
  13696. return 0; /* Empty rr list */
  13697. }
  13698. next_fcf_pri = 0;
  13699. /*
  13700. * Clear the rr_bmask and set all of the bits that are at this
  13701. * priority.
  13702. */
  13703. memset(phba->fcf.fcf_rr_bmask, 0,
  13704. sizeof(*phba->fcf.fcf_rr_bmask));
  13705. spin_lock_irq(&phba->hbalock);
  13706. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13707. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13708. continue;
  13709. /*
  13710. * the 1st priority that has not FLOGI failed
  13711. * will be the highest.
  13712. */
  13713. if (!next_fcf_pri)
  13714. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13715. spin_unlock_irq(&phba->hbalock);
  13716. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13717. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13718. fcf_pri->fcf_rec.fcf_index);
  13719. if (rc)
  13720. return 0;
  13721. }
  13722. spin_lock_irq(&phba->hbalock);
  13723. }
  13724. /*
  13725. * if next_fcf_pri was not set above and the list is not empty then
  13726. * we have failed flogis on all of them. So reset flogi failed
  13727. * and start at the begining.
  13728. */
  13729. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13730. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13731. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13732. /*
  13733. * the 1st priority that has not FLOGI failed
  13734. * will be the highest.
  13735. */
  13736. if (!next_fcf_pri)
  13737. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13738. spin_unlock_irq(&phba->hbalock);
  13739. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13740. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13741. fcf_pri->fcf_rec.fcf_index);
  13742. if (rc)
  13743. return 0;
  13744. }
  13745. spin_lock_irq(&phba->hbalock);
  13746. }
  13747. } else
  13748. ret = 1;
  13749. spin_unlock_irq(&phba->hbalock);
  13750. return ret;
  13751. }
  13752. /**
  13753. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13754. * @phba: pointer to lpfc hba data structure.
  13755. *
  13756. * This routine is to get the next eligible FCF record index in a round
  13757. * robin fashion. If the next eligible FCF record index equals to the
  13758. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13759. * shall be returned, otherwise, the next eligible FCF record's index
  13760. * shall be returned.
  13761. **/
  13762. uint16_t
  13763. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13764. {
  13765. uint16_t next_fcf_index;
  13766. /* Search start from next bit of currently registered FCF index */
  13767. next_priority:
  13768. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13769. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13770. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13771. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13772. next_fcf_index);
  13773. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13774. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13775. /*
  13776. * If we have wrapped then we need to clear the bits that
  13777. * have been tested so that we can detect when we should
  13778. * change the priority level.
  13779. */
  13780. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13781. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  13782. }
  13783. /* Check roundrobin failover list empty condition */
  13784. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  13785. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  13786. /*
  13787. * If next fcf index is not found check if there are lower
  13788. * Priority level fcf's in the fcf_priority list.
  13789. * Set up the rr_bmask with all of the avaiable fcf bits
  13790. * at that level and continue the selection process.
  13791. */
  13792. if (lpfc_check_next_fcf_pri_level(phba))
  13793. goto next_priority;
  13794. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13795. "2844 No roundrobin failover FCF available\n");
  13796. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  13797. return LPFC_FCOE_FCF_NEXT_NONE;
  13798. else {
  13799. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13800. "3063 Only FCF available idx %d, flag %x\n",
  13801. next_fcf_index,
  13802. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  13803. return next_fcf_index;
  13804. }
  13805. }
  13806. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  13807. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  13808. LPFC_FCF_FLOGI_FAILED)
  13809. goto next_priority;
  13810. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13811. "2845 Get next roundrobin failover FCF (x%x)\n",
  13812. next_fcf_index);
  13813. return next_fcf_index;
  13814. }
  13815. /**
  13816. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  13817. * @phba: pointer to lpfc hba data structure.
  13818. *
  13819. * This routine sets the FCF record index in to the eligible bmask for
  13820. * roundrobin failover search. It checks to make sure that the index
  13821. * does not go beyond the range of the driver allocated bmask dimension
  13822. * before setting the bit.
  13823. *
  13824. * Returns 0 if the index bit successfully set, otherwise, it returns
  13825. * -EINVAL.
  13826. **/
  13827. int
  13828. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  13829. {
  13830. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13831. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13832. "2610 FCF (x%x) reached driver's book "
  13833. "keeping dimension:x%x\n",
  13834. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13835. return -EINVAL;
  13836. }
  13837. /* Set the eligible FCF record index bmask */
  13838. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13839. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13840. "2790 Set FCF (x%x) to roundrobin FCF failover "
  13841. "bmask\n", fcf_index);
  13842. return 0;
  13843. }
  13844. /**
  13845. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  13846. * @phba: pointer to lpfc hba data structure.
  13847. *
  13848. * This routine clears the FCF record index from the eligible bmask for
  13849. * roundrobin failover search. It checks to make sure that the index
  13850. * does not go beyond the range of the driver allocated bmask dimension
  13851. * before clearing the bit.
  13852. **/
  13853. void
  13854. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  13855. {
  13856. struct lpfc_fcf_pri *fcf_pri;
  13857. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13858. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13859. "2762 FCF (x%x) reached driver's book "
  13860. "keeping dimension:x%x\n",
  13861. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13862. return;
  13863. }
  13864. /* Clear the eligible FCF record index bmask */
  13865. spin_lock_irq(&phba->hbalock);
  13866. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13867. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  13868. list_del_init(&fcf_pri->list);
  13869. break;
  13870. }
  13871. }
  13872. spin_unlock_irq(&phba->hbalock);
  13873. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13874. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13875. "2791 Clear FCF (x%x) from roundrobin failover "
  13876. "bmask\n", fcf_index);
  13877. }
  13878. /**
  13879. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  13880. * @phba: pointer to lpfc hba data structure.
  13881. *
  13882. * This routine is the completion routine for the rediscover FCF table mailbox
  13883. * command. If the mailbox command returned failure, it will try to stop the
  13884. * FCF rediscover wait timer.
  13885. **/
  13886. void
  13887. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  13888. {
  13889. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  13890. uint32_t shdr_status, shdr_add_status;
  13891. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  13892. shdr_status = bf_get(lpfc_mbox_hdr_status,
  13893. &redisc_fcf->header.cfg_shdr.response);
  13894. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  13895. &redisc_fcf->header.cfg_shdr.response);
  13896. if (shdr_status || shdr_add_status) {
  13897. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13898. "2746 Requesting for FCF rediscovery failed "
  13899. "status x%x add_status x%x\n",
  13900. shdr_status, shdr_add_status);
  13901. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  13902. spin_lock_irq(&phba->hbalock);
  13903. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  13904. spin_unlock_irq(&phba->hbalock);
  13905. /*
  13906. * CVL event triggered FCF rediscover request failed,
  13907. * last resort to re-try current registered FCF entry.
  13908. */
  13909. lpfc_retry_pport_discovery(phba);
  13910. } else {
  13911. spin_lock_irq(&phba->hbalock);
  13912. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  13913. spin_unlock_irq(&phba->hbalock);
  13914. /*
  13915. * DEAD FCF event triggered FCF rediscover request
  13916. * failed, last resort to fail over as a link down
  13917. * to FCF registration.
  13918. */
  13919. lpfc_sli4_fcf_dead_failthrough(phba);
  13920. }
  13921. } else {
  13922. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13923. "2775 Start FCF rediscover quiescent timer\n");
  13924. /*
  13925. * Start FCF rediscovery wait timer for pending FCF
  13926. * before rescan FCF record table.
  13927. */
  13928. lpfc_fcf_redisc_wait_start_timer(phba);
  13929. }
  13930. mempool_free(mbox, phba->mbox_mem_pool);
  13931. }
  13932. /**
  13933. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  13934. * @phba: pointer to lpfc hba data structure.
  13935. *
  13936. * This routine is invoked to request for rediscovery of the entire FCF table
  13937. * by the port.
  13938. **/
  13939. int
  13940. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  13941. {
  13942. LPFC_MBOXQ_t *mbox;
  13943. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  13944. int rc, length;
  13945. /* Cancel retry delay timers to all vports before FCF rediscover */
  13946. lpfc_cancel_all_vport_retry_delay_timer(phba);
  13947. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13948. if (!mbox) {
  13949. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13950. "2745 Failed to allocate mbox for "
  13951. "requesting FCF rediscover.\n");
  13952. return -ENOMEM;
  13953. }
  13954. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  13955. sizeof(struct lpfc_sli4_cfg_mhdr));
  13956. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  13957. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  13958. length, LPFC_SLI4_MBX_EMBED);
  13959. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  13960. /* Set count to 0 for invalidating the entire FCF database */
  13961. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  13962. /* Issue the mailbox command asynchronously */
  13963. mbox->vport = phba->pport;
  13964. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  13965. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  13966. if (rc == MBX_NOT_FINISHED) {
  13967. mempool_free(mbox, phba->mbox_mem_pool);
  13968. return -EIO;
  13969. }
  13970. return 0;
  13971. }
  13972. /**
  13973. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  13974. * @phba: pointer to lpfc hba data structure.
  13975. *
  13976. * This function is the failover routine as a last resort to the FCF DEAD
  13977. * event when driver failed to perform fast FCF failover.
  13978. **/
  13979. void
  13980. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  13981. {
  13982. uint32_t link_state;
  13983. /*
  13984. * Last resort as FCF DEAD event failover will treat this as
  13985. * a link down, but save the link state because we don't want
  13986. * it to be changed to Link Down unless it is already down.
  13987. */
  13988. link_state = phba->link_state;
  13989. lpfc_linkdown(phba);
  13990. phba->link_state = link_state;
  13991. /* Unregister FCF if no devices connected to it */
  13992. lpfc_unregister_unused_fcf(phba);
  13993. }
  13994. /**
  13995. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  13996. * @phba: pointer to lpfc hba data structure.
  13997. *
  13998. * This function read region 23 and parse TLV for port status to
  13999. * decide if the user disaled the port. If the TLV indicates the
  14000. * port is disabled, the hba_flag is set accordingly.
  14001. **/
  14002. void
  14003. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14004. {
  14005. LPFC_MBOXQ_t *pmb = NULL;
  14006. MAILBOX_t *mb;
  14007. uint8_t *rgn23_data = NULL;
  14008. uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
  14009. int rc;
  14010. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14011. if (!pmb) {
  14012. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14013. "2600 lpfc_sli_read_serdes_param failed to"
  14014. " allocate mailbox memory\n");
  14015. goto out;
  14016. }
  14017. mb = &pmb->u.mb;
  14018. /* Get adapter Region 23 data */
  14019. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14020. if (!rgn23_data)
  14021. goto out;
  14022. do {
  14023. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14024. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14025. if (rc != MBX_SUCCESS) {
  14026. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14027. "2601 lpfc_sli_read_link_ste failed to"
  14028. " read config region 23 rc 0x%x Status 0x%x\n",
  14029. rc, mb->mbxStatus);
  14030. mb->un.varDmp.word_cnt = 0;
  14031. }
  14032. /*
  14033. * dump mem may return a zero when finished or we got a
  14034. * mailbox error, either way we are done.
  14035. */
  14036. if (mb->un.varDmp.word_cnt == 0)
  14037. break;
  14038. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14039. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14040. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14041. rgn23_data + offset,
  14042. mb->un.varDmp.word_cnt);
  14043. offset += mb->un.varDmp.word_cnt;
  14044. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14045. data_size = offset;
  14046. offset = 0;
  14047. if (!data_size)
  14048. goto out;
  14049. /* Check the region signature first */
  14050. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14051. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14052. "2619 Config region 23 has bad signature\n");
  14053. goto out;
  14054. }
  14055. offset += 4;
  14056. /* Check the data structure version */
  14057. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14058. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14059. "2620 Config region 23 has bad version\n");
  14060. goto out;
  14061. }
  14062. offset += 4;
  14063. /* Parse TLV entries in the region */
  14064. while (offset < data_size) {
  14065. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14066. break;
  14067. /*
  14068. * If the TLV is not driver specific TLV or driver id is
  14069. * not linux driver id, skip the record.
  14070. */
  14071. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14072. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14073. (rgn23_data[offset + 3] != 0)) {
  14074. offset += rgn23_data[offset + 1] * 4 + 4;
  14075. continue;
  14076. }
  14077. /* Driver found a driver specific TLV in the config region */
  14078. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14079. offset += 4;
  14080. tlv_offset = 0;
  14081. /*
  14082. * Search for configured port state sub-TLV.
  14083. */
  14084. while ((offset < data_size) &&
  14085. (tlv_offset < sub_tlv_len)) {
  14086. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14087. offset += 4;
  14088. tlv_offset += 4;
  14089. break;
  14090. }
  14091. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14092. offset += rgn23_data[offset + 1] * 4 + 4;
  14093. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14094. continue;
  14095. }
  14096. /* This HBA contains PORT_STE configured */
  14097. if (!rgn23_data[offset + 2])
  14098. phba->hba_flag |= LINK_DISABLED;
  14099. goto out;
  14100. }
  14101. }
  14102. out:
  14103. if (pmb)
  14104. mempool_free(pmb, phba->mbox_mem_pool);
  14105. kfree(rgn23_data);
  14106. return;
  14107. }
  14108. /**
  14109. * lpfc_wr_object - write an object to the firmware
  14110. * @phba: HBA structure that indicates port to create a queue on.
  14111. * @dmabuf_list: list of dmabufs to write to the port.
  14112. * @size: the total byte value of the objects to write to the port.
  14113. * @offset: the current offset to be used to start the transfer.
  14114. *
  14115. * This routine will create a wr_object mailbox command to send to the port.
  14116. * the mailbox command will be constructed using the dma buffers described in
  14117. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14118. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14119. * used to indicate the starting offset of the transfer and will also return
  14120. * the offset after the write object mailbox has completed. @size is used to
  14121. * determine the end of the object and whether the eof bit should be set.
  14122. *
  14123. * Return 0 is successful and offset will contain the the new offset to use
  14124. * for the next write.
  14125. * Return negative value for error cases.
  14126. **/
  14127. int
  14128. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14129. uint32_t size, uint32_t *offset)
  14130. {
  14131. struct lpfc_mbx_wr_object *wr_object;
  14132. LPFC_MBOXQ_t *mbox;
  14133. int rc = 0, i = 0;
  14134. uint32_t shdr_status, shdr_add_status;
  14135. uint32_t mbox_tmo;
  14136. union lpfc_sli4_cfg_shdr *shdr;
  14137. struct lpfc_dmabuf *dmabuf;
  14138. uint32_t written = 0;
  14139. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14140. if (!mbox)
  14141. return -ENOMEM;
  14142. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14143. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14144. sizeof(struct lpfc_mbx_wr_object) -
  14145. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14146. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14147. wr_object->u.request.write_offset = *offset;
  14148. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14149. wr_object->u.request.object_name[0] =
  14150. cpu_to_le32(wr_object->u.request.object_name[0]);
  14151. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14152. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14153. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14154. break;
  14155. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14156. wr_object->u.request.bde[i].addrHigh =
  14157. putPaddrHigh(dmabuf->phys);
  14158. if (written + SLI4_PAGE_SIZE >= size) {
  14159. wr_object->u.request.bde[i].tus.f.bdeSize =
  14160. (size - written);
  14161. written += (size - written);
  14162. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14163. } else {
  14164. wr_object->u.request.bde[i].tus.f.bdeSize =
  14165. SLI4_PAGE_SIZE;
  14166. written += SLI4_PAGE_SIZE;
  14167. }
  14168. i++;
  14169. }
  14170. wr_object->u.request.bde_count = i;
  14171. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14172. if (!phba->sli4_hba.intr_enable)
  14173. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14174. else {
  14175. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14176. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14177. }
  14178. /* The IOCTL status is embedded in the mailbox subheader. */
  14179. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14180. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14181. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14182. if (rc != MBX_TIMEOUT)
  14183. mempool_free(mbox, phba->mbox_mem_pool);
  14184. if (shdr_status || shdr_add_status || rc) {
  14185. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14186. "3025 Write Object mailbox failed with "
  14187. "status x%x add_status x%x, mbx status x%x\n",
  14188. shdr_status, shdr_add_status, rc);
  14189. rc = -ENXIO;
  14190. } else
  14191. *offset += wr_object->u.response.actual_write_length;
  14192. return rc;
  14193. }
  14194. /**
  14195. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14196. * @vport: pointer to vport data structure.
  14197. *
  14198. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14199. * and REG_VPI mailbox commands associated with the vport. This function
  14200. * is called when driver want to restart discovery of the vport due to
  14201. * a Clear Virtual Link event.
  14202. **/
  14203. void
  14204. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14205. {
  14206. struct lpfc_hba *phba = vport->phba;
  14207. LPFC_MBOXQ_t *mb, *nextmb;
  14208. struct lpfc_dmabuf *mp;
  14209. struct lpfc_nodelist *ndlp;
  14210. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14211. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14212. LIST_HEAD(mbox_cmd_list);
  14213. uint8_t restart_loop;
  14214. /* Clean up internally queued mailbox commands with the vport */
  14215. spin_lock_irq(&phba->hbalock);
  14216. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14217. if (mb->vport != vport)
  14218. continue;
  14219. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14220. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14221. continue;
  14222. list_del(&mb->list);
  14223. list_add_tail(&mb->list, &mbox_cmd_list);
  14224. }
  14225. /* Clean up active mailbox command with the vport */
  14226. mb = phba->sli.mbox_active;
  14227. if (mb && (mb->vport == vport)) {
  14228. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14229. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14230. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14231. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14232. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14233. /* Put reference count for delayed processing */
  14234. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14235. /* Unregister the RPI when mailbox complete */
  14236. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14237. }
  14238. }
  14239. /* Cleanup any mailbox completions which are not yet processed */
  14240. do {
  14241. restart_loop = 0;
  14242. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14243. /*
  14244. * If this mailox is already processed or it is
  14245. * for another vport ignore it.
  14246. */
  14247. if ((mb->vport != vport) ||
  14248. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14249. continue;
  14250. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14251. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14252. continue;
  14253. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14254. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14255. ndlp = (struct lpfc_nodelist *)mb->context2;
  14256. /* Unregister the RPI when mailbox complete */
  14257. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14258. restart_loop = 1;
  14259. spin_unlock_irq(&phba->hbalock);
  14260. spin_lock(shost->host_lock);
  14261. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14262. spin_unlock(shost->host_lock);
  14263. spin_lock_irq(&phba->hbalock);
  14264. break;
  14265. }
  14266. }
  14267. } while (restart_loop);
  14268. spin_unlock_irq(&phba->hbalock);
  14269. /* Release the cleaned-up mailbox commands */
  14270. while (!list_empty(&mbox_cmd_list)) {
  14271. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14272. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14273. mp = (struct lpfc_dmabuf *) (mb->context1);
  14274. if (mp) {
  14275. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14276. kfree(mp);
  14277. }
  14278. ndlp = (struct lpfc_nodelist *) mb->context2;
  14279. mb->context2 = NULL;
  14280. if (ndlp) {
  14281. spin_lock(shost->host_lock);
  14282. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14283. spin_unlock(shost->host_lock);
  14284. lpfc_nlp_put(ndlp);
  14285. }
  14286. }
  14287. mempool_free(mb, phba->mbox_mem_pool);
  14288. }
  14289. /* Release the ndlp with the cleaned-up active mailbox command */
  14290. if (act_mbx_ndlp) {
  14291. spin_lock(shost->host_lock);
  14292. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14293. spin_unlock(shost->host_lock);
  14294. lpfc_nlp_put(act_mbx_ndlp);
  14295. }
  14296. }
  14297. /**
  14298. * lpfc_drain_txq - Drain the txq
  14299. * @phba: Pointer to HBA context object.
  14300. *
  14301. * This function attempt to submit IOCBs on the txq
  14302. * to the adapter. For SLI4 adapters, the txq contains
  14303. * ELS IOCBs that have been deferred because the there
  14304. * are no SGLs. This congestion can occur with large
  14305. * vport counts during node discovery.
  14306. **/
  14307. uint32_t
  14308. lpfc_drain_txq(struct lpfc_hba *phba)
  14309. {
  14310. LIST_HEAD(completions);
  14311. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14312. struct lpfc_iocbq *piocbq = 0;
  14313. unsigned long iflags = 0;
  14314. char *fail_msg = NULL;
  14315. struct lpfc_sglq *sglq;
  14316. union lpfc_wqe wqe;
  14317. spin_lock_irqsave(&phba->hbalock, iflags);
  14318. if (pring->txq_cnt > pring->txq_max)
  14319. pring->txq_max = pring->txq_cnt;
  14320. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14321. while (pring->txq_cnt) {
  14322. spin_lock_irqsave(&phba->hbalock, iflags);
  14323. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14324. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14325. if (!sglq) {
  14326. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14327. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14328. break;
  14329. } else {
  14330. if (!piocbq) {
  14331. /* The txq_cnt out of sync. This should
  14332. * never happen
  14333. */
  14334. sglq = __lpfc_clear_active_sglq(phba,
  14335. sglq->sli4_lxritag);
  14336. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14337. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14338. "2823 txq empty and txq_cnt is %d\n ",
  14339. pring->txq_cnt);
  14340. break;
  14341. }
  14342. }
  14343. /* The xri and iocb resources secured,
  14344. * attempt to issue request
  14345. */
  14346. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14347. piocbq->sli4_xritag = sglq->sli4_xritag;
  14348. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14349. fail_msg = "to convert bpl to sgl";
  14350. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14351. fail_msg = "to convert iocb to wqe";
  14352. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14353. fail_msg = " - Wq is full";
  14354. else
  14355. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14356. if (fail_msg) {
  14357. /* Failed means we can't issue and need to cancel */
  14358. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14359. "2822 IOCB failed %s iotag 0x%x "
  14360. "xri 0x%x\n",
  14361. fail_msg,
  14362. piocbq->iotag, piocbq->sli4_xritag);
  14363. list_add_tail(&piocbq->list, &completions);
  14364. }
  14365. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14366. }
  14367. /* Cancel all the IOCBs that cannot be issued */
  14368. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14369. IOERR_SLI_ABORTED);
  14370. return pring->txq_cnt;
  14371. }