lpfc_sli.c 485 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2012 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/blkdev.h>
  22. #include <linux/pci.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <scsi/scsi.h>
  27. #include <scsi/scsi_cmnd.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_host.h>
  30. #include <scsi/scsi_transport_fc.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <linux/aer.h>
  33. #include "lpfc_hw4.h"
  34. #include "lpfc_hw.h"
  35. #include "lpfc_sli.h"
  36. #include "lpfc_sli4.h"
  37. #include "lpfc_nl.h"
  38. #include "lpfc_disc.h"
  39. #include "lpfc_scsi.h"
  40. #include "lpfc.h"
  41. #include "lpfc_crtn.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_compat.h"
  44. #include "lpfc_debugfs.h"
  45. #include "lpfc_vport.h"
  46. /* There are only four IOCB completion types. */
  47. typedef enum _lpfc_iocb_type {
  48. LPFC_UNKNOWN_IOCB,
  49. LPFC_UNSOL_IOCB,
  50. LPFC_SOL_IOCB,
  51. LPFC_ABORT_IOCB
  52. } lpfc_iocb_type;
  53. /* Provide function prototypes local to this module. */
  54. static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
  55. uint32_t);
  56. static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
  57. uint8_t *, uint32_t *);
  58. static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
  59. struct lpfc_iocbq *);
  60. static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
  61. struct hbq_dmabuf *);
  62. static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
  63. struct lpfc_cqe *);
  64. static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
  65. int);
  66. static IOCB_t *
  67. lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
  68. {
  69. return &iocbq->iocb;
  70. }
  71. /**
  72. * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
  73. * @q: The Work Queue to operate on.
  74. * @wqe: The work Queue Entry to put on the Work queue.
  75. *
  76. * This routine will copy the contents of @wqe to the next available entry on
  77. * the @q. This function will then ring the Work Queue Doorbell to signal the
  78. * HBA to start processing the Work Queue Entry. This function returns 0 if
  79. * successful. If no entries are available on @q then this function will return
  80. * -ENOMEM.
  81. * The caller is expected to hold the hbalock when calling this routine.
  82. **/
  83. static uint32_t
  84. lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
  85. {
  86. union lpfc_wqe *temp_wqe;
  87. struct lpfc_register doorbell;
  88. uint32_t host_index;
  89. /* sanity check on queue memory */
  90. if (unlikely(!q))
  91. return -ENOMEM;
  92. temp_wqe = q->qe[q->host_index].wqe;
  93. /* If the host has not yet processed the next entry then we are done */
  94. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  95. return -ENOMEM;
  96. /* set consumption flag every once in a while */
  97. if (!((q->host_index + 1) % q->entry_repost))
  98. bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
  99. if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
  100. bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
  101. lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
  102. /* Update the host index before invoking device */
  103. host_index = q->host_index;
  104. q->host_index = ((q->host_index + 1) % q->entry_count);
  105. /* Ring Doorbell */
  106. doorbell.word0 = 0;
  107. bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
  108. bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
  109. bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
  110. writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
  111. readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
  112. return 0;
  113. }
  114. /**
  115. * lpfc_sli4_wq_release - Updates internal hba index for WQ
  116. * @q: The Work Queue to operate on.
  117. * @index: The index to advance the hba index to.
  118. *
  119. * This routine will update the HBA index of a queue to reflect consumption of
  120. * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
  121. * an entry the host calls this function to update the queue's internal
  122. * pointers. This routine returns the number of entries that were consumed by
  123. * the HBA.
  124. **/
  125. static uint32_t
  126. lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
  127. {
  128. uint32_t released = 0;
  129. /* sanity check on queue memory */
  130. if (unlikely(!q))
  131. return 0;
  132. if (q->hba_index == index)
  133. return 0;
  134. do {
  135. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  136. released++;
  137. } while (q->hba_index != index);
  138. return released;
  139. }
  140. /**
  141. * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
  142. * @q: The Mailbox Queue to operate on.
  143. * @wqe: The Mailbox Queue Entry to put on the Work queue.
  144. *
  145. * This routine will copy the contents of @mqe to the next available entry on
  146. * the @q. This function will then ring the Work Queue Doorbell to signal the
  147. * HBA to start processing the Work Queue Entry. This function returns 0 if
  148. * successful. If no entries are available on @q then this function will return
  149. * -ENOMEM.
  150. * The caller is expected to hold the hbalock when calling this routine.
  151. **/
  152. static uint32_t
  153. lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
  154. {
  155. struct lpfc_mqe *temp_mqe;
  156. struct lpfc_register doorbell;
  157. uint32_t host_index;
  158. /* sanity check on queue memory */
  159. if (unlikely(!q))
  160. return -ENOMEM;
  161. temp_mqe = q->qe[q->host_index].mqe;
  162. /* If the host has not yet processed the next entry then we are done */
  163. if (((q->host_index + 1) % q->entry_count) == q->hba_index)
  164. return -ENOMEM;
  165. lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
  166. /* Save off the mailbox pointer for completion */
  167. q->phba->mbox = (MAILBOX_t *)temp_mqe;
  168. /* Update the host index before invoking device */
  169. host_index = q->host_index;
  170. q->host_index = ((q->host_index + 1) % q->entry_count);
  171. /* Ring Doorbell */
  172. doorbell.word0 = 0;
  173. bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
  174. bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
  175. writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
  176. readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
  177. return 0;
  178. }
  179. /**
  180. * lpfc_sli4_mq_release - Updates internal hba index for MQ
  181. * @q: The Mailbox Queue to operate on.
  182. *
  183. * This routine will update the HBA index of a queue to reflect consumption of
  184. * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
  185. * an entry the host calls this function to update the queue's internal
  186. * pointers. This routine returns the number of entries that were consumed by
  187. * the HBA.
  188. **/
  189. static uint32_t
  190. lpfc_sli4_mq_release(struct lpfc_queue *q)
  191. {
  192. /* sanity check on queue memory */
  193. if (unlikely(!q))
  194. return 0;
  195. /* Clear the mailbox pointer for completion */
  196. q->phba->mbox = NULL;
  197. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  198. return 1;
  199. }
  200. /**
  201. * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
  202. * @q: The Event Queue to get the first valid EQE from
  203. *
  204. * This routine will get the first valid Event Queue Entry from @q, update
  205. * the queue's internal hba index, and return the EQE. If no valid EQEs are in
  206. * the Queue (no more work to do), or the Queue is full of EQEs that have been
  207. * processed, but not popped back to the HBA then this routine will return NULL.
  208. **/
  209. static struct lpfc_eqe *
  210. lpfc_sli4_eq_get(struct lpfc_queue *q)
  211. {
  212. struct lpfc_eqe *eqe;
  213. /* sanity check on queue memory */
  214. if (unlikely(!q))
  215. return NULL;
  216. eqe = q->qe[q->hba_index].eqe;
  217. /* If the next EQE is not valid then we are done */
  218. if (!bf_get_le32(lpfc_eqe_valid, eqe))
  219. return NULL;
  220. /* If the host has not yet processed the next entry then we are done */
  221. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  222. return NULL;
  223. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  224. return eqe;
  225. }
  226. /**
  227. * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
  228. * @q: The Event Queue that the host has completed processing for.
  229. * @arm: Indicates whether the host wants to arms this CQ.
  230. *
  231. * This routine will mark all Event Queue Entries on @q, from the last
  232. * known completed entry to the last entry that was processed, as completed
  233. * by clearing the valid bit for each completion queue entry. Then it will
  234. * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
  235. * The internal host index in the @q will be updated by this routine to indicate
  236. * that the host has finished processing the entries. The @arm parameter
  237. * indicates that the queue should be rearmed when ringing the doorbell.
  238. *
  239. * This function will return the number of EQEs that were popped.
  240. **/
  241. uint32_t
  242. lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
  243. {
  244. uint32_t released = 0;
  245. struct lpfc_eqe *temp_eqe;
  246. struct lpfc_register doorbell;
  247. /* sanity check on queue memory */
  248. if (unlikely(!q))
  249. return 0;
  250. /* while there are valid entries */
  251. while (q->hba_index != q->host_index) {
  252. temp_eqe = q->qe[q->host_index].eqe;
  253. bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
  254. released++;
  255. q->host_index = ((q->host_index + 1) % q->entry_count);
  256. }
  257. if (unlikely(released == 0 && !arm))
  258. return 0;
  259. /* ring doorbell for number popped */
  260. doorbell.word0 = 0;
  261. if (arm) {
  262. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  263. bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
  264. }
  265. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  266. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
  267. bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
  268. (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
  269. bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
  270. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  271. /* PCI read to flush PCI pipeline on re-arming for INTx mode */
  272. if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
  273. readl(q->phba->sli4_hba.EQCQDBregaddr);
  274. return released;
  275. }
  276. /**
  277. * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
  278. * @q: The Completion Queue to get the first valid CQE from
  279. *
  280. * This routine will get the first valid Completion Queue Entry from @q, update
  281. * the queue's internal hba index, and return the CQE. If no valid CQEs are in
  282. * the Queue (no more work to do), or the Queue is full of CQEs that have been
  283. * processed, but not popped back to the HBA then this routine will return NULL.
  284. **/
  285. static struct lpfc_cqe *
  286. lpfc_sli4_cq_get(struct lpfc_queue *q)
  287. {
  288. struct lpfc_cqe *cqe;
  289. /* sanity check on queue memory */
  290. if (unlikely(!q))
  291. return NULL;
  292. /* If the next CQE is not valid then we are done */
  293. if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
  294. return NULL;
  295. /* If the host has not yet processed the next entry then we are done */
  296. if (((q->hba_index + 1) % q->entry_count) == q->host_index)
  297. return NULL;
  298. cqe = q->qe[q->hba_index].cqe;
  299. q->hba_index = ((q->hba_index + 1) % q->entry_count);
  300. return cqe;
  301. }
  302. /**
  303. * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
  304. * @q: The Completion Queue that the host has completed processing for.
  305. * @arm: Indicates whether the host wants to arms this CQ.
  306. *
  307. * This routine will mark all Completion queue entries on @q, from the last
  308. * known completed entry to the last entry that was processed, as completed
  309. * by clearing the valid bit for each completion queue entry. Then it will
  310. * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
  311. * The internal host index in the @q will be updated by this routine to indicate
  312. * that the host has finished processing the entries. The @arm parameter
  313. * indicates that the queue should be rearmed when ringing the doorbell.
  314. *
  315. * This function will return the number of CQEs that were released.
  316. **/
  317. uint32_t
  318. lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
  319. {
  320. uint32_t released = 0;
  321. struct lpfc_cqe *temp_qe;
  322. struct lpfc_register doorbell;
  323. /* sanity check on queue memory */
  324. if (unlikely(!q))
  325. return 0;
  326. /* while there are valid entries */
  327. while (q->hba_index != q->host_index) {
  328. temp_qe = q->qe[q->host_index].cqe;
  329. bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
  330. released++;
  331. q->host_index = ((q->host_index + 1) % q->entry_count);
  332. }
  333. if (unlikely(released == 0 && !arm))
  334. return 0;
  335. /* ring doorbell for number popped */
  336. doorbell.word0 = 0;
  337. if (arm)
  338. bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
  339. bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
  340. bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
  341. bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
  342. (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
  343. bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
  344. writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
  345. return released;
  346. }
  347. /**
  348. * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
  349. * @q: The Header Receive Queue to operate on.
  350. * @wqe: The Receive Queue Entry to put on the Receive queue.
  351. *
  352. * This routine will copy the contents of @wqe to the next available entry on
  353. * the @q. This function will then ring the Receive Queue Doorbell to signal the
  354. * HBA to start processing the Receive Queue Entry. This function returns the
  355. * index that the rqe was copied to if successful. If no entries are available
  356. * on @q then this function will return -ENOMEM.
  357. * The caller is expected to hold the hbalock when calling this routine.
  358. **/
  359. static int
  360. lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
  361. struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
  362. {
  363. struct lpfc_rqe *temp_hrqe;
  364. struct lpfc_rqe *temp_drqe;
  365. struct lpfc_register doorbell;
  366. int put_index = hq->host_index;
  367. /* sanity check on queue memory */
  368. if (unlikely(!hq) || unlikely(!dq))
  369. return -ENOMEM;
  370. temp_hrqe = hq->qe[hq->host_index].rqe;
  371. temp_drqe = dq->qe[dq->host_index].rqe;
  372. if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
  373. return -EINVAL;
  374. if (hq->host_index != dq->host_index)
  375. return -EINVAL;
  376. /* If the host has not yet processed the next entry then we are done */
  377. if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
  378. return -EBUSY;
  379. lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
  380. lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
  381. /* Update the host index to point to the next slot */
  382. hq->host_index = ((hq->host_index + 1) % hq->entry_count);
  383. dq->host_index = ((dq->host_index + 1) % dq->entry_count);
  384. /* Ring The Header Receive Queue Doorbell */
  385. if (!(hq->host_index % hq->entry_repost)) {
  386. doorbell.word0 = 0;
  387. bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
  388. hq->entry_repost);
  389. bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
  390. writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
  391. }
  392. return put_index;
  393. }
  394. /**
  395. * lpfc_sli4_rq_release - Updates internal hba index for RQ
  396. * @q: The Header Receive Queue to operate on.
  397. *
  398. * This routine will update the HBA index of a queue to reflect consumption of
  399. * one Receive Queue Entry by the HBA. When the HBA indicates that it has
  400. * consumed an entry the host calls this function to update the queue's
  401. * internal pointers. This routine returns the number of entries that were
  402. * consumed by the HBA.
  403. **/
  404. static uint32_t
  405. lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
  406. {
  407. /* sanity check on queue memory */
  408. if (unlikely(!hq) || unlikely(!dq))
  409. return 0;
  410. if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
  411. return 0;
  412. hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
  413. dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
  414. return 1;
  415. }
  416. /**
  417. * lpfc_cmd_iocb - Get next command iocb entry in the ring
  418. * @phba: Pointer to HBA context object.
  419. * @pring: Pointer to driver SLI ring object.
  420. *
  421. * This function returns pointer to next command iocb entry
  422. * in the command ring. The caller must hold hbalock to prevent
  423. * other threads consume the next command iocb.
  424. * SLI-2/SLI-3 provide different sized iocbs.
  425. **/
  426. static inline IOCB_t *
  427. lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  428. {
  429. return (IOCB_t *) (((char *) pring->cmdringaddr) +
  430. pring->cmdidx * phba->iocb_cmd_size);
  431. }
  432. /**
  433. * lpfc_resp_iocb - Get next response iocb entry in the ring
  434. * @phba: Pointer to HBA context object.
  435. * @pring: Pointer to driver SLI ring object.
  436. *
  437. * This function returns pointer to next response iocb entry
  438. * in the response ring. The caller must hold hbalock to make sure
  439. * that no other thread consume the next response iocb.
  440. * SLI-2/SLI-3 provide different sized iocbs.
  441. **/
  442. static inline IOCB_t *
  443. lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  444. {
  445. return (IOCB_t *) (((char *) pring->rspringaddr) +
  446. pring->rspidx * phba->iocb_rsp_size);
  447. }
  448. /**
  449. * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  450. * @phba: Pointer to HBA context object.
  451. *
  452. * This function is called with hbalock held. This function
  453. * allocates a new driver iocb object from the iocb pool. If the
  454. * allocation is successful, it returns pointer to the newly
  455. * allocated iocb object else it returns NULL.
  456. **/
  457. struct lpfc_iocbq *
  458. __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  459. {
  460. struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
  461. struct lpfc_iocbq * iocbq = NULL;
  462. list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
  463. if (iocbq)
  464. phba->iocb_cnt++;
  465. if (phba->iocb_cnt > phba->iocb_max)
  466. phba->iocb_max = phba->iocb_cnt;
  467. return iocbq;
  468. }
  469. /**
  470. * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
  471. * @phba: Pointer to HBA context object.
  472. * @xritag: XRI value.
  473. *
  474. * This function clears the sglq pointer from the array of acive
  475. * sglq's. The xritag that is passed in is used to index into the
  476. * array. Before the xritag can be used it needs to be adjusted
  477. * by subtracting the xribase.
  478. *
  479. * Returns sglq ponter = success, NULL = Failure.
  480. **/
  481. static struct lpfc_sglq *
  482. __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  483. {
  484. struct lpfc_sglq *sglq;
  485. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  486. phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
  487. return sglq;
  488. }
  489. /**
  490. * __lpfc_get_active_sglq - Get the active sglq for this XRI.
  491. * @phba: Pointer to HBA context object.
  492. * @xritag: XRI value.
  493. *
  494. * This function returns the sglq pointer from the array of acive
  495. * sglq's. The xritag that is passed in is used to index into the
  496. * array. Before the xritag can be used it needs to be adjusted
  497. * by subtracting the xribase.
  498. *
  499. * Returns sglq ponter = success, NULL = Failure.
  500. **/
  501. struct lpfc_sglq *
  502. __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
  503. {
  504. struct lpfc_sglq *sglq;
  505. sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
  506. return sglq;
  507. }
  508. /**
  509. * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
  510. * @phba: Pointer to HBA context object.
  511. * @xritag: xri used in this exchange.
  512. * @rrq: The RRQ to be cleared.
  513. *
  514. **/
  515. void
  516. lpfc_clr_rrq_active(struct lpfc_hba *phba,
  517. uint16_t xritag,
  518. struct lpfc_node_rrq *rrq)
  519. {
  520. struct lpfc_nodelist *ndlp = NULL;
  521. if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
  522. ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
  523. /* The target DID could have been swapped (cable swap)
  524. * we should use the ndlp from the findnode if it is
  525. * available.
  526. */
  527. if ((!ndlp) && rrq->ndlp)
  528. ndlp = rrq->ndlp;
  529. if (!ndlp)
  530. goto out;
  531. if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
  532. rrq->send_rrq = 0;
  533. rrq->xritag = 0;
  534. rrq->rrq_stop_time = 0;
  535. }
  536. out:
  537. mempool_free(rrq, phba->rrq_pool);
  538. }
  539. /**
  540. * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
  541. * @phba: Pointer to HBA context object.
  542. *
  543. * This function is called with hbalock held. This function
  544. * Checks if stop_time (ratov from setting rrq active) has
  545. * been reached, if it has and the send_rrq flag is set then
  546. * it will call lpfc_send_rrq. If the send_rrq flag is not set
  547. * then it will just call the routine to clear the rrq and
  548. * free the rrq resource.
  549. * The timer is set to the next rrq that is going to expire before
  550. * leaving the routine.
  551. *
  552. **/
  553. void
  554. lpfc_handle_rrq_active(struct lpfc_hba *phba)
  555. {
  556. struct lpfc_node_rrq *rrq;
  557. struct lpfc_node_rrq *nextrrq;
  558. unsigned long next_time;
  559. unsigned long iflags;
  560. LIST_HEAD(send_rrq);
  561. spin_lock_irqsave(&phba->hbalock, iflags);
  562. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  563. next_time = jiffies + HZ * (phba->fc_ratov + 1);
  564. list_for_each_entry_safe(rrq, nextrrq,
  565. &phba->active_rrq_list, list) {
  566. if (time_after(jiffies, rrq->rrq_stop_time))
  567. list_move(&rrq->list, &send_rrq);
  568. else if (time_before(rrq->rrq_stop_time, next_time))
  569. next_time = rrq->rrq_stop_time;
  570. }
  571. spin_unlock_irqrestore(&phba->hbalock, iflags);
  572. if (!list_empty(&phba->active_rrq_list))
  573. mod_timer(&phba->rrq_tmr, next_time);
  574. list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
  575. list_del(&rrq->list);
  576. if (!rrq->send_rrq)
  577. /* this call will free the rrq */
  578. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  579. else if (lpfc_send_rrq(phba, rrq)) {
  580. /* if we send the rrq then the completion handler
  581. * will clear the bit in the xribitmap.
  582. */
  583. lpfc_clr_rrq_active(phba, rrq->xritag,
  584. rrq);
  585. }
  586. }
  587. }
  588. /**
  589. * lpfc_get_active_rrq - Get the active RRQ for this exchange.
  590. * @vport: Pointer to vport context object.
  591. * @xri: The xri used in the exchange.
  592. * @did: The targets DID for this exchange.
  593. *
  594. * returns NULL = rrq not found in the phba->active_rrq_list.
  595. * rrq = rrq for this xri and target.
  596. **/
  597. struct lpfc_node_rrq *
  598. lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
  599. {
  600. struct lpfc_hba *phba = vport->phba;
  601. struct lpfc_node_rrq *rrq;
  602. struct lpfc_node_rrq *nextrrq;
  603. unsigned long iflags;
  604. if (phba->sli_rev != LPFC_SLI_REV4)
  605. return NULL;
  606. spin_lock_irqsave(&phba->hbalock, iflags);
  607. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
  608. if (rrq->vport == vport && rrq->xritag == xri &&
  609. rrq->nlp_DID == did){
  610. list_del(&rrq->list);
  611. spin_unlock_irqrestore(&phba->hbalock, iflags);
  612. return rrq;
  613. }
  614. }
  615. spin_unlock_irqrestore(&phba->hbalock, iflags);
  616. return NULL;
  617. }
  618. /**
  619. * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
  620. * @vport: Pointer to vport context object.
  621. * @ndlp: Pointer to the lpfc_node_list structure.
  622. * If ndlp is NULL Remove all active RRQs for this vport from the
  623. * phba->active_rrq_list and clear the rrq.
  624. * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
  625. **/
  626. void
  627. lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  628. {
  629. struct lpfc_hba *phba = vport->phba;
  630. struct lpfc_node_rrq *rrq;
  631. struct lpfc_node_rrq *nextrrq;
  632. unsigned long iflags;
  633. LIST_HEAD(rrq_list);
  634. if (phba->sli_rev != LPFC_SLI_REV4)
  635. return;
  636. if (!ndlp) {
  637. lpfc_sli4_vport_delete_els_xri_aborted(vport);
  638. lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
  639. }
  640. spin_lock_irqsave(&phba->hbalock, iflags);
  641. list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
  642. if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
  643. list_move(&rrq->list, &rrq_list);
  644. spin_unlock_irqrestore(&phba->hbalock, iflags);
  645. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  646. list_del(&rrq->list);
  647. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  648. }
  649. }
  650. /**
  651. * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
  652. * @phba: Pointer to HBA context object.
  653. *
  654. * Remove all rrqs from the phba->active_rrq_list and free them by
  655. * calling __lpfc_clr_active_rrq
  656. *
  657. **/
  658. void
  659. lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
  660. {
  661. struct lpfc_node_rrq *rrq;
  662. struct lpfc_node_rrq *nextrrq;
  663. unsigned long next_time;
  664. unsigned long iflags;
  665. LIST_HEAD(rrq_list);
  666. if (phba->sli_rev != LPFC_SLI_REV4)
  667. return;
  668. spin_lock_irqsave(&phba->hbalock, iflags);
  669. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  670. next_time = jiffies + HZ * (phba->fc_ratov * 2);
  671. list_splice_init(&phba->active_rrq_list, &rrq_list);
  672. spin_unlock_irqrestore(&phba->hbalock, iflags);
  673. list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
  674. list_del(&rrq->list);
  675. lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
  676. }
  677. if (!list_empty(&phba->active_rrq_list))
  678. mod_timer(&phba->rrq_tmr, next_time);
  679. }
  680. /**
  681. * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
  682. * @phba: Pointer to HBA context object.
  683. * @ndlp: Targets nodelist pointer for this exchange.
  684. * @xritag the xri in the bitmap to test.
  685. *
  686. * This function is called with hbalock held. This function
  687. * returns 0 = rrq not active for this xri
  688. * 1 = rrq is valid for this xri.
  689. **/
  690. int
  691. lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  692. uint16_t xritag)
  693. {
  694. if (!ndlp)
  695. return 0;
  696. if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  697. return 1;
  698. else
  699. return 0;
  700. }
  701. /**
  702. * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
  703. * @phba: Pointer to HBA context object.
  704. * @ndlp: nodelist pointer for this target.
  705. * @xritag: xri used in this exchange.
  706. * @rxid: Remote Exchange ID.
  707. * @send_rrq: Flag used to determine if we should send rrq els cmd.
  708. *
  709. * This function takes the hbalock.
  710. * The active bit is always set in the active rrq xri_bitmap even
  711. * if there is no slot avaiable for the other rrq information.
  712. *
  713. * returns 0 rrq actived for this xri
  714. * < 0 No memory or invalid ndlp.
  715. **/
  716. int
  717. lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  718. uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
  719. {
  720. unsigned long iflags;
  721. struct lpfc_node_rrq *rrq;
  722. int empty;
  723. if (!ndlp)
  724. return -EINVAL;
  725. if (!phba->cfg_enable_rrq)
  726. return -EINVAL;
  727. spin_lock_irqsave(&phba->hbalock, iflags);
  728. if (phba->pport->load_flag & FC_UNLOADING) {
  729. phba->hba_flag &= ~HBA_RRQ_ACTIVE;
  730. goto out;
  731. }
  732. /*
  733. * set the active bit even if there is no mem available.
  734. */
  735. if (NLP_CHK_FREE_REQ(ndlp))
  736. goto out;
  737. if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
  738. goto out;
  739. if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
  740. goto out;
  741. spin_unlock_irqrestore(&phba->hbalock, iflags);
  742. rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
  743. if (!rrq) {
  744. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  745. "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
  746. " DID:0x%x Send:%d\n",
  747. xritag, rxid, ndlp->nlp_DID, send_rrq);
  748. return -EINVAL;
  749. }
  750. rrq->send_rrq = send_rrq;
  751. rrq->xritag = xritag;
  752. rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
  753. rrq->ndlp = ndlp;
  754. rrq->nlp_DID = ndlp->nlp_DID;
  755. rrq->vport = ndlp->vport;
  756. rrq->rxid = rxid;
  757. rrq->send_rrq = send_rrq;
  758. spin_lock_irqsave(&phba->hbalock, iflags);
  759. empty = list_empty(&phba->active_rrq_list);
  760. list_add_tail(&rrq->list, &phba->active_rrq_list);
  761. phba->hba_flag |= HBA_RRQ_ACTIVE;
  762. if (empty)
  763. lpfc_worker_wake_up(phba);
  764. spin_unlock_irqrestore(&phba->hbalock, iflags);
  765. return 0;
  766. out:
  767. spin_unlock_irqrestore(&phba->hbalock, iflags);
  768. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  769. "2921 Can't set rrq active xri:0x%x rxid:0x%x"
  770. " DID:0x%x Send:%d\n",
  771. xritag, rxid, ndlp->nlp_DID, send_rrq);
  772. return -EINVAL;
  773. }
  774. /**
  775. * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
  776. * @phba: Pointer to HBA context object.
  777. * @piocb: Pointer to the iocbq.
  778. *
  779. * This function is called with hbalock held. This function
  780. * gets a new driver sglq object from the sglq list. If the
  781. * list is not empty then it is successful, it returns pointer to the newly
  782. * allocated sglq object else it returns NULL.
  783. **/
  784. static struct lpfc_sglq *
  785. __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
  786. {
  787. struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
  788. struct lpfc_sglq *sglq = NULL;
  789. struct lpfc_sglq *start_sglq = NULL;
  790. struct lpfc_scsi_buf *lpfc_cmd;
  791. struct lpfc_nodelist *ndlp;
  792. int found = 0;
  793. if (piocbq->iocb_flag & LPFC_IO_FCP) {
  794. lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
  795. ndlp = lpfc_cmd->rdata->pnode;
  796. } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
  797. !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
  798. ndlp = piocbq->context_un.ndlp;
  799. else
  800. ndlp = piocbq->context1;
  801. list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
  802. start_sglq = sglq;
  803. while (!found) {
  804. if (!sglq)
  805. return NULL;
  806. if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
  807. /* This xri has an rrq outstanding for this DID.
  808. * put it back in the list and get another xri.
  809. */
  810. list_add_tail(&sglq->list, lpfc_sgl_list);
  811. sglq = NULL;
  812. list_remove_head(lpfc_sgl_list, sglq,
  813. struct lpfc_sglq, list);
  814. if (sglq == start_sglq) {
  815. sglq = NULL;
  816. break;
  817. } else
  818. continue;
  819. }
  820. sglq->ndlp = ndlp;
  821. found = 1;
  822. phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
  823. sglq->state = SGL_ALLOCATED;
  824. }
  825. return sglq;
  826. }
  827. /**
  828. * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
  829. * @phba: Pointer to HBA context object.
  830. *
  831. * This function is called with no lock held. This function
  832. * allocates a new driver iocb object from the iocb pool. If the
  833. * allocation is successful, it returns pointer to the newly
  834. * allocated iocb object else it returns NULL.
  835. **/
  836. struct lpfc_iocbq *
  837. lpfc_sli_get_iocbq(struct lpfc_hba *phba)
  838. {
  839. struct lpfc_iocbq * iocbq = NULL;
  840. unsigned long iflags;
  841. spin_lock_irqsave(&phba->hbalock, iflags);
  842. iocbq = __lpfc_sli_get_iocbq(phba);
  843. spin_unlock_irqrestore(&phba->hbalock, iflags);
  844. return iocbq;
  845. }
  846. /**
  847. * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
  848. * @phba: Pointer to HBA context object.
  849. * @iocbq: Pointer to driver iocb object.
  850. *
  851. * This function is called with hbalock held to release driver
  852. * iocb object to the iocb pool. The iotag in the iocb object
  853. * does not change for each use of the iocb object. This function
  854. * clears all other fields of the iocb object when it is freed.
  855. * The sqlq structure that holds the xritag and phys and virtual
  856. * mappings for the scatter gather list is retrieved from the
  857. * active array of sglq. The get of the sglq pointer also clears
  858. * the entry in the array. If the status of the IO indiactes that
  859. * this IO was aborted then the sglq entry it put on the
  860. * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
  861. * IO has good status or fails for any other reason then the sglq
  862. * entry is added to the free list (lpfc_sgl_list).
  863. **/
  864. static void
  865. __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  866. {
  867. struct lpfc_sglq *sglq;
  868. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  869. unsigned long iflag = 0;
  870. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  871. if (iocbq->sli4_xritag == NO_XRI)
  872. sglq = NULL;
  873. else
  874. sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
  875. if (sglq) {
  876. if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
  877. (sglq->state != SGL_XRI_ABORTED)) {
  878. spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
  879. iflag);
  880. list_add(&sglq->list,
  881. &phba->sli4_hba.lpfc_abts_els_sgl_list);
  882. spin_unlock_irqrestore(
  883. &phba->sli4_hba.abts_sgl_list_lock, iflag);
  884. } else {
  885. sglq->state = SGL_FREED;
  886. sglq->ndlp = NULL;
  887. list_add_tail(&sglq->list,
  888. &phba->sli4_hba.lpfc_sgl_list);
  889. /* Check if TXQ queue needs to be serviced */
  890. if (pring->txq_cnt)
  891. lpfc_worker_wake_up(phba);
  892. }
  893. }
  894. /*
  895. * Clean all volatile data fields, preserve iotag and node struct.
  896. */
  897. memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  898. iocbq->sli4_lxritag = NO_XRI;
  899. iocbq->sli4_xritag = NO_XRI;
  900. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  901. }
  902. /**
  903. * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
  904. * @phba: Pointer to HBA context object.
  905. * @iocbq: Pointer to driver iocb object.
  906. *
  907. * This function is called with hbalock held to release driver
  908. * iocb object to the iocb pool. The iotag in the iocb object
  909. * does not change for each use of the iocb object. This function
  910. * clears all other fields of the iocb object when it is freed.
  911. **/
  912. static void
  913. __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  914. {
  915. size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
  916. /*
  917. * Clean all volatile data fields, preserve iotag and node struct.
  918. */
  919. memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
  920. iocbq->sli4_xritag = NO_XRI;
  921. list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
  922. }
  923. /**
  924. * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
  925. * @phba: Pointer to HBA context object.
  926. * @iocbq: Pointer to driver iocb object.
  927. *
  928. * This function is called with hbalock held to release driver
  929. * iocb object to the iocb pool. The iotag in the iocb object
  930. * does not change for each use of the iocb object. This function
  931. * clears all other fields of the iocb object when it is freed.
  932. **/
  933. static void
  934. __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  935. {
  936. phba->__lpfc_sli_release_iocbq(phba, iocbq);
  937. phba->iocb_cnt--;
  938. }
  939. /**
  940. * lpfc_sli_release_iocbq - Release iocb to the iocb pool
  941. * @phba: Pointer to HBA context object.
  942. * @iocbq: Pointer to driver iocb object.
  943. *
  944. * This function is called with no lock held to release the iocb to
  945. * iocb pool.
  946. **/
  947. void
  948. lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  949. {
  950. unsigned long iflags;
  951. /*
  952. * Clean all volatile data fields, preserve iotag and node struct.
  953. */
  954. spin_lock_irqsave(&phba->hbalock, iflags);
  955. __lpfc_sli_release_iocbq(phba, iocbq);
  956. spin_unlock_irqrestore(&phba->hbalock, iflags);
  957. }
  958. /**
  959. * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
  960. * @phba: Pointer to HBA context object.
  961. * @iocblist: List of IOCBs.
  962. * @ulpstatus: ULP status in IOCB command field.
  963. * @ulpWord4: ULP word-4 in IOCB command field.
  964. *
  965. * This function is called with a list of IOCBs to cancel. It cancels the IOCB
  966. * on the list by invoking the complete callback function associated with the
  967. * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
  968. * fields.
  969. **/
  970. void
  971. lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
  972. uint32_t ulpstatus, uint32_t ulpWord4)
  973. {
  974. struct lpfc_iocbq *piocb;
  975. while (!list_empty(iocblist)) {
  976. list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
  977. if (!piocb->iocb_cmpl)
  978. lpfc_sli_release_iocbq(phba, piocb);
  979. else {
  980. piocb->iocb.ulpStatus = ulpstatus;
  981. piocb->iocb.un.ulpWord[4] = ulpWord4;
  982. (piocb->iocb_cmpl) (phba, piocb, piocb);
  983. }
  984. }
  985. return;
  986. }
  987. /**
  988. * lpfc_sli_iocb_cmd_type - Get the iocb type
  989. * @iocb_cmnd: iocb command code.
  990. *
  991. * This function is called by ring event handler function to get the iocb type.
  992. * This function translates the iocb command to an iocb command type used to
  993. * decide the final disposition of each completed IOCB.
  994. * The function returns
  995. * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
  996. * LPFC_SOL_IOCB if it is a solicited iocb completion
  997. * LPFC_ABORT_IOCB if it is an abort iocb
  998. * LPFC_UNSOL_IOCB if it is an unsolicited iocb
  999. *
  1000. * The caller is not required to hold any lock.
  1001. **/
  1002. static lpfc_iocb_type
  1003. lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
  1004. {
  1005. lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
  1006. if (iocb_cmnd > CMD_MAX_IOCB_CMD)
  1007. return 0;
  1008. switch (iocb_cmnd) {
  1009. case CMD_XMIT_SEQUENCE_CR:
  1010. case CMD_XMIT_SEQUENCE_CX:
  1011. case CMD_XMIT_BCAST_CN:
  1012. case CMD_XMIT_BCAST_CX:
  1013. case CMD_ELS_REQUEST_CR:
  1014. case CMD_ELS_REQUEST_CX:
  1015. case CMD_CREATE_XRI_CR:
  1016. case CMD_CREATE_XRI_CX:
  1017. case CMD_GET_RPI_CN:
  1018. case CMD_XMIT_ELS_RSP_CX:
  1019. case CMD_GET_RPI_CR:
  1020. case CMD_FCP_IWRITE_CR:
  1021. case CMD_FCP_IWRITE_CX:
  1022. case CMD_FCP_IREAD_CR:
  1023. case CMD_FCP_IREAD_CX:
  1024. case CMD_FCP_ICMND_CR:
  1025. case CMD_FCP_ICMND_CX:
  1026. case CMD_FCP_TSEND_CX:
  1027. case CMD_FCP_TRSP_CX:
  1028. case CMD_FCP_TRECEIVE_CX:
  1029. case CMD_FCP_AUTO_TRSP_CX:
  1030. case CMD_ADAPTER_MSG:
  1031. case CMD_ADAPTER_DUMP:
  1032. case CMD_XMIT_SEQUENCE64_CR:
  1033. case CMD_XMIT_SEQUENCE64_CX:
  1034. case CMD_XMIT_BCAST64_CN:
  1035. case CMD_XMIT_BCAST64_CX:
  1036. case CMD_ELS_REQUEST64_CR:
  1037. case CMD_ELS_REQUEST64_CX:
  1038. case CMD_FCP_IWRITE64_CR:
  1039. case CMD_FCP_IWRITE64_CX:
  1040. case CMD_FCP_IREAD64_CR:
  1041. case CMD_FCP_IREAD64_CX:
  1042. case CMD_FCP_ICMND64_CR:
  1043. case CMD_FCP_ICMND64_CX:
  1044. case CMD_FCP_TSEND64_CX:
  1045. case CMD_FCP_TRSP64_CX:
  1046. case CMD_FCP_TRECEIVE64_CX:
  1047. case CMD_GEN_REQUEST64_CR:
  1048. case CMD_GEN_REQUEST64_CX:
  1049. case CMD_XMIT_ELS_RSP64_CX:
  1050. case DSSCMD_IWRITE64_CR:
  1051. case DSSCMD_IWRITE64_CX:
  1052. case DSSCMD_IREAD64_CR:
  1053. case DSSCMD_IREAD64_CX:
  1054. type = LPFC_SOL_IOCB;
  1055. break;
  1056. case CMD_ABORT_XRI_CN:
  1057. case CMD_ABORT_XRI_CX:
  1058. case CMD_CLOSE_XRI_CN:
  1059. case CMD_CLOSE_XRI_CX:
  1060. case CMD_XRI_ABORTED_CX:
  1061. case CMD_ABORT_MXRI64_CN:
  1062. case CMD_XMIT_BLS_RSP64_CX:
  1063. type = LPFC_ABORT_IOCB;
  1064. break;
  1065. case CMD_RCV_SEQUENCE_CX:
  1066. case CMD_RCV_ELS_REQ_CX:
  1067. case CMD_RCV_SEQUENCE64_CX:
  1068. case CMD_RCV_ELS_REQ64_CX:
  1069. case CMD_ASYNC_STATUS:
  1070. case CMD_IOCB_RCV_SEQ64_CX:
  1071. case CMD_IOCB_RCV_ELS64_CX:
  1072. case CMD_IOCB_RCV_CONT64_CX:
  1073. case CMD_IOCB_RET_XRI64_CX:
  1074. type = LPFC_UNSOL_IOCB;
  1075. break;
  1076. case CMD_IOCB_XMIT_MSEQ64_CR:
  1077. case CMD_IOCB_XMIT_MSEQ64_CX:
  1078. case CMD_IOCB_RCV_SEQ_LIST64_CX:
  1079. case CMD_IOCB_RCV_ELS_LIST64_CX:
  1080. case CMD_IOCB_CLOSE_EXTENDED_CN:
  1081. case CMD_IOCB_ABORT_EXTENDED_CN:
  1082. case CMD_IOCB_RET_HBQE64_CN:
  1083. case CMD_IOCB_FCP_IBIDIR64_CR:
  1084. case CMD_IOCB_FCP_IBIDIR64_CX:
  1085. case CMD_IOCB_FCP_ITASKMGT64_CX:
  1086. case CMD_IOCB_LOGENTRY_CN:
  1087. case CMD_IOCB_LOGENTRY_ASYNC_CN:
  1088. printk("%s - Unhandled SLI-3 Command x%x\n",
  1089. __func__, iocb_cmnd);
  1090. type = LPFC_UNKNOWN_IOCB;
  1091. break;
  1092. default:
  1093. type = LPFC_UNKNOWN_IOCB;
  1094. break;
  1095. }
  1096. return type;
  1097. }
  1098. /**
  1099. * lpfc_sli_ring_map - Issue config_ring mbox for all rings
  1100. * @phba: Pointer to HBA context object.
  1101. *
  1102. * This function is called from SLI initialization code
  1103. * to configure every ring of the HBA's SLI interface. The
  1104. * caller is not required to hold any lock. This function issues
  1105. * a config_ring mailbox command for each ring.
  1106. * This function returns zero if successful else returns a negative
  1107. * error code.
  1108. **/
  1109. static int
  1110. lpfc_sli_ring_map(struct lpfc_hba *phba)
  1111. {
  1112. struct lpfc_sli *psli = &phba->sli;
  1113. LPFC_MBOXQ_t *pmb;
  1114. MAILBOX_t *pmbox;
  1115. int i, rc, ret = 0;
  1116. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1117. if (!pmb)
  1118. return -ENOMEM;
  1119. pmbox = &pmb->u.mb;
  1120. phba->link_state = LPFC_INIT_MBX_CMDS;
  1121. for (i = 0; i < psli->num_rings; i++) {
  1122. lpfc_config_ring(phba, i, pmb);
  1123. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  1124. if (rc != MBX_SUCCESS) {
  1125. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1126. "0446 Adapter failed to init (%d), "
  1127. "mbxCmd x%x CFG_RING, mbxStatus x%x, "
  1128. "ring %d\n",
  1129. rc, pmbox->mbxCommand,
  1130. pmbox->mbxStatus, i);
  1131. phba->link_state = LPFC_HBA_ERROR;
  1132. ret = -ENXIO;
  1133. break;
  1134. }
  1135. }
  1136. mempool_free(pmb, phba->mbox_mem_pool);
  1137. return ret;
  1138. }
  1139. /**
  1140. * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
  1141. * @phba: Pointer to HBA context object.
  1142. * @pring: Pointer to driver SLI ring object.
  1143. * @piocb: Pointer to the driver iocb object.
  1144. *
  1145. * This function is called with hbalock held. The function adds the
  1146. * new iocb to txcmplq of the given ring. This function always returns
  1147. * 0. If this function is called for ELS ring, this function checks if
  1148. * there is a vport associated with the ELS command. This function also
  1149. * starts els_tmofunc timer if this is an ELS command.
  1150. **/
  1151. static int
  1152. lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1153. struct lpfc_iocbq *piocb)
  1154. {
  1155. list_add_tail(&piocb->list, &pring->txcmplq);
  1156. piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
  1157. pring->txcmplq_cnt++;
  1158. if (pring->txcmplq_cnt > pring->txcmplq_max)
  1159. pring->txcmplq_max = pring->txcmplq_cnt;
  1160. if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
  1161. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  1162. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  1163. if (!piocb->vport)
  1164. BUG();
  1165. else
  1166. mod_timer(&piocb->vport->els_tmofunc,
  1167. jiffies + HZ * (phba->fc_ratov << 1));
  1168. }
  1169. return 0;
  1170. }
  1171. /**
  1172. * lpfc_sli_ringtx_get - Get first element of the txq
  1173. * @phba: Pointer to HBA context object.
  1174. * @pring: Pointer to driver SLI ring object.
  1175. *
  1176. * This function is called with hbalock held to get next
  1177. * iocb in txq of the given ring. If there is any iocb in
  1178. * the txq, the function returns first iocb in the list after
  1179. * removing the iocb from the list, else it returns NULL.
  1180. **/
  1181. struct lpfc_iocbq *
  1182. lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1183. {
  1184. struct lpfc_iocbq *cmd_iocb;
  1185. list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
  1186. if (cmd_iocb != NULL)
  1187. pring->txq_cnt--;
  1188. return cmd_iocb;
  1189. }
  1190. /**
  1191. * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
  1192. * @phba: Pointer to HBA context object.
  1193. * @pring: Pointer to driver SLI ring object.
  1194. *
  1195. * This function is called with hbalock held and the caller must post the
  1196. * iocb without releasing the lock. If the caller releases the lock,
  1197. * iocb slot returned by the function is not guaranteed to be available.
  1198. * The function returns pointer to the next available iocb slot if there
  1199. * is available slot in the ring, else it returns NULL.
  1200. * If the get index of the ring is ahead of the put index, the function
  1201. * will post an error attention event to the worker thread to take the
  1202. * HBA to offline state.
  1203. **/
  1204. static IOCB_t *
  1205. lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1206. {
  1207. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  1208. uint32_t max_cmd_idx = pring->numCiocb;
  1209. if ((pring->next_cmdidx == pring->cmdidx) &&
  1210. (++pring->next_cmdidx >= max_cmd_idx))
  1211. pring->next_cmdidx = 0;
  1212. if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
  1213. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  1214. if (unlikely(pring->local_getidx >= max_cmd_idx)) {
  1215. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1216. "0315 Ring %d issue: portCmdGet %d "
  1217. "is bigger than cmd ring %d\n",
  1218. pring->ringno,
  1219. pring->local_getidx, max_cmd_idx);
  1220. phba->link_state = LPFC_HBA_ERROR;
  1221. /*
  1222. * All error attention handlers are posted to
  1223. * worker thread
  1224. */
  1225. phba->work_ha |= HA_ERATT;
  1226. phba->work_hs = HS_FFER3;
  1227. lpfc_worker_wake_up(phba);
  1228. return NULL;
  1229. }
  1230. if (pring->local_getidx == pring->next_cmdidx)
  1231. return NULL;
  1232. }
  1233. return lpfc_cmd_iocb(phba, pring);
  1234. }
  1235. /**
  1236. * lpfc_sli_next_iotag - Get an iotag for the iocb
  1237. * @phba: Pointer to HBA context object.
  1238. * @iocbq: Pointer to driver iocb object.
  1239. *
  1240. * This function gets an iotag for the iocb. If there is no unused iotag and
  1241. * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
  1242. * array and assigns a new iotag.
  1243. * The function returns the allocated iotag if successful, else returns zero.
  1244. * Zero is not a valid iotag.
  1245. * The caller is not required to hold any lock.
  1246. **/
  1247. uint16_t
  1248. lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
  1249. {
  1250. struct lpfc_iocbq **new_arr;
  1251. struct lpfc_iocbq **old_arr;
  1252. size_t new_len;
  1253. struct lpfc_sli *psli = &phba->sli;
  1254. uint16_t iotag;
  1255. spin_lock_irq(&phba->hbalock);
  1256. iotag = psli->last_iotag;
  1257. if(++iotag < psli->iocbq_lookup_len) {
  1258. psli->last_iotag = iotag;
  1259. psli->iocbq_lookup[iotag] = iocbq;
  1260. spin_unlock_irq(&phba->hbalock);
  1261. iocbq->iotag = iotag;
  1262. return iotag;
  1263. } else if (psli->iocbq_lookup_len < (0xffff
  1264. - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
  1265. new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
  1266. spin_unlock_irq(&phba->hbalock);
  1267. new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
  1268. GFP_KERNEL);
  1269. if (new_arr) {
  1270. spin_lock_irq(&phba->hbalock);
  1271. old_arr = psli->iocbq_lookup;
  1272. if (new_len <= psli->iocbq_lookup_len) {
  1273. /* highly unprobable case */
  1274. kfree(new_arr);
  1275. iotag = psli->last_iotag;
  1276. if(++iotag < psli->iocbq_lookup_len) {
  1277. psli->last_iotag = iotag;
  1278. psli->iocbq_lookup[iotag] = iocbq;
  1279. spin_unlock_irq(&phba->hbalock);
  1280. iocbq->iotag = iotag;
  1281. return iotag;
  1282. }
  1283. spin_unlock_irq(&phba->hbalock);
  1284. return 0;
  1285. }
  1286. if (psli->iocbq_lookup)
  1287. memcpy(new_arr, old_arr,
  1288. ((psli->last_iotag + 1) *
  1289. sizeof (struct lpfc_iocbq *)));
  1290. psli->iocbq_lookup = new_arr;
  1291. psli->iocbq_lookup_len = new_len;
  1292. psli->last_iotag = iotag;
  1293. psli->iocbq_lookup[iotag] = iocbq;
  1294. spin_unlock_irq(&phba->hbalock);
  1295. iocbq->iotag = iotag;
  1296. kfree(old_arr);
  1297. return iotag;
  1298. }
  1299. } else
  1300. spin_unlock_irq(&phba->hbalock);
  1301. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  1302. "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
  1303. psli->last_iotag);
  1304. return 0;
  1305. }
  1306. /**
  1307. * lpfc_sli_submit_iocb - Submit an iocb to the firmware
  1308. * @phba: Pointer to HBA context object.
  1309. * @pring: Pointer to driver SLI ring object.
  1310. * @iocb: Pointer to iocb slot in the ring.
  1311. * @nextiocb: Pointer to driver iocb object which need to be
  1312. * posted to firmware.
  1313. *
  1314. * This function is called with hbalock held to post a new iocb to
  1315. * the firmware. This function copies the new iocb to ring iocb slot and
  1316. * updates the ring pointers. It adds the new iocb to txcmplq if there is
  1317. * a completion call back for this iocb else the function will free the
  1318. * iocb object.
  1319. **/
  1320. static void
  1321. lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  1322. IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
  1323. {
  1324. /*
  1325. * Set up an iotag
  1326. */
  1327. nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
  1328. if (pring->ringno == LPFC_ELS_RING) {
  1329. lpfc_debugfs_slow_ring_trc(phba,
  1330. "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  1331. *(((uint32_t *) &nextiocb->iocb) + 4),
  1332. *(((uint32_t *) &nextiocb->iocb) + 6),
  1333. *(((uint32_t *) &nextiocb->iocb) + 7));
  1334. }
  1335. /*
  1336. * Issue iocb command to adapter
  1337. */
  1338. lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
  1339. wmb();
  1340. pring->stats.iocb_cmd++;
  1341. /*
  1342. * If there is no completion routine to call, we can release the
  1343. * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
  1344. * that have no rsp ring completion, iocb_cmpl MUST be NULL.
  1345. */
  1346. if (nextiocb->iocb_cmpl)
  1347. lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
  1348. else
  1349. __lpfc_sli_release_iocbq(phba, nextiocb);
  1350. /*
  1351. * Let the HBA know what IOCB slot will be the next one the
  1352. * driver will put a command into.
  1353. */
  1354. pring->cmdidx = pring->next_cmdidx;
  1355. writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
  1356. }
  1357. /**
  1358. * lpfc_sli_update_full_ring - Update the chip attention register
  1359. * @phba: Pointer to HBA context object.
  1360. * @pring: Pointer to driver SLI ring object.
  1361. *
  1362. * The caller is not required to hold any lock for calling this function.
  1363. * This function updates the chip attention bits for the ring to inform firmware
  1364. * that there are pending work to be done for this ring and requests an
  1365. * interrupt when there is space available in the ring. This function is
  1366. * called when the driver is unable to post more iocbs to the ring due
  1367. * to unavailability of space in the ring.
  1368. **/
  1369. static void
  1370. lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1371. {
  1372. int ringno = pring->ringno;
  1373. pring->flag |= LPFC_CALL_RING_AVAILABLE;
  1374. wmb();
  1375. /*
  1376. * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
  1377. * The HBA will tell us when an IOCB entry is available.
  1378. */
  1379. writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
  1380. readl(phba->CAregaddr); /* flush */
  1381. pring->stats.iocb_cmd_full++;
  1382. }
  1383. /**
  1384. * lpfc_sli_update_ring - Update chip attention register
  1385. * @phba: Pointer to HBA context object.
  1386. * @pring: Pointer to driver SLI ring object.
  1387. *
  1388. * This function updates the chip attention register bit for the
  1389. * given ring to inform HBA that there is more work to be done
  1390. * in this ring. The caller is not required to hold any lock.
  1391. **/
  1392. static void
  1393. lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1394. {
  1395. int ringno = pring->ringno;
  1396. /*
  1397. * Tell the HBA that there is work to do in this ring.
  1398. */
  1399. if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
  1400. wmb();
  1401. writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
  1402. readl(phba->CAregaddr); /* flush */
  1403. }
  1404. }
  1405. /**
  1406. * lpfc_sli_resume_iocb - Process iocbs in the txq
  1407. * @phba: Pointer to HBA context object.
  1408. * @pring: Pointer to driver SLI ring object.
  1409. *
  1410. * This function is called with hbalock held to post pending iocbs
  1411. * in the txq to the firmware. This function is called when driver
  1412. * detects space available in the ring.
  1413. **/
  1414. static void
  1415. lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  1416. {
  1417. IOCB_t *iocb;
  1418. struct lpfc_iocbq *nextiocb;
  1419. /*
  1420. * Check to see if:
  1421. * (a) there is anything on the txq to send
  1422. * (b) link is up
  1423. * (c) link attention events can be processed (fcp ring only)
  1424. * (d) IOCB processing is not blocked by the outstanding mbox command.
  1425. */
  1426. if (pring->txq_cnt &&
  1427. lpfc_is_link_up(phba) &&
  1428. (pring->ringno != phba->sli.fcp_ring ||
  1429. phba->sli.sli_flag & LPFC_PROCESS_LA)) {
  1430. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  1431. (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
  1432. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  1433. if (iocb)
  1434. lpfc_sli_update_ring(phba, pring);
  1435. else
  1436. lpfc_sli_update_full_ring(phba, pring);
  1437. }
  1438. return;
  1439. }
  1440. /**
  1441. * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
  1442. * @phba: Pointer to HBA context object.
  1443. * @hbqno: HBQ number.
  1444. *
  1445. * This function is called with hbalock held to get the next
  1446. * available slot for the given HBQ. If there is free slot
  1447. * available for the HBQ it will return pointer to the next available
  1448. * HBQ entry else it will return NULL.
  1449. **/
  1450. static struct lpfc_hbq_entry *
  1451. lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
  1452. {
  1453. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1454. if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
  1455. ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
  1456. hbqp->next_hbqPutIdx = 0;
  1457. if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
  1458. uint32_t raw_index = phba->hbq_get[hbqno];
  1459. uint32_t getidx = le32_to_cpu(raw_index);
  1460. hbqp->local_hbqGetIdx = getidx;
  1461. if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
  1462. lpfc_printf_log(phba, KERN_ERR,
  1463. LOG_SLI | LOG_VPORT,
  1464. "1802 HBQ %d: local_hbqGetIdx "
  1465. "%u is > than hbqp->entry_count %u\n",
  1466. hbqno, hbqp->local_hbqGetIdx,
  1467. hbqp->entry_count);
  1468. phba->link_state = LPFC_HBA_ERROR;
  1469. return NULL;
  1470. }
  1471. if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
  1472. return NULL;
  1473. }
  1474. return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
  1475. hbqp->hbqPutIdx;
  1476. }
  1477. /**
  1478. * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
  1479. * @phba: Pointer to HBA context object.
  1480. *
  1481. * This function is called with no lock held to free all the
  1482. * hbq buffers while uninitializing the SLI interface. It also
  1483. * frees the HBQ buffers returned by the firmware but not yet
  1484. * processed by the upper layers.
  1485. **/
  1486. void
  1487. lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
  1488. {
  1489. struct lpfc_dmabuf *dmabuf, *next_dmabuf;
  1490. struct hbq_dmabuf *hbq_buf;
  1491. unsigned long flags;
  1492. int i, hbq_count;
  1493. uint32_t hbqno;
  1494. hbq_count = lpfc_sli_hbq_count();
  1495. /* Return all memory used by all HBQs */
  1496. spin_lock_irqsave(&phba->hbalock, flags);
  1497. for (i = 0; i < hbq_count; ++i) {
  1498. list_for_each_entry_safe(dmabuf, next_dmabuf,
  1499. &phba->hbqs[i].hbq_buffer_list, list) {
  1500. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1501. list_del(&hbq_buf->dbuf.list);
  1502. (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
  1503. }
  1504. phba->hbqs[i].buffer_count = 0;
  1505. }
  1506. /* Return all HBQ buffer that are in-fly */
  1507. list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
  1508. list) {
  1509. hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
  1510. list_del(&hbq_buf->dbuf.list);
  1511. if (hbq_buf->tag == -1) {
  1512. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1513. (phba, hbq_buf);
  1514. } else {
  1515. hbqno = hbq_buf->tag >> 16;
  1516. if (hbqno >= LPFC_MAX_HBQS)
  1517. (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
  1518. (phba, hbq_buf);
  1519. else
  1520. (phba->hbqs[hbqno].hbq_free_buffer)(phba,
  1521. hbq_buf);
  1522. }
  1523. }
  1524. /* Mark the HBQs not in use */
  1525. phba->hbq_in_use = 0;
  1526. spin_unlock_irqrestore(&phba->hbalock, flags);
  1527. }
  1528. /**
  1529. * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
  1530. * @phba: Pointer to HBA context object.
  1531. * @hbqno: HBQ number.
  1532. * @hbq_buf: Pointer to HBQ buffer.
  1533. *
  1534. * This function is called with the hbalock held to post a
  1535. * hbq buffer to the firmware. If the function finds an empty
  1536. * slot in the HBQ, it will post the buffer. The function will return
  1537. * pointer to the hbq entry if it successfully post the buffer
  1538. * else it will return NULL.
  1539. **/
  1540. static int
  1541. lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
  1542. struct hbq_dmabuf *hbq_buf)
  1543. {
  1544. return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
  1545. }
  1546. /**
  1547. * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
  1548. * @phba: Pointer to HBA context object.
  1549. * @hbqno: HBQ number.
  1550. * @hbq_buf: Pointer to HBQ buffer.
  1551. *
  1552. * This function is called with the hbalock held to post a hbq buffer to the
  1553. * firmware. If the function finds an empty slot in the HBQ, it will post the
  1554. * buffer and place it on the hbq_buffer_list. The function will return zero if
  1555. * it successfully post the buffer else it will return an error.
  1556. **/
  1557. static int
  1558. lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
  1559. struct hbq_dmabuf *hbq_buf)
  1560. {
  1561. struct lpfc_hbq_entry *hbqe;
  1562. dma_addr_t physaddr = hbq_buf->dbuf.phys;
  1563. /* Get next HBQ entry slot to use */
  1564. hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
  1565. if (hbqe) {
  1566. struct hbq_s *hbqp = &phba->hbqs[hbqno];
  1567. hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1568. hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1569. hbqe->bde.tus.f.bdeSize = hbq_buf->size;
  1570. hbqe->bde.tus.f.bdeFlags = 0;
  1571. hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
  1572. hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
  1573. /* Sync SLIM */
  1574. hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
  1575. writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
  1576. /* flush */
  1577. readl(phba->hbq_put + hbqno);
  1578. list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
  1579. return 0;
  1580. } else
  1581. return -ENOMEM;
  1582. }
  1583. /**
  1584. * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
  1585. * @phba: Pointer to HBA context object.
  1586. * @hbqno: HBQ number.
  1587. * @hbq_buf: Pointer to HBQ buffer.
  1588. *
  1589. * This function is called with the hbalock held to post an RQE to the SLI4
  1590. * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
  1591. * the hbq_buffer_list and return zero, otherwise it will return an error.
  1592. **/
  1593. static int
  1594. lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
  1595. struct hbq_dmabuf *hbq_buf)
  1596. {
  1597. int rc;
  1598. struct lpfc_rqe hrqe;
  1599. struct lpfc_rqe drqe;
  1600. hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
  1601. hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
  1602. drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
  1603. drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
  1604. rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
  1605. &hrqe, &drqe);
  1606. if (rc < 0)
  1607. return rc;
  1608. hbq_buf->tag = rc;
  1609. list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
  1610. return 0;
  1611. }
  1612. /* HBQ for ELS and CT traffic. */
  1613. static struct lpfc_hbq_init lpfc_els_hbq = {
  1614. .rn = 1,
  1615. .entry_count = 256,
  1616. .mask_count = 0,
  1617. .profile = 0,
  1618. .ring_mask = (1 << LPFC_ELS_RING),
  1619. .buffer_count = 0,
  1620. .init_count = 40,
  1621. .add_count = 40,
  1622. };
  1623. /* HBQ for the extra ring if needed */
  1624. static struct lpfc_hbq_init lpfc_extra_hbq = {
  1625. .rn = 1,
  1626. .entry_count = 200,
  1627. .mask_count = 0,
  1628. .profile = 0,
  1629. .ring_mask = (1 << LPFC_EXTRA_RING),
  1630. .buffer_count = 0,
  1631. .init_count = 0,
  1632. .add_count = 5,
  1633. };
  1634. /* Array of HBQs */
  1635. struct lpfc_hbq_init *lpfc_hbq_defs[] = {
  1636. &lpfc_els_hbq,
  1637. &lpfc_extra_hbq,
  1638. };
  1639. /**
  1640. * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
  1641. * @phba: Pointer to HBA context object.
  1642. * @hbqno: HBQ number.
  1643. * @count: Number of HBQ buffers to be posted.
  1644. *
  1645. * This function is called with no lock held to post more hbq buffers to the
  1646. * given HBQ. The function returns the number of HBQ buffers successfully
  1647. * posted.
  1648. **/
  1649. static int
  1650. lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
  1651. {
  1652. uint32_t i, posted = 0;
  1653. unsigned long flags;
  1654. struct hbq_dmabuf *hbq_buffer;
  1655. LIST_HEAD(hbq_buf_list);
  1656. if (!phba->hbqs[hbqno].hbq_alloc_buffer)
  1657. return 0;
  1658. if ((phba->hbqs[hbqno].buffer_count + count) >
  1659. lpfc_hbq_defs[hbqno]->entry_count)
  1660. count = lpfc_hbq_defs[hbqno]->entry_count -
  1661. phba->hbqs[hbqno].buffer_count;
  1662. if (!count)
  1663. return 0;
  1664. /* Allocate HBQ entries */
  1665. for (i = 0; i < count; i++) {
  1666. hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
  1667. if (!hbq_buffer)
  1668. break;
  1669. list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
  1670. }
  1671. /* Check whether HBQ is still in use */
  1672. spin_lock_irqsave(&phba->hbalock, flags);
  1673. if (!phba->hbq_in_use)
  1674. goto err;
  1675. while (!list_empty(&hbq_buf_list)) {
  1676. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1677. dbuf.list);
  1678. hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
  1679. (hbqno << 16));
  1680. if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
  1681. phba->hbqs[hbqno].buffer_count++;
  1682. posted++;
  1683. } else
  1684. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1685. }
  1686. spin_unlock_irqrestore(&phba->hbalock, flags);
  1687. return posted;
  1688. err:
  1689. spin_unlock_irqrestore(&phba->hbalock, flags);
  1690. while (!list_empty(&hbq_buf_list)) {
  1691. list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
  1692. dbuf.list);
  1693. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1694. }
  1695. return 0;
  1696. }
  1697. /**
  1698. * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
  1699. * @phba: Pointer to HBA context object.
  1700. * @qno: HBQ number.
  1701. *
  1702. * This function posts more buffers to the HBQ. This function
  1703. * is called with no lock held. The function returns the number of HBQ entries
  1704. * successfully allocated.
  1705. **/
  1706. int
  1707. lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1708. {
  1709. if (phba->sli_rev == LPFC_SLI_REV4)
  1710. return 0;
  1711. else
  1712. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1713. lpfc_hbq_defs[qno]->add_count);
  1714. }
  1715. /**
  1716. * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
  1717. * @phba: Pointer to HBA context object.
  1718. * @qno: HBQ queue number.
  1719. *
  1720. * This function is called from SLI initialization code path with
  1721. * no lock held to post initial HBQ buffers to firmware. The
  1722. * function returns the number of HBQ entries successfully allocated.
  1723. **/
  1724. static int
  1725. lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
  1726. {
  1727. if (phba->sli_rev == LPFC_SLI_REV4)
  1728. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1729. lpfc_hbq_defs[qno]->entry_count);
  1730. else
  1731. return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
  1732. lpfc_hbq_defs[qno]->init_count);
  1733. }
  1734. /**
  1735. * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
  1736. * @phba: Pointer to HBA context object.
  1737. * @hbqno: HBQ number.
  1738. *
  1739. * This function removes the first hbq buffer on an hbq list and returns a
  1740. * pointer to that buffer. If it finds no buffers on the list it returns NULL.
  1741. **/
  1742. static struct hbq_dmabuf *
  1743. lpfc_sli_hbqbuf_get(struct list_head *rb_list)
  1744. {
  1745. struct lpfc_dmabuf *d_buf;
  1746. list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
  1747. if (!d_buf)
  1748. return NULL;
  1749. return container_of(d_buf, struct hbq_dmabuf, dbuf);
  1750. }
  1751. /**
  1752. * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
  1753. * @phba: Pointer to HBA context object.
  1754. * @tag: Tag of the hbq buffer.
  1755. *
  1756. * This function is called with hbalock held. This function searches
  1757. * for the hbq buffer associated with the given tag in the hbq buffer
  1758. * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
  1759. * it returns NULL.
  1760. **/
  1761. static struct hbq_dmabuf *
  1762. lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
  1763. {
  1764. struct lpfc_dmabuf *d_buf;
  1765. struct hbq_dmabuf *hbq_buf;
  1766. uint32_t hbqno;
  1767. hbqno = tag >> 16;
  1768. if (hbqno >= LPFC_MAX_HBQS)
  1769. return NULL;
  1770. spin_lock_irq(&phba->hbalock);
  1771. list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
  1772. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  1773. if (hbq_buf->tag == tag) {
  1774. spin_unlock_irq(&phba->hbalock);
  1775. return hbq_buf;
  1776. }
  1777. }
  1778. spin_unlock_irq(&phba->hbalock);
  1779. lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
  1780. "1803 Bad hbq tag. Data: x%x x%x\n",
  1781. tag, phba->hbqs[tag >> 16].buffer_count);
  1782. return NULL;
  1783. }
  1784. /**
  1785. * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
  1786. * @phba: Pointer to HBA context object.
  1787. * @hbq_buffer: Pointer to HBQ buffer.
  1788. *
  1789. * This function is called with hbalock. This function gives back
  1790. * the hbq buffer to firmware. If the HBQ does not have space to
  1791. * post the buffer, it will free the buffer.
  1792. **/
  1793. void
  1794. lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
  1795. {
  1796. uint32_t hbqno;
  1797. if (hbq_buffer) {
  1798. hbqno = hbq_buffer->tag >> 16;
  1799. if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
  1800. (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
  1801. }
  1802. }
  1803. /**
  1804. * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
  1805. * @mbxCommand: mailbox command code.
  1806. *
  1807. * This function is called by the mailbox event handler function to verify
  1808. * that the completed mailbox command is a legitimate mailbox command. If the
  1809. * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
  1810. * and the mailbox event handler will take the HBA offline.
  1811. **/
  1812. static int
  1813. lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
  1814. {
  1815. uint8_t ret;
  1816. switch (mbxCommand) {
  1817. case MBX_LOAD_SM:
  1818. case MBX_READ_NV:
  1819. case MBX_WRITE_NV:
  1820. case MBX_WRITE_VPARMS:
  1821. case MBX_RUN_BIU_DIAG:
  1822. case MBX_INIT_LINK:
  1823. case MBX_DOWN_LINK:
  1824. case MBX_CONFIG_LINK:
  1825. case MBX_CONFIG_RING:
  1826. case MBX_RESET_RING:
  1827. case MBX_READ_CONFIG:
  1828. case MBX_READ_RCONFIG:
  1829. case MBX_READ_SPARM:
  1830. case MBX_READ_STATUS:
  1831. case MBX_READ_RPI:
  1832. case MBX_READ_XRI:
  1833. case MBX_READ_REV:
  1834. case MBX_READ_LNK_STAT:
  1835. case MBX_REG_LOGIN:
  1836. case MBX_UNREG_LOGIN:
  1837. case MBX_CLEAR_LA:
  1838. case MBX_DUMP_MEMORY:
  1839. case MBX_DUMP_CONTEXT:
  1840. case MBX_RUN_DIAGS:
  1841. case MBX_RESTART:
  1842. case MBX_UPDATE_CFG:
  1843. case MBX_DOWN_LOAD:
  1844. case MBX_DEL_LD_ENTRY:
  1845. case MBX_RUN_PROGRAM:
  1846. case MBX_SET_MASK:
  1847. case MBX_SET_VARIABLE:
  1848. case MBX_UNREG_D_ID:
  1849. case MBX_KILL_BOARD:
  1850. case MBX_CONFIG_FARP:
  1851. case MBX_BEACON:
  1852. case MBX_LOAD_AREA:
  1853. case MBX_RUN_BIU_DIAG64:
  1854. case MBX_CONFIG_PORT:
  1855. case MBX_READ_SPARM64:
  1856. case MBX_READ_RPI64:
  1857. case MBX_REG_LOGIN64:
  1858. case MBX_READ_TOPOLOGY:
  1859. case MBX_WRITE_WWN:
  1860. case MBX_SET_DEBUG:
  1861. case MBX_LOAD_EXP_ROM:
  1862. case MBX_ASYNCEVT_ENABLE:
  1863. case MBX_REG_VPI:
  1864. case MBX_UNREG_VPI:
  1865. case MBX_HEARTBEAT:
  1866. case MBX_PORT_CAPABILITIES:
  1867. case MBX_PORT_IOV_CONTROL:
  1868. case MBX_SLI4_CONFIG:
  1869. case MBX_SLI4_REQ_FTRS:
  1870. case MBX_REG_FCFI:
  1871. case MBX_UNREG_FCFI:
  1872. case MBX_REG_VFI:
  1873. case MBX_UNREG_VFI:
  1874. case MBX_INIT_VPI:
  1875. case MBX_INIT_VFI:
  1876. case MBX_RESUME_RPI:
  1877. case MBX_READ_EVENT_LOG_STATUS:
  1878. case MBX_READ_EVENT_LOG:
  1879. case MBX_SECURITY_MGMT:
  1880. case MBX_AUTH_PORT:
  1881. ret = mbxCommand;
  1882. break;
  1883. default:
  1884. ret = MBX_SHUTDOWN;
  1885. break;
  1886. }
  1887. return ret;
  1888. }
  1889. /**
  1890. * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
  1891. * @phba: Pointer to HBA context object.
  1892. * @pmboxq: Pointer to mailbox command.
  1893. *
  1894. * This is completion handler function for mailbox commands issued from
  1895. * lpfc_sli_issue_mbox_wait function. This function is called by the
  1896. * mailbox event handler function with no lock held. This function
  1897. * will wake up thread waiting on the wait queue pointed by context1
  1898. * of the mailbox.
  1899. **/
  1900. void
  1901. lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
  1902. {
  1903. wait_queue_head_t *pdone_q;
  1904. unsigned long drvr_flag;
  1905. /*
  1906. * If pdone_q is empty, the driver thread gave up waiting and
  1907. * continued running.
  1908. */
  1909. pmboxq->mbox_flag |= LPFC_MBX_WAKE;
  1910. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  1911. pdone_q = (wait_queue_head_t *) pmboxq->context1;
  1912. if (pdone_q)
  1913. wake_up_interruptible(pdone_q);
  1914. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  1915. return;
  1916. }
  1917. /**
  1918. * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
  1919. * @phba: Pointer to HBA context object.
  1920. * @pmb: Pointer to mailbox object.
  1921. *
  1922. * This function is the default mailbox completion handler. It
  1923. * frees the memory resources associated with the completed mailbox
  1924. * command. If the completed command is a REG_LOGIN mailbox command,
  1925. * this function will issue a UREG_LOGIN to re-claim the RPI.
  1926. **/
  1927. void
  1928. lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
  1929. {
  1930. struct lpfc_vport *vport = pmb->vport;
  1931. struct lpfc_dmabuf *mp;
  1932. struct lpfc_nodelist *ndlp;
  1933. struct Scsi_Host *shost;
  1934. uint16_t rpi, vpi;
  1935. int rc;
  1936. mp = (struct lpfc_dmabuf *) (pmb->context1);
  1937. if (mp) {
  1938. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  1939. kfree(mp);
  1940. }
  1941. /*
  1942. * If a REG_LOGIN succeeded after node is destroyed or node
  1943. * is in re-discovery driver need to cleanup the RPI.
  1944. */
  1945. if (!(phba->pport->load_flag & FC_UNLOADING) &&
  1946. pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
  1947. !pmb->u.mb.mbxStatus) {
  1948. rpi = pmb->u.mb.un.varWords[0];
  1949. vpi = pmb->u.mb.un.varRegLogin.vpi;
  1950. lpfc_unreg_login(phba, vpi, rpi, pmb);
  1951. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  1952. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  1953. if (rc != MBX_NOT_FINISHED)
  1954. return;
  1955. }
  1956. if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
  1957. !(phba->pport->load_flag & FC_UNLOADING) &&
  1958. !pmb->u.mb.mbxStatus) {
  1959. shost = lpfc_shost_from_vport(vport);
  1960. spin_lock_irq(shost->host_lock);
  1961. vport->vpi_state |= LPFC_VPI_REGISTERED;
  1962. vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
  1963. spin_unlock_irq(shost->host_lock);
  1964. }
  1965. if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  1966. ndlp = (struct lpfc_nodelist *)pmb->context2;
  1967. lpfc_nlp_put(ndlp);
  1968. pmb->context2 = NULL;
  1969. }
  1970. /* Check security permission status on INIT_LINK mailbox command */
  1971. if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
  1972. (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
  1973. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  1974. "2860 SLI authentication is required "
  1975. "for INIT_LINK but has not done yet\n");
  1976. if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
  1977. lpfc_sli4_mbox_cmd_free(phba, pmb);
  1978. else
  1979. mempool_free(pmb, phba->mbox_mem_pool);
  1980. }
  1981. /**
  1982. * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
  1983. * @phba: Pointer to HBA context object.
  1984. *
  1985. * This function is called with no lock held. This function processes all
  1986. * the completed mailbox commands and gives it to upper layers. The interrupt
  1987. * service routine processes mailbox completion interrupt and adds completed
  1988. * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
  1989. * Worker thread call lpfc_sli_handle_mb_event, which will return the
  1990. * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
  1991. * function returns the mailbox commands to the upper layer by calling the
  1992. * completion handler function of each mailbox.
  1993. **/
  1994. int
  1995. lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
  1996. {
  1997. MAILBOX_t *pmbox;
  1998. LPFC_MBOXQ_t *pmb;
  1999. int rc;
  2000. LIST_HEAD(cmplq);
  2001. phba->sli.slistat.mbox_event++;
  2002. /* Get all completed mailboxe buffers into the cmplq */
  2003. spin_lock_irq(&phba->hbalock);
  2004. list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
  2005. spin_unlock_irq(&phba->hbalock);
  2006. /* Get a Mailbox buffer to setup mailbox commands for callback */
  2007. do {
  2008. list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
  2009. if (pmb == NULL)
  2010. break;
  2011. pmbox = &pmb->u.mb;
  2012. if (pmbox->mbxCommand != MBX_HEARTBEAT) {
  2013. if (pmb->vport) {
  2014. lpfc_debugfs_disc_trc(pmb->vport,
  2015. LPFC_DISC_TRC_MBOX_VPORT,
  2016. "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
  2017. (uint32_t)pmbox->mbxCommand,
  2018. pmbox->un.varWords[0],
  2019. pmbox->un.varWords[1]);
  2020. }
  2021. else {
  2022. lpfc_debugfs_disc_trc(phba->pport,
  2023. LPFC_DISC_TRC_MBOX,
  2024. "MBOX cmpl: cmd:x%x mb:x%x x%x",
  2025. (uint32_t)pmbox->mbxCommand,
  2026. pmbox->un.varWords[0],
  2027. pmbox->un.varWords[1]);
  2028. }
  2029. }
  2030. /*
  2031. * It is a fatal error if unknown mbox command completion.
  2032. */
  2033. if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
  2034. MBX_SHUTDOWN) {
  2035. /* Unknown mailbox command compl */
  2036. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  2037. "(%d):0323 Unknown Mailbox command "
  2038. "x%x (x%x/x%x) Cmpl\n",
  2039. pmb->vport ? pmb->vport->vpi : 0,
  2040. pmbox->mbxCommand,
  2041. lpfc_sli_config_mbox_subsys_get(phba,
  2042. pmb),
  2043. lpfc_sli_config_mbox_opcode_get(phba,
  2044. pmb));
  2045. phba->link_state = LPFC_HBA_ERROR;
  2046. phba->work_hs = HS_FFER3;
  2047. lpfc_handle_eratt(phba);
  2048. continue;
  2049. }
  2050. if (pmbox->mbxStatus) {
  2051. phba->sli.slistat.mbox_stat_err++;
  2052. if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
  2053. /* Mbox cmd cmpl error - RETRYing */
  2054. lpfc_printf_log(phba, KERN_INFO,
  2055. LOG_MBOX | LOG_SLI,
  2056. "(%d):0305 Mbox cmd cmpl "
  2057. "error - RETRYing Data: x%x "
  2058. "(x%x/x%x) x%x x%x x%x\n",
  2059. pmb->vport ? pmb->vport->vpi : 0,
  2060. pmbox->mbxCommand,
  2061. lpfc_sli_config_mbox_subsys_get(phba,
  2062. pmb),
  2063. lpfc_sli_config_mbox_opcode_get(phba,
  2064. pmb),
  2065. pmbox->mbxStatus,
  2066. pmbox->un.varWords[0],
  2067. pmb->vport->port_state);
  2068. pmbox->mbxStatus = 0;
  2069. pmbox->mbxOwner = OWN_HOST;
  2070. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  2071. if (rc != MBX_NOT_FINISHED)
  2072. continue;
  2073. }
  2074. }
  2075. /* Mailbox cmd <cmd> Cmpl <cmpl> */
  2076. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  2077. "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
  2078. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2079. pmb->vport ? pmb->vport->vpi : 0,
  2080. pmbox->mbxCommand,
  2081. lpfc_sli_config_mbox_subsys_get(phba, pmb),
  2082. lpfc_sli_config_mbox_opcode_get(phba, pmb),
  2083. pmb->mbox_cmpl,
  2084. *((uint32_t *) pmbox),
  2085. pmbox->un.varWords[0],
  2086. pmbox->un.varWords[1],
  2087. pmbox->un.varWords[2],
  2088. pmbox->un.varWords[3],
  2089. pmbox->un.varWords[4],
  2090. pmbox->un.varWords[5],
  2091. pmbox->un.varWords[6],
  2092. pmbox->un.varWords[7]);
  2093. if (pmb->mbox_cmpl)
  2094. pmb->mbox_cmpl(phba,pmb);
  2095. } while (1);
  2096. return 0;
  2097. }
  2098. /**
  2099. * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
  2100. * @phba: Pointer to HBA context object.
  2101. * @pring: Pointer to driver SLI ring object.
  2102. * @tag: buffer tag.
  2103. *
  2104. * This function is called with no lock held. When QUE_BUFTAG_BIT bit
  2105. * is set in the tag the buffer is posted for a particular exchange,
  2106. * the function will return the buffer without replacing the buffer.
  2107. * If the buffer is for unsolicited ELS or CT traffic, this function
  2108. * returns the buffer and also posts another buffer to the firmware.
  2109. **/
  2110. static struct lpfc_dmabuf *
  2111. lpfc_sli_get_buff(struct lpfc_hba *phba,
  2112. struct lpfc_sli_ring *pring,
  2113. uint32_t tag)
  2114. {
  2115. struct hbq_dmabuf *hbq_entry;
  2116. if (tag & QUE_BUFTAG_BIT)
  2117. return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
  2118. hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
  2119. if (!hbq_entry)
  2120. return NULL;
  2121. return &hbq_entry->dbuf;
  2122. }
  2123. /**
  2124. * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
  2125. * @phba: Pointer to HBA context object.
  2126. * @pring: Pointer to driver SLI ring object.
  2127. * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
  2128. * @fch_r_ctl: the r_ctl for the first frame of the sequence.
  2129. * @fch_type: the type for the first frame of the sequence.
  2130. *
  2131. * This function is called with no lock held. This function uses the r_ctl and
  2132. * type of the received sequence to find the correct callback function to call
  2133. * to process the sequence.
  2134. **/
  2135. static int
  2136. lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2137. struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
  2138. uint32_t fch_type)
  2139. {
  2140. int i;
  2141. /* unSolicited Responses */
  2142. if (pring->prt[0].profile) {
  2143. if (pring->prt[0].lpfc_sli_rcv_unsol_event)
  2144. (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
  2145. saveq);
  2146. return 1;
  2147. }
  2148. /* We must search, based on rctl / type
  2149. for the right routine */
  2150. for (i = 0; i < pring->num_mask; i++) {
  2151. if ((pring->prt[i].rctl == fch_r_ctl) &&
  2152. (pring->prt[i].type == fch_type)) {
  2153. if (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2154. (pring->prt[i].lpfc_sli_rcv_unsol_event)
  2155. (phba, pring, saveq);
  2156. return 1;
  2157. }
  2158. }
  2159. return 0;
  2160. }
  2161. /**
  2162. * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
  2163. * @phba: Pointer to HBA context object.
  2164. * @pring: Pointer to driver SLI ring object.
  2165. * @saveq: Pointer to the unsolicited iocb.
  2166. *
  2167. * This function is called with no lock held by the ring event handler
  2168. * when there is an unsolicited iocb posted to the response ring by the
  2169. * firmware. This function gets the buffer associated with the iocbs
  2170. * and calls the event handler for the ring. This function handles both
  2171. * qring buffers and hbq buffers.
  2172. * When the function returns 1 the caller can free the iocb object otherwise
  2173. * upper layer functions will free the iocb objects.
  2174. **/
  2175. static int
  2176. lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2177. struct lpfc_iocbq *saveq)
  2178. {
  2179. IOCB_t * irsp;
  2180. WORD5 * w5p;
  2181. uint32_t Rctl, Type;
  2182. uint32_t match;
  2183. struct lpfc_iocbq *iocbq;
  2184. struct lpfc_dmabuf *dmzbuf;
  2185. match = 0;
  2186. irsp = &(saveq->iocb);
  2187. if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
  2188. if (pring->lpfc_sli_rcv_async_status)
  2189. pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
  2190. else
  2191. lpfc_printf_log(phba,
  2192. KERN_WARNING,
  2193. LOG_SLI,
  2194. "0316 Ring %d handler: unexpected "
  2195. "ASYNC_STATUS iocb received evt_code "
  2196. "0x%x\n",
  2197. pring->ringno,
  2198. irsp->un.asyncstat.evt_code);
  2199. return 1;
  2200. }
  2201. if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
  2202. (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
  2203. if (irsp->ulpBdeCount > 0) {
  2204. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2205. irsp->un.ulpWord[3]);
  2206. lpfc_in_buf_free(phba, dmzbuf);
  2207. }
  2208. if (irsp->ulpBdeCount > 1) {
  2209. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2210. irsp->unsli3.sli3Words[3]);
  2211. lpfc_in_buf_free(phba, dmzbuf);
  2212. }
  2213. if (irsp->ulpBdeCount > 2) {
  2214. dmzbuf = lpfc_sli_get_buff(phba, pring,
  2215. irsp->unsli3.sli3Words[7]);
  2216. lpfc_in_buf_free(phba, dmzbuf);
  2217. }
  2218. return 1;
  2219. }
  2220. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  2221. if (irsp->ulpBdeCount != 0) {
  2222. saveq->context2 = lpfc_sli_get_buff(phba, pring,
  2223. irsp->un.ulpWord[3]);
  2224. if (!saveq->context2)
  2225. lpfc_printf_log(phba,
  2226. KERN_ERR,
  2227. LOG_SLI,
  2228. "0341 Ring %d Cannot find buffer for "
  2229. "an unsolicited iocb. tag 0x%x\n",
  2230. pring->ringno,
  2231. irsp->un.ulpWord[3]);
  2232. }
  2233. if (irsp->ulpBdeCount == 2) {
  2234. saveq->context3 = lpfc_sli_get_buff(phba, pring,
  2235. irsp->unsli3.sli3Words[7]);
  2236. if (!saveq->context3)
  2237. lpfc_printf_log(phba,
  2238. KERN_ERR,
  2239. LOG_SLI,
  2240. "0342 Ring %d Cannot find buffer for an"
  2241. " unsolicited iocb. tag 0x%x\n",
  2242. pring->ringno,
  2243. irsp->unsli3.sli3Words[7]);
  2244. }
  2245. list_for_each_entry(iocbq, &saveq->list, list) {
  2246. irsp = &(iocbq->iocb);
  2247. if (irsp->ulpBdeCount != 0) {
  2248. iocbq->context2 = lpfc_sli_get_buff(phba, pring,
  2249. irsp->un.ulpWord[3]);
  2250. if (!iocbq->context2)
  2251. lpfc_printf_log(phba,
  2252. KERN_ERR,
  2253. LOG_SLI,
  2254. "0343 Ring %d Cannot find "
  2255. "buffer for an unsolicited iocb"
  2256. ". tag 0x%x\n", pring->ringno,
  2257. irsp->un.ulpWord[3]);
  2258. }
  2259. if (irsp->ulpBdeCount == 2) {
  2260. iocbq->context3 = lpfc_sli_get_buff(phba, pring,
  2261. irsp->unsli3.sli3Words[7]);
  2262. if (!iocbq->context3)
  2263. lpfc_printf_log(phba,
  2264. KERN_ERR,
  2265. LOG_SLI,
  2266. "0344 Ring %d Cannot find "
  2267. "buffer for an unsolicited "
  2268. "iocb. tag 0x%x\n",
  2269. pring->ringno,
  2270. irsp->unsli3.sli3Words[7]);
  2271. }
  2272. }
  2273. }
  2274. if (irsp->ulpBdeCount != 0 &&
  2275. (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
  2276. irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
  2277. int found = 0;
  2278. /* search continue save q for same XRI */
  2279. list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
  2280. if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
  2281. saveq->iocb.unsli3.rcvsli3.ox_id) {
  2282. list_add_tail(&saveq->list, &iocbq->list);
  2283. found = 1;
  2284. break;
  2285. }
  2286. }
  2287. if (!found)
  2288. list_add_tail(&saveq->clist,
  2289. &pring->iocb_continue_saveq);
  2290. if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
  2291. list_del_init(&iocbq->clist);
  2292. saveq = iocbq;
  2293. irsp = &(saveq->iocb);
  2294. } else
  2295. return 0;
  2296. }
  2297. if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
  2298. (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
  2299. (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
  2300. Rctl = FC_RCTL_ELS_REQ;
  2301. Type = FC_TYPE_ELS;
  2302. } else {
  2303. w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
  2304. Rctl = w5p->hcsw.Rctl;
  2305. Type = w5p->hcsw.Type;
  2306. /* Firmware Workaround */
  2307. if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
  2308. (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
  2309. irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
  2310. Rctl = FC_RCTL_ELS_REQ;
  2311. Type = FC_TYPE_ELS;
  2312. w5p->hcsw.Rctl = Rctl;
  2313. w5p->hcsw.Type = Type;
  2314. }
  2315. }
  2316. if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
  2317. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2318. "0313 Ring %d handler: unexpected Rctl x%x "
  2319. "Type x%x received\n",
  2320. pring->ringno, Rctl, Type);
  2321. return 1;
  2322. }
  2323. /**
  2324. * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
  2325. * @phba: Pointer to HBA context object.
  2326. * @pring: Pointer to driver SLI ring object.
  2327. * @prspiocb: Pointer to response iocb object.
  2328. *
  2329. * This function looks up the iocb_lookup table to get the command iocb
  2330. * corresponding to the given response iocb using the iotag of the
  2331. * response iocb. This function is called with the hbalock held.
  2332. * This function returns the command iocb object if it finds the command
  2333. * iocb else returns NULL.
  2334. **/
  2335. static struct lpfc_iocbq *
  2336. lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
  2337. struct lpfc_sli_ring *pring,
  2338. struct lpfc_iocbq *prspiocb)
  2339. {
  2340. struct lpfc_iocbq *cmd_iocb = NULL;
  2341. uint16_t iotag;
  2342. iotag = prspiocb->iocb.ulpIoTag;
  2343. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2344. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2345. list_del_init(&cmd_iocb->list);
  2346. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2347. pring->txcmplq_cnt--;
  2348. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2349. }
  2350. return cmd_iocb;
  2351. }
  2352. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2353. "0317 iotag x%x is out off "
  2354. "range: max iotag x%x wd0 x%x\n",
  2355. iotag, phba->sli.last_iotag,
  2356. *(((uint32_t *) &prspiocb->iocb) + 7));
  2357. return NULL;
  2358. }
  2359. /**
  2360. * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
  2361. * @phba: Pointer to HBA context object.
  2362. * @pring: Pointer to driver SLI ring object.
  2363. * @iotag: IOCB tag.
  2364. *
  2365. * This function looks up the iocb_lookup table to get the command iocb
  2366. * corresponding to the given iotag. This function is called with the
  2367. * hbalock held.
  2368. * This function returns the command iocb object if it finds the command
  2369. * iocb else returns NULL.
  2370. **/
  2371. static struct lpfc_iocbq *
  2372. lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
  2373. struct lpfc_sli_ring *pring, uint16_t iotag)
  2374. {
  2375. struct lpfc_iocbq *cmd_iocb;
  2376. if (iotag != 0 && iotag <= phba->sli.last_iotag) {
  2377. cmd_iocb = phba->sli.iocbq_lookup[iotag];
  2378. if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
  2379. /* remove from txcmpl queue list */
  2380. list_del_init(&cmd_iocb->list);
  2381. cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
  2382. pring->txcmplq_cnt--;
  2383. return cmd_iocb;
  2384. }
  2385. }
  2386. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2387. "0372 iotag x%x is out off range: max iotag (x%x)\n",
  2388. iotag, phba->sli.last_iotag);
  2389. return NULL;
  2390. }
  2391. /**
  2392. * lpfc_sli_process_sol_iocb - process solicited iocb completion
  2393. * @phba: Pointer to HBA context object.
  2394. * @pring: Pointer to driver SLI ring object.
  2395. * @saveq: Pointer to the response iocb to be processed.
  2396. *
  2397. * This function is called by the ring event handler for non-fcp
  2398. * rings when there is a new response iocb in the response ring.
  2399. * The caller is not required to hold any locks. This function
  2400. * gets the command iocb associated with the response iocb and
  2401. * calls the completion handler for the command iocb. If there
  2402. * is no completion handler, the function will free the resources
  2403. * associated with command iocb. If the response iocb is for
  2404. * an already aborted command iocb, the status of the completion
  2405. * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
  2406. * This function always returns 1.
  2407. **/
  2408. static int
  2409. lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2410. struct lpfc_iocbq *saveq)
  2411. {
  2412. struct lpfc_iocbq *cmdiocbp;
  2413. int rc = 1;
  2414. unsigned long iflag;
  2415. /* Based on the iotag field, get the cmd IOCB from the txcmplq */
  2416. spin_lock_irqsave(&phba->hbalock, iflag);
  2417. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
  2418. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2419. if (cmdiocbp) {
  2420. if (cmdiocbp->iocb_cmpl) {
  2421. /*
  2422. * If an ELS command failed send an event to mgmt
  2423. * application.
  2424. */
  2425. if (saveq->iocb.ulpStatus &&
  2426. (pring->ringno == LPFC_ELS_RING) &&
  2427. (cmdiocbp->iocb.ulpCommand ==
  2428. CMD_ELS_REQUEST64_CR))
  2429. lpfc_send_els_failure_event(phba,
  2430. cmdiocbp, saveq);
  2431. /*
  2432. * Post all ELS completions to the worker thread.
  2433. * All other are passed to the completion callback.
  2434. */
  2435. if (pring->ringno == LPFC_ELS_RING) {
  2436. if ((phba->sli_rev < LPFC_SLI_REV4) &&
  2437. (cmdiocbp->iocb_flag &
  2438. LPFC_DRIVER_ABORTED)) {
  2439. spin_lock_irqsave(&phba->hbalock,
  2440. iflag);
  2441. cmdiocbp->iocb_flag &=
  2442. ~LPFC_DRIVER_ABORTED;
  2443. spin_unlock_irqrestore(&phba->hbalock,
  2444. iflag);
  2445. saveq->iocb.ulpStatus =
  2446. IOSTAT_LOCAL_REJECT;
  2447. saveq->iocb.un.ulpWord[4] =
  2448. IOERR_SLI_ABORTED;
  2449. /* Firmware could still be in progress
  2450. * of DMAing payload, so don't free data
  2451. * buffer till after a hbeat.
  2452. */
  2453. spin_lock_irqsave(&phba->hbalock,
  2454. iflag);
  2455. saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
  2456. spin_unlock_irqrestore(&phba->hbalock,
  2457. iflag);
  2458. }
  2459. if (phba->sli_rev == LPFC_SLI_REV4) {
  2460. if (saveq->iocb_flag &
  2461. LPFC_EXCHANGE_BUSY) {
  2462. /* Set cmdiocb flag for the
  2463. * exchange busy so sgl (xri)
  2464. * will not be released until
  2465. * the abort xri is received
  2466. * from hba.
  2467. */
  2468. spin_lock_irqsave(
  2469. &phba->hbalock, iflag);
  2470. cmdiocbp->iocb_flag |=
  2471. LPFC_EXCHANGE_BUSY;
  2472. spin_unlock_irqrestore(
  2473. &phba->hbalock, iflag);
  2474. }
  2475. if (cmdiocbp->iocb_flag &
  2476. LPFC_DRIVER_ABORTED) {
  2477. /*
  2478. * Clear LPFC_DRIVER_ABORTED
  2479. * bit in case it was driver
  2480. * initiated abort.
  2481. */
  2482. spin_lock_irqsave(
  2483. &phba->hbalock, iflag);
  2484. cmdiocbp->iocb_flag &=
  2485. ~LPFC_DRIVER_ABORTED;
  2486. spin_unlock_irqrestore(
  2487. &phba->hbalock, iflag);
  2488. cmdiocbp->iocb.ulpStatus =
  2489. IOSTAT_LOCAL_REJECT;
  2490. cmdiocbp->iocb.un.ulpWord[4] =
  2491. IOERR_ABORT_REQUESTED;
  2492. /*
  2493. * For SLI4, irsiocb contains
  2494. * NO_XRI in sli_xritag, it
  2495. * shall not affect releasing
  2496. * sgl (xri) process.
  2497. */
  2498. saveq->iocb.ulpStatus =
  2499. IOSTAT_LOCAL_REJECT;
  2500. saveq->iocb.un.ulpWord[4] =
  2501. IOERR_SLI_ABORTED;
  2502. spin_lock_irqsave(
  2503. &phba->hbalock, iflag);
  2504. saveq->iocb_flag |=
  2505. LPFC_DELAY_MEM_FREE;
  2506. spin_unlock_irqrestore(
  2507. &phba->hbalock, iflag);
  2508. }
  2509. }
  2510. }
  2511. (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
  2512. } else
  2513. lpfc_sli_release_iocbq(phba, cmdiocbp);
  2514. } else {
  2515. /*
  2516. * Unknown initiating command based on the response iotag.
  2517. * This could be the case on the ELS ring because of
  2518. * lpfc_els_abort().
  2519. */
  2520. if (pring->ringno != LPFC_ELS_RING) {
  2521. /*
  2522. * Ring <ringno> handler: unexpected completion IoTag
  2523. * <IoTag>
  2524. */
  2525. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2526. "0322 Ring %d handler: "
  2527. "unexpected completion IoTag x%x "
  2528. "Data: x%x x%x x%x x%x\n",
  2529. pring->ringno,
  2530. saveq->iocb.ulpIoTag,
  2531. saveq->iocb.ulpStatus,
  2532. saveq->iocb.un.ulpWord[4],
  2533. saveq->iocb.ulpCommand,
  2534. saveq->iocb.ulpContext);
  2535. }
  2536. }
  2537. return rc;
  2538. }
  2539. /**
  2540. * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
  2541. * @phba: Pointer to HBA context object.
  2542. * @pring: Pointer to driver SLI ring object.
  2543. *
  2544. * This function is called from the iocb ring event handlers when
  2545. * put pointer is ahead of the get pointer for a ring. This function signal
  2546. * an error attention condition to the worker thread and the worker
  2547. * thread will transition the HBA to offline state.
  2548. **/
  2549. static void
  2550. lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  2551. {
  2552. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2553. /*
  2554. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2555. * rsp ring <portRspMax>
  2556. */
  2557. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2558. "0312 Ring %d handler: portRspPut %d "
  2559. "is bigger than rsp ring %d\n",
  2560. pring->ringno, le32_to_cpu(pgp->rspPutInx),
  2561. pring->numRiocb);
  2562. phba->link_state = LPFC_HBA_ERROR;
  2563. /*
  2564. * All error attention handlers are posted to
  2565. * worker thread
  2566. */
  2567. phba->work_ha |= HA_ERATT;
  2568. phba->work_hs = HS_FFER3;
  2569. lpfc_worker_wake_up(phba);
  2570. return;
  2571. }
  2572. /**
  2573. * lpfc_poll_eratt - Error attention polling timer timeout handler
  2574. * @ptr: Pointer to address of HBA context object.
  2575. *
  2576. * This function is invoked by the Error Attention polling timer when the
  2577. * timer times out. It will check the SLI Error Attention register for
  2578. * possible attention events. If so, it will post an Error Attention event
  2579. * and wake up worker thread to process it. Otherwise, it will set up the
  2580. * Error Attention polling timer for the next poll.
  2581. **/
  2582. void lpfc_poll_eratt(unsigned long ptr)
  2583. {
  2584. struct lpfc_hba *phba;
  2585. uint32_t eratt = 0;
  2586. phba = (struct lpfc_hba *)ptr;
  2587. /* Check chip HA register for error event */
  2588. eratt = lpfc_sli_check_eratt(phba);
  2589. if (eratt)
  2590. /* Tell the worker thread there is work to do */
  2591. lpfc_worker_wake_up(phba);
  2592. else
  2593. /* Restart the timer for next eratt poll */
  2594. mod_timer(&phba->eratt_poll, jiffies +
  2595. HZ * LPFC_ERATT_POLL_INTERVAL);
  2596. return;
  2597. }
  2598. /**
  2599. * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
  2600. * @phba: Pointer to HBA context object.
  2601. * @pring: Pointer to driver SLI ring object.
  2602. * @mask: Host attention register mask for this ring.
  2603. *
  2604. * This function is called from the interrupt context when there is a ring
  2605. * event for the fcp ring. The caller does not hold any lock.
  2606. * The function processes each response iocb in the response ring until it
  2607. * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
  2608. * LE bit set. The function will call the completion handler of the command iocb
  2609. * if the response iocb indicates a completion for a command iocb or it is
  2610. * an abort completion. The function will call lpfc_sli_process_unsol_iocb
  2611. * function if this is an unsolicited iocb.
  2612. * This routine presumes LPFC_FCP_RING handling and doesn't bother
  2613. * to check it explicitly.
  2614. */
  2615. int
  2616. lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
  2617. struct lpfc_sli_ring *pring, uint32_t mask)
  2618. {
  2619. struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
  2620. IOCB_t *irsp = NULL;
  2621. IOCB_t *entry = NULL;
  2622. struct lpfc_iocbq *cmdiocbq = NULL;
  2623. struct lpfc_iocbq rspiocbq;
  2624. uint32_t status;
  2625. uint32_t portRspPut, portRspMax;
  2626. int rc = 1;
  2627. lpfc_iocb_type type;
  2628. unsigned long iflag;
  2629. uint32_t rsp_cmpl = 0;
  2630. spin_lock_irqsave(&phba->hbalock, iflag);
  2631. pring->stats.iocb_event++;
  2632. /*
  2633. * The next available response entry should never exceed the maximum
  2634. * entries. If it does, treat it as an adapter hardware error.
  2635. */
  2636. portRspMax = pring->numRiocb;
  2637. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2638. if (unlikely(portRspPut >= portRspMax)) {
  2639. lpfc_sli_rsp_pointers_error(phba, pring);
  2640. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2641. return 1;
  2642. }
  2643. if (phba->fcp_ring_in_use) {
  2644. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2645. return 1;
  2646. } else
  2647. phba->fcp_ring_in_use = 1;
  2648. rmb();
  2649. while (pring->rspidx != portRspPut) {
  2650. /*
  2651. * Fetch an entry off the ring and copy it into a local data
  2652. * structure. The copy involves a byte-swap since the
  2653. * network byte order and pci byte orders are different.
  2654. */
  2655. entry = lpfc_resp_iocb(phba, pring);
  2656. phba->last_completion_time = jiffies;
  2657. if (++pring->rspidx >= portRspMax)
  2658. pring->rspidx = 0;
  2659. lpfc_sli_pcimem_bcopy((uint32_t *) entry,
  2660. (uint32_t *) &rspiocbq.iocb,
  2661. phba->iocb_rsp_size);
  2662. INIT_LIST_HEAD(&(rspiocbq.list));
  2663. irsp = &rspiocbq.iocb;
  2664. type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
  2665. pring->stats.iocb_rsp++;
  2666. rsp_cmpl++;
  2667. if (unlikely(irsp->ulpStatus)) {
  2668. /*
  2669. * If resource errors reported from HBA, reduce
  2670. * queuedepths of the SCSI device.
  2671. */
  2672. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2673. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2674. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2675. phba->lpfc_rampdown_queue_depth(phba);
  2676. spin_lock_irqsave(&phba->hbalock, iflag);
  2677. }
  2678. /* Rsp ring <ringno> error: IOCB */
  2679. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2680. "0336 Rsp Ring %d error: IOCB Data: "
  2681. "x%x x%x x%x x%x x%x x%x x%x x%x\n",
  2682. pring->ringno,
  2683. irsp->un.ulpWord[0],
  2684. irsp->un.ulpWord[1],
  2685. irsp->un.ulpWord[2],
  2686. irsp->un.ulpWord[3],
  2687. irsp->un.ulpWord[4],
  2688. irsp->un.ulpWord[5],
  2689. *(uint32_t *)&irsp->un1,
  2690. *((uint32_t *)&irsp->un1 + 1));
  2691. }
  2692. switch (type) {
  2693. case LPFC_ABORT_IOCB:
  2694. case LPFC_SOL_IOCB:
  2695. /*
  2696. * Idle exchange closed via ABTS from port. No iocb
  2697. * resources need to be recovered.
  2698. */
  2699. if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
  2700. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  2701. "0333 IOCB cmd 0x%x"
  2702. " processed. Skipping"
  2703. " completion\n",
  2704. irsp->ulpCommand);
  2705. break;
  2706. }
  2707. cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
  2708. &rspiocbq);
  2709. if (unlikely(!cmdiocbq))
  2710. break;
  2711. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
  2712. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  2713. if (cmdiocbq->iocb_cmpl) {
  2714. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2715. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
  2716. &rspiocbq);
  2717. spin_lock_irqsave(&phba->hbalock, iflag);
  2718. }
  2719. break;
  2720. case LPFC_UNSOL_IOCB:
  2721. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2722. lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
  2723. spin_lock_irqsave(&phba->hbalock, iflag);
  2724. break;
  2725. default:
  2726. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2727. char adaptermsg[LPFC_MAX_ADPTMSG];
  2728. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2729. memcpy(&adaptermsg[0], (uint8_t *) irsp,
  2730. MAX_MSG_DATA);
  2731. dev_warn(&((phba->pcidev)->dev),
  2732. "lpfc%d: %s\n",
  2733. phba->brd_no, adaptermsg);
  2734. } else {
  2735. /* Unknown IOCB command */
  2736. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2737. "0334 Unknown IOCB command "
  2738. "Data: x%x, x%x x%x x%x x%x\n",
  2739. type, irsp->ulpCommand,
  2740. irsp->ulpStatus,
  2741. irsp->ulpIoTag,
  2742. irsp->ulpContext);
  2743. }
  2744. break;
  2745. }
  2746. /*
  2747. * The response IOCB has been processed. Update the ring
  2748. * pointer in SLIM. If the port response put pointer has not
  2749. * been updated, sync the pgp->rspPutInx and fetch the new port
  2750. * response put pointer.
  2751. */
  2752. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  2753. if (pring->rspidx == portRspPut)
  2754. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2755. }
  2756. if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
  2757. pring->stats.iocb_rsp_full++;
  2758. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  2759. writel(status, phba->CAregaddr);
  2760. readl(phba->CAregaddr);
  2761. }
  2762. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  2763. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  2764. pring->stats.iocb_cmd_empty++;
  2765. /* Force update of the local copy of cmdGetInx */
  2766. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  2767. lpfc_sli_resume_iocb(phba, pring);
  2768. if ((pring->lpfc_sli_cmd_available))
  2769. (pring->lpfc_sli_cmd_available) (phba, pring);
  2770. }
  2771. phba->fcp_ring_in_use = 0;
  2772. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2773. return rc;
  2774. }
  2775. /**
  2776. * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
  2777. * @phba: Pointer to HBA context object.
  2778. * @pring: Pointer to driver SLI ring object.
  2779. * @rspiocbp: Pointer to driver response IOCB object.
  2780. *
  2781. * This function is called from the worker thread when there is a slow-path
  2782. * response IOCB to process. This function chains all the response iocbs until
  2783. * seeing the iocb with the LE bit set. The function will call
  2784. * lpfc_sli_process_sol_iocb function if the response iocb indicates a
  2785. * completion of a command iocb. The function will call the
  2786. * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
  2787. * The function frees the resources or calls the completion handler if this
  2788. * iocb is an abort completion. The function returns NULL when the response
  2789. * iocb has the LE bit set and all the chained iocbs are processed, otherwise
  2790. * this function shall chain the iocb on to the iocb_continueq and return the
  2791. * response iocb passed in.
  2792. **/
  2793. static struct lpfc_iocbq *
  2794. lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  2795. struct lpfc_iocbq *rspiocbp)
  2796. {
  2797. struct lpfc_iocbq *saveq;
  2798. struct lpfc_iocbq *cmdiocbp;
  2799. struct lpfc_iocbq *next_iocb;
  2800. IOCB_t *irsp = NULL;
  2801. uint32_t free_saveq;
  2802. uint8_t iocb_cmd_type;
  2803. lpfc_iocb_type type;
  2804. unsigned long iflag;
  2805. int rc;
  2806. spin_lock_irqsave(&phba->hbalock, iflag);
  2807. /* First add the response iocb to the countinueq list */
  2808. list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
  2809. pring->iocb_continueq_cnt++;
  2810. /* Now, determine whether the list is completed for processing */
  2811. irsp = &rspiocbp->iocb;
  2812. if (irsp->ulpLe) {
  2813. /*
  2814. * By default, the driver expects to free all resources
  2815. * associated with this iocb completion.
  2816. */
  2817. free_saveq = 1;
  2818. saveq = list_get_first(&pring->iocb_continueq,
  2819. struct lpfc_iocbq, list);
  2820. irsp = &(saveq->iocb);
  2821. list_del_init(&pring->iocb_continueq);
  2822. pring->iocb_continueq_cnt = 0;
  2823. pring->stats.iocb_rsp++;
  2824. /*
  2825. * If resource errors reported from HBA, reduce
  2826. * queuedepths of the SCSI device.
  2827. */
  2828. if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
  2829. (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
  2830. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2831. phba->lpfc_rampdown_queue_depth(phba);
  2832. spin_lock_irqsave(&phba->hbalock, iflag);
  2833. }
  2834. if (irsp->ulpStatus) {
  2835. /* Rsp ring <ringno> error: IOCB */
  2836. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  2837. "0328 Rsp Ring %d error: "
  2838. "IOCB Data: "
  2839. "x%x x%x x%x x%x "
  2840. "x%x x%x x%x x%x "
  2841. "x%x x%x x%x x%x "
  2842. "x%x x%x x%x x%x\n",
  2843. pring->ringno,
  2844. irsp->un.ulpWord[0],
  2845. irsp->un.ulpWord[1],
  2846. irsp->un.ulpWord[2],
  2847. irsp->un.ulpWord[3],
  2848. irsp->un.ulpWord[4],
  2849. irsp->un.ulpWord[5],
  2850. *(((uint32_t *) irsp) + 6),
  2851. *(((uint32_t *) irsp) + 7),
  2852. *(((uint32_t *) irsp) + 8),
  2853. *(((uint32_t *) irsp) + 9),
  2854. *(((uint32_t *) irsp) + 10),
  2855. *(((uint32_t *) irsp) + 11),
  2856. *(((uint32_t *) irsp) + 12),
  2857. *(((uint32_t *) irsp) + 13),
  2858. *(((uint32_t *) irsp) + 14),
  2859. *(((uint32_t *) irsp) + 15));
  2860. }
  2861. /*
  2862. * Fetch the IOCB command type and call the correct completion
  2863. * routine. Solicited and Unsolicited IOCBs on the ELS ring
  2864. * get freed back to the lpfc_iocb_list by the discovery
  2865. * kernel thread.
  2866. */
  2867. iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
  2868. type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
  2869. switch (type) {
  2870. case LPFC_SOL_IOCB:
  2871. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2872. rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
  2873. spin_lock_irqsave(&phba->hbalock, iflag);
  2874. break;
  2875. case LPFC_UNSOL_IOCB:
  2876. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2877. rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
  2878. spin_lock_irqsave(&phba->hbalock, iflag);
  2879. if (!rc)
  2880. free_saveq = 0;
  2881. break;
  2882. case LPFC_ABORT_IOCB:
  2883. cmdiocbp = NULL;
  2884. if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
  2885. cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
  2886. saveq);
  2887. if (cmdiocbp) {
  2888. /* Call the specified completion routine */
  2889. if (cmdiocbp->iocb_cmpl) {
  2890. spin_unlock_irqrestore(&phba->hbalock,
  2891. iflag);
  2892. (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
  2893. saveq);
  2894. spin_lock_irqsave(&phba->hbalock,
  2895. iflag);
  2896. } else
  2897. __lpfc_sli_release_iocbq(phba,
  2898. cmdiocbp);
  2899. }
  2900. break;
  2901. case LPFC_UNKNOWN_IOCB:
  2902. if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
  2903. char adaptermsg[LPFC_MAX_ADPTMSG];
  2904. memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
  2905. memcpy(&adaptermsg[0], (uint8_t *)irsp,
  2906. MAX_MSG_DATA);
  2907. dev_warn(&((phba->pcidev)->dev),
  2908. "lpfc%d: %s\n",
  2909. phba->brd_no, adaptermsg);
  2910. } else {
  2911. /* Unknown IOCB command */
  2912. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2913. "0335 Unknown IOCB "
  2914. "command Data: x%x "
  2915. "x%x x%x x%x\n",
  2916. irsp->ulpCommand,
  2917. irsp->ulpStatus,
  2918. irsp->ulpIoTag,
  2919. irsp->ulpContext);
  2920. }
  2921. break;
  2922. }
  2923. if (free_saveq) {
  2924. list_for_each_entry_safe(rspiocbp, next_iocb,
  2925. &saveq->list, list) {
  2926. list_del(&rspiocbp->list);
  2927. __lpfc_sli_release_iocbq(phba, rspiocbp);
  2928. }
  2929. __lpfc_sli_release_iocbq(phba, saveq);
  2930. }
  2931. rspiocbp = NULL;
  2932. }
  2933. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2934. return rspiocbp;
  2935. }
  2936. /**
  2937. * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
  2938. * @phba: Pointer to HBA context object.
  2939. * @pring: Pointer to driver SLI ring object.
  2940. * @mask: Host attention register mask for this ring.
  2941. *
  2942. * This routine wraps the actual slow_ring event process routine from the
  2943. * API jump table function pointer from the lpfc_hba struct.
  2944. **/
  2945. void
  2946. lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
  2947. struct lpfc_sli_ring *pring, uint32_t mask)
  2948. {
  2949. phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
  2950. }
  2951. /**
  2952. * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
  2953. * @phba: Pointer to HBA context object.
  2954. * @pring: Pointer to driver SLI ring object.
  2955. * @mask: Host attention register mask for this ring.
  2956. *
  2957. * This function is called from the worker thread when there is a ring event
  2958. * for non-fcp rings. The caller does not hold any lock. The function will
  2959. * remove each response iocb in the response ring and calls the handle
  2960. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  2961. **/
  2962. static void
  2963. lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
  2964. struct lpfc_sli_ring *pring, uint32_t mask)
  2965. {
  2966. struct lpfc_pgp *pgp;
  2967. IOCB_t *entry;
  2968. IOCB_t *irsp = NULL;
  2969. struct lpfc_iocbq *rspiocbp = NULL;
  2970. uint32_t portRspPut, portRspMax;
  2971. unsigned long iflag;
  2972. uint32_t status;
  2973. pgp = &phba->port_gp[pring->ringno];
  2974. spin_lock_irqsave(&phba->hbalock, iflag);
  2975. pring->stats.iocb_event++;
  2976. /*
  2977. * The next available response entry should never exceed the maximum
  2978. * entries. If it does, treat it as an adapter hardware error.
  2979. */
  2980. portRspMax = pring->numRiocb;
  2981. portRspPut = le32_to_cpu(pgp->rspPutInx);
  2982. if (portRspPut >= portRspMax) {
  2983. /*
  2984. * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
  2985. * rsp ring <portRspMax>
  2986. */
  2987. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  2988. "0303 Ring %d handler: portRspPut %d "
  2989. "is bigger than rsp ring %d\n",
  2990. pring->ringno, portRspPut, portRspMax);
  2991. phba->link_state = LPFC_HBA_ERROR;
  2992. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2993. phba->work_hs = HS_FFER3;
  2994. lpfc_handle_eratt(phba);
  2995. return;
  2996. }
  2997. rmb();
  2998. while (pring->rspidx != portRspPut) {
  2999. /*
  3000. * Build a completion list and call the appropriate handler.
  3001. * The process is to get the next available response iocb, get
  3002. * a free iocb from the list, copy the response data into the
  3003. * free iocb, insert to the continuation list, and update the
  3004. * next response index to slim. This process makes response
  3005. * iocb's in the ring available to DMA as fast as possible but
  3006. * pays a penalty for a copy operation. Since the iocb is
  3007. * only 32 bytes, this penalty is considered small relative to
  3008. * the PCI reads for register values and a slim write. When
  3009. * the ulpLe field is set, the entire Command has been
  3010. * received.
  3011. */
  3012. entry = lpfc_resp_iocb(phba, pring);
  3013. phba->last_completion_time = jiffies;
  3014. rspiocbp = __lpfc_sli_get_iocbq(phba);
  3015. if (rspiocbp == NULL) {
  3016. printk(KERN_ERR "%s: out of buffers! Failing "
  3017. "completion.\n", __func__);
  3018. break;
  3019. }
  3020. lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
  3021. phba->iocb_rsp_size);
  3022. irsp = &rspiocbp->iocb;
  3023. if (++pring->rspidx >= portRspMax)
  3024. pring->rspidx = 0;
  3025. if (pring->ringno == LPFC_ELS_RING) {
  3026. lpfc_debugfs_slow_ring_trc(phba,
  3027. "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
  3028. *(((uint32_t *) irsp) + 4),
  3029. *(((uint32_t *) irsp) + 6),
  3030. *(((uint32_t *) irsp) + 7));
  3031. }
  3032. writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
  3033. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3034. /* Handle the response IOCB */
  3035. rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
  3036. spin_lock_irqsave(&phba->hbalock, iflag);
  3037. /*
  3038. * If the port response put pointer has not been updated, sync
  3039. * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
  3040. * response put pointer.
  3041. */
  3042. if (pring->rspidx == portRspPut) {
  3043. portRspPut = le32_to_cpu(pgp->rspPutInx);
  3044. }
  3045. } /* while (pring->rspidx != portRspPut) */
  3046. if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
  3047. /* At least one response entry has been freed */
  3048. pring->stats.iocb_rsp_full++;
  3049. /* SET RxRE_RSP in Chip Att register */
  3050. status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
  3051. writel(status, phba->CAregaddr);
  3052. readl(phba->CAregaddr); /* flush */
  3053. }
  3054. if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
  3055. pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
  3056. pring->stats.iocb_cmd_empty++;
  3057. /* Force update of the local copy of cmdGetInx */
  3058. pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
  3059. lpfc_sli_resume_iocb(phba, pring);
  3060. if ((pring->lpfc_sli_cmd_available))
  3061. (pring->lpfc_sli_cmd_available) (phba, pring);
  3062. }
  3063. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3064. return;
  3065. }
  3066. /**
  3067. * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
  3068. * @phba: Pointer to HBA context object.
  3069. * @pring: Pointer to driver SLI ring object.
  3070. * @mask: Host attention register mask for this ring.
  3071. *
  3072. * This function is called from the worker thread when there is a pending
  3073. * ELS response iocb on the driver internal slow-path response iocb worker
  3074. * queue. The caller does not hold any lock. The function will remove each
  3075. * response iocb from the response worker queue and calls the handle
  3076. * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
  3077. **/
  3078. static void
  3079. lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
  3080. struct lpfc_sli_ring *pring, uint32_t mask)
  3081. {
  3082. struct lpfc_iocbq *irspiocbq;
  3083. struct hbq_dmabuf *dmabuf;
  3084. struct lpfc_cq_event *cq_event;
  3085. unsigned long iflag;
  3086. spin_lock_irqsave(&phba->hbalock, iflag);
  3087. phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
  3088. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3089. while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
  3090. /* Get the response iocb from the head of work queue */
  3091. spin_lock_irqsave(&phba->hbalock, iflag);
  3092. list_remove_head(&phba->sli4_hba.sp_queue_event,
  3093. cq_event, struct lpfc_cq_event, list);
  3094. spin_unlock_irqrestore(&phba->hbalock, iflag);
  3095. switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
  3096. case CQE_CODE_COMPL_WQE:
  3097. irspiocbq = container_of(cq_event, struct lpfc_iocbq,
  3098. cq_event);
  3099. /* Translate ELS WCQE to response IOCBQ */
  3100. irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
  3101. irspiocbq);
  3102. if (irspiocbq)
  3103. lpfc_sli_sp_handle_rspiocb(phba, pring,
  3104. irspiocbq);
  3105. break;
  3106. case CQE_CODE_RECEIVE:
  3107. case CQE_CODE_RECEIVE_V1:
  3108. dmabuf = container_of(cq_event, struct hbq_dmabuf,
  3109. cq_event);
  3110. lpfc_sli4_handle_received_buffer(phba, dmabuf);
  3111. break;
  3112. default:
  3113. break;
  3114. }
  3115. }
  3116. }
  3117. /**
  3118. * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
  3119. * @phba: Pointer to HBA context object.
  3120. * @pring: Pointer to driver SLI ring object.
  3121. *
  3122. * This function aborts all iocbs in the given ring and frees all the iocb
  3123. * objects in txq. This function issues an abort iocb for all the iocb commands
  3124. * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
  3125. * the return of this function. The caller is not required to hold any locks.
  3126. **/
  3127. void
  3128. lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  3129. {
  3130. LIST_HEAD(completions);
  3131. struct lpfc_iocbq *iocb, *next_iocb;
  3132. if (pring->ringno == LPFC_ELS_RING) {
  3133. lpfc_fabric_abort_hba(phba);
  3134. }
  3135. /* Error everything on txq and txcmplq
  3136. * First do the txq.
  3137. */
  3138. spin_lock_irq(&phba->hbalock);
  3139. list_splice_init(&pring->txq, &completions);
  3140. pring->txq_cnt = 0;
  3141. /* Next issue ABTS for everything on the txcmplq */
  3142. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  3143. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  3144. spin_unlock_irq(&phba->hbalock);
  3145. /* Cancel all the IOCBs from the completions list */
  3146. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  3147. IOERR_SLI_ABORTED);
  3148. }
  3149. /**
  3150. * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
  3151. * @phba: Pointer to HBA context object.
  3152. *
  3153. * This function flushes all iocbs in the fcp ring and frees all the iocb
  3154. * objects in txq and txcmplq. This function will not issue abort iocbs
  3155. * for all the iocb commands in txcmplq, they will just be returned with
  3156. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  3157. * slot has been permanently disabled.
  3158. **/
  3159. void
  3160. lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
  3161. {
  3162. LIST_HEAD(txq);
  3163. LIST_HEAD(txcmplq);
  3164. struct lpfc_sli *psli = &phba->sli;
  3165. struct lpfc_sli_ring *pring;
  3166. /* Currently, only one fcp ring */
  3167. pring = &psli->ring[psli->fcp_ring];
  3168. spin_lock_irq(&phba->hbalock);
  3169. /* Retrieve everything on txq */
  3170. list_splice_init(&pring->txq, &txq);
  3171. pring->txq_cnt = 0;
  3172. /* Retrieve everything on the txcmplq */
  3173. list_splice_init(&pring->txcmplq, &txcmplq);
  3174. pring->txcmplq_cnt = 0;
  3175. /* Indicate the I/O queues are flushed */
  3176. phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
  3177. spin_unlock_irq(&phba->hbalock);
  3178. /* Flush the txq */
  3179. lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
  3180. IOERR_SLI_DOWN);
  3181. /* Flush the txcmpq */
  3182. lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
  3183. IOERR_SLI_DOWN);
  3184. }
  3185. /**
  3186. * lpfc_sli_brdready_s3 - Check for sli3 host ready status
  3187. * @phba: Pointer to HBA context object.
  3188. * @mask: Bit mask to be checked.
  3189. *
  3190. * This function reads the host status register and compares
  3191. * with the provided bit mask to check if HBA completed
  3192. * the restart. This function will wait in a loop for the
  3193. * HBA to complete restart. If the HBA does not restart within
  3194. * 15 iterations, the function will reset the HBA again. The
  3195. * function returns 1 when HBA fail to restart otherwise returns
  3196. * zero.
  3197. **/
  3198. static int
  3199. lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
  3200. {
  3201. uint32_t status;
  3202. int i = 0;
  3203. int retval = 0;
  3204. /* Read the HBA Host Status Register */
  3205. if (lpfc_readl(phba->HSregaddr, &status))
  3206. return 1;
  3207. /*
  3208. * Check status register every 100ms for 5 retries, then every
  3209. * 500ms for 5, then every 2.5 sec for 5, then reset board and
  3210. * every 2.5 sec for 4.
  3211. * Break our of the loop if errors occurred during init.
  3212. */
  3213. while (((status & mask) != mask) &&
  3214. !(status & HS_FFERM) &&
  3215. i++ < 20) {
  3216. if (i <= 5)
  3217. msleep(10);
  3218. else if (i <= 10)
  3219. msleep(500);
  3220. else
  3221. msleep(2500);
  3222. if (i == 15) {
  3223. /* Do post */
  3224. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3225. lpfc_sli_brdrestart(phba);
  3226. }
  3227. /* Read the HBA Host Status Register */
  3228. if (lpfc_readl(phba->HSregaddr, &status)) {
  3229. retval = 1;
  3230. break;
  3231. }
  3232. }
  3233. /* Check to see if any errors occurred during init */
  3234. if ((status & HS_FFERM) || (i >= 20)) {
  3235. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3236. "2751 Adapter failed to restart, "
  3237. "status reg x%x, FW Data: A8 x%x AC x%x\n",
  3238. status,
  3239. readl(phba->MBslimaddr + 0xa8),
  3240. readl(phba->MBslimaddr + 0xac));
  3241. phba->link_state = LPFC_HBA_ERROR;
  3242. retval = 1;
  3243. }
  3244. return retval;
  3245. }
  3246. /**
  3247. * lpfc_sli_brdready_s4 - Check for sli4 host ready status
  3248. * @phba: Pointer to HBA context object.
  3249. * @mask: Bit mask to be checked.
  3250. *
  3251. * This function checks the host status register to check if HBA is
  3252. * ready. This function will wait in a loop for the HBA to be ready
  3253. * If the HBA is not ready , the function will will reset the HBA PCI
  3254. * function again. The function returns 1 when HBA fail to be ready
  3255. * otherwise returns zero.
  3256. **/
  3257. static int
  3258. lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
  3259. {
  3260. uint32_t status;
  3261. int retval = 0;
  3262. /* Read the HBA Host Status Register */
  3263. status = lpfc_sli4_post_status_check(phba);
  3264. if (status) {
  3265. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3266. lpfc_sli_brdrestart(phba);
  3267. status = lpfc_sli4_post_status_check(phba);
  3268. }
  3269. /* Check to see if any errors occurred during init */
  3270. if (status) {
  3271. phba->link_state = LPFC_HBA_ERROR;
  3272. retval = 1;
  3273. } else
  3274. phba->sli4_hba.intr_enable = 0;
  3275. return retval;
  3276. }
  3277. /**
  3278. * lpfc_sli_brdready - Wrapper func for checking the hba readyness
  3279. * @phba: Pointer to HBA context object.
  3280. * @mask: Bit mask to be checked.
  3281. *
  3282. * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
  3283. * from the API jump table function pointer from the lpfc_hba struct.
  3284. **/
  3285. int
  3286. lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
  3287. {
  3288. return phba->lpfc_sli_brdready(phba, mask);
  3289. }
  3290. #define BARRIER_TEST_PATTERN (0xdeadbeef)
  3291. /**
  3292. * lpfc_reset_barrier - Make HBA ready for HBA reset
  3293. * @phba: Pointer to HBA context object.
  3294. *
  3295. * This function is called before resetting an HBA. This function is called
  3296. * with hbalock held and requests HBA to quiesce DMAs before a reset.
  3297. **/
  3298. void lpfc_reset_barrier(struct lpfc_hba *phba)
  3299. {
  3300. uint32_t __iomem *resp_buf;
  3301. uint32_t __iomem *mbox_buf;
  3302. volatile uint32_t mbox;
  3303. uint32_t hc_copy, ha_copy, resp_data;
  3304. int i;
  3305. uint8_t hdrtype;
  3306. pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
  3307. if (hdrtype != 0x80 ||
  3308. (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
  3309. FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
  3310. return;
  3311. /*
  3312. * Tell the other part of the chip to suspend temporarily all
  3313. * its DMA activity.
  3314. */
  3315. resp_buf = phba->MBslimaddr;
  3316. /* Disable the error attention */
  3317. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  3318. return;
  3319. writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
  3320. readl(phba->HCregaddr); /* flush */
  3321. phba->link_flag |= LS_IGNORE_ERATT;
  3322. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3323. return;
  3324. if (ha_copy & HA_ERATT) {
  3325. /* Clear Chip error bit */
  3326. writel(HA_ERATT, phba->HAregaddr);
  3327. phba->pport->stopped = 1;
  3328. }
  3329. mbox = 0;
  3330. ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
  3331. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
  3332. writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
  3333. mbox_buf = phba->MBslimaddr;
  3334. writel(mbox, mbox_buf);
  3335. for (i = 0; i < 50; i++) {
  3336. if (lpfc_readl((resp_buf + 1), &resp_data))
  3337. return;
  3338. if (resp_data != ~(BARRIER_TEST_PATTERN))
  3339. mdelay(1);
  3340. else
  3341. break;
  3342. }
  3343. resp_data = 0;
  3344. if (lpfc_readl((resp_buf + 1), &resp_data))
  3345. return;
  3346. if (resp_data != ~(BARRIER_TEST_PATTERN)) {
  3347. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
  3348. phba->pport->stopped)
  3349. goto restore_hc;
  3350. else
  3351. goto clear_errat;
  3352. }
  3353. ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
  3354. resp_data = 0;
  3355. for (i = 0; i < 500; i++) {
  3356. if (lpfc_readl(resp_buf, &resp_data))
  3357. return;
  3358. if (resp_data != mbox)
  3359. mdelay(1);
  3360. else
  3361. break;
  3362. }
  3363. clear_errat:
  3364. while (++i < 500) {
  3365. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3366. return;
  3367. if (!(ha_copy & HA_ERATT))
  3368. mdelay(1);
  3369. else
  3370. break;
  3371. }
  3372. if (readl(phba->HAregaddr) & HA_ERATT) {
  3373. writel(HA_ERATT, phba->HAregaddr);
  3374. phba->pport->stopped = 1;
  3375. }
  3376. restore_hc:
  3377. phba->link_flag &= ~LS_IGNORE_ERATT;
  3378. writel(hc_copy, phba->HCregaddr);
  3379. readl(phba->HCregaddr); /* flush */
  3380. }
  3381. /**
  3382. * lpfc_sli_brdkill - Issue a kill_board mailbox command
  3383. * @phba: Pointer to HBA context object.
  3384. *
  3385. * This function issues a kill_board mailbox command and waits for
  3386. * the error attention interrupt. This function is called for stopping
  3387. * the firmware processing. The caller is not required to hold any
  3388. * locks. This function calls lpfc_hba_down_post function to free
  3389. * any pending commands after the kill. The function will return 1 when it
  3390. * fails to kill the board else will return 0.
  3391. **/
  3392. int
  3393. lpfc_sli_brdkill(struct lpfc_hba *phba)
  3394. {
  3395. struct lpfc_sli *psli;
  3396. LPFC_MBOXQ_t *pmb;
  3397. uint32_t status;
  3398. uint32_t ha_copy;
  3399. int retval;
  3400. int i = 0;
  3401. psli = &phba->sli;
  3402. /* Kill HBA */
  3403. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3404. "0329 Kill HBA Data: x%x x%x\n",
  3405. phba->pport->port_state, psli->sli_flag);
  3406. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3407. if (!pmb)
  3408. return 1;
  3409. /* Disable the error attention */
  3410. spin_lock_irq(&phba->hbalock);
  3411. if (lpfc_readl(phba->HCregaddr, &status)) {
  3412. spin_unlock_irq(&phba->hbalock);
  3413. mempool_free(pmb, phba->mbox_mem_pool);
  3414. return 1;
  3415. }
  3416. status &= ~HC_ERINT_ENA;
  3417. writel(status, phba->HCregaddr);
  3418. readl(phba->HCregaddr); /* flush */
  3419. phba->link_flag |= LS_IGNORE_ERATT;
  3420. spin_unlock_irq(&phba->hbalock);
  3421. lpfc_kill_board(phba, pmb);
  3422. pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  3423. retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  3424. if (retval != MBX_SUCCESS) {
  3425. if (retval != MBX_BUSY)
  3426. mempool_free(pmb, phba->mbox_mem_pool);
  3427. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  3428. "2752 KILL_BOARD command failed retval %d\n",
  3429. retval);
  3430. spin_lock_irq(&phba->hbalock);
  3431. phba->link_flag &= ~LS_IGNORE_ERATT;
  3432. spin_unlock_irq(&phba->hbalock);
  3433. return 1;
  3434. }
  3435. spin_lock_irq(&phba->hbalock);
  3436. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  3437. spin_unlock_irq(&phba->hbalock);
  3438. mempool_free(pmb, phba->mbox_mem_pool);
  3439. /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
  3440. * attention every 100ms for 3 seconds. If we don't get ERATT after
  3441. * 3 seconds we still set HBA_ERROR state because the status of the
  3442. * board is now undefined.
  3443. */
  3444. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3445. return 1;
  3446. while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
  3447. mdelay(100);
  3448. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  3449. return 1;
  3450. }
  3451. del_timer_sync(&psli->mbox_tmo);
  3452. if (ha_copy & HA_ERATT) {
  3453. writel(HA_ERATT, phba->HAregaddr);
  3454. phba->pport->stopped = 1;
  3455. }
  3456. spin_lock_irq(&phba->hbalock);
  3457. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3458. psli->mbox_active = NULL;
  3459. phba->link_flag &= ~LS_IGNORE_ERATT;
  3460. spin_unlock_irq(&phba->hbalock);
  3461. lpfc_hba_down_post(phba);
  3462. phba->link_state = LPFC_HBA_ERROR;
  3463. return ha_copy & HA_ERATT ? 0 : 1;
  3464. }
  3465. /**
  3466. * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
  3467. * @phba: Pointer to HBA context object.
  3468. *
  3469. * This function resets the HBA by writing HC_INITFF to the control
  3470. * register. After the HBA resets, this function resets all the iocb ring
  3471. * indices. This function disables PCI layer parity checking during
  3472. * the reset.
  3473. * This function returns 0 always.
  3474. * The caller is not required to hold any locks.
  3475. **/
  3476. int
  3477. lpfc_sli_brdreset(struct lpfc_hba *phba)
  3478. {
  3479. struct lpfc_sli *psli;
  3480. struct lpfc_sli_ring *pring;
  3481. uint16_t cfg_value;
  3482. int i;
  3483. psli = &phba->sli;
  3484. /* Reset HBA */
  3485. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3486. "0325 Reset HBA Data: x%x x%x\n",
  3487. phba->pport->port_state, psli->sli_flag);
  3488. /* perform board reset */
  3489. phba->fc_eventTag = 0;
  3490. phba->link_events = 0;
  3491. phba->pport->fc_myDID = 0;
  3492. phba->pport->fc_prevDID = 0;
  3493. /* Turn off parity checking and serr during the physical reset */
  3494. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3495. pci_write_config_word(phba->pcidev, PCI_COMMAND,
  3496. (cfg_value &
  3497. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3498. psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
  3499. /* Now toggle INITFF bit in the Host Control Register */
  3500. writel(HC_INITFF, phba->HCregaddr);
  3501. mdelay(1);
  3502. readl(phba->HCregaddr); /* flush */
  3503. writel(0, phba->HCregaddr);
  3504. readl(phba->HCregaddr); /* flush */
  3505. /* Restore PCI cmd register */
  3506. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3507. /* Initialize relevant SLI info */
  3508. for (i = 0; i < psli->num_rings; i++) {
  3509. pring = &psli->ring[i];
  3510. pring->flag = 0;
  3511. pring->rspidx = 0;
  3512. pring->next_cmdidx = 0;
  3513. pring->local_getidx = 0;
  3514. pring->cmdidx = 0;
  3515. pring->missbufcnt = 0;
  3516. }
  3517. phba->link_state = LPFC_WARM_START;
  3518. return 0;
  3519. }
  3520. /**
  3521. * lpfc_sli4_brdreset - Reset a sli-4 HBA
  3522. * @phba: Pointer to HBA context object.
  3523. *
  3524. * This function resets a SLI4 HBA. This function disables PCI layer parity
  3525. * checking during resets the device. The caller is not required to hold
  3526. * any locks.
  3527. *
  3528. * This function returns 0 always.
  3529. **/
  3530. int
  3531. lpfc_sli4_brdreset(struct lpfc_hba *phba)
  3532. {
  3533. struct lpfc_sli *psli = &phba->sli;
  3534. uint16_t cfg_value;
  3535. /* Reset HBA */
  3536. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3537. "0295 Reset HBA Data: x%x x%x\n",
  3538. phba->pport->port_state, psli->sli_flag);
  3539. /* perform board reset */
  3540. phba->fc_eventTag = 0;
  3541. phba->link_events = 0;
  3542. phba->pport->fc_myDID = 0;
  3543. phba->pport->fc_prevDID = 0;
  3544. spin_lock_irq(&phba->hbalock);
  3545. psli->sli_flag &= ~(LPFC_PROCESS_LA);
  3546. phba->fcf.fcf_flag = 0;
  3547. spin_unlock_irq(&phba->hbalock);
  3548. /* Now physically reset the device */
  3549. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3550. "0389 Performing PCI function reset!\n");
  3551. /* Turn off parity checking and serr during the physical reset */
  3552. pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
  3553. pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
  3554. ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
  3555. /* Perform FCoE PCI function reset */
  3556. lpfc_sli4_queue_destroy(phba);
  3557. lpfc_pci_function_reset(phba);
  3558. /* Restore PCI cmd register */
  3559. pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
  3560. return 0;
  3561. }
  3562. /**
  3563. * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
  3564. * @phba: Pointer to HBA context object.
  3565. *
  3566. * This function is called in the SLI initialization code path to
  3567. * restart the HBA. The caller is not required to hold any lock.
  3568. * This function writes MBX_RESTART mailbox command to the SLIM and
  3569. * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
  3570. * function to free any pending commands. The function enables
  3571. * POST only during the first initialization. The function returns zero.
  3572. * The function does not guarantee completion of MBX_RESTART mailbox
  3573. * command before the return of this function.
  3574. **/
  3575. static int
  3576. lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
  3577. {
  3578. MAILBOX_t *mb;
  3579. struct lpfc_sli *psli;
  3580. volatile uint32_t word0;
  3581. void __iomem *to_slim;
  3582. uint32_t hba_aer_enabled;
  3583. spin_lock_irq(&phba->hbalock);
  3584. /* Take PCIe device Advanced Error Reporting (AER) state */
  3585. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3586. psli = &phba->sli;
  3587. /* Restart HBA */
  3588. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3589. "0337 Restart HBA Data: x%x x%x\n",
  3590. phba->pport->port_state, psli->sli_flag);
  3591. word0 = 0;
  3592. mb = (MAILBOX_t *) &word0;
  3593. mb->mbxCommand = MBX_RESTART;
  3594. mb->mbxHc = 1;
  3595. lpfc_reset_barrier(phba);
  3596. to_slim = phba->MBslimaddr;
  3597. writel(*(uint32_t *) mb, to_slim);
  3598. readl(to_slim); /* flush */
  3599. /* Only skip post after fc_ffinit is completed */
  3600. if (phba->pport->port_state)
  3601. word0 = 1; /* This is really setting up word1 */
  3602. else
  3603. word0 = 0; /* This is really setting up word1 */
  3604. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  3605. writel(*(uint32_t *) mb, to_slim);
  3606. readl(to_slim); /* flush */
  3607. lpfc_sli_brdreset(phba);
  3608. phba->pport->stopped = 0;
  3609. phba->link_state = LPFC_INIT_START;
  3610. phba->hba_flag = 0;
  3611. spin_unlock_irq(&phba->hbalock);
  3612. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3613. psli->stats_start = get_seconds();
  3614. /* Give the INITFF and Post time to settle. */
  3615. mdelay(100);
  3616. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3617. if (hba_aer_enabled)
  3618. pci_disable_pcie_error_reporting(phba->pcidev);
  3619. lpfc_hba_down_post(phba);
  3620. return 0;
  3621. }
  3622. /**
  3623. * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
  3624. * @phba: Pointer to HBA context object.
  3625. *
  3626. * This function is called in the SLI initialization code path to restart
  3627. * a SLI4 HBA. The caller is not required to hold any lock.
  3628. * At the end of the function, it calls lpfc_hba_down_post function to
  3629. * free any pending commands.
  3630. **/
  3631. static int
  3632. lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
  3633. {
  3634. struct lpfc_sli *psli = &phba->sli;
  3635. uint32_t hba_aer_enabled;
  3636. /* Restart HBA */
  3637. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  3638. "0296 Restart HBA Data: x%x x%x\n",
  3639. phba->pport->port_state, psli->sli_flag);
  3640. /* Take PCIe device Advanced Error Reporting (AER) state */
  3641. hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
  3642. lpfc_sli4_brdreset(phba);
  3643. spin_lock_irq(&phba->hbalock);
  3644. phba->pport->stopped = 0;
  3645. phba->link_state = LPFC_INIT_START;
  3646. phba->hba_flag = 0;
  3647. spin_unlock_irq(&phba->hbalock);
  3648. memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
  3649. psli->stats_start = get_seconds();
  3650. /* Reset HBA AER if it was enabled, note hba_flag was reset above */
  3651. if (hba_aer_enabled)
  3652. pci_disable_pcie_error_reporting(phba->pcidev);
  3653. lpfc_hba_down_post(phba);
  3654. return 0;
  3655. }
  3656. /**
  3657. * lpfc_sli_brdrestart - Wrapper func for restarting hba
  3658. * @phba: Pointer to HBA context object.
  3659. *
  3660. * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
  3661. * API jump table function pointer from the lpfc_hba struct.
  3662. **/
  3663. int
  3664. lpfc_sli_brdrestart(struct lpfc_hba *phba)
  3665. {
  3666. return phba->lpfc_sli_brdrestart(phba);
  3667. }
  3668. /**
  3669. * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
  3670. * @phba: Pointer to HBA context object.
  3671. *
  3672. * This function is called after a HBA restart to wait for successful
  3673. * restart of the HBA. Successful restart of the HBA is indicated by
  3674. * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
  3675. * iteration, the function will restart the HBA again. The function returns
  3676. * zero if HBA successfully restarted else returns negative error code.
  3677. **/
  3678. static int
  3679. lpfc_sli_chipset_init(struct lpfc_hba *phba)
  3680. {
  3681. uint32_t status, i = 0;
  3682. /* Read the HBA Host Status Register */
  3683. if (lpfc_readl(phba->HSregaddr, &status))
  3684. return -EIO;
  3685. /* Check status register to see what current state is */
  3686. i = 0;
  3687. while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
  3688. /* Check every 10ms for 10 retries, then every 100ms for 90
  3689. * retries, then every 1 sec for 50 retires for a total of
  3690. * ~60 seconds before reset the board again and check every
  3691. * 1 sec for 50 retries. The up to 60 seconds before the
  3692. * board ready is required by the Falcon FIPS zeroization
  3693. * complete, and any reset the board in between shall cause
  3694. * restart of zeroization, further delay the board ready.
  3695. */
  3696. if (i++ >= 200) {
  3697. /* Adapter failed to init, timeout, status reg
  3698. <status> */
  3699. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3700. "0436 Adapter failed to init, "
  3701. "timeout, status reg x%x, "
  3702. "FW Data: A8 x%x AC x%x\n", status,
  3703. readl(phba->MBslimaddr + 0xa8),
  3704. readl(phba->MBslimaddr + 0xac));
  3705. phba->link_state = LPFC_HBA_ERROR;
  3706. return -ETIMEDOUT;
  3707. }
  3708. /* Check to see if any errors occurred during init */
  3709. if (status & HS_FFERM) {
  3710. /* ERROR: During chipset initialization */
  3711. /* Adapter failed to init, chipset, status reg
  3712. <status> */
  3713. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3714. "0437 Adapter failed to init, "
  3715. "chipset, status reg x%x, "
  3716. "FW Data: A8 x%x AC x%x\n", status,
  3717. readl(phba->MBslimaddr + 0xa8),
  3718. readl(phba->MBslimaddr + 0xac));
  3719. phba->link_state = LPFC_HBA_ERROR;
  3720. return -EIO;
  3721. }
  3722. if (i <= 10)
  3723. msleep(10);
  3724. else if (i <= 100)
  3725. msleep(100);
  3726. else
  3727. msleep(1000);
  3728. if (i == 150) {
  3729. /* Do post */
  3730. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3731. lpfc_sli_brdrestart(phba);
  3732. }
  3733. /* Read the HBA Host Status Register */
  3734. if (lpfc_readl(phba->HSregaddr, &status))
  3735. return -EIO;
  3736. }
  3737. /* Check to see if any errors occurred during init */
  3738. if (status & HS_FFERM) {
  3739. /* ERROR: During chipset initialization */
  3740. /* Adapter failed to init, chipset, status reg <status> */
  3741. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3742. "0438 Adapter failed to init, chipset, "
  3743. "status reg x%x, "
  3744. "FW Data: A8 x%x AC x%x\n", status,
  3745. readl(phba->MBslimaddr + 0xa8),
  3746. readl(phba->MBslimaddr + 0xac));
  3747. phba->link_state = LPFC_HBA_ERROR;
  3748. return -EIO;
  3749. }
  3750. /* Clear all interrupt enable conditions */
  3751. writel(0, phba->HCregaddr);
  3752. readl(phba->HCregaddr); /* flush */
  3753. /* setup host attn register */
  3754. writel(0xffffffff, phba->HAregaddr);
  3755. readl(phba->HAregaddr); /* flush */
  3756. return 0;
  3757. }
  3758. /**
  3759. * lpfc_sli_hbq_count - Get the number of HBQs to be configured
  3760. *
  3761. * This function calculates and returns the number of HBQs required to be
  3762. * configured.
  3763. **/
  3764. int
  3765. lpfc_sli_hbq_count(void)
  3766. {
  3767. return ARRAY_SIZE(lpfc_hbq_defs);
  3768. }
  3769. /**
  3770. * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
  3771. *
  3772. * This function adds the number of hbq entries in every HBQ to get
  3773. * the total number of hbq entries required for the HBA and returns
  3774. * the total count.
  3775. **/
  3776. static int
  3777. lpfc_sli_hbq_entry_count(void)
  3778. {
  3779. int hbq_count = lpfc_sli_hbq_count();
  3780. int count = 0;
  3781. int i;
  3782. for (i = 0; i < hbq_count; ++i)
  3783. count += lpfc_hbq_defs[i]->entry_count;
  3784. return count;
  3785. }
  3786. /**
  3787. * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
  3788. *
  3789. * This function calculates amount of memory required for all hbq entries
  3790. * to be configured and returns the total memory required.
  3791. **/
  3792. int
  3793. lpfc_sli_hbq_size(void)
  3794. {
  3795. return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
  3796. }
  3797. /**
  3798. * lpfc_sli_hbq_setup - configure and initialize HBQs
  3799. * @phba: Pointer to HBA context object.
  3800. *
  3801. * This function is called during the SLI initialization to configure
  3802. * all the HBQs and post buffers to the HBQ. The caller is not
  3803. * required to hold any locks. This function will return zero if successful
  3804. * else it will return negative error code.
  3805. **/
  3806. static int
  3807. lpfc_sli_hbq_setup(struct lpfc_hba *phba)
  3808. {
  3809. int hbq_count = lpfc_sli_hbq_count();
  3810. LPFC_MBOXQ_t *pmb;
  3811. MAILBOX_t *pmbox;
  3812. uint32_t hbqno;
  3813. uint32_t hbq_entry_index;
  3814. /* Get a Mailbox buffer to setup mailbox
  3815. * commands for HBA initialization
  3816. */
  3817. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3818. if (!pmb)
  3819. return -ENOMEM;
  3820. pmbox = &pmb->u.mb;
  3821. /* Initialize the struct lpfc_sli_hbq structure for each hbq */
  3822. phba->link_state = LPFC_INIT_MBX_CMDS;
  3823. phba->hbq_in_use = 1;
  3824. hbq_entry_index = 0;
  3825. for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
  3826. phba->hbqs[hbqno].next_hbqPutIdx = 0;
  3827. phba->hbqs[hbqno].hbqPutIdx = 0;
  3828. phba->hbqs[hbqno].local_hbqGetIdx = 0;
  3829. phba->hbqs[hbqno].entry_count =
  3830. lpfc_hbq_defs[hbqno]->entry_count;
  3831. lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
  3832. hbq_entry_index, pmb);
  3833. hbq_entry_index += phba->hbqs[hbqno].entry_count;
  3834. if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
  3835. /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
  3836. mbxStatus <status>, ring <num> */
  3837. lpfc_printf_log(phba, KERN_ERR,
  3838. LOG_SLI | LOG_VPORT,
  3839. "1805 Adapter failed to init. "
  3840. "Data: x%x x%x x%x\n",
  3841. pmbox->mbxCommand,
  3842. pmbox->mbxStatus, hbqno);
  3843. phba->link_state = LPFC_HBA_ERROR;
  3844. mempool_free(pmb, phba->mbox_mem_pool);
  3845. return -ENXIO;
  3846. }
  3847. }
  3848. phba->hbq_count = hbq_count;
  3849. mempool_free(pmb, phba->mbox_mem_pool);
  3850. /* Initially populate or replenish the HBQs */
  3851. for (hbqno = 0; hbqno < hbq_count; ++hbqno)
  3852. lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
  3853. return 0;
  3854. }
  3855. /**
  3856. * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
  3857. * @phba: Pointer to HBA context object.
  3858. *
  3859. * This function is called during the SLI initialization to configure
  3860. * all the HBQs and post buffers to the HBQ. The caller is not
  3861. * required to hold any locks. This function will return zero if successful
  3862. * else it will return negative error code.
  3863. **/
  3864. static int
  3865. lpfc_sli4_rb_setup(struct lpfc_hba *phba)
  3866. {
  3867. phba->hbq_in_use = 1;
  3868. phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
  3869. phba->hbq_count = 1;
  3870. /* Initially populate or replenish the HBQs */
  3871. lpfc_sli_hbqbuf_init_hbqs(phba, 0);
  3872. return 0;
  3873. }
  3874. /**
  3875. * lpfc_sli_config_port - Issue config port mailbox command
  3876. * @phba: Pointer to HBA context object.
  3877. * @sli_mode: sli mode - 2/3
  3878. *
  3879. * This function is called by the sli intialization code path
  3880. * to issue config_port mailbox command. This function restarts the
  3881. * HBA firmware and issues a config_port mailbox command to configure
  3882. * the SLI interface in the sli mode specified by sli_mode
  3883. * variable. The caller is not required to hold any locks.
  3884. * The function returns 0 if successful, else returns negative error
  3885. * code.
  3886. **/
  3887. int
  3888. lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
  3889. {
  3890. LPFC_MBOXQ_t *pmb;
  3891. uint32_t resetcount = 0, rc = 0, done = 0;
  3892. pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  3893. if (!pmb) {
  3894. phba->link_state = LPFC_HBA_ERROR;
  3895. return -ENOMEM;
  3896. }
  3897. phba->sli_rev = sli_mode;
  3898. while (resetcount < 2 && !done) {
  3899. spin_lock_irq(&phba->hbalock);
  3900. phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  3901. spin_unlock_irq(&phba->hbalock);
  3902. phba->pport->port_state = LPFC_VPORT_UNKNOWN;
  3903. lpfc_sli_brdrestart(phba);
  3904. rc = lpfc_sli_chipset_init(phba);
  3905. if (rc)
  3906. break;
  3907. spin_lock_irq(&phba->hbalock);
  3908. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  3909. spin_unlock_irq(&phba->hbalock);
  3910. resetcount++;
  3911. /* Call pre CONFIG_PORT mailbox command initialization. A
  3912. * value of 0 means the call was successful. Any other
  3913. * nonzero value is a failure, but if ERESTART is returned,
  3914. * the driver may reset the HBA and try again.
  3915. */
  3916. rc = lpfc_config_port_prep(phba);
  3917. if (rc == -ERESTART) {
  3918. phba->link_state = LPFC_LINK_UNKNOWN;
  3919. continue;
  3920. } else if (rc)
  3921. break;
  3922. phba->link_state = LPFC_INIT_MBX_CMDS;
  3923. lpfc_config_port(phba, pmb);
  3924. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  3925. phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
  3926. LPFC_SLI3_HBQ_ENABLED |
  3927. LPFC_SLI3_CRP_ENABLED |
  3928. LPFC_SLI3_BG_ENABLED |
  3929. LPFC_SLI3_DSS_ENABLED);
  3930. if (rc != MBX_SUCCESS) {
  3931. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3932. "0442 Adapter failed to init, mbxCmd x%x "
  3933. "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
  3934. pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
  3935. spin_lock_irq(&phba->hbalock);
  3936. phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
  3937. spin_unlock_irq(&phba->hbalock);
  3938. rc = -ENXIO;
  3939. } else {
  3940. /* Allow asynchronous mailbox command to go through */
  3941. spin_lock_irq(&phba->hbalock);
  3942. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  3943. spin_unlock_irq(&phba->hbalock);
  3944. done = 1;
  3945. if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
  3946. (pmb->u.mb.un.varCfgPort.gasabt == 0))
  3947. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  3948. "3110 Port did not grant ASABT\n");
  3949. }
  3950. }
  3951. if (!done) {
  3952. rc = -EINVAL;
  3953. goto do_prep_failed;
  3954. }
  3955. if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
  3956. if (!pmb->u.mb.un.varCfgPort.cMA) {
  3957. rc = -ENXIO;
  3958. goto do_prep_failed;
  3959. }
  3960. if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
  3961. phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
  3962. phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
  3963. phba->max_vports = (phba->max_vpi > phba->max_vports) ?
  3964. phba->max_vpi : phba->max_vports;
  3965. } else
  3966. phba->max_vpi = 0;
  3967. phba->fips_level = 0;
  3968. phba->fips_spec_rev = 0;
  3969. if (pmb->u.mb.un.varCfgPort.gdss) {
  3970. phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
  3971. phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
  3972. phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
  3973. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  3974. "2850 Security Crypto Active. FIPS x%d "
  3975. "(Spec Rev: x%d)",
  3976. phba->fips_level, phba->fips_spec_rev);
  3977. }
  3978. if (pmb->u.mb.un.varCfgPort.sec_err) {
  3979. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3980. "2856 Config Port Security Crypto "
  3981. "Error: x%x ",
  3982. pmb->u.mb.un.varCfgPort.sec_err);
  3983. }
  3984. if (pmb->u.mb.un.varCfgPort.gerbm)
  3985. phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
  3986. if (pmb->u.mb.un.varCfgPort.gcrp)
  3987. phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
  3988. phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
  3989. phba->port_gp = phba->mbox->us.s3_pgp.port;
  3990. if (phba->cfg_enable_bg) {
  3991. if (pmb->u.mb.un.varCfgPort.gbg)
  3992. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  3993. else
  3994. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  3995. "0443 Adapter did not grant "
  3996. "BlockGuard\n");
  3997. }
  3998. } else {
  3999. phba->hbq_get = NULL;
  4000. phba->port_gp = phba->mbox->us.s2.port;
  4001. phba->max_vpi = 0;
  4002. }
  4003. do_prep_failed:
  4004. mempool_free(pmb, phba->mbox_mem_pool);
  4005. return rc;
  4006. }
  4007. /**
  4008. * lpfc_sli_hba_setup - SLI intialization function
  4009. * @phba: Pointer to HBA context object.
  4010. *
  4011. * This function is the main SLI intialization function. This function
  4012. * is called by the HBA intialization code, HBA reset code and HBA
  4013. * error attention handler code. Caller is not required to hold any
  4014. * locks. This function issues config_port mailbox command to configure
  4015. * the SLI, setup iocb rings and HBQ rings. In the end the function
  4016. * calls the config_port_post function to issue init_link mailbox
  4017. * command and to start the discovery. The function will return zero
  4018. * if successful, else it will return negative error code.
  4019. **/
  4020. int
  4021. lpfc_sli_hba_setup(struct lpfc_hba *phba)
  4022. {
  4023. uint32_t rc;
  4024. int mode = 3, i;
  4025. int longs;
  4026. switch (lpfc_sli_mode) {
  4027. case 2:
  4028. if (phba->cfg_enable_npiv) {
  4029. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4030. "1824 NPIV enabled: Override lpfc_sli_mode "
  4031. "parameter (%d) to auto (0).\n",
  4032. lpfc_sli_mode);
  4033. break;
  4034. }
  4035. mode = 2;
  4036. break;
  4037. case 0:
  4038. case 3:
  4039. break;
  4040. default:
  4041. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4042. "1819 Unrecognized lpfc_sli_mode "
  4043. "parameter: %d.\n", lpfc_sli_mode);
  4044. break;
  4045. }
  4046. rc = lpfc_sli_config_port(phba, mode);
  4047. if (rc && lpfc_sli_mode == 3)
  4048. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
  4049. "1820 Unable to select SLI-3. "
  4050. "Not supported by adapter.\n");
  4051. if (rc && mode != 2)
  4052. rc = lpfc_sli_config_port(phba, 2);
  4053. if (rc)
  4054. goto lpfc_sli_hba_setup_error;
  4055. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  4056. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  4057. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  4058. if (!rc) {
  4059. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4060. "2709 This device supports "
  4061. "Advanced Error Reporting (AER)\n");
  4062. spin_lock_irq(&phba->hbalock);
  4063. phba->hba_flag |= HBA_AER_ENABLED;
  4064. spin_unlock_irq(&phba->hbalock);
  4065. } else {
  4066. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4067. "2708 This device does not support "
  4068. "Advanced Error Reporting (AER)\n");
  4069. phba->cfg_aer_support = 0;
  4070. }
  4071. }
  4072. if (phba->sli_rev == 3) {
  4073. phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
  4074. phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
  4075. } else {
  4076. phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
  4077. phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
  4078. phba->sli3_options = 0;
  4079. }
  4080. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  4081. "0444 Firmware in SLI %x mode. Max_vpi %d\n",
  4082. phba->sli_rev, phba->max_vpi);
  4083. rc = lpfc_sli_ring_map(phba);
  4084. if (rc)
  4085. goto lpfc_sli_hba_setup_error;
  4086. /* Initialize VPIs. */
  4087. if (phba->sli_rev == LPFC_SLI_REV3) {
  4088. /*
  4089. * The VPI bitmask and physical ID array are allocated
  4090. * and initialized once only - at driver load. A port
  4091. * reset doesn't need to reinitialize this memory.
  4092. */
  4093. if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
  4094. longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
  4095. phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
  4096. GFP_KERNEL);
  4097. if (!phba->vpi_bmask) {
  4098. rc = -ENOMEM;
  4099. goto lpfc_sli_hba_setup_error;
  4100. }
  4101. phba->vpi_ids = kzalloc(
  4102. (phba->max_vpi+1) * sizeof(uint16_t),
  4103. GFP_KERNEL);
  4104. if (!phba->vpi_ids) {
  4105. kfree(phba->vpi_bmask);
  4106. rc = -ENOMEM;
  4107. goto lpfc_sli_hba_setup_error;
  4108. }
  4109. for (i = 0; i < phba->max_vpi; i++)
  4110. phba->vpi_ids[i] = i;
  4111. }
  4112. }
  4113. /* Init HBQs */
  4114. if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
  4115. rc = lpfc_sli_hbq_setup(phba);
  4116. if (rc)
  4117. goto lpfc_sli_hba_setup_error;
  4118. }
  4119. spin_lock_irq(&phba->hbalock);
  4120. phba->sli.sli_flag |= LPFC_PROCESS_LA;
  4121. spin_unlock_irq(&phba->hbalock);
  4122. rc = lpfc_config_port_post(phba);
  4123. if (rc)
  4124. goto lpfc_sli_hba_setup_error;
  4125. return rc;
  4126. lpfc_sli_hba_setup_error:
  4127. phba->link_state = LPFC_HBA_ERROR;
  4128. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4129. "0445 Firmware initialization failed\n");
  4130. return rc;
  4131. }
  4132. /**
  4133. * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
  4134. * @phba: Pointer to HBA context object.
  4135. * @mboxq: mailbox pointer.
  4136. * This function issue a dump mailbox command to read config region
  4137. * 23 and parse the records in the region and populate driver
  4138. * data structure.
  4139. **/
  4140. static int
  4141. lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
  4142. {
  4143. LPFC_MBOXQ_t *mboxq;
  4144. struct lpfc_dmabuf *mp;
  4145. struct lpfc_mqe *mqe;
  4146. uint32_t data_length;
  4147. int rc;
  4148. /* Program the default value of vlan_id and fc_map */
  4149. phba->valid_vlan = 0;
  4150. phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
  4151. phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
  4152. phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
  4153. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4154. if (!mboxq)
  4155. return -ENOMEM;
  4156. mqe = &mboxq->u.mqe;
  4157. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
  4158. rc = -ENOMEM;
  4159. goto out_free_mboxq;
  4160. }
  4161. mp = (struct lpfc_dmabuf *) mboxq->context1;
  4162. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4163. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  4164. "(%d):2571 Mailbox cmd x%x Status x%x "
  4165. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4166. "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
  4167. "CQ: x%x x%x x%x x%x\n",
  4168. mboxq->vport ? mboxq->vport->vpi : 0,
  4169. bf_get(lpfc_mqe_command, mqe),
  4170. bf_get(lpfc_mqe_status, mqe),
  4171. mqe->un.mb_words[0], mqe->un.mb_words[1],
  4172. mqe->un.mb_words[2], mqe->un.mb_words[3],
  4173. mqe->un.mb_words[4], mqe->un.mb_words[5],
  4174. mqe->un.mb_words[6], mqe->un.mb_words[7],
  4175. mqe->un.mb_words[8], mqe->un.mb_words[9],
  4176. mqe->un.mb_words[10], mqe->un.mb_words[11],
  4177. mqe->un.mb_words[12], mqe->un.mb_words[13],
  4178. mqe->un.mb_words[14], mqe->un.mb_words[15],
  4179. mqe->un.mb_words[16], mqe->un.mb_words[50],
  4180. mboxq->mcqe.word0,
  4181. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  4182. mboxq->mcqe.trailer);
  4183. if (rc) {
  4184. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4185. kfree(mp);
  4186. rc = -EIO;
  4187. goto out_free_mboxq;
  4188. }
  4189. data_length = mqe->un.mb_words[5];
  4190. if (data_length > DMP_RGN23_SIZE) {
  4191. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4192. kfree(mp);
  4193. rc = -EIO;
  4194. goto out_free_mboxq;
  4195. }
  4196. lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
  4197. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  4198. kfree(mp);
  4199. rc = 0;
  4200. out_free_mboxq:
  4201. mempool_free(mboxq, phba->mbox_mem_pool);
  4202. return rc;
  4203. }
  4204. /**
  4205. * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
  4206. * @phba: pointer to lpfc hba data structure.
  4207. * @mboxq: pointer to the LPFC_MBOXQ_t structure.
  4208. * @vpd: pointer to the memory to hold resulting port vpd data.
  4209. * @vpd_size: On input, the number of bytes allocated to @vpd.
  4210. * On output, the number of data bytes in @vpd.
  4211. *
  4212. * This routine executes a READ_REV SLI4 mailbox command. In
  4213. * addition, this routine gets the port vpd data.
  4214. *
  4215. * Return codes
  4216. * 0 - successful
  4217. * -ENOMEM - could not allocated memory.
  4218. **/
  4219. static int
  4220. lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  4221. uint8_t *vpd, uint32_t *vpd_size)
  4222. {
  4223. int rc = 0;
  4224. uint32_t dma_size;
  4225. struct lpfc_dmabuf *dmabuf;
  4226. struct lpfc_mqe *mqe;
  4227. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  4228. if (!dmabuf)
  4229. return -ENOMEM;
  4230. /*
  4231. * Get a DMA buffer for the vpd data resulting from the READ_REV
  4232. * mailbox command.
  4233. */
  4234. dma_size = *vpd_size;
  4235. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  4236. dma_size,
  4237. &dmabuf->phys,
  4238. GFP_KERNEL);
  4239. if (!dmabuf->virt) {
  4240. kfree(dmabuf);
  4241. return -ENOMEM;
  4242. }
  4243. memset(dmabuf->virt, 0, dma_size);
  4244. /*
  4245. * The SLI4 implementation of READ_REV conflicts at word1,
  4246. * bits 31:16 and SLI4 adds vpd functionality not present
  4247. * in SLI3. This code corrects the conflicts.
  4248. */
  4249. lpfc_read_rev(phba, mboxq);
  4250. mqe = &mboxq->u.mqe;
  4251. mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
  4252. mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
  4253. mqe->un.read_rev.word1 &= 0x0000FFFF;
  4254. bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
  4255. bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
  4256. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4257. if (rc) {
  4258. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4259. dmabuf->virt, dmabuf->phys);
  4260. kfree(dmabuf);
  4261. return -EIO;
  4262. }
  4263. /*
  4264. * The available vpd length cannot be bigger than the
  4265. * DMA buffer passed to the port. Catch the less than
  4266. * case and update the caller's size.
  4267. */
  4268. if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
  4269. *vpd_size = mqe->un.read_rev.avail_vpd_len;
  4270. memcpy(vpd, dmabuf->virt, *vpd_size);
  4271. dma_free_coherent(&phba->pcidev->dev, dma_size,
  4272. dmabuf->virt, dmabuf->phys);
  4273. kfree(dmabuf);
  4274. return 0;
  4275. }
  4276. /**
  4277. * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
  4278. * @phba: pointer to lpfc hba data structure.
  4279. *
  4280. * This routine retrieves SLI4 device physical port name this PCI function
  4281. * is attached to.
  4282. *
  4283. * Return codes
  4284. * 0 - sucessful
  4285. * otherwise - failed to retrieve physical port name
  4286. **/
  4287. static int
  4288. lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
  4289. {
  4290. LPFC_MBOXQ_t *mboxq;
  4291. struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
  4292. struct lpfc_controller_attribute *cntl_attr;
  4293. struct lpfc_mbx_get_port_name *get_port_name;
  4294. void *virtaddr = NULL;
  4295. uint32_t alloclen, reqlen;
  4296. uint32_t shdr_status, shdr_add_status;
  4297. union lpfc_sli4_cfg_shdr *shdr;
  4298. char cport_name = 0;
  4299. int rc;
  4300. /* We assume nothing at this point */
  4301. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4302. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
  4303. mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4304. if (!mboxq)
  4305. return -ENOMEM;
  4306. /* obtain link type and link number via READ_CONFIG */
  4307. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
  4308. lpfc_sli4_read_config(phba);
  4309. if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
  4310. goto retrieve_ppname;
  4311. /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
  4312. reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
  4313. alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4314. LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
  4315. LPFC_SLI4_MBX_NEMBED);
  4316. if (alloclen < reqlen) {
  4317. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  4318. "3084 Allocated DMA memory size (%d) is "
  4319. "less than the requested DMA memory size "
  4320. "(%d)\n", alloclen, reqlen);
  4321. rc = -ENOMEM;
  4322. goto out_free_mboxq;
  4323. }
  4324. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4325. virtaddr = mboxq->sge_array->addr[0];
  4326. mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
  4327. shdr = &mbx_cntl_attr->cfg_shdr;
  4328. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4329. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4330. if (shdr_status || shdr_add_status || rc) {
  4331. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4332. "3085 Mailbox x%x (x%x/x%x) failed, "
  4333. "rc:x%x, status:x%x, add_status:x%x\n",
  4334. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4335. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4336. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4337. rc, shdr_status, shdr_add_status);
  4338. rc = -ENXIO;
  4339. goto out_free_mboxq;
  4340. }
  4341. cntl_attr = &mbx_cntl_attr->cntl_attr;
  4342. phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
  4343. phba->sli4_hba.lnk_info.lnk_tp =
  4344. bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
  4345. phba->sli4_hba.lnk_info.lnk_no =
  4346. bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
  4347. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4348. "3086 lnk_type:%d, lnk_numb:%d\n",
  4349. phba->sli4_hba.lnk_info.lnk_tp,
  4350. phba->sli4_hba.lnk_info.lnk_no);
  4351. retrieve_ppname:
  4352. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
  4353. LPFC_MBOX_OPCODE_GET_PORT_NAME,
  4354. sizeof(struct lpfc_mbx_get_port_name) -
  4355. sizeof(struct lpfc_sli4_cfg_mhdr),
  4356. LPFC_SLI4_MBX_EMBED);
  4357. get_port_name = &mboxq->u.mqe.un.get_port_name;
  4358. shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
  4359. bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
  4360. bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
  4361. phba->sli4_hba.lnk_info.lnk_tp);
  4362. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  4363. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  4364. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  4365. if (shdr_status || shdr_add_status || rc) {
  4366. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  4367. "3087 Mailbox x%x (x%x/x%x) failed: "
  4368. "rc:x%x, status:x%x, add_status:x%x\n",
  4369. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  4370. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  4371. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  4372. rc, shdr_status, shdr_add_status);
  4373. rc = -ENXIO;
  4374. goto out_free_mboxq;
  4375. }
  4376. switch (phba->sli4_hba.lnk_info.lnk_no) {
  4377. case LPFC_LINK_NUMBER_0:
  4378. cport_name = bf_get(lpfc_mbx_get_port_name_name0,
  4379. &get_port_name->u.response);
  4380. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4381. break;
  4382. case LPFC_LINK_NUMBER_1:
  4383. cport_name = bf_get(lpfc_mbx_get_port_name_name1,
  4384. &get_port_name->u.response);
  4385. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4386. break;
  4387. case LPFC_LINK_NUMBER_2:
  4388. cport_name = bf_get(lpfc_mbx_get_port_name_name2,
  4389. &get_port_name->u.response);
  4390. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4391. break;
  4392. case LPFC_LINK_NUMBER_3:
  4393. cport_name = bf_get(lpfc_mbx_get_port_name_name3,
  4394. &get_port_name->u.response);
  4395. phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
  4396. break;
  4397. default:
  4398. break;
  4399. }
  4400. if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
  4401. phba->Port[0] = cport_name;
  4402. phba->Port[1] = '\0';
  4403. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4404. "3091 SLI get port name: %s\n", phba->Port);
  4405. }
  4406. out_free_mboxq:
  4407. if (rc != MBX_TIMEOUT) {
  4408. if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
  4409. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  4410. else
  4411. mempool_free(mboxq, phba->mbox_mem_pool);
  4412. }
  4413. return rc;
  4414. }
  4415. /**
  4416. * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
  4417. * @phba: pointer to lpfc hba data structure.
  4418. *
  4419. * This routine is called to explicitly arm the SLI4 device's completion and
  4420. * event queues
  4421. **/
  4422. static void
  4423. lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
  4424. {
  4425. uint8_t fcp_eqidx;
  4426. lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
  4427. lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
  4428. fcp_eqidx = 0;
  4429. if (phba->sli4_hba.fcp_cq) {
  4430. do
  4431. lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
  4432. LPFC_QUEUE_REARM);
  4433. while (++fcp_eqidx < phba->cfg_fcp_eq_count);
  4434. }
  4435. lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
  4436. if (phba->sli4_hba.fp_eq) {
  4437. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count;
  4438. fcp_eqidx++)
  4439. lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
  4440. LPFC_QUEUE_REARM);
  4441. }
  4442. }
  4443. /**
  4444. * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
  4445. * @phba: Pointer to HBA context object.
  4446. * @type: The resource extent type.
  4447. * @extnt_count: buffer to hold port available extent count.
  4448. * @extnt_size: buffer to hold element count per extent.
  4449. *
  4450. * This function calls the port and retrievs the number of available
  4451. * extents and their size for a particular extent type.
  4452. *
  4453. * Returns: 0 if successful. Nonzero otherwise.
  4454. **/
  4455. int
  4456. lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
  4457. uint16_t *extnt_count, uint16_t *extnt_size)
  4458. {
  4459. int rc = 0;
  4460. uint32_t length;
  4461. uint32_t mbox_tmo;
  4462. struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
  4463. LPFC_MBOXQ_t *mbox;
  4464. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4465. if (!mbox)
  4466. return -ENOMEM;
  4467. /* Find out how many extents are available for this resource type */
  4468. length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
  4469. sizeof(struct lpfc_sli4_cfg_mhdr));
  4470. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4471. LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
  4472. length, LPFC_SLI4_MBX_EMBED);
  4473. /* Send an extents count of 0 - the GET doesn't use it. */
  4474. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4475. LPFC_SLI4_MBX_EMBED);
  4476. if (unlikely(rc)) {
  4477. rc = -EIO;
  4478. goto err_exit;
  4479. }
  4480. if (!phba->sli4_hba.intr_enable)
  4481. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4482. else {
  4483. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4484. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4485. }
  4486. if (unlikely(rc)) {
  4487. rc = -EIO;
  4488. goto err_exit;
  4489. }
  4490. rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
  4491. if (bf_get(lpfc_mbox_hdr_status,
  4492. &rsrc_info->header.cfg_shdr.response)) {
  4493. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4494. "2930 Failed to get resource extents "
  4495. "Status 0x%x Add'l Status 0x%x\n",
  4496. bf_get(lpfc_mbox_hdr_status,
  4497. &rsrc_info->header.cfg_shdr.response),
  4498. bf_get(lpfc_mbox_hdr_add_status,
  4499. &rsrc_info->header.cfg_shdr.response));
  4500. rc = -EIO;
  4501. goto err_exit;
  4502. }
  4503. *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
  4504. &rsrc_info->u.rsp);
  4505. *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
  4506. &rsrc_info->u.rsp);
  4507. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  4508. "3162 Retrieved extents type-%d from port: count:%d, "
  4509. "size:%d\n", type, *extnt_count, *extnt_size);
  4510. err_exit:
  4511. mempool_free(mbox, phba->mbox_mem_pool);
  4512. return rc;
  4513. }
  4514. /**
  4515. * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
  4516. * @phba: Pointer to HBA context object.
  4517. * @type: The extent type to check.
  4518. *
  4519. * This function reads the current available extents from the port and checks
  4520. * if the extent count or extent size has changed since the last access.
  4521. * Callers use this routine post port reset to understand if there is a
  4522. * extent reprovisioning requirement.
  4523. *
  4524. * Returns:
  4525. * -Error: error indicates problem.
  4526. * 1: Extent count or size has changed.
  4527. * 0: No changes.
  4528. **/
  4529. static int
  4530. lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
  4531. {
  4532. uint16_t curr_ext_cnt, rsrc_ext_cnt;
  4533. uint16_t size_diff, rsrc_ext_size;
  4534. int rc = 0;
  4535. struct lpfc_rsrc_blks *rsrc_entry;
  4536. struct list_head *rsrc_blk_list = NULL;
  4537. size_diff = 0;
  4538. curr_ext_cnt = 0;
  4539. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4540. &rsrc_ext_cnt,
  4541. &rsrc_ext_size);
  4542. if (unlikely(rc))
  4543. return -EIO;
  4544. switch (type) {
  4545. case LPFC_RSC_TYPE_FCOE_RPI:
  4546. rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4547. break;
  4548. case LPFC_RSC_TYPE_FCOE_VPI:
  4549. rsrc_blk_list = &phba->lpfc_vpi_blk_list;
  4550. break;
  4551. case LPFC_RSC_TYPE_FCOE_XRI:
  4552. rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4553. break;
  4554. case LPFC_RSC_TYPE_FCOE_VFI:
  4555. rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4556. break;
  4557. default:
  4558. break;
  4559. }
  4560. list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
  4561. curr_ext_cnt++;
  4562. if (rsrc_entry->rsrc_size != rsrc_ext_size)
  4563. size_diff++;
  4564. }
  4565. if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
  4566. rc = 1;
  4567. return rc;
  4568. }
  4569. /**
  4570. * lpfc_sli4_cfg_post_extnts -
  4571. * @phba: Pointer to HBA context object.
  4572. * @extnt_cnt - number of available extents.
  4573. * @type - the extent type (rpi, xri, vfi, vpi).
  4574. * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
  4575. * @mbox - pointer to the caller's allocated mailbox structure.
  4576. *
  4577. * This function executes the extents allocation request. It also
  4578. * takes care of the amount of memory needed to allocate or get the
  4579. * allocated extents. It is the caller's responsibility to evaluate
  4580. * the response.
  4581. *
  4582. * Returns:
  4583. * -Error: Error value describes the condition found.
  4584. * 0: if successful
  4585. **/
  4586. static int
  4587. lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
  4588. uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
  4589. {
  4590. int rc = 0;
  4591. uint32_t req_len;
  4592. uint32_t emb_len;
  4593. uint32_t alloc_len, mbox_tmo;
  4594. /* Calculate the total requested length of the dma memory */
  4595. req_len = extnt_cnt * sizeof(uint16_t);
  4596. /*
  4597. * Calculate the size of an embedded mailbox. The uint32_t
  4598. * accounts for extents-specific word.
  4599. */
  4600. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  4601. sizeof(uint32_t);
  4602. /*
  4603. * Presume the allocation and response will fit into an embedded
  4604. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  4605. */
  4606. *emb = LPFC_SLI4_MBX_EMBED;
  4607. if (req_len > emb_len) {
  4608. req_len = extnt_cnt * sizeof(uint16_t) +
  4609. sizeof(union lpfc_sli4_cfg_shdr) +
  4610. sizeof(uint32_t);
  4611. *emb = LPFC_SLI4_MBX_NEMBED;
  4612. }
  4613. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4614. LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
  4615. req_len, *emb);
  4616. if (alloc_len < req_len) {
  4617. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  4618. "2982 Allocated DMA memory size (x%x) is "
  4619. "less than the requested DMA memory "
  4620. "size (x%x)\n", alloc_len, req_len);
  4621. return -ENOMEM;
  4622. }
  4623. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
  4624. if (unlikely(rc))
  4625. return -EIO;
  4626. if (!phba->sli4_hba.intr_enable)
  4627. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4628. else {
  4629. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4630. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4631. }
  4632. if (unlikely(rc))
  4633. rc = -EIO;
  4634. return rc;
  4635. }
  4636. /**
  4637. * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
  4638. * @phba: Pointer to HBA context object.
  4639. * @type: The resource extent type to allocate.
  4640. *
  4641. * This function allocates the number of elements for the specified
  4642. * resource type.
  4643. **/
  4644. static int
  4645. lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
  4646. {
  4647. bool emb = false;
  4648. uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
  4649. uint16_t rsrc_id, rsrc_start, j, k;
  4650. uint16_t *ids;
  4651. int i, rc;
  4652. unsigned long longs;
  4653. unsigned long *bmask;
  4654. struct lpfc_rsrc_blks *rsrc_blks;
  4655. LPFC_MBOXQ_t *mbox;
  4656. uint32_t length;
  4657. struct lpfc_id_range *id_array = NULL;
  4658. void *virtaddr = NULL;
  4659. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  4660. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  4661. struct list_head *ext_blk_list;
  4662. rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
  4663. &rsrc_cnt,
  4664. &rsrc_size);
  4665. if (unlikely(rc))
  4666. return -EIO;
  4667. if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
  4668. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4669. "3009 No available Resource Extents "
  4670. "for resource type 0x%x: Count: 0x%x, "
  4671. "Size 0x%x\n", type, rsrc_cnt,
  4672. rsrc_size);
  4673. return -ENOMEM;
  4674. }
  4675. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
  4676. "2903 Post resource extents type-0x%x: "
  4677. "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
  4678. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4679. if (!mbox)
  4680. return -ENOMEM;
  4681. rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
  4682. if (unlikely(rc)) {
  4683. rc = -EIO;
  4684. goto err_exit;
  4685. }
  4686. /*
  4687. * Figure out where the response is located. Then get local pointers
  4688. * to the response data. The port does not guarantee to respond to
  4689. * all extents counts request so update the local variable with the
  4690. * allocated count from the port.
  4691. */
  4692. if (emb == LPFC_SLI4_MBX_EMBED) {
  4693. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  4694. id_array = &rsrc_ext->u.rsp.id[0];
  4695. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  4696. } else {
  4697. virtaddr = mbox->sge_array->addr[0];
  4698. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  4699. rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  4700. id_array = &n_rsrc->id;
  4701. }
  4702. longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
  4703. rsrc_id_cnt = rsrc_cnt * rsrc_size;
  4704. /*
  4705. * Based on the resource size and count, correct the base and max
  4706. * resource values.
  4707. */
  4708. length = sizeof(struct lpfc_rsrc_blks);
  4709. switch (type) {
  4710. case LPFC_RSC_TYPE_FCOE_RPI:
  4711. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  4712. sizeof(unsigned long),
  4713. GFP_KERNEL);
  4714. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  4715. rc = -ENOMEM;
  4716. goto err_exit;
  4717. }
  4718. phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
  4719. sizeof(uint16_t),
  4720. GFP_KERNEL);
  4721. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  4722. kfree(phba->sli4_hba.rpi_bmask);
  4723. rc = -ENOMEM;
  4724. goto err_exit;
  4725. }
  4726. /*
  4727. * The next_rpi was initialized with the maximum available
  4728. * count but the port may allocate a smaller number. Catch
  4729. * that case and update the next_rpi.
  4730. */
  4731. phba->sli4_hba.next_rpi = rsrc_id_cnt;
  4732. /* Initialize local ptrs for common extent processing later. */
  4733. bmask = phba->sli4_hba.rpi_bmask;
  4734. ids = phba->sli4_hba.rpi_ids;
  4735. ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
  4736. break;
  4737. case LPFC_RSC_TYPE_FCOE_VPI:
  4738. phba->vpi_bmask = kzalloc(longs *
  4739. sizeof(unsigned long),
  4740. GFP_KERNEL);
  4741. if (unlikely(!phba->vpi_bmask)) {
  4742. rc = -ENOMEM;
  4743. goto err_exit;
  4744. }
  4745. phba->vpi_ids = kzalloc(rsrc_id_cnt *
  4746. sizeof(uint16_t),
  4747. GFP_KERNEL);
  4748. if (unlikely(!phba->vpi_ids)) {
  4749. kfree(phba->vpi_bmask);
  4750. rc = -ENOMEM;
  4751. goto err_exit;
  4752. }
  4753. /* Initialize local ptrs for common extent processing later. */
  4754. bmask = phba->vpi_bmask;
  4755. ids = phba->vpi_ids;
  4756. ext_blk_list = &phba->lpfc_vpi_blk_list;
  4757. break;
  4758. case LPFC_RSC_TYPE_FCOE_XRI:
  4759. phba->sli4_hba.xri_bmask = kzalloc(longs *
  4760. sizeof(unsigned long),
  4761. GFP_KERNEL);
  4762. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  4763. rc = -ENOMEM;
  4764. goto err_exit;
  4765. }
  4766. phba->sli4_hba.max_cfg_param.xri_used = 0;
  4767. phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
  4768. sizeof(uint16_t),
  4769. GFP_KERNEL);
  4770. if (unlikely(!phba->sli4_hba.xri_ids)) {
  4771. kfree(phba->sli4_hba.xri_bmask);
  4772. rc = -ENOMEM;
  4773. goto err_exit;
  4774. }
  4775. /* Initialize local ptrs for common extent processing later. */
  4776. bmask = phba->sli4_hba.xri_bmask;
  4777. ids = phba->sli4_hba.xri_ids;
  4778. ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
  4779. break;
  4780. case LPFC_RSC_TYPE_FCOE_VFI:
  4781. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  4782. sizeof(unsigned long),
  4783. GFP_KERNEL);
  4784. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  4785. rc = -ENOMEM;
  4786. goto err_exit;
  4787. }
  4788. phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
  4789. sizeof(uint16_t),
  4790. GFP_KERNEL);
  4791. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  4792. kfree(phba->sli4_hba.vfi_bmask);
  4793. rc = -ENOMEM;
  4794. goto err_exit;
  4795. }
  4796. /* Initialize local ptrs for common extent processing later. */
  4797. bmask = phba->sli4_hba.vfi_bmask;
  4798. ids = phba->sli4_hba.vfi_ids;
  4799. ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
  4800. break;
  4801. default:
  4802. /* Unsupported Opcode. Fail call. */
  4803. id_array = NULL;
  4804. bmask = NULL;
  4805. ids = NULL;
  4806. ext_blk_list = NULL;
  4807. goto err_exit;
  4808. }
  4809. /*
  4810. * Complete initializing the extent configuration with the
  4811. * allocated ids assigned to this function. The bitmask serves
  4812. * as an index into the array and manages the available ids. The
  4813. * array just stores the ids communicated to the port via the wqes.
  4814. */
  4815. for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
  4816. if ((i % 2) == 0)
  4817. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
  4818. &id_array[k]);
  4819. else
  4820. rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
  4821. &id_array[k]);
  4822. rsrc_blks = kzalloc(length, GFP_KERNEL);
  4823. if (unlikely(!rsrc_blks)) {
  4824. rc = -ENOMEM;
  4825. kfree(bmask);
  4826. kfree(ids);
  4827. goto err_exit;
  4828. }
  4829. rsrc_blks->rsrc_start = rsrc_id;
  4830. rsrc_blks->rsrc_size = rsrc_size;
  4831. list_add_tail(&rsrc_blks->list, ext_blk_list);
  4832. rsrc_start = rsrc_id;
  4833. if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
  4834. phba->sli4_hba.scsi_xri_start = rsrc_start +
  4835. lpfc_sli4_get_els_iocb_cnt(phba);
  4836. while (rsrc_id < (rsrc_start + rsrc_size)) {
  4837. ids[j] = rsrc_id;
  4838. rsrc_id++;
  4839. j++;
  4840. }
  4841. /* Entire word processed. Get next word.*/
  4842. if ((i % 2) == 1)
  4843. k++;
  4844. }
  4845. err_exit:
  4846. lpfc_sli4_mbox_cmd_free(phba, mbox);
  4847. return rc;
  4848. }
  4849. /**
  4850. * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
  4851. * @phba: Pointer to HBA context object.
  4852. * @type: the extent's type.
  4853. *
  4854. * This function deallocates all extents of a particular resource type.
  4855. * SLI4 does not allow for deallocating a particular extent range. It
  4856. * is the caller's responsibility to release all kernel memory resources.
  4857. **/
  4858. static int
  4859. lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
  4860. {
  4861. int rc;
  4862. uint32_t length, mbox_tmo = 0;
  4863. LPFC_MBOXQ_t *mbox;
  4864. struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
  4865. struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
  4866. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  4867. if (!mbox)
  4868. return -ENOMEM;
  4869. /*
  4870. * This function sends an embedded mailbox because it only sends the
  4871. * the resource type. All extents of this type are released by the
  4872. * port.
  4873. */
  4874. length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
  4875. sizeof(struct lpfc_sli4_cfg_mhdr));
  4876. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  4877. LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
  4878. length, LPFC_SLI4_MBX_EMBED);
  4879. /* Send an extents count of 0 - the dealloc doesn't use it. */
  4880. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
  4881. LPFC_SLI4_MBX_EMBED);
  4882. if (unlikely(rc)) {
  4883. rc = -EIO;
  4884. goto out_free_mbox;
  4885. }
  4886. if (!phba->sli4_hba.intr_enable)
  4887. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  4888. else {
  4889. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  4890. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  4891. }
  4892. if (unlikely(rc)) {
  4893. rc = -EIO;
  4894. goto out_free_mbox;
  4895. }
  4896. dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
  4897. if (bf_get(lpfc_mbox_hdr_status,
  4898. &dealloc_rsrc->header.cfg_shdr.response)) {
  4899. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  4900. "2919 Failed to release resource extents "
  4901. "for type %d - Status 0x%x Add'l Status 0x%x. "
  4902. "Resource memory not released.\n",
  4903. type,
  4904. bf_get(lpfc_mbox_hdr_status,
  4905. &dealloc_rsrc->header.cfg_shdr.response),
  4906. bf_get(lpfc_mbox_hdr_add_status,
  4907. &dealloc_rsrc->header.cfg_shdr.response));
  4908. rc = -EIO;
  4909. goto out_free_mbox;
  4910. }
  4911. /* Release kernel memory resources for the specific type. */
  4912. switch (type) {
  4913. case LPFC_RSC_TYPE_FCOE_VPI:
  4914. kfree(phba->vpi_bmask);
  4915. kfree(phba->vpi_ids);
  4916. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4917. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4918. &phba->lpfc_vpi_blk_list, list) {
  4919. list_del_init(&rsrc_blk->list);
  4920. kfree(rsrc_blk);
  4921. }
  4922. break;
  4923. case LPFC_RSC_TYPE_FCOE_XRI:
  4924. kfree(phba->sli4_hba.xri_bmask);
  4925. kfree(phba->sli4_hba.xri_ids);
  4926. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4927. &phba->sli4_hba.lpfc_xri_blk_list, list) {
  4928. list_del_init(&rsrc_blk->list);
  4929. kfree(rsrc_blk);
  4930. }
  4931. break;
  4932. case LPFC_RSC_TYPE_FCOE_VFI:
  4933. kfree(phba->sli4_hba.vfi_bmask);
  4934. kfree(phba->sli4_hba.vfi_ids);
  4935. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4936. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4937. &phba->sli4_hba.lpfc_vfi_blk_list, list) {
  4938. list_del_init(&rsrc_blk->list);
  4939. kfree(rsrc_blk);
  4940. }
  4941. break;
  4942. case LPFC_RSC_TYPE_FCOE_RPI:
  4943. /* RPI bitmask and physical id array are cleaned up earlier. */
  4944. list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
  4945. &phba->sli4_hba.lpfc_rpi_blk_list, list) {
  4946. list_del_init(&rsrc_blk->list);
  4947. kfree(rsrc_blk);
  4948. }
  4949. break;
  4950. default:
  4951. break;
  4952. }
  4953. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  4954. out_free_mbox:
  4955. mempool_free(mbox, phba->mbox_mem_pool);
  4956. return rc;
  4957. }
  4958. /**
  4959. * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
  4960. * @phba: Pointer to HBA context object.
  4961. *
  4962. * This function allocates all SLI4 resource identifiers.
  4963. **/
  4964. int
  4965. lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
  4966. {
  4967. int i, rc, error = 0;
  4968. uint16_t count, base;
  4969. unsigned long longs;
  4970. if (!phba->sli4_hba.rpi_hdrs_in_use)
  4971. phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  4972. if (phba->sli4_hba.extents_in_use) {
  4973. /*
  4974. * The port supports resource extents. The XRI, VPI, VFI, RPI
  4975. * resource extent count must be read and allocated before
  4976. * provisioning the resource id arrays.
  4977. */
  4978. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  4979. LPFC_IDX_RSRC_RDY) {
  4980. /*
  4981. * Extent-based resources are set - the driver could
  4982. * be in a port reset. Figure out if any corrective
  4983. * actions need to be taken.
  4984. */
  4985. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4986. LPFC_RSC_TYPE_FCOE_VFI);
  4987. if (rc != 0)
  4988. error++;
  4989. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4990. LPFC_RSC_TYPE_FCOE_VPI);
  4991. if (rc != 0)
  4992. error++;
  4993. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4994. LPFC_RSC_TYPE_FCOE_XRI);
  4995. if (rc != 0)
  4996. error++;
  4997. rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
  4998. LPFC_RSC_TYPE_FCOE_RPI);
  4999. if (rc != 0)
  5000. error++;
  5001. /*
  5002. * It's possible that the number of resources
  5003. * provided to this port instance changed between
  5004. * resets. Detect this condition and reallocate
  5005. * resources. Otherwise, there is no action.
  5006. */
  5007. if (error) {
  5008. lpfc_printf_log(phba, KERN_INFO,
  5009. LOG_MBOX | LOG_INIT,
  5010. "2931 Detected extent resource "
  5011. "change. Reallocating all "
  5012. "extents.\n");
  5013. rc = lpfc_sli4_dealloc_extent(phba,
  5014. LPFC_RSC_TYPE_FCOE_VFI);
  5015. rc = lpfc_sli4_dealloc_extent(phba,
  5016. LPFC_RSC_TYPE_FCOE_VPI);
  5017. rc = lpfc_sli4_dealloc_extent(phba,
  5018. LPFC_RSC_TYPE_FCOE_XRI);
  5019. rc = lpfc_sli4_dealloc_extent(phba,
  5020. LPFC_RSC_TYPE_FCOE_RPI);
  5021. } else
  5022. return 0;
  5023. }
  5024. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5025. if (unlikely(rc))
  5026. goto err_exit;
  5027. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5028. if (unlikely(rc))
  5029. goto err_exit;
  5030. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5031. if (unlikely(rc))
  5032. goto err_exit;
  5033. rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5034. if (unlikely(rc))
  5035. goto err_exit;
  5036. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5037. LPFC_IDX_RSRC_RDY);
  5038. return rc;
  5039. } else {
  5040. /*
  5041. * The port does not support resource extents. The XRI, VPI,
  5042. * VFI, RPI resource ids were determined from READ_CONFIG.
  5043. * Just allocate the bitmasks and provision the resource id
  5044. * arrays. If a port reset is active, the resources don't
  5045. * need any action - just exit.
  5046. */
  5047. if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
  5048. LPFC_IDX_RSRC_RDY) {
  5049. lpfc_sli4_dealloc_resource_identifiers(phba);
  5050. lpfc_sli4_remove_rpis(phba);
  5051. }
  5052. /* RPIs. */
  5053. count = phba->sli4_hba.max_cfg_param.max_rpi;
  5054. base = phba->sli4_hba.max_cfg_param.rpi_base;
  5055. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5056. phba->sli4_hba.rpi_bmask = kzalloc(longs *
  5057. sizeof(unsigned long),
  5058. GFP_KERNEL);
  5059. if (unlikely(!phba->sli4_hba.rpi_bmask)) {
  5060. rc = -ENOMEM;
  5061. goto err_exit;
  5062. }
  5063. phba->sli4_hba.rpi_ids = kzalloc(count *
  5064. sizeof(uint16_t),
  5065. GFP_KERNEL);
  5066. if (unlikely(!phba->sli4_hba.rpi_ids)) {
  5067. rc = -ENOMEM;
  5068. goto free_rpi_bmask;
  5069. }
  5070. for (i = 0; i < count; i++)
  5071. phba->sli4_hba.rpi_ids[i] = base + i;
  5072. /* VPIs. */
  5073. count = phba->sli4_hba.max_cfg_param.max_vpi;
  5074. base = phba->sli4_hba.max_cfg_param.vpi_base;
  5075. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5076. phba->vpi_bmask = kzalloc(longs *
  5077. sizeof(unsigned long),
  5078. GFP_KERNEL);
  5079. if (unlikely(!phba->vpi_bmask)) {
  5080. rc = -ENOMEM;
  5081. goto free_rpi_ids;
  5082. }
  5083. phba->vpi_ids = kzalloc(count *
  5084. sizeof(uint16_t),
  5085. GFP_KERNEL);
  5086. if (unlikely(!phba->vpi_ids)) {
  5087. rc = -ENOMEM;
  5088. goto free_vpi_bmask;
  5089. }
  5090. for (i = 0; i < count; i++)
  5091. phba->vpi_ids[i] = base + i;
  5092. /* XRIs. */
  5093. count = phba->sli4_hba.max_cfg_param.max_xri;
  5094. base = phba->sli4_hba.max_cfg_param.xri_base;
  5095. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5096. phba->sli4_hba.xri_bmask = kzalloc(longs *
  5097. sizeof(unsigned long),
  5098. GFP_KERNEL);
  5099. if (unlikely(!phba->sli4_hba.xri_bmask)) {
  5100. rc = -ENOMEM;
  5101. goto free_vpi_ids;
  5102. }
  5103. phba->sli4_hba.max_cfg_param.xri_used = 0;
  5104. phba->sli4_hba.xri_ids = kzalloc(count *
  5105. sizeof(uint16_t),
  5106. GFP_KERNEL);
  5107. if (unlikely(!phba->sli4_hba.xri_ids)) {
  5108. rc = -ENOMEM;
  5109. goto free_xri_bmask;
  5110. }
  5111. for (i = 0; i < count; i++)
  5112. phba->sli4_hba.xri_ids[i] = base + i;
  5113. /* VFIs. */
  5114. count = phba->sli4_hba.max_cfg_param.max_vfi;
  5115. base = phba->sli4_hba.max_cfg_param.vfi_base;
  5116. longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
  5117. phba->sli4_hba.vfi_bmask = kzalloc(longs *
  5118. sizeof(unsigned long),
  5119. GFP_KERNEL);
  5120. if (unlikely(!phba->sli4_hba.vfi_bmask)) {
  5121. rc = -ENOMEM;
  5122. goto free_xri_ids;
  5123. }
  5124. phba->sli4_hba.vfi_ids = kzalloc(count *
  5125. sizeof(uint16_t),
  5126. GFP_KERNEL);
  5127. if (unlikely(!phba->sli4_hba.vfi_ids)) {
  5128. rc = -ENOMEM;
  5129. goto free_vfi_bmask;
  5130. }
  5131. for (i = 0; i < count; i++)
  5132. phba->sli4_hba.vfi_ids[i] = base + i;
  5133. /*
  5134. * Mark all resources ready. An HBA reset doesn't need
  5135. * to reset the initialization.
  5136. */
  5137. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  5138. LPFC_IDX_RSRC_RDY);
  5139. return 0;
  5140. }
  5141. free_vfi_bmask:
  5142. kfree(phba->sli4_hba.vfi_bmask);
  5143. free_xri_ids:
  5144. kfree(phba->sli4_hba.xri_ids);
  5145. free_xri_bmask:
  5146. kfree(phba->sli4_hba.xri_bmask);
  5147. free_vpi_ids:
  5148. kfree(phba->vpi_ids);
  5149. free_vpi_bmask:
  5150. kfree(phba->vpi_bmask);
  5151. free_rpi_ids:
  5152. kfree(phba->sli4_hba.rpi_ids);
  5153. free_rpi_bmask:
  5154. kfree(phba->sli4_hba.rpi_bmask);
  5155. err_exit:
  5156. return rc;
  5157. }
  5158. /**
  5159. * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
  5160. * @phba: Pointer to HBA context object.
  5161. *
  5162. * This function allocates the number of elements for the specified
  5163. * resource type.
  5164. **/
  5165. int
  5166. lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
  5167. {
  5168. if (phba->sli4_hba.extents_in_use) {
  5169. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
  5170. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
  5171. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
  5172. lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
  5173. } else {
  5174. kfree(phba->vpi_bmask);
  5175. kfree(phba->vpi_ids);
  5176. bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5177. kfree(phba->sli4_hba.xri_bmask);
  5178. kfree(phba->sli4_hba.xri_ids);
  5179. kfree(phba->sli4_hba.vfi_bmask);
  5180. kfree(phba->sli4_hba.vfi_ids);
  5181. bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5182. bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  5183. }
  5184. return 0;
  5185. }
  5186. /**
  5187. * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
  5188. * @phba: Pointer to HBA context object.
  5189. * @type: The resource extent type.
  5190. * @extnt_count: buffer to hold port extent count response
  5191. * @extnt_size: buffer to hold port extent size response.
  5192. *
  5193. * This function calls the port to read the host allocated extents
  5194. * for a particular type.
  5195. **/
  5196. int
  5197. lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
  5198. uint16_t *extnt_cnt, uint16_t *extnt_size)
  5199. {
  5200. bool emb;
  5201. int rc = 0;
  5202. uint16_t curr_blks = 0;
  5203. uint32_t req_len, emb_len;
  5204. uint32_t alloc_len, mbox_tmo;
  5205. struct list_head *blk_list_head;
  5206. struct lpfc_rsrc_blks *rsrc_blk;
  5207. LPFC_MBOXQ_t *mbox;
  5208. void *virtaddr = NULL;
  5209. struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
  5210. struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
  5211. union lpfc_sli4_cfg_shdr *shdr;
  5212. switch (type) {
  5213. case LPFC_RSC_TYPE_FCOE_VPI:
  5214. blk_list_head = &phba->lpfc_vpi_blk_list;
  5215. break;
  5216. case LPFC_RSC_TYPE_FCOE_XRI:
  5217. blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
  5218. break;
  5219. case LPFC_RSC_TYPE_FCOE_VFI:
  5220. blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
  5221. break;
  5222. case LPFC_RSC_TYPE_FCOE_RPI:
  5223. blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
  5224. break;
  5225. default:
  5226. return -EIO;
  5227. }
  5228. /* Count the number of extents currently allocatd for this type. */
  5229. list_for_each_entry(rsrc_blk, blk_list_head, list) {
  5230. if (curr_blks == 0) {
  5231. /*
  5232. * The GET_ALLOCATED mailbox does not return the size,
  5233. * just the count. The size should be just the size
  5234. * stored in the current allocated block and all sizes
  5235. * for an extent type are the same so set the return
  5236. * value now.
  5237. */
  5238. *extnt_size = rsrc_blk->rsrc_size;
  5239. }
  5240. curr_blks++;
  5241. }
  5242. /* Calculate the total requested length of the dma memory. */
  5243. req_len = curr_blks * sizeof(uint16_t);
  5244. /*
  5245. * Calculate the size of an embedded mailbox. The uint32_t
  5246. * accounts for extents-specific word.
  5247. */
  5248. emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
  5249. sizeof(uint32_t);
  5250. /*
  5251. * Presume the allocation and response will fit into an embedded
  5252. * mailbox. If not true, reconfigure to a non-embedded mailbox.
  5253. */
  5254. emb = LPFC_SLI4_MBX_EMBED;
  5255. req_len = emb_len;
  5256. if (req_len > emb_len) {
  5257. req_len = curr_blks * sizeof(uint16_t) +
  5258. sizeof(union lpfc_sli4_cfg_shdr) +
  5259. sizeof(uint32_t);
  5260. emb = LPFC_SLI4_MBX_NEMBED;
  5261. }
  5262. mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5263. if (!mbox)
  5264. return -ENOMEM;
  5265. memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
  5266. alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  5267. LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
  5268. req_len, emb);
  5269. if (alloc_len < req_len) {
  5270. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5271. "2983 Allocated DMA memory size (x%x) is "
  5272. "less than the requested DMA memory "
  5273. "size (x%x)\n", alloc_len, req_len);
  5274. rc = -ENOMEM;
  5275. goto err_exit;
  5276. }
  5277. rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
  5278. if (unlikely(rc)) {
  5279. rc = -EIO;
  5280. goto err_exit;
  5281. }
  5282. if (!phba->sli4_hba.intr_enable)
  5283. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  5284. else {
  5285. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  5286. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  5287. }
  5288. if (unlikely(rc)) {
  5289. rc = -EIO;
  5290. goto err_exit;
  5291. }
  5292. /*
  5293. * Figure out where the response is located. Then get local pointers
  5294. * to the response data. The port does not guarantee to respond to
  5295. * all extents counts request so update the local variable with the
  5296. * allocated count from the port.
  5297. */
  5298. if (emb == LPFC_SLI4_MBX_EMBED) {
  5299. rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
  5300. shdr = &rsrc_ext->header.cfg_shdr;
  5301. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
  5302. } else {
  5303. virtaddr = mbox->sge_array->addr[0];
  5304. n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
  5305. shdr = &n_rsrc->cfg_shdr;
  5306. *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
  5307. }
  5308. if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
  5309. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
  5310. "2984 Failed to read allocated resources "
  5311. "for type %d - Status 0x%x Add'l Status 0x%x.\n",
  5312. type,
  5313. bf_get(lpfc_mbox_hdr_status, &shdr->response),
  5314. bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
  5315. rc = -EIO;
  5316. goto err_exit;
  5317. }
  5318. err_exit:
  5319. lpfc_sli4_mbox_cmd_free(phba, mbox);
  5320. return rc;
  5321. }
  5322. /**
  5323. * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
  5324. * @phba: pointer to lpfc hba data structure.
  5325. *
  5326. * This routine walks the list of els buffers that have been allocated and
  5327. * repost them to the port by using SGL block post. This is needed after a
  5328. * pci_function_reset/warm_start or start. It attempts to construct blocks
  5329. * of els buffer sgls which contains contiguous xris and uses the non-embedded
  5330. * SGL block post mailbox commands to post them to the port. For single els
  5331. * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
  5332. * mailbox command for posting.
  5333. *
  5334. * Returns: 0 = success, non-zero failure.
  5335. **/
  5336. static int
  5337. lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
  5338. {
  5339. struct lpfc_sglq *sglq_entry = NULL;
  5340. struct lpfc_sglq *sglq_entry_next = NULL;
  5341. struct lpfc_sglq *sglq_entry_first = NULL;
  5342. int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
  5343. int last_xritag = NO_XRI;
  5344. LIST_HEAD(prep_sgl_list);
  5345. LIST_HEAD(blck_sgl_list);
  5346. LIST_HEAD(allc_sgl_list);
  5347. LIST_HEAD(post_sgl_list);
  5348. LIST_HEAD(free_sgl_list);
  5349. spin_lock(&phba->hbalock);
  5350. list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
  5351. spin_unlock(&phba->hbalock);
  5352. list_for_each_entry_safe(sglq_entry, sglq_entry_next,
  5353. &allc_sgl_list, list) {
  5354. list_del_init(&sglq_entry->list);
  5355. block_cnt++;
  5356. if ((last_xritag != NO_XRI) &&
  5357. (sglq_entry->sli4_xritag != last_xritag + 1)) {
  5358. /* a hole in xri block, form a sgl posting block */
  5359. list_splice_init(&prep_sgl_list, &blck_sgl_list);
  5360. post_cnt = block_cnt - 1;
  5361. /* prepare list for next posting block */
  5362. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5363. block_cnt = 1;
  5364. } else {
  5365. /* prepare list for next posting block */
  5366. list_add_tail(&sglq_entry->list, &prep_sgl_list);
  5367. /* enough sgls for non-embed sgl mbox command */
  5368. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  5369. list_splice_init(&prep_sgl_list,
  5370. &blck_sgl_list);
  5371. post_cnt = block_cnt;
  5372. block_cnt = 0;
  5373. }
  5374. }
  5375. num_posted++;
  5376. /* keep track of last sgl's xritag */
  5377. last_xritag = sglq_entry->sli4_xritag;
  5378. /* end of repost sgl list condition for els buffers */
  5379. if (num_posted == phba->sli4_hba.els_xri_cnt) {
  5380. if (post_cnt == 0) {
  5381. list_splice_init(&prep_sgl_list,
  5382. &blck_sgl_list);
  5383. post_cnt = block_cnt;
  5384. } else if (block_cnt == 1) {
  5385. status = lpfc_sli4_post_sgl(phba,
  5386. sglq_entry->phys, 0,
  5387. sglq_entry->sli4_xritag);
  5388. if (!status) {
  5389. /* successful, put sgl to posted list */
  5390. list_add_tail(&sglq_entry->list,
  5391. &post_sgl_list);
  5392. } else {
  5393. /* Failure, put sgl to free list */
  5394. lpfc_printf_log(phba, KERN_WARNING,
  5395. LOG_SLI,
  5396. "3159 Failed to post els "
  5397. "sgl, xritag:x%x\n",
  5398. sglq_entry->sli4_xritag);
  5399. list_add_tail(&sglq_entry->list,
  5400. &free_sgl_list);
  5401. spin_lock_irq(&phba->hbalock);
  5402. phba->sli4_hba.els_xri_cnt--;
  5403. spin_unlock_irq(&phba->hbalock);
  5404. }
  5405. }
  5406. }
  5407. /* continue until a nembed page worth of sgls */
  5408. if (post_cnt == 0)
  5409. continue;
  5410. /* post the els buffer list sgls as a block */
  5411. status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
  5412. post_cnt);
  5413. if (!status) {
  5414. /* success, put sgl list to posted sgl list */
  5415. list_splice_init(&blck_sgl_list, &post_sgl_list);
  5416. } else {
  5417. /* Failure, put sgl list to free sgl list */
  5418. sglq_entry_first = list_first_entry(&blck_sgl_list,
  5419. struct lpfc_sglq,
  5420. list);
  5421. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  5422. "3160 Failed to post els sgl-list, "
  5423. "xritag:x%x-x%x\n",
  5424. sglq_entry_first->sli4_xritag,
  5425. (sglq_entry_first->sli4_xritag +
  5426. post_cnt - 1));
  5427. list_splice_init(&blck_sgl_list, &free_sgl_list);
  5428. spin_lock_irq(&phba->hbalock);
  5429. phba->sli4_hba.els_xri_cnt -= post_cnt;
  5430. spin_unlock_irq(&phba->hbalock);
  5431. }
  5432. /* don't reset xirtag due to hole in xri block */
  5433. if (block_cnt == 0)
  5434. last_xritag = NO_XRI;
  5435. /* reset els sgl post count for next round of posting */
  5436. post_cnt = 0;
  5437. }
  5438. /* free the els sgls failed to post */
  5439. lpfc_free_sgl_list(phba, &free_sgl_list);
  5440. /* push els sgls posted to the availble list */
  5441. if (!list_empty(&post_sgl_list)) {
  5442. spin_lock(&phba->hbalock);
  5443. list_splice_init(&post_sgl_list,
  5444. &phba->sli4_hba.lpfc_sgl_list);
  5445. spin_unlock(&phba->hbalock);
  5446. } else {
  5447. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  5448. "3161 Failure to post els sgl to port.\n");
  5449. return -EIO;
  5450. }
  5451. return 0;
  5452. }
  5453. /**
  5454. * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
  5455. * @phba: Pointer to HBA context object.
  5456. *
  5457. * This function is the main SLI4 device intialization PCI function. This
  5458. * function is called by the HBA intialization code, HBA reset code and
  5459. * HBA error attention handler code. Caller is not required to hold any
  5460. * locks.
  5461. **/
  5462. int
  5463. lpfc_sli4_hba_setup(struct lpfc_hba *phba)
  5464. {
  5465. int rc;
  5466. LPFC_MBOXQ_t *mboxq;
  5467. struct lpfc_mqe *mqe;
  5468. uint8_t *vpd;
  5469. uint32_t vpd_size;
  5470. uint32_t ftr_rsp = 0;
  5471. struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
  5472. struct lpfc_vport *vport = phba->pport;
  5473. struct lpfc_dmabuf *mp;
  5474. /* Perform a PCI function reset to start from clean */
  5475. rc = lpfc_pci_function_reset(phba);
  5476. if (unlikely(rc))
  5477. return -ENODEV;
  5478. /* Check the HBA Host Status Register for readyness */
  5479. rc = lpfc_sli4_post_status_check(phba);
  5480. if (unlikely(rc))
  5481. return -ENODEV;
  5482. else {
  5483. spin_lock_irq(&phba->hbalock);
  5484. phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
  5485. spin_unlock_irq(&phba->hbalock);
  5486. }
  5487. /*
  5488. * Allocate a single mailbox container for initializing the
  5489. * port.
  5490. */
  5491. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  5492. if (!mboxq)
  5493. return -ENOMEM;
  5494. /* Issue READ_REV to collect vpd and FW information. */
  5495. vpd_size = SLI4_PAGE_SIZE;
  5496. vpd = kzalloc(vpd_size, GFP_KERNEL);
  5497. if (!vpd) {
  5498. rc = -ENOMEM;
  5499. goto out_free_mbox;
  5500. }
  5501. rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
  5502. if (unlikely(rc)) {
  5503. kfree(vpd);
  5504. goto out_free_mbox;
  5505. }
  5506. mqe = &mboxq->u.mqe;
  5507. phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
  5508. if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
  5509. phba->hba_flag |= HBA_FCOE_MODE;
  5510. else
  5511. phba->hba_flag &= ~HBA_FCOE_MODE;
  5512. if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
  5513. LPFC_DCBX_CEE_MODE)
  5514. phba->hba_flag |= HBA_FIP_SUPPORT;
  5515. else
  5516. phba->hba_flag &= ~HBA_FIP_SUPPORT;
  5517. phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
  5518. if (phba->sli_rev != LPFC_SLI_REV4) {
  5519. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5520. "0376 READ_REV Error. SLI Level %d "
  5521. "FCoE enabled %d\n",
  5522. phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
  5523. rc = -EIO;
  5524. kfree(vpd);
  5525. goto out_free_mbox;
  5526. }
  5527. /*
  5528. * Continue initialization with default values even if driver failed
  5529. * to read FCoE param config regions, only read parameters if the
  5530. * board is FCoE
  5531. */
  5532. if (phba->hba_flag & HBA_FCOE_MODE &&
  5533. lpfc_sli4_read_fcoe_params(phba))
  5534. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
  5535. "2570 Failed to read FCoE parameters\n");
  5536. /*
  5537. * Retrieve sli4 device physical port name, failure of doing it
  5538. * is considered as non-fatal.
  5539. */
  5540. rc = lpfc_sli4_retrieve_pport_name(phba);
  5541. if (!rc)
  5542. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5543. "3080 Successful retrieving SLI4 device "
  5544. "physical port name: %s.\n", phba->Port);
  5545. /*
  5546. * Evaluate the read rev and vpd data. Populate the driver
  5547. * state with the results. If this routine fails, the failure
  5548. * is not fatal as the driver will use generic values.
  5549. */
  5550. rc = lpfc_parse_vpd(phba, vpd, vpd_size);
  5551. if (unlikely(!rc)) {
  5552. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5553. "0377 Error %d parsing vpd. "
  5554. "Using defaults.\n", rc);
  5555. rc = 0;
  5556. }
  5557. kfree(vpd);
  5558. /* Save information as VPD data */
  5559. phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
  5560. phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
  5561. phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
  5562. phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
  5563. &mqe->un.read_rev);
  5564. phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
  5565. &mqe->un.read_rev);
  5566. phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
  5567. &mqe->un.read_rev);
  5568. phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
  5569. &mqe->un.read_rev);
  5570. phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
  5571. memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
  5572. phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
  5573. memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
  5574. phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
  5575. memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
  5576. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  5577. "(%d):0380 READ_REV Status x%x "
  5578. "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
  5579. mboxq->vport ? mboxq->vport->vpi : 0,
  5580. bf_get(lpfc_mqe_status, mqe),
  5581. phba->vpd.rev.opFwName,
  5582. phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
  5583. phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
  5584. /*
  5585. * Discover the port's supported feature set and match it against the
  5586. * hosts requests.
  5587. */
  5588. lpfc_request_features(phba, mboxq);
  5589. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5590. if (unlikely(rc)) {
  5591. rc = -EIO;
  5592. goto out_free_mbox;
  5593. }
  5594. /*
  5595. * The port must support FCP initiator mode as this is the
  5596. * only mode running in the host.
  5597. */
  5598. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
  5599. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5600. "0378 No support for fcpi mode.\n");
  5601. ftr_rsp++;
  5602. }
  5603. if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
  5604. phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
  5605. else
  5606. phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
  5607. /*
  5608. * If the port cannot support the host's requested features
  5609. * then turn off the global config parameters to disable the
  5610. * feature in the driver. This is not a fatal error.
  5611. */
  5612. phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
  5613. if (phba->cfg_enable_bg) {
  5614. if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
  5615. phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
  5616. else
  5617. ftr_rsp++;
  5618. }
  5619. if (phba->max_vpi && phba->cfg_enable_npiv &&
  5620. !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5621. ftr_rsp++;
  5622. if (ftr_rsp) {
  5623. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  5624. "0379 Feature Mismatch Data: x%08x %08x "
  5625. "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
  5626. mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
  5627. phba->cfg_enable_npiv, phba->max_vpi);
  5628. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
  5629. phba->cfg_enable_bg = 0;
  5630. if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
  5631. phba->cfg_enable_npiv = 0;
  5632. }
  5633. /* These SLI3 features are assumed in SLI4 */
  5634. spin_lock_irq(&phba->hbalock);
  5635. phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
  5636. spin_unlock_irq(&phba->hbalock);
  5637. /*
  5638. * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
  5639. * calls depends on these resources to complete port setup.
  5640. */
  5641. rc = lpfc_sli4_alloc_resource_identifiers(phba);
  5642. if (rc) {
  5643. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5644. "2920 Failed to alloc Resource IDs "
  5645. "rc = x%x\n", rc);
  5646. goto out_free_mbox;
  5647. }
  5648. /* Read the port's service parameters. */
  5649. rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
  5650. if (rc) {
  5651. phba->link_state = LPFC_HBA_ERROR;
  5652. rc = -ENOMEM;
  5653. goto out_free_mbox;
  5654. }
  5655. mboxq->vport = vport;
  5656. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5657. mp = (struct lpfc_dmabuf *) mboxq->context1;
  5658. if (rc == MBX_SUCCESS) {
  5659. memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
  5660. rc = 0;
  5661. }
  5662. /*
  5663. * This memory was allocated by the lpfc_read_sparam routine. Release
  5664. * it to the mbuf pool.
  5665. */
  5666. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  5667. kfree(mp);
  5668. mboxq->context1 = NULL;
  5669. if (unlikely(rc)) {
  5670. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5671. "0382 READ_SPARAM command failed "
  5672. "status %d, mbxStatus x%x\n",
  5673. rc, bf_get(lpfc_mqe_status, mqe));
  5674. phba->link_state = LPFC_HBA_ERROR;
  5675. rc = -EIO;
  5676. goto out_free_mbox;
  5677. }
  5678. lpfc_update_vport_wwn(vport);
  5679. /* Update the fc_host data structures with new wwn. */
  5680. fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
  5681. fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
  5682. /* update host els and scsi xri-sgl sizes and mappings */
  5683. rc = lpfc_sli4_xri_sgl_update(phba);
  5684. if (unlikely(rc)) {
  5685. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5686. "1400 Failed to update xri-sgl size and "
  5687. "mapping: %d\n", rc);
  5688. goto out_free_mbox;
  5689. }
  5690. /* register the els sgl pool to the port */
  5691. rc = lpfc_sli4_repost_els_sgl_list(phba);
  5692. if (unlikely(rc)) {
  5693. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5694. "0582 Error %d during els sgl post "
  5695. "operation\n", rc);
  5696. rc = -ENODEV;
  5697. goto out_free_mbox;
  5698. }
  5699. /* register the allocated scsi sgl pool to the port */
  5700. rc = lpfc_sli4_repost_scsi_sgl_list(phba);
  5701. if (unlikely(rc)) {
  5702. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5703. "0383 Error %d during scsi sgl post "
  5704. "operation\n", rc);
  5705. /* Some Scsi buffers were moved to the abort scsi list */
  5706. /* A pci function reset will repost them */
  5707. rc = -ENODEV;
  5708. goto out_free_mbox;
  5709. }
  5710. /* Post the rpi header region to the device. */
  5711. rc = lpfc_sli4_post_all_rpi_hdrs(phba);
  5712. if (unlikely(rc)) {
  5713. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5714. "0393 Error %d during rpi post operation\n",
  5715. rc);
  5716. rc = -ENODEV;
  5717. goto out_free_mbox;
  5718. }
  5719. lpfc_sli4_node_prep(phba);
  5720. /* Create all the SLI4 queues */
  5721. rc = lpfc_sli4_queue_create(phba);
  5722. if (rc) {
  5723. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  5724. "3089 Failed to allocate queues\n");
  5725. rc = -ENODEV;
  5726. goto out_stop_timers;
  5727. }
  5728. /* Set up all the queues to the device */
  5729. rc = lpfc_sli4_queue_setup(phba);
  5730. if (unlikely(rc)) {
  5731. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5732. "0381 Error %d during queue setup.\n ", rc);
  5733. goto out_destroy_queue;
  5734. }
  5735. /* Arm the CQs and then EQs on device */
  5736. lpfc_sli4_arm_cqeq_intr(phba);
  5737. /* Indicate device interrupt mode */
  5738. phba->sli4_hba.intr_enable = 1;
  5739. /* Allow asynchronous mailbox command to go through */
  5740. spin_lock_irq(&phba->hbalock);
  5741. phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  5742. spin_unlock_irq(&phba->hbalock);
  5743. /* Post receive buffers to the device */
  5744. lpfc_sli4_rb_setup(phba);
  5745. /* Reset HBA FCF states after HBA reset */
  5746. phba->fcf.fcf_flag = 0;
  5747. phba->fcf.current_rec.flag = 0;
  5748. /* Start the ELS watchdog timer */
  5749. mod_timer(&vport->els_tmofunc,
  5750. jiffies + HZ * (phba->fc_ratov * 2));
  5751. /* Start heart beat timer */
  5752. mod_timer(&phba->hb_tmofunc,
  5753. jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
  5754. phba->hb_outstanding = 0;
  5755. phba->last_completion_time = jiffies;
  5756. /* Start error attention (ERATT) polling timer */
  5757. mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
  5758. /* Enable PCIe device Advanced Error Reporting (AER) if configured */
  5759. if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
  5760. rc = pci_enable_pcie_error_reporting(phba->pcidev);
  5761. if (!rc) {
  5762. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5763. "2829 This device supports "
  5764. "Advanced Error Reporting (AER)\n");
  5765. spin_lock_irq(&phba->hbalock);
  5766. phba->hba_flag |= HBA_AER_ENABLED;
  5767. spin_unlock_irq(&phba->hbalock);
  5768. } else {
  5769. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  5770. "2830 This device does not support "
  5771. "Advanced Error Reporting (AER)\n");
  5772. phba->cfg_aer_support = 0;
  5773. }
  5774. rc = 0;
  5775. }
  5776. if (!(phba->hba_flag & HBA_FCOE_MODE)) {
  5777. /*
  5778. * The FC Port needs to register FCFI (index 0)
  5779. */
  5780. lpfc_reg_fcfi(phba, mboxq);
  5781. mboxq->vport = phba->pport;
  5782. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5783. if (rc != MBX_SUCCESS)
  5784. goto out_unset_queue;
  5785. rc = 0;
  5786. phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
  5787. &mboxq->u.mqe.un.reg_fcfi);
  5788. /* Check if the port is configured to be disabled */
  5789. lpfc_sli_read_link_ste(phba);
  5790. }
  5791. /*
  5792. * The port is ready, set the host's link state to LINK_DOWN
  5793. * in preparation for link interrupts.
  5794. */
  5795. spin_lock_irq(&phba->hbalock);
  5796. phba->link_state = LPFC_LINK_DOWN;
  5797. spin_unlock_irq(&phba->hbalock);
  5798. if (!(phba->hba_flag & HBA_FCOE_MODE) &&
  5799. (phba->hba_flag & LINK_DISABLED)) {
  5800. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5801. "3103 Adapter Link is disabled.\n");
  5802. lpfc_down_link(phba, mboxq);
  5803. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  5804. if (rc != MBX_SUCCESS) {
  5805. lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
  5806. "3104 Adapter failed to issue "
  5807. "DOWN_LINK mbox cmd, rc:x%x\n", rc);
  5808. goto out_unset_queue;
  5809. }
  5810. } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
  5811. /* don't perform init_link on SLI4 FC port loopback test */
  5812. if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
  5813. rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
  5814. if (rc)
  5815. goto out_unset_queue;
  5816. }
  5817. }
  5818. mempool_free(mboxq, phba->mbox_mem_pool);
  5819. return rc;
  5820. out_unset_queue:
  5821. /* Unset all the queues set up in this routine when error out */
  5822. lpfc_sli4_queue_unset(phba);
  5823. out_destroy_queue:
  5824. lpfc_sli4_queue_destroy(phba);
  5825. out_stop_timers:
  5826. lpfc_stop_hba_timers(phba);
  5827. out_free_mbox:
  5828. mempool_free(mboxq, phba->mbox_mem_pool);
  5829. return rc;
  5830. }
  5831. /**
  5832. * lpfc_mbox_timeout - Timeout call back function for mbox timer
  5833. * @ptr: context object - pointer to hba structure.
  5834. *
  5835. * This is the callback function for mailbox timer. The mailbox
  5836. * timer is armed when a new mailbox command is issued and the timer
  5837. * is deleted when the mailbox complete. The function is called by
  5838. * the kernel timer code when a mailbox does not complete within
  5839. * expected time. This function wakes up the worker thread to
  5840. * process the mailbox timeout and returns. All the processing is
  5841. * done by the worker thread function lpfc_mbox_timeout_handler.
  5842. **/
  5843. void
  5844. lpfc_mbox_timeout(unsigned long ptr)
  5845. {
  5846. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  5847. unsigned long iflag;
  5848. uint32_t tmo_posted;
  5849. spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
  5850. tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
  5851. if (!tmo_posted)
  5852. phba->pport->work_port_events |= WORKER_MBOX_TMO;
  5853. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
  5854. if (!tmo_posted)
  5855. lpfc_worker_wake_up(phba);
  5856. return;
  5857. }
  5858. /**
  5859. * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
  5860. * @phba: Pointer to HBA context object.
  5861. *
  5862. * This function is called from worker thread when a mailbox command times out.
  5863. * The caller is not required to hold any locks. This function will reset the
  5864. * HBA and recover all the pending commands.
  5865. **/
  5866. void
  5867. lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
  5868. {
  5869. LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
  5870. MAILBOX_t *mb = &pmbox->u.mb;
  5871. struct lpfc_sli *psli = &phba->sli;
  5872. struct lpfc_sli_ring *pring;
  5873. /* Check the pmbox pointer first. There is a race condition
  5874. * between the mbox timeout handler getting executed in the
  5875. * worklist and the mailbox actually completing. When this
  5876. * race condition occurs, the mbox_active will be NULL.
  5877. */
  5878. spin_lock_irq(&phba->hbalock);
  5879. if (pmbox == NULL) {
  5880. lpfc_printf_log(phba, KERN_WARNING,
  5881. LOG_MBOX | LOG_SLI,
  5882. "0353 Active Mailbox cleared - mailbox timeout "
  5883. "exiting\n");
  5884. spin_unlock_irq(&phba->hbalock);
  5885. return;
  5886. }
  5887. /* Mbox cmd <mbxCommand> timeout */
  5888. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5889. "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
  5890. mb->mbxCommand,
  5891. phba->pport->port_state,
  5892. phba->sli.sli_flag,
  5893. phba->sli.mbox_active);
  5894. spin_unlock_irq(&phba->hbalock);
  5895. /* Setting state unknown so lpfc_sli_abort_iocb_ring
  5896. * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
  5897. * it to fail all outstanding SCSI IO.
  5898. */
  5899. spin_lock_irq(&phba->pport->work_port_lock);
  5900. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  5901. spin_unlock_irq(&phba->pport->work_port_lock);
  5902. spin_lock_irq(&phba->hbalock);
  5903. phba->link_state = LPFC_LINK_UNKNOWN;
  5904. psli->sli_flag &= ~LPFC_SLI_ACTIVE;
  5905. spin_unlock_irq(&phba->hbalock);
  5906. pring = &psli->ring[psli->fcp_ring];
  5907. lpfc_sli_abort_iocb_ring(phba, pring);
  5908. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5909. "0345 Resetting board due to mailbox timeout\n");
  5910. /* Reset the HBA device */
  5911. lpfc_reset_hba(phba);
  5912. }
  5913. /**
  5914. * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
  5915. * @phba: Pointer to HBA context object.
  5916. * @pmbox: Pointer to mailbox object.
  5917. * @flag: Flag indicating how the mailbox need to be processed.
  5918. *
  5919. * This function is called by discovery code and HBA management code
  5920. * to submit a mailbox command to firmware with SLI-3 interface spec. This
  5921. * function gets the hbalock to protect the data structures.
  5922. * The mailbox command can be submitted in polling mode, in which case
  5923. * this function will wait in a polling loop for the completion of the
  5924. * mailbox.
  5925. * If the mailbox is submitted in no_wait mode (not polling) the
  5926. * function will submit the command and returns immediately without waiting
  5927. * for the mailbox completion. The no_wait is supported only when HBA
  5928. * is in SLI2/SLI3 mode - interrupts are enabled.
  5929. * The SLI interface allows only one mailbox pending at a time. If the
  5930. * mailbox is issued in polling mode and there is already a mailbox
  5931. * pending, then the function will return an error. If the mailbox is issued
  5932. * in NO_WAIT mode and there is a mailbox pending already, the function
  5933. * will return MBX_BUSY after queuing the mailbox into mailbox queue.
  5934. * The sli layer owns the mailbox object until the completion of mailbox
  5935. * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
  5936. * return codes the caller owns the mailbox command after the return of
  5937. * the function.
  5938. **/
  5939. static int
  5940. lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
  5941. uint32_t flag)
  5942. {
  5943. MAILBOX_t *mb;
  5944. struct lpfc_sli *psli = &phba->sli;
  5945. uint32_t status, evtctr;
  5946. uint32_t ha_copy, hc_copy;
  5947. int i;
  5948. unsigned long timeout;
  5949. unsigned long drvr_flag = 0;
  5950. uint32_t word0, ldata;
  5951. void __iomem *to_slim;
  5952. int processing_queue = 0;
  5953. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  5954. if (!pmbox) {
  5955. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  5956. /* processing mbox queue from intr_handler */
  5957. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  5958. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5959. return MBX_SUCCESS;
  5960. }
  5961. processing_queue = 1;
  5962. pmbox = lpfc_mbox_get(phba);
  5963. if (!pmbox) {
  5964. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5965. return MBX_SUCCESS;
  5966. }
  5967. }
  5968. if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
  5969. pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
  5970. if(!pmbox->vport) {
  5971. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5972. lpfc_printf_log(phba, KERN_ERR,
  5973. LOG_MBOX | LOG_VPORT,
  5974. "1806 Mbox x%x failed. No vport\n",
  5975. pmbox->u.mb.mbxCommand);
  5976. dump_stack();
  5977. goto out_not_finished;
  5978. }
  5979. }
  5980. /* If the PCI channel is in offline state, do not post mbox. */
  5981. if (unlikely(pci_channel_offline(phba->pcidev))) {
  5982. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5983. goto out_not_finished;
  5984. }
  5985. /* If HBA has a deferred error attention, fail the iocb. */
  5986. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  5987. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5988. goto out_not_finished;
  5989. }
  5990. psli = &phba->sli;
  5991. mb = &pmbox->u.mb;
  5992. status = MBX_SUCCESS;
  5993. if (phba->link_state == LPFC_HBA_ERROR) {
  5994. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  5995. /* Mbox command <mbxCommand> cannot issue */
  5996. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  5997. "(%d):0311 Mailbox command x%x cannot "
  5998. "issue Data: x%x x%x\n",
  5999. pmbox->vport ? pmbox->vport->vpi : 0,
  6000. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6001. goto out_not_finished;
  6002. }
  6003. if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
  6004. if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
  6005. !(hc_copy & HC_MBINT_ENA)) {
  6006. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6007. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6008. "(%d):2528 Mailbox command x%x cannot "
  6009. "issue Data: x%x x%x\n",
  6010. pmbox->vport ? pmbox->vport->vpi : 0,
  6011. pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
  6012. goto out_not_finished;
  6013. }
  6014. }
  6015. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6016. /* Polling for a mbox command when another one is already active
  6017. * is not allowed in SLI. Also, the driver must have established
  6018. * SLI2 mode to queue and process multiple mbox commands.
  6019. */
  6020. if (flag & MBX_POLL) {
  6021. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6022. /* Mbox command <mbxCommand> cannot issue */
  6023. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6024. "(%d):2529 Mailbox command x%x "
  6025. "cannot issue Data: x%x x%x\n",
  6026. pmbox->vport ? pmbox->vport->vpi : 0,
  6027. pmbox->u.mb.mbxCommand,
  6028. psli->sli_flag, flag);
  6029. goto out_not_finished;
  6030. }
  6031. if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
  6032. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6033. /* Mbox command <mbxCommand> cannot issue */
  6034. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6035. "(%d):2530 Mailbox command x%x "
  6036. "cannot issue Data: x%x x%x\n",
  6037. pmbox->vport ? pmbox->vport->vpi : 0,
  6038. pmbox->u.mb.mbxCommand,
  6039. psli->sli_flag, flag);
  6040. goto out_not_finished;
  6041. }
  6042. /* Another mailbox command is still being processed, queue this
  6043. * command to be processed later.
  6044. */
  6045. lpfc_mbox_put(phba, pmbox);
  6046. /* Mbox cmd issue - BUSY */
  6047. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6048. "(%d):0308 Mbox cmd issue - BUSY Data: "
  6049. "x%x x%x x%x x%x\n",
  6050. pmbox->vport ? pmbox->vport->vpi : 0xffffff,
  6051. mb->mbxCommand, phba->pport->port_state,
  6052. psli->sli_flag, flag);
  6053. psli->slistat.mbox_busy++;
  6054. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6055. if (pmbox->vport) {
  6056. lpfc_debugfs_disc_trc(pmbox->vport,
  6057. LPFC_DISC_TRC_MBOX_VPORT,
  6058. "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
  6059. (uint32_t)mb->mbxCommand,
  6060. mb->un.varWords[0], mb->un.varWords[1]);
  6061. }
  6062. else {
  6063. lpfc_debugfs_disc_trc(phba->pport,
  6064. LPFC_DISC_TRC_MBOX,
  6065. "MBOX Bsy: cmd:x%x mb:x%x x%x",
  6066. (uint32_t)mb->mbxCommand,
  6067. mb->un.varWords[0], mb->un.varWords[1]);
  6068. }
  6069. return MBX_BUSY;
  6070. }
  6071. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6072. /* If we are not polling, we MUST be in SLI2 mode */
  6073. if (flag != MBX_POLL) {
  6074. if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
  6075. (mb->mbxCommand != MBX_KILL_BOARD)) {
  6076. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6077. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6078. /* Mbox command <mbxCommand> cannot issue */
  6079. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6080. "(%d):2531 Mailbox command x%x "
  6081. "cannot issue Data: x%x x%x\n",
  6082. pmbox->vport ? pmbox->vport->vpi : 0,
  6083. pmbox->u.mb.mbxCommand,
  6084. psli->sli_flag, flag);
  6085. goto out_not_finished;
  6086. }
  6087. /* timeout active mbox command */
  6088. mod_timer(&psli->mbox_tmo, (jiffies +
  6089. (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
  6090. }
  6091. /* Mailbox cmd <cmd> issue */
  6092. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6093. "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
  6094. "x%x\n",
  6095. pmbox->vport ? pmbox->vport->vpi : 0,
  6096. mb->mbxCommand, phba->pport->port_state,
  6097. psli->sli_flag, flag);
  6098. if (mb->mbxCommand != MBX_HEARTBEAT) {
  6099. if (pmbox->vport) {
  6100. lpfc_debugfs_disc_trc(pmbox->vport,
  6101. LPFC_DISC_TRC_MBOX_VPORT,
  6102. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6103. (uint32_t)mb->mbxCommand,
  6104. mb->un.varWords[0], mb->un.varWords[1]);
  6105. }
  6106. else {
  6107. lpfc_debugfs_disc_trc(phba->pport,
  6108. LPFC_DISC_TRC_MBOX,
  6109. "MBOX Send: cmd:x%x mb:x%x x%x",
  6110. (uint32_t)mb->mbxCommand,
  6111. mb->un.varWords[0], mb->un.varWords[1]);
  6112. }
  6113. }
  6114. psli->slistat.mbox_cmd++;
  6115. evtctr = psli->slistat.mbox_event;
  6116. /* next set own bit for the adapter and copy over command word */
  6117. mb->mbxOwner = OWN_CHIP;
  6118. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6119. /* Populate mbox extension offset word. */
  6120. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
  6121. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6122. = (uint8_t *)phba->mbox_ext
  6123. - (uint8_t *)phba->mbox;
  6124. }
  6125. /* Copy the mailbox extension data */
  6126. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6127. lpfc_sli_pcimem_bcopy(pmbox->context2,
  6128. (uint8_t *)phba->mbox_ext,
  6129. pmbox->in_ext_byte_len);
  6130. }
  6131. /* Copy command data to host SLIM area */
  6132. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6133. } else {
  6134. /* Populate mbox extension offset word. */
  6135. if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
  6136. *(((uint32_t *)mb) + pmbox->mbox_offset_word)
  6137. = MAILBOX_HBA_EXT_OFFSET;
  6138. /* Copy the mailbox extension data */
  6139. if (pmbox->in_ext_byte_len && pmbox->context2) {
  6140. lpfc_memcpy_to_slim(phba->MBslimaddr +
  6141. MAILBOX_HBA_EXT_OFFSET,
  6142. pmbox->context2, pmbox->in_ext_byte_len);
  6143. }
  6144. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6145. /* copy command data into host mbox for cmpl */
  6146. lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
  6147. }
  6148. /* First copy mbox command data to HBA SLIM, skip past first
  6149. word */
  6150. to_slim = phba->MBslimaddr + sizeof (uint32_t);
  6151. lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
  6152. MAILBOX_CMD_SIZE - sizeof (uint32_t));
  6153. /* Next copy over first word, with mbxOwner set */
  6154. ldata = *((uint32_t *)mb);
  6155. to_slim = phba->MBslimaddr;
  6156. writel(ldata, to_slim);
  6157. readl(to_slim); /* flush */
  6158. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6159. /* switch over to host mailbox */
  6160. psli->sli_flag |= LPFC_SLI_ACTIVE;
  6161. }
  6162. }
  6163. wmb();
  6164. switch (flag) {
  6165. case MBX_NOWAIT:
  6166. /* Set up reference to mailbox command */
  6167. psli->mbox_active = pmbox;
  6168. /* Interrupt board to do it */
  6169. writel(CA_MBATT, phba->CAregaddr);
  6170. readl(phba->CAregaddr); /* flush */
  6171. /* Don't wait for it to finish, just return */
  6172. break;
  6173. case MBX_POLL:
  6174. /* Set up null reference to mailbox command */
  6175. psli->mbox_active = NULL;
  6176. /* Interrupt board to do it */
  6177. writel(CA_MBATT, phba->CAregaddr);
  6178. readl(phba->CAregaddr); /* flush */
  6179. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6180. /* First read mbox status word */
  6181. word0 = *((uint32_t *)phba->mbox);
  6182. word0 = le32_to_cpu(word0);
  6183. } else {
  6184. /* First read mbox status word */
  6185. if (lpfc_readl(phba->MBslimaddr, &word0)) {
  6186. spin_unlock_irqrestore(&phba->hbalock,
  6187. drvr_flag);
  6188. goto out_not_finished;
  6189. }
  6190. }
  6191. /* Read the HBA Host Attention Register */
  6192. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6193. spin_unlock_irqrestore(&phba->hbalock,
  6194. drvr_flag);
  6195. goto out_not_finished;
  6196. }
  6197. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
  6198. 1000) + jiffies;
  6199. i = 0;
  6200. /* Wait for command to complete */
  6201. while (((word0 & OWN_CHIP) == OWN_CHIP) ||
  6202. (!(ha_copy & HA_MBATT) &&
  6203. (phba->link_state > LPFC_WARM_START))) {
  6204. if (time_after(jiffies, timeout)) {
  6205. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6206. spin_unlock_irqrestore(&phba->hbalock,
  6207. drvr_flag);
  6208. goto out_not_finished;
  6209. }
  6210. /* Check if we took a mbox interrupt while we were
  6211. polling */
  6212. if (((word0 & OWN_CHIP) != OWN_CHIP)
  6213. && (evtctr != psli->slistat.mbox_event))
  6214. break;
  6215. if (i++ > 10) {
  6216. spin_unlock_irqrestore(&phba->hbalock,
  6217. drvr_flag);
  6218. msleep(1);
  6219. spin_lock_irqsave(&phba->hbalock, drvr_flag);
  6220. }
  6221. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6222. /* First copy command data */
  6223. word0 = *((uint32_t *)phba->mbox);
  6224. word0 = le32_to_cpu(word0);
  6225. if (mb->mbxCommand == MBX_CONFIG_PORT) {
  6226. MAILBOX_t *slimmb;
  6227. uint32_t slimword0;
  6228. /* Check real SLIM for any errors */
  6229. slimword0 = readl(phba->MBslimaddr);
  6230. slimmb = (MAILBOX_t *) & slimword0;
  6231. if (((slimword0 & OWN_CHIP) != OWN_CHIP)
  6232. && slimmb->mbxStatus) {
  6233. psli->sli_flag &=
  6234. ~LPFC_SLI_ACTIVE;
  6235. word0 = slimword0;
  6236. }
  6237. }
  6238. } else {
  6239. /* First copy command data */
  6240. word0 = readl(phba->MBslimaddr);
  6241. }
  6242. /* Read the HBA Host Attention Register */
  6243. if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
  6244. spin_unlock_irqrestore(&phba->hbalock,
  6245. drvr_flag);
  6246. goto out_not_finished;
  6247. }
  6248. }
  6249. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  6250. /* copy results back to user */
  6251. lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
  6252. /* Copy the mailbox extension data */
  6253. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6254. lpfc_sli_pcimem_bcopy(phba->mbox_ext,
  6255. pmbox->context2,
  6256. pmbox->out_ext_byte_len);
  6257. }
  6258. } else {
  6259. /* First copy command data */
  6260. lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
  6261. MAILBOX_CMD_SIZE);
  6262. /* Copy the mailbox extension data */
  6263. if (pmbox->out_ext_byte_len && pmbox->context2) {
  6264. lpfc_memcpy_from_slim(pmbox->context2,
  6265. phba->MBslimaddr +
  6266. MAILBOX_HBA_EXT_OFFSET,
  6267. pmbox->out_ext_byte_len);
  6268. }
  6269. }
  6270. writel(HA_MBATT, phba->HAregaddr);
  6271. readl(phba->HAregaddr); /* flush */
  6272. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6273. status = mb->mbxStatus;
  6274. }
  6275. spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
  6276. return status;
  6277. out_not_finished:
  6278. if (processing_queue) {
  6279. pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6280. lpfc_mbox_cmpl_put(phba, pmbox);
  6281. }
  6282. return MBX_NOT_FINISHED;
  6283. }
  6284. /**
  6285. * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
  6286. * @phba: Pointer to HBA context object.
  6287. *
  6288. * The function blocks the posting of SLI4 asynchronous mailbox commands from
  6289. * the driver internal pending mailbox queue. It will then try to wait out the
  6290. * possible outstanding mailbox command before return.
  6291. *
  6292. * Returns:
  6293. * 0 - the outstanding mailbox command completed; otherwise, the wait for
  6294. * the outstanding mailbox command timed out.
  6295. **/
  6296. static int
  6297. lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
  6298. {
  6299. struct lpfc_sli *psli = &phba->sli;
  6300. int rc = 0;
  6301. unsigned long timeout = 0;
  6302. /* Mark the asynchronous mailbox command posting as blocked */
  6303. spin_lock_irq(&phba->hbalock);
  6304. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  6305. /* Determine how long we might wait for the active mailbox
  6306. * command to be gracefully completed by firmware.
  6307. */
  6308. if (phba->sli.mbox_active)
  6309. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  6310. phba->sli.mbox_active) *
  6311. 1000) + jiffies;
  6312. spin_unlock_irq(&phba->hbalock);
  6313. /* Wait for the outstnading mailbox command to complete */
  6314. while (phba->sli.mbox_active) {
  6315. /* Check active mailbox complete status every 2ms */
  6316. msleep(2);
  6317. if (time_after(jiffies, timeout)) {
  6318. /* Timeout, marked the outstanding cmd not complete */
  6319. rc = 1;
  6320. break;
  6321. }
  6322. }
  6323. /* Can not cleanly block async mailbox command, fails it */
  6324. if (rc) {
  6325. spin_lock_irq(&phba->hbalock);
  6326. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6327. spin_unlock_irq(&phba->hbalock);
  6328. }
  6329. return rc;
  6330. }
  6331. /**
  6332. * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
  6333. * @phba: Pointer to HBA context object.
  6334. *
  6335. * The function unblocks and resume posting of SLI4 asynchronous mailbox
  6336. * commands from the driver internal pending mailbox queue. It makes sure
  6337. * that there is no outstanding mailbox command before resuming posting
  6338. * asynchronous mailbox commands. If, for any reason, there is outstanding
  6339. * mailbox command, it will try to wait it out before resuming asynchronous
  6340. * mailbox command posting.
  6341. **/
  6342. static void
  6343. lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
  6344. {
  6345. struct lpfc_sli *psli = &phba->sli;
  6346. spin_lock_irq(&phba->hbalock);
  6347. if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6348. /* Asynchronous mailbox posting is not blocked, do nothing */
  6349. spin_unlock_irq(&phba->hbalock);
  6350. return;
  6351. }
  6352. /* Outstanding synchronous mailbox command is guaranteed to be done,
  6353. * successful or timeout, after timing-out the outstanding mailbox
  6354. * command shall always be removed, so just unblock posting async
  6355. * mailbox command and resume
  6356. */
  6357. psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
  6358. spin_unlock_irq(&phba->hbalock);
  6359. /* wake up worker thread to post asynchronlous mailbox command */
  6360. lpfc_worker_wake_up(phba);
  6361. }
  6362. /**
  6363. * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
  6364. * @phba: Pointer to HBA context object.
  6365. * @mboxq: Pointer to mailbox object.
  6366. *
  6367. * The function posts a mailbox to the port. The mailbox is expected
  6368. * to be comletely filled in and ready for the port to operate on it.
  6369. * This routine executes a synchronous completion operation on the
  6370. * mailbox by polling for its completion.
  6371. *
  6372. * The caller must not be holding any locks when calling this routine.
  6373. *
  6374. * Returns:
  6375. * MBX_SUCCESS - mailbox posted successfully
  6376. * Any of the MBX error values.
  6377. **/
  6378. static int
  6379. lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  6380. {
  6381. int rc = MBX_SUCCESS;
  6382. unsigned long iflag;
  6383. uint32_t db_ready;
  6384. uint32_t mcqe_status;
  6385. uint32_t mbx_cmnd;
  6386. unsigned long timeout;
  6387. struct lpfc_sli *psli = &phba->sli;
  6388. struct lpfc_mqe *mb = &mboxq->u.mqe;
  6389. struct lpfc_bmbx_create *mbox_rgn;
  6390. struct dma_address *dma_address;
  6391. struct lpfc_register bmbx_reg;
  6392. /*
  6393. * Only one mailbox can be active to the bootstrap mailbox region
  6394. * at a time and there is no queueing provided.
  6395. */
  6396. spin_lock_irqsave(&phba->hbalock, iflag);
  6397. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6398. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6399. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6400. "(%d):2532 Mailbox command x%x (x%x/x%x) "
  6401. "cannot issue Data: x%x x%x\n",
  6402. mboxq->vport ? mboxq->vport->vpi : 0,
  6403. mboxq->u.mb.mbxCommand,
  6404. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6405. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6406. psli->sli_flag, MBX_POLL);
  6407. return MBXERR_ERROR;
  6408. }
  6409. /* The server grabs the token and owns it until release */
  6410. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6411. phba->sli.mbox_active = mboxq;
  6412. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6413. /*
  6414. * Initialize the bootstrap memory region to avoid stale data areas
  6415. * in the mailbox post. Then copy the caller's mailbox contents to
  6416. * the bmbx mailbox region.
  6417. */
  6418. mbx_cmnd = bf_get(lpfc_mqe_command, mb);
  6419. memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
  6420. lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
  6421. sizeof(struct lpfc_mqe));
  6422. /* Post the high mailbox dma address to the port and wait for ready. */
  6423. dma_address = &phba->sli4_hba.bmbx.dma_address;
  6424. writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
  6425. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6426. * 1000) + jiffies;
  6427. do {
  6428. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6429. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6430. if (!db_ready)
  6431. msleep(2);
  6432. if (time_after(jiffies, timeout)) {
  6433. rc = MBXERR_ERROR;
  6434. goto exit;
  6435. }
  6436. } while (!db_ready);
  6437. /* Post the low mailbox dma address to the port. */
  6438. writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
  6439. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
  6440. * 1000) + jiffies;
  6441. do {
  6442. bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
  6443. db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
  6444. if (!db_ready)
  6445. msleep(2);
  6446. if (time_after(jiffies, timeout)) {
  6447. rc = MBXERR_ERROR;
  6448. goto exit;
  6449. }
  6450. } while (!db_ready);
  6451. /*
  6452. * Read the CQ to ensure the mailbox has completed.
  6453. * If so, update the mailbox status so that the upper layers
  6454. * can complete the request normally.
  6455. */
  6456. lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
  6457. sizeof(struct lpfc_mqe));
  6458. mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
  6459. lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
  6460. sizeof(struct lpfc_mcqe));
  6461. mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
  6462. /*
  6463. * When the CQE status indicates a failure and the mailbox status
  6464. * indicates success then copy the CQE status into the mailbox status
  6465. * (and prefix it with x4000).
  6466. */
  6467. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  6468. if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
  6469. bf_set(lpfc_mqe_status, mb,
  6470. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  6471. rc = MBXERR_ERROR;
  6472. } else
  6473. lpfc_sli4_swap_str(phba, mboxq);
  6474. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6475. "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
  6476. "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
  6477. " x%x x%x CQ: x%x x%x x%x x%x\n",
  6478. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6479. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6480. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6481. bf_get(lpfc_mqe_status, mb),
  6482. mb->un.mb_words[0], mb->un.mb_words[1],
  6483. mb->un.mb_words[2], mb->un.mb_words[3],
  6484. mb->un.mb_words[4], mb->un.mb_words[5],
  6485. mb->un.mb_words[6], mb->un.mb_words[7],
  6486. mb->un.mb_words[8], mb->un.mb_words[9],
  6487. mb->un.mb_words[10], mb->un.mb_words[11],
  6488. mb->un.mb_words[12], mboxq->mcqe.word0,
  6489. mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
  6490. mboxq->mcqe.trailer);
  6491. exit:
  6492. /* We are holding the token, no needed for lock when release */
  6493. spin_lock_irqsave(&phba->hbalock, iflag);
  6494. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6495. phba->sli.mbox_active = NULL;
  6496. spin_unlock_irqrestore(&phba->hbalock, iflag);
  6497. return rc;
  6498. }
  6499. /**
  6500. * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
  6501. * @phba: Pointer to HBA context object.
  6502. * @pmbox: Pointer to mailbox object.
  6503. * @flag: Flag indicating how the mailbox need to be processed.
  6504. *
  6505. * This function is called by discovery code and HBA management code to submit
  6506. * a mailbox command to firmware with SLI-4 interface spec.
  6507. *
  6508. * Return codes the caller owns the mailbox command after the return of the
  6509. * function.
  6510. **/
  6511. static int
  6512. lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
  6513. uint32_t flag)
  6514. {
  6515. struct lpfc_sli *psli = &phba->sli;
  6516. unsigned long iflags;
  6517. int rc;
  6518. /* dump from issue mailbox command if setup */
  6519. lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
  6520. rc = lpfc_mbox_dev_check(phba);
  6521. if (unlikely(rc)) {
  6522. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6523. "(%d):2544 Mailbox command x%x (x%x/x%x) "
  6524. "cannot issue Data: x%x x%x\n",
  6525. mboxq->vport ? mboxq->vport->vpi : 0,
  6526. mboxq->u.mb.mbxCommand,
  6527. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6528. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6529. psli->sli_flag, flag);
  6530. goto out_not_finished;
  6531. }
  6532. /* Detect polling mode and jump to a handler */
  6533. if (!phba->sli4_hba.intr_enable) {
  6534. if (flag == MBX_POLL)
  6535. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6536. else
  6537. rc = -EIO;
  6538. if (rc != MBX_SUCCESS)
  6539. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6540. "(%d):2541 Mailbox command x%x "
  6541. "(x%x/x%x) failure: "
  6542. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6543. "Data: x%x x%x\n,",
  6544. mboxq->vport ? mboxq->vport->vpi : 0,
  6545. mboxq->u.mb.mbxCommand,
  6546. lpfc_sli_config_mbox_subsys_get(phba,
  6547. mboxq),
  6548. lpfc_sli_config_mbox_opcode_get(phba,
  6549. mboxq),
  6550. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6551. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6552. bf_get(lpfc_mcqe_ext_status,
  6553. &mboxq->mcqe),
  6554. psli->sli_flag, flag);
  6555. return rc;
  6556. } else if (flag == MBX_POLL) {
  6557. lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
  6558. "(%d):2542 Try to issue mailbox command "
  6559. "x%x (x%x/x%x) synchronously ahead of async"
  6560. "mailbox command queue: x%x x%x\n",
  6561. mboxq->vport ? mboxq->vport->vpi : 0,
  6562. mboxq->u.mb.mbxCommand,
  6563. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6564. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6565. psli->sli_flag, flag);
  6566. /* Try to block the asynchronous mailbox posting */
  6567. rc = lpfc_sli4_async_mbox_block(phba);
  6568. if (!rc) {
  6569. /* Successfully blocked, now issue sync mbox cmd */
  6570. rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
  6571. if (rc != MBX_SUCCESS)
  6572. lpfc_printf_log(phba, KERN_WARNING,
  6573. LOG_MBOX | LOG_SLI,
  6574. "(%d):2597 Sync Mailbox command "
  6575. "x%x (x%x/x%x) failure: "
  6576. "mqe_sta: x%x mcqe_sta: x%x/x%x "
  6577. "Data: x%x x%x\n,",
  6578. mboxq->vport ? mboxq->vport->vpi : 0,
  6579. mboxq->u.mb.mbxCommand,
  6580. lpfc_sli_config_mbox_subsys_get(phba,
  6581. mboxq),
  6582. lpfc_sli_config_mbox_opcode_get(phba,
  6583. mboxq),
  6584. bf_get(lpfc_mqe_status, &mboxq->u.mqe),
  6585. bf_get(lpfc_mcqe_status, &mboxq->mcqe),
  6586. bf_get(lpfc_mcqe_ext_status,
  6587. &mboxq->mcqe),
  6588. psli->sli_flag, flag);
  6589. /* Unblock the async mailbox posting afterward */
  6590. lpfc_sli4_async_mbox_unblock(phba);
  6591. }
  6592. return rc;
  6593. }
  6594. /* Now, interrupt mode asynchrous mailbox command */
  6595. rc = lpfc_mbox_cmd_check(phba, mboxq);
  6596. if (rc) {
  6597. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6598. "(%d):2543 Mailbox command x%x (x%x/x%x) "
  6599. "cannot issue Data: x%x x%x\n",
  6600. mboxq->vport ? mboxq->vport->vpi : 0,
  6601. mboxq->u.mb.mbxCommand,
  6602. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6603. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6604. psli->sli_flag, flag);
  6605. goto out_not_finished;
  6606. }
  6607. /* Put the mailbox command to the driver internal FIFO */
  6608. psli->slistat.mbox_busy++;
  6609. spin_lock_irqsave(&phba->hbalock, iflags);
  6610. lpfc_mbox_put(phba, mboxq);
  6611. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6612. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6613. "(%d):0354 Mbox cmd issue - Enqueue Data: "
  6614. "x%x (x%x/x%x) x%x x%x x%x\n",
  6615. mboxq->vport ? mboxq->vport->vpi : 0xffffff,
  6616. bf_get(lpfc_mqe_command, &mboxq->u.mqe),
  6617. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6618. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6619. phba->pport->port_state,
  6620. psli->sli_flag, MBX_NOWAIT);
  6621. /* Wake up worker thread to transport mailbox command from head */
  6622. lpfc_worker_wake_up(phba);
  6623. return MBX_BUSY;
  6624. out_not_finished:
  6625. return MBX_NOT_FINISHED;
  6626. }
  6627. /**
  6628. * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
  6629. * @phba: Pointer to HBA context object.
  6630. *
  6631. * This function is called by worker thread to send a mailbox command to
  6632. * SLI4 HBA firmware.
  6633. *
  6634. **/
  6635. int
  6636. lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
  6637. {
  6638. struct lpfc_sli *psli = &phba->sli;
  6639. LPFC_MBOXQ_t *mboxq;
  6640. int rc = MBX_SUCCESS;
  6641. unsigned long iflags;
  6642. struct lpfc_mqe *mqe;
  6643. uint32_t mbx_cmnd;
  6644. /* Check interrupt mode before post async mailbox command */
  6645. if (unlikely(!phba->sli4_hba.intr_enable))
  6646. return MBX_NOT_FINISHED;
  6647. /* Check for mailbox command service token */
  6648. spin_lock_irqsave(&phba->hbalock, iflags);
  6649. if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
  6650. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6651. return MBX_NOT_FINISHED;
  6652. }
  6653. if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
  6654. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6655. return MBX_NOT_FINISHED;
  6656. }
  6657. if (unlikely(phba->sli.mbox_active)) {
  6658. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6659. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6660. "0384 There is pending active mailbox cmd\n");
  6661. return MBX_NOT_FINISHED;
  6662. }
  6663. /* Take the mailbox command service token */
  6664. psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
  6665. /* Get the next mailbox command from head of queue */
  6666. mboxq = lpfc_mbox_get(phba);
  6667. /* If no more mailbox command waiting for post, we're done */
  6668. if (!mboxq) {
  6669. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6670. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6671. return MBX_SUCCESS;
  6672. }
  6673. phba->sli.mbox_active = mboxq;
  6674. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6675. /* Check device readiness for posting mailbox command */
  6676. rc = lpfc_mbox_dev_check(phba);
  6677. if (unlikely(rc))
  6678. /* Driver clean routine will clean up pending mailbox */
  6679. goto out_not_finished;
  6680. /* Prepare the mbox command to be posted */
  6681. mqe = &mboxq->u.mqe;
  6682. mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
  6683. /* Start timer for the mbox_tmo and log some mailbox post messages */
  6684. mod_timer(&psli->mbox_tmo, (jiffies +
  6685. (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
  6686. lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
  6687. "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
  6688. "x%x x%x\n",
  6689. mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
  6690. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6691. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6692. phba->pport->port_state, psli->sli_flag);
  6693. if (mbx_cmnd != MBX_HEARTBEAT) {
  6694. if (mboxq->vport) {
  6695. lpfc_debugfs_disc_trc(mboxq->vport,
  6696. LPFC_DISC_TRC_MBOX_VPORT,
  6697. "MBOX Send vport: cmd:x%x mb:x%x x%x",
  6698. mbx_cmnd, mqe->un.mb_words[0],
  6699. mqe->un.mb_words[1]);
  6700. } else {
  6701. lpfc_debugfs_disc_trc(phba->pport,
  6702. LPFC_DISC_TRC_MBOX,
  6703. "MBOX Send: cmd:x%x mb:x%x x%x",
  6704. mbx_cmnd, mqe->un.mb_words[0],
  6705. mqe->un.mb_words[1]);
  6706. }
  6707. }
  6708. psli->slistat.mbox_cmd++;
  6709. /* Post the mailbox command to the port */
  6710. rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
  6711. if (rc != MBX_SUCCESS) {
  6712. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  6713. "(%d):2533 Mailbox command x%x (x%x/x%x) "
  6714. "cannot issue Data: x%x x%x\n",
  6715. mboxq->vport ? mboxq->vport->vpi : 0,
  6716. mboxq->u.mb.mbxCommand,
  6717. lpfc_sli_config_mbox_subsys_get(phba, mboxq),
  6718. lpfc_sli_config_mbox_opcode_get(phba, mboxq),
  6719. psli->sli_flag, MBX_NOWAIT);
  6720. goto out_not_finished;
  6721. }
  6722. return rc;
  6723. out_not_finished:
  6724. spin_lock_irqsave(&phba->hbalock, iflags);
  6725. if (phba->sli.mbox_active) {
  6726. mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
  6727. __lpfc_mbox_cmpl_put(phba, mboxq);
  6728. /* Release the token */
  6729. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  6730. phba->sli.mbox_active = NULL;
  6731. }
  6732. spin_unlock_irqrestore(&phba->hbalock, iflags);
  6733. return MBX_NOT_FINISHED;
  6734. }
  6735. /**
  6736. * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
  6737. * @phba: Pointer to HBA context object.
  6738. * @pmbox: Pointer to mailbox object.
  6739. * @flag: Flag indicating how the mailbox need to be processed.
  6740. *
  6741. * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
  6742. * the API jump table function pointer from the lpfc_hba struct.
  6743. *
  6744. * Return codes the caller owns the mailbox command after the return of the
  6745. * function.
  6746. **/
  6747. int
  6748. lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
  6749. {
  6750. return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
  6751. }
  6752. /**
  6753. * lpfc_mbox_api_table_setup - Set up mbox api function jump table
  6754. * @phba: The hba struct for which this call is being executed.
  6755. * @dev_grp: The HBA PCI-Device group number.
  6756. *
  6757. * This routine sets up the mbox interface API function jump table in @phba
  6758. * struct.
  6759. * Returns: 0 - success, -ENODEV - failure.
  6760. **/
  6761. int
  6762. lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  6763. {
  6764. switch (dev_grp) {
  6765. case LPFC_PCI_DEV_LP:
  6766. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
  6767. phba->lpfc_sli_handle_slow_ring_event =
  6768. lpfc_sli_handle_slow_ring_event_s3;
  6769. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
  6770. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
  6771. phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
  6772. break;
  6773. case LPFC_PCI_DEV_OC:
  6774. phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
  6775. phba->lpfc_sli_handle_slow_ring_event =
  6776. lpfc_sli_handle_slow_ring_event_s4;
  6777. phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
  6778. phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
  6779. phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
  6780. break;
  6781. default:
  6782. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  6783. "1420 Invalid HBA PCI-device group: 0x%x\n",
  6784. dev_grp);
  6785. return -ENODEV;
  6786. break;
  6787. }
  6788. return 0;
  6789. }
  6790. /**
  6791. * __lpfc_sli_ringtx_put - Add an iocb to the txq
  6792. * @phba: Pointer to HBA context object.
  6793. * @pring: Pointer to driver SLI ring object.
  6794. * @piocb: Pointer to address of newly added command iocb.
  6795. *
  6796. * This function is called with hbalock held to add a command
  6797. * iocb to the txq when SLI layer cannot submit the command iocb
  6798. * to the ring.
  6799. **/
  6800. void
  6801. __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6802. struct lpfc_iocbq *piocb)
  6803. {
  6804. /* Insert the caller's iocb in the txq tail for later processing. */
  6805. list_add_tail(&piocb->list, &pring->txq);
  6806. pring->txq_cnt++;
  6807. }
  6808. /**
  6809. * lpfc_sli_next_iocb - Get the next iocb in the txq
  6810. * @phba: Pointer to HBA context object.
  6811. * @pring: Pointer to driver SLI ring object.
  6812. * @piocb: Pointer to address of newly added command iocb.
  6813. *
  6814. * This function is called with hbalock held before a new
  6815. * iocb is submitted to the firmware. This function checks
  6816. * txq to flush the iocbs in txq to Firmware before
  6817. * submitting new iocbs to the Firmware.
  6818. * If there are iocbs in the txq which need to be submitted
  6819. * to firmware, lpfc_sli_next_iocb returns the first element
  6820. * of the txq after dequeuing it from txq.
  6821. * If there is no iocb in the txq then the function will return
  6822. * *piocb and *piocb is set to NULL. Caller needs to check
  6823. * *piocb to find if there are more commands in the txq.
  6824. **/
  6825. static struct lpfc_iocbq *
  6826. lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  6827. struct lpfc_iocbq **piocb)
  6828. {
  6829. struct lpfc_iocbq * nextiocb;
  6830. nextiocb = lpfc_sli_ringtx_get(phba, pring);
  6831. if (!nextiocb) {
  6832. nextiocb = *piocb;
  6833. *piocb = NULL;
  6834. }
  6835. return nextiocb;
  6836. }
  6837. /**
  6838. * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
  6839. * @phba: Pointer to HBA context object.
  6840. * @ring_number: SLI ring number to issue iocb on.
  6841. * @piocb: Pointer to command iocb.
  6842. * @flag: Flag indicating if this command can be put into txq.
  6843. *
  6844. * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
  6845. * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
  6846. * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
  6847. * flag is turned on, the function returns IOCB_ERROR. When the link is down,
  6848. * this function allows only iocbs for posting buffers. This function finds
  6849. * next available slot in the command ring and posts the command to the
  6850. * available slot and writes the port attention register to request HBA start
  6851. * processing new iocb. If there is no slot available in the ring and
  6852. * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
  6853. * the function returns IOCB_BUSY.
  6854. *
  6855. * This function is called with hbalock held. The function will return success
  6856. * after it successfully submit the iocb to firmware or after adding to the
  6857. * txq.
  6858. **/
  6859. static int
  6860. __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
  6861. struct lpfc_iocbq *piocb, uint32_t flag)
  6862. {
  6863. struct lpfc_iocbq *nextiocb;
  6864. IOCB_t *iocb;
  6865. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  6866. if (piocb->iocb_cmpl && (!piocb->vport) &&
  6867. (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
  6868. (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
  6869. lpfc_printf_log(phba, KERN_ERR,
  6870. LOG_SLI | LOG_VPORT,
  6871. "1807 IOCB x%x failed. No vport\n",
  6872. piocb->iocb.ulpCommand);
  6873. dump_stack();
  6874. return IOCB_ERROR;
  6875. }
  6876. /* If the PCI channel is in offline state, do not post iocbs. */
  6877. if (unlikely(pci_channel_offline(phba->pcidev)))
  6878. return IOCB_ERROR;
  6879. /* If HBA has a deferred error attention, fail the iocb. */
  6880. if (unlikely(phba->hba_flag & DEFER_ERATT))
  6881. return IOCB_ERROR;
  6882. /*
  6883. * We should never get an IOCB if we are in a < LINK_DOWN state
  6884. */
  6885. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  6886. return IOCB_ERROR;
  6887. /*
  6888. * Check to see if we are blocking IOCB processing because of a
  6889. * outstanding event.
  6890. */
  6891. if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
  6892. goto iocb_busy;
  6893. if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
  6894. /*
  6895. * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
  6896. * can be issued if the link is not up.
  6897. */
  6898. switch (piocb->iocb.ulpCommand) {
  6899. case CMD_GEN_REQUEST64_CR:
  6900. case CMD_GEN_REQUEST64_CX:
  6901. if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
  6902. (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
  6903. FC_RCTL_DD_UNSOL_CMD) ||
  6904. (piocb->iocb.un.genreq64.w5.hcsw.Type !=
  6905. MENLO_TRANSPORT_TYPE))
  6906. goto iocb_busy;
  6907. break;
  6908. case CMD_QUE_RING_BUF_CN:
  6909. case CMD_QUE_RING_BUF64_CN:
  6910. /*
  6911. * For IOCBs, like QUE_RING_BUF, that have no rsp ring
  6912. * completion, iocb_cmpl MUST be 0.
  6913. */
  6914. if (piocb->iocb_cmpl)
  6915. piocb->iocb_cmpl = NULL;
  6916. /*FALLTHROUGH*/
  6917. case CMD_CREATE_XRI_CR:
  6918. case CMD_CLOSE_XRI_CN:
  6919. case CMD_CLOSE_XRI_CX:
  6920. break;
  6921. default:
  6922. goto iocb_busy;
  6923. }
  6924. /*
  6925. * For FCP commands, we must be in a state where we can process link
  6926. * attention events.
  6927. */
  6928. } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
  6929. !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
  6930. goto iocb_busy;
  6931. }
  6932. while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
  6933. (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
  6934. lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
  6935. if (iocb)
  6936. lpfc_sli_update_ring(phba, pring);
  6937. else
  6938. lpfc_sli_update_full_ring(phba, pring);
  6939. if (!piocb)
  6940. return IOCB_SUCCESS;
  6941. goto out_busy;
  6942. iocb_busy:
  6943. pring->stats.iocb_cmd_delay++;
  6944. out_busy:
  6945. if (!(flag & SLI_IOCB_RET_IOCB)) {
  6946. __lpfc_sli_ringtx_put(phba, pring, piocb);
  6947. return IOCB_SUCCESS;
  6948. }
  6949. return IOCB_BUSY;
  6950. }
  6951. /**
  6952. * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
  6953. * @phba: Pointer to HBA context object.
  6954. * @piocb: Pointer to command iocb.
  6955. * @sglq: Pointer to the scatter gather queue object.
  6956. *
  6957. * This routine converts the bpl or bde that is in the IOCB
  6958. * to a sgl list for the sli4 hardware. The physical address
  6959. * of the bpl/bde is converted back to a virtual address.
  6960. * If the IOCB contains a BPL then the list of BDE's is
  6961. * converted to sli4_sge's. If the IOCB contains a single
  6962. * BDE then it is converted to a single sli_sge.
  6963. * The IOCB is still in cpu endianess so the contents of
  6964. * the bpl can be used without byte swapping.
  6965. *
  6966. * Returns valid XRI = Success, NO_XRI = Failure.
  6967. **/
  6968. static uint16_t
  6969. lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
  6970. struct lpfc_sglq *sglq)
  6971. {
  6972. uint16_t xritag = NO_XRI;
  6973. struct ulp_bde64 *bpl = NULL;
  6974. struct ulp_bde64 bde;
  6975. struct sli4_sge *sgl = NULL;
  6976. struct lpfc_dmabuf *dmabuf;
  6977. IOCB_t *icmd;
  6978. int numBdes = 0;
  6979. int i = 0;
  6980. uint32_t offset = 0; /* accumulated offset in the sg request list */
  6981. int inbound = 0; /* number of sg reply entries inbound from firmware */
  6982. if (!piocbq || !sglq)
  6983. return xritag;
  6984. sgl = (struct sli4_sge *)sglq->sgl;
  6985. icmd = &piocbq->iocb;
  6986. if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
  6987. return sglq->sli4_xritag;
  6988. if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  6989. numBdes = icmd->un.genreq64.bdl.bdeSize /
  6990. sizeof(struct ulp_bde64);
  6991. /* The addrHigh and addrLow fields within the IOCB
  6992. * have not been byteswapped yet so there is no
  6993. * need to swap them back.
  6994. */
  6995. if (piocbq->context3)
  6996. dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
  6997. else
  6998. return xritag;
  6999. bpl = (struct ulp_bde64 *)dmabuf->virt;
  7000. if (!bpl)
  7001. return xritag;
  7002. for (i = 0; i < numBdes; i++) {
  7003. /* Should already be byte swapped. */
  7004. sgl->addr_hi = bpl->addrHigh;
  7005. sgl->addr_lo = bpl->addrLow;
  7006. sgl->word2 = le32_to_cpu(sgl->word2);
  7007. if ((i+1) == numBdes)
  7008. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7009. else
  7010. bf_set(lpfc_sli4_sge_last, sgl, 0);
  7011. /* swap the size field back to the cpu so we
  7012. * can assign it to the sgl.
  7013. */
  7014. bde.tus.w = le32_to_cpu(bpl->tus.w);
  7015. sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
  7016. /* The offsets in the sgl need to be accumulated
  7017. * separately for the request and reply lists.
  7018. * The request is always first, the reply follows.
  7019. */
  7020. if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
  7021. /* add up the reply sg entries */
  7022. if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
  7023. inbound++;
  7024. /* first inbound? reset the offset */
  7025. if (inbound == 1)
  7026. offset = 0;
  7027. bf_set(lpfc_sli4_sge_offset, sgl, offset);
  7028. bf_set(lpfc_sli4_sge_type, sgl,
  7029. LPFC_SGE_TYPE_DATA);
  7030. offset += bde.tus.f.bdeSize;
  7031. }
  7032. sgl->word2 = cpu_to_le32(sgl->word2);
  7033. bpl++;
  7034. sgl++;
  7035. }
  7036. } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
  7037. /* The addrHigh and addrLow fields of the BDE have not
  7038. * been byteswapped yet so they need to be swapped
  7039. * before putting them in the sgl.
  7040. */
  7041. sgl->addr_hi =
  7042. cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
  7043. sgl->addr_lo =
  7044. cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
  7045. sgl->word2 = le32_to_cpu(sgl->word2);
  7046. bf_set(lpfc_sli4_sge_last, sgl, 1);
  7047. sgl->word2 = cpu_to_le32(sgl->word2);
  7048. sgl->sge_len =
  7049. cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
  7050. }
  7051. return sglq->sli4_xritag;
  7052. }
  7053. /**
  7054. * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
  7055. * @phba: Pointer to HBA context object.
  7056. *
  7057. * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
  7058. * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
  7059. * held.
  7060. *
  7061. * Return: index into SLI4 fast-path FCP queue index.
  7062. **/
  7063. static uint32_t
  7064. lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
  7065. {
  7066. ++phba->fcp_qidx;
  7067. if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
  7068. phba->fcp_qidx = 0;
  7069. return phba->fcp_qidx;
  7070. }
  7071. /**
  7072. * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
  7073. * @phba: Pointer to HBA context object.
  7074. * @piocb: Pointer to command iocb.
  7075. * @wqe: Pointer to the work queue entry.
  7076. *
  7077. * This routine converts the iocb command to its Work Queue Entry
  7078. * equivalent. The wqe pointer should not have any fields set when
  7079. * this routine is called because it will memcpy over them.
  7080. * This routine does not set the CQ_ID or the WQEC bits in the
  7081. * wqe.
  7082. *
  7083. * Returns: 0 = Success, IOCB_ERROR = Failure.
  7084. **/
  7085. static int
  7086. lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
  7087. union lpfc_wqe *wqe)
  7088. {
  7089. uint32_t xmit_len = 0, total_len = 0;
  7090. uint8_t ct = 0;
  7091. uint32_t fip;
  7092. uint32_t abort_tag;
  7093. uint8_t command_type = ELS_COMMAND_NON_FIP;
  7094. uint8_t cmnd;
  7095. uint16_t xritag;
  7096. uint16_t abrt_iotag;
  7097. struct lpfc_iocbq *abrtiocbq;
  7098. struct ulp_bde64 *bpl = NULL;
  7099. uint32_t els_id = LPFC_ELS_ID_DEFAULT;
  7100. int numBdes, i;
  7101. struct ulp_bde64 bde;
  7102. struct lpfc_nodelist *ndlp;
  7103. uint32_t *pcmd;
  7104. uint32_t if_type;
  7105. fip = phba->hba_flag & HBA_FIP_SUPPORT;
  7106. /* The fcp commands will set command type */
  7107. if (iocbq->iocb_flag & LPFC_IO_FCP)
  7108. command_type = FCP_COMMAND;
  7109. else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
  7110. command_type = ELS_COMMAND_FIP;
  7111. else
  7112. command_type = ELS_COMMAND_NON_FIP;
  7113. /* Some of the fields are in the right position already */
  7114. memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
  7115. abort_tag = (uint32_t) iocbq->iotag;
  7116. xritag = iocbq->sli4_xritag;
  7117. wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
  7118. /* words0-2 bpl convert bde */
  7119. if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
  7120. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7121. sizeof(struct ulp_bde64);
  7122. bpl = (struct ulp_bde64 *)
  7123. ((struct lpfc_dmabuf *)iocbq->context3)->virt;
  7124. if (!bpl)
  7125. return IOCB_ERROR;
  7126. /* Should already be byte swapped. */
  7127. wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
  7128. wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
  7129. /* swap the size field back to the cpu so we
  7130. * can assign it to the sgl.
  7131. */
  7132. wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
  7133. xmit_len = wqe->generic.bde.tus.f.bdeSize;
  7134. total_len = 0;
  7135. for (i = 0; i < numBdes; i++) {
  7136. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7137. total_len += bde.tus.f.bdeSize;
  7138. }
  7139. } else
  7140. xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
  7141. iocbq->iocb.ulpIoTag = iocbq->iotag;
  7142. cmnd = iocbq->iocb.ulpCommand;
  7143. switch (iocbq->iocb.ulpCommand) {
  7144. case CMD_ELS_REQUEST64_CR:
  7145. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7146. if (!iocbq->iocb.ulpLe) {
  7147. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7148. "2007 Only Limited Edition cmd Format"
  7149. " supported 0x%x\n",
  7150. iocbq->iocb.ulpCommand);
  7151. return IOCB_ERROR;
  7152. }
  7153. wqe->els_req.payload_len = xmit_len;
  7154. /* Els_reguest64 has a TMO */
  7155. bf_set(wqe_tmo, &wqe->els_req.wqe_com,
  7156. iocbq->iocb.ulpTimeout);
  7157. /* Need a VF for word 4 set the vf bit*/
  7158. bf_set(els_req64_vf, &wqe->els_req, 0);
  7159. /* And a VFID for word 12 */
  7160. bf_set(els_req64_vfid, &wqe->els_req, 0);
  7161. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7162. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7163. iocbq->iocb.ulpContext);
  7164. bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
  7165. bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
  7166. /* CCP CCPE PV PRI in word10 were set in the memcpy */
  7167. if (command_type == ELS_COMMAND_FIP)
  7168. els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
  7169. >> LPFC_FIP_ELS_ID_SHIFT);
  7170. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7171. iocbq->context2)->virt);
  7172. if_type = bf_get(lpfc_sli_intf_if_type,
  7173. &phba->sli4_hba.sli_intf);
  7174. if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
  7175. if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
  7176. *pcmd == ELS_CMD_SCR ||
  7177. *pcmd == ELS_CMD_FDISC ||
  7178. *pcmd == ELS_CMD_LOGO ||
  7179. *pcmd == ELS_CMD_PLOGI)) {
  7180. bf_set(els_req64_sp, &wqe->els_req, 1);
  7181. bf_set(els_req64_sid, &wqe->els_req,
  7182. iocbq->vport->fc_myDID);
  7183. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7184. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7185. phba->vpi_ids[iocbq->vport->vpi]);
  7186. } else if (pcmd && iocbq->context1) {
  7187. bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
  7188. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7189. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7190. }
  7191. }
  7192. bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
  7193. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7194. bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
  7195. bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
  7196. bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
  7197. bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
  7198. bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7199. bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
  7200. break;
  7201. case CMD_XMIT_SEQUENCE64_CX:
  7202. bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
  7203. iocbq->iocb.un.ulpWord[3]);
  7204. bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
  7205. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7206. /* The entire sequence is transmitted for this IOCB */
  7207. xmit_len = total_len;
  7208. cmnd = CMD_XMIT_SEQUENCE64_CR;
  7209. if (phba->link_flag & LS_LOOPBACK_MODE)
  7210. bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
  7211. case CMD_XMIT_SEQUENCE64_CR:
  7212. /* word3 iocb=io_tag32 wqe=reserved */
  7213. wqe->xmit_sequence.rsvd3 = 0;
  7214. /* word4 relative_offset memcpy */
  7215. /* word5 r_ctl/df_ctl memcpy */
  7216. bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
  7217. bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
  7218. bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
  7219. LPFC_WQE_IOD_WRITE);
  7220. bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
  7221. LPFC_WQE_LENLOC_WORD12);
  7222. bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
  7223. wqe->xmit_sequence.xmit_len = xmit_len;
  7224. command_type = OTHER_COMMAND;
  7225. break;
  7226. case CMD_XMIT_BCAST64_CN:
  7227. /* word3 iocb=iotag32 wqe=seq_payload_len */
  7228. wqe->xmit_bcast64.seq_payload_len = xmit_len;
  7229. /* word4 iocb=rsvd wqe=rsvd */
  7230. /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
  7231. /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
  7232. bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
  7233. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7234. bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
  7235. bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
  7236. bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
  7237. LPFC_WQE_LENLOC_WORD3);
  7238. bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
  7239. break;
  7240. case CMD_FCP_IWRITE64_CR:
  7241. command_type = FCP_COMMAND_DATA_OUT;
  7242. /* word3 iocb=iotag wqe=payload_offset_len */
  7243. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7244. wqe->fcp_iwrite.payload_offset_len =
  7245. xmit_len + sizeof(struct fcp_rsp);
  7246. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7247. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7248. bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
  7249. iocbq->iocb.ulpFCP2Rcvy);
  7250. bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
  7251. /* Always open the exchange */
  7252. bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
  7253. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  7254. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
  7255. LPFC_WQE_LENLOC_WORD4);
  7256. bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
  7257. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
  7258. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7259. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7260. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7261. }
  7262. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
  7263. break;
  7264. case CMD_FCP_IREAD64_CR:
  7265. /* word3 iocb=iotag wqe=payload_offset_len */
  7266. /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
  7267. wqe->fcp_iread.payload_offset_len =
  7268. xmit_len + sizeof(struct fcp_rsp);
  7269. /* word4 iocb=parameter wqe=total_xfer_length memcpy */
  7270. /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
  7271. bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
  7272. iocbq->iocb.ulpFCP2Rcvy);
  7273. bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
  7274. /* Always open the exchange */
  7275. bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
  7276. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  7277. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
  7278. LPFC_WQE_LENLOC_WORD4);
  7279. bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
  7280. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
  7281. if (iocbq->iocb_flag & LPFC_IO_DIF) {
  7282. iocbq->iocb_flag &= ~LPFC_IO_DIF;
  7283. bf_set(wqe_dif, &wqe->generic.wqe_com, 1);
  7284. }
  7285. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
  7286. break;
  7287. case CMD_FCP_ICMND64_CR:
  7288. /* word3 iocb=IO_TAG wqe=reserved */
  7289. wqe->fcp_icmd.rsrvd3 = 0;
  7290. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  7291. /* Always open the exchange */
  7292. bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
  7293. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
  7294. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
  7295. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  7296. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
  7297. LPFC_WQE_LENLOC_NONE);
  7298. bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
  7299. break;
  7300. case CMD_GEN_REQUEST64_CR:
  7301. /* For this command calculate the xmit length of the
  7302. * request bde.
  7303. */
  7304. xmit_len = 0;
  7305. numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
  7306. sizeof(struct ulp_bde64);
  7307. for (i = 0; i < numBdes; i++) {
  7308. bde.tus.w = le32_to_cpu(bpl[i].tus.w);
  7309. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  7310. break;
  7311. xmit_len += bde.tus.f.bdeSize;
  7312. }
  7313. /* word3 iocb=IO_TAG wqe=request_payload_len */
  7314. wqe->gen_req.request_payload_len = xmit_len;
  7315. /* word4 iocb=parameter wqe=relative_offset memcpy */
  7316. /* word5 [rctl, type, df_ctl, la] copied in memcpy */
  7317. /* word6 context tag copied in memcpy */
  7318. if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
  7319. ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
  7320. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7321. "2015 Invalid CT %x command 0x%x\n",
  7322. ct, iocbq->iocb.ulpCommand);
  7323. return IOCB_ERROR;
  7324. }
  7325. bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
  7326. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
  7327. bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
  7328. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  7329. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  7330. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  7331. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  7332. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  7333. command_type = OTHER_COMMAND;
  7334. break;
  7335. case CMD_XMIT_ELS_RSP64_CX:
  7336. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7337. /* words0-2 BDE memcpy */
  7338. /* word3 iocb=iotag32 wqe=response_payload_len */
  7339. wqe->xmit_els_rsp.response_payload_len = xmit_len;
  7340. /* word4 iocb=did wge=rsvd. */
  7341. wqe->xmit_els_rsp.rsvd4 = 0;
  7342. /* word5 iocb=rsvd wge=did */
  7343. bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
  7344. iocbq->iocb.un.elsreq64.remoteID);
  7345. bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
  7346. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7347. bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
  7348. bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
  7349. iocbq->iocb.unsli3.rcvsli3.ox_id);
  7350. if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
  7351. bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
  7352. phba->vpi_ids[iocbq->vport->vpi]);
  7353. bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
  7354. bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
  7355. bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
  7356. bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
  7357. LPFC_WQE_LENLOC_WORD3);
  7358. bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
  7359. bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
  7360. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  7361. pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
  7362. iocbq->context2)->virt);
  7363. if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
  7364. bf_set(els_req64_sp, &wqe->els_req, 1);
  7365. bf_set(els_req64_sid, &wqe->els_req,
  7366. iocbq->vport->fc_myDID);
  7367. bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
  7368. bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
  7369. phba->vpi_ids[phba->pport->vpi]);
  7370. }
  7371. command_type = OTHER_COMMAND;
  7372. break;
  7373. case CMD_CLOSE_XRI_CN:
  7374. case CMD_ABORT_XRI_CN:
  7375. case CMD_ABORT_XRI_CX:
  7376. /* words 0-2 memcpy should be 0 rserved */
  7377. /* port will send abts */
  7378. abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
  7379. if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
  7380. abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
  7381. fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
  7382. } else
  7383. fip = 0;
  7384. if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
  7385. /*
  7386. * The link is down, or the command was ELS_FIP
  7387. * so the fw does not need to send abts
  7388. * on the wire.
  7389. */
  7390. bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
  7391. else
  7392. bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
  7393. bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
  7394. /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
  7395. wqe->abort_cmd.rsrvd5 = 0;
  7396. bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
  7397. ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
  7398. abort_tag = iocbq->iocb.un.acxri.abortIoTag;
  7399. /*
  7400. * The abort handler will send us CMD_ABORT_XRI_CN or
  7401. * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
  7402. */
  7403. bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  7404. bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
  7405. bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
  7406. LPFC_WQE_LENLOC_NONE);
  7407. cmnd = CMD_ABORT_XRI_CX;
  7408. command_type = OTHER_COMMAND;
  7409. xritag = 0;
  7410. break;
  7411. case CMD_XMIT_BLS_RSP64_CX:
  7412. ndlp = (struct lpfc_nodelist *)iocbq->context1;
  7413. /* As BLS ABTS RSP WQE is very different from other WQEs,
  7414. * we re-construct this WQE here based on information in
  7415. * iocbq from scratch.
  7416. */
  7417. memset(wqe, 0, sizeof(union lpfc_wqe));
  7418. /* OX_ID is invariable to who sent ABTS to CT exchange */
  7419. bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
  7420. bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
  7421. if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
  7422. LPFC_ABTS_UNSOL_INT) {
  7423. /* ABTS sent by initiator to CT exchange, the
  7424. * RX_ID field will be filled with the newly
  7425. * allocated responder XRI.
  7426. */
  7427. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7428. iocbq->sli4_xritag);
  7429. } else {
  7430. /* ABTS sent by responder to CT exchange, the
  7431. * RX_ID field will be filled with the responder
  7432. * RX_ID from ABTS.
  7433. */
  7434. bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
  7435. bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
  7436. }
  7437. bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
  7438. bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
  7439. /* Use CT=VPI */
  7440. bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
  7441. ndlp->nlp_DID);
  7442. bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
  7443. iocbq->iocb.ulpContext);
  7444. bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
  7445. bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
  7446. phba->vpi_ids[phba->pport->vpi]);
  7447. bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
  7448. bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
  7449. LPFC_WQE_LENLOC_NONE);
  7450. /* Overwrite the pre-set comnd type with OTHER_COMMAND */
  7451. command_type = OTHER_COMMAND;
  7452. if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
  7453. bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
  7454. bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
  7455. bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
  7456. bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
  7457. bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
  7458. bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
  7459. }
  7460. break;
  7461. case CMD_XRI_ABORTED_CX:
  7462. case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
  7463. case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
  7464. case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
  7465. case CMD_FCP_TRSP64_CX: /* Target mode rcv */
  7466. case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
  7467. default:
  7468. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7469. "2014 Invalid command 0x%x\n",
  7470. iocbq->iocb.ulpCommand);
  7471. return IOCB_ERROR;
  7472. break;
  7473. }
  7474. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
  7475. bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
  7476. wqe->generic.wqe_com.abort_tag = abort_tag;
  7477. bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
  7478. bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
  7479. bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
  7480. bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  7481. return 0;
  7482. }
  7483. /**
  7484. * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
  7485. * @phba: Pointer to HBA context object.
  7486. * @ring_number: SLI ring number to issue iocb on.
  7487. * @piocb: Pointer to command iocb.
  7488. * @flag: Flag indicating if this command can be put into txq.
  7489. *
  7490. * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
  7491. * an iocb command to an HBA with SLI-4 interface spec.
  7492. *
  7493. * This function is called with hbalock held. The function will return success
  7494. * after it successfully submit the iocb to firmware or after adding to the
  7495. * txq.
  7496. **/
  7497. static int
  7498. __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
  7499. struct lpfc_iocbq *piocb, uint32_t flag)
  7500. {
  7501. struct lpfc_sglq *sglq;
  7502. union lpfc_wqe wqe;
  7503. struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
  7504. if (piocb->sli4_xritag == NO_XRI) {
  7505. if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
  7506. piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
  7507. sglq = NULL;
  7508. else {
  7509. if (pring->txq_cnt) {
  7510. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7511. __lpfc_sli_ringtx_put(phba,
  7512. pring, piocb);
  7513. return IOCB_SUCCESS;
  7514. } else {
  7515. return IOCB_BUSY;
  7516. }
  7517. } else {
  7518. sglq = __lpfc_sli_get_sglq(phba, piocb);
  7519. if (!sglq) {
  7520. if (!(flag & SLI_IOCB_RET_IOCB)) {
  7521. __lpfc_sli_ringtx_put(phba,
  7522. pring,
  7523. piocb);
  7524. return IOCB_SUCCESS;
  7525. } else
  7526. return IOCB_BUSY;
  7527. }
  7528. }
  7529. }
  7530. } else if (piocb->iocb_flag & LPFC_IO_FCP) {
  7531. /* These IO's already have an XRI and a mapped sgl. */
  7532. sglq = NULL;
  7533. } else {
  7534. /*
  7535. * This is a continuation of a commandi,(CX) so this
  7536. * sglq is on the active list
  7537. */
  7538. sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
  7539. if (!sglq)
  7540. return IOCB_ERROR;
  7541. }
  7542. if (sglq) {
  7543. piocb->sli4_lxritag = sglq->sli4_lxritag;
  7544. piocb->sli4_xritag = sglq->sli4_xritag;
  7545. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
  7546. return IOCB_ERROR;
  7547. }
  7548. if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
  7549. return IOCB_ERROR;
  7550. if ((piocb->iocb_flag & LPFC_IO_FCP) ||
  7551. (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
  7552. /*
  7553. * For FCP command IOCB, get a new WQ index to distribute
  7554. * WQE across the WQsr. On the other hand, for abort IOCB,
  7555. * it carries the same WQ index to the original command
  7556. * IOCB.
  7557. */
  7558. if (piocb->iocb_flag & LPFC_IO_FCP)
  7559. piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
  7560. if (unlikely(!phba->sli4_hba.fcp_wq))
  7561. return IOCB_ERROR;
  7562. if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
  7563. &wqe))
  7564. return IOCB_ERROR;
  7565. } else {
  7566. if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  7567. return IOCB_ERROR;
  7568. }
  7569. lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
  7570. return 0;
  7571. }
  7572. /**
  7573. * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
  7574. *
  7575. * This routine wraps the actual lockless version for issusing IOCB function
  7576. * pointer from the lpfc_hba struct.
  7577. *
  7578. * Return codes:
  7579. * IOCB_ERROR - Error
  7580. * IOCB_SUCCESS - Success
  7581. * IOCB_BUSY - Busy
  7582. **/
  7583. int
  7584. __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7585. struct lpfc_iocbq *piocb, uint32_t flag)
  7586. {
  7587. return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7588. }
  7589. /**
  7590. * lpfc_sli_api_table_setup - Set up sli api function jump table
  7591. * @phba: The hba struct for which this call is being executed.
  7592. * @dev_grp: The HBA PCI-Device group number.
  7593. *
  7594. * This routine sets up the SLI interface API function jump table in @phba
  7595. * struct.
  7596. * Returns: 0 - success, -ENODEV - failure.
  7597. **/
  7598. int
  7599. lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  7600. {
  7601. switch (dev_grp) {
  7602. case LPFC_PCI_DEV_LP:
  7603. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
  7604. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
  7605. break;
  7606. case LPFC_PCI_DEV_OC:
  7607. phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
  7608. phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
  7609. break;
  7610. default:
  7611. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  7612. "1419 Invalid HBA PCI-device group: 0x%x\n",
  7613. dev_grp);
  7614. return -ENODEV;
  7615. break;
  7616. }
  7617. phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
  7618. return 0;
  7619. }
  7620. /**
  7621. * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
  7622. * @phba: Pointer to HBA context object.
  7623. * @pring: Pointer to driver SLI ring object.
  7624. * @piocb: Pointer to command iocb.
  7625. * @flag: Flag indicating if this command can be put into txq.
  7626. *
  7627. * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
  7628. * function. This function gets the hbalock and calls
  7629. * __lpfc_sli_issue_iocb function and will return the error returned
  7630. * by __lpfc_sli_issue_iocb function. This wrapper is used by
  7631. * functions which do not hold hbalock.
  7632. **/
  7633. int
  7634. lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
  7635. struct lpfc_iocbq *piocb, uint32_t flag)
  7636. {
  7637. unsigned long iflags;
  7638. int rc;
  7639. spin_lock_irqsave(&phba->hbalock, iflags);
  7640. rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
  7641. spin_unlock_irqrestore(&phba->hbalock, iflags);
  7642. return rc;
  7643. }
  7644. /**
  7645. * lpfc_extra_ring_setup - Extra ring setup function
  7646. * @phba: Pointer to HBA context object.
  7647. *
  7648. * This function is called while driver attaches with the
  7649. * HBA to setup the extra ring. The extra ring is used
  7650. * only when driver needs to support target mode functionality
  7651. * or IP over FC functionalities.
  7652. *
  7653. * This function is called with no lock held.
  7654. **/
  7655. static int
  7656. lpfc_extra_ring_setup( struct lpfc_hba *phba)
  7657. {
  7658. struct lpfc_sli *psli;
  7659. struct lpfc_sli_ring *pring;
  7660. psli = &phba->sli;
  7661. /* Adjust cmd/rsp ring iocb entries more evenly */
  7662. /* Take some away from the FCP ring */
  7663. pring = &psli->ring[psli->fcp_ring];
  7664. pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7665. pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7666. pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7667. pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7668. /* and give them to the extra ring */
  7669. pring = &psli->ring[psli->extra_ring];
  7670. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7671. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7672. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7673. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7674. /* Setup default profile for this ring */
  7675. pring->iotag_max = 4096;
  7676. pring->num_mask = 1;
  7677. pring->prt[0].profile = 0; /* Mask 0 */
  7678. pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
  7679. pring->prt[0].type = phba->cfg_multi_ring_type;
  7680. pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
  7681. return 0;
  7682. }
  7683. /* lpfc_sli_abts_recover_port - Recover a port that failed an ABTS.
  7684. * @vport: pointer to virtual port object.
  7685. * @ndlp: nodelist pointer for the impacted rport.
  7686. *
  7687. * The driver calls this routine in response to a XRI ABORT CQE
  7688. * event from the port. In this event, the driver is required to
  7689. * recover its login to the rport even though its login may be valid
  7690. * from the driver's perspective. The failed ABTS notice from the
  7691. * port indicates the rport is not responding.
  7692. */
  7693. static void
  7694. lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
  7695. struct lpfc_nodelist *ndlp)
  7696. {
  7697. struct Scsi_Host *shost;
  7698. struct lpfc_hba *phba;
  7699. unsigned long flags = 0;
  7700. shost = lpfc_shost_from_vport(vport);
  7701. phba = vport->phba;
  7702. if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
  7703. lpfc_printf_log(phba, KERN_INFO,
  7704. LOG_SLI, "3093 No rport recovery needed. "
  7705. "rport in state 0x%x\n",
  7706. ndlp->nlp_state);
  7707. return;
  7708. }
  7709. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7710. "3094 Start rport recovery on shost id 0x%x "
  7711. "fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
  7712. "flags 0x%x\n",
  7713. shost->host_no, ndlp->nlp_DID,
  7714. vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
  7715. ndlp->nlp_flag);
  7716. /*
  7717. * The rport is not responding. Don't attempt ADISC recovery.
  7718. * Remove the FCP-2 flag to force a PLOGI.
  7719. */
  7720. spin_lock_irqsave(shost->host_lock, flags);
  7721. ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
  7722. spin_unlock_irqrestore(shost->host_lock, flags);
  7723. lpfc_disc_state_machine(vport, ndlp, NULL,
  7724. NLP_EVT_DEVICE_RECOVERY);
  7725. lpfc_cancel_retry_delay_tmo(vport, ndlp);
  7726. spin_lock_irqsave(shost->host_lock, flags);
  7727. ndlp->nlp_flag |= NLP_NPR_2B_DISC;
  7728. spin_unlock_irqrestore(shost->host_lock, flags);
  7729. lpfc_disc_start(vport);
  7730. }
  7731. /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
  7732. * @phba: Pointer to HBA context object.
  7733. * @iocbq: Pointer to iocb object.
  7734. *
  7735. * The async_event handler calls this routine when it receives
  7736. * an ASYNC_STATUS_CN event from the port. The port generates
  7737. * this event when an Abort Sequence request to an rport fails
  7738. * twice in succession. The abort could be originated by the
  7739. * driver or by the port. The ABTS could have been for an ELS
  7740. * or FCP IO. The port only generates this event when an ABTS
  7741. * fails to complete after one retry.
  7742. */
  7743. static void
  7744. lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
  7745. struct lpfc_iocbq *iocbq)
  7746. {
  7747. struct lpfc_nodelist *ndlp = NULL;
  7748. uint16_t rpi = 0, vpi = 0;
  7749. struct lpfc_vport *vport = NULL;
  7750. /* The rpi in the ulpContext is vport-sensitive. */
  7751. vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
  7752. rpi = iocbq->iocb.ulpContext;
  7753. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7754. "3092 Port generated ABTS async event "
  7755. "on vpi %d rpi %d status 0x%x\n",
  7756. vpi, rpi, iocbq->iocb.ulpStatus);
  7757. vport = lpfc_find_vport_by_vpid(phba, vpi);
  7758. if (!vport)
  7759. goto err_exit;
  7760. ndlp = lpfc_findnode_rpi(vport, rpi);
  7761. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
  7762. goto err_exit;
  7763. if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
  7764. lpfc_sli_abts_recover_port(vport, ndlp);
  7765. return;
  7766. err_exit:
  7767. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7768. "3095 Event Context not found, no "
  7769. "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
  7770. iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
  7771. vpi, rpi);
  7772. }
  7773. /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
  7774. * @phba: pointer to HBA context object.
  7775. * @ndlp: nodelist pointer for the impacted rport.
  7776. * @axri: pointer to the wcqe containing the failed exchange.
  7777. *
  7778. * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
  7779. * port. The port generates this event when an abort exchange request to an
  7780. * rport fails twice in succession with no reply. The abort could be originated
  7781. * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
  7782. */
  7783. void
  7784. lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
  7785. struct lpfc_nodelist *ndlp,
  7786. struct sli4_wcqe_xri_aborted *axri)
  7787. {
  7788. struct lpfc_vport *vport;
  7789. uint32_t ext_status = 0;
  7790. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  7791. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  7792. "3115 Node Context not found, driver "
  7793. "ignoring abts err event\n");
  7794. return;
  7795. }
  7796. vport = ndlp->vport;
  7797. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  7798. "3116 Port generated FCP XRI ABORT event on "
  7799. "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
  7800. ndlp->vport->vpi, ndlp->nlp_rpi,
  7801. bf_get(lpfc_wcqe_xa_xri, axri),
  7802. bf_get(lpfc_wcqe_xa_status, axri),
  7803. axri->parameter);
  7804. /*
  7805. * Catch the ABTS protocol failure case. Older OCe FW releases returned
  7806. * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
  7807. * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
  7808. */
  7809. ext_status = axri->parameter & WCQE_PARAM_MASK;
  7810. if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
  7811. ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
  7812. lpfc_sli_abts_recover_port(vport, ndlp);
  7813. }
  7814. /**
  7815. * lpfc_sli_async_event_handler - ASYNC iocb handler function
  7816. * @phba: Pointer to HBA context object.
  7817. * @pring: Pointer to driver SLI ring object.
  7818. * @iocbq: Pointer to iocb object.
  7819. *
  7820. * This function is called by the slow ring event handler
  7821. * function when there is an ASYNC event iocb in the ring.
  7822. * This function is called with no lock held.
  7823. * Currently this function handles only temperature related
  7824. * ASYNC events. The function decodes the temperature sensor
  7825. * event message and posts events for the management applications.
  7826. **/
  7827. static void
  7828. lpfc_sli_async_event_handler(struct lpfc_hba * phba,
  7829. struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
  7830. {
  7831. IOCB_t *icmd;
  7832. uint16_t evt_code;
  7833. struct temp_event temp_event_data;
  7834. struct Scsi_Host *shost;
  7835. uint32_t *iocb_w;
  7836. icmd = &iocbq->iocb;
  7837. evt_code = icmd->un.asyncstat.evt_code;
  7838. switch (evt_code) {
  7839. case ASYNC_TEMP_WARN:
  7840. case ASYNC_TEMP_SAFE:
  7841. temp_event_data.data = (uint32_t) icmd->ulpContext;
  7842. temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
  7843. if (evt_code == ASYNC_TEMP_WARN) {
  7844. temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
  7845. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7846. "0347 Adapter is very hot, please take "
  7847. "corrective action. temperature : %d Celsius\n",
  7848. (uint32_t) icmd->ulpContext);
  7849. } else {
  7850. temp_event_data.event_code = LPFC_NORMAL_TEMP;
  7851. lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
  7852. "0340 Adapter temperature is OK now. "
  7853. "temperature : %d Celsius\n",
  7854. (uint32_t) icmd->ulpContext);
  7855. }
  7856. /* Send temperature change event to applications */
  7857. shost = lpfc_shost_from_vport(phba->pport);
  7858. fc_host_post_vendor_event(shost, fc_get_event_number(),
  7859. sizeof(temp_event_data), (char *) &temp_event_data,
  7860. LPFC_NL_VENDOR_ID);
  7861. break;
  7862. case ASYNC_STATUS_CN:
  7863. lpfc_sli_abts_err_handler(phba, iocbq);
  7864. break;
  7865. default:
  7866. iocb_w = (uint32_t *) icmd;
  7867. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  7868. "0346 Ring %d handler: unexpected ASYNC_STATUS"
  7869. " evt_code 0x%x\n"
  7870. "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
  7871. "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
  7872. "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
  7873. "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
  7874. pring->ringno, icmd->un.asyncstat.evt_code,
  7875. iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
  7876. iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
  7877. iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
  7878. iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
  7879. break;
  7880. }
  7881. }
  7882. /**
  7883. * lpfc_sli_setup - SLI ring setup function
  7884. * @phba: Pointer to HBA context object.
  7885. *
  7886. * lpfc_sli_setup sets up rings of the SLI interface with
  7887. * number of iocbs per ring and iotags. This function is
  7888. * called while driver attach to the HBA and before the
  7889. * interrupts are enabled. So there is no need for locking.
  7890. *
  7891. * This function always returns 0.
  7892. **/
  7893. int
  7894. lpfc_sli_setup(struct lpfc_hba *phba)
  7895. {
  7896. int i, totiocbsize = 0;
  7897. struct lpfc_sli *psli = &phba->sli;
  7898. struct lpfc_sli_ring *pring;
  7899. psli->num_rings = MAX_CONFIGURED_RINGS;
  7900. psli->sli_flag = 0;
  7901. psli->fcp_ring = LPFC_FCP_RING;
  7902. psli->next_ring = LPFC_FCP_NEXT_RING;
  7903. psli->extra_ring = LPFC_EXTRA_RING;
  7904. psli->iocbq_lookup = NULL;
  7905. psli->iocbq_lookup_len = 0;
  7906. psli->last_iotag = 0;
  7907. for (i = 0; i < psli->num_rings; i++) {
  7908. pring = &psli->ring[i];
  7909. switch (i) {
  7910. case LPFC_FCP_RING: /* ring 0 - FCP */
  7911. /* numCiocb and numRiocb are used in config_port */
  7912. pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
  7913. pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
  7914. pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
  7915. pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
  7916. pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
  7917. pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
  7918. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7919. SLI3_IOCB_CMD_SIZE :
  7920. SLI2_IOCB_CMD_SIZE;
  7921. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7922. SLI3_IOCB_RSP_SIZE :
  7923. SLI2_IOCB_RSP_SIZE;
  7924. pring->iotag_ctr = 0;
  7925. pring->iotag_max =
  7926. (phba->cfg_hba_queue_depth * 2);
  7927. pring->fast_iotag = pring->iotag_max;
  7928. pring->num_mask = 0;
  7929. break;
  7930. case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
  7931. /* numCiocb and numRiocb are used in config_port */
  7932. pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
  7933. pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
  7934. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7935. SLI3_IOCB_CMD_SIZE :
  7936. SLI2_IOCB_CMD_SIZE;
  7937. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7938. SLI3_IOCB_RSP_SIZE :
  7939. SLI2_IOCB_RSP_SIZE;
  7940. pring->iotag_max = phba->cfg_hba_queue_depth;
  7941. pring->num_mask = 0;
  7942. break;
  7943. case LPFC_ELS_RING: /* ring 2 - ELS / CT */
  7944. /* numCiocb and numRiocb are used in config_port */
  7945. pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
  7946. pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
  7947. pring->sizeCiocb = (phba->sli_rev == 3) ?
  7948. SLI3_IOCB_CMD_SIZE :
  7949. SLI2_IOCB_CMD_SIZE;
  7950. pring->sizeRiocb = (phba->sli_rev == 3) ?
  7951. SLI3_IOCB_RSP_SIZE :
  7952. SLI2_IOCB_RSP_SIZE;
  7953. pring->fast_iotag = 0;
  7954. pring->iotag_ctr = 0;
  7955. pring->iotag_max = 4096;
  7956. pring->lpfc_sli_rcv_async_status =
  7957. lpfc_sli_async_event_handler;
  7958. pring->num_mask = LPFC_MAX_RING_MASK;
  7959. pring->prt[0].profile = 0; /* Mask 0 */
  7960. pring->prt[0].rctl = FC_RCTL_ELS_REQ;
  7961. pring->prt[0].type = FC_TYPE_ELS;
  7962. pring->prt[0].lpfc_sli_rcv_unsol_event =
  7963. lpfc_els_unsol_event;
  7964. pring->prt[1].profile = 0; /* Mask 1 */
  7965. pring->prt[1].rctl = FC_RCTL_ELS_REP;
  7966. pring->prt[1].type = FC_TYPE_ELS;
  7967. pring->prt[1].lpfc_sli_rcv_unsol_event =
  7968. lpfc_els_unsol_event;
  7969. pring->prt[2].profile = 0; /* Mask 2 */
  7970. /* NameServer Inquiry */
  7971. pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
  7972. /* NameServer */
  7973. pring->prt[2].type = FC_TYPE_CT;
  7974. pring->prt[2].lpfc_sli_rcv_unsol_event =
  7975. lpfc_ct_unsol_event;
  7976. pring->prt[3].profile = 0; /* Mask 3 */
  7977. /* NameServer response */
  7978. pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
  7979. /* NameServer */
  7980. pring->prt[3].type = FC_TYPE_CT;
  7981. pring->prt[3].lpfc_sli_rcv_unsol_event =
  7982. lpfc_ct_unsol_event;
  7983. /* abort unsolicited sequence */
  7984. pring->prt[4].profile = 0; /* Mask 4 */
  7985. pring->prt[4].rctl = FC_RCTL_BA_ABTS;
  7986. pring->prt[4].type = FC_TYPE_BLS;
  7987. pring->prt[4].lpfc_sli_rcv_unsol_event =
  7988. lpfc_sli4_ct_abort_unsol_event;
  7989. break;
  7990. }
  7991. totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
  7992. (pring->numRiocb * pring->sizeRiocb);
  7993. }
  7994. if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
  7995. /* Too many cmd / rsp ring entries in SLI2 SLIM */
  7996. printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
  7997. "SLI2 SLIM Data: x%x x%lx\n",
  7998. phba->brd_no, totiocbsize,
  7999. (unsigned long) MAX_SLIM_IOCB_SIZE);
  8000. }
  8001. if (phba->cfg_multi_ring_support == 2)
  8002. lpfc_extra_ring_setup(phba);
  8003. return 0;
  8004. }
  8005. /**
  8006. * lpfc_sli_queue_setup - Queue initialization function
  8007. * @phba: Pointer to HBA context object.
  8008. *
  8009. * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
  8010. * ring. This function also initializes ring indices of each ring.
  8011. * This function is called during the initialization of the SLI
  8012. * interface of an HBA.
  8013. * This function is called with no lock held and always returns
  8014. * 1.
  8015. **/
  8016. int
  8017. lpfc_sli_queue_setup(struct lpfc_hba *phba)
  8018. {
  8019. struct lpfc_sli *psli;
  8020. struct lpfc_sli_ring *pring;
  8021. int i;
  8022. psli = &phba->sli;
  8023. spin_lock_irq(&phba->hbalock);
  8024. INIT_LIST_HEAD(&psli->mboxq);
  8025. INIT_LIST_HEAD(&psli->mboxq_cmpl);
  8026. /* Initialize list headers for txq and txcmplq as double linked lists */
  8027. for (i = 0; i < psli->num_rings; i++) {
  8028. pring = &psli->ring[i];
  8029. pring->ringno = i;
  8030. pring->next_cmdidx = 0;
  8031. pring->local_getidx = 0;
  8032. pring->cmdidx = 0;
  8033. INIT_LIST_HEAD(&pring->txq);
  8034. INIT_LIST_HEAD(&pring->txcmplq);
  8035. INIT_LIST_HEAD(&pring->iocb_continueq);
  8036. INIT_LIST_HEAD(&pring->iocb_continue_saveq);
  8037. INIT_LIST_HEAD(&pring->postbufq);
  8038. }
  8039. spin_unlock_irq(&phba->hbalock);
  8040. return 1;
  8041. }
  8042. /**
  8043. * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
  8044. * @phba: Pointer to HBA context object.
  8045. *
  8046. * This routine flushes the mailbox command subsystem. It will unconditionally
  8047. * flush all the mailbox commands in the three possible stages in the mailbox
  8048. * command sub-system: pending mailbox command queue; the outstanding mailbox
  8049. * command; and completed mailbox command queue. It is caller's responsibility
  8050. * to make sure that the driver is in the proper state to flush the mailbox
  8051. * command sub-system. Namely, the posting of mailbox commands into the
  8052. * pending mailbox command queue from the various clients must be stopped;
  8053. * either the HBA is in a state that it will never works on the outstanding
  8054. * mailbox command (such as in EEH or ERATT conditions) or the outstanding
  8055. * mailbox command has been completed.
  8056. **/
  8057. static void
  8058. lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
  8059. {
  8060. LIST_HEAD(completions);
  8061. struct lpfc_sli *psli = &phba->sli;
  8062. LPFC_MBOXQ_t *pmb;
  8063. unsigned long iflag;
  8064. /* Flush all the mailbox commands in the mbox system */
  8065. spin_lock_irqsave(&phba->hbalock, iflag);
  8066. /* The pending mailbox command queue */
  8067. list_splice_init(&phba->sli.mboxq, &completions);
  8068. /* The outstanding active mailbox command */
  8069. if (psli->mbox_active) {
  8070. list_add_tail(&psli->mbox_active->list, &completions);
  8071. psli->mbox_active = NULL;
  8072. psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  8073. }
  8074. /* The completed mailbox command queue */
  8075. list_splice_init(&phba->sli.mboxq_cmpl, &completions);
  8076. spin_unlock_irqrestore(&phba->hbalock, iflag);
  8077. /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
  8078. while (!list_empty(&completions)) {
  8079. list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
  8080. pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
  8081. if (pmb->mbox_cmpl)
  8082. pmb->mbox_cmpl(phba, pmb);
  8083. }
  8084. }
  8085. /**
  8086. * lpfc_sli_host_down - Vport cleanup function
  8087. * @vport: Pointer to virtual port object.
  8088. *
  8089. * lpfc_sli_host_down is called to clean up the resources
  8090. * associated with a vport before destroying virtual
  8091. * port data structures.
  8092. * This function does following operations:
  8093. * - Free discovery resources associated with this virtual
  8094. * port.
  8095. * - Free iocbs associated with this virtual port in
  8096. * the txq.
  8097. * - Send abort for all iocb commands associated with this
  8098. * vport in txcmplq.
  8099. *
  8100. * This function is called with no lock held and always returns 1.
  8101. **/
  8102. int
  8103. lpfc_sli_host_down(struct lpfc_vport *vport)
  8104. {
  8105. LIST_HEAD(completions);
  8106. struct lpfc_hba *phba = vport->phba;
  8107. struct lpfc_sli *psli = &phba->sli;
  8108. struct lpfc_sli_ring *pring;
  8109. struct lpfc_iocbq *iocb, *next_iocb;
  8110. int i;
  8111. unsigned long flags = 0;
  8112. uint16_t prev_pring_flag;
  8113. lpfc_cleanup_discovery_resources(vport);
  8114. spin_lock_irqsave(&phba->hbalock, flags);
  8115. for (i = 0; i < psli->num_rings; i++) {
  8116. pring = &psli->ring[i];
  8117. prev_pring_flag = pring->flag;
  8118. /* Only slow rings */
  8119. if (pring->ringno == LPFC_ELS_RING) {
  8120. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8121. /* Set the lpfc data pending flag */
  8122. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8123. }
  8124. /*
  8125. * Error everything on the txq since these iocbs have not been
  8126. * given to the FW yet.
  8127. */
  8128. list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
  8129. if (iocb->vport != vport)
  8130. continue;
  8131. list_move_tail(&iocb->list, &completions);
  8132. pring->txq_cnt--;
  8133. }
  8134. /* Next issue ABTS for everything on the txcmplq */
  8135. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
  8136. list) {
  8137. if (iocb->vport != vport)
  8138. continue;
  8139. lpfc_sli_issue_abort_iotag(phba, pring, iocb);
  8140. }
  8141. pring->flag = prev_pring_flag;
  8142. }
  8143. spin_unlock_irqrestore(&phba->hbalock, flags);
  8144. /* Cancel all the IOCBs from the completions list */
  8145. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8146. IOERR_SLI_DOWN);
  8147. return 1;
  8148. }
  8149. /**
  8150. * lpfc_sli_hba_down - Resource cleanup function for the HBA
  8151. * @phba: Pointer to HBA context object.
  8152. *
  8153. * This function cleans up all iocb, buffers, mailbox commands
  8154. * while shutting down the HBA. This function is called with no
  8155. * lock held and always returns 1.
  8156. * This function does the following to cleanup driver resources:
  8157. * - Free discovery resources for each virtual port
  8158. * - Cleanup any pending fabric iocbs
  8159. * - Iterate through the iocb txq and free each entry
  8160. * in the list.
  8161. * - Free up any buffer posted to the HBA
  8162. * - Free mailbox commands in the mailbox queue.
  8163. **/
  8164. int
  8165. lpfc_sli_hba_down(struct lpfc_hba *phba)
  8166. {
  8167. LIST_HEAD(completions);
  8168. struct lpfc_sli *psli = &phba->sli;
  8169. struct lpfc_sli_ring *pring;
  8170. struct lpfc_dmabuf *buf_ptr;
  8171. unsigned long flags = 0;
  8172. int i;
  8173. /* Shutdown the mailbox command sub-system */
  8174. lpfc_sli_mbox_sys_shutdown(phba);
  8175. lpfc_hba_down_prep(phba);
  8176. lpfc_fabric_abort_hba(phba);
  8177. spin_lock_irqsave(&phba->hbalock, flags);
  8178. for (i = 0; i < psli->num_rings; i++) {
  8179. pring = &psli->ring[i];
  8180. /* Only slow rings */
  8181. if (pring->ringno == LPFC_ELS_RING) {
  8182. pring->flag |= LPFC_DEFERRED_RING_EVENT;
  8183. /* Set the lpfc data pending flag */
  8184. set_bit(LPFC_DATA_READY, &phba->data_flags);
  8185. }
  8186. /*
  8187. * Error everything on the txq since these iocbs have not been
  8188. * given to the FW yet.
  8189. */
  8190. list_splice_init(&pring->txq, &completions);
  8191. pring->txq_cnt = 0;
  8192. }
  8193. spin_unlock_irqrestore(&phba->hbalock, flags);
  8194. /* Cancel all the IOCBs from the completions list */
  8195. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8196. IOERR_SLI_DOWN);
  8197. spin_lock_irqsave(&phba->hbalock, flags);
  8198. list_splice_init(&phba->elsbuf, &completions);
  8199. phba->elsbuf_cnt = 0;
  8200. phba->elsbuf_prev_cnt = 0;
  8201. spin_unlock_irqrestore(&phba->hbalock, flags);
  8202. while (!list_empty(&completions)) {
  8203. list_remove_head(&completions, buf_ptr,
  8204. struct lpfc_dmabuf, list);
  8205. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  8206. kfree(buf_ptr);
  8207. }
  8208. /* Return any active mbox cmds */
  8209. del_timer_sync(&psli->mbox_tmo);
  8210. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  8211. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  8212. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  8213. return 1;
  8214. }
  8215. /**
  8216. * lpfc_sli_pcimem_bcopy - SLI memory copy function
  8217. * @srcp: Source memory pointer.
  8218. * @destp: Destination memory pointer.
  8219. * @cnt: Number of words required to be copied.
  8220. *
  8221. * This function is used for copying data between driver memory
  8222. * and the SLI memory. This function also changes the endianness
  8223. * of each word if native endianness is different from SLI
  8224. * endianness. This function can be called with or without
  8225. * lock.
  8226. **/
  8227. void
  8228. lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8229. {
  8230. uint32_t *src = srcp;
  8231. uint32_t *dest = destp;
  8232. uint32_t ldata;
  8233. int i;
  8234. for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
  8235. ldata = *src;
  8236. ldata = le32_to_cpu(ldata);
  8237. *dest = ldata;
  8238. src++;
  8239. dest++;
  8240. }
  8241. }
  8242. /**
  8243. * lpfc_sli_bemem_bcopy - SLI memory copy function
  8244. * @srcp: Source memory pointer.
  8245. * @destp: Destination memory pointer.
  8246. * @cnt: Number of words required to be copied.
  8247. *
  8248. * This function is used for copying data between a data structure
  8249. * with big endian representation to local endianness.
  8250. * This function can be called with or without lock.
  8251. **/
  8252. void
  8253. lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
  8254. {
  8255. uint32_t *src = srcp;
  8256. uint32_t *dest = destp;
  8257. uint32_t ldata;
  8258. int i;
  8259. for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
  8260. ldata = *src;
  8261. ldata = be32_to_cpu(ldata);
  8262. *dest = ldata;
  8263. src++;
  8264. dest++;
  8265. }
  8266. }
  8267. /**
  8268. * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
  8269. * @phba: Pointer to HBA context object.
  8270. * @pring: Pointer to driver SLI ring object.
  8271. * @mp: Pointer to driver buffer object.
  8272. *
  8273. * This function is called with no lock held.
  8274. * It always return zero after adding the buffer to the postbufq
  8275. * buffer list.
  8276. **/
  8277. int
  8278. lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8279. struct lpfc_dmabuf *mp)
  8280. {
  8281. /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
  8282. later */
  8283. spin_lock_irq(&phba->hbalock);
  8284. list_add_tail(&mp->list, &pring->postbufq);
  8285. pring->postbufq_cnt++;
  8286. spin_unlock_irq(&phba->hbalock);
  8287. return 0;
  8288. }
  8289. /**
  8290. * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
  8291. * @phba: Pointer to HBA context object.
  8292. *
  8293. * When HBQ is enabled, buffers are searched based on tags. This function
  8294. * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
  8295. * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
  8296. * does not conflict with tags of buffer posted for unsolicited events.
  8297. * The function returns the allocated tag. The function is called with
  8298. * no locks held.
  8299. **/
  8300. uint32_t
  8301. lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
  8302. {
  8303. spin_lock_irq(&phba->hbalock);
  8304. phba->buffer_tag_count++;
  8305. /*
  8306. * Always set the QUE_BUFTAG_BIT to distiguish between
  8307. * a tag assigned by HBQ.
  8308. */
  8309. phba->buffer_tag_count |= QUE_BUFTAG_BIT;
  8310. spin_unlock_irq(&phba->hbalock);
  8311. return phba->buffer_tag_count;
  8312. }
  8313. /**
  8314. * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
  8315. * @phba: Pointer to HBA context object.
  8316. * @pring: Pointer to driver SLI ring object.
  8317. * @tag: Buffer tag.
  8318. *
  8319. * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
  8320. * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
  8321. * iocb is posted to the response ring with the tag of the buffer.
  8322. * This function searches the pring->postbufq list using the tag
  8323. * to find buffer associated with CMD_IOCB_RET_XRI64_CX
  8324. * iocb. If the buffer is found then lpfc_dmabuf object of the
  8325. * buffer is returned to the caller else NULL is returned.
  8326. * This function is called with no lock held.
  8327. **/
  8328. struct lpfc_dmabuf *
  8329. lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8330. uint32_t tag)
  8331. {
  8332. struct lpfc_dmabuf *mp, *next_mp;
  8333. struct list_head *slp = &pring->postbufq;
  8334. /* Search postbufq, from the beginning, looking for a match on tag */
  8335. spin_lock_irq(&phba->hbalock);
  8336. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8337. if (mp->buffer_tag == tag) {
  8338. list_del_init(&mp->list);
  8339. pring->postbufq_cnt--;
  8340. spin_unlock_irq(&phba->hbalock);
  8341. return mp;
  8342. }
  8343. }
  8344. spin_unlock_irq(&phba->hbalock);
  8345. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8346. "0402 Cannot find virtual addr for buffer tag on "
  8347. "ring %d Data x%lx x%p x%p x%x\n",
  8348. pring->ringno, (unsigned long) tag,
  8349. slp->next, slp->prev, pring->postbufq_cnt);
  8350. return NULL;
  8351. }
  8352. /**
  8353. * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
  8354. * @phba: Pointer to HBA context object.
  8355. * @pring: Pointer to driver SLI ring object.
  8356. * @phys: DMA address of the buffer.
  8357. *
  8358. * This function searches the buffer list using the dma_address
  8359. * of unsolicited event to find the driver's lpfc_dmabuf object
  8360. * corresponding to the dma_address. The function returns the
  8361. * lpfc_dmabuf object if a buffer is found else it returns NULL.
  8362. * This function is called by the ct and els unsolicited event
  8363. * handlers to get the buffer associated with the unsolicited
  8364. * event.
  8365. *
  8366. * This function is called with no lock held.
  8367. **/
  8368. struct lpfc_dmabuf *
  8369. lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8370. dma_addr_t phys)
  8371. {
  8372. struct lpfc_dmabuf *mp, *next_mp;
  8373. struct list_head *slp = &pring->postbufq;
  8374. /* Search postbufq, from the beginning, looking for a match on phys */
  8375. spin_lock_irq(&phba->hbalock);
  8376. list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
  8377. if (mp->phys == phys) {
  8378. list_del_init(&mp->list);
  8379. pring->postbufq_cnt--;
  8380. spin_unlock_irq(&phba->hbalock);
  8381. return mp;
  8382. }
  8383. }
  8384. spin_unlock_irq(&phba->hbalock);
  8385. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  8386. "0410 Cannot find virtual addr for mapped buf on "
  8387. "ring %d Data x%llx x%p x%p x%x\n",
  8388. pring->ringno, (unsigned long long)phys,
  8389. slp->next, slp->prev, pring->postbufq_cnt);
  8390. return NULL;
  8391. }
  8392. /**
  8393. * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
  8394. * @phba: Pointer to HBA context object.
  8395. * @cmdiocb: Pointer to driver command iocb object.
  8396. * @rspiocb: Pointer to driver response iocb object.
  8397. *
  8398. * This function is the completion handler for the abort iocbs for
  8399. * ELS commands. This function is called from the ELS ring event
  8400. * handler with no lock held. This function frees memory resources
  8401. * associated with the abort iocb.
  8402. **/
  8403. static void
  8404. lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8405. struct lpfc_iocbq *rspiocb)
  8406. {
  8407. IOCB_t *irsp = &rspiocb->iocb;
  8408. uint16_t abort_iotag, abort_context;
  8409. struct lpfc_iocbq *abort_iocb = NULL;
  8410. if (irsp->ulpStatus) {
  8411. /*
  8412. * Assume that the port already completed and returned, or
  8413. * will return the iocb. Just Log the message.
  8414. */
  8415. abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
  8416. abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
  8417. spin_lock_irq(&phba->hbalock);
  8418. if (phba->sli_rev < LPFC_SLI_REV4) {
  8419. if (abort_iotag != 0 &&
  8420. abort_iotag <= phba->sli.last_iotag)
  8421. abort_iocb =
  8422. phba->sli.iocbq_lookup[abort_iotag];
  8423. } else
  8424. /* For sli4 the abort_tag is the XRI,
  8425. * so the abort routine puts the iotag of the iocb
  8426. * being aborted in the context field of the abort
  8427. * IOCB.
  8428. */
  8429. abort_iocb = phba->sli.iocbq_lookup[abort_context];
  8430. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
  8431. "0327 Cannot abort els iocb %p "
  8432. "with tag %x context %x, abort status %x, "
  8433. "abort code %x\n",
  8434. abort_iocb, abort_iotag, abort_context,
  8435. irsp->ulpStatus, irsp->un.ulpWord[4]);
  8436. spin_unlock_irq(&phba->hbalock);
  8437. }
  8438. lpfc_sli_release_iocbq(phba, cmdiocb);
  8439. return;
  8440. }
  8441. /**
  8442. * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
  8443. * @phba: Pointer to HBA context object.
  8444. * @cmdiocb: Pointer to driver command iocb object.
  8445. * @rspiocb: Pointer to driver response iocb object.
  8446. *
  8447. * The function is called from SLI ring event handler with no
  8448. * lock held. This function is the completion handler for ELS commands
  8449. * which are aborted. The function frees memory resources used for
  8450. * the aborted ELS commands.
  8451. **/
  8452. static void
  8453. lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8454. struct lpfc_iocbq *rspiocb)
  8455. {
  8456. IOCB_t *irsp = &rspiocb->iocb;
  8457. /* ELS cmd tag <ulpIoTag> completes */
  8458. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  8459. "0139 Ignoring ELS cmd tag x%x completion Data: "
  8460. "x%x x%x x%x\n",
  8461. irsp->ulpIoTag, irsp->ulpStatus,
  8462. irsp->un.ulpWord[4], irsp->ulpTimeout);
  8463. if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
  8464. lpfc_ct_free_iocb(phba, cmdiocb);
  8465. else
  8466. lpfc_els_free_iocb(phba, cmdiocb);
  8467. return;
  8468. }
  8469. /**
  8470. * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
  8471. * @phba: Pointer to HBA context object.
  8472. * @pring: Pointer to driver SLI ring object.
  8473. * @cmdiocb: Pointer to driver command iocb object.
  8474. *
  8475. * This function issues an abort iocb for the provided command iocb down to
  8476. * the port. Other than the case the outstanding command iocb is an abort
  8477. * request, this function issues abort out unconditionally. This function is
  8478. * called with hbalock held. The function returns 0 when it fails due to
  8479. * memory allocation failure or when the command iocb is an abort request.
  8480. **/
  8481. static int
  8482. lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8483. struct lpfc_iocbq *cmdiocb)
  8484. {
  8485. struct lpfc_vport *vport = cmdiocb->vport;
  8486. struct lpfc_iocbq *abtsiocbp;
  8487. IOCB_t *icmd = NULL;
  8488. IOCB_t *iabt = NULL;
  8489. int retval;
  8490. /*
  8491. * There are certain command types we don't want to abort. And we
  8492. * don't want to abort commands that are already in the process of
  8493. * being aborted.
  8494. */
  8495. icmd = &cmdiocb->iocb;
  8496. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8497. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8498. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8499. return 0;
  8500. /* issue ABTS for this IOCB based on iotag */
  8501. abtsiocbp = __lpfc_sli_get_iocbq(phba);
  8502. if (abtsiocbp == NULL)
  8503. return 0;
  8504. /* This signals the response to set the correct status
  8505. * before calling the completion handler
  8506. */
  8507. cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
  8508. iabt = &abtsiocbp->iocb;
  8509. iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
  8510. iabt->un.acxri.abortContextTag = icmd->ulpContext;
  8511. if (phba->sli_rev == LPFC_SLI_REV4) {
  8512. iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
  8513. iabt->un.acxri.abortContextTag = cmdiocb->iotag;
  8514. }
  8515. else
  8516. iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
  8517. iabt->ulpLe = 1;
  8518. iabt->ulpClass = icmd->ulpClass;
  8519. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8520. abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
  8521. if (cmdiocb->iocb_flag & LPFC_IO_FCP)
  8522. abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
  8523. if (phba->link_state >= LPFC_LINK_UP)
  8524. iabt->ulpCommand = CMD_ABORT_XRI_CN;
  8525. else
  8526. iabt->ulpCommand = CMD_CLOSE_XRI_CN;
  8527. abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
  8528. lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
  8529. "0339 Abort xri x%x, original iotag x%x, "
  8530. "abort cmd iotag x%x\n",
  8531. iabt->un.acxri.abortIoTag,
  8532. iabt->un.acxri.abortContextTag,
  8533. abtsiocbp->iotag);
  8534. retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
  8535. if (retval)
  8536. __lpfc_sli_release_iocbq(phba, abtsiocbp);
  8537. /*
  8538. * Caller to this routine should check for IOCB_ERROR
  8539. * and handle it properly. This routine no longer removes
  8540. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8541. */
  8542. return retval;
  8543. }
  8544. /**
  8545. * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
  8546. * @phba: Pointer to HBA context object.
  8547. * @pring: Pointer to driver SLI ring object.
  8548. * @cmdiocb: Pointer to driver command iocb object.
  8549. *
  8550. * This function issues an abort iocb for the provided command iocb. In case
  8551. * of unloading, the abort iocb will not be issued to commands on the ELS
  8552. * ring. Instead, the callback function shall be changed to those commands
  8553. * so that nothing happens when them finishes. This function is called with
  8554. * hbalock held. The function returns 0 when the command iocb is an abort
  8555. * request.
  8556. **/
  8557. int
  8558. lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
  8559. struct lpfc_iocbq *cmdiocb)
  8560. {
  8561. struct lpfc_vport *vport = cmdiocb->vport;
  8562. int retval = IOCB_ERROR;
  8563. IOCB_t *icmd = NULL;
  8564. /*
  8565. * There are certain command types we don't want to abort. And we
  8566. * don't want to abort commands that are already in the process of
  8567. * being aborted.
  8568. */
  8569. icmd = &cmdiocb->iocb;
  8570. if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
  8571. icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
  8572. (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
  8573. return 0;
  8574. /*
  8575. * If we're unloading, don't abort iocb on the ELS ring, but change
  8576. * the callback so that nothing happens when it finishes.
  8577. */
  8578. if ((vport->load_flag & FC_UNLOADING) &&
  8579. (pring->ringno == LPFC_ELS_RING)) {
  8580. if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
  8581. cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
  8582. else
  8583. cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
  8584. goto abort_iotag_exit;
  8585. }
  8586. /* Now, we try to issue the abort to the cmdiocb out */
  8587. retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
  8588. abort_iotag_exit:
  8589. /*
  8590. * Caller to this routine should check for IOCB_ERROR
  8591. * and handle it properly. This routine no longer removes
  8592. * iocb off txcmplq and call compl in case of IOCB_ERROR.
  8593. */
  8594. return retval;
  8595. }
  8596. /**
  8597. * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
  8598. * @phba: Pointer to HBA context object.
  8599. * @pring: Pointer to driver SLI ring object.
  8600. *
  8601. * This function aborts all iocbs in the given ring and frees all the iocb
  8602. * objects in txq. This function issues abort iocbs unconditionally for all
  8603. * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
  8604. * to complete before the return of this function. The caller is not required
  8605. * to hold any locks.
  8606. **/
  8607. static void
  8608. lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
  8609. {
  8610. LIST_HEAD(completions);
  8611. struct lpfc_iocbq *iocb, *next_iocb;
  8612. if (pring->ringno == LPFC_ELS_RING)
  8613. lpfc_fabric_abort_hba(phba);
  8614. spin_lock_irq(&phba->hbalock);
  8615. /* Take off all the iocbs on txq for cancelling */
  8616. list_splice_init(&pring->txq, &completions);
  8617. pring->txq_cnt = 0;
  8618. /* Next issue ABTS for everything on the txcmplq */
  8619. list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
  8620. lpfc_sli_abort_iotag_issue(phba, pring, iocb);
  8621. spin_unlock_irq(&phba->hbalock);
  8622. /* Cancel all the IOCBs from the completions list */
  8623. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  8624. IOERR_SLI_ABORTED);
  8625. }
  8626. /**
  8627. * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
  8628. * @phba: pointer to lpfc HBA data structure.
  8629. *
  8630. * This routine will abort all pending and outstanding iocbs to an HBA.
  8631. **/
  8632. void
  8633. lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
  8634. {
  8635. struct lpfc_sli *psli = &phba->sli;
  8636. struct lpfc_sli_ring *pring;
  8637. int i;
  8638. for (i = 0; i < psli->num_rings; i++) {
  8639. pring = &psli->ring[i];
  8640. lpfc_sli_iocb_ring_abort(phba, pring);
  8641. }
  8642. }
  8643. /**
  8644. * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
  8645. * @iocbq: Pointer to driver iocb object.
  8646. * @vport: Pointer to driver virtual port object.
  8647. * @tgt_id: SCSI ID of the target.
  8648. * @lun_id: LUN ID of the scsi device.
  8649. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
  8650. *
  8651. * This function acts as an iocb filter for functions which abort or count
  8652. * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
  8653. * 0 if the filtering criteria is met for the given iocb and will return
  8654. * 1 if the filtering criteria is not met.
  8655. * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
  8656. * given iocb is for the SCSI device specified by vport, tgt_id and
  8657. * lun_id parameter.
  8658. * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
  8659. * given iocb is for the SCSI target specified by vport and tgt_id
  8660. * parameters.
  8661. * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
  8662. * given iocb is for the SCSI host associated with the given vport.
  8663. * This function is called with no locks held.
  8664. **/
  8665. static int
  8666. lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
  8667. uint16_t tgt_id, uint64_t lun_id,
  8668. lpfc_ctx_cmd ctx_cmd)
  8669. {
  8670. struct lpfc_scsi_buf *lpfc_cmd;
  8671. int rc = 1;
  8672. if (!(iocbq->iocb_flag & LPFC_IO_FCP))
  8673. return rc;
  8674. if (iocbq->vport != vport)
  8675. return rc;
  8676. lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  8677. if (lpfc_cmd->pCmd == NULL)
  8678. return rc;
  8679. switch (ctx_cmd) {
  8680. case LPFC_CTX_LUN:
  8681. if ((lpfc_cmd->rdata->pnode) &&
  8682. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
  8683. (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
  8684. rc = 0;
  8685. break;
  8686. case LPFC_CTX_TGT:
  8687. if ((lpfc_cmd->rdata->pnode) &&
  8688. (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
  8689. rc = 0;
  8690. break;
  8691. case LPFC_CTX_HOST:
  8692. rc = 0;
  8693. break;
  8694. default:
  8695. printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
  8696. __func__, ctx_cmd);
  8697. break;
  8698. }
  8699. return rc;
  8700. }
  8701. /**
  8702. * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
  8703. * @vport: Pointer to virtual port.
  8704. * @tgt_id: SCSI ID of the target.
  8705. * @lun_id: LUN ID of the scsi device.
  8706. * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8707. *
  8708. * This function returns number of FCP commands pending for the vport.
  8709. * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
  8710. * commands pending on the vport associated with SCSI device specified
  8711. * by tgt_id and lun_id parameters.
  8712. * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
  8713. * commands pending on the vport associated with SCSI target specified
  8714. * by tgt_id parameter.
  8715. * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
  8716. * commands pending on the vport.
  8717. * This function returns the number of iocbs which satisfy the filter.
  8718. * This function is called without any lock held.
  8719. **/
  8720. int
  8721. lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
  8722. lpfc_ctx_cmd ctx_cmd)
  8723. {
  8724. struct lpfc_hba *phba = vport->phba;
  8725. struct lpfc_iocbq *iocbq;
  8726. int sum, i;
  8727. for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
  8728. iocbq = phba->sli.iocbq_lookup[i];
  8729. if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
  8730. ctx_cmd) == 0)
  8731. sum++;
  8732. }
  8733. return sum;
  8734. }
  8735. /**
  8736. * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
  8737. * @phba: Pointer to HBA context object
  8738. * @cmdiocb: Pointer to command iocb object.
  8739. * @rspiocb: Pointer to response iocb object.
  8740. *
  8741. * This function is called when an aborted FCP iocb completes. This
  8742. * function is called by the ring event handler with no lock held.
  8743. * This function frees the iocb.
  8744. **/
  8745. void
  8746. lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  8747. struct lpfc_iocbq *rspiocb)
  8748. {
  8749. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8750. "3096 ABORT_XRI_CN completing on xri x%x "
  8751. "original iotag x%x, abort cmd iotag x%x "
  8752. "status 0x%x, reason 0x%x\n",
  8753. cmdiocb->iocb.un.acxri.abortContextTag,
  8754. cmdiocb->iocb.un.acxri.abortIoTag,
  8755. cmdiocb->iotag, rspiocb->iocb.ulpStatus,
  8756. rspiocb->iocb.un.ulpWord[4]);
  8757. lpfc_sli_release_iocbq(phba, cmdiocb);
  8758. return;
  8759. }
  8760. /**
  8761. * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
  8762. * @vport: Pointer to virtual port.
  8763. * @pring: Pointer to driver SLI ring object.
  8764. * @tgt_id: SCSI ID of the target.
  8765. * @lun_id: LUN ID of the scsi device.
  8766. * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
  8767. *
  8768. * This function sends an abort command for every SCSI command
  8769. * associated with the given virtual port pending on the ring
  8770. * filtered by lpfc_sli_validate_fcp_iocb function.
  8771. * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
  8772. * FCP iocbs associated with lun specified by tgt_id and lun_id
  8773. * parameters
  8774. * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
  8775. * FCP iocbs associated with SCSI target specified by tgt_id parameter.
  8776. * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
  8777. * FCP iocbs associated with virtual port.
  8778. * This function returns number of iocbs it failed to abort.
  8779. * This function is called with no locks held.
  8780. **/
  8781. int
  8782. lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
  8783. uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
  8784. {
  8785. struct lpfc_hba *phba = vport->phba;
  8786. struct lpfc_iocbq *iocbq;
  8787. struct lpfc_iocbq *abtsiocb;
  8788. IOCB_t *cmd = NULL;
  8789. int errcnt = 0, ret_val = 0;
  8790. int i;
  8791. for (i = 1; i <= phba->sli.last_iotag; i++) {
  8792. iocbq = phba->sli.iocbq_lookup[i];
  8793. if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
  8794. abort_cmd) != 0)
  8795. continue;
  8796. /* issue ABTS for this IOCB based on iotag */
  8797. abtsiocb = lpfc_sli_get_iocbq(phba);
  8798. if (abtsiocb == NULL) {
  8799. errcnt++;
  8800. continue;
  8801. }
  8802. cmd = &iocbq->iocb;
  8803. abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
  8804. abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
  8805. if (phba->sli_rev == LPFC_SLI_REV4)
  8806. abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
  8807. else
  8808. abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
  8809. abtsiocb->iocb.ulpLe = 1;
  8810. abtsiocb->iocb.ulpClass = cmd->ulpClass;
  8811. abtsiocb->vport = phba->pport;
  8812. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  8813. abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
  8814. if (iocbq->iocb_flag & LPFC_IO_FCP)
  8815. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  8816. if (lpfc_is_link_up(phba))
  8817. abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
  8818. else
  8819. abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
  8820. /* Setup callback routine and issue the command. */
  8821. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  8822. ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
  8823. abtsiocb, 0);
  8824. if (ret_val == IOCB_ERROR) {
  8825. lpfc_sli_release_iocbq(phba, abtsiocb);
  8826. errcnt++;
  8827. continue;
  8828. }
  8829. }
  8830. return errcnt;
  8831. }
  8832. /**
  8833. * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
  8834. * @phba: Pointer to HBA context object.
  8835. * @cmdiocbq: Pointer to command iocb.
  8836. * @rspiocbq: Pointer to response iocb.
  8837. *
  8838. * This function is the completion handler for iocbs issued using
  8839. * lpfc_sli_issue_iocb_wait function. This function is called by the
  8840. * ring event handler function without any lock held. This function
  8841. * can be called from both worker thread context and interrupt
  8842. * context. This function also can be called from other thread which
  8843. * cleans up the SLI layer objects.
  8844. * This function copy the contents of the response iocb to the
  8845. * response iocb memory object provided by the caller of
  8846. * lpfc_sli_issue_iocb_wait and then wakes up the thread which
  8847. * sleeps for the iocb completion.
  8848. **/
  8849. static void
  8850. lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
  8851. struct lpfc_iocbq *cmdiocbq,
  8852. struct lpfc_iocbq *rspiocbq)
  8853. {
  8854. wait_queue_head_t *pdone_q;
  8855. unsigned long iflags;
  8856. struct lpfc_scsi_buf *lpfc_cmd;
  8857. spin_lock_irqsave(&phba->hbalock, iflags);
  8858. cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
  8859. if (cmdiocbq->context2 && rspiocbq)
  8860. memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
  8861. &rspiocbq->iocb, sizeof(IOCB_t));
  8862. /* Set the exchange busy flag for task management commands */
  8863. if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
  8864. !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
  8865. lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
  8866. cur_iocbq);
  8867. lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
  8868. }
  8869. pdone_q = cmdiocbq->context_un.wait_queue;
  8870. if (pdone_q)
  8871. wake_up(pdone_q);
  8872. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8873. return;
  8874. }
  8875. /**
  8876. * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
  8877. * @phba: Pointer to HBA context object..
  8878. * @piocbq: Pointer to command iocb.
  8879. * @flag: Flag to test.
  8880. *
  8881. * This routine grabs the hbalock and then test the iocb_flag to
  8882. * see if the passed in flag is set.
  8883. * Returns:
  8884. * 1 if flag is set.
  8885. * 0 if flag is not set.
  8886. **/
  8887. static int
  8888. lpfc_chk_iocb_flg(struct lpfc_hba *phba,
  8889. struct lpfc_iocbq *piocbq, uint32_t flag)
  8890. {
  8891. unsigned long iflags;
  8892. int ret;
  8893. spin_lock_irqsave(&phba->hbalock, iflags);
  8894. ret = piocbq->iocb_flag & flag;
  8895. spin_unlock_irqrestore(&phba->hbalock, iflags);
  8896. return ret;
  8897. }
  8898. /**
  8899. * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
  8900. * @phba: Pointer to HBA context object..
  8901. * @pring: Pointer to sli ring.
  8902. * @piocb: Pointer to command iocb.
  8903. * @prspiocbq: Pointer to response iocb.
  8904. * @timeout: Timeout in number of seconds.
  8905. *
  8906. * This function issues the iocb to firmware and waits for the
  8907. * iocb to complete. If the iocb command is not
  8908. * completed within timeout seconds, it returns IOCB_TIMEDOUT.
  8909. * Caller should not free the iocb resources if this function
  8910. * returns IOCB_TIMEDOUT.
  8911. * The function waits for the iocb completion using an
  8912. * non-interruptible wait.
  8913. * This function will sleep while waiting for iocb completion.
  8914. * So, this function should not be called from any context which
  8915. * does not allow sleeping. Due to the same reason, this function
  8916. * cannot be called with interrupt disabled.
  8917. * This function assumes that the iocb completions occur while
  8918. * this function sleep. So, this function cannot be called from
  8919. * the thread which process iocb completion for this ring.
  8920. * This function clears the iocb_flag of the iocb object before
  8921. * issuing the iocb and the iocb completion handler sets this
  8922. * flag and wakes this thread when the iocb completes.
  8923. * The contents of the response iocb will be copied to prspiocbq
  8924. * by the completion handler when the command completes.
  8925. * This function returns IOCB_SUCCESS when success.
  8926. * This function is called with no lock held.
  8927. **/
  8928. int
  8929. lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
  8930. uint32_t ring_number,
  8931. struct lpfc_iocbq *piocb,
  8932. struct lpfc_iocbq *prspiocbq,
  8933. uint32_t timeout)
  8934. {
  8935. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  8936. long timeleft, timeout_req = 0;
  8937. int retval = IOCB_SUCCESS;
  8938. uint32_t creg_val;
  8939. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  8940. /*
  8941. * If the caller has provided a response iocbq buffer, then context2
  8942. * is NULL or its an error.
  8943. */
  8944. if (prspiocbq) {
  8945. if (piocb->context2)
  8946. return IOCB_ERROR;
  8947. piocb->context2 = prspiocbq;
  8948. }
  8949. piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
  8950. piocb->context_un.wait_queue = &done_q;
  8951. piocb->iocb_flag &= ~LPFC_IO_WAKE;
  8952. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8953. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8954. return IOCB_ERROR;
  8955. creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
  8956. writel(creg_val, phba->HCregaddr);
  8957. readl(phba->HCregaddr); /* flush */
  8958. }
  8959. retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
  8960. SLI_IOCB_RET_IOCB);
  8961. if (retval == IOCB_SUCCESS) {
  8962. timeout_req = timeout * HZ;
  8963. timeleft = wait_event_timeout(done_q,
  8964. lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
  8965. timeout_req);
  8966. if (piocb->iocb_flag & LPFC_IO_WAKE) {
  8967. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8968. "0331 IOCB wake signaled\n");
  8969. } else if (timeleft == 0) {
  8970. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8971. "0338 IOCB wait timeout error - no "
  8972. "wake response Data x%x\n", timeout);
  8973. retval = IOCB_TIMEDOUT;
  8974. } else {
  8975. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  8976. "0330 IOCB wake NOT set, "
  8977. "Data x%x x%lx\n",
  8978. timeout, (timeleft / jiffies));
  8979. retval = IOCB_TIMEDOUT;
  8980. }
  8981. } else if (retval == IOCB_BUSY) {
  8982. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8983. "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
  8984. phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
  8985. return retval;
  8986. } else {
  8987. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  8988. "0332 IOCB wait issue failed, Data x%x\n",
  8989. retval);
  8990. retval = IOCB_ERROR;
  8991. }
  8992. if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
  8993. if (lpfc_readl(phba->HCregaddr, &creg_val))
  8994. return IOCB_ERROR;
  8995. creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
  8996. writel(creg_val, phba->HCregaddr);
  8997. readl(phba->HCregaddr); /* flush */
  8998. }
  8999. if (prspiocbq)
  9000. piocb->context2 = NULL;
  9001. piocb->context_un.wait_queue = NULL;
  9002. piocb->iocb_cmpl = NULL;
  9003. return retval;
  9004. }
  9005. /**
  9006. * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
  9007. * @phba: Pointer to HBA context object.
  9008. * @pmboxq: Pointer to driver mailbox object.
  9009. * @timeout: Timeout in number of seconds.
  9010. *
  9011. * This function issues the mailbox to firmware and waits for the
  9012. * mailbox command to complete. If the mailbox command is not
  9013. * completed within timeout seconds, it returns MBX_TIMEOUT.
  9014. * The function waits for the mailbox completion using an
  9015. * interruptible wait. If the thread is woken up due to a
  9016. * signal, MBX_TIMEOUT error is returned to the caller. Caller
  9017. * should not free the mailbox resources, if this function returns
  9018. * MBX_TIMEOUT.
  9019. * This function will sleep while waiting for mailbox completion.
  9020. * So, this function should not be called from any context which
  9021. * does not allow sleeping. Due to the same reason, this function
  9022. * cannot be called with interrupt disabled.
  9023. * This function assumes that the mailbox completion occurs while
  9024. * this function sleep. So, this function cannot be called from
  9025. * the worker thread which processes mailbox completion.
  9026. * This function is called in the context of HBA management
  9027. * applications.
  9028. * This function returns MBX_SUCCESS when successful.
  9029. * This function is called with no lock held.
  9030. **/
  9031. int
  9032. lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
  9033. uint32_t timeout)
  9034. {
  9035. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
  9036. int retval;
  9037. unsigned long flag;
  9038. /* The caller must leave context1 empty. */
  9039. if (pmboxq->context1)
  9040. return MBX_NOT_FINISHED;
  9041. pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
  9042. /* setup wake call as IOCB callback */
  9043. pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
  9044. /* setup context field to pass wait_queue pointer to wake function */
  9045. pmboxq->context1 = &done_q;
  9046. /* now issue the command */
  9047. retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
  9048. if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
  9049. wait_event_interruptible_timeout(done_q,
  9050. pmboxq->mbox_flag & LPFC_MBX_WAKE,
  9051. timeout * HZ);
  9052. spin_lock_irqsave(&phba->hbalock, flag);
  9053. pmboxq->context1 = NULL;
  9054. /*
  9055. * if LPFC_MBX_WAKE flag is set the mailbox is completed
  9056. * else do not free the resources.
  9057. */
  9058. if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
  9059. retval = MBX_SUCCESS;
  9060. lpfc_sli4_swap_str(phba, pmboxq);
  9061. } else {
  9062. retval = MBX_TIMEOUT;
  9063. pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  9064. }
  9065. spin_unlock_irqrestore(&phba->hbalock, flag);
  9066. }
  9067. return retval;
  9068. }
  9069. /**
  9070. * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
  9071. * @phba: Pointer to HBA context.
  9072. *
  9073. * This function is called to shutdown the driver's mailbox sub-system.
  9074. * It first marks the mailbox sub-system is in a block state to prevent
  9075. * the asynchronous mailbox command from issued off the pending mailbox
  9076. * command queue. If the mailbox command sub-system shutdown is due to
  9077. * HBA error conditions such as EEH or ERATT, this routine shall invoke
  9078. * the mailbox sub-system flush routine to forcefully bring down the
  9079. * mailbox sub-system. Otherwise, if it is due to normal condition (such
  9080. * as with offline or HBA function reset), this routine will wait for the
  9081. * outstanding mailbox command to complete before invoking the mailbox
  9082. * sub-system flush routine to gracefully bring down mailbox sub-system.
  9083. **/
  9084. void
  9085. lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
  9086. {
  9087. struct lpfc_sli *psli = &phba->sli;
  9088. unsigned long timeout;
  9089. timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
  9090. spin_lock_irq(&phba->hbalock);
  9091. psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
  9092. if (psli->sli_flag & LPFC_SLI_ACTIVE) {
  9093. /* Determine how long we might wait for the active mailbox
  9094. * command to be gracefully completed by firmware.
  9095. */
  9096. if (phba->sli.mbox_active)
  9097. timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
  9098. phba->sli.mbox_active) *
  9099. 1000) + jiffies;
  9100. spin_unlock_irq(&phba->hbalock);
  9101. while (phba->sli.mbox_active) {
  9102. /* Check active mailbox complete status every 2ms */
  9103. msleep(2);
  9104. if (time_after(jiffies, timeout))
  9105. /* Timeout, let the mailbox flush routine to
  9106. * forcefully release active mailbox command
  9107. */
  9108. break;
  9109. }
  9110. } else
  9111. spin_unlock_irq(&phba->hbalock);
  9112. lpfc_sli_mbox_sys_flush(phba);
  9113. }
  9114. /**
  9115. * lpfc_sli_eratt_read - read sli-3 error attention events
  9116. * @phba: Pointer to HBA context.
  9117. *
  9118. * This function is called to read the SLI3 device error attention registers
  9119. * for possible error attention events. The caller must hold the hostlock
  9120. * with spin_lock_irq().
  9121. *
  9122. * This function returns 1 when there is Error Attention in the Host Attention
  9123. * Register and returns 0 otherwise.
  9124. **/
  9125. static int
  9126. lpfc_sli_eratt_read(struct lpfc_hba *phba)
  9127. {
  9128. uint32_t ha_copy;
  9129. /* Read chip Host Attention (HA) register */
  9130. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9131. goto unplug_err;
  9132. if (ha_copy & HA_ERATT) {
  9133. /* Read host status register to retrieve error event */
  9134. if (lpfc_sli_read_hs(phba))
  9135. goto unplug_err;
  9136. /* Check if there is a deferred error condition is active */
  9137. if ((HS_FFER1 & phba->work_hs) &&
  9138. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9139. HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
  9140. phba->hba_flag |= DEFER_ERATT;
  9141. /* Clear all interrupt enable conditions */
  9142. writel(0, phba->HCregaddr);
  9143. readl(phba->HCregaddr);
  9144. }
  9145. /* Set the driver HA work bitmap */
  9146. phba->work_ha |= HA_ERATT;
  9147. /* Indicate polling handles this ERATT */
  9148. phba->hba_flag |= HBA_ERATT_HANDLED;
  9149. return 1;
  9150. }
  9151. return 0;
  9152. unplug_err:
  9153. /* Set the driver HS work bitmap */
  9154. phba->work_hs |= UNPLUG_ERR;
  9155. /* Set the driver HA work bitmap */
  9156. phba->work_ha |= HA_ERATT;
  9157. /* Indicate polling handles this ERATT */
  9158. phba->hba_flag |= HBA_ERATT_HANDLED;
  9159. return 1;
  9160. }
  9161. /**
  9162. * lpfc_sli4_eratt_read - read sli-4 error attention events
  9163. * @phba: Pointer to HBA context.
  9164. *
  9165. * This function is called to read the SLI4 device error attention registers
  9166. * for possible error attention events. The caller must hold the hostlock
  9167. * with spin_lock_irq().
  9168. *
  9169. * This function returns 1 when there is Error Attention in the Host Attention
  9170. * Register and returns 0 otherwise.
  9171. **/
  9172. static int
  9173. lpfc_sli4_eratt_read(struct lpfc_hba *phba)
  9174. {
  9175. uint32_t uerr_sta_hi, uerr_sta_lo;
  9176. uint32_t if_type, portsmphr;
  9177. struct lpfc_register portstat_reg;
  9178. /*
  9179. * For now, use the SLI4 device internal unrecoverable error
  9180. * registers for error attention. This can be changed later.
  9181. */
  9182. if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
  9183. switch (if_type) {
  9184. case LPFC_SLI_INTF_IF_TYPE_0:
  9185. if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
  9186. &uerr_sta_lo) ||
  9187. lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
  9188. &uerr_sta_hi)) {
  9189. phba->work_hs |= UNPLUG_ERR;
  9190. phba->work_ha |= HA_ERATT;
  9191. phba->hba_flag |= HBA_ERATT_HANDLED;
  9192. return 1;
  9193. }
  9194. if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
  9195. (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
  9196. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9197. "1423 HBA Unrecoverable error: "
  9198. "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
  9199. "ue_mask_lo_reg=0x%x, "
  9200. "ue_mask_hi_reg=0x%x\n",
  9201. uerr_sta_lo, uerr_sta_hi,
  9202. phba->sli4_hba.ue_mask_lo,
  9203. phba->sli4_hba.ue_mask_hi);
  9204. phba->work_status[0] = uerr_sta_lo;
  9205. phba->work_status[1] = uerr_sta_hi;
  9206. phba->work_ha |= HA_ERATT;
  9207. phba->hba_flag |= HBA_ERATT_HANDLED;
  9208. return 1;
  9209. }
  9210. break;
  9211. case LPFC_SLI_INTF_IF_TYPE_2:
  9212. if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
  9213. &portstat_reg.word0) ||
  9214. lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
  9215. &portsmphr)){
  9216. phba->work_hs |= UNPLUG_ERR;
  9217. phba->work_ha |= HA_ERATT;
  9218. phba->hba_flag |= HBA_ERATT_HANDLED;
  9219. return 1;
  9220. }
  9221. if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
  9222. phba->work_status[0] =
  9223. readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
  9224. phba->work_status[1] =
  9225. readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
  9226. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9227. "2885 Port Status Event: "
  9228. "port status reg 0x%x, "
  9229. "port smphr reg 0x%x, "
  9230. "error 1=0x%x, error 2=0x%x\n",
  9231. portstat_reg.word0,
  9232. portsmphr,
  9233. phba->work_status[0],
  9234. phba->work_status[1]);
  9235. phba->work_ha |= HA_ERATT;
  9236. phba->hba_flag |= HBA_ERATT_HANDLED;
  9237. return 1;
  9238. }
  9239. break;
  9240. case LPFC_SLI_INTF_IF_TYPE_1:
  9241. default:
  9242. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9243. "2886 HBA Error Attention on unsupported "
  9244. "if type %d.", if_type);
  9245. return 1;
  9246. }
  9247. return 0;
  9248. }
  9249. /**
  9250. * lpfc_sli_check_eratt - check error attention events
  9251. * @phba: Pointer to HBA context.
  9252. *
  9253. * This function is called from timer soft interrupt context to check HBA's
  9254. * error attention register bit for error attention events.
  9255. *
  9256. * This function returns 1 when there is Error Attention in the Host Attention
  9257. * Register and returns 0 otherwise.
  9258. **/
  9259. int
  9260. lpfc_sli_check_eratt(struct lpfc_hba *phba)
  9261. {
  9262. uint32_t ha_copy;
  9263. /* If somebody is waiting to handle an eratt, don't process it
  9264. * here. The brdkill function will do this.
  9265. */
  9266. if (phba->link_flag & LS_IGNORE_ERATT)
  9267. return 0;
  9268. /* Check if interrupt handler handles this ERATT */
  9269. spin_lock_irq(&phba->hbalock);
  9270. if (phba->hba_flag & HBA_ERATT_HANDLED) {
  9271. /* Interrupt handler has handled ERATT */
  9272. spin_unlock_irq(&phba->hbalock);
  9273. return 0;
  9274. }
  9275. /*
  9276. * If there is deferred error attention, do not check for error
  9277. * attention
  9278. */
  9279. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9280. spin_unlock_irq(&phba->hbalock);
  9281. return 0;
  9282. }
  9283. /* If PCI channel is offline, don't process it */
  9284. if (unlikely(pci_channel_offline(phba->pcidev))) {
  9285. spin_unlock_irq(&phba->hbalock);
  9286. return 0;
  9287. }
  9288. switch (phba->sli_rev) {
  9289. case LPFC_SLI_REV2:
  9290. case LPFC_SLI_REV3:
  9291. /* Read chip Host Attention (HA) register */
  9292. ha_copy = lpfc_sli_eratt_read(phba);
  9293. break;
  9294. case LPFC_SLI_REV4:
  9295. /* Read device Uncoverable Error (UERR) registers */
  9296. ha_copy = lpfc_sli4_eratt_read(phba);
  9297. break;
  9298. default:
  9299. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  9300. "0299 Invalid SLI revision (%d)\n",
  9301. phba->sli_rev);
  9302. ha_copy = 0;
  9303. break;
  9304. }
  9305. spin_unlock_irq(&phba->hbalock);
  9306. return ha_copy;
  9307. }
  9308. /**
  9309. * lpfc_intr_state_check - Check device state for interrupt handling
  9310. * @phba: Pointer to HBA context.
  9311. *
  9312. * This inline routine checks whether a device or its PCI slot is in a state
  9313. * that the interrupt should be handled.
  9314. *
  9315. * This function returns 0 if the device or the PCI slot is in a state that
  9316. * interrupt should be handled, otherwise -EIO.
  9317. */
  9318. static inline int
  9319. lpfc_intr_state_check(struct lpfc_hba *phba)
  9320. {
  9321. /* If the pci channel is offline, ignore all the interrupts */
  9322. if (unlikely(pci_channel_offline(phba->pcidev)))
  9323. return -EIO;
  9324. /* Update device level interrupt statistics */
  9325. phba->sli.slistat.sli_intr++;
  9326. /* Ignore all interrupts during initialization. */
  9327. if (unlikely(phba->link_state < LPFC_LINK_DOWN))
  9328. return -EIO;
  9329. return 0;
  9330. }
  9331. /**
  9332. * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
  9333. * @irq: Interrupt number.
  9334. * @dev_id: The device context pointer.
  9335. *
  9336. * This function is directly called from the PCI layer as an interrupt
  9337. * service routine when device with SLI-3 interface spec is enabled with
  9338. * MSI-X multi-message interrupt mode and there are slow-path events in
  9339. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  9340. * interrupt mode, this function is called as part of the device-level
  9341. * interrupt handler. When the PCI slot is in error recovery or the HBA
  9342. * is undergoing initialization, the interrupt handler will not process
  9343. * the interrupt. The link attention and ELS ring attention events are
  9344. * handled by the worker thread. The interrupt handler signals the worker
  9345. * thread and returns for these events. This function is called without
  9346. * any lock held. It gets the hbalock to access and update SLI data
  9347. * structures.
  9348. *
  9349. * This function returns IRQ_HANDLED when interrupt is handled else it
  9350. * returns IRQ_NONE.
  9351. **/
  9352. irqreturn_t
  9353. lpfc_sli_sp_intr_handler(int irq, void *dev_id)
  9354. {
  9355. struct lpfc_hba *phba;
  9356. uint32_t ha_copy, hc_copy;
  9357. uint32_t work_ha_copy;
  9358. unsigned long status;
  9359. unsigned long iflag;
  9360. uint32_t control;
  9361. MAILBOX_t *mbox, *pmbox;
  9362. struct lpfc_vport *vport;
  9363. struct lpfc_nodelist *ndlp;
  9364. struct lpfc_dmabuf *mp;
  9365. LPFC_MBOXQ_t *pmb;
  9366. int rc;
  9367. /*
  9368. * Get the driver's phba structure from the dev_id and
  9369. * assume the HBA is not interrupting.
  9370. */
  9371. phba = (struct lpfc_hba *)dev_id;
  9372. if (unlikely(!phba))
  9373. return IRQ_NONE;
  9374. /*
  9375. * Stuff needs to be attented to when this function is invoked as an
  9376. * individual interrupt handler in MSI-X multi-message interrupt mode
  9377. */
  9378. if (phba->intr_type == MSIX) {
  9379. /* Check device state for handling interrupt */
  9380. if (lpfc_intr_state_check(phba))
  9381. return IRQ_NONE;
  9382. /* Need to read HA REG for slow-path events */
  9383. spin_lock_irqsave(&phba->hbalock, iflag);
  9384. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9385. goto unplug_error;
  9386. /* If somebody is waiting to handle an eratt don't process it
  9387. * here. The brdkill function will do this.
  9388. */
  9389. if (phba->link_flag & LS_IGNORE_ERATT)
  9390. ha_copy &= ~HA_ERATT;
  9391. /* Check the need for handling ERATT in interrupt handler */
  9392. if (ha_copy & HA_ERATT) {
  9393. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9394. /* ERATT polling has handled ERATT */
  9395. ha_copy &= ~HA_ERATT;
  9396. else
  9397. /* Indicate interrupt handler handles ERATT */
  9398. phba->hba_flag |= HBA_ERATT_HANDLED;
  9399. }
  9400. /*
  9401. * If there is deferred error attention, do not check for any
  9402. * interrupt.
  9403. */
  9404. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9405. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9406. return IRQ_NONE;
  9407. }
  9408. /* Clear up only attention source related to slow-path */
  9409. if (lpfc_readl(phba->HCregaddr, &hc_copy))
  9410. goto unplug_error;
  9411. writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
  9412. HC_LAINT_ENA | HC_ERINT_ENA),
  9413. phba->HCregaddr);
  9414. writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
  9415. phba->HAregaddr);
  9416. writel(hc_copy, phba->HCregaddr);
  9417. readl(phba->HAregaddr); /* flush */
  9418. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9419. } else
  9420. ha_copy = phba->ha_copy;
  9421. work_ha_copy = ha_copy & phba->work_ha_mask;
  9422. if (work_ha_copy) {
  9423. if (work_ha_copy & HA_LATT) {
  9424. if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
  9425. /*
  9426. * Turn off Link Attention interrupts
  9427. * until CLEAR_LA done
  9428. */
  9429. spin_lock_irqsave(&phba->hbalock, iflag);
  9430. phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
  9431. if (lpfc_readl(phba->HCregaddr, &control))
  9432. goto unplug_error;
  9433. control &= ~HC_LAINT_ENA;
  9434. writel(control, phba->HCregaddr);
  9435. readl(phba->HCregaddr); /* flush */
  9436. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9437. }
  9438. else
  9439. work_ha_copy &= ~HA_LATT;
  9440. }
  9441. if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
  9442. /*
  9443. * Turn off Slow Rings interrupts, LPFC_ELS_RING is
  9444. * the only slow ring.
  9445. */
  9446. status = (work_ha_copy &
  9447. (HA_RXMASK << (4*LPFC_ELS_RING)));
  9448. status >>= (4*LPFC_ELS_RING);
  9449. if (status & HA_RXMASK) {
  9450. spin_lock_irqsave(&phba->hbalock, iflag);
  9451. if (lpfc_readl(phba->HCregaddr, &control))
  9452. goto unplug_error;
  9453. lpfc_debugfs_slow_ring_trc(phba,
  9454. "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
  9455. control, status,
  9456. (uint32_t)phba->sli.slistat.sli_intr);
  9457. if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
  9458. lpfc_debugfs_slow_ring_trc(phba,
  9459. "ISR Disable ring:"
  9460. "pwork:x%x hawork:x%x wait:x%x",
  9461. phba->work_ha, work_ha_copy,
  9462. (uint32_t)((unsigned long)
  9463. &phba->work_waitq));
  9464. control &=
  9465. ~(HC_R0INT_ENA << LPFC_ELS_RING);
  9466. writel(control, phba->HCregaddr);
  9467. readl(phba->HCregaddr); /* flush */
  9468. }
  9469. else {
  9470. lpfc_debugfs_slow_ring_trc(phba,
  9471. "ISR slow ring: pwork:"
  9472. "x%x hawork:x%x wait:x%x",
  9473. phba->work_ha, work_ha_copy,
  9474. (uint32_t)((unsigned long)
  9475. &phba->work_waitq));
  9476. }
  9477. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9478. }
  9479. }
  9480. spin_lock_irqsave(&phba->hbalock, iflag);
  9481. if (work_ha_copy & HA_ERATT) {
  9482. if (lpfc_sli_read_hs(phba))
  9483. goto unplug_error;
  9484. /*
  9485. * Check if there is a deferred error condition
  9486. * is active
  9487. */
  9488. if ((HS_FFER1 & phba->work_hs) &&
  9489. ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
  9490. HS_FFER6 | HS_FFER7 | HS_FFER8) &
  9491. phba->work_hs)) {
  9492. phba->hba_flag |= DEFER_ERATT;
  9493. /* Clear all interrupt enable conditions */
  9494. writel(0, phba->HCregaddr);
  9495. readl(phba->HCregaddr);
  9496. }
  9497. }
  9498. if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
  9499. pmb = phba->sli.mbox_active;
  9500. pmbox = &pmb->u.mb;
  9501. mbox = phba->mbox;
  9502. vport = pmb->vport;
  9503. /* First check out the status word */
  9504. lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
  9505. if (pmbox->mbxOwner != OWN_HOST) {
  9506. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9507. /*
  9508. * Stray Mailbox Interrupt, mbxCommand <cmd>
  9509. * mbxStatus <status>
  9510. */
  9511. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9512. LOG_SLI,
  9513. "(%d):0304 Stray Mailbox "
  9514. "Interrupt mbxCommand x%x "
  9515. "mbxStatus x%x\n",
  9516. (vport ? vport->vpi : 0),
  9517. pmbox->mbxCommand,
  9518. pmbox->mbxStatus);
  9519. /* clear mailbox attention bit */
  9520. work_ha_copy &= ~HA_MBATT;
  9521. } else {
  9522. phba->sli.mbox_active = NULL;
  9523. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9524. phba->last_completion_time = jiffies;
  9525. del_timer(&phba->sli.mbox_tmo);
  9526. if (pmb->mbox_cmpl) {
  9527. lpfc_sli_pcimem_bcopy(mbox, pmbox,
  9528. MAILBOX_CMD_SIZE);
  9529. if (pmb->out_ext_byte_len &&
  9530. pmb->context2)
  9531. lpfc_sli_pcimem_bcopy(
  9532. phba->mbox_ext,
  9533. pmb->context2,
  9534. pmb->out_ext_byte_len);
  9535. }
  9536. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  9537. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  9538. lpfc_debugfs_disc_trc(vport,
  9539. LPFC_DISC_TRC_MBOX_VPORT,
  9540. "MBOX dflt rpi: : "
  9541. "status:x%x rpi:x%x",
  9542. (uint32_t)pmbox->mbxStatus,
  9543. pmbox->un.varWords[0], 0);
  9544. if (!pmbox->mbxStatus) {
  9545. mp = (struct lpfc_dmabuf *)
  9546. (pmb->context1);
  9547. ndlp = (struct lpfc_nodelist *)
  9548. pmb->context2;
  9549. /* Reg_LOGIN of dflt RPI was
  9550. * successful. new lets get
  9551. * rid of the RPI using the
  9552. * same mbox buffer.
  9553. */
  9554. lpfc_unreg_login(phba,
  9555. vport->vpi,
  9556. pmbox->un.varWords[0],
  9557. pmb);
  9558. pmb->mbox_cmpl =
  9559. lpfc_mbx_cmpl_dflt_rpi;
  9560. pmb->context1 = mp;
  9561. pmb->context2 = ndlp;
  9562. pmb->vport = vport;
  9563. rc = lpfc_sli_issue_mbox(phba,
  9564. pmb,
  9565. MBX_NOWAIT);
  9566. if (rc != MBX_BUSY)
  9567. lpfc_printf_log(phba,
  9568. KERN_ERR,
  9569. LOG_MBOX | LOG_SLI,
  9570. "0350 rc should have"
  9571. "been MBX_BUSY\n");
  9572. if (rc != MBX_NOT_FINISHED)
  9573. goto send_current_mbox;
  9574. }
  9575. }
  9576. spin_lock_irqsave(
  9577. &phba->pport->work_port_lock,
  9578. iflag);
  9579. phba->pport->work_port_events &=
  9580. ~WORKER_MBOX_TMO;
  9581. spin_unlock_irqrestore(
  9582. &phba->pport->work_port_lock,
  9583. iflag);
  9584. lpfc_mbox_cmpl_put(phba, pmb);
  9585. }
  9586. } else
  9587. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9588. if ((work_ha_copy & HA_MBATT) &&
  9589. (phba->sli.mbox_active == NULL)) {
  9590. send_current_mbox:
  9591. /* Process next mailbox command if there is one */
  9592. do {
  9593. rc = lpfc_sli_issue_mbox(phba, NULL,
  9594. MBX_NOWAIT);
  9595. } while (rc == MBX_NOT_FINISHED);
  9596. if (rc != MBX_SUCCESS)
  9597. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  9598. LOG_SLI, "0349 rc should be "
  9599. "MBX_SUCCESS\n");
  9600. }
  9601. spin_lock_irqsave(&phba->hbalock, iflag);
  9602. phba->work_ha |= work_ha_copy;
  9603. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9604. lpfc_worker_wake_up(phba);
  9605. }
  9606. return IRQ_HANDLED;
  9607. unplug_error:
  9608. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9609. return IRQ_HANDLED;
  9610. } /* lpfc_sli_sp_intr_handler */
  9611. /**
  9612. * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
  9613. * @irq: Interrupt number.
  9614. * @dev_id: The device context pointer.
  9615. *
  9616. * This function is directly called from the PCI layer as an interrupt
  9617. * service routine when device with SLI-3 interface spec is enabled with
  9618. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  9619. * ring event in the HBA. However, when the device is enabled with either
  9620. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  9621. * device-level interrupt handler. When the PCI slot is in error recovery
  9622. * or the HBA is undergoing initialization, the interrupt handler will not
  9623. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  9624. * the intrrupt context. This function is called without any lock held.
  9625. * It gets the hbalock to access and update SLI data structures.
  9626. *
  9627. * This function returns IRQ_HANDLED when interrupt is handled else it
  9628. * returns IRQ_NONE.
  9629. **/
  9630. irqreturn_t
  9631. lpfc_sli_fp_intr_handler(int irq, void *dev_id)
  9632. {
  9633. struct lpfc_hba *phba;
  9634. uint32_t ha_copy;
  9635. unsigned long status;
  9636. unsigned long iflag;
  9637. /* Get the driver's phba structure from the dev_id and
  9638. * assume the HBA is not interrupting.
  9639. */
  9640. phba = (struct lpfc_hba *) dev_id;
  9641. if (unlikely(!phba))
  9642. return IRQ_NONE;
  9643. /*
  9644. * Stuff needs to be attented to when this function is invoked as an
  9645. * individual interrupt handler in MSI-X multi-message interrupt mode
  9646. */
  9647. if (phba->intr_type == MSIX) {
  9648. /* Check device state for handling interrupt */
  9649. if (lpfc_intr_state_check(phba))
  9650. return IRQ_NONE;
  9651. /* Need to read HA REG for FCP ring and other ring events */
  9652. if (lpfc_readl(phba->HAregaddr, &ha_copy))
  9653. return IRQ_HANDLED;
  9654. /* Clear up only attention source related to fast-path */
  9655. spin_lock_irqsave(&phba->hbalock, iflag);
  9656. /*
  9657. * If there is deferred error attention, do not check for
  9658. * any interrupt.
  9659. */
  9660. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9661. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9662. return IRQ_NONE;
  9663. }
  9664. writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
  9665. phba->HAregaddr);
  9666. readl(phba->HAregaddr); /* flush */
  9667. spin_unlock_irqrestore(&phba->hbalock, iflag);
  9668. } else
  9669. ha_copy = phba->ha_copy;
  9670. /*
  9671. * Process all events on FCP ring. Take the optimized path for FCP IO.
  9672. */
  9673. ha_copy &= ~(phba->work_ha_mask);
  9674. status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9675. status >>= (4*LPFC_FCP_RING);
  9676. if (status & HA_RXMASK)
  9677. lpfc_sli_handle_fast_ring_event(phba,
  9678. &phba->sli.ring[LPFC_FCP_RING],
  9679. status);
  9680. if (phba->cfg_multi_ring_support == 2) {
  9681. /*
  9682. * Process all events on extra ring. Take the optimized path
  9683. * for extra ring IO.
  9684. */
  9685. status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9686. status >>= (4*LPFC_EXTRA_RING);
  9687. if (status & HA_RXMASK) {
  9688. lpfc_sli_handle_fast_ring_event(phba,
  9689. &phba->sli.ring[LPFC_EXTRA_RING],
  9690. status);
  9691. }
  9692. }
  9693. return IRQ_HANDLED;
  9694. } /* lpfc_sli_fp_intr_handler */
  9695. /**
  9696. * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
  9697. * @irq: Interrupt number.
  9698. * @dev_id: The device context pointer.
  9699. *
  9700. * This function is the HBA device-level interrupt handler to device with
  9701. * SLI-3 interface spec, called from the PCI layer when either MSI or
  9702. * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
  9703. * requires driver attention. This function invokes the slow-path interrupt
  9704. * attention handling function and fast-path interrupt attention handling
  9705. * function in turn to process the relevant HBA attention events. This
  9706. * function is called without any lock held. It gets the hbalock to access
  9707. * and update SLI data structures.
  9708. *
  9709. * This function returns IRQ_HANDLED when interrupt is handled, else it
  9710. * returns IRQ_NONE.
  9711. **/
  9712. irqreturn_t
  9713. lpfc_sli_intr_handler(int irq, void *dev_id)
  9714. {
  9715. struct lpfc_hba *phba;
  9716. irqreturn_t sp_irq_rc, fp_irq_rc;
  9717. unsigned long status1, status2;
  9718. uint32_t hc_copy;
  9719. /*
  9720. * Get the driver's phba structure from the dev_id and
  9721. * assume the HBA is not interrupting.
  9722. */
  9723. phba = (struct lpfc_hba *) dev_id;
  9724. if (unlikely(!phba))
  9725. return IRQ_NONE;
  9726. /* Check device state for handling interrupt */
  9727. if (lpfc_intr_state_check(phba))
  9728. return IRQ_NONE;
  9729. spin_lock(&phba->hbalock);
  9730. if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
  9731. spin_unlock(&phba->hbalock);
  9732. return IRQ_HANDLED;
  9733. }
  9734. if (unlikely(!phba->ha_copy)) {
  9735. spin_unlock(&phba->hbalock);
  9736. return IRQ_NONE;
  9737. } else if (phba->ha_copy & HA_ERATT) {
  9738. if (phba->hba_flag & HBA_ERATT_HANDLED)
  9739. /* ERATT polling has handled ERATT */
  9740. phba->ha_copy &= ~HA_ERATT;
  9741. else
  9742. /* Indicate interrupt handler handles ERATT */
  9743. phba->hba_flag |= HBA_ERATT_HANDLED;
  9744. }
  9745. /*
  9746. * If there is deferred error attention, do not check for any interrupt.
  9747. */
  9748. if (unlikely(phba->hba_flag & DEFER_ERATT)) {
  9749. spin_unlock(&phba->hbalock);
  9750. return IRQ_NONE;
  9751. }
  9752. /* Clear attention sources except link and error attentions */
  9753. if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
  9754. spin_unlock(&phba->hbalock);
  9755. return IRQ_HANDLED;
  9756. }
  9757. writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
  9758. | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
  9759. phba->HCregaddr);
  9760. writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
  9761. writel(hc_copy, phba->HCregaddr);
  9762. readl(phba->HAregaddr); /* flush */
  9763. spin_unlock(&phba->hbalock);
  9764. /*
  9765. * Invokes slow-path host attention interrupt handling as appropriate.
  9766. */
  9767. /* status of events with mailbox and link attention */
  9768. status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
  9769. /* status of events with ELS ring */
  9770. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
  9771. status2 >>= (4*LPFC_ELS_RING);
  9772. if (status1 || (status2 & HA_RXMASK))
  9773. sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
  9774. else
  9775. sp_irq_rc = IRQ_NONE;
  9776. /*
  9777. * Invoke fast-path host attention interrupt handling as appropriate.
  9778. */
  9779. /* status of events with FCP ring */
  9780. status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
  9781. status1 >>= (4*LPFC_FCP_RING);
  9782. /* status of events with extra ring */
  9783. if (phba->cfg_multi_ring_support == 2) {
  9784. status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
  9785. status2 >>= (4*LPFC_EXTRA_RING);
  9786. } else
  9787. status2 = 0;
  9788. if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
  9789. fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
  9790. else
  9791. fp_irq_rc = IRQ_NONE;
  9792. /* Return device-level interrupt handling status */
  9793. return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
  9794. } /* lpfc_sli_intr_handler */
  9795. /**
  9796. * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
  9797. * @phba: pointer to lpfc hba data structure.
  9798. *
  9799. * This routine is invoked by the worker thread to process all the pending
  9800. * SLI4 FCP abort XRI events.
  9801. **/
  9802. void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
  9803. {
  9804. struct lpfc_cq_event *cq_event;
  9805. /* First, declare the fcp xri abort event has been handled */
  9806. spin_lock_irq(&phba->hbalock);
  9807. phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
  9808. spin_unlock_irq(&phba->hbalock);
  9809. /* Now, handle all the fcp xri abort events */
  9810. while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
  9811. /* Get the first event from the head of the event queue */
  9812. spin_lock_irq(&phba->hbalock);
  9813. list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
  9814. cq_event, struct lpfc_cq_event, list);
  9815. spin_unlock_irq(&phba->hbalock);
  9816. /* Notify aborted XRI for FCP work queue */
  9817. lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9818. /* Free the event processed back to the free pool */
  9819. lpfc_sli4_cq_event_release(phba, cq_event);
  9820. }
  9821. }
  9822. /**
  9823. * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
  9824. * @phba: pointer to lpfc hba data structure.
  9825. *
  9826. * This routine is invoked by the worker thread to process all the pending
  9827. * SLI4 els abort xri events.
  9828. **/
  9829. void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
  9830. {
  9831. struct lpfc_cq_event *cq_event;
  9832. /* First, declare the els xri abort event has been handled */
  9833. spin_lock_irq(&phba->hbalock);
  9834. phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
  9835. spin_unlock_irq(&phba->hbalock);
  9836. /* Now, handle all the els xri abort events */
  9837. while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
  9838. /* Get the first event from the head of the event queue */
  9839. spin_lock_irq(&phba->hbalock);
  9840. list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
  9841. cq_event, struct lpfc_cq_event, list);
  9842. spin_unlock_irq(&phba->hbalock);
  9843. /* Notify aborted XRI for ELS work queue */
  9844. lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
  9845. /* Free the event processed back to the free pool */
  9846. lpfc_sli4_cq_event_release(phba, cq_event);
  9847. }
  9848. }
  9849. /**
  9850. * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
  9851. * @phba: pointer to lpfc hba data structure
  9852. * @pIocbIn: pointer to the rspiocbq
  9853. * @pIocbOut: pointer to the cmdiocbq
  9854. * @wcqe: pointer to the complete wcqe
  9855. *
  9856. * This routine transfers the fields of a command iocbq to a response iocbq
  9857. * by copying all the IOCB fields from command iocbq and transferring the
  9858. * completion status information from the complete wcqe.
  9859. **/
  9860. static void
  9861. lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
  9862. struct lpfc_iocbq *pIocbIn,
  9863. struct lpfc_iocbq *pIocbOut,
  9864. struct lpfc_wcqe_complete *wcqe)
  9865. {
  9866. unsigned long iflags;
  9867. uint32_t status;
  9868. size_t offset = offsetof(struct lpfc_iocbq, iocb);
  9869. memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
  9870. sizeof(struct lpfc_iocbq) - offset);
  9871. /* Map WCQE parameters into irspiocb parameters */
  9872. status = bf_get(lpfc_wcqe_c_status, wcqe);
  9873. pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
  9874. if (pIocbOut->iocb_flag & LPFC_IO_FCP)
  9875. if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
  9876. pIocbIn->iocb.un.fcpi.fcpi_parm =
  9877. pIocbOut->iocb.un.fcpi.fcpi_parm -
  9878. wcqe->total_data_placed;
  9879. else
  9880. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9881. else {
  9882. pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
  9883. pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
  9884. }
  9885. /* Convert BG errors for completion status */
  9886. if (status == CQE_STATUS_DI_ERROR) {
  9887. pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
  9888. if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
  9889. pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
  9890. else
  9891. pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
  9892. pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
  9893. if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
  9894. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9895. BGS_GUARD_ERR_MASK;
  9896. if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
  9897. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9898. BGS_APPTAG_ERR_MASK;
  9899. if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
  9900. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9901. BGS_REFTAG_ERR_MASK;
  9902. /* Check to see if there was any good data before the error */
  9903. if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
  9904. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9905. BGS_HI_WATER_MARK_PRESENT_MASK;
  9906. pIocbIn->iocb.unsli3.sli3_bg.bghm =
  9907. wcqe->total_data_placed;
  9908. }
  9909. /*
  9910. * Set ALL the error bits to indicate we don't know what
  9911. * type of error it is.
  9912. */
  9913. if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
  9914. pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
  9915. (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
  9916. BGS_GUARD_ERR_MASK);
  9917. }
  9918. /* Pick up HBA exchange busy condition */
  9919. if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
  9920. spin_lock_irqsave(&phba->hbalock, iflags);
  9921. pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
  9922. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9923. }
  9924. }
  9925. /**
  9926. * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
  9927. * @phba: Pointer to HBA context object.
  9928. * @wcqe: Pointer to work-queue completion queue entry.
  9929. *
  9930. * This routine handles an ELS work-queue completion event and construct
  9931. * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
  9932. * discovery engine to handle.
  9933. *
  9934. * Return: Pointer to the receive IOCBQ, NULL otherwise.
  9935. **/
  9936. static struct lpfc_iocbq *
  9937. lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
  9938. struct lpfc_iocbq *irspiocbq)
  9939. {
  9940. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  9941. struct lpfc_iocbq *cmdiocbq;
  9942. struct lpfc_wcqe_complete *wcqe;
  9943. unsigned long iflags;
  9944. wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
  9945. spin_lock_irqsave(&phba->hbalock, iflags);
  9946. pring->stats.iocb_event++;
  9947. /* Look up the ELS command IOCB and create pseudo response IOCB */
  9948. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  9949. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9950. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9951. if (unlikely(!cmdiocbq)) {
  9952. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  9953. "0386 ELS complete with no corresponding "
  9954. "cmdiocb: iotag (%d)\n",
  9955. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  9956. lpfc_sli_release_iocbq(phba, irspiocbq);
  9957. return NULL;
  9958. }
  9959. /* Fake the irspiocbq and copy necessary response information */
  9960. lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
  9961. return irspiocbq;
  9962. }
  9963. /**
  9964. * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
  9965. * @phba: Pointer to HBA context object.
  9966. * @cqe: Pointer to mailbox completion queue entry.
  9967. *
  9968. * This routine process a mailbox completion queue entry with asynchrous
  9969. * event.
  9970. *
  9971. * Return: true if work posted to worker thread, otherwise false.
  9972. **/
  9973. static bool
  9974. lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  9975. {
  9976. struct lpfc_cq_event *cq_event;
  9977. unsigned long iflags;
  9978. lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
  9979. "0392 Async Event: word0:x%x, word1:x%x, "
  9980. "word2:x%x, word3:x%x\n", mcqe->word0,
  9981. mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
  9982. /* Allocate a new internal CQ_EVENT entry */
  9983. cq_event = lpfc_sli4_cq_event_alloc(phba);
  9984. if (!cq_event) {
  9985. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  9986. "0394 Failed to allocate CQ_EVENT entry\n");
  9987. return false;
  9988. }
  9989. /* Move the CQE into an asynchronous event entry */
  9990. memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
  9991. spin_lock_irqsave(&phba->hbalock, iflags);
  9992. list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
  9993. /* Set the async event flag */
  9994. phba->hba_flag |= ASYNC_EVENT;
  9995. spin_unlock_irqrestore(&phba->hbalock, iflags);
  9996. return true;
  9997. }
  9998. /**
  9999. * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
  10000. * @phba: Pointer to HBA context object.
  10001. * @cqe: Pointer to mailbox completion queue entry.
  10002. *
  10003. * This routine process a mailbox completion queue entry with mailbox
  10004. * completion event.
  10005. *
  10006. * Return: true if work posted to worker thread, otherwise false.
  10007. **/
  10008. static bool
  10009. lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
  10010. {
  10011. uint32_t mcqe_status;
  10012. MAILBOX_t *mbox, *pmbox;
  10013. struct lpfc_mqe *mqe;
  10014. struct lpfc_vport *vport;
  10015. struct lpfc_nodelist *ndlp;
  10016. struct lpfc_dmabuf *mp;
  10017. unsigned long iflags;
  10018. LPFC_MBOXQ_t *pmb;
  10019. bool workposted = false;
  10020. int rc;
  10021. /* If not a mailbox complete MCQE, out by checking mailbox consume */
  10022. if (!bf_get(lpfc_trailer_completed, mcqe))
  10023. goto out_no_mqe_complete;
  10024. /* Get the reference to the active mbox command */
  10025. spin_lock_irqsave(&phba->hbalock, iflags);
  10026. pmb = phba->sli.mbox_active;
  10027. if (unlikely(!pmb)) {
  10028. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
  10029. "1832 No pending MBOX command to handle\n");
  10030. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10031. goto out_no_mqe_complete;
  10032. }
  10033. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10034. mqe = &pmb->u.mqe;
  10035. pmbox = (MAILBOX_t *)&pmb->u.mqe;
  10036. mbox = phba->mbox;
  10037. vport = pmb->vport;
  10038. /* Reset heartbeat timer */
  10039. phba->last_completion_time = jiffies;
  10040. del_timer(&phba->sli.mbox_tmo);
  10041. /* Move mbox data to caller's mailbox region, do endian swapping */
  10042. if (pmb->mbox_cmpl && mbox)
  10043. lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
  10044. /*
  10045. * For mcqe errors, conditionally move a modified error code to
  10046. * the mbox so that the error will not be missed.
  10047. */
  10048. mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
  10049. if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
  10050. if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
  10051. bf_set(lpfc_mqe_status, mqe,
  10052. (LPFC_MBX_ERROR_RANGE | mcqe_status));
  10053. }
  10054. if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
  10055. pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
  10056. lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
  10057. "MBOX dflt rpi: status:x%x rpi:x%x",
  10058. mcqe_status,
  10059. pmbox->un.varWords[0], 0);
  10060. if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
  10061. mp = (struct lpfc_dmabuf *)(pmb->context1);
  10062. ndlp = (struct lpfc_nodelist *)pmb->context2;
  10063. /* Reg_LOGIN of dflt RPI was successful. Now lets get
  10064. * RID of the PPI using the same mbox buffer.
  10065. */
  10066. lpfc_unreg_login(phba, vport->vpi,
  10067. pmbox->un.varWords[0], pmb);
  10068. pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
  10069. pmb->context1 = mp;
  10070. pmb->context2 = ndlp;
  10071. pmb->vport = vport;
  10072. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
  10073. if (rc != MBX_BUSY)
  10074. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
  10075. LOG_SLI, "0385 rc should "
  10076. "have been MBX_BUSY\n");
  10077. if (rc != MBX_NOT_FINISHED)
  10078. goto send_current_mbox;
  10079. }
  10080. }
  10081. spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
  10082. phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
  10083. spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
  10084. /* There is mailbox completion work to do */
  10085. spin_lock_irqsave(&phba->hbalock, iflags);
  10086. __lpfc_mbox_cmpl_put(phba, pmb);
  10087. phba->work_ha |= HA_MBATT;
  10088. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10089. workposted = true;
  10090. send_current_mbox:
  10091. spin_lock_irqsave(&phba->hbalock, iflags);
  10092. /* Release the mailbox command posting token */
  10093. phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
  10094. /* Setting active mailbox pointer need to be in sync to flag clear */
  10095. phba->sli.mbox_active = NULL;
  10096. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10097. /* Wake up worker thread to post the next pending mailbox command */
  10098. lpfc_worker_wake_up(phba);
  10099. out_no_mqe_complete:
  10100. if (bf_get(lpfc_trailer_consumed, mcqe))
  10101. lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
  10102. return workposted;
  10103. }
  10104. /**
  10105. * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
  10106. * @phba: Pointer to HBA context object.
  10107. * @cqe: Pointer to mailbox completion queue entry.
  10108. *
  10109. * This routine process a mailbox completion queue entry, it invokes the
  10110. * proper mailbox complete handling or asynchrous event handling routine
  10111. * according to the MCQE's async bit.
  10112. *
  10113. * Return: true if work posted to worker thread, otherwise false.
  10114. **/
  10115. static bool
  10116. lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
  10117. {
  10118. struct lpfc_mcqe mcqe;
  10119. bool workposted;
  10120. /* Copy the mailbox MCQE and convert endian order as needed */
  10121. lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
  10122. /* Invoke the proper event handling routine */
  10123. if (!bf_get(lpfc_trailer_async, &mcqe))
  10124. workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
  10125. else
  10126. workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
  10127. return workposted;
  10128. }
  10129. /**
  10130. * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
  10131. * @phba: Pointer to HBA context object.
  10132. * @wcqe: Pointer to work-queue completion queue entry.
  10133. *
  10134. * This routine handles an ELS work-queue completion event.
  10135. *
  10136. * Return: true if work posted to worker thread, otherwise false.
  10137. **/
  10138. static bool
  10139. lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
  10140. struct lpfc_wcqe_complete *wcqe)
  10141. {
  10142. struct lpfc_iocbq *irspiocbq;
  10143. unsigned long iflags;
  10144. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10145. /* Get an irspiocbq for later ELS response processing use */
  10146. irspiocbq = lpfc_sli_get_iocbq(phba);
  10147. if (!irspiocbq) {
  10148. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10149. "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
  10150. "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
  10151. pring->txq_cnt, phba->iocb_cnt,
  10152. phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
  10153. phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
  10154. return false;
  10155. }
  10156. /* Save off the slow-path queue event for work thread to process */
  10157. memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
  10158. spin_lock_irqsave(&phba->hbalock, iflags);
  10159. list_add_tail(&irspiocbq->cq_event.list,
  10160. &phba->sli4_hba.sp_queue_event);
  10161. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10162. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10163. return true;
  10164. }
  10165. /**
  10166. * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
  10167. * @phba: Pointer to HBA context object.
  10168. * @wcqe: Pointer to work-queue completion queue entry.
  10169. *
  10170. * This routine handles slow-path WQ entry comsumed event by invoking the
  10171. * proper WQ release routine to the slow-path WQ.
  10172. **/
  10173. static void
  10174. lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
  10175. struct lpfc_wcqe_release *wcqe)
  10176. {
  10177. /* sanity check on queue memory */
  10178. if (unlikely(!phba->sli4_hba.els_wq))
  10179. return;
  10180. /* Check for the slow-path ELS work queue */
  10181. if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
  10182. lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
  10183. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10184. else
  10185. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10186. "2579 Slow-path wqe consume event carries "
  10187. "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
  10188. bf_get(lpfc_wcqe_r_wqe_index, wcqe),
  10189. phba->sli4_hba.els_wq->queue_id);
  10190. }
  10191. /**
  10192. * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
  10193. * @phba: Pointer to HBA context object.
  10194. * @cq: Pointer to a WQ completion queue.
  10195. * @wcqe: Pointer to work-queue completion queue entry.
  10196. *
  10197. * This routine handles an XRI abort event.
  10198. *
  10199. * Return: true if work posted to worker thread, otherwise false.
  10200. **/
  10201. static bool
  10202. lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
  10203. struct lpfc_queue *cq,
  10204. struct sli4_wcqe_xri_aborted *wcqe)
  10205. {
  10206. bool workposted = false;
  10207. struct lpfc_cq_event *cq_event;
  10208. unsigned long iflags;
  10209. /* Allocate a new internal CQ_EVENT entry */
  10210. cq_event = lpfc_sli4_cq_event_alloc(phba);
  10211. if (!cq_event) {
  10212. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10213. "0602 Failed to allocate CQ_EVENT entry\n");
  10214. return false;
  10215. }
  10216. /* Move the CQE into the proper xri abort event list */
  10217. memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
  10218. switch (cq->subtype) {
  10219. case LPFC_FCP:
  10220. spin_lock_irqsave(&phba->hbalock, iflags);
  10221. list_add_tail(&cq_event->list,
  10222. &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
  10223. /* Set the fcp xri abort event flag */
  10224. phba->hba_flag |= FCP_XRI_ABORT_EVENT;
  10225. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10226. workposted = true;
  10227. break;
  10228. case LPFC_ELS:
  10229. spin_lock_irqsave(&phba->hbalock, iflags);
  10230. list_add_tail(&cq_event->list,
  10231. &phba->sli4_hba.sp_els_xri_aborted_work_queue);
  10232. /* Set the els xri abort event flag */
  10233. phba->hba_flag |= ELS_XRI_ABORT_EVENT;
  10234. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10235. workposted = true;
  10236. break;
  10237. default:
  10238. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10239. "0603 Invalid work queue CQE subtype (x%x)\n",
  10240. cq->subtype);
  10241. workposted = false;
  10242. break;
  10243. }
  10244. return workposted;
  10245. }
  10246. /**
  10247. * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
  10248. * @phba: Pointer to HBA context object.
  10249. * @rcqe: Pointer to receive-queue completion queue entry.
  10250. *
  10251. * This routine process a receive-queue completion queue entry.
  10252. *
  10253. * Return: true if work posted to worker thread, otherwise false.
  10254. **/
  10255. static bool
  10256. lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
  10257. {
  10258. bool workposted = false;
  10259. struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
  10260. struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
  10261. struct hbq_dmabuf *dma_buf;
  10262. uint32_t status, rq_id;
  10263. unsigned long iflags;
  10264. /* sanity check on queue memory */
  10265. if (unlikely(!hrq) || unlikely(!drq))
  10266. return workposted;
  10267. if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
  10268. rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
  10269. else
  10270. rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
  10271. if (rq_id != hrq->queue_id)
  10272. goto out;
  10273. status = bf_get(lpfc_rcqe_status, rcqe);
  10274. switch (status) {
  10275. case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
  10276. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10277. "2537 Receive Frame Truncated!!\n");
  10278. case FC_STATUS_RQ_SUCCESS:
  10279. lpfc_sli4_rq_release(hrq, drq);
  10280. spin_lock_irqsave(&phba->hbalock, iflags);
  10281. dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
  10282. if (!dma_buf) {
  10283. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10284. goto out;
  10285. }
  10286. memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
  10287. /* save off the frame for the word thread to process */
  10288. list_add_tail(&dma_buf->cq_event.list,
  10289. &phba->sli4_hba.sp_queue_event);
  10290. /* Frame received */
  10291. phba->hba_flag |= HBA_SP_QUEUE_EVT;
  10292. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10293. workposted = true;
  10294. break;
  10295. case FC_STATUS_INSUFF_BUF_NEED_BUF:
  10296. case FC_STATUS_INSUFF_BUF_FRM_DISC:
  10297. /* Post more buffers if possible */
  10298. spin_lock_irqsave(&phba->hbalock, iflags);
  10299. phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
  10300. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10301. workposted = true;
  10302. break;
  10303. }
  10304. out:
  10305. return workposted;
  10306. }
  10307. /**
  10308. * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
  10309. * @phba: Pointer to HBA context object.
  10310. * @cq: Pointer to the completion queue.
  10311. * @wcqe: Pointer to a completion queue entry.
  10312. *
  10313. * This routine process a slow-path work-queue or receive queue completion queue
  10314. * entry.
  10315. *
  10316. * Return: true if work posted to worker thread, otherwise false.
  10317. **/
  10318. static bool
  10319. lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10320. struct lpfc_cqe *cqe)
  10321. {
  10322. struct lpfc_cqe cqevt;
  10323. bool workposted = false;
  10324. /* Copy the work queue CQE and convert endian order if needed */
  10325. lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
  10326. /* Check and process for different type of WCQE and dispatch */
  10327. switch (bf_get(lpfc_cqe_code, &cqevt)) {
  10328. case CQE_CODE_COMPL_WQE:
  10329. /* Process the WQ/RQ complete event */
  10330. phba->last_completion_time = jiffies;
  10331. workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
  10332. (struct lpfc_wcqe_complete *)&cqevt);
  10333. break;
  10334. case CQE_CODE_RELEASE_WQE:
  10335. /* Process the WQ release event */
  10336. lpfc_sli4_sp_handle_rel_wcqe(phba,
  10337. (struct lpfc_wcqe_release *)&cqevt);
  10338. break;
  10339. case CQE_CODE_XRI_ABORTED:
  10340. /* Process the WQ XRI abort event */
  10341. phba->last_completion_time = jiffies;
  10342. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10343. (struct sli4_wcqe_xri_aborted *)&cqevt);
  10344. break;
  10345. case CQE_CODE_RECEIVE:
  10346. case CQE_CODE_RECEIVE_V1:
  10347. /* Process the RQ event */
  10348. phba->last_completion_time = jiffies;
  10349. workposted = lpfc_sli4_sp_handle_rcqe(phba,
  10350. (struct lpfc_rcqe *)&cqevt);
  10351. break;
  10352. default:
  10353. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10354. "0388 Not a valid WCQE code: x%x\n",
  10355. bf_get(lpfc_cqe_code, &cqevt));
  10356. break;
  10357. }
  10358. return workposted;
  10359. }
  10360. /**
  10361. * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
  10362. * @phba: Pointer to HBA context object.
  10363. * @eqe: Pointer to fast-path event queue entry.
  10364. *
  10365. * This routine process a event queue entry from the slow-path event queue.
  10366. * It will check the MajorCode and MinorCode to determine this is for a
  10367. * completion event on a completion queue, if not, an error shall be logged
  10368. * and just return. Otherwise, it will get to the corresponding completion
  10369. * queue and process all the entries on that completion queue, rearm the
  10370. * completion queue, and then return.
  10371. *
  10372. **/
  10373. static void
  10374. lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
  10375. {
  10376. struct lpfc_queue *cq = NULL, *childq, *speq;
  10377. struct lpfc_cqe *cqe;
  10378. bool workposted = false;
  10379. int ecount = 0;
  10380. uint16_t cqid;
  10381. if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
  10382. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10383. "0359 Not a valid slow-path completion "
  10384. "event: majorcode=x%x, minorcode=x%x\n",
  10385. bf_get_le32(lpfc_eqe_major_code, eqe),
  10386. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10387. return;
  10388. }
  10389. /* Get the reference to the corresponding CQ */
  10390. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10391. /* Search for completion queue pointer matching this cqid */
  10392. speq = phba->sli4_hba.sp_eq;
  10393. /* sanity check on queue memory */
  10394. if (unlikely(!speq))
  10395. return;
  10396. list_for_each_entry(childq, &speq->child_list, list) {
  10397. if (childq->queue_id == cqid) {
  10398. cq = childq;
  10399. break;
  10400. }
  10401. }
  10402. if (unlikely(!cq)) {
  10403. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10404. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10405. "0365 Slow-path CQ identifier "
  10406. "(%d) does not exist\n", cqid);
  10407. return;
  10408. }
  10409. /* Process all the entries to the CQ */
  10410. switch (cq->type) {
  10411. case LPFC_MCQ:
  10412. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10413. workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
  10414. if (!(++ecount % cq->entry_repost))
  10415. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10416. }
  10417. break;
  10418. case LPFC_WCQ:
  10419. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10420. if (cq->subtype == LPFC_FCP)
  10421. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
  10422. cqe);
  10423. else
  10424. workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
  10425. cqe);
  10426. if (!(++ecount % cq->entry_repost))
  10427. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10428. }
  10429. break;
  10430. default:
  10431. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10432. "0370 Invalid completion queue type (%d)\n",
  10433. cq->type);
  10434. return;
  10435. }
  10436. /* Catch the no cq entry condition, log an error */
  10437. if (unlikely(ecount == 0))
  10438. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10439. "0371 No entry from the CQ: identifier "
  10440. "(x%x), type (%d)\n", cq->queue_id, cq->type);
  10441. /* In any case, flash and re-arm the RCQ */
  10442. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10443. /* wake up worker thread if there are works to be done */
  10444. if (workposted)
  10445. lpfc_worker_wake_up(phba);
  10446. }
  10447. /**
  10448. * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
  10449. * @eqe: Pointer to fast-path completion queue entry.
  10450. *
  10451. * This routine process a fast-path work queue completion entry from fast-path
  10452. * event queue for FCP command response completion.
  10453. **/
  10454. static void
  10455. lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
  10456. struct lpfc_wcqe_complete *wcqe)
  10457. {
  10458. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
  10459. struct lpfc_iocbq *cmdiocbq;
  10460. struct lpfc_iocbq irspiocbq;
  10461. unsigned long iflags;
  10462. spin_lock_irqsave(&phba->hbalock, iflags);
  10463. pring->stats.iocb_event++;
  10464. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10465. /* Check for response status */
  10466. if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
  10467. /* If resource errors reported from HBA, reduce queue
  10468. * depth of the SCSI device.
  10469. */
  10470. if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
  10471. IOSTAT_LOCAL_REJECT) &&
  10472. (wcqe->parameter == IOERR_NO_RESOURCES)) {
  10473. phba->lpfc_rampdown_queue_depth(phba);
  10474. }
  10475. /* Log the error status */
  10476. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10477. "0373 FCP complete error: status=x%x, "
  10478. "hw_status=x%x, total_data_specified=%d, "
  10479. "parameter=x%x, word3=x%x\n",
  10480. bf_get(lpfc_wcqe_c_status, wcqe),
  10481. bf_get(lpfc_wcqe_c_hw_status, wcqe),
  10482. wcqe->total_data_placed, wcqe->parameter,
  10483. wcqe->word3);
  10484. }
  10485. /* Look up the FCP command IOCB and create pseudo response IOCB */
  10486. spin_lock_irqsave(&phba->hbalock, iflags);
  10487. cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
  10488. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10489. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10490. if (unlikely(!cmdiocbq)) {
  10491. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10492. "0374 FCP complete with no corresponding "
  10493. "cmdiocb: iotag (%d)\n",
  10494. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10495. return;
  10496. }
  10497. if (unlikely(!cmdiocbq->iocb_cmpl)) {
  10498. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10499. "0375 FCP cmdiocb not callback function "
  10500. "iotag: (%d)\n",
  10501. bf_get(lpfc_wcqe_c_request_tag, wcqe));
  10502. return;
  10503. }
  10504. /* Fake the irspiocb and copy necessary response information */
  10505. lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
  10506. if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
  10507. spin_lock_irqsave(&phba->hbalock, iflags);
  10508. cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
  10509. spin_unlock_irqrestore(&phba->hbalock, iflags);
  10510. }
  10511. /* Pass the cmd_iocb and the rsp state to the upper layer */
  10512. (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
  10513. }
  10514. /**
  10515. * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
  10516. * @phba: Pointer to HBA context object.
  10517. * @cq: Pointer to completion queue.
  10518. * @wcqe: Pointer to work-queue completion queue entry.
  10519. *
  10520. * This routine handles an fast-path WQ entry comsumed event by invoking the
  10521. * proper WQ release routine to the slow-path WQ.
  10522. **/
  10523. static void
  10524. lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10525. struct lpfc_wcqe_release *wcqe)
  10526. {
  10527. struct lpfc_queue *childwq;
  10528. bool wqid_matched = false;
  10529. uint16_t fcp_wqid;
  10530. /* Check for fast-path FCP work queue release */
  10531. fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
  10532. list_for_each_entry(childwq, &cq->child_list, list) {
  10533. if (childwq->queue_id == fcp_wqid) {
  10534. lpfc_sli4_wq_release(childwq,
  10535. bf_get(lpfc_wcqe_r_wqe_index, wcqe));
  10536. wqid_matched = true;
  10537. break;
  10538. }
  10539. }
  10540. /* Report warning log message if no match found */
  10541. if (wqid_matched != true)
  10542. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10543. "2580 Fast-path wqe consume event carries "
  10544. "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
  10545. }
  10546. /**
  10547. * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
  10548. * @cq: Pointer to the completion queue.
  10549. * @eqe: Pointer to fast-path completion queue entry.
  10550. *
  10551. * This routine process a fast-path work queue completion entry from fast-path
  10552. * event queue for FCP command response completion.
  10553. **/
  10554. static int
  10555. lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
  10556. struct lpfc_cqe *cqe)
  10557. {
  10558. struct lpfc_wcqe_release wcqe;
  10559. bool workposted = false;
  10560. /* Copy the work queue CQE and convert endian order if needed */
  10561. lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
  10562. /* Check and process for different type of WCQE and dispatch */
  10563. switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
  10564. case CQE_CODE_COMPL_WQE:
  10565. /* Process the WQ complete event */
  10566. phba->last_completion_time = jiffies;
  10567. lpfc_sli4_fp_handle_fcp_wcqe(phba,
  10568. (struct lpfc_wcqe_complete *)&wcqe);
  10569. break;
  10570. case CQE_CODE_RELEASE_WQE:
  10571. /* Process the WQ release event */
  10572. lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
  10573. (struct lpfc_wcqe_release *)&wcqe);
  10574. break;
  10575. case CQE_CODE_XRI_ABORTED:
  10576. /* Process the WQ XRI abort event */
  10577. phba->last_completion_time = jiffies;
  10578. workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
  10579. (struct sli4_wcqe_xri_aborted *)&wcqe);
  10580. break;
  10581. default:
  10582. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10583. "0144 Not a valid WCQE code: x%x\n",
  10584. bf_get(lpfc_wcqe_c_code, &wcqe));
  10585. break;
  10586. }
  10587. return workposted;
  10588. }
  10589. /**
  10590. * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
  10591. * @phba: Pointer to HBA context object.
  10592. * @eqe: Pointer to fast-path event queue entry.
  10593. *
  10594. * This routine process a event queue entry from the fast-path event queue.
  10595. * It will check the MajorCode and MinorCode to determine this is for a
  10596. * completion event on a completion queue, if not, an error shall be logged
  10597. * and just return. Otherwise, it will get to the corresponding completion
  10598. * queue and process all the entries on the completion queue, rearm the
  10599. * completion queue, and then return.
  10600. **/
  10601. static void
  10602. lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
  10603. uint32_t fcp_cqidx)
  10604. {
  10605. struct lpfc_queue *cq;
  10606. struct lpfc_cqe *cqe;
  10607. bool workposted = false;
  10608. uint16_t cqid;
  10609. int ecount = 0;
  10610. if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
  10611. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10612. "0366 Not a valid fast-path completion "
  10613. "event: majorcode=x%x, minorcode=x%x\n",
  10614. bf_get_le32(lpfc_eqe_major_code, eqe),
  10615. bf_get_le32(lpfc_eqe_minor_code, eqe));
  10616. return;
  10617. }
  10618. if (unlikely(!phba->sli4_hba.fcp_cq)) {
  10619. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10620. "3146 Fast-path completion queues "
  10621. "does not exist\n");
  10622. return;
  10623. }
  10624. cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
  10625. if (unlikely(!cq)) {
  10626. if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
  10627. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10628. "0367 Fast-path completion queue "
  10629. "(%d) does not exist\n", fcp_cqidx);
  10630. return;
  10631. }
  10632. /* Get the reference to the corresponding CQ */
  10633. cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
  10634. if (unlikely(cqid != cq->queue_id)) {
  10635. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10636. "0368 Miss-matched fast-path completion "
  10637. "queue identifier: eqcqid=%d, fcpcqid=%d\n",
  10638. cqid, cq->queue_id);
  10639. return;
  10640. }
  10641. /* Process all the entries to the CQ */
  10642. while ((cqe = lpfc_sli4_cq_get(cq))) {
  10643. workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
  10644. if (!(++ecount % cq->entry_repost))
  10645. lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
  10646. }
  10647. /* Catch the no cq entry condition */
  10648. if (unlikely(ecount == 0))
  10649. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  10650. "0369 No entry from fast-path completion "
  10651. "queue fcpcqid=%d\n", cq->queue_id);
  10652. /* In any case, flash and re-arm the CQ */
  10653. lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
  10654. /* wake up worker thread if there are works to be done */
  10655. if (workposted)
  10656. lpfc_worker_wake_up(phba);
  10657. }
  10658. static void
  10659. lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
  10660. {
  10661. struct lpfc_eqe *eqe;
  10662. /* walk all the EQ entries and drop on the floor */
  10663. while ((eqe = lpfc_sli4_eq_get(eq)))
  10664. ;
  10665. /* Clear and re-arm the EQ */
  10666. lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
  10667. }
  10668. /**
  10669. * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
  10670. * @irq: Interrupt number.
  10671. * @dev_id: The device context pointer.
  10672. *
  10673. * This function is directly called from the PCI layer as an interrupt
  10674. * service routine when device with SLI-4 interface spec is enabled with
  10675. * MSI-X multi-message interrupt mode and there are slow-path events in
  10676. * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
  10677. * interrupt mode, this function is called as part of the device-level
  10678. * interrupt handler. When the PCI slot is in error recovery or the HBA is
  10679. * undergoing initialization, the interrupt handler will not process the
  10680. * interrupt. The link attention and ELS ring attention events are handled
  10681. * by the worker thread. The interrupt handler signals the worker thread
  10682. * and returns for these events. This function is called without any lock
  10683. * held. It gets the hbalock to access and update SLI data structures.
  10684. *
  10685. * This function returns IRQ_HANDLED when interrupt is handled else it
  10686. * returns IRQ_NONE.
  10687. **/
  10688. irqreturn_t
  10689. lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
  10690. {
  10691. struct lpfc_hba *phba;
  10692. struct lpfc_queue *speq;
  10693. struct lpfc_eqe *eqe;
  10694. unsigned long iflag;
  10695. int ecount = 0;
  10696. /*
  10697. * Get the driver's phba structure from the dev_id
  10698. */
  10699. phba = (struct lpfc_hba *)dev_id;
  10700. if (unlikely(!phba))
  10701. return IRQ_NONE;
  10702. /* Get to the EQ struct associated with this vector */
  10703. speq = phba->sli4_hba.sp_eq;
  10704. if (unlikely(!speq))
  10705. return IRQ_NONE;
  10706. /* Check device state for handling interrupt */
  10707. if (unlikely(lpfc_intr_state_check(phba))) {
  10708. /* Check again for link_state with lock held */
  10709. spin_lock_irqsave(&phba->hbalock, iflag);
  10710. if (phba->link_state < LPFC_LINK_DOWN)
  10711. /* Flush, clear interrupt, and rearm the EQ */
  10712. lpfc_sli4_eq_flush(phba, speq);
  10713. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10714. return IRQ_NONE;
  10715. }
  10716. /*
  10717. * Process all the event on FCP slow-path EQ
  10718. */
  10719. while ((eqe = lpfc_sli4_eq_get(speq))) {
  10720. lpfc_sli4_sp_handle_eqe(phba, eqe);
  10721. if (!(++ecount % speq->entry_repost))
  10722. lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
  10723. }
  10724. /* Always clear and re-arm the slow-path EQ */
  10725. lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
  10726. /* Catch the no cq entry condition */
  10727. if (unlikely(ecount == 0)) {
  10728. if (phba->intr_type == MSIX)
  10729. /* MSI-X treated interrupt served as no EQ share INT */
  10730. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10731. "0357 MSI-X interrupt with no EQE\n");
  10732. else
  10733. /* Non MSI-X treated on interrupt as EQ share INT */
  10734. return IRQ_NONE;
  10735. }
  10736. return IRQ_HANDLED;
  10737. } /* lpfc_sli4_sp_intr_handler */
  10738. /**
  10739. * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
  10740. * @irq: Interrupt number.
  10741. * @dev_id: The device context pointer.
  10742. *
  10743. * This function is directly called from the PCI layer as an interrupt
  10744. * service routine when device with SLI-4 interface spec is enabled with
  10745. * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
  10746. * ring event in the HBA. However, when the device is enabled with either
  10747. * MSI or Pin-IRQ interrupt mode, this function is called as part of the
  10748. * device-level interrupt handler. When the PCI slot is in error recovery
  10749. * or the HBA is undergoing initialization, the interrupt handler will not
  10750. * process the interrupt. The SCSI FCP fast-path ring event are handled in
  10751. * the intrrupt context. This function is called without any lock held.
  10752. * It gets the hbalock to access and update SLI data structures. Note that,
  10753. * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
  10754. * equal to that of FCP CQ index.
  10755. *
  10756. * This function returns IRQ_HANDLED when interrupt is handled else it
  10757. * returns IRQ_NONE.
  10758. **/
  10759. irqreturn_t
  10760. lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
  10761. {
  10762. struct lpfc_hba *phba;
  10763. struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
  10764. struct lpfc_queue *fpeq;
  10765. struct lpfc_eqe *eqe;
  10766. unsigned long iflag;
  10767. int ecount = 0;
  10768. uint32_t fcp_eqidx;
  10769. /* Get the driver's phba structure from the dev_id */
  10770. fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
  10771. phba = fcp_eq_hdl->phba;
  10772. fcp_eqidx = fcp_eq_hdl->idx;
  10773. if (unlikely(!phba))
  10774. return IRQ_NONE;
  10775. if (unlikely(!phba->sli4_hba.fp_eq))
  10776. return IRQ_NONE;
  10777. /* Get to the EQ struct associated with this vector */
  10778. fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
  10779. if (unlikely(!fpeq))
  10780. return IRQ_NONE;
  10781. /* Check device state for handling interrupt */
  10782. if (unlikely(lpfc_intr_state_check(phba))) {
  10783. /* Check again for link_state with lock held */
  10784. spin_lock_irqsave(&phba->hbalock, iflag);
  10785. if (phba->link_state < LPFC_LINK_DOWN)
  10786. /* Flush, clear interrupt, and rearm the EQ */
  10787. lpfc_sli4_eq_flush(phba, fpeq);
  10788. spin_unlock_irqrestore(&phba->hbalock, iflag);
  10789. return IRQ_NONE;
  10790. }
  10791. /*
  10792. * Process all the event on FCP fast-path EQ
  10793. */
  10794. while ((eqe = lpfc_sli4_eq_get(fpeq))) {
  10795. lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
  10796. if (!(++ecount % fpeq->entry_repost))
  10797. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
  10798. }
  10799. /* Always clear and re-arm the fast-path EQ */
  10800. lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
  10801. if (unlikely(ecount == 0)) {
  10802. if (phba->intr_type == MSIX)
  10803. /* MSI-X treated interrupt served as no EQ share INT */
  10804. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  10805. "0358 MSI-X interrupt with no EQE\n");
  10806. else
  10807. /* Non MSI-X treated on interrupt as EQ share INT */
  10808. return IRQ_NONE;
  10809. }
  10810. return IRQ_HANDLED;
  10811. } /* lpfc_sli4_fp_intr_handler */
  10812. /**
  10813. * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
  10814. * @irq: Interrupt number.
  10815. * @dev_id: The device context pointer.
  10816. *
  10817. * This function is the device-level interrupt handler to device with SLI-4
  10818. * interface spec, called from the PCI layer when either MSI or Pin-IRQ
  10819. * interrupt mode is enabled and there is an event in the HBA which requires
  10820. * driver attention. This function invokes the slow-path interrupt attention
  10821. * handling function and fast-path interrupt attention handling function in
  10822. * turn to process the relevant HBA attention events. This function is called
  10823. * without any lock held. It gets the hbalock to access and update SLI data
  10824. * structures.
  10825. *
  10826. * This function returns IRQ_HANDLED when interrupt is handled, else it
  10827. * returns IRQ_NONE.
  10828. **/
  10829. irqreturn_t
  10830. lpfc_sli4_intr_handler(int irq, void *dev_id)
  10831. {
  10832. struct lpfc_hba *phba;
  10833. irqreturn_t sp_irq_rc, fp_irq_rc;
  10834. bool fp_handled = false;
  10835. uint32_t fcp_eqidx;
  10836. /* Get the driver's phba structure from the dev_id */
  10837. phba = (struct lpfc_hba *)dev_id;
  10838. if (unlikely(!phba))
  10839. return IRQ_NONE;
  10840. /*
  10841. * Invokes slow-path host attention interrupt handling as appropriate.
  10842. */
  10843. sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
  10844. /*
  10845. * Invoke fast-path host attention interrupt handling as appropriate.
  10846. */
  10847. for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
  10848. fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
  10849. &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
  10850. if (fp_irq_rc == IRQ_HANDLED)
  10851. fp_handled |= true;
  10852. }
  10853. return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
  10854. } /* lpfc_sli4_intr_handler */
  10855. /**
  10856. * lpfc_sli4_queue_free - free a queue structure and associated memory
  10857. * @queue: The queue structure to free.
  10858. *
  10859. * This function frees a queue structure and the DMAable memory used for
  10860. * the host resident queue. This function must be called after destroying the
  10861. * queue on the HBA.
  10862. **/
  10863. void
  10864. lpfc_sli4_queue_free(struct lpfc_queue *queue)
  10865. {
  10866. struct lpfc_dmabuf *dmabuf;
  10867. if (!queue)
  10868. return;
  10869. while (!list_empty(&queue->page_list)) {
  10870. list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
  10871. list);
  10872. dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
  10873. dmabuf->virt, dmabuf->phys);
  10874. kfree(dmabuf);
  10875. }
  10876. kfree(queue);
  10877. return;
  10878. }
  10879. /**
  10880. * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
  10881. * @phba: The HBA that this queue is being created on.
  10882. * @entry_size: The size of each queue entry for this queue.
  10883. * @entry count: The number of entries that this queue will handle.
  10884. *
  10885. * This function allocates a queue structure and the DMAable memory used for
  10886. * the host resident queue. This function must be called before creating the
  10887. * queue on the HBA.
  10888. **/
  10889. struct lpfc_queue *
  10890. lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
  10891. uint32_t entry_count)
  10892. {
  10893. struct lpfc_queue *queue;
  10894. struct lpfc_dmabuf *dmabuf;
  10895. int x, total_qe_count;
  10896. void *dma_pointer;
  10897. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10898. if (!phba->sli4_hba.pc_sli4_params.supported)
  10899. hw_page_size = SLI4_PAGE_SIZE;
  10900. queue = kzalloc(sizeof(struct lpfc_queue) +
  10901. (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
  10902. if (!queue)
  10903. return NULL;
  10904. queue->page_count = (ALIGN(entry_size * entry_count,
  10905. hw_page_size))/hw_page_size;
  10906. INIT_LIST_HEAD(&queue->list);
  10907. INIT_LIST_HEAD(&queue->page_list);
  10908. INIT_LIST_HEAD(&queue->child_list);
  10909. for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
  10910. dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  10911. if (!dmabuf)
  10912. goto out_fail;
  10913. dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
  10914. hw_page_size, &dmabuf->phys,
  10915. GFP_KERNEL);
  10916. if (!dmabuf->virt) {
  10917. kfree(dmabuf);
  10918. goto out_fail;
  10919. }
  10920. memset(dmabuf->virt, 0, hw_page_size);
  10921. dmabuf->buffer_tag = x;
  10922. list_add_tail(&dmabuf->list, &queue->page_list);
  10923. /* initialize queue's entry array */
  10924. dma_pointer = dmabuf->virt;
  10925. for (; total_qe_count < entry_count &&
  10926. dma_pointer < (hw_page_size + dmabuf->virt);
  10927. total_qe_count++, dma_pointer += entry_size) {
  10928. queue->qe[total_qe_count].address = dma_pointer;
  10929. }
  10930. }
  10931. queue->entry_size = entry_size;
  10932. queue->entry_count = entry_count;
  10933. /*
  10934. * entry_repost is calculated based on the number of entries in the
  10935. * queue. This works out except for RQs. If buffers are NOT initially
  10936. * posted for every RQE, entry_repost should be adjusted accordingly.
  10937. */
  10938. queue->entry_repost = (entry_count >> 3);
  10939. if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
  10940. queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
  10941. queue->phba = phba;
  10942. return queue;
  10943. out_fail:
  10944. lpfc_sli4_queue_free(queue);
  10945. return NULL;
  10946. }
  10947. /**
  10948. * lpfc_eq_create - Create an Event Queue on the HBA
  10949. * @phba: HBA structure that indicates port to create a queue on.
  10950. * @eq: The queue structure to use to create the event queue.
  10951. * @imax: The maximum interrupt per second limit.
  10952. *
  10953. * This function creates an event queue, as detailed in @eq, on a port,
  10954. * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
  10955. *
  10956. * The @phba struct is used to send mailbox command to HBA. The @eq struct
  10957. * is used to get the entry count and entry size that are necessary to
  10958. * determine the number of pages to allocate and use for this queue. This
  10959. * function will send the EQ_CREATE mailbox command to the HBA to setup the
  10960. * event queue. This function is asynchronous and will wait for the mailbox
  10961. * command to finish before continuing.
  10962. *
  10963. * On success this function will return a zero. If unable to allocate enough
  10964. * memory this function will return -ENOMEM. If the queue create mailbox command
  10965. * fails this function will return -ENXIO.
  10966. **/
  10967. uint32_t
  10968. lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
  10969. {
  10970. struct lpfc_mbx_eq_create *eq_create;
  10971. LPFC_MBOXQ_t *mbox;
  10972. int rc, length, status = 0;
  10973. struct lpfc_dmabuf *dmabuf;
  10974. uint32_t shdr_status, shdr_add_status;
  10975. union lpfc_sli4_cfg_shdr *shdr;
  10976. uint16_t dmult;
  10977. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  10978. /* sanity check on queue memory */
  10979. if (!eq)
  10980. return -ENODEV;
  10981. if (!phba->sli4_hba.pc_sli4_params.supported)
  10982. hw_page_size = SLI4_PAGE_SIZE;
  10983. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  10984. if (!mbox)
  10985. return -ENOMEM;
  10986. length = (sizeof(struct lpfc_mbx_eq_create) -
  10987. sizeof(struct lpfc_sli4_cfg_mhdr));
  10988. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  10989. LPFC_MBOX_OPCODE_EQ_CREATE,
  10990. length, LPFC_SLI4_MBX_EMBED);
  10991. eq_create = &mbox->u.mqe.un.eq_create;
  10992. bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
  10993. eq->page_count);
  10994. bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
  10995. LPFC_EQE_SIZE);
  10996. bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
  10997. /* Calculate delay multiper from maximum interrupt per second */
  10998. dmult = LPFC_DMULT_CONST/imax - 1;
  10999. bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
  11000. dmult);
  11001. switch (eq->entry_count) {
  11002. default:
  11003. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11004. "0360 Unsupported EQ count. (%d)\n",
  11005. eq->entry_count);
  11006. if (eq->entry_count < 256)
  11007. return -EINVAL;
  11008. /* otherwise default to smallest count (drop through) */
  11009. case 256:
  11010. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11011. LPFC_EQ_CNT_256);
  11012. break;
  11013. case 512:
  11014. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11015. LPFC_EQ_CNT_512);
  11016. break;
  11017. case 1024:
  11018. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11019. LPFC_EQ_CNT_1024);
  11020. break;
  11021. case 2048:
  11022. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11023. LPFC_EQ_CNT_2048);
  11024. break;
  11025. case 4096:
  11026. bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
  11027. LPFC_EQ_CNT_4096);
  11028. break;
  11029. }
  11030. list_for_each_entry(dmabuf, &eq->page_list, list) {
  11031. memset(dmabuf->virt, 0, hw_page_size);
  11032. eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11033. putPaddrLow(dmabuf->phys);
  11034. eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11035. putPaddrHigh(dmabuf->phys);
  11036. }
  11037. mbox->vport = phba->pport;
  11038. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11039. mbox->context1 = NULL;
  11040. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11041. shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
  11042. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11043. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11044. if (shdr_status || shdr_add_status || rc) {
  11045. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11046. "2500 EQ_CREATE mailbox failed with "
  11047. "status x%x add_status x%x, mbx status x%x\n",
  11048. shdr_status, shdr_add_status, rc);
  11049. status = -ENXIO;
  11050. }
  11051. eq->type = LPFC_EQ;
  11052. eq->subtype = LPFC_NONE;
  11053. eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
  11054. if (eq->queue_id == 0xFFFF)
  11055. status = -ENXIO;
  11056. eq->host_index = 0;
  11057. eq->hba_index = 0;
  11058. mempool_free(mbox, phba->mbox_mem_pool);
  11059. return status;
  11060. }
  11061. /**
  11062. * lpfc_cq_create - Create a Completion Queue on the HBA
  11063. * @phba: HBA structure that indicates port to create a queue on.
  11064. * @cq: The queue structure to use to create the completion queue.
  11065. * @eq: The event queue to bind this completion queue to.
  11066. *
  11067. * This function creates a completion queue, as detailed in @wq, on a port,
  11068. * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
  11069. *
  11070. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11071. * is used to get the entry count and entry size that are necessary to
  11072. * determine the number of pages to allocate and use for this queue. The @eq
  11073. * is used to indicate which event queue to bind this completion queue to. This
  11074. * function will send the CQ_CREATE mailbox command to the HBA to setup the
  11075. * completion queue. This function is asynchronous and will wait for the mailbox
  11076. * command to finish before continuing.
  11077. *
  11078. * On success this function will return a zero. If unable to allocate enough
  11079. * memory this function will return -ENOMEM. If the queue create mailbox command
  11080. * fails this function will return -ENXIO.
  11081. **/
  11082. uint32_t
  11083. lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
  11084. struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
  11085. {
  11086. struct lpfc_mbx_cq_create *cq_create;
  11087. struct lpfc_dmabuf *dmabuf;
  11088. LPFC_MBOXQ_t *mbox;
  11089. int rc, length, status = 0;
  11090. uint32_t shdr_status, shdr_add_status;
  11091. union lpfc_sli4_cfg_shdr *shdr;
  11092. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11093. /* sanity check on queue memory */
  11094. if (!cq || !eq)
  11095. return -ENODEV;
  11096. if (!phba->sli4_hba.pc_sli4_params.supported)
  11097. hw_page_size = SLI4_PAGE_SIZE;
  11098. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11099. if (!mbox)
  11100. return -ENOMEM;
  11101. length = (sizeof(struct lpfc_mbx_cq_create) -
  11102. sizeof(struct lpfc_sli4_cfg_mhdr));
  11103. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11104. LPFC_MBOX_OPCODE_CQ_CREATE,
  11105. length, LPFC_SLI4_MBX_EMBED);
  11106. cq_create = &mbox->u.mqe.un.cq_create;
  11107. shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
  11108. bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
  11109. cq->page_count);
  11110. bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
  11111. bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
  11112. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11113. phba->sli4_hba.pc_sli4_params.cqv);
  11114. if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
  11115. /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
  11116. bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
  11117. bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
  11118. eq->queue_id);
  11119. } else {
  11120. bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
  11121. eq->queue_id);
  11122. }
  11123. switch (cq->entry_count) {
  11124. default:
  11125. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11126. "0361 Unsupported CQ count. (%d)\n",
  11127. cq->entry_count);
  11128. if (cq->entry_count < 256)
  11129. return -EINVAL;
  11130. /* otherwise default to smallest count (drop through) */
  11131. case 256:
  11132. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11133. LPFC_CQ_CNT_256);
  11134. break;
  11135. case 512:
  11136. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11137. LPFC_CQ_CNT_512);
  11138. break;
  11139. case 1024:
  11140. bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
  11141. LPFC_CQ_CNT_1024);
  11142. break;
  11143. }
  11144. list_for_each_entry(dmabuf, &cq->page_list, list) {
  11145. memset(dmabuf->virt, 0, hw_page_size);
  11146. cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11147. putPaddrLow(dmabuf->phys);
  11148. cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11149. putPaddrHigh(dmabuf->phys);
  11150. }
  11151. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11152. /* The IOCTL status is embedded in the mailbox subheader. */
  11153. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11154. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11155. if (shdr_status || shdr_add_status || rc) {
  11156. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11157. "2501 CQ_CREATE mailbox failed with "
  11158. "status x%x add_status x%x, mbx status x%x\n",
  11159. shdr_status, shdr_add_status, rc);
  11160. status = -ENXIO;
  11161. goto out;
  11162. }
  11163. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11164. if (cq->queue_id == 0xFFFF) {
  11165. status = -ENXIO;
  11166. goto out;
  11167. }
  11168. /* link the cq onto the parent eq child list */
  11169. list_add_tail(&cq->list, &eq->child_list);
  11170. /* Set up completion queue's type and subtype */
  11171. cq->type = type;
  11172. cq->subtype = subtype;
  11173. cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
  11174. cq->assoc_qid = eq->queue_id;
  11175. cq->host_index = 0;
  11176. cq->hba_index = 0;
  11177. out:
  11178. mempool_free(mbox, phba->mbox_mem_pool);
  11179. return status;
  11180. }
  11181. /**
  11182. * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
  11183. * @phba: HBA structure that indicates port to create a queue on.
  11184. * @mq: The queue structure to use to create the mailbox queue.
  11185. * @mbox: An allocated pointer to type LPFC_MBOXQ_t
  11186. * @cq: The completion queue to associate with this cq.
  11187. *
  11188. * This function provides failback (fb) functionality when the
  11189. * mq_create_ext fails on older FW generations. It's purpose is identical
  11190. * to mq_create_ext otherwise.
  11191. *
  11192. * This routine cannot fail as all attributes were previously accessed and
  11193. * initialized in mq_create_ext.
  11194. **/
  11195. static void
  11196. lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11197. LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
  11198. {
  11199. struct lpfc_mbx_mq_create *mq_create;
  11200. struct lpfc_dmabuf *dmabuf;
  11201. int length;
  11202. length = (sizeof(struct lpfc_mbx_mq_create) -
  11203. sizeof(struct lpfc_sli4_cfg_mhdr));
  11204. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11205. LPFC_MBOX_OPCODE_MQ_CREATE,
  11206. length, LPFC_SLI4_MBX_EMBED);
  11207. mq_create = &mbox->u.mqe.un.mq_create;
  11208. bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
  11209. mq->page_count);
  11210. bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
  11211. cq->queue_id);
  11212. bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
  11213. switch (mq->entry_count) {
  11214. case 16:
  11215. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11216. LPFC_MQ_RING_SIZE_16);
  11217. break;
  11218. case 32:
  11219. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11220. LPFC_MQ_RING_SIZE_32);
  11221. break;
  11222. case 64:
  11223. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11224. LPFC_MQ_RING_SIZE_64);
  11225. break;
  11226. case 128:
  11227. bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
  11228. LPFC_MQ_RING_SIZE_128);
  11229. break;
  11230. }
  11231. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11232. mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11233. putPaddrLow(dmabuf->phys);
  11234. mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11235. putPaddrHigh(dmabuf->phys);
  11236. }
  11237. }
  11238. /**
  11239. * lpfc_mq_create - Create a mailbox Queue on the HBA
  11240. * @phba: HBA structure that indicates port to create a queue on.
  11241. * @mq: The queue structure to use to create the mailbox queue.
  11242. * @cq: The completion queue to associate with this cq.
  11243. * @subtype: The queue's subtype.
  11244. *
  11245. * This function creates a mailbox queue, as detailed in @mq, on a port,
  11246. * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
  11247. *
  11248. * The @phba struct is used to send mailbox command to HBA. The @cq struct
  11249. * is used to get the entry count and entry size that are necessary to
  11250. * determine the number of pages to allocate and use for this queue. This
  11251. * function will send the MQ_CREATE mailbox command to the HBA to setup the
  11252. * mailbox queue. This function is asynchronous and will wait for the mailbox
  11253. * command to finish before continuing.
  11254. *
  11255. * On success this function will return a zero. If unable to allocate enough
  11256. * memory this function will return -ENOMEM. If the queue create mailbox command
  11257. * fails this function will return -ENXIO.
  11258. **/
  11259. int32_t
  11260. lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
  11261. struct lpfc_queue *cq, uint32_t subtype)
  11262. {
  11263. struct lpfc_mbx_mq_create *mq_create;
  11264. struct lpfc_mbx_mq_create_ext *mq_create_ext;
  11265. struct lpfc_dmabuf *dmabuf;
  11266. LPFC_MBOXQ_t *mbox;
  11267. int rc, length, status = 0;
  11268. uint32_t shdr_status, shdr_add_status;
  11269. union lpfc_sli4_cfg_shdr *shdr;
  11270. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11271. /* sanity check on queue memory */
  11272. if (!mq || !cq)
  11273. return -ENODEV;
  11274. if (!phba->sli4_hba.pc_sli4_params.supported)
  11275. hw_page_size = SLI4_PAGE_SIZE;
  11276. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11277. if (!mbox)
  11278. return -ENOMEM;
  11279. length = (sizeof(struct lpfc_mbx_mq_create_ext) -
  11280. sizeof(struct lpfc_sli4_cfg_mhdr));
  11281. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11282. LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
  11283. length, LPFC_SLI4_MBX_EMBED);
  11284. mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
  11285. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
  11286. bf_set(lpfc_mbx_mq_create_ext_num_pages,
  11287. &mq_create_ext->u.request, mq->page_count);
  11288. bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
  11289. &mq_create_ext->u.request, 1);
  11290. bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
  11291. &mq_create_ext->u.request, 1);
  11292. bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
  11293. &mq_create_ext->u.request, 1);
  11294. bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
  11295. &mq_create_ext->u.request, 1);
  11296. bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
  11297. &mq_create_ext->u.request, 1);
  11298. bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
  11299. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11300. phba->sli4_hba.pc_sli4_params.mqv);
  11301. if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
  11302. bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
  11303. cq->queue_id);
  11304. else
  11305. bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
  11306. cq->queue_id);
  11307. switch (mq->entry_count) {
  11308. default:
  11309. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11310. "0362 Unsupported MQ count. (%d)\n",
  11311. mq->entry_count);
  11312. if (mq->entry_count < 16)
  11313. return -EINVAL;
  11314. /* otherwise default to smallest count (drop through) */
  11315. case 16:
  11316. bf_set(lpfc_mq_context_ring_size,
  11317. &mq_create_ext->u.request.context,
  11318. LPFC_MQ_RING_SIZE_16);
  11319. break;
  11320. case 32:
  11321. bf_set(lpfc_mq_context_ring_size,
  11322. &mq_create_ext->u.request.context,
  11323. LPFC_MQ_RING_SIZE_32);
  11324. break;
  11325. case 64:
  11326. bf_set(lpfc_mq_context_ring_size,
  11327. &mq_create_ext->u.request.context,
  11328. LPFC_MQ_RING_SIZE_64);
  11329. break;
  11330. case 128:
  11331. bf_set(lpfc_mq_context_ring_size,
  11332. &mq_create_ext->u.request.context,
  11333. LPFC_MQ_RING_SIZE_128);
  11334. break;
  11335. }
  11336. list_for_each_entry(dmabuf, &mq->page_list, list) {
  11337. memset(dmabuf->virt, 0, hw_page_size);
  11338. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
  11339. putPaddrLow(dmabuf->phys);
  11340. mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
  11341. putPaddrHigh(dmabuf->phys);
  11342. }
  11343. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11344. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11345. &mq_create_ext->u.response);
  11346. if (rc != MBX_SUCCESS) {
  11347. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  11348. "2795 MQ_CREATE_EXT failed with "
  11349. "status x%x. Failback to MQ_CREATE.\n",
  11350. rc);
  11351. lpfc_mq_create_fb_init(phba, mq, mbox, cq);
  11352. mq_create = &mbox->u.mqe.un.mq_create;
  11353. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11354. shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
  11355. mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
  11356. &mq_create->u.response);
  11357. }
  11358. /* The IOCTL status is embedded in the mailbox subheader. */
  11359. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11360. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11361. if (shdr_status || shdr_add_status || rc) {
  11362. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11363. "2502 MQ_CREATE mailbox failed with "
  11364. "status x%x add_status x%x, mbx status x%x\n",
  11365. shdr_status, shdr_add_status, rc);
  11366. status = -ENXIO;
  11367. goto out;
  11368. }
  11369. if (mq->queue_id == 0xFFFF) {
  11370. status = -ENXIO;
  11371. goto out;
  11372. }
  11373. mq->type = LPFC_MQ;
  11374. mq->assoc_qid = cq->queue_id;
  11375. mq->subtype = subtype;
  11376. mq->host_index = 0;
  11377. mq->hba_index = 0;
  11378. /* link the mq onto the parent cq child list */
  11379. list_add_tail(&mq->list, &cq->child_list);
  11380. out:
  11381. mempool_free(mbox, phba->mbox_mem_pool);
  11382. return status;
  11383. }
  11384. /**
  11385. * lpfc_wq_create - Create a Work Queue on the HBA
  11386. * @phba: HBA structure that indicates port to create a queue on.
  11387. * @wq: The queue structure to use to create the work queue.
  11388. * @cq: The completion queue to bind this work queue to.
  11389. * @subtype: The subtype of the work queue indicating its functionality.
  11390. *
  11391. * This function creates a work queue, as detailed in @wq, on a port, described
  11392. * by @phba by sending a WQ_CREATE mailbox command to the HBA.
  11393. *
  11394. * The @phba struct is used to send mailbox command to HBA. The @wq struct
  11395. * is used to get the entry count and entry size that are necessary to
  11396. * determine the number of pages to allocate and use for this queue. The @cq
  11397. * is used to indicate which completion queue to bind this work queue to. This
  11398. * function will send the WQ_CREATE mailbox command to the HBA to setup the
  11399. * work queue. This function is asynchronous and will wait for the mailbox
  11400. * command to finish before continuing.
  11401. *
  11402. * On success this function will return a zero. If unable to allocate enough
  11403. * memory this function will return -ENOMEM. If the queue create mailbox command
  11404. * fails this function will return -ENXIO.
  11405. **/
  11406. uint32_t
  11407. lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
  11408. struct lpfc_queue *cq, uint32_t subtype)
  11409. {
  11410. struct lpfc_mbx_wq_create *wq_create;
  11411. struct lpfc_dmabuf *dmabuf;
  11412. LPFC_MBOXQ_t *mbox;
  11413. int rc, length, status = 0;
  11414. uint32_t shdr_status, shdr_add_status;
  11415. union lpfc_sli4_cfg_shdr *shdr;
  11416. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11417. struct dma_address *page;
  11418. /* sanity check on queue memory */
  11419. if (!wq || !cq)
  11420. return -ENODEV;
  11421. if (!phba->sli4_hba.pc_sli4_params.supported)
  11422. hw_page_size = SLI4_PAGE_SIZE;
  11423. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11424. if (!mbox)
  11425. return -ENOMEM;
  11426. length = (sizeof(struct lpfc_mbx_wq_create) -
  11427. sizeof(struct lpfc_sli4_cfg_mhdr));
  11428. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11429. LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
  11430. length, LPFC_SLI4_MBX_EMBED);
  11431. wq_create = &mbox->u.mqe.un.wq_create;
  11432. shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
  11433. bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
  11434. wq->page_count);
  11435. bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
  11436. cq->queue_id);
  11437. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11438. phba->sli4_hba.pc_sli4_params.wqv);
  11439. if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
  11440. bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
  11441. wq->entry_count);
  11442. switch (wq->entry_size) {
  11443. default:
  11444. case 64:
  11445. bf_set(lpfc_mbx_wq_create_wqe_size,
  11446. &wq_create->u.request_1,
  11447. LPFC_WQ_WQE_SIZE_64);
  11448. break;
  11449. case 128:
  11450. bf_set(lpfc_mbx_wq_create_wqe_size,
  11451. &wq_create->u.request_1,
  11452. LPFC_WQ_WQE_SIZE_128);
  11453. break;
  11454. }
  11455. bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
  11456. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11457. page = wq_create->u.request_1.page;
  11458. } else {
  11459. page = wq_create->u.request.page;
  11460. }
  11461. list_for_each_entry(dmabuf, &wq->page_list, list) {
  11462. memset(dmabuf->virt, 0, hw_page_size);
  11463. page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
  11464. page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
  11465. }
  11466. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11467. /* The IOCTL status is embedded in the mailbox subheader. */
  11468. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11469. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11470. if (shdr_status || shdr_add_status || rc) {
  11471. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11472. "2503 WQ_CREATE mailbox failed with "
  11473. "status x%x add_status x%x, mbx status x%x\n",
  11474. shdr_status, shdr_add_status, rc);
  11475. status = -ENXIO;
  11476. goto out;
  11477. }
  11478. wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
  11479. if (wq->queue_id == 0xFFFF) {
  11480. status = -ENXIO;
  11481. goto out;
  11482. }
  11483. wq->type = LPFC_WQ;
  11484. wq->assoc_qid = cq->queue_id;
  11485. wq->subtype = subtype;
  11486. wq->host_index = 0;
  11487. wq->hba_index = 0;
  11488. wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
  11489. /* link the wq onto the parent cq child list */
  11490. list_add_tail(&wq->list, &cq->child_list);
  11491. out:
  11492. mempool_free(mbox, phba->mbox_mem_pool);
  11493. return status;
  11494. }
  11495. /**
  11496. * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
  11497. * @phba: HBA structure that indicates port to create a queue on.
  11498. * @rq: The queue structure to use for the receive queue.
  11499. * @qno: The associated HBQ number
  11500. *
  11501. *
  11502. * For SLI4 we need to adjust the RQ repost value based on
  11503. * the number of buffers that are initially posted to the RQ.
  11504. */
  11505. void
  11506. lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
  11507. {
  11508. uint32_t cnt;
  11509. /* sanity check on queue memory */
  11510. if (!rq)
  11511. return;
  11512. cnt = lpfc_hbq_defs[qno]->entry_count;
  11513. /* Recalc repost for RQs based on buffers initially posted */
  11514. cnt = (cnt >> 3);
  11515. if (cnt < LPFC_QUEUE_MIN_REPOST)
  11516. cnt = LPFC_QUEUE_MIN_REPOST;
  11517. rq->entry_repost = cnt;
  11518. }
  11519. /**
  11520. * lpfc_rq_create - Create a Receive Queue on the HBA
  11521. * @phba: HBA structure that indicates port to create a queue on.
  11522. * @hrq: The queue structure to use to create the header receive queue.
  11523. * @drq: The queue structure to use to create the data receive queue.
  11524. * @cq: The completion queue to bind this work queue to.
  11525. *
  11526. * This function creates a receive buffer queue pair , as detailed in @hrq and
  11527. * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
  11528. * to the HBA.
  11529. *
  11530. * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
  11531. * struct is used to get the entry count that is necessary to determine the
  11532. * number of pages to use for this queue. The @cq is used to indicate which
  11533. * completion queue to bind received buffers that are posted to these queues to.
  11534. * This function will send the RQ_CREATE mailbox command to the HBA to setup the
  11535. * receive queue pair. This function is asynchronous and will wait for the
  11536. * mailbox command to finish before continuing.
  11537. *
  11538. * On success this function will return a zero. If unable to allocate enough
  11539. * memory this function will return -ENOMEM. If the queue create mailbox command
  11540. * fails this function will return -ENXIO.
  11541. **/
  11542. uint32_t
  11543. lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11544. struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
  11545. {
  11546. struct lpfc_mbx_rq_create *rq_create;
  11547. struct lpfc_dmabuf *dmabuf;
  11548. LPFC_MBOXQ_t *mbox;
  11549. int rc, length, status = 0;
  11550. uint32_t shdr_status, shdr_add_status;
  11551. union lpfc_sli4_cfg_shdr *shdr;
  11552. uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
  11553. /* sanity check on queue memory */
  11554. if (!hrq || !drq || !cq)
  11555. return -ENODEV;
  11556. if (!phba->sli4_hba.pc_sli4_params.supported)
  11557. hw_page_size = SLI4_PAGE_SIZE;
  11558. if (hrq->entry_count != drq->entry_count)
  11559. return -EINVAL;
  11560. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  11561. if (!mbox)
  11562. return -ENOMEM;
  11563. length = (sizeof(struct lpfc_mbx_rq_create) -
  11564. sizeof(struct lpfc_sli4_cfg_mhdr));
  11565. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11566. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11567. length, LPFC_SLI4_MBX_EMBED);
  11568. rq_create = &mbox->u.mqe.un.rq_create;
  11569. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11570. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11571. phba->sli4_hba.pc_sli4_params.rqv);
  11572. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11573. bf_set(lpfc_rq_context_rqe_count_1,
  11574. &rq_create->u.request.context,
  11575. hrq->entry_count);
  11576. rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
  11577. bf_set(lpfc_rq_context_rqe_size,
  11578. &rq_create->u.request.context,
  11579. LPFC_RQE_SIZE_8);
  11580. bf_set(lpfc_rq_context_page_size,
  11581. &rq_create->u.request.context,
  11582. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11583. } else {
  11584. switch (hrq->entry_count) {
  11585. default:
  11586. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11587. "2535 Unsupported RQ count. (%d)\n",
  11588. hrq->entry_count);
  11589. if (hrq->entry_count < 512)
  11590. return -EINVAL;
  11591. /* otherwise default to smallest count (drop through) */
  11592. case 512:
  11593. bf_set(lpfc_rq_context_rqe_count,
  11594. &rq_create->u.request.context,
  11595. LPFC_RQ_RING_SIZE_512);
  11596. break;
  11597. case 1024:
  11598. bf_set(lpfc_rq_context_rqe_count,
  11599. &rq_create->u.request.context,
  11600. LPFC_RQ_RING_SIZE_1024);
  11601. break;
  11602. case 2048:
  11603. bf_set(lpfc_rq_context_rqe_count,
  11604. &rq_create->u.request.context,
  11605. LPFC_RQ_RING_SIZE_2048);
  11606. break;
  11607. case 4096:
  11608. bf_set(lpfc_rq_context_rqe_count,
  11609. &rq_create->u.request.context,
  11610. LPFC_RQ_RING_SIZE_4096);
  11611. break;
  11612. }
  11613. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11614. LPFC_HDR_BUF_SIZE);
  11615. }
  11616. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11617. cq->queue_id);
  11618. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11619. hrq->page_count);
  11620. list_for_each_entry(dmabuf, &hrq->page_list, list) {
  11621. memset(dmabuf->virt, 0, hw_page_size);
  11622. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11623. putPaddrLow(dmabuf->phys);
  11624. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11625. putPaddrHigh(dmabuf->phys);
  11626. }
  11627. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11628. /* The IOCTL status is embedded in the mailbox subheader. */
  11629. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11630. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11631. if (shdr_status || shdr_add_status || rc) {
  11632. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11633. "2504 RQ_CREATE mailbox failed with "
  11634. "status x%x add_status x%x, mbx status x%x\n",
  11635. shdr_status, shdr_add_status, rc);
  11636. status = -ENXIO;
  11637. goto out;
  11638. }
  11639. hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11640. if (hrq->queue_id == 0xFFFF) {
  11641. status = -ENXIO;
  11642. goto out;
  11643. }
  11644. hrq->type = LPFC_HRQ;
  11645. hrq->assoc_qid = cq->queue_id;
  11646. hrq->subtype = subtype;
  11647. hrq->host_index = 0;
  11648. hrq->hba_index = 0;
  11649. /* now create the data queue */
  11650. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11651. LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
  11652. length, LPFC_SLI4_MBX_EMBED);
  11653. bf_set(lpfc_mbox_hdr_version, &shdr->request,
  11654. phba->sli4_hba.pc_sli4_params.rqv);
  11655. if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
  11656. bf_set(lpfc_rq_context_rqe_count_1,
  11657. &rq_create->u.request.context, hrq->entry_count);
  11658. rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
  11659. bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
  11660. LPFC_RQE_SIZE_8);
  11661. bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
  11662. (PAGE_SIZE/SLI4_PAGE_SIZE));
  11663. } else {
  11664. switch (drq->entry_count) {
  11665. default:
  11666. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  11667. "2536 Unsupported RQ count. (%d)\n",
  11668. drq->entry_count);
  11669. if (drq->entry_count < 512)
  11670. return -EINVAL;
  11671. /* otherwise default to smallest count (drop through) */
  11672. case 512:
  11673. bf_set(lpfc_rq_context_rqe_count,
  11674. &rq_create->u.request.context,
  11675. LPFC_RQ_RING_SIZE_512);
  11676. break;
  11677. case 1024:
  11678. bf_set(lpfc_rq_context_rqe_count,
  11679. &rq_create->u.request.context,
  11680. LPFC_RQ_RING_SIZE_1024);
  11681. break;
  11682. case 2048:
  11683. bf_set(lpfc_rq_context_rqe_count,
  11684. &rq_create->u.request.context,
  11685. LPFC_RQ_RING_SIZE_2048);
  11686. break;
  11687. case 4096:
  11688. bf_set(lpfc_rq_context_rqe_count,
  11689. &rq_create->u.request.context,
  11690. LPFC_RQ_RING_SIZE_4096);
  11691. break;
  11692. }
  11693. bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
  11694. LPFC_DATA_BUF_SIZE);
  11695. }
  11696. bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
  11697. cq->queue_id);
  11698. bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
  11699. drq->page_count);
  11700. list_for_each_entry(dmabuf, &drq->page_list, list) {
  11701. rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
  11702. putPaddrLow(dmabuf->phys);
  11703. rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
  11704. putPaddrHigh(dmabuf->phys);
  11705. }
  11706. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  11707. /* The IOCTL status is embedded in the mailbox subheader. */
  11708. shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
  11709. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11710. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11711. if (shdr_status || shdr_add_status || rc) {
  11712. status = -ENXIO;
  11713. goto out;
  11714. }
  11715. drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
  11716. if (drq->queue_id == 0xFFFF) {
  11717. status = -ENXIO;
  11718. goto out;
  11719. }
  11720. drq->type = LPFC_DRQ;
  11721. drq->assoc_qid = cq->queue_id;
  11722. drq->subtype = subtype;
  11723. drq->host_index = 0;
  11724. drq->hba_index = 0;
  11725. /* link the header and data RQs onto the parent cq child list */
  11726. list_add_tail(&hrq->list, &cq->child_list);
  11727. list_add_tail(&drq->list, &cq->child_list);
  11728. out:
  11729. mempool_free(mbox, phba->mbox_mem_pool);
  11730. return status;
  11731. }
  11732. /**
  11733. * lpfc_eq_destroy - Destroy an event Queue on the HBA
  11734. * @eq: The queue structure associated with the queue to destroy.
  11735. *
  11736. * This function destroys a queue, as detailed in @eq by sending an mailbox
  11737. * command, specific to the type of queue, to the HBA.
  11738. *
  11739. * The @eq struct is used to get the queue ID of the queue to destroy.
  11740. *
  11741. * On success this function will return a zero. If the queue destroy mailbox
  11742. * command fails this function will return -ENXIO.
  11743. **/
  11744. uint32_t
  11745. lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
  11746. {
  11747. LPFC_MBOXQ_t *mbox;
  11748. int rc, length, status = 0;
  11749. uint32_t shdr_status, shdr_add_status;
  11750. union lpfc_sli4_cfg_shdr *shdr;
  11751. /* sanity check on queue memory */
  11752. if (!eq)
  11753. return -ENODEV;
  11754. mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
  11755. if (!mbox)
  11756. return -ENOMEM;
  11757. length = (sizeof(struct lpfc_mbx_eq_destroy) -
  11758. sizeof(struct lpfc_sli4_cfg_mhdr));
  11759. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11760. LPFC_MBOX_OPCODE_EQ_DESTROY,
  11761. length, LPFC_SLI4_MBX_EMBED);
  11762. bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
  11763. eq->queue_id);
  11764. mbox->vport = eq->phba->pport;
  11765. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11766. rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
  11767. /* The IOCTL status is embedded in the mailbox subheader. */
  11768. shdr = (union lpfc_sli4_cfg_shdr *)
  11769. &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
  11770. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11771. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11772. if (shdr_status || shdr_add_status || rc) {
  11773. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11774. "2505 EQ_DESTROY mailbox failed with "
  11775. "status x%x add_status x%x, mbx status x%x\n",
  11776. shdr_status, shdr_add_status, rc);
  11777. status = -ENXIO;
  11778. }
  11779. /* Remove eq from any list */
  11780. list_del_init(&eq->list);
  11781. mempool_free(mbox, eq->phba->mbox_mem_pool);
  11782. return status;
  11783. }
  11784. /**
  11785. * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
  11786. * @cq: The queue structure associated with the queue to destroy.
  11787. *
  11788. * This function destroys a queue, as detailed in @cq by sending an mailbox
  11789. * command, specific to the type of queue, to the HBA.
  11790. *
  11791. * The @cq struct is used to get the queue ID of the queue to destroy.
  11792. *
  11793. * On success this function will return a zero. If the queue destroy mailbox
  11794. * command fails this function will return -ENXIO.
  11795. **/
  11796. uint32_t
  11797. lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
  11798. {
  11799. LPFC_MBOXQ_t *mbox;
  11800. int rc, length, status = 0;
  11801. uint32_t shdr_status, shdr_add_status;
  11802. union lpfc_sli4_cfg_shdr *shdr;
  11803. /* sanity check on queue memory */
  11804. if (!cq)
  11805. return -ENODEV;
  11806. mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
  11807. if (!mbox)
  11808. return -ENOMEM;
  11809. length = (sizeof(struct lpfc_mbx_cq_destroy) -
  11810. sizeof(struct lpfc_sli4_cfg_mhdr));
  11811. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11812. LPFC_MBOX_OPCODE_CQ_DESTROY,
  11813. length, LPFC_SLI4_MBX_EMBED);
  11814. bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
  11815. cq->queue_id);
  11816. mbox->vport = cq->phba->pport;
  11817. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11818. rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
  11819. /* The IOCTL status is embedded in the mailbox subheader. */
  11820. shdr = (union lpfc_sli4_cfg_shdr *)
  11821. &mbox->u.mqe.un.wq_create.header.cfg_shdr;
  11822. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11823. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11824. if (shdr_status || shdr_add_status || rc) {
  11825. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11826. "2506 CQ_DESTROY mailbox failed with "
  11827. "status x%x add_status x%x, mbx status x%x\n",
  11828. shdr_status, shdr_add_status, rc);
  11829. status = -ENXIO;
  11830. }
  11831. /* Remove cq from any list */
  11832. list_del_init(&cq->list);
  11833. mempool_free(mbox, cq->phba->mbox_mem_pool);
  11834. return status;
  11835. }
  11836. /**
  11837. * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
  11838. * @qm: The queue structure associated with the queue to destroy.
  11839. *
  11840. * This function destroys a queue, as detailed in @mq by sending an mailbox
  11841. * command, specific to the type of queue, to the HBA.
  11842. *
  11843. * The @mq struct is used to get the queue ID of the queue to destroy.
  11844. *
  11845. * On success this function will return a zero. If the queue destroy mailbox
  11846. * command fails this function will return -ENXIO.
  11847. **/
  11848. uint32_t
  11849. lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
  11850. {
  11851. LPFC_MBOXQ_t *mbox;
  11852. int rc, length, status = 0;
  11853. uint32_t shdr_status, shdr_add_status;
  11854. union lpfc_sli4_cfg_shdr *shdr;
  11855. /* sanity check on queue memory */
  11856. if (!mq)
  11857. return -ENODEV;
  11858. mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
  11859. if (!mbox)
  11860. return -ENOMEM;
  11861. length = (sizeof(struct lpfc_mbx_mq_destroy) -
  11862. sizeof(struct lpfc_sli4_cfg_mhdr));
  11863. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  11864. LPFC_MBOX_OPCODE_MQ_DESTROY,
  11865. length, LPFC_SLI4_MBX_EMBED);
  11866. bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
  11867. mq->queue_id);
  11868. mbox->vport = mq->phba->pport;
  11869. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11870. rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
  11871. /* The IOCTL status is embedded in the mailbox subheader. */
  11872. shdr = (union lpfc_sli4_cfg_shdr *)
  11873. &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
  11874. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11875. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11876. if (shdr_status || shdr_add_status || rc) {
  11877. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11878. "2507 MQ_DESTROY mailbox failed with "
  11879. "status x%x add_status x%x, mbx status x%x\n",
  11880. shdr_status, shdr_add_status, rc);
  11881. status = -ENXIO;
  11882. }
  11883. /* Remove mq from any list */
  11884. list_del_init(&mq->list);
  11885. mempool_free(mbox, mq->phba->mbox_mem_pool);
  11886. return status;
  11887. }
  11888. /**
  11889. * lpfc_wq_destroy - Destroy a Work Queue on the HBA
  11890. * @wq: The queue structure associated with the queue to destroy.
  11891. *
  11892. * This function destroys a queue, as detailed in @wq by sending an mailbox
  11893. * command, specific to the type of queue, to the HBA.
  11894. *
  11895. * The @wq struct is used to get the queue ID of the queue to destroy.
  11896. *
  11897. * On success this function will return a zero. If the queue destroy mailbox
  11898. * command fails this function will return -ENXIO.
  11899. **/
  11900. uint32_t
  11901. lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
  11902. {
  11903. LPFC_MBOXQ_t *mbox;
  11904. int rc, length, status = 0;
  11905. uint32_t shdr_status, shdr_add_status;
  11906. union lpfc_sli4_cfg_shdr *shdr;
  11907. /* sanity check on queue memory */
  11908. if (!wq)
  11909. return -ENODEV;
  11910. mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
  11911. if (!mbox)
  11912. return -ENOMEM;
  11913. length = (sizeof(struct lpfc_mbx_wq_destroy) -
  11914. sizeof(struct lpfc_sli4_cfg_mhdr));
  11915. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11916. LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
  11917. length, LPFC_SLI4_MBX_EMBED);
  11918. bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
  11919. wq->queue_id);
  11920. mbox->vport = wq->phba->pport;
  11921. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11922. rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
  11923. shdr = (union lpfc_sli4_cfg_shdr *)
  11924. &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
  11925. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11926. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11927. if (shdr_status || shdr_add_status || rc) {
  11928. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11929. "2508 WQ_DESTROY mailbox failed with "
  11930. "status x%x add_status x%x, mbx status x%x\n",
  11931. shdr_status, shdr_add_status, rc);
  11932. status = -ENXIO;
  11933. }
  11934. /* Remove wq from any list */
  11935. list_del_init(&wq->list);
  11936. mempool_free(mbox, wq->phba->mbox_mem_pool);
  11937. return status;
  11938. }
  11939. /**
  11940. * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
  11941. * @rq: The queue structure associated with the queue to destroy.
  11942. *
  11943. * This function destroys a queue, as detailed in @rq by sending an mailbox
  11944. * command, specific to the type of queue, to the HBA.
  11945. *
  11946. * The @rq struct is used to get the queue ID of the queue to destroy.
  11947. *
  11948. * On success this function will return a zero. If the queue destroy mailbox
  11949. * command fails this function will return -ENXIO.
  11950. **/
  11951. uint32_t
  11952. lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
  11953. struct lpfc_queue *drq)
  11954. {
  11955. LPFC_MBOXQ_t *mbox;
  11956. int rc, length, status = 0;
  11957. uint32_t shdr_status, shdr_add_status;
  11958. union lpfc_sli4_cfg_shdr *shdr;
  11959. /* sanity check on queue memory */
  11960. if (!hrq || !drq)
  11961. return -ENODEV;
  11962. mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
  11963. if (!mbox)
  11964. return -ENOMEM;
  11965. length = (sizeof(struct lpfc_mbx_rq_destroy) -
  11966. sizeof(struct lpfc_sli4_cfg_mhdr));
  11967. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  11968. LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
  11969. length, LPFC_SLI4_MBX_EMBED);
  11970. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11971. hrq->queue_id);
  11972. mbox->vport = hrq->phba->pport;
  11973. mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  11974. rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
  11975. /* The IOCTL status is embedded in the mailbox subheader. */
  11976. shdr = (union lpfc_sli4_cfg_shdr *)
  11977. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11978. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11979. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11980. if (shdr_status || shdr_add_status || rc) {
  11981. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11982. "2509 RQ_DESTROY mailbox failed with "
  11983. "status x%x add_status x%x, mbx status x%x\n",
  11984. shdr_status, shdr_add_status, rc);
  11985. if (rc != MBX_TIMEOUT)
  11986. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  11987. return -ENXIO;
  11988. }
  11989. bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
  11990. drq->queue_id);
  11991. rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
  11992. shdr = (union lpfc_sli4_cfg_shdr *)
  11993. &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
  11994. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  11995. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  11996. if (shdr_status || shdr_add_status || rc) {
  11997. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  11998. "2510 RQ_DESTROY mailbox failed with "
  11999. "status x%x add_status x%x, mbx status x%x\n",
  12000. shdr_status, shdr_add_status, rc);
  12001. status = -ENXIO;
  12002. }
  12003. list_del_init(&hrq->list);
  12004. list_del_init(&drq->list);
  12005. mempool_free(mbox, hrq->phba->mbox_mem_pool);
  12006. return status;
  12007. }
  12008. /**
  12009. * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
  12010. * @phba: The virtual port for which this call being executed.
  12011. * @pdma_phys_addr0: Physical address of the 1st SGL page.
  12012. * @pdma_phys_addr1: Physical address of the 2nd SGL page.
  12013. * @xritag: the xritag that ties this io to the SGL pages.
  12014. *
  12015. * This routine will post the sgl pages for the IO that has the xritag
  12016. * that is in the iocbq structure. The xritag is assigned during iocbq
  12017. * creation and persists for as long as the driver is loaded.
  12018. * if the caller has fewer than 256 scatter gather segments to map then
  12019. * pdma_phys_addr1 should be 0.
  12020. * If the caller needs to map more than 256 scatter gather segment then
  12021. * pdma_phys_addr1 should be a valid physical address.
  12022. * physical address for SGLs must be 64 byte aligned.
  12023. * If you are going to map 2 SGL's then the first one must have 256 entries
  12024. * the second sgl can have between 1 and 256 entries.
  12025. *
  12026. * Return codes:
  12027. * 0 - Success
  12028. * -ENXIO, -ENOMEM - Failure
  12029. **/
  12030. int
  12031. lpfc_sli4_post_sgl(struct lpfc_hba *phba,
  12032. dma_addr_t pdma_phys_addr0,
  12033. dma_addr_t pdma_phys_addr1,
  12034. uint16_t xritag)
  12035. {
  12036. struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
  12037. LPFC_MBOXQ_t *mbox;
  12038. int rc;
  12039. uint32_t shdr_status, shdr_add_status;
  12040. uint32_t mbox_tmo;
  12041. union lpfc_sli4_cfg_shdr *shdr;
  12042. if (xritag == NO_XRI) {
  12043. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12044. "0364 Invalid param:\n");
  12045. return -EINVAL;
  12046. }
  12047. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12048. if (!mbox)
  12049. return -ENOMEM;
  12050. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12051. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
  12052. sizeof(struct lpfc_mbx_post_sgl_pages) -
  12053. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  12054. post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
  12055. &mbox->u.mqe.un.post_sgl_pages;
  12056. bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
  12057. bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
  12058. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
  12059. cpu_to_le32(putPaddrLow(pdma_phys_addr0));
  12060. post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
  12061. cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
  12062. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
  12063. cpu_to_le32(putPaddrLow(pdma_phys_addr1));
  12064. post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
  12065. cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
  12066. if (!phba->sli4_hba.intr_enable)
  12067. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12068. else {
  12069. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12070. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12071. }
  12072. /* The IOCTL status is embedded in the mailbox subheader. */
  12073. shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
  12074. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12075. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12076. if (rc != MBX_TIMEOUT)
  12077. mempool_free(mbox, phba->mbox_mem_pool);
  12078. if (shdr_status || shdr_add_status || rc) {
  12079. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12080. "2511 POST_SGL mailbox failed with "
  12081. "status x%x add_status x%x, mbx status x%x\n",
  12082. shdr_status, shdr_add_status, rc);
  12083. rc = -ENXIO;
  12084. }
  12085. return 0;
  12086. }
  12087. /**
  12088. * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
  12089. * @phba: pointer to lpfc hba data structure.
  12090. *
  12091. * This routine is invoked to post rpi header templates to the
  12092. * HBA consistent with the SLI-4 interface spec. This routine
  12093. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  12094. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  12095. *
  12096. * Returns
  12097. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  12098. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  12099. **/
  12100. uint16_t
  12101. lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
  12102. {
  12103. unsigned long xri;
  12104. /*
  12105. * Fetch the next logical xri. Because this index is logical,
  12106. * the driver starts at 0 each time.
  12107. */
  12108. spin_lock_irq(&phba->hbalock);
  12109. xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
  12110. phba->sli4_hba.max_cfg_param.max_xri, 0);
  12111. if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
  12112. spin_unlock_irq(&phba->hbalock);
  12113. return NO_XRI;
  12114. } else {
  12115. set_bit(xri, phba->sli4_hba.xri_bmask);
  12116. phba->sli4_hba.max_cfg_param.xri_used++;
  12117. }
  12118. spin_unlock_irq(&phba->hbalock);
  12119. return xri;
  12120. }
  12121. /**
  12122. * lpfc_sli4_free_xri - Release an xri for reuse.
  12123. * @phba: pointer to lpfc hba data structure.
  12124. *
  12125. * This routine is invoked to release an xri to the pool of
  12126. * available rpis maintained by the driver.
  12127. **/
  12128. void
  12129. __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12130. {
  12131. if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
  12132. phba->sli4_hba.max_cfg_param.xri_used--;
  12133. }
  12134. }
  12135. /**
  12136. * lpfc_sli4_free_xri - Release an xri for reuse.
  12137. * @phba: pointer to lpfc hba data structure.
  12138. *
  12139. * This routine is invoked to release an xri to the pool of
  12140. * available rpis maintained by the driver.
  12141. **/
  12142. void
  12143. lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
  12144. {
  12145. spin_lock_irq(&phba->hbalock);
  12146. __lpfc_sli4_free_xri(phba, xri);
  12147. spin_unlock_irq(&phba->hbalock);
  12148. }
  12149. /**
  12150. * lpfc_sli4_next_xritag - Get an xritag for the io
  12151. * @phba: Pointer to HBA context object.
  12152. *
  12153. * This function gets an xritag for the iocb. If there is no unused xritag
  12154. * it will return 0xffff.
  12155. * The function returns the allocated xritag if successful, else returns zero.
  12156. * Zero is not a valid xritag.
  12157. * The caller is not required to hold any lock.
  12158. **/
  12159. uint16_t
  12160. lpfc_sli4_next_xritag(struct lpfc_hba *phba)
  12161. {
  12162. uint16_t xri_index;
  12163. xri_index = lpfc_sli4_alloc_xri(phba);
  12164. if (xri_index == NO_XRI)
  12165. lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
  12166. "2004 Failed to allocate XRI.last XRITAG is %d"
  12167. " Max XRI is %d, Used XRI is %d\n",
  12168. xri_index,
  12169. phba->sli4_hba.max_cfg_param.max_xri,
  12170. phba->sli4_hba.max_cfg_param.xri_used);
  12171. return xri_index;
  12172. }
  12173. /**
  12174. * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
  12175. * @phba: pointer to lpfc hba data structure.
  12176. * @post_sgl_list: pointer to els sgl entry list.
  12177. * @count: number of els sgl entries on the list.
  12178. *
  12179. * This routine is invoked to post a block of driver's sgl pages to the
  12180. * HBA using non-embedded mailbox command. No Lock is held. This routine
  12181. * is only called when the driver is loading and after all IO has been
  12182. * stopped.
  12183. **/
  12184. static int
  12185. lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
  12186. struct list_head *post_sgl_list,
  12187. int post_cnt)
  12188. {
  12189. struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
  12190. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12191. struct sgl_page_pairs *sgl_pg_pairs;
  12192. void *viraddr;
  12193. LPFC_MBOXQ_t *mbox;
  12194. uint32_t reqlen, alloclen, pg_pairs;
  12195. uint32_t mbox_tmo;
  12196. uint16_t xritag_start = 0;
  12197. int rc = 0;
  12198. uint32_t shdr_status, shdr_add_status;
  12199. union lpfc_sli4_cfg_shdr *shdr;
  12200. reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
  12201. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12202. if (reqlen > SLI4_PAGE_SIZE) {
  12203. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12204. "2559 Block sgl registration required DMA "
  12205. "size (%d) great than a page\n", reqlen);
  12206. return -ENOMEM;
  12207. }
  12208. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12209. if (!mbox)
  12210. return -ENOMEM;
  12211. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12212. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12213. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12214. LPFC_SLI4_MBX_NEMBED);
  12215. if (alloclen < reqlen) {
  12216. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12217. "0285 Allocated DMA memory size (%d) is "
  12218. "less than the requested DMA memory "
  12219. "size (%d)\n", alloclen, reqlen);
  12220. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12221. return -ENOMEM;
  12222. }
  12223. /* Set up the SGL pages in the non-embedded DMA pages */
  12224. viraddr = mbox->sge_array->addr[0];
  12225. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12226. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12227. pg_pairs = 0;
  12228. list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
  12229. /* Set up the sge entry */
  12230. sgl_pg_pairs->sgl_pg0_addr_lo =
  12231. cpu_to_le32(putPaddrLow(sglq_entry->phys));
  12232. sgl_pg_pairs->sgl_pg0_addr_hi =
  12233. cpu_to_le32(putPaddrHigh(sglq_entry->phys));
  12234. sgl_pg_pairs->sgl_pg1_addr_lo =
  12235. cpu_to_le32(putPaddrLow(0));
  12236. sgl_pg_pairs->sgl_pg1_addr_hi =
  12237. cpu_to_le32(putPaddrHigh(0));
  12238. /* Keep the first xritag on the list */
  12239. if (pg_pairs == 0)
  12240. xritag_start = sglq_entry->sli4_xritag;
  12241. sgl_pg_pairs++;
  12242. pg_pairs++;
  12243. }
  12244. /* Complete initialization and perform endian conversion. */
  12245. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12246. bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
  12247. sgl->word0 = cpu_to_le32(sgl->word0);
  12248. if (!phba->sli4_hba.intr_enable)
  12249. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12250. else {
  12251. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12252. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12253. }
  12254. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12255. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12256. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12257. if (rc != MBX_TIMEOUT)
  12258. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12259. if (shdr_status || shdr_add_status || rc) {
  12260. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12261. "2513 POST_SGL_BLOCK mailbox command failed "
  12262. "status x%x add_status x%x mbx status x%x\n",
  12263. shdr_status, shdr_add_status, rc);
  12264. rc = -ENXIO;
  12265. }
  12266. return rc;
  12267. }
  12268. /**
  12269. * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
  12270. * @phba: pointer to lpfc hba data structure.
  12271. * @sblist: pointer to scsi buffer list.
  12272. * @count: number of scsi buffers on the list.
  12273. *
  12274. * This routine is invoked to post a block of @count scsi sgl pages from a
  12275. * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
  12276. * No Lock is held.
  12277. *
  12278. **/
  12279. int
  12280. lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
  12281. struct list_head *sblist,
  12282. int count)
  12283. {
  12284. struct lpfc_scsi_buf *psb;
  12285. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  12286. struct sgl_page_pairs *sgl_pg_pairs;
  12287. void *viraddr;
  12288. LPFC_MBOXQ_t *mbox;
  12289. uint32_t reqlen, alloclen, pg_pairs;
  12290. uint32_t mbox_tmo;
  12291. uint16_t xritag_start = 0;
  12292. int rc = 0;
  12293. uint32_t shdr_status, shdr_add_status;
  12294. dma_addr_t pdma_phys_bpl1;
  12295. union lpfc_sli4_cfg_shdr *shdr;
  12296. /* Calculate the requested length of the dma memory */
  12297. reqlen = count * sizeof(struct sgl_page_pairs) +
  12298. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  12299. if (reqlen > SLI4_PAGE_SIZE) {
  12300. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  12301. "0217 Block sgl registration required DMA "
  12302. "size (%d) great than a page\n", reqlen);
  12303. return -ENOMEM;
  12304. }
  12305. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  12306. if (!mbox) {
  12307. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12308. "0283 Failed to allocate mbox cmd memory\n");
  12309. return -ENOMEM;
  12310. }
  12311. /* Allocate DMA memory and set up the non-embedded mailbox command */
  12312. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  12313. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  12314. LPFC_SLI4_MBX_NEMBED);
  12315. if (alloclen < reqlen) {
  12316. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  12317. "2561 Allocated DMA memory size (%d) is "
  12318. "less than the requested DMA memory "
  12319. "size (%d)\n", alloclen, reqlen);
  12320. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12321. return -ENOMEM;
  12322. }
  12323. /* Get the first SGE entry from the non-embedded DMA memory */
  12324. viraddr = mbox->sge_array->addr[0];
  12325. /* Set up the SGL pages in the non-embedded DMA pages */
  12326. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  12327. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  12328. pg_pairs = 0;
  12329. list_for_each_entry(psb, sblist, list) {
  12330. /* Set up the sge entry */
  12331. sgl_pg_pairs->sgl_pg0_addr_lo =
  12332. cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
  12333. sgl_pg_pairs->sgl_pg0_addr_hi =
  12334. cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
  12335. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  12336. pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
  12337. else
  12338. pdma_phys_bpl1 = 0;
  12339. sgl_pg_pairs->sgl_pg1_addr_lo =
  12340. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  12341. sgl_pg_pairs->sgl_pg1_addr_hi =
  12342. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  12343. /* Keep the first xritag on the list */
  12344. if (pg_pairs == 0)
  12345. xritag_start = psb->cur_iocbq.sli4_xritag;
  12346. sgl_pg_pairs++;
  12347. pg_pairs++;
  12348. }
  12349. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  12350. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  12351. /* Perform endian conversion if necessary */
  12352. sgl->word0 = cpu_to_le32(sgl->word0);
  12353. if (!phba->sli4_hba.intr_enable)
  12354. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  12355. else {
  12356. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  12357. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  12358. }
  12359. shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
  12360. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  12361. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  12362. if (rc != MBX_TIMEOUT)
  12363. lpfc_sli4_mbox_cmd_free(phba, mbox);
  12364. if (shdr_status || shdr_add_status || rc) {
  12365. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12366. "2564 POST_SGL_BLOCK mailbox command failed "
  12367. "status x%x add_status x%x mbx status x%x\n",
  12368. shdr_status, shdr_add_status, rc);
  12369. rc = -ENXIO;
  12370. }
  12371. return rc;
  12372. }
  12373. /**
  12374. * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
  12375. * @phba: pointer to lpfc_hba struct that the frame was received on
  12376. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12377. *
  12378. * This function checks the fields in the @fc_hdr to see if the FC frame is a
  12379. * valid type of frame that the LPFC driver will handle. This function will
  12380. * return a zero if the frame is a valid frame or a non zero value when the
  12381. * frame does not pass the check.
  12382. **/
  12383. static int
  12384. lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
  12385. {
  12386. /* make rctl_names static to save stack space */
  12387. static char *rctl_names[] = FC_RCTL_NAMES_INIT;
  12388. char *type_names[] = FC_TYPE_NAMES_INIT;
  12389. struct fc_vft_header *fc_vft_hdr;
  12390. uint32_t *header = (uint32_t *) fc_hdr;
  12391. switch (fc_hdr->fh_r_ctl) {
  12392. case FC_RCTL_DD_UNCAT: /* uncategorized information */
  12393. case FC_RCTL_DD_SOL_DATA: /* solicited data */
  12394. case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
  12395. case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
  12396. case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
  12397. case FC_RCTL_DD_DATA_DESC: /* data descriptor */
  12398. case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
  12399. case FC_RCTL_DD_CMD_STATUS: /* command status */
  12400. case FC_RCTL_ELS_REQ: /* extended link services request */
  12401. case FC_RCTL_ELS_REP: /* extended link services reply */
  12402. case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
  12403. case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
  12404. case FC_RCTL_BA_NOP: /* basic link service NOP */
  12405. case FC_RCTL_BA_ABTS: /* basic link service abort */
  12406. case FC_RCTL_BA_RMC: /* remove connection */
  12407. case FC_RCTL_BA_ACC: /* basic accept */
  12408. case FC_RCTL_BA_RJT: /* basic reject */
  12409. case FC_RCTL_BA_PRMT:
  12410. case FC_RCTL_ACK_1: /* acknowledge_1 */
  12411. case FC_RCTL_ACK_0: /* acknowledge_0 */
  12412. case FC_RCTL_P_RJT: /* port reject */
  12413. case FC_RCTL_F_RJT: /* fabric reject */
  12414. case FC_RCTL_P_BSY: /* port busy */
  12415. case FC_RCTL_F_BSY: /* fabric busy to data frame */
  12416. case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
  12417. case FC_RCTL_LCR: /* link credit reset */
  12418. case FC_RCTL_END: /* end */
  12419. break;
  12420. case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
  12421. fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12422. fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
  12423. return lpfc_fc_frame_check(phba, fc_hdr);
  12424. default:
  12425. goto drop;
  12426. }
  12427. switch (fc_hdr->fh_type) {
  12428. case FC_TYPE_BLS:
  12429. case FC_TYPE_ELS:
  12430. case FC_TYPE_FCP:
  12431. case FC_TYPE_CT:
  12432. break;
  12433. case FC_TYPE_IP:
  12434. case FC_TYPE_ILS:
  12435. default:
  12436. goto drop;
  12437. }
  12438. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12439. "2538 Received frame rctl:%s type:%s "
  12440. "Frame Data:%08x %08x %08x %08x %08x %08x\n",
  12441. rctl_names[fc_hdr->fh_r_ctl],
  12442. type_names[fc_hdr->fh_type],
  12443. be32_to_cpu(header[0]), be32_to_cpu(header[1]),
  12444. be32_to_cpu(header[2]), be32_to_cpu(header[3]),
  12445. be32_to_cpu(header[4]), be32_to_cpu(header[5]));
  12446. return 0;
  12447. drop:
  12448. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12449. "2539 Dropped frame rctl:%s type:%s\n",
  12450. rctl_names[fc_hdr->fh_r_ctl],
  12451. type_names[fc_hdr->fh_type]);
  12452. return 1;
  12453. }
  12454. /**
  12455. * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
  12456. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12457. *
  12458. * This function processes the FC header to retrieve the VFI from the VF
  12459. * header, if one exists. This function will return the VFI if one exists
  12460. * or 0 if no VSAN Header exists.
  12461. **/
  12462. static uint32_t
  12463. lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
  12464. {
  12465. struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
  12466. if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
  12467. return 0;
  12468. return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
  12469. }
  12470. /**
  12471. * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
  12472. * @phba: Pointer to the HBA structure to search for the vport on
  12473. * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
  12474. * @fcfi: The FC Fabric ID that the frame came from
  12475. *
  12476. * This function searches the @phba for a vport that matches the content of the
  12477. * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
  12478. * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
  12479. * returns the matching vport pointer or NULL if unable to match frame to a
  12480. * vport.
  12481. **/
  12482. static struct lpfc_vport *
  12483. lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
  12484. uint16_t fcfi)
  12485. {
  12486. struct lpfc_vport **vports;
  12487. struct lpfc_vport *vport = NULL;
  12488. int i;
  12489. uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
  12490. fc_hdr->fh_d_id[1] << 8 |
  12491. fc_hdr->fh_d_id[2]);
  12492. if (did == Fabric_DID)
  12493. return phba->pport;
  12494. vports = lpfc_create_vport_work_array(phba);
  12495. if (vports != NULL)
  12496. for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
  12497. if (phba->fcf.fcfi == fcfi &&
  12498. vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
  12499. vports[i]->fc_myDID == did) {
  12500. vport = vports[i];
  12501. break;
  12502. }
  12503. }
  12504. lpfc_destroy_vport_work_array(phba, vports);
  12505. return vport;
  12506. }
  12507. /**
  12508. * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
  12509. * @vport: The vport to work on.
  12510. *
  12511. * This function updates the receive sequence time stamp for this vport. The
  12512. * receive sequence time stamp indicates the time that the last frame of the
  12513. * the sequence that has been idle for the longest amount of time was received.
  12514. * the driver uses this time stamp to indicate if any received sequences have
  12515. * timed out.
  12516. **/
  12517. void
  12518. lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
  12519. {
  12520. struct lpfc_dmabuf *h_buf;
  12521. struct hbq_dmabuf *dmabuf = NULL;
  12522. /* get the oldest sequence on the rcv list */
  12523. h_buf = list_get_first(&vport->rcv_buffer_list,
  12524. struct lpfc_dmabuf, list);
  12525. if (!h_buf)
  12526. return;
  12527. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12528. vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
  12529. }
  12530. /**
  12531. * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
  12532. * @vport: The vport that the received sequences were sent to.
  12533. *
  12534. * This function cleans up all outstanding received sequences. This is called
  12535. * by the driver when a link event or user action invalidates all the received
  12536. * sequences.
  12537. **/
  12538. void
  12539. lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
  12540. {
  12541. struct lpfc_dmabuf *h_buf, *hnext;
  12542. struct lpfc_dmabuf *d_buf, *dnext;
  12543. struct hbq_dmabuf *dmabuf = NULL;
  12544. /* start with the oldest sequence on the rcv list */
  12545. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12546. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12547. list_del_init(&dmabuf->hbuf.list);
  12548. list_for_each_entry_safe(d_buf, dnext,
  12549. &dmabuf->dbuf.list, list) {
  12550. list_del_init(&d_buf->list);
  12551. lpfc_in_buf_free(vport->phba, d_buf);
  12552. }
  12553. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12554. }
  12555. }
  12556. /**
  12557. * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
  12558. * @vport: The vport that the received sequences were sent to.
  12559. *
  12560. * This function determines whether any received sequences have timed out by
  12561. * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
  12562. * indicates that there is at least one timed out sequence this routine will
  12563. * go through the received sequences one at a time from most inactive to most
  12564. * active to determine which ones need to be cleaned up. Once it has determined
  12565. * that a sequence needs to be cleaned up it will simply free up the resources
  12566. * without sending an abort.
  12567. **/
  12568. void
  12569. lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
  12570. {
  12571. struct lpfc_dmabuf *h_buf, *hnext;
  12572. struct lpfc_dmabuf *d_buf, *dnext;
  12573. struct hbq_dmabuf *dmabuf = NULL;
  12574. unsigned long timeout;
  12575. int abort_count = 0;
  12576. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12577. vport->rcv_buffer_time_stamp);
  12578. if (list_empty(&vport->rcv_buffer_list) ||
  12579. time_before(jiffies, timeout))
  12580. return;
  12581. /* start with the oldest sequence on the rcv list */
  12582. list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
  12583. dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12584. timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
  12585. dmabuf->time_stamp);
  12586. if (time_before(jiffies, timeout))
  12587. break;
  12588. abort_count++;
  12589. list_del_init(&dmabuf->hbuf.list);
  12590. list_for_each_entry_safe(d_buf, dnext,
  12591. &dmabuf->dbuf.list, list) {
  12592. list_del_init(&d_buf->list);
  12593. lpfc_in_buf_free(vport->phba, d_buf);
  12594. }
  12595. lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
  12596. }
  12597. if (abort_count)
  12598. lpfc_update_rcv_time_stamp(vport);
  12599. }
  12600. /**
  12601. * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
  12602. * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
  12603. *
  12604. * This function searches through the existing incomplete sequences that have
  12605. * been sent to this @vport. If the frame matches one of the incomplete
  12606. * sequences then the dbuf in the @dmabuf is added to the list of frames that
  12607. * make up that sequence. If no sequence is found that matches this frame then
  12608. * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
  12609. * This function returns a pointer to the first dmabuf in the sequence list that
  12610. * the frame was linked to.
  12611. **/
  12612. static struct hbq_dmabuf *
  12613. lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
  12614. {
  12615. struct fc_frame_header *new_hdr;
  12616. struct fc_frame_header *temp_hdr;
  12617. struct lpfc_dmabuf *d_buf;
  12618. struct lpfc_dmabuf *h_buf;
  12619. struct hbq_dmabuf *seq_dmabuf = NULL;
  12620. struct hbq_dmabuf *temp_dmabuf = NULL;
  12621. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12622. dmabuf->time_stamp = jiffies;
  12623. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12624. /* Use the hdr_buf to find the sequence that this frame belongs to */
  12625. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12626. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12627. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12628. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12629. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12630. continue;
  12631. /* found a pending sequence that matches this frame */
  12632. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12633. break;
  12634. }
  12635. if (!seq_dmabuf) {
  12636. /*
  12637. * This indicates first frame received for this sequence.
  12638. * Queue the buffer on the vport's rcv_buffer_list.
  12639. */
  12640. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12641. lpfc_update_rcv_time_stamp(vport);
  12642. return dmabuf;
  12643. }
  12644. temp_hdr = seq_dmabuf->hbuf.virt;
  12645. if (be16_to_cpu(new_hdr->fh_seq_cnt) <
  12646. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12647. list_del_init(&seq_dmabuf->hbuf.list);
  12648. list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12649. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12650. lpfc_update_rcv_time_stamp(vport);
  12651. return dmabuf;
  12652. }
  12653. /* move this sequence to the tail to indicate a young sequence */
  12654. list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
  12655. seq_dmabuf->time_stamp = jiffies;
  12656. lpfc_update_rcv_time_stamp(vport);
  12657. if (list_empty(&seq_dmabuf->dbuf.list)) {
  12658. temp_hdr = dmabuf->hbuf.virt;
  12659. list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
  12660. return seq_dmabuf;
  12661. }
  12662. /* find the correct place in the sequence to insert this frame */
  12663. list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
  12664. temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12665. temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
  12666. /*
  12667. * If the frame's sequence count is greater than the frame on
  12668. * the list then insert the frame right after this frame
  12669. */
  12670. if (be16_to_cpu(new_hdr->fh_seq_cnt) >
  12671. be16_to_cpu(temp_hdr->fh_seq_cnt)) {
  12672. list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
  12673. return seq_dmabuf;
  12674. }
  12675. }
  12676. return NULL;
  12677. }
  12678. /**
  12679. * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
  12680. * @vport: pointer to a vitural port
  12681. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12682. *
  12683. * This function tries to abort from the partially assembed sequence, described
  12684. * by the information from basic abbort @dmabuf. It checks to see whether such
  12685. * partially assembled sequence held by the driver. If so, it shall free up all
  12686. * the frames from the partially assembled sequence.
  12687. *
  12688. * Return
  12689. * true -- if there is matching partially assembled sequence present and all
  12690. * the frames freed with the sequence;
  12691. * false -- if there is no matching partially assembled sequence present so
  12692. * nothing got aborted in the lower layer driver
  12693. **/
  12694. static bool
  12695. lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
  12696. struct hbq_dmabuf *dmabuf)
  12697. {
  12698. struct fc_frame_header *new_hdr;
  12699. struct fc_frame_header *temp_hdr;
  12700. struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
  12701. struct hbq_dmabuf *seq_dmabuf = NULL;
  12702. /* Use the hdr_buf to find the sequence that matches this frame */
  12703. INIT_LIST_HEAD(&dmabuf->dbuf.list);
  12704. INIT_LIST_HEAD(&dmabuf->hbuf.list);
  12705. new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12706. list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
  12707. temp_hdr = (struct fc_frame_header *)h_buf->virt;
  12708. if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
  12709. (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
  12710. (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
  12711. continue;
  12712. /* found a pending sequence that matches this frame */
  12713. seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
  12714. break;
  12715. }
  12716. /* Free up all the frames from the partially assembled sequence */
  12717. if (seq_dmabuf) {
  12718. list_for_each_entry_safe(d_buf, n_buf,
  12719. &seq_dmabuf->dbuf.list, list) {
  12720. list_del_init(&d_buf->list);
  12721. lpfc_in_buf_free(vport->phba, d_buf);
  12722. }
  12723. return true;
  12724. }
  12725. return false;
  12726. }
  12727. /**
  12728. * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
  12729. * @phba: Pointer to HBA context object.
  12730. * @cmd_iocbq: pointer to the command iocbq structure.
  12731. * @rsp_iocbq: pointer to the response iocbq structure.
  12732. *
  12733. * This function handles the sequence abort response iocb command complete
  12734. * event. It properly releases the memory allocated to the sequence abort
  12735. * accept iocb.
  12736. **/
  12737. static void
  12738. lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
  12739. struct lpfc_iocbq *cmd_iocbq,
  12740. struct lpfc_iocbq *rsp_iocbq)
  12741. {
  12742. if (cmd_iocbq)
  12743. lpfc_sli_release_iocbq(phba, cmd_iocbq);
  12744. /* Failure means BLS ABORT RSP did not get delivered to remote node*/
  12745. if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
  12746. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  12747. "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
  12748. rsp_iocbq->iocb.ulpStatus,
  12749. rsp_iocbq->iocb.un.ulpWord[4]);
  12750. }
  12751. /**
  12752. * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
  12753. * @phba: Pointer to HBA context object.
  12754. * @xri: xri id in transaction.
  12755. *
  12756. * This function validates the xri maps to the known range of XRIs allocated an
  12757. * used by the driver.
  12758. **/
  12759. uint16_t
  12760. lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
  12761. uint16_t xri)
  12762. {
  12763. int i;
  12764. for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
  12765. if (xri == phba->sli4_hba.xri_ids[i])
  12766. return i;
  12767. }
  12768. return NO_XRI;
  12769. }
  12770. /**
  12771. * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
  12772. * @phba: Pointer to HBA context object.
  12773. * @fc_hdr: pointer to a FC frame header.
  12774. *
  12775. * This function sends a basic response to a previous unsol sequence abort
  12776. * event after aborting the sequence handling.
  12777. **/
  12778. static void
  12779. lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
  12780. struct fc_frame_header *fc_hdr)
  12781. {
  12782. struct lpfc_iocbq *ctiocb = NULL;
  12783. struct lpfc_nodelist *ndlp;
  12784. uint16_t oxid, rxid;
  12785. uint32_t sid, fctl;
  12786. IOCB_t *icmd;
  12787. int rc;
  12788. if (!lpfc_is_link_up(phba))
  12789. return;
  12790. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12791. oxid = be16_to_cpu(fc_hdr->fh_ox_id);
  12792. rxid = be16_to_cpu(fc_hdr->fh_rx_id);
  12793. ndlp = lpfc_findnode_did(phba->pport, sid);
  12794. if (!ndlp) {
  12795. lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
  12796. "1268 Find ndlp returned NULL for oxid:x%x "
  12797. "SID:x%x\n", oxid, sid);
  12798. return;
  12799. }
  12800. if (lpfc_sli4_xri_inrange(phba, rxid))
  12801. lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
  12802. /* Allocate buffer for rsp iocb */
  12803. ctiocb = lpfc_sli_get_iocbq(phba);
  12804. if (!ctiocb)
  12805. return;
  12806. /* Extract the F_CTL field from FC_HDR */
  12807. fctl = sli4_fctl_from_fc_hdr(fc_hdr);
  12808. icmd = &ctiocb->iocb;
  12809. icmd->un.xseq64.bdl.bdeSize = 0;
  12810. icmd->un.xseq64.bdl.ulpIoTag32 = 0;
  12811. icmd->un.xseq64.w5.hcsw.Dfctl = 0;
  12812. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
  12813. icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
  12814. /* Fill in the rest of iocb fields */
  12815. icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
  12816. icmd->ulpBdeCount = 0;
  12817. icmd->ulpLe = 1;
  12818. icmd->ulpClass = CLASS3;
  12819. icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  12820. ctiocb->context1 = ndlp;
  12821. ctiocb->iocb_cmpl = NULL;
  12822. ctiocb->vport = phba->pport;
  12823. ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
  12824. ctiocb->sli4_lxritag = NO_XRI;
  12825. ctiocb->sli4_xritag = NO_XRI;
  12826. /* If the oxid maps to the FCP XRI range or if it is out of range,
  12827. * send a BLS_RJT. The driver no longer has that exchange.
  12828. * Override the IOCB for a BA_RJT.
  12829. */
  12830. if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
  12831. phba->sli4_hba.max_cfg_param.xri_base) ||
  12832. oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
  12833. phba->sli4_hba.max_cfg_param.xri_base)) {
  12834. icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
  12835. bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
  12836. bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
  12837. bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
  12838. }
  12839. if (fctl & FC_FC_EX_CTX) {
  12840. /* ABTS sent by responder to CT exchange, construction
  12841. * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
  12842. * field and RX_ID from ABTS for RX_ID field.
  12843. */
  12844. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
  12845. } else {
  12846. /* ABTS sent by initiator to CT exchange, construction
  12847. * of BA_ACC will need to allocate a new XRI as for the
  12848. * XRI_TAG field.
  12849. */
  12850. bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
  12851. }
  12852. bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
  12853. bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
  12854. /* Xmit CT abts response on exchange <xid> */
  12855. lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
  12856. "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
  12857. icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
  12858. rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
  12859. if (rc == IOCB_ERROR) {
  12860. lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
  12861. "2925 Failed to issue CT ABTS RSP x%x on "
  12862. "xri x%x, Data x%x\n",
  12863. icmd->un.xseq64.w5.hcsw.Rctl, oxid,
  12864. phba->link_state);
  12865. lpfc_sli_release_iocbq(phba, ctiocb);
  12866. }
  12867. }
  12868. /**
  12869. * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
  12870. * @vport: Pointer to the vport on which this sequence was received
  12871. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12872. *
  12873. * This function handles an SLI-4 unsolicited abort event. If the unsolicited
  12874. * receive sequence is only partially assembed by the driver, it shall abort
  12875. * the partially assembled frames for the sequence. Otherwise, if the
  12876. * unsolicited receive sequence has been completely assembled and passed to
  12877. * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
  12878. * unsolicited sequence has been aborted. After that, it will issue a basic
  12879. * accept to accept the abort.
  12880. **/
  12881. void
  12882. lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
  12883. struct hbq_dmabuf *dmabuf)
  12884. {
  12885. struct lpfc_hba *phba = vport->phba;
  12886. struct fc_frame_header fc_hdr;
  12887. uint32_t fctl;
  12888. bool abts_par;
  12889. /* Make a copy of fc_hdr before the dmabuf being released */
  12890. memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
  12891. fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
  12892. if (fctl & FC_FC_EX_CTX) {
  12893. /*
  12894. * ABTS sent by responder to exchange, just free the buffer
  12895. */
  12896. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12897. } else {
  12898. /*
  12899. * ABTS sent by initiator to exchange, need to do cleanup
  12900. */
  12901. /* Try to abort partially assembled seq */
  12902. abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
  12903. /* Send abort to ULP if partially seq abort failed */
  12904. if (abts_par == false)
  12905. lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
  12906. else
  12907. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  12908. }
  12909. /* Send basic accept (BA_ACC) to the abort requester */
  12910. lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
  12911. }
  12912. /**
  12913. * lpfc_seq_complete - Indicates if a sequence is complete
  12914. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12915. *
  12916. * This function checks the sequence, starting with the frame described by
  12917. * @dmabuf, to see if all the frames associated with this sequence are present.
  12918. * the frames associated with this sequence are linked to the @dmabuf using the
  12919. * dbuf list. This function looks for two major things. 1) That the first frame
  12920. * has a sequence count of zero. 2) There is a frame with last frame of sequence
  12921. * set. 3) That there are no holes in the sequence count. The function will
  12922. * return 1 when the sequence is complete, otherwise it will return 0.
  12923. **/
  12924. static int
  12925. lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
  12926. {
  12927. struct fc_frame_header *hdr;
  12928. struct lpfc_dmabuf *d_buf;
  12929. struct hbq_dmabuf *seq_dmabuf;
  12930. uint32_t fctl;
  12931. int seq_count = 0;
  12932. hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  12933. /* make sure first fame of sequence has a sequence count of zero */
  12934. if (hdr->fh_seq_cnt != seq_count)
  12935. return 0;
  12936. fctl = (hdr->fh_f_ctl[0] << 16 |
  12937. hdr->fh_f_ctl[1] << 8 |
  12938. hdr->fh_f_ctl[2]);
  12939. /* If last frame of sequence we can return success. */
  12940. if (fctl & FC_FC_END_SEQ)
  12941. return 1;
  12942. list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
  12943. seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  12944. hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12945. /* If there is a hole in the sequence count then fail. */
  12946. if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
  12947. return 0;
  12948. fctl = (hdr->fh_f_ctl[0] << 16 |
  12949. hdr->fh_f_ctl[1] << 8 |
  12950. hdr->fh_f_ctl[2]);
  12951. /* If last frame of sequence we can return success. */
  12952. if (fctl & FC_FC_END_SEQ)
  12953. return 1;
  12954. }
  12955. return 0;
  12956. }
  12957. /**
  12958. * lpfc_prep_seq - Prep sequence for ULP processing
  12959. * @vport: Pointer to the vport on which this sequence was received
  12960. * @dmabuf: pointer to a dmabuf that describes the FC sequence
  12961. *
  12962. * This function takes a sequence, described by a list of frames, and creates
  12963. * a list of iocbq structures to describe the sequence. This iocbq list will be
  12964. * used to issue to the generic unsolicited sequence handler. This routine
  12965. * returns a pointer to the first iocbq in the list. If the function is unable
  12966. * to allocate an iocbq then it throw out the received frames that were not
  12967. * able to be described and return a pointer to the first iocbq. If unable to
  12968. * allocate any iocbqs (including the first) this function will return NULL.
  12969. **/
  12970. static struct lpfc_iocbq *
  12971. lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
  12972. {
  12973. struct hbq_dmabuf *hbq_buf;
  12974. struct lpfc_dmabuf *d_buf, *n_buf;
  12975. struct lpfc_iocbq *first_iocbq, *iocbq;
  12976. struct fc_frame_header *fc_hdr;
  12977. uint32_t sid;
  12978. uint32_t len, tot_len;
  12979. struct ulp_bde64 *pbde;
  12980. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  12981. /* remove from receive buffer list */
  12982. list_del_init(&seq_dmabuf->hbuf.list);
  12983. lpfc_update_rcv_time_stamp(vport);
  12984. /* get the Remote Port's SID */
  12985. sid = sli4_sid_from_fc_hdr(fc_hdr);
  12986. tot_len = 0;
  12987. /* Get an iocbq struct to fill in. */
  12988. first_iocbq = lpfc_sli_get_iocbq(vport->phba);
  12989. if (first_iocbq) {
  12990. /* Initialize the first IOCB. */
  12991. first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
  12992. first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
  12993. first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
  12994. first_iocbq->iocb.ulpContext = NO_XRI;
  12995. first_iocbq->iocb.unsli3.rcvsli3.ox_id =
  12996. be16_to_cpu(fc_hdr->fh_ox_id);
  12997. /* iocbq is prepped for internal consumption. Physical vpi. */
  12998. first_iocbq->iocb.unsli3.rcvsli3.vpi =
  12999. vport->phba->vpi_ids[vport->vpi];
  13000. /* put the first buffer into the first IOCBq */
  13001. first_iocbq->context2 = &seq_dmabuf->dbuf;
  13002. first_iocbq->context3 = NULL;
  13003. first_iocbq->iocb.ulpBdeCount = 1;
  13004. first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13005. LPFC_DATA_BUF_SIZE;
  13006. first_iocbq->iocb.un.rcvels.remoteID = sid;
  13007. tot_len = bf_get(lpfc_rcqe_length,
  13008. &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
  13009. first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13010. }
  13011. iocbq = first_iocbq;
  13012. /*
  13013. * Each IOCBq can have two Buffers assigned, so go through the list
  13014. * of buffers for this sequence and save two buffers in each IOCBq
  13015. */
  13016. list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
  13017. if (!iocbq) {
  13018. lpfc_in_buf_free(vport->phba, d_buf);
  13019. continue;
  13020. }
  13021. if (!iocbq->context3) {
  13022. iocbq->context3 = d_buf;
  13023. iocbq->iocb.ulpBdeCount++;
  13024. pbde = (struct ulp_bde64 *)
  13025. &iocbq->iocb.unsli3.sli3Words[4];
  13026. pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
  13027. /* We need to get the size out of the right CQE */
  13028. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13029. len = bf_get(lpfc_rcqe_length,
  13030. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13031. iocbq->iocb.unsli3.rcvsli3.acc_len += len;
  13032. tot_len += len;
  13033. } else {
  13034. iocbq = lpfc_sli_get_iocbq(vport->phba);
  13035. if (!iocbq) {
  13036. if (first_iocbq) {
  13037. first_iocbq->iocb.ulpStatus =
  13038. IOSTAT_FCP_RSP_ERROR;
  13039. first_iocbq->iocb.un.ulpWord[4] =
  13040. IOERR_NO_RESOURCES;
  13041. }
  13042. lpfc_in_buf_free(vport->phba, d_buf);
  13043. continue;
  13044. }
  13045. iocbq->context2 = d_buf;
  13046. iocbq->context3 = NULL;
  13047. iocbq->iocb.ulpBdeCount = 1;
  13048. iocbq->iocb.un.cont64[0].tus.f.bdeSize =
  13049. LPFC_DATA_BUF_SIZE;
  13050. /* We need to get the size out of the right CQE */
  13051. hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
  13052. len = bf_get(lpfc_rcqe_length,
  13053. &hbq_buf->cq_event.cqe.rcqe_cmpl);
  13054. tot_len += len;
  13055. iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
  13056. iocbq->iocb.un.rcvels.remoteID = sid;
  13057. list_add_tail(&iocbq->list, &first_iocbq->list);
  13058. }
  13059. }
  13060. return first_iocbq;
  13061. }
  13062. static void
  13063. lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
  13064. struct hbq_dmabuf *seq_dmabuf)
  13065. {
  13066. struct fc_frame_header *fc_hdr;
  13067. struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
  13068. struct lpfc_hba *phba = vport->phba;
  13069. fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
  13070. iocbq = lpfc_prep_seq(vport, seq_dmabuf);
  13071. if (!iocbq) {
  13072. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13073. "2707 Ring %d handler: Failed to allocate "
  13074. "iocb Rctl x%x Type x%x received\n",
  13075. LPFC_ELS_RING,
  13076. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13077. return;
  13078. }
  13079. if (!lpfc_complete_unsol_iocb(phba,
  13080. &phba->sli.ring[LPFC_ELS_RING],
  13081. iocbq, fc_hdr->fh_r_ctl,
  13082. fc_hdr->fh_type))
  13083. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13084. "2540 Ring %d handler: unexpected Rctl "
  13085. "x%x Type x%x received\n",
  13086. LPFC_ELS_RING,
  13087. fc_hdr->fh_r_ctl, fc_hdr->fh_type);
  13088. /* Free iocb created in lpfc_prep_seq */
  13089. list_for_each_entry_safe(curr_iocb, next_iocb,
  13090. &iocbq->list, list) {
  13091. list_del_init(&curr_iocb->list);
  13092. lpfc_sli_release_iocbq(phba, curr_iocb);
  13093. }
  13094. lpfc_sli_release_iocbq(phba, iocbq);
  13095. }
  13096. /**
  13097. * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
  13098. * @phba: Pointer to HBA context object.
  13099. *
  13100. * This function is called with no lock held. This function processes all
  13101. * the received buffers and gives it to upper layers when a received buffer
  13102. * indicates that it is the final frame in the sequence. The interrupt
  13103. * service routine processes received buffers at interrupt contexts and adds
  13104. * received dma buffers to the rb_pend_list queue and signals the worker thread.
  13105. * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
  13106. * appropriate receive function when the final frame in a sequence is received.
  13107. **/
  13108. void
  13109. lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
  13110. struct hbq_dmabuf *dmabuf)
  13111. {
  13112. struct hbq_dmabuf *seq_dmabuf;
  13113. struct fc_frame_header *fc_hdr;
  13114. struct lpfc_vport *vport;
  13115. uint32_t fcfi;
  13116. /* Process each received buffer */
  13117. fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
  13118. /* check to see if this a valid type of frame */
  13119. if (lpfc_fc_frame_check(phba, fc_hdr)) {
  13120. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13121. return;
  13122. }
  13123. if ((bf_get(lpfc_cqe_code,
  13124. &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
  13125. fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
  13126. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13127. else
  13128. fcfi = bf_get(lpfc_rcqe_fcf_id,
  13129. &dmabuf->cq_event.cqe.rcqe_cmpl);
  13130. vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
  13131. if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
  13132. /* throw out the frame */
  13133. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13134. return;
  13135. }
  13136. /* Handle the basic abort sequence (BA_ABTS) event */
  13137. if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
  13138. lpfc_sli4_handle_unsol_abort(vport, dmabuf);
  13139. return;
  13140. }
  13141. /* Link this frame */
  13142. seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
  13143. if (!seq_dmabuf) {
  13144. /* unable to add frame to vport - throw it out */
  13145. lpfc_in_buf_free(phba, &dmabuf->dbuf);
  13146. return;
  13147. }
  13148. /* If not last frame in sequence continue processing frames. */
  13149. if (!lpfc_seq_complete(seq_dmabuf))
  13150. return;
  13151. /* Send the complete sequence to the upper layer protocol */
  13152. lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
  13153. }
  13154. /**
  13155. * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
  13156. * @phba: pointer to lpfc hba data structure.
  13157. *
  13158. * This routine is invoked to post rpi header templates to the
  13159. * HBA consistent with the SLI-4 interface spec. This routine
  13160. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13161. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13162. *
  13163. * This routine does not require any locks. It's usage is expected
  13164. * to be driver load or reset recovery when the driver is
  13165. * sequential.
  13166. *
  13167. * Return codes
  13168. * 0 - successful
  13169. * -EIO - The mailbox failed to complete successfully.
  13170. * When this error occurs, the driver is not guaranteed
  13171. * to have any rpi regions posted to the device and
  13172. * must either attempt to repost the regions or take a
  13173. * fatal error.
  13174. **/
  13175. int
  13176. lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
  13177. {
  13178. struct lpfc_rpi_hdr *rpi_page;
  13179. uint32_t rc = 0;
  13180. uint16_t lrpi = 0;
  13181. /* SLI4 ports that support extents do not require RPI headers. */
  13182. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13183. goto exit;
  13184. if (phba->sli4_hba.extents_in_use)
  13185. return -EIO;
  13186. list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
  13187. /*
  13188. * Assign the rpi headers a physical rpi only if the driver
  13189. * has not initialized those resources. A port reset only
  13190. * needs the headers posted.
  13191. */
  13192. if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
  13193. LPFC_RPI_RSRC_RDY)
  13194. rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13195. rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
  13196. if (rc != MBX_SUCCESS) {
  13197. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13198. "2008 Error %d posting all rpi "
  13199. "headers\n", rc);
  13200. rc = -EIO;
  13201. break;
  13202. }
  13203. }
  13204. exit:
  13205. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
  13206. LPFC_RPI_RSRC_RDY);
  13207. return rc;
  13208. }
  13209. /**
  13210. * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
  13211. * @phba: pointer to lpfc hba data structure.
  13212. * @rpi_page: pointer to the rpi memory region.
  13213. *
  13214. * This routine is invoked to post a single rpi header to the
  13215. * HBA consistent with the SLI-4 interface spec. This memory region
  13216. * maps up to 64 rpi context regions.
  13217. *
  13218. * Return codes
  13219. * 0 - successful
  13220. * -ENOMEM - No available memory
  13221. * -EIO - The mailbox failed to complete successfully.
  13222. **/
  13223. int
  13224. lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
  13225. {
  13226. LPFC_MBOXQ_t *mboxq;
  13227. struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
  13228. uint32_t rc = 0;
  13229. uint32_t shdr_status, shdr_add_status;
  13230. union lpfc_sli4_cfg_shdr *shdr;
  13231. /* SLI4 ports that support extents do not require RPI headers. */
  13232. if (!phba->sli4_hba.rpi_hdrs_in_use)
  13233. return rc;
  13234. if (phba->sli4_hba.extents_in_use)
  13235. return -EIO;
  13236. /* The port is notified of the header region via a mailbox command. */
  13237. mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13238. if (!mboxq) {
  13239. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13240. "2001 Unable to allocate memory for issuing "
  13241. "SLI_CONFIG_SPECIAL mailbox command\n");
  13242. return -ENOMEM;
  13243. }
  13244. /* Post all rpi memory regions to the port. */
  13245. hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
  13246. lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13247. LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
  13248. sizeof(struct lpfc_mbx_post_hdr_tmpl) -
  13249. sizeof(struct lpfc_sli4_cfg_mhdr),
  13250. LPFC_SLI4_MBX_EMBED);
  13251. /* Post the physical rpi to the port for this rpi header. */
  13252. bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
  13253. rpi_page->start_rpi);
  13254. bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
  13255. hdr_tmpl, rpi_page->page_count);
  13256. hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
  13257. hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
  13258. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  13259. shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
  13260. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13261. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13262. if (rc != MBX_TIMEOUT)
  13263. mempool_free(mboxq, phba->mbox_mem_pool);
  13264. if (shdr_status || shdr_add_status || rc) {
  13265. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13266. "2514 POST_RPI_HDR mailbox failed with "
  13267. "status x%x add_status x%x, mbx status x%x\n",
  13268. shdr_status, shdr_add_status, rc);
  13269. rc = -ENXIO;
  13270. }
  13271. return rc;
  13272. }
  13273. /**
  13274. * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
  13275. * @phba: pointer to lpfc hba data structure.
  13276. *
  13277. * This routine is invoked to post rpi header templates to the
  13278. * HBA consistent with the SLI-4 interface spec. This routine
  13279. * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
  13280. * SLI4_PAGE_SIZE modulo 64 rpi context headers.
  13281. *
  13282. * Returns
  13283. * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
  13284. * LPFC_RPI_ALLOC_ERROR if no rpis are available.
  13285. **/
  13286. int
  13287. lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
  13288. {
  13289. unsigned long rpi;
  13290. uint16_t max_rpi, rpi_limit;
  13291. uint16_t rpi_remaining, lrpi = 0;
  13292. struct lpfc_rpi_hdr *rpi_hdr;
  13293. max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
  13294. rpi_limit = phba->sli4_hba.next_rpi;
  13295. /*
  13296. * Fetch the next logical rpi. Because this index is logical,
  13297. * the driver starts at 0 each time.
  13298. */
  13299. spin_lock_irq(&phba->hbalock);
  13300. rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
  13301. if (rpi >= rpi_limit)
  13302. rpi = LPFC_RPI_ALLOC_ERROR;
  13303. else {
  13304. set_bit(rpi, phba->sli4_hba.rpi_bmask);
  13305. phba->sli4_hba.max_cfg_param.rpi_used++;
  13306. phba->sli4_hba.rpi_count++;
  13307. }
  13308. /*
  13309. * Don't try to allocate more rpi header regions if the device limit
  13310. * has been exhausted.
  13311. */
  13312. if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
  13313. (phba->sli4_hba.rpi_count >= max_rpi)) {
  13314. spin_unlock_irq(&phba->hbalock);
  13315. return rpi;
  13316. }
  13317. /*
  13318. * RPI header postings are not required for SLI4 ports capable of
  13319. * extents.
  13320. */
  13321. if (!phba->sli4_hba.rpi_hdrs_in_use) {
  13322. spin_unlock_irq(&phba->hbalock);
  13323. return rpi;
  13324. }
  13325. /*
  13326. * If the driver is running low on rpi resources, allocate another
  13327. * page now. Note that the next_rpi value is used because
  13328. * it represents how many are actually in use whereas max_rpi notes
  13329. * how many are supported max by the device.
  13330. */
  13331. rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
  13332. spin_unlock_irq(&phba->hbalock);
  13333. if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
  13334. rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
  13335. if (!rpi_hdr) {
  13336. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13337. "2002 Error Could not grow rpi "
  13338. "count\n");
  13339. } else {
  13340. lrpi = rpi_hdr->start_rpi;
  13341. rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
  13342. lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
  13343. }
  13344. }
  13345. return rpi;
  13346. }
  13347. /**
  13348. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13349. * @phba: pointer to lpfc hba data structure.
  13350. *
  13351. * This routine is invoked to release an rpi to the pool of
  13352. * available rpis maintained by the driver.
  13353. **/
  13354. void
  13355. __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13356. {
  13357. if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
  13358. phba->sli4_hba.rpi_count--;
  13359. phba->sli4_hba.max_cfg_param.rpi_used--;
  13360. }
  13361. }
  13362. /**
  13363. * lpfc_sli4_free_rpi - Release an rpi for reuse.
  13364. * @phba: pointer to lpfc hba data structure.
  13365. *
  13366. * This routine is invoked to release an rpi to the pool of
  13367. * available rpis maintained by the driver.
  13368. **/
  13369. void
  13370. lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
  13371. {
  13372. spin_lock_irq(&phba->hbalock);
  13373. __lpfc_sli4_free_rpi(phba, rpi);
  13374. spin_unlock_irq(&phba->hbalock);
  13375. }
  13376. /**
  13377. * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
  13378. * @phba: pointer to lpfc hba data structure.
  13379. *
  13380. * This routine is invoked to remove the memory region that
  13381. * provided rpi via a bitmask.
  13382. **/
  13383. void
  13384. lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
  13385. {
  13386. kfree(phba->sli4_hba.rpi_bmask);
  13387. kfree(phba->sli4_hba.rpi_ids);
  13388. bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
  13389. }
  13390. /**
  13391. * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
  13392. * @phba: pointer to lpfc hba data structure.
  13393. *
  13394. * This routine is invoked to remove the memory region that
  13395. * provided rpi via a bitmask.
  13396. **/
  13397. int
  13398. lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
  13399. void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
  13400. {
  13401. LPFC_MBOXQ_t *mboxq;
  13402. struct lpfc_hba *phba = ndlp->phba;
  13403. int rc;
  13404. /* The port is notified of the header region via a mailbox command. */
  13405. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13406. if (!mboxq)
  13407. return -ENOMEM;
  13408. /* Post all rpi memory regions to the port. */
  13409. lpfc_resume_rpi(mboxq, ndlp);
  13410. if (cmpl) {
  13411. mboxq->mbox_cmpl = cmpl;
  13412. mboxq->context1 = arg;
  13413. mboxq->context2 = ndlp;
  13414. } else
  13415. mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  13416. mboxq->vport = ndlp->vport;
  13417. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13418. if (rc == MBX_NOT_FINISHED) {
  13419. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  13420. "2010 Resume RPI Mailbox failed "
  13421. "status %d, mbxStatus x%x\n", rc,
  13422. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13423. mempool_free(mboxq, phba->mbox_mem_pool);
  13424. return -EIO;
  13425. }
  13426. return 0;
  13427. }
  13428. /**
  13429. * lpfc_sli4_init_vpi - Initialize a vpi with the port
  13430. * @vport: Pointer to the vport for which the vpi is being initialized
  13431. *
  13432. * This routine is invoked to activate a vpi with the port.
  13433. *
  13434. * Returns:
  13435. * 0 success
  13436. * -Evalue otherwise
  13437. **/
  13438. int
  13439. lpfc_sli4_init_vpi(struct lpfc_vport *vport)
  13440. {
  13441. LPFC_MBOXQ_t *mboxq;
  13442. int rc = 0;
  13443. int retval = MBX_SUCCESS;
  13444. uint32_t mbox_tmo;
  13445. struct lpfc_hba *phba = vport->phba;
  13446. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13447. if (!mboxq)
  13448. return -ENOMEM;
  13449. lpfc_init_vpi(phba, mboxq, vport->vpi);
  13450. mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
  13451. rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
  13452. if (rc != MBX_SUCCESS) {
  13453. lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
  13454. "2022 INIT VPI Mailbox failed "
  13455. "status %d, mbxStatus x%x\n", rc,
  13456. bf_get(lpfc_mqe_status, &mboxq->u.mqe));
  13457. retval = -EIO;
  13458. }
  13459. if (rc != MBX_TIMEOUT)
  13460. mempool_free(mboxq, vport->phba->mbox_mem_pool);
  13461. return retval;
  13462. }
  13463. /**
  13464. * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
  13465. * @phba: pointer to lpfc hba data structure.
  13466. * @mboxq: Pointer to mailbox object.
  13467. *
  13468. * This routine is invoked to manually add a single FCF record. The caller
  13469. * must pass a completely initialized FCF_Record. This routine takes
  13470. * care of the nonembedded mailbox operations.
  13471. **/
  13472. static void
  13473. lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
  13474. {
  13475. void *virt_addr;
  13476. union lpfc_sli4_cfg_shdr *shdr;
  13477. uint32_t shdr_status, shdr_add_status;
  13478. virt_addr = mboxq->sge_array->addr[0];
  13479. /* The IOCTL status is embedded in the mailbox subheader. */
  13480. shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
  13481. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  13482. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  13483. if ((shdr_status || shdr_add_status) &&
  13484. (shdr_status != STATUS_FCF_IN_USE))
  13485. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13486. "2558 ADD_FCF_RECORD mailbox failed with "
  13487. "status x%x add_status x%x\n",
  13488. shdr_status, shdr_add_status);
  13489. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13490. }
  13491. /**
  13492. * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
  13493. * @phba: pointer to lpfc hba data structure.
  13494. * @fcf_record: pointer to the initialized fcf record to add.
  13495. *
  13496. * This routine is invoked to manually add a single FCF record. The caller
  13497. * must pass a completely initialized FCF_Record. This routine takes
  13498. * care of the nonembedded mailbox operations.
  13499. **/
  13500. int
  13501. lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
  13502. {
  13503. int rc = 0;
  13504. LPFC_MBOXQ_t *mboxq;
  13505. uint8_t *bytep;
  13506. void *virt_addr;
  13507. dma_addr_t phys_addr;
  13508. struct lpfc_mbx_sge sge;
  13509. uint32_t alloc_len, req_len;
  13510. uint32_t fcfindex;
  13511. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13512. if (!mboxq) {
  13513. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13514. "2009 Failed to allocate mbox for ADD_FCF cmd\n");
  13515. return -ENOMEM;
  13516. }
  13517. req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
  13518. sizeof(uint32_t);
  13519. /* Allocate DMA memory and set up the non-embedded mailbox command */
  13520. alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
  13521. LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
  13522. req_len, LPFC_SLI4_MBX_NEMBED);
  13523. if (alloc_len < req_len) {
  13524. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13525. "2523 Allocated DMA memory size (x%x) is "
  13526. "less than the requested DMA memory "
  13527. "size (x%x)\n", alloc_len, req_len);
  13528. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13529. return -ENOMEM;
  13530. }
  13531. /*
  13532. * Get the first SGE entry from the non-embedded DMA memory. This
  13533. * routine only uses a single SGE.
  13534. */
  13535. lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
  13536. phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
  13537. virt_addr = mboxq->sge_array->addr[0];
  13538. /*
  13539. * Configure the FCF record for FCFI 0. This is the driver's
  13540. * hardcoded default and gets used in nonFIP mode.
  13541. */
  13542. fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
  13543. bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
  13544. lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
  13545. /*
  13546. * Copy the fcf_index and the FCF Record Data. The data starts after
  13547. * the FCoE header plus word10. The data copy needs to be endian
  13548. * correct.
  13549. */
  13550. bytep += sizeof(uint32_t);
  13551. lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
  13552. mboxq->vport = phba->pport;
  13553. mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
  13554. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13555. if (rc == MBX_NOT_FINISHED) {
  13556. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13557. "2515 ADD_FCF_RECORD mailbox failed with "
  13558. "status 0x%x\n", rc);
  13559. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13560. rc = -EIO;
  13561. } else
  13562. rc = 0;
  13563. return rc;
  13564. }
  13565. /**
  13566. * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
  13567. * @phba: pointer to lpfc hba data structure.
  13568. * @fcf_record: pointer to the fcf record to write the default data.
  13569. * @fcf_index: FCF table entry index.
  13570. *
  13571. * This routine is invoked to build the driver's default FCF record. The
  13572. * values used are hardcoded. This routine handles memory initialization.
  13573. *
  13574. **/
  13575. void
  13576. lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
  13577. struct fcf_record *fcf_record,
  13578. uint16_t fcf_index)
  13579. {
  13580. memset(fcf_record, 0, sizeof(struct fcf_record));
  13581. fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
  13582. fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
  13583. fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
  13584. bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
  13585. bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
  13586. bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
  13587. bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
  13588. bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
  13589. bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
  13590. bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
  13591. bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
  13592. bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
  13593. bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
  13594. bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
  13595. bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
  13596. bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
  13597. LPFC_FCF_FPMA | LPFC_FCF_SPMA);
  13598. /* Set the VLAN bit map */
  13599. if (phba->valid_vlan) {
  13600. fcf_record->vlan_bitmap[phba->vlan_id / 8]
  13601. = 1 << (phba->vlan_id % 8);
  13602. }
  13603. }
  13604. /**
  13605. * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
  13606. * @phba: pointer to lpfc hba data structure.
  13607. * @fcf_index: FCF table entry offset.
  13608. *
  13609. * This routine is invoked to scan the entire FCF table by reading FCF
  13610. * record and processing it one at a time starting from the @fcf_index
  13611. * for initial FCF discovery or fast FCF failover rediscovery.
  13612. *
  13613. * Return 0 if the mailbox command is submitted successfully, none 0
  13614. * otherwise.
  13615. **/
  13616. int
  13617. lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13618. {
  13619. int rc = 0, error;
  13620. LPFC_MBOXQ_t *mboxq;
  13621. phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
  13622. phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
  13623. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13624. if (!mboxq) {
  13625. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  13626. "2000 Failed to allocate mbox for "
  13627. "READ_FCF cmd\n");
  13628. error = -ENOMEM;
  13629. goto fail_fcf_scan;
  13630. }
  13631. /* Construct the read FCF record mailbox command */
  13632. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13633. if (rc) {
  13634. error = -EINVAL;
  13635. goto fail_fcf_scan;
  13636. }
  13637. /* Issue the mailbox command asynchronously */
  13638. mboxq->vport = phba->pport;
  13639. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
  13640. spin_lock_irq(&phba->hbalock);
  13641. phba->hba_flag |= FCF_TS_INPROG;
  13642. spin_unlock_irq(&phba->hbalock);
  13643. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13644. if (rc == MBX_NOT_FINISHED)
  13645. error = -EIO;
  13646. else {
  13647. /* Reset eligible FCF count for new scan */
  13648. if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
  13649. phba->fcf.eligible_fcf_cnt = 0;
  13650. error = 0;
  13651. }
  13652. fail_fcf_scan:
  13653. if (error) {
  13654. if (mboxq)
  13655. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13656. /* FCF scan failed, clear FCF_TS_INPROG flag */
  13657. spin_lock_irq(&phba->hbalock);
  13658. phba->hba_flag &= ~FCF_TS_INPROG;
  13659. spin_unlock_irq(&phba->hbalock);
  13660. }
  13661. return error;
  13662. }
  13663. /**
  13664. * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
  13665. * @phba: pointer to lpfc hba data structure.
  13666. * @fcf_index: FCF table entry offset.
  13667. *
  13668. * This routine is invoked to read an FCF record indicated by @fcf_index
  13669. * and to use it for FLOGI roundrobin FCF failover.
  13670. *
  13671. * Return 0 if the mailbox command is submitted successfully, none 0
  13672. * otherwise.
  13673. **/
  13674. int
  13675. lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13676. {
  13677. int rc = 0, error;
  13678. LPFC_MBOXQ_t *mboxq;
  13679. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13680. if (!mboxq) {
  13681. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13682. "2763 Failed to allocate mbox for "
  13683. "READ_FCF cmd\n");
  13684. error = -ENOMEM;
  13685. goto fail_fcf_read;
  13686. }
  13687. /* Construct the read FCF record mailbox command */
  13688. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13689. if (rc) {
  13690. error = -EINVAL;
  13691. goto fail_fcf_read;
  13692. }
  13693. /* Issue the mailbox command asynchronously */
  13694. mboxq->vport = phba->pport;
  13695. mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
  13696. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13697. if (rc == MBX_NOT_FINISHED)
  13698. error = -EIO;
  13699. else
  13700. error = 0;
  13701. fail_fcf_read:
  13702. if (error && mboxq)
  13703. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13704. return error;
  13705. }
  13706. /**
  13707. * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
  13708. * @phba: pointer to lpfc hba data structure.
  13709. * @fcf_index: FCF table entry offset.
  13710. *
  13711. * This routine is invoked to read an FCF record indicated by @fcf_index to
  13712. * determine whether it's eligible for FLOGI roundrobin failover list.
  13713. *
  13714. * Return 0 if the mailbox command is submitted successfully, none 0
  13715. * otherwise.
  13716. **/
  13717. int
  13718. lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
  13719. {
  13720. int rc = 0, error;
  13721. LPFC_MBOXQ_t *mboxq;
  13722. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  13723. if (!mboxq) {
  13724. lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
  13725. "2758 Failed to allocate mbox for "
  13726. "READ_FCF cmd\n");
  13727. error = -ENOMEM;
  13728. goto fail_fcf_read;
  13729. }
  13730. /* Construct the read FCF record mailbox command */
  13731. rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
  13732. if (rc) {
  13733. error = -EINVAL;
  13734. goto fail_fcf_read;
  13735. }
  13736. /* Issue the mailbox command asynchronously */
  13737. mboxq->vport = phba->pport;
  13738. mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
  13739. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
  13740. if (rc == MBX_NOT_FINISHED)
  13741. error = -EIO;
  13742. else
  13743. error = 0;
  13744. fail_fcf_read:
  13745. if (error && mboxq)
  13746. lpfc_sli4_mbox_cmd_free(phba, mboxq);
  13747. return error;
  13748. }
  13749. /**
  13750. * lpfc_check_next_fcf_pri
  13751. * phba pointer to the lpfc_hba struct for this port.
  13752. * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
  13753. * routine when the rr_bmask is empty. The FCF indecies are put into the
  13754. * rr_bmask based on their priority level. Starting from the highest priority
  13755. * to the lowest. The most likely FCF candidate will be in the highest
  13756. * priority group. When this routine is called it searches the fcf_pri list for
  13757. * next lowest priority group and repopulates the rr_bmask with only those
  13758. * fcf_indexes.
  13759. * returns:
  13760. * 1=success 0=failure
  13761. **/
  13762. int
  13763. lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
  13764. {
  13765. uint16_t next_fcf_pri;
  13766. uint16_t last_index;
  13767. struct lpfc_fcf_pri *fcf_pri;
  13768. int rc;
  13769. int ret = 0;
  13770. last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
  13771. LPFC_SLI4_FCF_TBL_INDX_MAX);
  13772. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13773. "3060 Last IDX %d\n", last_index);
  13774. if (list_empty(&phba->fcf.fcf_pri_list)) {
  13775. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13776. "3061 Last IDX %d\n", last_index);
  13777. return 0; /* Empty rr list */
  13778. }
  13779. next_fcf_pri = 0;
  13780. /*
  13781. * Clear the rr_bmask and set all of the bits that are at this
  13782. * priority.
  13783. */
  13784. memset(phba->fcf.fcf_rr_bmask, 0,
  13785. sizeof(*phba->fcf.fcf_rr_bmask));
  13786. spin_lock_irq(&phba->hbalock);
  13787. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13788. if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
  13789. continue;
  13790. /*
  13791. * the 1st priority that has not FLOGI failed
  13792. * will be the highest.
  13793. */
  13794. if (!next_fcf_pri)
  13795. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13796. spin_unlock_irq(&phba->hbalock);
  13797. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13798. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13799. fcf_pri->fcf_rec.fcf_index);
  13800. if (rc)
  13801. return 0;
  13802. }
  13803. spin_lock_irq(&phba->hbalock);
  13804. }
  13805. /*
  13806. * if next_fcf_pri was not set above and the list is not empty then
  13807. * we have failed flogis on all of them. So reset flogi failed
  13808. * and start at the begining.
  13809. */
  13810. if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
  13811. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13812. fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
  13813. /*
  13814. * the 1st priority that has not FLOGI failed
  13815. * will be the highest.
  13816. */
  13817. if (!next_fcf_pri)
  13818. next_fcf_pri = fcf_pri->fcf_rec.priority;
  13819. spin_unlock_irq(&phba->hbalock);
  13820. if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
  13821. rc = lpfc_sli4_fcf_rr_index_set(phba,
  13822. fcf_pri->fcf_rec.fcf_index);
  13823. if (rc)
  13824. return 0;
  13825. }
  13826. spin_lock_irq(&phba->hbalock);
  13827. }
  13828. } else
  13829. ret = 1;
  13830. spin_unlock_irq(&phba->hbalock);
  13831. return ret;
  13832. }
  13833. /**
  13834. * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
  13835. * @phba: pointer to lpfc hba data structure.
  13836. *
  13837. * This routine is to get the next eligible FCF record index in a round
  13838. * robin fashion. If the next eligible FCF record index equals to the
  13839. * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
  13840. * shall be returned, otherwise, the next eligible FCF record's index
  13841. * shall be returned.
  13842. **/
  13843. uint16_t
  13844. lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
  13845. {
  13846. uint16_t next_fcf_index;
  13847. /* Search start from next bit of currently registered FCF index */
  13848. next_priority:
  13849. next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
  13850. LPFC_SLI4_FCF_TBL_INDX_MAX;
  13851. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13852. LPFC_SLI4_FCF_TBL_INDX_MAX,
  13853. next_fcf_index);
  13854. /* Wrap around condition on phba->fcf.fcf_rr_bmask */
  13855. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13856. /*
  13857. * If we have wrapped then we need to clear the bits that
  13858. * have been tested so that we can detect when we should
  13859. * change the priority level.
  13860. */
  13861. next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
  13862. LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
  13863. }
  13864. /* Check roundrobin failover list empty condition */
  13865. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
  13866. next_fcf_index == phba->fcf.current_rec.fcf_indx) {
  13867. /*
  13868. * If next fcf index is not found check if there are lower
  13869. * Priority level fcf's in the fcf_priority list.
  13870. * Set up the rr_bmask with all of the avaiable fcf bits
  13871. * at that level and continue the selection process.
  13872. */
  13873. if (lpfc_check_next_fcf_pri_level(phba))
  13874. goto next_priority;
  13875. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13876. "2844 No roundrobin failover FCF available\n");
  13877. if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
  13878. return LPFC_FCOE_FCF_NEXT_NONE;
  13879. else {
  13880. lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
  13881. "3063 Only FCF available idx %d, flag %x\n",
  13882. next_fcf_index,
  13883. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
  13884. return next_fcf_index;
  13885. }
  13886. }
  13887. if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
  13888. phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
  13889. LPFC_FCF_FLOGI_FAILED)
  13890. goto next_priority;
  13891. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13892. "2845 Get next roundrobin failover FCF (x%x)\n",
  13893. next_fcf_index);
  13894. return next_fcf_index;
  13895. }
  13896. /**
  13897. * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
  13898. * @phba: pointer to lpfc hba data structure.
  13899. *
  13900. * This routine sets the FCF record index in to the eligible bmask for
  13901. * roundrobin failover search. It checks to make sure that the index
  13902. * does not go beyond the range of the driver allocated bmask dimension
  13903. * before setting the bit.
  13904. *
  13905. * Returns 0 if the index bit successfully set, otherwise, it returns
  13906. * -EINVAL.
  13907. **/
  13908. int
  13909. lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
  13910. {
  13911. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13912. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13913. "2610 FCF (x%x) reached driver's book "
  13914. "keeping dimension:x%x\n",
  13915. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13916. return -EINVAL;
  13917. }
  13918. /* Set the eligible FCF record index bmask */
  13919. set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13920. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13921. "2790 Set FCF (x%x) to roundrobin FCF failover "
  13922. "bmask\n", fcf_index);
  13923. return 0;
  13924. }
  13925. /**
  13926. * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
  13927. * @phba: pointer to lpfc hba data structure.
  13928. *
  13929. * This routine clears the FCF record index from the eligible bmask for
  13930. * roundrobin failover search. It checks to make sure that the index
  13931. * does not go beyond the range of the driver allocated bmask dimension
  13932. * before clearing the bit.
  13933. **/
  13934. void
  13935. lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
  13936. {
  13937. struct lpfc_fcf_pri *fcf_pri;
  13938. if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
  13939. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13940. "2762 FCF (x%x) reached driver's book "
  13941. "keeping dimension:x%x\n",
  13942. fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
  13943. return;
  13944. }
  13945. /* Clear the eligible FCF record index bmask */
  13946. spin_lock_irq(&phba->hbalock);
  13947. list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
  13948. if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
  13949. list_del_init(&fcf_pri->list);
  13950. break;
  13951. }
  13952. }
  13953. spin_unlock_irq(&phba->hbalock);
  13954. clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
  13955. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  13956. "2791 Clear FCF (x%x) from roundrobin failover "
  13957. "bmask\n", fcf_index);
  13958. }
  13959. /**
  13960. * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
  13961. * @phba: pointer to lpfc hba data structure.
  13962. *
  13963. * This routine is the completion routine for the rediscover FCF table mailbox
  13964. * command. If the mailbox command returned failure, it will try to stop the
  13965. * FCF rediscover wait timer.
  13966. **/
  13967. void
  13968. lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
  13969. {
  13970. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  13971. uint32_t shdr_status, shdr_add_status;
  13972. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  13973. shdr_status = bf_get(lpfc_mbox_hdr_status,
  13974. &redisc_fcf->header.cfg_shdr.response);
  13975. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
  13976. &redisc_fcf->header.cfg_shdr.response);
  13977. if (shdr_status || shdr_add_status) {
  13978. lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
  13979. "2746 Requesting for FCF rediscovery failed "
  13980. "status x%x add_status x%x\n",
  13981. shdr_status, shdr_add_status);
  13982. if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
  13983. spin_lock_irq(&phba->hbalock);
  13984. phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
  13985. spin_unlock_irq(&phba->hbalock);
  13986. /*
  13987. * CVL event triggered FCF rediscover request failed,
  13988. * last resort to re-try current registered FCF entry.
  13989. */
  13990. lpfc_retry_pport_discovery(phba);
  13991. } else {
  13992. spin_lock_irq(&phba->hbalock);
  13993. phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
  13994. spin_unlock_irq(&phba->hbalock);
  13995. /*
  13996. * DEAD FCF event triggered FCF rediscover request
  13997. * failed, last resort to fail over as a link down
  13998. * to FCF registration.
  13999. */
  14000. lpfc_sli4_fcf_dead_failthrough(phba);
  14001. }
  14002. } else {
  14003. lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
  14004. "2775 Start FCF rediscover quiescent timer\n");
  14005. /*
  14006. * Start FCF rediscovery wait timer for pending FCF
  14007. * before rescan FCF record table.
  14008. */
  14009. lpfc_fcf_redisc_wait_start_timer(phba);
  14010. }
  14011. mempool_free(mbox, phba->mbox_mem_pool);
  14012. }
  14013. /**
  14014. * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
  14015. * @phba: pointer to lpfc hba data structure.
  14016. *
  14017. * This routine is invoked to request for rediscovery of the entire FCF table
  14018. * by the port.
  14019. **/
  14020. int
  14021. lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
  14022. {
  14023. LPFC_MBOXQ_t *mbox;
  14024. struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
  14025. int rc, length;
  14026. /* Cancel retry delay timers to all vports before FCF rediscover */
  14027. lpfc_cancel_all_vport_retry_delay_timer(phba);
  14028. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14029. if (!mbox) {
  14030. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14031. "2745 Failed to allocate mbox for "
  14032. "requesting FCF rediscover.\n");
  14033. return -ENOMEM;
  14034. }
  14035. length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
  14036. sizeof(struct lpfc_sli4_cfg_mhdr));
  14037. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  14038. LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
  14039. length, LPFC_SLI4_MBX_EMBED);
  14040. redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
  14041. /* Set count to 0 for invalidating the entire FCF database */
  14042. bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
  14043. /* Issue the mailbox command asynchronously */
  14044. mbox->vport = phba->pport;
  14045. mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
  14046. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
  14047. if (rc == MBX_NOT_FINISHED) {
  14048. mempool_free(mbox, phba->mbox_mem_pool);
  14049. return -EIO;
  14050. }
  14051. return 0;
  14052. }
  14053. /**
  14054. * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
  14055. * @phba: pointer to lpfc hba data structure.
  14056. *
  14057. * This function is the failover routine as a last resort to the FCF DEAD
  14058. * event when driver failed to perform fast FCF failover.
  14059. **/
  14060. void
  14061. lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
  14062. {
  14063. uint32_t link_state;
  14064. /*
  14065. * Last resort as FCF DEAD event failover will treat this as
  14066. * a link down, but save the link state because we don't want
  14067. * it to be changed to Link Down unless it is already down.
  14068. */
  14069. link_state = phba->link_state;
  14070. lpfc_linkdown(phba);
  14071. phba->link_state = link_state;
  14072. /* Unregister FCF if no devices connected to it */
  14073. lpfc_unregister_unused_fcf(phba);
  14074. }
  14075. /**
  14076. * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
  14077. * @phba: pointer to lpfc hba data structure.
  14078. * @rgn23_data: pointer to configure region 23 data.
  14079. *
  14080. * This function gets SLI3 port configure region 23 data through memory dump
  14081. * mailbox command. When it successfully retrieves data, the size of the data
  14082. * will be returned, otherwise, 0 will be returned.
  14083. **/
  14084. static uint32_t
  14085. lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14086. {
  14087. LPFC_MBOXQ_t *pmb = NULL;
  14088. MAILBOX_t *mb;
  14089. uint32_t offset = 0;
  14090. int rc;
  14091. if (!rgn23_data)
  14092. return 0;
  14093. pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14094. if (!pmb) {
  14095. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14096. "2600 failed to allocate mailbox memory\n");
  14097. return 0;
  14098. }
  14099. mb = &pmb->u.mb;
  14100. do {
  14101. lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
  14102. rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
  14103. if (rc != MBX_SUCCESS) {
  14104. lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
  14105. "2601 failed to read config "
  14106. "region 23, rc 0x%x Status 0x%x\n",
  14107. rc, mb->mbxStatus);
  14108. mb->un.varDmp.word_cnt = 0;
  14109. }
  14110. /*
  14111. * dump mem may return a zero when finished or we got a
  14112. * mailbox error, either way we are done.
  14113. */
  14114. if (mb->un.varDmp.word_cnt == 0)
  14115. break;
  14116. if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
  14117. mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
  14118. lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
  14119. rgn23_data + offset,
  14120. mb->un.varDmp.word_cnt);
  14121. offset += mb->un.varDmp.word_cnt;
  14122. } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
  14123. mempool_free(pmb, phba->mbox_mem_pool);
  14124. return offset;
  14125. }
  14126. /**
  14127. * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
  14128. * @phba: pointer to lpfc hba data structure.
  14129. * @rgn23_data: pointer to configure region 23 data.
  14130. *
  14131. * This function gets SLI4 port configure region 23 data through memory dump
  14132. * mailbox command. When it successfully retrieves data, the size of the data
  14133. * will be returned, otherwise, 0 will be returned.
  14134. **/
  14135. static uint32_t
  14136. lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
  14137. {
  14138. LPFC_MBOXQ_t *mboxq = NULL;
  14139. struct lpfc_dmabuf *mp = NULL;
  14140. struct lpfc_mqe *mqe;
  14141. uint32_t data_length = 0;
  14142. int rc;
  14143. if (!rgn23_data)
  14144. return 0;
  14145. mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14146. if (!mboxq) {
  14147. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14148. "3105 failed to allocate mailbox memory\n");
  14149. return 0;
  14150. }
  14151. if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
  14152. goto out;
  14153. mqe = &mboxq->u.mqe;
  14154. mp = (struct lpfc_dmabuf *) mboxq->context1;
  14155. rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
  14156. if (rc)
  14157. goto out;
  14158. data_length = mqe->un.mb_words[5];
  14159. if (data_length == 0)
  14160. goto out;
  14161. if (data_length > DMP_RGN23_SIZE) {
  14162. data_length = 0;
  14163. goto out;
  14164. }
  14165. lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
  14166. out:
  14167. mempool_free(mboxq, phba->mbox_mem_pool);
  14168. if (mp) {
  14169. lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14170. kfree(mp);
  14171. }
  14172. return data_length;
  14173. }
  14174. /**
  14175. * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
  14176. * @phba: pointer to lpfc hba data structure.
  14177. *
  14178. * This function read region 23 and parse TLV for port status to
  14179. * decide if the user disaled the port. If the TLV indicates the
  14180. * port is disabled, the hba_flag is set accordingly.
  14181. **/
  14182. void
  14183. lpfc_sli_read_link_ste(struct lpfc_hba *phba)
  14184. {
  14185. uint8_t *rgn23_data = NULL;
  14186. uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
  14187. uint32_t offset = 0;
  14188. /* Get adapter Region 23 data */
  14189. rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
  14190. if (!rgn23_data)
  14191. goto out;
  14192. if (phba->sli_rev < LPFC_SLI_REV4)
  14193. data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
  14194. else {
  14195. if_type = bf_get(lpfc_sli_intf_if_type,
  14196. &phba->sli4_hba.sli_intf);
  14197. if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
  14198. goto out;
  14199. data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
  14200. }
  14201. if (!data_size)
  14202. goto out;
  14203. /* Check the region signature first */
  14204. if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
  14205. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14206. "2619 Config region 23 has bad signature\n");
  14207. goto out;
  14208. }
  14209. offset += 4;
  14210. /* Check the data structure version */
  14211. if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
  14212. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14213. "2620 Config region 23 has bad version\n");
  14214. goto out;
  14215. }
  14216. offset += 4;
  14217. /* Parse TLV entries in the region */
  14218. while (offset < data_size) {
  14219. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
  14220. break;
  14221. /*
  14222. * If the TLV is not driver specific TLV or driver id is
  14223. * not linux driver id, skip the record.
  14224. */
  14225. if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
  14226. (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
  14227. (rgn23_data[offset + 3] != 0)) {
  14228. offset += rgn23_data[offset + 1] * 4 + 4;
  14229. continue;
  14230. }
  14231. /* Driver found a driver specific TLV in the config region */
  14232. sub_tlv_len = rgn23_data[offset + 1] * 4;
  14233. offset += 4;
  14234. tlv_offset = 0;
  14235. /*
  14236. * Search for configured port state sub-TLV.
  14237. */
  14238. while ((offset < data_size) &&
  14239. (tlv_offset < sub_tlv_len)) {
  14240. if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
  14241. offset += 4;
  14242. tlv_offset += 4;
  14243. break;
  14244. }
  14245. if (rgn23_data[offset] != PORT_STE_TYPE) {
  14246. offset += rgn23_data[offset + 1] * 4 + 4;
  14247. tlv_offset += rgn23_data[offset + 1] * 4 + 4;
  14248. continue;
  14249. }
  14250. /* This HBA contains PORT_STE configured */
  14251. if (!rgn23_data[offset + 2])
  14252. phba->hba_flag |= LINK_DISABLED;
  14253. goto out;
  14254. }
  14255. }
  14256. out:
  14257. kfree(rgn23_data);
  14258. return;
  14259. }
  14260. /**
  14261. * lpfc_wr_object - write an object to the firmware
  14262. * @phba: HBA structure that indicates port to create a queue on.
  14263. * @dmabuf_list: list of dmabufs to write to the port.
  14264. * @size: the total byte value of the objects to write to the port.
  14265. * @offset: the current offset to be used to start the transfer.
  14266. *
  14267. * This routine will create a wr_object mailbox command to send to the port.
  14268. * the mailbox command will be constructed using the dma buffers described in
  14269. * @dmabuf_list to create a list of BDEs. This routine will fill in as many
  14270. * BDEs that the imbedded mailbox can support. The @offset variable will be
  14271. * used to indicate the starting offset of the transfer and will also return
  14272. * the offset after the write object mailbox has completed. @size is used to
  14273. * determine the end of the object and whether the eof bit should be set.
  14274. *
  14275. * Return 0 is successful and offset will contain the the new offset to use
  14276. * for the next write.
  14277. * Return negative value for error cases.
  14278. **/
  14279. int
  14280. lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
  14281. uint32_t size, uint32_t *offset)
  14282. {
  14283. struct lpfc_mbx_wr_object *wr_object;
  14284. LPFC_MBOXQ_t *mbox;
  14285. int rc = 0, i = 0;
  14286. uint32_t shdr_status, shdr_add_status;
  14287. uint32_t mbox_tmo;
  14288. union lpfc_sli4_cfg_shdr *shdr;
  14289. struct lpfc_dmabuf *dmabuf;
  14290. uint32_t written = 0;
  14291. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  14292. if (!mbox)
  14293. return -ENOMEM;
  14294. lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
  14295. LPFC_MBOX_OPCODE_WRITE_OBJECT,
  14296. sizeof(struct lpfc_mbx_wr_object) -
  14297. sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
  14298. wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
  14299. wr_object->u.request.write_offset = *offset;
  14300. sprintf((uint8_t *)wr_object->u.request.object_name, "/");
  14301. wr_object->u.request.object_name[0] =
  14302. cpu_to_le32(wr_object->u.request.object_name[0]);
  14303. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
  14304. list_for_each_entry(dmabuf, dmabuf_list, list) {
  14305. if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
  14306. break;
  14307. wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
  14308. wr_object->u.request.bde[i].addrHigh =
  14309. putPaddrHigh(dmabuf->phys);
  14310. if (written + SLI4_PAGE_SIZE >= size) {
  14311. wr_object->u.request.bde[i].tus.f.bdeSize =
  14312. (size - written);
  14313. written += (size - written);
  14314. bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
  14315. } else {
  14316. wr_object->u.request.bde[i].tus.f.bdeSize =
  14317. SLI4_PAGE_SIZE;
  14318. written += SLI4_PAGE_SIZE;
  14319. }
  14320. i++;
  14321. }
  14322. wr_object->u.request.bde_count = i;
  14323. bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
  14324. if (!phba->sli4_hba.intr_enable)
  14325. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  14326. else {
  14327. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  14328. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  14329. }
  14330. /* The IOCTL status is embedded in the mailbox subheader. */
  14331. shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
  14332. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  14333. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  14334. if (rc != MBX_TIMEOUT)
  14335. mempool_free(mbox, phba->mbox_mem_pool);
  14336. if (shdr_status || shdr_add_status || rc) {
  14337. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  14338. "3025 Write Object mailbox failed with "
  14339. "status x%x add_status x%x, mbx status x%x\n",
  14340. shdr_status, shdr_add_status, rc);
  14341. rc = -ENXIO;
  14342. } else
  14343. *offset += wr_object->u.response.actual_write_length;
  14344. return rc;
  14345. }
  14346. /**
  14347. * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
  14348. * @vport: pointer to vport data structure.
  14349. *
  14350. * This function iterate through the mailboxq and clean up all REG_LOGIN
  14351. * and REG_VPI mailbox commands associated with the vport. This function
  14352. * is called when driver want to restart discovery of the vport due to
  14353. * a Clear Virtual Link event.
  14354. **/
  14355. void
  14356. lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
  14357. {
  14358. struct lpfc_hba *phba = vport->phba;
  14359. LPFC_MBOXQ_t *mb, *nextmb;
  14360. struct lpfc_dmabuf *mp;
  14361. struct lpfc_nodelist *ndlp;
  14362. struct lpfc_nodelist *act_mbx_ndlp = NULL;
  14363. struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
  14364. LIST_HEAD(mbox_cmd_list);
  14365. uint8_t restart_loop;
  14366. /* Clean up internally queued mailbox commands with the vport */
  14367. spin_lock_irq(&phba->hbalock);
  14368. list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
  14369. if (mb->vport != vport)
  14370. continue;
  14371. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14372. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14373. continue;
  14374. list_del(&mb->list);
  14375. list_add_tail(&mb->list, &mbox_cmd_list);
  14376. }
  14377. /* Clean up active mailbox command with the vport */
  14378. mb = phba->sli.mbox_active;
  14379. if (mb && (mb->vport == vport)) {
  14380. if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
  14381. (mb->u.mb.mbxCommand == MBX_REG_VPI))
  14382. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14383. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14384. act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
  14385. /* Put reference count for delayed processing */
  14386. act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
  14387. /* Unregister the RPI when mailbox complete */
  14388. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14389. }
  14390. }
  14391. /* Cleanup any mailbox completions which are not yet processed */
  14392. do {
  14393. restart_loop = 0;
  14394. list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
  14395. /*
  14396. * If this mailox is already processed or it is
  14397. * for another vport ignore it.
  14398. */
  14399. if ((mb->vport != vport) ||
  14400. (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
  14401. continue;
  14402. if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
  14403. (mb->u.mb.mbxCommand != MBX_REG_VPI))
  14404. continue;
  14405. mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
  14406. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14407. ndlp = (struct lpfc_nodelist *)mb->context2;
  14408. /* Unregister the RPI when mailbox complete */
  14409. mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
  14410. restart_loop = 1;
  14411. spin_unlock_irq(&phba->hbalock);
  14412. spin_lock(shost->host_lock);
  14413. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14414. spin_unlock(shost->host_lock);
  14415. spin_lock_irq(&phba->hbalock);
  14416. break;
  14417. }
  14418. }
  14419. } while (restart_loop);
  14420. spin_unlock_irq(&phba->hbalock);
  14421. /* Release the cleaned-up mailbox commands */
  14422. while (!list_empty(&mbox_cmd_list)) {
  14423. list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
  14424. if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
  14425. mp = (struct lpfc_dmabuf *) (mb->context1);
  14426. if (mp) {
  14427. __lpfc_mbuf_free(phba, mp->virt, mp->phys);
  14428. kfree(mp);
  14429. }
  14430. ndlp = (struct lpfc_nodelist *) mb->context2;
  14431. mb->context2 = NULL;
  14432. if (ndlp) {
  14433. spin_lock(shost->host_lock);
  14434. ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14435. spin_unlock(shost->host_lock);
  14436. lpfc_nlp_put(ndlp);
  14437. }
  14438. }
  14439. mempool_free(mb, phba->mbox_mem_pool);
  14440. }
  14441. /* Release the ndlp with the cleaned-up active mailbox command */
  14442. if (act_mbx_ndlp) {
  14443. spin_lock(shost->host_lock);
  14444. act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
  14445. spin_unlock(shost->host_lock);
  14446. lpfc_nlp_put(act_mbx_ndlp);
  14447. }
  14448. }
  14449. /**
  14450. * lpfc_drain_txq - Drain the txq
  14451. * @phba: Pointer to HBA context object.
  14452. *
  14453. * This function attempt to submit IOCBs on the txq
  14454. * to the adapter. For SLI4 adapters, the txq contains
  14455. * ELS IOCBs that have been deferred because the there
  14456. * are no SGLs. This congestion can occur with large
  14457. * vport counts during node discovery.
  14458. **/
  14459. uint32_t
  14460. lpfc_drain_txq(struct lpfc_hba *phba)
  14461. {
  14462. LIST_HEAD(completions);
  14463. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  14464. struct lpfc_iocbq *piocbq = 0;
  14465. unsigned long iflags = 0;
  14466. char *fail_msg = NULL;
  14467. struct lpfc_sglq *sglq;
  14468. union lpfc_wqe wqe;
  14469. spin_lock_irqsave(&phba->hbalock, iflags);
  14470. if (pring->txq_cnt > pring->txq_max)
  14471. pring->txq_max = pring->txq_cnt;
  14472. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14473. while (pring->txq_cnt) {
  14474. spin_lock_irqsave(&phba->hbalock, iflags);
  14475. piocbq = lpfc_sli_ringtx_get(phba, pring);
  14476. sglq = __lpfc_sli_get_sglq(phba, piocbq);
  14477. if (!sglq) {
  14478. __lpfc_sli_ringtx_put(phba, pring, piocbq);
  14479. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14480. break;
  14481. } else {
  14482. if (!piocbq) {
  14483. /* The txq_cnt out of sync. This should
  14484. * never happen
  14485. */
  14486. sglq = __lpfc_clear_active_sglq(phba,
  14487. sglq->sli4_lxritag);
  14488. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14489. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14490. "2823 txq empty and txq_cnt is %d\n ",
  14491. pring->txq_cnt);
  14492. break;
  14493. }
  14494. }
  14495. /* The xri and iocb resources secured,
  14496. * attempt to issue request
  14497. */
  14498. piocbq->sli4_lxritag = sglq->sli4_lxritag;
  14499. piocbq->sli4_xritag = sglq->sli4_xritag;
  14500. if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
  14501. fail_msg = "to convert bpl to sgl";
  14502. else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
  14503. fail_msg = "to convert iocb to wqe";
  14504. else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
  14505. fail_msg = " - Wq is full";
  14506. else
  14507. lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
  14508. if (fail_msg) {
  14509. /* Failed means we can't issue and need to cancel */
  14510. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  14511. "2822 IOCB failed %s iotag 0x%x "
  14512. "xri 0x%x\n",
  14513. fail_msg,
  14514. piocbq->iotag, piocbq->sli4_xritag);
  14515. list_add_tail(&piocbq->list, &completions);
  14516. }
  14517. spin_unlock_irqrestore(&phba->hbalock, iflags);
  14518. }
  14519. /* Cancel all the IOCBs that cannot be issued */
  14520. lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
  14521. IOERR_SLI_ABORTED);
  14522. return pring->txq_cnt;
  14523. }